@sswroom/sswr 1.5.5 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/certutil.js ADDED
@@ -0,0 +1,2692 @@
1
+ import * as data from "./data.js";
2
+ import * as text from "./text.js";
3
+
4
+ export const ASN1ItemType = {
5
+ UNKNOWN: 0,
6
+ BOOLEAN: 0x01,
7
+ INTEGER: 0x02,
8
+ BIT_STRING: 0x03,
9
+ OCTET_STRING: 0x04,
10
+ NULL: 0x05,
11
+ OID: 0x06,
12
+ ENUMERATED: 0x0a,
13
+ UTF8STRING: 0x0c,
14
+ NUMERICSTRING: 0x12,
15
+ PRINTABLESTRING: 0x13,
16
+ T61STRING: 0x14,
17
+ VIDEOTEXSTRING: 0x15,
18
+ IA5STRING: 0x16,
19
+ UTCTIME: 0x17,
20
+ GENERALIZEDTIME: 0x18,
21
+ UNIVERSALSTRING: 0x1c,
22
+ BMPSTRING: 0x1e,
23
+ SEQUENCE: 0x30,
24
+ SET: 0x31,
25
+ CHOICE_0: 0x80,
26
+ CHOICE_1: 0x81,
27
+ CHOICE_2: 0x82,
28
+ CHOICE_3: 0x83,
29
+ CHOICE_4: 0x84,
30
+ CHOICE_5: 0x85,
31
+ CHOICE_6: 0x86,
32
+ CHOICE_7: 0x87,
33
+ CHOICE_8: 0x88,
34
+ CONTEXT_SPECIFIC_0: 0xa0,
35
+ CONTEXT_SPECIFIC_1: 0xa1,
36
+ CONTEXT_SPECIFIC_2: 0xa2,
37
+ CONTEXT_SPECIFIC_3: 0xa3,
38
+ CONTEXT_SPECIFIC_4: 0xa4
39
+ }
40
+
41
+ export const RuleCond = {
42
+ Any: 0,
43
+ TypeIsItemType: 1,
44
+ TypeIsTime: 2,
45
+ TypeIsString: 3,
46
+ TypeIsOpt: 4,
47
+ RepeatIfTypeIs: 5,
48
+ LastOIDAndTypeIs: 6,
49
+ AllNotMatch: 7
50
+ }
51
+
52
+ export class PDUInfo
53
+ {
54
+ constructor(rawOfst, hdrLen, contLen, itemType)
55
+ {
56
+ this.rawOfst = rawOfst;
57
+ this.hdrLen = hdrLen;
58
+ this.contLen = contLen;
59
+ this.itemType = itemType;
60
+ }
61
+
62
+ get dataOfst()
63
+ {
64
+ return this.rawOfst + this.hdrLen;
65
+ }
66
+
67
+ get endOfst()
68
+ {
69
+ return this.rawOfst + this.hdrLen + this.contLen;
70
+ }
71
+ }
72
+
73
+ export class ASN1Util
74
+ {
75
+ static pduParseLen(reader, ofst, endOfst)
76
+ {
77
+ if (ofst >= endOfst)
78
+ return null;
79
+ let v = reader.readUInt8(ofst);
80
+ if (v & 0x80)
81
+ {
82
+ if (v == 0x81)
83
+ {
84
+ if (ofst + 2 > endOfst)
85
+ return null;
86
+ return {nextOfst: ofst + 2, pduLen: reader.readUInt8(ofst + 1)};
87
+ }
88
+ else if (v == 0x82)
89
+ {
90
+ if (ofst + 3 > endOfst)
91
+ return null;
92
+ return {nextOfst: ofst + 3, pduLen: reader.readUInt16(ofst + 1, false)};
93
+ }
94
+ else if (v == 0x83)
95
+ {
96
+ if (ofst + 4 > endOfst)
97
+ return null;
98
+ return {nextOfst: ofst + 4, pduLen: reader.readUInt24(ofst + 1, false)};
99
+ }
100
+ else if (v == 0x84)
101
+ {
102
+ if (ofst + 5 > endOfst)
103
+ return null;
104
+ return {nextOfst: ofst + 5, pduLen: reader.readUInt32(ofst + 1, false)};
105
+ }
106
+ else if (v == 0x80)
107
+ {
108
+ return {nextOfst: ofst + 1, pduLen: 0};
109
+ }
110
+ return null;
111
+ }
112
+ else
113
+ {
114
+ return {nextOfst: ofst + 1, pduLen: v};
115
+ }
116
+ }
117
+
118
+ static pduParseUInt32(reader, ofst, endOfst)
119
+ {
120
+ if (endOfst - ofst < 3)
121
+ return null;
122
+ if (reader.readUInt8(ofst) != 2)
123
+ return null;
124
+ let len = reader.readUInt8(ofst + 1);
125
+ if (len == 1)
126
+ {
127
+ return {nextOfst: ofst + 3,
128
+ val: reader.readUInt8(ofst + 2)};
129
+ }
130
+ else if (len == 2 && endOfst - ofst >= 4)
131
+ {
132
+ return {nextOfst: ofst + 4,
133
+ val: reader.readUInt16(ofst + 2, false)};
134
+ }
135
+ else if (len == 3 && endOfst - ofst >= 5)
136
+ {
137
+ return {nextOfst: ofst + 5,
138
+ val: reader.readUInt24(ofst + 2, false)};
139
+ }
140
+ else if (len == 4 && endOfst - ofst >= 6)
141
+ {
142
+ return {nextOfst: ofst + 6,
143
+ val: reader.readUInt32(ofst + 2, false)};
144
+ }
145
+ else
146
+ {
147
+ return null;
148
+ }
149
+ }
150
+
151
+ static pduParseString(reader, ofst, endOfst)
152
+ {
153
+ let len = ASN1Util.pduParseLen(reader, ofst, endOfst);
154
+ if (len == null)
155
+ return null;
156
+ if (len.itemType != 4)
157
+ return null;
158
+ if (len.nextOfst + len.pduLen > endOfst)
159
+ return null;
160
+ return {nextOfst: len.nextOfst + len.pduLen,
161
+ val: reader.readUTF8(len.nextOfst, len.pduLen)};
162
+ }
163
+
164
+ static pduParseChoice(reader, ofst, endOfst)
165
+ {
166
+ if (endOfst - ofst < 3)
167
+ return null;
168
+ if (reader.readUInt8(ofst) != 10)
169
+ return null;
170
+ let len = reader.readUInt8(ofst + 1);
171
+ if (len == 1)
172
+ {
173
+ return {nextOfst: ofst + 3,
174
+ val: reader.readUInt8(ofst + 2)};
175
+ }
176
+ else if (len == 2 && endOfst - ofst >= 4)
177
+ {
178
+ return {nextOfst: ofst + 4,
179
+ val: reader.readUInt16(ofst + 2, false)};
180
+ }
181
+ else if (len == 3 && endOfst - ofst >= 5)
182
+ {
183
+ return {nextOfst: ofst + 5,
184
+ val: reader.readUInt24(ofst + 2, false)};
185
+ }
186
+ else if (len == 4 && endOfst - ofst >= 6)
187
+ {
188
+ return {nextOfst: ofst + 6,
189
+ val: reader.readUInt32(ofst + 2, false)};
190
+ }
191
+ else
192
+ {
193
+ return null;
194
+ }
195
+ }
196
+
197
+ static pduParseUTCTimeCont(reader, startOfst, endOfst)
198
+ {
199
+ if ((endOfst - startOfst) == 13 && reader.readUInt8(startOfst + 12) == 'Z'.charCodeAt(0))
200
+ {
201
+ let tv = new data.TimeValue();
202
+ tv.year = ASN1Util.str2Digit(reader, startOfst);
203
+ if (tv.year < 70)
204
+ {
205
+ tv.year += 2000;
206
+ }
207
+ else
208
+ {
209
+ tv.year += 1900;
210
+ }
211
+ tv.month = ASN1Util.str2Digit(reader, startOfst + 2);
212
+ tv.day = ASN1Util.str2Digit(reader, startOfst + 4);
213
+ tv.hour = ASN1Util.str2Digit(reader, startOfst + 6);
214
+ tv.minute = ASN1Util.str2Digit(reader, startOfst + 8);
215
+ tv.second = ASN1Util.str2Digit(reader, startOfst + 10);
216
+ tv.nanosec = 0;
217
+ tv.tzQhr = 0;
218
+ return data.Timestamp.fromTimeValue(tv);
219
+ }
220
+ else if ((endOfst - startOfst) == 15 && reader.readUInt8(startOfst + 14) == 'Z'.charCodeAt(0))
221
+ {
222
+ let tv = new data.TimeValue();
223
+ tv.year = ASN1Util.str2Digit(reader, startOfst) * 100 + ASN1Util.str2Digit(reader, startOfst + 2);
224
+ tv.month = ASN1Util.str2Digit(reader, startOfst + 4);
225
+ tv.day = ASN1Util.str2Digit(reader, startOfst + 6);
226
+ tv.hour = ASN1Util.str2Digit(reader, startOfst + 8);
227
+ tv.minute = ASN1Util.str2Digit(reader, startOfst + 10);
228
+ tv.second = ASN1Util.str2Digit(reader, startOfst + 12);
229
+ tv.nanosec = 0;
230
+ tv.tzQhr = 0;
231
+ return data.Timestamp.fromTimeValue(tv);
232
+ }
233
+ return null;
234
+ }
235
+
236
+ static pduToString(reader, startOfst, endOfst, outLines, level, names)
237
+ {
238
+ while (startOfst < endOfst)
239
+ {
240
+ let type = reader.readUInt8(startOfst);
241
+ let sb;
242
+ let len = ASN1Util.pduParseLen(reader, startOfst + 1, endOfst);
243
+ if (len == null)
244
+ {
245
+ return null;
246
+ }
247
+ else if (len.nextOfst + len.pduLen > endOfst)
248
+ {
249
+ return null;
250
+ }
251
+
252
+ let name;
253
+ if (names == null)
254
+ name = null;
255
+ else
256
+ name = names.readNameNoDef(type, len.pduLen, reader, len.nextOfst);
257
+
258
+ switch (type)
259
+ {
260
+ case 0x1:
261
+ sb = ["\t".repeat(level)];
262
+ if (name) sb.push(name+" ")
263
+ sb.push("BOOLEAN (");
264
+ sb.push(ASN1Util.booleanToString(reader, len.nextOfst, len.pduLen));
265
+ sb.push(')');
266
+ outLines.push(sb.join(""));
267
+ startOfst = len.nextOfst + len.pduLen;
268
+ break;
269
+ case 0x2:
270
+ if (len.pduLen <= 4)
271
+ {
272
+ let val = ASN1Util.pduParseUInt32(reader, startOfst, endOfst);
273
+ if (val == null)
274
+ {
275
+ return null;
276
+ }
277
+ sb = ["\t".repeat(level)];
278
+ if (name) sb.push(name+" ")
279
+ sb.push("INTEGER ");
280
+ sb.push(val.val.toString());
281
+ outLines.push(sb.join(""));
282
+ startOfst = len.nextOfst + len.pduLen;
283
+ }
284
+ else if (len.pduLen <= 32)
285
+ {
286
+ sb = ["\t".repeat(level)];
287
+ if (name) sb.push(name+" ")
288
+ sb.push("INTEGER ");
289
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), " ", null));
290
+ outLines.push(sb.join(""));
291
+ startOfst = len.nextOfst + len.pduLen;
292
+ }
293
+ else
294
+ {
295
+ sb = ["\t".repeat(level)];
296
+ if (name) sb.push(name+" ")
297
+ sb.push("INTEGER");
298
+ outLines.push(sb.join(""));
299
+ outLines.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), " ", "\r\n"));
300
+ startOfst = len.nextOfst + len.pduLen;
301
+ }
302
+ break;
303
+ case 0x3:
304
+ sb = ["\t".repeat(level)];
305
+ if (name) sb.push(name+" ")
306
+ sb.push("BIT STRING ");
307
+ sb.push(text.toHex8(reader.readUInt8(len.nextOfst)));
308
+ if (ASN1Util.pduIsValid(reader, len.nextOfst + 1, len.nextOfst + len.pduLen))
309
+ {
310
+ sb.push(" {");
311
+ outLines.push(sb.join(""));
312
+ if (names) names.readContainerBegin();
313
+ ASN1Util.pduToString(reader, len.nextOfst + 1, len.nextOfst + len.pduLen, outLines, level + 1, names);
314
+ if (names) names.readContainerEnd();
315
+ outLines.push("\t".repeat(level)+"}");
316
+ }
317
+ else
318
+ {
319
+ sb.push(" (");
320
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst + 1, len.pduLen - 1)), " ", null));
321
+ sb.push(")");
322
+ outLines.push(sb.join(""));
323
+ }
324
+ startOfst = len.nextOfst + len.pduLen;
325
+ break;
326
+ case 0x4:
327
+ sb = ["\t".repeat(level)];
328
+ if (name) sb.push(name+" ")
329
+ sb.push("OCTET STRING ");
330
+ if (ASN1Util.pduIsValid(reader, len.nextOfst, len.nextOfst + len.pduLen))
331
+ {
332
+ sb.push("{");
333
+ outLines.push(sb.join(""));
334
+ if (names) names.readContainerBegin();
335
+ ASN1Util.pduToString(reader, len.nextOfst, len.nextOfst + len.pduLen, outLines, level + 1, names);
336
+ if (names) names.readContainerEnd();
337
+ outLines.push("\t".repeat(level)+"}");
338
+ }
339
+ else
340
+ {
341
+ sb.push("(");
342
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst + 1, len.pduLen - 1)), " ", null));
343
+ sb.push(")");
344
+ outLines.push(sb.join(""));
345
+ }
346
+ startOfst = len.nextOfst + len.pduLen;
347
+ break;
348
+ case 0x5:
349
+ sb = ["\t".repeat(level)];
350
+ if (name) sb.push(name+" ")
351
+ sb.push("NULL");
352
+ outLines.push(sb.join(""));
353
+ startOfst = len.nextOfst + len.pduLen;
354
+ break;
355
+ case 0x6:
356
+ sb = ["\t".repeat(level)];
357
+ if (name) sb.push(name+" ")
358
+ sb.push("OID ");
359
+ let oidPDU = new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen));
360
+ sb.push(ASN1Util.oidToString(oidPDU));
361
+ sb.push(" (");
362
+ //sb.push(oiddb.oidToNameString(oidPDU));
363
+ sb.push(")");
364
+ outLines.push(sb.join(""));
365
+ startOfst = len.nextOfst + len.pduLen;
366
+ break;
367
+ case 0x0a:
368
+ if (len.pduLen == 1)
369
+ {
370
+ sb = ["\t".repeat(level)];
371
+ if (name) sb.push(name+" ")
372
+ sb.push("ENUMERATED ");
373
+ sb.push(reader.readUInt8(len.nextOfst).toString());
374
+ outLines.push(sb.join(""));
375
+ startOfst = len.nextOfst + len.pduLen;
376
+ }
377
+ else
378
+ {
379
+ return null;
380
+ }
381
+ break;
382
+ case 0x0C:
383
+ sb = ["\t".repeat(level)];
384
+ if (name) sb.push(name+" ")
385
+ sb.push("UTF8String ");
386
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
387
+ outLines.push(sb.join(""));
388
+ startOfst = len.nextOfst + len.pduLen;
389
+ break;
390
+ case 0x12:
391
+ sb = ["\t".repeat(level)];
392
+ if (name) sb.push(name+" ")
393
+ sb.push("NumericString ");
394
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
395
+ outLines.push(sb.join(""));
396
+ startOfst = len.nextOfst + len.pduLen;
397
+ break;
398
+ case 0x13:
399
+ sb = ["\t".repeat(level)];
400
+ if (name) sb.push(name+" ")
401
+ sb.push("PrintableString ");
402
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
403
+ outLines.push(sb.join(""));
404
+ startOfst = len.nextOfst + len.pduLen;
405
+ break;
406
+ case 0x14:
407
+ sb = ["\t".repeat(level)];
408
+ if (name) sb.push(name+" ")
409
+ sb.push("T61String ");
410
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
411
+ outLines.push(sb.join(""));
412
+ startOfst = len.nextOfst + len.pduLen;
413
+ break;
414
+ case 0x15:
415
+ sb = ["\t".repeat(level)];
416
+ if (name) sb.push(name+" ")
417
+ sb.push("VideotexString ");
418
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
419
+ outLines.push(sb.join(""));
420
+ startOfst = len.nextOfst + len.pduLen;
421
+ break;
422
+ case 0x16:
423
+ sb = ["\t".repeat(level)];
424
+ if (name) sb.push(name+" ")
425
+ sb.push("IA5String ");
426
+ if (reader.readUInt8(len.nextOfst + len.pduLen - 1) == 0)
427
+ sb.push(reader.readUTF8(len.nextOfst + 1, len.pduLen - 1));
428
+ else
429
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
430
+ outLines.push(sb.join(""));
431
+ startOfst = len.nextOfst + len.pduLen;
432
+ break;
433
+ case 0x17:
434
+ sb = ["\t".repeat(level)];
435
+ if (name) sb.push(name+" ")
436
+ sb.push("UTCTIME ");
437
+ if (len.pduLen == 13 && reader.readUTF8(len.nextOfst + 12, 1) == "Z")
438
+ {
439
+ let tv = new data.TimeValue();
440
+ tv.year = ASN1Util.str2Digit(reader, len.nextOfst) + 2000;
441
+ tv.month = ASN1Util.str2Digit(reader, len.nextOfst + 2);
442
+ tv.day = ASN1Util.str2Digit(reader, len.nextOfst + 4);
443
+ tv.hour = ASN1Util.str2Digit(reader, len.nextOfst + 6);
444
+ tv.minute = ASN1Util.str2Digit(reader, len.nextOfst + 8);
445
+ tv.second = ASN1Util.str2Digit(reader, len.nextOfst + 10);
446
+ tv.nanosec = 0;
447
+ tv.tzQhr = 0;
448
+ sb.push(data.Timestamp.fromTimeValue(tv).toStringNoZone());
449
+ }
450
+ else
451
+ {
452
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
453
+ }
454
+ outLines.push(sb.join(""));
455
+ startOfst = len.nextOfst + len.pduLen;
456
+ break;
457
+ case 0x18:
458
+ sb = ["\t".repeat(level)];
459
+ if (name) sb.push(name+" ")
460
+ sb.push("GeneralizedTime ");
461
+ if (len.pduLen == 15 && reader.readUTF8(len.nextOfst + 14, 1) == "Z")
462
+ {
463
+ let tv = new data.TimeValue();
464
+ tv.year = ASN1Util.str2Digit(reader, len.nextOfst) * 100 + ASN1Util.str2Digit(reader, len.nextOfst + 2);
465
+ tv.month = ASN1Util.str2Digit(reader, len.nextOfst + 4);
466
+ tv.day = ASN1Util.str2Digit(reader, len.nextOfst + 6);
467
+ tv.hour = ASN1Util.str2Digit(reader, len.nextOfst + 8);
468
+ tv.minute = ASN1Util.str2Digit(reader, len.nextOfst + 10);
469
+ tv.second = ASN1Util.str2Digit(reader, len.nextOfst + 12);
470
+ tv.nanosec = 0;
471
+ tv.tzQhr = 0;
472
+ sb.push(data.Timestamp.fromTimeValue(tv).toStringNoZone());
473
+ }
474
+ else
475
+ {
476
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
477
+ }
478
+ outLines.push(sb.join(""));
479
+ startOfst = len.nextOfst + len.pduLen;
480
+ break;
481
+ case 0x1C:
482
+ sb = ["\t".repeat(level)];
483
+ if (name) sb.push(name+" ")
484
+ sb.push("UniversalString ");
485
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
486
+ outLines.push(sb.join(""));
487
+ startOfst = len.nextOfst + len.pduLen;
488
+ break;
489
+ case 0x1E:
490
+ sb = ["\t".repeat(level)];
491
+ if (name) sb.push(name+" ")
492
+ if (len & 1)
493
+ {
494
+ sb.push("BMPString (");
495
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), ' ', null));
496
+ sb.push(")");
497
+ }
498
+ else
499
+ {
500
+ sb.push("BMPString ");
501
+ sb.push(reader.readUTF16(len.nextOfst, len.pduLen >> 1, false));
502
+ }
503
+ outLines.push(sb.join(""));
504
+ startOfst = len.nextOfst + len.pduLen;
505
+ break;
506
+ case 0x0:
507
+ if (len.pduLen == 0)
508
+ {
509
+ return startOfst + 2;
510
+ }
511
+ default:
512
+ if (type < 0x30)
513
+ {
514
+ sb = ["\t".repeat(level)];
515
+ if (name) sb.push(name+" ")
516
+ sb.push("UNKNOWN 0x");
517
+ sb.push(text.toHex8(type));
518
+ sb.push(" (");
519
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), " ", null));
520
+ sb.push(')');
521
+ outLines.push(sb.join(""));
522
+ startOfst = len.nextOfst + len.pduLen;
523
+ break;
524
+ }
525
+ else
526
+ {
527
+ sb = ["\t".repeat(level)];
528
+ if (name) sb.push(name+" ")
529
+ sb.push("UNKNOWN 0x");
530
+ sb.push(text.toHex8(type));
531
+ if (ASN1Util.pduIsValid(reader, len.nextOfst, len.nextOfst + len.pduLen))
532
+ {
533
+ sb.push(" {");
534
+ outLines.push(sb.join(""));
535
+ if (names) names.readContainerBegin();
536
+ ASN1Util.pduToString(reader, len.nextOfst, len.nextOfst + len.pduLen, outLines, level + 1, names);
537
+ if (names) names.readContainerEnd();
538
+ outLines.push("\t".repeat(level)+"}");
539
+ }
540
+ else
541
+ {
542
+ sb.push(" (");
543
+ if (reader.isASCIIText(len.nextOfst, len.pduLen))
544
+ {
545
+ sb.push(reader.getUTF8(len.nextOfst, len.pduLen));
546
+ }
547
+ else
548
+ {
549
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), " ", null));
550
+ }
551
+ sb.push(")");
552
+ }
553
+ outLines.push(sb.join(""));
554
+ startOfst = len.nextOfst + len.pduLen;
555
+ break;
556
+ }
557
+ case 0x30:
558
+ sb = ["\t".repeat(level)];
559
+ if (name) sb.push(name+" ")
560
+ sb.push("SEQUENCE {");
561
+ outLines.push(sb.join(""));
562
+
563
+ if (reader.readUInt8(startOfst + 1) == 0x80)
564
+ {
565
+ startOfst = len.nextOfst;
566
+ if (names) names.readContainerBegin();
567
+ startOfst = ASN1Util.pduToString(reader, startOfst, endOfst, outLines, level + 1, names);
568
+ if (startOfst == null)
569
+ {
570
+ return null;
571
+ }
572
+ if (names) names.readContainerEnd();
573
+ }
574
+ else
575
+ {
576
+ startOfst = len.nextOfst;
577
+ if (names) names.readContainerBegin();
578
+ startOfst = ASN1Util.pduToString(reader, startOfst, startOfst + len.pduLen, outLines, level + 1, names);
579
+ if (startOfst == null)
580
+ {
581
+ return null;
582
+ }
583
+ if (names) names.readContainerEnd();
584
+ startOfst = len.nextOfst + len.pduLen;
585
+ }
586
+ outLines.push("\t".repeat(level)+"}");
587
+ break;
588
+ case 0x31:
589
+ sb = ["\t".repeat(level)];
590
+ if (name) sb.push(name+" ")
591
+ sb.push("SET {");
592
+ outLines.push(sb.join(""));
593
+
594
+ if (reader.readUInt8(startOfst + 1) == 0x80)
595
+ {
596
+ startOfst = len.nextOfst;
597
+ if (names) names.readContainerBegin();
598
+ startOfst = ASN1Util.pduToString(reader, startOfst, endOfst, outLines, level + 1, names);
599
+ if (startOfst == null)
600
+ {
601
+ return null;
602
+ }
603
+ if (names) names.readContainerEnd();
604
+ }
605
+ else
606
+ {
607
+ startOfst = len.nextOfst;
608
+ if (names) names.readContainerBegin();
609
+ startOfst = ASN1Util.pduToString(reader, startOfst, startOfst + len.pduLen, outLines, level + 1, names);
610
+ if (startOfst == null)
611
+ {
612
+ return null;
613
+ }
614
+ if (names) names.readContainerEnd();
615
+ startOfst = len.nextOfst + len.pduLen;
616
+ }
617
+ outLines.push("\t".repeat(level)+"}");
618
+ break;
619
+ case 0x80:
620
+ case 0x81:
621
+ case 0x82:
622
+ case 0x83:
623
+ case 0x84:
624
+ case 0x85:
625
+ case 0x86:
626
+ case 0x87:
627
+ case 0x88:
628
+ sb = ["\t".repeat(level)];
629
+ if (name) sb.push(name+" ")
630
+ sb.push("CHOICE[");
631
+ sb.push((type - 0x80).toString());
632
+ sb.push("] ");
633
+ if (reader.readUInt8(startOfst + 1) == 0x80)
634
+ {
635
+ sb.push("{");
636
+ outLines.push(sb.join(""));
637
+ if (names) names.readContainerBegin();
638
+ startOfst = ASN1Util.pduToString(reader, len.nextOfst, len.nextOfst + len.pduLen, outLines, level + 1, names);
639
+ if (startOfst == null)
640
+ {
641
+ return false;
642
+ }
643
+ if (names) names.readContainerEnd();
644
+ outLines.push("\t".repeat(level)+"}");
645
+ }
646
+ else
647
+ {
648
+ if (ASN1Util.pduIsValid(reader, len.nextOfst, len.nextOfst + len.pduLen))
649
+ {
650
+ sb.push("{");
651
+ outLines.push(sb.join(""));
652
+ if (names) names.readContainerBegin();
653
+ ASN1Util.pduToString(reader, len.nextOfst, len.nextOfst + len.pduLen, outLines, level + 1, names);
654
+ if (names) names.readContainerEnd();
655
+ outLines.push("\t".repeat(level)+"}");
656
+ }
657
+ else
658
+ {
659
+ sb.push(" (");
660
+ if (reader.isASCIIText(len.nextOfst, len.pduLen))
661
+ {
662
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
663
+ }
664
+ else
665
+ {
666
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), " ", null));
667
+ }
668
+ sb.push(")");
669
+ outLines.push(sb.join(""));
670
+ }
671
+ startOfst = len.nextOfst + len.pduLen;
672
+ }
673
+ break;
674
+ case 0xA0:
675
+ case 0xA1:
676
+ case 0xA2:
677
+ case 0xA3:
678
+ case 0xA4:
679
+ case 0xA5:
680
+ case 0xA6:
681
+ case 0xA7:
682
+ case 0xA8:
683
+ sb = ["\t".repeat(level)];
684
+ if (name) sb.push(name+" ")
685
+ sb.push("CONTEXT SPECIFIC[");
686
+ sb.push((type - 0xA0).toString());
687
+ sb.push("] ");
688
+ if (reader.readUInt8(startOfst + 1) == 0x80)
689
+ {
690
+ sb.push("{");
691
+ outLines.push(sb.join(""));
692
+ if (names) names.readContainerBegin();
693
+ startOfst = ASN1Util.pduToString(reader, len.nextOfst, endOfst, outLines, level + 1, names);
694
+ if (startOfst == null)
695
+ {
696
+ return false;
697
+ }
698
+ if (names) names.readContainerEnd();
699
+ outLines.push("\t".repeat(level)+"}");
700
+ }
701
+ else
702
+ {
703
+ if (ASN1Util.pduIsValid(reader, len.nextOfst, len.nextOfst + len.pduLen))
704
+ {
705
+ sb.push("{");
706
+ outLines.push(sb.join(""));
707
+ if (names) names.readContainerBegin();
708
+ ASN1Util.pduToString(reader, len.nextOfst, len.nextOfst + len.pduLen, outLines, level + 1, names);
709
+ if (names) names.readContainerEnd();
710
+ outLines.push("\t".repeat(level)+"}");
711
+ }
712
+ else
713
+ {
714
+ sb.push(" (");
715
+ if (reader.isASCIIText(len.nextOfst, len.pduLen))
716
+ {
717
+ sb.push(reader.readUTF8(len.nextOfst, len.pduLen));
718
+ }
719
+ else
720
+ {
721
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(len.nextOfst, len.pduLen)), " ", null));
722
+ }
723
+ sb.push(")");
724
+ outLines.push(sb.join(""));
725
+ }
726
+ startOfst = len.nextOfst + len.pduLen;
727
+ }
728
+ break;
729
+ }
730
+ }
731
+ return startOfst;
732
+ }
733
+
734
+ static pduDSizeEnd(reader, startOfst, endOfst)
735
+ {
736
+ while (startOfst < endOfst)
737
+ {
738
+ let len = ASN1Util.pduParseLen(reader, startOfst + 1, endOfst);
739
+ if (len == null)
740
+ {
741
+ return null;
742
+ }
743
+ else if (len.nextOfst + len.pduLen > endOfst)
744
+ {
745
+ return null;
746
+ }
747
+
748
+ if (reader.readUInt8(startOfst) == 0 && reader.readUInt8(startOfst + 1) == 0)
749
+ {
750
+ return startOfst + 2;
751
+ }
752
+ else if (reader.readUInt8(startOfst + 1) == 0x80)
753
+ {
754
+ startOfst = ASN1Util.pduDSizeEnd(reader, len.nextOfst, endOfst);
755
+ if (startOfst == null)
756
+ {
757
+ return null;
758
+ }
759
+ }
760
+ else
761
+ {
762
+ startOfst = len.nextOfst + len.pduLen;
763
+ }
764
+ }
765
+ return startOfst;
766
+ }
767
+
768
+ static pduGetItem(reader, startOfst, endOfst, path)
769
+ {
770
+ if (path == null || path == "")
771
+ return null;
772
+ let i = path.indexOf(".");
773
+ let cnt;
774
+ if (i == -1)
775
+ {
776
+ cnt = Number.parseInt(path);
777
+ path = "";
778
+ }
779
+ else
780
+ {
781
+ cnt = Number.parseInt(path.substr(0, i));
782
+ path = path.substr(i + 1);
783
+ }
784
+ if (Number.isNaN(cnt) || cnt < 1)
785
+ return null;
786
+
787
+ while (startOfst < endOfst)
788
+ {
789
+ let len = ASN1Util.pduParseLen(reader, startOfst + 1, endOfst);
790
+ if (len == null)
791
+ {
792
+ return null;
793
+ }
794
+ else if (len.nextOfst + len.pduLen > endOfst)
795
+ {
796
+ return null;
797
+ }
798
+ else if (reader.readUInt8(startOfst) == 0 && reader.readUInt8(startOfst + 1) == 0)
799
+ {
800
+ return null;
801
+ }
802
+
803
+ cnt--;
804
+ if (cnt == 0)
805
+ {
806
+ if (path == "")
807
+ {
808
+ if (reader.readUInt8(startOfst + 1) == 0x80)
809
+ {
810
+ let ret = new PDUInfo(startOfst, len.nextOfst - startOfst, ASN1Util.pduDSizeEnd(reader, len.nextOfst, endOfst), reader.readUInt8(startOfst));
811
+ if (ret.contLen == null)
812
+ return null;
813
+ return ret;
814
+ }
815
+ else
816
+ {
817
+ return new PDUInfo(startOfst, len.nextOfst - startOfst, len.pduLen, reader.readUInt8(startOfst));
818
+ }
819
+ }
820
+ if (reader.readUInt8(startOfst + 1) == 0x80)
821
+ {
822
+ return ASN1Util.pduGetItem(reader, len.nextOfst, endOfst, path);
823
+ }
824
+ else
825
+ {
826
+ return ASN1Util.pduGetItem(reader, len.nextOfst, len.nextOfst + len.pduLen, path);
827
+ }
828
+ }
829
+ else if (reader.readUInt8(startOfst + 1) == 0x80)
830
+ {
831
+ startOfst = ASN1Util.pduDSizeEnd(reader, len.nextOfst, endOfst);
832
+ if (startOfst == null)
833
+ {
834
+ return null;
835
+ }
836
+ }
837
+ else
838
+ {
839
+ startOfst = len.nextOfst + len.pduLen;
840
+ }
841
+ }
842
+ return null;
843
+ }
844
+
845
+ static pduGetItemType(reader, startOfst, endOfst, path)
846
+ {
847
+ let item = ASN1Util.pduGetItem(reader, startOfst, endOfst, path);
848
+ if (item == null)
849
+ return ASN1ItemType.UNKNOWN;
850
+ else
851
+ return item.itemType;
852
+ }
853
+
854
+ static pduCountItem(reader, startOfst, endOfst, path)
855
+ {
856
+ let cnt;
857
+ let len;
858
+ if (path == null || path == "")
859
+ {
860
+ cnt = 0;
861
+ while (startOfst < endOfst)
862
+ {
863
+ len = ASN1Util.pduParseLen(reader, startOfst + 1, endOfst);
864
+ if (len == null)
865
+ {
866
+ return 0;
867
+ }
868
+ else if (len.nextOfst + len.pduLen > endOfst)
869
+ {
870
+ return 0;
871
+ }
872
+ else if (reader.readUInt8(startOfst) == 0 && reader.readUInt8(startOfst + 1) == 0)
873
+ {
874
+ return cnt;
875
+ }
876
+ cnt++;
877
+ if (reader.readUInt8(startOfst + 1) == 0x80)
878
+ {
879
+ if (ASN1Util.pduDSizeEnd(reader, len.nextOfst, len.nextOfst + len.pduLen) != null)
880
+ {
881
+ return cnt;
882
+ }
883
+ }
884
+ else
885
+ {
886
+ startOfst = len.nextOfst + len.pduLen;
887
+ }
888
+ }
889
+ return cnt;
890
+ }
891
+ len = path.indexOf(".");
892
+ if (len == -1)
893
+ {
894
+ cnt = Number.parseInt(path);
895
+ path = "";
896
+ }
897
+ else
898
+ {
899
+ cnt = Number.parseInt(path.substr(0, len));
900
+ path = path.substr(len + 1);
901
+ }
902
+
903
+ if (Number.isNaN(cnt) || cnt < 1)
904
+ {
905
+ return 0;
906
+ }
907
+
908
+ while (startOfst < endOfst)
909
+ {
910
+ len = ASN1Util.pduParseLen(reader, startOfst + 1, endOfst);
911
+ if (len == null)
912
+ {
913
+ return 0;
914
+ }
915
+ else if (len.nextOfst + len.pduLen > endOfst)
916
+ {
917
+ return 0;
918
+ }
919
+ else if (reader.readUInt8(startOfst) == 0 && reader.readUInt8(startOfst + 1) == 0)
920
+ {
921
+ return 0;
922
+ }
923
+
924
+ cnt--;
925
+ if (cnt == 0)
926
+ {
927
+ if (reader.readUInt8(startOfst + 1) == 0x80)
928
+ {
929
+ return ASN1Util.pduCountItem(reader, len.nextOfst, endOfst, path);
930
+ }
931
+ else
932
+ {
933
+ return ASN1Util.pduCountItem(reader, len.nextOfst, len.nextOfst + len.pduLen, path);
934
+ }
935
+ }
936
+ else if (reader.readUInt8(startOfst + 1) == 0x80)
937
+ {
938
+ startOfst = ASN1Util.pduDSizeEnd(reader, len.nextOfst, endOfst);
939
+ if (startOfst == null)
940
+ {
941
+ return 0;
942
+ }
943
+ }
944
+ else
945
+ {
946
+ startOfst = len.nextOfst + len.pduLen;
947
+ }
948
+ }
949
+ return 0;
950
+ }
951
+
952
+ static pduIsValid(reader, startOfst, endOfst)
953
+ {
954
+ while (startOfst < endOfst)
955
+ {
956
+ let len = ASN1Util.pduParseLen(reader, startOfst + 1, endOfst);
957
+ if (len == null)
958
+ {
959
+ return false;
960
+ }
961
+ else if (len.nextOfst + len.pduLen > endOfst)
962
+ {
963
+ return false;
964
+ }
965
+ let type = reader.readUInt8(startOfst);
966
+ if (type >= 0x30 && type < 0x40)
967
+ {
968
+ if (!ASN1Util.pduIsValid(reader, len.nextOfst, len.nextOfst + len.pduLen))
969
+ {
970
+ return false;
971
+ }
972
+ }
973
+ startOfst = len.nextOfst + len.pduLen;
974
+ }
975
+ return true;
976
+ }
977
+
978
+ static oidCompare(oid1, oid2)
979
+ {
980
+ let i = 0;
981
+ let oid1Len = oid1.length;
982
+ let oid2Len = oid2.length;
983
+ while (true)
984
+ {
985
+ if (i == oid1Len && i == oid2Len)
986
+ {
987
+ return 0;
988
+ }
989
+ else if (i >= oid1Len)
990
+ {
991
+ return -1;
992
+ }
993
+ else if (i >= oid2Len)
994
+ {
995
+ return 1;
996
+ }
997
+ else if (oid1[i] > oid2[i])
998
+ {
999
+ return 1;
1000
+ }
1001
+ else if (oid1[i] < oid2[i])
1002
+ {
1003
+ return -1;
1004
+ }
1005
+ i++;
1006
+ }
1007
+ }
1008
+
1009
+ static oidEqualsText(oidPDU, oidText)
1010
+ {
1011
+ let oid2 = ASN1Util.oidText2PDU(oidText);
1012
+ return ASN1Util.oidCompare(oidPDU, oid2) == 0;
1013
+ }
1014
+
1015
+ static oidToString(oidPDU)
1016
+ {
1017
+ let sb = [];
1018
+ let v = 0;
1019
+ let i = 1;
1020
+ sb.push(Math.floor(oidPDU[0] / 40));
1021
+ sb.push(oidPDU[0] % 40);
1022
+ while (i < oidPDU.length)
1023
+ {
1024
+ v = (v << 7) | (oidPDU[i] & 0x7f);
1025
+ if ((oidPDU[i] & 0x80) == 0)
1026
+ {
1027
+ sb.push(v);
1028
+ v = 0;
1029
+ }
1030
+ i++;
1031
+ }
1032
+ return sb.join(".");
1033
+ }
1034
+
1035
+ static oidText2PDU(oidText)
1036
+ {
1037
+ let sarr = oidText.split(".");
1038
+ let i = 2;
1039
+ let j = sarr.length;
1040
+ if (j == 1)
1041
+ {
1042
+ return [Number.parseInt(sarr[0])];
1043
+ }
1044
+ let pduBuff = [];
1045
+ pduBuff.push(pduBuff[0] * 40 + pduBuff[1]);
1046
+ if (j == 2)
1047
+ {
1048
+ return pduBuff;
1049
+ }
1050
+ while (i < j)
1051
+ {
1052
+ let v = Number.parseInt(sarr[i]);
1053
+ if (Number.isNaN(v))
1054
+ return null;
1055
+ if (v < 128)
1056
+ {
1057
+ pduBuff.push(v);
1058
+ }
1059
+ else if (v < 0x4000)
1060
+ {
1061
+ pduBuff.push(0x80 | (v >> 7));
1062
+ pduBuff.push((v & 0x7f));
1063
+ }
1064
+ else if (v < 0x200000)
1065
+ {
1066
+ pduBuff.push(0x80 | (v >> 14));
1067
+ pduBuff.push(0x80 | ((v >> 7) & 0x7f));
1068
+ pduBuff.push(v & 0x7f);
1069
+ }
1070
+ else if (v < 0x10000000)
1071
+ {
1072
+ pduBuff.push(0x80 | (v >> 21));
1073
+ pduBuff.push(0x80 | ((v >> 14) & 0x7f));
1074
+ pduBuff.push(0x80 | ((v >> 7) & 0x7f));
1075
+ pduBuff.push(v & 0x7f);
1076
+ }
1077
+ else
1078
+ {
1079
+ pduBuff.push(0x80 | (v >> 28));
1080
+ pduBuff.push(0x80 | ((v >> 21) & 0x7f));
1081
+ pduBuff.push(0x80 | ((v >> 14) & 0x7f));
1082
+ pduBuff.push(0x80 | ((v >> 7) & 0x7f));
1083
+ pduBuff.push(v & 0x7f);
1084
+ }
1085
+ i++;
1086
+ }
1087
+ return pduBuff;
1088
+ }
1089
+
1090
+ static booleanToString(reader, ofst, len)
1091
+ {
1092
+ if (len == 1)
1093
+ {
1094
+ let v = reader.readUInt8(ofst);
1095
+ if (v == 0xFF)
1096
+ {
1097
+ return "0xFF TRUE";
1098
+ }
1099
+ else if (v == 0)
1100
+ {
1101
+ return "0x00 FALSE";
1102
+ }
1103
+ else
1104
+ {
1105
+ return "0x"+text.toHex8(v);
1106
+ }
1107
+ }
1108
+ else
1109
+ {
1110
+ return text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(ofst, len)), " ", null);
1111
+ }
1112
+ }
1113
+
1114
+ static integerToString(reader, ofst, len)
1115
+ {
1116
+ switch (len)
1117
+ {
1118
+ case 1:
1119
+ return reader.readUInt8(ofst).toString();
1120
+ case 2:
1121
+ return reader.readUInt16(ofst, false).toString();
1122
+ case 3:
1123
+ return reader.readUInt24(ofst, false).toString();
1124
+ case 4:
1125
+ return reader.readUInt32(ofst, false).toString();
1126
+ default:
1127
+ return text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(ofst, len)), " ", null);
1128
+ }
1129
+ }
1130
+
1131
+ static utcTimeToString(reader, ofst, len)
1132
+ {
1133
+ let ts = ASN1Util.pduParseUTCTimeCont(reader, ofst, ofst + len);
1134
+ if (ts)
1135
+ {
1136
+ return ts.toString("yyyy-MM-dd HH:mm:ss");
1137
+ }
1138
+ return "";
1139
+ }
1140
+
1141
+ static itemTypeGetName(itemType)
1142
+ {
1143
+ switch (itemType)
1144
+ {
1145
+ case ASN1ItemType.UNKNOWN:
1146
+ return "UNKNOWN";
1147
+ case ASN1ItemType.BOOLEAN:
1148
+ return "BOOLEAN";
1149
+ case ASN1ItemType.INTEGER:
1150
+ return "INTEGER";
1151
+ case ASN1ItemType.BIT_STRING:
1152
+ return "BIT_STRING";
1153
+ case ASN1ItemType.OCTET_STRING:
1154
+ return "OCTET_STRING";
1155
+ case ASN1ItemType.NULL:
1156
+ return "NULL";
1157
+ case ASN1ItemType.OID:
1158
+ return "OID";
1159
+ case ASN1ItemType.ENUMERATED:
1160
+ return "ENUMERATED";
1161
+ case ASN1ItemType.UTF8STRING:
1162
+ return "UTF8STRING";
1163
+ case ASN1ItemType.NUMERICSTRING:
1164
+ return "NUMERICSTRING";
1165
+ case ASN1ItemType.PRINTABLESTRING:
1166
+ return "PRINTABLESTRING";
1167
+ case ASN1ItemType.T61STRING:
1168
+ return "T61STRING";
1169
+ case ASN1ItemType.VIDEOTEXSTRING:
1170
+ return "VIDEOTEXSTRING";
1171
+ case ASN1ItemType.IA5STRING:
1172
+ return "IA5STRING";
1173
+ case ASN1ItemType.UTCTIME:
1174
+ return "UTCTIME";
1175
+ case ASN1ItemType.GENERALIZEDTIME:
1176
+ return "GENERALIZEDTIME";
1177
+ case ASN1ItemType.UNIVERSALSTRING:
1178
+ return "UNIVERSALSTRING";
1179
+ case ASN1ItemType.BMPSTRING:
1180
+ return "BMPSTRING";
1181
+ case ASN1ItemType.SEQUENCE:
1182
+ return "SEQUENCE";
1183
+ case ASN1ItemType.SET:
1184
+ return "SET";
1185
+ case ASN1ItemType.CHOICE_0:
1186
+ return "CHOICE_0";
1187
+ case ASN1ItemType.CHOICE_1:
1188
+ return "CHOICE_1";
1189
+ case ASN1ItemType.CHOICE_2:
1190
+ return "CHOICE_2";
1191
+ case ASN1ItemType.CHOICE_3:
1192
+ return "CHOICE_3";
1193
+ case ASN1ItemType.CHOICE_4:
1194
+ return "CHOICE_4";
1195
+ case ASN1ItemType.CHOICE_5:
1196
+ return "CHOICE_5";
1197
+ case ASN1ItemType.CHOICE_6:
1198
+ return "CHOICE_6";
1199
+ case ASN1ItemType.CHOICE_7:
1200
+ return "CHOICE_7";
1201
+ case ASN1ItemType.CHOICE_8:
1202
+ return "CHOICE_8";
1203
+ case ASN1ItemType.CONTEXT_SPECIFIC_0:
1204
+ return "CONTEXT_SPECIFIC_0";
1205
+ case ASN1ItemType.CONTEXT_SPECIFIC_1:
1206
+ return "CONTEXT_SPECIFIC_1";
1207
+ case ASN1ItemType.CONTEXT_SPECIFIC_2:
1208
+ return "CONTEXT_SPECIFIC_2";
1209
+ case ASN1ItemType.CONTEXT_SPECIFIC_3:
1210
+ return "CONTEXT_SPECIFIC_3";
1211
+ case ASN1ItemType.CONTEXT_SPECIFIC_4:
1212
+ return "CONTEXT_SPECIFIC_4";
1213
+ default:
1214
+ return "Unknown";
1215
+ }
1216
+ }
1217
+
1218
+ static str2Digit(reader, ofst)
1219
+ {
1220
+ return Number.parseInt(reader.readUTF8(ofst, 2));
1221
+ }
1222
+ }
1223
+
1224
+ class RuleContainer
1225
+ {
1226
+ constructor()
1227
+ {
1228
+ this.rules = [];
1229
+ }
1230
+ }
1231
+
1232
+ export class ASN1Names
1233
+ {
1234
+ addRule(rule)
1235
+ {
1236
+ if (this.readContainer)
1237
+ this.readContainer.rules.push(rule);
1238
+ else
1239
+ this.rules.push(rule);
1240
+ this.anyCond();
1241
+ }
1242
+
1243
+ constructor()
1244
+ {
1245
+ this.readContainer = null;
1246
+ this.readLev = [];
1247
+ this.readLastOID = null;
1248
+ this.readIndex = 0;
1249
+ this.rules = [];
1250
+ this.readBegin();
1251
+ this.anyCond();
1252
+ }
1253
+
1254
+ readBegin()
1255
+ {
1256
+ this.readLev = [];
1257
+ this.readIndex = 0;
1258
+ this.readContainer = 0;
1259
+ this.readLastOIDLen = 0;
1260
+ }
1261
+
1262
+ readName(itemType, len, reader, ofst)
1263
+ {
1264
+ let name = this.readNameNoDef(itemType, len, reader, ofst);
1265
+ if (name)
1266
+ return name;
1267
+ return ASN1Util.itemTypeGetName(itemType);
1268
+ }
1269
+
1270
+ readNameNoDef(itemType, len, reader, ofst)
1271
+ {
1272
+ let anyMatch = false;
1273
+ if (itemType == ASN1ItemType.OID)
1274
+ {
1275
+ this.readLastOID = new Uint8Array(reader.getArrayBuffer(ofst, len));
1276
+ }
1277
+ if (this.readIndex == -1)
1278
+ {
1279
+ return null;
1280
+ }
1281
+ let rule;
1282
+ while (true)
1283
+ {
1284
+ if (this.readContainer)
1285
+ {
1286
+ rule = this.readContainer.rules[this.readIndex];
1287
+ }
1288
+ else
1289
+ {
1290
+ rule = this.rules[this.readIndex];
1291
+ }
1292
+ if (rule == null)
1293
+ {
1294
+ return null;
1295
+ }
1296
+ switch (rule.cond)
1297
+ {
1298
+ default:
1299
+ case RuleCond.Any:
1300
+ this.readIndex++;
1301
+ return rule.name;
1302
+ case RuleCond.TypeIsItemType:
1303
+ this.readIndex++;
1304
+ if (rule.itemType == itemType)
1305
+ return rule.name;
1306
+ break;
1307
+ case RuleCond.TypeIsTime:
1308
+ this.readIndex++;
1309
+ if (itemType == ASN1ItemType.UTCTIME || itemType == ASN1ItemType.GENERALIZEDTIME)
1310
+ return rule.name;
1311
+ break;
1312
+ case RuleCond.TypeIsString:
1313
+ this.readIndex++;
1314
+ if (itemType == ASN1ItemType.BMPSTRING ||
1315
+ itemType == ASN1ItemType.UTF8STRING ||
1316
+ itemType == ASN1ItemType.UNIVERSALSTRING ||
1317
+ itemType == ASN1ItemType.PRINTABLESTRING ||
1318
+ itemType == ASN1ItemType.T61STRING)
1319
+ return rule.name;
1320
+ break;
1321
+ case RuleCond.TypeIsOpt:
1322
+ this.readIndex++;
1323
+ if (itemType == rule.itemType + ASN1ItemType.CHOICE_0 ||
1324
+ itemType == rule.itemType + ASN1ItemType.CONTEXT_SPECIFIC_0)
1325
+ return rule.name;
1326
+ break;
1327
+ case RuleCond.LastOIDAndTypeIs:
1328
+ this.readIndex++;
1329
+ if (itemType == rule.itemType && ASN1Util.oidEqualsText(this.readLastOID, rule.condParam))
1330
+ return rule.name;
1331
+ break;
1332
+ case RuleCond.RepeatIfTypeIs:
1333
+ if (itemType == rule.itemType)
1334
+ return rule.name;
1335
+ this.readIndex++;
1336
+ break;
1337
+ case RuleCond.AllNotMatch:
1338
+ this.readIndex = 0;
1339
+ if (anyMatch)
1340
+ return rule.name;
1341
+ anyMatch = true;
1342
+ break;
1343
+ }
1344
+ }
1345
+ }
1346
+
1347
+ readContainerBegin()
1348
+ {
1349
+ let rule;
1350
+ if (this.readIndex == -1)
1351
+ {
1352
+ this.readLev.push(-1);
1353
+ }
1354
+ else
1355
+ {
1356
+ if (this.readContainer)
1357
+ {
1358
+ if (this.readIndex == 0)
1359
+ rule = this.readContainer.rules[0];
1360
+ else
1361
+ rule = this.readContainer.rules[this.readIndex - 1];
1362
+ }
1363
+ else
1364
+ {
1365
+ if (this.readIndex == 0)
1366
+ rule = this.rules[0];
1367
+ else
1368
+ rule = this.rules[this.readIndex - 1];
1369
+ }
1370
+ this.readLev.push(this.readIndex);
1371
+ if (rule == null || rule.contentFunc == null)
1372
+ {
1373
+ this.readIndex = -1;
1374
+ }
1375
+ else
1376
+ {
1377
+ this.readIndex = 0;
1378
+ let container = new RuleContainer();
1379
+ container.parent = this.readContainer;
1380
+ this.readContainer = container;
1381
+ rule.contentFunc(this);
1382
+ }
1383
+ }
1384
+ }
1385
+
1386
+ readContainerEnd()
1387
+ {
1388
+ if (this.readIndex == -1)
1389
+ {
1390
+ this.readIndex = this.readLev.pop();
1391
+ }
1392
+ else
1393
+ {
1394
+ this.readIndex = this.readLev.pop();
1395
+ if (this.readContainer)
1396
+ {
1397
+ this.readContainer = this.readContainer.parent;
1398
+ }
1399
+ }
1400
+ }
1401
+
1402
+ anyCond()
1403
+ {
1404
+ this.currCond = RuleCond.Any;
1405
+ this.currItemType = ASN1ItemType.UNKNOWN;
1406
+ this.currCondParam = null;
1407
+ return this;
1408
+ }
1409
+
1410
+ typeIs(itemType)
1411
+ {
1412
+ this.currCond = RuleCond.TypeIsItemType;
1413
+ this.currItemType = itemType;
1414
+ this.currCondParam = null;
1415
+ return this;
1416
+ }
1417
+
1418
+ typeIsTime()
1419
+ {
1420
+ this.currCond = RuleCond.TypeIsTime;
1421
+ this.currItemType = ASN1ItemType.UNKNOWN;
1422
+ this.currCondParam = null;
1423
+ return this;
1424
+ }
1425
+
1426
+ typeIsString()
1427
+ {
1428
+ this.currCond = RuleCond.TypeIsString;
1429
+ this.currItemType = ASN1ItemType.UNKNOWN;
1430
+ this.currCondParam = null;
1431
+ return this;
1432
+ }
1433
+
1434
+ typeIsOpt(index)
1435
+ {
1436
+ this.currCond = RuleCond.TypeIsOpt;
1437
+ this.currItemType = index;
1438
+ this.currCondParam = null;
1439
+ return this;
1440
+ }
1441
+
1442
+ repeatIfTypeIs(itemType)
1443
+ {
1444
+ this.currCond = RuleCond.RepeatIfTypeIs;
1445
+ this.currItemType = itemType;
1446
+ this.currCondParam = null;
1447
+ return this;
1448
+ }
1449
+
1450
+ lastOIDAndTypeIs(oidText, itemType)
1451
+ {
1452
+ this.currCond = RuleCond.LastOIDAndTypeIs;
1453
+ this.currItemType = itemType;
1454
+ this.currCondParam = oidText;
1455
+ return this;
1456
+ }
1457
+
1458
+ allNotMatch()
1459
+ {
1460
+ this.currCond = RuleCond.AllNotMatch;
1461
+ this.currItemType = ASN1ItemType.UNKNOWN;
1462
+ this.currCondParam = null;
1463
+ return this;
1464
+ }
1465
+
1466
+ container(name, contFunc)
1467
+ {
1468
+ let rule = {
1469
+ cond: this.currCond,
1470
+ itemType: this.currItemType,
1471
+ condParam: this.currCondParam,
1472
+ name: name,
1473
+ contentFunc: contFunc,
1474
+ enumVals: null};
1475
+ this.addRule(rule);
1476
+ return this;
1477
+ }
1478
+
1479
+ nextValue(name)
1480
+ {
1481
+ let rule = {
1482
+ cond: this.currCond,
1483
+ itemType: this.currItemType,
1484
+ condParam: this.currCondParam,
1485
+ name: name,
1486
+ contentFunc: null,
1487
+ enumVals: null};
1488
+ this.addRule(rule);
1489
+ return this;
1490
+ }
1491
+
1492
+ enum(name, enums)
1493
+ {
1494
+ let rule = {
1495
+ cond: this.currCond,
1496
+ itemType: this.currItemType,
1497
+ condParam: this.currCondParam,
1498
+ name: name,
1499
+ contentFunc: null,
1500
+ enumVals: enums};
1501
+ this.addRule(rule);
1502
+ return this;
1503
+ }
1504
+
1505
+ setCertificate()
1506
+ {
1507
+ PKIX1Explicit88.certificate(this);
1508
+ return this;
1509
+ }
1510
+
1511
+ setRSAPublicKey()
1512
+ {
1513
+ PKCS1.rsaPublicKey(this);
1514
+ return this;
1515
+ }
1516
+
1517
+ setRSAPrivateKey()
1518
+ {
1519
+ PKCS1.rsaPrivateKey(this);
1520
+ return this;
1521
+ }
1522
+
1523
+ setPKCS7ContentInfo()
1524
+ {
1525
+ PKCS7.contentInfo(this);
1526
+ return this;
1527
+ }
1528
+
1529
+ setCertificationRequest()
1530
+ {
1531
+ PKCS10.certificationRequest(this);
1532
+ return this;
1533
+ }
1534
+
1535
+ setCertificateList()
1536
+ {
1537
+ PKIX1Explicit88.certificateList(this);
1538
+ return this;
1539
+ }
1540
+
1541
+ setPFX()
1542
+ {
1543
+ PKCS12.pfx(this);
1544
+ return this;
1545
+ }
1546
+ }
1547
+
1548
+ class General
1549
+ {
1550
+ static pbeParam(names)
1551
+ {
1552
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("salt");
1553
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("iterations");
1554
+ }
1555
+
1556
+ static extendedValidationCertificates(names)
1557
+ {
1558
+ names.repeatIfTypeIs(ASN1ItemType.OCTET_STRING).nextValue("signedCertTimestamp");
1559
+ }
1560
+
1561
+ static attributeOutlookExpress(names)
1562
+ {
1563
+ names.typeIs(ASN1ItemType.SEQUENCE).container("issuerAndSerialNumber", PKCS7.issuerAndSerialNumberCont);
1564
+ }
1565
+ }
1566
+
1567
+ class InformationFramework
1568
+ {
1569
+ static attributeCont(names)
1570
+ {
1571
+ names.typeIs(ASN1ItemType.OID).nextValue("attrId");
1572
+ // names.lastOIDAndTypeIs("1.2.840.113549.1.9.1", ASN1ItemType.SET).container("attrValues", AttributeEmailAddress);
1573
+ // names.lastOIDAndTypeIs("1.2.840.113549.1.9.2", ASN1ItemType.SET).container("attrValues", AttributeUnstructuredName);
1574
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.3", ASN1ItemType.SET).container("attrValues", PKCS9.attributeContentType);
1575
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.4", ASN1ItemType.SET).container("attrValues", PKCS9.attributeMessageDigest);
1576
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.5", ASN1ItemType.SET).container("attrValues", PKCS9.attributeSigningTime);
1577
+ // names.lastOIDAndTypeIs("1.2.840.113549.1.9.6", ASN1ItemType.SET).container("attrValues", AttributeCounterSignature);
1578
+ // names.lastOIDAndTypeIs("1.2.840.113549.1.9.7", ASN1ItemType.SET).container("attrValues", AttributeChallengePassword);
1579
+ // names.lastOIDAndTypeIs("1.2.840.113549.1.9.8", ASN1ItemType.SET).container("attrValues", AttributeUnstructuredAddress);
1580
+ // names.lastOIDAndTypeIs("1.2.840.113549.1.9.9", ASN1ItemType.SET).container("attrValues", AttributeExtendedCertificateAttributes);
1581
+
1582
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.15", ASN1ItemType.SET).container("attrValues", PKCS9.attributeSMIMECapabilities);
1583
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.16.2.11", ASN1ItemType.SET).container("attrValues", RFC8551.smimeEncryptionKeyPreference);
1584
+
1585
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.20", ASN1ItemType.SET).container("attrValues", PKCS9.attributeFriendlyName);
1586
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.21", ASN1ItemType.SET).container("attrValues", PKCS9.attributeLocalKeyId);
1587
+ names.lastOIDAndTypeIs("1.3.6.1.4.1.311.16.4", ASN1ItemType.SET).container("attrValues", General.attributeOutlookExpress);
1588
+
1589
+ names.typeIs(ASN1ItemType.SET).nextValue("attrValues");
1590
+ }
1591
+ }
1592
+
1593
+ class PKCS1
1594
+ {
1595
+ static rsaPublicKey(names)
1596
+ {
1597
+ names.typeIs(ASN1ItemType.SEQUENCE).container("RSAPublicKey", PKCS1.rsaPublicKeyCont);
1598
+ }
1599
+
1600
+ static rsaPublicKeyCont(names)
1601
+ {
1602
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("modulus");
1603
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("publicExponent");
1604
+ }
1605
+
1606
+ static rsaPrivateKey(names)
1607
+ {
1608
+ names.typeIs(ASN1ItemType.SEQUENCE).container("RSAPrivateKey", PKCS1.rsaPrivateKeyCont);
1609
+ }
1610
+
1611
+ static rsaPrivateKeyCont(names)
1612
+ {
1613
+ let version = ["two-prime", "multi"];
1614
+ names.typeIs(ASN1ItemType.INTEGER).enum("Version", version);
1615
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("modulus");
1616
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("publicExponent");
1617
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("privateExponent");
1618
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("prime1");
1619
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("prime2");
1620
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("exponent1");
1621
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("exponent2");
1622
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("coefficient");
1623
+ names.typeIs(ASN1ItemType.SEQUENCE).container("otherPrimeInfos", PKCS1.otherPrimeInfos);
1624
+ }
1625
+
1626
+ static otherPrimeInfos(names)
1627
+ {
1628
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("prime");
1629
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("exponent");
1630
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("coefficient");
1631
+ }
1632
+
1633
+ static addDigestInfo(names, name)
1634
+ {
1635
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKCS1.digestInfoCont);
1636
+ }
1637
+
1638
+ static digestInfoCont(names)
1639
+ {
1640
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "digestAlgorithm");
1641
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("digest");
1642
+ }
1643
+
1644
+ }
1645
+
1646
+ class PKCS7
1647
+ {
1648
+ static addContentInfo(names, name)
1649
+ {
1650
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKCS7.contentInfoCont);
1651
+ }
1652
+
1653
+ static contentInfo(names)
1654
+ {
1655
+ names.typeIs(ASN1ItemType.SEQUENCE).container("ContentInfo", PKCS7.contentInfoCont);
1656
+ }
1657
+
1658
+ static contentInfoCont(names)
1659
+ {
1660
+ names.typeIs(ASN1ItemType.OID).nextValue("content-type");
1661
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.1", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.data);
1662
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.2", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.signedData);
1663
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.3", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.envelopedData);
1664
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.4", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.signedAndEnvelopedData);
1665
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.5", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.digestedData);
1666
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.6", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.encryptedData);
1667
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.16.1.2", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.authenticatedData);
1668
+ names.nextValue("pkcs7-content"); ////////////////////////
1669
+ }
1670
+
1671
+ static data(names)
1672
+ {
1673
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("data");
1674
+ }
1675
+
1676
+ static signedData(names)
1677
+ {
1678
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signed-data", PKCS7.signedDataCont);
1679
+ }
1680
+
1681
+ static signedDataCont(names)
1682
+ {
1683
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1684
+ names.typeIs(ASN1ItemType.SET).container("digestAlgorithms", PKCS7.digestAlgorithmIdentifiers);
1685
+ names.typeIs(ASN1ItemType.SEQUENCE).container("contentInfo", PKCS7.contentInfoCont);
1686
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("certificates", PKCS7.certificateSet);
1687
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).container("crls", PKCS7.certificateRevocationLists);
1688
+ names.typeIs(ASN1ItemType.SET).container("signerInfos", PKCS7.signerInfos);
1689
+ }
1690
+
1691
+ static digestAlgorithmIdentifiers(names)
1692
+ {
1693
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("DigestAlgorithmIdentifier", PKIX1Explicit88.algorithmIdentifierCont);
1694
+ }
1695
+
1696
+ static certificateSet(names)
1697
+ {
1698
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("certificate", PKIX1Explicit88.certificateCont);
1699
+ names.repeatIfTypeIs(ASN1ItemType.CHOICE_0).nextValue("extendedCertificate");//, ExtendedCertificate);
1700
+ names.repeatIfTypeIs(ASN1ItemType.CHOICE_1).nextValue("attributeCertificate");//, AttributeCertificate);
1701
+ }
1702
+
1703
+ static certificateRevocationLists(names)
1704
+ {
1705
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).nextValue("CertificateRevocationLists");//, CertificateListCont);
1706
+ }
1707
+
1708
+ static signerInfos(names)
1709
+ {
1710
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("SignerInfo", PKCS7.signerInfoCont);
1711
+ }
1712
+
1713
+ static signerInfoCont(names)
1714
+ {
1715
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1716
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signerIdentifier", PKCS7.issuerAndSerialNumberCont);
1717
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_2).container("signerIdentifier", PKIX1Implicit88.subjectKeyIdentifier);
1718
+ names.typeIs(ASN1ItemType.SEQUENCE).container("digestAlgorithm", PKIX1Explicit88.algorithmIdentifierCont);
1719
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("authenticatedAttributes", PKCS10.attributesCont);
1720
+ names.typeIs(ASN1ItemType.SEQUENCE).container("digestEncryptionAlgorithm", PKIX1Explicit88.algorithmIdentifierCont);
1721
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("encryptedDigest");
1722
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).container("unauthenticatedAttributes", PKCS10.attributesCont);
1723
+ }
1724
+
1725
+ static issuerAndSerialNumberCont(names)
1726
+ {
1727
+ PKIX1Explicit88.addName(names, "issuer");
1728
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("serialNumber");
1729
+ }
1730
+
1731
+ static addDigestInfo(names, name)
1732
+ {
1733
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKCS7.digestInfoCont);
1734
+ }
1735
+
1736
+ static digestInfoCont(names)
1737
+ {
1738
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "digestAlgorithm");
1739
+ names.nextValue("digest"); ////////////////////////
1740
+ }
1741
+
1742
+ static envelopedData(names)
1743
+ {
1744
+ names.typeIs(ASN1ItemType.SEQUENCE).container("enveloped-data", PKCS7.envelopedDataCont);
1745
+ }
1746
+
1747
+ static envelopedDataCont(names)
1748
+ {
1749
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1750
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("originatorInfo", PKCS7.originatorInfoCont);
1751
+ names.typeIs(ASN1ItemType.SET).container("recipientInfos", PKCS7.recipientInfos);
1752
+ names.typeIs(ASN1ItemType.SEQUENCE).container("encryptedContentInfo", PKCS7.contentInfoCont);
1753
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).container("unprotectedAttributes", PKCS10.attributesCont);
1754
+ }
1755
+
1756
+ static originatorInfoCont(names)
1757
+ {
1758
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("certificates", PKCS7.certificateSet);
1759
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).container("crls", PKCS7.certificateRevocationLists);
1760
+ }
1761
+
1762
+ static recipientInfos(names)
1763
+ {
1764
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("keyTransportRecipientInfo", PKCS7.keyTransportRecipientInfoCont);
1765
+ names.repeatIfTypeIs(ASN1ItemType.CHOICE_0).nextValue("keyAgreementRecipientInfo");//, PKCS7.keyAgreementRecipientInfo);
1766
+ names.repeatIfTypeIs(ASN1ItemType.CHOICE_1).nextValue("keyEncryptionKeyRecipientInfo");//, PKCS7.keyEncryptionKeyRecipientInfo);
1767
+ }
1768
+
1769
+ static keyTransportRecipientInfoCont(names)
1770
+ {
1771
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1772
+ names//.typeIs(ASN1ItemType.INTEGER)
1773
+ .nextValue("recipientIdentifier"); //PKCS7.RecipientIdentifier
1774
+ names.typeIs(ASN1ItemType.SEQUENCE).container("keyEncryptionAlgorithm", PKIX1Explicit88.algorithmIdentifierCont);
1775
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("encryptedKey");
1776
+ }
1777
+
1778
+ static signedAndEnvelopedData(names)
1779
+ {
1780
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signed-data", PKCS7.signedAndEnvelopedDataCont);
1781
+ }
1782
+
1783
+ static signedAndEnvelopedDataCont(names)
1784
+ {
1785
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1786
+ names.typeIs(ASN1ItemType.SET).nextValue("recipientInfos");
1787
+ names.typeIs(ASN1ItemType.SET).container("digestAlgorithms", PKCS7.digestAlgorithmIdentifiers);
1788
+ names.typeIs(ASN1ItemType.SEQUENCE).container("contentInfo", PKCS7.contentInfoCont);
1789
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("certificates", PKCS7.certificateSet);
1790
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).container("crls", PKCS7.certificateRevocationLists);
1791
+ names.typeIs(ASN1ItemType.SET).container("signerInfos", PKCS7.signerInfos);
1792
+ }
1793
+
1794
+ static digestedData(names)
1795
+ {
1796
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signed-data", PKCS7.digestedDataCont);
1797
+ }
1798
+
1799
+ static digestedDataCont(names)
1800
+ {
1801
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signed-data", PKCS7.digestedDataCont);
1802
+ }
1803
+
1804
+ static encryptedData(names)
1805
+ {
1806
+ names.typeIs(ASN1ItemType.SEQUENCE).container("encrypted-data", PKCS7.encryptedDataCont);
1807
+ }
1808
+
1809
+ static encryptedDataCont(names)
1810
+ {
1811
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1812
+ names.typeIs(ASN1ItemType.SEQUENCE).container("encryptedContentInfo", PKCS7.encryptedContentInfoCont);
1813
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).container("unprotectedAttributes", PKCS10.attributesCont);
1814
+ }
1815
+
1816
+ static encryptedContentInfoCont(names)
1817
+ {
1818
+ names.typeIs(ASN1ItemType.OID).nextValue("contentType");
1819
+ names.typeIs(ASN1ItemType.SEQUENCE).container("contentEncryptionAlgorithm", PKIX1Explicit88.algorithmIdentifierCont);
1820
+ names.typeIs(ASN1ItemType.CHOICE_0).nextValue("encryptedContent");
1821
+ }
1822
+
1823
+ static authenticatedData(names)
1824
+ {
1825
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signed-data", PKCS7.authenticatedData);
1826
+ }
1827
+ }
1828
+
1829
+ class PKCS8
1830
+ {
1831
+ static privateKeyInfo(names)
1832
+ {
1833
+ names.typeIs(ASN1ItemType.SEQUENCE).container("PrivateKeyInfo", PKCS8.privateKeyInfoCont);
1834
+ }
1835
+
1836
+ static privateKeyInfoCont(names)
1837
+ {
1838
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1839
+ names.typeIs(ASN1ItemType.SEQUENCE).container("privateKeyAlgorithm", PKIX1Explicit88.algorithmIdentifierCont);
1840
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("privateKey");
1841
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("attributes", PKCS10.attributesCont);
1842
+ }
1843
+
1844
+ static encryptedPrivateKeyInfo(names)
1845
+ {
1846
+ names.typeIs(ASN1ItemType.SEQUENCE).container("EncryptedPrivateKeyInfo", PKCS8.encryptedPrivateKeyInfoCont);
1847
+ }
1848
+
1849
+ static encryptedPrivateKeyInfoCont(names)
1850
+ {
1851
+ names.typeIs(ASN1ItemType.SEQUENCE).container("encryptionAlgorithm", PKIX1Explicit88.algorithmIdentifierCont);
1852
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("encryptedData");
1853
+ }
1854
+ }
1855
+
1856
+ class PKCS9
1857
+ {
1858
+ static attributeContentType(names)
1859
+ {
1860
+ names.typeIs(ASN1ItemType.OID).nextValue("contentType");
1861
+ }
1862
+
1863
+ static attributeMessageDigest(names)
1864
+ {
1865
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("messageDigest");
1866
+ }
1867
+
1868
+ static attributeSigningTime(names)
1869
+ {
1870
+ names.typeIsTime().nextValue("signingTime");
1871
+ }
1872
+
1873
+ static attributeFriendlyName(names)
1874
+ {
1875
+ names.typeIs(ASN1ItemType.BMPSTRING).nextValue("friendlyName");
1876
+ }
1877
+
1878
+ static attributeSMIMECapabilities(names)
1879
+ {
1880
+ names.typeIs(ASN1ItemType.SEQUENCE).container("smimeCapabilities", PKCS9.smimeCapabilitiesCont);
1881
+ }
1882
+
1883
+ static attributeLocalKeyId(names)
1884
+ {
1885
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("localKeyId");
1886
+ }
1887
+
1888
+ static smimeCapabilitiesCont(names)
1889
+ {
1890
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("SMIMECapability", PKCS9.smimeCapabilityCont);
1891
+ }
1892
+
1893
+ static smimeCapabilityCont(names)
1894
+ {
1895
+ names.typeIs(ASN1ItemType.OID).nextValue("algorithm");
1896
+ names.nextValue("parameters");
1897
+ }
1898
+ }
1899
+
1900
+ class PKCS10
1901
+ {
1902
+ static addCertificationRequestInfo(names, name)
1903
+ {
1904
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKCS10.certificationRequestInfoCont);
1905
+ }
1906
+
1907
+ static certificationRequestInfoCont(names)
1908
+ {
1909
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("version");
1910
+ PKIX1Explicit88.addName(names, "subject");
1911
+ PKIX1Explicit88.addSubjectPublicKeyInfo(names, "subjectPKInfo");
1912
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("attributes", PKCS10.attributesCont);
1913
+ }
1914
+
1915
+ static attributesCont(names)
1916
+ {
1917
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("Attribute", InformationFramework.attributeCont);
1918
+ }
1919
+
1920
+ static certificationRequest(names)
1921
+ {
1922
+ names.typeIs(ASN1ItemType.SEQUENCE).container("CertificationRequest", PKCS10.certificationRequestCont);
1923
+ }
1924
+
1925
+ static certificationRequestCont(names)
1926
+ {
1927
+ PKCS10.addCertificationRequestInfo(names, "certificationRequestInfo");
1928
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "signatureAlgorithm");
1929
+ names.typeIs(ASN1ItemType.BIT_STRING).nextValue("signature");
1930
+ }
1931
+ }
1932
+
1933
+ class PKCS12
1934
+ {
1935
+ static pfx(names)
1936
+ {
1937
+ names.typeIs(ASN1ItemType.SEQUENCE).container("PFX", PKCS12.pfxCont);
1938
+ }
1939
+
1940
+ static pfxCont(names)
1941
+ {
1942
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("Version");
1943
+ names.typeIs(ASN1ItemType.SEQUENCE).container("authSafe", PKCS12.authenticatedSafeContentInfoCont);
1944
+ PKCS12.addMacData(names, "macData");
1945
+ }
1946
+
1947
+ static addMacData(names, name)
1948
+ {
1949
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKCS12.macDataCont);
1950
+ }
1951
+
1952
+ static macDataCont(names)
1953
+ {
1954
+ PKCS7.addDigestInfo(names, "mac");
1955
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("macSalt");
1956
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("iterations");
1957
+ }
1958
+
1959
+ static authenticatedSafeContentInfoCont(names)
1960
+ {
1961
+ names.typeIs(ASN1ItemType.OID).nextValue("content-type");
1962
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.1", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS12.authenticatedSafeData);
1963
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.3", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS12.authenticatedSafeEnvelopedData);
1964
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.6", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS12.authenticatedSafeEncryptedData);
1965
+ names.nextValue("pkcs7-content"); ////////////////////////
1966
+ }
1967
+
1968
+ static authenticatedSafeData(names)
1969
+ {
1970
+ names.typeIs(ASN1ItemType.OCTET_STRING).container("data", PKCS12.authenticatedSafe);
1971
+ }
1972
+
1973
+ static authenticatedSafeEnvelopedData(names)
1974
+ {
1975
+ names.typeIs(ASN1ItemType.SEQUENCE).container("signed-data", PKCS7.envelopedDataCont);
1976
+ }
1977
+
1978
+ static authenticatedSafeEncryptedData(names)
1979
+ {
1980
+ names.typeIs(ASN1ItemType.SEQUENCE).container("encrypted-data", PKCS7.encryptedDataCont);
1981
+ }
1982
+
1983
+ static authenticatedSafe(names)
1984
+ {
1985
+ names.typeIs(ASN1ItemType.SEQUENCE).container("AuthenticatedSafe", PKCS12.authSafeContentInfo);
1986
+ }
1987
+
1988
+ static authSafeContentInfo(names)
1989
+ {
1990
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("ContentInfo", PKCS12.authSafeContentInfoCont);
1991
+ }
1992
+
1993
+ static authSafeContentInfoCont(names)
1994
+ {
1995
+ names.typeIs(ASN1ItemType.OID).nextValue("content-type");
1996
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.1", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS12.safeContentsData);
1997
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.2", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.signedData);
1998
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.3", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.envelopedData);
1999
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.4", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.signedAndEnvelopedData);
2000
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.5", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.digestedData);
2001
+ names.lastOIDAndTypeIs("1.2.840.113549.1.7.6", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.encryptedData);
2002
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.16.1.2", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs7-content", PKCS7.authenticatedData);
2003
+ names.nextValue("pkcs7-content"); ////////////////////////
2004
+ }
2005
+
2006
+ static safeContentsData(names)
2007
+ {
2008
+ names.typeIs(ASN1ItemType.OCTET_STRING).container("data", PKCS12.safeContents);
2009
+ }
2010
+
2011
+ static safeContents(names)
2012
+ {
2013
+ names.typeIs(ASN1ItemType.SEQUENCE).container("SafeContents", PKCS12.safeContentsCont);
2014
+ }
2015
+
2016
+ static safeContentsCont(names)
2017
+ {
2018
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("SafeBag", PKCS12.safeBagCont);
2019
+ }
2020
+
2021
+ static safeBagCont(names)
2022
+ {
2023
+ names.typeIs(ASN1ItemType.OID).nextValue("bagId");
2024
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.10.1.1", ASN1ItemType.CONTEXT_SPECIFIC_0).container("keyBag", PKCS8.privateKeyInfo);
2025
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.10.1.2", ASN1ItemType.CONTEXT_SPECIFIC_0).container("pkcs8ShroudedKeyBag", PKCS8.encryptedPrivateKeyInfo);
2026
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.10.1.3", ASN1ItemType.CONTEXT_SPECIFIC_0).container("certBag", PKCS12.certBag);
2027
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.10.1.4", ASN1ItemType.CONTEXT_SPECIFIC_0).container("crlBag", PKCS12.crlBag);
2028
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.10.1.5", ASN1ItemType.CONTEXT_SPECIFIC_0).container("secretBag", PKCS12.secretBag);
2029
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.10.1.6", ASN1ItemType.CONTEXT_SPECIFIC_0).container("safeContentsBag", PKCS12.safeContents);
2030
+ names.typeIs(ASN1ItemType.SET).container("bagAttributes", PKCS12.pkcs12Attributes);
2031
+ }
2032
+
2033
+ static certBag(names)
2034
+ {
2035
+ names.typeIs(ASN1ItemType.SEQUENCE).container("CertBag", PKCS12.certBagCont);
2036
+ }
2037
+
2038
+ static certBagCont(names)
2039
+ {
2040
+ names.typeIs(ASN1ItemType.OID).nextValue("certId");
2041
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.22.1", ASN1ItemType.CONTEXT_SPECIFIC_0).container("certValue", PKCS12.x509Certificate);
2042
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.22.2", ASN1ItemType.CONTEXT_SPECIFIC_0).container("certValue", PKCS12.sdsiCertificate);
2043
+ }
2044
+
2045
+ static x509Certificate(names)
2046
+ {
2047
+ names.typeIs(ASN1ItemType.OCTET_STRING).container("x509Certificate", PKIX1Explicit88.certificate);
2048
+ }
2049
+
2050
+ static sdsiCertificate(names)
2051
+ {
2052
+ names.typeIs(ASN1ItemType.IA5STRING).nextValue("sdsiCertificate");
2053
+ }
2054
+
2055
+ static crlBag(names)
2056
+ {
2057
+ names.typeIs(ASN1ItemType.SEQUENCE).container("CRLBag", PKCS12.crlBagCont);
2058
+ }
2059
+
2060
+ static crlBagCont(names)
2061
+ {
2062
+ names.typeIs(ASN1ItemType.OID).nextValue("crlId");
2063
+ names.lastOIDAndTypeIs("1.2.840.113549.1.9.23.1", ASN1ItemType.CONTEXT_SPECIFIC_0).container("crlValue", PKCS12.x509CRL);
2064
+ }
2065
+
2066
+ static x509CRL(names)
2067
+ {
2068
+ names.typeIs(ASN1ItemType.OCTET_STRING).container("x509CRL", PKIX1Explicit88.certificateList);
2069
+ }
2070
+
2071
+ static secretBag(names)
2072
+ {
2073
+ names.typeIs(ASN1ItemType.SEQUENCE).container("SecretBag", PKCS12.secretBagCont);
2074
+ }
2075
+
2076
+ static secretBagCont(names)
2077
+ {
2078
+ names.typeIs(ASN1ItemType.OID).nextValue("secretTypeId");
2079
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).nextValue("secretValue");
2080
+ }
2081
+
2082
+ static pkcs12Attributes(names)
2083
+ {
2084
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("PKCS12Attribute", InformationFramework.attributeCont);
2085
+ }
2086
+ }
2087
+
2088
+ class PKIX1Explicit88
2089
+ {
2090
+ static addAttributeTypeAndValue(names, name)
2091
+ {
2092
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.attributeTypeAndValueCont);
2093
+ }
2094
+
2095
+ static attributeTypeAndValueCont(names)
2096
+ {
2097
+ names.typeIs(ASN1ItemType.OID).nextValue("type");
2098
+ names.nextValue("value");
2099
+ }
2100
+
2101
+ static addName(names, name)
2102
+ {
2103
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.rdnSequenceCont);
2104
+ }
2105
+
2106
+ static name(names)
2107
+ {
2108
+ names.typeIs(ASN1ItemType.SEQUENCE).container("Name", PKIX1Explicit88.rdnSequenceCont);
2109
+ }
2110
+
2111
+ static rdnSequenceCont(names)
2112
+ {
2113
+ names.repeatIfTypeIs(ASN1ItemType.SET).container("rdnSequence", PKIX1Explicit88.relativeDistinguishedNameCont);
2114
+ }
2115
+
2116
+ static relativeDistinguishedName(names)
2117
+ {
2118
+ names.typeIs(ASN1ItemType.SET).container("RelativeDistinguishedName", PKIX1Explicit88.relativeDistinguishedNameCont);
2119
+ }
2120
+
2121
+ static relativeDistinguishedNameCont(names)
2122
+ {
2123
+ PKIX1Explicit88.addAttributeTypeAndValue(names, "AttributeTypeAndValue");
2124
+ }
2125
+
2126
+ static certificate(names)
2127
+ {
2128
+ names.typeIs(ASN1ItemType.SEQUENCE).container("Certificate", PKIX1Explicit88.certificateCont);
2129
+ }
2130
+
2131
+ static certificateCont(names)
2132
+ {
2133
+ PKIX1Explicit88.addTBSCertificate(names, "tbsCertificate");
2134
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "signatureAlgorithm");
2135
+ names.typeIs(ASN1ItemType.BIT_STRING).nextValue("signature");
2136
+ }
2137
+
2138
+ static addTBSCertificate(names, name)
2139
+ {
2140
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.tbsCertificateCont);
2141
+ }
2142
+
2143
+ static tbsCertificateCont(names)
2144
+ {
2145
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("version", PKIX1Explicit88.version);
2146
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("serialNumber");
2147
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "signature");
2148
+ PKIX1Explicit88.addName(names, "issuer");
2149
+ PKIX1Explicit88.addValidity(names, "validity");
2150
+ PKIX1Explicit88.addName(names, "subject");
2151
+ PKIX1Explicit88.addSubjectPublicKeyInfo(names, "subjectPublicKeyInfo");
2152
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).nextValue("issuerUniqueID");/////////////////////
2153
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_2).nextValue("subjectUniqueID");//////////////////////
2154
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_3).container("extensions", PKIX1Explicit88.extensions);
2155
+ }
2156
+
2157
+ static version(names)
2158
+ {
2159
+ let versions = ["v1", "v2", "v3"];
2160
+ names.typeIs(ASN1ItemType.INTEGER).enum("Version", versions);
2161
+ }
2162
+
2163
+ static addValidity(names, name)
2164
+ {
2165
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.validityCont);
2166
+ }
2167
+
2168
+ static validityCont(names)
2169
+ {
2170
+ names.typeIsTime().nextValue("notBefore");
2171
+ names.typeIsTime().nextValue("notAfter");
2172
+ }
2173
+
2174
+ static addSubjectPublicKeyInfo(names, name)
2175
+ {
2176
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.subjectPublicKeyInfoCont);
2177
+ }
2178
+
2179
+ static subjectPublicKeyInfoCont(names)
2180
+ {
2181
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "algorithm");
2182
+ names.lastOIDAndTypeIs("1.2.840.113549.1.1.1", ASN1ItemType.BIT_STRING).container("subjectPublicKey", PKCS1.rsaPublicKey);
2183
+ names.typeIs(ASN1ItemType.BIT_STRING).nextValue("subjectPublicKey");
2184
+ }
2185
+
2186
+ static addExtensions(names, name)
2187
+ {
2188
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.extensionsCont);
2189
+ }
2190
+
2191
+ static extensions(names)
2192
+ {
2193
+ names.typeIs(ASN1ItemType.SEQUENCE).container("Extensions", PKIX1Explicit88.extensionsCont);
2194
+ }
2195
+
2196
+ static extensionsCont(names)
2197
+ {
2198
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("Extension", PKIX1Explicit88.extensionCont);
2199
+ }
2200
+
2201
+ static extensionCont(names)
2202
+ {
2203
+ names.typeIs(ASN1ItemType.OID).nextValue("extnID");
2204
+ names.typeIs(ASN1ItemType.BOOLEAN).nextValue("critical");
2205
+ names.lastOIDAndTypeIs("1.3.6.1.4.1.11129.2.4.2", ASN1ItemType.OCTET_STRING).container("extendedValidationCertificates", General.extendedValidationCertificates);
2206
+ names.lastOIDAndTypeIs("1.3.6.1.5.5.7.1.1", ASN1ItemType.OCTET_STRING).container("authorityInfoAccess", RFC2459.authorityInfoAccessSyntax);
2207
+ names.lastOIDAndTypeIs("2.5.29.14", ASN1ItemType.OCTET_STRING).container("subjectKeyIdentifier", PKIX1Implicit88.subjectKeyIdentifier);
2208
+ names.lastOIDAndTypeIs("2.5.29.15", ASN1ItemType.OCTET_STRING).container("keyUsage", PKIX1Implicit88.keyUsage);
2209
+ names.lastOIDAndTypeIs("2.5.29.17", ASN1ItemType.OCTET_STRING).container("subjectAltName", PKIX1Implicit88.generalNames);
2210
+ names.lastOIDAndTypeIs("2.5.29.19", ASN1ItemType.OCTET_STRING).container("basicConstraints", PKIX1Implicit88.basicConstraints);
2211
+ names.lastOIDAndTypeIs("2.5.29.31", ASN1ItemType.OCTET_STRING).container("cRLDistributionPoints", PKIX1Implicit88.crlDistributionPoints);
2212
+ names.lastOIDAndTypeIs("2.5.29.32", ASN1ItemType.OCTET_STRING).container("certificatePolicies", PKIX1Implicit88.certificatePolicies);
2213
+ names.lastOIDAndTypeIs("2.5.29.35", ASN1ItemType.OCTET_STRING).container("authorityKeyIdentifier", PKIX1Implicit88.authorityKeyIdentifier);
2214
+ names.lastOIDAndTypeIs("2.5.29.37", ASN1ItemType.OCTET_STRING).container("extKeyUsage", PKIX1Implicit88.extKeyUsageSyntax);
2215
+ names.nextValue("extnValue");//////////////////////////////
2216
+ }
2217
+
2218
+ static certificateList(names)
2219
+ {
2220
+ names.typeIs(ASN1ItemType.SEQUENCE).container("CertificateList", PKIX1Explicit88.certificateListCont);
2221
+ }
2222
+
2223
+ static certificateListCont(names)
2224
+ {
2225
+ PKIX1Explicit88.addTBSCertList(names, "tbsCertList");
2226
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "signatureAlgorithm");
2227
+ names.typeIs(ASN1ItemType.BIT_STRING).nextValue("signature");
2228
+ }
2229
+
2230
+ static addTBSCertList(names, name)
2231
+ {
2232
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.tbsCertListCont);
2233
+ }
2234
+
2235
+ static tbsCertListCont(names)
2236
+ {
2237
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("Version");
2238
+ PKIX1Explicit88.addAlgorithmIdentifier(names, "signature");
2239
+ PKIX1Explicit88.addName(names, "issuer");
2240
+ names.typeIsTime().nextValue("thisUpdate");
2241
+ names.typeIsTime().nextValue("nextUpdate");
2242
+ names.typeIs(ASN1ItemType.SEQUENCE).container("revokedCertificates", PKIX1Explicit88.revokedCertificates);
2243
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("crlExtensions", PKIX1Explicit88.extensions);
2244
+ }
2245
+
2246
+ static revokedCertificates(names)
2247
+ {
2248
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("revokedCertificate", PKIX1Explicit88.revokedCertificateCont);
2249
+ }
2250
+
2251
+ static revokedCertificateCont(names)
2252
+ {
2253
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("userCertificate");
2254
+ names.typeIsTime().nextValue("revocationDate");
2255
+ names.typeIs(ASN1ItemType.SEQUENCE).container("crlEntryExtensions", PKIX1Explicit88.extensionsCont);
2256
+ }
2257
+
2258
+ static addAlgorithmIdentifier(names, name)
2259
+ {
2260
+ names.typeIs(ASN1ItemType.SEQUENCE).container(name, PKIX1Explicit88.algorithmIdentifierCont);
2261
+ }
2262
+
2263
+ static algorithmIdentifierCont(names)
2264
+ {
2265
+ names.typeIs(ASN1ItemType.OID).nextValue("algorithm");
2266
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.1.3", ASN1ItemType.SEQUENCE).container("parameters", General.pbeParam); //pbeWithSHAAnd3-KeyTripleDES-CBC
2267
+ names.lastOIDAndTypeIs("1.2.840.113549.1.12.1.6", ASN1ItemType.SEQUENCE).container("parameters", General.pbeParam); //pbeWithSHAAnd40BitRC2-CBC
2268
+ names.nextValue("parameters");
2269
+ }
2270
+ }
2271
+
2272
+ class PKIX1Implicit88
2273
+ {
2274
+ static authorityKeyIdentifier(names)
2275
+ {
2276
+ names.typeIs(ASN1ItemType.SEQUENCE).container("AuthorityKeyIdentifier", PKIX1Implicit88.authorityKeyIdentifierCont);
2277
+ }
2278
+
2279
+ static authorityKeyIdentifierCont(names)
2280
+ {
2281
+ names.typeIsOpt(0).nextValue("keyIdentifier");
2282
+ names.typeIsOpt(1).container("authorityCertIssuer", PKIX1Implicit88.generalNameCont);
2283
+ names.typeIsOpt(2).nextValue("authorityCertSerialNumber");
2284
+ }
2285
+
2286
+ static subjectKeyIdentifier(names)
2287
+ {
2288
+ names.typeIs(ASN1ItemType.OCTET_STRING).nextValue("SubjectKeyIdentifier");
2289
+ }
2290
+
2291
+ static keyUsage(names)
2292
+ {
2293
+ names.typeIs(ASN1ItemType.BIT_STRING).nextValue("KeyUsage");
2294
+ }
2295
+
2296
+ static certificatePolicies(names)
2297
+ {
2298
+ names.typeIs(ASN1ItemType.SEQUENCE).container("CertificatePolicies", PKIX1Implicit88.certificatePoliciesCont);
2299
+ }
2300
+
2301
+ static certificatePoliciesCont(names)
2302
+ {
2303
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("PolicyInformation", PKIX1Implicit88.policyInformationCont);
2304
+ }
2305
+
2306
+ static policyInformationCont(names)
2307
+ {
2308
+ names.typeIs(ASN1ItemType.OID).nextValue("policyIdentifier");
2309
+ names.typeIs(ASN1ItemType.SEQUENCE).container("policyQualifiers", PKIX1Implicit88.policyQualifiers);
2310
+ }
2311
+
2312
+ static policyQualifiers(names)
2313
+ {
2314
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("PolicyQualifierInfo", PKIX1Implicit88.policyQualifierInfoCont);
2315
+ }
2316
+
2317
+ static policyQualifierInfoCont(names)
2318
+ {
2319
+ names.typeIs(ASN1ItemType.OID).nextValue("policyQualifierId");
2320
+ names.nextValue("qualifier");
2321
+ }
2322
+
2323
+ static generalNames(names)
2324
+ {
2325
+ names.typeIs(ASN1ItemType.SEQUENCE).container("GeneralName", PKIX1Implicit88.generalNameCont);
2326
+ }
2327
+
2328
+ static generalNameCont(names)
2329
+ {
2330
+ names.typeIsOpt(0).nextValue("otherName");
2331
+ names.typeIsOpt(1).nextValue("rfc822Name");
2332
+ names.typeIsOpt(2).nextValue("dNSName");
2333
+ names.typeIsOpt(3).nextValue("x400Address");
2334
+ names.typeIsOpt(4).container("directoryName", PKIX1Explicit88.name);
2335
+ names.typeIsOpt(5).nextValue("ediPartyName");
2336
+ names.typeIsOpt(6).nextValue("uniformResourceIdentifier");
2337
+ names.typeIsOpt(7).nextValue("iPAddress");
2338
+ names.typeIsOpt(8).nextValue("registeredID");
2339
+ names.allNotMatch().nextValue("unknown");
2340
+ }
2341
+
2342
+ static basicConstraints(names)
2343
+ {
2344
+ names.typeIs(ASN1ItemType.SEQUENCE).container("BasicConstraints", PKIX1Implicit88.basicConstraintsCont);
2345
+ }
2346
+
2347
+ static basicConstraintsCont(names)
2348
+ {
2349
+ names.typeIs(ASN1ItemType.BOOLEAN).nextValue("cA");
2350
+ names.typeIs(ASN1ItemType.INTEGER).nextValue("pathLenConstraint");
2351
+ }
2352
+
2353
+ static crlDistributionPoints(names)
2354
+ {
2355
+ names.typeIs(ASN1ItemType.SEQUENCE).container("CRLDistributionPoints", PKIX1Implicit88.crlDistributionPointsCont);
2356
+ }
2357
+
2358
+ static crlDistributionPointsCont(names)
2359
+ {
2360
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("DistributionPoint", PKIX1Implicit88.distributionPointCont);
2361
+ }
2362
+
2363
+ static distributionPointCont(names)
2364
+ {
2365
+ names.typeIsOpt(0).container("distributionPoint", PKIX1Implicit88.distributionPointName);
2366
+ names.typeIsOpt(1).container("reasons", PKIX1Implicit88.reasonFlags);
2367
+ names.typeIsOpt(2).container("cRLIssuer", PKIX1Implicit88.generalNames);
2368
+ }
2369
+
2370
+ static distributionPointName(names)
2371
+ {
2372
+ names.typeIsOpt(0).container("fullName", PKIX1Implicit88.generalNameCont);
2373
+ names.typeIsOpt(1).container("nameRelativeToCRLIssuer", PKIX1Explicit88.relativeDistinguishedNameCont);
2374
+ }
2375
+
2376
+ static reasonFlags(names)
2377
+ {
2378
+ names.typeIs(ASN1ItemType.BIT_STRING).nextValue("ReasonFlags");
2379
+ }
2380
+
2381
+ static extKeyUsageSyntax(names)
2382
+ {
2383
+ names.typeIs(ASN1ItemType.SEQUENCE).container("ExtKeyUsageSyntax", PKIX1Implicit88.extKeyUsageSyntaxCont);
2384
+ }
2385
+
2386
+ static extKeyUsageSyntaxCont(names)
2387
+ {
2388
+ names.repeatIfTypeIs(ASN1ItemType.OID).nextValue("KeyPurposeId");
2389
+ }
2390
+ }
2391
+
2392
+ class RFC2459
2393
+ {
2394
+ static authorityInfoAccessSyntax(names)
2395
+ {
2396
+ names.typeIs(ASN1ItemType.SEQUENCE).container("AuthorityInfoAccessSyntax", RFC2459.authorityInfoAccessSyntaxCont);
2397
+ }
2398
+
2399
+ static authorityInfoAccessSyntaxCont(names)
2400
+ {
2401
+ names.repeatIfTypeIs(ASN1ItemType.SEQUENCE).container("AccessDescription", RFC2459.accessDescriptionCont);
2402
+ }
2403
+
2404
+ static accessDescriptionCont(names)
2405
+ {
2406
+ names.typeIs(ASN1ItemType.OID).nextValue("accessMethod");
2407
+ names.nextValue("accessLocation");
2408
+ }
2409
+ }
2410
+
2411
+ class RFC8551
2412
+ {
2413
+ static smimeEncryptionKeyPreference(names)
2414
+ {
2415
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_0).container("issuerAndSerialNumber", PKCS7.issuerAndSerialNumberCont);
2416
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_1).nextValue("receipentKeyId");//, RecipientKeyIdentifierCont);
2417
+ names.typeIs(ASN1ItemType.CONTEXT_SPECIFIC_2).nextValue("subjectAltKeyIdentifier");//, SubjectKeyIdentifierCont);
2418
+ }
2419
+ }
2420
+
2421
+ export class ASN1PDUBuilder
2422
+ {
2423
+ constructor()
2424
+ {
2425
+ this.seqOffset = [];
2426
+ this.buff = [];
2427
+ }
2428
+
2429
+ beginOther(type)
2430
+ {
2431
+ this.buff.push(type);
2432
+ this.buff.push(0);
2433
+ this.seqOffset.push(this.buff.length);
2434
+ }
2435
+
2436
+ beginSequence()
2437
+ {
2438
+ this.beginOther(0x30);
2439
+ }
2440
+
2441
+ beginSet()
2442
+ {
2443
+ this.beginOther(0x31);
2444
+ }
2445
+
2446
+ beginContentSpecific(n)
2447
+ {
2448
+ this.beginOther(0xa0 + n);
2449
+ }
2450
+
2451
+ endLevel()
2452
+ {
2453
+ if (this.seqOffset.length > 0)
2454
+ {
2455
+ let seqOffset = this.seqOffset.pop();
2456
+ let seqLen = this.buff.length - seqOffset;
2457
+ if (seqLen < 128)
2458
+ {
2459
+ this.buff[seqOffset - 1] = seqLen;
2460
+ }
2461
+ else if (seqLen < 256)
2462
+ {
2463
+ this.buff[seqOffset - 1] = 0x81;
2464
+ this.buff.splice(seqOffset, 0, seqLen);
2465
+ }
2466
+ else if (seqLen < 65536)
2467
+ {
2468
+ this.buff[seqOffset - 1] = 0x82;
2469
+ this.buff.splice(seqOffset, 0, seqLen >> 8, seqLen & 0xff);
2470
+ }
2471
+ else
2472
+ {
2473
+ this.buff[seqOffset - 1] = 0x83;
2474
+ this.buff.splice(seqOffset, 0, seqLen >> 16, (seqLen >> 8) & 0xff, seqLen & 0xff);
2475
+ }
2476
+ }
2477
+ }
2478
+
2479
+ endAll()
2480
+ {
2481
+ while (this.seqOffset.length > 0)
2482
+ {
2483
+ this.endLevel();
2484
+ }
2485
+ }
2486
+
2487
+ appendBool(v)
2488
+ {
2489
+ this.buff.push(1);
2490
+ this.buff.push(1);
2491
+ this.buff.push(v?0xFF:0);
2492
+ }
2493
+
2494
+ appendInt32(v)
2495
+ {
2496
+ this.buff.push(2);
2497
+ if (v < 128 && v >= -128)
2498
+ {
2499
+ this.buff.push(1);
2500
+ this.buff.push(v & 0xff);
2501
+ }
2502
+ else if (v < 32768 && v >= -32768)
2503
+ {
2504
+ this.buff.push(2);
2505
+ this.buff.push((v >> 8) & 0xff);
2506
+ this.buff.push(v & 0xff);
2507
+ }
2508
+ else if (v < 8388608 && v >= -8388608)
2509
+ {
2510
+ this.buff.push(3);
2511
+ this.buff.push((v >> 16) & 0xff);
2512
+ this.buff.push((v >> 8) & 0xff);
2513
+ this.buff.push(v & 0xff);
2514
+ }
2515
+ else
2516
+ {
2517
+ this.buff.push(4);
2518
+ this.buff.push((v >> 24) & 0xff);
2519
+ this.buff.push((v >> 16) & 0xff);
2520
+ this.buff.push((v >> 8) & 0xff);
2521
+ this.buff.push(v & 0xff);
2522
+ }
2523
+ }
2524
+
2525
+ appendBitString(bitLeft, buff)
2526
+ {
2527
+ this.appendTypeLen(3, buff.byteLength + 1);
2528
+ this.buff.push(bitLeft);
2529
+ this.appendArrayBuffer(buff);
2530
+ }
2531
+
2532
+ appendOctetString(buff)
2533
+ {
2534
+ if (buff instanceof ArrayBuffer)
2535
+ {
2536
+ this.appendTypeLen(4, buff.byteLength);
2537
+ this.appendArrayBuffer(buff);
2538
+ }
2539
+ else if (typeof buff == "string")
2540
+ {
2541
+ let b = new TextEncoder().encode(buff);
2542
+ this.appendTypeLen(4, b.byteLength);
2543
+ this.appendArrayBuffer(b);
2544
+ }
2545
+ else
2546
+ {
2547
+ throw new Error("Unknown type");
2548
+ }
2549
+ }
2550
+
2551
+ appendNull()
2552
+ {
2553
+ this.appendTypeLen(5, 0);
2554
+ }
2555
+
2556
+ appendOID(buff)
2557
+ {
2558
+ this.appendTypeLen(6, buff.byteLength);
2559
+ this.appendArrayBuffer(buff);
2560
+ }
2561
+
2562
+ appendOIDString(oidStr)
2563
+ {
2564
+ let buff = ASN1Util.oidText2PDU(oidStr);
2565
+ this.appendOID(new Uint8Array(buff).buffer);
2566
+ }
2567
+
2568
+ appendChoice(v)
2569
+ {
2570
+ this.buff.push(10);
2571
+ if (v < 128 && v >= -128)
2572
+ {
2573
+ this.buff.push(1);
2574
+ this.buff.push(v & 0xff);
2575
+ }
2576
+ else if (v < 32768 && v >= -32768)
2577
+ {
2578
+ this.buff.push(2);
2579
+ this.buff.push((v >> 8) & 0xff);
2580
+ this.buff.push(v & 0xff);
2581
+ }
2582
+ else if (v < 8388608 && v >= -8388608)
2583
+ {
2584
+ this.buff.push(3);
2585
+ this.buff.push((v >> 16) & 0xff);
2586
+ this.buff.push((v >> 8) & 0xff);
2587
+ this.buff.push(v & 0xff);
2588
+ }
2589
+ else
2590
+ {
2591
+ this.buff.push(4);
2592
+ this.buff.push((v >> 24) & 0xff);
2593
+ this.buff.push((v >> 16) & 0xff);
2594
+ this.buff.push((v >> 8) & 0xff);
2595
+ this.buff.push(v & 0xff);
2596
+ }
2597
+ }
2598
+
2599
+ appendPrintableString(s)
2600
+ {
2601
+ this.appendOther(0x13, new TextEncoder().encode(s).buffer);
2602
+ }
2603
+
2604
+ appendUTF8String(s)
2605
+ {
2606
+ this.appendOther(0xc, new TextEncoder().encode(s).buffer);
2607
+ }
2608
+
2609
+ appendIA5String(s)
2610
+ {
2611
+ this.appendOther(0x16, new TextEncoder().encode(s).buffer);
2612
+ }
2613
+
2614
+ appendUTCTime(t)
2615
+ {
2616
+ let s = t.toUTCTime().toString("yyMMddHHmmss")+"Z";
2617
+ this.appendOther(0x17, new TextEncoder().encode(s).buffer);
2618
+ }
2619
+
2620
+ appendOther(type, buff)
2621
+ {
2622
+ this.appendTypeLen(type, buff.byteLength);
2623
+ this.appendArrayBuffer(buff);
2624
+ }
2625
+
2626
+ appendContentSpecific(n, buff)
2627
+ {
2628
+ this.appendOther(0xa0 + n, buff);
2629
+ }
2630
+
2631
+ appendSequence(buff)
2632
+ {
2633
+ this.appendOther(0x30, buff);
2634
+ }
2635
+
2636
+ appendInteger(buff)
2637
+ {
2638
+ this.appendOther(2, buff);
2639
+ }
2640
+
2641
+ getArrayBuffer()
2642
+ {
2643
+ this.endAll();
2644
+ return new Uint8Array(this.buff).buffer;
2645
+ }
2646
+
2647
+ appendTypeLen(type, len)
2648
+ {
2649
+ this.buff.push(type);
2650
+ if (len < 128)
2651
+ {
2652
+ this.buff.push(len & 0xff);
2653
+ }
2654
+ else if (len < 256)
2655
+ {
2656
+ this.buff.push(0x81);
2657
+ this.buff.push(len);
2658
+ }
2659
+ else if (len < 65536)
2660
+ {
2661
+ this.buff.push(0x82);
2662
+ this.buff.push(len >> 8);
2663
+ this.buff.push(len & 0xff);
2664
+ }
2665
+ else if (len < 0x1000000)
2666
+ {
2667
+ this.buff.push(0x83);
2668
+ this.buff.push(len >> 16);
2669
+ this.buff.push((len >> 8) & 0xff);
2670
+ this.buff.push(len & 0xff);
2671
+ }
2672
+ else
2673
+ {
2674
+ this.buff.push(0x84);
2675
+ this.buff.push(len >> 24);
2676
+ this.buff.push((len >> 16) & 0xff);
2677
+ this.buff.push((len >> 8) & 0xff);
2678
+ this.buff.push(len & 0xff);
2679
+ }
2680
+ }
2681
+
2682
+ appendArrayBuffer(buff)
2683
+ {
2684
+ let arr = new Uint8Array(buff);
2685
+ let i = 0;
2686
+ while (i < arr.length)
2687
+ {
2688
+ this.buff.push(arr[i]);
2689
+ i++;
2690
+ }
2691
+ }
2692
+ }