asn1.js 7.5 KB

1
  1. (function(){function t(k){var l=k.jsustoolkitErrCode=k.jsustoolkitErrCode||{},b=k.asn1=k.asn1||{};b.Class={UNIVERSAL:0,APPLICATION:64,CONTEXT_SPECIFIC:128,PRIVATE:192};b.Type={NONE:0,BOOLEAN:1,INTEGER:2,BITSTRING:3,OCTETSTRING:4,NULL:5,OID:6,ODESC:7,EXTERNAL:8,REAL:9,ENUMERATED:10,EMBEDDED:11,UTF8:12,ROID:13,SEQUENCE:16,SET:17,PRINTABLESTRING:19,IA5STRING:22,UTCTIME:23,GENERALIZEDTIME:24,BMPSTRING:30};b.create=function(a,d,b,c){if(c.constructor==Array){for(var g=[],f=0;f<c.length;++f)void 0!==c[f]&&g.push(c[f]);c=g}return{tagClass:a,type:d,constructed:b,composed:b||c.constructor==Array,value:c}};b.fromDer=function(a){if(null==a||"undefined"==typeof a)throw{code:"110005",message:l["110005"]};a.constructor==String&&(a=k.util.createBuffer(a));if(2>a.length())throw{code:"110001",message:l["110001"]+"(bytes:"+a.length()+")"};var d=a.getByte(),g=d&192,c=d&31;var e=a;var f=e.getByte();e=128==f?void 0:f&128?e.getInt((f&127)<<3):f;if(a.length()<e)throw{code:"110002",message:l["110002"]+"(detail:"+a.length()+" < "+e+")"};f=d=32==(d&32);if(!f&&g===b.Class.UNIVERSAL&&c===b.Type.BITSTRING&&1<e){var h=a.read;0===a.getByte()&&(f=!1);a.read=h}if(f)if(f=[],void 0===e)for(;;){if(a.bytes(2)===String.fromCharCode(0,0)){a.getBytes(2);break}f.push(b.fromDer(a))}else for(h=a.length();0<e;)f.push(b.fromDer(a)),e-=h-a.length(),h=a.length();else{if(void 0===e)throw{code:"110004",message:l["110004"]};if(c===b.Type.BMPSTRING)for(f="",h=0;h<e;h+=2)f+=String.fromCharCode(a.getInt16());else f=a.getBytes(e)}return b.create(g,c,d,f)};b.toDer=function(a){var d=k.util.createBuffer(),g=a.tagClass|a.type,c=k.util.createBuffer();if(a.composed){a.constructed?g|=32:c.putByte(0);for(var e=0;e<a.value.length;++e)void 0!==a.value[e]&&c.putBuffer(b.toDer(a.value[e]))}else if(a.type===b.Type.BMPSTRING)for(e=0;e<a.value.length;++e)c.putInt16(a.value.charCodeAt(e));else c.putBytes(a.value);d.putByte(g);if(127>=c.length())d.putByte(c.length()&127);else{e=c.length();a="";do a+=String.fromCharCode(e&255),e>>>=8;while(0<e);d.putByte(a.length|128);for(e=a.length-1;0<=e;--e)d.putByte(a.charCodeAt(e))}d.putBuffer(c);return d};b.oidToDer=function(a){a=a.split(".");var d=k.util.createBuffer();d.putByte(40*parseInt(a[0],10)+parseInt(a[1],10));for(var b,c,e,f,h=2;h<a.length;++h){b=!0;c=[];e=parseInt(a[h],10);do f=e&127,e>>>=7,b||(f|=128),c.push(f),b=!1;while(0<e);for(b=c.length-1;0<=b;--b)d.putByte(c[b])}return d};b.derToOid=function(a){a.constructor==String&&(a=k.util.createBuffer(a));var d=a.getByte();var b=Math.floor(d/40)+"."+d%40;for(var c=0;0<a.length();)d=a.getByte(),c<<=7,d&128?c+=d&127:(b+="."+(c+d),c=0);return b};b.utcTimeToDate=function(a){var d=new Date,b=parseInt(a.substr(0,2),10);b=50<=b?1900+b:2E3+b;var c=parseInt(a.substr(2,2),10)-1,e=parseInt(a.substr(4,2),10),f=parseInt(a.substr(6,2),10),h=parseInt(a.substr(8,2),10),k=0;if(11<a.length){var n=a.charAt(10),m=10;"+"!==n&&"-"!==n&&(k=parseInt(a.substr(10,2),10),m+=2)}d.setUTCFullYear(b,c,e);d.setUTCHours(f,h,k,0);m&&(n=a.charAt(m),"+"===n||"-"===n)&&(b=parseInt(a.substr(m+1,2),10),a=parseInt(a.substr(m+4,2),10),a=6E4*(60*b+a),"+"===n?d.setTime(+d-a):d.setTime(+d+a));return d};b.generalizedTimeToDate=function(a){var d=new Date,b=parseInt(a.substr(0,4),10),c=parseInt(a.substr(4,2),10)-1,e=parseInt(a.substr(6,2),10),f=parseInt(a.substr(8,2),10),h=parseInt(a.substr(10,2),10),k=parseInt(a.substr(12,2),10),n=0,m=0,l=!1;"Z"==a.charAt(a.length-1)&&(l=!0);var p=a.length-5,q=a.charAt(p);if("+"===q||"-"===q)m=parseInt(a.substr(p+1,2),10),p=parseInt(a.substr(p+4,2),10),m=6E4*(60*m+p),"+"===q&&(m*=-1),l=!0;"."==a.charAt(14)&&(n=1E3*parseFloat(a.substr(14),10));l?(d.setUTCFullYear(b,c,e),d.setUTCHours(f,h,k,n),d.setTime(+d+m)):(d.setFullYear(b,c,e),d.setHours(f,h,k,n));return d};b.dateToUtcTime=function(a){var d="",b=[];b.push((""+a.getUTCFullYear()).substr(2));b.push(""+(a.getUTCMonth()+1));b.push(""+a.getUTCDate());b.push(""+a.getUTCHours());b.push(""+a.getUTCMinutes());b.push(""+a.getUTCSeconds());for(a=0;a<b.length;++a)2>b[a].length&&(d+="0"),d+=b[a];return d+"Z"};b.dateToGeneralizedTime=function(a){var b="",g=[];g.push(""+a.getUTCFullYear());g.push(""+(a.getUTCMonth()+1));g.push(""+a.getUTCDate());g.push(""+a.getUTCHours());g.push(""+a.getUTCMinutes());g.push(""+a.getUTCSeconds());for(a=0;a<g.length;++a)0==a?4>g[a].length&&(b+="0"):2>g[a].length&&(b+="0"),b+=g[a];return b+"Z"};b.validate=function(a,d,g,c){var e=!1;if(a.tagClass!==d.tagClass&&"undefined"!==typeof d.tagClass||a.type!==d.type&&"undefined"!==typeof d.type)c&&(a.tagClass!==d.tagClass&&c.push("["+d.name+'] Expected tag class "'+d.tagClass+'", got "'+a.tagClass+'"'),a.type!==d.type&&c.push("["+d.name+'] Expected type "'+d.type+'", got "'+a.type+'"'));else if(a.constructed===d.constructed||"undefined"===typeof d.constructed){e=!0;if(d.value&&d.value.constructor==Array)for(var f=0,h=0;e&&h<d.value.length;++h)e=d.value[h].optional||!1,a.value[f]&&((e=b.validate(a.value[f],d.value[h],g,c))?++f:d.value[h].optional&&(e=!0)),!e&&c&&c.push("["+d.name+'] Tag class "'+d.tagClass+'", type "'+d.type+'" expected value length "'+d.value.length+'", got "'+a.value.length+'"');e&&g&&(d.capture&&(g[d.capture]=a.value),d.captureAsn1&&(g[d.captureAsn1]=a))}else c&&c.push("["+d.name+'] Expected constructed "'+d.constructed+'", got "'+a.constructed+'"');return e};var q=/[^\\u0000-\\u00ff]/;b.prettyPrint=function(a,d,g){var c="";d=d||0;g=g||2;0<d&&(c+="\n");for(var e="",f=0;f<d*g;++f)e+=" ";c+=e+"Tag: ";switch(a.tagClass){case b.Class.UNIVERSAL:c+="Universal:";break;case b.Class.APPLICATION:c+="Application:";break;case b.Class.CONTEXT_SPECIFIC:c+="Context-Specific:";break;case b.Class.PRIVATE:c+="Private:"}if(a.tagClass===b.Class.UNIVERSAL)switch(c+=a.type,a.type){case b.Type.NONE:c+=" (None)";break;case b.Type.BOOLEAN:c+=" (Boolean)";break;case b.Type.BITSTRING:c+=" (Bit string)";break;case b.Type.INTEGER:c+=" (Integer)";break;case b.Type.OCTETSTRING:c+=" (Octet string)";break;case b.Type.NULL:c+=" (Null)";break;case b.Type.OID:c+=" (Object Identifier)";break;case b.Type.ODESC:c+=" (Object Descriptor)";break;case b.Type.EXTERNAL:c+=" (External or Instance of)";break;case b.Type.REAL:c+=" (Real)";break;case b.Type.ENUMERATED:c+=" (Enumerated)";break;case b.Type.EMBEDDED:c+=" (Embedded PDV)";break;case b.Type.UTF8:c+=" (UTF8)";break;case b.Type.ROID:c+=" (Relative Object Identifier)";break;case b.Type.SEQUENCE:c+=" (Sequence)";break;case b.Type.SET:c+=" (Set)";break;case b.Type.PRINTABLESTRING:c+=" (Printable String)";break;case b.Type.IA5String:c+=" (IA5String (ASCII))";break;case b.Type.UTCTIME:c+=" (UTC time)";break;case b.Type.GENERALIZEDTIME:c+=" (Generalized time)";break;case b.Type.BMPSTRING:c+=" (BMP String)"}else c+=a.type;c=c+"\n"+(e+"Constructed: "+a.constructed+"\n");if(a.composed){var h=0,l="";for(f=0;f<a.value.length;++f)void 0!==a.value[f]&&(h+=1,l+=b.prettyPrint(a.value[f],d+1,g),f+1<a.value.length&&(l+=","));c+=e+"Sub values: "+h+l}else c+=e+"Value: ",a.type===b.Type.OID?(a=b.derToOid(a.value),c+=a,k.pki&&k.pki.oids&&a in k.pki.oids&&(c+=" ("+k.pki.oids[a]+")")):c=q.test(a.value)?c+("0x"+k.util.createBuffer(a.value,"utf8").toHex()):0===a.value.length?c+"[null]":c+a.value;return c}}var u=["./util","./oids","./jsustoolkitErrCode"],r=null;"function"!==typeof define&&("object"===typeof module&&module.exports?r=function(k,l){l(require,module)}:(crosscert=window.crosscert=window.crosscert||{},t(crosscert)));(r||"function"===typeof define)&&(r||define)(["require","module"].concat(u),function(k,l){l.exports=function(b){var l=u.map(function(a){return k(a)}).concat(t);b=b||{};b.defined=b.defined||{};if(b.defined.asn1)return b.asn1;b.defined.asn1=!0;for(var a=0;a<l.length;++a)l[a](b);return b.asn1}})})();