Skip to content

Commit a5984e4

Browse files
pimterrytargos
authored andcommittedOct 2, 2024
crypto: return a clearer error when loading an unsupported pkcs12
PR-URL: #54485 Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 2e3e177 commit a5984e4

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
 

‎src/crypto/crypto_context.cc

+10
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,16 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
11101110
if (!ret) {
11111111
// TODO(@jasnell): Should this use ThrowCryptoError?
11121112
unsigned long err = ERR_get_error(); // NOLINT(runtime/int)
1113+
1114+
#if OPENSSL_VERSION_MAJOR >= 3
1115+
if (ERR_GET_REASON(err) == ERR_R_UNSUPPORTED) {
1116+
// OpenSSL's "unsupported" error without any context is very
1117+
// common and not very helpful, so we override it:
1118+
return THROW_ERR_CRYPTO_UNSUPPORTED_OPERATION(
1119+
env, "Unsupported PKCS12 PFX data");
1120+
}
1121+
#endif
1122+
11131123
const char* str = ERR_reason_error_string(err);
11141124
str = str != nullptr ? str : "Unknown error";
11151125

‎test/fixtures/keys/legacy.pfx

1.03 KB
Binary file not shown.

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

+27
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)
Please sign in to comment.