|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +const { isBoringSSL } = require('../common/crypto'); |
| 8 | +if (isBoringSSL) |
| 9 | + common.skip('this test requires OpenSSL provider support'); |
| 10 | + |
| 11 | +const { isMainThread } = require('worker_threads'); |
| 12 | +if (!isMainThread) |
| 13 | + common.skip('crypto.setFips() is not supported in workers'); |
| 14 | + |
| 15 | +const assert = require('assert'); |
| 16 | +const crypto = require('crypto'); |
| 17 | + |
| 18 | +// createHmac(), createSign(), createVerify() and the synchronous one-shot |
| 19 | +// functions cache digests and HMAC contexts per Environment. Changing the FIPS |
| 20 | +// state must invalidate those caches without breaking objects that already |
| 21 | +// hold a context. |
| 22 | + |
| 23 | +const key = Buffer.from('Jefe'); |
| 24 | +const data = 'what do ya want for nothing?'; |
| 25 | +// RFC 4231, test case 2. |
| 26 | +const expectedSha256 = |
| 27 | + '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843'; |
| 28 | + |
| 29 | +const { privateKey, publicKey } = |
| 30 | + crypto.generateKeyPairSync('rsa', { modulusLength: 2048 }); |
| 31 | + |
| 32 | +const operations = { |
| 33 | + createHmac: (hash) => crypto.createHmac(hash, key).update(data).digest('hex'), |
| 34 | + createSign: (hash) => |
| 35 | + crypto.createSign(hash).update(data).sign(privateKey, 'hex'), |
| 36 | + createVerify: (hash) => { |
| 37 | + const signature = crypto.sign(hash, Buffer.from(data), privateKey); |
| 38 | + return crypto.createVerify(hash).update(data).verify(publicKey, signature); |
| 39 | + }, |
| 40 | + sign: (hash) => crypto.sign(hash, Buffer.from(data), privateKey).toString('hex'), |
| 41 | + verify: (hash) => { |
| 42 | + const signature = crypto.sign(hash, Buffer.from(data), privateKey); |
| 43 | + return crypto.verify(hash, Buffer.from(data), publicKey, signature); |
| 44 | + }, |
| 45 | + hkdfSync: (hash) => |
| 46 | + Buffer.from(crypto.hkdfSync(hash, key, 'salt', 'info', 32)).toString('hex'), |
| 47 | + pbkdf2Sync: (hash) => |
| 48 | + crypto.pbkdf2Sync(key, 'salt', 1, 32, hash).toString('hex'), |
| 49 | +}; |
| 50 | + |
| 51 | +function runAll(hash) { |
| 52 | + const results = {}; |
| 53 | + for (const [name, operation] of Object.entries(operations)) |
| 54 | + results[name] = operation(hash); |
| 55 | + return results; |
| 56 | +} |
| 57 | + |
| 58 | +const originalFips = crypto.getFips(); |
| 59 | +try { |
| 60 | + crypto.setFips(false); |
| 61 | +} catch (err) { |
| 62 | + if (err.code !== 'ERR_CRYPTO_FIPS_FORCED') throw err; |
| 63 | + common.skip('FIPS mode cannot be disabled'); |
| 64 | +} |
| 65 | + |
| 66 | +try { |
| 67 | + // Warm the caches with a digest that FIPS mode never allows. |
| 68 | + const md5 = runAll('md5'); |
| 69 | + assert.strictEqual(md5.createVerify, true); |
| 70 | + assert.strictEqual(md5.verify, true); |
| 71 | + assert.strictEqual(runAll('sha256').createHmac, expectedSha256); |
| 72 | + |
| 73 | + const live = crypto.createHmac('sha256', key).update(data.slice(0, 10)); |
| 74 | + |
| 75 | + crypto.setFips(true); |
| 76 | + |
| 77 | + // New operations follow the current policy instead of reusing cached |
| 78 | + // implementations. |
| 79 | + for (const [name, operation] of Object.entries(operations)) |
| 80 | + assert.throws(() => operation('md5'), Error, name); |
| 81 | + |
| 82 | + let fipsSha256; |
| 83 | + try { |
| 84 | + fipsSha256 = crypto.createHash('sha256').update(data).digest('hex'); |
| 85 | + } catch { |
| 86 | + // No FIPS provider is available, so nothing can use SHA-256 either. |
| 87 | + } |
| 88 | + if (fipsSha256 === undefined) { |
| 89 | + assert.throws(() => operations.createHmac('sha256'), Error); |
| 90 | + } else { |
| 91 | + assert.strictEqual(operations.createHmac('sha256'), expectedSha256); |
| 92 | + } |
| 93 | + |
| 94 | + // An HMAC that already has its context is not affected. |
| 95 | + assert.strictEqual(live.update(data.slice(10)).digest('hex'), |
| 96 | + expectedSha256); |
| 97 | + |
| 98 | + crypto.setFips(false); |
| 99 | + assert.deepStrictEqual(runAll('md5'), md5); |
| 100 | + assert.strictEqual(operations.createHmac('sha256'), expectedSha256); |
| 101 | +} finally { |
| 102 | + crypto.setFips(originalFips); |
| 103 | +} |
0 commit comments