1 /* asn1tsp-1.0.3.js (c) 2014-2017 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 7.2.1 asn1tsp 1.0.3 (2017-Jun-03)
 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 Accuracy ASN.1 object
 65  * @name KJUR.asn1.tsp.Accuracy
 66  * @class class for TSP Accuracy ASN.1 object
 67  * @param {Array} params associative array of parameters
 68  * @extends KJUR.asn1.ASN1Object
 69  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
 70  * @description
 71  * <pre>
 72  * Accuracy ::= SEQUENCE {
 73  *       seconds        INTEGER              OPTIONAL,
 74  *       millis     [0] INTEGER  (1..999)    OPTIONAL,
 75  *       micros     [1] INTEGER  (1..999)    OPTIONAL  }
 76  * </pre>
 77  * @example
 78  * o = new KJUR.asn1.tsp.Accuracy({seconds: 1,
 79  *                                 millis: 500,
 80  *                                 micros: 500});
 81  */
 82 KJUR.asn1.tsp.Accuracy = function(params) {
 83     var _KJUR = KJUR,
 84 	_KJUR_asn1 = _KJUR.asn1,
 85 	_DERInteger = _KJUR_asn1.DERInteger,
 86 	_DERSequence = _KJUR_asn1.DERSequence,
 87 	_DERTaggedObject = _KJUR_asn1.DERTaggedObject;
 88 
 89     _KJUR_asn1.tsp.Accuracy.superclass.constructor.call(this);
 90 
 91     this.seconds = null;
 92     this.millis = null;
 93     this.micros = null;
 94 
 95     this.getEncodedHex = function() {
 96         var dSeconds = null;
 97         var dTagMillis = null;
 98         var dTagMicros = null;
 99         
100         var a = [];
101         if (this.seconds != null) {
102             dSeconds = new _DERInteger({'int': this.seconds});
103             a.push(dSeconds);
104         }
105         if (this.millis != null) {
106             var dMillis = new _DERInteger({'int': this.millis});
107             dTagMillis = new _DERTaggedObject({obj: dMillis,
108                                                tag: '80',
109                                                explicit: false});
110             a.push(dTagMillis);
111         }
112         if (this.micros != null) {
113             var dMicros = new _DERInteger({'int': this.micros});
114             dTagMicros = new _DERTaggedObject({obj: dMicros,
115                                                tag: '81',
116                                                explicit: false});
117             a.push(dTagMicros);
118         }
119         var seq = new _DERSequence({array: a});
120         this.hTLV = seq.getEncodedHex();
121         return this.hTLV;
122     };
123 
124     if (params !== undefined) {
125         if (typeof params.seconds == "number") this.seconds = params.seconds;
126         if (typeof params.millis == "number") this.millis = params.millis;
127         if (typeof params.micros == "number") this.micros = params.micros;
128     }
129 };
130 YAHOO.lang.extend(KJUR.asn1.tsp.Accuracy, KJUR.asn1.ASN1Object);
131 
132 /**
133  * class for TSP MessageImprint ASN.1 object
134  * @name KJUR.asn1.tsp.MessageImprint
135  * @class class for TSP MessageImprint ASN.1 object
136  * @param {Array} params associative array of parameters
137  * @extends KJUR.asn1.ASN1Object
138  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
139  * @description
140  * <pre>
141  * MessageImprint ::= SEQUENCE  {
142  *      hashAlgorithm                AlgorithmIdentifier,
143  *      hashedMessage                OCTET STRING  }
144  * </pre>
145  * @example
146  * o = new KJUR.asn1.tsp.MessageImprint({hashAlg: 'sha1',
147  *                                       hashValue: '1f3dea...'});
148  */
149 KJUR.asn1.tsp.MessageImprint = function(params) {
150     var _KJUR = KJUR,
151 	_KJUR_asn1 = _KJUR.asn1,
152 	_DERSequence = _KJUR_asn1.DERSequence,
153 	_DEROctetString = _KJUR_asn1.DEROctetString,
154 	_KJUR_asn1_x509 = _KJUR_asn1.x509,
155 	_AlgorithmIdentifier = _KJUR_asn1_x509.AlgorithmIdentifier;
156 
157     _KJUR_asn1.tsp.MessageImprint.superclass.constructor.call(this);
158 
159     this.dHashAlg = null;
160     this.dHashValue = null;
161 
162     this.getEncodedHex = function() {
163         if (typeof this.hTLV == "string") return this.hTLV;
164         var seq = 
165             new _DERSequence({array: [this.dHashAlg, this.dHashValue]});
166         return seq.getEncodedHex();
167     };
168 
169     if (params !== undefined) {
170         if (typeof params.hashAlg == "string") {
171             this.dHashAlg = new _AlgorithmIdentifier({name: params.hashAlg});
172         } 
173         if (typeof params.hashValue == "string") {
174             this.dHashValue = new _DEROctetString({hex: params.hashValue});
175         }
176     }
177 };
178 YAHOO.lang.extend(KJUR.asn1.tsp.MessageImprint, KJUR.asn1.ASN1Object);
179 
180 /**
181  * class for TSP TimeStampReq ASN.1 object
182  * @name KJUR.asn1.tsp.TimeStampReq
183  * @class class for TSP TimeStampReq ASN.1 object
184  * @param {Array} params associative array of parameters
185  * @extends KJUR.asn1.ASN1Object
186  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
187  * @description
188  * <pre>
189  * TimeStampReq ::= SEQUENCE  {
190  *    version          INTEGER  { v1(1) },
191  *    messageImprint   MessageImprint,
192  *    reqPolicy        TSAPolicyId               OPTIONAL,
193  *    nonce            INTEGER                   OPTIONAL,
194  *    certReq          BOOLEAN                   DEFAULT FALSE,
195  *    extensions       [0] IMPLICIT Extensions   OPTIONAL  }
196  * </pre>
197  */
198 KJUR.asn1.tsp.TimeStampReq = function(params) {
199     var _KJUR = KJUR,
200 	_KJUR_asn1 = _KJUR.asn1,
201 	_DERSequence = _KJUR_asn1.DERSequence,
202 	_DERInteger = _KJUR_asn1.DERInteger,
203 	_DERBoolean = _KJUR_asn1.DERBoolean,
204 	_DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier,
205 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
206 	_MessageImprint = _KJUR_asn1_tsp.MessageImprint;
207 
208     _KJUR_asn1_tsp.TimeStampReq.superclass.constructor.call(this);
209 
210     this.dVersion = new _DERInteger({'int': 1});
211     this.dMessageImprint = null;
212     this.dPolicy = null;
213     this.dNonce = null;
214     this.certReq = true;
215 
216     this.setMessageImprint = function(params) {
217         if (params instanceof _MessageImprint) {
218             this.dMessageImprint = params;
219             return;
220         }
221         if (typeof params == "object") {
222             this.dMessageImprint = new _MessageImprint(params);
223         }
224     };
225 
226     this.getEncodedHex = function() {
227         if (this.dMessageImprint == null)
228             throw "messageImprint shall be specified";
229 
230         var a = [this.dVersion, this.dMessageImprint];
231         if (this.dPolicy != null) a.push(this.dPolicy);
232         if (this.dNonce != null)  a.push(this.dNonce);
233         if (this.certReq)         a.push(new _DERBoolean());
234 
235         var seq = new _DERSequence({array: a});
236         this.hTLV = seq.getEncodedHex();
237         return this.hTLV;
238     };
239 
240     if (params !== undefined) {
241         if (typeof params.mi == "object") {
242             this.setMessageImprint(params.mi);
243         }
244         if (typeof params.policy == "object") {
245             this.dPolicy = new _DERObjectIdentifier(params.policy);
246         }
247         if (typeof params.nonce == "object") {
248             this.dNonce = new _DERInteger(params.nonce);
249         }
250         if (typeof params.certreq == "boolean") {
251             this.certReq = params.certreq;
252         }
253     }
254 };
255 YAHOO.lang.extend(KJUR.asn1.tsp.TimeStampReq, KJUR.asn1.ASN1Object);
256 
257 /**
258  * class for TSP TSTInfo ASN.1 object
259  * @name KJUR.asn1.tsp.TSTInfo
260  * @class class for TSP TSTInfo ASN.1 object
261  * @param {Array} params associative array of parameters
262  * @extends KJUR.asn1.ASN1Object
263  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
264  * @description
265  * <pre>
266  * TSTInfo ::= SEQUENCE  {
267  *    version         INTEGER  { v1(1) },
268  *    policy          TSAPolicyId,
269  *    messageImprint  MessageImprint,
270  *    serialNumber    INTEGER, -- up to 160bit
271  *    genTime         GeneralizedTime,
272  *    accuracy        Accuracy                 OPTIONAL,
273  *    ordering        BOOLEAN                  DEFAULT FALSE,
274  *    nonce           INTEGER                  OPTIONAL,
275  *    tsa             [0] GeneralName          OPTIONAL,
276  *    extensions      [1] IMPLICIT Extensions  OPTIONAL   }
277  * </pre>
278  * @example
279  * o = new KJUR.asn1.tsp.TSTInfo({
280  *     policy:    '1.2.3.4.5',
281  *     messageImprint: {hashAlg: 'sha256', hashMsgHex: '1abc...'},
282  *     genTime:   {withMillis: true},     // OPTION
283  *     accuracy:  {micros: 500},          // OPTION
284  *     ordering:  true,                   // OPITON
285  *     nonce:     {hex: '52fab1...'},     // OPTION
286  *     tsa:       {str: '/C=US/O=TSA1'}   // OPITON
287  * });
288  */
289 KJUR.asn1.tsp.TSTInfo = function(params) {
290     var _KJUR = KJUR,
291 	_KJUR_asn1 = _KJUR.asn1,
292 	_DERSequence = _KJUR_asn1.DERSequence,
293 	_DERInteger = _KJUR_asn1.DERInteger,
294 	_DERBoolean = _KJUR_asn1.DERBoolean,
295 	_DERGeneralizedTime = _KJUR_asn1.DERGeneralizedTime,
296 	_DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier,
297 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
298 	_MessageImprint = _KJUR_asn1_tsp.MessageImprint,
299 	_Accuracy = _KJUR_asn1_tsp.Accuracy,
300         _X500Name = _KJUR_asn1.x509.X500Name;
301 
302     _KJUR_asn1_tsp.TSTInfo.superclass.constructor.call(this);
303 
304     this.dVersion = new _DERInteger({'int': 1});
305     this.dPolicy = null;
306     this.dMessageImprint = null;
307     this.dSerialNumber = null;
308     this.dGenTime = null;
309     this.dAccuracy = null;
310     this.dOrdering = null;
311     this.dNonce = null;
312     this.dTsa = null;
313 
314     this.getEncodedHex = function() {
315         var a = [this.dVersion];
316 
317         if (this.dPolicy == null) throw "policy shall be specified.";
318         a.push(this.dPolicy);
319 
320         if (this.dMessageImprint == null)
321             throw "messageImprint shall be specified.";
322         a.push(this.dMessageImprint);
323 
324         if (this.dSerialNumber == null)
325             throw "serialNumber shall be specified.";
326         a.push(this.dSerialNumber);
327 
328         if (this.dGenTime == null)
329             throw "genTime shall be specified.";
330         a.push(this.dGenTime);
331 
332         if (this.dAccuracy != null) a.push(this.dAccuracy);
333         if (this.dOrdering != null) a.push(this.dOrdering);
334         if (this.dNonce != null) a.push(this.dNonce);
335         if (this.dTsa != null) a.push(this.dTsa);
336 
337         var seq = new _DERSequence({array: a});
338         this.hTLV = seq.getEncodedHex();
339         return this.hTLV;
340     };
341 
342     if (params !== undefined) {
343         if (typeof params.policy == "string") {
344             if (! params.policy.match(/^[0-9.]+$/))
345                 throw "policy shall be oid like 0.1.4.134";
346             this.dPolicy = new _DERObjectIdentifier({oid: params.policy});
347         }
348         if (params.messageImprint !== undefined) {
349             this.dMessageImprint = new _MessageImprint(params.messageImprint);
350         }
351         if (params.serialNumber !== undefined) {
352             this.dSerialNumber = new _DERInteger(params.serialNumber);
353         }
354         if (params.genTime !== undefined) {
355             this.dGenTime = new _DERGeneralizedTime(params.genTime);
356         }
357         if (params.accuracy !== undefined) {
358             this.dAccuracy = new _Accuracy(params.accuracy);
359         }
360         if (params.ordering !== undefined &&
361             params.ordering == true) {
362             this.dOrdering = new _DERBoolean();
363         }
364         if (params.nonce !== undefined) {
365             this.dNonce = new _DERInteger(params.nonce);
366         }
367         if (params.tsa !== undefined) {
368             this.dTsa = new _X500Name(params.tsa);
369         }
370     }
371 };
372 YAHOO.lang.extend(KJUR.asn1.tsp.TSTInfo, KJUR.asn1.ASN1Object);
373 
374 /**
375  * class for TSP TimeStampResp ASN.1 object
376  * @name KJUR.asn1.tsp.TimeStampResp
377  * @class class for TSP TimeStampResp ASN.1 object
378  * @param {Array} params associative array of parameters
379  * @extends KJUR.asn1.ASN1Object
380  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
381  * @description
382  * <pre>
383  * TimeStampResp ::= SEQUENCE  {
384  *    status                  PKIStatusInfo,
385  *    timeStampToken          TimeStampToken     OPTIONAL  }
386  * </pre>
387  */
388 KJUR.asn1.tsp.TimeStampResp = function(params) {
389     var _KJUR = KJUR,
390 	_KJUR_asn1 = _KJUR.asn1,
391 	_DERSequence = _KJUR_asn1.DERSequence,
392 	_ASN1Object = _KJUR_asn1.ASN1Object,
393 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
394 	_PKIStatusInfo = _KJUR_asn1_tsp.PKIStatusInfo;
395 
396     _KJUR_asn1_tsp.TimeStampResp.superclass.constructor.call(this);
397 
398     this.dStatus = null;
399     this.dTST = null;
400 
401     this.getEncodedHex = function() {
402         if (this.dStatus == null)
403             throw "status shall be specified";
404         var a = [this.dStatus];
405         if (this.dTST != null) a.push(this.dTST);
406         var seq = new _DERSequence({array: a});
407         this.hTLV = seq.getEncodedHex();
408         return this.hTLV;
409     };
410 
411     if (params !== undefined) {
412         if (typeof params.status == "object") {
413             this.dStatus = new _PKIStatusInfo(params.status);
414         }
415         if (params.tst !== undefined &&
416             params.tst instanceof _ASN1Object) {
417             this.dTST = params.tst.getContentInfo();
418         }
419     }
420 };
421 YAHOO.lang.extend(KJUR.asn1.tsp.TimeStampResp, KJUR.asn1.ASN1Object);
422 
423 // --- BEGIN OF RFC 2510 CMP -----------------------------------------------
424 
425 /**
426  * class for TSP PKIStatusInfo ASN.1 object
427  * @name KJUR.asn1.tsp.PKIStatusInfo
428  * @class class for TSP PKIStatusInfo ASN.1 object
429  * @param {Array} params associative array of parameters
430  * @extends KJUR.asn1.ASN1Object
431  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
432  * @description
433  * <pre>
434  * PKIStatusInfo ::= SEQUENCE {
435  *    status                  PKIStatus,
436  *    statusString            PKIFreeText     OPTIONAL,
437  *    failInfo                PKIFailureInfo  OPTIONAL  }
438  * </pre>
439  */
440 KJUR.asn1.tsp.PKIStatusInfo = function(params) {
441     var _KJUR = KJUR,
442 	_KJUR_asn1 = _KJUR.asn1,
443 	_DERSequence = _KJUR_asn1.DERSequence,
444 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
445 	_PKIStatus = _KJUR_asn1_tsp.PKIStatus,
446 	_PKIFreeText = _KJUR_asn1_tsp.PKIFreeText,
447 	_PKIFailureInfo = _KJUR_asn1_tsp.PKIFailureInfo;
448 
449     _KJUR_asn1_tsp.PKIStatusInfo.superclass.constructor.call(this);
450 
451     this.dStatus = null;
452     this.dStatusString = null;
453     this.dFailureInfo = null;
454 
455     this.getEncodedHex = function() {
456         if (this.dStatus == null)
457             throw "status shall be specified";
458         var a = [this.dStatus];
459         if (this.dStatusString != null) a.push(this.dStatusString);
460         if (this.dFailureInfo != null) a.push(this.dFailureInfo);
461         var seq = new _DERSequence({array: a});
462         this.hTLV = seq.getEncodedHex();
463         return this.hTLV;
464     };
465 
466     if (params !== undefined) {
467         if (typeof params.status == "object") { // param for int
468             this.dStatus = new _PKIStatus(params.status);
469         }
470         if (typeof params.statstr == "object") { // array of str
471             this.dStatusString = 
472                 new _PKIFreeText({array: params.statstr});
473         }
474         if (typeof params.failinfo == "object") {
475             this.dFailureInfo = 
476                 new _PKIFailureInfo(params.failinfo); // param for bitstr
477         }
478     };
479 };
480 YAHOO.lang.extend(KJUR.asn1.tsp.PKIStatusInfo, KJUR.asn1.ASN1Object);
481 
482 /**
483  * class for TSP PKIStatus ASN.1 object
484  * @name KJUR.asn1.tsp.PKIStatus
485  * @class class for TSP PKIStatus ASN.1 object
486  * @param {Array} params associative array of parameters
487  * @extends KJUR.asn1.ASN1Object
488  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
489  * @description
490  * <pre>
491  * PKIStatus ::= INTEGER {
492  *    granted                (0),
493  *    grantedWithMods        (1),
494  *    rejection              (2),
495  *    waiting                (3),
496  *    revocationWarning      (4),
497  *    revocationNotification (5) }
498  * </pre>
499  */
500 KJUR.asn1.tsp.PKIStatus = function(params) {
501     var _KJUR = KJUR,
502 	_KJUR_asn1 = _KJUR.asn1,
503 	_DERInteger = _KJUR_asn1.DERInteger,
504 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
505 	_PKIStatus = _KJUR_asn1_tsp.PKIStatus;
506 
507     _KJUR_asn1_tsp.PKIStatus.superclass.constructor.call(this);
508 
509     var dStatus = null;
510 
511     this.getEncodedHex = function() {
512         this.hTLV = this.dStatus.getEncodedHex();
513         return this.hTLV;
514     };
515 
516     if (params !== undefined) {
517         if (params.name !== undefined) {
518             var list = _PKIStatus.valueList;
519             if (list[params.name] === undefined)
520                 throw "name undefined: " + params.name;
521             this.dStatus = 
522                 new _DERInteger({'int': list[params.name]});
523         } else {
524             this.dStatus = new _DERInteger(params);
525         }
526     }
527 };
528 YAHOO.lang.extend(KJUR.asn1.tsp.PKIStatus, KJUR.asn1.ASN1Object);
529 
530 KJUR.asn1.tsp.PKIStatus.valueList = {
531     granted:                0,
532     grantedWithMods:        1,
533     rejection:              2,
534     waiting:                3,
535     revocationWarning:      4,
536     revocationNotification: 5
537 };
538 
539 /**
540  * class for TSP PKIFreeText ASN.1 object
541  * @name KJUR.asn1.tsp.PKIFreeText
542  * @class class for TSP PKIFreeText ASN.1 object
543  * @param {Array} params associative array of parameters
544  * @extends KJUR.asn1.ASN1Object
545  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
546  * @description
547  * <pre>
548  * PKIFreeText ::= SEQUENCE {
549  *    SIZE (1..MAX) OF UTF8String }
550  * </pre>
551  */
552 KJUR.asn1.tsp.PKIFreeText = function(params) {
553     var _KJUR = KJUR,
554 	_KJUR_asn1 = _KJUR.asn1,
555 	_DERSequence = _KJUR_asn1.DERSequence,
556 	_DERUTF8String = _KJUR_asn1.DERUTF8String,
557 	_KJUR_asn1_tsp = _KJUR_asn1.tsp;
558 
559     _KJUR_asn1_tsp.PKIFreeText.superclass.constructor.call(this);
560 
561     this.textList = [];
562 
563     this.getEncodedHex = function() {
564         var a = [];
565         for (var i = 0; i < this.textList.length; i++) {
566             a.push(new _DERUTF8String({str: this.textList[i]}));
567         }
568         var seq = new _DERSequence({array: a});
569         this.hTLV = seq.getEncodedHex();
570         return this.hTLV;
571     };
572 
573     if (params !== undefined) {
574         if (typeof params.array == "object") {
575             this.textList = params.array;
576         }
577     }
578 };
579 YAHOO.lang.extend(KJUR.asn1.tsp.PKIFreeText, KJUR.asn1.ASN1Object);
580 
581 /**
582  * class for TSP PKIFailureInfo ASN.1 object
583  * @name KJUR.asn1.tsp.PKIFailureInfo
584  * @class class for TSP PKIFailureInfo ASN.1 object
585  * @param {Array} params associative array of parameters
586  * @extends KJUR.asn1.ASN1Object
587  * @since jsrsasign 4.6.0 asn1tsp 1.0.0
588  * @description
589  * <pre>
590  * PKIFailureInfo ::= BIT STRING {
591  *    badAlg                 (0),
592  *    badRequest             (2),
593  *    badDataFormat          (5),
594  *    timeNotAvailable       (14),
595  *    unacceptedPolicy       (15),
596  *    unacceptedExtension    (16),
597  *    addInfoNotAvailable    (17),
598  *    systemFailure          (25) }
599  * </pre>
600  */
601 KJUR.asn1.tsp.PKIFailureInfo = function(params) {
602     var _KJUR = KJUR,
603 	_KJUR_asn1 = _KJUR.asn1,
604 	_DERBitString = _KJUR_asn1.DERBitString,
605 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
606 	_PKIFailureInfo = _KJUR_asn1_tsp.PKIFailureInfo;
607 
608     _PKIFailureInfo.superclass.constructor.call(this);
609 
610     this.value = null;
611 
612     this.getEncodedHex = function() {
613         if (this.value == null)
614             throw "value shall be specified";
615         var binValue = new Number(this.value).toString(2);
616         var dValue = new _DERBitString();
617         dValue.setByBinaryString(binValue);
618         this.hTLV = dValue.getEncodedHex();
619         return this.hTLV;
620     };
621 
622     if (params !== undefined) {
623         if (typeof params.name == "string") {
624             var list = _PKIFailureInfo.valueList;
625             if (list[params.name] === undefined)
626                 throw "name undefined: " + params.name;
627             this.value = list[params.name];
628         } else if (typeof params['int'] == "number") {
629             this.value = params['int'];
630         }
631     }
632 };
633 YAHOO.lang.extend(KJUR.asn1.tsp.PKIFailureInfo, KJUR.asn1.ASN1Object);
634 
635 KJUR.asn1.tsp.PKIFailureInfo.valueList = {
636     badAlg:                 0,
637     badRequest:             2,
638     badDataFormat:          5,
639     timeNotAvailable:       14,
640     unacceptedPolicy:       15,
641     unacceptedExtension:    16,
642     addInfoNotAvailable:    17,
643     systemFailure:          25
644 };
645 
646 // --- END OF RFC 2510 CMP -------------------------------------------
647 
648 /**
649  * abstract class for TimeStampToken generator
650  * @name KJUR.asn1.tsp.AbstractTSAAdapter
651  * @class abstract class for TimeStampToken generator
652  * @param {Array} params associative array of parameters
653  * @since jsrsasign 4.7.0 asn1tsp 1.0.1
654  * @description
655  */
656 KJUR.asn1.tsp.AbstractTSAAdapter = function(params) {
657     this.getTSTHex = function(msgHex, hashAlg) {
658         throw "not implemented yet";
659     };
660 };
661 
662 /**
663  * class for simple TimeStampToken generator
664  * @name KJUR.asn1.tsp.SimpleTSAAdapter
665  * @class class for simple TimeStampToken generator
666  * @param {Array} params associative array of parameters
667  * @since jsrsasign 4.7.0 asn1tsp 1.0.1
668  * @description
669  */
670 KJUR.asn1.tsp.SimpleTSAAdapter = function(initParams) {
671     var _KJUR = KJUR,
672 	_KJUR_asn1 = _KJUR.asn1,
673 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
674 	_hashHex = _KJUR.crypto.Util.hashHex;
675 
676     _KJUR_asn1_tsp.SimpleTSAAdapter.superclass.constructor.call(this);
677     this.params = null;
678     this.serial = 0;
679 
680     this.getTSTHex = function(msgHex, hashAlg) {
681         // messageImprint
682         var hashHex = _hashHex(msgHex, hashAlg);
683         this.params.tstInfo.messageImprint =
684             {hashAlg: hashAlg, hashValue: hashHex};
685 
686         // serial
687         this.params.tstInfo.serialNumber = {'int': this.serial++};
688 
689         // nonce
690         var nonceValue = Math.floor(Math.random() * 1000000000);
691         this.params.tstInfo.nonce = {'int': nonceValue};
692 
693         var obj = 
694             _KJUR_asn1_tsp.TSPUtil.newTimeStampToken(this.params);
695         return obj.getContentInfoEncodedHex();
696     };
697 
698     if (initParams !== undefined) {
699         this.params = initParams;
700     }
701 };
702 YAHOO.lang.extend(KJUR.asn1.tsp.SimpleTSAAdapter,
703                   KJUR.asn1.tsp.AbstractTSAAdapter);
704 
705 /**
706  * class for fixed TimeStampToken generator
707  * @name KJUR.asn1.tsp.FixedTSAAdapter
708  * @class class for fixed TimeStampToken generator
709  * @param {Array} params associative array of parameters
710  * @since jsrsasign 4.7.0 asn1tsp 1.0.1
711  * @description
712  * This class generates fixed TimeStampToken except messageImprint
713  * for testing purpose.
714  * General TSA generates TimeStampToken which varies following
715  * fields:
716  * <ul>
717  * <li>genTime</li>
718  * <li>serialNumber</li>
719  * <li>nonce</li>
720  * </ul>
721  * Those values are provided by initial parameters.
722  */
723 KJUR.asn1.tsp.FixedTSAAdapter = function(initParams) {
724     var _KJUR = KJUR,
725 	_KJUR_asn1 = _KJUR.asn1,
726 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
727 	_hashHex = _KJUR.crypto.Util.hashHex; //o
728 
729     _KJUR_asn1_tsp.FixedTSAAdapter.superclass.constructor.call(this);
730     this.params = null;
731 
732     this.getTSTHex = function(msgHex, hashAlg) {
733         // fixed serialNumber
734         // fixed nonce        
735         var hashHex = _hashHex(msgHex, hashAlg);
736         this.params.tstInfo.messageImprint =
737             {hashAlg: hashAlg, hashValue: hashHex};
738         var obj = 
739             _KJUR_asn1_tsp.TSPUtil.newTimeStampToken(this.params);
740         return obj.getContentInfoEncodedHex();
741     };
742 
743     if (initParams !== undefined) {
744         this.params = initParams;
745     }
746 };
747 YAHOO.lang.extend(KJUR.asn1.tsp.FixedTSAAdapter,
748                   KJUR.asn1.tsp.AbstractTSAAdapter);
749 
750 // --- TSP utilities -------------------------------------------------
751 
752 /**
753  * TSP utiliteis class
754  * @name KJUR.asn1.tsp.TSPUtil
755  * @class TSP utilities class
756  */
757 KJUR.asn1.tsp.TSPUtil = new function() {
758 };
759 /**
760  * generate TimeStampToken ASN.1 object specified by JSON parameters
761  * @name newTimeStampToken
762  * @memberOf KJUR.asn1.tsp.TSPUtil
763  * @function
764  * @param {Array} param JSON parameter to generate TimeStampToken
765  * @return {KJUR.asn1.cms.SignedData} object just generated
766  * @description
767  * @example
768  */
769 KJUR.asn1.tsp.TSPUtil.newTimeStampToken = function(param) {
770     var _KJUR = KJUR,
771 	_KJUR_asn1 = _KJUR.asn1,
772 	_KJUR_asn1_cms = _KJUR_asn1.cms,
773 	_KJUR_asn1_tsp = _KJUR_asn1.tsp,
774 	_TSTInfo = _KJUR_asn1.tsp.TSTInfo;
775 
776     var sd = new _KJUR_asn1_cms.SignedData();
777 
778     var dTSTInfo = new _TSTInfo(param.tstInfo);
779     var tstInfoHex = dTSTInfo.getEncodedHex();
780     sd.dEncapContentInfo.setContentValue({hex: tstInfoHex});
781     sd.dEncapContentInfo.setContentType('tstinfo');
782 
783     if (typeof param.certs == "object") {
784         for (var i = 0; i < param.certs.length; i++) {
785             sd.addCertificatesByPEM(param.certs[i]);
786         }
787     }
788 
789     var si = sd.signerInfoList[0];
790     si.setSignerIdentifier(param.signerCert);
791     si.setForContentAndHash({sdObj: sd,
792                              eciObj: sd.dEncapContentInfo,
793                              hashAlg: param.hashAlg});
794     var signingCertificate = 
795         new _KJUR_asn1_cms.SigningCertificate({array: [param.signerCert]});
796     si.dSignedAttrs.add(signingCertificate);
797 
798     si.sign(param.signerPrvKey, param.sigAlg);
799 
800     return sd;
801 };
802 
803 /**
804  * parse hexadecimal string of TimeStampReq
805  * @name parseTimeStampReq
806  * @memberOf KJUR.asn1.tsp.TSPUtil
807  * @function
808  * @param {String} hexadecimal string of TimeStampReq
809  * @return {Array} JSON object of parsed parameters
810  * @description
811  * This method parses a hexadecimal string of TimeStampReq
812  * and returns parsed their fields:
813  * @example
814  * var json = KJUR.asn1.tsp.TSPUtil.parseTimeStampReq("302602...");
815  * // resulted DUMP of above 'json':
816  * {mi: {hashAlg: 'sha256',          // MessageImprint hashAlg
817  *       hashValue: 'a1a2a3a4...'},  // MessageImprint hashValue
818  *  policy: '1.2.3.4.5',             // tsaPolicy (OPTION)
819  *  nonce: '9abcf318...',            // nonce (OPTION)
820  *  certreq: true}                   // certReq (OPTION)
821  */
822 KJUR.asn1.tsp.TSPUtil.parseTimeStampReq = function(reqHex) {
823     var _ASN1HEX = ASN1HEX;
824     var _getChildIdx = _ASN1HEX.getChildIdx;
825     var _getV = _ASN1HEX.getV;
826     var _getTLV = _ASN1HEX.getTLV;
827     var json = {};
828     json.certreq = false;
829 
830     var idxList = _getChildIdx(reqHex, 0);
831 
832     if (idxList.length < 2)
833         throw "TimeStampReq must have at least 2 items";
834 
835     var miHex = _getTLV(reqHex, idxList[1]);
836     json.mi = KJUR.asn1.tsp.TSPUtil.parseMessageImprint(miHex); 
837 
838     for (var i = 2; i < idxList.length; i++) {
839         var idx = idxList[i];
840         var tag = reqHex.substr(idx, 2);
841         if (tag == "06") { // case OID
842             var policyHex = _getV(reqHex, idx);
843             json.policy = _ASN1HEX.hextooidstr(policyHex);
844         }
845         if (tag == "02") { // case INTEGER
846             json.nonce = _getV(reqHex, idx);
847         }
848         if (tag == "01") { // case BOOLEAN
849             json.certreq = true;
850         }
851     }
852 
853     return json;
854 };
855 
856 /**
857  * parse hexadecimal string of MessageImprint
858  * @name parseMessageImprint
859  * @memberOf KJUR.asn1.tsp.TSPUtil
860  * @function
861  * @param {String} hexadecimal string of MessageImprint
862  * @return {Array} JSON object of parsed parameters
863  * @description
864  * This method parses a hexadecimal string of MessageImprint
865  * and returns parsed their fields:
866  * @example
867  * var json = KJUR.asn1.tsp.TSPUtil.parseMessageImprint("302602...");
868  * // resulted DUMP of above 'json':
869  * {hashAlg: 'sha256',          // MessageImprint hashAlg
870  *  hashValue: 'a1a2a3a4...'}   // MessageImprint hashValue
871  */
872 KJUR.asn1.tsp.TSPUtil.parseMessageImprint = function(miHex) {
873     var _ASN1HEX = ASN1HEX;
874     var _getChildIdx = _ASN1HEX.getChildIdx;
875     var _getV = _ASN1HEX.getV;
876     var _getIdxbyList = _ASN1HEX.getIdxbyList;
877     var json = {};
878 
879     if (miHex.substr(0, 2) != "30")
880         throw "head of messageImprint hex shall be '30'";
881 
882     var idxList = _getChildIdx(miHex, 0);
883     var hashAlgOidIdx = _getIdxbyList(miHex, 0, [0, 0]);
884     var hashAlgHex = _getV(miHex, hashAlgOidIdx);
885     var hashAlgOid = _ASN1HEX.hextooidstr(hashAlgHex);
886     var hashAlgName = KJUR.asn1.x509.OID.oid2name(hashAlgOid);
887     if (hashAlgName == '')
888         throw "hashAlg name undefined: " + hashAlgOid;
889     var hashAlg = hashAlgName;
890     var hashValueIdx = _getIdxbyList(miHex, 0, [1]);
891 
892     json.hashAlg = hashAlg;
893     json.hashValue = _getV(miHex, hashValueIdx); 
894 
895     return json;
896 };
897 
898