crypto: add crypto.parsePKCS12() - #65627
Conversation
|
Review requested:
|
| // OpenSSL treats NULL and "" differently for both MAC verification and bag | ||
| // decryption, so callers must not collapse them. PKCS12_parse() verifies the | ||
| // MAC itself when one is present, and tolerates the NULL / "" ambiguity. | ||
| PKCS12ParseResult ParseBundle(const BIOPointer& bio, const char* pass) { |
There was a problem hiding this comment.
Could this direct parser be shared with SecureContext::LoadPKCS12() instead of maintaining two implementations of the same decoding? TLS can consume the shared parse result and then apply its stricter requirement that both a key and certificate are present.
There was a problem hiding this comment.
would you be open to a follow up PR to do this? I'd considered the same pathway you suggest here for DRYness sake, but opted to keep the scope smaller and reviewable especially as I am learning the codebase
There was a problem hiding this comment.
if you are open to it, i'll look :) just trying to guard everyone's time
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65627 +/- ##
==========================================
+ Coverage 90.07% 90.12% +0.05%
==========================================
Files 769 770 +1
Lines 261396 261613 +217
Branches 49629 49681 +52
==========================================
+ Hits 235451 235787 +336
+ Misses 16965 16840 -125
- Partials 8980 8986 +6
🚀 New features to boost your workflow:
|
2d6065c to
1bc0b49
Compare
This comment was marked as outdated.
This comment was marked as outdated.
dc7a151 to
dbcba0a
Compare
dbcba0a to
3a8e383
Compare
Return the private key, end-entity certificate, and any other non-matching certificates from a PKCS#12 (.p12/.pfx) bundle as a KeyObject and X509Certificate instances. Node.js already parses PKCS#12 in SecureContext::LoadPKCS12, which backs tls's `pfx` option, but the results are consumed directly into an SSL_CTX and never reach JavaScript. Callers who need the key or the certificates for anything other than an immediate TLS connection have to shell out to `openssl pkcs12` or take a userland dependency. The binding wraps d2i_PKCS12_bio() and PKCS12_parse() and follows their semantics, matching the existing TLS path: the first private key is returned, the end-entity certificate is the one associated with that key, and any remaining certificates are returned through `additionalCertificates`. A bundle containing no private key reports `certificate` as null and returns its certificates through `additionalCertificates`. Absent and empty passphrases are kept distinct, since OpenSSL treats them differently. Bundles that require OpenSSL's legacy provider throw ERR_CRYPTO_UNSUPPORTED_OPERATION, reusing the error added for the TLS path. Signed-off-by: bmuenzenmeyer <[email protected]>
3a8e383 to
06b6a3b
Compare
| if (!d2i_PKCS12_bio(bio.get(), &p12_ptr) || p12_ptr == nullptr) { | ||
| return PKCS12ParseResult(PKCS12ParseError::NOT_RECOGNIZED, | ||
| static_cast<int>(ERR_get_error())); | ||
| } |
There was a problem hiding this comment.
Doesn't need to be done now, but it probably makes sense to move this into ncrypto
There was a problem hiding this comment.
yeah i picked up on that from some of my earliest research (agent-informed). i am happy to explore the work, but it feels like a follow-up
Return the private key, end-entity certificate, and other certificates from a PKCS#12 (.p12/.pfx) bundle as a KeyObject and X509Certificate instances.
Reading a
.p12/.pfxbundle from JavaScript today means shelling out to theopenssl pkcs12CLI or taking a userland dependency such as node-forge. In talking to a colleague about this unfortunate missing method in core, I (with Claude) noticed Node.js already parses this internally.This PR exposes those internals to end users. Our use case is loading identity files supplied by our environment, to be forwarded during MCP tool calls. This allows us to use real identity instead of service account.
Note
This is my first significant contribution to core that touches the internals. I am still getting my bearings with regard to the module mechanics, bindings, and c++. I'm committed to shaping this, but learning.
SecureContext::LoadPKCS12has backed tls'spfxoption for years, but its results are loaded straight into anSSL_CTXand never reach JavaScript.Returns
{ privateKey: KeyObject|null, certificate: X509Certificate|null, additionalCertificates: X509Certificate[] }.