@@ -273,6 +273,8 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
273273 SetProtoMethod (isolate, tmpl, " setKey" , SetKey);
274274 SetProtoMethod (isolate, tmpl, " setCert" , SetCert);
275275 SetProtoMethod (isolate, tmpl, " addCACert" , AddCACert);
276+ SetProtoMethod (
277+ isolate, tmpl, " setAllowPartialTrustChain" , SetAllowPartialTrustChain);
276278 SetProtoMethod (isolate, tmpl, " addCRL" , AddCRL);
277279 SetProtoMethod (isolate, tmpl, " addRootCerts" , AddRootCerts);
278280 SetProtoMethod (isolate, tmpl, " setCipherSuites" , SetCipherSuites);
@@ -354,6 +356,7 @@ void SecureContext::RegisterExternalReferences(
354356 registry->Register (AddCACert);
355357 registry->Register (AddCRL);
356358 registry->Register (AddRootCerts);
359+ registry->Register (SetAllowPartialTrustChain);
357360 registry->Register (SetCipherSuites);
358361 registry->Register (SetCiphers);
359362 registry->Register (SetSigalgs);
@@ -715,17 +718,39 @@ void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
715718 USE (sc->AddCert (env, std::move (bio)));
716719}
717720
721+ // NOLINTNEXTLINE(runtime/int)
722+ void SecureContext::SetX509StoreFlag (unsigned long flags) {
723+ X509_STORE* cert_store = GetCertStoreOwnedByThisSecureContext ();
724+ CHECK_EQ (1 , X509_STORE_set_flags (cert_store, flags));
725+ }
726+
727+ X509_STORE* SecureContext::GetCertStoreOwnedByThisSecureContext () {
728+ if (own_cert_store_cache_ != nullptr ) return own_cert_store_cache_;
729+
730+ X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
731+ if (cert_store == GetOrCreateRootCertStore ()) {
732+ cert_store = NewRootCertStore ();
733+ SSL_CTX_set_cert_store (ctx_.get (), cert_store);
734+ }
735+
736+ return own_cert_store_cache_ = cert_store;
737+ }
738+
739+ void SecureContext::SetAllowPartialTrustChain (
740+ const FunctionCallbackInfo<Value>& args) {
741+ SecureContext* sc;
742+ ASSIGN_OR_RETURN_UNWRAP (&sc, args.This ());
743+ sc->SetX509StoreFlag (X509_V_FLAG_PARTIAL_CHAIN);
744+ }
745+
718746void SecureContext::SetCACert (const BIOPointer& bio) {
719747 ClearErrorOnReturn clear_error_on_return;
720748 if (!bio) return ;
721- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
722749 while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
723750 bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
724- if (cert_store == GetOrCreateRootCertStore ()) {
725- cert_store = NewRootCertStore ();
726- SSL_CTX_set_cert_store (ctx_.get (), cert_store);
727- }
728- CHECK_EQ (1 , X509_STORE_add_cert (cert_store, x509.get ()));
751+ CHECK_EQ (1 ,
752+ X509_STORE_add_cert (GetCertStoreOwnedByThisSecureContext (),
753+ x509.get ()));
729754 CHECK_EQ (1 , SSL_CTX_add_client_CA (ctx_.get (), x509.get ()));
730755 }
731756}
@@ -754,11 +779,7 @@ Maybe<bool> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
754779 return Nothing<bool >();
755780 }
756781
757- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
758- if (cert_store == GetOrCreateRootCertStore ()) {
759- cert_store = NewRootCertStore ();
760- SSL_CTX_set_cert_store (ctx_.get (), cert_store);
761- }
782+ X509_STORE* cert_store = GetCertStoreOwnedByThisSecureContext ();
762783
763784 CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
764785 CHECK_EQ (1 ,
@@ -1042,8 +1063,6 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
10421063 sc->issuer_ .reset ();
10431064 sc->cert_ .reset ();
10441065
1045- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
1046-
10471066 DeleteFnPtr<PKCS12, PKCS12_free> p12;
10481067 EVPKeyPointer pkey;
10491068 X509Pointer cert;
@@ -1097,11 +1116,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
10971116 for (int i = 0 ; i < sk_X509_num (extra_certs.get ()); i++) {
10981117 X509* ca = sk_X509_value (extra_certs.get (), i);
10991118
1100- if (cert_store == GetOrCreateRootCertStore ()) {
1101- cert_store = NewRootCertStore ();
1102- SSL_CTX_set_cert_store (sc->ctx_ .get (), cert_store);
1103- }
1104- X509_STORE_add_cert (cert_store, ca);
1119+ X509_STORE_add_cert (sc->GetCertStoreOwnedByThisSecureContext (), ca);
11051120 SSL_CTX_add_client_CA (sc->ctx_ .get (), ca);
11061121 }
11071122 ret = true ;
0 commit comments