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
|
import * as asn1js from "asn1js";
import * as pvtsutils from "pvtsutils";
import * as pvutils from "pvutils";
import * as common from "./common";
import { MessageImprint, HASHED_MESSAGE, HASH_ALGORITHM, MessageImprintSchema, MessageImprintJson } from "./MessageImprint";
import { Accuracy, AccuracyJson, AccuracySchema, MICROS, MILLIS, SECONDS } from "./Accuracy";
import { GeneralName, GeneralNameJson, GeneralNameSchema, TYPE, VALUE } from "./GeneralName";
import { Extension, ExtensionJson, ExtensionSchema } from "./Extension";
import * as Schema from "./Schema";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import { AsnError } from "./errors";
import { EMPTY_STRING } from "./constants";
const VERSION = "version";
const POLICY = "policy";
const MESSAGE_IMPRINT = "messageImprint";
const SERIAL_NUMBER = "serialNumber";
const GEN_TIME = "genTime";
const ORDERING = "ordering";
const NONCE = "nonce";
const ACCURACY = "accuracy";
const TSA = "tsa";
const EXTENSIONS = "extensions";
const TST_INFO = "TSTInfo";
const TST_INFO_VERSION = `${TST_INFO}.${VERSION}`;
const TST_INFO_POLICY = `${TST_INFO}.${POLICY}`;
const TST_INFO_MESSAGE_IMPRINT = `${TST_INFO}.${MESSAGE_IMPRINT}`;
const TST_INFO_SERIAL_NUMBER = `${TST_INFO}.${SERIAL_NUMBER}`;
const TST_INFO_GEN_TIME = `${TST_INFO}.${GEN_TIME}`;
const TST_INFO_ACCURACY = `${TST_INFO}.${ACCURACY}`;
const TST_INFO_ORDERING = `${TST_INFO}.${ORDERING}`;
const TST_INFO_NONCE = `${TST_INFO}.${NONCE}`;
const TST_INFO_TSA = `${TST_INFO}.${TSA}`;
const TST_INFO_EXTENSIONS = `${TST_INFO}.${EXTENSIONS}`;
const CLEAR_PROPS = [
TST_INFO_VERSION,
TST_INFO_POLICY,
TST_INFO_MESSAGE_IMPRINT,
TST_INFO_SERIAL_NUMBER,
TST_INFO_GEN_TIME,
TST_INFO_ACCURACY,
TST_INFO_ORDERING,
TST_INFO_NONCE,
TST_INFO_TSA,
TST_INFO_EXTENSIONS
];
export interface ITSTInfo {
/**
* Version of the time-stamp token.
*
* Conforming time-stamping servers MUST be able to provide version 1 time-stamp tokens.
*/
version: number;
/**
* TSA's policy under which the response was produced.
*
* If a similar field was present in the TimeStampReq, then it MUST have the same value,
* otherwise an error (unacceptedPolicy) MUST be returned
*/
policy: string;
/**
* The messageImprint MUST have the same value as the similar field in
* TimeStampReq, provided that the size of the hash value matches the
* expected size of the hash algorithm identified in hashAlgorithm.
*/
messageImprint: MessageImprint;
/**
* Integer assigned by the TSA to each TimeStampToken.
*
* It MUST be unique for each TimeStampToken issued by a given TSA.
*/
serialNumber: asn1js.Integer;
/**
* Time at which the time-stamp token has been created by the TSA
*/
genTime: Date;
/**
* Represents the time deviation around the UTC time contained in GeneralizedTime
*/
accuracy?: Accuracy;
/**
* If the ordering field is missing, or if the ordering field is present
* and set to false, then the genTime field only indicates the time at
* which the time-stamp token has been created by the TSA.In such a
* case, the ordering of time-stamp tokens issued by the same TSA or
* different TSAs is only possible when the difference between the
* genTime of the first time-stamp token and the genTime of the second
* time-stamp token is greater than the sum of the accuracies of the
* genTime for each time-stamp token.
*
* If the ordering field is present and set to true, every time-stamp
* token from the same TSA can always be ordered based on the genTime
* field, regardless of the genTime accuracy.
*/
ordering?: boolean;
/**
* Field MUST be present if it was present in the TimeStampReq.
* In such a case it MUST equal the value provided in the TimeStampReq structure.
*/
nonce?: asn1js.Integer;
/**
* `tsa` field is to give a hint in identifying the name of the TSA.
* If present, it MUST correspond to one of the subject names included
* in the certificate that is to be used to verify the token.
*/
tsa?: GeneralName;
/**
* Additional information in the future. Extensions is defined in [RFC2459](https://datatracker.ietf.org/doc/html/rfc2459)
*/
extensions?: Extension[];
}
export interface TSTInfoJson {
version: number;
policy: string;
messageImprint: MessageImprintJson;
serialNumber: asn1js.IntegerJson;
genTime: Date;
accuracy?: AccuracyJson;
ordering?: boolean;
nonce?: asn1js.IntegerJson;
tsa?: GeneralNameJson;
extensions?: ExtensionJson[];
}
export type TSTInfoParameters = PkiObjectParameters & Partial<ITSTInfo>;
export interface TSTInfoVerifyParams {
data: ArrayBuffer;
notBefore?: Date;
notAfter?: Date;
}
/**
* Represents the TSTInfo structure described in [RFC3161](https://www.ietf.org/rfc/rfc3161.txt)
*/
export class TSTInfo extends PkiObject implements ITSTInfo {
public static override CLASS_NAME = "TSTInfo";
public version!: number;
public policy!: string;
public messageImprint!: MessageImprint;
public serialNumber!: asn1js.Integer;
public genTime!: Date;
public accuracy?: Accuracy;
public ordering?: boolean;
public nonce?: asn1js.Integer;
public tsa?: GeneralName;
public extensions?: Extension[];
/**
* Initializes a new instance of the {@link TSTInfo} class
* @param parameters Initialization parameters
*/
constructor(parameters: TSTInfoParameters = {}) {
super();
this.version = pvutils.getParametersValue(parameters, VERSION, TSTInfo.defaultValues(VERSION));
this.policy = pvutils.getParametersValue(parameters, POLICY, TSTInfo.defaultValues(POLICY));
this.messageImprint = pvutils.getParametersValue(parameters, MESSAGE_IMPRINT, TSTInfo.defaultValues(MESSAGE_IMPRINT));
this.serialNumber = pvutils.getParametersValue(parameters, SERIAL_NUMBER, TSTInfo.defaultValues(SERIAL_NUMBER));
this.genTime = pvutils.getParametersValue(parameters, GEN_TIME, TSTInfo.defaultValues(GEN_TIME));
if (ACCURACY in parameters) {
this.accuracy = pvutils.getParametersValue(parameters, ACCURACY, TSTInfo.defaultValues(ACCURACY));
}
if (ORDERING in parameters) {
this.ordering = pvutils.getParametersValue(parameters, ORDERING, TSTInfo.defaultValues(ORDERING));
}
if (NONCE in parameters) {
this.nonce = pvutils.getParametersValue(parameters, NONCE, TSTInfo.defaultValues(NONCE));
}
if (TSA in parameters) {
this.tsa = pvutils.getParametersValue(parameters, TSA, TSTInfo.defaultValues(TSA));
}
if (EXTENSIONS in parameters) {
this.extensions = pvutils.getParametersValue(parameters, EXTENSIONS, TSTInfo.defaultValues(EXTENSIONS));
}
if (parameters.schema) {
this.fromSchema(parameters.schema);
}
}
/**
* Returns default values for all class members
* @param memberName String name for a class member
* @returns Default value
*/
public static override defaultValues(memberName: typeof VERSION): number;
public static override defaultValues(memberName: typeof POLICY): string;
public static override defaultValues(memberName: typeof MESSAGE_IMPRINT): MessageImprint;
public static override defaultValues(memberName: typeof SERIAL_NUMBER): asn1js.Integer;
public static override defaultValues(memberName: typeof GEN_TIME): Date;
public static override defaultValues(memberName: typeof ACCURACY): Accuracy;
public static override defaultValues(memberName: typeof ORDERING): boolean;
public static override defaultValues(memberName: typeof NONCE): asn1js.Integer;
public static override defaultValues(memberName: typeof TSA): GeneralName;
public static override defaultValues(memberName: typeof EXTENSIONS): Extension[];
public static override defaultValues(memberName: string): any {
switch (memberName) {
case VERSION:
return 0;
case POLICY:
return EMPTY_STRING;
case MESSAGE_IMPRINT:
return new MessageImprint();
case SERIAL_NUMBER:
return new asn1js.Integer();
case GEN_TIME:
return new Date(0, 0, 0);
case ACCURACY:
return new Accuracy();
case ORDERING:
return false;
case NONCE:
return new asn1js.Integer();
case TSA:
return new GeneralName();
case EXTENSIONS:
return [];
default:
return super.defaultValues(memberName);
}
}
/**
* Compare values with default values for all class members
* @param memberName String name for a class member
* @param memberValue Value to compare with default value
*/
public static compareWithDefault(memberName: string, memberValue: any): boolean {
switch (memberName) {
case VERSION:
case POLICY:
case GEN_TIME:
case ORDERING:
return (memberValue === TSTInfo.defaultValues(ORDERING));
case MESSAGE_IMPRINT:
return ((MessageImprint.compareWithDefault(HASH_ALGORITHM, memberValue.hashAlgorithm)) &&
(MessageImprint.compareWithDefault(HASHED_MESSAGE, memberValue.hashedMessage)));
case SERIAL_NUMBER:
case NONCE:
return (memberValue.isEqual(TSTInfo.defaultValues(NONCE)));
case ACCURACY:
return ((Accuracy.compareWithDefault(SECONDS, memberValue.seconds)) &&
(Accuracy.compareWithDefault(MILLIS, memberValue.millis)) &&
(Accuracy.compareWithDefault(MICROS, memberValue.micros)));
case TSA:
return ((GeneralName.compareWithDefault(TYPE, memberValue.type)) &&
(GeneralName.compareWithDefault(VALUE, memberValue.value)));
case EXTENSIONS:
return (memberValue.length === 0);
default:
return super.defaultValues(memberName);
}
}
/**
* @inheritdoc
* @asn ASN.1 schema
* ```asn
* TSTInfo ::= SEQUENCE {
* version INTEGER { v1(1) },
* policy TSAPolicyId,
* messageImprint MessageImprint,
* serialNumber INTEGER,
* genTime GeneralizedTime,
* accuracy Accuracy OPTIONAL,
* ordering BOOLEAN DEFAULT FALSE,
* nonce INTEGER OPTIONAL,
* tsa [0] GeneralName OPTIONAL,
* extensions [1] IMPLICIT Extensions OPTIONAL }
*```
*/
public static override schema(parameters: Schema.SchemaParameters<{
version?: string;
policy?: string;
messageImprint?: MessageImprintSchema;
serialNumber?: string;
genTime?: string;
accuracy?: AccuracySchema;
ordering?: string;
nonce?: string;
tsa?: GeneralNameSchema;
extensions?: string;
extension?: ExtensionSchema;
}> = {}): Schema.SchemaType {
const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
return (new asn1js.Sequence({
name: (names.blockName || TST_INFO),
value: [
new asn1js.Integer({ name: (names.version || TST_INFO_VERSION) }),
new asn1js.ObjectIdentifier({ name: (names.policy || TST_INFO_POLICY) }),
MessageImprint.schema(names.messageImprint || {
names: {
blockName: TST_INFO_MESSAGE_IMPRINT
}
}),
new asn1js.Integer({ name: (names.serialNumber || TST_INFO_SERIAL_NUMBER) }),
new asn1js.GeneralizedTime({ name: (names.genTime || TST_INFO_GEN_TIME) }),
Accuracy.schema(names.accuracy || {
names: {
blockName: TST_INFO_ACCURACY
}
}),
new asn1js.Boolean({
name: (names.ordering || TST_INFO_ORDERING),
optional: true
}),
new asn1js.Integer({
name: (names.nonce || TST_INFO_NONCE),
optional: true
}),
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: [GeneralName.schema(names.tsa || {
names: {
blockName: TST_INFO_TSA
}
})]
}),
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 1 // [1]
},
value: [
new asn1js.Repeated({
name: (names.extensions || TST_INFO_EXTENSIONS),
value: Extension.schema(names.extension || {})
})
]
}) // IMPLICIT Extensions
]
}));
}
public fromSchema(schema: Schema.SchemaType): void {
// Clear input data first
pvutils.clearProps(schema, CLEAR_PROPS);
// Check the schema is valid
const asn1 = asn1js.compareSchema(schema,
schema,
TSTInfo.schema()
);
AsnError.assertSchema(asn1, this.className);
// Get internal properties from parsed schema
this.version = asn1.result[TST_INFO_VERSION].valueBlock.valueDec;
this.policy = asn1.result[TST_INFO_POLICY].valueBlock.toString();
this.messageImprint = new MessageImprint({ schema: asn1.result[TST_INFO_MESSAGE_IMPRINT] });
this.serialNumber = asn1.result[TST_INFO_SERIAL_NUMBER];
this.genTime = asn1.result[TST_INFO_GEN_TIME].toDate();
if (TST_INFO_ACCURACY in asn1.result)
this.accuracy = new Accuracy({ schema: asn1.result[TST_INFO_ACCURACY] });
if (TST_INFO_ORDERING in asn1.result)
this.ordering = asn1.result[TST_INFO_ORDERING].valueBlock.value;
if (TST_INFO_NONCE in asn1.result)
this.nonce = asn1.result[TST_INFO_NONCE];
if (TST_INFO_TSA in asn1.result)
this.tsa = new GeneralName({ schema: asn1.result[TST_INFO_TSA] });
if (TST_INFO_EXTENSIONS in asn1.result)
this.extensions = Array.from(asn1.result[TST_INFO_EXTENSIONS], element => new Extension({ schema: element }));
}
public toSchema(): asn1js.Sequence {
//#region Create array for output sequence
const outputArray = [];
outputArray.push(new asn1js.Integer({ value: this.version }));
outputArray.push(new asn1js.ObjectIdentifier({ value: this.policy }));
outputArray.push(this.messageImprint.toSchema());
outputArray.push(this.serialNumber);
outputArray.push(new asn1js.GeneralizedTime({ valueDate: this.genTime }));
if (this.accuracy)
outputArray.push(this.accuracy.toSchema());
if (this.ordering !== undefined)
outputArray.push(new asn1js.Boolean({ value: this.ordering }));
if (this.nonce)
outputArray.push(this.nonce);
if (this.tsa) {
outputArray.push(new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: [this.tsa.toSchema()]
}));
}
//#region Create array of extensions
if (this.extensions) {
outputArray.push(new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 1 // [1]
},
value: Array.from(this.extensions, o => o.toSchema())
}));
}
//#endregion
//#endregion
//#region Construct and return new ASN.1 schema for this object
return (new asn1js.Sequence({
value: outputArray
}));
//#endregion
}
public toJSON(): TSTInfoJson {
const res: TSTInfoJson = {
version: this.version,
policy: this.policy,
messageImprint: this.messageImprint.toJSON(),
serialNumber: this.serialNumber.toJSON(),
genTime: this.genTime
};
if (this.accuracy)
res.accuracy = this.accuracy.toJSON();
if (this.ordering !== undefined)
res.ordering = this.ordering;
if (this.nonce)
res.nonce = this.nonce.toJSON();
if (this.tsa)
res.tsa = this.tsa.toJSON();
if (this.extensions)
res.extensions = Array.from(this.extensions, o => o.toJSON());
return res;
}
/**
* Verify current TST Info value
* @param params Input parameters
* @param crypto Crypto engine
*/
public async verify(params: TSTInfoVerifyParams, crypto = common.getCrypto(true)): Promise<boolean> {
//#region Get initial parameters
if (!params.data) {
throw new Error("\"data\" is a mandatory attribute for TST_INFO verification");
}
const data = params.data;
//#endregion
//#region Check date
if (params.notBefore) {
if (this.genTime < params.notBefore)
throw new Error("Generation time for TSTInfo object is less than notBefore value");
}
if (params.notAfter) {
if (this.genTime > params.notAfter)
throw new Error("Generation time for TSTInfo object is more than notAfter value");
}
//#endregion
// Find hashing algorithm
const shaAlgorithm = crypto.getAlgorithmByOID(this.messageImprint.hashAlgorithm.algorithmId, true, "MessageImprint.hashAlgorithm");
// Calculate message digest for input "data" buffer
const hash = await crypto.digest(shaAlgorithm.name, new Uint8Array(data));
return pvtsutils.BufferSourceConverter.isEqual(hash, this.messageImprint.hashedMessage.valueBlock.valueHexView);
}
}
|