GeezerVison is showing github.com — test: move crypto related common utilities in common/crypto · nodejs/node@6edf04e
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/addons/openssl-providers/providers.cjs +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const common = require('../../common'); | 4 | | -if (!common.hasCrypto) | | 4 | +if (!common.hasCrypto) { | 5 | 5 | common.skip('missing crypto'); | | 6 | +} | | 7 | +const { hasOpenSSL3 } = require('../../common/crypto'); | 6 | 8 |
| 7 | | -if (!common.hasOpenSSL3) | | 9 | +if (!hasOpenSSL3) { | 8 | 10 | common.skip('this test requires OpenSSL 3.x'); | | 11 | +} | 9 | 12 | const assert = require('node:assert'); | 10 | 13 | const { createHash, getCiphers, getHashes } = require('node:crypto'); | 11 | 14 | const { debuglog } = require('node:util'); | |
Collapse file Copy file name to clipboardExpand all lines: test/benchmark/test-benchmark-crypto.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,8 +5,11 @@ const common = require('../common'); | 5 | 5 | if (!common.hasCrypto) | 6 | 6 | common.skip('missing crypto'); | 7 | 7 |
| 8 | | -if (common.hasFipsCrypto) | | 8 | +const { getFips } = require('crypto'); | | 9 | + | | 10 | +if (getFips()) { | 9 | 11 | common.skip('some benchmarks are FIPS-incompatible'); | | 12 | +} | 10 | 13 |
| 11 | 14 | const runBenchmark = require('../common/benchmark'); | 12 | 15 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/common/README.md -17Lines changed: 0 additions & 17 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -226,17 +226,6 @@ The TTY file descriptor is assumed to be capable of being writable. | 226 | 226 |
| 227 | 227 | Indicates whether OpenSSL is available. | 228 | 228 |
| 229 | | -### `hasFipsCrypto` | 230 | | - | 231 | | -* [\<boolean>][<boolean>] | 232 | | - | 233 | | -Indicates that Node.js has been linked with a FIPS compatible OpenSSL library, | 234 | | -and that FIPS as been enabled using `--enable-fips`. | 235 | | - | 236 | | -To only detect if the OpenSSL library is FIPS compatible, regardless if it has | 237 | | -been enabled or not, then `process.config.variables.openssl_is_fips` can be | 238 | | -used to determine that situation. | 239 | | - | 240 | 229 | ### `hasIntl` | 241 | 230 |
| 242 | 231 | * [\<boolean>][<boolean>] | @@ -417,12 +406,6 @@ Returns `true` if the exit code `exitCode` and/or signal name `signal` represent | 417 | 406 | the exit code and/or signal name of a node process that aborted, `false` | 418 | 407 | otherwise. | 419 | 408 |
| 420 | | -### `opensslCli` | 421 | | - | 422 | | -* [\<boolean>][<boolean>] | 423 | | - | 424 | | -Indicates whether 'opensslCli' is supported. | 425 | | - | 426 | 409 | ### `platformTimeout(ms)` | 427 | 410 |
| 428 | 411 | * `ms` [\<number>][<number>] | [\<bigint>][<bigint>] | |
Collapse file Copy file name to clipboardExpand all lines: test/common/crypto.js +51-1Lines changed: 51 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | 3 | const common = require('../common'); | 4 | | -if (!common.hasCrypto) | | 4 | +if (!common.hasCrypto) { | 5 | 5 | common.skip('missing crypto'); | | 6 | +} | 6 | 7 |
| 7 | 8 | const assert = require('assert'); | 8 | 9 | const crypto = require('crypto'); | @@ -98,6 +99,27 @@ const pkcs8EncExp = getRegExpForPEM('ENCRYPTED PRIVATE KEY'); | 98 | 99 | const sec1Exp = getRegExpForPEM('EC PRIVATE KEY'); | 99 | 100 | const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher); | 100 | 101 |
| | 102 | +// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL | | 103 | +const opensslVersionNumber = (major = 0, minor = 0, patch = 0) => { | | 104 | + assert(major >= 0 && major <= 0xf); | | 105 | + assert(minor >= 0 && minor <= 0xff); | | 106 | + assert(patch >= 0 && patch <= 0xff); | | 107 | + return (major << 28) | (minor << 20) | (patch << 4); | | 108 | +}; | | 109 | + | | 110 | +let OPENSSL_VERSION_NUMBER; | | 111 | +const hasOpenSSL = (major = 0, minor = 0, patch = 0) => { | | 112 | + if (!common.hasCrypto) return false; | | 113 | + if (OPENSSL_VERSION_NUMBER === undefined) { | | 114 | + const regexp = /(?<m>\d+)\.(?<n>\d+)\.(?<p>\d+)/; | | 115 | + const { m, n, p } = process.versions.openssl.match(regexp).groups; | | 116 | + OPENSSL_VERSION_NUMBER = opensslVersionNumber(m, n, p); | | 117 | + } | | 118 | + return OPENSSL_VERSION_NUMBER >= opensslVersionNumber(major, minor, patch); | | 119 | +}; | | 120 | + | | 121 | +let opensslCli = null; | | 122 | + | 101 | 123 | module.exports = { | 102 | 124 | modp2buf, | 103 | 125 | assertApproximateSize, | @@ -111,4 +133,32 @@ module.exports = { | 111 | 133 | pkcs8EncExp, // used once | 112 | 134 | sec1Exp, | 113 | 135 | sec1EncExp, | | 136 | + hasOpenSSL, | | 137 | + get hasOpenSSL3() { | | 138 | + return hasOpenSSL(3); | | 139 | + }, | | 140 | + // opensslCli defined lazily to reduce overhead of spawnSync | | 141 | + get opensslCli() { | | 142 | + if (opensslCli !== null) return opensslCli; | | 143 | + | | 144 | + if (process.config.variables.node_shared_openssl) { | | 145 | + // Use external command | | 146 | + opensslCli = 'openssl'; | | 147 | + } else { | | 148 | + const path = require('path'); | | 149 | + // Use command built from sources included in Node.js repository | | 150 | + opensslCli = path.join(path.dirname(process.execPath), 'openssl-cli'); | | 151 | + } | | 152 | + | | 153 | + if (exports.isWindows) opensslCli += '.exe'; | | 154 | + | | 155 | + const { spawnSync } = require('child_process'); | | 156 | + | | 157 | + const opensslCmd = spawnSync(opensslCli, ['version']); | | 158 | + if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) { | | 159 | + // OpenSSL command cannot be executed | | 160 | + opensslCli = false; | | 161 | + } | | 162 | + return opensslCli; | | 163 | + }, | 114 | 164 | }; |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.js -52Lines changed: 0 additions & 52 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | 21 | 21 |
| 22 | | -/* eslint-disable node-core/crypto-check */ | 23 | 22 | 'use strict'; | 24 | 23 | const process = global.process; // Some tests tamper with the process global. | 25 | 24 |
| @@ -57,25 +56,6 @@ const noop = () => {}; | 57 | 56 | const hasCrypto = Boolean(process.versions.openssl) && | 58 | 57 | !process.env.NODE_SKIP_CRYPTO; | 59 | 58 |
| 60 | | -// Synthesize OPENSSL_VERSION_NUMBER format with the layout 0xMNN00PPSL | 61 | | -const opensslVersionNumber = (major = 0, minor = 0, patch = 0) => { | 62 | | - assert(major >= 0 && major <= 0xf); | 63 | | - assert(minor >= 0 && minor <= 0xff); | 64 | | - assert(patch >= 0 && patch <= 0xff); | 65 | | - return (major << 28) | (minor << 20) | (patch << 4); | 66 | | -}; | 67 | | - | 68 | | -let OPENSSL_VERSION_NUMBER; | 69 | | -const hasOpenSSL = (major = 0, minor = 0, patch = 0) => { | 70 | | - if (!hasCrypto) return false; | 71 | | - if (OPENSSL_VERSION_NUMBER === undefined) { | 72 | | - const regexp = /(?<m>\d+)\.(?<n>\d+)\.(?<p>\d+)/; | 73 | | - const { m, n, p } = process.versions.openssl.match(regexp).groups; | 74 | | - OPENSSL_VERSION_NUMBER = opensslVersionNumber(m, n, p); | 75 | | - } | 76 | | - return OPENSSL_VERSION_NUMBER >= opensslVersionNumber(major, minor, patch); | 77 | | -}; | 78 | | - | 79 | 59 | const hasQuic = hasCrypto && !!process.config.variables.openssl_quic; | 80 | 60 |
| 81 | 61 | function parseTestFlags(filename = process.argv[1]) { | @@ -220,7 +200,6 @@ if (process.env.NODE_TEST_WITH_ASYNC_HOOKS) { | 220 | 200 | }).enable(); | 221 | 201 | } | 222 | 202 |
| 223 | | -let opensslCli = null; | 224 | 203 | let inFreeBSDJail = null; | 225 | 204 | let localhostIPv4 = null; | 226 | 205 |
| @@ -985,7 +964,6 @@ const common = { | 985 | 964 | getTTYfd, | 986 | 965 | hasIntl, | 987 | 966 | hasCrypto, | 988 | | - hasOpenSSL, | 989 | 967 | hasQuic, | 990 | 968 | hasMultiLocalhost, | 991 | 969 | invalidArgTypeHelper, | @@ -1027,10 +1005,6 @@ const common = { | 1027 | 1005 | return require('os').totalmem() > 0x70000000; /* 1.75 Gb */ | 1028 | 1006 | }, | 1029 | 1007 |
| 1030 | | - get hasFipsCrypto() { | 1031 | | - return hasCrypto && require('crypto').getFips(); | 1032 | | - }, | 1033 | | - | 1034 | 1008 | get hasIPv6() { | 1035 | 1009 | const iFaces = require('os').networkInterfaces(); | 1036 | 1010 | let re; | @@ -1047,10 +1021,6 @@ const common = { | 1047 | 1021 | }); | 1048 | 1022 | }, | 1049 | 1023 |
| 1050 | | - get hasOpenSSL3() { | 1051 | | - return hasOpenSSL(3); | 1052 | | - }, | 1053 | | - | 1054 | 1024 | get inFreeBSDJail() { | 1055 | 1025 | if (inFreeBSDJail !== null) return inFreeBSDJail; | 1056 | 1026 |
| @@ -1100,28 +1070,6 @@ const common = { | 1100 | 1070 | return localhostIPv4; | 1101 | 1071 | }, | 1102 | 1072 |
| 1103 | | - // opensslCli defined lazily to reduce overhead of spawnSync | 1104 | | - get opensslCli() { | 1105 | | - if (opensslCli !== null) return opensslCli; | 1106 | | - | 1107 | | - if (process.config.variables.node_shared_openssl) { | 1108 | | - // Use external command | 1109 | | - opensslCli = 'openssl'; | 1110 | | - } else { | 1111 | | - // Use command built from sources included in Node.js repository | 1112 | | - opensslCli = path.join(path.dirname(process.execPath), 'openssl-cli'); | 1113 | | - } | 1114 | | - | 1115 | | - if (exports.isWindows) opensslCli += '.exe'; | 1116 | | - | 1117 | | - const opensslCmd = spawnSync(opensslCli, ['version']); | 1118 | | - if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) { | 1119 | | - // OpenSSL command cannot be executed | 1120 | | - opensslCli = false; | 1121 | | - } | 1122 | | - return opensslCli; | 1123 | | - }, | 1124 | | - | 1125 | 1073 | get PORT() { | 1126 | 1074 | if (+process.env.TEST_PARALLEL) { | 1127 | 1075 | throw new Error('common.PORT cannot be used in a parallelized test'); | |
Collapse file Copy file name to clipboardExpand all lines: test/common/index.mjs -2Lines changed: 0 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,7 +41,6 @@ const { | 41 | 41 | mustNotMutateObjectDeep, | 42 | 42 | mustSucceed, | 43 | 43 | nodeProcessAborted, | 44 | | - opensslCli, | 45 | 44 | parseTestFlags, | 46 | 45 | PIPE, | 47 | 46 | platformTimeout, | @@ -97,7 +96,6 @@ export { | 97 | 96 | mustNotMutateObjectDeep, | 98 | 97 | mustSucceed, | 99 | 98 | nodeProcessAborted, | 100 | | - opensslCli, | 101 | 99 | parseTestFlags, | 102 | 100 | PIPE, | 103 | 101 | platformTimeout, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-cli-node-options.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ const { Worker } = require('worker_threads'); | 12 | 12 |
| 13 | 13 | const fixtures = require('../common/fixtures'); | 14 | 14 | const tmpdir = require('../common/tmpdir'); | | 15 | +const { hasOpenSSL3 } = require('../common/crypto'); | 15 | 16 | tmpdir.refresh(); | 16 | 17 |
| 17 | 18 | const printA = path.relative(tmpdir.path, fixtures.path('printA.js')); | @@ -64,7 +65,7 @@ if (common.isLinux) { | 64 | 65 | if (common.hasCrypto) { | 65 | 66 | expectNoWorker('--use-openssl-ca', 'B\n'); | 66 | 67 | expectNoWorker('--use-bundled-ca', 'B\n'); | 67 | | - if (!common.hasOpenSSL3) | | 68 | + if (!hasOpenSSL3) | 68 | 69 | expectNoWorker('--openssl-config=_ossl_cfg', 'B\n'); | 69 | 70 | } | 70 | 71 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-authenticated.js +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | // Flags: --no-warnings | 22 | 22 | 'use strict'; | 23 | 23 | const common = require('../common'); | 24 | | -if (!common.hasCrypto) | | 24 | +if (!common.hasCrypto) { | 25 | 25 | common.skip('missing crypto'); | | 26 | +} | 26 | 27 |
| 27 | 28 | const assert = require('assert'); | 28 | 29 | const crypto = require('crypto'); | 29 | 30 | const { inspect } = require('util'); | 30 | 31 | const fixtures = require('../common/fixtures'); | | 32 | +const { hasOpenSSL3 } = require('../common/crypto'); | | 33 | + | | 34 | +const isFipsEnabled = crypto.getFips(); | 31 | 35 |
| 32 | 36 | // | 33 | 37 | // Test authenticated encryption modes. | @@ -53,7 +57,7 @@ for (const test of TEST_CASES) { | 53 | 57 | continue; | 54 | 58 | } | 55 | 59 |
| 56 | | - if (common.hasFipsCrypto && test.iv.length < 24) { | | 60 | + if (isFipsEnabled && test.iv.length < 24) { | 57 | 61 | common.printSkipMessage('IV len < 12 bytes unsupported in FIPS mode'); | 58 | 62 | continue; | 59 | 63 | } | @@ -95,7 +99,7 @@ for (const test of TEST_CASES) { | 95 | 99 | } | 96 | 100 |
| 97 | 101 | { | 98 | | - if (isCCM && common.hasFipsCrypto) { | | 102 | + if (isCCM && isFipsEnabled) { | 99 | 103 | assert.throws(() => { | 100 | 104 | crypto.createDecipheriv(test.algo, | 101 | 105 | Buffer.from(test.key, 'hex'), | @@ -286,7 +290,7 @@ for (const test of TEST_CASES) { | 286 | 290 | }); | 287 | 291 | }, errMessages.authTagLength); | 288 | 292 |
| 289 | | - if (!common.hasFipsCrypto) { | | 293 | + if (!isFipsEnabled) { | 290 | 294 | assert.throws(() => { | 291 | 295 | crypto.createDecipheriv('aes-256-ccm', | 292 | 296 | 'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8', | @@ -312,7 +316,7 @@ for (const test of TEST_CASES) { | 312 | 316 | }); | 313 | 317 |
| 314 | 318 | // CCM decryption and create(De|C)ipher are unsupported in FIPS mode. | 315 | | - if (!common.hasFipsCrypto) { | | 319 | + if (!isFipsEnabled) { | 316 | 320 | assert.throws(() => { | 317 | 321 | crypto.createDecipheriv(`aes-256-${mode}`, | 318 | 322 | 'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8', | @@ -388,7 +392,7 @@ for (const test of TEST_CASES) { | 388 | 392 | cipher.setAAD(Buffer.from('0123456789', 'hex')); | 389 | 393 | }, /options\.plaintextLength required for CCM mode with AAD/); | 390 | 394 |
| 391 | | - if (!common.hasFipsCrypto) { | | 395 | + if (!isFipsEnabled) { | 392 | 396 | assert.throws(() => { | 393 | 397 | const cipher = crypto.createDecipheriv('aes-256-ccm', | 394 | 398 | 'FxLKsqdmv0E9xrQhp0b1ZgI0K7JFZJM8', | @@ -403,7 +407,7 @@ for (const test of TEST_CASES) { | 403 | 407 |
| 404 | 408 | // Test that final() throws in CCM mode when no authentication tag is provided. | 405 | 409 | { | 406 | | - if (!common.hasFipsCrypto) { | | 410 | + if (!isFipsEnabled) { | 407 | 411 | const key = Buffer.from('1ed2233fa2223ef5d7df08546049406c', 'hex'); | 408 | 412 | const iv = Buffer.from('7305220bca40d4c90e1791e9', 'hex'); | 409 | 413 | const ct = Buffer.from('8beba09d4d4d861f957d51c0794f4abf8030848e', 'hex'); | @@ -562,7 +566,7 @@ for (const test of TEST_CASES) { | 562 | 566 | ]) { | 563 | 567 | assert.throws(() => { | 564 | 568 | cipher.final(); | 565 | | - }, common.hasOpenSSL3 ? { | | 569 | + }, hasOpenSSL3 ? { | 566 | 570 | code: 'ERR_OSSL_TAG_NOT_SET' | 567 | 571 | } : { | 568 | 572 | message: /Unsupported state/ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-cipheriv-decipheriv.js +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,8 @@ if (!common.hasCrypto) | 5 | 5 |
| 6 | 6 | const assert = require('assert'); | 7 | 7 | const crypto = require('crypto'); | | 8 | +const { hasOpenSSL3 } = require('../common/crypto'); | | 9 | +const isFipsEnabled = crypto.getFips(); | 8 | 10 |
| 9 | 11 | function testCipher1(key, iv) { | 10 | 12 | // Test encryption and decryption with explicit key and iv | @@ -150,7 +152,7 @@ testCipher1(Buffer.from('0123456789abcd0123456789'), '12345678'); | 150 | 152 | testCipher1(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678')); | 151 | 153 | testCipher2(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678')); | 152 | 154 |
| 153 | | -if (!common.hasFipsCrypto) { | | 155 | +if (!isFipsEnabled) { | 154 | 156 | testCipher3(Buffer.from('000102030405060708090A0B0C0D0E0F', 'hex'), | 155 | 157 | Buffer.from('A6A6A6A6A6A6A6A6', 'hex')); | 156 | 158 | } | @@ -193,10 +195,10 @@ assert.throws( | 193 | 195 | errMessage); | 194 | 196 |
| 195 | 197 | // But all other IV lengths should be accepted. | 196 | | -const minIvLength = common.hasOpenSSL3 ? 8 : 1; | 197 | | -const maxIvLength = common.hasOpenSSL3 ? 64 : 256; | | 198 | +const minIvLength = hasOpenSSL3 ? 8 : 1; | | 199 | +const maxIvLength = hasOpenSSL3 ? 64 : 256; | 198 | 200 | for (let n = minIvLength; n < maxIvLength; n += 1) { | 199 | | - if (common.hasFipsCrypto && n < 12) continue; | | 201 | + if (isFipsEnabled && n < 12) continue; | 200 | 202 | crypto.createCipheriv('aes-128-gcm', Buffer.alloc(16), Buffer.alloc(n)); | 201 | 203 | } | 202 | 204 |
| |
|