@@ -120,6 +120,7 @@ Function MongoCrypt::Init(Napi::Env env) {
120
120
InstanceMethod (" makeDataKeyContext" , &MongoCrypt::MakeDataKeyContext),
121
121
InstanceMethod (" makeRewrapManyDataKeyContext" , &MongoCrypt::MakeRewrapManyDataKeyContext),
122
122
InstanceAccessor (" status" , &MongoCrypt::Status, nullptr ),
123
+ InstanceAccessor (" cryptoHooksProvider" , &MongoCrypt::CryptoHooksProvider, nullptr ),
123
124
InstanceAccessor (
124
125
" cryptSharedLibVersionInfo" , &MongoCrypt::CryptSharedLibVersionInfo, nullptr ),
125
126
StaticValue (" libmongocryptVersion" , String::New (env, mongocrypt_version (nullptr )))});
@@ -201,7 +202,7 @@ static bool aes_256_generic_hook(MongoCrypt* mongoCrypt,
201
202
return true ;
202
203
}
203
204
204
- bool MongoCrypt::setupCryptoHooks () {
205
+ std::unique_ptr<CryptoHooks> MongoCrypt::createJSCryptoHooks () {
205
206
auto aes_256_cbc_encrypt = [](void * ctx,
206
207
mongocrypt_binary_t * key,
207
208
mongocrypt_binary_t * iv,
@@ -398,26 +399,47 @@ bool MongoCrypt::setupCryptoHooks() {
398
399
return true ;
399
400
};
400
401
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;
401
418
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 )) {
409
426
return false ;
410
427
}
411
428
412
429
// Added after `mongocrypt_setopt_crypto_hooks`, they should be treated as the same during
413
430
// configuration
414
431
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 )) {
416
433
return false ;
417
434
}
418
435
419
436
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 )) {
421
443
return false ;
422
444
}
423
445
@@ -472,7 +494,10 @@ MongoCrypt::MongoCrypt(const CallbackInfo& info)
472
494
}
473
495
}
474
496
475
- if (options.Has (" cryptoCallbacks" )) {
497
+ if (!_crypto_hooks) {
498
+ _crypto_hooks = opensslcrypto::createOpenSSLCryptoHooks ();
499
+ }
500
+ if (!_crypto_hooks && options.Has (" cryptoCallbacks" )) {
476
501
Object cryptoCallbacks = options.Get (" cryptoCallbacks" ).ToObject ();
477
502
478
503
SetCallback (" aes256CbcEncryptHook" , cryptoCallbacks[" aes256CbcEncryptHook" ]);
@@ -484,10 +509,10 @@ MongoCrypt::MongoCrypt(const CallbackInfo& info)
484
509
SetCallback (" hmacSha256Hook" , cryptoCallbacks[" hmacSha256Hook" ]);
485
510
SetCallback (" sha256Hook" , cryptoCallbacks[" sha256Hook" ]);
486
511
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 " );
491
516
}
492
517
493
518
if (options.Has (" cryptSharedLibSearchPaths" )) {
@@ -535,6 +560,12 @@ Value MongoCrypt::CryptSharedLibVersionInfo(const CallbackInfo& info) {
535
560
return ret;
536
561
}
537
562
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
+
538
569
Value MongoCrypt::Status (const CallbackInfo& info) {
539
570
std::unique_ptr<mongocrypt_status_t , MongoCryptStatusDeleter> status (mongocrypt_status_new ());
540
571
mongocrypt_status (_mongo_crypt.get (), status.get ());
0 commit comments