pkcs8.js 11 KB

1
  1. (function(){function q(f){var g=f.jsustoolkitErrCode=f.jsustoolkitErrCode||{},c=f.pkcs8=f.pkcs8||{},a=f.asn1,u=f.pkcs5=f.pkcs5||{},k=f.pki=f.pki||{},l=k.oids,p={name:"EncryptedPrivateKeyInfo",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"EncryptedPrivateKeyInfo.encryptionAlgorithm",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"AlgorithmIdentifier.algorithm",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"encryptionOid"},{name:"AlgorithmIdentifier.parameters",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,captureAsn1:"encryptionParams"}]},{name:"EncryptedPrivateKeyInfo.encryptedData",tagClass:a.Class.UNIVERSAL,type:a.Type.OCTETSTRING,constructed:!1,capture:"encryptedData"}]};c.privateKeyValidator={name:"PrivateKeyInfo",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"PrivateKeyInfo.version",tagClass:a.Class.UNIVERSAL,type:a.Type.INTEGER,constructed:!1,capture:"privateKeyVersion"},{name:"PrivateKeyInfo.privateKeyAlgorithm",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"AlgorithmIdentifier.algorithm",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"privateKeyOid"}]},{name:"PrivateKeyInfo",tagClass:a.Class.UNIVERSAL,type:a.Type.OCTETSTRING,constructed:!1,capture:"privateKey"},{name:"Random",tagClass:a.Class.CONTEXT_SPECIFIC,type:0,constructed:!0,optional:!0,value:[{name:"Attributes",tagClass:a.Class.UNIVERSAL,type:a.Type.SEQUENCE,constructed:!0,value:[{name:"AttrOID",tagClass:a.Class.UNIVERSAL,type:a.Type.OID,constructed:!1,capture:"attributesOid"},{name:"AttributesValue",tagClass:a.Class.UNIVERSAL,type:a.Type.SET,constructed:!0,value:[{name:"rand",tagClass:a.Class.UNIVERSAL,type:a.Type.BITSTRING,constructed:!1,capture:"attributesRandom"}]}]}]}]};c.getPrivateKeyAttributesRandom=function(b){if(null==b||"undefined"==typeof b)throw{code:"111019",message:g["111019"]};var e={},d=[];if(!a.validate(b,f.pkcs8.privateKeyValidator,e,d))throw{code:"111020",message:g["111020"],errors:d};return"undefined"!=typeof e.attributesOid&&a.derToOid(e.attributesOid)==l.identifyData_random?(b=f.util.createBuffer(e.attributesRandom),++b.read,b.getBytes()):""};c.rsaPrivateKeyInfo=function(b,e){if(null==b||"undefined"==typeof b)throw{code:"111021",message:g["111021"]};var d=a.oidToDer(l.RSAEncryption).getBytes(),h=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[]);h.value.push(a.create(a.Class.UNIVERSAL,a.Type.OID,!1,d));h.value.push(a.create(a.Class.UNIVERSAL,a.Type.NULL,!1,""));b=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,String.fromCharCode(0)),h,a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,a.toDer(b).getBytes())]);null!=e&&(e=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(l.identifyData_random).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.SET,!0,[a.create(a.Class.UNIVERSAL,a.Type.BITSTRING,!1,String.fromCharCode(0)+e)])])]),b.value.push(e));return b};c.encryptPrivateKeyInfo=function(b,e,d){if(null==b||"undefined"==typeof b)throw{code:"111022",message:g["111022"]};if(null==e||"undefined"==typeof e)throw{code:"111023",message:g["111023"]};d=d||{};d.saltSize=d.saltSize||8;d.count=d.count||2048;d.algorithm=d.algorithm||"seed";d.salt=d.salt||f.random.getBytes(d.saltSize);var h=d.salt,c=d.count,m=f.util.createBuffer();m.putInt16(c);if(0===d.algorithm.indexOf("aes")||0===d.algorithm.indexOf("seed")){if("aes128"===d.algorithm){var n=16;var k=l["aes128-CBC"]}else if("aes192"===d.algorithm)n=24,k=l["aes192-CBC"];else if("aes256"===d.algorithm)n=32,k=l["aes256-CBC"];else if("seed"===d.algorithm)n=16,k=l["seed-CBC"];else throw{code:"111024",message:g["111024"]+"("+d.algorithm+")"};n=f.pkcs5.pbkdf2(e,h,c,n);e=f.random.getBytes(16);d=f.cipher.algorithms[d.algorithm].createEncryptionCipher(n);d.start(e);d.update(a.toDer(b));d.finish();b=d.output.getBytes();h=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(l.pkcs5PBES2).getBytes()),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(l.pkcs5PBKDF2).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,h),a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,m.getBytes())])]),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(k).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,e)])])])}else if("3des"===d.algorithm)n=24,k=new f.util.ByteBuffer(h),n=f.pkcs12.generateKey(e,k,1,c,n),e=f.pkcs12.generateKey(e,k,2,c,8),d=f.des.createEncryptionCipher(n),d.start(e),d.update(a.toDer(b)),d.finish(),b=d.output.getBytes(),h=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OID,!1,a.oidToDer(l["pbeWithSHAAnd3-KeyTripleDES-CBC"]).getBytes()),a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,h),a.create(a.Class.UNIVERSAL,a.Type.INTEGER,!1,m.getBytes())])]);else throw{code:"111025",message:g["111025"]+"("+d.algorithm+")"};return a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[h,a.create(a.Class.UNIVERSAL,a.Type.OCTETSTRING,!1,b)])};c.decryptPrivateKeyInfo=function(b,e){if(null==b||"undefined"==typeof b)throw{code:"111026",message:g["111026"]};if(null==e||"undefined"==typeof e)throw{code:"111027",message:g["111027"]};var d=null,c={},r=[];if(!a.validate(b,p,c,r))throw{code:"111028",message:g["111028"],errors:r};b=a.derToOid(c.encryptionOid);e=u.pbe.getCipher(b,c.encryptionParams,e);c=f.util.createBuffer(c.encryptedData);e.update(c);e.finish()&&(d=a.fromDer(e.output));return d};c.encryptedPrivateKeyToPem=function(b,e){if(null==b||"undefined"==typeof b)throw{code:"111029",message:g["111029"]};b=a.toDer(b);b=f.util.encode64(b.getBytes(),e||64);return"-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n"+b+"\r\n-----END ENCRYPTED PRIVATE KEY-----"};c.encryptedPrivateKeyToBase64=function(b){if(null==b||"undefined"==typeof b)throw{code:"111030",message:g["111030"]};b=a.toDer(b);return f.util.encode64(b.getBytes())};c.encryptedPrivateKeyFromPem=function(b){if(null==b||"undefined"==typeof b)throw{code:"111031",message:g["111031"]};b=k.pemToDer(b);return a.fromDer(b)};c.encryptedPrivateKeyFromBase64=function(b){if(null==b||"undefined"==typeof b)throw{code:"111032",message:g["111032"]};b=k.base64ToDer(b);return a.fromDer(b)};c.encryptRsaPrivateKey=function(a,e,d,h,f){f=f||"bin";a=c.rsaPrivateKeyInfo(k.privateKeyToAsn1(a),e);a=c.encryptPrivateKeyInfo(a,d,h);if("bin"==f)return a;if("base64"==f)return c.encryptedPrivateKeyToBase64(a);if("pem"==f)return c.encryptedPrivateKeyToPem(a);throw{code:"111040",message:g["111040"]+"(type:"+f+")"};};c.decryptRsaPrivateKeyFromPem=function(a,e){a=c.decryptRsaPrivateKeyInfoFromPem(a,e);null!==a&&(a=k.privateKeyFromAsn1(a));return a};c.decryptRsaPrivateKeyInfoFromPem=function(a,e){a=c.encryptedPrivateKeyFromPem(a);return c.decryptPrivateKeyInfo(a,e)};c.decryptRsaPrivateKeyFromBase64=function(a,e){a=c.decryptRsaPrivateKeyInfoFromBase64(a,e);null!==a&&(a=k.privateKeyFromAsn1(a));return a};c.decryptRsaPrivateKeyInfoFromBase64=function(a,e){a=c.encryptedPrivateKeyFromBase64(a);return c.decryptPrivateKeyInfo(a,e)};c.checkUserCertPassword=function(a,e){if(null==e||"undefined"==typeof e)throw{code:"111034",message:g["111034"]};if(null==a||"undefined"==typeof a)throw{code:"111035",message:g["111035"]};try{return a=c.decryptPrivateKeyInfo(a,e),null!==a?(k.privateKeyFromAsn1(a),!0):!1}catch(d){return!1}};c.changePassword=function(a,e,d,h){if(null==a||"undefined"==typeof a)throw{code:"111036",message:g["111036"]};if(null==e||"undefined"==typeof e)throw{code:"111037",message:g["111037"]};if(null==d||"undefined"==typeof d)throw{code:"111038",message:g["111038"]};h=h||"bin";a=c.decryptPrivateKeyInfo(a,e);a=c.encryptPrivateKeyInfo(a,d,{algorithm:"seed"});if("bin"==h)return a;if("Base64"==h)return c.encryptedPrivateKeyToBase64(a);if("PEM"==h)return c.encryptedPrivateKeyToPem(a);throw{code:"111039",message:g["111039"]+"("+h+")"};};c.verifyVIDForHSM=function(b,e,d){if(null==b||"undefined"==typeof b)throw{code:"111045",message:g["111045"]};if(null==e||"undefined"==typeof e)throw{code:"111043",message:g["111043"]};if(null==d||"undefined"==typeof d)throw{code:"111044",message:g["111044"]};for(var c=f.asn1.fromDer(d.getExtension("subjectAltName").value),k=0;k<c.value.length;k++)if(0==c.value[k].type&&l.identifyData==a.derToOid(c.value[k].value[0].value)){d=l[a.derToOid(c.value[k].value[1].value[0].value[1].value[0].value[1].value[0].value[0].value)];c=c.value[k].value[1].value[0].value[1].value[0].value[1].value[1].value[0].value;b=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.PRINTABLESTRING,!1,e),a.create(a.Class.UNIVERSAL,a.Type.BITSTRING,!1,String.fromCharCode(0)+b)]);e=f.md.algorithms[d].create();e.start();e.update(a.toDer(b).getBytes());b=e.digest();e.start();e.update(b.bytes());b=e.digest();if(c==b.bytes())return!0;break}return!1};c.verifyVID=function(b,e,d,h){if(null==b||"undefined"==typeof b)throw{code:"111041",message:g["111041"]};if(null==e||"undefined"==typeof e)throw{code:"111042",message:g["111042"]};if(null==d||"undefined"==typeof d)throw{code:"111043",message:g["111043"]};if(null==h||"undefined"==typeof h)throw{code:"111044",message:g["111044"]};for(var k=f.asn1.fromDer(h.getExtension("subjectAltName").value),m=0;m<k.value.length;m++)if(0==k.value[m].type&&l.identifyData==a.derToOid(k.value[m].value[0].value)){h=l[a.derToOid(k.value[m].value[1].value[0].value[1].value[0].value[1].value[0].value[0].value)];k=k.value[m].value[1].value[0].value[1].value[0].value[1].value[1].value[0].value;b=c.decryptPrivateKeyInfo(b,e);b=c.getPrivateKeyAttributesRandom(b);d=a.create(a.Class.UNIVERSAL,a.Type.SEQUENCE,!0,[a.create(a.Class.UNIVERSAL,a.Type.PRINTABLESTRING,!1,d),a.create(a.Class.UNIVERSAL,a.Type.BITSTRING,!1,String.fromCharCode(0)+b)]);h=f.md.algorithms[h].create();h.start();h.update(a.toDer(d).getBytes());d=h.digest();h.start();h.update(d.bytes());d=h.digest();if(k==d.bytes())return!0;break}return!1}}var t="./aes ./seed ./asn1 ./jsbn ./md ./oids ./random ./rsa ./util ./jsustoolkitErrCode".split(" "),p=null;"function"!==typeof define&&("object"===typeof module&&module.exports?p=function(f,g){g(require,module)}:(crosscert=window.crosscert=window.crosscert||{},q(crosscert)));(p||"function"===typeof define)&&(p||define)(["require","module"].concat(t),function(f,g){g.exports=function(c){var a=t.map(function(a){return f(a)}).concat(q);c=c||{};c.defined=c.defined||{};if(c.defined.pkcs8)return c.pkcs8;c.defined.pkcs8=!0;for(var g=0;g<a.length;++g)a[g](c);return c.pkcs8}})})();