Skip to content

createPfx ​

createPfx ​

Builds a PKCS#12/PFX archive containing certificates and/or private keys.

When encryption is provided, the key-bag ContentInfo is PBES2-encrypted. When mac is provided, a PKCS#12 MAC integrity block is appended.

Returns a CreatePfxResult: the container material on success, or a typed invalid_certificate failure when a certificate source is not a single PEM/DER certificate.

When mac is provided, mac.type selects the RFC 7292 MAC (the default) or RFC 9879 PBMAC1.

ts
function createPfx(
	input: CreatePfxInput,
): Promise<CreatePfxResult>

Parameters

Throws

  • ResultError — with a CreatePkcs12MacDataErrorCode when mac.iterations is out of range or mac.password cannot be encoded for the selected MAC, and with PfxEncoderErrorCode invalid_friendly_name when a bag friendlyName is not a BMPString of 1 to 255 characters.

Examples

ts
import { createPfx, unwrap } from 'micro509';

const result = await createPfx({
  certificates: [{ certificate: certPem }],
  privateKeys: [{ privateKey: keyPair.privateKey }],
  encryption: { password: 's3cret' },
  mac: { password: 's3cret' },
});
if (result.ok) {
  const pfx = result.value; // pfx.der, pfx.pem, pfx.base64
}
// or, when inputs are already validated: const pfx = unwrap(result);

Released under the MIT License.