mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 22:40:57 +00:00

Since `common/crypto` already exists, it makes sense to keep crypto-related utilities there. The only exception being common.hasCrypto which is needed up front to determine if tests should be skipped. Eliminate the redundant check in hasFipsCrypto and just use crypto.getFips() directly where needed. PR-URL: https://github.com/nodejs/node/pull/56714 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
const assert = require('assert');
|
|
const {
|
|
generateKeyPair,
|
|
} = require('crypto');
|
|
const {
|
|
assertApproximateSize,
|
|
testSignVerify,
|
|
spkiExp,
|
|
} = require('../common/crypto');
|
|
|
|
const { hasOpenSSL3 } = require('../common/crypto');
|
|
|
|
// Test async DSA key generation.
|
|
{
|
|
const privateKeyEncoding = {
|
|
type: 'pkcs8',
|
|
format: 'der'
|
|
};
|
|
|
|
generateKeyPair('dsa', {
|
|
modulusLength: hasOpenSSL3 ? 2048 : 512,
|
|
divisorLength: 256,
|
|
publicKeyEncoding: {
|
|
type: 'spki',
|
|
format: 'pem'
|
|
},
|
|
privateKeyEncoding: {
|
|
cipher: 'aes-128-cbc',
|
|
passphrase: 'secret',
|
|
...privateKeyEncoding
|
|
}
|
|
}, common.mustSucceed((publicKey, privateKeyDER) => {
|
|
assert.strictEqual(typeof publicKey, 'string');
|
|
assert.match(publicKey, spkiExp);
|
|
// The private key is DER-encoded.
|
|
assert(Buffer.isBuffer(privateKeyDER));
|
|
|
|
assertApproximateSize(publicKey, hasOpenSSL3 ? 1194 : 440);
|
|
assertApproximateSize(privateKeyDER, hasOpenSSL3 ? 721 : 336);
|
|
|
|
// Since the private key is encrypted, signing shouldn't work anymore.
|
|
assert.throws(() => {
|
|
return testSignVerify(publicKey, {
|
|
key: privateKeyDER,
|
|
...privateKeyEncoding
|
|
});
|
|
}, {
|
|
name: 'TypeError',
|
|
code: 'ERR_MISSING_PASSPHRASE',
|
|
message: 'Passphrase required for encrypted key'
|
|
});
|
|
|
|
// Signing should work with the correct password.
|
|
testSignVerify(publicKey, {
|
|
key: privateKeyDER,
|
|
...privateKeyEncoding,
|
|
passphrase: 'secret'
|
|
});
|
|
}));
|
|
}
|