@@ -4069,7 +4069,54 @@ void InitCryptoOnce() {
40694069 sk_SSL_COMP_zero (comp_methods);
40704070 assert (sk_SSL_COMP_num (comp_methods) == 0 );
40714071#endif
4072+
4073+ #ifndef OPENSSL_NO_ENGINE
4074+ ERR_load_ENGINE_strings ();
4075+ ENGINE_load_builtin_engines ();
4076+ #endif // !OPENSSL_NO_ENGINE
4077+ }
4078+
4079+
4080+ #ifndef OPENSSL_NO_ENGINE
4081+ void SetEngine (const FunctionCallbackInfo<Value>& args) {
4082+ CHECK (args.Length () >= 2 && args[0 ]->IsString ());
4083+ unsigned int flags = args[1 ]->Uint32Value ();
4084+
4085+ ClearErrorOnReturn clear_error_on_return;
4086+ (void ) &clear_error_on_return; // Silence compiler warning.
4087+
4088+ const String::Utf8Value engine_id (args[0 ]);
4089+ ENGINE* engine = ENGINE_by_id (*engine_id);
4090+
4091+ // Engine not found, try loading dynamically
4092+ if (engine == NULL ) {
4093+ engine = ENGINE_by_id (" dynamic" );
4094+ if (engine != NULL ) {
4095+ if (!ENGINE_ctrl_cmd_string (engine, " SO_PATH" , *engine_id, 0 ) ||
4096+ !ENGINE_ctrl_cmd_string (engine, " LOAD" , NULL , 0 )) {
4097+ ENGINE_free (engine);
4098+ engine = NULL ;
4099+ }
4100+ }
4101+ }
4102+
4103+ if (engine == NULL ) {
4104+ int err = ERR_get_error ();
4105+ if (err == 0 ) {
4106+ char tmp[1024 ];
4107+ snprintf (tmp, sizeof (tmp), " Engine \" %s\" was not found" , *engine_id);
4108+ return ThrowError (tmp);
4109+ } else {
4110+ return ThrowCryptoError (err);
4111+ }
4112+ }
4113+
4114+ int r = ENGINE_set_default (engine, flags);
4115+ ENGINE_free (engine);
4116+ if (r == 0 )
4117+ return ThrowCryptoError (ERR_get_error ());
40724118}
4119+ #endif // !OPENSSL_NO_ENGINE
40734120
40744121
40754122// FIXME(bnoordhuis) Handle global init correctly.
@@ -4090,6 +4137,9 @@ void InitCrypto(Handle<Object> target,
40904137 Verify::Initialize (env, target);
40914138 Certificate::Initialize (target);
40924139
4140+ #ifndef OPENSSL_NO_ENGINE
4141+ NODE_SET_METHOD (target, " setEngine" , SetEngine);
4142+ #endif // !OPENSSL_NO_ENGINE
40934143 NODE_SET_METHOD (target, " PBKDF2" , PBKDF2);
40944144 NODE_SET_METHOD (target, " randomBytes" , RandomBytes<false >);
40954145 NODE_SET_METHOD (target, " pseudoRandomBytes" , RandomBytes<true >);
0 commit comments