@@ -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,51 @@ bool MongoCrypt::setupCryptoHooks() {
398
399
return true ;
399
400
};
400
401
402
+ return std::make_unique<CryptoHooks>(CryptoHooks {
403
+ " js" ,
404
+ aes_256_cbc_encrypt,
405
+ aes_256_cbc_decrypt,
406
+ random,
407
+ hmac_sha_512,
408
+ hmac_sha_256,
409
+ sha_256,
410
+ aes_256_ctr_encrypt,
411
+ aes_256_ctr_decrypt,
412
+ nullptr ,
413
+ sign_rsa_sha256,
414
+ this
415
+ });
416
+ }
417
+
418
+ bool MongoCrypt::installCryptoHooks () {
419
+ const auto & hooks = *_crypto_hooks;
401
420
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 )) {
421
+ hooks. aes_256_cbc_encrypt ,
422
+ hooks. aes_256_cbc_decrypt ,
423
+ hooks. random ,
424
+ hooks. hmac_sha_512 ,
425
+ hooks. hmac_sha_256 ,
426
+ hooks. sha_256 ,
427
+ hooks. ctx )) {
409
428
return false ;
410
429
}
411
430
412
431
// Added after `mongocrypt_setopt_crypto_hooks`, they should be treated as the same during
413
432
// configuration
414
433
if (!mongocrypt_setopt_crypto_hook_sign_rsaes_pkcs1_v1_5 (
415
- _mongo_crypt.get (), sign_rsa_sha256, this )) {
434
+ _mongo_crypt.get (), hooks. sign_rsa_sha256 , this )) {
416
435
return false ;
417
436
}
418
437
419
438
if (!mongocrypt_setopt_aes_256_ctr (
420
- _mongo_crypt.get (), aes_256_ctr_encrypt, aes_256_ctr_decrypt, this )) {
439
+ _mongo_crypt.get (), hooks.aes_256_ctr_encrypt , hooks.aes_256_ctr_decrypt , hooks.ctx )) {
440
+ return false ;
441
+ }
442
+
443
+ if (hooks.aes_256_ecb_encrypt &&
444
+ !mongocrypt_setopt_aes_256_ecb (
445
+ _mongo_crypt.get (), hooks.aes_256_ecb_encrypt , hooks.ctx )
446
+ ) {
421
447
return false ;
422
448
}
423
449
@@ -472,7 +498,10 @@ MongoCrypt::MongoCrypt(const CallbackInfo& info)
472
498
}
473
499
}
474
500
475
- if (options.Has (" cryptoCallbacks" )) {
501
+ if (!_crypto_hooks) {
502
+ _crypto_hooks = opensslcrypto::createOpenSSLCryptoHooks ();
503
+ }
504
+ if (!_crypto_hooks && options.Has (" cryptoCallbacks" )) {
476
505
Object cryptoCallbacks = options.Get (" cryptoCallbacks" ).ToObject ();
477
506
478
507
SetCallback (" aes256CbcEncryptHook" , cryptoCallbacks[" aes256CbcEncryptHook" ]);
@@ -484,10 +513,10 @@ MongoCrypt::MongoCrypt(const CallbackInfo& info)
484
513
SetCallback (" hmacSha256Hook" , cryptoCallbacks[" hmacSha256Hook" ]);
485
514
SetCallback (" sha256Hook" , cryptoCallbacks[" sha256Hook" ]);
486
515
SetCallback (" signRsaSha256Hook" , cryptoCallbacks[" signRsaSha256Hook" ]);
487
-
488
- if (! setupCryptoHooks ()) {
489
- throw Error::New ( Env (), " unable to configure crypto hooks " );
490
- }
516
+ _crypto_hooks = createJSCryptoHooks ();
517
+ }
518
+ if (_crypto_hooks && ! installCryptoHooks ()) {
519
+ throw Error::New ( Env (), " unable to configure crypto hooks " );
491
520
}
492
521
493
522
if (options.Has (" cryptSharedLibSearchPaths" )) {
@@ -535,6 +564,11 @@ Value MongoCrypt::CryptSharedLibVersionInfo(const CallbackInfo& info) {
535
564
return ret;
536
565
}
537
566
567
+ Value MongoCrypt::CryptoHooksProvider (const CallbackInfo& info) {
568
+ if (!_crypto_hooks) return Env ().Null ();
569
+ return String::New (Env (), _crypto_hooks->id );
570
+ }
571
+
538
572
Value MongoCrypt::Status (const CallbackInfo& info) {
539
573
std::unique_ptr<mongocrypt_status_t , MongoCryptStatusDeleter> status (mongocrypt_status_new ());
540
574
mongocrypt_status (_mongo_crypt.get (), status.get ());
0 commit comments