Skip to content

Commit d706c0d

Browse files
Shigeki OhtsuMylesBorins
Shigeki Ohtsu
authored andcommitted
tls,crypto: move NPN protcol data to hidden value
cherry-pick 7eee372 from v6-staging. This fix is to be consistent implementation with ALPN. Tow NPN protocol data in the persistent memebers move to hidden variables in the wrap object. PR-URL: #2564 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 1a40f2d commit d706c0d

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ namespace node {
134134
V(netmask_string, "netmask") \
135135
V(nice_string, "nice") \
136136
V(nlink_string, "nlink") \
137+
V(npn_buffer_string, "npnBuffer") \
137138
V(nsname_string, "nsname") \
138139
V(ocsp_request_string, "OCSPRequest") \
139140
V(offset_string, "offset") \
@@ -184,6 +185,7 @@ namespace node {
184185
V(serial_string, "serial") \
185186
V(scavenge_string, "scavenge") \
186187
V(scopeid_string, "scopeid") \
188+
V(selected_npn_buffer_string, "selectedNpnBuffer") \
187189
V(sent_shutdown_string, "sentShutdown") \
188190
V(serial_number_string, "serialNumber") \
189191
V(service_string, "service") \

src/node_crypto.cc

+29-18
Original file line numberDiff line numberDiff line change
@@ -1960,14 +1960,17 @@ int SSLWrap<Base>::AdvertiseNextProtoCallback(SSL* s,
19601960
HandleScope handle_scope(env->isolate());
19611961
Context::Scope context_scope(env->context());
19621962

1963-
if (w->npn_protos_.IsEmpty()) {
1963+
Local<Value> npn_buffer =
1964+
w->object()->GetHiddenValue(env->npn_buffer_string());
1965+
1966+
if (npn_buffer.IsEmpty()) {
19641967
// No initialization - no NPN protocols
19651968
*data = reinterpret_cast<const unsigned char*>("");
19661969
*len = 0;
19671970
} else {
1968-
Local<Object> obj = PersistentToLocal(env->isolate(), w->npn_protos_);
1969-
*data = reinterpret_cast<const unsigned char*>(Buffer::Data(obj));
1970-
*len = Buffer::Length(obj);
1971+
CHECK(Buffer::HasInstance(npn_buffer));
1972+
*data = reinterpret_cast<const unsigned char*>(Buffer::Data(npn_buffer));
1973+
*len = Buffer::Length(npn_buffer);
19711974
}
19721975

19731976
return SSL_TLSEXT_ERR_OK;
@@ -1986,25 +1989,27 @@ int SSLWrap<Base>::SelectNextProtoCallback(SSL* s,
19861989
HandleScope handle_scope(env->isolate());
19871990
Context::Scope context_scope(env->context());
19881991

1989-
// Release old protocol handler if present
1990-
w->selected_npn_proto_.Reset();
1992+
Local<Value> npn_buffer =
1993+
w->object()->GetHiddenValue(env->npn_buffer_string());
19911994

1992-
if (w->npn_protos_.IsEmpty()) {
1995+
if (npn_buffer.IsEmpty()) {
19931996
// We should at least select one protocol
19941997
// If server is using NPN
19951998
*out = reinterpret_cast<unsigned char*>(const_cast<char*>("http/1.1"));
19961999
*outlen = 8;
19972000

19982001
// set status: unsupported
1999-
w->selected_npn_proto_.Reset(env->isolate(), False(env->isolate()));
2002+
bool r = w->object()->SetHiddenValue(env->selected_npn_buffer_string(),
2003+
False(env->isolate()));
2004+
CHECK(r);
20002005

20012006
return SSL_TLSEXT_ERR_OK;
20022007
}
20032008

2004-
Local<Object> obj = PersistentToLocal(env->isolate(), w->npn_protos_);
2009+
CHECK(Buffer::HasInstance(npn_buffer));
20052010
const unsigned char* npn_protos =
2006-
reinterpret_cast<const unsigned char*>(Buffer::Data(obj));
2007-
size_t len = Buffer::Length(obj);
2011+
reinterpret_cast<const unsigned char*>(Buffer::Data(npn_buffer));
2012+
size_t len = Buffer::Length(npn_buffer);
20082013

20092014
int status = SSL_select_next_proto(out, outlen, in, inlen, npn_protos, len);
20102015
Local<Value> result;
@@ -2022,8 +2027,9 @@ int SSLWrap<Base>::SelectNextProtoCallback(SSL* s,
20222027
break;
20232028
}
20242029

2025-
if (!result.IsEmpty())
2026-
w->selected_npn_proto_.Reset(env->isolate(), result);
2030+
bool r = w->object()->SetHiddenValue(env->selected_npn_buffer_string(),
2031+
result);
2032+
CHECK(r);
20272033

20282034
return SSL_TLSEXT_ERR_OK;
20292035
}
@@ -2036,9 +2042,12 @@ void SSLWrap<Base>::GetNegotiatedProto(
20362042
ASSIGN_OR_RETURN_UNWRAP(&w, args.Holder());
20372043

20382044
if (w->is_client()) {
2039-
if (w->selected_npn_proto_.IsEmpty() == false) {
2040-
args.GetReturnValue().Set(w->selected_npn_proto_);
2041-
}
2045+
Local<Value> selected_npn_buffer =
2046+
w->object()->GetHiddenValue(w->env()->selected_npn_buffer_string());
2047+
2048+
if (selected_npn_buffer.IsEmpty() == false)
2049+
args.GetReturnValue().Set(selected_npn_buffer);
2050+
20422051
return;
20432052
}
20442053

@@ -2062,9 +2071,11 @@ void SSLWrap<Base>::SetNPNProtocols(const FunctionCallbackInfo<Value>& args) {
20622071
Environment* env = w->ssl_env();
20632072

20642073
if (args.Length() < 1 || !Buffer::HasInstance(args[0]))
2065-
return w->env()->ThrowTypeError("Must give a Buffer as first argument");
2074+
return env->ThrowTypeError("Must give a Buffer as first argument");
20662075

2067-
w->npn_protos_.Reset(args.GetIsolate(), args[0].As<Object>());
2076+
Local<Value> npn_buffer = Local<Value>::New(env->isolate(), args[0]);
2077+
bool r = w->object()->SetHiddenValue(env->npn_buffer_string(), npn_buffer);
2078+
CHECK(r);
20682079
}
20692080
#endif // OPENSSL_NPN_NEGOTIATED
20702081

src/node_crypto.h

-9
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ class SSLWrap {
193193
next_sess_ = nullptr;
194194
}
195195

196-
#ifdef OPENSSL_NPN_NEGOTIATED
197-
npn_protos_.Reset();
198-
selected_npn_proto_.Reset();
199-
#endif
200196
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
201197
sni_context_.Reset();
202198
#endif
@@ -313,11 +309,6 @@ class SSLWrap {
313309
v8::Persistent<v8::Object> ocsp_response_;
314310
#endif // NODE__HAVE_TLSEXT_STATUS_CB
315311

316-
#ifdef OPENSSL_NPN_NEGOTIATED
317-
v8::Persistent<v8::Object> npn_protos_;
318-
v8::Persistent<v8::Value> selected_npn_proto_;
319-
#endif // OPENSSL_NPN_NEGOTIATED
320-
321312
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
322313
v8::Persistent<v8::Value> sni_context_;
323314
#endif

0 commit comments

Comments
 (0)