UNPKG

@shockpkg/ria-packager

Version:

Package for creating Adobe AIR packages

151 lines (141 loc) 3.71 kB
import forge from 'node-forge'; import { SecurityCertificateX509 } from "../certificate/x509.mjs"; import { SecurityKeyPrivateRsa } from "../key/private/rsa.mjs"; import { SecurityKeystore } from "../keystore.mjs"; /** * SecurityKeystorePkcs12 object. */ export class SecurityKeystorePkcs12 extends SecurityKeystore { /** * Certificate. */ _certificate = null; /** * Private key. */ _privateKey = null; /** * SecurityKeystorePkcs12 constructor. */ constructor() { super(); } /** * Reset the internal state. */ reset() { this._certificate = null; this._privateKey = null; } /** * Get certificate or throw if none. * * @returns Certificate instance. */ getCertificate() { const r = this._certificate; if (!r) { throw new Error('No certificate'); } return r; } /** * Get private key or throw if none. * * @returns Private key instance. */ getPrivateKey() { const r = this._privateKey; if (!r) { throw new Error('No private key'); } return r; } /** * Decode from file data. * * @param data File data. * @param password The password if necessary. */ decode(data, password = null) { this.reset(); const asn1 = forge.asn1.fromDer(new forge.util.ByteStringBuffer(data)); const p12 = password ? forge.pkcs12.pkcs12FromAsn1(asn1, true, password) : forge.pkcs12.pkcs12FromAsn1(asn1, true); const certificates = []; const privateKeys = []; for (const safeContent of p12.safeContents) { for (const safeBag of safeContent.safeBags) { switch (safeBag.type) { case forge.pki.oids.certBag: { const { cert } = safeBag; if (!cert) { throw new Error('Internal error'); } certificates.push(cert); break; } case forge.pki.oids.pkcs8ShroudedKeyBag: { const { key } = safeBag; if (!key) { throw new Error('Internal error'); } privateKeys.push(key); break; } default: { // Do nothing. } } } } if (certificates.length > 1) { throw new Error(`Found multiple certificates: ${certificates.length}`); } if (privateKeys.length > 1) { throw new Error(`Found multiple private keys: ${privateKeys.length}`); } const certificate = certificates.length ? this._createCertificateX509(forge.pki.certificateToPem(certificates[0])) : null; const privateKey = privateKeys.length ? this._createSecurityKeyPrivateRsa(forge.pki.privateKeyToPem(privateKeys[0])) : null; this._certificate = certificate; this._privateKey = privateKey; } /** * Create CertificateX509. * * @param certificate X509 certificate in PEM format. * @returns New CertificateX509. */ _createCertificateX509(certificate) { return new SecurityCertificateX509(certificate); } /** * Create KeyPrivateRsa. * * @param privateKey RSA private key in PEM format. * @returns New KeyPrivateRsa. */ _createSecurityKeyPrivateRsa(privateKey) { return new SecurityKeyPrivateRsa(privateKey); } /** * Create from file data. * * @param data File data. * @param password The password if necessary. * @returns New instance. */ static decode(data, password = null) { const T = this.prototype.constructor; const r = new T(); r.decode(data, password); return r; } } //# sourceMappingURL=pkcs12.mjs.map