@@ -273,6 +273,8 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
273
273
SetProtoMethod (isolate, tmpl, " setKey" , SetKey);
274
274
SetProtoMethod (isolate, tmpl, " setCert" , SetCert);
275
275
SetProtoMethod (isolate, tmpl, " addCACert" , AddCACert);
276
+ SetProtoMethod (
277
+ isolate, tmpl, " setAllowPartialTrustChain" , SetAllowPartialTrustChain);
276
278
SetProtoMethod (isolate, tmpl, " addCRL" , AddCRL);
277
279
SetProtoMethod (isolate, tmpl, " addRootCerts" , AddRootCerts);
278
280
SetProtoMethod (isolate, tmpl, " setCipherSuites" , SetCipherSuites);
@@ -354,6 +356,7 @@ void SecureContext::RegisterExternalReferences(
354
356
registry->Register (AddCACert);
355
357
registry->Register (AddCRL);
356
358
registry->Register (AddRootCerts);
359
+ registry->Register (SetAllowPartialTrustChain);
357
360
registry->Register (SetCipherSuites);
358
361
registry->Register (SetCiphers);
359
362
registry->Register (SetSigalgs);
@@ -715,17 +718,39 @@ void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
715
718
USE (sc->AddCert (env, std::move (bio)));
716
719
}
717
720
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
+
718
746
void SecureContext::SetCACert (const BIOPointer& bio) {
719
747
ClearErrorOnReturn clear_error_on_return;
720
748
if (!bio) return ;
721
- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
722
749
while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
723
750
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 ()));
729
754
CHECK_EQ (1 , SSL_CTX_add_client_CA (ctx_.get (), x509.get ()));
730
755
}
731
756
}
@@ -754,11 +779,7 @@ Maybe<bool> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
754
779
return Nothing<bool >();
755
780
}
756
781
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 ();
762
783
763
784
CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
764
785
CHECK_EQ (1 ,
@@ -1042,8 +1063,6 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1042
1063
sc->issuer_ .reset ();
1043
1064
sc->cert_ .reset ();
1044
1065
1045
- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
1046
-
1047
1066
DeleteFnPtr<PKCS12, PKCS12_free> p12;
1048
1067
EVPKeyPointer pkey;
1049
1068
X509Pointer cert;
@@ -1097,11 +1116,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1097
1116
for (int i = 0 ; i < sk_X509_num (extra_certs.get ()); i++) {
1098
1117
X509* ca = sk_X509_value (extra_certs.get (), i);
1099
1118
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);
1105
1120
SSL_CTX_add_client_CA (sc->ctx_ .get (), ca);
1106
1121
}
1107
1122
ret = true ;
0 commit comments