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
  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.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  */
 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) { // 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      * set value by a Date object<br/>
725      * @name setByDate
726      * @memberOf KJUR.asn1.DERAbstractTime#
727      * @function
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     /**
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 /**
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.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     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 	if (params.tage != undefined) {
1794 	    params.tag = params.tage;
1795 	    params.explicit = true;
1796 	}
1797 	if (params.tagi != undefined) {
1798 	    params.tag = params.tagi;
1799 	    params.explicit = false;
1800 	}
1801 
1802 	if (params.str != undefined) {
1803 	    this.hV = utf8tohex(params.str);
1804 	} else if (params.hex != undefined) {
1805 	    this.hV = params.hex;
1806 	} else if (params.obj != undefined) {
1807 	    var hV1;
1808 	    if (params.obj instanceof _KJUR_asn1.ASN1Object) {
1809 		hV1 = params.obj.tohex();
1810 	    } else if (typeof params.obj == "object") {
1811 		hV1 = _newObject(params.obj).tohex();
1812 	    }
1813 	    if (params.explicit) {
1814 		this.hV = hV1;
1815 	    } else {
1816 		this.hV = _getV(hV1, 0);
1817 	    }
1818 	} else {
1819 	    throw new Error("str, hex nor obj not specified");
1820 	}
1821 
1822 	if (params.tag == undefined) params.tag = "a0";
1823 	this.hT = params.tag;
1824         this.hTLV = null;
1825         this.isModified = true;
1826 
1827         return this.hV;
1828     };
1829 
1830     this.setByParam = function(params) {
1831 	this.params = params;
1832     };
1833 
1834     if (params !== undefined) this.setByParam(params);
1835 };
1836 extendClass(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object);
1837