1 /* asn1-1.0.26.js (c) 2013-2022 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.5.22 asn1 1.0.26 (2022-May-24)
 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 
895     /**
896      * set value by Tom Wu's BigInteger object
897      * @name setByBigInteger
898      * @memberOf KJUR.asn1.DERInteger#
899      * @function
900      * @param {BigInteger} bigIntegerValue to set
901      */
902     this.setByBigInteger = function(bigIntegerValue) {
903         this.hTLV = null;
904         this.isModified = true;
905         this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
906     };
907 
908     /**
909      * set value by integer value
910      * @name setByInteger
911      * @memberOf KJUR.asn1.DERInteger
912      * @function
913      * @param {Integer} integer value to set
914      */
915     this.setByInteger = function(intValue) {
916         var bi = new BigInteger(String(intValue), 10);
917         this.setByBigInteger(bi);
918     };
919 
920     /**
921      * set value by integer value
922      * @name setValueHex
923      * @memberOf KJUR.asn1.DERInteger#
924      * @function
925      * @param {String} hexadecimal string of integer value
926      * @description
927      * <br/>
928      * NOTE: Value shall be represented by minimum octet length of
929      * two's complement representation.
930      * @example
931      * new KJUR.asn1.DERInteger(123);
932      * new KJUR.asn1.DERInteger({'int': 123});
933      * new KJUR.asn1.DERInteger({'hex': '1fad'});
934      */
935     this.setValueHex = function(newHexString) {
936         this.hV = newHexString;
937     };
938 
939     this.getFreshValueHex = function() {
940         return this.hV;
941     };
942 
943     if (typeof params != "undefined") {
944         if (typeof params['bigint'] != "undefined") {
945             this.setByBigInteger(params['bigint']);
946         } else if (typeof params['int'] != "undefined") {
947             this.setByInteger(params['int']);
948         } else if (typeof params == "number") {
949             this.setByInteger(params);
950         } else if (typeof params['hex'] != "undefined") {
951             this.setValueHex(params['hex']);
952         }
953     }
954 };
955 extendClass(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object);
956 
957 // ********************************************************************
958 /**
959  * class for ASN.1 DER encoded BitString primitive
960  * @name KJUR.asn1.DERBitString
961  * @class class for ASN.1 DER encoded BitString primitive
962  * @extends KJUR.asn1.ASN1Object
963  * @description 
964  * <br/>
965  * As for argument 'params' for constructor, you can specify one of
966  * following properties:
967  * <ul>
968  * <li>bin - specify binary string (ex. '10111')</li>
969  * <li>array - specify array of boolean (ex. [true,false,true,true])</li>
970  * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li>
971  * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} 
972  * argument for "BitString encapsulates" structure.</li>
973  * </ul>
974  * NOTE1: 'params' can be omitted.<br/>
975  * NOTE2: 'obj' parameter have been supported since
976  * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/>
977  *
978  * @example
979  * // default constructor
980  * o = new KJUR.asn1.DERBitString();
981  * // initialize with binary string
982  * o = new KJUR.asn1.DERBitString({bin: "1011"});
983  * // initialize with boolean array
984  * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]});
985  * // initialize with hexadecimal string (04 is unused bits)
986  * o = new KJUR.asn1.DERBitString({hex: "04bac0"});
987  * // initialize with ASN1Util.newObject argument for encapsulated
988  * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}});
989  * // above generates a ASN.1 data like this:
990  * // BIT STRING, encapsulates {
991  * //   SEQUENCE {
992  * //     INTEGER 3
993  * //     PrintableString 'aaa'
994  * //     }
995  * //   } 
996  */
997 KJUR.asn1.DERBitString = function(params) {
998     if (params !== undefined && typeof params.obj !== "undefined") {
999 	var o = KJUR.asn1.ASN1Util.newObject(params.obj);
1000 	params.hex = "00" + o.tohex();
1001     }
1002     KJUR.asn1.DERBitString.superclass.constructor.call(this);
1003     this.hT = "03";
1004 
1005     /**
1006      * set ASN.1 value(V) by a hexadecimal string including unused bits
1007      * @name setHexValueIncludingUnusedBits
1008      * @memberOf KJUR.asn1.DERBitString#
1009      * @function
1010      * @param {String} newHexStringIncludingUnusedBits
1011      */
1012     this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) {
1013         this.hTLV = null;
1014         this.isModified = true;
1015         this.hV = newHexStringIncludingUnusedBits;
1016     };
1017 
1018     /**
1019      * set ASN.1 value(V) by unused bit and hexadecimal string of value
1020      * @name setUnusedBitsAndHexValue
1021      * @memberOf KJUR.asn1.DERBitString#
1022      * @function
1023      * @param {Integer} unusedBits
1024      * @param {String} hValue
1025      */
1026     this.setUnusedBitsAndHexValue = function(unusedBits, hValue) {
1027         if (unusedBits < 0 || 7 < unusedBits) {
1028             throw "unused bits shall be from 0 to 7: u = " + unusedBits;
1029         }
1030         var hUnusedBits = "0" + unusedBits;
1031         this.hTLV = null;
1032         this.isModified = true;
1033         this.hV = hUnusedBits + hValue;
1034     };
1035 
1036     /**
1037      * set ASN.1 DER BitString by binary string<br/>
1038      * @name setByBinaryString
1039      * @memberOf KJUR.asn1.DERBitString#
1040      * @function
1041      * @param {String} binaryString binary value string (i.e. '10111')
1042      * @description
1043      * Its unused bits will be calculated automatically by length of 
1044      * 'binaryValue'. <br/>
1045      * NOTE: Leading zeros '0' will be ignored.
1046      * @example
1047      * o = new KJUR.asn1.DERBitString();
1048      * o.setByBinaryString("1011");
1049      * o.setByBinaryString("001"); // leading zeros ignored
1050      */
1051     this.setByBinaryString = function(binaryString) {
1052         binaryString = binaryString.replace(/0+$/, '');
1053         var unusedBits = 8 - binaryString.length % 8;
1054         if (unusedBits == 8) unusedBits = 0;
1055 	
1056 	binaryString += "0000000".substr(0, unusedBits);
1057 
1058         var h = '';
1059         for (var i = 0; i < binaryString.length - 1; i += 8) {
1060             var b = binaryString.substr(i, 8);
1061             var x = parseInt(b, 2).toString(16);
1062             if (x.length == 1) x = '0' + x;
1063             h += x;  
1064         }
1065         this.hTLV = null;
1066         this.isModified = true;
1067         this.hV = '0' + unusedBits + h;
1068     };
1069 
1070     /**
1071      * set ASN.1 TLV value(V) by an array of boolean<br/>
1072      * @name setByBooleanArray
1073      * @memberOf KJUR.asn1.DERBitString#
1074      * @function
1075      * @param {array} booleanArray array of boolean (ex. [true, false, true])
1076      * @description
1077      * NOTE: Trailing falses will be ignored in the ASN.1 DER Object.
1078      * @example
1079      * o = new KJUR.asn1.DERBitString();
1080      * o.setByBooleanArray([false, true, false, true, true]);
1081      */
1082     this.setByBooleanArray = function(booleanArray) {
1083         var s = '';
1084         for (var i = 0; i < booleanArray.length; i++) {
1085             if (booleanArray[i] == true) {
1086                 s += '1';
1087             } else {
1088                 s += '0';
1089             }
1090         }
1091         this.setByBinaryString(s);
1092     };
1093 
1094     /**
1095      * generate an array of falses with specified length<br/>
1096      * @name newFalseArray
1097      * @memberOf KJUR.asn1.DERBitString
1098      * @function
1099      * @param {Integer} nLength length of array to generate
1100      * @return {array} array of boolean falses
1101      * @description
1102      * This static method may be useful to initialize boolean array.
1103      * @example
1104      * o = new KJUR.asn1.DERBitString();
1105      * o.newFalseArray(3) → [false, false, false]
1106      */
1107     this.newFalseArray = function(nLength) {
1108         var a = new Array(nLength);
1109         for (var i = 0; i < nLength; i++) {
1110             a[i] = false;
1111         }
1112         return a;
1113     };
1114 
1115     this.getFreshValueHex = function() {
1116         return this.hV;
1117     };
1118 
1119     if (typeof params != "undefined") {
1120         if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) {
1121             this.setHexValueIncludingUnusedBits(params);
1122         } else if (typeof params['hex'] != "undefined") {
1123             this.setHexValueIncludingUnusedBits(params['hex']);
1124         } else if (typeof params['bin'] != "undefined") {
1125             this.setByBinaryString(params['bin']);
1126         } else if (typeof params['array'] != "undefined") {
1127             this.setByBooleanArray(params['array']);
1128         }
1129     }
1130 };
1131 extendClass(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object);
1132 
1133 // ********************************************************************
1134 /**
1135  * class for ASN.1 DER OctetString<br/>
1136  * @name KJUR.asn1.DEROctetString
1137  * @class class for ASN.1 DER OctetString
1138  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1139  * @extends KJUR.asn1.DERAbstractString
1140  * @description
1141  * This class provides ASN.1 OctetString simple type.<br/>
1142  * Supported "params" attributes are:
1143  * <ul>
1144  * <li>str - to set a string as a value</li>
1145  * <li>hex - to set a hexadecimal string as a value</li>
1146  * <li>obj - to set a encapsulated ASN.1 value by JSON object 
1147  * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li>
1148  * </ul>
1149  * NOTE: A parameter 'obj' have been supported 
1150  * for "OCTET STRING, encapsulates" structure.
1151  * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).
1152  * @see KJUR.asn1.DERAbstractString - superclass
1153  * @example
1154  * // default constructor
1155  * o = new KJUR.asn1.DEROctetString();
1156  * // initialize with string
1157  * o = new KJUR.asn1.DEROctetString({str: "aaa"});
1158  * // initialize with hexadecimal string
1159  * o = new KJUR.asn1.DEROctetString({hex: "616161"});
1160  * // initialize with ASN1Util.newObject argument 
1161  * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}});
1162  * // above generates a ASN.1 data like this:
1163  * // OCTET STRING, encapsulates {
1164  * //   SEQUENCE {
1165  * //     INTEGER 3
1166  * //     PrintableString 'aaa'
1167  * //     }
1168  * //   } 
1169  */
1170 KJUR.asn1.DEROctetString = function(params) {
1171     if (params !== undefined && typeof params.obj !== "undefined") {
1172 	var o = KJUR.asn1.ASN1Util.newObject(params.obj);
1173 	params.hex = o.tohex();
1174     }
1175     KJUR.asn1.DEROctetString.superclass.constructor.call(this, params);
1176     this.hT = "04";
1177 };
1178 extendClass(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString);
1179 
1180 // ********************************************************************
1181 /**
1182  * class for ASN.1 DER Null
1183  * @name KJUR.asn1.DERNull
1184  * @class class for ASN.1 DER Null
1185  * @extends KJUR.asn1.ASN1Object
1186  * @description
1187  * @see KJUR.asn1.ASN1Object - superclass
1188  */
1189 KJUR.asn1.DERNull = function() {
1190     KJUR.asn1.DERNull.superclass.constructor.call(this);
1191     this.hT = "05";
1192     this.hTLV = "0500";
1193 };
1194 extendClass(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object);
1195 
1196 // ********************************************************************
1197 /**
1198  * class for ASN.1 DER ObjectIdentifier
1199  * @name KJUR.asn1.DERObjectIdentifier
1200  * @class class for ASN.1 DER ObjectIdentifier
1201  * @param {Object} JSON object or string of parameters (ex. {'oid': '2.5.4.5'})
1202  * @extends KJUR.asn1.ASN1Object
1203  * @see oidtohex
1204  * 
1205  * @description
1206  * <br/>
1207  * As for argument 'params' for constructor, you can specify one of
1208  * following properties:
1209  * <ul>
1210  * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li>
1211  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
1212  * </ul>
1213  * NOTE: 'params' can be omitted.
1214  * @example
1215  * new DERObjectIdentifier({"name": "sha1"})
1216  * new DERObjectIdentifier({"oid": "1.2.3.4"})
1217  * new DERObjectIdentifier({"hex": "2d..."})
1218  * new DERObjectIdentifier("1.2.3.4")
1219  * new DERObjectIdentifier("SHA1withRSA")
1220  */
1221 KJUR.asn1.DERObjectIdentifier = function(params) {
1222     KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this);
1223     this.hT = "06";
1224 
1225     /**
1226      * set value by a hexadecimal string
1227      * @name setValueHex
1228      * @memberOf KJUR.asn1.DERObjectIdentifier#
1229      * @function
1230      * @param {String} newHexString hexadecimal value of OID bytes
1231      */
1232     this.setValueHex = function(newHexString) {
1233         this.hTLV = null;
1234         this.isModified = true;
1235         this.s = null;
1236         this.hV = newHexString;
1237     };
1238 
1239     /**
1240      * set value by a OID string<br/>
1241      * @name setValueOidString
1242      * @memberOf KJUR.asn1.DERObjectIdentifier#
1243      * @function
1244      * @param {String} oidString OID string (ex. 2.5.4.13)
1245      * @example
1246      * o = new KJUR.asn1.DERObjectIdentifier();
1247      * o.setValueOidString("2.5.4.13");
1248      */
1249     this.setValueOidString = function(oidString) {
1250 	var h = oidtohex(oidString);
1251 	if (h == null)
1252             throw new Error("malformed oid string: " + oidString);
1253         this.hTLV = null;
1254         this.isModified = true;
1255         this.s = null;
1256         this.hV = h;
1257     };
1258 
1259     /**
1260      * set value by a OID name
1261      * @name setValueName
1262      * @memberOf KJUR.asn1.DERObjectIdentifier#
1263      * @function
1264      * @param {String} oidName OID name (ex. 'serverAuth')
1265      * @since 1.0.1
1266      * @description
1267      * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'.
1268      * Otherwise raise error.
1269      * @example
1270      * o = new KJUR.asn1.DERObjectIdentifier();
1271      * o.setValueName("serverAuth");
1272      */
1273     this.setValueName = function(oidName) {
1274 	var oid = KJUR.asn1.x509.OID.name2oid(oidName);
1275 	if (oid !== '') {
1276             this.setValueOidString(oid);
1277         } else {
1278             throw new Error("DERObjectIdentifier oidName undefined: " + oidName);
1279         }
1280     };
1281 
1282     this.setValueNameOrOid = function(nameOrOid) {
1283 	if (nameOrOid.match(/^[0-2].[0-9.]+$/)) {
1284 	    this.setValueOidString(nameOrOid);
1285 	} else {
1286 	    this.setValueName(nameOrOid);
1287 	}
1288     }
1289 
1290     this.getFreshValueHex = function() {
1291         return this.hV;
1292     };
1293 
1294     this.setByParam = function(params) {
1295         if (typeof params === "string") {
1296 	    this.setValueNameOrOid(params);
1297         } else if (params.oid !== undefined) {
1298 	    this.setValueNameOrOid(params.oid);
1299         } else if (params.name !== undefined) {
1300             this.setValueNameOrOid(params.name);
1301         } else if (params.hex !== undefined) {
1302             this.setValueHex(params.hex);
1303         }
1304     };
1305 
1306     if (params !== undefined) this.setByParam(params);
1307 };
1308 extendClass(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object);
1309 
1310 // ********************************************************************
1311 /**
1312  * class for ASN.1 DER Enumerated
1313  * @name KJUR.asn1.DEREnumerated
1314  * @class class for ASN.1 DER Enumerated
1315  * @extends KJUR.asn1.ASN1Object
1316  * @description
1317  * <br/>
1318  * As for argument 'params' for constructor, you can specify one of
1319  * following properties:
1320  * <ul>
1321  * <li>int - specify initial ASN.1 value(V) by integer value</li>
1322  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
1323  * </ul>
1324  * NOTE: 'params' can be omitted.
1325  * @example
1326  * new KJUR.asn1.DEREnumerated(123);
1327  * new KJUR.asn1.DEREnumerated({int: 123});
1328  * new KJUR.asn1.DEREnumerated({hex: '1fad'});
1329  */
1330 KJUR.asn1.DEREnumerated = function(params) {
1331     KJUR.asn1.DEREnumerated.superclass.constructor.call(this);
1332     this.hT = "0a";
1333 
1334     /**
1335      * set value by Tom Wu's BigInteger object
1336      * @name setByBigInteger
1337      * @memberOf KJUR.asn1.DEREnumerated#
1338      * @function
1339      * @param {BigInteger} bigIntegerValue to set
1340      */
1341     this.setByBigInteger = function(bigIntegerValue) {
1342         this.hTLV = null;
1343         this.isModified = true;
1344         this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
1345     };
1346 
1347     /**
1348      * set value by integer value
1349      * @name setByInteger
1350      * @memberOf KJUR.asn1.DEREnumerated#
1351      * @function
1352      * @param {Integer} integer value to set
1353      */
1354     this.setByInteger = function(intValue) {
1355         var bi = new BigInteger(String(intValue), 10);
1356         this.setByBigInteger(bi);
1357     };
1358 
1359     /**
1360      * set value by integer value
1361      * @name setValueHex
1362      * @memberOf KJUR.asn1.DEREnumerated#
1363      * @function
1364      * @param {String} hexadecimal string of integer value
1365      * @description
1366      * <br/>
1367      * NOTE: Value shall be represented by minimum octet length of
1368      * two's complement representation.
1369      */
1370     this.setValueHex = function(newHexString) {
1371         this.hV = newHexString;
1372     };
1373 
1374     this.getFreshValueHex = function() {
1375         return this.hV;
1376     };
1377 
1378     if (typeof params != "undefined") {
1379         if (typeof params['int'] != "undefined") {
1380             this.setByInteger(params['int']);
1381         } else if (typeof params == "number") {
1382             this.setByInteger(params);
1383         } else if (typeof params['hex'] != "undefined") {
1384             this.setValueHex(params['hex']);
1385         }
1386     }
1387 };
1388 extendClass(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object);
1389 
1390 // ********************************************************************
1391 /**
1392  * class for ASN.1 DER UTF8String
1393  * @name KJUR.asn1.DERUTF8String
1394  * @class class for ASN.1 DER UTF8String
1395  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1396  * @extends KJUR.asn1.DERAbstractString
1397  * @description
1398  * @see KJUR.asn1.DERAbstractString - superclass
1399  */
1400 KJUR.asn1.DERUTF8String = function(params) {
1401     KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params);
1402     this.hT = "0c";
1403 };
1404 extendClass(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString);
1405 
1406 // ********************************************************************
1407 /**
1408  * class for ASN.1 DER NumericString
1409  * @name KJUR.asn1.DERNumericString
1410  * @class class for ASN.1 DER NumericString
1411  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1412  * @extends KJUR.asn1.DERAbstractString
1413  * @description
1414  * @see KJUR.asn1.DERAbstractString - superclass
1415  */
1416 KJUR.asn1.DERNumericString = function(params) {
1417     KJUR.asn1.DERNumericString.superclass.constructor.call(this, params);
1418     this.hT = "12";
1419 };
1420 extendClass(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString);
1421 
1422 // ********************************************************************
1423 /**
1424  * class for ASN.1 DER PrintableString
1425  * @name KJUR.asn1.DERPrintableString
1426  * @class class for ASN.1 DER PrintableString
1427  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1428  * @extends KJUR.asn1.DERAbstractString
1429  * @description
1430  * @see KJUR.asn1.DERAbstractString - superclass
1431  */
1432 KJUR.asn1.DERPrintableString = function(params) {
1433     KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params);
1434     this.hT = "13";
1435 };
1436 extendClass(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString);
1437 
1438 // ********************************************************************
1439 /**
1440  * class for ASN.1 DER TeletexString
1441  * @name KJUR.asn1.DERTeletexString
1442  * @class class for ASN.1 DER TeletexString
1443  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1444  * @extends KJUR.asn1.DERAbstractString
1445  * @description
1446  * @see KJUR.asn1.DERAbstractString - superclass
1447  */
1448 KJUR.asn1.DERTeletexString = function(params) {
1449     KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params);
1450     this.hT = "14";
1451 };
1452 extendClass(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString);
1453 
1454 // ********************************************************************
1455 /**
1456  * class for ASN.1 DER IA5String
1457  * @name KJUR.asn1.DERIA5String
1458  * @class class for ASN.1 DER IA5String
1459  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1460  * @extends KJUR.asn1.DERAbstractString
1461  * @description
1462  * @see KJUR.asn1.DERAbstractString - superclass
1463  */
1464 KJUR.asn1.DERIA5String = function(params) {
1465     KJUR.asn1.DERIA5String.superclass.constructor.call(this, params);
1466     this.hT = "16";
1467 };
1468 extendClass(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString);
1469 
1470 // ********************************************************************
1471 /**
1472  * class for ASN.1 DER VisibleString
1473  * @name KJUR.asn1.DERVisibleString
1474  * @class class for ASN.1 DER VisibleString
1475  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1476  * @extends KJUR.asn1.DERAbstractString
1477  * @since jsrsasign 8.0.23 asn1 1.0.15
1478  * @description
1479  * @see KJUR.asn1.DERAbstractString - superclass
1480  */
1481 KJUR.asn1.DERVisibleString = function(params) {
1482     KJUR.asn1.DERIA5String.superclass.constructor.call(this, params);
1483     this.hT = "1a";
1484 };
1485 extendClass(KJUR.asn1.DERVisibleString, KJUR.asn1.DERAbstractString);
1486 
1487 // ********************************************************************
1488 /**
1489  * class for ASN.1 DER BMPString
1490  * @name KJUR.asn1.DERBMPString
1491  * @class class for ASN.1 DER BMPString
1492  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1493  * @extends KJUR.asn1.DERAbstractString
1494  * @since jsrsasign 8.0.23 asn1 1.0.15
1495  * @description
1496  * @see KJUR.asn1.DERAbstractString - superclass
1497  */
1498 KJUR.asn1.DERBMPString = function(params) {
1499     KJUR.asn1.DERBMPString.superclass.constructor.call(this, params);
1500     this.hT = "1e";
1501 };
1502 extendClass(KJUR.asn1.DERBMPString, KJUR.asn1.DERAbstractString);
1503 
1504 // ********************************************************************
1505 /**
1506  * class for ASN.1 DER UTCTime
1507  * @name KJUR.asn1.DERUTCTime
1508  * @class class for ASN.1 DER UTCTime
1509  * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'})
1510  * @extends KJUR.asn1.DERAbstractTime
1511  * @see KJUR.asn1.DERGeneralizedTime
1512  * @see KJUR.asn1.x509.Time
1513  *
1514  * @description
1515  * <br/>
1516  * As for argument 'params' for constructor, you can specify one of
1517  * following properties:
1518  * <ul>
1519  * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li>
1520  * <li>date - specify Date object.</li>
1521  * <li>millis - specify flag to show milliseconds (from 1.0.6)</li>
1522  * </ul>
1523  * NOTE1: 'params' can be omitted.
1524  * NOTE2: 'millis' property is supported from jsrsasign 10.4.1 asn1 1.0.22.
1525  *
1526  * <h4>EXAMPLES</h4>
1527  * @example
1528  * new DERUTCTime("20151231235959Z")
1529  * new DERUTCTime("20151231235959.123Z")
1530  * new DERUTCTime(new Date())
1531  * new DERUTCTime(new Date(Date.UTC(2015,11,31,23,59,59,123)))
1532  * new DERUTCTime({str: "20151231235959.123Z"})
1533  * new DERUTCTime({date: new Date()})
1534  * new DERUTCTime({date: new Date(), millis: true})
1535  * new DERUTCTime({millis: true})
1536  */
1537 KJUR.asn1.DERUTCTime = function(params) {
1538     KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params);
1539     this.hT = "17";
1540     this.params = undefined;
1541 
1542     this.getFreshValueHex = function() {
1543 	var params = this.params;
1544 
1545 	if (this.params == undefined) params = { date: new Date() };
1546 
1547 	if (typeof params == "string") {
1548 	    if (params.match(/^[0-9]{12}Z$/) ||
1549 		params.match(/^[0-9]{12}\.[0-9]+Z$/)) {
1550 		this.hV = stohex(params);
1551 	    } else {
1552 		throw new Error("malformed string for UTCTime: " + params);
1553 	    }
1554 	} else if (params.str != undefined) {
1555 	    this.hV = stohex(params.str);
1556 	} else if (params.date == undefined && params.millis == true) {
1557 	    var date = new Date();
1558 	    this.hV = stohex(this.formatDate(date, 'utc', true));
1559 	} else if (params.date != undefined &&
1560 		   params.date instanceof Date) {
1561 	    var withMillis = (params.millis === true);
1562 	    this.hV = stohex(this.formatDate(params.date, 'utc', withMillis));
1563 	} else if (params instanceof Date) {
1564 	    this.hV = stohex(this.formatDate(params, 'utc'));
1565 	}
1566 
1567 	if (this.hV == undefined) {
1568 	    throw new Error("parameter not specified properly for UTCTime");
1569 	}
1570 	return this.hV;
1571     };
1572 
1573     if (params != undefined) this.setByParam(params);
1574 };
1575 extendClass(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime);
1576 
1577 // ********************************************************************
1578 /**
1579  * class for ASN.1 DER GeneralizedTime
1580  * @name KJUR.asn1.DERGeneralizedTime
1581  * @class class for ASN.1 DER GeneralizedTime
1582  * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'})
1583  * @property {Boolean} withMillis flag to show milliseconds or not
1584  * @extends KJUR.asn1.DERAbstractTime
1585  * @see KJUR.asn1.DERUTCTime
1586  * @see KJUR.asn1.x509.Time
1587  *
1588  * @description
1589  * <br/>
1590  * As for argument 'params' for constructor, you can specify one of
1591  * following properties:
1592  * <ul>
1593  * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li>
1594  * <li>date - specify Date object.</li>
1595  * <li>millis - specify flag to show milliseconds (from 1.0.6)</li>
1596  * </ul>
1597  * NOTE1: 'params' can be omitted.
1598  * NOTE2: 'millis' property is supported from asn1 1.0.6.
1599  *
1600  * <h4>EXAMPLES</h4>
1601  * @example
1602  * new DERGeneralizedTime("20151231235959Z")
1603  * new DERGeneralizedTime("20151231235959.123Z")
1604  * new DERGeneralizedTime(new Date())
1605  * new DERGeneralizedTime(new Date(Date.UTC(2015,11,31,23,59,59,123)))
1606  * new DERGeneralizedTime({str: "20151231235959.123Z"})
1607  * new DERGeneralizedTime({date: new Date()})
1608  * new DERGeneralizedTime({date: new Date(), millis: true})
1609  * new DERGeneralizedTime({millis: true})
1610  */
1611 KJUR.asn1.DERGeneralizedTime = function(params) {
1612     KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params);
1613     this.hT = "18";
1614     this.params = params;
1615 
1616     this.getFreshValueHex = function() {
1617 	var params = this.params;
1618 
1619 	if (this.params == undefined) params = { date: new Date() };
1620 
1621 	if (typeof params == "string") {
1622 	    if (params.match(/^[0-9]{14}Z$/) ||
1623 		params.match(/^[0-9]{14}\.[0-9]+Z$/)) {
1624 		this.hV = stohex(params);
1625 	    } else {
1626 		throw new Error("malformed string for GeneralizedTime: " + params);
1627 	    }
1628 	} else if (params.str != undefined) {
1629 	    this.hV = stohex(params.str);
1630 	} else if (params.date == undefined && params.millis == true) {
1631 	    var date = new Date();
1632 	    this.hV = stohex(this.formatDate(date, 'gen', true));
1633 	} else if (params.date != undefined &&
1634 		   params.date instanceof Date) {
1635 	    var withMillis = (params.millis === true);
1636 	    this.hV = stohex(this.formatDate(params.date, 'gen', withMillis));
1637 	} else if (params instanceof Date) {
1638 	    this.hV = stohex(this.formatDate(params, 'gen'));
1639 	}
1640 
1641 	if (this.hV == undefined) {
1642 	    throw new Error("parameter not specified properly for GeneralizedTime");
1643 	}
1644 	return this.hV;
1645     };
1646 
1647     if (params != undefined) this.setByParam(params);
1648 };
1649 extendClass(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime);
1650 
1651 // ********************************************************************
1652 /**
1653  * class for ASN.1 DER Sequence
1654  * @name KJUR.asn1.DERSequence
1655  * @class class for ASN.1 DER Sequence
1656  * @extends KJUR.asn1.DERAbstractStructured
1657  * @description
1658  * <br/>
1659  * As for argument 'params' for constructor, you can specify one of
1660  * following properties:
1661  * <ul>
1662  * <li>array - specify array of ASN1Object to set elements of content</li>
1663  * </ul>
1664  * NOTE: 'params' can be omitted.
1665  */
1666 KJUR.asn1.DERSequence = function(params) {
1667     KJUR.asn1.DERSequence.superclass.constructor.call(this, params);
1668     this.hT = "30";
1669     this.getFreshValueHex = function() {
1670         var h = '';
1671         for (var i = 0; i < this.asn1Array.length; i++) {
1672             var asn1Obj = this.asn1Array[i];
1673             h += asn1Obj.tohex();
1674         }
1675         this.hV = h;
1676         return this.hV;
1677     };
1678 };
1679 extendClass(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured);
1680 
1681 // ********************************************************************
1682 /**
1683  * class for ASN.1 DER Set
1684  * @name KJUR.asn1.DERSet
1685  * @class class for ASN.1 DER Set
1686  * @extends KJUR.asn1.DERAbstractStructured
1687  * @description
1688  * <br/>
1689  * As for argument 'params' for constructor, you can specify one of
1690  * following properties:
1691  * <ul>
1692  * <li>array - specify array of ASN1Object to set elements of content</li>
1693  * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li>
1694  * </ul>
1695  * NOTE1: 'params' can be omitted.<br/>
1696  * NOTE2: sortflag is supported since 1.0.5.
1697  */
1698 KJUR.asn1.DERSet = function(params) {
1699     KJUR.asn1.DERSet.superclass.constructor.call(this, params);
1700     this.hT = "31";
1701     this.sortFlag = true; // item shall be sorted only in ASN.1 DER
1702     this.getFreshValueHex = function() {
1703         var a = new Array();
1704         for (var i = 0; i < this.asn1Array.length; i++) {
1705             var asn1Obj = this.asn1Array[i];
1706             a.push(asn1Obj.tohex());
1707         }
1708         if (this.sortFlag == true) a.sort();
1709         this.hV = a.join('');
1710         return this.hV;
1711     };
1712 
1713     if (typeof params != "undefined") {
1714         if (typeof params.sortflag != "undefined" &&
1715             params.sortflag == false)
1716             this.sortFlag = false;
1717     }
1718 };
1719 extendClass(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured);
1720 
1721 // ********************************************************************
1722 /**
1723  * class for ASN.1 DER TaggedObject
1724  * @name KJUR.asn1.DERTaggedObject
1725  * @class class for ASN.1 DER TaggedObject
1726  * @extends KJUR.asn1.ASN1Object
1727  * @see KJUR_asn1.ASN1Util.newObject
1728  *
1729  * @description
1730  * <br/>
1731  * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object.
1732  * For example, if you find '[1]' tag in a ASN.1 dump, 
1733  * 'tagNoHex' will be 'a1'.
1734  * <br/>
1735  * As for optional argument 'params' for constructor, you can specify *ANY* of
1736  * following properties:
1737  * <ul>
1738  * <li>tag - specify tag (default is 'a0' which means [0])</li>
1739  * <li>explicit - specify true if this is explicit tag otherwise false 
1740  *     (default is 'true').</li>
1741  * <li>obj - specify ASN1Object or JSON object which will be tagged</li>
1742  * <li>tage - specify tag with explicit</li>
1743  * <li>tagi - specify tag with implicit</li>
1744  * </ul>
1745  * As for the member "obj" value of JSON object, 
1746  * {@link KJUR_asn1.ASN1Util.newObject} is used to generate.
1747  *
1748  * @example
1749  * // by JSON
1750  * new KJUR.asn1.DERTaggedObject({
1751  *  tag:'a0', explicit: true, obj: { "prnstr": { "str": "aaa" } }
1752  * }).tohex()
1753  *
1754  * // by ASN1Object object
1755  * new KJUR.asn1.DERTaggedObject({
1756  *  tage:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // explicit
1757  * }) 
1758  * new KJUR.asn1.DERTaggedObject({
1759  *  tagi:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // implicit
1760  * }) 
1761  * new KJUR.asn1.DERTaggedObject({
1762  *  tag:'a0', explicit: true, obj: new KJUR.asn1.DERInteger({int: 3}) // explicit
1763  * }) 
1764  *
1765  * // to hexadecimal
1766  * d1 = new KJUR.asn1.DERUTF8String({str':'a'})
1767  * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1});
1768  * hex = d2.tohex();
1769  */
1770 KJUR.asn1.DERTaggedObject = function(params) {
1771     KJUR.asn1.DERTaggedObject.superclass.constructor.call(this);
1772 
1773     var _KJUR_asn1 = KJUR.asn1,
1774 	_ASN1HEX = ASN1HEX,
1775 	_getV = _ASN1HEX.getV,
1776 	_isASN1HEX = _ASN1HEX.isASN1HEX,
1777 	_newObject = _KJUR_asn1.ASN1Util.newObject;
1778 
1779     this.hT = "a0";
1780     this.hV = '';
1781     this.isExplicit = true;
1782     this.asn1Object = null;
1783     this.params = {tag: "a0", explicit: true}; //"tag": "a0, "explicit": true};
1784 
1785     /**
1786      * set value by an ASN1Object
1787      * @name setString
1788      * @memberOf KJUR.asn1.DERTaggedObject#
1789      * @function
1790      * @param {Boolean} isExplicitFlag flag for explicit/implicit tag
1791      * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag
1792      * @param {ASN1Object} asn1Object ASN.1 to encapsulate
1793      * @deprecated since jsrsasign 10.5.4 please use setByParam instead
1794      */
1795     this.setASN1Object = function(isExplicitFlag, tagNoHex, asn1Object) {
1796 	this.params = {tag: tagNoHex,
1797 		       explicit: isExplicitFlag,
1798 		       obj: asn1Object};
1799     };
1800 
1801     this.getFreshValueHex = function() {
1802 	var params = this.params;
1803 
1804 	if (params.explicit == undefined) params.explicit = true;
1805 
1806 	if (params.tage != undefined) {
1807 	    params.tag = params.tage;
1808 	    params.explicit = true;
1809 	}
1810 	if (params.tagi != undefined) {
1811 	    params.tag = params.tagi;
1812 	    params.explicit = false;
1813 	}
1814 
1815 	if (params.str != undefined) {
1816 	    this.hV = utf8tohex(params.str);
1817 	} else if (params.hex != undefined) {
1818 	    this.hV = params.hex;
1819 	} else if (params.obj != undefined) {
1820 	    var hV1;
1821 	    if (params.obj instanceof _KJUR_asn1.ASN1Object) {
1822 		hV1 = params.obj.tohex();
1823 	    } else if (typeof params.obj == "object") {
1824 		hV1 = _newObject(params.obj).tohex();
1825 	    }
1826 	    if (params.explicit) {
1827 		this.hV = hV1;
1828 	    } else {
1829 		this.hV = _getV(hV1, 0);
1830 	    }
1831 	} else {
1832 	    throw new Error("str, hex nor obj not specified");
1833 	}
1834 
1835 	if (params.tag == undefined) params.tag = "a0";
1836 	this.hT = params.tag;
1837         this.hTLV = null;
1838         this.isModified = true;
1839 
1840         return this.hV;
1841     };
1842 
1843     this.setByParam = function(params) {
1844 	this.params = params;
1845     };
1846 
1847     if (params !== undefined) this.setByParam(params);
1848 };
1849 extendClass(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object);
1850