Skip to content

Commit cb4ed3c

Browse files
committed
crypto: never store pointer to conn in SSL_CTX
SSL_CTX is shared between multiple connections and is not a right place to store per-connection data. fix nodejs#8348 Reviewed-By: Trevor Norris
1 parent c615545 commit cb4ed3c

3 files changed

Lines changed: 18 additions & 24 deletions

File tree

src/node_crypto.cc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ X509_STORE* root_cert_store;
120120
template class SSLWrap<TLSCallbacks>;
121121
template void SSLWrap<TLSCallbacks>::AddMethods(Environment* env,
122122
Handle<FunctionTemplate> t);
123-
template void SSLWrap<TLSCallbacks>::InitNPN(SecureContext* sc,
124-
TLSCallbacks* base);
123+
template void SSLWrap<TLSCallbacks>::InitNPN(SecureContext* sc);
125124
template SSL_SESSION* SSLWrap<TLSCallbacks>::GetSessionCallback(
126125
SSL* s,
127126
unsigned char* key,
@@ -1013,26 +1012,21 @@ void SSLWrap<Base>::AddMethods(Environment* env, Handle<FunctionTemplate> t) {
10131012

10141013

10151014
template <class Base>
1016-
void SSLWrap<Base>::InitNPN(SecureContext* sc, Base* base) {
1017-
if (base->is_server()) {
1015+
void SSLWrap<Base>::InitNPN(SecureContext* sc) {
10181016
#ifdef OPENSSL_NPN_NEGOTIATED
1019-
// Server should advertise NPN protocols
1020-
SSL_CTX_set_next_protos_advertised_cb(sc->ctx_,
1021-
AdvertiseNextProtoCallback,
1022-
base);
1017+
// Server should advertise NPN protocols
1018+
SSL_CTX_set_next_protos_advertised_cb(sc->ctx_,
1019+
AdvertiseNextProtoCallback,
1020+
NULL);
1021+
// Client should select protocol from list of advertised
1022+
// If server supports NPN
1023+
SSL_CTX_set_next_proto_select_cb(sc->ctx_, SelectNextProtoCallback, NULL);
10231024
#endif // OPENSSL_NPN_NEGOTIATED
1024-
} else {
1025-
#ifdef OPENSSL_NPN_NEGOTIATED
1026-
// Client should select protocol from list of advertised
1027-
// If server supports NPN
1028-
SSL_CTX_set_next_proto_select_cb(sc->ctx_, SelectNextProtoCallback, base);
1029-
#endif // OPENSSL_NPN_NEGOTIATED
1030-
}
10311025

10321026
#ifdef NODE__HAVE_TLSEXT_STATUS_CB
10331027
// OCSP stapling
10341028
SSL_CTX_set_tlsext_status_cb(sc->ctx_, TLSExtStatusCallback);
1035-
SSL_CTX_set_tlsext_status_arg(sc->ctx_, base);
1029+
SSL_CTX_set_tlsext_status_arg(sc->ctx_, NULL);
10361030
#endif // NODE__HAVE_TLSEXT_STATUS_CB
10371031
}
10381032

@@ -1688,7 +1682,7 @@ int SSLWrap<Base>::AdvertiseNextProtoCallback(SSL* s,
16881682
const unsigned char** data,
16891683
unsigned int* len,
16901684
void* arg) {
1691-
Base* w = static_cast<Base*>(arg);
1685+
Base* w = static_cast<Base*>(SSL_get_app_data(s));
16921686
Environment* env = w->env();
16931687
HandleScope handle_scope(env->isolate());
16941688
Context::Scope context_scope(env->context());
@@ -1714,7 +1708,7 @@ int SSLWrap<Base>::SelectNextProtoCallback(SSL* s,
17141708
const unsigned char* in,
17151709
unsigned int inlen,
17161710
void* arg) {
1717-
Base* w = static_cast<Base*>(arg);
1711+
Base* w = static_cast<Base*>(SSL_get_app_data(s));
17181712
Environment* env = w->env();
17191713
HandleScope handle_scope(env->isolate());
17201714
Context::Scope context_scope(env->context());
@@ -1806,7 +1800,7 @@ void SSLWrap<Base>::SetNPNProtocols(const FunctionCallbackInfo<Value>& args) {
18061800
#ifdef NODE__HAVE_TLSEXT_STATUS_CB
18071801
template <class Base>
18081802
int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
1809-
Base* w = static_cast<Base*>(arg);
1803+
Base* w = static_cast<Base*>(SSL_get_app_data(s));
18101804
Environment* env = w->env();
18111805
HandleScope handle_scope(env->isolate());
18121806

@@ -2122,7 +2116,7 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) {
21222116
if (secure_context_constructor_template->HasInstance(ret)) {
21232117
conn->sniContext_.Reset(env->isolate(), ret);
21242118
SecureContext* sc = Unwrap<SecureContext>(ret.As<Object>());
2125-
InitNPN(sc, conn);
2119+
InitNPN(sc);
21262120
SSL_set_SSL_CTX(s, sc->ctx_);
21272121
} else {
21282122
return SSL_TLSEXT_ERR_NOACK;
@@ -2158,7 +2152,7 @@ void Connection::New(const FunctionCallbackInfo<Value>& args) {
21582152
if (is_server)
21592153
SSL_set_info_callback(conn->ssl_, SSLInfoCallback);
21602154

2161-
InitNPN(sc, conn);
2155+
InitNPN(sc);
21622156

21632157
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
21642158
if (is_server) {

src/node_crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class SSLWrap {
188188
inline bool is_waiting_new_session() const { return new_session_wait_; }
189189

190190
protected:
191-
static void InitNPN(SecureContext* sc, Base* base);
191+
static void InitNPN(SecureContext* sc);
192192
static void AddMethods(Environment* env, v8::Handle<v8::FunctionTemplate> t);
193193

194194
static SSL_SESSION* GetSessionCallback(SSL* s,

src/tls_wrap.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void TLSCallbacks::InitSSL() {
186186
}
187187
#endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
188188

189-
InitNPN(sc_, this);
189+
InitNPN(sc_);
190190

191191
if (is_server()) {
192192
SSL_set_accept_state(ssl_);
@@ -800,7 +800,7 @@ int TLSCallbacks::SelectSNIContextCallback(SSL* s, int* ad, void* arg) {
800800
p->sni_context_.Reset(env->isolate(), ctx);
801801

802802
SecureContext* sc = Unwrap<SecureContext>(ctx.As<Object>());
803-
InitNPN(sc, p);
803+
InitNPN(sc);
804804
SSL_set_SSL_CTX(s, sc->ctx_);
805805
return SSL_TLSEXT_ERR_OK;
806806
}

0 commit comments

Comments
 (0)