@@ -314,6 +314,8 @@ Local<FunctionTemplate> SecureContext::GetConstructorTemplate(
314
314
SetProtoMethod (isolate, tmpl, " setKey" , SetKey);
315
315
SetProtoMethod (isolate, tmpl, " setCert" , SetCert);
316
316
SetProtoMethod (isolate, tmpl, " addCACert" , AddCACert);
317
+ SetProtoMethod (
318
+ isolate, tmpl, " setAllowPartialTrustChain" , SetAllowPartialTrustChain);
317
319
SetProtoMethod (isolate, tmpl, " addCRL" , AddCRL);
318
320
SetProtoMethod (isolate, tmpl, " addRootCerts" , AddRootCerts);
319
321
SetProtoMethod (isolate, tmpl, " setCipherSuites" , SetCipherSuites);
@@ -390,6 +392,7 @@ void SecureContext::RegisterExternalReferences(
390
392
registry->Register (AddCACert);
391
393
registry->Register (AddCRL);
392
394
registry->Register (AddRootCerts);
395
+ registry->Register (SetAllowPartialTrustChain);
393
396
registry->Register (SetCipherSuites);
394
397
registry->Register (SetCiphers);
395
398
registry->Register (SetSigalgs);
@@ -753,17 +756,39 @@ void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
753
756
USE (sc->AddCert (env, std::move (bio)));
754
757
}
755
758
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
+
756
784
void SecureContext::SetCACert (const BIOPointer& bio) {
757
785
ClearErrorOnReturn clear_error_on_return;
758
786
if (!bio) return ;
759
- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
760
787
while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
761
788
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 ()));
767
792
CHECK_EQ (1 , SSL_CTX_add_client_CA (ctx_.get (), x509.get ()));
768
793
}
769
794
}
@@ -793,11 +818,7 @@ Maybe<void> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
793
818
return Nothing<void >();
794
819
}
795
820
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 ();
801
822
802
823
CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
803
824
CHECK_EQ (1 ,
@@ -1080,8 +1101,6 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1080
1101
sc->issuer_ .reset ();
1081
1102
sc->cert_ .reset ();
1082
1103
1083
- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
1084
-
1085
1104
DeleteFnPtr<PKCS12, PKCS12_free> p12;
1086
1105
EVPKeyPointer pkey;
1087
1106
X509Pointer cert;
@@ -1135,11 +1154,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1135
1154
for (int i = 0 ; i < sk_X509_num (extra_certs.get ()); i++) {
1136
1155
X509* ca = sk_X509_value (extra_certs.get (), i);
1137
1156
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);
1143
1158
SSL_CTX_add_client_CA (sc->ctx_ .get (), ca);
1144
1159
}
1145
1160
ret = true ;
0 commit comments