diff --git a/api/files.html b/api/files.html index b5874659..b2ce4865 100644 --- a/api/files.html +++ b/api/files.html @@ -535,7 +535,7 @@
pPBES2 = { + "prf": "hmacWithSHA256", + "salt": "1234567890abcdef", + "iter": 2048, + "encalg": "aes256-CBC", + "enciv": "12ab...", + "enc": "34cd..." +} +KEYUTIL.getDKFromPBES2Param(pPBES2, "passwd") → "3ab10fd..."+ + + + +
// to convert plain PKCS#5 private key to encrypted PKCS#8 private // key with PBKDF2 with TripleDES -% openssl pkcs8 -in plain_p5.pem -topk8 -v2 -des3 -out encrypted_p8.pem+% openssl pkcs8 -in plain_p5.pem -topk8 -v2 des3 -out encrypted_p8.pem @@ -1861,6 +1976,13 @@
pem = "-----BEGIN ENCRYPTED PRIVATE KEY..."; +KEYUTIL.getPlainHexFromEncryptedPKCS8PEM(pem, "passwd") → "3082..."+ + + + +
KEYUTIL.parsePBES2("3082...") → +{ + "prf": "hmacWithSHA256", + "salt": "1234567890abcdef", + "iter": 2048, + "encalg": "aes256-CBC", + "enciv": "12ab...", + "enc": "34cd..." +} + +// to convert plain PKCS#5 private key to encrypted PKCS#8 private +// key with PBKDF2 with TripleDES +% openssl pkcs8 -in plain_p5.pem -topk8 -v2 des3 -out encrypted_p8.pem+ + + + +
+// asynchronous cipher KJUR.crypto.Cipher.decrypt("aaa", prvRSAKeyObj) → "1abc2d..." -KJUR.crypto.Cipher.decrypt("aaa", prvRSAKeyObj, "RSAOAEP) → "23ab02..."+KJUR.crypto.Cipher.decrypt("aaa", prvRSAKeyObj, "RSAOAEP) → "23ab02..." +// synchronous cipher +KJUR.crypto.Cipher.decrypt("12abcd...", "5a7d...", "aes256-CBC", { iv: "1b3c..." }) +KJUR.crypto.Cipher.decrypt("12abcd...", "5a7d...", any, { encalg: "aes128-CBC", iv: "1b3c..." }) +KJUR.crypto.Cipher.decrypt("12abcd...", any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41..." }) +KJUR.crypto.Cipher.decrypt(any, any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41...", enc: "12abcd..." }) @@ -739,22 +747,28 @@
+// asynchronous cipher KJUR.crypto.Cipher.encrypt("aaa", pubRSAKeyObj) → "1abc2d..." -KJUR.crypto.Cipher.encrypt("aaa", pubRSAKeyObj, "RSAOAEP") → "23ab02..."+KJUR.crypto.Cipher.encrypt("aaa", pubRSAKeyObj, "RSAOAEP") → "23ab02..." +// synchronous cipher +KJUR.crypto.Cipher.encrypt("12abcd...", "5a7d...", "aes256-CBC", { iv: "1b3c..." }) +KJUR.crypto.Cipher.encrypt("12abcd...", "5a7d...", any, { encalg: "aes128-CBC", iv: "1b3c..." }) +KJUR.crypto.Cipher.encrypt("12abcd...", any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41..." }) +KJUR.crypto.Cipher.encrypt(any, any, any, { encalg: "des-EDE3-CBC", iv: "1b3c...", key: "3d41...", enc: "12abcd..." }) @@ -822,7 +844,13 @@
inttohex(1) → "01" +inttohex(-1) → "ff" +inttohex(2048) → "0800" +inttohex(-2048) → "f800"+ + + + +
twoscompl(new BigInteger("1", 10)) → "01" +twoscompl(new BigInteger("-1", 10)) → "ff"+ + + + +
1 /* asn1-1.0.27.js (c) 2013-2023 Kenji Urushima | kjur.github.io/jsrsasign/license +1 /* asn1-1.0.28.js (c) 2013-2023 Kenji Urushima | kjur.github.io/jsrsasign/license 2 */ 3 /* 4 * asn1.js - ASN.1 DER encoder classes @@ -23,7 +23,7 @@ 16 * @fileOverview 17 * @name asn1-1.0.js 18 * @author Kenji Urushima kenji.urushima@gmail.com - 19 * @version jsrsasign 10.8.0 asn1 1.0.27 (2023-Apr-08) + 19 * @version jsrsasign 10.9.0 asn1 1.0.28 (2023-Nov-27) 20 * @since jsrsasign 2.1 21 * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a> 22 */ @@ -110,1762 +110,1735 @@ 103 if ((h.length % 2) == 1) h = '0' + h; 104 return h; 105 }; -106 this.bigIntToMinTwosComplementsHex = function(bigIntegerValue) { -107 var h = bigIntegerValue.toString(16); -108 if (h.substr(0, 1) != '-') { -109 if (h.length % 2 == 1) { -110 h = '0' + h; -111 } else { -112 if (! h.match(/^[0-7]/)) { -113 h = '00' + h; -114 } -115 } -116 } else { -117 var hPos = h.substr(1); -118 var xorLen = hPos.length; -119 if (xorLen % 2 == 1) { -120 xorLen += 1; -121 } else { -122 if (! h.match(/^[0-7]/)) { -123 xorLen += 2; -124 } -125 } -126 var hMask = ''; -127 for (var i = 0; i < xorLen; i++) { -128 hMask += 'f'; -129 } -130 var biMask = new BigInteger(hMask, 16); -131 var biNeg = biMask.xor(bigIntegerValue).add(BigInteger.ONE); -132 h = biNeg.toString(16).replace(/^-/, ''); -133 } -134 return h; -135 }; -136 /** -137 * get PEM string from hexadecimal data and header string -138 * @name getPEMStringFromHex -139 * @memberOf KJUR.asn1.ASN1Util -140 * @function -141 * @param {String} dataHex hexadecimal string of PEM body -142 * @param {String} pemHeader PEM header string (ex. 'RSA PRIVATE KEY') -143 * @return {String} PEM formatted string of input data -144 * @description -145 * This method converts a hexadecimal string to a PEM string with -146 * a specified header. Its line break will be CRLF("\r\n"). -147 * @example -148 * var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex('616161', 'RSA PRIVATE KEY'); -149 * // value of pem will be: -150 * -----BEGIN PRIVATE KEY----- -151 * YWFh -152 * -----END PRIVATE KEY----- -153 */ -154 this.getPEMStringFromHex = function(dataHex, pemHeader) { -155 return hextopem(dataHex, pemHeader); -156 }; -157 -158 /** -159 * generate ASN1Object specifed by JSON parameters -160 * @name newObject -161 * @memberOf KJUR.asn1.ASN1Util -162 * @function -163 * @param {Array} param JSON parameter to generate ASN1Object -164 * @return {KJUR.asn1.ASN1Object} generated object -165 * @since asn1 1.0.3 -166 * @description -167 * generate any ASN1Object specified by JSON param -168 * including ASN.1 primitive or structured. -169 * Generally 'param' can be described as follows: -170 * <blockquote> -171 * {TYPE-OF-ASNOBJ: ASN1OBJ-PARAMETER} -172 * </blockquote> -173 * 'TYPE-OF-ASN1OBJ' can be one of following symbols: -174 * <ul> -175 * <li>'bool' - {@link KJUR.asn1.DERBoolean}</li> -176 * <li>'int' - {@link KJUR.asn1.DERInteger}</li> -177 * <li>'bitstr' - {@link KJUR.asn1.DERBitString}</li> -178 * <li>'octstr' - {@link KJUR.asn1.DEROctetString}</li> -179 * <li>'null' - {@link KJUR.asn1.DERNull}</li> -180 * <li>'oid' - {@link KJUR.asn1.DERObjectIdentifier}</li> -181 * <li>'enum' - {@link KJUR.asn1.DEREnumerated}</li> -182 * <li>'utf8str' - {@link KJUR.asn1.DERUTF8String}</li> -183 * <li>'numstr' - {@link KJUR.asn1.DERNumericString}</li> -184 * <li>'prnstr' - {@link KJUR.asn1.DERPrintableString}</li> -185 * <li>'telstr' - {@link KJUR.asn1.DERTeletexString}</li> -186 * <li>'ia5str' - {@link KJUR.asn1.DERIA5String}</li> -187 * <li>'utctime' - {@link KJUR.asn1.DERUTCTime}</li> -188 * <li>'gentime' - {@link KJUR.asn1.DERGeneralizedTime}</li> -189 * <li>'visstr' - {@link KJUR.asn1.DERVisibleString}</li> -190 * <li>'bmpstr' - {@link KJUR.asn1.DERBMPString}</li> -191 * <li>'seq' - {@link KJUR.asn1.DERSequence}</li> -192 * <li>'set' - {@link KJUR.asn1.DERSet}</li> -193 * <li>'tag' - {@link KJUR.asn1.DERTaggedObject}</li> -194 * <li>'asn1' - {@link KJUR.asn1.ASN1Object}</li> -195 * </ul> -196 * <br/> -197 * NOTE: Structured object such as SEQUENCE or SET can conclude -198 * ASN1Object as well as JSON parameters since jsrsasign 9.0.0. -199 * -200 * @example -201 * newObject({'prnstr': 'aaa'}); -202 * newObject({'seq': [{'int': 3}, {'prnstr': 'aaa'}]}) -203 * newObject({seq: [{int: 3}, new DERInteger({int: 3})]}) // mixed -204 * // ASN.1 Tagged Object -205 * newObject({'tag': {'tag': 'a1', -206 * 'explicit': true, -207 * 'obj': {'seq': [{'int': 3}, {'prnstr': 'aaa'}]}}}); -208 * // more simple representation of ASN.1 Tagged Object -209 * newObject({'tag': ['a1', -210 * true, -211 * {'seq': [ -212 * {'int': 3}, -213 * {'prnstr': 'aaa'}]} -214 * ]}); -215 */ -216 this.newObject = function(param) { -217 var _KJUR = KJUR, -218 _KJUR_asn1 = _KJUR.asn1, -219 _ASN1Object = _KJUR_asn1.ASN1Object, -220 _DERBoolean = _KJUR_asn1.DERBoolean, -221 _DERInteger = _KJUR_asn1.DERInteger, -222 _DERBitString = _KJUR_asn1.DERBitString, -223 _DEROctetString = _KJUR_asn1.DEROctetString, -224 _DERNull = _KJUR_asn1.DERNull, -225 _DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier, -226 _DEREnumerated = _KJUR_asn1.DEREnumerated, -227 _DERUTF8String = _KJUR_asn1.DERUTF8String, -228 _DERNumericString = _KJUR_asn1.DERNumericString, -229 _DERPrintableString = _KJUR_asn1.DERPrintableString, -230 _DERTeletexString = _KJUR_asn1.DERTeletexString, -231 _DERIA5String = _KJUR_asn1.DERIA5String, -232 _DERUTCTime = _KJUR_asn1.DERUTCTime, -233 _DERGeneralizedTime = _KJUR_asn1.DERGeneralizedTime, -234 _DERVisibleString = _KJUR_asn1.DERVisibleString, -235 _DERBMPString = _KJUR_asn1.DERBMPString, -236 _DERSequence = _KJUR_asn1.DERSequence, -237 _DERSet = _KJUR_asn1.DERSet, -238 _DERTaggedObject = _KJUR_asn1.DERTaggedObject, -239 _newObject = _KJUR_asn1.ASN1Util.newObject; -240 -241 if (param instanceof _KJUR_asn1.ASN1Object) return param; -242 -243 var keys = Object.keys(param); -244 if (keys.length != 1) -245 throw new Error("key of param shall be only one."); -246 var key = keys[0]; -247 -248 if (":asn1:bool:int:bitstr:octstr:null:oid:enum:utf8str:numstr:prnstr:telstr:ia5str:utctime:gentime:visstr:bmpstr:seq:set:tag:".indexOf(":" + key + ":") == -1) -249 throw new Error("undefined key: " + key); -250 -251 if (key == "bool") return new _DERBoolean(param[key]); -252 if (key == "int") return new _DERInteger(param[key]); -253 if (key == "bitstr") return new _DERBitString(param[key]); -254 if (key == "octstr") return new _DEROctetString(param[key]); -255 if (key == "null") return new _DERNull(param[key]); -256 if (key == "oid") return new _DERObjectIdentifier(param[key]); -257 if (key == "enum") return new _DEREnumerated(param[key]); -258 if (key == "utf8str") return new _DERUTF8String(param[key]); -259 if (key == "numstr") return new _DERNumericString(param[key]); -260 if (key == "prnstr") return new _DERPrintableString(param[key]); -261 if (key == "telstr") return new _DERTeletexString(param[key]); -262 if (key == "ia5str") return new _DERIA5String(param[key]); -263 if (key == "utctime") return new _DERUTCTime(param[key]); -264 if (key == "gentime") return new _DERGeneralizedTime(param[key]); -265 if (key == "visstr") return new _DERVisibleString(param[key]); -266 if (key == "bmpstr") return new _DERBMPString(param[key]); -267 if (key == "asn1") return new _ASN1Object(param[key]); -268 -269 if (key == "seq") { -270 var paramList = param[key]; -271 var a = []; -272 for (var i = 0; i < paramList.length; i++) { -273 var asn1Obj = _newObject(paramList[i]); -274 a.push(asn1Obj); -275 } -276 return new _DERSequence({'array': a}); -277 } -278 -279 if (key == "set") { -280 var paramList = param[key]; -281 var a = []; -282 for (var i = 0; i < paramList.length; i++) { -283 var asn1Obj = _newObject(paramList[i]); -284 a.push(asn1Obj); -285 } -286 return new _DERSet({'array': a}); -287 } -288 -289 if (key == "tag") { -290 var tagParam = param[key]; -291 if (Object.prototype.toString.call(tagParam) === '[object Array]' && -292 tagParam.length == 3) { -293 var obj = _newObject(tagParam[2]); -294 return new _DERTaggedObject({tag: tagParam[0], -295 explicit: tagParam[1], -296 obj: obj}); -297 } else { -298 return new _DERTaggedObject(tagParam); -299 } -300 } -301 }; -302 -303 /** -304 * get encoded hexadecimal string of ASN1Object specifed by JSON parameters -305 * @name jsonToASN1HEX -306 * @memberOf KJUR.asn1.ASN1Util -307 * @function -308 * @param {Array} param JSON parameter to generate ASN1Object -309 * @return hexadecimal string of ASN1Object -310 * @since asn1 1.0.4 -311 * @description -312 * As for ASN.1 object representation of JSON object, -313 * please see {@link newObject}. -314 * @example -315 * jsonToASN1HEX({'prnstr': 'aaa'}); -316 */ -317 this.jsonToASN1HEX = function(param) { -318 var asn1Obj = this.newObject(param); -319 return asn1Obj.tohex(); -320 }; -321 }; -322 -323 /** -324 * get dot noted oid number string from hexadecimal value of OID -325 * @name oidHexToInt -326 * @memberOf KJUR.asn1.ASN1Util -327 * @function -328 * @param {String} hex hexadecimal value of object identifier -329 * @return {String} dot noted string of object identifier -330 * @since jsrsasign 4.8.3 asn1 1.0.7 -331 * @description -332 * This static method converts from hexadecimal string representation of -333 * ASN.1 value of object identifier to oid number string. -334 * @example -335 * KJUR.asn1.ASN1Util.oidHexToInt('550406') → "2.5.4.6" -336 */ -337 KJUR.asn1.ASN1Util.oidHexToInt = function(hex) { -338 var s = ""; -339 var i01 = parseInt(hex.substr(0, 2), 16); -340 var i0 = Math.floor(i01 / 40); -341 var i1 = i01 % 40; -342 var s = i0 + "." + i1; -343 -344 var binbuf = ""; -345 for (var i = 2; i < hex.length; i += 2) { -346 var value = parseInt(hex.substr(i, 2), 16); -347 var bin = ("00000000" + value.toString(2)).slice(- 8); -348 binbuf = binbuf + bin.substr(1, 7); -349 if (bin.substr(0, 1) == "0") { -350 var bi = new BigInteger(binbuf, 2); -351 s = s + "." + bi.toString(10); -352 binbuf = ""; -353 } -354 }; -355 -356 return s; -357 }; -358 -359 /** -360 * get hexadecimal value of object identifier from dot noted oid value (DEPRECATED) -361 * @name oidIntToHex -362 * @memberOf KJUR.asn1.ASN1Util -363 * @function -364 * @param {String} oidString dot noted string of object identifier -365 * @return {String} hexadecimal value of object identifier -366 * @since jsrsasign 4.8.3 asn1 1.0.7 -367 * @see {@link ASN1HEX.hextooidstr} -368 * @deprecated from jsrsasign 10.0.6. please use {@link oidtohex} -369 * -370 * @description -371 * This static method converts from object identifier value string. -372 * to hexadecimal string representation of it. -373 * {@link ASN1HEX.hextooidstr} is a reverse function of this. -374 * @example -375 * KJUR.asn1.ASN1Util.oidIntToHex("2.5.4.6") → "550406" -376 */ -377 KJUR.asn1.ASN1Util.oidIntToHex = function(oidString) { -378 var itox = function(i) { -379 var h = i.toString(16); -380 if (h.length == 1) h = '0' + h; -381 return h; -382 }; -383 -384 var roidtox = function(roid) { -385 var h = ''; -386 var bi = new BigInteger(roid, 10); -387 var b = bi.toString(2); -388 var padLen = 7 - b.length % 7; -389 if (padLen == 7) padLen = 0; -390 var bPad = ''; -391 for (var i = 0; i < padLen; i++) bPad += '0'; -392 b = bPad + b; -393 for (var i = 0; i < b.length - 1; i += 7) { -394 var b8 = b.substr(i, 7); -395 if (i != b.length - 7) b8 = '1' + b8; -396 h += itox(parseInt(b8, 2)); -397 } -398 return h; -399 }; -400 -401 if (! oidString.match(/^[0-9.]+$/)) { -402 throw "malformed oid string: " + oidString; -403 } -404 var h = ''; -405 var a = oidString.split('.'); -406 var i0 = parseInt(a[0]) * 40 + parseInt(a[1]); -407 h += itox(i0); -408 a.splice(0, 2); -409 for (var i = 0; i < a.length; i++) { -410 h += roidtox(a[i]); -411 } -412 return h; -413 }; -414 -415 -416 // ******************************************************************** -417 // Abstract ASN.1 Classes -418 // ******************************************************************** -419 -420 // ******************************************************************** -421 -422 /** -423 * base class for ASN.1 DER encoder object<br/> -424 * @name KJUR.asn1.ASN1Object -425 * @class base class for ASN.1 DER encoder object -426 * @param {Array} params JSON object parameter for constructor -427 * @property {Boolean} isModified flag whether internal data was changed -428 * @property {Array} params JSON object parameter for ASN.1 encode -429 * @property {String} hTLV hexadecimal string of ASN.1 TLV -430 * @property {String} hT hexadecimal string of ASN.1 TLV tag(T) -431 * @property {String} hL hexadecimal string of ASN.1 TLV length(L) -432 * @property {String} hV hexadecimal string of ASN.1 TLV value(V) -433 * -434 * @description -435 * This class is ASN.1 DER object encode base class. -436 * -437 * @example -438 * new KJUR.asn1.ASN1Object({tlv: "030101"}) -439 */ -440 KJUR.asn1.ASN1Object = function(params) { -441 var isModified = true; -442 var hTLV = null; -443 var hT = '00'; -444 var hL = '00'; -445 var hV = ''; -446 this.params = null; -447 -448 /** -449 * get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)<br/> -450 * @name getLengthHexFromValue -451 * @memberOf KJUR.asn1.ASN1Object# -452 * @function -453 * @return {String} hexadecimal string of ASN.1 TLV length(L) -454 */ -455 this.getLengthHexFromValue = function() { -456 if (typeof this.hV == "undefined" || this.hV == null) { -457 throw new Error("this.hV is null or undefined"); -458 } -459 if (this.hV.length % 2 == 1) { -460 throw new Error("value hex must be even length: n=" + -461 hV.length + ",v=" + this.hV); -462 } -463 var n = this.hV.length / 2; -464 var hN = n.toString(16); -465 if (hN.length % 2 == 1) { -466 hN = "0" + hN; -467 } -468 if (n < 128) { -469 return hN; -470 } else { -471 var hNlen = hN.length / 2; -472 if (hNlen > 15) { -473 throw new Error("ASN.1 length too long to represent by 8x: n = " -474 + n.toString(16)); -475 } -476 var head = 128 + hNlen; -477 return head.toString(16) + hN; -478 } -479 }; -480 -481 /** -482 * get hexadecimal string of ASN.1 TLV bytes<br/> -483 * @name tohex -484 * @memberOf KJUR.asn1.ASN1Object# -485 * @function -486 * @return {String} hexadecimal string of ASN.1 TLV -487 * @since jsrsasign 10.5.16 asn1 1.0.24 -488 * @see KJUR.asn1.ASN1Object#getEncodedHex -489 * @example -490 * ...ASN1ObjectInstance.tohex() → "3003020101" -491 */ -492 this.tohex = function() { -493 if (this.hTLV == null || this.isModified) { -494 this.hV = this.getFreshValueHex(); -495 this.hL = this.getLengthHexFromValue(); -496 this.hTLV = this.hT + this.hL + this.hV; -497 this.isModified = false; -498 //alert("first time: " + this.hTLV); -499 } -500 return this.hTLV; -501 }; -502 -503 /** -504 * get hexadecimal string of ASN.1 TLV bytes (DEPRECATED)<br/> -505 * @name getEncodedHex -506 * @memberOf KJUR.asn1.ASN1Object# -507 * @function -508 * @return {String} hexadecimal string of ASN.1 TLV -509 * @deprecated since jsrsasign 10.5.16 please use {@link KJUR.asn1.ASN1Object#tohex} -510 */ -511 this.getEncodedHex = function() { return this.tohex(); }; -512 -513 /** -514 * get hexadecimal string of ASN.1 TLV value(V) bytes -515 * @name getValueHex -516 * @memberOf KJUR.asn1.ASN1Object# -517 * @function -518 * @return {String} hexadecimal string of ASN.1 TLV value(V) bytes -519 */ -520 this.getValueHex = function() { -521 this.tohex(); -522 return this.hV; -523 } -524 -525 this.getFreshValueHex = function() { -526 return ''; -527 }; -528 -529 this.setByParam = function(params) { -530 this.params = params; -531 }; -532 -533 if (params != undefined) { -534 if (params.tlv != undefined) { -535 this.hTLV = params.tlv; -536 this.isModified = false; -537 } -538 } -539 }; -540 -541 // == BEGIN DERAbstractString ================================================ -542 /** -543 * base class for ASN.1 DER string classes -544 * @name KJUR.asn1.DERAbstractString -545 * @class base class for ASN.1 DER string classes -546 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -547 * @property {String} s internal string of value -548 * @extends KJUR.asn1.ASN1Object -549 * @description -550 * <br/> -551 * As for argument 'params' for constructor, you can specify one of -552 * following properties: -553 * <ul> -554 * <li>str - specify initial ASN.1 value(V) by a string</li> -555 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -556 * </ul> -557 * NOTE: 'params' can be omitted. -558 */ -559 KJUR.asn1.DERAbstractString = function(params) { -560 KJUR.asn1.DERAbstractString.superclass.constructor.call(this); -561 var s = null; -562 var hV = null; -563 -564 /** -565 * get string value of this string object -566 * @name getString -567 * @memberOf KJUR.asn1.DERAbstractString# -568 * @function -569 * @return {String} string value of this string object -570 */ -571 this.getString = function() { -572 return this.s; -573 }; -574 -575 /** -576 * set value by a string -577 * @name setString -578 * @memberOf KJUR.asn1.DERAbstractString# -579 * @function -580 * @param {String} newS value by a string to set -581 * @description -582 * This method set value by string. <br/> -583 * NOTE: This method assumes that the argument string is -584 * UTF-8 encoded even though ASN.1 primitive -585 * such as IA5String or PrintableString doesn't -586 * support all of UTF-8 characters. -587 * @example -588 * o = new KJUR.asn1.DERIA5String(); -589 * o.setString("abc"); -590 * o.setString("あいう"); -591 */ -592 this.setString = function(newS) { -593 this.hTLV = null; -594 this.isModified = true; -595 this.s = newS; -596 this.hV = utf8tohex(this.s).toLowerCase(); -597 }; -598 -599 /** -600 * set value by a hexadecimal string -601 * @name setStringHex -602 * @memberOf KJUR.asn1.DERAbstractString# -603 * @function -604 * @param {String} newHexString value by a hexadecimal string to set -605 */ -606 this.setStringHex = function(newHexString) { -607 this.hTLV = null; -608 this.isModified = true; -609 this.s = null; -610 this.hV = newHexString; -611 }; -612 -613 this.getFreshValueHex = function() { -614 return this.hV; -615 }; -616 -617 if (typeof params != "undefined") { -618 if (typeof params == "string") { -619 this.setString(params); -620 } else if (typeof params['str'] != "undefined") { -621 this.setString(params['str']); -622 } else if (typeof params['hex'] != "undefined") { -623 this.setStringHex(params['hex']); -624 } -625 } -626 }; -627 extendClass(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object); -628 // == END DERAbstractString ================================================ -629 -630 // == BEGIN DERAbstractTime ================================================== -631 /** -632 * base class for ASN.1 DER Generalized/UTCTime class -633 * @name KJUR.asn1.DERAbstractTime -634 * @class base class for ASN.1 DER Generalized/UTCTime class -635 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) -636 * @extends KJUR.asn1.ASN1Object -637 * @description -638 * @see KJUR.asn1.ASN1Object - superclass -639 * @see KJUR.asn1.DERGeneralizedTime -640 * @see KJUR.asn1.DERUTCTime -641 * @see KJUR.asn1.x509.Time -642 */ -643 KJUR.asn1.DERAbstractTime = function(params) { -644 KJUR.asn1.DERAbstractTime.superclass.constructor.call(this); -645 var s = null; -646 var date = null; -647 -648 // --- PRIVATE METHODS -------------------- -649 this.localDateToUTC = function(d) { -650 var utc = d.getTime() + (d.getTimezoneOffset() * 60000); -651 var utcDate = new Date(utc); -652 return utcDate; -653 }; -654 -655 /* -656 * format date string by Data object -657 * @name formatDate -658 * @memberOf KJUR.asn1.AbstractTime; -659 * @param {Date} dateObject -660 * @param {string} type 'utc' or 'gen' -661 * @param {boolean} withMillis flag for with millisections or not -662 * @description -663 * 'withMillis' flag is supported from asn1 1.0.6. -664 */ -665 this.formatDate = function(dateObject, type, withMillis) { -666 var pad = this.zeroPadding; -667 var d = this.localDateToUTC(dateObject); -668 var year = String(d.getFullYear()); -669 if (type == 'utc') year = year.substr(2, 2); -670 var month = pad(String(d.getMonth() + 1), 2); -671 var day = pad(String(d.getDate()), 2); -672 var hour = pad(String(d.getHours()), 2); -673 var min = pad(String(d.getMinutes()), 2); -674 var sec = pad(String(d.getSeconds()), 2); -675 var s = year + month + day + hour + min + sec; -676 if (withMillis === true) { -677 var millis = d.getMilliseconds(); -678 if (millis != 0) { -679 var sMillis = pad(String(millis), 3); -680 sMillis = sMillis.replace(/[0]+$/, ""); -681 s = s + "." + sMillis; -682 } -683 } -684 return s + "Z"; -685 }; -686 -687 this.zeroPadding = function(s, len) { -688 if (s.length >= len) return s; -689 return new Array(len - s.length + 1).join('0') + s; -690 }; -691 -692 // --- PUBLIC METHODS -------------------- -693 -694 /** -695 * set parameter of time -696 * @name setByParam -697 * @memberOf KJUR.asn1.DERAbstractTime# -698 * @function -699 * @param {Object} params JSON object, Date object or string of time -700 * @since jsrsasign 10.4.1 asn1 1.0.22 -701 * -702 * NOTE: If a member "millis" has a value "true", -703 * a fraction of second will be specified for this object. -704 * This default is "false". -705 * -706 * @example -707 * d1 = new KJUR.asn1.DERGeneralizedTime(); -708 * d1.setByParam("20210930235959.123Z"); -709 * d1.setByParam({str: "20210930235959.123Z"}); -710 * -711 * d1.setByParam(new Date("2013/12/31 23:59:59.12")); -712 * date1 = new Date(Date.UTC(2021,8,31,23,59,59,123)); -713 * d1.setByParam(date1); -714 * d1.setByParam({date: date1}); -715 * d1.setByParam({date: date1, millis: true}); -716 */ -717 this.setByParam = function(params) { -718 this.hV = null; -719 this.hTLV = null; -720 this.params = params; +106 this.bigIntToMinTwosComplementsHex = function(bigIntegerValue) { // DEPRECATED. use twoscompl +107 return twoscompl(bigIntegerValue); +108 } +109 /** +110 * get PEM string from hexadecimal data and header string +111 * @name getPEMStringFromHex +112 * @memberOf KJUR.asn1.ASN1Util +113 * @function +114 * @param {String} dataHex hexadecimal string of PEM body +115 * @param {String} pemHeader PEM header string (ex. 'RSA PRIVATE KEY') +116 * @return {String} PEM formatted string of input data +117 * @description +118 * This method converts a hexadecimal string to a PEM string with +119 * a specified header. Its line break will be CRLF("\r\n"). +120 * @example +121 * var pem = KJUR.asn1.ASN1Util.getPEMStringFromHex('616161', 'RSA PRIVATE KEY'); +122 * // value of pem will be: +123 * -----BEGIN PRIVATE KEY----- +124 * YWFh +125 * -----END PRIVATE KEY----- +126 */ +127 this.getPEMStringFromHex = function(dataHex, pemHeader) { +128 return hextopem(dataHex, pemHeader); +129 }; +130 +131 /** +132 * generate ASN1Object specifed by JSON parameters +133 * @name newObject +134 * @memberOf KJUR.asn1.ASN1Util +135 * @function +136 * @param {Array} param JSON parameter to generate ASN1Object +137 * @return {KJUR.asn1.ASN1Object} generated object +138 * @since asn1 1.0.3 +139 * @description +140 * generate any ASN1Object specified by JSON param +141 * including ASN.1 primitive or structured. +142 * Generally 'param' can be described as follows: +143 * <blockquote> +144 * {TYPE-OF-ASNOBJ: ASN1OBJ-PARAMETER} +145 * </blockquote> +146 * 'TYPE-OF-ASN1OBJ' can be one of following symbols: +147 * <ul> +148 * <li>'bool' - {@link KJUR.asn1.DERBoolean}</li> +149 * <li>'int' - {@link KJUR.asn1.DERInteger}</li> +150 * <li>'bitstr' - {@link KJUR.asn1.DERBitString}</li> +151 * <li>'octstr' - {@link KJUR.asn1.DEROctetString}</li> +152 * <li>'null' - {@link KJUR.asn1.DERNull}</li> +153 * <li>'oid' - {@link KJUR.asn1.DERObjectIdentifier}</li> +154 * <li>'enum' - {@link KJUR.asn1.DEREnumerated}</li> +155 * <li>'utf8str' - {@link KJUR.asn1.DERUTF8String}</li> +156 * <li>'numstr' - {@link KJUR.asn1.DERNumericString}</li> +157 * <li>'prnstr' - {@link KJUR.asn1.DERPrintableString}</li> +158 * <li>'telstr' - {@link KJUR.asn1.DERTeletexString}</li> +159 * <li>'ia5str' - {@link KJUR.asn1.DERIA5String}</li> +160 * <li>'utctime' - {@link KJUR.asn1.DERUTCTime}</li> +161 * <li>'gentime' - {@link KJUR.asn1.DERGeneralizedTime}</li> +162 * <li>'visstr' - {@link KJUR.asn1.DERVisibleString}</li> +163 * <li>'bmpstr' - {@link KJUR.asn1.DERBMPString}</li> +164 * <li>'seq' - {@link KJUR.asn1.DERSequence}</li> +165 * <li>'set' - {@link KJUR.asn1.DERSet}</li> +166 * <li>'tag' - {@link KJUR.asn1.DERTaggedObject}</li> +167 * <li>'asn1' - {@link KJUR.asn1.ASN1Object}</li> +168 * </ul> +169 * <br/> +170 * NOTE: Structured object such as SEQUENCE or SET can conclude +171 * ASN1Object as well as JSON parameters since jsrsasign 9.0.0. +172 * +173 * @example +174 * newObject({'prnstr': 'aaa'}); +175 * newObject({'seq': [{'int': 3}, {'prnstr': 'aaa'}]}) +176 * newObject({seq: [{int: 3}, new DERInteger({int: 3})]}) // mixed +177 * // ASN.1 Tagged Object +178 * newObject({'tag': {'tag': 'a1', +179 * 'explicit': true, +180 * 'obj': {'seq': [{'int': 3}, {'prnstr': 'aaa'}]}}}); +181 * // more simple representation of ASN.1 Tagged Object +182 * newObject({'tag': ['a1', +183 * true, +184 * {'seq': [ +185 * {'int': 3}, +186 * {'prnstr': 'aaa'}]} +187 * ]}); +188 */ +189 this.newObject = function(param) { +190 var _KJUR = KJUR, +191 _KJUR_asn1 = _KJUR.asn1, +192 _ASN1Object = _KJUR_asn1.ASN1Object, +193 _DERBoolean = _KJUR_asn1.DERBoolean, +194 _DERInteger = _KJUR_asn1.DERInteger, +195 _DERBitString = _KJUR_asn1.DERBitString, +196 _DEROctetString = _KJUR_asn1.DEROctetString, +197 _DERNull = _KJUR_asn1.DERNull, +198 _DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier, +199 _DEREnumerated = _KJUR_asn1.DEREnumerated, +200 _DERUTF8String = _KJUR_asn1.DERUTF8String, +201 _DERNumericString = _KJUR_asn1.DERNumericString, +202 _DERPrintableString = _KJUR_asn1.DERPrintableString, +203 _DERTeletexString = _KJUR_asn1.DERTeletexString, +204 _DERIA5String = _KJUR_asn1.DERIA5String, +205 _DERUTCTime = _KJUR_asn1.DERUTCTime, +206 _DERGeneralizedTime = _KJUR_asn1.DERGeneralizedTime, +207 _DERVisibleString = _KJUR_asn1.DERVisibleString, +208 _DERBMPString = _KJUR_asn1.DERBMPString, +209 _DERSequence = _KJUR_asn1.DERSequence, +210 _DERSet = _KJUR_asn1.DERSet, +211 _DERTaggedObject = _KJUR_asn1.DERTaggedObject, +212 _newObject = _KJUR_asn1.ASN1Util.newObject; +213 +214 if (param instanceof _KJUR_asn1.ASN1Object) return param; +215 +216 var keys = Object.keys(param); +217 if (keys.length != 1) +218 throw new Error("key of param shall be only one."); +219 var key = keys[0]; +220 +221 if (":asn1:bool:int:bitstr:octstr:null:oid:enum:utf8str:numstr:prnstr:telstr:ia5str:utctime:gentime:visstr:bmpstr:seq:set:tag:".indexOf(":" + key + ":") == -1) +222 throw new Error("undefined key: " + key); +223 +224 if (key == "bool") return new _DERBoolean(param[key]); +225 if (key == "int") return new _DERInteger(param[key]); +226 if (key == "bitstr") return new _DERBitString(param[key]); +227 if (key == "octstr") return new _DEROctetString(param[key]); +228 if (key == "null") return new _DERNull(param[key]); +229 if (key == "oid") return new _DERObjectIdentifier(param[key]); +230 if (key == "enum") return new _DEREnumerated(param[key]); +231 if (key == "utf8str") return new _DERUTF8String(param[key]); +232 if (key == "numstr") return new _DERNumericString(param[key]); +233 if (key == "prnstr") return new _DERPrintableString(param[key]); +234 if (key == "telstr") return new _DERTeletexString(param[key]); +235 if (key == "ia5str") return new _DERIA5String(param[key]); +236 if (key == "utctime") return new _DERUTCTime(param[key]); +237 if (key == "gentime") return new _DERGeneralizedTime(param[key]); +238 if (key == "visstr") return new _DERVisibleString(param[key]); +239 if (key == "bmpstr") return new _DERBMPString(param[key]); +240 if (key == "asn1") return new _ASN1Object(param[key]); +241 +242 if (key == "seq") { +243 var paramList = param[key]; +244 var a = []; +245 for (var i = 0; i < paramList.length; i++) { +246 var asn1Obj = _newObject(paramList[i]); +247 a.push(asn1Obj); +248 } +249 return new _DERSequence({'array': a}); +250 } +251 +252 if (key == "set") { +253 var paramList = param[key]; +254 var a = []; +255 for (var i = 0; i < paramList.length; i++) { +256 var asn1Obj = _newObject(paramList[i]); +257 a.push(asn1Obj); +258 } +259 return new _DERSet({'array': a}); +260 } +261 +262 if (key == "tag") { +263 var tagParam = param[key]; +264 if (Object.prototype.toString.call(tagParam) === '[object Array]' && +265 tagParam.length == 3) { +266 var obj = _newObject(tagParam[2]); +267 return new _DERTaggedObject({tag: tagParam[0], +268 explicit: tagParam[1], +269 obj: obj}); +270 } else { +271 return new _DERTaggedObject(tagParam); +272 } +273 } +274 }; +275 +276 /** +277 * get encoded hexadecimal string of ASN1Object specifed by JSON parameters +278 * @name jsonToASN1HEX +279 * @memberOf KJUR.asn1.ASN1Util +280 * @function +281 * @param {Array} param JSON parameter to generate ASN1Object +282 * @return hexadecimal string of ASN1Object +283 * @since asn1 1.0.4 +284 * @description +285 * As for ASN.1 object representation of JSON object, +286 * please see {@link newObject}. +287 * @example +288 * jsonToASN1HEX({'prnstr': 'aaa'}); +289 */ +290 this.jsonToASN1HEX = function(param) { +291 var asn1Obj = this.newObject(param); +292 return asn1Obj.tohex(); +293 }; +294 }; +295 +296 /** +297 * get dot noted oid number string from hexadecimal value of OID +298 * @name oidHexToInt +299 * @memberOf KJUR.asn1.ASN1Util +300 * @function +301 * @param {String} hex hexadecimal value of object identifier +302 * @return {String} dot noted string of object identifier +303 * @since jsrsasign 4.8.3 asn1 1.0.7 +304 * @description +305 * This static method converts from hexadecimal string representation of +306 * ASN.1 value of object identifier to oid number string. +307 * @example +308 * KJUR.asn1.ASN1Util.oidHexToInt('550406') → "2.5.4.6" +309 */ +310 KJUR.asn1.ASN1Util.oidHexToInt = function(hex) { +311 var s = ""; +312 var i01 = parseInt(hex.substr(0, 2), 16); +313 var i0 = Math.floor(i01 / 40); +314 var i1 = i01 % 40; +315 var s = i0 + "." + i1; +316 +317 var binbuf = ""; +318 for (var i = 2; i < hex.length; i += 2) { +319 var value = parseInt(hex.substr(i, 2), 16); +320 var bin = ("00000000" + value.toString(2)).slice(- 8); +321 binbuf = binbuf + bin.substr(1, 7); +322 if (bin.substr(0, 1) == "0") { +323 var bi = new BigInteger(binbuf, 2); +324 s = s + "." + bi.toString(10); +325 binbuf = ""; +326 } +327 }; +328 +329 return s; +330 }; +331 +332 /** +333 * get hexadecimal value of object identifier from dot noted oid value (DEPRECATED) +334 * @name oidIntToHex +335 * @memberOf KJUR.asn1.ASN1Util +336 * @function +337 * @param {String} oidString dot noted string of object identifier +338 * @return {String} hexadecimal value of object identifier +339 * @since jsrsasign 4.8.3 asn1 1.0.7 +340 * @see {@link ASN1HEX.hextooidstr} +341 * @deprecated from jsrsasign 10.0.6. please use {@link oidtohex} +342 * +343 * @description +344 * This static method converts from object identifier value string. +345 * to hexadecimal string representation of it. +346 * {@link ASN1HEX.hextooidstr} is a reverse function of this. +347 * @example +348 * KJUR.asn1.ASN1Util.oidIntToHex("2.5.4.6") → "550406" +349 */ +350 KJUR.asn1.ASN1Util.oidIntToHex = function(oidString) { +351 var itox = function(i) { +352 var h = i.toString(16); +353 if (h.length == 1) h = '0' + h; +354 return h; +355 }; +356 +357 var roidtox = function(roid) { +358 var h = ''; +359 var bi = new BigInteger(roid, 10); +360 var b = bi.toString(2); +361 var padLen = 7 - b.length % 7; +362 if (padLen == 7) padLen = 0; +363 var bPad = ''; +364 for (var i = 0; i < padLen; i++) bPad += '0'; +365 b = bPad + b; +366 for (var i = 0; i < b.length - 1; i += 7) { +367 var b8 = b.substr(i, 7); +368 if (i != b.length - 7) b8 = '1' + b8; +369 h += itox(parseInt(b8, 2)); +370 } +371 return h; +372 }; +373 +374 if (! oidString.match(/^[0-9.]+$/)) { +375 throw "malformed oid string: " + oidString; +376 } +377 var h = ''; +378 var a = oidString.split('.'); +379 var i0 = parseInt(a[0]) * 40 + parseInt(a[1]); +380 h += itox(i0); +381 a.splice(0, 2); +382 for (var i = 0; i < a.length; i++) { +383 h += roidtox(a[i]); +384 } +385 return h; +386 }; +387 +388 +389 // ******************************************************************** +390 // Abstract ASN.1 Classes +391 // ******************************************************************** +392 +393 // ******************************************************************** +394 +395 /** +396 * base class for ASN.1 DER encoder object<br/> +397 * @name KJUR.asn1.ASN1Object +398 * @class base class for ASN.1 DER encoder object +399 * @param {Array} params JSON object parameter for constructor +400 * @property {Boolean} isModified flag whether internal data was changed +401 * @property {Array} params JSON object parameter for ASN.1 encode +402 * @property {String} hTLV hexadecimal string of ASN.1 TLV +403 * @property {String} hT hexadecimal string of ASN.1 TLV tag(T) +404 * @property {String} hL hexadecimal string of ASN.1 TLV length(L) +405 * @property {String} hV hexadecimal string of ASN.1 TLV value(V) +406 * +407 * @description +408 * This class is ASN.1 DER object encode base class. +409 * +410 * @example +411 * new KJUR.asn1.ASN1Object({tlv: "030101"}) +412 */ +413 KJUR.asn1.ASN1Object = function(params) { +414 var isModified = true; +415 var hTLV = null; +416 var hT = '00'; +417 var hL = '00'; +418 var hV = ''; +419 this.params = null; +420 +421 /** +422 * get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)<br/> +423 * @name getLengthHexFromValue +424 * @memberOf KJUR.asn1.ASN1Object# +425 * @function +426 * @return {String} hexadecimal string of ASN.1 TLV length(L) +427 */ +428 this.getLengthHexFromValue = function() { +429 if (typeof this.hV == "undefined" || this.hV == null) { +430 throw new Error("this.hV is null or undefined"); +431 } +432 if (this.hV.length % 2 == 1) { +433 throw new Error("value hex must be even length: n=" + +434 hV.length + ",v=" + this.hV); +435 } +436 var n = this.hV.length / 2; +437 var hN = n.toString(16); +438 if (hN.length % 2 == 1) { +439 hN = "0" + hN; +440 } +441 if (n < 128) { +442 return hN; +443 } else { +444 var hNlen = hN.length / 2; +445 if (hNlen > 15) { +446 throw new Error("ASN.1 length too long to represent by 8x: n = " +447 + n.toString(16)); +448 } +449 var head = 128 + hNlen; +450 return head.toString(16) + hN; +451 } +452 }; +453 +454 /** +455 * get hexadecimal string of ASN.1 TLV bytes<br/> +456 * @name tohex +457 * @memberOf KJUR.asn1.ASN1Object# +458 * @function +459 * @return {String} hexadecimal string of ASN.1 TLV +460 * @since jsrsasign 10.5.16 asn1 1.0.24 +461 * @see KJUR.asn1.ASN1Object#getEncodedHex +462 * @example +463 * ...ASN1ObjectInstance.tohex() → "3003020101" +464 */ +465 this.tohex = function() { +466 if (this.hTLV == null || this.isModified) { +467 this.hV = this.getFreshValueHex(); +468 this.hL = this.getLengthHexFromValue(); +469 this.hTLV = this.hT + this.hL + this.hV; +470 this.isModified = false; +471 //alert("first time: " + this.hTLV); +472 } +473 return this.hTLV; +474 }; +475 +476 /** +477 * get hexadecimal string of ASN.1 TLV bytes (DEPRECATED)<br/> +478 * @name getEncodedHex +479 * @memberOf KJUR.asn1.ASN1Object# +480 * @function +481 * @return {String} hexadecimal string of ASN.1 TLV +482 * @deprecated since jsrsasign 10.5.16 please use {@link KJUR.asn1.ASN1Object#tohex} +483 */ +484 this.getEncodedHex = function() { return this.tohex(); }; +485 +486 /** +487 * get hexadecimal string of ASN.1 TLV value(V) bytes +488 * @name getValueHex +489 * @memberOf KJUR.asn1.ASN1Object# +490 * @function +491 * @return {String} hexadecimal string of ASN.1 TLV value(V) bytes +492 */ +493 this.getValueHex = function() { +494 this.tohex(); +495 return this.hV; +496 } +497 +498 this.getFreshValueHex = function() { +499 return ''; +500 }; +501 +502 this.setByParam = function(params) { +503 this.params = params; +504 }; +505 +506 if (params != undefined) { +507 if (params.tlv != undefined) { +508 this.hTLV = params.tlv; +509 this.isModified = false; +510 } +511 } +512 }; +513 +514 // == BEGIN DERAbstractString ================================================ +515 /** +516 * base class for ASN.1 DER string classes +517 * @name KJUR.asn1.DERAbstractString +518 * @class base class for ASN.1 DER string classes +519 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +520 * @property {String} s internal string of value +521 * @extends KJUR.asn1.ASN1Object +522 * @description +523 * <br/> +524 * As for argument 'params' for constructor, you can specify one of +525 * following properties: +526 * <ul> +527 * <li>str - specify initial ASN.1 value(V) by a string</li> +528 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +529 * </ul> +530 * NOTE: 'params' can be omitted. +531 */ +532 KJUR.asn1.DERAbstractString = function(params) { +533 KJUR.asn1.DERAbstractString.superclass.constructor.call(this); +534 var s = null; +535 var hV = null; +536 +537 /** +538 * get string value of this string object +539 * @name getString +540 * @memberOf KJUR.asn1.DERAbstractString# +541 * @function +542 * @return {String} string value of this string object +543 */ +544 this.getString = function() { +545 return this.s; +546 }; +547 +548 /** +549 * set value by a string +550 * @name setString +551 * @memberOf KJUR.asn1.DERAbstractString# +552 * @function +553 * @param {String} newS value by a string to set +554 * @description +555 * This method set value by string. <br/> +556 * NOTE: This method assumes that the argument string is +557 * UTF-8 encoded even though ASN.1 primitive +558 * such as IA5String or PrintableString doesn't +559 * support all of UTF-8 characters. +560 * @example +561 * o = new KJUR.asn1.DERIA5String(); +562 * o.setString("abc"); +563 * o.setString("あいう"); +564 */ +565 this.setString = function(newS) { +566 this.hTLV = null; +567 this.isModified = true; +568 this.s = newS; +569 this.hV = utf8tohex(this.s).toLowerCase(); +570 }; +571 +572 /** +573 * set value by a hexadecimal string +574 * @name setStringHex +575 * @memberOf KJUR.asn1.DERAbstractString# +576 * @function +577 * @param {String} newHexString value by a hexadecimal string to set +578 */ +579 this.setStringHex = function(newHexString) { +580 this.hTLV = null; +581 this.isModified = true; +582 this.s = null; +583 this.hV = newHexString; +584 }; +585 +586 this.getFreshValueHex = function() { +587 return this.hV; +588 }; +589 +590 if (typeof params != "undefined") { +591 if (typeof params == "string") { +592 this.setString(params); +593 } else if (typeof params['str'] != "undefined") { +594 this.setString(params['str']); +595 } else if (typeof params['hex'] != "undefined") { +596 this.setStringHex(params['hex']); +597 } +598 } +599 }; +600 extendClass(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object); +601 // == END DERAbstractString ================================================ +602 +603 // == BEGIN DERAbstractTime ================================================== +604 /** +605 * base class for ASN.1 DER Generalized/UTCTime class +606 * @name KJUR.asn1.DERAbstractTime +607 * @class base class for ASN.1 DER Generalized/UTCTime class +608 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) +609 * @extends KJUR.asn1.ASN1Object +610 * @description +611 * @see KJUR.asn1.ASN1Object - superclass +612 * @see KJUR.asn1.DERGeneralizedTime +613 * @see KJUR.asn1.DERUTCTime +614 * @see KJUR.asn1.x509.Time +615 */ +616 KJUR.asn1.DERAbstractTime = function(params) { +617 KJUR.asn1.DERAbstractTime.superclass.constructor.call(this); +618 var s = null; +619 var date = null; +620 +621 // --- PRIVATE METHODS -------------------- +622 this.localDateToUTC = function(d) { +623 var utc = d.getTime() + (d.getTimezoneOffset() * 60000); +624 var utcDate = new Date(utc); +625 return utcDate; +626 }; +627 +628 /* +629 * format date string by Data object +630 * @name formatDate +631 * @memberOf KJUR.asn1.AbstractTime; +632 * @param {Date} dateObject +633 * @param {string} type 'utc' or 'gen' +634 * @param {boolean} withMillis flag for with millisections or not +635 * @description +636 * 'withMillis' flag is supported from asn1 1.0.6. +637 */ +638 this.formatDate = function(dateObject, type, withMillis) { +639 var pad = this.zeroPadding; +640 var d = this.localDateToUTC(dateObject); +641 var year = String(d.getFullYear()); +642 if (type == 'utc') year = year.substr(2, 2); +643 var month = pad(String(d.getMonth() + 1), 2); +644 var day = pad(String(d.getDate()), 2); +645 var hour = pad(String(d.getHours()), 2); +646 var min = pad(String(d.getMinutes()), 2); +647 var sec = pad(String(d.getSeconds()), 2); +648 var s = year + month + day + hour + min + sec; +649 if (withMillis === true) { +650 var millis = d.getMilliseconds(); +651 if (millis != 0) { +652 var sMillis = pad(String(millis), 3); +653 sMillis = sMillis.replace(/[0]+$/, ""); +654 s = s + "." + sMillis; +655 } +656 } +657 return s + "Z"; +658 }; +659 +660 this.zeroPadding = function(s, len) { +661 if (s.length >= len) return s; +662 return new Array(len - s.length + 1).join('0') + s; +663 }; +664 +665 // --- PUBLIC METHODS -------------------- +666 +667 /** +668 * set parameter of time +669 * @name setByParam +670 * @memberOf KJUR.asn1.DERAbstractTime# +671 * @function +672 * @param {Object} params JSON object, Date object or string of time +673 * @since jsrsasign 10.4.1 asn1 1.0.22 +674 * +675 * NOTE: If a member "millis" has a value "true", +676 * a fraction of second will be specified for this object. +677 * This default is "false". +678 * +679 * @example +680 * d1 = new KJUR.asn1.DERGeneralizedTime(); +681 * d1.setByParam("20210930235959.123Z"); +682 * d1.setByParam({str: "20210930235959.123Z"}); +683 * +684 * d1.setByParam(new Date("2013/12/31 23:59:59.12")); +685 * date1 = new Date(Date.UTC(2021,8,31,23,59,59,123)); +686 * d1.setByParam(date1); +687 * d1.setByParam({date: date1}); +688 * d1.setByParam({date: date1, millis: true}); +689 */ +690 this.setByParam = function(params) { +691 this.hV = null; +692 this.hTLV = null; +693 this.params = params; +694 }; +695 +696 /** +697 * get string value of this string object (DEPRECATED) +698 * @name getString +699 * @memberOf KJUR.asn1.DERAbstractTime# +700 * @function +701 * @return {String} string value of this time object +702 * @deprecated from jsrsasign 10.4.1 asn1 1.0.22. +703 */ +704 this.getString = function() { +705 return undefined; +706 }; +707 +708 /** +709 * set value by a string (DEPRECATED) +710 * @name setString +711 * @memberOf KJUR.asn1.DERAbstractTime# +712 * @function +713 * @param {String} newS value by a string to set such like "130430235959Z" +714 * @deprecated from jsrsasign 10.4.1 asn1 1.0.22. +715 */ +716 this.setString = function(newS) { +717 this.hTLV = null; +718 this.isModified = true; +719 if (this.params == undefined) this.params = {}; +720 this.params.str = newS; 721 }; 722 723 /** -724 * get string value of this string object (DEPRECATED) -725 * @name getString +724 * set value by a Date object<br/> +725 * @name setByDate 726 * @memberOf KJUR.asn1.DERAbstractTime# 727 * @function -728 * @return {String} string value of this time object -729 * @deprecated from jsrsasign 10.4.1 asn1 1.0.22. -730 */ -731 this.getString = function() { -732 return undefined; -733 }; -734 -735 /** -736 * set value by a string (DEPRECATED) -737 * @name setString -738 * @memberOf KJUR.asn1.DERAbstractTime# -739 * @function -740 * @param {String} newS value by a string to set such like "130430235959Z" -741 * @deprecated from jsrsasign 10.4.1 asn1 1.0.22. -742 */ -743 this.setString = function(newS) { -744 this.hTLV = null; -745 this.isModified = true; -746 if (this.params == undefined) this.params = {}; -747 this.params.str = newS; -748 }; -749 -750 /** -751 * set value by a Date object<br/> -752 * @name setByDate -753 * @memberOf KJUR.asn1.DERAbstractTime# -754 * @function -755 * @param {Date} dateObject Date object to set ASN.1 value(V) -756 * @since jsrsasign 10.4.1 asn1 1.0.22 -757 * -758 * @example -759 * o = new KJUR.asn1.DERUTCTime(); -760 * o.setByDate(new Date("2016/12/31 23:59:59.12")); -761 * // 2015-Jan-31 23:59:59.12 -762 * o.setByDate(new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0))); -763 */ -764 this.setByDate = function(dateObject) { -765 this.hTLV = null; -766 this.isModified = true; -767 if (this.params == undefined) this.params = {}; -768 this.params.date = dateObject; -769 }; -770 -771 /** -772 * set value by a Date object -773 * @name setByDateValue -774 * @memberOf KJUR.asn1.DERAbstractTime# -775 * @function -776 * @param {Integer} year year of date (ex. 2013) -777 * @param {Integer} month month of date between 1 and 12 (ex. 12) -778 * @param {Integer} day day of month -779 * @param {Integer} hour hours of date -780 * @param {Integer} min minutes of date -781 * @param {Integer} sec seconds of date -782 */ -783 this.setByDateValue = function(year, month, day, hour, min, sec) { -784 var dateObject = new Date(Date.UTC(year, month - 1, day, -785 hour, min, sec, 0)); -786 this.setByDate(dateObject); -787 }; -788 -789 this.getFreshValueHex = function() { -790 return this.hV; -791 }; -792 }; -793 extendClass(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object); -794 // == END DERAbstractTime ================================================== +728 * @param {Date} dateObject Date object to set ASN.1 value(V) +729 * @since jsrsasign 10.4.1 asn1 1.0.22 +730 * +731 * @example +732 * o = new KJUR.asn1.DERUTCTime(); +733 * o.setByDate(new Date("2016/12/31 23:59:59.12")); +734 * // 2015-Jan-31 23:59:59.12 +735 * o.setByDate(new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0))); +736 */ +737 this.setByDate = function(dateObject) { +738 this.hTLV = null; +739 this.isModified = true; +740 if (this.params == undefined) this.params = {}; +741 this.params.date = dateObject; +742 }; +743 +744 /** +745 * set value by a Date object +746 * @name setByDateValue +747 * @memberOf KJUR.asn1.DERAbstractTime# +748 * @function +749 * @param {Integer} year year of date (ex. 2013) +750 * @param {Integer} month month of date between 1 and 12 (ex. 12) +751 * @param {Integer} day day of month +752 * @param {Integer} hour hours of date +753 * @param {Integer} min minutes of date +754 * @param {Integer} sec seconds of date +755 */ +756 this.setByDateValue = function(year, month, day, hour, min, sec) { +757 var dateObject = new Date(Date.UTC(year, month - 1, day, +758 hour, min, sec, 0)); +759 this.setByDate(dateObject); +760 }; +761 +762 this.getFreshValueHex = function() { +763 return this.hV; +764 }; +765 }; +766 extendClass(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object); +767 // == END DERAbstractTime ================================================== +768 +769 // == BEGIN DERAbstractStructured ============================================ +770 /** +771 * base class for ASN.1 DER structured class +772 * @name KJUR.asn1.DERAbstractStructured +773 * @class base class for ASN.1 DER structured class +774 * @property {Array} asn1Array internal array of ASN1Object +775 * @extends KJUR.asn1.ASN1Object +776 * @description +777 * @see KJUR.asn1.ASN1Object - superclass +778 */ +779 KJUR.asn1.DERAbstractStructured = function(params) { +780 KJUR.asn1.DERAbstractString.superclass.constructor.call(this); +781 var asn1Array = null; +782 +783 /** +784 * set value by array of ASN1Object +785 * @name setByASN1ObjectArray +786 * @memberOf KJUR.asn1.DERAbstractStructured# +787 * @function +788 * @param {array} asn1ObjectArray array of ASN1Object to set +789 */ +790 this.setByASN1ObjectArray = function(asn1ObjectArray) { +791 this.hTLV = null; +792 this.isModified = true; +793 this.asn1Array = asn1ObjectArray; +794 }; 795 -796 // == BEGIN DERAbstractStructured ============================================ -797 /** -798 * base class for ASN.1 DER structured class -799 * @name KJUR.asn1.DERAbstractStructured -800 * @class base class for ASN.1 DER structured class -801 * @property {Array} asn1Array internal array of ASN1Object -802 * @extends KJUR.asn1.ASN1Object -803 * @description -804 * @see KJUR.asn1.ASN1Object - superclass -805 */ -806 KJUR.asn1.DERAbstractStructured = function(params) { -807 KJUR.asn1.DERAbstractString.superclass.constructor.call(this); -808 var asn1Array = null; -809 -810 /** -811 * set value by array of ASN1Object -812 * @name setByASN1ObjectArray -813 * @memberOf KJUR.asn1.DERAbstractStructured# -814 * @function -815 * @param {array} asn1ObjectArray array of ASN1Object to set -816 */ -817 this.setByASN1ObjectArray = function(asn1ObjectArray) { -818 this.hTLV = null; -819 this.isModified = true; -820 this.asn1Array = asn1ObjectArray; -821 }; +796 /** +797 * append an ASN1Object to internal array +798 * @name appendASN1Object +799 * @memberOf KJUR.asn1.DERAbstractStructured# +800 * @function +801 * @param {ASN1Object} asn1Object to add +802 */ +803 this.appendASN1Object = function(asn1Object) { +804 this.hTLV = null; +805 this.isModified = true; +806 this.asn1Array.push(asn1Object); +807 }; +808 +809 this.asn1Array = new Array(); +810 if (typeof params != "undefined") { +811 if (typeof params['array'] != "undefined") { +812 this.asn1Array = params['array']; +813 } +814 } +815 }; +816 extendClass(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object); +817 +818 +819 // ******************************************************************** +820 // ASN.1 Object Classes +821 // ******************************************************************** 822 -823 /** -824 * append an ASN1Object to internal array -825 * @name appendASN1Object -826 * @memberOf KJUR.asn1.DERAbstractStructured# -827 * @function -828 * @param {ASN1Object} asn1Object to add -829 */ -830 this.appendASN1Object = function(asn1Object) { -831 this.hTLV = null; -832 this.isModified = true; -833 this.asn1Array.push(asn1Object); -834 }; -835 -836 this.asn1Array = new Array(); -837 if (typeof params != "undefined") { -838 if (typeof params['array'] != "undefined") { -839 this.asn1Array = params['array']; -840 } -841 } -842 }; -843 extendClass(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object); -844 -845 -846 // ******************************************************************** -847 // ASN.1 Object Classes -848 // ******************************************************************** -849 -850 // ******************************************************************** -851 /** -852 * class for ASN.1 DER Boolean -853 * @name KJUR.asn1.DERBoolean -854 * @class class for ASN.1 DER Boolean -855 * @extends KJUR.asn1.ASN1Object -856 * @see KJUR.asn1.ASN1Object - superclass -857 * @description -858 * In ASN.1 DER, DER Boolean "false" shall be omitted. -859 * However this supports boolean false for future BER support. -860 * @example -861 * new KJUR.asn1.DERBoolean(true) -862 * new KJUR.asn1.DERBoolean(false) +823 // ******************************************************************** +824 /** +825 * class for ASN.1 DER Boolean +826 * @name KJUR.asn1.DERBoolean +827 * @class class for ASN.1 DER Boolean +828 * @extends KJUR.asn1.ASN1Object +829 * @see KJUR.asn1.ASN1Object - superclass +830 * @description +831 * In ASN.1 DER, DER Boolean "false" shall be omitted. +832 * However this supports boolean false for future BER support. +833 * @example +834 * new KJUR.asn1.DERBoolean(true) +835 * new KJUR.asn1.DERBoolean(false) +836 */ +837 KJUR.asn1.DERBoolean = function(params) { +838 KJUR.asn1.DERBoolean.superclass.constructor.call(this); +839 this.hT = "01"; +840 if (params == false) +841 this.hTLV = "010100"; +842 else +843 this.hTLV = "0101ff"; +844 }; +845 extendClass(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object); +846 +847 // ******************************************************************** +848 /** +849 * class for ASN.1 DER Integer +850 * @name KJUR.asn1.DERInteger +851 * @class class for ASN.1 DER Integer +852 * @extends KJUR.asn1.ASN1Object +853 * @description +854 * <br/> +855 * As for argument 'params' for constructor, you can specify one of +856 * following properties: +857 * <ul> +858 * <li>int - specify initial ASN.1 value(V) by integer value</li> +859 * <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li> +860 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +861 * </ul> +862 * NOTE: 'params' can be omitted. 863 */ -864 KJUR.asn1.DERBoolean = function(params) { -865 KJUR.asn1.DERBoolean.superclass.constructor.call(this); -866 this.hT = "01"; -867 if (params == false) -868 this.hTLV = "010100"; -869 else -870 this.hTLV = "0101ff"; -871 }; -872 extendClass(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object); -873 -874 // ******************************************************************** -875 /** -876 * class for ASN.1 DER Integer -877 * @name KJUR.asn1.DERInteger -878 * @class class for ASN.1 DER Integer -879 * @extends KJUR.asn1.ASN1Object -880 * @description -881 * <br/> -882 * As for argument 'params' for constructor, you can specify one of -883 * following properties: -884 * <ul> -885 * <li>int - specify initial ASN.1 value(V) by integer value</li> -886 * <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li> -887 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -888 * </ul> -889 * NOTE: 'params' can be omitted. -890 */ -891 KJUR.asn1.DERInteger = function(params) { -892 KJUR.asn1.DERInteger.superclass.constructor.call(this); -893 this.hT = "02"; -894 this.params = null; -895 var _biToTwoCompl = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex; -896 -897 /** -898 * set value by Tom Wu's BigInteger object -899 * @name setByBigInteger -900 * @memberOf KJUR.asn1.DERInteger# -901 * @function -902 * @param {BigInteger} bigIntegerValue to set -903 */ -904 this.setByBigInteger = function(bigIntegerValue) { -905 this.isModified = true; -906 this.params = { bigint: bigIntegerValue }; -907 }; -908 -909 /** -910 * set value by integer value -911 * @name setByInteger -912 * @memberOf KJUR.asn1.DERInteger -913 * @function -914 * @param {Integer} integer value to set -915 */ -916 this.setByInteger = function(intValue) { -917 this.isModified = true; -918 this.params = intValue; -919 }; -920 -921 /** -922 * set value by integer value -923 * @name setValueHex -924 * @memberOf KJUR.asn1.DERInteger# -925 * @function -926 * @param {String} hexadecimal string of integer value -927 * @description -928 * <br/> -929 * NOTE: Value shall be represented by minimum octet length of -930 * two's complement representation. -931 * @example -932 * new KJUR.asn1.DERInteger(123); -933 * new KJUR.asn1.DERInteger({'int': 123}); -934 * new KJUR.asn1.DERInteger({'hex': '1fad'}); -935 * new KJUR.asn1.DERInteger({'bigint': new BigInteger("1234", 10)}); -936 */ -937 this.setValueHex = function(newHexString) { -938 this.isModified = true; -939 this.params = { hex: newHexString }; -940 }; -941 -942 this.getFreshValueHex = function() { -943 var params = this.params; -944 var bi = null; -945 if (params == null) throw new Error("value not set"); -946 -947 if (typeof params == "object" && params.hex != undefined) { -948 this.hV = params.hex; -949 return this.hV; -950 } -951 -952 if (typeof params == "number") { -953 bi = new BigInteger(String(params), 10); -954 } else if (params["int"] != undefined) { -955 bi = new BigInteger(String(params["int"]), 10); -956 } else if (params.bigint != undefined) { -957 bi = params.bigint; -958 } else { -959 throw new Error("wrong parameter"); -960 } -961 this.hV = _biToTwoCompl(bi); -962 return this.hV; -963 }; -964 -965 if (params != undefined) { -966 this.params = params; -967 } -968 }; -969 extendClass(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object); -970 -971 // ******************************************************************** -972 /** -973 * class for ASN.1 DER encoded BitString primitive -974 * @name KJUR.asn1.DERBitString -975 * @class class for ASN.1 DER encoded BitString primitive -976 * @extends KJUR.asn1.ASN1Object -977 * @description -978 * <br/> -979 * As for argument 'params' for constructor, you can specify one of -980 * following properties: -981 * <ul> -982 * <li>bin - specify binary string (ex. '10111')</li> -983 * <li>array - specify array of boolean (ex. [true,false,true,true])</li> -984 * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li> -985 * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} -986 * argument for "BitString encapsulates" structure.</li> -987 * </ul> -988 * NOTE1: 'params' can be omitted.<br/> -989 * NOTE2: 'obj' parameter have been supported since -990 * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/> -991 * -992 * @example -993 * // default constructor -994 * o = new KJUR.asn1.DERBitString(); -995 * // initialize with binary string -996 * o = new KJUR.asn1.DERBitString({bin: "1011"}); -997 * // initialize with boolean array -998 * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]}); -999 * // initialize with hexadecimal string (04 is unused bits) -1000 * o = new KJUR.asn1.DERBitString({hex: "04bac0"}); -1001 * // initialize with ASN1Util.newObject argument for encapsulated -1002 * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); -1003 * // above generates a ASN.1 data like this: -1004 * // BIT STRING, encapsulates { -1005 * // SEQUENCE { -1006 * // INTEGER 3 -1007 * // PrintableString 'aaa' -1008 * // } -1009 * // } -1010 */ -1011 KJUR.asn1.DERBitString = function(params) { -1012 if (params !== undefined && typeof params.obj !== "undefined") { -1013 var o = KJUR.asn1.ASN1Util.newObject(params.obj); -1014 params.hex = "00" + o.tohex(); -1015 } -1016 KJUR.asn1.DERBitString.superclass.constructor.call(this); -1017 this.hT = "03"; -1018 -1019 /** -1020 * set ASN.1 value(V) by a hexadecimal string including unused bits -1021 * @name setHexValueIncludingUnusedBits -1022 * @memberOf KJUR.asn1.DERBitString# -1023 * @function -1024 * @param {String} newHexStringIncludingUnusedBits -1025 */ -1026 this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) { -1027 this.hTLV = null; -1028 this.isModified = true; -1029 this.hV = newHexStringIncludingUnusedBits; -1030 }; -1031 -1032 /** -1033 * set ASN.1 value(V) by unused bit and hexadecimal string of value -1034 * @name setUnusedBitsAndHexValue -1035 * @memberOf KJUR.asn1.DERBitString# -1036 * @function -1037 * @param {Integer} unusedBits -1038 * @param {String} hValue -1039 */ -1040 this.setUnusedBitsAndHexValue = function(unusedBits, hValue) { -1041 if (unusedBits < 0 || 7 < unusedBits) { -1042 throw "unused bits shall be from 0 to 7: u = " + unusedBits; -1043 } -1044 var hUnusedBits = "0" + unusedBits; -1045 this.hTLV = null; -1046 this.isModified = true; -1047 this.hV = hUnusedBits + hValue; -1048 }; -1049 -1050 /** -1051 * set ASN.1 DER BitString by binary string<br/> -1052 * @name setByBinaryString -1053 * @memberOf KJUR.asn1.DERBitString# -1054 * @function -1055 * @param {String} binaryString binary value string (i.e. '10111') -1056 * @description -1057 * Its unused bits will be calculated automatically by length of -1058 * 'binaryValue'. <br/> -1059 * NOTE: Leading zeros '0' will be ignored. -1060 * @example -1061 * o = new KJUR.asn1.DERBitString(); -1062 * o.setByBinaryString("1011"); -1063 * o.setByBinaryString("001"); // leading zeros ignored -1064 */ -1065 this.setByBinaryString = function(binaryString) { -1066 binaryString = binaryString.replace(/0+$/, ''); -1067 var unusedBits = 8 - binaryString.length % 8; -1068 if (unusedBits == 8) unusedBits = 0; -1069 -1070 binaryString += "0000000".substr(0, unusedBits); -1071 -1072 var h = ''; -1073 for (var i = 0; i < binaryString.length - 1; i += 8) { -1074 var b = binaryString.substr(i, 8); -1075 var x = parseInt(b, 2).toString(16); -1076 if (x.length == 1) x = '0' + x; -1077 h += x; -1078 } -1079 this.hTLV = null; -1080 this.isModified = true; -1081 this.hV = '0' + unusedBits + h; -1082 }; -1083 -1084 /** -1085 * set ASN.1 TLV value(V) by an array of boolean<br/> -1086 * @name setByBooleanArray -1087 * @memberOf KJUR.asn1.DERBitString# -1088 * @function -1089 * @param {array} booleanArray array of boolean (ex. [true, false, true]) -1090 * @description -1091 * NOTE: Trailing falses will be ignored in the ASN.1 DER Object. -1092 * @example -1093 * o = new KJUR.asn1.DERBitString(); -1094 * o.setByBooleanArray([false, true, false, true, true]); -1095 */ -1096 this.setByBooleanArray = function(booleanArray) { -1097 var s = ''; -1098 for (var i = 0; i < booleanArray.length; i++) { -1099 if (booleanArray[i] == true) { -1100 s += '1'; -1101 } else { -1102 s += '0'; -1103 } -1104 } -1105 this.setByBinaryString(s); -1106 }; -1107 -1108 /** -1109 * generate an array of falses with specified length<br/> -1110 * @name newFalseArray -1111 * @memberOf KJUR.asn1.DERBitString -1112 * @function -1113 * @param {Integer} nLength length of array to generate -1114 * @return {array} array of boolean falses -1115 * @description -1116 * This static method may be useful to initialize boolean array. -1117 * @example -1118 * o = new KJUR.asn1.DERBitString(); -1119 * o.newFalseArray(3) → [false, false, false] -1120 */ -1121 this.newFalseArray = function(nLength) { -1122 var a = new Array(nLength); -1123 for (var i = 0; i < nLength; i++) { -1124 a[i] = false; -1125 } -1126 return a; -1127 }; -1128 -1129 this.getFreshValueHex = function() { -1130 return this.hV; -1131 }; -1132 -1133 if (typeof params != "undefined") { -1134 if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) { -1135 this.setHexValueIncludingUnusedBits(params); -1136 } else if (typeof params['hex'] != "undefined") { -1137 this.setHexValueIncludingUnusedBits(params['hex']); -1138 } else if (typeof params['bin'] != "undefined") { -1139 this.setByBinaryString(params['bin']); -1140 } else if (typeof params['array'] != "undefined") { -1141 this.setByBooleanArray(params['array']); -1142 } -1143 } -1144 }; -1145 extendClass(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object); -1146 -1147 // ******************************************************************** -1148 /** -1149 * class for ASN.1 DER OctetString<br/> -1150 * @name KJUR.asn1.DEROctetString -1151 * @class class for ASN.1 DER OctetString -1152 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1153 * @extends KJUR.asn1.DERAbstractString -1154 * @description -1155 * This class provides ASN.1 OctetString simple type.<br/> -1156 * Supported "params" attributes are: -1157 * <ul> -1158 * <li>str - to set a string as a value</li> -1159 * <li>hex - to set a hexadecimal string as a value</li> -1160 * <li>obj - to set a encapsulated ASN.1 value by JSON object -1161 * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li> -1162 * </ul> -1163 * NOTE: A parameter 'obj' have been supported -1164 * for "OCTET STRING, encapsulates" structure. -1165 * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25). -1166 * @see KJUR.asn1.DERAbstractString - superclass -1167 * @example -1168 * // default constructor -1169 * o = new KJUR.asn1.DEROctetString(); -1170 * // initialize with string -1171 * o = new KJUR.asn1.DEROctetString({str: "aaa"}); -1172 * // initialize with hexadecimal string -1173 * o = new KJUR.asn1.DEROctetString({hex: "616161"}); -1174 * // initialize with ASN1Util.newObject argument -1175 * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); -1176 * // above generates a ASN.1 data like this: -1177 * // OCTET STRING, encapsulates { -1178 * // SEQUENCE { -1179 * // INTEGER 3 -1180 * // PrintableString 'aaa' -1181 * // } -1182 * // } -1183 */ -1184 KJUR.asn1.DEROctetString = function(params) { -1185 if (params !== undefined && typeof params.obj !== "undefined") { -1186 var o = KJUR.asn1.ASN1Util.newObject(params.obj); -1187 params.hex = o.tohex(); -1188 } -1189 KJUR.asn1.DEROctetString.superclass.constructor.call(this, params); -1190 this.hT = "04"; -1191 }; -1192 extendClass(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString); -1193 -1194 // ******************************************************************** -1195 /** -1196 * class for ASN.1 DER Null -1197 * @name KJUR.asn1.DERNull -1198 * @class class for ASN.1 DER Null -1199 * @extends KJUR.asn1.ASN1Object -1200 * @description -1201 * @see KJUR.asn1.ASN1Object - superclass -1202 */ -1203 KJUR.asn1.DERNull = function() { -1204 KJUR.asn1.DERNull.superclass.constructor.call(this); -1205 this.hT = "05"; -1206 this.hTLV = "0500"; -1207 }; -1208 extendClass(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object); -1209 -1210 // ******************************************************************** -1211 /** -1212 * class for ASN.1 DER ObjectIdentifier -1213 * @name KJUR.asn1.DERObjectIdentifier -1214 * @class class for ASN.1 DER ObjectIdentifier -1215 * @param {Object} JSON object or string of parameters (ex. {'oid': '2.5.4.5'}) -1216 * @extends KJUR.asn1.ASN1Object -1217 * @see oidtohex -1218 * -1219 * @description -1220 * <br/> -1221 * As for argument 'params' for constructor, you can specify one of -1222 * following properties: -1223 * <ul> -1224 * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li> -1225 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1226 * </ul> -1227 * NOTE: 'params' can be omitted. -1228 * @example -1229 * new DERObjectIdentifier({"name": "sha1"}) -1230 * new DERObjectIdentifier({"oid": "1.2.3.4"}) -1231 * new DERObjectIdentifier({"hex": "2d..."}) -1232 * new DERObjectIdentifier("1.2.3.4") -1233 * new DERObjectIdentifier("SHA1withRSA") -1234 */ -1235 KJUR.asn1.DERObjectIdentifier = function(params) { -1236 KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this); -1237 this.hT = "06"; -1238 -1239 /** -1240 * set value by a hexadecimal string -1241 * @name setValueHex -1242 * @memberOf KJUR.asn1.DERObjectIdentifier# -1243 * @function -1244 * @param {String} newHexString hexadecimal value of OID bytes -1245 */ -1246 this.setValueHex = function(newHexString) { -1247 this.hTLV = null; -1248 this.isModified = true; -1249 this.s = null; -1250 this.hV = newHexString; -1251 }; -1252 -1253 /** -1254 * set value by a OID string<br/> -1255 * @name setValueOidString -1256 * @memberOf KJUR.asn1.DERObjectIdentifier# -1257 * @function -1258 * @param {String} oidString OID string (ex. 2.5.4.13) -1259 * @example -1260 * o = new KJUR.asn1.DERObjectIdentifier(); -1261 * o.setValueOidString("2.5.4.13"); -1262 */ -1263 this.setValueOidString = function(oidString) { -1264 var h = oidtohex(oidString); -1265 if (h == null) -1266 throw new Error("malformed oid string: " + oidString); -1267 this.hTLV = null; -1268 this.isModified = true; -1269 this.s = null; -1270 this.hV = h; -1271 }; -1272 -1273 /** -1274 * set value by a OID name -1275 * @name setValueName -1276 * @memberOf KJUR.asn1.DERObjectIdentifier# -1277 * @function -1278 * @param {String} oidName OID name (ex. 'serverAuth') -1279 * @since 1.0.1 -1280 * @description -1281 * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'. -1282 * Otherwise raise error. -1283 * @example -1284 * o = new KJUR.asn1.DERObjectIdentifier(); -1285 * o.setValueName("serverAuth"); -1286 */ -1287 this.setValueName = function(oidName) { -1288 var oid = KJUR.asn1.x509.OID.name2oid(oidName); -1289 if (oid !== '') { -1290 this.setValueOidString(oid); -1291 } else { -1292 throw new Error("DERObjectIdentifier oidName undefined: " + oidName); -1293 } -1294 }; -1295 -1296 this.setValueNameOrOid = function(nameOrOid) { -1297 if (nameOrOid.match(/^[0-2].[0-9.]+$/)) { -1298 this.setValueOidString(nameOrOid); -1299 } else { -1300 this.setValueName(nameOrOid); -1301 } -1302 } -1303 -1304 this.getFreshValueHex = function() { -1305 return this.hV; -1306 }; -1307 -1308 this.setByParam = function(params) { -1309 if (typeof params === "string") { -1310 this.setValueNameOrOid(params); -1311 } else if (params.oid !== undefined) { -1312 this.setValueNameOrOid(params.oid); -1313 } else if (params.name !== undefined) { -1314 this.setValueNameOrOid(params.name); -1315 } else if (params.hex !== undefined) { -1316 this.setValueHex(params.hex); -1317 } -1318 }; -1319 -1320 if (params !== undefined) this.setByParam(params); -1321 }; -1322 extendClass(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object); -1323 -1324 // ******************************************************************** -1325 /** -1326 * class for ASN.1 DER Enumerated -1327 * @name KJUR.asn1.DEREnumerated -1328 * @class class for ASN.1 DER Enumerated -1329 * @extends KJUR.asn1.ASN1Object -1330 * @description -1331 * <br/> -1332 * As for argument 'params' for constructor, you can specify one of -1333 * following properties: -1334 * <ul> -1335 * <li>int - specify initial ASN.1 value(V) by integer value</li> -1336 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> -1337 * </ul> -1338 * NOTE: 'params' can be omitted. -1339 * @example -1340 * new KJUR.asn1.DEREnumerated(123); -1341 * new KJUR.asn1.DEREnumerated({int: 123}); -1342 * new KJUR.asn1.DEREnumerated({hex: '1fad'}); -1343 */ -1344 KJUR.asn1.DEREnumerated = function(params) { -1345 KJUR.asn1.DEREnumerated.superclass.constructor.call(this); -1346 this.hT = "0a"; -1347 -1348 /** -1349 * set value by Tom Wu's BigInteger object -1350 * @name setByBigInteger -1351 * @memberOf KJUR.asn1.DEREnumerated# -1352 * @function -1353 * @param {BigInteger} bigIntegerValue to set -1354 */ -1355 this.setByBigInteger = function(bigIntegerValue) { -1356 this.hTLV = null; -1357 this.isModified = true; -1358 this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue); +864 KJUR.asn1.DERInteger = function(params) { +865 KJUR.asn1.DERInteger.superclass.constructor.call(this); +866 this.hT = "02"; +867 this.params = null; +868 var _biToTwoCompl = twoscompl; +869 +870 /** +871 * set value by Tom Wu's BigInteger object +872 * @name setByBigInteger +873 * @memberOf KJUR.asn1.DERInteger# +874 * @function +875 * @param {BigInteger} bigIntegerValue to set +876 */ +877 this.setByBigInteger = function(bigIntegerValue) { +878 this.isModified = true; +879 this.params = { bigint: bigIntegerValue }; +880 }; +881 +882 /** +883 * set value by integer value +884 * @name setByInteger +885 * @memberOf KJUR.asn1.DERInteger +886 * @function +887 * @param {Integer} integer value to set +888 */ +889 this.setByInteger = function(intValue) { +890 this.isModified = true; +891 this.params = intValue; +892 }; +893 +894 /** +895 * set value by integer value +896 * @name setValueHex +897 * @memberOf KJUR.asn1.DERInteger# +898 * @function +899 * @param {String} hexadecimal string of integer value +900 * @description +901 * <br/> +902 * NOTE: Value shall be represented by minimum octet length of +903 * two's complement representation. +904 * @example +905 * new KJUR.asn1.DERInteger(123); +906 * new KJUR.asn1.DERInteger({'int': 123}); +907 * new KJUR.asn1.DERInteger({'hex': '1fad'}); +908 * new KJUR.asn1.DERInteger({'bigint': new BigInteger("1234", 10)}); +909 */ +910 this.setValueHex = function(newHexString) { +911 this.isModified = true; +912 this.params = { hex: newHexString }; +913 }; +914 +915 this.getFreshValueHex = function() { +916 var params = this.params; +917 var bi = null; +918 if (params == null) throw new Error("value not set"); +919 +920 if (typeof params == "object" && params.hex != undefined) { +921 this.hV = params.hex; +922 return this.hV; +923 } +924 +925 if (typeof params == "number") { +926 bi = new BigInteger(String(params), 10); +927 } else if (params["int"] != undefined) { +928 bi = new BigInteger(String(params["int"]), 10); +929 } else if (params.bigint != undefined) { +930 bi = params.bigint; +931 } else { +932 throw new Error("wrong parameter"); +933 } +934 this.hV = _biToTwoCompl(bi); +935 return this.hV; +936 }; +937 +938 if (params != undefined) { +939 this.params = params; +940 } +941 }; +942 extendClass(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object); +943 +944 // ******************************************************************** +945 /** +946 * class for ASN.1 DER encoded BitString primitive +947 * @name KJUR.asn1.DERBitString +948 * @class class for ASN.1 DER encoded BitString primitive +949 * @extends KJUR.asn1.ASN1Object +950 * @description +951 * <br/> +952 * As for argument 'params' for constructor, you can specify one of +953 * following properties: +954 * <ul> +955 * <li>bin - specify binary string (ex. '10111')</li> +956 * <li>array - specify array of boolean (ex. [true,false,true,true])</li> +957 * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li> +958 * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} +959 * argument for "BitString encapsulates" structure.</li> +960 * </ul> +961 * NOTE1: 'params' can be omitted.<br/> +962 * NOTE2: 'obj' parameter have been supported since +963 * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/> +964 * +965 * @example +966 * // default constructor +967 * o = new KJUR.asn1.DERBitString(); +968 * // initialize with binary string +969 * o = new KJUR.asn1.DERBitString({bin: "1011"}); +970 * // initialize with boolean array +971 * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]}); +972 * // initialize with hexadecimal string (04 is unused bits) +973 * o = new KJUR.asn1.DERBitString({hex: "04bac0"}); +974 * // initialize with ASN1Util.newObject argument for encapsulated +975 * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +976 * // above generates a ASN.1 data like this: +977 * // BIT STRING, encapsulates { +978 * // SEQUENCE { +979 * // INTEGER 3 +980 * // PrintableString 'aaa' +981 * // } +982 * // } +983 */ +984 KJUR.asn1.DERBitString = function(params) { +985 if (params !== undefined && typeof params.obj !== "undefined") { +986 var o = KJUR.asn1.ASN1Util.newObject(params.obj); +987 params.hex = "00" + o.tohex(); +988 } +989 KJUR.asn1.DERBitString.superclass.constructor.call(this); +990 this.hT = "03"; +991 +992 /** +993 * set ASN.1 value(V) by a hexadecimal string including unused bits +994 * @name setHexValueIncludingUnusedBits +995 * @memberOf KJUR.asn1.DERBitString# +996 * @function +997 * @param {String} newHexStringIncludingUnusedBits +998 */ +999 this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) { +1000 this.hTLV = null; +1001 this.isModified = true; +1002 this.hV = newHexStringIncludingUnusedBits; +1003 }; +1004 +1005 /** +1006 * set ASN.1 value(V) by unused bit and hexadecimal string of value +1007 * @name setUnusedBitsAndHexValue +1008 * @memberOf KJUR.asn1.DERBitString# +1009 * @function +1010 * @param {Integer} unusedBits +1011 * @param {String} hValue +1012 */ +1013 this.setUnusedBitsAndHexValue = function(unusedBits, hValue) { +1014 if (unusedBits < 0 || 7 < unusedBits) { +1015 throw "unused bits shall be from 0 to 7: u = " + unusedBits; +1016 } +1017 var hUnusedBits = "0" + unusedBits; +1018 this.hTLV = null; +1019 this.isModified = true; +1020 this.hV = hUnusedBits + hValue; +1021 }; +1022 +1023 /** +1024 * set ASN.1 DER BitString by binary string<br/> +1025 * @name setByBinaryString +1026 * @memberOf KJUR.asn1.DERBitString# +1027 * @function +1028 * @param {String} binaryString binary value string (i.e. '10111') +1029 * @description +1030 * Its unused bits will be calculated automatically by length of +1031 * 'binaryValue'. <br/> +1032 * NOTE: Leading zeros '0' will be ignored. +1033 * @example +1034 * o = new KJUR.asn1.DERBitString(); +1035 * o.setByBinaryString("1011"); +1036 * o.setByBinaryString("001"); // leading zeros ignored +1037 */ +1038 this.setByBinaryString = function(binaryString) { +1039 binaryString = binaryString.replace(/0+$/, ''); +1040 var unusedBits = 8 - binaryString.length % 8; +1041 if (unusedBits == 8) unusedBits = 0; +1042 +1043 binaryString += "0000000".substr(0, unusedBits); +1044 +1045 var h = ''; +1046 for (var i = 0; i < binaryString.length - 1; i += 8) { +1047 var b = binaryString.substr(i, 8); +1048 var x = parseInt(b, 2).toString(16); +1049 if (x.length == 1) x = '0' + x; +1050 h += x; +1051 } +1052 this.hTLV = null; +1053 this.isModified = true; +1054 this.hV = '0' + unusedBits + h; +1055 }; +1056 +1057 /** +1058 * set ASN.1 TLV value(V) by an array of boolean<br/> +1059 * @name setByBooleanArray +1060 * @memberOf KJUR.asn1.DERBitString# +1061 * @function +1062 * @param {array} booleanArray array of boolean (ex. [true, false, true]) +1063 * @description +1064 * NOTE: Trailing falses will be ignored in the ASN.1 DER Object. +1065 * @example +1066 * o = new KJUR.asn1.DERBitString(); +1067 * o.setByBooleanArray([false, true, false, true, true]); +1068 */ +1069 this.setByBooleanArray = function(booleanArray) { +1070 var s = ''; +1071 for (var i = 0; i < booleanArray.length; i++) { +1072 if (booleanArray[i] == true) { +1073 s += '1'; +1074 } else { +1075 s += '0'; +1076 } +1077 } +1078 this.setByBinaryString(s); +1079 }; +1080 +1081 /** +1082 * generate an array of falses with specified length<br/> +1083 * @name newFalseArray +1084 * @memberOf KJUR.asn1.DERBitString +1085 * @function +1086 * @param {Integer} nLength length of array to generate +1087 * @return {array} array of boolean falses +1088 * @description +1089 * This static method may be useful to initialize boolean array. +1090 * @example +1091 * o = new KJUR.asn1.DERBitString(); +1092 * o.newFalseArray(3) → [false, false, false] +1093 */ +1094 this.newFalseArray = function(nLength) { +1095 var a = new Array(nLength); +1096 for (var i = 0; i < nLength; i++) { +1097 a[i] = false; +1098 } +1099 return a; +1100 }; +1101 +1102 this.getFreshValueHex = function() { +1103 return this.hV; +1104 }; +1105 +1106 if (typeof params != "undefined") { +1107 if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) { +1108 this.setHexValueIncludingUnusedBits(params); +1109 } else if (typeof params['hex'] != "undefined") { +1110 this.setHexValueIncludingUnusedBits(params['hex']); +1111 } else if (typeof params['bin'] != "undefined") { +1112 this.setByBinaryString(params['bin']); +1113 } else if (typeof params['array'] != "undefined") { +1114 this.setByBooleanArray(params['array']); +1115 } +1116 } +1117 }; +1118 extendClass(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object); +1119 +1120 // ******************************************************************** +1121 /** +1122 * class for ASN.1 DER OctetString<br/> +1123 * @name KJUR.asn1.DEROctetString +1124 * @class class for ASN.1 DER OctetString +1125 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1126 * @extends KJUR.asn1.DERAbstractString +1127 * @description +1128 * This class provides ASN.1 OctetString simple type.<br/> +1129 * Supported "params" attributes are: +1130 * <ul> +1131 * <li>str - to set a string as a value</li> +1132 * <li>hex - to set a hexadecimal string as a value</li> +1133 * <li>obj - to set a encapsulated ASN.1 value by JSON object +1134 * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li> +1135 * </ul> +1136 * NOTE: A parameter 'obj' have been supported +1137 * for "OCTET STRING, encapsulates" structure. +1138 * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25). +1139 * @see KJUR.asn1.DERAbstractString - superclass +1140 * @example +1141 * // default constructor +1142 * o = new KJUR.asn1.DEROctetString(); +1143 * // initialize with string +1144 * o = new KJUR.asn1.DEROctetString({str: "aaa"}); +1145 * // initialize with hexadecimal string +1146 * o = new KJUR.asn1.DEROctetString({hex: "616161"}); +1147 * // initialize with ASN1Util.newObject argument +1148 * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}}); +1149 * // above generates a ASN.1 data like this: +1150 * // OCTET STRING, encapsulates { +1151 * // SEQUENCE { +1152 * // INTEGER 3 +1153 * // PrintableString 'aaa' +1154 * // } +1155 * // } +1156 */ +1157 KJUR.asn1.DEROctetString = function(params) { +1158 if (params !== undefined && typeof params.obj !== "undefined") { +1159 var o = KJUR.asn1.ASN1Util.newObject(params.obj); +1160 params.hex = o.tohex(); +1161 } +1162 KJUR.asn1.DEROctetString.superclass.constructor.call(this, params); +1163 this.hT = "04"; +1164 }; +1165 extendClass(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString); +1166 +1167 // ******************************************************************** +1168 /** +1169 * class for ASN.1 DER Null +1170 * @name KJUR.asn1.DERNull +1171 * @class class for ASN.1 DER Null +1172 * @extends KJUR.asn1.ASN1Object +1173 * @description +1174 * @see KJUR.asn1.ASN1Object - superclass +1175 */ +1176 KJUR.asn1.DERNull = function() { +1177 KJUR.asn1.DERNull.superclass.constructor.call(this); +1178 this.hT = "05"; +1179 this.hTLV = "0500"; +1180 }; +1181 extendClass(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object); +1182 +1183 // ******************************************************************** +1184 /** +1185 * class for ASN.1 DER ObjectIdentifier +1186 * @name KJUR.asn1.DERObjectIdentifier +1187 * @class class for ASN.1 DER ObjectIdentifier +1188 * @param {Object} JSON object or string of parameters (ex. {'oid': '2.5.4.5'}) +1189 * @extends KJUR.asn1.ASN1Object +1190 * @see oidtohex +1191 * +1192 * @description +1193 * <br/> +1194 * As for argument 'params' for constructor, you can specify one of +1195 * following properties: +1196 * <ul> +1197 * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li> +1198 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1199 * </ul> +1200 * NOTE: 'params' can be omitted. +1201 * @example +1202 * new DERObjectIdentifier({"name": "sha1"}) +1203 * new DERObjectIdentifier({"oid": "1.2.3.4"}) +1204 * new DERObjectIdentifier({"hex": "2d..."}) +1205 * new DERObjectIdentifier("1.2.3.4") +1206 * new DERObjectIdentifier("SHA1withRSA") +1207 */ +1208 KJUR.asn1.DERObjectIdentifier = function(params) { +1209 KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this); +1210 this.hT = "06"; +1211 +1212 /** +1213 * set value by a hexadecimal string +1214 * @name setValueHex +1215 * @memberOf KJUR.asn1.DERObjectIdentifier# +1216 * @function +1217 * @param {String} newHexString hexadecimal value of OID bytes +1218 */ +1219 this.setValueHex = function(newHexString) { +1220 this.hTLV = null; +1221 this.isModified = true; +1222 this.s = null; +1223 this.hV = newHexString; +1224 }; +1225 +1226 /** +1227 * set value by a OID string<br/> +1228 * @name setValueOidString +1229 * @memberOf KJUR.asn1.DERObjectIdentifier# +1230 * @function +1231 * @param {String} oidString OID string (ex. 2.5.4.13) +1232 * @example +1233 * o = new KJUR.asn1.DERObjectIdentifier(); +1234 * o.setValueOidString("2.5.4.13"); +1235 */ +1236 this.setValueOidString = function(oidString) { +1237 var h = oidtohex(oidString); +1238 if (h == null) +1239 throw new Error("malformed oid string: " + oidString); +1240 this.hTLV = null; +1241 this.isModified = true; +1242 this.s = null; +1243 this.hV = h; +1244 }; +1245 +1246 /** +1247 * set value by a OID name +1248 * @name setValueName +1249 * @memberOf KJUR.asn1.DERObjectIdentifier# +1250 * @function +1251 * @param {String} oidName OID name (ex. 'serverAuth') +1252 * @since 1.0.1 +1253 * @description +1254 * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'. +1255 * Otherwise raise error. +1256 * @example +1257 * o = new KJUR.asn1.DERObjectIdentifier(); +1258 * o.setValueName("serverAuth"); +1259 */ +1260 this.setValueName = function(oidName) { +1261 var oid = KJUR.asn1.x509.OID.name2oid(oidName); +1262 if (oid !== '') { +1263 this.setValueOidString(oid); +1264 } else { +1265 throw new Error("DERObjectIdentifier oidName undefined: " + oidName); +1266 } +1267 }; +1268 +1269 this.setValueNameOrOid = function(nameOrOid) { +1270 if (nameOrOid.match(/^[0-2].[0-9.]+$/)) { +1271 this.setValueOidString(nameOrOid); +1272 } else { +1273 this.setValueName(nameOrOid); +1274 } +1275 } +1276 +1277 this.getFreshValueHex = function() { +1278 return this.hV; +1279 }; +1280 +1281 this.setByParam = function(params) { +1282 if (typeof params === "string") { +1283 this.setValueNameOrOid(params); +1284 } else if (params.oid !== undefined) { +1285 this.setValueNameOrOid(params.oid); +1286 } else if (params.name !== undefined) { +1287 this.setValueNameOrOid(params.name); +1288 } else if (params.hex !== undefined) { +1289 this.setValueHex(params.hex); +1290 } +1291 }; +1292 +1293 if (params !== undefined) this.setByParam(params); +1294 }; +1295 extendClass(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object); +1296 +1297 // ******************************************************************** +1298 /** +1299 * class for ASN.1 DER Enumerated +1300 * @name KJUR.asn1.DEREnumerated +1301 * @class class for ASN.1 DER Enumerated +1302 * @extends KJUR.asn1.ASN1Object +1303 * @description +1304 * <br/> +1305 * As for argument 'params' for constructor, you can specify one of +1306 * following properties: +1307 * <ul> +1308 * <li>int - specify initial ASN.1 value(V) by integer value</li> +1309 * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li> +1310 * </ul> +1311 * NOTE: 'params' can be omitted. +1312 * @example +1313 * new KJUR.asn1.DEREnumerated(123); +1314 * new KJUR.asn1.DEREnumerated({int: 123}); +1315 * new KJUR.asn1.DEREnumerated({hex: '1fad'}); +1316 */ +1317 KJUR.asn1.DEREnumerated = function(params) { +1318 KJUR.asn1.DEREnumerated.superclass.constructor.call(this); +1319 this.hT = "0a"; +1320 +1321 /** +1322 * set value by Tom Wu's BigInteger object +1323 * @name setByBigInteger +1324 * @memberOf KJUR.asn1.DEREnumerated# +1325 * @function +1326 * @param {BigInteger} bigIntegerValue to set +1327 */ +1328 this.setByBigInteger = function(bigIntegerValue) { +1329 this.hTLV = null; +1330 this.isModified = true; +1331 this.hV = twoscompl(bigIntegerValue); +1332 }; +1333 +1334 /** +1335 * set value by integer value +1336 * @name setByInteger +1337 * @memberOf KJUR.asn1.DEREnumerated# +1338 * @function +1339 * @param {Integer} integer value to set +1340 */ +1341 this.setByInteger = function(intValue) { +1342 var bi = new BigInteger(String(intValue), 10); +1343 this.setByBigInteger(bi); +1344 }; +1345 +1346 /** +1347 * set value by integer value +1348 * @name setValueHex +1349 * @memberOf KJUR.asn1.DEREnumerated# +1350 * @function +1351 * @param {String} hexadecimal string of integer value +1352 * @description +1353 * <br/> +1354 * NOTE: Value shall be represented by minimum octet length of +1355 * two's complement representation. +1356 */ +1357 this.setValueHex = function(newHexString) { +1358 this.hV = newHexString; 1359 }; 1360 -1361 /** -1362 * set value by integer value -1363 * @name setByInteger -1364 * @memberOf KJUR.asn1.DEREnumerated# -1365 * @function -1366 * @param {Integer} integer value to set -1367 */ -1368 this.setByInteger = function(intValue) { -1369 var bi = new BigInteger(String(intValue), 10); -1370 this.setByBigInteger(bi); -1371 }; -1372 -1373 /** -1374 * set value by integer value -1375 * @name setValueHex -1376 * @memberOf KJUR.asn1.DEREnumerated# -1377 * @function -1378 * @param {String} hexadecimal string of integer value -1379 * @description -1380 * <br/> -1381 * NOTE: Value shall be represented by minimum octet length of -1382 * two's complement representation. -1383 */ -1384 this.setValueHex = function(newHexString) { -1385 this.hV = newHexString; -1386 }; -1387 -1388 this.getFreshValueHex = function() { -1389 return this.hV; -1390 }; -1391 -1392 if (typeof params != "undefined") { -1393 if (typeof params['int'] != "undefined") { -1394 this.setByInteger(params['int']); -1395 } else if (typeof params == "number") { -1396 this.setByInteger(params); -1397 } else if (typeof params['hex'] != "undefined") { -1398 this.setValueHex(params['hex']); -1399 } -1400 } -1401 }; -1402 extendClass(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object); -1403 -1404 // ******************************************************************** -1405 /** -1406 * class for ASN.1 DER UTF8String -1407 * @name KJUR.asn1.DERUTF8String -1408 * @class class for ASN.1 DER UTF8String -1409 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1410 * @extends KJUR.asn1.DERAbstractString -1411 * @description -1412 * @see KJUR.asn1.DERAbstractString - superclass -1413 */ -1414 KJUR.asn1.DERUTF8String = function(params) { -1415 KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params); -1416 this.hT = "0c"; -1417 }; -1418 extendClass(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString); -1419 -1420 // ******************************************************************** -1421 /** -1422 * class for ASN.1 DER NumericString -1423 * @name KJUR.asn1.DERNumericString -1424 * @class class for ASN.1 DER NumericString -1425 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1426 * @extends KJUR.asn1.DERAbstractString -1427 * @description -1428 * @see KJUR.asn1.DERAbstractString - superclass -1429 */ -1430 KJUR.asn1.DERNumericString = function(params) { -1431 KJUR.asn1.DERNumericString.superclass.constructor.call(this, params); -1432 this.hT = "12"; -1433 }; -1434 extendClass(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString); -1435 -1436 // ******************************************************************** -1437 /** -1438 * class for ASN.1 DER PrintableString -1439 * @name KJUR.asn1.DERPrintableString -1440 * @class class for ASN.1 DER PrintableString -1441 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1442 * @extends KJUR.asn1.DERAbstractString -1443 * @description -1444 * @see KJUR.asn1.DERAbstractString - superclass -1445 */ -1446 KJUR.asn1.DERPrintableString = function(params) { -1447 KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params); -1448 this.hT = "13"; -1449 }; -1450 extendClass(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString); -1451 -1452 // ******************************************************************** -1453 /** -1454 * class for ASN.1 DER TeletexString -1455 * @name KJUR.asn1.DERTeletexString -1456 * @class class for ASN.1 DER TeletexString -1457 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1458 * @extends KJUR.asn1.DERAbstractString -1459 * @description -1460 * @see KJUR.asn1.DERAbstractString - superclass -1461 */ -1462 KJUR.asn1.DERTeletexString = function(params) { -1463 KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params); -1464 this.hT = "14"; -1465 }; -1466 extendClass(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString); -1467 -1468 // ******************************************************************** -1469 /** -1470 * class for ASN.1 DER IA5String -1471 * @name KJUR.asn1.DERIA5String -1472 * @class class for ASN.1 DER IA5String -1473 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1474 * @extends KJUR.asn1.DERAbstractString -1475 * @description -1476 * @see KJUR.asn1.DERAbstractString - superclass -1477 */ -1478 KJUR.asn1.DERIA5String = function(params) { -1479 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); -1480 this.hT = "16"; -1481 }; -1482 extendClass(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString); -1483 -1484 // ******************************************************************** -1485 /** -1486 * class for ASN.1 DER VisibleString -1487 * @name KJUR.asn1.DERVisibleString -1488 * @class class for ASN.1 DER VisibleString -1489 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1490 * @extends KJUR.asn1.DERAbstractString -1491 * @since jsrsasign 8.0.23 asn1 1.0.15 -1492 * @description -1493 * @see KJUR.asn1.DERAbstractString - superclass -1494 */ -1495 KJUR.asn1.DERVisibleString = function(params) { -1496 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); -1497 this.hT = "1a"; -1498 }; -1499 extendClass(KJUR.asn1.DERVisibleString, KJUR.asn1.DERAbstractString); -1500 -1501 // ******************************************************************** -1502 /** -1503 * class for ASN.1 DER BMPString -1504 * @name KJUR.asn1.DERBMPString -1505 * @class class for ASN.1 DER BMPString -1506 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) -1507 * @extends KJUR.asn1.DERAbstractString -1508 * @since jsrsasign 8.0.23 asn1 1.0.15 -1509 * @description -1510 * @see KJUR.asn1.DERAbstractString - superclass -1511 */ -1512 KJUR.asn1.DERBMPString = function(params) { -1513 KJUR.asn1.DERBMPString.superclass.constructor.call(this, params); -1514 this.hT = "1e"; -1515 }; -1516 extendClass(KJUR.asn1.DERBMPString, KJUR.asn1.DERAbstractString); -1517 -1518 // ******************************************************************** -1519 /** -1520 * class for ASN.1 DER UTCTime -1521 * @name KJUR.asn1.DERUTCTime -1522 * @class class for ASN.1 DER UTCTime -1523 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) -1524 * @extends KJUR.asn1.DERAbstractTime -1525 * @see KJUR.asn1.DERGeneralizedTime -1526 * @see KJUR.asn1.x509.Time -1527 * -1528 * @description -1529 * <br/> -1530 * As for argument 'params' for constructor, you can specify one of -1531 * following properties: -1532 * <ul> -1533 * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li> -1534 * <li>date - specify Date object.</li> -1535 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> -1536 * </ul> -1537 * NOTE1: 'params' can be omitted. -1538 * NOTE2: 'millis' property is supported from jsrsasign 10.4.1 asn1 1.0.22. -1539 * -1540 * <h4>EXAMPLES</h4> -1541 * @example -1542 * new DERUTCTime("20151231235959Z") -1543 * new DERUTCTime("20151231235959.123Z") -1544 * new DERUTCTime(new Date()) -1545 * new DERUTCTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) -1546 * new DERUTCTime({str: "20151231235959.123Z"}) -1547 * new DERUTCTime({date: new Date()}) -1548 * new DERUTCTime({date: new Date(), millis: true}) -1549 * new DERUTCTime({millis: true}) -1550 */ -1551 KJUR.asn1.DERUTCTime = function(params) { -1552 KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params); -1553 this.hT = "17"; -1554 this.params = undefined; -1555 -1556 this.getFreshValueHex = function() { -1557 var params = this.params; -1558 -1559 if (this.params == undefined) params = { date: new Date() }; -1560 -1561 if (typeof params == "string") { -1562 if (params.match(/^[0-9]{12}Z$/) || -1563 params.match(/^[0-9]{12}\.[0-9]+Z$/)) { -1564 this.hV = stohex(params); -1565 } else { -1566 throw new Error("malformed string for UTCTime: " + params); -1567 } -1568 } else if (params.str != undefined) { -1569 this.hV = stohex(params.str); -1570 } else if (params.date == undefined && params.millis == true) { -1571 var date = new Date(); -1572 this.hV = stohex(this.formatDate(date, 'utc', true)); -1573 } else if (params.date != undefined && -1574 params.date instanceof Date) { -1575 var withMillis = (params.millis === true); -1576 this.hV = stohex(this.formatDate(params.date, 'utc', withMillis)); -1577 } else if (params instanceof Date) { -1578 this.hV = stohex(this.formatDate(params, 'utc')); -1579 } -1580 -1581 if (this.hV == undefined) { -1582 throw new Error("parameter not specified properly for UTCTime"); -1583 } -1584 return this.hV; -1585 }; -1586 -1587 if (params != undefined) this.setByParam(params); -1588 }; -1589 extendClass(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime); -1590 -1591 // ******************************************************************** -1592 /** -1593 * class for ASN.1 DER GeneralizedTime -1594 * @name KJUR.asn1.DERGeneralizedTime -1595 * @class class for ASN.1 DER GeneralizedTime -1596 * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'}) -1597 * @property {Boolean} withMillis flag to show milliseconds or not -1598 * @extends KJUR.asn1.DERAbstractTime -1599 * @see KJUR.asn1.DERUTCTime -1600 * @see KJUR.asn1.x509.Time -1601 * -1602 * @description -1603 * <br/> -1604 * As for argument 'params' for constructor, you can specify one of -1605 * following properties: -1606 * <ul> -1607 * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li> -1608 * <li>date - specify Date object.</li> -1609 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> -1610 * </ul> -1611 * NOTE1: 'params' can be omitted. -1612 * NOTE2: 'millis' property is supported from asn1 1.0.6. -1613 * -1614 * <h4>EXAMPLES</h4> -1615 * @example -1616 * new DERGeneralizedTime("20151231235959Z") -1617 * new DERGeneralizedTime("20151231235959.123Z") -1618 * new DERGeneralizedTime(new Date()) -1619 * new DERGeneralizedTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) -1620 * new DERGeneralizedTime({str: "20151231235959.123Z"}) -1621 * new DERGeneralizedTime({date: new Date()}) -1622 * new DERGeneralizedTime({date: new Date(), millis: true}) -1623 * new DERGeneralizedTime({millis: true}) -1624 */ -1625 KJUR.asn1.DERGeneralizedTime = function(params) { -1626 KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params); -1627 this.hT = "18"; -1628 this.params = params; -1629 -1630 this.getFreshValueHex = function() { -1631 var params = this.params; -1632 -1633 if (this.params == undefined) params = { date: new Date() }; -1634 -1635 if (typeof params == "string") { -1636 if (params.match(/^[0-9]{14}Z$/) || -1637 params.match(/^[0-9]{14}\.[0-9]+Z$/)) { -1638 this.hV = stohex(params); -1639 } else { -1640 throw new Error("malformed string for GeneralizedTime: " + params); -1641 } -1642 } else if (params.str != undefined) { -1643 this.hV = stohex(params.str); -1644 } else if (params.date == undefined && params.millis == true) { -1645 var date = new Date(); -1646 this.hV = stohex(this.formatDate(date, 'gen', true)); -1647 } else if (params.date != undefined && -1648 params.date instanceof Date) { -1649 var withMillis = (params.millis === true); -1650 this.hV = stohex(this.formatDate(params.date, 'gen', withMillis)); -1651 } else if (params instanceof Date) { -1652 this.hV = stohex(this.formatDate(params, 'gen')); -1653 } -1654 -1655 if (this.hV == undefined) { -1656 throw new Error("parameter not specified properly for GeneralizedTime"); -1657 } -1658 return this.hV; -1659 }; -1660 -1661 if (params != undefined) this.setByParam(params); -1662 }; -1663 extendClass(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime); -1664 -1665 // ******************************************************************** -1666 /** -1667 * class for ASN.1 DER Sequence -1668 * @name KJUR.asn1.DERSequence -1669 * @class class for ASN.1 DER Sequence -1670 * @extends KJUR.asn1.DERAbstractStructured -1671 * @description -1672 * <br/> -1673 * As for argument 'params' for constructor, you can specify one of -1674 * following properties: -1675 * <ul> -1676 * <li>array - specify array of ASN1Object to set elements of content</li> -1677 * </ul> -1678 * NOTE: 'params' can be omitted. -1679 */ -1680 KJUR.asn1.DERSequence = function(params) { -1681 KJUR.asn1.DERSequence.superclass.constructor.call(this, params); -1682 this.hT = "30"; -1683 this.getFreshValueHex = function() { -1684 var h = ''; -1685 for (var i = 0; i < this.asn1Array.length; i++) { -1686 var asn1Obj = this.asn1Array[i]; -1687 h += asn1Obj.tohex(); -1688 } -1689 this.hV = h; -1690 return this.hV; -1691 }; -1692 }; -1693 extendClass(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured); -1694 -1695 // ******************************************************************** -1696 /** -1697 * class for ASN.1 DER Set -1698 * @name KJUR.asn1.DERSet -1699 * @class class for ASN.1 DER Set -1700 * @extends KJUR.asn1.DERAbstractStructured -1701 * @description -1702 * <br/> -1703 * As for argument 'params' for constructor, you can specify one of -1704 * following properties: -1705 * <ul> -1706 * <li>array - specify array of ASN1Object to set elements of content</li> -1707 * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li> -1708 * </ul> -1709 * NOTE1: 'params' can be omitted.<br/> -1710 * NOTE2: sortflag is supported since 1.0.5. -1711 */ -1712 KJUR.asn1.DERSet = function(params) { -1713 KJUR.asn1.DERSet.superclass.constructor.call(this, params); -1714 this.hT = "31"; -1715 this.sortFlag = true; // item shall be sorted only in ASN.1 DER -1716 this.getFreshValueHex = function() { -1717 var a = new Array(); -1718 for (var i = 0; i < this.asn1Array.length; i++) { -1719 var asn1Obj = this.asn1Array[i]; -1720 a.push(asn1Obj.tohex()); -1721 } -1722 if (this.sortFlag == true) a.sort(); -1723 this.hV = a.join(''); -1724 return this.hV; -1725 }; -1726 -1727 if (typeof params != "undefined") { -1728 if (typeof params.sortflag != "undefined" && -1729 params.sortflag == false) -1730 this.sortFlag = false; -1731 } -1732 }; -1733 extendClass(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured); -1734 -1735 // ******************************************************************** -1736 /** -1737 * class for ASN.1 DER TaggedObject -1738 * @name KJUR.asn1.DERTaggedObject -1739 * @class class for ASN.1 DER TaggedObject -1740 * @extends KJUR.asn1.ASN1Object -1741 * @see KJUR_asn1.ASN1Util.newObject -1742 * -1743 * @description -1744 * <br/> -1745 * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object. -1746 * For example, if you find '[1]' tag in a ASN.1 dump, -1747 * 'tagNoHex' will be 'a1'. -1748 * <br/> -1749 * As for optional argument 'params' for constructor, you can specify *ANY* of -1750 * following properties: -1751 * <ul> -1752 * <li>tag - specify tag (default is 'a0' which means [0])</li> -1753 * <li>explicit - specify true if this is explicit tag otherwise false -1754 * (default is 'true').</li> -1755 * <li>obj - specify ASN1Object or JSON object which will be tagged</li> -1756 * <li>tage - specify tag with explicit</li> -1757 * <li>tagi - specify tag with implicit</li> -1758 * </ul> -1759 * As for the member "obj" value of JSON object, -1760 * {@link KJUR_asn1.ASN1Util.newObject} is used to generate. -1761 * -1762 * @example -1763 * // by JSON -1764 * new KJUR.asn1.DERTaggedObject({ -1765 * tag:'a0', explicit: true, obj: { "prnstr": { "str": "aaa" } } -1766 * }).tohex() -1767 * -1768 * // by ASN1Object object -1769 * new KJUR.asn1.DERTaggedObject({ -1770 * tage:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // explicit -1771 * }) -1772 * new KJUR.asn1.DERTaggedObject({ -1773 * tagi:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // implicit -1774 * }) -1775 * new KJUR.asn1.DERTaggedObject({ -1776 * tag:'a0', explicit: true, obj: new KJUR.asn1.DERInteger({int: 3}) // explicit -1777 * }) -1778 * -1779 * // to hexadecimal -1780 * d1 = new KJUR.asn1.DERUTF8String({str':'a'}) -1781 * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1}); -1782 * hex = d2.tohex(); -1783 */ -1784 KJUR.asn1.DERTaggedObject = function(params) { -1785 KJUR.asn1.DERTaggedObject.superclass.constructor.call(this); -1786 -1787 var _KJUR_asn1 = KJUR.asn1, -1788 _ASN1HEX = ASN1HEX, -1789 _getV = _ASN1HEX.getV, -1790 _isASN1HEX = _ASN1HEX.isASN1HEX, -1791 _newObject = _KJUR_asn1.ASN1Util.newObject; +1361 this.getFreshValueHex = function() { +1362 return this.hV; +1363 }; +1364 +1365 if (typeof params != "undefined") { +1366 if (typeof params['int'] != "undefined") { +1367 this.setByInteger(params['int']); +1368 } else if (typeof params == "number") { +1369 this.setByInteger(params); +1370 } else if (typeof params['hex'] != "undefined") { +1371 this.setValueHex(params['hex']); +1372 } +1373 } +1374 }; +1375 extendClass(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object); +1376 +1377 // ******************************************************************** +1378 /** +1379 * class for ASN.1 DER UTF8String +1380 * @name KJUR.asn1.DERUTF8String +1381 * @class class for ASN.1 DER UTF8String +1382 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1383 * @extends KJUR.asn1.DERAbstractString +1384 * @description +1385 * @see KJUR.asn1.DERAbstractString - superclass +1386 */ +1387 KJUR.asn1.DERUTF8String = function(params) { +1388 KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params); +1389 this.hT = "0c"; +1390 }; +1391 extendClass(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString); +1392 +1393 // ******************************************************************** +1394 /** +1395 * class for ASN.1 DER NumericString +1396 * @name KJUR.asn1.DERNumericString +1397 * @class class for ASN.1 DER NumericString +1398 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1399 * @extends KJUR.asn1.DERAbstractString +1400 * @description +1401 * @see KJUR.asn1.DERAbstractString - superclass +1402 */ +1403 KJUR.asn1.DERNumericString = function(params) { +1404 KJUR.asn1.DERNumericString.superclass.constructor.call(this, params); +1405 this.hT = "12"; +1406 }; +1407 extendClass(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString); +1408 +1409 // ******************************************************************** +1410 /** +1411 * class for ASN.1 DER PrintableString +1412 * @name KJUR.asn1.DERPrintableString +1413 * @class class for ASN.1 DER PrintableString +1414 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1415 * @extends KJUR.asn1.DERAbstractString +1416 * @description +1417 * @see KJUR.asn1.DERAbstractString - superclass +1418 */ +1419 KJUR.asn1.DERPrintableString = function(params) { +1420 KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params); +1421 this.hT = "13"; +1422 }; +1423 extendClass(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString); +1424 +1425 // ******************************************************************** +1426 /** +1427 * class for ASN.1 DER TeletexString +1428 * @name KJUR.asn1.DERTeletexString +1429 * @class class for ASN.1 DER TeletexString +1430 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1431 * @extends KJUR.asn1.DERAbstractString +1432 * @description +1433 * @see KJUR.asn1.DERAbstractString - superclass +1434 */ +1435 KJUR.asn1.DERTeletexString = function(params) { +1436 KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params); +1437 this.hT = "14"; +1438 }; +1439 extendClass(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString); +1440 +1441 // ******************************************************************** +1442 /** +1443 * class for ASN.1 DER IA5String +1444 * @name KJUR.asn1.DERIA5String +1445 * @class class for ASN.1 DER IA5String +1446 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1447 * @extends KJUR.asn1.DERAbstractString +1448 * @description +1449 * @see KJUR.asn1.DERAbstractString - superclass +1450 */ +1451 KJUR.asn1.DERIA5String = function(params) { +1452 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); +1453 this.hT = "16"; +1454 }; +1455 extendClass(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString); +1456 +1457 // ******************************************************************** +1458 /** +1459 * class for ASN.1 DER VisibleString +1460 * @name KJUR.asn1.DERVisibleString +1461 * @class class for ASN.1 DER VisibleString +1462 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1463 * @extends KJUR.asn1.DERAbstractString +1464 * @since jsrsasign 8.0.23 asn1 1.0.15 +1465 * @description +1466 * @see KJUR.asn1.DERAbstractString - superclass +1467 */ +1468 KJUR.asn1.DERVisibleString = function(params) { +1469 KJUR.asn1.DERIA5String.superclass.constructor.call(this, params); +1470 this.hT = "1a"; +1471 }; +1472 extendClass(KJUR.asn1.DERVisibleString, KJUR.asn1.DERAbstractString); +1473 +1474 // ******************************************************************** +1475 /** +1476 * class for ASN.1 DER BMPString +1477 * @name KJUR.asn1.DERBMPString +1478 * @class class for ASN.1 DER BMPString +1479 * @param {Array} params associative array of parameters (ex. {'str': 'aaa'}) +1480 * @extends KJUR.asn1.DERAbstractString +1481 * @since jsrsasign 8.0.23 asn1 1.0.15 +1482 * @description +1483 * @see KJUR.asn1.DERAbstractString - superclass +1484 */ +1485 KJUR.asn1.DERBMPString = function(params) { +1486 KJUR.asn1.DERBMPString.superclass.constructor.call(this, params); +1487 this.hT = "1e"; +1488 }; +1489 extendClass(KJUR.asn1.DERBMPString, KJUR.asn1.DERAbstractString); +1490 +1491 // ******************************************************************** +1492 /** +1493 * class for ASN.1 DER UTCTime +1494 * @name KJUR.asn1.DERUTCTime +1495 * @class class for ASN.1 DER UTCTime +1496 * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'}) +1497 * @extends KJUR.asn1.DERAbstractTime +1498 * @see KJUR.asn1.DERGeneralizedTime +1499 * @see KJUR.asn1.x509.Time +1500 * +1501 * @description +1502 * <br/> +1503 * As for argument 'params' for constructor, you can specify one of +1504 * following properties: +1505 * <ul> +1506 * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li> +1507 * <li>date - specify Date object.</li> +1508 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> +1509 * </ul> +1510 * NOTE1: 'params' can be omitted. +1511 * NOTE2: 'millis' property is supported from jsrsasign 10.4.1 asn1 1.0.22. +1512 * +1513 * <h4>EXAMPLES</h4> +1514 * @example +1515 * new DERUTCTime("20151231235959Z") +1516 * new DERUTCTime("20151231235959.123Z") +1517 * new DERUTCTime(new Date()) +1518 * new DERUTCTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) +1519 * new DERUTCTime({str: "20151231235959.123Z"}) +1520 * new DERUTCTime({date: new Date()}) +1521 * new DERUTCTime({date: new Date(), millis: true}) +1522 * new DERUTCTime({millis: true}) +1523 */ +1524 KJUR.asn1.DERUTCTime = function(params) { +1525 KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params); +1526 this.hT = "17"; +1527 this.params = undefined; +1528 +1529 this.getFreshValueHex = function() { +1530 var params = this.params; +1531 +1532 if (this.params == undefined) params = { date: new Date() }; +1533 +1534 if (typeof params == "string") { +1535 if (params.match(/^[0-9]{12}Z$/) || +1536 params.match(/^[0-9]{12}\.[0-9]+Z$/)) { +1537 this.hV = stohex(params); +1538 } else { +1539 throw new Error("malformed string for UTCTime: " + params); +1540 } +1541 } else if (params.str != undefined) { +1542 this.hV = stohex(params.str); +1543 } else if (params.date == undefined && params.millis == true) { +1544 var date = new Date(); +1545 this.hV = stohex(this.formatDate(date, 'utc', true)); +1546 } else if (params.date != undefined && +1547 params.date instanceof Date) { +1548 var withMillis = (params.millis === true); +1549 this.hV = stohex(this.formatDate(params.date, 'utc', withMillis)); +1550 } else if (params instanceof Date) { +1551 this.hV = stohex(this.formatDate(params, 'utc')); +1552 } +1553 +1554 if (this.hV == undefined) { +1555 throw new Error("parameter not specified properly for UTCTime"); +1556 } +1557 return this.hV; +1558 }; +1559 +1560 if (params != undefined) this.setByParam(params); +1561 }; +1562 extendClass(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime); +1563 +1564 // ******************************************************************** +1565 /** +1566 * class for ASN.1 DER GeneralizedTime +1567 * @name KJUR.asn1.DERGeneralizedTime +1568 * @class class for ASN.1 DER GeneralizedTime +1569 * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'}) +1570 * @property {Boolean} withMillis flag to show milliseconds or not +1571 * @extends KJUR.asn1.DERAbstractTime +1572 * @see KJUR.asn1.DERUTCTime +1573 * @see KJUR.asn1.x509.Time +1574 * +1575 * @description +1576 * <br/> +1577 * As for argument 'params' for constructor, you can specify one of +1578 * following properties: +1579 * <ul> +1580 * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li> +1581 * <li>date - specify Date object.</li> +1582 * <li>millis - specify flag to show milliseconds (from 1.0.6)</li> +1583 * </ul> +1584 * NOTE1: 'params' can be omitted. +1585 * NOTE2: 'millis' property is supported from asn1 1.0.6. +1586 * +1587 * <h4>EXAMPLES</h4> +1588 * @example +1589 * new DERGeneralizedTime("20151231235959Z") +1590 * new DERGeneralizedTime("20151231235959.123Z") +1591 * new DERGeneralizedTime(new Date()) +1592 * new DERGeneralizedTime(new Date(Date.UTC(2015,11,31,23,59,59,123))) +1593 * new DERGeneralizedTime({str: "20151231235959.123Z"}) +1594 * new DERGeneralizedTime({date: new Date()}) +1595 * new DERGeneralizedTime({date: new Date(), millis: true}) +1596 * new DERGeneralizedTime({millis: true}) +1597 */ +1598 KJUR.asn1.DERGeneralizedTime = function(params) { +1599 KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params); +1600 this.hT = "18"; +1601 this.params = params; +1602 +1603 this.getFreshValueHex = function() { +1604 var params = this.params; +1605 +1606 if (this.params == undefined) params = { date: new Date() }; +1607 +1608 if (typeof params == "string") { +1609 if (params.match(/^[0-9]{14}Z$/) || +1610 params.match(/^[0-9]{14}\.[0-9]+Z$/)) { +1611 this.hV = stohex(params); +1612 } else { +1613 throw new Error("malformed string for GeneralizedTime: " + params); +1614 } +1615 } else if (params.str != undefined) { +1616 this.hV = stohex(params.str); +1617 } else if (params.date == undefined && params.millis == true) { +1618 var date = new Date(); +1619 this.hV = stohex(this.formatDate(date, 'gen', true)); +1620 } else if (params.date != undefined && +1621 params.date instanceof Date) { +1622 var withMillis = (params.millis === true); +1623 this.hV = stohex(this.formatDate(params.date, 'gen', withMillis)); +1624 } else if (params instanceof Date) { +1625 this.hV = stohex(this.formatDate(params, 'gen')); +1626 } +1627 +1628 if (this.hV == undefined) { +1629 throw new Error("parameter not specified properly for GeneralizedTime"); +1630 } +1631 return this.hV; +1632 }; +1633 +1634 if (params != undefined) this.setByParam(params); +1635 }; +1636 extendClass(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime); +1637 +1638 // ******************************************************************** +1639 /** +1640 * class for ASN.1 DER Sequence +1641 * @name KJUR.asn1.DERSequence +1642 * @class class for ASN.1 DER Sequence +1643 * @extends KJUR.asn1.DERAbstractStructured +1644 * @description +1645 * <br/> +1646 * As for argument 'params' for constructor, you can specify one of +1647 * following properties: +1648 * <ul> +1649 * <li>array - specify array of ASN1Object to set elements of content</li> +1650 * </ul> +1651 * NOTE: 'params' can be omitted. +1652 */ +1653 KJUR.asn1.DERSequence = function(params) { +1654 KJUR.asn1.DERSequence.superclass.constructor.call(this, params); +1655 this.hT = "30"; +1656 this.getFreshValueHex = function() { +1657 var h = ''; +1658 for (var i = 0; i < this.asn1Array.length; i++) { +1659 var asn1Obj = this.asn1Array[i]; +1660 h += asn1Obj.tohex(); +1661 } +1662 this.hV = h; +1663 return this.hV; +1664 }; +1665 }; +1666 extendClass(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured); +1667 +1668 // ******************************************************************** +1669 /** +1670 * class for ASN.1 DER Set +1671 * @name KJUR.asn1.DERSet +1672 * @class class for ASN.1 DER Set +1673 * @extends KJUR.asn1.DERAbstractStructured +1674 * @description +1675 * <br/> +1676 * As for argument 'params' for constructor, you can specify one of +1677 * following properties: +1678 * <ul> +1679 * <li>array - specify array of ASN1Object to set elements of content</li> +1680 * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li> +1681 * </ul> +1682 * NOTE1: 'params' can be omitted.<br/> +1683 * NOTE2: sortflag is supported since 1.0.5. +1684 */ +1685 KJUR.asn1.DERSet = function(params) { +1686 KJUR.asn1.DERSet.superclass.constructor.call(this, params); +1687 this.hT = "31"; +1688 this.sortFlag = true; // item shall be sorted only in ASN.1 DER +1689 this.getFreshValueHex = function() { +1690 var a = new Array(); +1691 for (var i = 0; i < this.asn1Array.length; i++) { +1692 var asn1Obj = this.asn1Array[i]; +1693 a.push(asn1Obj.tohex()); +1694 } +1695 if (this.sortFlag == true) a.sort(); +1696 this.hV = a.join(''); +1697 return this.hV; +1698 }; +1699 +1700 if (typeof params != "undefined") { +1701 if (typeof params.sortflag != "undefined" && +1702 params.sortflag == false) +1703 this.sortFlag = false; +1704 } +1705 }; +1706 extendClass(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured); +1707 +1708 // ******************************************************************** +1709 /** +1710 * class for ASN.1 DER TaggedObject +1711 * @name KJUR.asn1.DERTaggedObject +1712 * @class class for ASN.1 DER TaggedObject +1713 * @extends KJUR.asn1.ASN1Object +1714 * @see KJUR_asn1.ASN1Util.newObject +1715 * +1716 * @description +1717 * <br/> +1718 * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object. +1719 * For example, if you find '[1]' tag in a ASN.1 dump, +1720 * 'tagNoHex' will be 'a1'. +1721 * <br/> +1722 * As for optional argument 'params' for constructor, you can specify *ANY* of +1723 * following properties: +1724 * <ul> +1725 * <li>tag - specify tag (default is 'a0' which means [0])</li> +1726 * <li>explicit - specify true if this is explicit tag otherwise false +1727 * (default is 'true').</li> +1728 * <li>obj - specify ASN1Object or JSON object which will be tagged</li> +1729 * <li>tage - specify tag with explicit</li> +1730 * <li>tagi - specify tag with implicit</li> +1731 * </ul> +1732 * As for the member "obj" value of JSON object, +1733 * {@link KJUR_asn1.ASN1Util.newObject} is used to generate. +1734 * +1735 * @example +1736 * // by JSON +1737 * new KJUR.asn1.DERTaggedObject({ +1738 * tag:'a0', explicit: true, obj: { "prnstr": { "str": "aaa" } } +1739 * }).tohex() +1740 * +1741 * // by ASN1Object object +1742 * new KJUR.asn1.DERTaggedObject({ +1743 * tage:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // explicit +1744 * }) +1745 * new KJUR.asn1.DERTaggedObject({ +1746 * tagi:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // implicit +1747 * }) +1748 * new KJUR.asn1.DERTaggedObject({ +1749 * tag:'a0', explicit: true, obj: new KJUR.asn1.DERInteger({int: 3}) // explicit +1750 * }) +1751 * +1752 * // to hexadecimal +1753 * d1 = new KJUR.asn1.DERUTF8String({str':'a'}) +1754 * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1}); +1755 * hex = d2.tohex(); +1756 */ +1757 KJUR.asn1.DERTaggedObject = function(params) { +1758 KJUR.asn1.DERTaggedObject.superclass.constructor.call(this); +1759 +1760 var _KJUR_asn1 = KJUR.asn1, +1761 _ASN1HEX = ASN1HEX, +1762 _getV = _ASN1HEX.getV, +1763 _isASN1HEX = _ASN1HEX.isASN1HEX, +1764 _newObject = _KJUR_asn1.ASN1Util.newObject; +1765 +1766 this.hT = "a0"; +1767 this.hV = ''; +1768 this.isExplicit = true; +1769 this.asn1Object = null; +1770 this.params = {tag: "a0", explicit: true}; //"tag": "a0, "explicit": true}; +1771 +1772 /** +1773 * set value by an ASN1Object +1774 * @name setString +1775 * @memberOf KJUR.asn1.DERTaggedObject# +1776 * @function +1777 * @param {Boolean} isExplicitFlag flag for explicit/implicit tag +1778 * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag +1779 * @param {ASN1Object} asn1Object ASN.1 to encapsulate +1780 * @deprecated since jsrsasign 10.5.4 please use setByParam instead +1781 */ +1782 this.setASN1Object = function(isExplicitFlag, tagNoHex, asn1Object) { +1783 this.params = {tag: tagNoHex, +1784 explicit: isExplicitFlag, +1785 obj: asn1Object}; +1786 }; +1787 +1788 this.getFreshValueHex = function() { +1789 var params = this.params; +1790 +1791 if (params.explicit == undefined) params.explicit = true; 1792 -1793 this.hT = "a0"; -1794 this.hV = ''; -1795 this.isExplicit = true; -1796 this.asn1Object = null; -1797 this.params = {tag: "a0", explicit: true}; //"tag": "a0, "explicit": true}; -1798 -1799 /** -1800 * set value by an ASN1Object -1801 * @name setString -1802 * @memberOf KJUR.asn1.DERTaggedObject# -1803 * @function -1804 * @param {Boolean} isExplicitFlag flag for explicit/implicit tag -1805 * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag -1806 * @param {ASN1Object} asn1Object ASN.1 to encapsulate -1807 * @deprecated since jsrsasign 10.5.4 please use setByParam instead -1808 */ -1809 this.setASN1Object = function(isExplicitFlag, tagNoHex, asn1Object) { -1810 this.params = {tag: tagNoHex, -1811 explicit: isExplicitFlag, -1812 obj: asn1Object}; -1813 }; -1814 -1815 this.getFreshValueHex = function() { -1816 var params = this.params; -1817 -1818 if (params.explicit == undefined) params.explicit = true; -1819 -1820 if (params.tage != undefined) { -1821 params.tag = params.tage; -1822 params.explicit = true; -1823 } -1824 if (params.tagi != undefined) { -1825 params.tag = params.tagi; -1826 params.explicit = false; -1827 } -1828 -1829 if (params.str != undefined) { -1830 this.hV = utf8tohex(params.str); -1831 } else if (params.hex != undefined) { -1832 this.hV = params.hex; -1833 } else if (params.obj != undefined) { -1834 var hV1; -1835 if (params.obj instanceof _KJUR_asn1.ASN1Object) { -1836 hV1 = params.obj.tohex(); -1837 } else if (typeof params.obj == "object") { -1838 hV1 = _newObject(params.obj).tohex(); -1839 } -1840 if (params.explicit) { -1841 this.hV = hV1; -1842 } else { -1843 this.hV = _getV(hV1, 0); -1844 } -1845 } else { -1846 throw new Error("str, hex nor obj not specified"); -1847 } -1848 -1849 if (params.tag == undefined) params.tag = "a0"; -1850 this.hT = params.tag; -1851 this.hTLV = null; -1852 this.isModified = true; -1853 -1854 return this.hV; -1855 }; -1856 -1857 this.setByParam = function(params) { -1858 this.params = params; -1859 }; -1860 -1861 if (params !== undefined) this.setByParam(params); -1862 }; -1863 extendClass(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object); -1864