Skip to content

Commit 3981853

Browse files
pimterryaduh95
authored andcommitted
crypto: return a clearer error when loading an unsupported pkcs12
PR-URL: #54485 Reviewed-By: Luigi Pinca <[email protected]>
1 parent ee89c31 commit 3981853

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/crypto/crypto_context.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,16 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
11481148
if (!ret) {
11491149
// TODO(@jasnell): Should this use ThrowCryptoError?
11501150
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
1151+
1152+
#if OPENSSL_VERSION_MAJOR >= 3
1153+
if (ERR_GET_REASON(err) == ERR_R_UNSUPPORTED) {
1154+
// OpenSSL's "unsupported" error without any context is very
1155+
// common and not very helpful, so we override it:
1156+
return THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(
1157+
env, "Unsupported PKCS12 PFX data");
1158+
}
1159+
#endif
1160+
11511161
const char* str = ERR_reason_error_string(err);
11521162
str = str != nullptr ? str : "Unknown error";
11531163

test/fixtures/keys/legacy.pfx

1.03 KB
Binary file not shown.

test/parallel/test-tls-legacy-pfx.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
if (!common.hasOpenSSL3)
6+
common.skip('OpenSSL legacy failures are only testable with OpenSSL 3+');
7+
8+
const fixtures = require('../common/fixtures');
9+
10+
const {
11+
assert, connect, keys
12+
} = require(fixtures.path('tls-connect'));
13+
14+
const legacyPfx = fixtures.readKey('legacy.pfx');
15+
16+
connect({
17+
client: {
18+
pfx: legacyPfx,
19+
passphrase: 'legacy',
20+
rejectUnauthorized: false
21+
},
22+
server: keys.agent1
23+
}, common.mustCall((e, pair, cleanup) => {
24+
assert.strictEqual(e.code, 'ERR_CRYPTO_UNSUPPORTED_OPERATION');
25+
assert.strictEqual(e.message, 'Unsupported PKCS12 PFX data');
26+
cleanup();
27+
}));

0 commit comments

Comments
 (0)