UNPKG

@shockpkg/ria-packager

Version:

Package for creating Adobe AIR packages

177 lines (143 loc) 3.9 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import fse from 'fs-extra'; import forge from 'node-forge'; import { SecurityCertificateX509 } from "../certificate/x509.mjs"; import { SecurityKeyPrivateRsa } from "../key/private/rsa.mjs"; import { SecurityKeystore } from "../keystore.mjs"; const forgeOidCertBag = forge.pki.oids.certBag; const forgeOidPkcs8ShroudedKeyBag = forge.pki.oids.pkcs8ShroudedKeyBag; /** * SecurityKeystorePkcs12 constructor. */ export class SecurityKeystorePkcs12 extends SecurityKeystore { /** * Certificate. */ /** * Private key. */ constructor() { super(); _defineProperty(this, "_certificate", null); _defineProperty(this, "_keyPrivate", null); } /** * Reset the internal state. */ reset() { this._certificate = null; this._keyPrivate = 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. */ getKeyPrivate() { const r = this._keyPrivate; if (!r) { throw new Error('No private key'); } return r; } /** * Read data from buffer. * * @param data File data. * @param password The password if necessary. */ readData(data, password = null) { this.reset(); const der = forge.util.decode64(data.toString('base64')); const asn1 = forge.asn1.fromDer(der); const p12 = password ? forge.pkcs12.pkcs12FromAsn1(asn1, true, password) : forge.pkcs12.pkcs12FromAsn1(asn1, true); const certificates = []; const keyPrivates = []; for (const safeContent of p12.safeContents) { for (const safeBag of safeContent.safeBags) { switch (safeBag.type) { case forgeOidCertBag: { const { cert } = safeBag; if (!cert) { throw new Error('Internal error'); } certificates.push(cert); break; } case forgeOidPkcs8ShroudedKeyBag: { const { key } = safeBag; if (!key) { throw new Error('Internal error'); } keyPrivates.push(key); break; } default: {// Do nothing. } } } } if (certificates.length > 1) { throw new Error(`Found multiple certificates: ${certificates.length}`); } if (keyPrivates.length > 1) { throw new Error(`Found multiple private keys: ${keyPrivates.length}`); } const certificate = certificates.length ? this._createCertificateX509(certificates[0]) : null; const keyPrivate = keyPrivates.length ? this._SecurityKeyPrivateRsa(keyPrivates[0]) : null; this._certificate = certificate; this._keyPrivate = keyPrivate; } /** * Read data from file. * * @param path File path. * @param password The password if necessary. */ async readFile(path, password = null) { const data = await fse.readFile(path); this.readData(data, password); } /** * Create CertificateX509. * * @param certificate Force certificate. * @returns New CertificateX509. */ _createCertificateX509(certificate) { const r = new SecurityCertificateX509(); r.readForgeCertificate(certificate); return r; } /** * Create KeyPrivateRsa. * * @param keyPrivate Force private key. * @returns New KeyPrivateRsa. */ _SecurityKeyPrivateRsa(keyPrivate) { const r = new SecurityKeyPrivateRsa(); r.readForgeKeyPrivate(keyPrivate); return r; } } //# sourceMappingURL=pkcs12.mjs.map