Skip to content

Commit d490ef4

Browse files
committed
test: accept decoder errors for wrong FIPS passphrases
Wrong-passphrase CBC decryption can occasionally produce valid padding. OpenSSL then parses the invalid plaintext and reports ASN.1 or decoder errors instead of bad decrypt. Accept these equivalent failures for FIPS-generated keys while keeping strict checks for fixed non-FIPS fixtures. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com>
1 parent c18fd90 commit d490ef4

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

test/parallel/test-crypto-rsa-dsa.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@ const {
1616
const fips3 = hasFIPS(3);
1717
const fips35 = hasFIPS(3, 5);
1818
const fips30 = fips3 && !fips35;
19-
const fips4 = hasFIPS(4);
2019
const fipsDigestErrorCode = 'ERR_OSSL_DIGEST_NOT_ALLOWED';
2120
const wrongPassphrase = 'wrong-password';
21+
const wrongPassphraseError =
22+
/bad decrypt|DECRYPTION_FAILED|BAD_DECRYPT|bad password|DECODE[ _]ERROR/i;
23+
24+
// A wrong passphrase usually fails during cipher finalization, but CBC output
25+
// can have valid padding by chance. OpenSSL then parses the bad plaintext and
26+
// may report ASN.1 or decoder errors from the same failed key import.
27+
function isWrongPassphraseError(err) {
28+
return err.code === 'ERR_OSSL_BAD_DECRYPT' ||
29+
wrongPassphraseError.test(err.message) ||
30+
err.code?.startsWith('ERR_OSSL_ASN1_') ||
31+
err.code === 'ERR_OSSL_UNSUPPORTED';
32+
}
2233

2334
// Test certificates
2435
const certPem = fixtures.readKey('rsa_cert.crt');
@@ -71,8 +82,8 @@ const openssl1DecryptError = {
7182
library: 'digital envelope routines',
7283
};
7384

74-
const decryptError = fips4 ?
75-
{ code: 'ERR_OSSL_BAD_DECRYPT' } : hasOpenSSL(3) ?
85+
const decryptError = fips3 ?
86+
isWrongPassphraseError : hasOpenSSL(3) ?
7687
{ message: 'error:1C800064:Provider routines::bad decrypt' } :
7788
process.features.openssl_is_boringssl ? {
7889
message: 'error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT',
@@ -83,13 +94,12 @@ const decryptError = fips4 ?
8394
} :
8495
openssl1DecryptError;
8596

86-
const decryptPrivateKeyError = fips4 ? {
87-
code: 'ERR_OSSL_BAD_DECRYPT',
88-
} : hasOpenSSL(3) ? {
89-
message: 'error:1C800064:Provider routines::bad decrypt',
90-
} : process.features.openssl_is_boringssl ? {
91-
message: 'error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT',
92-
} : openssl1DecryptError;
97+
const decryptPrivateKeyError = fips3 ?
98+
isWrongPassphraseError : hasOpenSSL(3) ? {
99+
message: 'error:1C800064:Provider routines::bad decrypt',
100+
} : process.features.openssl_is_boringssl ? {
101+
message: 'error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT',
102+
} : openssl1DecryptError;
93103

94104
function getBufferCopy(buf) {
95105
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);

0 commit comments

Comments
 (0)