Skip to content

Commit 46a0d0d

Browse files
authored
test,crypto: add and update empty passphrase regression tests
Refs: openssl/openssl#17507 Refs: #41428 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #42319 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 76d85a8 commit 46a0d0d

File tree

1 file changed

+54
-33
lines changed

1 file changed

+54
-33
lines changed

test/parallel/test-crypto-keygen.js

+54-33
Original file line numberDiff line numberDiff line change
@@ -1543,44 +1543,65 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
15431543
}
15441544
}
15451545

1546-
if (!common.hasOpenSSL3) {
1547-
// Passing an empty passphrase string should not cause OpenSSL's default
1548-
// passphrase prompt in the terminal.
1549-
// See https://github.com/nodejs/node/issues/35898.
1550-
1551-
for (const type of ['pkcs1', 'pkcs8']) {
1552-
generateKeyPair('rsa', {
1553-
modulusLength: 1024,
1554-
privateKeyEncoding: {
1555-
type,
1556-
format: 'pem',
1557-
cipher: 'aes-256-cbc',
1558-
passphrase: ''
1559-
}
1560-
}, common.mustSucceed((publicKey, privateKey) => {
1561-
assert.strictEqual(publicKey.type, 'public');
1546+
// Passing an empty passphrase string should not cause OpenSSL's default
1547+
// passphrase prompt in the terminal.
1548+
// See https://github.com/nodejs/node/issues/35898.
15621549

1563-
for (const passphrase of ['', Buffer.alloc(0)]) {
1564-
const privateKeyObject = createPrivateKey({
1565-
passphrase,
1566-
key: privateKey
1567-
});
1568-
assert.strictEqual(privateKeyObject.asymmetricKeyType, 'rsa');
1569-
}
1550+
for (const type of ['pkcs1', 'pkcs8']) {
1551+
generateKeyPair('rsa', {
1552+
modulusLength: 1024,
1553+
privateKeyEncoding: {
1554+
type,
1555+
format: 'pem',
1556+
cipher: 'aes-256-cbc',
1557+
passphrase: ''
1558+
}
1559+
}, common.mustSucceed((publicKey, privateKey) => {
1560+
assert.strictEqual(publicKey.type, 'public');
15701561

1571-
// Encrypting with an empty passphrase is not the same as not encrypting
1572-
// the key, and not specifying a passphrase should fail when decoding it.
1573-
assert.throws(() => {
1574-
return testSignVerify(publicKey, privateKey);
1575-
}, {
1576-
name: 'TypeError',
1577-
code: 'ERR_MISSING_PASSPHRASE',
1578-
message: 'Passphrase required for encrypted key'
1562+
for (const passphrase of ['', Buffer.alloc(0)]) {
1563+
const privateKeyObject = createPrivateKey({
1564+
passphrase,
1565+
key: privateKey
15791566
});
1580-
}));
1581-
}
1567+
assert.strictEqual(privateKeyObject.asymmetricKeyType, 'rsa');
1568+
}
1569+
1570+
// Encrypting with an empty passphrase is not the same as not encrypting
1571+
// the key, and not specifying a passphrase should fail when decoding it.
1572+
assert.throws(() => {
1573+
return testSignVerify(publicKey, privateKey);
1574+
}, common.hasOpenSSL3 ? {
1575+
name: 'Error',
1576+
code: 'ERR_OSSL_CRYPTO_INTERRUPTED_OR_CANCELLED',
1577+
message: 'error:07880109:common libcrypto routines::interrupted or cancelled'
1578+
} : {
1579+
name: 'TypeError',
1580+
code: 'ERR_MISSING_PASSPHRASE',
1581+
message: 'Passphrase required for encrypted key'
1582+
});
1583+
}));
15821584
}
15831585

1586+
// Passing an empty passphrase string should not throw ERR_OSSL_CRYPTO_MALLOC_FAILURE even on OpenSSL 3.
1587+
// Regression test for https://github.com/nodejs/node/issues/41428.
1588+
generateKeyPair('rsa', {
1589+
modulusLength: 4096,
1590+
publicKeyEncoding: {
1591+
type: 'spki',
1592+
format: 'pem'
1593+
},
1594+
privateKeyEncoding: {
1595+
type: 'pkcs8',
1596+
format: 'pem',
1597+
cipher: 'aes-256-cbc',
1598+
passphrase: ''
1599+
}
1600+
}, common.mustSucceed((publicKey, privateKey) => {
1601+
assert.strictEqual(typeof publicKey, 'string');
1602+
assert.strictEqual(typeof privateKey, 'string');
1603+
}));
1604+
15841605
{
15851606
// Proprietary Web Cryptography API ECDH/ECDSA namedCurve parameters
15861607
// should not be recognized in this API.

0 commit comments

Comments
 (0)