cloudsign.js 9.7 KB

1
  1. var __crosscertcloudsign=function(k){function z(c,b,a){if(0<c.length){var g=function(a,d,c){l(n,a[d],function(e){e=JSON.parse(e);b.push(e.certificate);++d<a.length?g(a,d,c):c(b)})};g(c,0,a)}else a(b)}function u(c,b,a){a||(b.putByte(128),c=b.length()==c?0:c-b.length(),b.fillWithByte(0,c));return!0}function l(c,b,a){var g={},e=JSON.parse(b);if(void 0==c)g.operation=e.operation,g.messageNumber=e.messageNumber,g.resultCode="0051",g.resultMessage="Initialization failed, please check server url.",a(JSON.stringify(g));else{var d=A("POST",c);if(!d)throw Error("CORS not supported");d.onload=function(){a(d.responseText)};d.onerror=function(){g.operation=e.operation;g.messageNumber=e.messageNumber;g.resultCode="0052";g.resultMessage="unexpected answer from Security Server :"+d.status;a(JSON.stringify(g))};d.send(b)}}function A(c,b){var a=new XMLHttpRequest;"withCredentials"in a?(a.open(c,b,!0),a.setRequestHeader("Content-Type","application/json;charset=UTF-8")):"undefined"!=typeof XDomainRequest?(a=new XDomainRequest,a.open(c,b)):a=null;return a}function q(){return JSON.parse(JSON.stringify({resultCode:"0053",resultMessage:"empty answer from Security Server"}))}function t(){h+=1;l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",operation:"CLOSE_SESSION",pktVersion:m}),function(c){"0000"==JSON.parse(c).resultCode&&(h=0)})}var f=k.usWebToolkit,h,p,r,n,m,B=k.ESVS.joinCode?k.ESVS.joinCode:"1111",C=k.ESVS.siteName?k.ESVS.siteName:"crosscert",D=k.ESVS.memberURL?k.ESVS.memberURL:"https://proxy.cloud-sign.co.kr:37005/member",E=k.ESVS.smsURL?k.ESVS.smsURL:"https://proxy.cloud-sign.co.kr:37005/easysign";return{reqMemberInfo:function(c,b){l(D,JSON.stringify({messageNumber:0,sessionID:"",operation:"REQUEST_MEMBER_INFO",phoneNum:c}),function(a){if(""!=a&&0<a.length)switch(a=JSON.parse(a),a.serviceCode){case "-1":case "0":"-1"==a.serviceCode?b(a.resultCode,a.resultMessage,a.serviceCode):b(a.resultCode,a.resultMessage,a.serviceCode+"|"+a.joinpageInfo);break;case "1":case "2":n=a.serverInfo.url_string,m=a.serverInfo.pktVersion,b(a.resultCode,a.resultMessage,a.serviceCode)}else a=q(),b(a.resultCode,a.resultMessage,"")})},reqGetCert:function(c,b,a){var g={};g.siteName=C;g.siteURL=encodeURIComponent(window.location.protocol+"//"+window.location.host);g.purpose=b;h=2E3;l(n,JSON.stringify({messageNumber:h,sessionID:"",client:"PC-NX",operation:"CREATE_SESSION_PC",userID:c,version:"1.0",pktVersion:m,siteInfo:g}),function(e){if(""!=e&&0<e.length){var d=JSON.parse(e);if("0000"==d.resultCode){p=d.sessionID;r=c;e=d.certList.length;for(var g=[],f=1;f<e+1;f++)h+=1,g.push(JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",certificateIndex:f,operation:"GET_CERTIFICATE",pktVersion:m}));z(g,[],function(b){a(d.resultCode,d.resultMessage,b)})}else"0021"==d.resultCode?a(d.resultCode,c,b):a(d.resultCode,d.resultMessage,"")}else d=q(),a(d.resultCode,d.resultMessage,"")})},reqGetCertR:function(c,b){var a=f.sha256.create();a.start();a.update(r);a=a.digest().getBytes();var g=a.substring(0,16),e=a.substring(16,32);h+=1;l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",certificateIndex:c,operation:"GET_CERTIFICATE_R",pktVersion:m}),function(a){if(""!=a&&0<a.length){if(a=JSON.parse(a),"0000"==a.resultCode){var d=a.certificateR,c=f.cipher.algorithms.aes.startDecrypting(g,e);c.update(f.util.createBuffer(f.util.decode64(d)));c.finish(c.tmonetpadding);d=c.output.toHex();b(a.resultCode,a.resultMessage,d)}}else a=q(),b(a.resultCode,a.resultMessage,"")})},reqGenSignNonVerifyPin:function(c,b,a,g){var e=f.sha256.create();e.start();e.update(b);b=e.digest();e=f.sha256.create();e.start();e.update(r);e=e.digest().getBytes();var d=e.substring(0,16),x=e.substring(16,32);e=f.util.createBuffer();var k=f.util.hexToBytes("0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003031300D060960864801650304020105000420");e.putBytes(k);e.putBytes(b.getBytes());b=f.cipher.algorithms.aes.startEncrypting(d,x);b.update(e.getBytes());b.finish(u);b=b.output.getBytes();h+=1;c={messageNumber:h,sessionID:p,client:"PC-NX",operation:"GENERATE_SIGNATURE",certificateIndex:c,tobeSignedData:f.util.encode64(b),keyLength:2048,mechanism:0,pktVersion:m,checkCRL:a};l(n,JSON.stringify(c),function(a){if(""!=a&&0<a.length){a=JSON.parse(a);if("0000"==a.resultCode){var b=a.signature,e=f.cipher.algorithms.aes.startDecrypting(d,x);e.update(f.util.createBuffer(f.util.decode64(b)));e.finish(e.tmonetpadding);b=e.output.toHex();b=f.util.encode64(f.util.hexToBytes(b));g(a.resultCode,a.resultMessage,b)}else g(a.resultCode,a.resultMessage,"");t()}else a=q(),g(a.resultCode,a.resultMessage,"")})},reqGenSignMultiNonVerifyPin:function(c,b,a,g,e){b=parseInt(b);var d=f.sha256.create();d.start();d.update(r);d=d.digest().getBytes();var k=d.substring(0,16),y=d.substring(16,32);d=[];for(var F in a){var v=f.util.createBuffer(f.util.decode64(a[F])),w=f.cipher.algorithms.aes.startEncrypting(k,y);w.update(v.getBytes());w.finish(u);v=w.output.getBytes();d.push(f.util.encode64(v))}h+=1;l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",operation:"GENERATE_SIGNATURE_MULTI",certificateIndex:c,keyLength:2048,mechanism:2,pktVersion:m,count:b,tobeSignedDataList:d,checkCRL:g}),function(a){if(""!=a&&0<a.length){a=JSON.parse(a);if("0000"==a.resultCode){var b=a.signatureList,c=[],d;for(d in b){var g=f.util.createBuffer(f.util.decode64(b[d])),h=f.cipher.algorithms.aes.startDecrypting(k,y);h.update(g);h.finish(h.tmonetpadding);g=h.output.getBytes();c.push(f.util.encode64(g))}e(a.resultCode,a.resultMessage,c)}else e(a.resultCode,a.resultMessage,"");t()}else a=q(),e(a.resultCode,a.resultMessage,"")})},reqRegDefaultCert:function(c,b,a,g){var e=f.sha256.create();e.start();e.update(b);b=e.digest();e=f.sha256.create();e.start();e.update(r);e=e.digest().getBytes();var d=e.substring(0,16),k=e.substring(16,32);e=f.util.createBuffer();var m=f.util.hexToBytes("0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003031300D060960864801650304020105000420");e.putBytes(m);e.putBytes(b.getBytes());b=f.cipher.algorithms.aes.startEncrypting(d,k);b.update(e.getBytes());b.finish(u);b=b.output.getBytes();h+=1;c={messageNumber:h,sessionID:p,client:"PC-NX",operation:"REGISTER_DEFAULT_CERT",certificateIndex:c,mechanism:0,tobeSignedData:f.util.encode64(b),registrationNo:a};l(n,JSON.stringify(c),function(a){if(""!=a&&0<a.length){a=JSON.parse(a);if("0000"==a.resultCode){var b=a.signature;b=f.util.createBuffer(f.util.decode64(b));var e=f.cipher.algorithms.aes.startDecrypting(d,k);e.update(b);e.finish(e.tmonetpadding);b=f.util.encode64(e.output.getBytes());g(a.resultCode,a.resultMessage,b)}else g(a.resultCode,a.resultMessage,"");t()}else a=q(),g(a.resultCode,a.resultMessage,"")})},reqVerifyVID:function(c,b,a){if(0==b.length)alert("\uc8fc\ubbfc\ub4f1\ub85d\ubc88\ud638/\uc0ac\uc5c5\uc790\ub4f1\ub85d\ubc88\ud638\ub97c \uc785\ub825\ud574\uc8fc\uc138\uc694.");else{var g,e,d=f.md.algorithms.sha256.create();d.start();d.update(r);d=d.digest().getBytes();var k=d.substring(0,16),q=d.substring(16,32);h+=1;l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",certificateIndex:c,operation:"GET_CERTIFICATE",pktVersion:m}),function(d){d=JSON.parse(d);"0000"==d.resultCode&&(g=d.certificate,h+=1,l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",certificateIndex:c,operation:"GET_CERTIFICATE_R",pktVersion:m}),function(d){d=JSON.parse(d);if("0000"==d.resultCode){d=d.certificateR;var c=f.cipher.algorithms.aes.startDecrypting(k,q);c.update(f.util.createBuffer(f.util.decode64(d)));c.finish(c.tmonetpadding);e=c.output.toHex();d=f.pki.certificateFromBase64(g);c=f.util.hexToBytes(e);try{f.pkcs8.verifyVIDForHSM(c,b,d)?a("0000","OK",!0):a("0000","OK",!1)}catch(G){a("0014","FAIL",!1)}}}))})}},reqGetCertOID:function(c,b){h+=1;var a;l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",certificateIndex:c,operation:"GET_CERTIFICATE",pktVersion:m}),function(c){a=JSON.parse(c).certificate;c=window.crosscert||{};c.x509Certificate.parser(a,"Base64");b("0000","OK",c.x509Certificate.getCertificatePoliciesOid())})},reqGetCertExpirationDate:function(c,b){h+=1;var a;l(n,JSON.stringify({messageNumber:h,sessionID:p,client:"PC-NX",certificateIndex:c,operation:"GET_CERTIFICATE",pktVersion:m}),function(c){a=JSON.parse(c).certificate;c=window.crosscert||{};c.x509Certificate.parser(a,"Base64");b("0000","OK",c.x509Certificate.getNotAfter())})},requestAppDown:function(c,b){var a="";"easysign"==b?a="ES01":"cloudsign"==b&&(a="");l(E,JSON.stringify({version:a,messageNumber:0,sessionID:"",operation:"SUBMIT_MEMBER_INFO",phoneNumber:c,joinCode:B}),function(b){"0000"!=JSON.parse(b).resultCode?alert("\uc77c\uc2dc\uc801\uc778 \uc624\ub958\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.\n\uc7a0\uc2dc \ud6c4 \ub2e4\uc2dc \uc2dc\ub3c4\ud574\uc8fc\uc138\uc694."):"ES01"==a?alert("[\uc774\uc9c0\uc2f8\uc778] \uc571 \ub2e4\uc6b4\ub85c\ub4dc \ub9c1\ud06c\ub97c \ubb38\uc790\ub85c \ubc1c\uc1a1\ud588\uc2b5\ub2c8\ub2e4.\n\ud734\ub300\ud3f0\uc5d0\uc11c \ud655\uc778\ud574 \uc8fc\uc138\uc694."):alert("[\ud074\ub77c\uc6b0\ub4dc\uc0ac\uc778] \uc571 \ub2e4\uc6b4\ub85c\ub4dc \ub9c1\ud06c\ub97c \ubb38\uc790\ub85c \ubc1c\uc1a1\ud588\uc2b5\ub2c8\ub2e4.\n\ud734\ub300\ud3f0\uc5d0\uc11c \ud655\uc778\ud574 \uc8fc\uc138\uc694.")})},reqCloseSession:function(){t()}}};