-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tls: use SSL_set_cert_cb
for async SNI/OCSP
#1464
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,8 @@ template int SSLWrap<TLSWrap>::SelectNextProtoCallback( | |
void* arg); | ||
#endif | ||
template int SSLWrap<TLSWrap>::TLSExtStatusCallback(SSL* s, void* arg); | ||
template int SSLWrap<TLSWrap>::SSLCertCallback(SSL* s, void* arg); | ||
template void SSLWrap<TLSWrap>::WaitForCertCb(CertCb cb, void* arg); | ||
|
||
|
||
static void crypto_threadid_cb(CRYPTO_THREADID* tid) { | ||
|
@@ -509,7 +511,8 @@ int SSL_CTX_use_certificate_chain(SSL_CTX* ctx, | |
} | ||
|
||
while ((ca = PEM_read_bio_X509(in, nullptr, CryptoPemCallback, nullptr))) { | ||
r = SSL_CTX_add_extra_chain_cert(ctx, ca); | ||
// NOTE: Increments reference count on `ca` | ||
r = SSL_CTX_add1_chain_cert(ctx, ca); | ||
|
||
if (!r) { | ||
X509_free(ca); | ||
|
@@ -978,6 +981,7 @@ void SSLWrap<Base>::AddMethods(Environment* env, Handle<FunctionTemplate> t) { | |
env->SetProtoMethod(t, "verifyError", VerifyError); | ||
env->SetProtoMethod(t, "getCurrentCipher", GetCurrentCipher); | ||
env->SetProtoMethod(t, "endParser", EndParser); | ||
env->SetProtoMethod(t, "certCbDone", CertCbDone); | ||
env->SetProtoMethod(t, "renegotiate", Renegotiate); | ||
env->SetProtoMethod(t, "shutdownSSL", Shutdown); | ||
env->SetProtoMethod(t, "getTLSTicket", GetTLSTicket); | ||
|
@@ -1860,6 +1864,122 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) { | |
#endif // NODE__HAVE_TLSEXT_STATUS_CB | ||
|
||
|
||
template <class Base> | ||
void SSLWrap<Base>::WaitForCertCb(CertCb cb, void* arg) { | ||
cert_cb_ = cb; | ||
cert_cb_arg_ = arg; | ||
} | ||
|
||
|
||
template <class Base> | ||
int SSLWrap<Base>::SSLCertCallback(SSL* s, void* arg) { | ||
Base* w = static_cast<Base*>(SSL_get_app_data(s)); | ||
|
||
if (!w->is_server()) | ||
return 1; | ||
|
||
if (!w->is_waiting_cert_cb()) | ||
return 1; | ||
|
||
if (w->cert_cb_running_) | ||
return -1; | ||
|
||
Environment* env = w->env(); | ||
HandleScope handle_scope(env->isolate()); | ||
Context::Scope context_scope(env->context()); | ||
w->cert_cb_running_ = true; | ||
|
||
Local<Object> info = Object::New(env->isolate()); | ||
|
||
SSL_SESSION* sess = SSL_get_session(s); | ||
if (sess != nullptr) { | ||
if (sess->tlsext_hostname == nullptr) { | ||
info->Set(env->servername_string(), String::Empty(env->isolate())); | ||
} else { | ||
Local<String> servername = OneByteString(env->isolate(), | ||
sess->tlsext_hostname, | ||
strlen(sess->tlsext_hostname)); | ||
info->Set(env->servername_string(), servername); | ||
} | ||
info->Set(env->tls_ticket_string(), | ||
Boolean::New(env->isolate(), sess->tlsext_ticklen != 0)); | ||
} | ||
bool ocsp = s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp; | ||
info->Set(env->ocsp_request_string(), Boolean::New(env->isolate(), ocsp)); | ||
|
||
Local<Value> argv[] = { info }; | ||
w->MakeCallback(env->oncertcb_string(), ARRAY_SIZE(argv), argv); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we avoid invoking the JS callback if no status_type and no server name? Furthermore can we check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is not invoked unless Good point about not invoking it, but let's do it in a follow-up. I'm going to remove most of the ClientHelloParser and cleanup the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @indutny Okay. Let's do it later. |
||
|
||
if (!w->cert_cb_running_) | ||
return 1; | ||
|
||
// Performing async action, wait... | ||
return -1; | ||
} | ||
|
||
|
||
template <class Base> | ||
void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) { | ||
Base* w = Unwrap<Base>(args.Holder()); | ||
Environment* env = w->env(); | ||
|
||
CHECK(w->is_waiting_cert_cb() && w->cert_cb_running_); | ||
|
||
Local<Object> object = w->object(); | ||
Local<Value> ctx = object->Get(env->sni_context_string()); | ||
Local<FunctionTemplate> cons = env->secure_context_constructor_template(); | ||
|
||
// Not an object, probably undefined or null | ||
if (!ctx->IsObject()) | ||
goto fire_cb; | ||
|
||
if (cons->HasInstance(ctx)) { | ||
SecureContext* sc = Unwrap<SecureContext>(ctx.As<Object>()); | ||
w->sni_context_.Reset(); | ||
w->sni_context_.Reset(env->isolate(), ctx); | ||
|
||
int rv; | ||
|
||
// NOTE: reference count is not increased by this API methods | ||
X509* x509 = SSL_CTX_get0_certificate(sc->ctx_); | ||
EVP_PKEY* pkey = SSL_CTX_get0_privatekey(sc->ctx_); | ||
STACK_OF(X509)* chain; | ||
|
||
rv = SSL_CTX_get0_chain_certs(sc->ctx_, &chain); | ||
if (rv) | ||
rv = SSL_use_certificate(w->ssl_, x509); | ||
if (rv) | ||
rv = SSL_use_PrivateKey(w->ssl_, pkey); | ||
if (rv && chain != nullptr) | ||
rv = SSL_set1_chain(w->ssl_, chain); | ||
if (!rv) { | ||
unsigned long err = ERR_get_error(); | ||
if (!err) | ||
return env->ThrowError("CertCbDone"); | ||
return ThrowCryptoError(env, err); | ||
} | ||
} else { | ||
// Failure: incorrect SNI context object | ||
Local<Value> err = Exception::TypeError(env->sni_context_err_string()); | ||
w->MakeCallback(env->onerror_string(), 1, &err); | ||
return; | ||
} | ||
|
||
fire_cb: | ||
CertCb cb; | ||
void* arg; | ||
|
||
cb = w->cert_cb_; | ||
arg = w->cert_cb_arg_; | ||
|
||
w->cert_cb_running_ = false; | ||
w->cert_cb_ = nullptr; | ||
w->cert_cb_arg_ = nullptr; | ||
|
||
cb(arg); | ||
} | ||
|
||
|
||
template <class Base> | ||
void SSLWrap<Base>::SSLGetter(Local<String> property, | ||
const PropertyCallbackInfo<Value>& info) { | ||
|
@@ -1955,6 +2075,10 @@ int Connection::HandleSSLError(const char* func, | |
DEBUG_PRINT("[%p] SSL: %s want read\n", ssl_, func); | ||
return 0; | ||
|
||
} else if (err == SSL_ERROR_WANT_X509_LOOKUP) { | ||
DEBUG_PRINT("[%p] SSL: %s want x509 lookup\n", ssl_, func); | ||
return 0; | ||
|
||
} else if (err == SSL_ERROR_ZERO_RETURN) { | ||
HandleScope scope(ssl_env()->isolate()); | ||
|
||
|
@@ -2120,7 +2244,7 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) { | |
|
||
// Call the SNI callback and use its return value as context | ||
if (!conn->sniObject_.IsEmpty()) { | ||
conn->sniContext_.Reset(); | ||
conn->sni_context_.Reset(); | ||
|
||
Local<Value> arg = PersistentToLocal(env->isolate(), conn->servername_); | ||
Local<Value> ret = conn->MakeCallback(env->onselect_string(), 1, &arg); | ||
|
@@ -2129,7 +2253,7 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) { | |
Local<FunctionTemplate> secure_context_constructor_template = | ||
env->secure_context_constructor_template(); | ||
if (secure_context_constructor_template->HasInstance(ret)) { | ||
conn->sniContext_.Reset(env->isolate(), ret); | ||
conn->sni_context_.Reset(env->isolate(), ret); | ||
SecureContext* sc = Unwrap<SecureContext>(ret.As<Object>()); | ||
InitNPN(sc); | ||
SSL_set_SSL_CTX(s, sc->ctx_); | ||
|
@@ -2168,6 +2292,8 @@ void Connection::New(const FunctionCallbackInfo<Value>& args) { | |
|
||
InitNPN(sc); | ||
|
||
SSL_set_cert_cb(conn->ssl_, SSLWrap<Connection>::SSLCertCallback, conn); | ||
|
||
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB | ||
if (is_server) { | ||
SSL_CTX_set_tlsext_servername_callback(sc->ctx_, SelectSNIContextCallback_); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctx
isundefined
if (!servername || !self._SNICallback) in loadSNI. Do we require SNI to use OCSP stapling?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, we don't :)