|
1 | 1 | import * as semver from 'semver';
|
2 | 2 | import * as process from 'node:process';
|
| 3 | +import * as crypto from 'node:crypto'; |
3 | 4 | import * as path from 'node:path';
|
4 | 5 | import * as fs from 'node:fs';
|
5 | 6 | import * as sinon from 'sinon';
|
6 | 7 | import { EJSON, BSON, Binary } from 'bson';
|
7 | 8 | import { MongoCrypt, MongoCryptConstructor } from '../src';
|
8 | 9 | import { expect } from 'chai';
|
9 | 10 |
|
| 11 | +function makeAES256Hook(method, mode) { |
| 12 | + return function (key, iv, input, output) { |
| 13 | + let result; |
| 14 | + try { |
| 15 | + const cipher = crypto[method](mode, key, iv); |
| 16 | + cipher.setAutoPadding(false); |
| 17 | + result = cipher.update(input); |
| 18 | + const final = cipher.final(); |
| 19 | + if (final.length > 0) { |
| 20 | + result = Buffer.concat([result, final]); |
| 21 | + } |
| 22 | + } catch (e) { |
| 23 | + return e; |
| 24 | + } |
| 25 | + result.copy(output); |
| 26 | + return result.length; |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function randomHook(buffer, count) { |
| 31 | + try { |
| 32 | + crypto.randomFillSync(buffer, 0, count); |
| 33 | + } catch (e) { |
| 34 | + return e; |
| 35 | + } |
| 36 | + return count; |
| 37 | +} |
| 38 | + |
| 39 | +function sha256Hook(input, output) { |
| 40 | + let result; |
| 41 | + try { |
| 42 | + result = crypto.createHash('sha256').update(input).digest(); |
| 43 | + } catch (e) { |
| 44 | + return e; |
| 45 | + } |
| 46 | + result.copy(output); |
| 47 | + return result.length; |
| 48 | +} |
| 49 | + |
| 50 | +function makeHmacHook(algorithm) { |
| 51 | + return (key, input, output) => { |
| 52 | + let result; |
| 53 | + try { |
| 54 | + result = crypto.createHmac(algorithm, key).update(input).digest(); |
| 55 | + } catch (e) { |
| 56 | + return e; |
| 57 | + } |
| 58 | + result.copy(output); |
| 59 | + return result.length; |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function signRsaSha256Hook(key, input, output) { |
| 64 | + let result; |
| 65 | + try { |
| 66 | + const signer = crypto.createSign('sha256WithRSAEncryption'); |
| 67 | + const privateKey = Buffer.from( |
| 68 | + `-----BEGIN PRIVATE KEY-----\n${key.toString('base64')}\n-----END PRIVATE KEY-----\n` |
| 69 | + ); |
| 70 | + result = signer.update(input).end().sign(privateKey); |
| 71 | + } catch (e) { |
| 72 | + return e; |
| 73 | + } |
| 74 | + result.copy(output); |
| 75 | + return result.length; |
| 76 | +} |
| 77 | + |
| 78 | +const aes256CbcEncryptHook = makeAES256Hook('createCipheriv', 'aes-256-cbc'); |
| 79 | +const aes256CbcDecryptHook = makeAES256Hook('createDecipheriv', 'aes-256-cbc'); |
| 80 | +const aes256CtrEncryptHook = makeAES256Hook('createCipheriv', 'aes-256-ctr'); |
| 81 | +const aes256CtrDecryptHook = makeAES256Hook('createDecipheriv', 'aes-256-ctr'); |
| 82 | +const hmacSha512Hook = makeHmacHook('sha512'); |
| 83 | +const hmacSha256Hook = makeHmacHook('sha256'); |
| 84 | + |
| 85 | +export const cryptoCallbacks = { |
| 86 | + randomHook, |
| 87 | + sha256Hook, |
| 88 | + signRsaSha256Hook, |
| 89 | + aes256CbcEncryptHook, |
| 90 | + aes256CbcDecryptHook, |
| 91 | + aes256CtrEncryptHook, |
| 92 | + aes256CtrDecryptHook, |
| 93 | + hmacSha512Hook, |
| 94 | + hmacSha256Hook |
| 95 | +}; |
| 96 | + |
10 | 97 | const NEED_MONGO_KEYS = 3;
|
11 | 98 | const READY = 5;
|
12 | 99 | const ERROR = 0;
|
@@ -58,14 +145,6 @@ function createEncryptedDocument(mongoCrypt: MongoCrypt) {
|
58 | 145 | }
|
59 | 146 |
|
60 | 147 | describe('Crypto hooks', () => {
|
61 |
| - let cryptoCallbacks; |
62 |
| - |
63 |
| - before(async () => { |
64 |
| - const module = path.join(__dirname, 'benchmarks', 'crypto_callbacks.mjs'); |
65 |
| - const { cryptoCallbacks: c } = await eval(`import(${JSON.stringify(module)})`); |
66 |
| - cryptoCallbacks = c; |
67 |
| - }); |
68 |
| - |
69 | 148 | describe('when openssl 3 available', () => {
|
70 | 149 | beforeEach('check ssl version', function () {
|
71 | 150 | const openssl = semver.coerce(process.versions.openssl);
|
|
0 commit comments