1 /* asn1tsp-2.0.4.js (c) 2014-2020 Kenji Urushima | kjur.github.com/jsrsasign/license
  2  */
  3 /*
  4  * asn1tsp.js - ASN.1 DER encoder classes for RFC 3161 Time Stamp Protocol
  5  *
  6  * Copyright (c) 2014-2017 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 asn1tsp-1.0.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version jsrsasign 10.1.4 asn1tsp 2.0.4 (2020-Nov-22)
 20  * @since jsrsasign 4.5.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  * // already documented in asn1-1.0.js
 27  * @name KJUR
 28  * @namespace kjur's class library name space
 29  */
 30 if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
 31 
 32 /*
 33  * kjur's ASN.1 class library name space
 34  * // already documented in asn1-1.0.js
 35  * @name KJUR.asn1
 36  * @namespace
 37  */
 38 if (typeof KJUR.asn1 == "undefined" || !KJUR.asn1) KJUR.asn1 = {};
 39 
 40 /**
 41  * kjur's ASN.1 class for RFC 3161 Time Stamp Protocol
 42  * <p>
 43  * This name space provides 
 44  * <a href="https://tools.ietf.org/html/rfc3161">RFC 3161
 45  * Time-Stamp Protocol(TSP)</a> data generator.
 46  *
 47  * <h4>FEATURES</h4>
 48  * <ul>
 49  * <li>easily generate CMS SignedData</li>
 50  * <li>APIs are very similar to BouncyCastle library ASN.1 classes. So easy to learn.</li>
 51  * </ul>
 52  * 
 53  * <h4>PROVIDED CLASSES</h4>
 54  * <ul>
 55  * </ul>
 56  * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
 57  * </p>
 58  * @name KJUR.asn1.tsp
 59  * @namespace
 60  */
 61 if (typeof KJUR.asn1.tsp == "undefined" || !KJUR.asn1.tsp) KJUR.asn1.tsp = {};
 62 
 63 /**
 64  * class for TSP TimeStampToken ASN.1 object<br/>
 65  * @name KJUR.asn1.tsp.TimeStampToken
 66  * @class class for TSP TimeStampToken ASN.1 object
 67  * @param {Array} params JSON object for constructor parameters
 68  * @extends KJUR.asn1.cms.SignedData
 69  * @since jsrsasign 10.0.0 asn1tsp 2.0.0
 70  * @see KJUR.asn1.tsp.TimeStampResp
 71  * @see KJUR.asn1.tsp.TSTInfo
 72  *
 73  * @description
 74  * This is an ASN.1 encoder for TimeStampToken
 75  * ASN.1 structure defined in
 76  * <a href="https://tools.ietf.org/html/rfc3161#section-2.4.2">
 77  * RFC 3161 TSP section 2.4.2</a>.
 78  * <pre>
 79  * TimeStampToken ::= ContentInfo
 80  *   -- contentType is id-signedData ([CMS])
 81  *   -- content is SignedData ([CMS])
 82  * id-ct-TSTInfo  OBJECT IDENTIFIER ::= { iso(1) member-body(2)
 83  * us(840) rsadsi(113549) pkcs(1) pkcs-9(9) smime(16) ct(1) 4}
 84  * </pre>
 85  * Constructor argument "params" is similar to
 86  * {@link KJUR.asn1.cms.SignedData} however "econtent"
 87  * value is different as follows:
 88  * <ul>
 89  * <li>econtent.type - shall be "tstinfo"</li>
 90  * <li>econtent.content - shall be {@link KJUR.asn1.tsp.TSTInfo} parameter</li>
 91  * </ul>
 92  *
 93  * @example
 94  * new KJUR.asn1.tsp.TimeStampToken({
 95  *   version: 1,
 96  *   hashalgs: ["sha256"],
 97  *   econtent: {
 98  *     type: "tstinfo",
 99  *     content: {
100  *       policy: '1.2.3.4.5',
101  *       messageImprint: { hashAlg: 'sha1', hashValue: 'a1a2a3a4' },
102  *       serial: {'int': 3},
103  *       genTime: {str: '20131231235959.123Z', millis: true},
104  *       accuracy: { millis: 500 },
105  *       ordering: true,
106  *       nonce: {'int': 3},
107  *     }
108  *   },
109  *   sinfos: [{
110  *     version: 1,
111  *     id: {type:'isssn', cert: sZ4_CERPEM},
112  *     hashalg: "sha256",
113  *     sattrs: {array: [{
114  *       attr: "contentType",
115  *       type: "data"
116  *     },{
117  *       attr: "signingTime",
118  *       str: '131231235959Z'
119  *     },{
120  *       attr: "messageDigest",
121  *       hex: 'ffff'
122  *     }]},
123  *     sigalg: "SHA256withRSA",
124  *     signkey: sZ4_PRVP8PPEM
125  *   }]
126  * })
127  */
128 KJUR.asn1.tsp.TimeStampToken = function(params) {
129     var _KJUR = KJUR,
130 	_KJUR_asn1 = _KJUR.asn1,
131 	_KJUR_asn1_tsp = _KJUR_asn1.tsp;
132 
133     _KJUR_asn1_tsp.TimeStampToken.superclass.constructor.call(this);
134 
135     this.params = null;
136 
137     this.getEncodedHexPrepare = function() {
138 	//alert("getEncodedHexPrepare called...");
139 	var dTSTInfo = new _KJUR_asn1_tsp.TSTInfo(this.params.econtent.content);
140 	this.params.econtent.content.hex = dTSTInfo.getEncodedHex();
141     };
142 
143     if (params != undefined) this.setByParam(params);
144 };
145 extendClass(KJUR.asn1.tsp.TimeStampToken, KJUR.asn1.cms.SignedData);
146 
147 /**
148  * class for TSP TSTInfo ASN.1 object
149  * @name KJUR.asn1.tsp.TSTInfo
150  * @class class for TSP TSTInfo ASN.1 object
151  * @param {Array} params JSON object for TSTInfo parameters
152  * @extends KJUR.asn1.ASN1Object
153  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
154  * @see KJUR.asn1.x509.X500Name
155  * @see KJUR.asn1.x509.GeneralName
156  * @description
157  * This class represents TSTInfo ASN.1 structure.
158  * <pre>
159  * TSTInfo ::= SEQUENCE  {
160  *    version         INTEGER  { v1(1) },
161  *    policy          TSAPolicyId,
162  *    messageImprint  MessageImprint,
163  *    serialNumber    INTEGER, -- up to 160bit
164  *    genTime         GeneralizedTime,
165  *    accuracy        Accuracy                 OPTIONAL,
166  *    ordering        BOOLEAN                  DEFAULT FALSE,
167  *    nonce           INTEGER                  OPTIONAL,
168  *    tsa             [0] GeneralName          OPTIONAL,
169  *    extensions      [1] IMPLICIT Extensions  OPTIONAL   }
170  * </pre>
171  * For "params" arguent, following properties are accepted:
172  * <ul>
173  * <li>{Array}tsa - {@link KJUR.asn1.x509.X500Name} parameter for
174  * tsa field even though tsa field is GeneralName.</li>
175  * </ul>
176  * @example
177  * o = new KJUR.asn1.tsp.TSTInfo({
178  *     policy:    '1.2.3.4.5',
179  *     messageImprint: {alg: 'sha256', hash: '1abc...'},
180  *     serial:    {int: 3},
181  *     genTime:   {millis: true},         // OPTION
182  *     accuracy:  {micros: 500},          // OPTION
183  *     ordering:  true,                   // OPITON
184  *     nonce:     {hex: '52fab1...'},     // OPTION
185  *     tsa:       {str: '/C=US/O=TSA1'}   // OPITON
186  * });
187  */
188 KJUR.asn1.tsp.TSTInfo = function(params) {
189     var _Error = Error,
190 	_KJUR = KJUR,
191 	_KJUR_asn1 = _KJUR.asn1,
192 	_DERSequence = _KJUR_asn1.DERSequence,
193 	_DERInteger = _KJUR_asn1.DERInteger,
194 	_DERBoolean = _KJUR_asn1.DERBoolean,
195 	_DERGeneralizedTime = _KJUR_asn1.DERGeneralizedTime,
196 	_DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier,
197 	_DERTaggedObject = _KJUR_asn1.DERTaggedObject,
198 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
199 	_MessageImprint = _KJUR_asn1_tsp.MessageImprint,
200 	_Accuracy = _KJUR_asn1_tsp.Accuracy,
201         _X500Name = _KJUR_asn1.x509.X500Name,
202         _GeneralName = _KJUR_asn1.x509.GeneralName;
203 	
204 
205     _KJUR_asn1_tsp.TSTInfo.superclass.constructor.call(this);
206 
207     this.dVersion = new _DERInteger({'int': 1});
208     this.dPolicy = null;
209     this.dMessageImprint = null;
210     this.dSerial = null;
211     this.dGenTime = null;
212     this.dAccuracy = null;
213     this.dOrdering = null;
214     this.dNonce = null;
215     this.dTsa = null;
216 
217     this.getEncodedHex = function() {
218         var a = [this.dVersion];
219 
220         if (this.dPolicy == null) throw new Error("policy shall be specified.");
221         a.push(this.dPolicy);
222 
223         if (this.dMessageImprint == null)
224             throw new Error("messageImprint shall be specified.");
225         a.push(this.dMessageImprint);
226 
227         if (this.dSerial == null)
228             throw new Error("serialNumber shall be specified.");
229         a.push(this.dSerial);
230 
231         if (this.dGenTime == null)
232             throw new Error("genTime shall be specified.");
233         a.push(this.dGenTime);
234 
235         if (this.dAccuracy != null) a.push(this.dAccuracy);
236         if (this.dOrdering != null) a.push(this.dOrdering);
237         if (this.dNonce != null) a.push(this.dNonce);
238         if (this.dTsa != null) a.push(this.dTsa);
239 
240         var seq = new _DERSequence({array: a});
241         this.hTLV = seq.getEncodedHex();
242         return this.hTLV;
243     };
244 
245     if (params !== undefined) {
246         if (typeof params.policy == "string") {
247             if (! params.policy.match(/^[0-9.]+$/))
248                 throw "policy shall be oid like 0.1.4.134";
249             this.dPolicy = new _DERObjectIdentifier({oid: params.policy});
250         }
251         if (params.messageImprint !== undefined) {
252             this.dMessageImprint = new _MessageImprint(params.messageImprint);
253         }
254         if (params.serial !== undefined) {
255             this.dSerial = new _DERInteger(params.serial);
256         }
257         if (params.genTime !== undefined) {
258             this.dGenTime = new _DERGeneralizedTime(params.genTime);
259         }
260         if (params.accuracy !== undefined) {
261             this.dAccuracy = new _Accuracy(params.accuracy);
262         }
263         if (params.ordering !== undefined &&
264             params.ordering == true) {
265             this.dOrdering = new _DERBoolean();
266         }
267         if (params.nonce !== undefined) {
268             this.dNonce = new _DERInteger(params.nonce);
269         }
270         if (params.tsa !== undefined) {
271             this.dTsa = new _DERTaggedObject({
272 		tag: "a0",
273 		explicit: true,
274 		obj: new _GeneralName({dn: params.tsa})
275 	    });
276         }
277     }
278 };
279 extendClass(KJUR.asn1.tsp.TSTInfo, KJUR.asn1.ASN1Object);
280 
281 /**
282  * class for TSP Accuracy ASN.1 object
283  * @name KJUR.asn1.tsp.Accuracy
284  * @class class for TSP Accuracy ASN.1 object
285  * @param {Array} params associative array of parameters
286  * @extends KJUR.asn1.ASN1Object
287  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
288  *
289  * @description
290  * This is an ASN.1 encoder for Accuracy
291  * ASN.1 structure defined in
292  * <a href="https://tools.ietf.org/html/rfc3161#section-2.4.2">
293  * RFC 3161 TSP section 2.4.2</a>.
294  * <pre>
295  * Accuracy ::= SEQUENCE {
296  *    seconds        INTEGER              OPTIONAL,
297  *    millis     [0] INTEGER  (1..999)    OPTIONAL,
298  *    micros     [1] INTEGER  (1..999)    OPTIONAL  }
299  * </pre>
300  *
301  * @example
302  * new KJUR.asn1.tsp.Accuracy({
303  *   seconds: 1,   // OPTION
304  *   millis: 500,  // OPTION
305  *   micros: 500   // OPTION
306  * });
307  */
308 KJUR.asn1.tsp.Accuracy = function(params) {
309     var _KJUR = KJUR,
310 	_KJUR_asn1 = _KJUR.asn1,
311 	_newObject = _KJUR_asn1.ASN1Util.newObject;
312 
313     _KJUR_asn1.tsp.Accuracy.superclass.constructor.call(this);
314 
315     this.params = null;
316 
317     this.getEncodedHex = function() {
318 	var params = this.params;
319 	var a = [];
320 	if (params.seconds != undefined &&
321 	    typeof params.seconds == "number") {
322 	    a.push({"int": params.seconds});
323 	}
324 	if (params.millis != undefined &&
325 	    typeof params.millis == "number") {
326 	    a.push({tag: {tagi:"80", obj:{"int": params.millis}}});
327 	}
328 	if (params.micros != undefined &&
329 	    typeof params.micros == "number") {
330 	    a.push({tag: {tagi:"81", obj:{"int": params.micros}}});
331 	}
332 	return _newObject({"seq": a}).getEncodedHex();
333     };
334 
335     if (params != undefined) this.setByParam(params);
336 };
337 extendClass(KJUR.asn1.tsp.Accuracy, KJUR.asn1.ASN1Object);
338 
339 /**
340  * class for TSP MessageImprint ASN.1 object
341  * @name KJUR.asn1.tsp.MessageImprint
342  * @class class for TSP MessageImprint ASN.1 object
343  * @param {Array} params associative array of parameters
344  * @extends KJUR.asn1.ASN1Object
345  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
346  *
347  * @description
348  * This is an ASN.1 encoder for Accuracy
349  * ASN.1 structure defined in
350  * <a href="https://tools.ietf.org/html/rfc3161#section-2.4.2">
351  * RFC 3161 TSP section 2.4.2</a>.
352  * <pre>
353  * MessageImprint ::= SEQUENCE  {
354  *      hashAlgorithm                AlgorithmIdentifier,
355  *      hashedMessage                OCTET STRING  }
356  * </pre>
357  *
358  * @example
359  * // OLD
360  * new KJUR.asn1.tsp.MessageImprint({
361  *   hashAlg: 'sha256',
362  *   hashValue: '1f3dea...'
363  * });
364  * // NEW
365  * new KJUR.asn1.tsp.MessageImprint({
366  *   alg: 'sha256',
367  *   hash: '1f3dea...'
368  * });
369  */
370 KJUR.asn1.tsp.MessageImprint = function(params) {
371     var _KJUR = KJUR,
372 	_KJUR_asn1 = _KJUR.asn1,
373 	_DERSequence = _KJUR_asn1.DERSequence,
374 	_DEROctetString = _KJUR_asn1.DEROctetString,
375 	_KJUR_asn1_x509 = _KJUR_asn1.x509,
376 	_AlgorithmIdentifier = _KJUR_asn1_x509.AlgorithmIdentifier;
377 
378     _KJUR_asn1.tsp.MessageImprint.superclass.constructor.call(this);
379 
380     this.params = null;
381 
382     this.getEncodedHex = function() {
383 	var params = this.params;
384 	var dAlg = new _AlgorithmIdentifier({name: params.alg});
385 	var dHash = new _DEROctetString({hex: params.hash});
386 	var seq = new _DERSequence({array: [dAlg, dHash]});
387 	return seq.getEncodedHex();
388     };
389 
390     if (params !== undefined) this.setByParam(params);
391 };
392 extendClass(KJUR.asn1.tsp.MessageImprint, KJUR.asn1.ASN1Object);
393 
394 /**
395  * class for TSP TimeStampReq ASN.1 object<br/>
396  * @name KJUR.asn1.tsp.TimeStampReq
397  * @class class for TSP TimeStampReq ASN.1 object
398  * @param {Array} params JSON object of parameters
399  * @extends KJUR.asn1.ASN1Object
400  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
401  * @see KJUR.asn1.tsp.MessageImprint
402  *
403  * @description
404  * <pre>
405  * TimeStampReq ::= SEQUENCE  {
406  *    version          INTEGER  { v1(1) },
407  *    messageImprint   MessageImprint,
408  *    reqPolicy        TSAPolicyId               OPTIONAL,
409  *    nonce            INTEGER                   OPTIONAL,
410  *    certReq          BOOLEAN                   DEFAULT FALSE,
411  *    extensions       [0] IMPLICIT Extensions   OPTIONAL  }
412  * </pre>
413  *
414  * @example
415  * new KJUR.asn1.tsp.TimeStampReq({
416  *   messageImprint: {alg: "sha256", hash: "12ab..."},
417  *   policy: "1.2.3.4.5",
418  *   nonce: {hex: "1a2b..."},
419  *   certreq: true
420  * })
421  */
422 KJUR.asn1.tsp.TimeStampReq = function(params) {
423     var _KJUR = KJUR,
424 	_KJUR_asn1 = _KJUR.asn1,
425 	_DERSequence = _KJUR_asn1.DERSequence,
426 	_DERInteger = _KJUR_asn1.DERInteger,
427 	_DERBoolean = _KJUR_asn1.DERBoolean,
428 	_ASN1Object = _KJUR_asn1.ASN1Object,
429 	_DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier,
430 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
431 	_MessageImprint = _KJUR_asn1_tsp.MessageImprint;
432 
433     _KJUR_asn1_tsp.TimeStampReq.superclass.constructor.call(this);
434 
435     this.params = null;
436 
437     this.getEncodedHex = function() {
438 	var params = this.params;
439 
440 	var a = [];
441 	a.push(new _DERInteger({'int': 1}));
442 	if (params.messageImprint instanceof KJUR.asn1.ASN1Object) {
443 	    a.push(params.messageImprint);
444 	} else {
445 	    a.push(new _MessageImprint(params.messageImprint));
446 	}
447 	if (params.policy != undefined)
448 	    a.push(new _DERObjectIdentifier(params.policy));
449 	if (params.nonce != undefined)
450 	    a.push(new _DERInteger(params.nonce));
451 	if (params.certreq == true)
452 	    a.push(new _DERBoolean());
453 
454         var seq = new _DERSequence({array: a});
455 	return seq.getEncodedHex();
456     };
457 
458     if (params != undefined) this.setByParam(params);
459 };
460 extendClass(KJUR.asn1.tsp.TimeStampReq, KJUR.asn1.ASN1Object);
461 
462 /**
463  * class for TSP TimeStampResp ASN.1 object
464  * @name KJUR.asn1.tsp.TimeStampResp
465  * @class class for TSP TimeStampResp ASN.1 object
466  * @param {Array} params associative array of parameters
467  * @extends KJUR.asn1.ASN1Object
468  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
469  * @description
470  * <pre>
471  * TimeStampResp ::= SEQUENCE  {
472  *    status                  PKIStatusInfo,
473  *    timeStampToken          TimeStampToken     OPTIONAL  }
474  * </pre>
475  */
476 KJUR.asn1.tsp.TimeStampResp = function(params) {
477     var _KJUR = KJUR,
478 	_KJUR_asn1 = _KJUR.asn1,
479 	_DERSequence = _KJUR_asn1.DERSequence,
480 	_ASN1Object = _KJUR_asn1.ASN1Object,
481 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
482 	_PKIStatusInfo = _KJUR_asn1_tsp.PKIStatusInfo;
483 
484     _KJUR_asn1_tsp.TimeStampResp.superclass.constructor.call(this);
485 
486     this.params = null;
487 
488     this.getEncodedHex = function() {
489 	var params = this.params;
490 
491 	var a = [new _PKIStatusInfo(params.statusinfo)];
492 
493 	if (params.econtent != undefined) {
494 	    a.push((new _KJUR_asn1_tsp.TimeStampToken(params)).getContentInfo());
495 	}
496 
497 	if (params.tst != undefined && 
498 	    params.tst instanceof _KJUR_asn1.ASN1Object) {
499 	    a.push(params.tst);
500 	}
501 
502 	var seq = new _DERSequence({array: a});
503 	return seq.getEncodedHex();
504     };
505 
506     if (params != undefined) this.setByParam(params);
507 };
508 extendClass(KJUR.asn1.tsp.TimeStampResp, KJUR.asn1.ASN1Object);
509 
510 // --- BEGIN OF RFC 2510 CMP -----------------------------------------------
511 
512 /**
513  * class for TSP PKIStatusInfo ASN.1 object
514  * @name KJUR.asn1.tsp.PKIStatusInfo
515  * @class class for TSP PKIStatusInfo ASN.1 object
516  * @param {Array} params associative array of parameters
517  * @extends KJUR.asn1.ASN1Object
518  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
519  * @see KJUR.asn1.tsp.PKIStatus
520  * @see KJUR.asn1.tsp.PKIFreeText
521  * @see KJUR.asn1.tsp.PKIFailureInfo
522  * @see KJUR.asn1.tsp.TSPParser#getPKIStatusInfo
523  *
524  * @description
525  * This class provides ASN.1 PKIStatusInfo encoder
526  * defined in 
527  * <a href="https://tools.ietf.org/html/rfc3161#section-2.4.2">
528  * RFC 3161 section 2.4.2</a>.
529  * <pre>
530  * PKIStatusInfo ::= SEQUENCE {
531  *    status                  PKIStatus,
532  *    statusString            PKIFreeText     OPTIONAL,
533  *    failInfo                PKIFailureInfo  OPTIONAL  }
534  * </pre>
535  *
536  * @example
537  * new KJUR.asn1.tsp.PKIStatusInfo("granted")
538  * new KJUR.asn1.tsp.PKIStatusInfo({status: "granted"})
539  * new KJUR.asn1.tsp.PKIStatusInfo({
540  *   status: 2, // rejection
541  *   statusstr: ["unsupported algorithm"], // OPTION
542  *   failinfo: 'badAlg' // OPTION
543  * })
544  */
545 KJUR.asn1.tsp.PKIStatusInfo = function(params) {
546     var _Error = Error,
547 	_KJUR = KJUR,
548 	_KJUR_asn1 = _KJUR.asn1,
549 	_DERSequence = _KJUR_asn1.DERSequence,
550 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
551 	_PKIStatus = _KJUR_asn1_tsp.PKIStatus,
552 	_PKIFreeText = _KJUR_asn1_tsp.PKIFreeText,
553 	_PKIFailureInfo = _KJUR_asn1_tsp.PKIFailureInfo;
554 
555     _KJUR_asn1_tsp.PKIStatusInfo.superclass.constructor.call(this);
556 
557     this.params = null;
558 
559     this.getEncodedHex = function() {
560 	var params = this.params;
561 
562 	var a = [];
563 	if (typeof params == "string") {
564 	    a.push(new _PKIStatus(params));
565 	} else {
566 	    if (params.status == undefined)
567 		throw new _Error("property 'status' unspecified");
568 
569 	    a.push(new _PKIStatus(params.status));
570 
571 	    if (params.statusstr != undefined)
572 		a.push(new _PKIFreeText(params.statusstr));
573 
574 	    if (params.failinfo != undefined)
575 		a.push(new _PKIFailureInfo(params.failinfo));
576 	}
577 
578 	var seq = new _DERSequence({array: a});
579 	return seq.getEncodedHex();
580     };
581 
582     if (params != undefined) this.setByParam(params);
583 };
584 extendClass(KJUR.asn1.tsp.PKIStatusInfo, KJUR.asn1.ASN1Object);
585 
586 /**
587  * class for TSP PKIStatus ASN.1 object
588  * @name KJUR.asn1.tsp.PKIStatus
589  * @class class for TSP PKIStatus ASN.1 object
590  * @param {Array} params associative array of parameters
591  * @extends KJUR.asn1.ASN1Object
592  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
593  * @description
594  * <pre>
595  * PKIStatus ::= INTEGER {
596  *    granted                (0),
597  *    grantedWithMods        (1),
598  *    rejection              (2),
599  *    waiting                (3),
600  *    revocationWarning      (4),
601  *    revocationNotification (5) }
602  * </pre>
603  *
604  * @example
605  * new KJUR.asn1.tsp.PKIStatus('granted')
606  * new KJUR.asn1.tsp.PKIStatus(2)
607  */
608 KJUR.asn1.tsp.PKIStatus = function(params) {
609     var _Error = Error,
610 	_KJUR = KJUR,
611 	_KJUR_asn1 = _KJUR.asn1,
612 	_DERInteger = _KJUR_asn1.DERInteger,
613 	_KJUR_asn1_tsp = _KJUR_asn1.tsp;
614 
615     _KJUR_asn1_tsp.PKIStatus.superclass.constructor.call(this);
616 
617     var _nameValue = {
618 	granted:                0,
619 	grantedWithMods:        1,
620 	rejection:              2,
621 	waiting:                3,
622 	revocationWarning:      4,
623 	revocationNotification: 5
624     };
625 
626     this.params = null;
627 
628     this.getEncodedHex = function() {
629 	var params = this.params;
630 
631 	var dObj, value;
632 
633 	if (typeof params == "string") {
634 	    try {
635 		value = _nameValue[params];
636 	    } catch (ex) {
637 		throw new _Error("undefined name: " + params);
638 		}
639 	} else if (typeof params == "number") {
640 	    value = params;
641 	} else {
642 	    throw new _Error("unsupported params");
643 	}
644 
645 	return (new _DERInteger({"int": value})).getEncodedHex();
646     };
647 
648     if (params != undefined) this.setByParam(params);
649 };
650 extendClass(KJUR.asn1.tsp.PKIStatus, KJUR.asn1.ASN1Object);
651 
652 /**
653  * class for TSP PKIFreeText ASN.1 object
654  * @name KJUR.asn1.tsp.PKIFreeText
655  * @class class for TSP PKIFreeText ASN.1 object
656  * @param {Array} params associative array of parameters
657  * @extends KJUR.asn1.ASN1Object
658  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
659  * @description
660  * This class provides ASN.1 encoder for PKIFreeText
661  * defined in <a href="https://tools.ietf.org/html/rfc4210#section-5.1.1">
662  * RFC 4210 CMP section 5.1.1</a>.
663  * <pre>
664  * PKIFreeText ::= SEQUENCE { SIZE (1..MAX) OF UTF8String }
665  * </pre>
666  * 
667  * @example
668  * new KJUR.asn1.tsp.PKIFreeText([
669  *   "aaa", "bbb", "ccc"
670  * ])
671  */
672 KJUR.asn1.tsp.PKIFreeText = function(params) {
673     var _Error = Error,
674 	_KJUR = KJUR,
675 	_KJUR_asn1 = _KJUR.asn1,
676 	_DERSequence = _KJUR_asn1.DERSequence,
677 	_DERUTF8String = _KJUR_asn1.DERUTF8String,
678 	_KJUR_asn1_tsp = _KJUR_asn1.tsp;
679 
680     _KJUR_asn1_tsp.PKIFreeText.superclass.constructor.call(this);
681 
682     this.params = null;
683 
684     this.getEncodedHex = function() {
685 	var params = this.params;
686 
687 	if (! params instanceof Array)
688 	    throw new _Error("wrong params: not array");
689 
690 	var a = [];
691 	for (var i = 0; i < params.length; i++) {
692 	    a.push(new _DERUTF8String({str: params[i]}));
693 	};
694 	
695 	var seq = new _DERSequence({array: a});
696 	return seq.getEncodedHex();
697     };
698 
699     if (params != undefined) this.setByParam(params);
700 };
701 extendClass(KJUR.asn1.tsp.PKIFreeText, KJUR.asn1.ASN1Object);
702 
703 /**
704  * class for TSP PKIFailureInfo ASN.1 object
705  * @name KJUR.asn1.tsp.PKIFailureInfo
706  * @class class for TSP PKIFailureInfo ASN.1 object
707  * @param {Array} params associative array of parameters
708  * @extends KJUR.asn1.ASN1Object
709  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
710  * @description
711  * <pre>
712  * PKIFailureInfo ::= BIT STRING {
713  *    badAlg                 (0),
714  *    badRequest             (2),
715  *    badDataFormat          (5),
716  *    timeNotAvailable       (14),
717  *    unacceptedPolicy       (15),
718  *    unacceptedExtension    (16),
719  *    addInfoNotAvailable    (17),
720  *    systemFailure          (25) }
721  * </pre>
722  * 
723  * @example
724  * new KJUR.asn1.tsp.PKIFailureInfo('badAlg')
725  * new KJUR.asn1.tsp.PKIFailureInfo(5)
726  */
727 KJUR.asn1.tsp.PKIFailureInfo = function(params) {
728     var _Error = Error,
729 	_KJUR = KJUR,
730 	_KJUR_asn1 = _KJUR.asn1,
731 	_DERBitString = _KJUR_asn1.DERBitString,
732 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
733 	_PKIFailureInfo = _KJUR_asn1_tsp.PKIFailureInfo;
734 
735     var _nameValue = {
736 	badAlg:                 0,
737 	badRequest:             2,
738 	badDataFormat:          5,
739 	timeNotAvailable:       14,
740 	unacceptedPolicy:       15,
741 	unacceptedExtension:    16,
742 	addInfoNotAvailable:    17,
743 	systemFailure:          25
744     };
745 
746     _PKIFailureInfo.superclass.constructor.call(this);
747 
748     this.params = null;
749 
750     this.getEncodedHex = function() {
751 	var params = this.params;
752 
753 	var value;
754 	if (typeof params == "string") {
755 	    try {
756 		value = _nameValue[params];
757 	    } catch(ex) {
758 		throw new _Error("undefined name: " + params);
759 	    }
760 	} else if (typeof params == "number") {
761 	    value = params;
762 	} else {
763 	    throw new _Error("wrong params");
764 	}
765 	return (new _DERBitString({"bin": value.toString(2)})).getEncodedHex();
766     };
767 
768     if (params != undefined) this.setByParam(params);
769 };
770 extendClass(KJUR.asn1.tsp.PKIFailureInfo, KJUR.asn1.ASN1Object);
771 
772 // --- END OF RFC 2510 CMP -------------------------------------------
773 
774 /**
775  * abstract class for TimeStampToken generator (DEPRECATED)<br/>
776  * @name KJUR.asn1.tsp.AbstractTSAAdapter
777  * @class abstract class for TimeStampToken generator
778  * @param {Array} params associative array of parameters
779  * @since jsrsasign 4.7.0 asn1tsp 1.0.1
780  * @deprecated since jsrsasign 10.0.0 asn1tsp 2.0.0
781  *
782  * @description
783  * This is abstract class for TimeStampToken generator.
784  */
785 KJUR.asn1.tsp.AbstractTSAAdapter = function(params) {
786     this.getTSTHex = function(msgHex, hashAlg) {
787         throw "not implemented yet";
788     };
789 };
790 
791 /**
792  * class for simple TimeStampToken generator (DEPRECATED)<br/>
793  * @name KJUR.asn1.tsp.SimpleTSAAdapter
794  * @class class for simple TimeStampToken generator
795  * @extends KJUR.asn1.tsp.AbstractTSAAdapter
796  * @param {Array} params associative array of parameters
797  * @since jsrsasign 4.7.0 asn1tsp 1.0.1
798  * @deprecated since jsrsasign 10.0.0 asn1tsp 2.0.0
799  *
800  * @description
801  * This is a simple TimeStampToken generator class.
802  */
803 KJUR.asn1.tsp.SimpleTSAAdapter = function(params) {
804     var _KJUR = KJUR,
805 	_KJUR_asn1 = _KJUR.asn1,
806 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
807 	_hashHex = _KJUR.crypto.Util.hashHex;
808 
809     _KJUR_asn1_tsp.SimpleTSAAdapter.superclass.constructor.call(this);
810     this.params = null;
811     this.serial = 0;
812 
813     this.getTSTHex = function(msgHex, hashAlg) {
814         // messageImprint
815         var hashHex = _hashHex(msgHex, hashAlg);
816         this.params.econtent.content.messageImprint =
817             {alg: hashAlg, hash: hashHex};
818 
819         // serial
820         this.params.econtent.content.serial =
821 	    {'int': this.serial++};
822 
823         // nonce
824         var nonceValue = Math.floor(Math.random() * 1000000000);
825         this.params.econtent.content.nonce =
826 	    {'int': nonceValue};
827 
828         var obj = 
829             new _KJUR_asn1_tsp.TimeStampToken(this.params);
830         return obj.getContentInfoEncodedHex();
831     };
832 
833     if (params !== undefined) this.params = params;
834 };
835 extendClass(KJUR.asn1.tsp.SimpleTSAAdapter,
836                   KJUR.asn1.tsp.AbstractTSAAdapter);
837 
838 /**
839  * class for fixed TimeStampToken generator (DEPRECATED)<br/>
840  * @name KJUR.asn1.tsp.FixedTSAAdapter
841  * @class class for fixed TimeStampToken generator
842  * @extends KJUR.asn1.tsp.AbstractTSAAdapter
843  * @param {Array} params associative array of parameters
844  * @since jsrsasign 4.7.0 asn1tsp 1.0.1
845  * @deprecated since jsrsasign 10.0.0 asn1tsp 2.0.0
846  *
847  * @description
848  * This class generates fixed TimeStampToken except messageImprint
849  * for testing purpose.
850  * General TSA generates TimeStampToken which varies following
851  * fields:
852  * <ul>
853  * <li>genTime</li>
854  * <li>serialNumber</li>
855  * <li>nonce</li>
856  * </ul>
857  * Those values are provided by initial parameters.
858  */
859 KJUR.asn1.tsp.FixedTSAAdapter = function(params) {
860     var _KJUR = KJUR,
861 	_KJUR_asn1 = _KJUR.asn1,
862 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
863 	_hashHex = _KJUR.crypto.Util.hashHex;
864 
865     _KJUR_asn1_tsp.FixedTSAAdapter.superclass.constructor.call(this);
866     this.params = null;
867 
868     this.getTSTHex = function(msgHex, hashAlg) {
869         // fixed serialNumber
870         // fixed nonce        
871         var hashHex = _hashHex(msgHex, hashAlg);
872         this.params.econtent.content.messageImprint =
873             {alg: hashAlg, hash: hashHex};
874         var obj = new _KJUR_asn1_tsp.TimeStampToken(this.params);
875         return obj.getContentInfoEncodedHex();
876     };
877 
878     if (params !== undefined) this.params = params;
879 };
880 extendClass(KJUR.asn1.tsp.FixedTSAAdapter,
881                   KJUR.asn1.tsp.AbstractTSAAdapter);
882 
883 // --- TSP utilities -------------------------------------------------
884 
885 /**
886  * TSP utiliteis class
887  * @name KJUR.asn1.tsp.TSPUtil
888  * @class TSP utilities class
889  */
890 KJUR.asn1.tsp.TSPUtil = new function() {
891 };
892 /**
893  * generate TimeStampToken ASN.1 object specified by JSON parameters (DEPRECATED)<br/>
894  * @name newTimeStampToken
895  * @memberOf KJUR.asn1.tsp.TSPUtil
896  * @function
897  * @param {Array} param JSON parameter to generate TimeStampToken
898  * @return {KJUR.asn1.cms.SignedData} object just generated
899  * @deprecated since jsrsasign 10.0.0 asn1tsp 2.0.0 use TimeStampToken class
900  * @see KJUR.asn1.tsp.TimeStampToken
901  *
902  * @description
903  * @example
904  */
905 KJUR.asn1.tsp.TSPUtil.newTimeStampToken = function(params) {
906     return new KJUR.asn1.tsp.TimeStampToken(params);
907 };
908 
909 /**
910  * parse hexadecimal string of TimeStampReq
911  * @name parseTimeStampReq
912  * @memberOf KJUR.asn1.tsp.TSPUtil
913  * @function
914  * @param {String} hexadecimal string of TimeStampReq
915  * @return {Array} JSON object of parsed parameters
916  * @description
917  * This method parses a hexadecimal string of TimeStampReq
918  * and returns parsed their fields:
919  * @example
920  * var json = KJUR.asn1.tsp.TSPUtil.parseTimeStampReq("302602...");
921  * // resulted DUMP of above 'json':
922  * {
923  *  messageImprint: {
924  *       alg: 'sha256',          // MessageImprint hashAlg
925  *       hash: 'a1a2a3a4...'},   // MessageImprint hashValue
926  *  policy: '1.2.3.4.5',             // tsaPolicy (OPTION)
927  *  nonce: '9abcf318...',            // nonce (OPTION)
928  *  certreq: true}                   // certReq (OPTION)
929  */
930 KJUR.asn1.tsp.TSPUtil.parseTimeStampReq = function(reqHex) {
931     var _ASN1HEX = ASN1HEX;
932     var _getChildIdx = _ASN1HEX.getChildIdx;
933     var _getV = _ASN1HEX.getV;
934     var _getTLV = _ASN1HEX.getTLV;
935     var json = {};
936     json.certreq = false;
937 
938     var idxList = _getChildIdx(reqHex, 0);
939 
940     if (idxList.length < 2)
941         throw "TimeStampReq must have at least 2 items";
942 
943     var miHex = _getTLV(reqHex, idxList[1]);
944     json.messageImprint = KJUR.asn1.tsp.TSPUtil.parseMessageImprint(miHex); 
945 
946     for (var i = 2; i < idxList.length; i++) {
947         var idx = idxList[i];
948         var tag = reqHex.substr(idx, 2);
949         if (tag == "06") { // case OID
950             var policyHex = _getV(reqHex, idx);
951             json.policy = _ASN1HEX.hextooidstr(policyHex);
952         }
953         if (tag == "02") { // case INTEGER
954             json.nonce = _getV(reqHex, idx);
955         }
956         if (tag == "01") { // case BOOLEAN
957             json.certreq = true;
958         }
959     }
960 
961     return json;
962 };
963 
964 /**
965  * parse hexadecimal string of MessageImprint
966  * @name parseMessageImprint
967  * @memberOf KJUR.asn1.tsp.TSPUtil
968  * @function
969  * @param {String} hexadecimal string of MessageImprint
970  * @return {Array} JSON object of parsed parameters
971  * @description
972  * This method parses a hexadecimal string of MessageImprint
973  * and returns parsed their fields:
974  * @example
975  * var json = KJUR.asn1.tsp.TSPUtil.parseMessageImprint("302602...");
976  * // resulted DUMP of above 'json':
977  * {hashAlg: 'sha256',          // MessageImprint hashAlg
978  *  hashValue: 'a1a2a3a4...'}   // MessageImprint hashValue
979  */
980 KJUR.asn1.tsp.TSPUtil.parseMessageImprint = function(miHex) {
981     var _ASN1HEX = ASN1HEX;
982     var _getChildIdx = _ASN1HEX.getChildIdx;
983     var _getV = _ASN1HEX.getV;
984     var _getIdxbyList = _ASN1HEX.getIdxbyList;
985     var json = {};
986 
987     if (miHex.substr(0, 2) != "30")
988         throw "head of messageImprint hex shall be '30'";
989 
990     var idxList = _getChildIdx(miHex, 0);
991     var hashAlgOidIdx = _getIdxbyList(miHex, 0, [0, 0]);
992     var hashAlgHex = _getV(miHex, hashAlgOidIdx);
993     var hashAlgOid = _ASN1HEX.hextooidstr(hashAlgHex);
994     var hashAlgName = KJUR.asn1.x509.OID.oid2name(hashAlgOid);
995     if (hashAlgName == '')
996         throw "hashAlg name undefined: " + hashAlgOid;
997     var hashAlg = hashAlgName;
998     var hashValueIdx = _getIdxbyList(miHex, 0, [1]);
999 
1000     json.alg = hashAlg;
1001     json.hash = _getV(miHex, hashValueIdx); 
1002 
1003     return json;
1004 };
1005 
1006 /**
1007  * class for parsing RFC 3161 TimeStamp protocol data<br/>
1008  * @name KJUR.asn1.tsp.TSPParser
1009  * @class RFC 3161 TimeStamp protocol parser class
1010  * @since jsrsasign 10.1.0 asn1tsp 2.0.1
1011  *
1012  * @description
1013  * This is an ASN.1 parser for 
1014  * <a href="https://tools.ietf.org/html/rfc3161">RFC 3161</a>.
1015  */
1016 KJUR.asn1.tsp.TSPParser = function() {
1017     var _Error = Error,
1018 	_X509 = X509,
1019 	_x509obj = new _X509(),
1020 	_ASN1HEX = ASN1HEX,
1021 	_getV = _ASN1HEX.getV,
1022 	_getTLV = _ASN1HEX.getTLV,
1023 	_getIdxbyList = _ASN1HEX.getIdxbyList,
1024 	_getTLVbyListEx = _ASN1HEX.getTLVbyListEx,
1025 	_getChildIdx = _ASN1HEX.getChildIdx;
1026     var _aSTATUSSTR = [
1027 	"granted", "grantedWithMods", "rejection", "waiting",
1028 	"revocationWarning", "revocationNotification" ];
1029     var _pFAILUREINFO = {
1030 	0: "badAlg", 2: "badRequest", 5: "badDataFormat",
1031 	14: "timeNotAvailable", 15: "unacceptedPolicy",
1032 	16: "unacceptedExtension", 17: "addInfoNotAvailable",
1033 	25: "systemFailure"
1034     };
1035     
1036     /**
1037      * parse ASN.1 TimeStampResp<br/>
1038      * @name getResponse
1039      * @memberOf KJUR.asn1.tsp.TSPParser#
1040      * @function
1041      * @param {String} h hexadecimal string of ASN.1 TimeStampResp
1042      * @return {Array} JSON object of TimeStampResp parameter
1043      * @see KJUR.asn1.tsp.TimeStampResp
1044      * @see KJUR.asn1.tsp.TimeStampToken
1045      * @see KJUR.asn1.cms.CMSParser#getCMSSignedData
1046      *
1047      * @description
1048      * This method parses ASN.1 TimeStampRsp defined in RFC 3161.
1049      * <pre>
1050      * TimeStampResp ::= SEQUENCE {
1051      *   status          PKIStatusInfo,
1052      *   timeStampToken  TimeStampToken  OPTIONAL }
1053      * </pre>
1054      * When "h" is a TSP error response,
1055      * returned parameter contains "statusinfo" only.
1056      *
1057      * @example
1058      * parser = new KJUR.asn1.tsp.TSPParser();
1059      * parser.getResponse("30...") →
1060      * { 
1061      *   statusinfo: 'granted',
1062      *   ... // almost the same as CMS SignedData parameters
1063      *   econtent: {
1064      *     type: "tstinfo",
1065      *     content: { // TSTInfo parameter
1066      *       policy: '1.2.3.4.5',
1067      *       messageImprint: {alg: 'sha256', hash: 'a1a2a3a4...'},
1068      *       serial: {'int': 3},
1069      *       genTime: {str: '20131231235959.123Z'},
1070      *       accuracy: {millis: 500},
1071      *       ordering: true,
1072      *       nonce: {int: 3}
1073      *     }
1074      *   },
1075      *   ...
1076      * }
1077      */
1078     this.getResponse = function(h) {
1079 	var aIdx = _getChildIdx(h, 0);
1080 	
1081 	if (aIdx.length == 1) {
1082 	    return this.getPKIStatusInfo(_getTLV(h, aIdx[0]));
1083 	} else if (aIdx.length > 1) {
1084 	    var pPKIStatusInfo = this.getPKIStatusInfo(_getTLV(h, aIdx[0]));
1085 	    var hTST = _getTLV(h, aIdx[1]);
1086 	    var pResult = this.getToken(hTST);
1087 	    pResult.statusinfo = pPKIStatusInfo;
1088 	    return pResult;
1089 	}
1090     };
1091 
1092     /**
1093      * parse ASN.1 TimeStampToken<br/>
1094      * @name getToken
1095      * @memberOf KJUR.asn1.tsp.TSPParser#
1096      * @function
1097      * @param {String} h hexadecimal string of ASN.1 TimeStampToken
1098      * @return {Array} JSON object of TimeStampToken parameter
1099      * @see KJUR.asn1.tsp.TimeStampToken
1100      * @see KJUR.asn1.cms.CMSParser#getCMSSignedData
1101      * @see KJUR.asn1.tsp.TSPParser#setTSTInfo
1102      *
1103      * @description
1104      * This method parses ASN.1 TimeStampRsp defined in RFC 3161.
1105      * This method will parse "h" as CMS SigneData by
1106      * {@link KJUR.asn1.cms.CMSParser#getCMSSignedData}, then
1107      * parse and modify "econtent.content" parameter by
1108      * {@link KJUR.asn1.tsp.TSPParser#setTSTInfo} method.
1109      *
1110      * @example
1111      * parser = new KJUR.asn1.tsp.TSPParser();
1112      * parser.getToken("30...") →
1113      * { 
1114      *   ... // almost the same as CMS SignedData parameters
1115      *   econtent: {
1116      *     type: "tstinfo",
1117      *     content: { // TSTInfo parameter
1118      *       policy: '1.2.3.4.5',
1119      *       messageImprint: {alg: 'sha256', hash: 'a1a2a3a4...'},
1120      *       serial: {'int': 3},
1121      *       genTime: {str: '20131231235959.123Z'},
1122      *       accuracy: {millis: 500},
1123      *       ordering: true,
1124      *       nonce: {int: 3}
1125      *     }
1126      *   },
1127      *   ...
1128      * }
1129      */
1130     this.getToken = function(h) {
1131 	var _CMSParser = new KJUR.asn1.cms.CMSParser;
1132 	var p = _CMSParser.getCMSSignedData(h);
1133 	this.setTSTInfo(p);
1134 	return p;
1135     };
1136 
1137     /**
1138      * set ASN.1 TSTInfo parameter to CMS SignedData parameter<br/>
1139      * @name setTSTInfo
1140      * @memberOf KJUR.asn1.tsp.TSPParser#
1141      * @function
1142      * @param {Array} pCMSSignedData JSON object of CMS SignedData parameter
1143      * @see KJUR.asn1.tsp.TimeStampToken
1144      * @see KJUR.asn1.cms.CMSParser#getCMSSignedData
1145      *
1146      * @description
1147      * This method modifies "econtent.content" of CMS SignedData parameter
1148      * to parsed TSTInfo.
1149      * <pre>
1150      *
1151      * @example
1152      * parser = new KJUR.asn1.tsp.TSPParser();
1153      * pCMSSignedData = { 
1154      *   ... // almost the same as CMS SignedData parameters
1155      *   econtent: {
1156      *     type: "tstinfo",
1157      *     content: { hex: "30..." }
1158      *   },
1159      *   ...
1160      * };
1161      * parser.setTSTInfo(pCMSSignedData);
1162      * pCMSSignedData → { 
1163      *   ... // almost the same as CMS SignedData parameters
1164      *   econtent: {
1165      *     type: "tstinfo",
1166      *     content: { // TSTInfo parameter
1167      *       policy: '1.2.3.4.5',
1168      *       messageImprint: {alg: 'sha256', hash: 'a1a2a3a4...'},
1169      *       serial: {int: 3},
1170      *       genTime: {str: '20131231235959.123Z'},
1171      *       accuracy: {millis: 500},
1172      *       ordering: true,
1173      *       nonce: {int: 3}
1174      *     }
1175      *   },
1176      *   ...
1177      * };
1178      */
1179     this.setTSTInfo = function(pCMSSignedData) {
1180 	var pEContent = pCMSSignedData.econtent;
1181 	if (pEContent.type == "tstinfo") {
1182 	    var hContent = pEContent.content.hex;
1183 	    var pTSTInfo = this.getTSTInfo(hContent);
1184 	    //pTSTInfo.hex_ = hContent;
1185 	    pEContent.content = pTSTInfo;
1186 	}
1187     };
1188 
1189     /**
1190      * parse ASN.1 TSTInfo<br/>
1191      * @name getTSTInfo
1192      * @memberOf KJUR.asn1.tsp.TSPParser#
1193      * @function
1194      * @param {String} h hexadecimal string of ASN.1 TSTInfo
1195      * @return {Array} JSON object of TSTInfo parameter
1196      * @see KJUR.asn1.tsp.TSTInfo
1197      *
1198      * @description
1199      * This method parses ASN.1 TSTInfo defined in RFC 3161.
1200      * <pre>
1201      * TSTInfo ::= SEQUENCE  {
1202      *    version          INTEGER  { v1(1) },
1203      *    policy           TSAPolicyId,
1204      *    messageImprint   MessageImprint,
1205      *    serialNumber     INTEGER,
1206      *    genTime          GeneralizedTime,
1207      *    accuracy         Accuracy                 OPTIONAL,
1208      *    ordering         BOOLEAN             DEFAULT FALSE,
1209      *    nonce            INTEGER                  OPTIONAL,
1210      *    tsa              [0] GeneralName          OPTIONAL,
1211      *    extensions       [1] IMPLICIT Extensions  OPTIONAL }
1212      * </pre>
1213      *
1214      * @example
1215      * parser = new KJUR.asn1.tsp.TSPParser();
1216      * parser.getTSTInfo("30...") →
1217      * {
1218      *   policy: '1.2.3.4.5',
1219      *   messageImprint: {alg: 'sha256', hash: 'a1a2a3a4...'},
1220      *   serial: {'int': 3},
1221      *   genTime: {str: '20131231235959.123Z'},
1222      *   accuracy: {millis: 500},
1223      *   ordering: true,
1224      *   nonce: {int: 3}
1225      * }
1226      */
1227     this.getTSTInfo = function(h) {
1228 	var pResult = {};
1229 	var aIdx = _getChildIdx(h, 0);
1230 
1231 	var hPolicy = _getV(h, aIdx[1]);
1232 	pResult.policy = hextooid(hPolicy);
1233 
1234 	var hMessageImprint = _getTLV(h, aIdx[2]);
1235 	pResult.messageImprint = this.getMessageImprint(hMessageImprint);
1236 
1237 	var hSerial = _getV(h, aIdx[3]);
1238 	pResult.serial = {hex: hSerial};
1239 
1240 	var hGenTime = _getV(h, aIdx[4]);
1241 	pResult.genTime = {str: hextoutf8(hGenTime)};
1242 
1243 	var offset = 0;
1244 
1245 	if (aIdx.length > 5 && h.substr(aIdx[5], 2) == "30") {
1246 	    var hAccuracy = _getTLV(h, aIdx[5]);
1247 	    pResult.accuracy = this.getAccuracy(hAccuracy);
1248 	    offset++;
1249 	}
1250 
1251 	if (aIdx.length > 5 + offset && 
1252 	    h.substr(aIdx[5 + offset], 2) == "01") {
1253 	    var hOrdering = _getV(h, aIdx[5 + offset]);
1254 	    if (hOrdering == "ff") pResult.ordering = true;
1255 	    offset++;
1256 	}
1257 
1258 	if (aIdx.length > 5 + offset &&
1259 	    h.substr(aIdx[5 + offset], 2) == "02") {
1260 	    var hNonce = _getV(h, aIdx[5 + offset]);
1261 	    pResult.nonce = {hex: hNonce};
1262 	    offset++;
1263 	}
1264 
1265 	if (aIdx.length > 5 + offset &&
1266 	    h.substr(aIdx[5 + offset], 2) == "a0") {
1267 	    var hGeneralNames = _getTLV(h, aIdx[5 + offset]);
1268 	    hGeneralNames = "30" + hGeneralNames.substr(2);
1269 	    pGeneralNames = _x509obj.getGeneralNames(hGeneralNames);
1270 	    var pName = pGeneralNames[0].dn;
1271 	    pResult.tsa = pName;
1272 	    offset++;
1273 	}
1274 
1275 	if (aIdx.length > 5 + offset &&
1276 	    h.substr(aIdx[5 + offset], 2) == "a1") {
1277 	    var hExt = _getTLV(h, aIdx[5 + offset]);
1278 	    hExt = "30" + hExt.substr(2);
1279 	    var aExt = _x509obj.getExtParamArray(hExt);
1280 	    pResult.ext = aExt;
1281 	    offset++;
1282 	}
1283 
1284 	return pResult;
1285     };
1286 
1287     /**
1288      * parse ASN.1 Accuracy<br/>
1289      * @name getAccuracy
1290      * @memberOf KJUR.asn1.tsp.TSPParser#
1291      * @function
1292      * @param {String} h hexadecimal string of ASN.1 Accuracy
1293      * @return {Array} JSON object of Accuracy parameter
1294      * @see KJUR.asn1.tsp.Accuracy
1295      *
1296      * @description
1297      * This method parses ASN.1 Accuracy defined in RFC 3161.
1298      * <pre>
1299      * Accuracy ::= SEQUENCE {
1300      *    seconds        INTEGER              OPTIONAL,
1301      *    millis     [0] INTEGER  (1..999)    OPTIONAL,
1302      *    micros     [1] INTEGER  (1..999)    OPTIONAL  }
1303      * </pre>
1304      *
1305      * @example
1306      * parser = new KJUR.asn1.tsp.TSPParser();
1307      * parser.getAccuracy("30...") → {millis: 500}
1308      */
1309     this.getAccuracy = function(h) {
1310 	var pResult = {};
1311 
1312 	var aIdx = _getChildIdx(h, 0);
1313 
1314 	for (var i = 0; i < aIdx.length; i++) {
1315 	    var tag = h.substr(aIdx[i], 2);
1316 	    var hV = _getV(h, aIdx[i]);
1317 	    var iV = parseInt(hV, 16);
1318 
1319 	    if (tag == "02") {
1320 		pResult.seconds = iV;
1321 	    } else if (tag == "80") {
1322 		pResult.millis = iV;
1323 	    } else if (tag == "81") {
1324 		pResult.micros = iV;
1325 	    }
1326 	}
1327 
1328 	return pResult;
1329     };
1330 
1331     /**
1332      * parse ASN.1 MessageImprint<br/>
1333      * @name getMessageImprint
1334      * @memberOf KJUR.asn1.tsp.TSPParser#
1335      * @function
1336      * @param {String} h hexadecimal string of ASN.1 MessageImprint
1337      * @return {Array} JSON object of MessageImprint parameter
1338      * @see KJUR.asn1.tsp.MessageImprint
1339      *
1340      * @description
1341      * This method parses ASN.1 MessageImprint defined in RFC 3161.
1342      *
1343      * @example
1344      * parser = new KJUR.asn1.tsp.TSPParser();
1345      * parser.getMessageImprint("30...") → 
1346      * { alg: "sha256", hash: "12ab..." }
1347      */
1348     this.getMessageImprint = function(h) {
1349 	if (h.substr(0, 2) != "30")
1350             throw new Error("head of messageImprint hex shall be x30");
1351 
1352 	var json = {};
1353 	var idxList = _getChildIdx(h, 0);
1354 	var hashAlgOidIdx = _getIdxbyList(h, 0, [0, 0]);
1355 	var hashAlgHex = _getV(h, hashAlgOidIdx);
1356 	var hashAlgOid = _ASN1HEX.hextooidstr(hashAlgHex);
1357 	var hashAlgName = KJUR.asn1.x509.OID.oid2name(hashAlgOid);
1358 	if (hashAlgName == '')
1359             throw new Error("hashAlg name undefined: " + hashAlgOid);
1360 	var hashAlg = hashAlgName;
1361 	var hashValueIdx = _getIdxbyList(h, 0, [1]);
1362 	
1363 	json.alg = hashAlg;
1364 	json.hash = _getV(h, hashValueIdx); 
1365 
1366 	return json;
1367     };
1368 
1369     /**
1370      * parse ASN.1 PKIStatusInfo<br/>
1371      * @name getPKIStatusInfo
1372      * @memberOf KJUR.asn1.tsp.TSPParser#
1373      * @function
1374      * @param {String} h hexadecimal string of ASN.1 PKIStatusInfo
1375      * @return {Array} JSON object of PKIStatusInfo parameter
1376      * @see KJUR.asn1.tsp.PKIStatusInfo
1377      *
1378      * @description
1379      * This method parses ASN.1 PKIStatusInfo defined in RFC 3161.
1380      *
1381      * @example
1382      * parser = new KJUR.asn1.tsp.TSPParser();
1383      * parser.getPKIStatusInfo("30...") → 
1384      * { status: "rejection",
1385      *   statusstr: ["unsupported algorithm"],
1386      *   failinfo: "badAlg" }
1387      */
1388     this.getPKIStatusInfo = function(h) {
1389 	var pResult = {};
1390 	var aIdx = _getChildIdx(h, 0);
1391 	var offset = 0;
1392 
1393 	try {
1394 	    var hStatus = _getV(h, aIdx[0]);
1395 	    var iStatus = parseInt(hStatus, 16);
1396 	    pResult.status = _aSTATUSSTR[iStatus];
1397 	} catch(ex) {};
1398 
1399 	if (aIdx.length > 1 && h.substr(aIdx[1], 2) == "30") {
1400 	    var hPKIFreeText = _getTLV(h, aIdx[1]);
1401 	    pResult.statusstr = 
1402 		this.getPKIFreeText(hPKIFreeText);
1403 	    offset++;
1404 	}
1405 
1406 	if (aIdx.length > offset &&
1407 	    h.substr(aIdx[1 + offset], 2) == "03") {
1408 	    var hPKIFailureInfo = _getTLV(h, aIdx[1 + offset]);
1409 	    pResult.failinfo = 
1410 		this.getPKIFailureInfo(hPKIFailureInfo);
1411 	}
1412 
1413 	return pResult;
1414     };
1415 
1416     /**
1417      * parse ASN.1 PKIFreeText<br/>
1418      * @name getPKIFreeText
1419      * @memberOf KJUR.asn1.tsp.TSPParser#
1420      * @function
1421      * @param {String} h hexadecimal string of ASN.1 PKIFreeText
1422      * @return {Array} array of string
1423      * @since jsrsasign 10.1.3 asn1tsp 2.0.3
1424      * @see KJUR.asn1.tsp.PKIFreeText
1425      *
1426      * @description
1427      * This method parses ASN.1 PKIFreeText defined in RFC 3161.
1428      *
1429      * @example
1430      * parser = new KJUR.asn1.tsp.TSPParser();
1431      * parser.getPKIFreeText("300a0c036161610c03616161") → 
1432      * ["aaa", "aaa"]
1433      */
1434     this.getPKIFreeText = function(h) {
1435 	var aResult = [];
1436 	var aIdx = _getChildIdx(h, 0);
1437 	for (var i = 0; i < aIdx.length; i++) {
1438 	    aResult.push(_ASN1HEX.getString(h, aIdx[i]));
1439 	}
1440 	return aResult;
1441     };
1442 
1443     /**
1444      * parse ASN.1 PKIFailureInfo<br/>
1445      * @name getPKIFailureInfo
1446      * @memberOf KJUR.asn1.tsp.TSPParser#
1447      * @function
1448      * @param {String} h hexadecimal string of ASN.1 PKIFailureInfo
1449      * @return {Object} failureInfo string or number
1450      * @since jsrsasign 10.1.3 asn1tsp 2.0.3
1451      * @see KJUR.asn1.tsp.PKIFailureInfo
1452      *
1453      * @description
1454      * This method parses ASN.1 PKIFailureInfo defined in RFC 3161.
1455      *
1456      * @example
1457      * parser = new KJUR.asn1.tsp.TSPParser();
1458      * parser.getPKIFailureInfo("03020700") → "badAlg"
1459      * parser.getPKIFailureInfo("03020780") → 1
1460      * parser.getPKIFailureInfo("030203c8") → "systemFailure"
1461      */
1462     this.getPKIFailureInfo = function(h) {
1463 	var n = _ASN1HEX.getInt(h, 0);
1464 	if (_pFAILUREINFO[n] != undefined) {
1465 	    return _pFAILUREINFO[n];
1466 	} else {
1467 	    return n;
1468 	}
1469     };
1470 };
1471