1 /* asn1-1.0.21.js (c) 2013-2020 Kenji Urushima | kjur.github.io/jsrsasign/license
  2  */
  3 /*
  4  * asn1.js - ASN.1 DER encoder classes
  5  *
  6  * Copyright (c) 2013-2020 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.1.0 asn1 1.0.21 (2020-Nov-18)
 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                 var newParam = {};
301                 if (tagParam.explicit !== undefined)
302                     newParam.explicit = tagParam.explicit;
303                 if (tagParam.tag !== undefined)
304                     newParam.tag = tagParam.tag;
305                 if (tagParam.obj === undefined)
306                     throw "obj shall be specified for 'tag'.";
307                 newParam.obj = _newObject(tagParam.obj);
308                 return new _DERTaggedObject(newParam);
309  */
310             }
311         }
312     };
313 
314     /**
315      * get encoded hexadecimal string of ASN1Object specifed by JSON parameters
316      * @name jsonToASN1HEX
317      * @memberOf KJUR.asn1.ASN1Util
318      * @function
319      * @param {Array} param JSON parameter to generate ASN1Object
320      * @return hexadecimal string of ASN1Object
321      * @since asn1 1.0.4
322      * @description
323      * As for ASN.1 object representation of JSON object,
324      * please see {@link newObject}.
325      * @example
326      * jsonToASN1HEX({'prnstr': 'aaa'}); 
327      */
328     this.jsonToASN1HEX = function(param) {
329         var asn1Obj = this.newObject(param);
330         return asn1Obj.getEncodedHex();
331     };
332 };
333 
334 /**
335  * get dot noted oid number string from hexadecimal value of OID
336  * @name oidHexToInt
337  * @memberOf KJUR.asn1.ASN1Util
338  * @function
339  * @param {String} hex hexadecimal value of object identifier
340  * @return {String} dot noted string of object identifier
341  * @since jsrsasign 4.8.3 asn1 1.0.7
342  * @description
343  * This static method converts from hexadecimal string representation of 
344  * ASN.1 value of object identifier to oid number string.
345  * @example
346  * KJUR.asn1.ASN1Util.oidHexToInt('550406') → "2.5.4.6"
347  */
348 KJUR.asn1.ASN1Util.oidHexToInt = function(hex) {
349     var s = "";
350     var i01 = parseInt(hex.substr(0, 2), 16);
351     var i0 = Math.floor(i01 / 40);
352     var i1 = i01 % 40;
353     var s = i0 + "." + i1;
354 
355     var binbuf = "";
356     for (var i = 2; i < hex.length; i += 2) {
357 	var value = parseInt(hex.substr(i, 2), 16);
358         var bin = ("00000000" + value.toString(2)).slice(- 8);
359 	binbuf = binbuf + bin.substr(1, 7);
360 	if (bin.substr(0, 1) == "0") {
361 	    var bi = new BigInteger(binbuf, 2);
362 	    s = s + "." + bi.toString(10);
363 	    binbuf = "";
364 	}
365     };
366 
367     return s;
368 };
369 
370 /**
371  * get hexadecimal value of object identifier from dot noted oid value (DEPRECATED)
372  * @name oidIntToHex
373  * @memberOf KJUR.asn1.ASN1Util
374  * @function
375  * @param {String} oidString dot noted string of object identifier
376  * @return {String} hexadecimal value of object identifier
377  * @since jsrsasign 4.8.3 asn1 1.0.7
378  * @see {@link ASN1HEX.hextooidstr}
379  * @deprecated from jsrsasign 10.0.6. please use {@link oidtohex}
380  *
381  * @description
382  * This static method converts from object identifier value string.
383  * to hexadecimal string representation of it.
384  * {@link ASN1HEX.hextooidstr} is a reverse function of this.
385  * @example
386  * KJUR.asn1.ASN1Util.oidIntToHex("2.5.4.6") → "550406"
387  */
388 KJUR.asn1.ASN1Util.oidIntToHex = function(oidString) {
389     var itox = function(i) {
390         var h = i.toString(16);
391         if (h.length == 1) h = '0' + h;
392         return h;
393     };
394 
395     var roidtox = function(roid) {
396         var h = '';
397         var bi = new BigInteger(roid, 10);
398         var b = bi.toString(2);
399         var padLen = 7 - b.length % 7;
400         if (padLen == 7) padLen = 0;
401         var bPad = '';
402         for (var i = 0; i < padLen; i++) bPad += '0';
403         b = bPad + b;
404         for (var i = 0; i < b.length - 1; i += 7) {
405             var b8 = b.substr(i, 7);
406             if (i != b.length - 7) b8 = '1' + b8;
407             h += itox(parseInt(b8, 2));
408         }
409         return h;
410     };
411     
412     if (! oidString.match(/^[0-9.]+$/)) {
413         throw "malformed oid string: " + oidString;
414     }
415     var h = '';
416     var a = oidString.split('.');
417     var i0 = parseInt(a[0]) * 40 + parseInt(a[1]);
418     h += itox(i0);
419     a.splice(0, 2);
420     for (var i = 0; i < a.length; i++) {
421         h += roidtox(a[i]);
422     }
423     return h;
424 };
425 
426 
427 // ********************************************************************
428 //  Abstract ASN.1 Classes
429 // ********************************************************************
430 
431 // ********************************************************************
432 
433 /**
434  * base class for ASN.1 DER encoder object<br/>
435  * @name KJUR.asn1.ASN1Object
436  * @class base class for ASN.1 DER encoder object
437  * @param {Array} params JSON object parameter for constructor
438  * @property {Boolean} isModified flag whether internal data was changed
439  * @property {Array} params JSON object parameter for ASN.1 encode
440  * @property {String} hTLV hexadecimal string of ASN.1 TLV
441  * @property {String} hT hexadecimal string of ASN.1 TLV tag(T)
442  * @property {String} hL hexadecimal string of ASN.1 TLV length(L)
443  * @property {String} hV hexadecimal string of ASN.1 TLV value(V)
444  *
445  * @description
446  * This class is ASN.1 DER object encode base class.
447  * 
448  * @example
449  * new KJUR.asn1.ASN1Object({tlv: "030101"})
450  */
451 KJUR.asn1.ASN1Object = function(params) {
452     var isModified = true;
453     var hTLV = null;
454     var hT = '00';
455     var hL = '00';
456     var hV = '';
457     this.params = null;
458 
459     /**
460      * get hexadecimal ASN.1 TLV length(L) bytes from TLV value(V)<br/>
461      * @name getLengthHexFromValue
462      * @memberOf KJUR.asn1.ASN1Object#
463      * @function
464      * @return {String} hexadecimal string of ASN.1 TLV length(L)
465      */
466     this.getLengthHexFromValue = function() {
467         if (typeof this.hV == "undefined" || this.hV == null) {
468             throw new Error("this.hV is null or undefined");
469         }
470         if (this.hV.length % 2 == 1) {
471             throw new Error("value hex must be even length: n=" +
472 			    hV.length + ",v=" + this.hV);
473         }
474         var n = this.hV.length / 2;
475         var hN = n.toString(16);
476         if (hN.length % 2 == 1) {
477             hN = "0" + hN;
478         }
479         if (n < 128) {
480             return hN;
481         } else {
482             var hNlen = hN.length / 2;
483             if (hNlen > 15) {
484                 throw "ASN.1 length too long to represent by 8x: n = " + n.toString(16);
485             }
486             var head = 128 + hNlen;
487             return head.toString(16) + hN;
488         }
489     };
490 
491     /**
492      * get hexadecimal string of ASN.1 TLV bytes
493      * @name getEncodedHex
494      * @memberOf KJUR.asn1.ASN1Object#
495      * @function
496      * @return {String} hexadecimal string of ASN.1 TLV
497      */
498     this.getEncodedHex = function() {
499         if (this.hTLV == null || this.isModified) {
500             this.hV = this.getFreshValueHex();
501             this.hL = this.getLengthHexFromValue();
502             this.hTLV = this.hT + this.hL + this.hV;
503             this.isModified = false;
504             //alert("first time: " + this.hTLV);
505         }
506         return this.hTLV;
507     };
508 
509     /**
510      * get hexadecimal string of ASN.1 TLV value(V) bytes
511      * @name getValueHex
512      * @memberOf KJUR.asn1.ASN1Object#
513      * @function
514      * @return {String} hexadecimal string of ASN.1 TLV value(V) bytes
515      */
516     this.getValueHex = function() {
517         this.getEncodedHex();
518         return this.hV;
519     }
520 
521     this.getFreshValueHex = function() {
522         return '';
523     };
524 
525     this.setByParam = function(params) {
526 	this.params = params;
527     };
528 
529     if (params != undefined) {
530 	if (params.tlv != undefined) {
531 	    this.hTLV = params.tlv;
532 	    this.isModified = false;
533 	}
534     }
535 };
536 
537 // == BEGIN DERAbstractString ================================================
538 /**
539  * base class for ASN.1 DER string classes
540  * @name KJUR.asn1.DERAbstractString
541  * @class base class for ASN.1 DER string classes
542  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
543  * @property {String} s internal string of value
544  * @extends KJUR.asn1.ASN1Object
545  * @description
546  * <br/>
547  * As for argument 'params' for constructor, you can specify one of
548  * following properties:
549  * <ul>
550  * <li>str - specify initial ASN.1 value(V) by a string</li>
551  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
552  * </ul>
553  * NOTE: 'params' can be omitted.
554  */
555 KJUR.asn1.DERAbstractString = function(params) {
556     KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
557     var s = null;
558     var hV = null;
559 
560     /**
561      * get string value of this string object
562      * @name getString
563      * @memberOf KJUR.asn1.DERAbstractString#
564      * @function
565      * @return {String} string value of this string object
566      */
567     this.getString = function() {
568         return this.s;
569     };
570 
571     /**
572      * set value by a string
573      * @name setString
574      * @memberOf KJUR.asn1.DERAbstractString#
575      * @function
576      * @param {String} newS value by a string to set
577      * @description
578      * This method set value by string. <br/>
579      * NOTE: This method assumes that the argument string is
580      * UTF-8 encoded even though ASN.1 primitive 
581      * such as IA5String or PrintableString doesn't
582      * support all of UTF-8 characters.
583      * @example
584      * o = new KJUR.asn1.DERIA5String();
585      * o.setString("abc");
586      * o.setString("あいう");
587      */
588     this.setString = function(newS) {
589         this.hTLV = null;
590         this.isModified = true;
591         this.s = newS;
592         this.hV = utf8tohex(this.s).toLowerCase();
593     };
594 
595     /**
596      * set value by a hexadecimal string
597      * @name setStringHex
598      * @memberOf KJUR.asn1.DERAbstractString#
599      * @function
600      * @param {String} newHexString value by a hexadecimal string to set
601      */
602     this.setStringHex = function(newHexString) {
603         this.hTLV = null;
604         this.isModified = true;
605         this.s = null;
606         this.hV = newHexString;
607     };
608 
609     this.getFreshValueHex = function() {
610         return this.hV;
611     };
612 
613     if (typeof params != "undefined") {
614         if (typeof params == "string") {
615             this.setString(params);
616         } else if (typeof params['str'] != "undefined") {
617             this.setString(params['str']);
618         } else if (typeof params['hex'] != "undefined") {
619             this.setStringHex(params['hex']);
620         }
621     }
622 };
623 extendClass(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object);
624 // == END   DERAbstractString ================================================
625 
626 // == BEGIN DERAbstractTime ==================================================
627 /**
628  * base class for ASN.1 DER Generalized/UTCTime class
629  * @name KJUR.asn1.DERAbstractTime
630  * @class base class for ASN.1 DER Generalized/UTCTime class
631  * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'})
632  * @extends KJUR.asn1.ASN1Object
633  * @description
634  * @see KJUR.asn1.ASN1Object - superclass
635  */
636 KJUR.asn1.DERAbstractTime = function(params) {
637     KJUR.asn1.DERAbstractTime.superclass.constructor.call(this);
638     var s = null;
639     var date = null;
640 
641     // --- PRIVATE METHODS --------------------
642     this.localDateToUTC = function(d) {
643         var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
644         var utcDate = new Date(utc);
645         return utcDate;
646     };
647 
648     /*
649      * format date string by Data object
650      * @name formatDate
651      * @memberOf KJUR.asn1.AbstractTime;
652      * @param {Date} dateObject 
653      * @param {string} type 'utc' or 'gen'
654      * @param {boolean} withMillis flag for with millisections or not
655      * @description
656      * 'withMillis' flag is supported from asn1 1.0.6.
657      */
658     this.formatDate = function(dateObject, type, withMillis) {
659         var pad = this.zeroPadding;
660         var d = this.localDateToUTC(dateObject);
661         var year = String(d.getFullYear());
662         if (type == 'utc') year = year.substr(2, 2);
663         var month = pad(String(d.getMonth() + 1), 2);
664         var day = pad(String(d.getDate()), 2);
665         var hour = pad(String(d.getHours()), 2);
666         var min = pad(String(d.getMinutes()), 2);
667         var sec = pad(String(d.getSeconds()), 2);
668         var s = year + month + day + hour + min + sec;
669         if (withMillis === true) {
670             var millis = d.getMilliseconds();
671             if (millis != 0) {
672                 var sMillis = pad(String(millis), 3);
673                 sMillis = sMillis.replace(/[0]+$/, "");
674                 s = s + "." + sMillis;
675             }
676         }
677         return s + "Z";
678     };
679 
680     this.zeroPadding = function(s, len) {
681         if (s.length >= len) return s;
682         return new Array(len - s.length + 1).join('0') + s;
683     };
684 
685     // --- PUBLIC METHODS --------------------
686     /**
687      * get string value of this string object
688      * @name getString
689      * @memberOf KJUR.asn1.DERAbstractTime#
690      * @function
691      * @return {String} string value of this time object
692      */
693     this.getString = function() {
694         return this.s;
695     };
696 
697     /**
698      * set value by a string
699      * @name setString
700      * @memberOf KJUR.asn1.DERAbstractTime#
701      * @function
702      * @param {String} newS value by a string to set such like "130430235959Z"
703      */
704     this.setString = function(newS) {
705         this.hTLV = null;
706         this.isModified = true;
707         this.s = newS;
708         this.hV = stohex(newS);
709     };
710 
711     /**
712      * set value by a Date object
713      * @name setByDateValue
714      * @memberOf KJUR.asn1.DERAbstractTime#
715      * @function
716      * @param {Integer} year year of date (ex. 2013)
717      * @param {Integer} month month of date between 1 and 12 (ex. 12)
718      * @param {Integer} day day of month
719      * @param {Integer} hour hours of date
720      * @param {Integer} min minutes of date
721      * @param {Integer} sec seconds of date
722      */
723     this.setByDateValue = function(year, month, day, hour, min, sec) {
724         var dateObject = new Date(Date.UTC(year, month - 1, day, hour, min, sec, 0));
725         this.setByDate(dateObject);
726     };
727 
728     this.getFreshValueHex = function() {
729         return this.hV;
730     };
731 };
732 extendClass(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object);
733 // == END   DERAbstractTime ==================================================
734 
735 // == BEGIN DERAbstractStructured ============================================
736 /**
737  * base class for ASN.1 DER structured class
738  * @name KJUR.asn1.DERAbstractStructured
739  * @class base class for ASN.1 DER structured class
740  * @property {Array} asn1Array internal array of ASN1Object
741  * @extends KJUR.asn1.ASN1Object
742  * @description
743  * @see KJUR.asn1.ASN1Object - superclass
744  */
745 KJUR.asn1.DERAbstractStructured = function(params) {
746     KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
747     var asn1Array = null;
748 
749     /**
750      * set value by array of ASN1Object
751      * @name setByASN1ObjectArray
752      * @memberOf KJUR.asn1.DERAbstractStructured#
753      * @function
754      * @param {array} asn1ObjectArray array of ASN1Object to set
755      */
756     this.setByASN1ObjectArray = function(asn1ObjectArray) {
757         this.hTLV = null;
758         this.isModified = true;
759         this.asn1Array = asn1ObjectArray;
760     };
761 
762     /**
763      * append an ASN1Object to internal array
764      * @name appendASN1Object
765      * @memberOf KJUR.asn1.DERAbstractStructured#
766      * @function
767      * @param {ASN1Object} asn1Object to add
768      */
769     this.appendASN1Object = function(asn1Object) {
770         this.hTLV = null;
771         this.isModified = true;
772         this.asn1Array.push(asn1Object);
773     };
774 
775     this.asn1Array = new Array();
776     if (typeof params != "undefined") {
777         if (typeof params['array'] != "undefined") {
778             this.asn1Array = params['array'];
779         }
780     }
781 };
782 extendClass(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object);
783 
784 
785 // ********************************************************************
786 //  ASN.1 Object Classes
787 // ********************************************************************
788 
789 // ********************************************************************
790 /**
791  * class for ASN.1 DER Boolean
792  * @name KJUR.asn1.DERBoolean
793  * @class class for ASN.1 DER Boolean
794  * @extends KJUR.asn1.ASN1Object
795  * @see KJUR.asn1.ASN1Object - superclass
796  * @description
797  * In ASN.1 DER, DER Boolean "false" shall be omitted.
798  * However this supports boolean false for future BER support.
799  * @example
800  * new KJUR.asn1.DERBoolean(true)
801  * new KJUR.asn1.DERBoolean(false)
802  */
803 KJUR.asn1.DERBoolean = function(params) {
804     KJUR.asn1.DERBoolean.superclass.constructor.call(this);
805     this.hT = "01";
806     if (params == false)
807 	this.hTLV = "010100";
808     else 
809 	this.hTLV = "0101ff";
810 };
811 extendClass(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object);
812 
813 // ********************************************************************
814 /**
815  * class for ASN.1 DER Integer
816  * @name KJUR.asn1.DERInteger
817  * @class class for ASN.1 DER Integer
818  * @extends KJUR.asn1.ASN1Object
819  * @description
820  * <br/>
821  * As for argument 'params' for constructor, you can specify one of
822  * following properties:
823  * <ul>
824  * <li>int - specify initial ASN.1 value(V) by integer value</li>
825  * <li>bigint - specify initial ASN.1 value(V) by BigInteger object</li>
826  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
827  * </ul>
828  * NOTE: 'params' can be omitted.
829  */
830 KJUR.asn1.DERInteger = function(params) {
831     KJUR.asn1.DERInteger.superclass.constructor.call(this);
832     this.hT = "02";
833 
834     /**
835      * set value by Tom Wu's BigInteger object
836      * @name setByBigInteger
837      * @memberOf KJUR.asn1.DERInteger#
838      * @function
839      * @param {BigInteger} bigIntegerValue to set
840      */
841     this.setByBigInteger = function(bigIntegerValue) {
842         this.hTLV = null;
843         this.isModified = true;
844         this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
845     };
846 
847     /**
848      * set value by integer value
849      * @name setByInteger
850      * @memberOf KJUR.asn1.DERInteger
851      * @function
852      * @param {Integer} integer value to set
853      */
854     this.setByInteger = function(intValue) {
855         var bi = new BigInteger(String(intValue), 10);
856         this.setByBigInteger(bi);
857     };
858 
859     /**
860      * set value by integer value
861      * @name setValueHex
862      * @memberOf KJUR.asn1.DERInteger#
863      * @function
864      * @param {String} hexadecimal string of integer value
865      * @description
866      * <br/>
867      * NOTE: Value shall be represented by minimum octet length of
868      * two's complement representation.
869      * @example
870      * new KJUR.asn1.DERInteger(123);
871      * new KJUR.asn1.DERInteger({'int': 123});
872      * new KJUR.asn1.DERInteger({'hex': '1fad'});
873      */
874     this.setValueHex = function(newHexString) {
875         this.hV = newHexString;
876     };
877 
878     this.getFreshValueHex = function() {
879         return this.hV;
880     };
881 
882     if (typeof params != "undefined") {
883         if (typeof params['bigint'] != "undefined") {
884             this.setByBigInteger(params['bigint']);
885         } else if (typeof params['int'] != "undefined") {
886             this.setByInteger(params['int']);
887         } else if (typeof params == "number") {
888             this.setByInteger(params);
889         } else if (typeof params['hex'] != "undefined") {
890             this.setValueHex(params['hex']);
891         }
892     }
893 };
894 extendClass(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object);
895 
896 // ********************************************************************
897 /**
898  * class for ASN.1 DER encoded BitString primitive
899  * @name KJUR.asn1.DERBitString
900  * @class class for ASN.1 DER encoded BitString primitive
901  * @extends KJUR.asn1.ASN1Object
902  * @description 
903  * <br/>
904  * As for argument 'params' for constructor, you can specify one of
905  * following properties:
906  * <ul>
907  * <li>bin - specify binary string (ex. '10111')</li>
908  * <li>array - specify array of boolean (ex. [true,false,true,true])</li>
909  * <li>hex - specify hexadecimal string of ASN.1 value(V) including unused bits</li>
910  * <li>obj - specify {@link KJUR.asn1.ASN1Util.newObject} 
911  * argument for "BitString encapsulates" structure.</li>
912  * </ul>
913  * NOTE1: 'params' can be omitted.<br/>
914  * NOTE2: 'obj' parameter have been supported since
915  * asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).<br/>
916  * @example
917  * // default constructor
918  * o = new KJUR.asn1.DERBitString();
919  * // initialize with binary string
920  * o = new KJUR.asn1.DERBitString({bin: "1011"});
921  * // initialize with boolean array
922  * o = new KJUR.asn1.DERBitString({array: [true,false,true,true]});
923  * // initialize with hexadecimal string (04 is unused bits)
924  * o = new KJUR.asn1.DEROctetString({hex: "04bac0"});
925  * // initialize with ASN1Util.newObject argument for encapsulated
926  * o = new KJUR.asn1.DERBitString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}});
927  * // above generates a ASN.1 data like this:
928  * // BIT STRING, encapsulates {
929  * //   SEQUENCE {
930  * //     INTEGER 3
931  * //     PrintableString 'aaa'
932  * //     }
933  * //   } 
934  */
935 KJUR.asn1.DERBitString = function(params) {
936     if (params !== undefined && typeof params.obj !== "undefined") {
937 	var o = KJUR.asn1.ASN1Util.newObject(params.obj);
938 	params.hex = "00" + o.getEncodedHex();
939     }
940     KJUR.asn1.DERBitString.superclass.constructor.call(this);
941     this.hT = "03";
942 
943     /**
944      * set ASN.1 value(V) by a hexadecimal string including unused bits
945      * @name setHexValueIncludingUnusedBits
946      * @memberOf KJUR.asn1.DERBitString#
947      * @function
948      * @param {String} newHexStringIncludingUnusedBits
949      */
950     this.setHexValueIncludingUnusedBits = function(newHexStringIncludingUnusedBits) {
951         this.hTLV = null;
952         this.isModified = true;
953         this.hV = newHexStringIncludingUnusedBits;
954     };
955 
956     /**
957      * set ASN.1 value(V) by unused bit and hexadecimal string of value
958      * @name setUnusedBitsAndHexValue
959      * @memberOf KJUR.asn1.DERBitString#
960      * @function
961      * @param {Integer} unusedBits
962      * @param {String} hValue
963      */
964     this.setUnusedBitsAndHexValue = function(unusedBits, hValue) {
965         if (unusedBits < 0 || 7 < unusedBits) {
966             throw "unused bits shall be from 0 to 7: u = " + unusedBits;
967         }
968         var hUnusedBits = "0" + unusedBits;
969         this.hTLV = null;
970         this.isModified = true;
971         this.hV = hUnusedBits + hValue;
972     };
973 
974     /**
975      * set ASN.1 DER BitString by binary string<br/>
976      * @name setByBinaryString
977      * @memberOf KJUR.asn1.DERBitString#
978      * @function
979      * @param {String} binaryString binary value string (i.e. '10111')
980      * @description
981      * Its unused bits will be calculated automatically by length of 
982      * 'binaryValue'. <br/>
983      * NOTE: Trailing zeros '0' will be ignored.
984      * @example
985      * o = new KJUR.asn1.DERBitString();
986      * o.setByBooleanArray("01011");
987      */
988     this.setByBinaryString = function(binaryString) {
989         binaryString = binaryString.replace(/0+$/, '');
990         var unusedBits = 8 - binaryString.length % 8;
991         if (unusedBits == 8) unusedBits = 0;
992         for (var i = 0; i <= unusedBits; i++) {
993             binaryString += '0';
994         }
995         var h = '';
996         for (var i = 0; i < binaryString.length - 1; i += 8) {
997             var b = binaryString.substr(i, 8);
998             var x = parseInt(b, 2).toString(16);
999             if (x.length == 1) x = '0' + x;
1000             h += x;  
1001         }
1002         this.hTLV = null;
1003         this.isModified = true;
1004         this.hV = '0' + unusedBits + h;
1005     };
1006 
1007     /**
1008      * set ASN.1 TLV value(V) by an array of boolean<br/>
1009      * @name setByBooleanArray
1010      * @memberOf KJUR.asn1.DERBitString#
1011      * @function
1012      * @param {array} booleanArray array of boolean (ex. [true, false, true])
1013      * @description
1014      * NOTE: Trailing falses will be ignored in the ASN.1 DER Object.
1015      * @example
1016      * o = new KJUR.asn1.DERBitString();
1017      * o.setByBooleanArray([false, true, false, true, true]);
1018      */
1019     this.setByBooleanArray = function(booleanArray) {
1020         var s = '';
1021         for (var i = 0; i < booleanArray.length; i++) {
1022             if (booleanArray[i] == true) {
1023                 s += '1';
1024             } else {
1025                 s += '0';
1026             }
1027         }
1028         this.setByBinaryString(s);
1029     };
1030 
1031     /**
1032      * generate an array of falses with specified length<br/>
1033      * @name newFalseArray
1034      * @memberOf KJUR.asn1.DERBitString
1035      * @function
1036      * @param {Integer} nLength length of array to generate
1037      * @return {array} array of boolean falses
1038      * @description
1039      * This static method may be useful to initialize boolean array.
1040      * @example
1041      * o = new KJUR.asn1.DERBitString();
1042      * o.newFalseArray(3) → [false, false, false]
1043      */
1044     this.newFalseArray = function(nLength) {
1045         var a = new Array(nLength);
1046         for (var i = 0; i < nLength; i++) {
1047             a[i] = false;
1048         }
1049         return a;
1050     };
1051 
1052     this.getFreshValueHex = function() {
1053         return this.hV;
1054     };
1055 
1056     if (typeof params != "undefined") {
1057         if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) {
1058             this.setHexValueIncludingUnusedBits(params);
1059         } else if (typeof params['hex'] != "undefined") {
1060             this.setHexValueIncludingUnusedBits(params['hex']);
1061         } else if (typeof params['bin'] != "undefined") {
1062             this.setByBinaryString(params['bin']);
1063         } else if (typeof params['array'] != "undefined") {
1064             this.setByBooleanArray(params['array']);
1065         }
1066     }
1067 };
1068 extendClass(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object);
1069 
1070 // ********************************************************************
1071 /**
1072  * class for ASN.1 DER OctetString<br/>
1073  * @name KJUR.asn1.DEROctetString
1074  * @class class for ASN.1 DER OctetString
1075  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1076  * @extends KJUR.asn1.DERAbstractString
1077  * @description
1078  * This class provides ASN.1 OctetString simple type.<br/>
1079  * Supported "params" attributes are:
1080  * <ul>
1081  * <li>str - to set a string as a value</li>
1082  * <li>hex - to set a hexadecimal string as a value</li>
1083  * <li>obj - to set a encapsulated ASN.1 value by JSON object 
1084  * which is defined in {@link KJUR.asn1.ASN1Util.newObject}</li>
1085  * </ul>
1086  * NOTE: A parameter 'obj' have been supported 
1087  * for "OCTET STRING, encapsulates" structure.
1088  * since asn1 1.0.11, jsrsasign 6.1.1 (2016-Sep-25).
1089  * @see KJUR.asn1.DERAbstractString - superclass
1090  * @example
1091  * // default constructor
1092  * o = new KJUR.asn1.DEROctetString();
1093  * // initialize with string
1094  * o = new KJUR.asn1.DEROctetString({str: "aaa"});
1095  * // initialize with hexadecimal string
1096  * o = new KJUR.asn1.DEROctetString({hex: "616161"});
1097  * // initialize with ASN1Util.newObject argument 
1098  * o = new KJUR.asn1.DEROctetString({obj: {seq: [{int: 3}, {prnstr: 'aaa'}]}});
1099  * // above generates a ASN.1 data like this:
1100  * // OCTET STRING, encapsulates {
1101  * //   SEQUENCE {
1102  * //     INTEGER 3
1103  * //     PrintableString 'aaa'
1104  * //     }
1105  * //   } 
1106  */
1107 KJUR.asn1.DEROctetString = function(params) {
1108     if (params !== undefined && typeof params.obj !== "undefined") {
1109 	var o = KJUR.asn1.ASN1Util.newObject(params.obj);
1110 	params.hex = o.getEncodedHex();
1111     }
1112     KJUR.asn1.DEROctetString.superclass.constructor.call(this, params);
1113     this.hT = "04";
1114 };
1115 extendClass(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString);
1116 
1117 // ********************************************************************
1118 /**
1119  * class for ASN.1 DER Null
1120  * @name KJUR.asn1.DERNull
1121  * @class class for ASN.1 DER Null
1122  * @extends KJUR.asn1.ASN1Object
1123  * @description
1124  * @see KJUR.asn1.ASN1Object - superclass
1125  */
1126 KJUR.asn1.DERNull = function() {
1127     KJUR.asn1.DERNull.superclass.constructor.call(this);
1128     this.hT = "05";
1129     this.hTLV = "0500";
1130 };
1131 extendClass(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object);
1132 
1133 // ********************************************************************
1134 /**
1135  * class for ASN.1 DER ObjectIdentifier
1136  * @name KJUR.asn1.DERObjectIdentifier
1137  * @class class for ASN.1 DER ObjectIdentifier
1138  * @param {Object} JSON object or string of parameters (ex. {'oid': '2.5.4.5'})
1139  * @extends KJUR.asn1.ASN1Object
1140  * @see oidtohex
1141  * 
1142  * @description
1143  * <br/>
1144  * As for argument 'params' for constructor, you can specify one of
1145  * following properties:
1146  * <ul>
1147  * <li>oid - specify initial ASN.1 value(V) by a oid string (ex. 2.5.4.13)</li>
1148  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
1149  * </ul>
1150  * NOTE: 'params' can be omitted.
1151  * @example
1152  * new DERObjectIdentifier({"name": "sha1"})
1153  * new DERObjectIdentifier({"oid": "1.2.3.4"})
1154  * new DERObjectIdentifier({"hex": "2d..."})
1155  * new DERObjectIdentifier("1.2.3.4")
1156  * new DERObjectIdentifier("SHA1withRSA")
1157  */
1158 KJUR.asn1.DERObjectIdentifier = function(params) {
1159     KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this);
1160     this.hT = "06";
1161 
1162     /**
1163      * set value by a hexadecimal string
1164      * @name setValueHex
1165      * @memberOf KJUR.asn1.DERObjectIdentifier#
1166      * @function
1167      * @param {String} newHexString hexadecimal value of OID bytes
1168      */
1169     this.setValueHex = function(newHexString) {
1170         this.hTLV = null;
1171         this.isModified = true;
1172         this.s = null;
1173         this.hV = newHexString;
1174     };
1175 
1176     /**
1177      * set value by a OID string<br/>
1178      * @name setValueOidString
1179      * @memberOf KJUR.asn1.DERObjectIdentifier#
1180      * @function
1181      * @param {String} oidString OID string (ex. 2.5.4.13)
1182      * @example
1183      * o = new KJUR.asn1.DERObjectIdentifier();
1184      * o.setValueOidString("2.5.4.13");
1185      */
1186     this.setValueOidString = function(oidString) {
1187 	var h = oidtohex(oidString);
1188 	if (h == null)
1189             throw new Error("malformed oid string: " + oidString);
1190         this.hTLV = null;
1191         this.isModified = true;
1192         this.s = null;
1193         this.hV = h;
1194     };
1195 
1196     /**
1197      * set value by a OID name
1198      * @name setValueName
1199      * @memberOf KJUR.asn1.DERObjectIdentifier#
1200      * @function
1201      * @param {String} oidName OID name (ex. 'serverAuth')
1202      * @since 1.0.1
1203      * @description
1204      * OID name shall be defined in 'KJUR.asn1.x509.OID.name2oidList'.
1205      * Otherwise raise error.
1206      * @example
1207      * o = new KJUR.asn1.DERObjectIdentifier();
1208      * o.setValueName("serverAuth");
1209      */
1210     this.setValueName = function(oidName) {
1211 	var oid = KJUR.asn1.x509.OID.name2oid(oidName);
1212 	if (oid !== '') {
1213             this.setValueOidString(oid);
1214         } else {
1215             throw new Error("DERObjectIdentifier oidName undefined: " + oidName);
1216         }
1217     };
1218 
1219     this.setValueNameOrOid = function(nameOrOid) {
1220 	if (nameOrOid.match(/^[0-2].[0-9.]+$/)) {
1221 	    this.setValueOidString(nameOrOid);
1222 	} else {
1223 	    this.setValueName(nameOrOid);
1224 	}
1225     }
1226 
1227     this.getFreshValueHex = function() {
1228         return this.hV;
1229     };
1230 
1231     this.setByParam = function(params) {
1232         if (typeof params === "string") {
1233 	    this.setValueNameOrOid(params);
1234         } else if (params.oid !== undefined) {
1235 	    this.setValueNameOrOid(params.oid);
1236         } else if (params.name !== undefined) {
1237             this.setValueNameOrOid(params.name);
1238         } else if (params.hex !== undefined) {
1239             this.setValueHex(params.hex);
1240         }
1241     };
1242 
1243     if (params !== undefined) this.setByParam(params);
1244 };
1245 extendClass(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object);
1246 
1247 // ********************************************************************
1248 /**
1249  * class for ASN.1 DER Enumerated
1250  * @name KJUR.asn1.DEREnumerated
1251  * @class class for ASN.1 DER Enumerated
1252  * @extends KJUR.asn1.ASN1Object
1253  * @description
1254  * <br/>
1255  * As for argument 'params' for constructor, you can specify one of
1256  * following properties:
1257  * <ul>
1258  * <li>int - specify initial ASN.1 value(V) by integer value</li>
1259  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
1260  * </ul>
1261  * NOTE: 'params' can be omitted.
1262  * @example
1263  * new KJUR.asn1.DEREnumerated(123);
1264  * new KJUR.asn1.DEREnumerated({int: 123});
1265  * new KJUR.asn1.DEREnumerated({hex: '1fad'});
1266  */
1267 KJUR.asn1.DEREnumerated = function(params) {
1268     KJUR.asn1.DEREnumerated.superclass.constructor.call(this);
1269     this.hT = "0a";
1270 
1271     /**
1272      * set value by Tom Wu's BigInteger object
1273      * @name setByBigInteger
1274      * @memberOf KJUR.asn1.DEREnumerated#
1275      * @function
1276      * @param {BigInteger} bigIntegerValue to set
1277      */
1278     this.setByBigInteger = function(bigIntegerValue) {
1279         this.hTLV = null;
1280         this.isModified = true;
1281         this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
1282     };
1283 
1284     /**
1285      * set value by integer value
1286      * @name setByInteger
1287      * @memberOf KJUR.asn1.DEREnumerated#
1288      * @function
1289      * @param {Integer} integer value to set
1290      */
1291     this.setByInteger = function(intValue) {
1292         var bi = new BigInteger(String(intValue), 10);
1293         this.setByBigInteger(bi);
1294     };
1295 
1296     /**
1297      * set value by integer value
1298      * @name setValueHex
1299      * @memberOf KJUR.asn1.DEREnumerated#
1300      * @function
1301      * @param {String} hexadecimal string of integer value
1302      * @description
1303      * <br/>
1304      * NOTE: Value shall be represented by minimum octet length of
1305      * two's complement representation.
1306      */
1307     this.setValueHex = function(newHexString) {
1308         this.hV = newHexString;
1309     };
1310 
1311     this.getFreshValueHex = function() {
1312         return this.hV;
1313     };
1314 
1315     if (typeof params != "undefined") {
1316         if (typeof params['int'] != "undefined") {
1317             this.setByInteger(params['int']);
1318         } else if (typeof params == "number") {
1319             this.setByInteger(params);
1320         } else if (typeof params['hex'] != "undefined") {
1321             this.setValueHex(params['hex']);
1322         }
1323     }
1324 };
1325 extendClass(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object);
1326 
1327 // ********************************************************************
1328 /**
1329  * class for ASN.1 DER UTF8String
1330  * @name KJUR.asn1.DERUTF8String
1331  * @class class for ASN.1 DER UTF8String
1332  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1333  * @extends KJUR.asn1.DERAbstractString
1334  * @description
1335  * @see KJUR.asn1.DERAbstractString - superclass
1336  */
1337 KJUR.asn1.DERUTF8String = function(params) {
1338     KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params);
1339     this.hT = "0c";
1340 };
1341 extendClass(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString);
1342 
1343 // ********************************************************************
1344 /**
1345  * class for ASN.1 DER NumericString
1346  * @name KJUR.asn1.DERNumericString
1347  * @class class for ASN.1 DER NumericString
1348  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1349  * @extends KJUR.asn1.DERAbstractString
1350  * @description
1351  * @see KJUR.asn1.DERAbstractString - superclass
1352  */
1353 KJUR.asn1.DERNumericString = function(params) {
1354     KJUR.asn1.DERNumericString.superclass.constructor.call(this, params);
1355     this.hT = "12";
1356 };
1357 extendClass(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString);
1358 
1359 // ********************************************************************
1360 /**
1361  * class for ASN.1 DER PrintableString
1362  * @name KJUR.asn1.DERPrintableString
1363  * @class class for ASN.1 DER PrintableString
1364  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1365  * @extends KJUR.asn1.DERAbstractString
1366  * @description
1367  * @see KJUR.asn1.DERAbstractString - superclass
1368  */
1369 KJUR.asn1.DERPrintableString = function(params) {
1370     KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params);
1371     this.hT = "13";
1372 };
1373 extendClass(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString);
1374 
1375 // ********************************************************************
1376 /**
1377  * class for ASN.1 DER TeletexString
1378  * @name KJUR.asn1.DERTeletexString
1379  * @class class for ASN.1 DER TeletexString
1380  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1381  * @extends KJUR.asn1.DERAbstractString
1382  * @description
1383  * @see KJUR.asn1.DERAbstractString - superclass
1384  */
1385 KJUR.asn1.DERTeletexString = function(params) {
1386     KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params);
1387     this.hT = "14";
1388 };
1389 extendClass(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString);
1390 
1391 // ********************************************************************
1392 /**
1393  * class for ASN.1 DER IA5String
1394  * @name KJUR.asn1.DERIA5String
1395  * @class class for ASN.1 DER IA5String
1396  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1397  * @extends KJUR.asn1.DERAbstractString
1398  * @description
1399  * @see KJUR.asn1.DERAbstractString - superclass
1400  */
1401 KJUR.asn1.DERIA5String = function(params) {
1402     KJUR.asn1.DERIA5String.superclass.constructor.call(this, params);
1403     this.hT = "16";
1404 };
1405 extendClass(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString);
1406 
1407 // ********************************************************************
1408 /**
1409  * class for ASN.1 DER VisibleString
1410  * @name KJUR.asn1.DERVisibleString
1411  * @class class for ASN.1 DER VisibleString
1412  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1413  * @extends KJUR.asn1.DERAbstractString
1414  * @since jsrsasign 8.0.23 asn1 1.0.15
1415  * @description
1416  * @see KJUR.asn1.DERAbstractString - superclass
1417  */
1418 KJUR.asn1.DERVisibleString = function(params) {
1419     KJUR.asn1.DERIA5String.superclass.constructor.call(this, params);
1420     this.hT = "1a";
1421 };
1422 extendClass(KJUR.asn1.DERVisibleString, KJUR.asn1.DERAbstractString);
1423 
1424 // ********************************************************************
1425 /**
1426  * class for ASN.1 DER BMPString
1427  * @name KJUR.asn1.DERBMPString
1428  * @class class for ASN.1 DER BMPString
1429  * @param {Array} params associative array of parameters (ex. {'str': 'aaa'})
1430  * @extends KJUR.asn1.DERAbstractString
1431  * @since jsrsasign 8.0.23 asn1 1.0.15
1432  * @description
1433  * @see KJUR.asn1.DERAbstractString - superclass
1434  */
1435 KJUR.asn1.DERBMPString = function(params) {
1436     KJUR.asn1.DERBMPString.superclass.constructor.call(this, params);
1437     this.hT = "1e";
1438 };
1439 extendClass(KJUR.asn1.DERBMPString, KJUR.asn1.DERAbstractString);
1440 
1441 // ********************************************************************
1442 /**
1443  * class for ASN.1 DER UTCTime
1444  * @name KJUR.asn1.DERUTCTime
1445  * @class class for ASN.1 DER UTCTime
1446  * @param {Array} params associative array of parameters (ex. {'str': '130430235959Z'})
1447  * @extends KJUR.asn1.DERAbstractTime
1448  * @description
1449  * <br/>
1450  * As for argument 'params' for constructor, you can specify one of
1451  * following properties:
1452  * <ul>
1453  * <li>str - specify initial ASN.1 value(V) by a string (ex.'130430235959Z')</li>
1454  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
1455  * <li>date - specify Date object.</li>
1456  * </ul>
1457  * NOTE: 'params' can be omitted.
1458  * <h4>EXAMPLES</h4>
1459  * @example
1460  * d1 = new KJUR.asn1.DERUTCTime();
1461  * d1.setString('130430125959Z');
1462  *
1463  * d2 = new KJUR.asn1.DERUTCTime({'str': '130430125959Z'});
1464  * d3 = new KJUR.asn1.DERUTCTime({'date': new Date(Date.UTC(2015, 0, 31, 0, 0, 0, 0))});
1465  * d4 = new KJUR.asn1.DERUTCTime('130430125959Z');
1466  */
1467 KJUR.asn1.DERUTCTime = function(params) {
1468     KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params);
1469     this.hT = "17";
1470 
1471     /**
1472      * set value by a Date object<br/>
1473      * @name setByDate
1474      * @memberOf KJUR.asn1.DERUTCTime#
1475      * @function
1476      * @param {Date} dateObject Date object to set ASN.1 value(V)
1477      * @example
1478      * o = new KJUR.asn1.DERUTCTime();
1479      * o.setByDate(new Date("2016/12/31"));
1480      */
1481     this.setByDate = function(dateObject) {
1482         this.hTLV = null;
1483         this.isModified = true;
1484         this.date = dateObject;
1485         this.s = this.formatDate(this.date, 'utc');
1486         this.hV = stohex(this.s);
1487     };
1488 
1489     this.getFreshValueHex = function() {
1490         if (typeof this.date == "undefined" && typeof this.s == "undefined") {
1491             this.date = new Date();
1492             this.s = this.formatDate(this.date, 'utc');
1493             this.hV = stohex(this.s);
1494         }
1495         return this.hV;
1496     };
1497 
1498     if (params !== undefined) {
1499         if (params.str !== undefined) {
1500             this.setString(params.str);
1501         } else if (typeof params == "string" && params.match(/^[0-9]{12}Z$/)) {
1502             this.setString(params);
1503         } else if (params.hex !== undefined) {
1504             this.setStringHex(params.hex);
1505         } else if (params.date !== undefined) {
1506             this.setByDate(params.date);
1507         }
1508     }
1509 };
1510 extendClass(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime);
1511 
1512 // ********************************************************************
1513 /**
1514  * class for ASN.1 DER GeneralizedTime
1515  * @name KJUR.asn1.DERGeneralizedTime
1516  * @class class for ASN.1 DER GeneralizedTime
1517  * @param {Array} params associative array of parameters (ex. {'str': '20130430235959Z'})
1518  * @property {Boolean} withMillis flag to show milliseconds or not
1519  * @extends KJUR.asn1.DERAbstractTime
1520  * @description
1521  * <br/>
1522  * As for argument 'params' for constructor, you can specify one of
1523  * following properties:
1524  * <ul>
1525  * <li>str - specify initial ASN.1 value(V) by a string (ex.'20130430235959Z')</li>
1526  * <li>hex - specify initial ASN.1 value(V) by a hexadecimal string</li>
1527  * <li>date - specify Date object.</li>
1528  * <li>millis - specify flag to show milliseconds (from 1.0.6)</li>
1529  * </ul>
1530  * NOTE1: 'params' can be omitted.
1531  * NOTE2: 'withMillis' property is supported from asn1 1.0.6.
1532  */
1533 KJUR.asn1.DERGeneralizedTime = function(params) {
1534     KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params);
1535     this.hT = "18";
1536     this.withMillis = false;
1537 
1538     /**
1539      * set value by a Date object
1540      * @name setByDate
1541      * @memberOf KJUR.asn1.DERGeneralizedTime#
1542      * @function
1543      * @param {Date} dateObject Date object to set ASN.1 value(V)
1544      * @example
1545      * When you specify UTC time, use 'Date.UTC' method like this:<br/>
1546      * o1 = new DERUTCTime();
1547      * o1.setByDate(date);
1548      *
1549      * date = new Date(Date.UTC(2015, 0, 31, 23, 59, 59, 0)); #2015JAN31 23:59:59
1550      */
1551     this.setByDate = function(dateObject) {
1552         this.hTLV = null;
1553         this.isModified = true;
1554         this.date = dateObject;
1555         this.s = this.formatDate(this.date, 'gen', this.withMillis);
1556         this.hV = stohex(this.s);
1557     };
1558 
1559     this.getFreshValueHex = function() {
1560         if (this.date === undefined && this.s === undefined) {
1561             this.date = new Date();
1562             this.s = this.formatDate(this.date, 'gen', this.withMillis);
1563             this.hV = stohex(this.s);
1564         }
1565         return this.hV;
1566     };
1567 
1568     if (params !== undefined) {
1569         if (params.str !== undefined) {
1570             this.setString(params.str);
1571         } else if (typeof params == "string" && params.match(/^[0-9]{14}Z$/)) {
1572             this.setString(params);
1573         } else if (params.hex !== undefined) {
1574             this.setStringHex(params.hex);
1575         } else if (params.date !== undefined) {
1576             this.setByDate(params.date);
1577         }
1578         if (params.millis === true) {
1579             this.withMillis = true;
1580         }
1581     }
1582 };
1583 extendClass(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime);
1584 
1585 // ********************************************************************
1586 /**
1587  * class for ASN.1 DER Sequence
1588  * @name KJUR.asn1.DERSequence
1589  * @class class for ASN.1 DER Sequence
1590  * @extends KJUR.asn1.DERAbstractStructured
1591  * @description
1592  * <br/>
1593  * As for argument 'params' for constructor, you can specify one of
1594  * following properties:
1595  * <ul>
1596  * <li>array - specify array of ASN1Object to set elements of content</li>
1597  * </ul>
1598  * NOTE: 'params' can be omitted.
1599  */
1600 KJUR.asn1.DERSequence = function(params) {
1601     KJUR.asn1.DERSequence.superclass.constructor.call(this, params);
1602     this.hT = "30";
1603     this.getFreshValueHex = function() {
1604         var h = '';
1605         for (var i = 0; i < this.asn1Array.length; i++) {
1606             var asn1Obj = this.asn1Array[i];
1607             h += asn1Obj.getEncodedHex();
1608         }
1609         this.hV = h;
1610         return this.hV;
1611     };
1612 };
1613 extendClass(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured);
1614 
1615 // ********************************************************************
1616 /**
1617  * class for ASN.1 DER Set
1618  * @name KJUR.asn1.DERSet
1619  * @class class for ASN.1 DER Set
1620  * @extends KJUR.asn1.DERAbstractStructured
1621  * @description
1622  * <br/>
1623  * As for argument 'params' for constructor, you can specify one of
1624  * following properties:
1625  * <ul>
1626  * <li>array - specify array of ASN1Object to set elements of content</li>
1627  * <li>sortflag - flag for sort (default: true). ASN.1 BER is not sorted in 'SET OF'.</li>
1628  * </ul>
1629  * NOTE1: 'params' can be omitted.<br/>
1630  * NOTE2: sortflag is supported since 1.0.5.
1631  */
1632 KJUR.asn1.DERSet = function(params) {
1633     KJUR.asn1.DERSet.superclass.constructor.call(this, params);
1634     this.hT = "31";
1635     this.sortFlag = true; // item shall be sorted only in ASN.1 DER
1636     this.getFreshValueHex = function() {
1637         var a = new Array();
1638         for (var i = 0; i < this.asn1Array.length; i++) {
1639             var asn1Obj = this.asn1Array[i];
1640             a.push(asn1Obj.getEncodedHex());
1641         }
1642         if (this.sortFlag == true) a.sort();
1643         this.hV = a.join('');
1644         return this.hV;
1645     };
1646 
1647     if (typeof params != "undefined") {
1648         if (typeof params.sortflag != "undefined" &&
1649             params.sortflag == false)
1650             this.sortFlag = false;
1651     }
1652 };
1653 extendClass(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured);
1654 
1655 // ********************************************************************
1656 /**
1657  * class for ASN.1 DER TaggedObject
1658  * @name KJUR.asn1.DERTaggedObject
1659  * @class class for ASN.1 DER TaggedObject
1660  * @extends KJUR.asn1.ASN1Object
1661  *
1662  * @description
1663  * <br/>
1664  * Parameter 'tagNoNex' is ASN.1 tag(T) value for this object.
1665  * For example, if you find '[1]' tag in a ASN.1 dump, 
1666  * 'tagNoHex' will be 'a1'.
1667  * <br/>
1668  * As for optional argument 'params' for constructor, you can specify *ANY* of
1669  * following properties:
1670  * <ul>
1671  * <li>tag - specify tag (default is 'a0' which means [0])</li>
1672  * <li>explicit - specify true if this is explicit tag otherwise false 
1673  *     (default is 'true').</li>
1674  * <li>obj - specify ASN1Object which is tagged</li>
1675  * <li>tage - specify tag with explicit</li>
1676  * <li>tagi - specify tag with implicit</li>
1677  * </ul>
1678  *
1679  * @example
1680  * new KJUR.asn1.DERTaggedObject({
1681  *  tage:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // explicit
1682  * }) 
1683  * new KJUR.asn1.DERTaggedObject({
1684  *  tagi:'a0', obj: new KJUR.asn1.DERInteger({int: 3}) // implicit
1685  * }) 
1686  * new KJUR.asn1.DERTaggedObject({
1687  *  tag:'a0', explicit: true, obj: new KJUR.asn1.DERInteger({int: 3}) // explicit
1688  * }) 
1689  *
1690  * // to hexadecimal
1691  * d1 = new KJUR.asn1.DERUTF8String({str':'a'})
1692  * d2 = new KJUR.asn1.DERTaggedObject({'obj': d1});
1693  * hex = d2.getEncodedHex();
1694  */
1695 KJUR.asn1.DERTaggedObject = function(params) {
1696     KJUR.asn1.DERTaggedObject.superclass.constructor.call(this);
1697 
1698     var _KJUR_asn1 = KJUR.asn1;
1699 
1700     this.hT = "a0";
1701     this.hV = '';
1702     this.isExplicit = true;
1703     this.asn1Object = null;
1704 
1705     /**
1706      * set value by an ASN1Object
1707      * @name setString
1708      * @memberOf KJUR.asn1.DERTaggedObject#
1709      * @function
1710      * @param {Boolean} isExplicitFlag flag for explicit/implicit tag
1711      * @param {Integer} tagNoHex hexadecimal string of ASN.1 tag
1712      * @param {ASN1Object} asn1Object ASN.1 to encapsulate
1713      */
1714     this.setASN1Object = function(isExplicitFlag, tagNoHex, asn1Object) {
1715         this.hT = tagNoHex;
1716         this.isExplicit = isExplicitFlag;
1717         this.asn1Object = asn1Object;
1718         if (this.isExplicit) {
1719             this.hV = this.asn1Object.getEncodedHex();
1720             this.hTLV = null;
1721             this.isModified = true;
1722         } else {
1723             this.hV = null;
1724             this.hTLV = asn1Object.getEncodedHex();
1725             this.hTLV = this.hTLV.replace(/^../, tagNoHex);
1726             this.isModified = false;
1727         }
1728     };
1729 
1730     this.getFreshValueHex = function() {
1731         return this.hV;
1732     };
1733 
1734     this.setByParam = function(params) {
1735         if (params.tag != undefined) {
1736             this.hT = params.tag;
1737         }
1738         if (params.explicit != undefined) {
1739             this.isExplicit = params.explicit;
1740         }
1741 	if (params.tage != undefined) {
1742 	    this.hT = params.tage;
1743             this.isExplicit = true;
1744 	}
1745 	if (params.tagi != undefined) {
1746 	    this.hT = params.tagi;
1747             this.isExplicit = false;
1748 	}
1749         if (params.obj != undefined) {
1750 	    if (params.obj instanceof _KJUR_asn1.ASN1Object) {
1751 		this.asn1Object = params.obj;
1752 		this.setASN1Object(this.isExplicit, this.hT, this.asn1Object);
1753 	    } else if (typeof params.obj == "object") {
1754 		this.asn1Object = _KJUR_asn1.ASN1Util.newObject(params.obj);
1755 		this.setASN1Object(this.isExplicit, this.hT, this.asn1Object);
1756 	    }
1757         }
1758     };
1759 
1760     if (params != undefined) this.setByParam(params);
1761 };
1762 extendClass(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object);
1763