Skip to content

Commit 01cd887

Browse files
authored
chore: surface wrong-passphrase client cert PEM errors consistently in OpenSSL 3 (#33893)
When `clientCertificates` was configured with an encrypted PEM key and the wrong passphrase, the underlying `createPrivateKey` call could surface either `ERR_OSSL_BAD_DECRYPT` or `error:1E08010C:DECODER routines::unsupported` depending on what bytes fell out of the failed decryption (the latter happens when the decrypted gibberish looks like a partial DER blob the decoder can't classify). Only the former was translated into the friendly "Cannot decrypt PEM key with supplied passphrase" message; the latter leaked through as a generic "Cannot parse PEM key" error. Also treat the DECODER-unsupported error as a passphrase failure, but only when the key file actually looks like an encrypted PEM, so a genuinely malformed key with a passphrase still surfaces "Cannot parse PEM key". This also de-flakes the EC and RSA invalid-passphrase unit tests (the RSA one had been `it.skip`'d for this reason and is re-enabled here).
1 parent 4534d04 commit 01cd887

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎packages/network/lib/client-certificates.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,17 @@ export function loadClientCertificateConfig (config: Config) {
274274
try {
275275
createPrivateKey({ key: pemKeyRaw, passphrase })
276276
} catch (error: any) {
277-
if (passphrase && error.code === 'ERR_OSSL_BAD_DECRYPT') {
277+
// OpenSSL 3 may surface a wrong passphrase as either ERR_OSSL_BAD_DECRYPT (cipher
278+
// padding/MAC check fails) or "DECODER routines::unsupported" (decrypted bytes are
279+
// garbage the DER decoder can't classify). Which one fires is data-dependent, so
280+
// also treat the DECODER error as a passphrase failure — but only when the key
281+
// actually looks like an encrypted PEM, to avoid masking a genuinely malformed key.
282+
const isBadDecrypt = error.code === 'ERR_OSSL_BAD_DECRYPT'
283+
|| (typeof error.message === 'string'
284+
&& error.message.includes('DECODER routines::unsupported')
285+
&& isEncryptedPemKey(pemKeyRaw))
286+
287+
if (passphrase && isBadDecrypt) {
278288
throw new Error(
279289
`Cannot decrypt PEM key with supplied passphrase (check the passphrase file content and that it doesn't have unexpected whitespace at the end)`,
280290
)
@@ -346,6 +356,15 @@ function loadBinaryFromFile (filepath: string): Buffer {
346356
return fs.readFileSync(filepath)
347357
}
348358

359+
function isEncryptedPemKey (pem: Buffer): boolean {
360+
const text = pem.toString('utf8')
361+
362+
// PKCS#8 encrypted (`-----BEGIN ENCRYPTED PRIVATE KEY-----`) or a traditional PEM that
363+
// declares `Proc-Type: 4,ENCRYPTED` in its headers.
364+
return /-----BEGIN ENCRYPTED PRIVATE KEY-----/.test(text)
365+
|| /Proc-Type:\s*4,ENCRYPTED/i.test(text)
366+
}
367+
349368
function loadTextFromFile (filepath: string): string {
350369
debug(`loadPassphraseFile: ${filepath}`)
351370

‎packages/network/test/unit/client_certificates.spec.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,7 @@ describe('lib/client-certificates', () => {
630630
)
631631
})
632632

633-
// TODO: fix this flaky test
634-
it.skip('detects invalid PEM key passphrase', () => {
633+
it('detects invalid PEM key passphrase', () => {
635634
const passphrase = 'a_phrase'
636635

637636
createPemFiles(

0 commit comments

Comments
 (0)