22#include " base_object-inl.h"
33#include " crypto/crypto_bio.h"
44#include " crypto/crypto_common.h"
5+ #include " crypto/crypto_tls_context.h"
56#include " crypto/crypto_util.h"
67#include " env-inl.h"
78#include " memory_tracker-inl.h"
@@ -1731,22 +1732,14 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
17311732 ByteSource passphrase;
17321733 if (args[1 ]->IsString ())
17331734 passphrase = ByteSource::FromString (env, args[1 ].As <String>());
1734- // This redirection is necessary because the PasswordCallback expects a
1735- // pointer to a pointer to the passphrase ByteSource to allow passing in
1736- // const ByteSources.
1737- const ByteSource* pass_ptr = &passphrase;
1738-
1739- EVPKeyPointer key (
1740- PEM_read_bio_PrivateKey (bio.get (),
1741- nullptr ,
1742- PasswordCallback,
1743- &pass_ptr));
1744-
1745- if (!key)
1746- return ThrowCryptoError (env, ERR_get_error (), " PEM_read_bio_PrivateKey" );
1747-
1748- if (!SSL_CTX_use_PrivateKey (sc->ctx_ .get (), key.get ()))
1749- return ThrowCryptoError (env, ERR_get_error (), " SSL_CTX_use_PrivateKey" );
1735+ switch (UsePrivateKey (sc->ctx_ .get (), bio, &passphrase)) {
1736+ case PrivateKeyResult::kSuccess :
1737+ break ;
1738+ case PrivateKeyResult::kParseError :
1739+ return ThrowCryptoError (env, ERR_get_error (), " PEM_read_bio_PrivateKey" );
1740+ case PrivateKeyResult::kApplyError :
1741+ return ThrowCryptoError (env, ERR_get_error (), " SSL_CTX_use_PrivateKey" );
1742+ }
17501743}
17511744
17521745void SecureContext::SetSigalgs (const FunctionCallbackInfo<Value>& args) {
@@ -1849,16 +1842,7 @@ void SecureContext::SetX509StoreFlag(unsigned long flags) {
18491842}
18501843
18511844X509_STORE * SecureContext::GetCertStoreOwnedByThisSecureContext () {
1852- Environment* env = this ->env ();
1853- if (own_cert_store_cache_ != nullptr ) return own_cert_store_cache_;
1854-
1855- X509_STORE * cert_store = SSL_CTX_get_cert_store (ctx_.get ());
1856- if (cert_store == GetOrCreateRootCertStore (env)) {
1857- cert_store = NewRootCertStore (env);
1858- SSL_CTX_set_cert_store (ctx_.get (), cert_store);
1859- }
1860-
1861- return own_cert_store_cache_ = cert_store;
1845+ return GetOrCreateOwnedCertStore (env (), ctx_.get (), &own_cert_store_cache_);
18621846}
18631847
18641848void SecureContext::SetAllowPartialTrustChain (
@@ -1870,13 +1854,7 @@ void SecureContext::SetAllowPartialTrustChain(
18701854
18711855void SecureContext::SetCACert (const BIOPointer& bio) {
18721856 ClearErrorOnReturn clear_error_on_return;
1873- if (!bio) return ;
1874- while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
1875- bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
1876- CHECK_EQ (1 ,
1877- X509_STORE_add_cert (GetCertStoreOwnedByThisSecureContext (), x509));
1878- CHECK_EQ (1 , SSL_CTX_add_client_CA (ctx_.get (), x509));
1879- }
1857+ AddCACertificates (env (), ctx_.get (), bio, &own_cert_store_cache_);
18801858}
18811859
18821860void SecureContext::AddCACert (const FunctionCallbackInfo<Value>& args) {
@@ -1896,20 +1874,11 @@ Maybe<void> SecureContext::SetCRL(Environment* env, const BIOPointer& bio) {
18961874 // TODO(tniessen): this should be checked by the caller and not treated as ok
18971875 if (!bio) return JustVoid ();
18981876
1899- DeleteFnPtr<X509_CRL , X509_CRL_free> crl (
1900- PEM_read_bio_X509_CRL (bio.get (), nullptr , NoPasswordCallback, nullptr ));
1901-
1902- if (!crl) {
1877+ if (!crypto::AddCRL (env, ctx_.get (), bio, &own_cert_store_cache_)) {
19031878 THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to parse CRL" );
19041879 return Nothing<void >();
19051880 }
19061881
1907- X509_STORE * cert_store = GetCertStoreOwnedByThisSecureContext ();
1908-
1909- CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
1910- CHECK_EQ (1 ,
1911- X509_STORE_set_flags (
1912- cert_store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL ));
19131882 return JustVoid ();
19141883}
19151884
@@ -1927,12 +1896,7 @@ void SecureContext::AddCRL(const FunctionCallbackInfo<Value>& args) {
19271896
19281897void SecureContext::SetRootCerts () {
19291898 ClearErrorOnReturn clear_error_on_return;
1930- Environment* env = this ->env ();
1931- auto store = GetOrCreateRootCertStore (env);
1932-
1933- // Increment reference count so global store is not deleted along with CTX.
1934- X509_STORE_up_ref (store);
1935- SSL_CTX_set_cert_store (ctx_.get (), store);
1899+ UseDefaultRootCertStore (env (), ctx_.get ());
19361900}
19371901
19381902void SecureContext::AddRootCerts (const FunctionCallbackInfo<Value>& args) {
0 commit comments