@@ -768,6 +768,14 @@ void SecureContext::SetSigalgs(const FunctionCallbackInfo<Value>& args) {
768
768
}
769
769
770
770
#ifndef OPENSSL_NO_ENGINE
771
+ // Helpers for the smart pointer.
772
+ void ENGINE_free_fn (ENGINE* engine) { ENGINE_free (engine); }
773
+
774
+ void ENGINE_finish_and_free_fn (ENGINE* engine) {
775
+ ENGINE_finish (engine);
776
+ ENGINE_free (engine);
777
+ }
778
+
771
779
void SecureContext::SetEngineKey (const FunctionCallbackInfo<Value>& args) {
772
780
Environment* env = Environment::GetCurrent (args);
773
781
@@ -778,17 +786,22 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
778
786
779
787
char errmsg[1024 ];
780
788
const node::Utf8Value engine_id (env->isolate (), args[1 ]);
781
- ENGINE* e = LoadEngineById (*engine_id, &errmsg);
782
- if (e == nullptr ) {
789
+ std::unique_ptr<ENGINE, std::function<void (ENGINE*)>> e =
790
+ { LoadEngineById (*engine_id, &errmsg),
791
+ ENGINE_free_fn };
792
+ if (e.get () == nullptr ) {
783
793
return env->ThrowError (errmsg);
784
794
}
785
795
786
- if (!ENGINE_init (e)) {
796
+ if (!ENGINE_init (e. get () )) {
787
797
return env->ThrowError (" ENGINE_init" );
788
798
}
789
799
800
+ e.get_deleter () = ENGINE_finish_and_free_fn;
801
+
790
802
const node::Utf8Value key_name (env->isolate (), args[0 ]);
791
- EVPKeyPointer key (ENGINE_load_private_key (e, *key_name, nullptr , nullptr ));
803
+ EVPKeyPointer key (ENGINE_load_private_key (e.get (), *key_name,
804
+ nullptr , nullptr ));
792
805
793
806
if (!key) {
794
807
return ThrowCryptoError (env, ERR_get_error (), " ENGINE_load_private_key" );
@@ -799,6 +812,8 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
799
812
if (rv == 0 ) {
800
813
return ThrowCryptoError (env, ERR_get_error (), " SSL_CTX_use_PrivateKey" );
801
814
}
815
+
816
+ sc->private_key_engine_ = std::move (e);
802
817
}
803
818
#endif // !OPENSSL_NO_ENGINE
804
819
@@ -1476,9 +1491,6 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
1476
1491
1477
1492
1478
1493
#ifndef OPENSSL_NO_ENGINE
1479
- // Helper for the smart pointer.
1480
- void ENGINE_free_fn (ENGINE* engine) { ENGINE_free (engine); }
1481
-
1482
1494
void SecureContext::SetClientCertEngine (
1483
1495
const FunctionCallbackInfo<Value>& args) {
1484
1496
Environment* env = Environment::GetCurrent (args);
0 commit comments