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