@@ -314,6 +314,7 @@ 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 (isolate, tmpl, " setAllowPartialTrustChain" , SetAllowPartialTrustChain);
317
318
SetProtoMethod (isolate, tmpl, " addCRL" , AddCRL);
318
319
SetProtoMethod (isolate, tmpl, " addRootCerts" , AddRootCerts);
319
320
SetProtoMethod (isolate, tmpl, " setCipherSuites" , SetCipherSuites);
@@ -753,17 +754,35 @@ void SecureContext::SetCert(const FunctionCallbackInfo<Value>& args) {
753
754
USE (sc->AddCert (env, std::move (bio)));
754
755
}
755
756
757
+ void SecureContext::SetX509StoreFlag (unsigned long flags) {
758
+ X509_STORE* cert_store = GetCertStoreOwnedByThisSecureContext ();
759
+ CHECK_EQ (1 , X509_STORE_set_flags (cert_store, flags));
760
+ }
761
+
762
+ X509_STORE* SecureContext::GetCertStoreOwnedByThisSecureContext () {
763
+ if (owned_cert_store_cached_ != nullptr ) return owned_cert_store_cached_;
764
+
765
+ X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
766
+ if (cert_store == GetOrCreateRootCertStore ()) {
767
+ cert_store = NewRootCertStore ();
768
+ SSL_CTX_set_cert_store (ctx_.get (), cert_store);
769
+ }
770
+
771
+ return owned_cert_store_cached_ = cert_store;
772
+ }
773
+
774
+ void SecureContext::SetAllowPartialTrustChain (const FunctionCallbackInfo<Value>& args) {
775
+ SecureContext* sc;
776
+ ASSIGN_OR_RETURN_UNWRAP (&sc, args.This ());
777
+ sc->SetX509StoreFlag (X509_V_FLAG_PARTIAL_CHAIN);
778
+ }
779
+
756
780
void SecureContext::SetCACert (const BIOPointer& bio) {
757
781
ClearErrorOnReturn clear_error_on_return;
758
782
if (!bio) return ;
759
- X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
760
783
while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
761
784
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 ()));
785
+ CHECK_EQ (1 , X509_STORE_add_cert (GetCertStoreOwnedByThisSecureContext (), x509.get ()));
767
786
CHECK_EQ (1 , SSL_CTX_add_client_CA (ctx_.get (), x509.get ()));
768
787
}
769
788
}
@@ -793,11 +812,7 @@ Maybe<void> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
793
812
return Nothing<void >();
794
813
}
795
814
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
- }
815
+ X509_STORE* cert_store = GetCertStoreOwnedByThisSecureContext ();
801
816
802
817
CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
803
818
CHECK_EQ (1 ,
@@ -1080,8 +1095,6 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1080
1095
sc->issuer_ .reset ();
1081
1096
sc->cert_ .reset ();
1082
1097
1083
- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
1084
-
1085
1098
DeleteFnPtr<PKCS12, PKCS12_free> p12;
1086
1099
EVPKeyPointer pkey;
1087
1100
X509Pointer cert;
@@ -1135,11 +1148,7 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1135
1148
for (int i = 0 ; i < sk_X509_num (extra_certs.get ()); i++) {
1136
1149
X509* ca = sk_X509_value (extra_certs.get (), i);
1137
1150
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);
1151
+ X509_STORE_add_cert (sc->GetCertStoreOwnedByThisSecureContext (), ca);
1143
1152
SSL_CTX_add_client_CA (sc->ctx_ .get (), ca);
1144
1153
}
1145
1154
ret = true ;
0 commit comments