{"version":3,"file":"pkcs12.mjs","names":["forge","SecurityCertificateX509","SecurityKeyPrivateRsa","SecurityKeystore","SecurityKeystorePkcs12","_certificate","_privateKey","constructor","reset","getCertificate","r","Error","getPrivateKey","decode","data","password","asn1","fromDer","util","ByteStringBuffer","p12","pkcs12","pkcs12FromAsn1","certificates","privateKeys","safeContent","safeContents","safeBag","safeBags","type","pki","oids","certBag","cert","push","pkcs8ShroudedKeyBag","key","length","certificate","_createCertificateX509","certificateToPem","privateKey","_createSecurityKeyPrivateRsa","privateKeyToPem","T","prototype"],"sources":["../../../src/security/keystore/pkcs12.ts"],"sourcesContent":["import forge from 'node-forge';\n\nimport {SecurityCertificateX509} from '../certificate/x509.ts';\nimport {SecurityKeyPrivateRsa} from '../key/private/rsa.ts';\nimport {SecurityKeystore} from '../keystore.ts';\n\n/**\n * SecurityKeystorePkcs12 object.\n */\nexport class SecurityKeystorePkcs12 extends SecurityKeystore {\n\t/**\n\t * Certificate.\n\t */\n\tprotected _certificate: SecurityCertificateX509 | null = null;\n\n\t/**\n\t * Private key.\n\t */\n\tprotected _privateKey: SecurityKeyPrivateRsa | null = null;\n\n\t/**\n\t * SecurityKeystorePkcs12 constructor.\n\t */\n\tconstructor() {\n\t\tsuper();\n\t}\n\n\t/**\n\t * Reset the internal state.\n\t */\n\tpublic reset() {\n\t\tthis._certificate = null;\n\t\tthis._privateKey = null;\n\t}\n\n\t/**\n\t * Get certificate or throw if none.\n\t *\n\t * @returns Certificate instance.\n\t */\n\tpublic getCertificate() {\n\t\tconst r = this._certificate;\n\t\tif (!r) {\n\t\t\tthrow new Error('No certificate');\n\t\t}\n\t\treturn r;\n\t}\n\n\t/**\n\t * Get private key or throw if none.\n\t *\n\t * @returns Private key instance.\n\t */\n\tpublic getPrivateKey() {\n\t\tconst r = this._privateKey;\n\t\tif (!r) {\n\t\t\tthrow new Error('No private key');\n\t\t}\n\t\treturn r;\n\t}\n\n\t/**\n\t * Decode from file data.\n\t *\n\t * @param data File data.\n\t * @param password The password if necessary.\n\t */\n\tpublic decode(data: Readonly<Uint8Array>, password: string | null = null) {\n\t\tthis.reset();\n\n\t\tconst asn1 = forge.asn1.fromDer(\n\t\t\tnew forge.util.ByteStringBuffer(data as Uint8Array<ArrayBuffer>)\n\t\t);\n\t\tconst p12 = password\n\t\t\t? forge.pkcs12.pkcs12FromAsn1(asn1, true, password)\n\t\t\t: forge.pkcs12.pkcs12FromAsn1(asn1, true);\n\n\t\tconst certificates: forge.pki.Certificate[] = [];\n\t\tconst privateKeys: forge.pki.PrivateKey[] = [];\n\t\tfor (const safeContent of p12.safeContents) {\n\t\t\tfor (const safeBag of safeContent.safeBags) {\n\t\t\t\tswitch (safeBag.type) {\n\t\t\t\t\tcase forge.pki.oids.certBag: {\n\t\t\t\t\t\tconst {cert} = safeBag;\n\t\t\t\t\t\tif (!cert) {\n\t\t\t\t\t\t\tthrow new Error('Internal error');\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcertificates.push(cert);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tcase forge.pki.oids.pkcs8ShroudedKeyBag: {\n\t\t\t\t\t\tconst {key} = safeBag;\n\t\t\t\t\t\tif (!key) {\n\t\t\t\t\t\t\tthrow new Error('Internal error');\n\t\t\t\t\t\t}\n\t\t\t\t\t\tprivateKeys.push(key);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\t// Do nothing.\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (certificates.length > 1) {\n\t\t\tthrow new Error(\n\t\t\t\t`Found multiple certificates: ${certificates.length}`\n\t\t\t);\n\t\t}\n\t\tif (privateKeys.length > 1) {\n\t\t\tthrow new Error(\n\t\t\t\t`Found multiple private keys: ${privateKeys.length}`\n\t\t\t);\n\t\t}\n\n\t\tconst certificate = certificates.length\n\t\t\t? this._createCertificateX509(\n\t\t\t\t\tforge.pki.certificateToPem(certificates[0])\n\t\t\t\t)\n\t\t\t: null;\n\n\t\tconst privateKey = privateKeys.length\n\t\t\t? this._createSecurityKeyPrivateRsa(\n\t\t\t\t\tforge.pki.privateKeyToPem(privateKeys[0])\n\t\t\t\t)\n\t\t\t: null;\n\n\t\tthis._certificate = certificate;\n\t\tthis._privateKey = privateKey;\n\t}\n\n\t/**\n\t * Create CertificateX509.\n\t *\n\t * @param certificate X509 certificate in PEM format.\n\t * @returns New CertificateX509.\n\t */\n\tprotected _createCertificateX509(certificate: string) {\n\t\treturn new SecurityCertificateX509(certificate);\n\t}\n\n\t/**\n\t * Create KeyPrivateRsa.\n\t *\n\t * @param privateKey RSA private key in PEM format.\n\t * @returns New KeyPrivateRsa.\n\t */\n\tprotected _createSecurityKeyPrivateRsa(privateKey: string) {\n\t\treturn new SecurityKeyPrivateRsa(privateKey);\n\t}\n\n\t/**\n\t * Create from file data.\n\t *\n\t * @param data File data.\n\t * @param password The password if necessary.\n\t * @returns New instance.\n\t */\n\tpublic static decode(\n\t\tdata: Readonly<Uint8Array>,\n\t\tpassword: string | null = null\n\t) {\n\t\tconst T = this.prototype.constructor as typeof SecurityKeystorePkcs12;\n\t\tconst r = new T();\n\t\tr.decode(data, password);\n\t\treturn r;\n\t}\n}\n"],"mappings":"AAAA,OAAOA,KAAK,MAAM,YAAY;AAE9B,SAAQC,uBAAuB,QAAO,yBAAwB;AAC9D,SAAQC,qBAAqB,QAAO,wBAAuB;AAC3D,SAAQC,gBAAgB,QAAO,iBAAgB;;AAE/C;AACA;AACA;AACA,OAAO,MAAMC,sBAAsB,SAASD,gBAAgB,CAAC;EAC5D;AACD;AACA;EACWE,YAAY,GAAmC,IAAI;;EAE7D;AACD;AACA;EACWC,WAAW,GAAiC,IAAI;;EAE1D;AACD;AACA;EACCC,WAAWA,CAAA,EAAG;IACb,KAAK,CAAC,CAAC;EACR;;EAEA;AACD;AACA;EACQC,KAAKA,CAAA,EAAG;IACd,IAAI,CAACH,YAAY,GAAG,IAAI;IACxB,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