@sswroom/sswr 1.5.4 → 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/cert.js ADDED
@@ -0,0 +1,4616 @@
1
+ import {ASN1ItemType, ASN1Util, ASN1Names, ASN1PDUBuilder} from "./certutil.js";
2
+ import * as data from "./data.js";
3
+ import * as hash from "./hash.js";
4
+ import * as net from "./net.js";
5
+ import * as text from "./text.js";
6
+
7
+ export const ASN1Type = {
8
+ X509: 0
9
+ }
10
+
11
+ export const X509FileType = {
12
+ Cert: 0,
13
+ Key: 1,
14
+ CertRequest: 2,
15
+ PrivateKey: 3,
16
+ PublicKey: 4,
17
+ PKCS7: 5,
18
+ PKCS12: 6,
19
+ CRL: 7,
20
+ FileList: 8
21
+ }
22
+
23
+ export const KeyType = {
24
+ Unknown: 0,
25
+ RSA: 1,
26
+ DSA: 2,
27
+ ECDSA: 3,
28
+ ED25519: 4,
29
+ RSAPublic: 5,
30
+ ECPublic: 6
31
+ }
32
+
33
+ export const CertValidStatus = {
34
+ Valid: "Valid",
35
+ SelfSigned: "SelfSigned",
36
+ SignatureInvalid: "SignatureInvalid",
37
+ Revoked: "Revoked",
38
+ FileFormatInvalid: "FileFormatInvalid",
39
+ UnknownIssuer: "UnknownIssuer",
40
+ Expired: "Expired",
41
+ UnsupportedAlgorithm: "UnsupportedAlgorithm"
42
+ }
43
+
44
+ export const AlgType = {
45
+ Unknown: "Unknown",
46
+ MD2WithRSAEncryption: "MD2WithRSAEncryption",
47
+ MD5WithRSAEncryption: "MD5WithRSAEncryption",
48
+ SHA1WithRSAEncryption: "SHA1WithRSAEncryption",
49
+ SHA256WithRSAEncryption: "SHA256WithRSAEncryption",
50
+ SHA384WithRSAEncryption: "SHA384WithRSAEncryption",
51
+ SHA512WithRSAEncryption: "SHA512WithRSAEncryption",
52
+ SHA224WithRSAEncryption: "SHA224WithRSAEncryption",
53
+ ECDSAWithSHA256: "ECDSAWithSHA256",
54
+ ECDSAWithSHA384: "ECDSAWithSHA384"
55
+ }
56
+
57
+ export const ECName = {
58
+ Unknown: 0,
59
+ secp256r1: 1,
60
+ secp384r1: 2,
61
+ secp521r1: 3
62
+ }
63
+
64
+ export const ContentDataType = {
65
+ Unknown: 0,
66
+ AuthenticatedSafe: 1
67
+ }
68
+
69
+ export class ASN1Data extends data.ParsedObject
70
+ {
71
+ constructor(sourceName, objType, buff)
72
+ {
73
+ super(sourceName, objType);
74
+ this.reader = new data.ByteReader(buff);
75
+ }
76
+
77
+ toASN1String()
78
+ {
79
+ let names = this.createNames();
80
+ let lines = [];
81
+ ASN1Util.pduToString(this.reader, 0, this.reader.getLength(), lines, 0, names);
82
+ return lines.join("\r\n");
83
+ }
84
+
85
+ getASN1Buff()
86
+ {
87
+ return this.reader;
88
+ }
89
+
90
+ static appendInteger(arr, reader, ofst, len)
91
+ {
92
+ if (len == 1)
93
+ {
94
+ arr.push(reader.readUInt8(ofst).toString());
95
+ }
96
+ else if (len == 2)
97
+ {
98
+ arr.push(reader.readInt16(ofst, false).toString());
99
+ }
100
+ else if (len == 3)
101
+ {
102
+ arr.push(reader.readInt24(ofst, false).toString());
103
+ }
104
+ else if (len == 4)
105
+ {
106
+ arr.push(reader.readInt32(ofst, false).toString());
107
+ }
108
+ else if (len == 8)
109
+ {
110
+ arr.push(reader.readInt64(ofst, false).toString());
111
+ }
112
+ else
113
+ {
114
+ arr.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(ofst, len)), ' ', null));
115
+ }
116
+ }
117
+ }
118
+
119
+ export class X509File extends ASN1Data
120
+ {
121
+ constructor(sourceName, objType, buff)
122
+ {
123
+ super(sourceName, objType, buff);
124
+ }
125
+
126
+ getASN1Type()
127
+ {
128
+ return ASN1Type.X509;
129
+ }
130
+
131
+ getCertCount()
132
+ {
133
+ return 0;
134
+ }
135
+
136
+ getCertName(index)
137
+ {
138
+ return null;
139
+ }
140
+
141
+ getNewCert(index)
142
+ {
143
+ return null;
144
+ }
145
+
146
+ toShortString()
147
+ {
148
+ return fileTypeGetName(this.getFileType())+": "+this.toShortName();
149
+ }
150
+
151
+ isSignatureKey(key)
152
+ {
153
+ let data = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1");
154
+ let signature = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3");
155
+ if (data == 0 || signature == 0 || signature.itemType != ASN1ItemType.BIT_STRING)
156
+ {
157
+ return false;
158
+ }
159
+ let signOfst = signature.rawOfst + signature.hdrLen;
160
+ let signSize = signature.contLen;
161
+ if (this.reader.readUInt8(signOfst) != 0)
162
+ return false;
163
+ signOfst++;
164
+ signSize--;
165
+ if (!key.signatureVerify(hash.HashType.SHA256, this.reader.getArrayBuffer(data.rawOfst, data.hdrLen + data.contLen), this.reader.getArrayBuffer(signOfst, signOfst + signSize)))
166
+ {
167
+ return false;
168
+ }
169
+ return true;
170
+ }
171
+
172
+ getSignedInfo()
173
+ {
174
+ let payload = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1");
175
+ if (payload == null)
176
+ {
177
+ return null;
178
+ }
179
+ let itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
180
+ let algType;
181
+ if (itemPDU == null || itemPDU.itemType != ASN1ItemType.SEQUENCE || (algType = X509File.algorithmIdentifierGet(this.reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen)) == AlgType.Unknown)
182
+ {
183
+ return null;
184
+ }
185
+ if ((itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3")) == null || itemPDU.itemType != ASN1ItemType.BIT_STRING)
186
+ {
187
+ return null;
188
+ }
189
+ return {payload: this.reader.getArrayBuffer(payload.rawOfst, payload.hdrLen + payload.contLen),
190
+ signature: this.reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen),
191
+ algType: algType};
192
+ }
193
+
194
+ static isSigned(reader, startOfst, endOfst, path)
195
+ {
196
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
197
+ if (cnt < 3)
198
+ {
199
+ return false;
200
+ }
201
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".2") != ASN1ItemType.SEQUENCE)
202
+ {
203
+ return false;
204
+ }
205
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".3") != ASN1ItemType.BIT_STRING)
206
+ {
207
+ return false;
208
+ }
209
+ return true;
210
+ }
211
+
212
+ static appendSigned(reader, startOfst, endOfst, path, sb, varName)
213
+ {
214
+ let name;
215
+ let itemPDU;
216
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+".2")) != null)
217
+ {
218
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
219
+ {
220
+ name = "algorithmIdentifier";
221
+ if (varName)
222
+ {
223
+ name = varName + "." + name;
224
+ }
225
+ X509File.appendAlgorithmIdentifier(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name, false);
226
+ }
227
+ }
228
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+".3")) != null)
229
+ {
230
+ if (itemPDU.itemType == ASN1ItemType.BIT_STRING)
231
+ {
232
+ if (varName)
233
+ {
234
+ sb.push(varName+".");
235
+ }
236
+ sb.push("signature = ");
237
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen + 1, itemPDU.contLen - 1)), ':', null));
238
+ sb.push("\r\n");
239
+ }
240
+ }
241
+ }
242
+
243
+ static isTBSCertificate(reader, startOfst, endOfst, path)
244
+ {
245
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
246
+ if (cnt < 6)
247
+ {
248
+ return false;
249
+ }
250
+ let i = 1;
251
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i)) == ASN1ItemType.CONTEXT_SPECIFIC_0)
252
+ {
253
+ i++;
254
+ }
255
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.INTEGER)
256
+ {
257
+ return false;
258
+ }
259
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
260
+ {
261
+ return false;
262
+ }
263
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
264
+ {
265
+ return false;
266
+ }
267
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
268
+ {
269
+ return false;
270
+ }
271
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
272
+ {
273
+ return false;
274
+ }
275
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
276
+ {
277
+ return false;
278
+ }
279
+ return true;
280
+ }
281
+
282
+ static appendTBSCertificate(reader, startOfst, endOfst, path, sb, varName)
283
+ {
284
+ let name;
285
+ let i = 1;
286
+ let itemPDU;
287
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i))) != null)
288
+ {
289
+ if (itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
290
+ {
291
+ if (varName)
292
+ {
293
+ sb.push(varName+".");
294
+ }
295
+ sb.push("version = ");
296
+ X509File.appendVersion(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1", sb);
297
+ sb.push("\r\n");
298
+ i++;
299
+ }
300
+ }
301
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
302
+ {
303
+ if (itemPDU.itemType == ASN1ItemType.INTEGER)
304
+ {
305
+ if (varName)
306
+ {
307
+ sb.push(varName+".");
308
+ }
309
+ sb.push("serialNumber = ");
310
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(itemPDU.dataOfst, itemPDU.contLen)), ':', null));
311
+ sb.push("\r\n");
312
+ }
313
+ }
314
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
315
+ {
316
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
317
+ {
318
+ name = "signature";
319
+ if (varName)
320
+ {
321
+ name = varName + "." + name;
322
+ }
323
+ X509File.appendAlgorithmIdentifier(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, name, false);
324
+ }
325
+ }
326
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
327
+ {
328
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
329
+ {
330
+ name = "issuer";
331
+ if (varName)
332
+ {
333
+ name = varName + "." + name;
334
+ }
335
+ X509File.appendName(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, name);
336
+ }
337
+ }
338
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
339
+ {
340
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
341
+ {
342
+ name = "validity";
343
+ if (varName)
344
+ {
345
+ name = varName + "." + name;
346
+ }
347
+ X509File.appendValidity(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name);
348
+ }
349
+ }
350
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
351
+ {
352
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
353
+ {
354
+ name = "subject";
355
+ if (varName)
356
+ {
357
+ name = varName + "." + name;
358
+ }
359
+ X509File.appendName(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name);
360
+ }
361
+ }
362
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
363
+ {
364
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
365
+ {
366
+ name = "subjectPublicKeyInfo";
367
+ if (varName)
368
+ {
369
+ name = varName + "." + name;
370
+ }
371
+ X509File.appendSubjectPublicKeyInfo(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name);
372
+ let pubKey = new X509PubKey(name, reader.getArrayBuffer(itemPDU.rawOfst, itemPDU.hdrLen + itemPDU.contLen));
373
+ let key = pubKey.createKey();
374
+ if (key)
375
+ {
376
+ sb.push(key.toString());
377
+ sb.push("\r\n");
378
+ }
379
+ }
380
+ }
381
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
382
+ {
383
+ if (itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_3)
384
+ {
385
+ name = "extensions";
386
+ if (varName)
387
+ {
388
+ name = varName + "." + name;
389
+ }
390
+ X509File.appendCRLExtensions(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name);
391
+ }
392
+ }
393
+ }
394
+
395
+ static isCertificate(reader, startOfst, endOfst, path)
396
+ {
397
+ return X509File.isSigned(reader, startOfst, endOfst, path) && X509File.isTBSCertificate(reader, startOfst, endOfst, path+".1");
398
+ }
399
+
400
+ static appendCertificate(reader, startOfst, endOfst, path, sb, varName)
401
+ {
402
+ X509File.appendTBSCertificate(reader, startOfst, endOfst, path+".1", sb, varName);
403
+ X509File.appendSigned(reader, startOfst, endOfst, path, sb, varName);
404
+ }
405
+
406
+ static isTBSCertList(reader, startOfst, endOfst, path)
407
+ {
408
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
409
+ let i = 1;
410
+ if (cnt < 4)
411
+ {
412
+ return false;
413
+ }
414
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i)) == ASN1ItemType.INTEGER)
415
+ {
416
+ i++;
417
+ }
418
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
419
+ {
420
+ return false;
421
+ }
422
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
423
+ {
424
+ return false;
425
+ }
426
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.UTCTIME)
427
+ {
428
+ return false;
429
+ }
430
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i)) == ASN1ItemType.UTCTIME)
431
+ {
432
+ i++;
433
+ }
434
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
435
+ {
436
+ return false;
437
+ }
438
+ let itemType = ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++));
439
+ if (itemType != ASN1ItemType.CONTEXT_SPECIFIC_0 && itemType != ASN1ItemType.UNKNOWN)
440
+ {
441
+ return false;
442
+ }
443
+ return true;
444
+ }
445
+
446
+ static appendTBSCertList(reader, startOfst, endOfst, path, sb, varName)
447
+ {
448
+ let dt;
449
+ let name;
450
+ let i = 1;
451
+ let itemPDU;
452
+ let subitemPDU;
453
+ let subsubitemPDU;
454
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i))) != null)
455
+ {
456
+ if (itemPDU.itemType == ASN1ItemType.INTEGER)
457
+ {
458
+ if (varName)
459
+ {
460
+ sb.push(varName+".");
461
+ }
462
+ sb.push("version = ");
463
+ X509File.appendVersion(reader, startOfst, endOfst, path+"."+(i), sb);
464
+ sb.push("\r\n");
465
+ i++;
466
+ }
467
+ }
468
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
469
+ {
470
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
471
+ {
472
+ name = "signature";
473
+ if (varName)
474
+ {
475
+ name = varName+"."+name;
476
+ }
477
+ X509File.appendAlgorithmIdentifier(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name, false);
478
+ }
479
+ }
480
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
481
+ {
482
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
483
+ {
484
+ name = "issuer";
485
+ if (varName)
486
+ {
487
+ name = varName+"."+name;
488
+ }
489
+ X509File.appendName(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, name);
490
+ }
491
+ }
492
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
493
+ {
494
+ if (itemPDU.itemType == ASN1ItemType.UTCTIME && (dt = ASN1Util.pduParseUTCTimeCont(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen)))
495
+ {
496
+ if (varName)
497
+ {
498
+ sb.push(varName+".");
499
+ }
500
+ sb.push("thisUpdate = ");
501
+ sb.push(dt.toStringNoZone());
502
+ sb.push("\r\n");
503
+ }
504
+ }
505
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i))) != null && itemPDU.itemType == ASN1ItemType.UTCTIME)
506
+ {
507
+ if (dt = ASN1Util.pduParseUTCTimeCont(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen))
508
+ {
509
+ if (varName)
510
+ {
511
+ sb.push(varName+".");
512
+ }
513
+ sb.push("nextUpdate = ");
514
+ sb.push(dt.toStringNoZone());
515
+ sb.push("\r\n");
516
+ }
517
+ i++;
518
+ }
519
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
520
+ {
521
+ let j = 0;
522
+ while (true)
523
+ {
524
+ j++;
525
+ if ((subitemPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, j.toString())) == null || subitemPDU.itemType != ASN1ItemType.SEQUENCE)
526
+ {
527
+ break;
528
+ }
529
+
530
+ if ((subsubitemPDU = ASN1Util.pduGetItem(reader, subitemPDU.dataOfst, subitemPDU.endOfst, "1")) != null && subsubitemPDU.itemType == ASN1ItemType.INTEGER)
531
+ {
532
+ if (varName)
533
+ {
534
+ sb.push(varName+".");
535
+ }
536
+ sb.push("revokedCertificates[");
537
+ sb.push(j.toString());
538
+ sb.push("].userCertificate = ");
539
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(subsubitemPDU.dataOfst, subsubitemPDU.contLen)), ':', null));
540
+ sb.push("\r\n");
541
+ }
542
+ if ((subsubitemPDU = ASN1Util.pduGetItem(reader, subitemPDU.dataOfst, subitemPDU.endOfst, "2")) != null && subsubitemPDU.itemType == ASN1ItemType.UTCTIME && (dt = ASN1Util.pduParseUTCTimeCont(reader, subsubitemPDU.rawOfst + subsubitemPDU.hdrLen, subsubitemPDU.rawOfst + subsubitemPDU.hdrLen + subsubitemPDU.contLen)))
543
+ {
544
+ if (varName)
545
+ {
546
+ sb.push(varName+".");
547
+ }
548
+ sb.push("revokedCertificates[");
549
+ sb.push(j);
550
+ sb.push("].revocationDate = ");
551
+ sb.push(dt.toStringNoZone());
552
+ sb.push("\r\n");
553
+ }
554
+ if ((subsubitemPDU = ASN1Util.pduGetItem(reader, subitemPDU.dataOfst, subitemPDU.endOfst, "3")) != null && subsubitemPDU.itemType == ASN1ItemType.SEQUENCE)
555
+ {
556
+ name = "revokedCertificates[";
557
+ if (varName)
558
+ {
559
+ name = varName+"."+name;
560
+ }
561
+ name = name + j + "].crlEntryExtensions";
562
+ X509File.appendCRLExtensions(reader, subsubitemPDU.rawOfst, subsubitemPDU.endOfst, sb, name);
563
+ }
564
+ }
565
+ }
566
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
567
+ {
568
+ if (itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
569
+ {
570
+ name = "crlExtensions";
571
+ if (varName)
572
+ {
573
+ name = varName + "." + name;
574
+ }
575
+ X509File.appendCRLExtensions(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, name);
576
+ }
577
+ }
578
+ }
579
+
580
+ static isCertificateList(reader, startOfst, endOfst, path)
581
+ {
582
+ return X509File.isSigned(reader, startOfst, endOfst, path) && X509File.isTBSCertList(reader, startOfst, endOfst, path+".1");
583
+ }
584
+
585
+ static appendCertificateList(reader, startOfst, endOfst, path, sb, varName)
586
+ {
587
+ X509File.appendTBSCertList(reader, startOfst, endOfst, path+".1", sb, varName);
588
+ X509File.appendSigned(reader, startOfst, endOfst, path, sb, varName);
589
+ }
590
+
591
+ /* static isPrivateKeyInfo(reader, startOfst, endOfst, path)
592
+ {
593
+ UOSInt cnt = ASN1Util.pduCountItem(pdu, pduEnd, path);
594
+ if (cnt != 3 && cnt != 4)
595
+ {
596
+ return false;
597
+ }
598
+ Char sbuff[256];
599
+ Char *sptr;
600
+ sptr = Text.StrConcat(sbuff, path);
601
+ Text.StrConcat(sptr, ".1");
602
+ if (ASN1Util.pduGetItemType(pdu, pduEnd, sbuff) != ASN1ItemType.INTEGER)
603
+ {
604
+ return false;
605
+ }
606
+ Text.StrConcat(sptr, ".2");
607
+ if (ASN1Util.pduGetItemType(pdu, pduEnd, sbuff) != ASN1ItemType.SEQUENCE)
608
+ {
609
+ return false;
610
+ }
611
+ Text.StrConcat(sptr, ".3");
612
+ if (ASN1Util.pduGetItemType(pdu, pduEnd, sbuff) != ASN1ItemType.OCTET_STRING)
613
+ {
614
+ return false;
615
+ }
616
+ if (cnt == 4)
617
+ {
618
+ Text.StrConcat(sptr, ".4");
619
+ if (ASN1Util.pduGetItemType(pdu, pduEnd, sbuff) != ASN1ItemType.SET)
620
+ {
621
+ return false;
622
+ }
623
+ }
624
+ return true;
625
+ }
626
+
627
+ static appendPrivateKeyInfo(reader, startOfst, endOfst, path, sb)
628
+ {
629
+ Char sbuff[256];
630
+ Char *sptr;
631
+ const UInt8 *itemPDU;
632
+ UOSInt len;
633
+ Net.ASN1Util.ItemType itemType;
634
+ KeyType keyType = KeyType.Unknown;
635
+ sptr = Text.StrConcat(sbuff, path);
636
+ Text.StrConcat(sptr, ".1");
637
+ if ((itemPDU = ASN1Util.pduGetItem(pdu, pduEnd, sbuff, len, itemType)) != 0)
638
+ {
639
+ if (itemType == ASN1ItemType.INTEGER)
640
+ {
641
+ sb.push("version = "));
642
+ AppendVersion(pdu, pduEnd, sbuff, sb);
643
+ sb.push("\r\n"));
644
+ }
645
+ }
646
+ Text.StrConcat(sptr, ".2");
647
+ if ((itemPDU = ASN1Util.pduGetItem(pdu, pduEnd, sbuff, len, itemType)) != 0)
648
+ {
649
+ if (itemType == ASN1ItemType.SEQUENCE)
650
+ {
651
+ AppendAlgorithmIdentifier(itemPDU, itemPDU + len, sb, CSTR("privateKeyAlgorithm"), false, &keyType);
652
+ }
653
+ }
654
+ Text.StrConcat(sptr, ".3");
655
+ if ((itemPDU = ASN1Util.pduGetItem(pdu, pduEnd, sbuff, len, itemType)) != 0)
656
+ {
657
+ if (itemType == ASN1ItemType.OCTET_STRING)
658
+ {
659
+ sb.push("privateKey = "));
660
+ sb.push("\r\n"));
661
+ if (keyType != KeyType.Unknown)
662
+ {
663
+ Crypto.Cert.X509Key privkey(CSTR("PrivKey"), Data.ByteArrayR(itemPDU, len), keyType);
664
+ privkey.ToString(sb);
665
+ }
666
+ }
667
+ }
668
+ }*/
669
+
670
+ static isCertificateRequestInfo(reader, startOfst, endOfst, path)
671
+ {
672
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
673
+ if (cnt < 4)
674
+ {
675
+ return false;
676
+ }
677
+ let i = 1;
678
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.INTEGER)
679
+ {
680
+ return false;
681
+ }
682
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
683
+ {
684
+ return false;
685
+ }
686
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.SEQUENCE)
687
+ {
688
+ return false;
689
+ }
690
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+"."+(i++)) != ASN1ItemType.CONTEXT_SPECIFIC_0)
691
+ {
692
+ return false;
693
+ }
694
+ return true;
695
+ }
696
+
697
+ static appendCertificateRequestInfo(reader, startOfst, endOfst, path, sb)
698
+ {
699
+ let i = 1;
700
+ let itemPDU;
701
+ let subitemPDU;
702
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i))) != null)
703
+ {
704
+ if (itemPDU.itemType == ASN1ItemType.INTEGER)
705
+ {
706
+ sb.push("serialNumber = ");
707
+ X509File.appendVersion(reader, startOfst, endOfst, path+"."+(i), sb);
708
+ sb.push("\r\n");
709
+ }
710
+ }
711
+ i++;
712
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
713
+ {
714
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
715
+ {
716
+ X509File.appendName(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, "subject");
717
+ }
718
+ }
719
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null)
720
+ {
721
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
722
+ {
723
+ X509File.appendSubjectPublicKeyInfo(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, "subjectPublicKeyInfo");
724
+ let pubKey = new X509PubKey("PubKey", reader.getArrayBuffer(itemPDU.rawOfst, itemPDU.hdrLen + itemPDU.contLen));
725
+ let key = pubKey.createKey();
726
+ if (key)
727
+ {
728
+ sb.push(key.toString() + "\r\n");
729
+ }
730
+ }
731
+ }
732
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path+"."+(i++))) != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
733
+ {
734
+ i = 1;
735
+ while ((subitemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString())) != null && subitemPDU.itemType == ASN1ItemType.SEQUENCE)
736
+ {
737
+ let extOID;
738
+ let ext;
739
+ if ((extOID = ASN1Util.pduGetItem(reader, subitemPDU.dataOfst, subitemPDU.endOfst, "1")) != null && extOID.itemType == ASN1ItemType.OID &&
740
+ (ext = ASN1Util.pduGetItem(reader, subitemPDU.dataOfst, subitemPDU.endOfst, "2")) != null && ext.itemType == ASN1ItemType.SET)
741
+ {
742
+ let oid = reader.getU8Arr(extOID.dataOfst, extOID.contLen);
743
+ if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.14"))
744
+ {
745
+ X509File.appendCRLExtensions(reader, ext.dataOfst, ext.endOfst, sb, "extensionRequest");
746
+ }
747
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.4.1.311.13.2.3")) //szOID_OS_VERSION
748
+ {
749
+ X509File.appendMSOSVersion(reader, ext.dataOfst, ext.endOfst, sb, "osVersion");
750
+ }
751
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.4.1.311.21.20")) //szOID_REQUEST_CLIENT_INFO
752
+ {
753
+ X509File.appendMSRequestClientInfo(reader, ext.dataOfst, ext.endOfst, sb, "reqClientInfo");
754
+ }
755
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.4.1.311.13.2.2")) //szOID_ENROLLMENT_CSP_PROVIDER
756
+ {
757
+ X509File.appendMSEnrollmentCSPProvider(reader, ext.dataOfst, ext.endOfst, sb, "enrollCSPProv");
758
+ }
759
+ }
760
+ i++;
761
+ }
762
+ }
763
+ }
764
+
765
+ static isCertificateRequest(reader, startOfst, endOfst, path)
766
+ {
767
+ return X509File.isSigned(reader, startOfst, endOfst, path) && X509File.isCertificateRequestInfo(reader, startOfst, endOfst, path+".1");
768
+ }
769
+
770
+ static appendCertificateRequest(reader, startOfst, endOfst, path, sb)
771
+ {
772
+ X509File.appendCertificateRequestInfo(reader, startOfst, endOfst, path+".1", sb);
773
+ X509File.appendSigned(reader, startOfst, endOfst, path, sb, null);
774
+ }
775
+
776
+ static isPublicKeyInfo(reader, startOfst, endOfst, path)
777
+ {
778
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
779
+ if (cnt < 2)
780
+ {
781
+ return false;
782
+ }
783
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".1") != ASN1ItemType.SEQUENCE)
784
+ {
785
+ return false;
786
+ }
787
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".2") != ASN1ItemType.BIT_STRING)
788
+ {
789
+ return false;
790
+ }
791
+ return true;
792
+ }
793
+
794
+ static appendPublicKeyInfo(reader, startOfst, endOfst, path, sb)
795
+ {
796
+ let buff = ASN1Util.pduGetItem(reader, startOfst, endOfst, path);
797
+ if (buff.itemType == ASN1ItemType.SEQUENCE)
798
+ {
799
+ X509File.appendSubjectPublicKeyInfo(reader, buff.dataOfst, buff.endOfst, sb, "PubKey");
800
+ let pubKey = new X509PubKey("PubKey", reader.getArrayBuffer(buff.rawOfst, buff.hdrLen + buff.contLen));
801
+ let key = pubKey.createKey();
802
+ if (key)
803
+ {
804
+ sb.push(key.toString()+"\r\n");
805
+ }
806
+ }
807
+ }
808
+
809
+ static isContentInfo(reader, startOfst, endOfst, path)
810
+ {
811
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path) != ASN1ItemType.SEQUENCE)
812
+ return false;
813
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
814
+ if (cnt != 2)
815
+ {
816
+ return false;
817
+ }
818
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".1") != ASN1ItemType.OID)
819
+ {
820
+ return false;
821
+ }
822
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".2") != ASN1ItemType.CONTEXT_SPECIFIC_0)
823
+ {
824
+ return false;
825
+ }
826
+ return true;
827
+ }
828
+
829
+ static appendContentInfo(reader, startOfst, endOfst, path, sb, varName, dataType)
830
+ {
831
+ let buff = ASN1Util.pduGetItem(reader, startOfst, endOfst, path);
832
+ if (buff.itemType == ASN1ItemType.SEQUENCE)
833
+ {
834
+ let contentType = ASN1Util.pduGetItem(reader, buff.dataOfst, buff.endOfst, "1");
835
+ let content = ASN1Util.pduGetItem(reader, buff.dataOfst, buff.endOfst, "2");
836
+
837
+ if (contentType)
838
+ {
839
+ if (varName)
840
+ {
841
+ sb.push(varName+".");
842
+ }
843
+ sb.push("content-type = ");
844
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(contentType.dataOfst, contentType.contLen)));
845
+ let oid = null; //oiddb.oidGetEntry(reader.getU8Arr(contentType.dataOfst, contentType.contLen));
846
+ if (oid)
847
+ {
848
+ sb.push(" ("+oid.name+')');
849
+ }
850
+ sb.push("\r\n");
851
+ }
852
+ if (contentType && content)
853
+ {
854
+ let oid = reader.getU8Arr(contentType.dataOfst, contentType.contLen);
855
+ if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.7.1")) //data
856
+ {
857
+ let itemPDU = ASN1Util.pduGetItem(reader, content.dataOfst, content.endOfst, "1");
858
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.OCTET_STRING)
859
+ {
860
+ X509File.appendData(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".pkcs7-content", dataType);
861
+ }
862
+ }
863
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.7.2")) //signedData
864
+ {
865
+ X509File.appendPKCS7SignedData(reader, content.dataOfst, content.endOfst, sb, varName+".pkcs7-content");
866
+ }
867
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.7.3")) //envelopedData
868
+ {
869
+
870
+ }
871
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.7.4")) //signedAndEnvelopedData
872
+ {
873
+
874
+ }
875
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.7.5")) //digestedData
876
+ {
877
+
878
+ }
879
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.7.6")) //encryptedData
880
+ {
881
+ X509File.appendEncryptedData(reader, content.dataOfst, content.endOfst, sb, varName + ".pkcs7-content", dataType);
882
+ }
883
+ }
884
+ }
885
+ }
886
+
887
+ static isPFX(reader, startOfst, endOfst, path)
888
+ {
889
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path) != ASN1ItemType.SEQUENCE)
890
+ return false;
891
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, path);
892
+ if (cnt != 2 && cnt != 3)
893
+ {
894
+ return false;
895
+ }
896
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".1") != ASN1ItemType.INTEGER)
897
+ {
898
+ return false;
899
+ }
900
+ if (!X509File.isContentInfo(reader, startOfst, endOfst, path+".2"))
901
+ {
902
+ return false;
903
+ }
904
+ if (cnt == 3)
905
+ {
906
+ if (ASN1Util.pduGetItemType(reader, startOfst, endOfst, path+".3") != ASN1ItemType.SEQUENCE)
907
+ {
908
+ return false;
909
+ }
910
+ }
911
+ return true;
912
+ }
913
+
914
+ static appendPFX(reader, startOfst, endOfst, path, sb, varName)
915
+ {
916
+ let buff = ASN1Util.pduGetItem(reader, startOfst, endOfst, path);
917
+ if (buff.itemType == ASN1ItemType.SEQUENCE)
918
+ {
919
+ let cnt = ASN1Util.pduCountItem(reader, buff.dataOfst, buff.endOfst, null);
920
+ let version = ASN1Util.pduGetItem(reader, buff.dataOfst, buff.endOfst, "1");
921
+ if (version && version.itemType == ASN1ItemType.INTEGER)
922
+ {
923
+ if (varName)
924
+ {
925
+ sb.push(varName+".");
926
+ }
927
+ sb.push("version = ");
928
+ sb.push(ASN1Util.integerToString(reader, version.dataOfst, version.contLen));
929
+ sb.push("\r\n");
930
+ }
931
+ var name = "authSafe";
932
+ if (varName)
933
+ {
934
+ name = varName + "."+name;
935
+ }
936
+ X509File.appendContentInfo(reader, buff.dataOfst, buff.endOfst, "2", sb, name, ContentDataType.AuthenticatedSafe);
937
+ if (cnt == 3)
938
+ {
939
+ name = "macData";
940
+ if (varName)
941
+ {
942
+ name = varName + "."+name;
943
+ }
944
+ X509File.appendMacData(reader, buff.dataOfst, buff.endOfst, "3", sb, name);
945
+ }
946
+ }
947
+ }
948
+
949
+ static appendVersion(reader, startOfst, endOfst, path, sb)
950
+ {
951
+ let itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path);
952
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.INTEGER)
953
+ {
954
+ if (itemPDU.contLen == 1)
955
+ {
956
+ switch (reader.readUInt8(itemPDU.rawOfst + itemPDU.hdrLen))
957
+ {
958
+ case 0:
959
+ sb.push("v1");
960
+ break;
961
+ case 1:
962
+ sb.push("v2");
963
+ break;
964
+ case 2:
965
+ sb.push("v3");
966
+ break;
967
+ }
968
+ }
969
+ }
970
+ }
971
+
972
+ static appendAlgorithmIdentifier(reader, startOfst, endOfst, sb, varName, pubKey)
973
+ {
974
+ let itemPDU;
975
+ let keyType = KeyType.Unknown;
976
+ let innerKeyType = KeyType.Unknown;
977
+ let algorithm = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
978
+ let parameters = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2");
979
+ if (algorithm && algorithm.itemType == ASN1ItemType.OID)
980
+ {
981
+ sb.push(varName+".");
982
+ sb.push("algorithm = ");
983
+ sb.push(ASN1Util.oidToString(new Uint8Array(reader.getArrayBuffer(algorithm.rawOfst + algorithm.hdrLen, algorithm.contLen))));
984
+ keyType = X509File.keyTypeFromOID(new Uint8Array(reader.getArrayBuffer(algorithm.rawOfst + algorithm.hdrLen, algorithm.contLen)), pubKey);
985
+ let oid = null; //oiddb.oidGetEntry(new Uint8Array(reader.getArrayBuffer(algorithm.rawOfst + algorithm.hdrLen, algorithm.contLen)));
986
+ if (oid)
987
+ {
988
+ sb.push(" (" + oid.name + ")");
989
+ }
990
+ sb.push("\r\n");
991
+ }
992
+ if (parameters)
993
+ {
994
+ let oid = new Uint8Array(reader.getArrayBuffer(algorithm.rawOfst + algorithm.hdrLen, algorithm.contLen));
995
+ if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.5.13") && parameters.itemType == ASN1ItemType.SEQUENCE)
996
+ {
997
+ if ((itemPDU = ASN1Util.pduGetItem(reader, parameters.rawOfst + parameters.hdrLen, parameters.rawOfst + parameters.hdrLen + parameters.contLen, "1")) != null)
998
+ {
999
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1000
+ {
1001
+ innerKeyType = X509File.appendAlgorithmIdentifier(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, varName + ".pbes2.kdf", true);
1002
+ }
1003
+ }
1004
+ if ((itemPDU = ASN1Util.pduGetItem(reader, parameters.rawOfst + parameters.hdrLen, parameters.rawOfst + parameters.hdrLen + parameters.contLen, "2")) != null)
1005
+ {
1006
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1007
+ {
1008
+ innerKeyType = X509File.appendAlgorithmIdentifier(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, varName + ".pbes2.encryptScheme", true);
1009
+ }
1010
+ }
1011
+ }
1012
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.5.12") && parameters.itemType == ASN1ItemType.SEQUENCE)
1013
+ {
1014
+ if ((itemPDU = ASN1Util.pduGetItem(reader, parameters.rawOfst + parameters.hdrLen, parameters.rawOfst + parameters.hdrLen + parameters.contLen, "1")) != null)
1015
+ {
1016
+ if (itemPDU.itemType == ASN1ItemType.OCTET_STRING)
1017
+ {
1018
+ sb.push(varName);
1019
+ sb.push(".pbkdf2.salt = ");
1020
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen)), ' ', null));
1021
+ sb.push("\r\n");
1022
+ }
1023
+ }
1024
+ if ((itemPDU = ASN1Util.pduGetItem(reader, parameters.rawOfst + parameters.hdrLen, parameters.rawOfst + parameters.hdrLen + parameters.contLen, "2")) != null)
1025
+ {
1026
+ if (itemPDU.itemType == ASN1ItemType.INTEGER)
1027
+ {
1028
+ sb.push(varName);
1029
+ sb.push(".pbkdf2.iterationCount = ");
1030
+ sb.push(ASN1Util.integerToString(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen));
1031
+ sb.push("\r\n");
1032
+ }
1033
+ }
1034
+ if ((itemPDU = ASN1Util.pduGetItem(reader, parameters.rawOfst + parameters.hdrLen, parameters.rawOfst + parameters.hdrLen + parameters.contLen, "3")) != null)
1035
+ {
1036
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1037
+ {
1038
+ innerKeyType = X509File.appendAlgorithmIdentifier(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, sb, varName + ".pbkdf2.prf", true);
1039
+ }
1040
+ }
1041
+ }
1042
+ else if (ASN1Util.oidEqualsText(oid, "2.16.840.1.101.3.4.1.42") && parameters.itemType == ASN1ItemType.OCTET_STRING)
1043
+ {
1044
+ sb.push(varName);
1045
+ sb.push(".aes256-cbc.iv = ");
1046
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(parameters.rawOfst + parameters.hdrLen, parameters.contLen)), ' ', null));
1047
+ sb.push("\r\n");
1048
+ }
1049
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.10045.2.1") && parameters.itemType == ASN1ItemType.OID)
1050
+ {
1051
+ sb.push(varName);
1052
+ sb.push(".ecPublicKey.parameters = ");
1053
+ sb.push(ASN1Util.oidToString(new Uint8Array(reader.getArrayBuffer(parameters.rawOfst + parameters.hdrLen, parameters.contLen))));
1054
+ let oidInfo = null; //oiddb.oidGetEntry(new Uint8Array(reader.getArrayBuffer(parameters.rawOfst + parameters.hdrLen, parameters.contLen)));
1055
+ if (oidInfo)
1056
+ {
1057
+ sb.push(" ("+oidInfo.name+")");
1058
+ }
1059
+ sb.push("\r\n");
1060
+ }
1061
+ else
1062
+ {
1063
+ sb.push(varName+".parameters = ");
1064
+ if (parameters.itemType == ASN1ItemType.NULL)
1065
+ {
1066
+ sb.push("NULL");
1067
+ }
1068
+ sb.push("\r\n");
1069
+ }
1070
+ }
1071
+ return keyType;
1072
+ }
1073
+
1074
+ static appendValidity(reader, startOfst, endOfst, sb, varName)
1075
+ {
1076
+ let dt;
1077
+ let itemPDU;
1078
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null)
1079
+ {
1080
+ if ((itemPDU.itemType == ASN1ItemType.UTCTIME || itemPDU.itemType == ASN1ItemType.GENERALIZEDTIME) && (dt = ASN1Util.pduParseUTCTimeCont(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen)))
1081
+ {
1082
+ sb.push(varName+".notBefore = ");
1083
+ sb.push(dt.toStringNoZone());
1084
+ sb.push("\r\n");
1085
+ }
1086
+ }
1087
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2")) != null)
1088
+ {
1089
+ if ((itemPDU.itemType == ASN1ItemType.UTCTIME || itemPDU.itemType == ASN1ItemType.GENERALIZEDTIME) && (dt = ASN1Util.pduParseUTCTimeCont(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen)))
1090
+ {
1091
+ sb.push(varName+".notAfter = ");
1092
+ sb.push(dt.toStringNoZone());
1093
+ sb.push("\r\n");
1094
+ }
1095
+ }
1096
+ }
1097
+
1098
+ static appendSubjectPublicKeyInfo(reader, startOfst, endOfst, sb, varName)
1099
+ {
1100
+ let itemPDU;
1101
+ let keyType = KeyType.Unknown;
1102
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null)
1103
+ {
1104
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1105
+ {
1106
+ keyType = X509File.appendAlgorithmIdentifier(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".algorithm", true);
1107
+ }
1108
+ }
1109
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2")) != null)
1110
+ {
1111
+ if (itemPDU.itemType == ASN1ItemType.BIT_STRING)
1112
+ {
1113
+ sb.push(varName);
1114
+ sb.push(".subjectPublicKey = ");
1115
+ sb.push(text.u8Arr2Hex(new Uint8Array(reader.getArrayBuffer(itemPDU.dataOfst + 1, itemPDU.contLen - 1)), ':', null));
1116
+ sb.push("\r\n");
1117
+ if (keyType != KeyType.Unknown)
1118
+ {
1119
+ let pkey = new X509Key(varName+".subjectPublicKey", reader.getArrayBuffer(itemPDU.dataOfst + 1, itemPDU.contLen - 1), keyType);
1120
+ sb.push(pkey.toString());
1121
+ sb.push("\r\n");
1122
+ }
1123
+ }
1124
+ }
1125
+ }
1126
+
1127
+ static appendName(reader, startOfst, endOfst, sb, varName)
1128
+ {
1129
+ let itemPDU;
1130
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
1131
+ let i = 0;
1132
+ while (i < cnt)
1133
+ {
1134
+ i++;
1135
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, i.toString())) != null)
1136
+ {
1137
+ if (itemPDU.itemType == ASN1ItemType.SET)
1138
+ {
1139
+ X509File.appendRelativeDistinguishedName(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName);
1140
+ }
1141
+ }
1142
+ }
1143
+ }
1144
+
1145
+ static appendRelativeDistinguishedName(reader, startOfst, endOfst, sb, varName)
1146
+ {
1147
+ let itemPDU;
1148
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
1149
+ let i = 0;
1150
+ while (i < cnt)
1151
+ {
1152
+ i++;
1153
+
1154
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, i.toString())) != null)
1155
+ {
1156
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1157
+ {
1158
+ X509File.appendAttributeTypeAndDistinguishedValue(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName);
1159
+ }
1160
+ }
1161
+ }
1162
+ }
1163
+
1164
+ static appendAttributeTypeAndDistinguishedValue(reader, startOfst, endOfst, sb, varName)
1165
+ {
1166
+ let typePDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
1167
+ let valuePDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2");
1168
+ if (typePDU && valuePDU && typePDU.itemType == ASN1ItemType.OID)
1169
+ {
1170
+ sb.push(varName+".");
1171
+ let typeOID = reader.getU8Arr(typePDU.dataOfst, typePDU.contLen);
1172
+ if (ASN1Util.oidEqualsText(typeOID, "2.5.4.3"))
1173
+ {
1174
+ sb.push("commonName");
1175
+ }
1176
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.4"))
1177
+ {
1178
+ sb.push("surname");
1179
+ }
1180
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.6"))
1181
+ {
1182
+ sb.push("countryName");
1183
+ }
1184
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.7"))
1185
+ {
1186
+ sb.push("localityName");
1187
+ }
1188
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.8"))
1189
+ {
1190
+ sb.push("stateOrProvinceName");
1191
+ }
1192
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.9"))
1193
+ {
1194
+ sb.push("streetAddress");
1195
+ }
1196
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.10"))
1197
+ {
1198
+ sb.push("organizationName");
1199
+ }
1200
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.11"))
1201
+ {
1202
+ sb.push("organizationalUnitName");
1203
+ }
1204
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.12"))
1205
+ {
1206
+ sb.push("title");
1207
+ }
1208
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.42"))
1209
+ {
1210
+ sb.push("givenName");
1211
+ }
1212
+ else if (ASN1Util.oidEqualsText(typeOID, "2.5.4.43"))
1213
+ {
1214
+ sb.push("initials");
1215
+ }
1216
+ else if (ASN1Util.oidEqualsText(typeOID, "0.9.2342.19200300.100.1.25"))
1217
+ {
1218
+ sb.push("domainComponent");
1219
+ }
1220
+ else if (ASN1Util.oidEqualsText(typeOID, "1.2.840.113549.1.9.1"))
1221
+ {
1222
+ sb.push("emailAddress");
1223
+ }
1224
+ else
1225
+ {
1226
+ sb.push(ASN1Util.oidToString(typeOID));
1227
+ }
1228
+ sb.push(" = ");
1229
+ if (valuePDU.itemType == ASN1ItemType.BMPSTRING)
1230
+ {
1231
+ sb.push(reader.readUTF16(valuePDU.rawOfst + valuePDU.hdrLen, valuePDU.contLen >> 1, false));
1232
+ }
1233
+ else
1234
+ {
1235
+ sb.push(reader.readUTF8(valuePDU.rawOfst + valuePDU.hdrLen, valuePDU.contLen));
1236
+ }
1237
+ sb.push("\r\n");
1238
+ }
1239
+ }
1240
+
1241
+ static appendCRLExtensions(reader, startOfst, endOfst, sb, varName)
1242
+ {
1243
+ let itemPDU;
1244
+ let subItemPDU;
1245
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null)
1246
+ {
1247
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE || itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
1248
+ {
1249
+ let i = 1;
1250
+ while ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, i.toString())) != null)
1251
+ {
1252
+ X509File.appendCRLExtension(reader, subItemPDU.rawOfst + subItemPDU.hdrLen, subItemPDU.rawOfst + subItemPDU.hdrLen + subItemPDU.contLen, sb, varName);
1253
+ i++;
1254
+ }
1255
+ }
1256
+ }
1257
+ }
1258
+
1259
+ static appendCRLExtension(reader, startOfst, endOfst, sb, varName)
1260
+ {
1261
+ let extension;
1262
+ let itemPDU;
1263
+ let subItemPDU;
1264
+ if ((extension = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null)
1265
+ {
1266
+ if (extension.itemType == ASN1ItemType.OID)
1267
+ {
1268
+ sb.push(varName+".");
1269
+ sb.push("extensionType = ");
1270
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(extension.dataOfst, extension.contLen)));
1271
+ let oid = null; //oiddb.oidGetEntry(reader.getU8Arr(extension.dataOfst, extension.contLen));
1272
+ if (oid)
1273
+ {
1274
+ sb.push(" ("+oid.name+')');
1275
+ }
1276
+ sb.push("\r\n");
1277
+ }
1278
+ }
1279
+ else
1280
+ {
1281
+ return;
1282
+ }
1283
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2")) != null)
1284
+ {
1285
+ if (itemPDU.itemType == ASN1ItemType.BOOLEAN)
1286
+ {
1287
+ sb.push(varName+".");
1288
+ sb.push("critical = ");
1289
+ sb.push(ASN1Util.booleanToString(reader, itemPDU.dataOfst, itemPDU.contLen));
1290
+ sb.push("\r\n");
1291
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "3")) == null)
1292
+ {
1293
+ return;
1294
+ }
1295
+ }
1296
+ if (itemPDU.itemType == ASN1ItemType.OCTET_STRING)
1297
+ {
1298
+ let extOID = reader.getU8Arr(extension.dataOfst, extension.contLen);
1299
+ if (ASN1Util.oidEqualsText(extOID, "1.3.6.1.5.5.7.1.1")) //id-pe-authorityInfoAccess
1300
+ {
1301
+ if ((itemPDU = ASN1Util.pduGetItem(itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1302
+ {
1303
+ let i = 1;
1304
+ while ((subItemPDU = ASN1Util.pduGetItem(itemPDU.dataOfst, itemPDU.endOfst, i.toString())) != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1305
+ {
1306
+ let descPDU;
1307
+ if ((descPDU = ASN1Util.pduGetItem(subItemPDU.dataOfst, subItemPDU.endOfst, "1")) != null && descPDU.itemType == ASN1ItemType.OID)
1308
+ {
1309
+ sb.push(varName);
1310
+ sb.push(".authorityInfoAccess[");
1311
+ sb.push(i.toString());
1312
+ sb.push("].accessMethod = ");
1313
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(descPDU.dataOfst, descPDU.contLen)));
1314
+ sb.push(" (");
1315
+ //sb.push(oiddb.oidToNameString(reader.getU8Arr(descPDU.dataOfst, descPDU.contLen));
1316
+ sb.push(")\r\n");
1317
+ }
1318
+ let name = varName + ".authorityInfoAccess[" + i + "].accessLocation";
1319
+ X509File.appendGeneralName(reader, subItemPDU.dataOfst, subItemPDU.endOfst, "2", sb, name);
1320
+
1321
+ i++;
1322
+ }
1323
+ }
1324
+ }
1325
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.14")) //id-ce-subjectKeyIdentifier
1326
+ {
1327
+ sb.push(varName);
1328
+ sb.push(".subjectKeyId = ");
1329
+ if (itemPDU.contLen == 22 && reader.readUInt8(itemPDU.dataOfst + 1) == 20)
1330
+ {
1331
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(itemPDU.dataOfst + 2, itemPDU.contLen - 2), ':', null));
1332
+ }
1333
+ else
1334
+ {
1335
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen), ':', null));
1336
+ }
1337
+ sb.push("\r\n");
1338
+ }
1339
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.15")) //id-ce-keyUsage
1340
+ {
1341
+ if ((subItemPDU = ASN1Util.pduGetItem(itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.BIT_STRING)
1342
+ {
1343
+ sb.push(varName);
1344
+ sb.push(".keyUsage =");
1345
+ if (subItemPDU.contLen >= 2)
1346
+ {
1347
+ let v = reader.readUInt8(subitemPDU.dataOfst + 1);
1348
+ if (v & 0x80) sb.push(" digitalSignature");
1349
+ if (v & 0x40) sb.push(" nonRepudiation");
1350
+ if (v & 0x20) sb.push(" keyEncipherment");
1351
+ if (v & 0x10) sb.push(" dataEncipherment");
1352
+ if (v & 0x8) sb.push(" keyAgreement");
1353
+ if (v & 0x4) sb.push(" keyCertSign");
1354
+ if (v & 0x2) sb.push(" cRLSign");
1355
+ if (v & 0x1) sb.push(" encipherOnly");
1356
+ }
1357
+ if (subItemPDU.contLen >= 3)
1358
+ {
1359
+ let v = reader.readUInt8(subitemPDU.dataOfst + 2);
1360
+ if (v & 0x80) sb.push(" decipherOnly");
1361
+ }
1362
+ sb.push("\r\n");
1363
+ }
1364
+ }
1365
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.17")) //id-ce-subjectAltName
1366
+ {
1367
+ let name = varName + ".subjectAltName";
1368
+ X509File.appendGeneralNames(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, name);
1369
+ }
1370
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.19")) //id-ce-basicConstraints
1371
+ {
1372
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1373
+ {
1374
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.BOOLEAN)
1375
+ {
1376
+ sb.push(varName);
1377
+ sb.push(".basicConstraints.cA = ");
1378
+ sb.push(ASN1Util.booleanToString(reader, subItemPDU.dataOfst, subItemPDU.contLen));
1379
+ sb.push("\r\n");
1380
+ }
1381
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2")) != null && subItemPDU.itemType == ASN1ItemType.INTEGER)
1382
+ {
1383
+ sb.push(varName);
1384
+ sb.push(".basicConstraints.pathLenConstraint = ");
1385
+ sb.push(ASN1Util.integerToString(reader, subItemPDU.dataOfst, subItemPDU.contLen));
1386
+ sb.push("\r\n");
1387
+ }
1388
+ }
1389
+ }
1390
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.20")) //id-ce-cRLNumber
1391
+ {
1392
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.INTEGER)
1393
+ {
1394
+ sb.push(varName);
1395
+ sb.push(".cRLNumber = ");
1396
+ sb.push(ASN1Util.integerToString(reader, subItemPDU.dataOfst, subItemPDU.contLen));
1397
+ sb.push("\r\n");
1398
+ }
1399
+ }
1400
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.21")) //id-ce-cRLReasons
1401
+ {
1402
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.ENUMERATED && subItemPDU.contLen == 1)
1403
+ {
1404
+ sb.push(varName);
1405
+ sb.push(".cRLReasons = ");
1406
+ switch (reader.readUInt8(subItemPDU.dataOfst))
1407
+ {
1408
+ case 0:
1409
+ sb.push("unspecified");
1410
+ break;
1411
+ case 1:
1412
+ sb.push("keyCompromise");
1413
+ break;
1414
+ case 2:
1415
+ sb.push("cACompromise");
1416
+ break;
1417
+ case 3:
1418
+ sb.push("affiliationChanged");
1419
+ break;
1420
+ case 4:
1421
+ sb.push("superseded");
1422
+ break;
1423
+ case 5:
1424
+ sb.push("cessationOfOperation");
1425
+ break;
1426
+ case 6:
1427
+ sb.push("certificateHold");
1428
+ break;
1429
+ case 8:
1430
+ sb.push("removeFromCRL");
1431
+ break;
1432
+ case 9:
1433
+ sb.push("privilegeWithdrawn");
1434
+ break;
1435
+ case 10:
1436
+ sb.push("aACompromise");
1437
+ break;
1438
+ default:
1439
+ sb.push("unknown");
1440
+ break;
1441
+ }
1442
+ sb.push("\r\n");
1443
+ }
1444
+ }
1445
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.31")) //id-ce-cRLDistributionPoints
1446
+ {
1447
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1448
+ {
1449
+ let i = 1;
1450
+ while (X509File.appendDistributionPoint(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString(), sb, varName))
1451
+ {
1452
+ i++;
1453
+ }
1454
+ }
1455
+ }
1456
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.32")) //id-ce-certificatePolicies
1457
+ {
1458
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1459
+ {
1460
+ let i = 1;
1461
+ while (X509File.appendPolicyInformation(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString(), sb, varName))
1462
+ {
1463
+ i++;
1464
+ }
1465
+ }
1466
+ }
1467
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.35")) //id-ce-authorityKeyIdentifier
1468
+ {
1469
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1470
+ {
1471
+ let i = 1;
1472
+ while ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString())) != null)
1473
+ {
1474
+ if (subItemPDU.itemType == 0x80)
1475
+ {
1476
+ sb.push(varName);
1477
+ sb.push(".authorityKey.keyId = ");
1478
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen), ':', null));
1479
+ sb.push("\r\n");
1480
+ }
1481
+ else if (subItemPDU.itemType == 0x81 || subItemPDU.itemType == 0xa1)
1482
+ {
1483
+ let name = varName + ".authorityKey.authorityCertIssuer";
1484
+ X509File.appendGeneralName(reader, subItemPDU.dataOfst, subItemPDU.endOfst, "1", sb, name);
1485
+ }
1486
+ else if (subItemPDU.itemType == 0x82)
1487
+ {
1488
+ sb.push(varName);
1489
+ sb.push(".authorityKey.authorityCertSerialNumber = ");
1490
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen), ':', null));
1491
+ sb.push("\r\n");
1492
+ }
1493
+ i++;
1494
+ }
1495
+ }
1496
+ }
1497
+ else if (ASN1Util.oidEqualsText(extOID, "2.5.29.37")) //id-ce-extKeyUsage
1498
+ {
1499
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1500
+ {
1501
+ let i = 1;
1502
+ sb.push(varName);
1503
+ sb.push(".extKeyUsage =");
1504
+
1505
+ while ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString())) != null)
1506
+ {
1507
+ if (subItemPDU.itemType == ASN1ItemType.OID)
1508
+ {
1509
+ let oid = reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen);
1510
+ if (ASN1Util.oidEqualsText(oid, "1.3.6.1.5.5.7.3.1"))
1511
+ {
1512
+ sb.push(" serverAuth");
1513
+ }
1514
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.5.5.7.3.2"))
1515
+ {
1516
+ sb.push(" clientAuth");
1517
+ }
1518
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.5.5.7.3.3"))
1519
+ {
1520
+ sb.push(" codeSigning");
1521
+ }
1522
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.5.5.7.3.4"))
1523
+ {
1524
+ sb.push(" emailProtection");
1525
+ }
1526
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.5.5.7.3.8"))
1527
+ {
1528
+ sb.push(" timeStamping");
1529
+ }
1530
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.5.5.7.3.9"))
1531
+ {
1532
+ sb.push(" OCSPSigning");
1533
+ }
1534
+ }
1535
+ i++;
1536
+ }
1537
+ sb.push("\r\n");
1538
+ }
1539
+ }
1540
+ }
1541
+ }
1542
+ }
1543
+
1544
+ /* static appendMSOSVersion(reader, startOfst, endOfst, sb, varName)
1545
+ {
1546
+ Net.ASN1Util.ItemType itemType;
1547
+ UOSInt itemLen;
1548
+ const UInt8 *itemPDU = ASN1Util.pduGetItem(pdu, pduEnd, "1", itemLen, itemType);
1549
+ if (itemType == Net.ASN1Util.ItemType.IT_IA5STRING)
1550
+ {
1551
+ sb.push(varName);
1552
+ sb.push(".version = "));
1553
+ sb->AppendC(itemPDU, itemLen);
1554
+ sb.push("\r\n"));
1555
+ }
1556
+ }
1557
+
1558
+ static appendMSRequestClientInfo(reader, startOfst, endOfst, sb, varName)
1559
+ {
1560
+ Net.ASN1Util.ItemType itemType;
1561
+ UOSInt itemLen;
1562
+ const UInt8 *itemPDU;
1563
+ if ((itemPDU = ASN1Util.pduGetItem(pdu, pduEnd, "1", itemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_SEQUENCE)
1564
+ {
1565
+ UOSInt subitemLen;
1566
+ const UInt8 *subitemPDU;
1567
+ if ((subitemPDU = ASN1Util.pduGetItem(itemPDU, itemPDU + itemLen, "1", subitemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_INTEGER)
1568
+ {
1569
+ sb.push(varName);
1570
+ sb.push(".unknown = "));
1571
+ Net.ASN1Util.IntegerToString(subitemPDU, subitemLen, sb);
1572
+ sb.push("\r\n"));
1573
+ }
1574
+ if ((subitemPDU = ASN1Util.pduGetItem(itemPDU, itemPDU + itemLen, "2", subitemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_UTF8STRING)
1575
+ {
1576
+ sb.push(varName);
1577
+ sb.push(".machine = "));
1578
+ sb->AppendC(subitemPDU, subitemLen);
1579
+ sb.push("\r\n"));
1580
+ }
1581
+ if ((subitemPDU = ASN1Util.pduGetItem(itemPDU, itemPDU + itemLen, "3", subitemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_UTF8STRING)
1582
+ {
1583
+ sb.push(varName);
1584
+ sb.push(".user = "));
1585
+ sb->AppendC(subitemPDU, subitemLen);
1586
+ sb.push("\r\n"));
1587
+ }
1588
+ if ((subitemPDU = ASN1Util.pduGetItem(itemPDU, itemPDU + itemLen, "4", subitemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_UTF8STRING)
1589
+ {
1590
+ sb.push(varName);
1591
+ sb.push(".software = "));
1592
+ sb->AppendC(subitemPDU, subitemLen);
1593
+ sb.push("\r\n"));
1594
+ }
1595
+ }
1596
+ }
1597
+
1598
+ static appendMSEnrollmentCSPProvider(reader, startOfst, endOfst, sb, varName)
1599
+ {
1600
+ Net.ASN1Util.ItemType itemType;
1601
+ UOSInt itemLen;
1602
+ const UInt8 *itemPDU;
1603
+ if ((itemPDU = ASN1Util.pduGetItem(pdu, pduEnd, "1", itemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_SEQUENCE)
1604
+ {
1605
+ UOSInt subitemLen;
1606
+ const UInt8 *subitemPDU;
1607
+ if ((subitemPDU = ASN1Util.pduGetItem(itemPDU, itemPDU + itemLen, "1", subitemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_INTEGER)
1608
+ {
1609
+ sb.push(varName);
1610
+ sb.push(".unknown = "));
1611
+ Net.ASN1Util.IntegerToString(subitemPDU, subitemLen, sb);
1612
+ sb.push("\r\n"));
1613
+ }
1614
+ if ((subitemPDU = ASN1Util.pduGetItem(itemPDU, itemPDU + itemLen, "2", subitemLen, itemType)) != 0 && itemType == Net.ASN1Util.ItemType.IT_BMPSTRING)
1615
+ {
1616
+ sb.push(varName);
1617
+ sb.push(".provider = "));
1618
+ sb->AppendUTF16BE(subitemPDU, subitemLen >> 1);
1619
+ sb.push("\r\n"));
1620
+ }
1621
+ }
1622
+ }*/
1623
+
1624
+ static appendGeneralNames(reader, startOfst, endOfst, sb, varName)
1625
+ {
1626
+ let itemPDU;
1627
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null)
1628
+ {
1629
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1630
+ {
1631
+ let i = 1;
1632
+ while (X509File.appendGeneralName(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString(), sb, varName))
1633
+ {
1634
+ i++;
1635
+ }
1636
+ }
1637
+ }
1638
+ }
1639
+
1640
+ static appendGeneralName(reader, startOfst, endOfst, path, sb, varName)
1641
+ {
1642
+ let subItemPDU;
1643
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path)) != null)
1644
+ {
1645
+ switch (0x8F & subItemPDU.itemType)
1646
+ {
1647
+ case 0x80:
1648
+ sb.push(varName);
1649
+ sb.push(".otherName = ");
1650
+ sb.push(reader.readUTF8(subItemPDU.dataOfst, subItemPDU.contLen));
1651
+ sb.push("\r\n");
1652
+ return true;
1653
+ case 0x81:
1654
+ sb.push(varName);
1655
+ sb.push(".rfc822Name = ");
1656
+ sb.push(reader.readUTF8(subItemPDU.dataOfst, subItemPDU.contLen));
1657
+ sb.push("\r\n");
1658
+ return true;
1659
+ case 0x82:
1660
+ sb.push(varName);
1661
+ sb.push(".dNSName = ");
1662
+ sb.push(reader.readUTF8(subItemPDU.dataOfst, subItemPDU.contLen));
1663
+ sb.push("\r\n");
1664
+ return true;
1665
+ case 0x83:
1666
+ sb.push(varName);
1667
+ sb.push(".x400Address = ");
1668
+ sb.push(reader.readUTF8(subItemPDU.dataOfst, subItemPDU.contLen));
1669
+ sb.push("\r\n");
1670
+ return true;
1671
+ case 0x84:
1672
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, subItemPDU.dataOfst, subItemPDU.endOfst, path)) != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1673
+ {
1674
+ let name = varName + ".directoryName";
1675
+ X509File.appendName(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, name);
1676
+ }
1677
+ return true;
1678
+ case 0x85:
1679
+ sb.push(varName);
1680
+ sb.push(".ediPartyName = ");
1681
+ sb.push(reader.readUTF8(subItemPDU.dataOfst, subItemPDU.contLen));
1682
+ sb.push("\r\n");
1683
+ return true;
1684
+ case 0x86:
1685
+ sb.push(varName);
1686
+ sb.push(".uniformResourceIdentifier = ");
1687
+ sb.push(reader.readUTF8(subItemPDU.dataOfst, subItemPDU.contLen));
1688
+ sb.push("\r\n");
1689
+ return true;
1690
+ case 0x87:
1691
+ sb.push(varName);
1692
+ sb.push(".iPAddress = ");
1693
+ if (subItemPDU.contLen == 4)
1694
+ {
1695
+ sb.push(net.getIPv4Name(reader.readInt32(subItemPDU.dataOfst, false)));
1696
+ }
1697
+ else if (subItemLen == 16)
1698
+ {
1699
+ /* Net.SocketUtil.AddressInfo addr;
1700
+ Net.SocketUtil.SetAddrInfoV6(addr, subItemPDU, 0);
1701
+ sptr = Net.SocketUtil.GetAddrName(sbuff, addr);
1702
+ sb->AppendP(sbuff, sptr);*/
1703
+ }
1704
+ sb.push("\r\n");
1705
+ return true;
1706
+ case 0x88:
1707
+ sb.push(varName);
1708
+ sb.push(".registeredID = ");
1709
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen)));
1710
+ {
1711
+ let ent = null; //oiddb.oidGetEntry(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen));
1712
+ if (ent)
1713
+ {
1714
+ sb.push(" ("+ent.name+')');
1715
+ }
1716
+ }
1717
+ sb.push("\r\n");
1718
+ return true;
1719
+ }
1720
+ }
1721
+ return false;
1722
+ }
1723
+
1724
+ static appendDistributionPoint(reader, startOfst, endOfst, path, sb, varName)
1725
+ {
1726
+ let itemPDU;
1727
+ let subItemPDU;
1728
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path)) != null)
1729
+ {
1730
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1731
+ {
1732
+ let i = 1;
1733
+ while ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, i.toString())) != null)
1734
+ {
1735
+ switch (subItemPDU.itemType)
1736
+ {
1737
+ case ASN1ItemType.CONTEXT_SPECIFIC_0:
1738
+ X509File.appendDistributionPointName(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".distributionPoint");
1739
+ break;
1740
+ case ASN1ItemType.CONTEXT_SPECIFIC_1:
1741
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, subItemPDU.dataOfst, subItemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.BIT_STRING)
1742
+ {
1743
+ sb.push(varName);
1744
+ sb.push(".reasons =");
1745
+ if (subItemPDU.contLen >= 2)
1746
+ {
1747
+ let v = reader.readUInt8(subItemPDU.dataOfst + 1);
1748
+ if (v & 0x80) sb.push("unused");
1749
+ if (v & 0x40) sb.push("keyCompromise");
1750
+ if (v & 0x20) sb.push("cACompromise");
1751
+ if (v & 0x10) sb.push("affiliationChanged");
1752
+ if (v & 0x8) sb.push("superseded");
1753
+ if (v & 0x4) sb.push("cessationOfOperation");
1754
+ if (v & 0x2) sb.push("certificateHold");
1755
+ if (v & 0x1) sb.push("privilegeWithdrawn");
1756
+ }
1757
+ if (subItemLen >= 3)
1758
+ {
1759
+ let v = reader.readUInt8(subItemPDU.dataOfst + 2);
1760
+ if (v & 0x80) sb.push("aACompromise");
1761
+ }
1762
+ sb.push("\r\n");
1763
+ }
1764
+ break;
1765
+ case ASN1ItemType.CONTEXT_SPECIFIC_2:
1766
+ X509File.appendGeneralNames(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".cRLIssuer");
1767
+ break;
1768
+ }
1769
+ i++;
1770
+ }
1771
+ return true;
1772
+ }
1773
+ }
1774
+ return false;
1775
+ }
1776
+
1777
+ static appendDistributionPointName(reader, startOfst, endOfst, sb, varName)
1778
+ {
1779
+ let i;
1780
+ let itemPDU;
1781
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null)
1782
+ {
1783
+ if (itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
1784
+ {
1785
+ i = 0;
1786
+ while (X509File.appendGeneralName(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i).toString(), sb, varName+".fullName"))
1787
+ {
1788
+ i++;
1789
+ }
1790
+ }
1791
+ else if (itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_1)
1792
+ {
1793
+ X509File.appendRelativeDistinguishedName(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".nameRelativeToCRLIssuer");
1794
+ }
1795
+ }
1796
+ }
1797
+
1798
+ static appendPolicyInformation(reader, startOfst, endOfst, path, sb, varName)
1799
+ {
1800
+ let itemPDU;
1801
+ let subItemPDU;
1802
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path)) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1803
+ {
1804
+ subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1");
1805
+ if (subItemPDU != null && subItemPDU.itemType == ASN1ItemType.OID)
1806
+ {
1807
+ sb.push(varName);
1808
+ sb.push(".policyIdentifier = ");
1809
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen)));
1810
+ sb.push(" (");
1811
+ //sb.push(oiddb.oidToNameString(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen)));
1812
+ sb.push(')');
1813
+ sb.push("\r\n");
1814
+ }
1815
+ subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2");
1816
+ if (subItemPDU != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1817
+ {
1818
+ let policyQualifierInfoPDU;
1819
+ let i = 0;
1820
+ while ((policyQualifierInfoPDU = ASN1Util.pduGetItem(reader, subItemPDU.dataOfst, subItemPDU.endOfst, (++i).toString())) != null && policyQualifierInfoPDU.itemType == ASN1ItemType.SEQUENCE)
1821
+ {
1822
+ if ((itemPDU = ASN1Util.pduGetItem(reader, policyQualifierInfoPDU.dataOfst, policyQualifierInfoPDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.OID)
1823
+ {
1824
+ sb.push(varName);
1825
+ sb.push(",policyQualifiers["+i+"].policyQualifierId = ");
1826
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen)));
1827
+ sb.push(" (");
1828
+ //sb.push(oiddb.oidToNameString(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen)));
1829
+ sb.push(')');
1830
+ sb.push("\r\n");
1831
+ }
1832
+ if ((itemPDU = ASN1Util.pduGetItem(reader, policyQualifierInfoPDU.dataOfst, policyQualifierInfoPDU.endOfst, "2")) != null)
1833
+ {
1834
+ if (itemPDU.itemType == ASN1ItemType.IA5STRING)
1835
+ {
1836
+ sb.push(varName);
1837
+ sb.push(".policyQualifiers["+i+"].qualifier = ");
1838
+ sb.push(reader.readUTF8(itemPDU.dataOfst, itemPDU.contLen));
1839
+ sb.push("\r\n");
1840
+ }
1841
+ else if (itemType == ASN1ItemType.SEQUENCE)
1842
+ {
1843
+ /////////////////////////////////// UserNotice
1844
+ }
1845
+ }
1846
+ }
1847
+ }
1848
+ return true;
1849
+ }
1850
+ return false;
1851
+ }
1852
+
1853
+ static appendPKCS7SignedData(reader, startOfst, endOfst, sb, varName)
1854
+ {
1855
+ let itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
1856
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1857
+ {
1858
+ let subItemPDU;
1859
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.INTEGER)
1860
+ {
1861
+ sb.push("signedData.version = ");
1862
+ sb.push(ASN1Util.integerToString(reader, subItemPDU.dataOfst, subItemPDU.contLen));
1863
+ sb.push("\r\n");
1864
+ }
1865
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2")) != null && subItemPDU.itemType == ASN1ItemType.SET)
1866
+ {
1867
+ X509File.appendPKCS7DigestAlgorithmIdentifiers(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, "signedData.digestAlgorithms");
1868
+ }
1869
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "3")) != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1870
+ {
1871
+ X509File.appendContentInfo(reader, subItemPDU.rawOfst, subItemPDU.endOfst, "1", sb, "signedData.contentInfo", ContentDataType.Unknown);
1872
+ }
1873
+ let i = 4;
1874
+ subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "4");
1875
+ if (subItemPDU != 0 && subItemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
1876
+ {
1877
+ X509File.appendCertificate(reader, subItemPDU.dataOfst, subItemPDU.endOfst, "1", sb, "signedData.certificates");
1878
+ subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i).toString());
1879
+ }
1880
+ if (subItemPDU != 0 && subItemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_1)
1881
+ {
1882
+ //AppendCertificate(subItemPDU, subItemPDU + subItemLen, "1", sb, CSTR("signedData.crls"));
1883
+ subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i).toString());
1884
+ }
1885
+ if (subItemPDU != 0 && subItemPDU.itemType == ASN1ItemType.SET)
1886
+ {
1887
+ X509File.appendPKCS7SignerInfos(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, "signedData.signerInfos");
1888
+ }
1889
+ }
1890
+ }
1891
+
1892
+ static appendPKCS7DigestAlgorithmIdentifiers(reader, startOfst, endOfst, sb, varName)
1893
+ {
1894
+ let itemPDU;
1895
+ let i = 0;
1896
+ while (true)
1897
+ {
1898
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, (++i).toString())) == null)
1899
+ {
1900
+ return;
1901
+ }
1902
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1903
+ {
1904
+ X509File.appendAlgorithmIdentifier(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName, false);
1905
+ }
1906
+ }
1907
+ }
1908
+
1909
+ static appendPKCS7SignerInfos(reader, startOfst, endOfst, sb, varName)
1910
+ {
1911
+ let itemPDU;
1912
+ let i = 0;
1913
+ while (true)
1914
+ {
1915
+ itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, (++i).toString());
1916
+ if (itemPDU == null)
1917
+ {
1918
+ break;
1919
+ }
1920
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
1921
+ {
1922
+ X509File.appendPKCS7SignerInfo(reader, itemPDU.rawOfst, itemPDU.endOfst, sb, varName);
1923
+ }
1924
+ }
1925
+ }
1926
+
1927
+ static appendPKCS7SignerInfo(reader, startOfst, endOfst, sb, varName)
1928
+ {
1929
+ let i;
1930
+ let itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
1931
+ if (itemPDU == null || itemPDU.itemType != ASN1ItemType.SEQUENCE)
1932
+ {
1933
+ return;
1934
+ }
1935
+ let subItemPDU;
1936
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.INTEGER)
1937
+ {
1938
+ sb.push(varName);
1939
+ sb.push(".version = ");
1940
+ sb.push(ASN1Util.integerToString(reader, subItemPDU.dataOfst, subItemPDU.contLen));
1941
+ sb.push("\r\n");
1942
+ }
1943
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2")) != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1944
+ {
1945
+ X509File.appendIssuerAndSerialNumber(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".issuerAndSerialNumber");
1946
+ }
1947
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "3")) != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1948
+ {
1949
+ X509File.appendAlgorithmIdentifier(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".digestAlgorithm", false);
1950
+ }
1951
+ i = 4;
1952
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "4")) != null && subItemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
1953
+ {
1954
+ X509File.appendPKCS7Attributes(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".authenticatedAttributes");
1955
+ subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i).toString());
1956
+ }
1957
+ if (subItemPDU != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
1958
+ {
1959
+ X509File.appendAlgorithmIdentifier(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".digestEncryptionAlgorithm", false);
1960
+ }
1961
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i).toString())) != null && subItemPDU.itemType == ASN1ItemType.OCTET_STRING)
1962
+ {
1963
+ sb.push(varName);
1964
+ sb.push(".encryptedDigest = ");
1965
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen), ':', null));
1966
+ sb.push("\r\n");
1967
+ }
1968
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i).toString())) != null && subItemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_1)
1969
+ {
1970
+ X509File.appendPKCS7Attributes(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".unauthenticatedAttributes");
1971
+ }
1972
+ }
1973
+
1974
+ static appendIssuerAndSerialNumber(reader, startOfst, endOfst, sb, varName)
1975
+ {
1976
+ let itemPDU;
1977
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
1978
+ {
1979
+ X509File.appendName(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".issuer");
1980
+ }
1981
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2")) != null && itemPDU.itemType == ASN1ItemType.INTEGER)
1982
+ {
1983
+ sb.push(varName);
1984
+ sb.push(".serialNumber = ");
1985
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen), ':', null));
1986
+ sb.push("\r\n");
1987
+ }
1988
+ }
1989
+
1990
+ static appendPKCS7Attributes(reader, startOfst, endOfst, sb, varName)
1991
+ {
1992
+ let itemPDU;
1993
+ let oidPDU;
1994
+ let valuePDU;
1995
+ let i = 0;
1996
+ while (true)
1997
+ {
1998
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, (++i).toString())) == null)
1999
+ {
2000
+ return;
2001
+ }
2002
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
2003
+ {
2004
+ oidPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1");
2005
+ valuePDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2");
2006
+ if (oidPDU != null && oidPDU.itemType == ASN1ItemType.OID)
2007
+ {
2008
+ sb.push(varName);
2009
+ sb.push(".attributeType = ");
2010
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(oidPDU.dataOfst, oidPDU.contLen)));
2011
+ let oid = null; //oiddb.oidGetEntry(reader.getU8Arr(oidPDU.dataOfst, oidPDU.contLen));
2012
+ if (oid)
2013
+ {
2014
+ sb.push(" ("+oid.name+")");
2015
+ }
2016
+ sb.push("\r\n");
2017
+ }
2018
+ if (valuePDU && valuePDU.itemType == ASN1ItemType.SET)
2019
+ {
2020
+ let oid = reader.getU8Arr(oidPDU.dataOfst, oidPDU.contLen);
2021
+ if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.3")) //contentType
2022
+ {
2023
+ if ((itemPDU = ASN1Util.pduGetItem(reader, valuePDU.dataOfst, valuePDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.OID)
2024
+ {
2025
+ sb.push(varName);
2026
+ sb.push(".contentType = ");
2027
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen)));
2028
+ let oid = null; //oiddb.oidGetEntry(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen));
2029
+ if (oid)
2030
+ {
2031
+ sb.push(" ("+oid.name+")");
2032
+ }
2033
+ sb.push("\r\n");
2034
+ }
2035
+ }
2036
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.4")) //messageDigest
2037
+ {
2038
+ if ((itemPDU = ASN1Util.pduGetItem(reader, valuePDU.dataOfst, valuePDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.OCTET_STRING)
2039
+ {
2040
+ sb.push(varName);
2041
+ sb.push(".messageDigest = ");
2042
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen), ':', null));
2043
+ sb.push("\r\n");
2044
+ }
2045
+ }
2046
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.5")) //signing-time
2047
+ {
2048
+ if ((itemPDU = ASN1Util.pduGetItem(reader, valuePDU.dataOfst, valuePDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.UTCTIME)
2049
+ {
2050
+ sb.push(varName);
2051
+ sb.push(".signing-time = ");
2052
+ sb.push(ASN1Util.utcTimeToString(reader, itemPDU.dataOfst, itemPDU.contLen));
2053
+ sb.push("\r\n");
2054
+ }
2055
+ }
2056
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.15")) //smimeCapabilities
2057
+ {
2058
+ /////////////////////////////////////
2059
+ }
2060
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.16.2.11")) //id-aa-encrypKeyPref
2061
+ {
2062
+ if ((itemPDU = ASN1Util.pduGetItem(reader, valuePDU.dataOfst, valuePDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2063
+ {
2064
+ X509File.appendIssuerAndSerialNumber(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".encrypKeyPref");
2065
+ }
2066
+ }
2067
+ else if (ASN1Util.oidEqualsText(oid, "1.3.6.1.4.1.311.16.4")) //outlookExpress
2068
+ {
2069
+ if ((itemPDU = ASN1Util.pduGetItem(reader, valuePDU.dataOfst, valuePDU.endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
2070
+ {
2071
+ X509File.appendIssuerAndSerialNumber(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".outlookExpress");
2072
+ }
2073
+ }
2074
+
2075
+ }
2076
+ }
2077
+ }
2078
+ }
2079
+
2080
+ static appendMacData(reader, startOfst, endOfst, path, sb, varName)
2081
+ {
2082
+ let itemPDU;
2083
+ let subItemPDU;
2084
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path)) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
2085
+ {
2086
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "1")) != null && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
2087
+ {
2088
+ X509File.appendDigestInfo(reader, subItemPDU.dataOfst, subItemPDU.endOfst, sb, varName+".mac");
2089
+ }
2090
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2")) != null && subItemPDU.itemType == ASN1ItemType.OCTET_STRING)
2091
+ {
2092
+ sb.push(varName);
2093
+ sb.push(".macSalt = ");
2094
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(subItemPDU.dataOfst, subItemPDU.contLen), ' ', null));
2095
+ sb.push("\r\n");
2096
+ }
2097
+ if ((subItemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "3")) != null && subItemPDU.itemType == ASN1ItemType.INTEGER)
2098
+ {
2099
+ sb.push(varName);
2100
+ sb.push(".iterations = ");
2101
+ sb.push(ASN1Util.integerToString(reader, subItemPDU.dataOfst, subItemPDU.contLen));
2102
+ sb.push("\r\n");
2103
+ }
2104
+ return true;
2105
+ }
2106
+ return false;
2107
+ }
2108
+
2109
+ static appendDigestInfo(reader, startOfst, endOfst, sb, varName)
2110
+ {
2111
+ let itemPDU;
2112
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
2113
+ {
2114
+ X509File.appendAlgorithmIdentifier(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, varName+".digestAlgorithm", false);
2115
+ }
2116
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2")) != null && itemPDU.itemType == ASN1ItemType.OCTET_STRING)
2117
+ {
2118
+ sb.push(varName);
2119
+ sb.push(".digest = ");
2120
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen), ' ', null));
2121
+ sb.push("\r\n");
2122
+ }
2123
+ }
2124
+
2125
+ static appendData(reader, startOfst, endOfst, sb, varName, dataType)
2126
+ {
2127
+ switch (dataType)
2128
+ {
2129
+ case ContentDataType.AuthenticatedSafe:
2130
+ X509File.appendAuthenticatedSafe(reader, startOfst, endOfst, sb, varName);
2131
+ break;
2132
+ case ContentDataType.Unknown:
2133
+ default:
2134
+ break;
2135
+ }
2136
+ }
2137
+
2138
+ static appendEncryptedData(reader, startOfst, endOfst, sb, varName, dataType)
2139
+ {
2140
+ let itemPDU;
2141
+ let subitemPDU;
2142
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
2143
+ {
2144
+ if (varName)
2145
+ {
2146
+ sb.push(varName+".");
2147
+ }
2148
+ sb.push("version = ");
2149
+ X509File.appendVersion(reader, itemPDU.dataOfst, itemPDU.endOfst, "1", sb);
2150
+ sb.push("\r\n");
2151
+
2152
+ if ((subitemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "2")) != null && subitemPDU.itemType == ASN1ItemType.SEQUENCE)
2153
+ {
2154
+ let name = "encryptedContentInfo";
2155
+ if (varName)
2156
+ {
2157
+ name = varName+"."+name;
2158
+ }
2159
+ X509File.appendEncryptedContentInfo(reader, subitemPDU.dataOfst, subitemPDU.endOfst, sb, name, dataType);
2160
+ }
2161
+ if ((subitemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, "3")) != null && subitemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2162
+ {
2163
+ let name = "unprotectedAttributes";
2164
+ if (varName.v)
2165
+ {
2166
+ name = varName+"."+name;
2167
+ }
2168
+ X509File.appendPKCS7Attributes(reader, subitemPDU.dataOfst, subitemPDU.endOfst, sb, name);
2169
+ }
2170
+ }
2171
+ }
2172
+
2173
+ static appendAuthenticatedSafe(reader, startOfst, endOfst, sb, varName)
2174
+ {
2175
+ let itemPDU;
2176
+ let i;
2177
+ let j;
2178
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.SEQUENCE)
2179
+ {
2180
+ i = 0;
2181
+ j = ASN1Util.pduCountItem(reader, itemPDU.dataOfst, itemPDU.endOfst, null);
2182
+ while (i < j)
2183
+ {
2184
+ X509File.appendContentInfo(reader, itemPDU.dataOfst, itemPDU.endOfst, (i + 1).toString(), sb, varName+"["+i+"]", ContentDataType.Unknown);
2185
+ i++;
2186
+ }
2187
+ }
2188
+ }
2189
+
2190
+ static appendEncryptedContentInfo(reader, startOfst, endOfst, sb, varName, dataType)
2191
+ {
2192
+ let itemPDU;
2193
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != 0 && itemPDU.itemType == ASN1ItemType.OID)
2194
+ {
2195
+ sb.push(varName);
2196
+ sb.push(".contentType = ");
2197
+ sb.push(ASN1Util.oidToString(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen)));
2198
+ let oid = null; //oiddb.oidGetEntry(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen));
2199
+ if (oid)
2200
+ {
2201
+ sb.push(" ("+oid.name+")");
2202
+ }
2203
+ sb.push("\r\n");
2204
+ }
2205
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2")) != 0 && itemPDU.itemType == ASN1ItemType.SEQUENCE)
2206
+ {
2207
+ let name = "contentEncryptionAlgorithm";
2208
+ if (varName)
2209
+ {
2210
+ name = varName+"."+name;
2211
+ }
2212
+ X509File.appendAlgorithmIdentifier(reader, itemPDU.dataOfst, itemPDU.endOfst, sb, name, false);
2213
+ }
2214
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "3")) != null && (itemPDU.itemType & 0x8F) == 0x80)
2215
+ {
2216
+ sb.push(varName);
2217
+ sb.push(".encryptedContent = ");
2218
+ sb.push(text.u8Arr2Hex(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen), ' ', null));
2219
+ sb.push("\r\n");
2220
+ }
2221
+ }
2222
+
2223
+ static nameGetByOID(reader, startOfst, endOfst, oidText)
2224
+ {
2225
+ if (!(reader instanceof data.ByteReader))
2226
+ return null;
2227
+ let itemPDU;
2228
+ let oidPDU;
2229
+ let strPDU;
2230
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
2231
+ let i = 0;
2232
+ while (i < cnt)
2233
+ {
2234
+ i++;
2235
+
2236
+ let path = i.toString()+".1";
2237
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, path)) != null)
2238
+ {
2239
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
2240
+ {
2241
+ oidPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
2242
+ if (oidPDU != null && oidPDU.itemType == ASN1ItemType.OID && ASN1Util.oidEqualsText(new Uint8Array(reader.getArrayBuffer(oidPDU.rawOfst + oidPDU.hdrLen, oidPDU.contLen)), oidText))
2243
+ {
2244
+ strPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "2");
2245
+ if (strPDU)
2246
+ {
2247
+ if (strPDU.itemType == ASN1ItemType.BMPSTRING)
2248
+ {
2249
+ return reader.readUTF16(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen >> 1, false);
2250
+ }
2251
+ else
2252
+ {
2253
+ return reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2254
+ }
2255
+ }
2256
+ }
2257
+ }
2258
+ }
2259
+ }
2260
+ return null;
2261
+ }
2262
+
2263
+ static nameGetCN(reader, startOfst, endOfst)
2264
+ {
2265
+ return X509File.nameGetByOID(reader, startOfst, endOfst, "2.5.4.3");
2266
+ }
2267
+
2268
+ static namesGet(reader, startOfst, endOfst)
2269
+ {
2270
+ let names = {};
2271
+ if (!(reader instanceof data.ByteReader))
2272
+ return names;
2273
+ let itemPDU;
2274
+ let oidPDU;
2275
+ let strPDU;
2276
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
2277
+ let i = 0;
2278
+ while (i < cnt)
2279
+ {
2280
+ i++;
2281
+
2282
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, i.toString()+".1")) != null)
2283
+ {
2284
+ if (itemType == ASN1ItemType.SEQUENCE)
2285
+ {
2286
+ oidPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
2287
+ if (oidPDU != null && oidPDU.itemType == ASN1ItemType.OID)
2288
+ {
2289
+ strPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "2");
2290
+ if (strPDU)
2291
+ {
2292
+ let oid = new Uint8Array(reader.getArrayBuffer(oidPDU.rawOfst + oidPDU.hdrLen, oidPDU.contLen));
2293
+ if (ASN1Util.oidEqualsText(oid, "2.5.4.6"))
2294
+ {
2295
+ names.countryName = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2296
+ }
2297
+ else if (ASN1Util.oidEqualsText(oid, "2.5.4.8"))
2298
+ {
2299
+ names.stateOrProvinceName = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2300
+ }
2301
+ else if (ASN1Util.oidEqualsText(oid, "2.5.4.7"))
2302
+ {
2303
+ names.localityName = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2304
+ }
2305
+ else if (ASN1Util.oidEqualsText(oid, "2.5.4.10"))
2306
+ {
2307
+ names.organizationName = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2308
+ }
2309
+ else if (ASN1Util.oidEqualsText(oid, "2.5.4.11"))
2310
+ {
2311
+ names.organizationUnitName = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2312
+ }
2313
+ else if (ASN1Util.oidEqualsText(oid, "2.5.4.3"))
2314
+ {
2315
+ names.commonName = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2316
+ }
2317
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.9.1"))
2318
+ {
2319
+ names.emailAddress = reader.readUTF8(strPDU.rawOfst + strPDU.hdrLen, strPDU.contLen);
2320
+ }
2321
+ }
2322
+ }
2323
+ }
2324
+ }
2325
+ }
2326
+ return names;
2327
+ }
2328
+
2329
+ static extensionsGet(reader, startOfst, endOfst)
2330
+ {
2331
+ let ext = {};
2332
+ if (!(reader instanceof data.ByteReader))
2333
+ return ext;
2334
+ let itemPDU;
2335
+ let oidPDU;
2336
+ let strPDU;
2337
+ let subItemPDU;
2338
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
2339
+ let i = 0;
2340
+ while (i < cnt)
2341
+ {
2342
+ i++;
2343
+
2344
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, i.toString())) != null)
2345
+ {
2346
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
2347
+ {
2348
+ oidPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
2349
+ if (oidPDU != null && oidPDU.itemType == ASN1ItemType.OID)
2350
+ {
2351
+ strPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "2");
2352
+ if (strPDU && strPDU.itemType == ASN1ItemType.BOOLEAN)
2353
+ {
2354
+ strPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "3");
2355
+ }
2356
+ if (strPDU && strPDU.itemType == ASN1ItemType.OCTET_STRING)
2357
+ {
2358
+ let oid = new Uint8Array(reader.getArrayBuffer(oidPDU.rawOfst + oidPDU.hdrLen, oidPDU.contLen));
2359
+ if (ASN1Util.oidEqualsText(oid, "2.5.29.17")) //id-ce-subjectAltName
2360
+ {
2361
+ ext.subjectAltName = [];
2362
+ let j = 0;
2363
+ let k = ASN1Util.pduCountItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1");
2364
+ while (j < k)
2365
+ {
2366
+ j++;
2367
+ subItemPDU = ASN1Util.pduGetItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1."+j);
2368
+ if (subItemPDU)
2369
+ {
2370
+ if (subItemPDU.itemType == 0x87)
2371
+ {
2372
+ ext.subjectAltName.push(net.getIPv4Name(reader.readInt32(subItemPDU.rawOfst + subItemPDU.hdrLen, false)));
2373
+ }
2374
+ else
2375
+ {
2376
+ ext.subjectAltName.push(reader.readUTF8(subItemPDU.rawOfst + subItemPDU.hdrLen, subItemPDU.contLen));
2377
+ }
2378
+ }
2379
+ }
2380
+ }
2381
+ else if (ASN1Util.oidEqualsText(oid, "2.5.29.14")) //id-ce-subjectKeyIdentifier
2382
+ {
2383
+ subItemPDU = ASN1Util.pduGetItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1");
2384
+ if (subItemPDU && subItemPDU.contLen == 20)
2385
+ {
2386
+ ext.subjKeyId = reader.getArrayBuffer(subItemPDU.rawOfst + subItemPDU.hdrLen, subItemPDU.contLen);
2387
+ }
2388
+ }
2389
+ else if (ASN1Util.oidEqualsText(oid, "2.5.29.35")) //id-ce-authorityKeyIdentifier
2390
+ {
2391
+ subItemPDU = ASN1Util.pduGetItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1.1");
2392
+ if (subItemPDU && subItemPDU.contLen == 20)
2393
+ {
2394
+ ext.authKeyId = reader.getArrayBuffer(subItemPDU.rawOfst + subItemPDU.hdrLen, subItemPDU.contLen);
2395
+ }
2396
+ }
2397
+ else if (ASN1Util.oidEqualsText(oid, "2.5.29.15")) //id-ce-keyUsage
2398
+ {
2399
+ subItemPDU = ASN1Util.pduGetItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1");
2400
+ if (subItemPDU && subItemPDU.itemType == ASN1ItemType.BIT_STRING && subItemPDU.contLen >= 2)
2401
+ {
2402
+ let v = reader.readUInt8(subItemPDU.rawOfst + subItemPDU.hdrLen + 1);
2403
+ ext.caCert = (v & 6) != 0;
2404
+ ext.digitalSign = (v & 0x80) != 0;
2405
+ }
2406
+ }
2407
+ }
2408
+ }
2409
+ }
2410
+ }
2411
+ }
2412
+ return ext;
2413
+ }
2414
+
2415
+ static extensionsGetCRLDistributionPoints(reader, startOfst, endOfst)
2416
+ {
2417
+ let ret = [];
2418
+ let itemPDU;
2419
+ let oidPDU;
2420
+ let strPDU;
2421
+ let subItemPDU;
2422
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
2423
+ let i = 0;
2424
+ while (i < cnt)
2425
+ {
2426
+ i++;
2427
+
2428
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, i.toString())) != null)
2429
+ {
2430
+ if (itemPDU.itemType == ASN1ItemType.SEQUENCE)
2431
+ {
2432
+ oidPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
2433
+ if (oidPDU != null && oidPDU.itemType == ASN1ItemType.OID)
2434
+ {
2435
+ strPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "2");
2436
+ if (strPDU && strPDU.itemType == ASN1ItemType.BOOLEAN)
2437
+ {
2438
+ strPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "3");
2439
+ }
2440
+ if (strPDU && strPDU.itemType == ASN1ItemType.OCTET_STRING)
2441
+ {
2442
+ if (ASN1Util.oidEqualsText(new Uint8Array(reader.getArrayBuffer(oidPDU.rawOfst + oidPDU.hdrLen, oidPDU.contLen)), "2.5.29.31")) //id-ce-cRLDistributionPoints
2443
+ {
2444
+ let j = 0;
2445
+ let k = ASN1Util.pduCountItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1");
2446
+ while (j < k)
2447
+ {
2448
+ j++;
2449
+ subItemPDU = ASN1Util.pduGetItem(reader, strPDU.rawOfst + strPDU.hdrLen, strPDU.rawOfst + strPDU.hdrLen + strPDU.contLen, "1."+j);
2450
+ if (subItemPDU && subItemPDU.itemType == ASN1ItemType.SEQUENCE)
2451
+ {
2452
+ X509File.distributionPointAdd(reader, subItemPDU.rawOfst + subItemPDU.hdrLen, subItemPDU.rawOfst + subItemPDU.hdrLen + subItemPDU.contLen, ret);
2453
+ }
2454
+ }
2455
+ }
2456
+ }
2457
+ }
2458
+ }
2459
+ }
2460
+ }
2461
+ return ret;
2462
+ }
2463
+
2464
+ static distributionPointAdd(reader, startOfst, endOfst, distPoints)
2465
+ {
2466
+ let itemPDU;
2467
+ if ((itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1")) != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2468
+ {
2469
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst, itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1")) != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2470
+ {
2471
+ if ((itemPDU = ASN1Util.pduGetItem(reader, itemPDU.rawOfst, itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1")) != null && itemPDU.itemType == ASN1ItemType.CHOICE_6)
2472
+ {
2473
+ distPoints.push(reader.readUTF8(itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen));
2474
+ return true;
2475
+ }
2476
+ }
2477
+ }
2478
+ return false;
2479
+ }
2480
+
2481
+ static publicKeyGetNew(reader, startOfst, endOfst)
2482
+ {
2483
+ let oidPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1.1");
2484
+ let bstrPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "2");
2485
+ if (oidPDU != null && oidPDU.itemType == ASN1ItemType.OID && bstrPDU != null && bstrPDU.itemType == ASN1ItemType.BIT_STRING)
2486
+ {
2487
+ let keyType = X509File.keyTypeFromOID(reader.getArrayBuffer(oidPDU.rawOfst + oidPDU.hdrLen, oidPDU.contLen), true);
2488
+ if (keyType == KeyType.ECPublic)
2489
+ {
2490
+ let paramPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1.2");
2491
+ if (paramPDU != null && paramPDU.itemType == ASN1ItemType.OID)
2492
+ {
2493
+ return X509Key.fromECPublicKey(reader.getArrayBuffer(bstrPDU.rawOfst + bstrPDU.hdrLen + 1, bstrPDU.contLen - 1), reader.getArrayBuffer(paramPDU.rawOfst + paramPDU.hdrLen, paramPDU.contLen));
2494
+ }
2495
+ }
2496
+ else if (keyType != KeyType.Unknown)
2497
+ {
2498
+ return new X509Key("public.key", reader.getArrayBuffer(bstrPDU.rawOfst + bstrPDU.hdrLen + 1, bstrPDU.contLen - 1), keyType);
2499
+ }
2500
+ }
2501
+ return null;
2502
+ }
2503
+
2504
+ static keyGetLeng(reader, startOfst, endOfst, keyType)
2505
+ {
2506
+ let keyPDU;
2507
+ switch (keyType)
2508
+ {
2509
+ case KeyType.RSA:
2510
+ keyPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
2511
+ if (keyPDU && keyPDU.itemType == ASN1ItemType.SEQUENCE)
2512
+ {
2513
+ let cnt = ASN1Util.pduCountItem(reader, keyPDU.rawOfst + keyPDU.hdrLen, keyPDU.rawOfst + keyPDU.hdrLen + keyPDU.contLen, null);
2514
+ if (cnt > 4)
2515
+ {
2516
+ let modulus = ASN1Util.pduGetItem(reader, keyPDU.rawOfst + keyPDU.hdrLen, keyPDU.rawOfst + keyPDU.hdrLen + keyPDU.contLen, "2");
2517
+ let privateExponent = ASN1Util.pduGetItem(reader, keyPDU.rawOfst + keyPDU.hdrLen, keyPDU.rawOfst + keyPDU.hdrLen + keyPDU.contLen, "4");
2518
+ if (modulus && privateExponent)
2519
+ {
2520
+ return (modulus.contLen - 1) << 3;
2521
+ }
2522
+ }
2523
+ }
2524
+ return 0;
2525
+ case KeyType.RSAPublic:
2526
+ if (reader.readUInt8(startOfst) == 0)
2527
+ {
2528
+ startOfst++;
2529
+ }
2530
+ keyPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
2531
+ if (keyPDU && keyPDU.itemType == ASN1ItemType.SEQUENCE)
2532
+ {
2533
+ let modulus = ASN1Util.pduGetItem(reader, keyPDU.rawOfst + keyPDU.hdrLen, keyPDU.rawOfst + keyPDU.hdrLen + keyPDU.contLen, "1");
2534
+ if (modulus)
2535
+ {
2536
+ return (modulus.contLen - 1) << 3;
2537
+ }
2538
+ }
2539
+ return 0;
2540
+ case KeyType.ECPublic:
2541
+ return 0;
2542
+ case KeyType.DSA:
2543
+ case KeyType.ECDSA:
2544
+ case KeyType.ED25519:
2545
+ case KeyType.Unknown:
2546
+ default:
2547
+ return 0;
2548
+ }
2549
+ }
2550
+
2551
+ static keyTypeFromOID(oid, pubKey)
2552
+ {
2553
+ let arr = new Uint8Array(oid);
2554
+ if (ASN1Util.oidEqualsText(arr, "1.2.840.113549.1.1.1"))
2555
+ {
2556
+ if (pubKey)
2557
+ {
2558
+ return KeyType.RSAPublic;
2559
+ }
2560
+ else
2561
+ {
2562
+ return KeyType.RSA;
2563
+ }
2564
+ }
2565
+ else if (ASN1Util.oidEqualsText(arr, "1.2.840.10045.2.1"))
2566
+ {
2567
+ return KeyType.ECPublic;
2568
+ }
2569
+ return KeyType.Unknown;
2570
+ }
2571
+
2572
+ static algorithmIdentifierGet(reader, startOfst, endOfst)
2573
+ {
2574
+ let cnt = ASN1Util.pduCountItem(reader, startOfst, endOfst, null);
2575
+ if (cnt != 2 && cnt != 1)
2576
+ {
2577
+ return AlgType.Unknown;
2578
+ }
2579
+ let itemPDU = ASN1Util.pduGetItem(reader, startOfst, endOfst, "1");
2580
+ if (itemPDU == null || itemPDU.itemType != ASN1ItemType.OID)
2581
+ {
2582
+ return AlgType.Unknown;
2583
+ }
2584
+ let oid = new Uint8Array(reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen));
2585
+ if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.2")) //md2WithRSAEncryption
2586
+ {
2587
+ return AlgType.MD2WithRSAEncryption;
2588
+ }
2589
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.4")) //md5WithRSAEncryption
2590
+ {
2591
+ return AlgType.MD5WithRSAEncryption;
2592
+ }
2593
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.5")) //sha1WithRSAEncryption
2594
+ {
2595
+ return AlgType.SHA1WithRSAEncryption;
2596
+ }
2597
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.11")) //sha256WithRSAEncryption
2598
+ {
2599
+ return AlgType.SHA256WithRSAEncryption;
2600
+ }
2601
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.12")) //sha384WithRSAEncryption
2602
+ {
2603
+ return AlgType.SHA384WithRSAEncryption;
2604
+ }
2605
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.13")) //sha512WithRSAEncryption
2606
+ {
2607
+ return AlgType.SHA512WithRSAEncryption;
2608
+ }
2609
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.113549.1.1.14")) //sha224WithRSAEncryption
2610
+ {
2611
+ return AlgType.SHA224WithRSAEncryption;
2612
+ }
2613
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.10045.4.3.2")) //ecdsa-with-SHA256
2614
+ {
2615
+ return AlgType.ECDSAWithSHA256;
2616
+ }
2617
+ else if (ASN1Util.oidEqualsText(oid, "1.2.840.10045.4.3.3")) //ecdsa-with-SHA384
2618
+ {
2619
+ return AlgType.ECDSAWithSHA384;
2620
+ }
2621
+ else
2622
+ {
2623
+ return AlgType.Unknown;
2624
+ }
2625
+ }
2626
+ }
2627
+
2628
+ export class X509Cert extends X509File
2629
+ {
2630
+ constructor(sourceName, buff)
2631
+ {
2632
+ super(sourceName, "application/x-pem-file", buff);
2633
+ }
2634
+
2635
+ getSubjectCN()
2636
+ {
2637
+ let tmpBuff;
2638
+ if (ASN1Util.pduGetItemType(this.reader, 0, this.reader.getLength(), "1.1.1") == ASN1ItemType.CONTEXT_SPECIFIC_0)
2639
+ {
2640
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.6");
2641
+ }
2642
+ else
2643
+ {
2644
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.5");
2645
+ }
2646
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
2647
+ {
2648
+ return X509File.nameGetCN(this.reader, tmpBuff.rawOfst + tmpBuff.hdrLen, tmpBuff.rawOfst + tmpBuff.hdrLen + tmpBuff.contLen);
2649
+ }
2650
+ else
2651
+ {
2652
+ return null;
2653
+ }
2654
+ }
2655
+
2656
+ getIssuerCN()
2657
+ {
2658
+ let tmpBuff;
2659
+ if (ASN1Util.pduGetItemType(this.reader, 0, this.reader.getLength(), "1.1.1") == ASN1ItemType.CONTEXT_SPECIFIC_0)
2660
+ {
2661
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4");
2662
+ }
2663
+ else
2664
+ {
2665
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
2666
+ }
2667
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
2668
+ {
2669
+ return X509File.nameGetCN(this.reader, tmpBuff.rawOfst + tmpBuff.hdrLen, tmpBuff.rawOfst + tmpBuff.hdrLen + tmpBuff.contLen);
2670
+ }
2671
+ else
2672
+ {
2673
+ return null;
2674
+ }
2675
+ }
2676
+
2677
+ setDefaultSourceName()
2678
+ {
2679
+ let cn = this.getSubjectCN();
2680
+ if (cn)
2681
+ {
2682
+ this.sourceName = cn+".crt";
2683
+ }
2684
+ }
2685
+
2686
+ getFileType()
2687
+ {
2688
+ return X509FileType.Cert;
2689
+ }
2690
+
2691
+ toShortName()
2692
+ {
2693
+ return this.getSubjectCN();
2694
+ }
2695
+
2696
+ getCertCount()
2697
+ {
2698
+ return 1;
2699
+ }
2700
+
2701
+ getCertName(index)
2702
+ {
2703
+ if (index != 0)
2704
+ return null;
2705
+ return this.getSubjectCN();
2706
+ }
2707
+
2708
+ getNewCert(index)
2709
+ {
2710
+ if (index != 0)
2711
+ return null;
2712
+ return this.clone();
2713
+ }
2714
+
2715
+ isValid()
2716
+ {
2717
+ /*
2718
+ if (trustStore == 0)
2719
+ {
2720
+ trustStore = ssl->GetTrustStore();
2721
+ }*/
2722
+ let issuerCN = this.getIssuerCN();
2723
+ if (issuerCN == null)
2724
+ {
2725
+ return CertValidStatus.FileFormatInvalid;
2726
+ }
2727
+ let dt;
2728
+ let currTime = new Date().getTime();
2729
+ if ((dt = this.getNotBefore()) == null)
2730
+ {
2731
+ return CertValidStatus.FileFormatInvalid;
2732
+ }
2733
+ if (dt.toEpochMS() > currTime)
2734
+ {
2735
+ return CertValidStatus.Expired;
2736
+ }
2737
+ if ((dt = this.getNotAfter()) == null)
2738
+ {
2739
+ return CertValidStatus.FileFormatInvalid;
2740
+ }
2741
+ if (dt.toEpochMS() < currTime)
2742
+ {
2743
+ return CertValidStatus.Expired;
2744
+ }
2745
+ let signedInfo;
2746
+ if ((signedInfo = this.getSignedInfo()) == null)
2747
+ {
2748
+ return CertValidStatus.FileFormatInvalid;
2749
+ }
2750
+ let hashType = algTypeGetHash(signedInfo.algType);
2751
+ if (hashType == hash.HashType.Unknown)
2752
+ {
2753
+ return CertValidStatus.UnsupportedAlgorithm;
2754
+ }
2755
+
2756
+ let issuer;// = trustStore.getCertByCN(issuerCN);
2757
+ if (issuer == null)
2758
+ {
2759
+ if (!this.isSelfSigned())
2760
+ {
2761
+ return CertValidStatus.UnknownIssuer;
2762
+ }
2763
+ let key = this.getNewPublicKey();
2764
+ if (key == null)
2765
+ {
2766
+ return CertValidStatus.FileFormatInvalid;
2767
+ }
2768
+ let signValid = key.signatureVerify(hashType, signedInfo.payload, signedInfo.signature);
2769
+ if (signValid)
2770
+ {
2771
+ return CertValidStatus.SelfSigned;
2772
+ }
2773
+ else
2774
+ {
2775
+ return CertValidStatus.SignatureInvalid;
2776
+ }
2777
+ }
2778
+
2779
+ let key = issuer.getNewPublicKey();
2780
+ if (key == null)
2781
+ {
2782
+ return CertValidStatus.FileFormatInvalid;
2783
+ }
2784
+ let signValid = key.signatureVerify(hashType, signedInfo.payload, signedInfo.signature);
2785
+ if (!signValid)
2786
+ {
2787
+ return CertValidStatus.SignatureInvalid;
2788
+ }
2789
+
2790
+ let crlDistributionPoints = this.getCRLDistributionPoints();
2791
+ //////////////////////////
2792
+ // CRL
2793
+ return CertValidStatus.Valid;
2794
+ }
2795
+
2796
+ clone()
2797
+ {
2798
+ return new X509Cert(this.sourceName, this.reader.getArrayBuffer());
2799
+ }
2800
+
2801
+ createX509Cert()
2802
+ {
2803
+ return new X509Cert(this.sourceName, this.reader.getArrayBuffer());
2804
+ }
2805
+
2806
+ toString()
2807
+ {
2808
+ let sb = [];
2809
+ if (X509File.isCertificate(this.reader, 0, this.reader.getLength(), "1"))
2810
+ {
2811
+ X509File.appendCertificate(this.reader, 0, this.reader.getLength(), "1", sb, null);
2812
+ }
2813
+ return sb.join("");
2814
+ }
2815
+
2816
+ createNames()
2817
+ {
2818
+ return new ASN1Names().setCertificate();
2819
+ }
2820
+
2821
+ getIssuerNames()
2822
+ {
2823
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
2824
+ if (pdu == null)
2825
+ {
2826
+ return null;
2827
+ }
2828
+ if (pdu.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2829
+ {
2830
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4");
2831
+ if (pdu)
2832
+ {
2833
+ return X509File.namesGet(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2834
+ }
2835
+ }
2836
+ else
2837
+ {
2838
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
2839
+ if (pdu)
2840
+ {
2841
+ return X509File.namesGet(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2842
+ }
2843
+ }
2844
+ return null;
2845
+ }
2846
+
2847
+ getSubjNames()
2848
+ {
2849
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
2850
+ if (pdu == null)
2851
+ {
2852
+ return null;
2853
+ }
2854
+ if (pdu.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2855
+ {
2856
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.6");
2857
+ if (pdu)
2858
+ {
2859
+ return X509File.namesGet(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2860
+ }
2861
+ }
2862
+ else
2863
+ {
2864
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.5");
2865
+ if (pdu)
2866
+ {
2867
+ return X509File.namesGet(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2868
+ }
2869
+ }
2870
+ return null;
2871
+ }
2872
+
2873
+ getExtensions()
2874
+ {
2875
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
2876
+ if (pdu == null)
2877
+ {
2878
+ return null;
2879
+ }
2880
+ if (pdu.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
2881
+ {
2882
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.8.1");
2883
+ if (pdu)
2884
+ {
2885
+ return X509File.extensionsGet(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2886
+ }
2887
+ }
2888
+ else
2889
+ {
2890
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.7.1");
2891
+ if (pdu)
2892
+ {
2893
+ return X509File.extensionsGet(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2894
+ }
2895
+ }
2896
+ return null;
2897
+ }
2898
+
2899
+ getNewPublicKey()
2900
+ {
2901
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
2902
+ if (pdu == null)
2903
+ {
2904
+ return null;
2905
+ }
2906
+ if (pdu.itemType == ASN1Util.CONTEXT_SPECIFIC_0)
2907
+ {
2908
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.7");
2909
+ if (pdu)
2910
+ {
2911
+ return X509File.publicKeyGetNew(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2912
+ }
2913
+ }
2914
+ else
2915
+ {
2916
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.6");
2917
+ if (pdu)
2918
+ {
2919
+ return X509File.publicKeyGetNew(this.reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
2920
+ }
2921
+ }
2922
+ return null;
2923
+ }
2924
+
2925
+ getKeyId()
2926
+ {
2927
+ let key = this.getNewPublicKey();
2928
+ if (key == null)
2929
+ {
2930
+ return null;
2931
+ }
2932
+ return key.getKeyId();
2933
+ }
2934
+
2935
+ getNotBefore()
2936
+ {
2937
+ let tmpBuff;
2938
+ if (ASN1Util.pduGetItemType(this.reader, 0, this.reader.getLength(), "1.1.1") == ASN1ItemType.CONTEXT_SPECIFIC_0)
2939
+ {
2940
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.5.1");
2941
+ }
2942
+ else
2943
+ {
2944
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4.1");
2945
+ }
2946
+ if (tmpBuff == null)
2947
+ return null;
2948
+ if (tmpBuff.itemType == ASN1ItemType.UTCTIME || tmpBuff.itemType == ASN1ItemType.GENERALIZEDTIME)
2949
+ {
2950
+ return ASN1Util.pduParseUTCTimeCont(this.reader, tmpBuff.rawOfst + tmpBuff.hdrLen, tmpBuff.rawOfst + tmpBuff.hdrLen + tmpBuff.contLen);
2951
+ }
2952
+ return null;
2953
+ }
2954
+
2955
+ getNotAfter()
2956
+ {
2957
+ let tmpBuff;
2958
+ if (ASN1Util.pduGetItemType(this.reader, 0, this.reader.getLength(), "1.1.1") == ASN1ItemType.CONTEXT_SPECIFIC_0)
2959
+ {
2960
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.5.2");
2961
+ }
2962
+ else
2963
+ {
2964
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4.2");
2965
+ }
2966
+ if (tmpBuff == null)
2967
+ return null;
2968
+ if (tmpBuff.itemType == ASN1ItemType.UTCTIME || tmpBuff.itemType == ASN1ItemType.GENERALIZEDTIME)
2969
+ {
2970
+ return ASN1Util.pduParseUTCTimeCont(this.reader, tmpBuff.rawOfst + tmpBuff.hdrLen, tmpBuff.rawOfst + tmpBuff.hdrLen + tmpBuff.contLen);
2971
+ }
2972
+ return null;
2973
+ }
2974
+
2975
+ domainValid(domain)
2976
+ {
2977
+ let valid = false;
2978
+ let subjNames;
2979
+ if (subjNames = this.getSubjNames())
2980
+ {
2981
+ if (subjNames.commonName)
2982
+ {
2983
+ if (subjNames.commonName.startsWith("*."))
2984
+ {
2985
+ valid = (domain.toUpperCase() == subjNames.commonName.substring(2).toUpperCase()) || domain.toUpperCase().endsWith(subjNames.commonName.substring(1).toUpperCase());
2986
+ }
2987
+ else
2988
+ {
2989
+ valid = domain.toUpperCase() == subjNames.commonName;
2990
+ }
2991
+ }
2992
+ if (valid)
2993
+ {
2994
+ return true;
2995
+ }
2996
+ }
2997
+
2998
+ let exts;
2999
+ let s;
3000
+ if (exts = this.getExtensions())
3001
+ {
3002
+ if (exts.subjectAltName)
3003
+ {
3004
+ let i = 0;
3005
+ let j = exts.subjectAltName.length;
3006
+ while (i < j)
3007
+ {
3008
+ s = exts.subjectAltName[i];
3009
+ if (s.startsWith("*."))
3010
+ {
3011
+ valid = (domain.toUpperCase() == s.substring(2).toUpperCase()) || domain.toUpperCase().endsWith(s.substring(1).toUpperCase());
3012
+ }
3013
+ else
3014
+ {
3015
+ valid = domain.toUpperCase() == s.toUpperCase();
3016
+ }
3017
+ if (valid)
3018
+ break;
3019
+ }
3020
+ }
3021
+ if (valid)
3022
+ {
3023
+ return true;
3024
+ }
3025
+ }
3026
+ return false;
3027
+ }
3028
+
3029
+ isSelfSigned()
3030
+ {
3031
+ let subjNames;
3032
+ let issueNames;
3033
+ if ((issueNames = this.getIssuerNames()) && (subjNames = this.getSubjNames()))
3034
+ {
3035
+ return issueNames.commonName == subjNames.commonName;
3036
+ }
3037
+ return false;
3038
+ }
3039
+
3040
+ getCRLDistributionPoints()
3041
+ {
3042
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
3043
+ if (pdu == null)
3044
+ {
3045
+ return [];
3046
+ }
3047
+ if (pdu.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
3048
+ {
3049
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.8.1");
3050
+ if (pdu)
3051
+ {
3052
+ return X509File.extensionsGetCRLDistributionPoints(reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
3053
+ }
3054
+ }
3055
+ else
3056
+ {
3057
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.7.1");
3058
+ if (pdu)
3059
+ {
3060
+ return X509File.extensionsGetCRLDistributionPoints(reader, pdu.rawOfst + pdu.hdrLen, pdu.rawOfst + pdu.hdrLen + pdu.contLen);
3061
+ }
3062
+ }
3063
+ return [];
3064
+ }
3065
+
3066
+ getIssuerNamesSeq()
3067
+ {
3068
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
3069
+ if (pdu == null)
3070
+ {
3071
+ return null;
3072
+ }
3073
+ if (pdu.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
3074
+ {
3075
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4");
3076
+ if (pdu && pdu.itemType == ASN1ItemType.SEQUENCE)
3077
+ {
3078
+ return this.reader.getArrayBuffer(pdu.rawOfst + pdu.hdrLen, pdu.contLen);
3079
+ }
3080
+ }
3081
+ else
3082
+ {
3083
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
3084
+ if (pdu && pdu.itemType == ASN1ItemType.SEQUENCE)
3085
+ {
3086
+ return this.reader.getArrayBuffer(pdu.rawOfst + pdu.hdrLen, pdu.contLen);
3087
+ }
3088
+ }
3089
+ return null;
3090
+ }
3091
+
3092
+ getSerialNumber()
3093
+ {
3094
+ let pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
3095
+ if (pdu == null)
3096
+ {
3097
+ return null;
3098
+ }
3099
+ if (pdu.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
3100
+ {
3101
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.2");
3102
+ if (pdu && pdu.itemType == ASN1ItemType.INTEGER)
3103
+ {
3104
+ return this.reader.getArrayBuffer(pdu.rawOfst + pdu.hdrLen, pdu.contLen);
3105
+ }
3106
+ }
3107
+ else
3108
+ {
3109
+ pdu = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
3110
+ if (pdu && pdu.itemType == ASN1ItemType.INTEGER)
3111
+ {
3112
+ return this.reader.getArrayBuffer(pdu.rawOfst + pdu.hdrLen, pdu.contLen);
3113
+ }
3114
+ }
3115
+ return null;
3116
+ }
3117
+ }
3118
+
3119
+ export class X509CertReq extends X509File
3120
+ {
3121
+ constructor(sourceName, buff)
3122
+ {
3123
+ super(sourceName, "application/x-pem-file", buff);
3124
+ }
3125
+
3126
+ getFileType()
3127
+ {
3128
+ return X509FileType.CertRequest;
3129
+ }
3130
+
3131
+ toShortName()
3132
+ {
3133
+ let tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.2");
3134
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
3135
+ {
3136
+ return X509File.nameGetCN(this.reader, tmpBuff.dataOfst, tmpBuff.endOfst);
3137
+ }
3138
+ return "";
3139
+ }
3140
+
3141
+ isValid()
3142
+ {
3143
+ let signedInfo;
3144
+ if ((signedInfo = this.getSignedInfo()) == null)
3145
+ {
3146
+ return CertValidStatus.FileFormatInvalid;
3147
+ }
3148
+ let hashType = algTypeGetHash(signedInfo.algType);
3149
+ if (hashType == hash.HashType.Unknown)
3150
+ {
3151
+ return CertValidStatus.UnsupportedAlgorithm;
3152
+ }
3153
+ let key = this.getNewPublicKey();
3154
+ if (key == null)
3155
+ {
3156
+ return CertValidStatus.FileFormatInvalid;
3157
+ }
3158
+ let valid = key.signatureVerify(hashType, signedInfo.payload, signedInfo.signature);
3159
+ if (valid)
3160
+ {
3161
+ return CertValidStatus.Valid;
3162
+ }
3163
+ else
3164
+ {
3165
+ return CertValidStatus.SignatureInvalid;
3166
+ }
3167
+ }
3168
+
3169
+ clone()
3170
+ {
3171
+ return new X509CertReq(this.sourceName, this.reader.getArrayBuffer());
3172
+ }
3173
+
3174
+ toString()
3175
+ {
3176
+ let sb = [];
3177
+ if (X509File.isCertificateRequest(this.reader, 0, this.reader.getLength(), "1"))
3178
+ {
3179
+ X509File.appendCertificateRequest(this.reader, 0, this.reader.getLength(), "1", sb);
3180
+ }
3181
+ return sb.join("");
3182
+ }
3183
+
3184
+ createNames()
3185
+ {
3186
+ return new ASN1Names().setCertificationRequest();
3187
+ }
3188
+
3189
+ getNames()
3190
+ {
3191
+ let namesPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.2");
3192
+ if (namesPDU)
3193
+ {
3194
+ return X509File.namesGet(this.reader, namesPDU.dataOfst, namesPDU.endOfst);
3195
+ }
3196
+ return null;
3197
+ }
3198
+
3199
+ getExtensions()
3200
+ {
3201
+ let extPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4.1");
3202
+ if (extPDU && extPDU.itemType == ASN1ItemType.SEQUENCE)
3203
+ {
3204
+ let oid = ASN1Util.pduGetItem(this.reader, extPDU.dataOfst, extPDU.endOfst, "1");
3205
+ if (oid && ASN1Util.oidEqualsText(this.reader.getU8Arr(oid.dataOfst, oid.contLen), "1.2.840.113549.1.9.14")) //extensionRequest
3206
+ {
3207
+ let extSeq = ASN1Util.pduGetItem(this.reader, extPDU.dataOfst, extPDU.endOfst, "2.1");
3208
+ if (extSeq && extSeq.itemType == ASN1ItemType.SEQUENCE)
3209
+ {
3210
+ return X509File.extensionsGet(this.reader, extSeq.dataOfst, extSeq.endOfst);
3211
+ }
3212
+ }
3213
+ }
3214
+ return null;
3215
+ }
3216
+
3217
+ getNewPublicKey()
3218
+ {
3219
+ let keyPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
3220
+ if (keyPDU)
3221
+ {
3222
+ return X509File.publicKeyGetNew(this.reader, keyPDU.dataOfst, keyPDU.endOfst);
3223
+ }
3224
+ return null;
3225
+ }
3226
+
3227
+ getKeyId()
3228
+ {
3229
+ let key = this.getNewPublicKey();
3230
+ if (key == null)
3231
+ {
3232
+ return null;
3233
+ }
3234
+ return key.getKeyId();
3235
+ }
3236
+ }
3237
+
3238
+ export class X509Key extends X509File
3239
+ {
3240
+ constructor(sourceName, buff, keyType)
3241
+ {
3242
+ super(sourceName, "application/x-pem-file", buff);
3243
+ this.keyType = keyType;
3244
+ }
3245
+
3246
+ getFileType()
3247
+ {
3248
+ return X509FileType.Key;
3249
+ }
3250
+
3251
+ toShortName()
3252
+ {
3253
+ return keyTypeGetName(this.keyType)+" "+this.getKeySizeBits()+" bits";
3254
+ }
3255
+
3256
+ isValid()
3257
+ {
3258
+ if (this.keyType == KeyType.Unknown)
3259
+ {
3260
+ return CertValidStatus.FileFormatInvalid;
3261
+ }
3262
+ return CertValidStatus.Valid;
3263
+ }
3264
+
3265
+ clone()
3266
+ {
3267
+ return new X509Key(this.sourceName, this.reader.getArrayBuffer(0, this.reader.getLength()), this.keyType);
3268
+ }
3269
+
3270
+ toString()
3271
+ {
3272
+ let strs = [];
3273
+ let buff;
3274
+ if (this.keyType == KeyType.RSA)
3275
+ {
3276
+ buff = this.getRSAModulus();
3277
+ if (buff)
3278
+ {
3279
+ strs.push(this.sourceName+".RSA.Modulus = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3280
+ }
3281
+ buff = this.getRSAPublicExponent();
3282
+ if (buff)
3283
+ {
3284
+ strs.push(this.sourceName+".RSA.Public Exponent = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3285
+ }
3286
+ buff = this.getRSAPrivateExponent();
3287
+ if (buff)
3288
+ {
3289
+ strs.push(this.sourceName+".RSA.Private Exponent = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3290
+ }
3291
+ buff = this.getRSAPrime1();
3292
+ if (buff)
3293
+ {
3294
+ strs.push(this.sourceName+".RSA.Prime1 = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3295
+ }
3296
+ buff = this.getRSAPrime2();
3297
+ if (buff)
3298
+ {
3299
+ strs.push(this.sourceName+".RSA.Prime2 = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3300
+ }
3301
+ buff = this.getRSAExponent1();
3302
+ if (buff)
3303
+ {
3304
+ strs.push(this.sourceName+".RSA.Exponent1 = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3305
+ }
3306
+ buff = this.getRSAExponent2();
3307
+ if (buff)
3308
+ {
3309
+ strs.push(this.sourceName+".RSA.Exponent2 = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3310
+ }
3311
+ buff = this.getRSACoefficient();
3312
+ if (buff)
3313
+ {
3314
+ strs.push(this.sourceName+".RSA.Coefficient = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3315
+ }
3316
+ }
3317
+ else if (this.keyType == KeyType.RSAPublic)
3318
+ {
3319
+ buff = this.getRSAModulus();
3320
+ if (buff)
3321
+ {
3322
+ strs.push(this.sourceName+".RSA.Modulus = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3323
+ }
3324
+ buff = this.getRSAPublicExponent();
3325
+ if (buff)
3326
+ {
3327
+ strs.push(this.sourceName+".RSA.Public Exponent = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3328
+ }
3329
+ }
3330
+ else if (this.keyType == KeyType.ECPublic)
3331
+ {
3332
+ let ecName = this.getECName();
3333
+ strs.push(this.sourceName+".EC.Name = "+ecNameGetName(ecName));
3334
+
3335
+ buff = this.getECPublic();
3336
+ if (buff)
3337
+ {
3338
+ strs.push(this.sourceName+".EC.Public = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3339
+ }
3340
+ }
3341
+ else if (this.keyType == KeyType.ECDSA)
3342
+ {
3343
+ let ecName = this.getECName();
3344
+ strs.push(this.sourceName+".EC.Name = "+ecNameGetName(ecName));
3345
+
3346
+ buff = this.getECPrivate();
3347
+ if (buff)
3348
+ {
3349
+ strs.push(this.sourceName+".EC.Private = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3350
+ }
3351
+ buff = this.getECPublic();
3352
+ if (buff)
3353
+ {
3354
+ strs.push(this.sourceName+".EC.Public = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3355
+ }
3356
+ }
3357
+
3358
+ buff = this.getKeyId();
3359
+ if (buff)
3360
+ {
3361
+ strs.push(this.sourceName+".KeyId = "+text.u8Arr2Hex(new Uint8Array(buff), ' ', null));
3362
+ }
3363
+ return strs.join("\r\n");
3364
+ }
3365
+
3366
+ createNames()
3367
+ {
3368
+ let names = new ASN1Names();
3369
+ switch (this.keyType)
3370
+ {
3371
+ case KeyType.RSA:
3372
+ return names.setRSAPrivateKey();
3373
+ case KeyType.RSAPublic:
3374
+ return names.setRSAPublicKey();
3375
+ default:
3376
+ case KeyType.DSA:
3377
+ case KeyType.ECDSA:
3378
+ case KeyType.ECPublic:
3379
+ case KeyType.ED25519:
3380
+ case KeyType.Unknown:
3381
+ return names;
3382
+ }
3383
+ }
3384
+
3385
+ getKeyType()
3386
+ {
3387
+ return this.keyType;
3388
+ }
3389
+
3390
+ getKeySizeBits()
3391
+ {
3392
+ return X509File.keyGetLeng(this.reader, 0, this.reader.getLength(), this.keyType);
3393
+ }
3394
+
3395
+ isPrivateKey()
3396
+ {
3397
+ switch (this.keyType)
3398
+ {
3399
+ case KeyType.DSA:
3400
+ case KeyType.ECDSA:
3401
+ case KeyType.ED25519:
3402
+ case KeyType.RSA:
3403
+ return true;
3404
+ case KeyType.RSAPublic:
3405
+ case KeyType.ECPublic:
3406
+ case KeyType.Unknown:
3407
+ default:
3408
+ return false;
3409
+ }
3410
+ }
3411
+
3412
+ createPublicKey()
3413
+ {
3414
+ if (this.keyType == KeyType.RSAPublic)
3415
+ {
3416
+ return this.clone();
3417
+ }
3418
+ else if (this.keyType == KeyType.RSA)
3419
+ {
3420
+ let builder = new ASN1PDUBuilder();
3421
+ let buff;
3422
+ builder.beginSequence();
3423
+ if ((buff = this.getRSAModulus()) == null) return null;
3424
+ builder.appendOther(ASN1ItemType.INTEGER, buff);
3425
+ if ((buff = this.getRSAPublicExponent()) == null) return null;
3426
+ builder.appendOther(ASN1ItemType.INTEGER, buff);
3427
+ builder.endLevel();
3428
+ return new X509Key(this.sourceName, builder.getArrayBuffer(), KeyType.RSAPublic);
3429
+ }
3430
+ else if (this.keyType == KeyType.ECPublic)
3431
+ {
3432
+ return this.clone();
3433
+ }
3434
+ else
3435
+ {
3436
+ return null;
3437
+ }
3438
+ }
3439
+
3440
+ getKeyId()
3441
+ {
3442
+ let pubKey = this.createPublicKey();
3443
+ if (pubKey)
3444
+ {
3445
+ let sha1 = new hash.SHA1();
3446
+ sha1.calc(pubKey.getASN1Buff().getArrayBuffer());
3447
+ return sha1.getValue();
3448
+ }
3449
+ return null;
3450
+ }
3451
+
3452
+ getRSAModulus()
3453
+ {
3454
+ let len = null;
3455
+ if (this.keyType == KeyType.RSA)
3456
+ {
3457
+ len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
3458
+ }
3459
+ else if (this.keyType == KeyType.RSAPublic)
3460
+ {
3461
+ len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1");
3462
+ }
3463
+ if (len)
3464
+ {
3465
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3466
+ }
3467
+ return null;
3468
+ }
3469
+
3470
+ getRSAPublicExponent()
3471
+ {
3472
+ let len = null;
3473
+ if (this.keyType == KeyType.RSA)
3474
+ {
3475
+ len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3");
3476
+ }
3477
+ else if (this.keyType == KeyType.RSAPublic)
3478
+ {
3479
+ len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
3480
+ }
3481
+ if (len)
3482
+ {
3483
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3484
+ }
3485
+ return null;
3486
+ }
3487
+
3488
+ getRSAPrivateExponent()
3489
+ {
3490
+ if (this.keyType != KeyType.RSA) return 0;
3491
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.4");
3492
+ if (len)
3493
+ {
3494
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3495
+ }
3496
+ return null;
3497
+ }
3498
+
3499
+ getRSAPrime1()
3500
+ {
3501
+ if (this.keyType != KeyType.RSA) return 0;
3502
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.5");
3503
+ if (len)
3504
+ {
3505
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3506
+ }
3507
+ return null;
3508
+ }
3509
+
3510
+ getRSAPrime2()
3511
+ {
3512
+ if (this.keyType != KeyType.RSA) return 0;
3513
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.6");
3514
+ if (len)
3515
+ {
3516
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3517
+ }
3518
+ return null;
3519
+ }
3520
+
3521
+ getRSAExponent1()
3522
+ {
3523
+ if (this.keyType != KeyType.RSA) return 0;
3524
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.7");
3525
+ if (len)
3526
+ {
3527
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3528
+ }
3529
+ return null;
3530
+ }
3531
+
3532
+ getRSAExponent2()
3533
+ {
3534
+ if (this.keyType != KeyType.RSA) return 0;
3535
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.8");
3536
+ if (len)
3537
+ {
3538
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3539
+ }
3540
+ return null;
3541
+ }
3542
+
3543
+ getRSACoefficient()
3544
+ {
3545
+ if (this.keyType != KeyType.RSA) return 0;
3546
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.9");
3547
+ if (len)
3548
+ {
3549
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3550
+ }
3551
+ return null;
3552
+ }
3553
+
3554
+ getECPrivate()
3555
+ {
3556
+ if (this.keyType == KeyType.ECDSA)
3557
+ {
3558
+ let len = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
3559
+ if (len)
3560
+ {
3561
+ return this.reader.getArrayBuffer(len.rawOfst + len.hdrLen, len.contLen);
3562
+ }
3563
+ return null;
3564
+ }
3565
+ else
3566
+ {
3567
+ return null;
3568
+ }
3569
+ }
3570
+
3571
+ getECPublic()
3572
+ {
3573
+ let itemPDU;
3574
+ if (this.keyType == KeyType.ECPublic)
3575
+ {
3576
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
3577
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.BIT_STRING)
3578
+ {
3579
+ return this.reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen + 1, itemPDU.contLen - 1);
3580
+ }
3581
+ return 0;
3582
+ }
3583
+ else if (this.keyType == KeyType.ECDSA)
3584
+ {
3585
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3");
3586
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_1)
3587
+ {
3588
+ itemPDU = ASN1Util.pduGetItem(this.reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
3589
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.BIT_STRING)
3590
+ {
3591
+ return this.reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen + 1, itemPDU.contLen - 1);
3592
+ }
3593
+ return 0;
3594
+ }
3595
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.4");
3596
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_1)
3597
+ {
3598
+ itemPDU = ASN1Util.pduGetItem(this.reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
3599
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.BIT_STRING)
3600
+ {
3601
+ return this.reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen + 1, itemPDU.contLen - 1);
3602
+ }
3603
+ return 0;
3604
+ }
3605
+ return 0;
3606
+ }
3607
+ else
3608
+ {
3609
+ return 0;
3610
+ }
3611
+ }
3612
+
3613
+ getECName()
3614
+ {
3615
+ if (this.keyType == KeyType.ECPublic)
3616
+ {
3617
+ let itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.2");
3618
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.OID)
3619
+ {
3620
+ return ecNameFromOID(this.reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen));
3621
+ }
3622
+ }
3623
+ else if (this.keyType == KeyType.ECDSA)
3624
+ {
3625
+ let itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3");
3626
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
3627
+ {
3628
+ itemPDU = ASN1Util.pduGetItem(this.reader, itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.rawOfst + itemPDU.hdrLen + itemPDU.contLen, "1");
3629
+ if (itemPDU != null && itemPDU.itemType == ASN1ItemType.OID)
3630
+ {
3631
+ return ecNameFromOID(this.reader.getArrayBuffer(itemPDU.rawOfst + itemPDU.hdrLen, itemPDU.contLen));
3632
+ }
3633
+ }
3634
+ }
3635
+ return ECName.Unknown;
3636
+ }
3637
+
3638
+ static fromECPublicKey(buff, paramOID)
3639
+ {
3640
+ let pdu = new ASN1PDUBuilder();
3641
+ pdu.beginSequence();
3642
+ pdu.beginSequence();
3643
+ pdu.appendOIDString("1.2.840.10045.2.1");
3644
+ pdu.appendOID(paramOID);
3645
+ pdu.endLevel();
3646
+ pdu.appendBitString(0, buff);
3647
+ pdu.endLevel();
3648
+ return new X509Key("ECPublic.key", pdu.getArrayBuffer(), KeyType.ECPublic);
3649
+ }
3650
+ }
3651
+
3652
+ export class X509PrivKey extends X509File
3653
+ {
3654
+ constructor(sourceName, buff)
3655
+ {
3656
+ super(sourceName, "application/x-pem-file", buff);
3657
+ }
3658
+
3659
+ getFileType()
3660
+ {
3661
+ return X509FileType.PrivateKey;
3662
+ }
3663
+
3664
+ toShortName()
3665
+ {
3666
+ let oidPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1");
3667
+ if (oidPDU == null || oidPDU.itemType != ASN1ItemType.OID)
3668
+ {
3669
+ return "";
3670
+ }
3671
+ let keyType = X509File.keyTypeFromOID(reader.getArrayBuffer(oidPDU.dataOfst, oidPDU.contLen), false);
3672
+ let keyPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3");
3673
+ if (keyPDU && keyPDU.itemType == ASN1ItemType.OCTET_STRING)
3674
+ {
3675
+ let sb = [];
3676
+ sb.push(keyTypeGetName(keyType));
3677
+ sb.push(' ');
3678
+ sb.push(X509File.keyGetLeng(reader, keyPDU.dataOfst, keyPDU.endOfst, keyType));
3679
+ sb.push(" bits");
3680
+ return sb.join("");
3681
+ }
3682
+ else
3683
+ {
3684
+ return "";
3685
+ }
3686
+ }
3687
+
3688
+ isValid()
3689
+ {
3690
+ return CertValidStatus.SignatureInvalid;
3691
+ }
3692
+
3693
+ clone()
3694
+ {
3695
+ return new X509PrivKey(this.sourceName, this.reader.getArrayBuffer());
3696
+ }
3697
+
3698
+ toString()
3699
+ {
3700
+ let sb = [];
3701
+ if (X509File.isPrivateKeyInfo(this.reader, 0, this.reader.getLength(), "1"))
3702
+ {
3703
+ X509File.appendPrivateKeyInfo(this.reader, 0, this.reader.getLength(), "1", sb);
3704
+ }
3705
+ return sb.join("");
3706
+ }
3707
+
3708
+ createNames()
3709
+ {
3710
+ return null;
3711
+ }
3712
+
3713
+ getKeyType()
3714
+ {
3715
+ let keyTypeOID = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1");
3716
+ if (keyTypeOID != null)
3717
+ {
3718
+ return X509File.keyTypeFromOID(this.reader.getArrayBuffer(keyTypeOID.dataOfst, keyTypeOID.contLen), false);
3719
+ }
3720
+ return KeyType.Unknown;
3721
+ }
3722
+
3723
+ createKey()
3724
+ {
3725
+ let keyType = this.getKeyType();
3726
+ if (keyType == KeyType.Unknown)
3727
+ {
3728
+ return null;
3729
+ }
3730
+ let keyData = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.3");
3731
+ if (keyData != null)
3732
+ {
3733
+ return new X509Key(this.sourceName, this.reader.getArrayBuffer(keyData.dataOfst, keyData.contLen), keyType);
3734
+ }
3735
+ return null;
3736
+ }
3737
+
3738
+ static createFromKeyBuff(keyType, buff, sourceName)
3739
+ {
3740
+ if (sourceName == null)
3741
+ {
3742
+ sourceName = "";
3743
+ }
3744
+ let keyPDU = new ASN1PDUBuilder();
3745
+ keyPDU.beginSequence();
3746
+ keyPDU.appendInt32(0);
3747
+ keyPDU.beginSequence();
3748
+ let oidStr = keyTypeGetOID(keyType);
3749
+ keyPDU.appendOIDString(oidStr);
3750
+ keyPDU.appendNull();
3751
+ keyPDU.endLevel();
3752
+ keyPDU.appendOctetString(buff);
3753
+ keyPDU.endLevel();
3754
+ return new X509PrivKey(sourceName, keyPDU.getArrayBuffer());
3755
+ }
3756
+
3757
+ static createFromKey(key)
3758
+ {
3759
+ if (!(key instanceof X509Key))
3760
+ return null;
3761
+ let keyType = key.getKeyType();
3762
+ if (keyType == KeyType.ECDSA)
3763
+ {
3764
+ let ecName = key.getECName();
3765
+ let keyBuff;
3766
+ let keyPDU = new ASN1PDUBuilder();
3767
+ keyPDU.beginSequence();
3768
+ keyPDU.appendInt32(0);
3769
+ keyPDU.beginSequence();
3770
+ let oidStr = keyTypeGetOID(keyType);
3771
+ keyPDU.appendOIDString(oidStr);
3772
+ oidStr = ecNameGetOID(ecName);
3773
+ keyPDU.appendOIDString(oidStr);
3774
+ keyPDU.endLevel();
3775
+ keyPDU.beginOther(4);
3776
+ keyPDU.beginSequence();
3777
+ keyPDU.appendInt32(1);
3778
+ keyBuff = key.getECPrivate();
3779
+ keyPDU.appendOctetString(keyBuff);
3780
+ keyBuff = key.getECPublic();
3781
+ if (keyBuff)
3782
+ {
3783
+ keyPDU.beginContentSpecific(1);
3784
+ keyPDU.appendBitString(0, keyBuff);
3785
+ keyPDU.endLevel();
3786
+ }
3787
+ keyPDU.endLevel();
3788
+ keyPDU.endLevel();
3789
+ keyPDU.endLevel();
3790
+ return new X509PrivKey(key.sourceName, keyPDU.getArrayBuffer());
3791
+ }
3792
+ else if (keyType == KeyType.RSA)
3793
+ {
3794
+ return X509PrivKey.createFromKeyBuff(keyType, key.getASN1Buff().getArrayBuffer(), key.sourceName);
3795
+ }
3796
+ else
3797
+ {
3798
+ return null;
3799
+ }
3800
+ }
3801
+ }
3802
+
3803
+ export class X509PubKey extends X509File
3804
+ {
3805
+ constructor(sourceName, buff)
3806
+ {
3807
+ super(sourceName, "application/x-pem-file", buff);
3808
+ }
3809
+
3810
+ getFileType()
3811
+ {
3812
+ return X509FileType.PublicKey;
3813
+ }
3814
+
3815
+ toShortName()
3816
+ {
3817
+ let oidPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
3818
+ if (oidPDU == null || oidPDU.itemType != ASN1ItemType.OID)
3819
+ {
3820
+ return null;
3821
+ }
3822
+ let keyType = X509File.keyTypeFromOID(reader.getArrayBuffer(oidPDU.rawOfst + oidPDU.hdrLen, oidPDU.contLen), true);
3823
+ let keyPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
3824
+ if (keyPDU && keyPDU.itemType == ASN1ItemType.OCTET_STRING)
3825
+ {
3826
+ let sb = [];
3827
+ sb.push(keyTypeGetName(keyType)+" ");
3828
+ sb.push(keyGetLeng(reader, keyPDU.rawOfst + keyPDU.hdrLen, keyPDU.rawOfst + keyPDU.hdrLen + keyPDU.contLen, keyType));
3829
+ sb.push(" bits");
3830
+ return sb.join("");
3831
+ }
3832
+ return null;
3833
+ }
3834
+
3835
+ isValid()
3836
+ {
3837
+ return CertValidStatus.SignatureInvalid;
3838
+ }
3839
+
3840
+ clone()
3841
+ {
3842
+ return new X509PubKey(this.sourceName, this.reader.getArrayBuffer());
3843
+ }
3844
+
3845
+ toString()
3846
+ {
3847
+ let sb = [];
3848
+ if (X509File.isPublicKeyInfo(this.reader, 0, this.reader.getLength(), "1"))
3849
+ {
3850
+ X509File.appendPublicKeyInfo(this.reader, 0, this.reader.getLength(), "1", sb);
3851
+ }
3852
+ return sb.join("");
3853
+ }
3854
+
3855
+ createNames()
3856
+ {
3857
+ return null;
3858
+ }
3859
+
3860
+ createKey()
3861
+ {
3862
+ let keyTypeOID = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1");
3863
+ let keyData = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2");
3864
+ if (keyTypeOID != null && keyData != null)
3865
+ {
3866
+ if (this.reader.readUInt8(keyData.rawOfst + keyData.hdrLen) == 0)
3867
+ {
3868
+ keyData.hdrLen++;
3869
+ keyData.contLen--;
3870
+ }
3871
+ return new X509Key(this.sourceName, this.reader.getArrayBuffer(keyData.rawOfst + keyData.hdrLen, keyData.contLen), X509File.keyTypeFromOID(this.reader.getArrayBuffer(keyTypeOID.rawOfst + keyTypeOID.hdrLen, keyTypeOID.contLen), true));
3872
+ }
3873
+ return null;
3874
+ }
3875
+
3876
+ static createFromKeyBuff(keyType, buff, sourceName)
3877
+ {
3878
+ let keyPDU = new ASN1PDUBuilder();
3879
+ keyPDU.beginSequence();
3880
+ keyPDU.beginSequence();
3881
+ let oidStr = keyTypeGetOID(keyType);
3882
+ keyPDU.appendOIDString(oidStr);
3883
+ keyPDU.appendNull();
3884
+ keyPDU.endLevel();
3885
+ if (keyType == KeyType.RSAPublic)
3886
+ {
3887
+ keyPDU.appendBitString(0, buff);
3888
+ }
3889
+ else
3890
+ {
3891
+ keyPDU.appendBitString(0, buff);
3892
+ }
3893
+ keyPDU.endLevel();
3894
+ return new X509PubKey(sourceName, keyPDU.getArrayBuffer());
3895
+ }
3896
+
3897
+ static createFromKey(key)
3898
+ {
3899
+ return createFromKeyBuff(key.getKeyType(), key.getASN1Buff().getArrayBuffer(), key.sourceName);
3900
+ }
3901
+ }
3902
+
3903
+ export class X509PKCS7 extends X509File
3904
+ {
3905
+ constructor(sourceName, buff)
3906
+ {
3907
+ super(sourceName, "application/x-pem-file", buff);
3908
+ }
3909
+
3910
+ getFileType()
3911
+ {
3912
+ return X509FileType.PKCS7;
3913
+ }
3914
+
3915
+ toShortName()
3916
+ {
3917
+ }
3918
+
3919
+ getCertCount()
3920
+ {
3921
+ let certListPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1.4");
3922
+ if (certListPDU == null || certListPDU.itemType != ASN1ItemType.CONTEXT_SPECIFIC_0)
3923
+ {
3924
+ return 0;
3925
+ }
3926
+ return ASN1Util.pduCountItem(this.reader, certListPDU.dataOfst, certListPDU.endOfst, null);
3927
+ }
3928
+
3929
+ getCertName(index)
3930
+ {
3931
+ let certListPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1.4");
3932
+ if (certListPDU == null || certListPDU.itemType != ASN1ItemType.CONTEXT_SPECIFIC_0)
3933
+ {
3934
+ return null;
3935
+ }
3936
+
3937
+ let tmpBuff;
3938
+ if (ASN1Util.pduGetItemType(this.reader, certListPDU.dataOfst, certListPDU.endOfst, (index + 1)+".1.1") == ASN1ItemType.CONTEXT_SPECIFIC_0)
3939
+ {
3940
+ tmpBuff = ASN1Util.pduGetItem(this.reader, certListPDU.dataOfst, certListPDU.endOfst, (index + 1)+".1.6");
3941
+ }
3942
+ else
3943
+ {
3944
+ tmpBuff = ASN1Util.pduGetItem(this.reader, certListPDU.dataOfst, certListPDU.endOfst, (index + 1)+".1.5");
3945
+ }
3946
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
3947
+ {
3948
+ return X509File.nameGetCN(this.reader, tmpBuff.dataOfst, tmpBuff.endOfst);
3949
+ }
3950
+ return null;
3951
+ }
3952
+
3953
+ getNewCert(index)
3954
+ {
3955
+ let certListPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1.4");
3956
+ if (certListPDU == null || certListPDU.itemType != ASN1ItemType.CONTEXT_SPECIFIC_0)
3957
+ {
3958
+ return null;
3959
+ }
3960
+ let certPDU = ASN1Util.pduGetItem(this.reader, certListPDU.dataOfst, certListPDU.endOfst, (index + 1).toString());
3961
+ if (certPDU)
3962
+ {
3963
+ return new X509Cert(this.sourceName, this.reader.getArrayBuffer(certPDU.rawOfst, certPDU.hdrLen + certPDU.contLen));
3964
+ }
3965
+ return null;
3966
+ }
3967
+
3968
+ isValid()
3969
+ {
3970
+ return CertValidStatus.SignatureInvalid;
3971
+ }
3972
+
3973
+ clone()
3974
+ {
3975
+ return new X509PKCS7(this.sourceName, this.reader.getArrayBuffer());
3976
+ }
3977
+
3978
+ toString()
3979
+ {
3980
+ let sb = [];
3981
+ if (X509File.isContentInfo(this.reader, 0, this.reader.getLength(), "1"))
3982
+ {
3983
+ X509File.appendContentInfo(this.reader, 0, this.reader.getLength(), "1", sb, null, ContentDataType.Unknown);
3984
+ }
3985
+ return sb.join("");
3986
+ }
3987
+
3988
+ createNames()
3989
+ {
3990
+ return new ASN1Names().setPKCS7ContentInfo();
3991
+ }
3992
+
3993
+ isSignData()
3994
+ {
3995
+ let itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1");
3996
+ if (itemPDU == null || itemPDU.itemType != ASN1ItemType.OID)
3997
+ {
3998
+ return false;
3999
+ }
4000
+ return ASN1Util.oidEqualsText(reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen), "1.2.840.113549.1.7.2");
4001
+ }
4002
+
4003
+ getDigestType()
4004
+ {
4005
+ if (!this.isSignData())
4006
+ {
4007
+ return hash.HashType.Unknown;
4008
+ }
4009
+ let itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1.5.1.3.1");
4010
+ if (itemPDU && itemPDU.itemType == ASN1ItemType.OID)
4011
+ {
4012
+ return hashTypeFromOID(this.reader.getU8Arr(itemPDU.dataOfst, itemPDU.contLen));
4013
+ }
4014
+ return hash.HashType.Unknown;
4015
+ }
4016
+
4017
+ getMessageDigest()
4018
+ {
4019
+ let itemPDU;
4020
+ let subitemPDU;
4021
+ if ((itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1.5.1.4")) != null && itemPDU.itemType == ASN1ItemType.CONTEXT_SPECIFIC_0)
4022
+ {
4023
+ let i = 0;
4024
+ while (true)
4025
+ {
4026
+ if ((subitemPDU = ASN1Util.pduGetItem(this.reader, itemPDU.dataOfst, itemPDU.endOfst, (++i)+".1")) == null)
4027
+ {
4028
+ break;
4029
+ }
4030
+ else if (subitemPDU.itemType == ASN1ItemType.OID && ASN1Util.oidEqualsText(this.reader.getU8Arr(subitemPDU.dataOfst, subitemPDU.contLen), "1.2.840.113549.1.9.4"))
4031
+ {
4032
+ if ((subitemPDU = ASN1Util.pduGetItem(this.reader, itemPDU.dataOfst, itemPDU.endOfst, (i)+".2.1")) != null &&
4033
+ subitemPDU.itemType == ASN1ItemType.OCTET_STRING)
4034
+ {
4035
+ return this.reader.getArrayBuffer(subitemPDU.dataOfst, subitemPDU.contLen);
4036
+ }
4037
+ }
4038
+ }
4039
+ }
4040
+ return null;
4041
+ }
4042
+
4043
+ getEncryptedDigest()
4044
+ {
4045
+ let itemPDU;
4046
+ if ((itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.2.1.5.1.6")) != null && itemPDU.itemType == ASN1ItemType.OCTET_STRING)
4047
+ {
4048
+ return this.reader.getArrayBuffer(itemPDU.dataOfst, itemPDU.contLen);
4049
+ }
4050
+ return null;
4051
+ }
4052
+ }
4053
+
4054
+ export class X509PKCS12 extends X509File
4055
+ {
4056
+ constructor(sourceName, buff)
4057
+ {
4058
+ super(sourceName, "application/x-pem-file", buff);
4059
+ }
4060
+
4061
+ getFileType()
4062
+ {
4063
+ return X509FileType.PKCS12;
4064
+ }
4065
+
4066
+ toShortName()
4067
+ {
4068
+ }
4069
+
4070
+ getCertCount()
4071
+ {
4072
+ return 0;
4073
+ }
4074
+
4075
+ getCertName(index)
4076
+ {
4077
+ return null;
4078
+ }
4079
+
4080
+ getNewCert(index)
4081
+ {
4082
+ return null;
4083
+ }
4084
+
4085
+ isValid()
4086
+ {
4087
+ return CertValidStatus.SignatureInvalid
4088
+ }
4089
+
4090
+ clone()
4091
+ {
4092
+ return new X509PKCS12(this.sourceName, this.reader.getArrayBuffer());
4093
+ }
4094
+
4095
+ toString()
4096
+ {
4097
+ let sb = [];
4098
+ if (X509File.isPFX(this.reader, 0, this.reader.getLength(), "1"))
4099
+ {
4100
+ X509File.appendPFX(this.reader, 0, this.reader.getLength(), "1", sb, null);
4101
+ }
4102
+ return sb.join("");
4103
+ }
4104
+
4105
+ createNames()
4106
+ {
4107
+ return new ASN1Names().setPFX();
4108
+ }
4109
+ }
4110
+
4111
+ export class X509FileList extends X509File
4112
+ {
4113
+ constructor(sourceName, cert)
4114
+ {
4115
+ super(sourceName, "application/x-pem-file", cert.reader.getArrayBuffer());
4116
+ this.fileList = [cert];
4117
+ }
4118
+
4119
+ getFileType()
4120
+ {
4121
+ return X509FileType.FileList;
4122
+ }
4123
+
4124
+ toShortName()
4125
+ {
4126
+ return this.fileList[0].toShortName();
4127
+ }
4128
+
4129
+ getCertCount()
4130
+ {
4131
+ let ret = 0;
4132
+ let i;
4133
+ for (i in this.fileList)
4134
+ {
4135
+ ret += this.fileList[i].getCertCount();
4136
+ }
4137
+ return ret;
4138
+ }
4139
+
4140
+ getCertName(index)
4141
+ {
4142
+ let i;
4143
+ let cnt;
4144
+ let file;
4145
+ for (i in this.fileList)
4146
+ {
4147
+ file = this.fileList[i];
4148
+ cnt = file.getCertCount();
4149
+ if (index < cnt)
4150
+ {
4151
+ return file.getCertName(index);
4152
+ }
4153
+ index -= cnt;
4154
+ }
4155
+ return null;
4156
+ }
4157
+
4158
+ getNewCert(index)
4159
+ {
4160
+ let i;
4161
+ let cnt;
4162
+ let file;
4163
+ for (i in this.fileList)
4164
+ {
4165
+ file = this.fileList[i];
4166
+ cnt = file.getCertCount();
4167
+ if (index < cnt)
4168
+ {
4169
+ return file.getNewCert(index);
4170
+ }
4171
+ index -= cnt;
4172
+ }
4173
+ return null;
4174
+ }
4175
+
4176
+ isValid()
4177
+ {
4178
+ let status;
4179
+ let i;
4180
+ if (this.fileList.length > 1)
4181
+ {
4182
+ let file;
4183
+ for (i in this.fileList)
4184
+ {
4185
+ file = this.fileList[i];
4186
+ status = file.isValid();
4187
+ if (status != CertValidStatus.Valid)
4188
+ {
4189
+ return status;
4190
+ }
4191
+ }
4192
+ return CertValidStatus.Valid;
4193
+ }
4194
+ else
4195
+ {
4196
+ return this.fileList[0],isValid();
4197
+ }
4198
+ }
4199
+
4200
+ clone()
4201
+ {
4202
+ let fileList = new X509FileList(this.sourceName, this.fileList[0].clone());
4203
+ let i = 1;
4204
+ let j = this.fileList.length;
4205
+ while (i < j)
4206
+ {
4207
+ fileList.addFile(this.fileList[i].clone());
4208
+ i++;
4209
+ }
4210
+ return fileList;
4211
+ }
4212
+
4213
+ createX509Cert()
4214
+ {
4215
+ return this.fileList[0].clone();
4216
+ }
4217
+
4218
+ toString()
4219
+ {
4220
+ let sb = [];
4221
+ let i;
4222
+ for (i in this.fileList)
4223
+ {
4224
+ sb.push(this.fileList[i].toString());
4225
+ }
4226
+ return sb.join("\r\n");
4227
+ }
4228
+
4229
+ createNames()
4230
+ {
4231
+ return this.fileList[0].createNames();
4232
+ }
4233
+
4234
+ addFile(file)
4235
+ {
4236
+ this.fileList.push(file);
4237
+ }
4238
+
4239
+ getFileCount()
4240
+ {
4241
+ return this.fileList.length;
4242
+ }
4243
+
4244
+ getFile(index)
4245
+ {
4246
+ return this.fileList[index];
4247
+ }
4248
+
4249
+ setDefaultSourceName()
4250
+ {
4251
+ let file;
4252
+ let j = INVALID_INDEX;
4253
+ let i = this.fileList.length;
4254
+ while (i-- > 0)
4255
+ {
4256
+ if (this.fileList[i] && this.fileList[i].getFileType() == X509FileType.Cert)
4257
+ {
4258
+ this.fileList[i].setDefaultSourceName();
4259
+ j = i;
4260
+ }
4261
+ }
4262
+ if (j != INVALID_INDEX)
4263
+ {
4264
+ this.setSourceName(this.fileList[j].sourceName);
4265
+ }
4266
+ }
4267
+ }
4268
+
4269
+ export class X509CRL extends X509File
4270
+ {
4271
+ constructor(sourceName, buff)
4272
+ {
4273
+ super(sourceName, "application/x-pem-file", buff);
4274
+ }
4275
+
4276
+ getFileType()
4277
+ {
4278
+ return X509FileType.CRL;
4279
+ }
4280
+
4281
+ toShortName()
4282
+ {
4283
+ let tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
4284
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
4285
+ {
4286
+ return X509File.nameGetCN(reader, tmpBuff.dataOfst, tmpBuff.endOfst);
4287
+ }
4288
+ else
4289
+ {
4290
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.2");
4291
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
4292
+ {
4293
+ return X509File.nameGetCN(reader, tmpBuff.dataOfst, tmpBuff.endOfst);
4294
+ }
4295
+ }
4296
+ return "";
4297
+ }
4298
+
4299
+ isValid()
4300
+ {
4301
+ let issuer = this.getIssuerCN();
4302
+ if (issuer == null)
4303
+ {
4304
+ return CertValidStatus.FileFormatInvalid;
4305
+ }
4306
+ let dt;
4307
+ let currTime = data.Timestamp.now();
4308
+ if ((dt = this.getThisUpdate()) == null)
4309
+ {
4310
+ return CertValidStatus.FileFormatInvalid;
4311
+ }
4312
+ if (dt.toTicks() > currTime.toTicks())
4313
+ {
4314
+ return CertValidStatus.Expired;
4315
+ }
4316
+ if (dt = this.getNextUpdate())
4317
+ {
4318
+ if (dt.toTicks() < currTime.toTicks())
4319
+ {
4320
+ return CertValidStatus.Expired;
4321
+ }
4322
+ }
4323
+ let signedInfo = this.getSignedInfo();
4324
+ if (signedInfo == null)
4325
+ {
4326
+ return CertValidStatus.FileFormatInvalid;
4327
+ }
4328
+ let hashType = algTypeGetHash(signedInfo.algType);
4329
+ if (hashType == hash.HashType.Unknown)
4330
+ {
4331
+ return CertValidStatus.UnsupportedAlgorithm;
4332
+ }
4333
+
4334
+ let issuerCert = null; //trustStore.getCertByCN(issuer);
4335
+ if (issuerCert == null)
4336
+ {
4337
+ return CertValidStatus.UnknownIssuer;
4338
+ }
4339
+ let key = issuerCert.getNewPublicKey();
4340
+ if (key == null)
4341
+ {
4342
+ return CertValidStatus.FileFormatInvalid;
4343
+ }
4344
+ let signValid = key.signatureVerify(hashType, signedInfo.payload, signedInfo.signature);
4345
+ if (!signValid)
4346
+ {
4347
+ return CertValidStatus.SignatureInvalid;
4348
+ }
4349
+ return CertValidStatus.Valid;
4350
+ }
4351
+
4352
+ clone()
4353
+ {
4354
+ return new X509CRL(this.sourceName, this.reader.getArrayBuffer());
4355
+ }
4356
+
4357
+ toString()
4358
+ {
4359
+ let sb = [];
4360
+ if (X509File.isCertificateList(this.reader, 0, this.reader.getLength(), "1"))
4361
+ {
4362
+ X509File.appendCertificateList(this.reader, 0, this.reader.getLength(), "1", sb, null);
4363
+ }
4364
+ return sb.join("");
4365
+ }
4366
+
4367
+ createNames()
4368
+ {
4369
+ return new ASN1Names().setCertificateList();
4370
+ }
4371
+
4372
+ hasVersion()
4373
+ {
4374
+ let itemPDU;
4375
+ if ((itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.1") != null) && itemPDU.itemType == ASN1ItemType.INTEGER)
4376
+ {
4377
+ return true;
4378
+ }
4379
+ return false;
4380
+ }
4381
+
4382
+ getIssuerCN()
4383
+ {
4384
+ let tmpBuff;
4385
+ if (this.hasVersion())
4386
+ {
4387
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
4388
+ }
4389
+ else
4390
+ {
4391
+ tmpBuff = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.2");
4392
+ }
4393
+ if (tmpBuff != null && tmpBuff.itemType == ASN1ItemType.SEQUENCE)
4394
+ {
4395
+ return X509File.nameGetCN(reader, tmpBuff.dataOfst, tmpBuff.endOfst);
4396
+ }
4397
+ else
4398
+ {
4399
+ return null;
4400
+ }
4401
+ }
4402
+
4403
+ getThisUpdate()
4404
+ {
4405
+ let itemPDU;
4406
+ if (this.hasVersion())
4407
+ {
4408
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4");
4409
+ }
4410
+ else
4411
+ {
4412
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.3");
4413
+ }
4414
+ if (itemPDU != null && (itemPDU.itemType == ASN1ItemType.UTCTIME || itemPDU.itemType == ASN1ItemType.GENERALIZEDTIME))
4415
+ {
4416
+ return ASN1Util.pduParseUTCTimeCont(reader, itemPDU.dataOfst, itemPDU.endOfst);
4417
+ }
4418
+ else
4419
+ {
4420
+ return null;
4421
+ }
4422
+ }
4423
+
4424
+ getNextUpdate()
4425
+ {
4426
+ let itemPDU;
4427
+ if (this.hasVersion())
4428
+ {
4429
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.5");
4430
+ }
4431
+ else
4432
+ {
4433
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4");
4434
+ }
4435
+ if (itemPDU != null && (itemPDU.itemType == ASN1ItemType.UTCTIME || itemPDU.itemType == ASN1ItemType.GENERALIZEDTIME))
4436
+ {
4437
+ return ASN1Util.pduParseUTCTimeCont(reader, itemPDU.dataOfst, itemPDU.endOfst);
4438
+ }
4439
+ else
4440
+ {
4441
+ return null;
4442
+ }
4443
+ }
4444
+
4445
+ isRevoked(cert)
4446
+ {
4447
+ let sn = cert.getSerialNumber();
4448
+ if (sn == null)
4449
+ return true;
4450
+ let itemPDU;
4451
+ let i;
4452
+ if (this.hasVersion())
4453
+ {
4454
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.5");
4455
+ i = 5;
4456
+ }
4457
+ else
4458
+ {
4459
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1.4");
4460
+ i = 4;
4461
+ }
4462
+ if (itemPDU == null)
4463
+ {
4464
+ return false;
4465
+ }
4466
+ if (itemPDU.itemType != ASN1ItemType.SEQUENCE)
4467
+ {
4468
+ i++;
4469
+ itemPDU = ASN1Util.pduGetItem(this.reader, 0, this.reader.getLength(), "1.1."+i);
4470
+ if (itemPDU == null || itemPDU.itemType != ASN1ItemType.SEQUENCE)
4471
+ {
4472
+ return false;
4473
+ }
4474
+ }
4475
+ i = 0;
4476
+ while (true)
4477
+ {
4478
+ let subitemPDU;
4479
+ subitemPDU = ASN1Util.pduGetItem(reader, itemPDU.dataOfst, itemPDU.endOfst, (++i)+".1");
4480
+ if (subitemPDU == null)
4481
+ break;
4482
+ if (subitemPDU.itemType == ASN1ItemType.INTEGER && data.arrayBufferEquals(reader.getArrayBuffer(subitemPDU.dataOfst, subitemPDU.contLen), sn))
4483
+ {
4484
+ return true;
4485
+ }
4486
+ }
4487
+ return false;
4488
+ }
4489
+ }
4490
+
4491
+ export function algTypeGetHash(algType)
4492
+ {
4493
+ switch (algType)
4494
+ {
4495
+ case AlgType.SHA1WithRSAEncryption:
4496
+ return hash.HashType.SHA1;
4497
+ case AlgType.SHA256WithRSAEncryption:
4498
+ return hash.HashType.SHA256;
4499
+ case AlgType.SHA512WithRSAEncryption:
4500
+ return hash.HashType.SHA512;
4501
+ case AlgType.SHA384WithRSAEncryption:
4502
+ return hash.HashType.SHA384;
4503
+ case AlgType.SHA224WithRSAEncryption:
4504
+ return hash.HashType.SHA224;
4505
+ case AlgType.MD2WithRSAEncryption:
4506
+ return hash.HashType.Unknown;
4507
+ case AlgType.MD5WithRSAEncryption:
4508
+ return hash.HashType.MD5;
4509
+ case AlgType.ECDSAWithSHA256:
4510
+ return hash.HashType.SHA256;
4511
+ case AlgType.ECDSAWithSHA384:
4512
+ return hash.HashType.SHA384;
4513
+ case AlgType.Unknown:
4514
+ default:
4515
+ return hash.HashType.Unknown;
4516
+ }
4517
+ }
4518
+
4519
+ export function fileTypeGetName(fileType)
4520
+ {
4521
+ switch (fileType)
4522
+ {
4523
+ case X509FileType.Cert:
4524
+ return "Cert";
4525
+ case X509FileType.CertRequest:
4526
+ return "CertReq";
4527
+ case X509FileType.Key:
4528
+ return "Key";
4529
+ case X509FileType.PrivateKey:
4530
+ return "PrivateKey";
4531
+ case X509FileType.PublicKey:
4532
+ return "PublicKey";
4533
+ case X509FileType.PKCS7:
4534
+ return "PKCS7";
4535
+ case X509FileType.PKCS12:
4536
+ return "PKCS12";
4537
+ case X509FileType.CRL:
4538
+ return "CRL";
4539
+ case X509FileType.FileList:
4540
+ return "FileList";
4541
+ default:
4542
+ return "Unknown";
4543
+ }
4544
+ }
4545
+
4546
+ export function keyTypeGetName(keyType)
4547
+ {
4548
+ switch (keyType)
4549
+ {
4550
+ case KeyType.RSA:
4551
+ return "RSA";
4552
+ case KeyType.DSA:
4553
+ return "DSA";
4554
+ case KeyType.ECDSA:
4555
+ return "ECDSA";
4556
+ case KeyType.ED25519:
4557
+ return "ED25519";
4558
+ case KeyType.RSAPublic:
4559
+ return "RSAPublic";
4560
+ case KeyType.ECPublic:
4561
+ return "ECPublic";
4562
+ case KeyType.Unknown:
4563
+ default:
4564
+ return "Unknown";
4565
+ }
4566
+ }
4567
+
4568
+ export function ecNameGetName(ecName)
4569
+ {
4570
+ switch (ecName)
4571
+ {
4572
+ case ECName.secp256r1:
4573
+ return "secp256r1";
4574
+ case ECName.secp384r1:
4575
+ return "secp384r1";
4576
+ case ECName.secp521r1:
4577
+ return "secp521r1";
4578
+ case ECName.Unknown:
4579
+ default:
4580
+ return "Unknown";
4581
+ }
4582
+ }
4583
+
4584
+ export function ecNameGetOID(ecName)
4585
+ {
4586
+ switch (ecName)
4587
+ {
4588
+ case ECName.secp256r1:
4589
+ return "1.2.840.10045.3.1.7";
4590
+ case ECName.secp384r1:
4591
+ return "1.3.132.0.34";
4592
+ case ECName.secp521r1:
4593
+ return "1.3.132.0.35";
4594
+ case ECName.Unknown:
4595
+ default:
4596
+ return "1.3.132.0.34";
4597
+ }
4598
+ }
4599
+
4600
+ export function ecNameFromOID(buff)
4601
+ {
4602
+ let arr = new Uint8Array(buff);
4603
+ if (ASN1Util.oidEqualsText(arr, "1.2.840.10045.3.1.7"))
4604
+ {
4605
+ return ECName.secp256r1;
4606
+ }
4607
+ else if (ASN1Util.oidEqualsText(arr, "1.3.132.0.34"))
4608
+ {
4609
+ return ECName.secp384r1;
4610
+ }
4611
+ else if (ASN1Util.oidEqualsText(arr, "1.3.132.0.35"))
4612
+ {
4613
+ return ECName.secp521r1;
4614
+ }
4615
+ return ECName.Unknown;
4616
+ }