node-opcua-crypto 1.11.0 → 2.0.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.
Files changed (108) hide show
  1. package/.fossa.yml +18 -18
  2. package/.github/FUNDING.yml +12 -12
  3. package/.github/workflows/main.yml +32 -32
  4. package/.prettierrc.js +6 -6
  5. package/LICENSE +23 -22
  6. package/README.md +14 -14
  7. package/dist/source/asn1.d.ts +73 -73
  8. package/dist/source/asn1.js +359 -359
  9. package/dist/source/buffer_utils.d.ts +6 -6
  10. package/dist/source/buffer_utils.js +21 -21
  11. package/dist/source/certificate_matches_private_key.d.ts +2 -0
  12. package/dist/source/certificate_matches_private_key.js +22 -0
  13. package/dist/source/certificate_matches_private_key.js.map +1 -0
  14. package/dist/source/common.d.ts +12 -12
  15. package/dist/source/common.js +2 -2
  16. package/dist/source/crypto_explore_certificate.d.ts +107 -107
  17. package/dist/source/crypto_explore_certificate.js +601 -600
  18. package/dist/source/crypto_explore_certificate.js.map +1 -1
  19. package/dist/source/crypto_utils.d.ts +80 -78
  20. package/dist/source/crypto_utils.js +312 -280
  21. package/dist/source/crypto_utils.js.map +1 -1
  22. package/dist/source/derived_keys.d.ts +72 -72
  23. package/dist/source/derived_keys.js +248 -248
  24. package/dist/source/explore_certificate.d.ts +30 -30
  25. package/dist/source/explore_certificate.js +43 -43
  26. package/dist/source/explore_certificate_revocation_list.d.ts +28 -28
  27. package/dist/source/explore_certificate_revocation_list.js +69 -69
  28. package/dist/source/explore_certificate_signing_request.d.ts +13 -13
  29. package/dist/source/explore_certificate_signing_request.js +44 -44
  30. package/dist/source/explore_private_key.d.ts +29 -29
  31. package/dist/source/explore_private_key.js +96 -96
  32. package/dist/source/explore_private_key.js.map +1 -1
  33. package/dist/source/index.d.ts +13 -13
  34. package/dist/source/index.js +29 -29
  35. package/dist/source/oid_map.d.ts +7 -7
  36. package/dist/source/oid_map.js +303 -303
  37. package/dist/source/public_private_match.d.ts +3 -3
  38. package/dist/source/public_private_match.js +37 -16
  39. package/dist/source/public_private_match.js.map +1 -1
  40. package/dist/source/verify_certificate_signature.d.ts +10 -10
  41. package/dist/source/verify_certificate_signature.js +101 -101
  42. package/dist/source_nodejs/index.d.ts +3 -3
  43. package/dist/source_nodejs/index.js +19 -19
  44. package/dist/source_nodejs/read.d.ts +29 -29
  45. package/dist/source_nodejs/read.js +94 -94
  46. package/dist/source_nodejs/read.js.map +1 -1
  47. package/dist/source_nodejs/read_certificate_revocation_list.d.ts +2 -2
  48. package/dist/source_nodejs/read_certificate_revocation_list.js +27 -27
  49. package/dist/source_nodejs/read_certificate_signing_request.d.ts +3 -3
  50. package/dist/source_nodejs/read_certificate_signing_request.js +27 -27
  51. package/index.d.ts +2 -2
  52. package/index.js +4 -4
  53. package/index_web.js +3 -3
  54. package/package.json +12 -12
  55. package/source/asn1.ts +404 -404
  56. package/source/buffer_utils.ts +18 -18
  57. package/source/common.ts +13 -13
  58. package/source/crypto_explore_certificate.ts +764 -763
  59. package/source/crypto_utils.ts +359 -321
  60. package/source/derived_keys.ts +287 -287
  61. package/source/explore_certificate.ts +66 -66
  62. package/source/explore_certificate_revocation_list.ts +122 -122
  63. package/source/explore_certificate_signing_request.ts +58 -58
  64. package/source/explore_private_key.ts +121 -121
  65. package/source/index.ts +13 -13
  66. package/source/oid_map.ts +310 -310
  67. package/source/public_private_match.ts +41 -18
  68. package/source/verify_certificate_signature.ts +105 -105
  69. package/source_nodejs/index.ts +2 -2
  70. package/source_nodejs/read.ts +95 -95
  71. package/source_nodejs/read_certificate_revocation_list.ts +14 -14
  72. package/source_nodejs/read_certificate_signing_request.ts +17 -17
  73. package/test_certificate.ts +34 -34
  74. package/tsconfig.json +18 -18
  75. package/tslint.json +34 -34
  76. package/dist/asn1.d.ts +0 -69
  77. package/dist/asn1.js +0 -349
  78. package/dist/asn1.js.map +0 -1
  79. package/dist/buffer_utils.d.ts +0 -6
  80. package/dist/buffer_utils.js +0 -22
  81. package/dist/buffer_utils.js.map +0 -1
  82. package/dist/common.d.ts +0 -11
  83. package/dist/common.js +0 -3
  84. package/dist/common.js.map +0 -1
  85. package/dist/crypto_explore_certificate.d.ts +0 -95
  86. package/dist/crypto_explore_certificate.js +0 -547
  87. package/dist/crypto_explore_certificate.js.map +0 -1
  88. package/dist/crypto_utils.d.ts +0 -106
  89. package/dist/crypto_utils.js +0 -370
  90. package/dist/crypto_utils.js.map +0 -1
  91. package/dist/derived_keys.d.ts +0 -72
  92. package/dist/derived_keys.js +0 -247
  93. package/dist/derived_keys.js.map +0 -1
  94. package/dist/explore_certificate.d.ts +0 -30
  95. package/dist/explore_certificate.js +0 -44
  96. package/dist/explore_certificate.js.map +0 -1
  97. package/dist/explore_certificate_revocation_list.d.ts +0 -30
  98. package/dist/explore_certificate_revocation_list.js +0 -67
  99. package/dist/explore_certificate_revocation_list.js.map +0 -1
  100. package/dist/index.d.ts +0 -10
  101. package/dist/index.js +0 -23
  102. package/dist/index.js.map +0 -1
  103. package/dist/oid_map.d.ts +0 -7
  104. package/dist/oid_map.js +0 -262
  105. package/dist/oid_map.js.map +0 -1
  106. package/dist/verify_cerficate_signature.d.ts +0 -10
  107. package/dist/verify_cerficate_signature.js +0 -102
  108. package/dist/verify_cerficate_signature.js.map +0 -1
package/source/asn1.ts CHANGED
@@ -1,404 +1,404 @@
1
- import * as assert from "assert";
2
- import { oid_map } from "./oid_map";
3
-
4
- // https://github.com/lapo-luchini/asn1js/blob/master/asn1.js
5
- export enum TagType {
6
- BOOLEAN = 0x01,
7
- INTEGER = 0x02,
8
- BIT_STRING = 0x03,
9
- OCTET_STRING = 0x04,
10
- NULL = 0x05,
11
- OBJECT_IDENTIFIER = 0x06,
12
- UTF8String = 0x0c,
13
- NumericString = 0x12,
14
- PrintableString = 0x13,
15
- TeletexString = 0x14,
16
- IA5String = 0x16,
17
- UTCTime = 0x17,
18
- GeneralizedTime = 0x18,
19
- GraphicString = 0x19,
20
- VisibleString = 0x1a,
21
- GeneralString = 0x1b,
22
- UniversalString = 0x1c,
23
- BMPString = 0x1e,
24
-
25
- SEQUENCE = 0x30,
26
- SET = 0x31,
27
-
28
- A3 = 0xA3
29
- }
30
-
31
- export interface BlockInfo {
32
- tag: TagType | number;
33
- position: number;
34
- length: number;
35
- }
36
-
37
- export function readTag(buf: Buffer, pos: number): BlockInfo {
38
- assert(buf instanceof Buffer);
39
- assert(Number.isFinite(pos) && pos >= 0);
40
- // istanbul ignore next
41
- if (buf.length <= pos) {
42
- throw new Error("Invalid position : buf.length=" + buf.length + " pos =" + pos);
43
- }
44
- const tag = buf.readUInt8(pos);
45
- pos += 1;
46
-
47
- let length = buf.readUInt8(pos);
48
- pos += 1;
49
-
50
- // tslint:disable:no-bitwise
51
- if (length > 127) {
52
- const nbBytes = length & 0x7f;
53
- length = 0;
54
- for (let i = 0; i < nbBytes; i++) {
55
- length = length * 256 + buf.readUInt8(pos);
56
- pos += 1;
57
- }
58
- }
59
- return { tag, position: pos, length };
60
- }
61
-
62
- export function _readStruct(buf: Buffer, blockInfo: BlockInfo): BlockInfo[] {
63
- const length = blockInfo.length;
64
- let cursor = blockInfo.position;
65
- const end = blockInfo.position + length;
66
- const blocks: BlockInfo[] = [];
67
- while (cursor < end) {
68
- const inner = readTag(buf, cursor);
69
- cursor = inner.position + inner.length;
70
- blocks.push(inner);
71
- }
72
- return blocks;
73
- }
74
-
75
- export function parseBitString(buffer: Buffer, start: number, end: number, maxLength: number): string {
76
- const unusedBit = buffer.readUInt8(start),
77
- lenBit = ((end - start - 1) << 3) - unusedBit,
78
- intro = "(" + lenBit + " bit)\n";
79
-
80
- let s = "",
81
- skip = unusedBit;
82
-
83
- for (let i = end - 1; i > start; --i) {
84
- const b = buffer.readUInt8(i);
85
-
86
- for (let j = skip; j < 8; ++j) {
87
- // noinspection JSBitwiseOperatorUsage
88
- s += (b >> j) & 1 ? "1" : "0";
89
- }
90
- skip = 0;
91
- assert(s.length <= maxLength);
92
- }
93
- return intro + s;
94
- }
95
-
96
- export interface BitString {
97
- lengthInBits: number;
98
- lengthInBytes: number;
99
- data: Buffer;
100
- debug?: any;
101
- }
102
-
103
- export function _readBitString(buffer: Buffer, block: BlockInfo): BitString {
104
- assert(block.tag === TagType.BIT_STRING);
105
- const data = _getBlock(buffer, block);
106
- // number of skipped bits
107
- const ignore_bits = data.readUInt8(0);
108
-
109
- return {
110
- lengthInBits: data.length * 8 - ignore_bits,
111
- lengthInBytes: data.length - 1,
112
- data: data.slice(1),
113
- debug: parseBitString(buffer, block.position, block.length + block.position, 5000),
114
- };
115
- }
116
-
117
- export function formatBuffer2DigitHexWithColum(buffer: Buffer): string {
118
- const value: string[] = [];
119
- for (let i = 0; i < buffer.length; i++) {
120
- value.push(("00" + buffer.readUInt8(i).toString(16)).substr(-2, 2));
121
- }
122
- // remove leading 00
123
- return value
124
- .join(":")
125
- .toUpperCase()
126
- .replace(/^(00:)*/, "");
127
- }
128
-
129
- export function _readOctetString(buffer: Buffer, block: BlockInfo): Buffer {
130
- assert(block.tag === TagType.OCTET_STRING);
131
- const tag = readTag(buffer, block.position);
132
- assert(tag.tag === TagType.OCTET_STRING);
133
-
134
- const nbBytes = tag.length;
135
- const pos = tag.position;
136
- const b = buffer.slice(pos, pos + nbBytes);
137
- return b;
138
- }
139
-
140
- export function _getBlock(buffer: Buffer, block: BlockInfo): Buffer {
141
- const start = block.position;
142
- const end = block.position + block.length;
143
- return buffer.slice(start, end);
144
- }
145
-
146
- export interface AlgorithmIdentifier {
147
- identifier: string;
148
- }
149
-
150
- export function _readIntegerAsByteString(buffer: Buffer, block: BlockInfo): Buffer {
151
- return _getBlock(buffer, block);
152
- }
153
-
154
- export function _readListOfInteger(buffer: Buffer): Buffer[] {
155
- const block = readTag(buffer, 0);
156
- const inner_blocks = _readStruct(buffer, block);
157
- return inner_blocks.map((bblock: BlockInfo) => {
158
- return _readIntegerAsByteString(buffer, bblock);
159
- });
160
- }
161
-
162
- function parseOID(buffer: Buffer, start: number, end: number): string {
163
- // ASN.1 JavaScript decoder
164
- // Copyright (c) 2008-2014 Lapo Luchini <lapo@lapo.it>
165
- let s = "",
166
- n = 0,
167
- bits = 0;
168
- for (let i = start; i < end; ++i) {
169
- const v = buffer.readUInt8(i);
170
-
171
- // tslint:disable-next-line: no-bitwise
172
- n = n * 128 + (v & 0x7f);
173
- bits += 7;
174
-
175
- // noinspection JSBitwiseOperatorUsage
176
- // tslint:disable-next-line: no-bitwise
177
- if (!(v & 0x80)) {
178
- // finished
179
- if (s === "") {
180
- const m = n < 80 ? (n < 40 ? 0 : 1) : 2;
181
- s = m + "." + (n - m * 40);
182
- } else {
183
- s += "." + n.toString();
184
- }
185
- n = 0;
186
- bits = 0;
187
- }
188
- }
189
- assert(bits === 0); // if (bits > 0) { s += ".incomplete"; }
190
- return s;
191
- }
192
-
193
- export function _readObjectIdentifier(buffer: Buffer, block: BlockInfo): { oid: string; name: string } {
194
- assert(block.tag === TagType.OBJECT_IDENTIFIER);
195
- const b = buffer.slice(block.position, block.position + block.length);
196
- const oid = parseOID(b, 0, block.length);
197
- return {
198
- oid,
199
- name: oid_map[oid] ? oid_map[oid].d : oid,
200
- };
201
- }
202
-
203
- export function _readAlgorithmIdentifier(buffer: Buffer, block: BlockInfo): AlgorithmIdentifier {
204
- const inner_blocks = _readStruct(buffer, block);
205
- return {
206
- identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name,
207
- }
208
- };
209
-
210
- export function _readECCAlgorithmIdentifier(buffer: Buffer, block: BlockInfo): AlgorithmIdentifier {
211
- const inner_blocks = _readStruct(buffer, block);
212
- return {
213
- identifier: _readObjectIdentifier(buffer, inner_blocks[1]).name, // difference with RSA as algorithm is second element of nested block
214
- }
215
- };
216
-
217
- export type SignatureValue = string;
218
-
219
- export function _readSignatureValueBin(buffer: Buffer, block: BlockInfo): Buffer {
220
- return _readBitString(buffer, block).data;
221
- }
222
-
223
- export function _readSignatureValue(buffer: Buffer, block: BlockInfo): SignatureValue {
224
- return _readSignatureValueBin(buffer, block).toString("hex");
225
- }
226
-
227
- export function _readLongIntegerValue(buffer: Buffer, block: BlockInfo): Buffer {
228
- assert(block.tag === TagType.INTEGER, "expecting a INTEGER tag");
229
- const pos = block.position;
230
- const nbBytes = block.length;
231
- const buf = buffer.slice(pos, pos + nbBytes);
232
- return buf;
233
- }
234
-
235
- export function _readIntegerValue(buffer: Buffer, block: BlockInfo): number {
236
- assert(block.tag === TagType.INTEGER, "expecting a INTEGER tag");
237
- let pos = block.position;
238
- const nbBytes = block.length;
239
- assert(nbBytes < 4);
240
- let value = 0;
241
- for (let i = 0; i < nbBytes; i++) {
242
- value = value * 256 + buffer.readUInt8(pos);
243
- pos += 1;
244
- }
245
- return value;
246
- }
247
-
248
- export function _readBooleanValue(buffer: Buffer, block: BlockInfo): boolean {
249
- assert(block.tag === TagType.BOOLEAN, "expecting a BOOLEAN tag. got " + TagType[block.tag]);
250
- const pos = block.position;
251
- const nbBytes = block.length;
252
- assert(nbBytes < 4);
253
- const value = buffer.readUInt8(pos) ? true : false;
254
- return value as boolean;
255
- }
256
-
257
- export function _readVersionValue(buffer: Buffer, block: BlockInfo): number {
258
- block = readTag(buffer, block.position);
259
- return _readIntegerValue(buffer, block);
260
- }
261
-
262
- /*
263
- 4.1.2.5.2 GeneralizedTime
264
-
265
- The generalized time type, GeneralizedTime, is a standard ASN.1 type
266
- for variable precision representation of time. Optionally, the
267
- GeneralizedTime field can include a representation of the time
268
- differential between local and Greenwich Mean Time.
269
-
270
- For the purposes of this profile, GeneralizedTime values MUST be
271
- expressed Greenwich Mean Time (Zulu) and MUST include seconds (i.e.,
272
- times are YYYYMMDDHHMMSSZ), even where the number of seconds is zero.
273
- GeneralizedTime values MUST NOT include fractional seconds.
274
-
275
- */
276
- function convertGeneralizedTime(str: string): Date {
277
- const year = parseInt(str.substr(0, 4), 10);
278
- const month = parseInt(str.substr(4, 2), 10) - 1;
279
- const day = parseInt(str.substr(6, 2), 10);
280
- const hours = parseInt(str.substr(8, 2), 10);
281
- const mins = parseInt(str.substr(10, 2), 10);
282
- const secs = parseInt(str.substr(12, 2), 10);
283
-
284
- return new Date(Date.UTC(year, month, day, hours, mins, secs));
285
- }
286
-
287
- function _readBMPString(buffer: Buffer, block: BlockInfo): string {
288
- const strBuff = _getBlock(buffer, block);
289
- let str = "";
290
- for (let i = 0; i < strBuff.length; i += 2) {
291
- const word = strBuff.readUInt16BE(i);
292
- str += String.fromCharCode(word);
293
- }
294
- return str;
295
- }
296
-
297
- /*
298
- http://tools.ietf.org/html/rfc5280
299
-
300
- 4.1.2.5. Validity
301
- [...]
302
- As conforming to this profile MUST always encode certificate
303
- validity dates through the year 2049 as UTCTime; certificate validity
304
- dates in 2050 or later MUST be encoded as GeneralizedTime.
305
- Conforming applications MUST be able to process validity dates that
306
- are encoded in either UTCTime or GeneralizedTime.
307
- [...]
308
-
309
- 4.1.2.5.1 UTCTime
310
-
311
- The universal time type, UTCTime, is a standard ASN.1 type intended
312
- for representation of dates and time. UTCTime specifies the year
313
- through the two low order digits and time is specified to the
314
- precision of one minute or one second. UTCTime includes either Z
315
- (for Zulu, or Greenwich Mean Time) or a time differential.
316
-
317
- For the purposes of this profile, UTCTime values MUST be expressed
318
- Greenwich Mean Time (Zulu) and MUST include seconds (i.e., times are
319
- YYMMDDHHMMSSZ), even where the number of seconds is zero. Conforming
320
- systems MUST interpret the year field (YY) as follows:
321
-
322
- Where YY is greater than or equal to 50, the year SHALL be
323
- interpreted as 19YY; and
324
-
325
- Where YY is less than 50, the year SHALL be interpreted as 20YY.
326
- */
327
- function convertUTCTime(str: string): Date {
328
- let year = parseInt(str.substr(0, 2), 10);
329
- const month = parseInt(str.substr(2, 2), 10) - 1;
330
- const day = parseInt(str.substr(4, 2), 10);
331
- const hours = parseInt(str.substr(6, 2), 10);
332
- const mins = parseInt(str.substr(8, 2), 10);
333
- const secs = parseInt(str.substr(10, 2), 10);
334
-
335
- year += year >= 50 ? 1900 : 2000;
336
- return new Date(Date.UTC(year, month, day, hours, mins, secs));
337
- }
338
-
339
- export function _readValue(buffer: Buffer, block: BlockInfo): any {
340
- switch (block.tag) {
341
- case TagType.BOOLEAN:
342
- return _readBooleanValue(buffer, block);
343
- case TagType.BMPString:
344
- return _readBMPString(buffer, block);
345
- case TagType.PrintableString:
346
- case TagType.TeletexString:
347
- case TagType.UTF8String:
348
- case TagType.NumericString:
349
- case TagType.IA5String:
350
- return _getBlock(buffer, block).toString("ascii");
351
- case TagType.UTCTime:
352
- return convertUTCTime(_getBlock(buffer, block).toString("ascii"));
353
- case TagType.GeneralizedTime:
354
- return convertGeneralizedTime(_getBlock(buffer, block).toString("ascii"));
355
- default:
356
- throw new Error("Invalid tag 0x" + block.tag.toString(16) + "");
357
- //xx return " ??? <" + block.tag + ">";
358
- }
359
- }
360
-
361
- export interface DirectoryName {
362
- stateOrProvinceName?: string;
363
- localityName?: string;
364
- organizationName?: string;
365
- organizationUnitName?: string;
366
- commonName?: string;
367
- countryName?: string;
368
- }
369
- export function compactDirectoryName(d: DirectoryName): string {
370
- return JSON.stringify(d);
371
- }
372
-
373
- export function _readDirectoryName(buffer: Buffer, block: BlockInfo): DirectoryName {
374
- // AttributeTypeAndValue ::= SEQUENCE {
375
- // type ATTRIBUTE.&id({SupportedAttributes}),
376
- // value ATTRIBUTE.&Type({SupportedAttributes}{@type}),
377
- const set_blocks = _readStruct(buffer, block);
378
- const names: DirectoryName = {};
379
- for (const set_block of set_blocks) {
380
- assert(set_block.tag === 0x31);
381
- const blocks = _readStruct(buffer, set_block);
382
- assert(blocks.length === 1);
383
- assert(blocks[0].tag === 0x30);
384
-
385
- const sequenceBlock = _readStruct(buffer, blocks[0]);
386
- assert(sequenceBlock.length === 2);
387
-
388
- const type = _readObjectIdentifier(buffer, sequenceBlock[0]);
389
- (names as any)[type.name] = _readValue(buffer, sequenceBlock[1]);
390
- }
391
- return names;
392
- }
393
-
394
- export function _findBlockAtIndex(blocks: BlockInfo[], index: number): BlockInfo | null {
395
- const tmp = blocks.filter((b: BlockInfo) => b.tag === 0xa0 + index || b.tag === 0x80 + index);
396
- if (tmp.length === 0) {
397
- return null;
398
- }
399
- return tmp[0];
400
- }
401
-
402
- export function _readTime(buffer: Buffer, block: BlockInfo): any {
403
- return _readValue(buffer, block);
404
- }
1
+ import * as assert from "assert";
2
+ import { oid_map } from "./oid_map";
3
+
4
+ // https://github.com/lapo-luchini/asn1js/blob/master/asn1.js
5
+ export enum TagType {
6
+ BOOLEAN = 0x01,
7
+ INTEGER = 0x02,
8
+ BIT_STRING = 0x03,
9
+ OCTET_STRING = 0x04,
10
+ NULL = 0x05,
11
+ OBJECT_IDENTIFIER = 0x06,
12
+ UTF8String = 0x0c,
13
+ NumericString = 0x12,
14
+ PrintableString = 0x13,
15
+ TeletexString = 0x14,
16
+ IA5String = 0x16,
17
+ UTCTime = 0x17,
18
+ GeneralizedTime = 0x18,
19
+ GraphicString = 0x19,
20
+ VisibleString = 0x1a,
21
+ GeneralString = 0x1b,
22
+ UniversalString = 0x1c,
23
+ BMPString = 0x1e,
24
+
25
+ SEQUENCE = 0x30,
26
+ SET = 0x31,
27
+
28
+ A3 = 0xA3
29
+ }
30
+
31
+ export interface BlockInfo {
32
+ tag: TagType | number;
33
+ position: number;
34
+ length: number;
35
+ }
36
+
37
+ export function readTag(buf: Buffer, pos: number): BlockInfo {
38
+ assert(buf instanceof Buffer);
39
+ assert(Number.isFinite(pos) && pos >= 0);
40
+ // istanbul ignore next
41
+ if (buf.length <= pos) {
42
+ throw new Error("Invalid position : buf.length=" + buf.length + " pos =" + pos);
43
+ }
44
+ const tag = buf.readUInt8(pos);
45
+ pos += 1;
46
+
47
+ let length = buf.readUInt8(pos);
48
+ pos += 1;
49
+
50
+ // tslint:disable:no-bitwise
51
+ if (length > 127) {
52
+ const nbBytes = length & 0x7f;
53
+ length = 0;
54
+ for (let i = 0; i < nbBytes; i++) {
55
+ length = length * 256 + buf.readUInt8(pos);
56
+ pos += 1;
57
+ }
58
+ }
59
+ return { tag, position: pos, length };
60
+ }
61
+
62
+ export function _readStruct(buf: Buffer, blockInfo: BlockInfo): BlockInfo[] {
63
+ const length = blockInfo.length;
64
+ let cursor = blockInfo.position;
65
+ const end = blockInfo.position + length;
66
+ const blocks: BlockInfo[] = [];
67
+ while (cursor < end) {
68
+ const inner = readTag(buf, cursor);
69
+ cursor = inner.position + inner.length;
70
+ blocks.push(inner);
71
+ }
72
+ return blocks;
73
+ }
74
+
75
+ export function parseBitString(buffer: Buffer, start: number, end: number, maxLength: number): string {
76
+ const unusedBit = buffer.readUInt8(start),
77
+ lenBit = ((end - start - 1) << 3) - unusedBit,
78
+ intro = "(" + lenBit + " bit)\n";
79
+
80
+ let s = "",
81
+ skip = unusedBit;
82
+
83
+ for (let i = end - 1; i > start; --i) {
84
+ const b = buffer.readUInt8(i);
85
+
86
+ for (let j = skip; j < 8; ++j) {
87
+ // noinspection JSBitwiseOperatorUsage
88
+ s += (b >> j) & 1 ? "1" : "0";
89
+ }
90
+ skip = 0;
91
+ assert(s.length <= maxLength);
92
+ }
93
+ return intro + s;
94
+ }
95
+
96
+ export interface BitString {
97
+ lengthInBits: number;
98
+ lengthInBytes: number;
99
+ data: Buffer;
100
+ debug?: any;
101
+ }
102
+
103
+ export function _readBitString(buffer: Buffer, block: BlockInfo): BitString {
104
+ assert(block.tag === TagType.BIT_STRING);
105
+ const data = _getBlock(buffer, block);
106
+ // number of skipped bits
107
+ const ignore_bits = data.readUInt8(0);
108
+
109
+ return {
110
+ lengthInBits: data.length * 8 - ignore_bits,
111
+ lengthInBytes: data.length - 1,
112
+ data: data.slice(1),
113
+ debug: parseBitString(buffer, block.position, block.length + block.position, 5000),
114
+ };
115
+ }
116
+
117
+ export function formatBuffer2DigitHexWithColum(buffer: Buffer): string {
118
+ const value: string[] = [];
119
+ for (let i = 0; i < buffer.length; i++) {
120
+ value.push(("00" + buffer.readUInt8(i).toString(16)).substr(-2, 2));
121
+ }
122
+ // remove leading 00
123
+ return value
124
+ .join(":")
125
+ .toUpperCase()
126
+ .replace(/^(00:)*/, "");
127
+ }
128
+
129
+ export function _readOctetString(buffer: Buffer, block: BlockInfo): Buffer {
130
+ assert(block.tag === TagType.OCTET_STRING);
131
+ const tag = readTag(buffer, block.position);
132
+ assert(tag.tag === TagType.OCTET_STRING);
133
+
134
+ const nbBytes = tag.length;
135
+ const pos = tag.position;
136
+ const b = buffer.slice(pos, pos + nbBytes);
137
+ return b;
138
+ }
139
+
140
+ export function _getBlock(buffer: Buffer, block: BlockInfo): Buffer {
141
+ const start = block.position;
142
+ const end = block.position + block.length;
143
+ return buffer.slice(start, end);
144
+ }
145
+
146
+ export interface AlgorithmIdentifier {
147
+ identifier: string;
148
+ }
149
+
150
+ export function _readIntegerAsByteString(buffer: Buffer, block: BlockInfo): Buffer {
151
+ return _getBlock(buffer, block);
152
+ }
153
+
154
+ export function _readListOfInteger(buffer: Buffer): Buffer[] {
155
+ const block = readTag(buffer, 0);
156
+ const inner_blocks = _readStruct(buffer, block);
157
+ return inner_blocks.map((bblock: BlockInfo) => {
158
+ return _readIntegerAsByteString(buffer, bblock);
159
+ });
160
+ }
161
+
162
+ function parseOID(buffer: Buffer, start: number, end: number): string {
163
+ // ASN.1 JavaScript decoder
164
+ // Copyright (c) 2008-2014 Lapo Luchini <lapo@lapo.it>
165
+ let s = "",
166
+ n = 0,
167
+ bits = 0;
168
+ for (let i = start; i < end; ++i) {
169
+ const v = buffer.readUInt8(i);
170
+
171
+ // tslint:disable-next-line: no-bitwise
172
+ n = n * 128 + (v & 0x7f);
173
+ bits += 7;
174
+
175
+ // noinspection JSBitwiseOperatorUsage
176
+ // tslint:disable-next-line: no-bitwise
177
+ if (!(v & 0x80)) {
178
+ // finished
179
+ if (s === "") {
180
+ const m = n < 80 ? (n < 40 ? 0 : 1) : 2;
181
+ s = m + "." + (n - m * 40);
182
+ } else {
183
+ s += "." + n.toString();
184
+ }
185
+ n = 0;
186
+ bits = 0;
187
+ }
188
+ }
189
+ assert(bits === 0); // if (bits > 0) { s += ".incomplete"; }
190
+ return s;
191
+ }
192
+
193
+ export function _readObjectIdentifier(buffer: Buffer, block: BlockInfo): { oid: string; name: string } {
194
+ assert(block.tag === TagType.OBJECT_IDENTIFIER);
195
+ const b = buffer.slice(block.position, block.position + block.length);
196
+ const oid = parseOID(b, 0, block.length);
197
+ return {
198
+ oid,
199
+ name: oid_map[oid] ? oid_map[oid].d : oid,
200
+ };
201
+ }
202
+
203
+ export function _readAlgorithmIdentifier(buffer: Buffer, block: BlockInfo): AlgorithmIdentifier {
204
+ const inner_blocks = _readStruct(buffer, block);
205
+ return {
206
+ identifier: _readObjectIdentifier(buffer, inner_blocks[0]).name,
207
+ }
208
+ };
209
+
210
+ export function _readECCAlgorithmIdentifier(buffer: Buffer, block: BlockInfo): AlgorithmIdentifier {
211
+ const inner_blocks = _readStruct(buffer, block);
212
+ return {
213
+ identifier: _readObjectIdentifier(buffer, inner_blocks[1]).name, // difference with RSA as algorithm is second element of nested block
214
+ }
215
+ };
216
+
217
+ export type SignatureValue = string;
218
+
219
+ export function _readSignatureValueBin(buffer: Buffer, block: BlockInfo): Buffer {
220
+ return _readBitString(buffer, block).data;
221
+ }
222
+
223
+ export function _readSignatureValue(buffer: Buffer, block: BlockInfo): SignatureValue {
224
+ return _readSignatureValueBin(buffer, block).toString("hex");
225
+ }
226
+
227
+ export function _readLongIntegerValue(buffer: Buffer, block: BlockInfo): Buffer {
228
+ assert(block.tag === TagType.INTEGER, "expecting a INTEGER tag");
229
+ const pos = block.position;
230
+ const nbBytes = block.length;
231
+ const buf = buffer.slice(pos, pos + nbBytes);
232
+ return buf;
233
+ }
234
+
235
+ export function _readIntegerValue(buffer: Buffer, block: BlockInfo): number {
236
+ assert(block.tag === TagType.INTEGER, "expecting a INTEGER tag");
237
+ let pos = block.position;
238
+ const nbBytes = block.length;
239
+ assert(nbBytes < 4);
240
+ let value = 0;
241
+ for (let i = 0; i < nbBytes; i++) {
242
+ value = value * 256 + buffer.readUInt8(pos);
243
+ pos += 1;
244
+ }
245
+ return value;
246
+ }
247
+
248
+ export function _readBooleanValue(buffer: Buffer, block: BlockInfo): boolean {
249
+ assert(block.tag === TagType.BOOLEAN, "expecting a BOOLEAN tag. got " + TagType[block.tag]);
250
+ const pos = block.position;
251
+ const nbBytes = block.length;
252
+ assert(nbBytes < 4);
253
+ const value = buffer.readUInt8(pos) ? true : false;
254
+ return value as boolean;
255
+ }
256
+
257
+ export function _readVersionValue(buffer: Buffer, block: BlockInfo): number {
258
+ block = readTag(buffer, block.position);
259
+ return _readIntegerValue(buffer, block);
260
+ }
261
+
262
+ /*
263
+ 4.1.2.5.2 GeneralizedTime
264
+
265
+ The generalized time type, GeneralizedTime, is a standard ASN.1 type
266
+ for variable precision representation of time. Optionally, the
267
+ GeneralizedTime field can include a representation of the time
268
+ differential between local and Greenwich Mean Time.
269
+
270
+ For the purposes of this profile, GeneralizedTime values MUST be
271
+ expressed Greenwich Mean Time (Zulu) and MUST include seconds (i.e.,
272
+ times are YYYYMMDDHHMMSSZ), even where the number of seconds is zero.
273
+ GeneralizedTime values MUST NOT include fractional seconds.
274
+
275
+ */
276
+ function convertGeneralizedTime(str: string): Date {
277
+ const year = parseInt(str.substr(0, 4), 10);
278
+ const month = parseInt(str.substr(4, 2), 10) - 1;
279
+ const day = parseInt(str.substr(6, 2), 10);
280
+ const hours = parseInt(str.substr(8, 2), 10);
281
+ const mins = parseInt(str.substr(10, 2), 10);
282
+ const secs = parseInt(str.substr(12, 2), 10);
283
+
284
+ return new Date(Date.UTC(year, month, day, hours, mins, secs));
285
+ }
286
+
287
+ function _readBMPString(buffer: Buffer, block: BlockInfo): string {
288
+ const strBuff = _getBlock(buffer, block);
289
+ let str = "";
290
+ for (let i = 0; i < strBuff.length; i += 2) {
291
+ const word = strBuff.readUInt16BE(i);
292
+ str += String.fromCharCode(word);
293
+ }
294
+ return str;
295
+ }
296
+
297
+ /*
298
+ http://tools.ietf.org/html/rfc5280
299
+
300
+ 4.1.2.5. Validity
301
+ [...]
302
+ As conforming to this profile MUST always encode certificate
303
+ validity dates through the year 2049 as UTCTime; certificate validity
304
+ dates in 2050 or later MUST be encoded as GeneralizedTime.
305
+ Conforming applications MUST be able to process validity dates that
306
+ are encoded in either UTCTime or GeneralizedTime.
307
+ [...]
308
+
309
+ 4.1.2.5.1 UTCTime
310
+
311
+ The universal time type, UTCTime, is a standard ASN.1 type intended
312
+ for representation of dates and time. UTCTime specifies the year
313
+ through the two low order digits and time is specified to the
314
+ precision of one minute or one second. UTCTime includes either Z
315
+ (for Zulu, or Greenwich Mean Time) or a time differential.
316
+
317
+ For the purposes of this profile, UTCTime values MUST be expressed
318
+ Greenwich Mean Time (Zulu) and MUST include seconds (i.e., times are
319
+ YYMMDDHHMMSSZ), even where the number of seconds is zero. Conforming
320
+ systems MUST interpret the year field (YY) as follows:
321
+
322
+ Where YY is greater than or equal to 50, the year SHALL be
323
+ interpreted as 19YY; and
324
+
325
+ Where YY is less than 50, the year SHALL be interpreted as 20YY.
326
+ */
327
+ function convertUTCTime(str: string): Date {
328
+ let year = parseInt(str.substr(0, 2), 10);
329
+ const month = parseInt(str.substr(2, 2), 10) - 1;
330
+ const day = parseInt(str.substr(4, 2), 10);
331
+ const hours = parseInt(str.substr(6, 2), 10);
332
+ const mins = parseInt(str.substr(8, 2), 10);
333
+ const secs = parseInt(str.substr(10, 2), 10);
334
+
335
+ year += year >= 50 ? 1900 : 2000;
336
+ return new Date(Date.UTC(year, month, day, hours, mins, secs));
337
+ }
338
+
339
+ export function _readValue(buffer: Buffer, block: BlockInfo): any {
340
+ switch (block.tag) {
341
+ case TagType.BOOLEAN:
342
+ return _readBooleanValue(buffer, block);
343
+ case TagType.BMPString:
344
+ return _readBMPString(buffer, block);
345
+ case TagType.PrintableString:
346
+ case TagType.TeletexString:
347
+ case TagType.UTF8String:
348
+ case TagType.NumericString:
349
+ case TagType.IA5String:
350
+ return _getBlock(buffer, block).toString("ascii");
351
+ case TagType.UTCTime:
352
+ return convertUTCTime(_getBlock(buffer, block).toString("ascii"));
353
+ case TagType.GeneralizedTime:
354
+ return convertGeneralizedTime(_getBlock(buffer, block).toString("ascii"));
355
+ default:
356
+ throw new Error("Invalid tag 0x" + block.tag.toString(16) + "");
357
+ //xx return " ??? <" + block.tag + ">";
358
+ }
359
+ }
360
+
361
+ export interface DirectoryName {
362
+ stateOrProvinceName?: string;
363
+ localityName?: string;
364
+ organizationName?: string;
365
+ organizationUnitName?: string;
366
+ commonName?: string;
367
+ countryName?: string;
368
+ }
369
+ export function compactDirectoryName(d: DirectoryName): string {
370
+ return JSON.stringify(d);
371
+ }
372
+
373
+ export function _readDirectoryName(buffer: Buffer, block: BlockInfo): DirectoryName {
374
+ // AttributeTypeAndValue ::= SEQUENCE {
375
+ // type ATTRIBUTE.&id({SupportedAttributes}),
376
+ // value ATTRIBUTE.&Type({SupportedAttributes}{@type}),
377
+ const set_blocks = _readStruct(buffer, block);
378
+ const names: DirectoryName = {};
379
+ for (const set_block of set_blocks) {
380
+ assert(set_block.tag === 0x31);
381
+ const blocks = _readStruct(buffer, set_block);
382
+ assert(blocks.length === 1);
383
+ assert(blocks[0].tag === 0x30);
384
+
385
+ const sequenceBlock = _readStruct(buffer, blocks[0]);
386
+ assert(sequenceBlock.length === 2);
387
+
388
+ const type = _readObjectIdentifier(buffer, sequenceBlock[0]);
389
+ (names as any)[type.name] = _readValue(buffer, sequenceBlock[1]);
390
+ }
391
+ return names;
392
+ }
393
+
394
+ export function _findBlockAtIndex(blocks: BlockInfo[], index: number): BlockInfo | null {
395
+ const tmp = blocks.filter((b: BlockInfo) => b.tag === 0xa0 + index || b.tag === 0x80 + index);
396
+ if (tmp.length === 0) {
397
+ return null;
398
+ }
399
+ return tmp[0];
400
+ }
401
+
402
+ export function _readTime(buffer: Buffer, block: BlockInfo): any {
403
+ return _readValue(buffer, block);
404
+ }