/
TSTInfo.js
455 lines (425 loc) · 14 KB
/
TSTInfo.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
import * as asn1js from "asn1js";
import { getParametersValue, isEqualBuffer, clearProps } from "pvutils";
import { getCrypto, getAlgorithmByOID } from "./common.js";
import MessageImprint from "./MessageImprint.js";
import Accuracy from "./Accuracy.js";
import GeneralName from "./GeneralName.js";
import Extension from "./Extension.js";
//**************************************************************************************
/**
* Class from RFC3161
*/
export default class TSTInfo
{
//**********************************************************************************
/**
* Constructor for TSTInfo class
* @param {Object} [parameters={}]
* @param {Object} [parameters.schema] asn1js parsed value to initialize the class from
*/
constructor(parameters = {})
{
//region Internal properties of the object
/**
* @type {number}
* @desc version
*/
this.version = getParametersValue(parameters, "version", TSTInfo.defaultValues("version"));
/**
* @type {string}
* @desc policy
*/
this.policy = getParametersValue(parameters, "policy", TSTInfo.defaultValues("policy"));
/**
* @type {MessageImprint}
* @desc messageImprint
*/
this.messageImprint = getParametersValue(parameters, "messageImprint", TSTInfo.defaultValues("messageImprint"));
/**
* @type {Integer}
* @desc serialNumber
*/
this.serialNumber = getParametersValue(parameters, "serialNumber", TSTInfo.defaultValues("serialNumber"));
/**
* @type {Date}
* @desc genTime
*/
this.genTime = getParametersValue(parameters, "genTime", TSTInfo.defaultValues("genTime"));
if("accuracy" in parameters)
/**
* @type {Accuracy}
* @desc accuracy
*/
this.accuracy = getParametersValue(parameters, "accuracy", TSTInfo.defaultValues("accuracy"));
if("ordering" in parameters)
/**
* @type {boolean}
* @desc ordering
*/
this.ordering = getParametersValue(parameters, "ordering", TSTInfo.defaultValues("ordering"));
if("nonce" in parameters)
/**
* @type {Integer}
* @desc nonce
*/
this.nonce = getParametersValue(parameters, "nonce", TSTInfo.defaultValues("nonce"));
if("tsa" in parameters)
/**
* @type {GeneralName}
* @desc tsa
*/
this.tsa = getParametersValue(parameters, "tsa", TSTInfo.defaultValues("tsa"));
if("extensions" in parameters)
/**
* @type {Array.<Extension>}
* @desc extensions
*/
this.extensions = getParametersValue(parameters, "extensions", TSTInfo.defaultValues("extensions"));
//endregion
//region If input argument array contains "schema" for this object
if("schema" in parameters)
this.fromSchema(parameters.schema);
//endregion
}
//**********************************************************************************
/**
* Return default values for all class members
* @param {string} memberName String name for a class member
*/
static defaultValues(memberName)
{
switch(memberName)
{
case "version":
return 0;
case "policy":
return "";
case "messageImprint":
return new MessageImprint();
case "serialNumber":
return new asn1js.Integer();
case "genTime":
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:
throw new Error(`Invalid member name for TSTInfo class: ${memberName}`);
}
}
//**********************************************************************************
/**
* Compare values with default values for all class members
* @param {string} memberName String name for a class member
* @param {*} memberValue Value to compare with default value
*/
static compareWithDefault(memberName, memberValue)
{
switch(memberName)
{
case "version":
case "policy":
case "genTime":
case "ordering":
return (memberValue === TSTInfo.defaultValues(memberName));
case "messageImprint":
return ((MessageImprint.compareWithDefault("hashAlgorithm", memberValue.hashAlgorithm)) &&
(MessageImprint.compareWithDefault("hashedMessage", memberValue.hashedMessage)));
case "serialNumber":
case "nonce":
return (memberValue.isEqual(TSTInfo.defaultValues(memberName)));
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:
throw new Error(`Invalid member name for TSTInfo class: ${memberName}`);
}
}
//**********************************************************************************
/**
* Return value of pre-defined ASN.1 schema for current class
*
* ASN.1 schema:
* ```asn1
* 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 }
* ```
*
* @param {Object} parameters Input parameters for the schema
* @returns {Object} asn1js schema object
*/
static schema(parameters = {})
{
/**
* @type {Object}
* @property {string} [blockName]
* @property {string} [version]
* @property {string} [policy]
* @property {string} [messageImprint]
* @property {string} [serialNumber]
* @property {string} [genTime]
* @property {string} [accuracy]
* @property {string} [ordering]
* @property {string} [nonce]
* @property {string} [tsa]
* @property {string} [extensions]
*/
const names = getParametersValue(parameters, "names", {});
return (new asn1js.Sequence({
name: (names.blockName || "TSTInfo"),
value: [
new asn1js.Integer({ name: (names.version || "TSTInfo.version") }),
new asn1js.ObjectIdentifier({ name: (names.policy || "TSTInfo.policy") }),
MessageImprint.schema(names.messageImprint || {
names: {
blockName: "TSTInfo.messageImprint"
}
}),
new asn1js.Integer({ name: (names.serialNumber || "TSTInfo.serialNumber") }),
new asn1js.GeneralizedTime({ name: (names.genTime || "TSTInfo.genTime") }),
Accuracy.schema(names.accuracy || {
names: {
blockName: "TSTInfo.accuracy"
}
}),
new asn1js.Boolean({
name: (names.ordering || "TSTInfo.ordering"),
optional: true
}),
new asn1js.Integer({
name: (names.nonce || "TSTInfo.nonce"),
optional: true
}),
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0 // [0]
},
value: [GeneralName.schema(names.tsa || {
names: {
blockName: "TSTInfo.tsa"
}
})]
}),
new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 1 // [1]
},
value: [
new asn1js.Repeated({
name: (names.extensions || "TSTInfo.extensions"),
value: Extension.schema(names.extension || {})
})
]
}) // IMPLICIT Extensions
]
}));
}
//**********************************************************************************
/**
* Convert parsed asn1js object into current class
* @param {!Object} schema
*/
fromSchema(schema)
{
//region Clear input data first
clearProps(schema, [
"TSTInfo.version",
"TSTInfo.policy",
"TSTInfo.messageImprint",
"TSTInfo.serialNumber",
"TSTInfo.genTime",
"TSTInfo.accuracy",
"TSTInfo.ordering",
"TSTInfo.nonce",
"TSTInfo.tsa",
"TSTInfo.extensions"
]);
//endregion
//region Check the schema is valid
const asn1 = asn1js.compareSchema(schema,
schema,
TSTInfo.schema()
);
if(asn1.verified === false)
throw new Error("Object's schema was not verified against input data for TSTInfo");
//endregion
//region Get internal properties from parsed schema
this.version = asn1.result["TSTInfo.version"].valueBlock.valueDec;
this.policy = asn1.result["TSTInfo.policy"].valueBlock.toString();
this.messageImprint = new MessageImprint({ schema: asn1.result["TSTInfo.messageImprint"] });
this.serialNumber = asn1.result["TSTInfo.serialNumber"];
this.genTime = asn1.result["TSTInfo.genTime"].toDate();
if("TSTInfo.accuracy" in asn1.result)
this.accuracy = new Accuracy({ schema: asn1.result["TSTInfo.accuracy"] });
if("TSTInfo.ordering" in asn1.result)
this.ordering = asn1.result["TSTInfo.ordering"].valueBlock.value;
if("TSTInfo.nonce" in asn1.result)
this.nonce = asn1.result["TSTInfo.nonce"];
if("TSTInfo.tsa" in asn1.result)
this.tsa = new GeneralName({ schema: asn1.result["TSTInfo.tsa"] });
if("TSTInfo.extensions" in asn1.result)
this.extensions = Array.from(asn1.result["TSTInfo.extensions"], element => new Extension({ schema: element }));
//endregion
}
//**********************************************************************************
/**
* Convert current object to asn1js object and set correct values
* @returns {Object} asn1js object
*/
toSchema()
{
//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("accuracy" in this)
outputArray.push(this.accuracy.toSchema());
if("ordering" in this)
outputArray.push(new asn1js.Boolean({ value: this.ordering }));
if("nonce" in this)
outputArray.push(this.nonce);
if("tsa" in this)
{
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("extensions" in this)
{
outputArray.push(new asn1js.Constructed({
optional: true,
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 1 // [1]
},
value: Array.from(this.extensions, element => element.toSchema())
}));
}
//endregion
//endregion
//region Construct and return new ASN.1 schema for this object
return (new asn1js.Sequence({
value: outputArray
}));
//endregion
}
//**********************************************************************************
/**
* Convertion for the class to JSON object
* @returns {Object}
*/
toJSON()
{
const _object = {
version: this.version,
policy: this.policy,
messageImprint: this.messageImprint.toJSON(),
serialNumber: this.serialNumber.toJSON(),
genTime: this.genTime
};
if("accuracy" in this)
_object.accuracy = this.accuracy.toJSON();
if("ordering" in this)
_object.ordering = this.ordering;
if("nonce" in this)
_object.nonce = this.nonce.toJSON();
if("tsa" in this)
_object.tsa = this.tsa.toJSON();
if("extensions" in this)
_object.extensions = Array.from(this.extensions, element => element.toJSON());
return _object;
}
//**********************************************************************************
/**
* Verify current TST Info value
* @param {{data: ArrayBuffer, notBefore: Date, notAfter: Date}} parameters Input parameters
* @returns {Promise}
*/
verify(parameters = {})
{
//region Initial variables
let sequence = Promise.resolve();
let data;
let notBefore = null;
let notAfter = null;
//endregion
//region Get a "crypto" extension
const crypto = getCrypto();
if(typeof crypto === "undefined")
return Promise.reject("Unable to create WebCrypto object");
//endregion
//region Get initial parameters
if("data" in parameters)
data = parameters.data;
else
return Promise.reject("\"data\" is a mandatory attribute for TST_INFO verification");
if("notBefore" in parameters)
notBefore = parameters.notBefore;
if("notAfter" in parameters)
notAfter = parameters.notAfter;
//endregion
//region Check date
if(notBefore !== null)
{
if(this.genTime < notBefore)
return Promise.reject("Generation time for TSTInfo object is less than notBefore value");
}
if(notAfter !== null)
{
if(this.genTime > notAfter)
return Promise.reject("Generation time for TSTInfo object is more than notAfter value");
}
//endregion
//region Find hashing algorithm
const shaAlgorithm = getAlgorithmByOID(this.messageImprint.hashAlgorithm.algorithmId);
if(("name" in shaAlgorithm) === false)
return Promise.reject(`Unsupported signature algorithm: ${this.messageImprint.hashAlgorithm.algorithmId}`);
//endregion
//region Calculate message digest for input "data" buffer
// noinspection JSCheckFunctionSignatures
sequence = sequence.then(() =>
crypto.digest(shaAlgorithm.name, new Uint8Array(data))
).then(
result => isEqualBuffer(result, this.messageImprint.hashedMessage.valueBlock.valueHex)
);
//endregion
return sequence;
}
//**********************************************************************************
}
//**************************************************************************************