UNPKG

@shockpkg/ria-packager

Version:

Package for creating Adobe AIR packages

157 lines (147 loc) 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SecurityKeystorePkcs12 = void 0; var _nodeForge = require("node-forge"); var _x = require("../certificate/x509.js"); var _rsa = require("../key/private/rsa.js"); var _keystore = require("../keystore.js"); /** * SecurityKeystorePkcs12 object. */ class SecurityKeystorePkcs12 extends _keystore.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 = _nodeForge.asn1.fromDer(new _nodeForge.util.ByteStringBuffer(data)); const p12 = password ? _nodeForge.pkcs12.pkcs12FromAsn1(asn1, true, password) : _nodeForge.pkcs12.pkcs12FromAsn1(asn1, true); const certificates = []; const privateKeys = []; for (const safeContent of p12.safeContents) { for (const safeBag of safeContent.safeBags) { switch (safeBag.type) { case _nodeForge.pki.oids.certBag: { const { cert } = safeBag; if (!cert) { throw new Error('Internal error'); } certificates.push(cert); break; } case _nodeForge.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(_nodeForge.pki.certificateToPem(certificates[0])) : null; const privateKey = privateKeys.length ? this._createSecurityKeyPrivateRsa(_nodeForge.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 _x.SecurityCertificateX509(certificate); } /** * Create KeyPrivateRsa. * * @param privateKey RSA private key in PEM format. * @returns New KeyPrivateRsa. */ _createSecurityKeyPrivateRsa(privateKey) { return new _rsa.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; } } exports.SecurityKeystorePkcs12 = SecurityKeystorePkcs12; //# sourceMappingURL=pkcs12.js.map