usWebCMP.js 7.4 KB

1
  1. (function(){function y(e){e=window.crosscert||{};var m=e.usWebCMPXMLHttp=e.usWebCMPXMLHttp||{},b=e.usWebCMP=e.usWebCMP||{},k=e.cmp=e.cmp||{},d=e.util||{},t=e.asn1||{},w=null,f=null,A=null,B=null,u=null,C=null,z=null;b.info={version:"0a",pkiReq:"00",pkiRep:"05",flags:"00",CMPUrl:"https://solar1.crosscert.com:8443"};b.revReason=k.revReason;b.issueCert=function(a,g,c,e,l,h,d,q,p,r){try{if(null==h||"undefined"==typeof h)throw{code:"120004",message:"StorageHendler is empty."};w=h;if(null==d||"undefined"==typeof d)throw{code:"120005",message:"The CMP callback function is empty."};f=d;null!=q&&"undefined"!=typeof q&&(A=q);null!=p&&"undefined"!=typeof p&&(B=p);null!=r&&"undefined"!=typeof r&&(u=r);b.info.storageInfo=e;var D=k.createGenmMessage(a,g,c,l),n=_create(D.toAsn1());m.uniSignE2ERequest("POST",n,b.info.CMPUrl+"/GenmGenp.do",_issueCertGenPIR)}catch(x){f(3,"",x)}};_issueCertGenPIR=function(a){if("object"==typeof a)_showErrMSG(a);else try{var g=_parse(a);k.messageFromAsn1(g);try{var c=e.cmp.createIRMessage(A,B),d=_create(c.toAsn1());m.uniSignE2ERequest("POST",d,b.info.CMPUrl+"/IrIp.do",_issueCertIPConf)}catch(l){f(3,"",l)}}catch(l){null!=l.code?_errMsg(l.message):f(3,"",l)}};_issueCertIPConf=function(a){if("object"==typeof a)_showErrMSG(a);else try{var g=_parse(a);k.messageFromAsn1(g);try{var c;if(null==u||"undefined"==typeof u)if(c=_saveCertificate()){var d=e.cmp.createConfirmMessage(),l=_create(d.toAsn1());m.uniSignE2ERequest("POST",l,b.info.CMPUrl+"/Confirm.do",_CMPConfirm)}else _CMPConfirm({status:"-1",statusText:"Fail to Save Certificate"});else u(w,k.user,_getCAName(e.pki.certificateFromBase64(k.user.signcert)),function(a){a?(a=e.cmp.createConfirmMessage(),a=_create(a.toAsn1()),m.uniSignE2ERequest("POST",a,b.info.CMPUrl+"/Confirm.do",_CMPConfirm)):_CMPConfirm({status:"-1",statusText:"Fail to Save Certificate"})})}catch(h){f(3,"",h)}}catch(h){null!=h.code?_errMsg(h.message):f(3,"",h)}};_CMPConfirm=function(a){if("object"==typeof a)_showErrMSG(a);else try{var b=_parse(a);endtime=(new Date).getTime();"00"==d.bytesToHex(b)?f(1,""):f(2,"CMP fail!")}catch(c){null!=c.code?_errMsg(c.message):f(3,"",c)}};_errMsg=function(a){a="EncodingType=utf8&ErrMessage="+encodeURIComponent(b.messageToBase64(e.util.createBuffer(a)));m.uniSignE2ERequest("POST",a,b.info.CMPUrl+"/EncodingErrMsg.do",_showErrMSG,"application/x-www-form-urlencoded;charset=UTF-8")};_showErrMSG=function(a){"object"==typeof a?f(2,(""!=a.statusText?a.statusText:a.responseText)+"(status :"+a.status+")"):f(2,b.messageFromBase64(a).toString())};_create=function(a){var g=d.createBuffer();a=t.toDer(a).getBytes();g.putBuffer(y(a.length+3));g.putBytes(d.hexToBytes(b.info.version));g.putBytes(d.hexToBytes(b.info.flags));g.putBytes(d.hexToBytes(b.info.pkiReq));g.putBytes(a);return b.messageToBase64(g)};_parse=function(a){a=b.messageFromBase64(decodeURIComponent(a));var g=a.getInt32();if(g!=a.length())throw{code:"120001",message:"WebCMP Message has wrong length."};if(a.getBytes(1)!=d.hexToBytes(b.info.version))throw{code:"120002",message:"WebCMP version is not supported."};a.getBytes(1);if(a.getBytes(1)!=d.hexToBytes(b.info.pkiRep))throw{code:"120003",message:"WebCMP message type is not supported."};return a.getBytes(g-3)};_saveCertificate=function(){try{w.SaveUserCert(_getCAName(e.pki.certificateFromBase64(k.user.signcert)),k.user,b.info.storageInfo,!0)}catch(a){throw a;}return!0};_getCertName=function(a){return(new e.x509Certificate.certUtil).getDN(a.subject)};_getCAName=function(a){a=a.subject;for(var b=0;b<a.attributes.length;b++)if("o"==a.attributes[a.attributes.length-(b+1)].shortName)return e.util.createBuffer(a.attributes[a.attributes.length-(b+1)].value).toString().toLowerCase()};_deleteCertificate=function(){var a=_getCertName(k.info.signCert);return w.DeleteUserCertByDN(_getCAName(k.info.signCert),a,b.info.storageInfo)};b.revocation=function(a,g,c,d,l,h,n,q,p,r){try{if(null==n||"undefined"==typeof n)throw{code:"120006",message:"StorageHendler is empty."};w=n;if(null==q||"undefined"==typeof q)throw{code:"120007",message:"The CMP callback function is empty."};f=q;null!=p&&"undefined"!=typeof p&&(z=p);null!=r&&"undefined"!=typeof r&&(C=r);b.info.storageInfo=l;a=e.pki.certificateFromBase64(a);if(null==p||"undefined"==typeof p)g=e.pkcs8.encryptedPrivateKeyFromBase64(g);c=null!=k.info.kmCert&&"undefined"!=typeof k.info.kmCert?e.pki.certificateFromBase64(c):null;var v=k.createRRMessage(a,g,c,d,h,z),u=_create(v.toAsn1());m.uniSignE2ERequest("POST",u,b.info.CMPUrl+"/Rr.do",_revocation1)}catch(x){f(3,"",x)}};_revocation1=function(a){if("object"==typeof a)_showErrMSG(a);else try{var b=_parse(a);k.messageFromAsn1(b);try{null==C||"undefined"==typeof C?_deleteCertificate():C(w,k.info.signCert),f(1)}catch(c){f(3,"",c)}}catch(c){null!=c.code?_errMsg(c.message):f(3,"",c)}};b.update=function(a,g,c,d,l,h,n,q,p,r,v,t,x){try{if(null==q||"undefined"==typeof q)throw{code:"120008",message:"StorageHendler is empty."};w=q;if(null==p||"undefined"==typeof p)throw{code:"120009",message:"The CMP callback function is empty."};f=p;null!=r&&"undefined"!=typeof r&&(A=r);null!=v&&"undefined"!=typeof v&&(B=v);null!=t&&"undefined"!=typeof t&&(z=t);null!=x&&"undefined"!=typeof x&&(u=x);b.info.storageInfo=h;h={};h.signCert=e.pki.certificateFromBase64(a);if(null==v||"undefined"==typeof v)h.signKey=e.pkcs8.encryptedPrivateKeyFromBase64(g);h.pw=l;"undefined"!==typeof c&&(h.kmCert=e.pki.certificateFromBase64(c),null==v||"undefined"==typeof v)&&(h.kmKey=e.pkcs8.encryptedPrivateKeyFromBase64(d));var D=k.createGenmMessage(h.signCert.serialNumber,"",l,n,h),y=_create(D.toAsn1());m.uniSignE2ERequest("POST",y,b.info.CMPUrl+"/GenmGenp.do",_updateCertGenPKUR)}catch(F){f(3,"",F)}};_updateCertGenPKUR=function(a){if("object"==typeof a)_showErrMSG(a);else try{var g=_parse(a);k.messageFromAsn1(g);var c=e.cmp.createKURMessage(A,B,z),d=_create(c.toAsn1());m.uniSignE2ERequest("POST",d,b.info.CMPUrl+"/KurKup.do",_updateCertKUPConf)}catch(l){null!=l.code?_errMsg(l.message):f(3,"",l)}};_updateCertKUPConf=function(a){if("object"==typeof a)_showErrMSG(a);else try{var d=_parse(a);k.messageFromAsn1(d);try{if(null==u||"undefined"==typeof u?_saveCertificate():u(w,k.user)){var c=e.cmp.createConfirmMessage(z),t=_create(c.toAsn1());m.uniSignE2ERequest("POST",t,b.info.CMPUrl+"/Confirm.do",_CMPConfirm)}else{var l=e.cmp.createErrorMessage(),h=_create(l.toAsn1());m.uniSignE2ERequest("POST",h,b.info.CMPUrl+"/Confirm.do")}}catch(n){f(3,"",n)}}catch(n){null!=n.code?_errMsg(n.message):f(3,"",n)}};_updateCertConf=function(a){if("object"==typeof a)_showErrMSG(a);else try{var b=_parse(a);endtime=(new Date).getTime();"00"==d.bytesToHex(b)?f(1,""):f(2,"update fail!")}catch(c){null!=c.code?_errMsg(c.message):f(3,"",c)}};b.messageFromBase64=function(a){return d.createBuffer(d.decode64(a))};b.messageToBase64=function(a){"string"==typeof a&&(a=d.createBuffer(d.trim(a)));return d.encode64(a.getBytes())};var y=function(a){var b="";do b+=String.fromCharCode(a&255),a>>>=8;while(0<a);a=d.createBuffer();for(var c=0;c<4-b.length;c++)a.putByte(0);for(c=b.length-1;0<=c;c--)a.putByte(b.charCodeAt(c));return a}}var E=["./cmpasn1","./cmp","./usWebCMPXMLHttp"],t=null;"function"!==typeof define&&("object"===typeof module&&module.exports?t=function(e,m){m(require,module)}:(crosscert=window.crosscert=window.crosscert||{},y(crosscert)));(t||"function"===typeof define)&&(t||define)(["require","module"].concat(E),function(e,m){m.exports=function(b){var k=E.map(function(b){return e(b)}).concat(y);b=b||{};b.defined=b.defined||{};if(b.defined.asn1)return b.asn1;b.defined.asn1=!0;for(var d=0;d<k.length;++d)k[d](b);return b.asn1}})})();