@@ -32,7 +32,10 @@ using v8::HandleScope;
32
32
using v8::Int32;
33
33
using v8::Integer;
34
34
using v8::Isolate;
35
+ using v8::Just;
35
36
using v8::Local;
37
+ using v8::Maybe;
38
+ using v8::Nothing;
36
39
using v8::Object;
37
40
using v8::PropertyAttribute;
38
41
using v8::ReadOnly;
@@ -574,6 +577,22 @@ void SecureContext::SetKeylogCallback(KeylogCb cb) {
574
577
SSL_CTX_set_keylog_callback (ctx_.get (), cb);
575
578
}
576
579
580
+ Maybe<bool > SecureContext::UseKey (Environment* env,
581
+ std::shared_ptr<KeyObjectData> key) {
582
+ if (key->GetKeyType () != KeyType::kKeyTypePrivate ) {
583
+ THROW_ERR_CRYPTO_INVALID_KEYTYPE (env);
584
+ return Nothing<bool >();
585
+ }
586
+
587
+ ClearErrorOnReturn clear_error_on_return;
588
+ if (!SSL_CTX_use_PrivateKey (ctx_.get (), key->GetAsymmetricKey ().get ())) {
589
+ ThrowCryptoError (env, ERR_get_error (), " SSL_CTX_use_PrivateKey" );
590
+ return Nothing<bool >();
591
+ }
592
+
593
+ return Just (true );
594
+ }
595
+
577
596
void SecureContext::SetKey (const FunctionCallbackInfo<Value>& args) {
578
597
Environment* env = Environment::GetCurrent (args);
579
598
@@ -662,97 +681,112 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
662
681
}
663
682
#endif // !OPENSSL_NO_ENGINE
664
683
665
- void SecureContext::SetCert (const FunctionCallbackInfo<Value>& args) {
666
- Environment* env = Environment::GetCurrent (args);
667
-
668
- SecureContext* sc;
669
- ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
670
-
671
- CHECK_GE (args.Length (), 1 ); // Certificate argument is mandator
672
-
673
- BIOPointer bio (LoadBIO (env, args[0 ]));
674
- if (!bio)
675
- return ;
676
-
677
- sc->cert_ .reset ();
678
- sc->issuer_ .reset ();
684
+ Maybe<bool > SecureContext::AddCert (Environment* env, BIOPointer&& bio) {
685
+ ClearErrorOnReturn clear_error_on_return;
686
+ if (!bio) return Just (false );
687
+ cert_.reset ();
688
+ issuer_.reset ();
679
689
680
- if (!SSL_CTX_use_certificate_chain (
681
- sc->ctx_ .get (),
682
- std::move (bio),
683
- &sc->cert_ ,
684
- &sc->issuer_ )) {
685
- return ThrowCryptoError (
686
- env,
687
- ERR_get_error (),
688
- " SSL_CTX_use_certificate_chain" );
690
+ // The SSL_CTX_use_certificate_chain call here is not from openssl, this is
691
+ // the method implemented elsewhere in this file. The naming is a bit
692
+ // confusing, unfortunately.
693
+ if (SSL_CTX_use_certificate_chain (
694
+ ctx_.get (), std::move (bio), &cert_, &issuer_) == 0 ) {
695
+ ThrowCryptoError (env, ERR_get_error (), " SSL_CTX_use_certificate_chain" );
696
+ return Nothing<bool >();
689
697
}
698
+ return Just (true );
690
699
}
691
700
692
- void SecureContext::AddCACert (const FunctionCallbackInfo<Value>& args) {
701
+ void SecureContext::SetCert (const FunctionCallbackInfo<Value>& args) {
693
702
Environment* env = Environment::GetCurrent (args);
694
703
695
704
SecureContext* sc;
696
705
ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
697
- ClearErrorOnReturn clear_error_on_return;
698
706
699
- CHECK_GE (args.Length (), 1 ); // CA certificate argument is mandatory
707
+ CHECK_GE (args.Length (), 1 ); // Certificate argument is mandatory
700
708
701
709
BIOPointer bio (LoadBIO (env, args[0 ]));
702
- if (! bio)
703
- return ;
710
+ USE (sc-> AddCert (env, std::move ( bio)));
711
+ }
704
712
705
- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc->ctx_ .get ());
713
+ void SecureContext::SetCACert (const BIOPointer& bio) {
714
+ ClearErrorOnReturn clear_error_on_return;
715
+ if (!bio) return ;
716
+ X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
706
717
while (X509Pointer x509 = X509Pointer (PEM_read_bio_X509_AUX (
707
718
bio.get (), nullptr , NoPasswordCallback, nullptr ))) {
708
719
if (cert_store == GetOrCreateRootCertStore ()) {
709
720
cert_store = NewRootCertStore ();
710
- SSL_CTX_set_cert_store (sc-> ctx_ .get (), cert_store);
721
+ SSL_CTX_set_cert_store (ctx_.get (), cert_store);
711
722
}
712
- X509_STORE_add_cert (cert_store, x509.get ());
713
- SSL_CTX_add_client_CA (sc-> ctx_ .get (), x509.get ());
723
+ CHECK_EQ ( 1 , X509_STORE_add_cert (cert_store, x509.get () ));
724
+ CHECK_EQ ( 1 , SSL_CTX_add_client_CA (ctx_.get (), x509.get () ));
714
725
}
715
726
}
716
727
717
- void SecureContext::AddCRL (const FunctionCallbackInfo<Value>& args) {
728
+ void SecureContext::AddCACert (const FunctionCallbackInfo<Value>& args) {
718
729
Environment* env = Environment::GetCurrent (args);
719
730
720
731
SecureContext* sc;
721
732
ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
722
733
723
- CHECK_GE (args.Length (), 1 ); // CRL argument is mandatory
724
-
725
- ClearErrorOnReturn clear_error_on_return;
734
+ CHECK_GE (args.Length (), 1 ); // CA certificate argument is mandatory
726
735
727
736
BIOPointer bio (LoadBIO (env, args[0 ]));
728
- if (!bio)
729
- return ;
737
+ sc->SetCACert (bio);
738
+ }
739
+
740
+ Maybe<bool > SecureContext::SetCRL (Environment* env, const BIOPointer& bio) {
741
+ ClearErrorOnReturn clear_error_on_return;
742
+ if (!bio) return Just (false );
730
743
731
744
DeleteFnPtr<X509_CRL, X509_CRL_free> crl (
732
745
PEM_read_bio_X509_CRL (bio.get (), nullptr , NoPasswordCallback, nullptr ));
733
746
734
- if (!crl)
735
- return THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to parse CRL" );
747
+ if (!crl) {
748
+ THROW_ERR_CRYPTO_OPERATION_FAILED (env, " Failed to parse CRL" );
749
+ return Nothing<bool >();
750
+ }
736
751
737
- X509_STORE* cert_store = SSL_CTX_get_cert_store (sc-> ctx_ .get ());
752
+ X509_STORE* cert_store = SSL_CTX_get_cert_store (ctx_.get ());
738
753
if (cert_store == GetOrCreateRootCertStore ()) {
739
754
cert_store = NewRootCertStore ();
740
- SSL_CTX_set_cert_store (sc-> ctx_ .get (), cert_store);
755
+ SSL_CTX_set_cert_store (ctx_.get (), cert_store);
741
756
}
742
757
743
- X509_STORE_add_crl (cert_store, crl.get ());
744
- X509_STORE_set_flags (cert_store,
745
- X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
758
+ CHECK_EQ (1 , X509_STORE_add_crl (cert_store, crl.get ()));
759
+ CHECK_EQ (1 ,
760
+ X509_STORE_set_flags (
761
+ cert_store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL));
762
+ return Just (true );
746
763
}
747
764
748
- void SecureContext::AddRootCerts (const FunctionCallbackInfo<Value>& args) {
765
+ void SecureContext::AddCRL (const FunctionCallbackInfo<Value>& args) {
766
+ Environment* env = Environment::GetCurrent (args);
767
+
749
768
SecureContext* sc;
750
769
ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
770
+
771
+ CHECK_GE (args.Length (), 1 ); // CRL argument is mandatory
772
+
773
+ BIOPointer bio (LoadBIO (env, args[0 ]));
774
+ USE (sc->SetCRL (env, bio));
775
+ }
776
+
777
+ void SecureContext::SetRootCerts () {
751
778
ClearErrorOnReturn clear_error_on_return;
752
- X509_STORE* store = GetOrCreateRootCertStore ();
779
+ auto store = GetOrCreateRootCertStore ();
780
+
753
781
// Increment reference count so global store is not deleted along with CTX.
754
782
X509_STORE_up_ref (store);
755
- SSL_CTX_set_cert_store (sc->ctx_ .get (), store);
783
+ SSL_CTX_set_cert_store (ctx_.get (), store);
784
+ }
785
+
786
+ void SecureContext::AddRootCerts (const FunctionCallbackInfo<Value>& args) {
787
+ SecureContext* sc;
788
+ ASSIGN_OR_RETURN_UNWRAP (&sc, args.Holder ());
789
+ sc->SetRootCerts ();
756
790
}
757
791
758
792
void SecureContext::SetCipherSuites (const FunctionCallbackInfo<Value>& args) {
0 commit comments