1 /* jws-3.3.12 (c) 2013-2022 Kenji Urushima | kjur.github.io/jsrsasign/license 2 */ 3 /* 4 * jws.js - JSON Web Signature(JWS) and JSON Web Token(JWT) Class 5 * 6 * Copyright (c) 2010-2022 Kenji Urushima (kenji.urushima@gmail.com) 7 * 8 * This software is licensed under the terms of the MIT License. 9 * https://kjur.github.io/jsrsasign/license/ 10 * 11 * The above copyright and license notice shall be 12 * included in all copies or substantial portions of the Software. 13 */ 14 15 /** 16 * @fileOverview 17 * @name jws-3.3.js 18 * @author Kenji Urushima kenji.urushima@gmail.com 19 * @version jsrsasign 10.5.25 jws 3.3.12 (2022-Jun-23) 20 * @since jsjws 1.0, jsrsasign 4.8.0 21 * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a> 22 */ 23 24 if (typeof KJUR == "undefined" || !KJUR) KJUR = {}; 25 26 /** 27 * kjur's JSON Web Signature/Token(JWS/JWT) library name space 28 * <p> 29 * This namespace privides following JWS/JWS related classes. 30 * <ul> 31 * <li>{@link KJUR.jws.JWS} - JSON Web Signature/Token(JWS/JWT) class</li> 32 * <li>{@link KJUR.jws.JWSJS} - JWS JSON Serialization(JWSJS) class</li> 33 * <li>{@link KJUR.jws.IntDate} - UNIX origin time utility class</li> 34 * </ul> 35 * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2. 36 * </p> 37 * @name KJUR.jws 38 * @namespace 39 */ 40 if (typeof KJUR.jws == "undefined" || !KJUR.jws) KJUR.jws = {}; 41 42 /** 43 * JSON Web Signature(JWS) class.<br/> 44 * @name KJUR.jws.JWS 45 * @class JSON Web Signature(JWS) class 46 * @see <a href="https://kjur.github.io/jsjws/">'jwjws'(JWS JavaScript Library) home page https://kjur.github.io/jsjws/</a> 47 * @see <a href="https://kjur.github.io/jsrsasigns/">'jwrsasign'(RSA Sign JavaScript Library) home page https://kjur.github.io/jsrsasign/</a> 48 * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-14">IETF I-D JSON Web Algorithms (JWA)</a> 49 * @since jsjws 1.0 50 * @description 51 * This class provides JSON Web Signature(JWS)/JSON Web Token(JWT) signing and validation. 52 * 53 * <h4>METHOD SUMMARY</h4> 54 * Here is major methods of {@link KJUR.jws.JWS} class. 55 * <ul> 56 * <li><b>SIGN</b><br/> 57 * <li>{@link KJUR.jws.JWS.sign} - sign JWS</li> 58 * </li> 59 * <li><b>VERIFY</b><br/> 60 * <li>{@link KJUR.jws.JWS.verify} - verify JWS signature</li> 61 * <li>{@link KJUR.jws.JWS.verifyJWT} - verify properties of JWT token at specified time</li> 62 * </li> 63 * <li><b>UTILITY</b><br/> 64 * <li>{@link KJUR.jws.JWS.getJWKthumbprint} - get RFC 7638 JWK thumbprint</li> 65 * <li>{@link KJUR.jws.JWS.isSafeJSONString} - check whether safe JSON string or not</li> 66 * <li>{@link KJUR.jws.JWS.readSafeJSONString} - read safe JSON string only</li> 67 * </li> 68 * </ul> 69 * 70 * <h4>SUPPORTED SIGNATURE ALGORITHMS</h4> 71 * Here is supported algorithm names for {@link KJUR.jws.JWS.sign} and 72 * {@link KJUR.jws.JWS.verify} methods. 73 * <table> 74 * <tr><th>alg value</th><th>spec requirement</th><th>jsjws support</th></tr> 75 * <tr><td>HS256</td><td>REQUIRED</td><td>SUPPORTED</td></tr> 76 * <tr><td>HS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 77 * <tr><td>HS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 78 * <tr><td>RS256</td><td>RECOMMENDED</td><td>SUPPORTED</td></tr> 79 * <tr><td>RS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 80 * <tr><td>RS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 81 * <tr><td>ES256</td><td>RECOMMENDED+</td><td>SUPPORTED</td></tr> 82 * <tr><td>ES384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 83 * <tr><td>ES512</td><td>OPTIONAL</td><td>-</td></tr> 84 * <tr><td>PS256</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 85 * <tr><td>PS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 86 * <tr><td>PS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 87 * <tr><td>none</td><td>REQUIRED</td><td>SUPPORTED(signature generation only)</td></tr> 88 * </table> 89 * <dl> 90 * <dt><b>NOTE1</b> 91 * <dd>HS384 is supported since jsjws 3.0.2 with jsrsasign 4.1.4. 92 * <dt><b>NOTE2</b> 93 * <dd>Some deprecated methods have been removed since jws 3.3 of jsrsasign 4.10.0. 94 * Removed methods are following: 95 * <ul> 96 * <li>JWS.verifyJWSByNE</li> 97 * <li>JWS.verifyJWSByKey</li> 98 * <li>JWS.generateJWSByNED</li> 99 * <li>JWS.generateJWSByKey</li> 100 * <li>JWS.generateJWSByP1PrvKey</li> 101 * </ul> 102 * </dl> 103 * <b>EXAMPLE</b><br/> 104 * @example 105 * // JWS signing 106 * sJWS = KJUR.jws.JWS.sign(null, '{"alg":"HS256", "cty":"JWT"}', '{"age": 21}', {"utf8": "password"}); 107 * // JWS validation 108 * isValid = KJUR.jws.JWS.verify('eyJjdHkiOiJKV1QiLCJhbGc...', {"utf8": "password"}); 109 * // JWT validation 110 * isValid = KJUR.jws.JWS.verifyJWT('eyJh...', {"utf8": "password"}, { 111 * alg: ['HS256', 'HS384'], 112 * iss: ['http://foo.com'] 113 * }); 114 */ 115 KJUR.jws.JWS = function() { 116 var _KJUR = KJUR, 117 _KJUR_jws_JWS = _KJUR.jws.JWS, 118 _isSafeJSONString = _KJUR_jws_JWS.isSafeJSONString; 119 120 // === utility ============================================================= 121 122 /** 123 * parse JWS string and set public property 'parsedJWS' dictionary.<br/> 124 * @name parseJWS 125 * @memberOf KJUR.jws.JWS 126 * @function 127 * @param {String} sJWS JWS signature string to be parsed. 128 * @throws if sJWS is not comma separated string such like "Header.Payload.Signature". 129 * @throws if JWS Header is a malformed JSON string. 130 * @since jws 1.1 131 */ 132 this.parseJWS = function(sJWS, sigValNotNeeded) { 133 if ((this.parsedJWS !== undefined) && 134 (sigValNotNeeded || (this.parsedJWS.sigvalH !== undefined))) { 135 return; 136 } 137 var matchResult = sJWS.match(/^([^.]+)\.([^.]+)\.([^.]+)$/); 138 if (matchResult == null) { 139 throw "JWS signature is not a form of 'Head.Payload.SigValue'."; 140 } 141 var b6Head = matchResult[1]; 142 var b6Payload = matchResult[2]; 143 var b6SigVal = matchResult[3]; 144 var sSI = b6Head + "." + b6Payload; 145 this.parsedJWS = {}; 146 this.parsedJWS.headB64U = b6Head; 147 this.parsedJWS.payloadB64U = b6Payload; 148 this.parsedJWS.sigvalB64U = b6SigVal; 149 this.parsedJWS.si = sSI; 150 151 if (!sigValNotNeeded) { 152 var hSigVal = b64utohex(b6SigVal); 153 var biSigVal = parseBigInt(hSigVal, 16); 154 this.parsedJWS.sigvalH = hSigVal; 155 this.parsedJWS.sigvalBI = biSigVal; 156 } 157 158 var sHead = b64utoutf8(b6Head); 159 var sPayload = b64utoutf8(b6Payload); 160 this.parsedJWS.headS = sHead; 161 this.parsedJWS.payloadS = sPayload; 162 163 if (! _isSafeJSONString(sHead, this.parsedJWS, 'headP')) 164 throw "malformed JSON string for JWS Head: " + sHead; 165 }; 166 }; 167 168 // === major static method ======================================================== 169 170 /** 171 * generate JWS signature by specified key<br/> 172 * @name sign 173 * @memberOf KJUR.jws.JWS 174 * @function 175 * @static 176 * @param {String} alg JWS algorithm name to sign and force set to sHead or null 177 * @param {String} spHead string or object of JWS Header 178 * @param {String} spPayload string or object of JWS Payload 179 * @param {String} key string of private key or mac key object to sign 180 * @param {String} pass (OPTION)passcode to use encrypted asymmetric private key 181 * @return {String} JWS signature string 182 * @since jws 3.0.0 183 * @see <a href="https://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Signature.html">jsrsasign KJUR.crypto.Signature method</a> 184 * @see <a href="https://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Mac.html">jsrsasign KJUR.crypto.Mac method</a> 185 * @description 186 * This method supports following algorithms. 187 * <table> 188 * <tr><th>alg value</th><th>spec requirement</th><th>jsjws support</th></tr> 189 * <tr><td>HS256</td><td>REQUIRED</td><td>SUPPORTED</td></tr> 190 * <tr><td>HS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 191 * <tr><td>HS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 192 * <tr><td>RS256</td><td>RECOMMENDED</td><td>SUPPORTED</td></tr> 193 * <tr><td>RS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 194 * <tr><td>RS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 195 * <tr><td>ES256</td><td>RECOMMENDED+</td><td>SUPPORTED</td></tr> 196 * <tr><td>ES384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 197 * <tr><td>ES512</td><td>OPTIONAL</td><td>-</td></tr> 198 * <tr><td>PS256</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 199 * <tr><td>PS384</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 200 * <tr><td>PS512</td><td>OPTIONAL</td><td>SUPPORTED</td></tr> 201 * <tr><td>none</td><td>REQUIRED</td><td>SUPPORTED(signature generation only)</td></tr> 202 * </table> 203 * <dl> 204 * <dt>NOTE1: 205 * <dd>salt length of RSAPSS signature is the same as the hash algorithm length 206 * because of <a href="http://www.ietf.org/mail-archive/web/jose/current/msg02901.html">IETF JOSE ML discussion</a>. 207 * <dt>NOTE2: 208 * <dd>To support HS384, patched version of CryptoJS is used. 209 * <a href="https://code.google.com/p/crypto-js/issues/detail?id=84">See here for detail</a>. 210 * <dt>NOTE3: 211 * From jsrsasign 4.10.0 jws 3.3.0, Way to provide password 212 * for HS* algorithm is changed. The 'key' attribute value is 213 * passed to {@link KJUR.crypto.Mac.setPassword} so please see 214 * {@link KJUR.crypto.Mac.setPassword} for detail. 215 * As for backword compatibility, if key is a string, has even length and 216 * 0..9, A-F or a-f characters, key string is treated as a hexadecimal 217 * otherwise it is treated as a raw string. 218 * <dd> 219 * </dl> 220 * <b>EXAMPLE</b><br/> 221 * @example 222 * // sign HS256 signature with password "aaa" implicitly handled as string 223 * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256", cty: "JWT"}, {age: 21}, "aaa"); 224 * // sign HS256 signature with password "6161" implicitly handled as hex 225 * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256", cty: "JWT"}, {age: 21}, "6161"); 226 * // sign HS256 signature with base64 password 227 * sJWS = KJUR.jws.JWS.sign(null, {alg: "HS256"}, {age: 21}, {b64: "Mi/8..a="}); 228 * // sign RS256 signature with PKCS#8 PEM RSA private key 229 * sJWS = KJUR.jws.JWS.sign(null, {alg: "RS256"}, {age: 21}, "-----BEGIN PRIVATE KEY..."); 230 * // sign RS256 signature with PKCS#8 PEM ECC private key with passcode 231 * sJWS = KJUR.jws.JWS.sign(null, {alg: "ES256"}, {age: 21}, 232 * "-----BEGIN PRIVATE KEY...", "keypass"); 233 * // header and payload can be passed by both string and object 234 * sJWS = KJUR.jws.JWS.sign(null, '{alg:"HS256",cty:"JWT"}', '{age:21}', "aaa"); 235 */ 236 KJUR.jws.JWS.sign = function(alg, spHeader, spPayload, key, pass) { 237 var _KJUR = KJUR, 238 _KJUR_jws = _KJUR.jws, 239 _KJUR_jws_JWS = _KJUR_jws.JWS, 240 _readSafeJSONString = _KJUR_jws_JWS.readSafeJSONString, 241 _isSafeJSONString = _KJUR_jws_JWS.isSafeJSONString, 242 _KJUR_crypto = _KJUR.crypto, 243 _ECDSA = _KJUR_crypto.ECDSA, 244 _Mac = _KJUR_crypto.Mac, 245 _Signature = _KJUR_crypto.Signature, 246 _JSON = JSON; 247 248 var sHeader, pHeader, sPayload; 249 250 // 1. check signatureInput(Header, Payload) is string or object 251 if (typeof spHeader != 'string' && typeof spHeader != 'object') 252 throw "spHeader must be JSON string or object: " + spHeader; 253 254 if (typeof spHeader == 'object') { 255 pHeader = spHeader; 256 sHeader = _JSON.stringify(pHeader); 257 } 258 259 if (typeof spHeader == 'string') { 260 sHeader = spHeader; 261 if (! _isSafeJSONString(sHeader)) 262 throw "JWS Head is not safe JSON string: " + sHeader; 263 pHeader = _readSafeJSONString(sHeader); 264 265 } 266 267 sPayload = spPayload; 268 if (typeof spPayload == 'object') sPayload = _JSON.stringify(spPayload); 269 270 // 2. use alg if defined in sHeader 271 if ((alg == '' || alg == null) && 272 pHeader['alg'] !== undefined) { 273 alg = pHeader['alg']; 274 } 275 276 // 3. update sHeader to add alg if alg undefined 277 if ((alg != '' && alg != null) && 278 pHeader['alg'] === undefined) { 279 pHeader['alg'] = alg; 280 sHeader = _JSON.stringify(pHeader); 281 } 282 283 // 4. check explicit algorithm doesn't match with JWS header. 284 if (alg !== pHeader.alg) 285 throw "alg and sHeader.alg doesn't match: " + alg + "!=" + pHeader.alg; 286 287 // 5. set signature algorithm like SHA1withRSA 288 var sigAlg = null; 289 if (_KJUR_jws_JWS.jwsalg2sigalg[alg] === undefined) { 290 throw "unsupported alg name: " + alg; 291 } else { 292 sigAlg = _KJUR_jws_JWS.jwsalg2sigalg[alg]; 293 } 294 295 var uHeader = utf8tob64u(sHeader); 296 var uPayload = utf8tob64u(sPayload); 297 var uSignatureInput = uHeader + "." + uPayload 298 // 6. sign 299 var hSig = ""; 300 if (sigAlg.substr(0, 4) == "Hmac") { 301 if (key === undefined) 302 throw "mac key shall be specified for HS* alg"; 303 //alert("sigAlg=" + sigAlg); 304 var mac = new _Mac({'alg': sigAlg, 'prov': 'cryptojs', 'pass': key}); 305 mac.updateString(uSignatureInput); 306 hSig = mac.doFinal(); 307 } else if (sigAlg.indexOf("withECDSA") != -1) { 308 var sig = new _Signature({'alg': sigAlg}); 309 sig.init(key, pass); 310 sig.updateString(uSignatureInput); 311 var hASN1Sig = sig.sign(); 312 hSig = KJUR.crypto.ECDSA.asn1SigToConcatSig(hASN1Sig); 313 } else if (sigAlg != "none") { 314 var sig = new _Signature({'alg': sigAlg}); 315 sig.init(key, pass); 316 sig.updateString(uSignatureInput); 317 hSig = sig.sign(); 318 } 319 320 var uSig = hextob64u(hSig); 321 return uSignatureInput + "." + uSig; 322 }; 323 324 /** 325 * verify JWS signature by specified key or certificate<br/> 326 * @name verify 327 * @memberOf KJUR.jws.JWS 328 * @function 329 * @static 330 * @param {String} sJWS string of JWS signature to verify 331 * @param {Object} key string of public key, certificate or key object to verify 332 * @param {String} acceptAlgs array of algorithm name strings (OPTION) 333 * @return {Boolean} true if the signature is valid otherwise false including no signature case or without head and payload 334 * @since jws 3.0.0 335 * @see <a href="https://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Signature.html">jsrsasign KJUR.crypto.Signature method</a> 336 * @see <a href="https://kjur.github.io/jsrsasign/api/symbols/KJUR.crypto.Mac.html">jsrsasign KJUR.crypto.Mac method</a> 337 * @description 338 * <p> 339 * This method verifies a JSON Web Signature Compact Serialization string by the validation 340 * algorithm as described in 341 * <a href="http://self-issued.info/docs/draft-jones-json-web-signature-04.html#anchor5"> 342 * the section 5 of Internet Draft draft-jones-json-web-signature-04.</a> 343 * </p> 344 * <p> 345 * Since 3.2.0 strict key checking has been provided against a JWS algorithm 346 * in a JWS header. 347 * <ul> 348 * <li>In case 'alg' is 'HS*' in the JWS header, 349 * 'key' shall be hexadecimal string for Hmac{256,384,512} shared secret key. 350 * Otherwise it raise an error.</li> 351 * <li>In case 'alg' is 'RS*' or 'PS*' in the JWS header, 352 * 'key' shall be a RSAKey object or a PEM string of 353 * X.509 RSA public key certificate or PKCS#8 RSA public key. 354 * Otherwise it raise an error.</li> 355 * <li>In case 'alg' is 'ES*' in the JWS header, 356 * 'key' shall be a KJUR.crypto.ECDSA object or a PEM string of 357 * X.509 ECC public key certificate or PKCS#8 ECC public key. 358 * Otherwise it raise an error.</li> 359 * <li>In case 'alg' is 'none' in the JWS header, 360 * validation not supported after jsjws 3.1.0.</li> 361 * </ul> 362 * </p> 363 * <p> 364 * NOTE1: The argument 'acceptAlgs' is supported since 3.2.0. 365 * Strongly recommended to provide acceptAlgs to mitigate 366 * signature replacement attacks.<br/> 367 * </p> 368 * <p> 369 * NOTE2: From jsrsasign 4.9.0 jws 3.2.5, Way to provide password 370 * for HS* algorithm is changed. The 'key' attribute value is 371 * passed to {@link KJUR.crypto.Mac.setPassword} so please see 372 * {@link KJUR.crypto.Mac.setPassword} for detail. 373 * As for backword compatibility, if key is a string, has even length and 374 * 0..9, A-F or a-f characters, key string is treated as a hexadecimal 375 * otherwise it is treated as a raw string. 376 * </p> 377 * @example 378 * // 1) verify a RS256 JWS signature by a certificate string. 379 * isValid = KJUR.jws.JWS.verify('eyJh...', '-----BEGIN...', ['RS256']); 380 * 381 * // 2) verify a HS256 JWS signature by a certificate string. 382 * isValid = KJUR.jws.JWS.verify('eyJh...', {hex: '6f62ad...'}, ['HS256']); 383 * isValid = KJUR.jws.JWS.verify('eyJh...', {b64: 'Mi/ab8...a=='}, ['HS256']); 384 * isValid = KJUR.jws.JWS.verify('eyJh...', {utf8: 'Secret秘密'}, ['HS256']); 385 * isValid = KJUR.jws.JWS.verify('eyJh...', '6f62ad', ['HS256']); // implicit hex 386 * isValid = KJUR.jws.JWS.verify('eyJh...', '6f62ada', ['HS256']); // implicit raw string 387 * 388 * // 3) verify a ES256 JWS signature by a KJUR.crypto.ECDSA key object. 389 * var pubkey = KEYUTIL.getKey('-----BEGIN CERT...'); 390 * var isValid = KJUR.jws.JWS.verify('eyJh...', pubkey); 391 */ 392 KJUR.jws.JWS.verify = function(sJWS, key, acceptAlgs) { 393 var _KJUR = KJUR, 394 _KJUR_jws = _KJUR.jws, 395 _KJUR_jws_JWS = _KJUR_jws.JWS, 396 _readSafeJSONString = _KJUR_jws_JWS.readSafeJSONString, 397 _KJUR_crypto = _KJUR.crypto, 398 _ECDSA = _KJUR_crypto.ECDSA, 399 _Mac = _KJUR_crypto.Mac, 400 _Signature = _KJUR_crypto.Signature, 401 _RSAKey; 402 403 if (typeof RSAKey !== undefined) _RSAKey = RSAKey; 404 405 // 0. checking dot concatinatd Base64URL encoded string 406 if (! isBase64URLDot(sJWS)) return false; 407 408 var a = sJWS.split("."); 409 if (a.length !== 3) return false; 410 411 var uHeader = a[0]; 412 var uPayload = a[1]; 413 var uSignatureInput = uHeader + "." + uPayload; 414 var hSig = b64utohex(a[2]); 415 416 // 1. parse JWS header 417 var pHeader = _readSafeJSONString(b64utoutf8(a[0])); 418 var alg = null; 419 var algType = null; // HS|RS|PS|ES|no 420 if (pHeader.alg === undefined) { 421 throw "algorithm not specified in header"; 422 } else { 423 alg = pHeader.alg; 424 algType = alg.substr(0, 2); 425 } 426 427 // 2. check whether alg is acceptable algorithms 428 if (acceptAlgs != null && 429 Object.prototype.toString.call(acceptAlgs) === '[object Array]' && 430 acceptAlgs.length > 0) { 431 var acceptAlgStr = ":" + acceptAlgs.join(":") + ":"; 432 if (acceptAlgStr.indexOf(":" + alg + ":") == -1) { 433 throw "algorithm '" + alg + "' not accepted in the list"; 434 } 435 } 436 437 // 3. check whether key is a proper key for alg. 438 if (alg != "none" && key === null) { 439 throw "key shall be specified to verify."; 440 } 441 442 // 3.1. There is no key check for HS* because Mac will check it. 443 // since jsrsasign 5.0.0. 444 445 // 3.2. convert key object if key is a public key or cert PEM string 446 if (typeof key == "string" && 447 key.indexOf("-----BEGIN ") != -1) { 448 key = KEYUTIL.getKey(key); 449 } 450 451 // 3.3. check whether key is RSAKey obj if alg is RS* or PS*. 452 if (algType == "RS" || algType == "PS") { 453 if (!(key instanceof _RSAKey)) { 454 throw "key shall be a RSAKey obj for RS* and PS* algs"; 455 } 456 } 457 458 // 3.4. check whether key is ECDSA obj if alg is ES*. 459 if (algType == "ES") { 460 if (!(key instanceof _ECDSA)) { 461 throw "key shall be a ECDSA obj for ES* algs"; 462 } 463 } 464 465 // 3.5. check when alg is 'none' 466 if (alg == "none") { 467 } 468 469 // 4. check whether alg is supported alg in jsjws. 470 var sigAlg = null; 471 if (_KJUR_jws_JWS.jwsalg2sigalg[pHeader.alg] === undefined) { 472 throw "unsupported alg name: " + alg; 473 } else { 474 sigAlg = _KJUR_jws_JWS.jwsalg2sigalg[alg]; 475 } 476 477 // 5. verify 478 if (sigAlg == "none") { 479 throw "not supported"; 480 } else if (sigAlg.substr(0, 4) == "Hmac") { 481 var hSig2 = null; 482 if (key === undefined) 483 throw "hexadecimal key shall be specified for HMAC"; 484 //try { 485 var mac = new _Mac({'alg': sigAlg, 'pass': key}); 486 mac.updateString(uSignatureInput); 487 hSig2 = mac.doFinal(); 488 //} catch(ex) {}; 489 return hSig == hSig2; 490 } else if (sigAlg.indexOf("withECDSA") != -1) { 491 var hASN1Sig = null; 492 try { 493 hASN1Sig = _ECDSA.concatSigToASN1Sig(hSig); 494 } catch (ex) { 495 return false; 496 } 497 var sig = new _Signature({'alg': sigAlg}); 498 sig.init(key) 499 sig.updateString(uSignatureInput); 500 return sig.verify(hASN1Sig); 501 } else { 502 var sig = new _Signature({'alg': sigAlg}); 503 sig.init(key) 504 sig.updateString(uSignatureInput); 505 return sig.verify(hSig); 506 } 507 }; 508 509 /** 510 * parse header and payload of JWS signature<br/> 511 * @name parse 512 * @memberOf KJUR.jws.JWS 513 * @function 514 * @static 515 * @param {String} sJWS string of JWS signature to parse 516 * @return {Array} associative array of parsed header and payload. See below. 517 * @throws if sJWS is malformed JWS signature 518 * @since jws 3.3.3 519 * @description 520 * This method parses JWS signature string. 521 * Resulted associative array has following properties: 522 * <ul> 523 * <li>headerObj - JSON object of header</li> 524 * <li>payloadObj - JSON object of payload if payload is JSON string otherwise undefined</li> 525 * <li>headerPP - pretty printed JSON header by stringify</li> 526 * <li>payloadPP - pretty printed JSON payload by stringify if payload is JSON otherwise Base64URL decoded raw string of payload</li> 527 * <li>sigHex - hexadecimal string of signature</li> 528 * </ul> 529 * @example 530 * KJUR.jws.JWS.parse(sJWS) -> 531 * { 532 * headerObj: {"alg": "RS256", "typ": "JWS"}, 533 * payloadObj: {"product": "orange", "quantity": 100}, 534 * headerPP: 535 * '{ 536 * "alg": "RS256", 537 * "typ": "JWS" 538 * }', 539 * payloadPP: 540 * '{ 541 * "product": "orange", 542 * "quantity": 100 543 * }', 544 * sigHex: "91f3cd..." 545 * } 546 */ 547 KJUR.jws.JWS.parse = function(sJWS) { 548 var a = sJWS.split("."); 549 var result = {}; 550 var uHeader, uPayload, uSig; 551 if (a.length != 2 && a.length != 3) 552 throw "malformed sJWS: wrong number of '.' splitted elements"; 553 554 uHeader = a[0]; 555 uPayload = a[1]; 556 if (a.length == 3) uSig = a[2]; 557 558 result.headerObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(uHeader)); 559 result.payloadObj = KJUR.jws.JWS.readSafeJSONString(b64utoutf8(uPayload)); 560 561 result.headerPP = JSON.stringify(result.headerObj, null, " "); 562 if (result.payloadObj == null) { 563 result.payloadPP = b64utoutf8(uPayload); 564 } else { 565 result.payloadPP = JSON.stringify(result.payloadObj, null, " "); 566 } 567 568 if (uSig !== undefined) { 569 result.sigHex = b64utohex(uSig); 570 } 571 572 return result; 573 }; 574 575 /** 576 * @name verifyJWT 577 * @memberOf KJUR.jws.JWS 578 * @function 579 * @static 580 * @param {String} sJWT string of JSON Web Token(JWT) to verify 581 * @param {Object} key string of public key, certificate or key object to verify 582 * @param {Array} acceptField associative array of acceptable fields (OPTION) 583 * @return {Boolean} true if the JWT token is valid otherwise false 584 * @since jws 3.2.3 jsrsasign 4.8.0 585 * 586 * @description 587 * This method verifies a 588 * <a href="https://tools.ietf.org/html/rfc7519">RFC 7519</a> 589 * JSON Web Token(JWT). 590 * It will verify following: 591 * <ul> 592 * <li>Header.alg 593 * <ul> 594 * <li>alg is specified in JWT header.</li> 595 * <li>alg is included in acceptField.alg array. (MANDATORY)</li> 596 * <li>alg is proper for key.</li> 597 * </ul> 598 * </li> 599 * <li>Payload.iss (issuer) - Payload.iss is included in acceptField.iss array if specified. (OPTION)</li> 600 * <li>Payload.sub (subject) - Payload.sub is included in acceptField.sub array if specified. (OPTION)</li> 601 * <li>Payload.aud (audience) - Payload.aud is included in acceptField.aud array or 602 * the same as value if specified. (OPTION)</li> 603 * <li>Time validity 604 * <ul> 605 * <li> 606 * If acceptField.verifyAt as number of UNIX origin time is specifed for validation time, 607 * this method will verify at the time for it, otherwise current time will be used to verify. 608 * </li> 609 * <li> 610 * Clock of JWT generator or verifier can be fast or slow. If these clocks are 611 * very different, JWT validation may fail. To avoid such case, 'jsrsasign' supports 612 * 'acceptField.gracePeriod' parameter which specifies acceptable time difference 613 * of those clocks in seconds. So if you want to accept slow or fast in 2 hours, 614 * you can specify <code>acceptField.gracePeriod = 2 * 60 * 60;</code>. 615 * "gracePeriod" is zero by default. 616 * "gracePeriod" is supported since jsrsasign 5.0.12. 617 * </li> 618 * <li>Payload.exp (expire) - Validation time is smaller than Payload.exp + gracePeriod.</li> 619 * <li>Payload.nbf (not before) - Validation time is greater than Payload.nbf - gracePeriod.</li> 620 * <li>Payload.iat (issued at) - Validation time is greater than Payload.iat - gracePeriod.</li> 621 * </ul> 622 * </li> 623 * <li>Payload.jti (JWT id) - Payload.jti is included in acceptField.jti if specified. (OPTION)</li> 624 * <li>JWS signature of JWS is valid for specified key.</li> 625 * </ul> 626 * 627 * <h4>acceptField parameters</h4> 628 * Here is available acceptField argument parameters: 629 * <ul> 630 * <li>alg - array of acceptable signature algorithm names (ex. ["HS256", "HS384"])</li> 631 * <li>iss - array of acceptable issuer names (ex. ['http://foo.com'])</li> 632 * <li>sub - array of acceptable subject names (ex. ['mailto:john@foo.com'])</li> 633 * <li>aud - array of acceptable audience name (ex. ['http://foo.com'])</li> 634 * <li>jti - string of acceptable JWT ID (OPTION) (ex. 'id1234')</li> 635 * <li> 636 * verifyAt - time to verify 'nbf', 'iat' and 'exp' in UNIX seconds 637 * (OPTION) (ex. 1377663900). 638 * If this is not specified, current time of verifier will be used. 639 * {@link KJUR.jws.IntDate} may be useful to specify it. 640 * </li> 641 * <li>gracePeriod - acceptable time difference between signer and verifier 642 * in seconds (ex. 3600). If this is not specified, zero will be used.</li> 643 * </ul> 644 * 645 * @example 646 * // simple validation for HS256 647 * isValid = KJUR.jws.JWS.verifyJWT("eyJhbG...", "616161", {alg: ["HS256"]}), 648 * 649 * // full validation for RS or PS 650 * pubkey = KEYUTIL.getKey('-----BEGIN CERT...'); 651 * isValid = KJUR.jws.JWS.verifyJWT('eyJh...', pubkey, { 652 * alg: ['RS256', 'RS512', 'PS256', 'PS512'], 653 * iss: ['http://foo.com'], 654 * sub: ['mailto:john@foo.com', 'mailto:alice@foo.com'], 655 * verifyAt: KJUR.jws.IntDate.get('20150520235959Z'), 656 * aud: ['http://foo.com'], // aud: 'http://foo.com' is fine too. 657 * jti: 'id123456', 658 * gracePeriod: 1 * 60 * 60 // accept 1 hour slow or fast 659 * }); 660 */ 661 KJUR.jws.JWS.verifyJWT = function(sJWT, key, acceptField) { 662 var _KJUR = KJUR, 663 _KJUR_jws = _KJUR.jws, 664 _KJUR_jws_JWS = _KJUR_jws.JWS, 665 _readSafeJSONString = _KJUR_jws_JWS.readSafeJSONString, 666 _inArray = _KJUR_jws_JWS.inArray, 667 _includedArray = _KJUR_jws_JWS.includedArray; 668 669 // 0. checking dot concatinatd Base64URL encoded string 670 if (! isBase64URLDot(sJWT)) return false; 671 672 // 1. parse JWT 673 var a = sJWT.split("."); 674 if (a.length != 3) return false; 675 var uHeader = a[0]; 676 var uPayload = a[1]; 677 var uSignatureInput = uHeader + "." + uPayload; 678 var hSig = b64utohex(a[2]); 679 680 // 2. parse JWS header 681 var pHeader = _readSafeJSONString(b64utoutf8(uHeader)); 682 683 // 3. parse JWS payload 684 var pPayload = _readSafeJSONString(b64utoutf8(uPayload)); 685 686 // 4. algorithm ('alg' in header) check 687 if (pHeader.alg === undefined) return false; 688 if (acceptField.alg === undefined) 689 throw "acceptField.alg shall be specified"; 690 if (! _inArray(pHeader.alg, acceptField.alg)) return false; 691 692 // 5. issuer ('iss' in payload) check 693 if (pPayload.iss !== undefined && typeof acceptField.iss === "object") { 694 if (! _inArray(pPayload.iss, acceptField.iss)) return false; 695 } 696 697 // 6. subject ('sub' in payload) check 698 if (pPayload.sub !== undefined && typeof acceptField.sub === "object") { 699 if (! _inArray(pPayload.sub, acceptField.sub)) return false; 700 } 701 702 // 7. audience ('aud' in payload) check 703 if (pPayload.aud !== undefined && typeof acceptField.aud === "object") { 704 if (typeof pPayload.aud == "string") { 705 if (! _inArray(pPayload.aud, acceptField.aud)) 706 return false; 707 } else if (typeof pPayload.aud == "object") { 708 if (! _includedArray(pPayload.aud, acceptField.aud)) 709 return false; 710 } 711 } 712 713 // 8. time validity 714 // (nbf - gracePeriod < now < exp + gracePeriod) && (iat - gracePeriod < now) 715 var now = _KJUR_jws.IntDate.getNow(); 716 if (acceptField.verifyAt !== undefined && typeof acceptField.verifyAt === "number") { 717 now = acceptField.verifyAt; 718 } 719 if (acceptField.gracePeriod === undefined || 720 typeof acceptField.gracePeriod !== "number") { 721 acceptField.gracePeriod = 0; 722 } 723 724 // 8.1 expired time 'exp' check 725 if (pPayload.exp !== undefined && typeof pPayload.exp == "number") { 726 if (pPayload.exp + acceptField.gracePeriod < now) return false; 727 } 728 729 // 8.2 not before time 'nbf' check 730 if (pPayload.nbf !== undefined && typeof pPayload.nbf == "number") { 731 if (now < pPayload.nbf - acceptField.gracePeriod) return false; 732 } 733 734 // 8.3 issued at time 'iat' check 735 if (pPayload.iat !== undefined && typeof pPayload.iat == "number") { 736 if (now < pPayload.iat - acceptField.gracePeriod) return false; 737 } 738 739 // 9 JWT id 'jti' check 740 if (pPayload.jti !== undefined && acceptField.jti !== undefined) { 741 if (pPayload.jti !== acceptField.jti) return false; 742 } 743 744 // 10 JWS signature check 745 if (! _KJUR_jws_JWS.verify(sJWT, key, acceptField.alg)) return false; 746 747 // 11 passed all check 748 return true; 749 }; 750 751 /** 752 * check whether array is included by another array 753 * @name includedArray 754 * @memberOf KJUR.jws.JWS 755 * @function 756 * @static 757 * @param {Array} a1 check whether set a1 is included by a2 758 * @param {Array} a2 check whether set a1 is included by a2 759 * @return {Boolean} check whether set a1 is included by a2 760 * @since jws 3.2.3 761 * This method verifies whether an array is included by another array. 762 * It doesn't care about item ordering in a array. 763 * @example 764 * KJUR.jws.JWS.includedArray(['b'], ['b', 'c', 'a']) => true 765 * KJUR.jws.JWS.includedArray(['a', 'b'], ['b', 'c', 'a']) => true 766 * KJUR.jws.JWS.includedArray(['a', 'b'], ['b', 'c']) => false 767 */ 768 KJUR.jws.JWS.includedArray = function(a1, a2) { 769 var _inArray = KJUR.jws.JWS.inArray; 770 if (a1 === null) return false; 771 if (typeof a1 !== "object") return false; 772 if (typeof a1.length !== "number") return false; 773 774 for (var i = 0; i < a1.length; i++) { 775 if (! _inArray(a1[i], a2)) return false; 776 } 777 return true; 778 }; 779 780 /** 781 * check whether item is included by array 782 * @name inArray 783 * @memberOf KJUR.jws.JWS 784 * @function 785 * @static 786 * @param {String} item check whether item is included by array 787 * @param {Array} a check whether item is included by array 788 * @return {Boolean} check whether item is included by array 789 * @since jws 3.2.3 790 * This method verifies whether an item is included by an array. 791 * It doesn't care about item ordering in an array. 792 * @example 793 * KJUR.jws.JWS.inArray('b', ['b', 'c', 'a']) => true 794 * KJUR.jws.JWS.inArray('a', ['b', 'c', 'a']) => true 795 * KJUR.jws.JWS.inArray('a', ['b', 'c']) => false 796 */ 797 KJUR.jws.JWS.inArray = function(item, a) { 798 if (a === null) return false; 799 if (typeof a !== "object") return false; 800 if (typeof a.length !== "number") return false; 801 for (var i = 0; i < a.length; i++) { 802 if (a[i] == item) return true; 803 } 804 return false; 805 }; 806 807 /** 808 * static associative array of general signature algorithm name from JWS algorithm name 809 * @since jws 3.0.0 810 */ 811 KJUR.jws.JWS.jwsalg2sigalg = { 812 "HS256": "HmacSHA256", 813 "HS384": "HmacSHA384", 814 "HS512": "HmacSHA512", 815 "RS256": "SHA256withRSA", 816 "RS384": "SHA384withRSA", 817 "RS512": "SHA512withRSA", 818 "ES256": "SHA256withECDSA", 819 "ES384": "SHA384withECDSA", 820 "ES512": "SHA512withECDSA", 821 "PS256": "SHA256withRSAandMGF1", 822 "PS384": "SHA384withRSAandMGF1", 823 "PS512": "SHA512withRSAandMGF1", 824 "none": "none", 825 }; 826 827 // === utility static method ================================================== 828 829 /** 830 * check whether a String "s" is a safe JSON string or not.<br/> 831 * If a String "s" is a malformed JSON string or an other object type 832 * this returns 0, otherwise this returns 1. 833 * @name isSafeJSONString 834 * @memberOf KJUR.jws.JWS 835 * @function 836 * @static 837 * @param {String} s JSON string 838 * @return {Number} 1 or 0 839 */ 840 KJUR.jws.JWS.isSafeJSONString = function(s, h, p) { 841 var o = null; 842 try { 843 o = jsonParse(s); 844 if (typeof o != "object") return 0; 845 if (o.constructor === Array) return 0; 846 if (h) h[p] = o; 847 return 1; 848 } catch (ex) { 849 return 0; 850 } 851 }; 852 853 /** 854 * read a String "s" as JSON object if it is safe.<br/> 855 * If a String "s" is a malformed JSON string or not JSON string, 856 * this returns null, otherwise returns JSON object. 857 * @name readSafeJSONString 858 * @memberOf KJUR.jws.JWS 859 * @function 860 * @static 861 * @param {String} s JSON string 862 * @return {Object} JSON object or null 863 * @since 1.1.1 864 */ 865 KJUR.jws.JWS.readSafeJSONString = function(s) { 866 var o = null; 867 try { 868 o = jsonParse(s); 869 if (typeof o != "object") return null; 870 if (o.constructor === Array) return null; 871 return o; 872 } catch (ex) { 873 return null; 874 } 875 }; 876 877 /** 878 * get Encoed Signature Value from JWS string.<br/> 879 * @name getEncodedSignatureValueFromJWS 880 * @memberOf KJUR.jws.JWS 881 * @function 882 * @static 883 * @param {String} sJWS JWS signature string to be verified 884 * @return {String} string of Encoded Signature Value 885 * @throws if sJWS is not comma separated string such like "Header.Payload.Signature". 886 */ 887 KJUR.jws.JWS.getEncodedSignatureValueFromJWS = function(sJWS) { 888 var matchResult = sJWS.match(/^[^.]+\.[^.]+\.([^.]+)$/); 889 if (matchResult == null) { 890 throw "JWS signature is not a form of 'Head.Payload.SigValue'."; 891 } 892 return matchResult[1]; 893 }; 894 895 /** 896 * get RFC 7638 JWK thumbprint from JWK object 897 * @name getJWKthumbprint 898 * @memberOf KJUR.jws.JWS 899 * @function 900 * @static 901 * @param {Object} o JWK object to be calculated thumbprint 902 * @return {String} Base64 URL encoded JWK thumbprint value 903 * @since jsrsasign 5.0.2 jws 3.3.2 904 * @description 905 * This method calculates JWK thmubprint for specified JWK object 906 * as described in 907 * <a href="https://tools.ietf.org/html/rfc7638">RFC 7638</a>. 908 * It supports all type of "kty". (i.e. "RSA", "EC" and "oct" 909 * (for symmetric key)) 910 * Working sample is 911 * <a href="https://kjur.github.io/jsrsasign/sample/tool_jwktp.html">here</a>. 912 * @example 913 * jwk = {"kty":"RSA", "n":"0vx...", "e":"AQAB", ...}; 914 * thumbprint = KJUR.jws.JWS.getJWKthumbprint(jwk); 915 */ 916 KJUR.jws.JWS.getJWKthumbprint = function(o) { 917 if (o.kty !== "RSA" && 918 o.kty !== "EC" && 919 o.kty !== "oct") 920 throw "unsupported algorithm for JWK Thumprint"; 921 922 // 1. get canonically ordered json string 923 var s = '{'; 924 if (o.kty === "RSA") { 925 if (typeof o.n != "string" || typeof o.e != "string") 926 throw "wrong n and e value for RSA key"; 927 s += '"' + 'e' + '":"' + o.e + '",'; 928 s += '"' + 'kty' + '":"' + o.kty + '",'; 929 s += '"' + 'n' + '":"' + o.n + '"}'; 930 } else if (o.kty === "EC") { 931 if (typeof o.crv != "string" || 932 typeof o.x != "string" || 933 typeof o.y != "string") 934 throw "wrong crv, x and y value for EC key"; 935 s += '"' + 'crv' + '":"' + o.crv + '",'; 936 s += '"' + 'kty' + '":"' + o.kty + '",'; 937 s += '"' + 'x' + '":"' + o.x + '",'; 938 s += '"' + 'y' + '":"' + o.y + '"}'; 939 } else if (o.kty === "oct") { 940 if (typeof o.k != "string") 941 throw "wrong k value for oct(symmetric) key"; 942 s += '"' + 'kty' + '":"' + o.kty + '",'; 943 s += '"' + 'k' + '":"' + o.k + '"}'; 944 } 945 //alert(s); 946 947 // 2. get thumb print 948 var hJWK = rstrtohex(s); 949 var hash = KJUR.crypto.Util.hashHex(hJWK, "sha256"); 950 var hashB64U = hextob64u(hash); 951 952 return hashB64U; 953 }; 954 955 /** 956 * IntDate class for time representation for JSON Web Token(JWT) 957 * @class KJUR.jws.IntDate class 958 * @name KJUR.jws.IntDate 959 * @since jws 3.0.1 960 * @description 961 * Utility class for IntDate which is integer representation of UNIX origin time 962 * used in JSON Web Token(JWT). 963 */ 964 KJUR.jws.IntDate = {}; 965 966 /** 967 * get UNIX origin time from by string 968 * @name get 969 * @memberOf KJUR.jws.IntDate 970 * @function 971 * @static 972 * @param {String} s string of time representation 973 * @return {Integer} UNIX origin time in seconds for argument 's' 974 * @since jws 3.0.1 975 * @throws "unsupported format: s" when malformed format 976 * @description 977 * This method will accept following representation of time. 978 * <ul> 979 * <li>now - current time</li> 980 * <li>now + 1hour - after 1 hour from now</li> 981 * <li>now + 1day - after 1 day from now</li> 982 * <li>now + 1month - after 30 days from now</li> 983 * <li>now + 1year - after 365 days from now</li> 984 * <li>YYYYmmDDHHMMSSZ - UTC time (ex. 20130828235959Z)</li> 985 * <li>number - UNIX origin time (seconds from 1970-01-01 00:00:00) (ex. 1377714748)</li> 986 * </ul> 987 */ 988 KJUR.jws.IntDate.get = function(s) { 989 var _KJUR_jws_IntDate = KJUR.jws.IntDate, 990 _getNow = _KJUR_jws_IntDate.getNow, 991 _getZulu = _KJUR_jws_IntDate.getZulu; 992 993 if (s == "now") { 994 return _getNow(); 995 } else if (s == "now + 1hour") { 996 return _getNow() + 60 * 60; 997 } else if (s == "now + 1day") { 998 return _getNow() + 60 * 60 * 24; 999 } else if (s == "now + 1month") { 1000 return _getNow() + 60 * 60 * 24 * 30; 1001 } else if (s == "now + 1year") { 1002 return _getNow() + 60 * 60 * 24 * 365; 1003 } else if (s.match(/Z$/)) { 1004 return _getZulu(s); 1005 } else if (s.match(/^[0-9]+$/)) { 1006 return parseInt(s); 1007 } 1008 throw "unsupported format: " + s; 1009 }; 1010 1011 /** 1012 * get UNIX origin time from Zulu time representation string 1013 * @name getZulu 1014 * @memberOf KJUR.jws.IntDate 1015 * @function 1016 * @static 1017 * @param {String} s string of Zulu time representation (ex. 20151012125959Z) 1018 * @return {Integer} UNIX origin time in seconds for argument 's' 1019 * @since jws 3.0.1 1020 * @throws "unsupported format: s" when malformed format 1021 * @description 1022 * This method provides UNIX origin time from Zulu time. 1023 * Following representations are supported: 1024 * <ul> 1025 * <li>YYYYMMDDHHmmSSZ - GeneralizedTime format</li> 1026 * <li>YYMMDDHHmmSSZ - UTCTime format. If YY is greater or equal to 1027 * 50 then it represents 19YY otherwise 20YY.</li> 1028 * </ul> 1029 * @example 1030 * KJUR.jws.IntDate.getZulu("20151012125959Z") => 1478... 1031 * KJUR.jws.IntDate.getZulu("151012125959Z") => 1478... 1032 */ 1033 KJUR.jws.IntDate.getZulu = function(s) { 1034 return zulutosec(s); 1035 }; 1036 1037 /** 1038 * get UNIX origin time of current time 1039 * @name getNow 1040 * @memberOf KJUR.jws.IntDate 1041 * @function 1042 * @static 1043 * @return {Integer} UNIX origin time for current time 1044 * @since jws 3.0.1 1045 * @description 1046 * This method provides UNIX origin time for current time 1047 * @example 1048 * KJUR.jws.IntDate.getNow() => 1478... 1049 */ 1050 KJUR.jws.IntDate.getNow = function() { 1051 var d = ~~(new Date() / 1000); 1052 return d; 1053 }; 1054 1055 /** 1056 * get UTC time string from UNIX origin time value 1057 * @name intDate2UTCString 1058 * @memberOf KJUR.jws.IntDate 1059 * @function 1060 * @static 1061 * @param {Integer} intDate UNIX origin time value (ex. 1478...) 1062 * @return {String} UTC time string 1063 * @since jws 3.0.1 1064 * @description 1065 * This method provides UTC time string for UNIX origin time value. 1066 * @example 1067 * KJUR.jws.IntDate.intDate2UTCString(1478...) => "2015 Oct ..." 1068 */ 1069 KJUR.jws.IntDate.intDate2UTCString = function(intDate) { 1070 var d = new Date(intDate * 1000); 1071 return d.toUTCString(); 1072 }; 1073 1074 /** 1075 * get UTC time string from UNIX origin time value 1076 * @name intDate2Zulu 1077 * @memberOf KJUR.jws.IntDate 1078 * @function 1079 * @static 1080 * @param {Integer} intDate UNIX origin time value (ex. 1478...) 1081 * @return {String} Zulu time string 1082 * @since jws 3.0.1 1083 * @description 1084 * This method provides Zulu time string for UNIX origin time value. 1085 * @example 1086 * KJUR.jws.IntDate.intDate2UTCString(1478...) => "20151012...Z" 1087 */ 1088 KJUR.jws.IntDate.intDate2Zulu = function(intDate) { 1089 var d = new Date(intDate * 1000), 1090 year = ("0000" + d.getUTCFullYear()).slice(-4), 1091 mon = ("00" + (d.getUTCMonth() + 1)).slice(-2), 1092 day = ("00" + d.getUTCDate()).slice(-2), 1093 hour = ("00" + d.getUTCHours()).slice(-2), 1094 min = ("00" + d.getUTCMinutes()).slice(-2), 1095 sec = ("00" + d.getUTCSeconds()).slice(-2); 1096 return year + mon + day + hour + min + sec + "Z"; 1097 }; 1098 1099