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