@@ -105,6 +105,12 @@ SSL_SESSION* GetSessionCallback( |
105 | 105 | return w->ReleaseSession(); |
106 | 106 | } |
107 | 107 |
|
| 108 | +// The server name is handled in EarlyClientHelloCallback, so this only |
| 109 | +// acknowledges it, which also has the TLS library store it in new sessions. |
| 110 | +int AcknowledgeServerName(SSL* s, int* ad, void* arg) { |
| 111 | + return SSL_TLSEXT_ERR_OK; |
| 112 | +} |
| 113 | + |
108 | 114 | // Reads a 16-bit length prefixed vector from the front of data. Returns |
109 | 115 | // nothing if the length does not fit within data. |
110 | 116 | std::optional<std::span<const uint8_t>> ReadVector16( |
@@ -158,10 +164,7 @@ ssl_select_cert_result_t EarlyClientHelloCallback(const SSL_CLIENT_HELLO* ch) { |
158 | 164 | // If we can't parse the hello SNI, reject it explicitly: |
159 | 165 | if (!servername.has_value()) return ssl_select_cert_error; |
160 | 166 |
|
161 | | - // Session resumption must only be allowed for sessions with exactly the same |
162 | | - // SNI. We use the SNI to derive the session id, so no handshake can resume |
163 | | - // from a mismatched SNI value. Mismatches fall back to full handshakes: |
164 | | - if (!w->BindSessionsToServerName(*servername)) return ssl_select_cert_error; |
| 167 | + if (!w->PinServerName(*servername)) return ssl_select_cert_error; |
165 | 168 |
|
166 | 169 | return ssl_select_cert_success; |
167 | 170 | } |
@@ -183,10 +186,10 @@ int EarlyClientHelloCallback(SSL* s, int* al, void* arg) { |
183 | 186 | return SSL_CLIENT_HELLO_ERROR; |
184 | 187 | } |
185 | 188 |
|
186 | | - // Session resumption must only be allowed for sessions with exactly the same |
187 | | - // SNI. We use the SNI to derive the session id, so no handshake can resume |
188 | | - // from a mismatched SNI value. Mismatches fall back to full handshakes: |
189 | | - if (!w->BindSessionsToServerName(*servername)) return SSL_CLIENT_HELLO_ERROR; |
| 189 | + if (!w->PinServerName(*servername)) { |
| 190 | + *al = SSL_AD_ILLEGAL_PARAMETER; |
| 191 | + return SSL_CLIENT_HELLO_ERROR; |
| 192 | + } |
190 | 193 |
|
191 | 194 | return SSL_CLIENT_HELLO_SUCCESS; |
192 | 195 | } |
@@ -604,8 +607,7 @@ void TLSWrap::InitSSL() { |
604 | 607 | // https://github.com/openssl/openssl/issues/7199#issuecomment-420670544 |
605 | 608 | SSL_set_info_callback(ssl_.get(), SSLInfoCallback); |
606 | 609 |
|
607 | | - if (is_server()) |
608 | | - sc_->SetSelectSNIContextCallback(SelectSNIContextCallback); |
| 610 | + if (is_server()) sc_->SetSelectSNIContextCallback(AcknowledgeServerName); |
609 | 611 |
|
610 | 612 | ConfigureSecureContext(sc_.get()); |
611 | 613 |
|
@@ -1486,39 +1488,34 @@ void TLSWrap::SetServername(const FunctionCallbackInfo<Value>& args) { |
1486 | 1488 | SSL_set_tlsext_host_name(wrap->ssl_.get(), *servername); |
1487 | 1489 | } |
1488 | 1490 |
|
1489 | | -int TLSWrap::SelectSNIContextCallback(SSL* s, int* ad, void* arg) { |
1490 | | - TLSWrap* p = static_cast<TLSWrap*>(SSL_get_app_data(s)); |
1491 | | - Environment* env = p->env(); |
1492 | | - HandleScope handle_scope(env->isolate()); |
1493 | | - Context::Scope context_scope(env->context()); |
1494 | | - |
1495 | | - auto servername = SSLPointer::GetServerName(s); |
1496 | | - if (!servername.has_value() || |
1497 | | - !Set(env, p->GetOwner(), env->servername_string(), servername.value())) |
1498 | | - return SSL_TLSEXT_ERR_NOACK; |
1499 | | - |
1500 | | - Local<Value> ctx = p->object()->Get(env->context(), env->sni_context_string()) |
1501 | | - .FromMaybe(Local<Value>()); |
1502 | | - |
1503 | | - // Acknowlege the name so it's correctly stored on the session: |
1504 | | - if (ctx.IsEmpty() || !ctx->IsObject()) return SSL_TLSEXT_ERR_OK; |
1505 | | - |
1506 | | - if (!env->secure_context_constructor_template()->HasInstance(ctx)) { |
1507 | | - // Failure: incorrect SNI context object |
1508 | | - Local<Value> err = Exception::TypeError(env->sni_context_err_string()); |
1509 | | - p->MakeCallback(env->onerror_string(), 1, &err); |
1510 | | - return SSL_TLSEXT_ERR_NOACK; |
| 1491 | +// The connection's server name is fixed by its first ClientHello: later ones (a |
| 1492 | +// renegotiation, or the second of a HelloRetryRequest) must ask for exactly the |
| 1493 | +// same, as the connection's secure context was chosen for it. |
| 1494 | +bool TLSWrap::PinServerName(std::string_view servername) { |
| 1495 | + if (servername_pinned_) { |
| 1496 | + if (servername == servername_) return true; |
| 1497 | + Debug(this, |
| 1498 | + "ClientHello asked for server name '%s', connection pinned to '%s'", |
| 1499 | + servername, |
| 1500 | + servername_); |
| 1501 | + return false; |
1511 | 1502 | } |
| 1503 | + servername_pinned_ = true; |
| 1504 | + servername_ = servername; |
1512 | 1505 |
|
1513 | | - SecureContext* sc = Unwrap<SecureContext>(ctx.As<Object>()); |
1514 | | - CHECK_NOT_NULL(sc); |
1515 | | - p->sni_context_ = BaseObjectPtr<SecureContext>(sc); |
1516 | | - |
1517 | | - ConfigureSecureContext(sc); |
1518 | | - CHECK_EQ(SSL_set_SSL_CTX(p->ssl_.get(), sc->ctx().get()), sc->ctx().get()); |
1519 | | - p->SetCACerts(sc); |
| 1506 | + if (!servername.empty()) { |
| 1507 | + HandleScope handle_scope(env()->isolate()); |
| 1508 | + Context::Scope context_scope(env()->context()); |
| 1509 | + if (!Set(env(), GetOwner(), env()->servername_string(), servername_)) { |
| 1510 | + return false; |
| 1511 | + } |
| 1512 | + } |
1520 | 1513 |
|
1521 | | - return SSL_TLSEXT_ERR_OK; |
| 1514 | + // Session resumption must only be allowed for sessions with exactly the same |
| 1515 | + // SNI. We use the SNI to derive the session id context, so no handshake can |
| 1516 | + // resume from a mismatched SNI value. Mismatches fall back to full |
| 1517 | + // handshakes: |
| 1518 | + return BindSessionsToServerName(servername); |
1522 | 1519 | } |
1523 | 1520 |
|
1524 | 1521 | bool TLSWrap::BindSessionsToServerName(std::string_view servername) { |
@@ -1699,6 +1696,7 @@ void TLSWrap::MemoryInfo(MemoryTracker* tracker) const { |
1699 | 1696 | tracker->TrackField("ocsp_response", ocsp_response_); |
1700 | 1697 | tracker->TrackField("sni_context", sni_context_); |
1701 | 1698 | tracker->TrackField("error", error_); |
| 1699 | + tracker->TrackField("servername", servername_); |
1702 | 1700 | if (pending_cleartext_input_) |
1703 | 1701 | tracker->TrackField("pending_cleartext_input", pending_cleartext_input_); |
1704 | 1702 | if (enc_in_ != nullptr) |
|