@@ -120,6 +120,7 @@ Function MongoCrypt::Init(Napi::Env env) {
120120 InstanceMethod (" makeDataKeyContext" , &MongoCrypt::MakeDataKeyContext),
121121 InstanceMethod (" makeRewrapManyDataKeyContext" , &MongoCrypt::MakeRewrapManyDataKeyContext),
122122 InstanceAccessor (" status" , &MongoCrypt::Status, nullptr ),
123+ InstanceAccessor (" cryptoHooksProvider" , &MongoCrypt::CryptoHooksProvider, nullptr ),
123124 InstanceAccessor (
124125 " cryptSharedLibVersionInfo" , &MongoCrypt::CryptSharedLibVersionInfo, nullptr ),
125126 StaticValue (" libmongocryptVersion" , String::New (env, mongocrypt_version (nullptr )))});
@@ -201,7 +202,7 @@ static bool aes_256_generic_hook(MongoCrypt* mongoCrypt,
201202 return true ;
202203}
203204
204- bool MongoCrypt::setupCryptoHooks () {
205+ std::unique_ptr<CryptoHooks> MongoCrypt::createJSCryptoHooks () {
205206 auto aes_256_cbc_encrypt = [](void * ctx,
206207 mongocrypt_binary_t * key,
207208 mongocrypt_binary_t * iv,
@@ -398,26 +399,47 @@ bool MongoCrypt::setupCryptoHooks() {
398399 return true ;
399400 };
400401
402+ return std::make_unique<CryptoHooks>(CryptoHooks{" js" ,
403+ aes_256_cbc_encrypt,
404+ aes_256_cbc_decrypt,
405+ random,
406+ hmac_sha_512,
407+ hmac_sha_256,
408+ sha_256,
409+ aes_256_ctr_encrypt,
410+ aes_256_ctr_decrypt,
411+ nullptr ,
412+ sign_rsa_sha256,
413+ this });
414+ }
415+
416+ bool MongoCrypt::installCryptoHooks () {
417+ const auto & hooks = *_crypto_hooks;
401418 if (!mongocrypt_setopt_crypto_hooks (_mongo_crypt.get (),
402- aes_256_cbc_encrypt,
403- aes_256_cbc_decrypt,
404- random,
405- hmac_sha_512,
406- hmac_sha_256,
407- sha_256,
408- this )) {
419+ hooks. aes_256_cbc_encrypt ,
420+ hooks. aes_256_cbc_decrypt ,
421+ hooks. random ,
422+ hooks. hmac_sha_512 ,
423+ hooks. hmac_sha_256 ,
424+ hooks. sha_256 ,
425+ hooks. ctx )) {
409426 return false ;
410427 }
411428
412429 // Added after `mongocrypt_setopt_crypto_hooks`, they should be treated as the same during
413430 // configuration
414431 if (!mongocrypt_setopt_crypto_hook_sign_rsaes_pkcs1_v1_5 (
415- _mongo_crypt.get (), sign_rsa_sha256, this )) {
432+ _mongo_crypt.get (), hooks. sign_rsa_sha256 , this )) {
416433 return false ;
417434 }
418435
419436 if (!mongocrypt_setopt_aes_256_ctr (
420- _mongo_crypt.get (), aes_256_ctr_encrypt, aes_256_ctr_decrypt, this )) {
437+ _mongo_crypt.get (), hooks.aes_256_ctr_encrypt , hooks.aes_256_ctr_decrypt , hooks.ctx )) {
438+ return false ;
439+ }
440+
441+ if (hooks.aes_256_ecb_encrypt &&
442+ !mongocrypt_setopt_aes_256_ecb (_mongo_crypt.get (), hooks.aes_256_ecb_encrypt , hooks.ctx )) {
421443 return false ;
422444 }
423445
@@ -472,7 +494,10 @@ MongoCrypt::MongoCrypt(const CallbackInfo& info)
472494 }
473495 }
474496
475- if (options.Has (" cryptoCallbacks" )) {
497+ if (!_crypto_hooks) {
498+ _crypto_hooks = opensslcrypto::createOpenSSLCryptoHooks ();
499+ }
500+ if (!_crypto_hooks && options.Has (" cryptoCallbacks" )) {
476501 Object cryptoCallbacks = options.Get (" cryptoCallbacks" ).ToObject ();
477502
478503 SetCallback (" aes256CbcEncryptHook" , cryptoCallbacks[" aes256CbcEncryptHook" ]);
@@ -484,10 +509,10 @@ MongoCrypt::MongoCrypt(const CallbackInfo& info)
484509 SetCallback (" hmacSha256Hook" , cryptoCallbacks[" hmacSha256Hook" ]);
485510 SetCallback (" sha256Hook" , cryptoCallbacks[" sha256Hook" ]);
486511 SetCallback (" signRsaSha256Hook" , cryptoCallbacks[" signRsaSha256Hook" ]);
487-
488- if (! setupCryptoHooks ()) {
489- throw Error::New ( Env (), " unable to configure crypto hooks " );
490- }
512+ _crypto_hooks = createJSCryptoHooks ();
513+ }
514+ if (_crypto_hooks && ! installCryptoHooks ()) {
515+ throw Error::New ( Env (), " unable to configure crypto hooks " );
491516 }
492517
493518 if (options.Has (" cryptSharedLibSearchPaths" )) {
@@ -535,6 +560,12 @@ Value MongoCrypt::CryptSharedLibVersionInfo(const CallbackInfo& info) {
535560 return ret;
536561}
537562
563+ Value MongoCrypt::CryptoHooksProvider (const CallbackInfo& info) {
564+ if (!_crypto_hooks)
565+ return Env ().Null ();
566+ return String::New (Env (), _crypto_hooks->id );
567+ }
568+
538569Value MongoCrypt::Status (const CallbackInfo& info) {
539570 std::unique_ptr<mongocrypt_status_t , MongoCryptStatusDeleter> status (mongocrypt_status_new ());
540571 mongocrypt_status (_mongo_crypt.get (), status.get ());
0 commit comments