1 /* asn1-1.0.27.js (c) 2013-2023 Kenji Urushima | kjur.github.io/jsrsasign/license
  2  */
  3 /*
  4  * asn1.js - ASN.1 DER encoder classes
  5  *
  6  * Copyright (c) 2013-2022 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * https://kjur.github.io/jsrsasign/license
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name 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)
 20  * @since jsrsasign 2.1
 21  * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 
 24 /** 
 25  * kjur's class library name space
 26  * <p>
 27  * This name space provides following name spaces:
 28  * <ul>
 29  * <li>{@link KJUR.asn1} - ASN.1 primitive hexadecimal encoder</li>
 30  * <li>{@link KJUR.asn1.x509} - ASN.1 structure for X.509 certificate and CRL</li>
 31  * <li>{@link KJUR.crypto} - Java Cryptographic Extension(JCE) style MessageDigest/Signature 
 32  * class and utilities</li>
 33  * </ul>
 34  * </p> 
 35  * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
 36  * @name KJUR
 37  * @namespace kjur's class library name space
 38  */
 39 if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
 40 
 41 /**
 42  * kjur's ASN.1 class library name space
 43  * <p>
 44  * This is ITU-T X.690 ASN.1 DER encoder class library and
 45  * class structure and methods is very similar to 
 46  * org.bouncycastle.asn1 package of 
 47  * well known BouncyCaslte Cryptography Library.
 48  * <h4>PROVIDING ASN.1 PRIMITIVES</h4>
 49  * Here are ASN.1 DER primitive classes.
 50  * <ul>
 51  * <li>0x01 {@link KJUR.asn1.DERBoolean}</li>
 52  * <li>0x02 {@link KJUR.asn1.DERInteger}</li>
 53  * <li>0x03 {@link KJUR.asn1.DERBitString}</li>
 54  * <li>0x04 {@link KJUR.asn1.DEROctetString}</li>
 55  * <li>0x05 {@link KJUR.asn1.DERNull}</li>
 56  * <li>0x06 {@link KJUR.asn1.DERObjectIdentifier}</li>
 57  * <li>0x0a {@link KJUR.asn1.DEREnumerated}</li>
 58  * <li>0x0c {@link KJUR.asn1.DERUTF8String}</li>
 59  * <li>0x12 {@link KJUR.asn1.DERNumericString}</li>
 60  * <li>0x13 {@link KJUR.asn1.DERPrintableString}</li>
 61  * <li>0x14 {@link KJUR.asn1.DERTeletexString}</li>
 62  * <li>0x16 {@link KJUR.asn1.DERIA5String}</li>
 63  * <li>0x17 {@link KJUR.asn1.DERUTCTime}</li>
 64  * <li>0x18 {@link KJUR.asn1.DERGeneralizedTime}</li>
 65  * <li>0x1a {@link KJUR.asn1.DERVisibleString}</li>
 66  * <li>0x1e {@link KJUR.asn1.DERBMPString}</li>
 67  * <li>0x30 {@link KJUR.asn1.DERSequence}</li>
 68  * <li>0x31 {@link KJUR.asn1.DERSet}</li>
 69  * </ul>
 70  * <h4>OTHER ASN.1 CLASSES</h4>
 71  * <ul>
 72  * <li>{@link KJUR.asn1.ASN1Object}</li>
 73  * <li>{@link KJUR.asn1.DERAbstractString}</li>
 74  * <li>{@link KJUR.asn1.DERAbstractTime}</li>
 75  * <li>{@link KJUR.asn1.DERAbstractStructured}</li>
 76  * <li>{@link KJUR.asn1.DERTaggedObject}</li>
 77  * </ul>
 78  * <h4>SUB NAME SPACES</h4>
 79  * <ul>
 80  * <li>{@link KJUR.asn1.cades} - CAdES long term signature format</li>
 81  * <li>{@link KJUR.asn1.cms} - Cryptographic Message Syntax</li>
 82  * <li>{@link KJUR.asn1.csr} - Certificate Signing Request (CSR/PKCS#10)</li>
 83  * <li>{@link KJUR.asn1.tsp} - RFC 3161 Timestamping Protocol Format</li>
 84  * <li>{@link KJUR.asn1.x509} - RFC 5280 X.509 certificate and CRL</li>
 85  * </ul>
 86  * </p>
 87  * NOTE: Please ignore method summary and document of this namespace. 
 88  * This caused by a bug of jsdoc2.
 89  * @name KJUR.asn1
 90  * @namespace
 91  */
 92 if (typeof KJUR.asn1 == "undefined" || !KJUR.asn1) KJUR.asn1 = {};
 93 
 94 /**
 95  * ASN1 utilities class
 96  * @name KJUR.asn1.ASN1Util
 97  * @class ASN1 utilities class
 98  * @since asn1 1.0.2
 99  */
100 KJUR.asn1.ASN1Util = new function() {
101     this.integerToByteHex = function(i) {
102         var h = i.toString(16);
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;
721     };
722 
723     /**
724      * get string value of this string object (DEPRECATED)
725      * @name getString
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 ==================================================
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     };
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)
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);
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;
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