crypto: add crypto.parsePKCS12() by bmuenzenmeyer · Pull Request #65627 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions doc/api/crypto.md
2 changes: 2 additions & 0 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const {
createSecretKey,
createPublicKey,
createPrivateKey,
parsePKCS12,
KeyObject,
} = require('internal/crypto/keys');
const {
Expand Down Expand Up @@ -216,6 +217,7 @@ module.exports = {
getMacs,
hkdf,
hkdfSync,
parsePKCS12,
pbkdf2,
pbkdf2Sync,
generateKeyPair,
Expand Down
64 changes: 64 additions & 0 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const {
ArrayPrototypeMap,
ArrayPrototypeSlice,
ObjectDefineProperties,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
StringPrototypeIncludes,
StringPrototypeStartsWith,
SymbolToStringTag,
TypedArrayPrototypeIncludes,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -35,6 +37,7 @@ const {
kKeyEncodingPKCS8,
kKeyEncodingSPKI,
kKeyEncodingSEC1,
parsePKCS12: _parsePKCS12,
} = internalBinding('crypto');

const {
Expand Down Expand Up @@ -63,6 +66,7 @@ const {

const {
getArrayBufferOrView,
getBufferSourceBytes,
bigIntArrayToUnsignedBigInt,
normalizeAlgorithm,
hasAnyNotIn,
Expand Down Expand Up @@ -784,6 +788,65 @@ function createPublicKey(key) {
return new PublicKeyObject(handle);
}

/**
* Parses a PKCS#12 (.p12 / .pfx) bundle. Returns an object holding the first
* private key as `privateKey`, the certificate associated with it as
* `certificate`, and every other certificate in the bundle as an array in
* `additionalCertificates`. `privateKey` and `certificate` are null when the
* bundle contains none.
* @param {ArrayBuffer|Buffer|TypedArray|DataView} bundle
* @param {object} [options]
* @returns {object}
*/
function parsePKCS12(bundle, options = kEmptyObject) {
if (!isArrayBufferView(bundle) && !isAnyArrayBuffer(bundle)) {
throw new ERR_INVALID_ARG_TYPE(
'bundle',
['ArrayBuffer', 'TypedArray', 'DataView', 'Buffer'],
bundle);
}

validateObject(options, 'options');
const { passphrase } = options;

// `undefined` means no passphrase, '' a zero-length one. OpenSSL accepts
// either PKCS#12 password encoding for both, so they behave the same.
let passBuf;
if (passphrase !== undefined) {
passBuf = getArrayBufferOrView(passphrase, 'options.passphrase', 'utf8');
// OpenSSL takes the passphrase as a NUL-terminated C string, so one
// containing a NUL byte would be truncated there and the bundle opened
// with only the bytes before it. A PKCS#12 password cannot represent an
// embedded NUL anyway, so reject it outright.
if (TypedArrayPrototypeIncludes(getBufferSourceBytes(passBuf), 0)) {
throw new ERR_INVALID_ARG_VALUE(
'options.passphrase', passphrase, 'must not contain null bytes');
}
// The binding reads the passphrase as a view; wrap a bare ArrayBuffer.
if (isAnyArrayBuffer(passBuf)) passBuf = Buffer.from(passBuf);
}

// Likewise, the binding reads the bundle as a view.
const bundleBuf = isAnyArrayBuffer(bundle) ? Buffer.from(bundle) : bundle;

const {
0: keyHandle,
1: certHandle,
2: otherHandles,
} = _parsePKCS12(bundleBuf, passBuf);

// Required lazily: internal/crypto/x509 depends on this module.
const { InternalX509Certificate } = require('internal/crypto/x509');

return {
privateKey: keyHandle === null ? null : new PrivateKeyObject(keyHandle),
certificate:
certHandle === null ? null : new InternalX509Certificate(certHandle),
additionalCertificates:
ArrayPrototypeMap(otherHandles, (h) => new InternalX509Certificate(h)),
};
}

/**
* Converts a secret KeyObjectHandle to a CryptoKey by dispatching to the
* algorithm-specific Web Crypto import path.
Expand Down Expand Up @@ -1433,6 +1496,7 @@ module.exports = {
createSecretKey,
createPublicKey,
createPrivateKey,
parsePKCS12,
KeyObject,
CryptoKey,
InternalCryptoKey,
Expand Down
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
'src/crypto/crypto_hash.cc',
'src/crypto/crypto_keys.cc',
'src/crypto/crypto_keygen.cc',
'src/crypto/crypto_pkcs12.cc',
'src/crypto/crypto_scrypt.cc',
'src/crypto/crypto_tls.cc',
'src/crypto/crypto_x509.cc',
Expand All @@ -432,6 +433,7 @@
'src/crypto/crypto_hash.h',
'src/crypto/crypto_keys.h',
'src/crypto/crypto_keygen.h',
'src/crypto/crypto_pkcs12.h',
'src/crypto/crypto_scrypt.h',
'src/crypto/crypto_tls.h',
'src/crypto/crypto_context.h',
Expand Down
103 changes: 44 additions & 59 deletions src/crypto/crypto_context.cc
Loading
Loading