@@ -314,6 +314,8 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
314314 SetProtoMethod (isolate, tmpl, " setKey" , SetKey);
315315 SetProtoMethod (isolate, tmpl, " setCert" , SetCert);
316316 SetProtoMethod (isolate, tmpl, " addCACert" , AddCACert);
317+ SetProtoMethod (
318+ isolate, tmpl, " setAllowPartialTrustChain" , SetAllowPartialTrustChain);
317319 SetProtoMethod (isolate, tmpl, " addCRL" , AddCRL);
318320 SetProtoMethod (isolate, tmpl, " addRootCerts" , AddRootCerts);
319321 SetProtoMethod (isolate, tmpl, " setCipherSuites" , SetCipherSuites);
@@ -390,6 +392,7 @@ void SecureContext::RegisterExternalReferences(
390392 registry->Register (AddCACert);
391393 registry->Register (AddCRL);
392394 registry->Register (AddRootCerts);
395+ registry->Register (SetAllowPartialTrustChain);
393396 registry->Register (SetCipherSuites);
394397 registry->Register (SetCiphers);
395398 registry->Register (SetSigalgs);
@@ -753,17 +756,39 @@ void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
753756 USE (sc->AddCert (env, std::move (bio)));
754757}
755758
759+ // NOLINTNEXTLINE(runtime/int)
760+ void SecureContext::SetX509StoreFlag (unsigned long flags) {
761+ X509_STORE* cert_store = GetCertStoreOwnedByThisSecureContext ();
762+ CHECK_EQ (1 , X509_STORE_set_flags (cert_store, flags));
763+ }
764+
765+ X509_STORE* SecureContext::GetCertStoreOwnedByThisSecureContext () {
766+ if (own_cert_store_cache_ != nullptr ) return own_cert_store_cache_;
767+
768+ X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
769+ if (cert_store == GetOrCreateRootCertStore ()) {
770+ cert_store = NewRootCertStore ();
771+ SSL_CTX_set_cert_store (ctx_.get (), cert_store);
772+ }
773+
774+ return own_cert_store_cache_ = cert_store;
775+ }
776+
777+ void SecureContext::SetAllowPartialTrustChain (
778+ const FunctionCallbackInfo<Value>& args) {
779+ SecureContext* sc;
780+ ASSIGN_OR_RETURN_UNWRAP (&sc, args.This ());
781+ sc->SetX509StoreFlag (X509_V_FLAG_PARTIAL_CHAIN);
782+ }
783+
756784void SecureContext::SetCACert (const BIOPointer& bio) {
757785 ClearErrorOnReturn clear_error_on_return;
758786 if (!bio) return ;
759- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
760787 while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
761788 bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
762- if (cert_store == GetOrCreateRootCertStore ()) {
763- cert_store = NewRootCertStore ();
764- SSL_CTX_set_cert_store (ctx_.get (), cert_store);
765- }
766- CHECK_EQ (1 , X509_STORE_add_cert (cert_store, x509.get ()));
789+ CHECK_EQ (1 ,
790+ X509_STORE_add_cert (GetCertStoreOwnedByThisSecureContext (),
791+ x509.get ()));
767792 CHECK_EQ (1 , SSL_CTX_add_client_CA (ctx_.get (), x509.get ()));
768793 }
769794}
@@ -793,11 +818,7 @@ Maybe<void> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
793818 return Nothing<void >();
794819 }
795820
796- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
797- if (cert_store == GetOrCreateRootCertStore ()) {
798- cert_store = NewRootCertStore ();
799- SSL_CTX_set_cert_store (ctx_.get (), cert_store);
800- }
821+ X509_STORE* cert_store = GetCertStoreOwnedByThisSecureContext ();
801822
802823 CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
803824 CHECK_EQ (1 ,
@@ -1080,8 +1101,6 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
10801101 sc->issuer_ .reset ();
10811102 sc->cert_ .reset ();
10821103
1083- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
1084-
10851104 DeleteFnPtr<PKCS12, PKCS12_free> p12;
10861105 EVPKeyPointer pkey;
10871106 X509Pointer cert;
@@ -1135,11 +1154,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
11351154 for (int i = 0 ; i < sk_X509_num (extra_certs.get ()); i++) {
11361155 X509* ca = sk_X509_value (extra_certs.get (), i);
11371156
1138- if (cert_store == GetOrCreateRootCertStore ()) {
1139- cert_store = NewRootCertStore ();
1140- SSL_CTX_set_cert_store (sc->ctx_ .get (), cert_store);
1141- }
1142- X509_STORE_add_cert (cert_store, ca);
1157+ X509_STORE_add_cert (sc->GetCertStoreOwnedByThisSecureContext (), ca);
11431158 SSL_CTX_add_client_CA (sc->ctx_ .get (), ca);
11441159 }
11451160 ret = true ;
0 commit comments