pkcs12.js 22 KB

1
  1. var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.arrayIteratorImpl=function(b){var r=0;return function(){return r<b.length?{done:!1,value:b[r++]}:{done:!0}}};$jscomp.arrayIterator=function(b){return{next:$jscomp.arrayIteratorImpl(b)}};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(b,r,q){b!=Array.prototype&&b!=Object.prototype&&(b[r]=q.value)};$jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global&&null!=global?global:b};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.SymbolClass=function(b,r){this.$jscomp$symbol$id_=b;$jscomp.defineProperty(this,"description",{configurable:!0,writable:!0,value:r})};$jscomp.SymbolClass.prototype.toString=function(){return this.$jscomp$symbol$id_};$jscomp.Symbol=function(){function b(q){if(this instanceof b)throw new TypeError("Symbol is not a constructor");return new $jscomp.SymbolClass($jscomp.SYMBOL_PREFIX+(q||"")+"_"+r++,q)}var r=0;return b}();$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var b=$jscomp.global.Symbol.iterator;b||(b=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("Symbol.iterator"));"function"!=typeof Array.prototype[b]&&$jscomp.defineProperty(Array.prototype,b,{configurable:!0,writable:!0,value:function(){return $jscomp.iteratorPrototype($jscomp.arrayIteratorImpl(this))}});$jscomp.initSymbolIterator=function(){}};$jscomp.initSymbolAsyncIterator=function(){$jscomp.initSymbol();var b=$jscomp.global.Symbol.asyncIterator;b||(b=$jscomp.global.Symbol.asyncIterator=$jscomp.global.Symbol("Symbol.asyncIterator"));$jscomp.initSymbolAsyncIterator=function(){}};$jscomp.iteratorPrototype=function(b){$jscomp.initSymbolIterator();b={next:b};b[$jscomp.global.Symbol.iterator]=function(){return this};return b};$jscomp.iteratorFromArray=function(b,r){$jscomp.initSymbolIterator();b instanceof String&&(b+="");var q=0,e={next:function(){if(q<b.length){var t=q++;return{value:r(t,b[t]),done:!1}}e.next=function(){return{done:!0,value:void 0}};return e.next()}};e[Symbol.iterator]=function(){return e};return e};$jscomp.polyfill=function(b,r,q,e){if(r){q=$jscomp.global;b=b.split(".");for(e=0;e<b.length-1;e++){var t=b[e];t in q||(q[t]={});q=q[t]}b=b[b.length-1];e=q[b];r=r(e);r!=e&&null!=r&&$jscomp.defineProperty(q,b,{configurable:!0,writable:!0,value:r})}};$jscomp.polyfill("Array.prototype.values",function(b){return b?b:function(){return $jscomp.iteratorFromArray(this,function(b,q){return q})}},"es8","es3");(function(){function b(e){function b(a,k,f,e){for(var d=[],c=0;c<a.length;c++)for(var l=0;l<a[c].safeBags.length;l++){var g=a[c].safeBags[l];if(void 0===e||g.type===e)null===k?d.push(g):void 0!==g.attributes[k]&&0<=g.attributes[k].indexOf(f)&&d.push(g)}return d}function q(d,k,f,b){k=a.fromDer(k);if(k.tagClass!==a.Class.UNIVERSAL||k.type!==a.Type.SEQUENCE||!0!==k.constructed)throw{code:"115011",message:p["115011"]+"(PKCS#12 AuthenticatedSafe expected to be a SEQUENCE OF ContentInfo)"};for(var h=0;h<k.value.length;h++){var c={},l=[];if(!a.validate(k.value[h],C,c,l))throw{code:"115012",message:p["115012"]+"Cannot read ContentInfo.",errors:l};l={encrypted:!1};var g=c.content.value[0];switch(a.derToOid(c.contentType)){case m.oids.data:if(g.tagClass!==a.Class.UNIVERSAL||g.type!==a.Type.OCTETSTRING)throw{code:"115013",message:p["115013"]};c=g.value;break;case m.oids.encryptedData:if(void 0===f)throw{code:"115014",message:p["115014"]};var n=f;c={};var A=[];if(!a.validate(g,e.pkcs7.asn1.encryptedDataValidator,c,A))throw{code:"115016",message:p["115016"]+"Cannot read EncryptedContentInfo.",errors:A};g=a.derToOid(c.contentType);if(g!==m.oids.data)throw{code:"115017",message:p["115017"]+"(PKCS#12 EncryptedContentInfo ContentType is not Data. oid : "+g+")"};g=a.derToOid(c.encAlgorithm);g=e.pkcs5.pbe.getCipher(g,c.encParameter,n);c=e.util.createBuffer(c.encContent);g.update(c);if(!g.finish())throw{code:"115018",message:p["115018"]+"Failed to decrypt PKCS#12 SafeContents."};c=g.output.getBytes();l.encrypted=!0;break;default:throw{code:"115015",message:p["115015"]+"(contentType:"+a.derToOid(c.contentType)+")"};}l.safeBags=x(c,b);d.safeContents.push(l)}}function r(d,k,f){var b=m.oids["pbeWithSHAAnd3-KeyTripleDES-CBC"];k=e.pkcs5.pbe.setCipher(b,f,k);k.update(a.toDer(d));if(!k.finish())throw{code:"115019",message:p["115019"]};d=e.util.createBuffer();d.putInt16(f.count);f=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.data).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(b).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,f.salt),a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,d.getBytes())])]),a.create(a.Class.CONTEXT_SPECIFIC,0,!1,k.output.getBytes())]);return a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.encryptedData).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,String.fromCharCode(0)),f])])])}function x(d,k){d=a.fromDer(d);if(d.tagClass!==a.Class.UNIVERSAL||d.type!==a.Type.SEQUENCE||!0!==d.constructed)throw{code:"115020",message:p["115020"]+"PKCS#12 SafeContents expected to be a SEQUENCE OF SafeBag."};for(var f=[],e=0;e<d.value.length;e++){var h={},c=[];if(!a.validate(d.value[e],G,h,c))throw{code:"115021",message:p["115021"]+"Cannot read SafeBag.",errors:c};var l={type:a.derToOid(h.bagId),attributes:H(h.bagAttributes)};f.push(l);var g=h.bagValue.value[0];switch(l.type){case m.oids.pkcs8ShroudedKeyBag:if(void 0===k){l.key=g;continue}g=v.decryptPrivateKeyInfo(g,k);if(null===g)throw{code:"115022",message:p["115022"]+"Unable to decrypt PKCS#8 ShroudedKeyBag, wrong password?"};case m.oids.keyBag:l.key=m.privateKeyFromAsn1(g);l.rand=v.getPrivateKeyAttributesRandom(g);continue;case m.oids.certBag:var b=I;var A=function(){if(a.derToOid(h.certId)!==m.oids.x509Certificate)throw{code:"115023",message:p["115023"]+"(Unsupported certificate type, only X.509 supported. oid:"+a.derToOid(h.certId)+")"};l.cert=m.certificateFromAsn1(a.fromDer(h.cert),!0)};break;default:throw{code:"115024",message:p["115024"]+"(oid:"+l.type+")"};}if(void 0!==b&&!a.validate(g,b,h,c))throw{code:"115025",message:p["115025"]+b.name,errors:c};A()}return f}function H(d){var e={};if(void 0!==d)for(var f=0;f<d.length;f++){var b={},h=[];if(!a.validate(d[f],J,b,h))throw{code:"115026",message:p["115026"]+"Cannot read PKCS#12 BagAttribute. validator="+validator.name,errors:h};h=a.derToOid(b.oid);if(void 0!==m.oids[h]){e[m.oids[h]]=[];for(var c=0;c<b.values.length;c++)e[m.oids[h]].push(b.values[c].value)}}return e}function y(){var d=e.random.getBytes(20);if(null!==d){d=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.localKeyId).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.SET,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,d)])]);var b=a.create(a.Class.UNIVERSAL,a.Type.SET,!0,[]);b.value.push(d);return b}throw{code:"115027",message:p["115027"],errors:errors};}function D(d,b,f,n){return null===b?a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.keyBag).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[d]),f]):a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.pkcs8ShroudedKeyBag).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[e.pkcs8.encryptPrivateKeyInfo(d,b,n)]),f])}function E(d,b){return a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.pkcs8ShroudedKeyBag).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[d]),b])}function z(d,b){"string"===typeof d&&(d=m.certificateFromPem(d));d=m.certificateToAsn1(d);return a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.certBag).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.x509Certificate).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,a.toDer(d).getBytes())])])]),b])}var p=e.jsustoolkitErrCode=e.jsustoolkitErrCode||{},a=e.asn1,m=e.pki=e.pki||{},v=e.pkcs8=e.pkcs8||{},u=e.pkcs12=e.pkcs12||{},C={name:"ContentInfo",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"ContentInfo.contentType",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"contentType"},{name:"ContentInfo.content",tagClass:a.Class.CONTEXT_SPECIFIC,constructed:!0,captureAsn1:"content"}]},F={name:"PFX",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"PFX.version",tagClass:a.Class.UNIVERSAL,type:a.Type.INTEGER,constructed:!1,capture:"version"},C,{name:"PFX.macData",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,optional:!0,captureAsn1:"mac",value:[{name:"PFX.macData.mac",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"PFX.macData.mac.digestAlgorithm",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"PFX.macData.mac.digestAlgorithm.algorithm",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"macAlgorithm"},{name:"PFX.macData.mac.digestAlgorithm.parameters",tagClass:a.Class.UNIVERSAL,captureAsn1:"macAlgorithmParameters"}]},{name:"PFX.macData.mac.digest",tagClass:a.Class.UNIVERSAL,type:a.Type.OCTETSTRING,constructed:!1,capture:"macDigest"}]},{name:"PFX.macData.macSalt",tagClass:a.Class.UNIVERSAL,type:a.Type.OCTETSTRING,constructed:!1,capture:"macSalt"},{name:"PFX.macData.iterations",tagClass:a.Class.UNIVERSAL,type:a.Type.INTEGER,constructed:!1,optional:!0,capture:"macIterations"}]}]},G={name:"SafeBag",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"SafeBag.bagId",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"bagId"},{name:"SafeBag.bagValue",tagClass:a.Class.CONTEXT_SPECIFIC,constructed:!0,captureAsn1:"bagValue"},{name:"SafeBag.bagAttributes",tagClass:a.Class.UNIVERSAL,type:a.Type.SET,constructed:!0,optional:!0,capture:"bagAttributes"}]},J={name:"Attribute",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"Attribute.attrId",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"oid"},{name:"Attribute.attrValues",tagClass:a.Class.UNIVERSAL,type:a.Type.SET,constructed:!0,capture:"values"}]},I={name:"CertBag",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"CertBag.certId",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"certId"},{name:"CertBag.certValue",tagClass:a.Class.CONTEXT_SPECIFIC,constructed:!0,value:[{name:"CertBag.certValue[0]",tagClass:a.Class.UNIVERSAL,type:a.Class.OCTETSTRING,constructed:!1,capture:"cert"}]}]};u.getCertNKeyFromPKCS12=function(d,k){if(null==d||"undefined"==typeof d)throw{code:"115001",message:p["115001"]};if(null==k||"undefined"==typeof k)throw{code:"115002",message:p["115002"]};var f={},n=[];if(!a.validate(d,F,f,n))throw{code:"115003",message:p["115003"],errors:n};var h={version:f.version.charCodeAt(0),safeContents:[],getCertAndKeyFromPKCS12:function(a){for(var c={sign:{}},d=0;d<h.safeContents[0].safeBags.length;d++){var e=h.safeContents[0].safeBags[d].attributes.localKeyId[0],f=b(h.safeContents,"localKeyId",e,m.oids.pkcs8ShroudedKeyBag);0==f.length&&(f=b(h.safeContents,"localKeyId",e,m.oids.keyBag));if(2==f.length){b(h.safeContents,"localKeyId",e,m.oids.certBag);c.sign.cert=h.safeContents[1].safeBags[0].cert;e=v.rsaPrivateKeyInfo(m.privateKeyToAsn1(f[0].key),f[0].rand);c.sign.prikey=v.encryptPrivateKeyInfo(e,a,{algorithm:"seed"});var g={};g.cert=h.safeContents[1].safeBags[1].cert;e=v.rsaPrivateKeyInfo(m.privateKeyToAsn1(f[1].key),f[1].rand);g.prikey=v.encryptPrivateKeyInfo(e,a,{algorithm:"seed"});c.km=g;break}else if(1==f.length)g=b(h.safeContents,"localKeyId",e,m.oids.certBag),e=v.rsaPrivateKeyInfo(m.privateKeyToAsn1(f[0].key),f[0].rand),e=v.encryptPrivateKeyInfo(e,a,{algorithm:"seed"}),f=g[0].cert,f.getExtension("keyUsage").keyEncipherment?(g={},g.cert=f,g.prikey=e,c.km=g):(c.sign.cert=f,c.sign.prikey=e);else throw{code:"115004",message:p["115004"]};}return c}};if(3!==f.version.charCodeAt(0))throw{code:"115005",message:p["115005"]+"(version: "+f.version.charCodeAt(0)+")"};if(a.derToOid(f.contentType)!==m.oids.data)throw{code:"115006",message:p["115006"]+"(oid :"+a.derToOid(f.contentType)+")"};d=f.content.value[0];if(d.tagClass!==a.Class.UNIVERSAL||d.type!==a.Type.OCTETSTRING)throw{code:"115007",message:p["115007"]+"(PKCS#12 authSafe content data is not an OCTET STRING.)"};if(f.mac){n=null;var c=0,l=a.derToOid(f.macAlgorithm);switch(l){case m.oids.sha1:n=e.md.sha1.create();c=20;break;case m.oids.sha256:n=e.md.sha256.create();c=32;break;case m.oids.md5:n=e.md.md5.create();c=16;break;default:throw{code:"115008",message:p["115008"]+"(PKCS#12 uses unsupported MAC algorithm: "+l+")"};}if(null===n)throw{code:"115009",message:p["115009"]+l};l=new e.util.ByteBuffer(f.macSalt);var g="macIterations"in f?parseInt(e.util.bytesToHex(f.macIterations),16):1;c=u.generateKey(k||"",l,3,g,c,n);l=e.hmac.create();l.start(n,c);l.update(d.value);if(l.getMac().toHex()!==e.util.bytesToHex(f.macDigest))throw{code:"115010",message:p["115010"]};}q(h,d.value,k,k);return h};u.makePKCS12=function(d,b,f,n,h,c){if(null==d||"undefined"==typeof d)throw{code:"115028",message:p["115028"]};if(null==b||"undefined"==typeof b)throw{code:"115029",message:p["115029"]};if(null==h||"undefined"==typeof h)throw{code:"115030",message:p["115030"]};c=c||{};c.saltSize=c.saltSize||20;c.count=c.count||2E3;c.encAlgorithm=c.encAlgorithm||"3des";c.algorithm=c.algorithm||"3des";c.generateLocalKeyId=c.generateLocalKeyId||!0;c.useMac=c.useMac||!0;"useMac"in c||(c.useMac=!0);"generateLocalKeyId"in c||(c.generateLocalKeyId=!0);var l=[],g=y();l.push(g);null!==f&&"undefined"!=typeof f&&(g=y(),l.push(g));g=[];if(null!==b){b=e.pkcs8.decryptPrivateKeyInfo(b,h);var k=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[]);b=D(b,h,l[0],c);k.value.push(b);null!==n&&"undefined"!=typeof n&&(n=e.pkcs8.decryptPrivateKeyInfo(n,h),b=D(n,h,l[1],c),k.value.push(b));n=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.data).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,a.toDer(k).getBytes())])]);g.push(n)}null!==d&&(n=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[]),d=z(d,l[0]),n.value.push(d),null!==f&&"undefined"!=typeof f&&(d=z(f,l[1]),n.value.push(d)),f=r(n,h,c),g.push(f));f=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,g);l=void 0;c.useMac&&(g=e.md.sha1.create(),l=new e.util.ByteBuffer(e.random.getBytes(c.saltSize)),c=c.count,h=u.generateKey(h||"",l,3,c,20),d=e.hmac.create(),d.start(g,h),d.update(a.toDer(f).getBytes()),h=d.getMac(),l=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.sha1).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.NULL,!1,"")]),a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,h.getBytes())]),a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,l.getBytes()),a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,e.util.hexToBytes(c.toString(16)))]));return a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,String.fromCharCode(3)),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.data).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,a.toDer(f).getBytes())])]),l])};u.makePKCS12FromCertNEncPKCS8=function(b,k,f,n,h,c){if(null==b||"undefined"==typeof b)throw{code:"115031",message:p["115031"]};if(null==k||"undefined"==typeof k)throw{code:"115032",message:p["115032"]};if(null==h||"undefined"==typeof h)throw{code:"115033",message:p["115033"]};c=c||{};c.saltSize=c.saltSize||20;c.count=c.count||2E3;c.encAlgorithm=c.encAlgorithm||"3des";c.algorithm=c.algorithm||"3des";c.generateLocalKeyId=c.generateLocalKeyId||!0;c.useMac=c.useMac||!0;c.salt=c.salt||e.random.getBytes(c.saltSize);"useMac"in c||(c.useMac=!0);"generateLocalKeyId"in c||(c.generateLocalKeyId=!0);var d=[],g=y();d.push(g);null!==f&&"undefined"!=typeof f&&(g=y(),d.push(g));g=[];if(null!==k){var q=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[]);k=E(k,d[0]);q.value.push(k);null!==n&&"undefined"!=typeof n&&(k=E(n,d[1]),q.value.push(k));n=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.data).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,a.toDer(q).getBytes())])]);g.push(n)}null!==b&&(n=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[]),b=z(b,d[0]),n.value.push(b),null!==f&&"undefined"!=typeof f&&(b=z(f,d[1]),n.value.push(b)),f=r(n,h,c),g.push(f));f=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,g);d=void 0;c.useMac&&(g=e.md.sha1.create(),d=new e.util.ByteBuffer(e.random.getBytes(c.saltSize)),c=c.count,h=u.generateKey(h||"",d,3,c,20),b=e.hmac.create(),b.start(g,h),b.update(a.toDer(f).getBytes()),h=b.getMac(),d=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.sha1).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.NULL,!1,"")]),a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,h.getBytes())]),a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,d.getBytes()),a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,e.util.hexToBytes(c.toString(16)))]));return a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,String.fromCharCode(3)),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(m.oids.data).getBytes()),a.create(a.Class.CONTEXT_SPECIFIC,0,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,a.toDer(f).getBytes())])]),d])};u.getCertNKeyFromPKCS12WithEncPKCS8=function(d,k){if(null==d||"undefined"==typeof d)throw{code:"115034",message:p["115034"]};if(null==k||"undefined"==typeof k)throw{code:"115035",message:p["115035"]};var f={},n=[];if(!a.validate(d,F,f,n))throw{code:"115036",message:p["115036"],errors:n};var h={version:f.version.charCodeAt(0),safeContents:[],getCertAndKeyFromPKCS12:function(){for(var a={sign:{}},c=0;c<h.safeContents[0].safeBags.length;c++){var d=h.safeContents[0].safeBags[c].attributes.localKeyId[0],e=b(h.safeContents,"localKeyId",d,m.oids.pkcs8ShroudedKeyBag);0==e.length&&(e=b(h.safeContents,"localKeyId",d,m.oids.keyBag));if(2==e.length){b(h.safeContents,"localKeyId",d,m.oids.certBag);a.sign.cert=h.safeContents[1].safeBags[0].cert;a.sign.prikey=e[0].key;d={};d.cert=h.safeContents[1].safeBags[1].cert;d.prikey=e[1].key;a.km=d;break}else if(1==e.length){d=b(h.safeContents,"localKeyId",d,m.oids.certBag);e=e[0].key;var f=d[0].cert;f.getExtension("keyUsage").keyEncipherment?(d={},d.cert=f,d.prikey=e,a.km=d):(a.sign.cert=f,a.sign.prikey=e)}else throw{code:"115037",message:p["115037"]};}return a}};if(3!==f.version.charCodeAt(0))throw{code:"115038",message:p["115038"]+"(version : "+f.version.charCodeAt(0)+")"};if(a.derToOid(f.contentType)!==m.oids.data)throw{code:"115039",message:p["115039"]+"(oid : "+a.derToOid(f.contentType)+")"};d=f.content.value[0];if(d.tagClass!==a.Class.UNIVERSAL||d.type!==a.Type.OCTETSTRING)throw{code:"115040",message:p["115040"]};if(f.mac){n=null;var c=0,l=a.derToOid(f.macAlgorithm);switch(l){case m.oids.sha1:n=e.md.sha1.create();c=20;break;case m.oids.sha256:n=e.md.sha256.create();c=32;break;case m.oids.md5:n=e.md.md5.create();c=16;break;default:throw{code:"115041",message:p["115041"]+"("+l+")"};}if(null===n)throw{code:"115042",message:p["115042"]+"("+l+")"};l=new e.util.ByteBuffer(f.macSalt);var g="macIterations"in f?parseInt(e.util.bytesToHex(f.macIterations),16):1;c=u.generateKey(k||"",l,3,g,c,n);l=e.hmac.create();l.start(n,c);l.update(d.value);if(l.getMac().toHex()!==e.util.bytesToHex(f.macDigest))throw{code:"115043",message:p["115043"]};}q(h,d.value,k);return h};u.generateKey=function(a,b,f,n,h,c){if(null==a||"undefined"==typeof a)throw{code:"115044",message:p["115044"]};if(null==b||"undefined"==typeof b)throw{code:"115045",message:p["115045"]};if(null==f||"undefined"==typeof f)throw{code:"115046",message:p["115046"]};if(null==n||"undefined"==typeof n)throw{code:"115047",message:p["115047"]};if(null==h||"undefined"==typeof h)throw{code:"115001",message:p["115001"]};var d,g;if("undefined"===typeof c||null===c)c=e.md.sha1.create();var m=c.digestLength,k=c.blockLength,q=new e.util.ByteBuffer,r=new e.util.ByteBuffer;for(g=0;g<a.length;g++)r.putInt16(a.charCodeAt(g));r.putInt16(0);a=r.length();var t=b.length(),B=new e.util.ByteBuffer;B.fillWithByte(f,k);var w=k*Math.ceil(t/k);f=new e.util.ByteBuffer;for(g=0;g<w;g++)f.putByte(b.at(g%t));w=k*Math.ceil(a/k);b=new e.util.ByteBuffer;for(g=0;g<w;g++)b.putByte(r.at(g%a));r=f;r.putBuffer(b);b=Math.ceil(h/m);for(f=1;f<=b;f++){w=new e.util.ByteBuffer;w.putBytes(B.bytes());w.putBytes(r.bytes());for(g=0;g<n;g++)c.start(),c.update(w.getBytes()),w=c.digest();var u=new e.util.ByteBuffer;for(g=0;g<k;g++)u.putByte(w.at(g%m));var v=Math.ceil(t/k)+Math.ceil(a/k),x=new e.util.ByteBuffer;for(d=0;d<v;d++){var y=new e.util.ByteBuffer(r.getBytes(k)),z=511;for(g=u.length()-1;0<=g;g--)z>>=8,z+=u.at(g)+y.at(g),y.setAt(g,z&255);x.putBuffer(y)}r=x;q.putBuffer(w)}q.truncate(q.length()-h);return q}}var r="./asn1 ./sha1 ./pkcs7asn1 ./pki ./util ./random ./hmac ./jsustoolkitErrCode".split(" "),q=null;"function"!==typeof define&&("object"===typeof module&&module.exports?q=function(b,r){r(require,module)}:(crosscert=window.crosscert=window.crosscert||{},b(crosscert)));(q||"function"===typeof define)&&(q||define)(["require","module"].concat(r),function(e,q){q.exports=function(q){var t=r.map(function(b){return e(b)}).concat(b);q=q||{};q.defined=q.defined||{};if(q.defined.pkcs12)return q.pkcs12;q.defined.pkcs12=!0;for(var x=0;x<t.length;++x)t[x](q);return q.pkcs12}})})();