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