Skip to content
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

src: use BaseObjectPtr to store SNI context #30548

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/node_crypto.cc
Expand Up @@ -142,8 +142,8 @@ static bool extra_root_certs_loaded = false;
template void SSLWrap<TLSWrap>::AddMethods(Environment* env,
Local<FunctionTemplate> t);
template void SSLWrap<TLSWrap>::ConfigureSecureContext(SecureContext* sc);
template void SSLWrap<TLSWrap>::SetSNIContext(SecureContext* sc);
template int SSLWrap<TLSWrap>::SetCACerts(SecureContext* sc);
template void SSLWrap<TLSWrap>::MemoryInfo(MemoryTracker* tracker) const;
template SSL_SESSION* SSLWrap<TLSWrap>::GetSessionCallback(
SSL* s,
const unsigned char* key,
Expand Down Expand Up @@ -2990,9 +2990,10 @@ void SSLWrap<Base>::CertCbDone(const FunctionCallbackInfo<Value>& args) {
goto fire_cb;

if (cons->HasInstance(ctx)) {
SecureContext* sc;
ASSIGN_OR_RETURN_UNWRAP(&sc, ctx.As<Object>());
w->sni_context_.Reset(env->isolate(), ctx);
SecureContext* sc = Unwrap<SecureContext>(ctx.As<Object>());
CHECK_NOT_NULL(sc);
// Store the SNI context for later use.
w->sni_context_ = BaseObjectPtr<SecureContext>(sc);
bnoordhuis marked this conversation as resolved.
Show resolved Hide resolved

int rv;

Expand Down Expand Up @@ -3050,15 +3051,6 @@ void SSLWrap<Base>::DestroySSL() {
}


template <class Base>
void SSLWrap<Base>::SetSNIContext(SecureContext* sc) {
ConfigureSecureContext(sc);
CHECK_EQ(SSL_set_SSL_CTX(ssl_.get(), sc->ctx_.get()), sc->ctx_.get());

SetCACerts(sc);
}


template <class Base>
int SSLWrap<Base>::SetCACerts(SecureContext* sc) {
int err = SSL_set1_verify_cert_store(ssl_.get(),
Expand All @@ -3074,6 +3066,12 @@ int SSLWrap<Base>::SetCACerts(SecureContext* sc) {
return 1;
}

template <class Base>
void SSLWrap<Base>::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("ocsp_response", ocsp_response_);
tracker->TrackField("sni_context", sni_context_);
}

int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) {
// From https://www.openssl.org/docs/man1.1.1/man3/SSL_verify_cb:
//
Expand Down
5 changes: 3 additions & 2 deletions src/node_crypto.h
Expand Up @@ -222,6 +222,8 @@ class SSLWrap {
inline bool is_awaiting_new_session() const { return awaiting_new_session_; }
inline bool is_waiting_cert_cb() const { return cert_cb_ != nullptr; }

void MemoryInfo(MemoryTracker* tracker) const;

protected:
typedef void (*CertCb)(void* arg);

Expand Down Expand Up @@ -286,7 +288,6 @@ class SSLWrap {

void DestroySSL();
void WaitForCertCb(CertCb cb, void* arg);
void SetSNIContext(SecureContext* sc);
int SetCACerts(SecureContext* sc);

inline Environment* ssl_env() const {
Expand All @@ -308,7 +309,7 @@ class SSLWrap {
ClientHelloParser hello_parser_;

v8::Global<v8::ArrayBufferView> ocsp_response_;
v8::Global<v8::Value> sni_context_;
BaseObjectPtr<SecureContext> sni_context_;

friend class SecureContext;
};
Expand Down
10 changes: 7 additions & 3 deletions src/tls_wrap.cc
Expand Up @@ -1065,11 +1065,14 @@ int TLSWrap::SelectSNIContextCallback(SSL* s, int* ad, void* arg) {
return SSL_TLSEXT_ERR_NOACK;
}

p->sni_context_.Reset(env->isolate(), ctx);

SecureContext* sc = Unwrap<SecureContext>(ctx.As<Object>());
CHECK_NOT_NULL(sc);
p->SetSNIContext(sc);
p->sni_context_ = BaseObjectPtr<SecureContext>(sc);

p->ConfigureSecureContext(sc);
CHECK_EQ(SSL_set_SSL_CTX(p->ssl_.get(), sc->ctx_.get()), sc->ctx_.get());
p->SetCACerts(sc);

return SSL_TLSEXT_ERR_OK;
}

Expand All @@ -1089,6 +1092,7 @@ void TLSWrap::GetWriteQueueSize(const FunctionCallbackInfo<Value>& info) {


void TLSWrap::MemoryInfo(MemoryTracker* tracker) const {
SSLWrap<TLSWrap>::MemoryInfo(tracker);
tracker->TrackField("error", error_);
tracker->TrackFieldWithSize("pending_cleartext_input",
pending_cleartext_input_.size(),
Expand Down