@ne1410s/crypto 0.2.329 → 0.2.331

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.
@@ -1,3667 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ne_crypto = {}));
5
- })(this, (function (exports) { 'use strict';
6
-
7
- /******************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
22
-
23
-
24
- function __awaiter(thisArg, _arguments, P, generator) {
25
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
- return new (P || (P = Promise))(function (resolve, reject) {
27
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
30
- step((generator = generator.apply(thisArg, _arguments || [])).next());
31
- });
32
- }
33
-
34
- function __generator(thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
36
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- }
61
-
62
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
63
- var e = new Error(message);
64
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
65
- };
66
-
67
- /*!
68
- * MIT License
69
- *
70
- * Copyright (c) 2017-2024 Peculiar Ventures, LLC
71
- *
72
- * Permission is hereby granted, free of charge, to any person obtaining a copy
73
- * of this software and associated documentation files (the "Software"), to deal
74
- * in the Software without restriction, including without limitation the rights
75
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76
- * copies of the Software, and to permit persons to whom the Software is
77
- * furnished to do so, subject to the following conditions:
78
- *
79
- * The above copyright notice and this permission notice shall be included in all
80
- * copies or substantial portions of the Software.
81
- *
82
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
88
- * SOFTWARE.
89
- *
90
- */
91
-
92
- const ARRAY_BUFFER_NAME = "[object ArrayBuffer]";
93
- class BufferSourceConverter {
94
- static isArrayBuffer(data) {
95
- return Object.prototype.toString.call(data) === ARRAY_BUFFER_NAME;
96
- }
97
- static toArrayBuffer(data) {
98
- if (this.isArrayBuffer(data)) {
99
- return data;
100
- }
101
- if (data.byteLength === data.buffer.byteLength) {
102
- return data.buffer;
103
- }
104
- if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
105
- return data.buffer;
106
- }
107
- return this.toUint8Array(data.buffer)
108
- .slice(data.byteOffset, data.byteOffset + data.byteLength)
109
- .buffer;
110
- }
111
- static toUint8Array(data) {
112
- return this.toView(data, Uint8Array);
113
- }
114
- static toView(data, type) {
115
- if (data.constructor === type) {
116
- return data;
117
- }
118
- if (this.isArrayBuffer(data)) {
119
- return new type(data);
120
- }
121
- if (this.isArrayBufferView(data)) {
122
- return new type(data.buffer, data.byteOffset, data.byteLength);
123
- }
124
- throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
125
- }
126
- static isBufferSource(data) {
127
- return this.isArrayBufferView(data)
128
- || this.isArrayBuffer(data);
129
- }
130
- static isArrayBufferView(data) {
131
- return ArrayBuffer.isView(data)
132
- || (data && this.isArrayBuffer(data.buffer));
133
- }
134
- static isEqual(a, b) {
135
- const aView = BufferSourceConverter.toUint8Array(a);
136
- const bView = BufferSourceConverter.toUint8Array(b);
137
- if (aView.length !== bView.byteLength) {
138
- return false;
139
- }
140
- for (let i = 0; i < aView.length; i++) {
141
- if (aView[i] !== bView[i]) {
142
- return false;
143
- }
144
- }
145
- return true;
146
- }
147
- static concat(...args) {
148
- let buffers;
149
- if (Array.isArray(args[0]) && !(args[1] instanceof Function)) {
150
- buffers = args[0];
151
- }
152
- else if (Array.isArray(args[0]) && args[1] instanceof Function) {
153
- buffers = args[0];
154
- }
155
- else {
156
- if (args[args.length - 1] instanceof Function) {
157
- buffers = args.slice(0, args.length - 1);
158
- }
159
- else {
160
- buffers = args;
161
- }
162
- }
163
- let size = 0;
164
- for (const buffer of buffers) {
165
- size += buffer.byteLength;
166
- }
167
- const res = new Uint8Array(size);
168
- let offset = 0;
169
- for (const buffer of buffers) {
170
- const view = this.toUint8Array(buffer);
171
- res.set(view, offset);
172
- offset += view.length;
173
- }
174
- if (args[args.length - 1] instanceof Function) {
175
- return this.toView(res, args[args.length - 1]);
176
- }
177
- return res.buffer;
178
- }
179
- }
180
-
181
- const STRING_TYPE = "string";
182
- const HEX_REGEX = /^[0-9a-f\s]+$/i;
183
- const BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
184
- const BASE64URL_REGEX = /^[a-zA-Z0-9-_]+$/;
185
- class Utf8Converter {
186
- static fromString(text) {
187
- const s = unescape(encodeURIComponent(text));
188
- const uintArray = new Uint8Array(s.length);
189
- for (let i = 0; i < s.length; i++) {
190
- uintArray[i] = s.charCodeAt(i);
191
- }
192
- return uintArray.buffer;
193
- }
194
- static toString(buffer) {
195
- const buf = BufferSourceConverter.toUint8Array(buffer);
196
- let encodedString = "";
197
- for (let i = 0; i < buf.length; i++) {
198
- encodedString += String.fromCharCode(buf[i]);
199
- }
200
- const decodedString = decodeURIComponent(escape(encodedString));
201
- return decodedString;
202
- }
203
- }
204
- class Utf16Converter {
205
- static toString(buffer, littleEndian = false) {
206
- const arrayBuffer = BufferSourceConverter.toArrayBuffer(buffer);
207
- const dataView = new DataView(arrayBuffer);
208
- let res = "";
209
- for (let i = 0; i < arrayBuffer.byteLength; i += 2) {
210
- const code = dataView.getUint16(i, littleEndian);
211
- res += String.fromCharCode(code);
212
- }
213
- return res;
214
- }
215
- static fromString(text, littleEndian = false) {
216
- const res = new ArrayBuffer(text.length * 2);
217
- const dataView = new DataView(res);
218
- for (let i = 0; i < text.length; i++) {
219
- dataView.setUint16(i * 2, text.charCodeAt(i), littleEndian);
220
- }
221
- return res;
222
- }
223
- }
224
- class Convert {
225
- static isHex(data) {
226
- return typeof data === STRING_TYPE
227
- && HEX_REGEX.test(data);
228
- }
229
- static isBase64(data) {
230
- return typeof data === STRING_TYPE
231
- && BASE64_REGEX.test(data);
232
- }
233
- static isBase64Url(data) {
234
- return typeof data === STRING_TYPE
235
- && BASE64URL_REGEX.test(data);
236
- }
237
- static ToString(buffer, enc = "utf8") {
238
- const buf = BufferSourceConverter.toUint8Array(buffer);
239
- switch (enc.toLowerCase()) {
240
- case "utf8":
241
- return this.ToUtf8String(buf);
242
- case "binary":
243
- return this.ToBinary(buf);
244
- case "hex":
245
- return this.ToHex(buf);
246
- case "base64":
247
- return this.ToBase64(buf);
248
- case "base64url":
249
- return this.ToBase64Url(buf);
250
- case "utf16le":
251
- return Utf16Converter.toString(buf, true);
252
- case "utf16":
253
- case "utf16be":
254
- return Utf16Converter.toString(buf);
255
- default:
256
- throw new Error(`Unknown type of encoding '${enc}'`);
257
- }
258
- }
259
- static FromString(str, enc = "utf8") {
260
- if (!str) {
261
- return new ArrayBuffer(0);
262
- }
263
- switch (enc.toLowerCase()) {
264
- case "utf8":
265
- return this.FromUtf8String(str);
266
- case "binary":
267
- return this.FromBinary(str);
268
- case "hex":
269
- return this.FromHex(str);
270
- case "base64":
271
- return this.FromBase64(str);
272
- case "base64url":
273
- return this.FromBase64Url(str);
274
- case "utf16le":
275
- return Utf16Converter.fromString(str, true);
276
- case "utf16":
277
- case "utf16be":
278
- return Utf16Converter.fromString(str);
279
- default:
280
- throw new Error(`Unknown type of encoding '${enc}'`);
281
- }
282
- }
283
- static ToBase64(buffer) {
284
- const buf = BufferSourceConverter.toUint8Array(buffer);
285
- if (typeof btoa !== "undefined") {
286
- const binary = this.ToString(buf, "binary");
287
- return btoa(binary);
288
- }
289
- else {
290
- return Buffer.from(buf).toString("base64");
291
- }
292
- }
293
- static FromBase64(base64) {
294
- const formatted = this.formatString(base64);
295
- if (!formatted) {
296
- return new ArrayBuffer(0);
297
- }
298
- if (!Convert.isBase64(formatted)) {
299
- throw new TypeError("Argument 'base64Text' is not Base64 encoded");
300
- }
301
- if (typeof atob !== "undefined") {
302
- return this.FromBinary(atob(formatted));
303
- }
304
- else {
305
- return new Uint8Array(Buffer.from(formatted, "base64")).buffer;
306
- }
307
- }
308
- static FromBase64Url(base64url) {
309
- const formatted = this.formatString(base64url);
310
- if (!formatted) {
311
- return new ArrayBuffer(0);
312
- }
313
- if (!Convert.isBase64Url(formatted)) {
314
- throw new TypeError("Argument 'base64url' is not Base64Url encoded");
315
- }
316
- return this.FromBase64(this.Base64Padding(formatted.replace(/\-/g, "+").replace(/\_/g, "/")));
317
- }
318
- static ToBase64Url(data) {
319
- return this.ToBase64(data).replace(/\+/g, "-").replace(/\//g, "_").replace(/\=/g, "");
320
- }
321
- static FromUtf8String(text, encoding = Convert.DEFAULT_UTF8_ENCODING) {
322
- switch (encoding) {
323
- case "ascii":
324
- return this.FromBinary(text);
325
- case "utf8":
326
- return Utf8Converter.fromString(text);
327
- case "utf16":
328
- case "utf16be":
329
- return Utf16Converter.fromString(text);
330
- case "utf16le":
331
- case "usc2":
332
- return Utf16Converter.fromString(text, true);
333
- default:
334
- throw new Error(`Unknown type of encoding '${encoding}'`);
335
- }
336
- }
337
- static ToUtf8String(buffer, encoding = Convert.DEFAULT_UTF8_ENCODING) {
338
- switch (encoding) {
339
- case "ascii":
340
- return this.ToBinary(buffer);
341
- case "utf8":
342
- return Utf8Converter.toString(buffer);
343
- case "utf16":
344
- case "utf16be":
345
- return Utf16Converter.toString(buffer);
346
- case "utf16le":
347
- case "usc2":
348
- return Utf16Converter.toString(buffer, true);
349
- default:
350
- throw new Error(`Unknown type of encoding '${encoding}'`);
351
- }
352
- }
353
- static FromBinary(text) {
354
- const stringLength = text.length;
355
- const resultView = new Uint8Array(stringLength);
356
- for (let i = 0; i < stringLength; i++) {
357
- resultView[i] = text.charCodeAt(i);
358
- }
359
- return resultView.buffer;
360
- }
361
- static ToBinary(buffer) {
362
- const buf = BufferSourceConverter.toUint8Array(buffer);
363
- let res = "";
364
- for (let i = 0; i < buf.length; i++) {
365
- res += String.fromCharCode(buf[i]);
366
- }
367
- return res;
368
- }
369
- static ToHex(buffer) {
370
- const buf = BufferSourceConverter.toUint8Array(buffer);
371
- let result = "";
372
- const len = buf.length;
373
- for (let i = 0; i < len; i++) {
374
- const byte = buf[i];
375
- if (byte < 16) {
376
- result += "0";
377
- }
378
- result += byte.toString(16);
379
- }
380
- return result;
381
- }
382
- static FromHex(hexString) {
383
- let formatted = this.formatString(hexString);
384
- if (!formatted) {
385
- return new ArrayBuffer(0);
386
- }
387
- if (!Convert.isHex(formatted)) {
388
- throw new TypeError("Argument 'hexString' is not HEX encoded");
389
- }
390
- if (formatted.length % 2) {
391
- formatted = `0${formatted}`;
392
- }
393
- const res = new Uint8Array(formatted.length / 2);
394
- for (let i = 0; i < formatted.length; i = i + 2) {
395
- const c = formatted.slice(i, i + 2);
396
- res[i / 2] = parseInt(c, 16);
397
- }
398
- return res.buffer;
399
- }
400
- static ToUtf16String(buffer, littleEndian = false) {
401
- return Utf16Converter.toString(buffer, littleEndian);
402
- }
403
- static FromUtf16String(text, littleEndian = false) {
404
- return Utf16Converter.fromString(text, littleEndian);
405
- }
406
- static Base64Padding(base64) {
407
- const padCount = 4 - (base64.length % 4);
408
- if (padCount < 4) {
409
- for (let i = 0; i < padCount; i++) {
410
- base64 += "=";
411
- }
412
- }
413
- return base64;
414
- }
415
- static formatString(data) {
416
- return (data === null || data === void 0 ? void 0 : data.replace(/[\n\r\t ]/g, "")) || "";
417
- }
418
- }
419
- Convert.DEFAULT_UTF8_ENCODING = "utf8";
420
-
421
- /*!
422
- Copyright (c) Peculiar Ventures, LLC
423
- */
424
-
425
- function utilFromBase(inputBuffer, inputBase) {
426
- let result = 0;
427
- if (inputBuffer.length === 1) {
428
- return inputBuffer[0];
429
- }
430
- for (let i = (inputBuffer.length - 1); i >= 0; i--) {
431
- result += inputBuffer[(inputBuffer.length - 1) - i] * Math.pow(2, inputBase * i);
432
- }
433
- return result;
434
- }
435
- function utilToBase(value, base, reserved = (-1)) {
436
- const internalReserved = reserved;
437
- let internalValue = value;
438
- let result = 0;
439
- let biggest = Math.pow(2, base);
440
- for (let i = 1; i < 8; i++) {
441
- if (value < biggest) {
442
- let retBuf;
443
- if (internalReserved < 0) {
444
- retBuf = new ArrayBuffer(i);
445
- result = i;
446
- }
447
- else {
448
- if (internalReserved < i) {
449
- return (new ArrayBuffer(0));
450
- }
451
- retBuf = new ArrayBuffer(internalReserved);
452
- result = internalReserved;
453
- }
454
- const retView = new Uint8Array(retBuf);
455
- for (let j = (i - 1); j >= 0; j--) {
456
- const basis = Math.pow(2, j * base);
457
- retView[result - j - 1] = Math.floor(internalValue / basis);
458
- internalValue -= (retView[result - j - 1]) * basis;
459
- }
460
- return retBuf;
461
- }
462
- biggest *= Math.pow(2, base);
463
- }
464
- return new ArrayBuffer(0);
465
- }
466
- function utilConcatView(...views) {
467
- let outputLength = 0;
468
- let prevLength = 0;
469
- for (const view of views) {
470
- outputLength += view.length;
471
- }
472
- const retBuf = new ArrayBuffer(outputLength);
473
- const retView = new Uint8Array(retBuf);
474
- for (const view of views) {
475
- retView.set(view, prevLength);
476
- prevLength += view.length;
477
- }
478
- return retView;
479
- }
480
- function utilDecodeTC() {
481
- const buf = new Uint8Array(this.valueHex);
482
- if (this.valueHex.byteLength >= 2) {
483
- const condition1 = (buf[0] === 0xFF) && (buf[1] & 0x80);
484
- const condition2 = (buf[0] === 0x00) && ((buf[1] & 0x80) === 0x00);
485
- if (condition1 || condition2) {
486
- this.warnings.push("Needlessly long format");
487
- }
488
- }
489
- const bigIntBuffer = new ArrayBuffer(this.valueHex.byteLength);
490
- const bigIntView = new Uint8Array(bigIntBuffer);
491
- for (let i = 0; i < this.valueHex.byteLength; i++) {
492
- bigIntView[i] = 0;
493
- }
494
- bigIntView[0] = (buf[0] & 0x80);
495
- const bigInt = utilFromBase(bigIntView, 8);
496
- const smallIntBuffer = new ArrayBuffer(this.valueHex.byteLength);
497
- const smallIntView = new Uint8Array(smallIntBuffer);
498
- for (let j = 0; j < this.valueHex.byteLength; j++) {
499
- smallIntView[j] = buf[j];
500
- }
501
- smallIntView[0] &= 0x7F;
502
- const smallInt = utilFromBase(smallIntView, 8);
503
- return (smallInt - bigInt);
504
- }
505
- function utilEncodeTC(value) {
506
- const modValue = (value < 0) ? (value * (-1)) : value;
507
- let bigInt = 128;
508
- for (let i = 1; i < 8; i++) {
509
- if (modValue <= bigInt) {
510
- if (value < 0) {
511
- const smallInt = bigInt - modValue;
512
- const retBuf = utilToBase(smallInt, 8, i);
513
- const retView = new Uint8Array(retBuf);
514
- retView[0] |= 0x80;
515
- return retBuf;
516
- }
517
- let retBuf = utilToBase(modValue, 8, i);
518
- let retView = new Uint8Array(retBuf);
519
- if (retView[0] & 0x80) {
520
- const tempBuf = retBuf.slice(0);
521
- const tempView = new Uint8Array(tempBuf);
522
- retBuf = new ArrayBuffer(retBuf.byteLength + 1);
523
- retView = new Uint8Array(retBuf);
524
- for (let k = 0; k < tempBuf.byteLength; k++) {
525
- retView[k + 1] = tempView[k];
526
- }
527
- retView[0] = 0x00;
528
- }
529
- return retBuf;
530
- }
531
- bigInt *= Math.pow(2, 8);
532
- }
533
- return (new ArrayBuffer(0));
534
- }
535
- function isEqualBuffer(inputBuffer1, inputBuffer2) {
536
- if (inputBuffer1.byteLength !== inputBuffer2.byteLength) {
537
- return false;
538
- }
539
- const view1 = new Uint8Array(inputBuffer1);
540
- const view2 = new Uint8Array(inputBuffer2);
541
- for (let i = 0; i < view1.length; i++) {
542
- if (view1[i] !== view2[i]) {
543
- return false;
544
- }
545
- }
546
- return true;
547
- }
548
- function padNumber(inputNumber, fullLength) {
549
- const str = inputNumber.toString(10);
550
- if (fullLength < str.length) {
551
- return "";
552
- }
553
- const dif = fullLength - str.length;
554
- const padding = new Array(dif);
555
- for (let i = 0; i < dif; i++) {
556
- padding[i] = "0";
557
- }
558
- const paddingString = padding.join("");
559
- return paddingString.concat(str);
560
- }
561
-
562
- /*!
563
- * Copyright (c) 2014, GMO GlobalSign
564
- * Copyright (c) 2015-2022, Peculiar Ventures
565
- * All rights reserved.
566
- *
567
- * Author 2014-2019, Yury Strozhevsky
568
- *
569
- * Redistribution and use in source and binary forms, with or without modification,
570
- * are permitted provided that the following conditions are met:
571
- *
572
- * * Redistributions of source code must retain the above copyright notice, this
573
- * list of conditions and the following disclaimer.
574
- *
575
- * * Redistributions in binary form must reproduce the above copyright notice, this
576
- * list of conditions and the following disclaimer in the documentation and/or
577
- * other materials provided with the distribution.
578
- *
579
- * * Neither the name of the copyright holder nor the names of its
580
- * contributors may be used to endorse or promote products derived from
581
- * this software without specific prior written permission.
582
- *
583
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
584
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
585
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
586
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
587
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
588
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
589
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
590
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
591
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
592
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
593
- *
594
- */
595
-
596
-
597
- function assertBigInt() {
598
- if (typeof BigInt === "undefined") {
599
- throw new Error("BigInt is not defined. Your environment doesn't implement BigInt.");
600
- }
601
- }
602
- function concat(buffers) {
603
- let outputLength = 0;
604
- let prevLength = 0;
605
- for (let i = 0; i < buffers.length; i++) {
606
- const buffer = buffers[i];
607
- outputLength += buffer.byteLength;
608
- }
609
- const retView = new Uint8Array(outputLength);
610
- for (let i = 0; i < buffers.length; i++) {
611
- const buffer = buffers[i];
612
- retView.set(new Uint8Array(buffer), prevLength);
613
- prevLength += buffer.byteLength;
614
- }
615
- return retView.buffer;
616
- }
617
- function checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength) {
618
- if (!(inputBuffer instanceof Uint8Array)) {
619
- baseBlock.error = "Wrong parameter: inputBuffer must be 'Uint8Array'";
620
- return false;
621
- }
622
- if (!inputBuffer.byteLength) {
623
- baseBlock.error = "Wrong parameter: inputBuffer has zero length";
624
- return false;
625
- }
626
- if (inputOffset < 0) {
627
- baseBlock.error = "Wrong parameter: inputOffset less than zero";
628
- return false;
629
- }
630
- if (inputLength < 0) {
631
- baseBlock.error = "Wrong parameter: inputLength less than zero";
632
- return false;
633
- }
634
- if ((inputBuffer.byteLength - inputOffset - inputLength) < 0) {
635
- baseBlock.error = "End of input reached before message was fully decoded (inconsistent offset and length values)";
636
- return false;
637
- }
638
- return true;
639
- }
640
-
641
- class ViewWriter {
642
- constructor() {
643
- this.items = [];
644
- }
645
- write(buf) {
646
- this.items.push(buf);
647
- }
648
- final() {
649
- return concat(this.items);
650
- }
651
- }
652
-
653
- const powers2 = [new Uint8Array([1])];
654
- const digitsString = "0123456789";
655
- const EMPTY_STRING = "";
656
- const EMPTY_BUFFER = new ArrayBuffer(0);
657
- const EMPTY_VIEW = new Uint8Array(0);
658
- const END_OF_CONTENT_NAME = "EndOfContent";
659
- const OCTET_STRING_NAME = "OCTET STRING";
660
- const BIT_STRING_NAME = "BIT STRING";
661
-
662
- function HexBlock(BaseClass) {
663
- var _a;
664
- return _a = class Some extends BaseClass {
665
- get valueHex() {
666
- return this.valueHexView.slice().buffer;
667
- }
668
- set valueHex(value) {
669
- this.valueHexView = new Uint8Array(value);
670
- }
671
- constructor(...args) {
672
- var _b;
673
- super(...args);
674
- const params = args[0] || {};
675
- this.isHexOnly = (_b = params.isHexOnly) !== null && _b !== void 0 ? _b : false;
676
- this.valueHexView = params.valueHex ? BufferSourceConverter.toUint8Array(params.valueHex) : EMPTY_VIEW;
677
- }
678
- fromBER(inputBuffer, inputOffset, inputLength) {
679
- const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer;
680
- if (!checkBufferParams(this, view, inputOffset, inputLength)) {
681
- return -1;
682
- }
683
- const endLength = inputOffset + inputLength;
684
- this.valueHexView = view.subarray(inputOffset, endLength);
685
- if (!this.valueHexView.length) {
686
- this.warnings.push("Zero buffer length");
687
- return inputOffset;
688
- }
689
- this.blockLength = inputLength;
690
- return endLength;
691
- }
692
- toBER(sizeOnly = false) {
693
- if (!this.isHexOnly) {
694
- this.error = "Flag 'isHexOnly' is not set, abort";
695
- return EMPTY_BUFFER;
696
- }
697
- if (sizeOnly) {
698
- return new ArrayBuffer(this.valueHexView.byteLength);
699
- }
700
- return (this.valueHexView.byteLength === this.valueHexView.buffer.byteLength)
701
- ? this.valueHexView.buffer
702
- : this.valueHexView.slice().buffer;
703
- }
704
- toJSON() {
705
- return {
706
- ...super.toJSON(),
707
- isHexOnly: this.isHexOnly,
708
- valueHex: Convert.ToHex(this.valueHexView),
709
- };
710
- }
711
- },
712
- _a.NAME = "hexBlock",
713
- _a;
714
- }
715
-
716
- class LocalBaseBlock {
717
- static blockName() {
718
- return this.NAME;
719
- }
720
- get valueBeforeDecode() {
721
- return this.valueBeforeDecodeView.slice().buffer;
722
- }
723
- set valueBeforeDecode(value) {
724
- this.valueBeforeDecodeView = new Uint8Array(value);
725
- }
726
- constructor({ blockLength = 0, error = EMPTY_STRING, warnings = [], valueBeforeDecode = EMPTY_VIEW, } = {}) {
727
- this.blockLength = blockLength;
728
- this.error = error;
729
- this.warnings = warnings;
730
- this.valueBeforeDecodeView = BufferSourceConverter.toUint8Array(valueBeforeDecode);
731
- }
732
- toJSON() {
733
- return {
734
- blockName: this.constructor.NAME,
735
- blockLength: this.blockLength,
736
- error: this.error,
737
- warnings: this.warnings,
738
- valueBeforeDecode: Convert.ToHex(this.valueBeforeDecodeView),
739
- };
740
- }
741
- }
742
- LocalBaseBlock.NAME = "baseBlock";
743
-
744
- class ValueBlock extends LocalBaseBlock {
745
- fromBER(_inputBuffer, _inputOffset, _inputLength) {
746
- throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
747
- }
748
- toBER(_sizeOnly, _writer) {
749
- throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
750
- }
751
- }
752
- ValueBlock.NAME = "valueBlock";
753
-
754
- class LocalIdentificationBlock extends HexBlock(LocalBaseBlock) {
755
- constructor({ idBlock = {} } = {}) {
756
- var _a, _b, _c, _d;
757
- super();
758
- if (idBlock) {
759
- this.isHexOnly = (_a = idBlock.isHexOnly) !== null && _a !== void 0 ? _a : false;
760
- this.valueHexView = idBlock.valueHex
761
- ? BufferSourceConverter.toUint8Array(idBlock.valueHex)
762
- : EMPTY_VIEW;
763
- this.tagClass = (_b = idBlock.tagClass) !== null && _b !== void 0 ? _b : -1;
764
- this.tagNumber = (_c = idBlock.tagNumber) !== null && _c !== void 0 ? _c : -1;
765
- this.isConstructed = (_d = idBlock.isConstructed) !== null && _d !== void 0 ? _d : false;
766
- }
767
- else {
768
- this.tagClass = -1;
769
- this.tagNumber = -1;
770
- this.isConstructed = false;
771
- }
772
- }
773
- toBER(sizeOnly = false) {
774
- let firstOctet = 0;
775
- switch (this.tagClass) {
776
- case 1:
777
- firstOctet |= 0x00;
778
- break;
779
- case 2:
780
- firstOctet |= 0x40;
781
- break;
782
- case 3:
783
- firstOctet |= 0x80;
784
- break;
785
- case 4:
786
- firstOctet |= 0xC0;
787
- break;
788
- default:
789
- this.error = "Unknown tag class";
790
- return EMPTY_BUFFER;
791
- }
792
- if (this.isConstructed)
793
- firstOctet |= 0x20;
794
- if (this.tagNumber < 31 && !this.isHexOnly) {
795
- const retView = new Uint8Array(1);
796
- if (!sizeOnly) {
797
- let number = this.tagNumber;
798
- number &= 0x1F;
799
- firstOctet |= number;
800
- retView[0] = firstOctet;
801
- }
802
- return retView.buffer;
803
- }
804
- if (!this.isHexOnly) {
805
- const encodedBuf = utilToBase(this.tagNumber, 7);
806
- const encodedView = new Uint8Array(encodedBuf);
807
- const size = encodedBuf.byteLength;
808
- const retView = new Uint8Array(size + 1);
809
- retView[0] = (firstOctet | 0x1F);
810
- if (!sizeOnly) {
811
- for (let i = 0; i < (size - 1); i++)
812
- retView[i + 1] = encodedView[i] | 0x80;
813
- retView[size] = encodedView[size - 1];
814
- }
815
- return retView.buffer;
816
- }
817
- const retView = new Uint8Array(this.valueHexView.byteLength + 1);
818
- retView[0] = (firstOctet | 0x1F);
819
- if (!sizeOnly) {
820
- const curView = this.valueHexView;
821
- for (let i = 0; i < (curView.length - 1); i++)
822
- retView[i + 1] = curView[i] | 0x80;
823
- retView[this.valueHexView.byteLength] = curView[curView.length - 1];
824
- }
825
- return retView.buffer;
826
- }
827
- fromBER(inputBuffer, inputOffset, inputLength) {
828
- const inputView = BufferSourceConverter.toUint8Array(inputBuffer);
829
- if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
830
- return -1;
831
- }
832
- const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
833
- if (intBuffer.length === 0) {
834
- this.error = "Zero buffer length";
835
- return -1;
836
- }
837
- const tagClassMask = intBuffer[0] & 0xC0;
838
- switch (tagClassMask) {
839
- case 0x00:
840
- this.tagClass = (1);
841
- break;
842
- case 0x40:
843
- this.tagClass = (2);
844
- break;
845
- case 0x80:
846
- this.tagClass = (3);
847
- break;
848
- case 0xC0:
849
- this.tagClass = (4);
850
- break;
851
- default:
852
- this.error = "Unknown tag class";
853
- return -1;
854
- }
855
- this.isConstructed = (intBuffer[0] & 0x20) === 0x20;
856
- this.isHexOnly = false;
857
- const tagNumberMask = intBuffer[0] & 0x1F;
858
- if (tagNumberMask !== 0x1F) {
859
- this.tagNumber = (tagNumberMask);
860
- this.blockLength = 1;
861
- }
862
- else {
863
- let count = 1;
864
- let intTagNumberBuffer = this.valueHexView = new Uint8Array(255);
865
- let tagNumberBufferMaxLength = 255;
866
- while (intBuffer[count] & 0x80) {
867
- intTagNumberBuffer[count - 1] = intBuffer[count] & 0x7F;
868
- count++;
869
- if (count >= intBuffer.length) {
870
- this.error = "End of input reached before message was fully decoded";
871
- return -1;
872
- }
873
- if (count === tagNumberBufferMaxLength) {
874
- tagNumberBufferMaxLength += 255;
875
- const tempBufferView = new Uint8Array(tagNumberBufferMaxLength);
876
- for (let i = 0; i < intTagNumberBuffer.length; i++)
877
- tempBufferView[i] = intTagNumberBuffer[i];
878
- intTagNumberBuffer = this.valueHexView = new Uint8Array(tagNumberBufferMaxLength);
879
- }
880
- }
881
- this.blockLength = (count + 1);
882
- intTagNumberBuffer[count - 1] = intBuffer[count] & 0x7F;
883
- const tempBufferView = new Uint8Array(count);
884
- for (let i = 0; i < count; i++)
885
- tempBufferView[i] = intTagNumberBuffer[i];
886
- intTagNumberBuffer = this.valueHexView = new Uint8Array(count);
887
- intTagNumberBuffer.set(tempBufferView);
888
- if (this.blockLength <= 9)
889
- this.tagNumber = utilFromBase(intTagNumberBuffer, 7);
890
- else {
891
- this.isHexOnly = true;
892
- this.warnings.push("Tag too long, represented as hex-coded");
893
- }
894
- }
895
- if (((this.tagClass === 1))
896
- && (this.isConstructed)) {
897
- switch (this.tagNumber) {
898
- case 1:
899
- case 2:
900
- case 5:
901
- case 6:
902
- case 9:
903
- case 13:
904
- case 14:
905
- case 23:
906
- case 24:
907
- case 31:
908
- case 32:
909
- case 33:
910
- case 34:
911
- this.error = "Constructed encoding used for primitive type";
912
- return -1;
913
- }
914
- }
915
- return (inputOffset + this.blockLength);
916
- }
917
- toJSON() {
918
- return {
919
- ...super.toJSON(),
920
- tagClass: this.tagClass,
921
- tagNumber: this.tagNumber,
922
- isConstructed: this.isConstructed,
923
- };
924
- }
925
- }
926
- LocalIdentificationBlock.NAME = "identificationBlock";
927
-
928
- class LocalLengthBlock extends LocalBaseBlock {
929
- constructor({ lenBlock = {} } = {}) {
930
- var _a, _b, _c;
931
- super();
932
- this.isIndefiniteForm = (_a = lenBlock.isIndefiniteForm) !== null && _a !== void 0 ? _a : false;
933
- this.longFormUsed = (_b = lenBlock.longFormUsed) !== null && _b !== void 0 ? _b : false;
934
- this.length = (_c = lenBlock.length) !== null && _c !== void 0 ? _c : 0;
935
- }
936
- fromBER(inputBuffer, inputOffset, inputLength) {
937
- const view = BufferSourceConverter.toUint8Array(inputBuffer);
938
- if (!checkBufferParams(this, view, inputOffset, inputLength)) {
939
- return -1;
940
- }
941
- const intBuffer = view.subarray(inputOffset, inputOffset + inputLength);
942
- if (intBuffer.length === 0) {
943
- this.error = "Zero buffer length";
944
- return -1;
945
- }
946
- if (intBuffer[0] === 0xFF) {
947
- this.error = "Length block 0xFF is reserved by standard";
948
- return -1;
949
- }
950
- this.isIndefiniteForm = intBuffer[0] === 0x80;
951
- if (this.isIndefiniteForm) {
952
- this.blockLength = 1;
953
- return (inputOffset + this.blockLength);
954
- }
955
- this.longFormUsed = !!(intBuffer[0] & 0x80);
956
- if (this.longFormUsed === false) {
957
- this.length = (intBuffer[0]);
958
- this.blockLength = 1;
959
- return (inputOffset + this.blockLength);
960
- }
961
- const count = intBuffer[0] & 0x7F;
962
- if (count > 8) {
963
- this.error = "Too big integer";
964
- return -1;
965
- }
966
- if ((count + 1) > intBuffer.length) {
967
- this.error = "End of input reached before message was fully decoded";
968
- return -1;
969
- }
970
- const lenOffset = inputOffset + 1;
971
- const lengthBufferView = view.subarray(lenOffset, lenOffset + count);
972
- if (lengthBufferView[count - 1] === 0x00)
973
- this.warnings.push("Needlessly long encoded length");
974
- this.length = utilFromBase(lengthBufferView, 8);
975
- if (this.longFormUsed && (this.length <= 127))
976
- this.warnings.push("Unnecessary usage of long length form");
977
- this.blockLength = count + 1;
978
- return (inputOffset + this.blockLength);
979
- }
980
- toBER(sizeOnly = false) {
981
- let retBuf;
982
- let retView;
983
- if (this.length > 127)
984
- this.longFormUsed = true;
985
- if (this.isIndefiniteForm) {
986
- retBuf = new ArrayBuffer(1);
987
- if (sizeOnly === false) {
988
- retView = new Uint8Array(retBuf);
989
- retView[0] = 0x80;
990
- }
991
- return retBuf;
992
- }
993
- if (this.longFormUsed) {
994
- const encodedBuf = utilToBase(this.length, 8);
995
- if (encodedBuf.byteLength > 127) {
996
- this.error = "Too big length";
997
- return (EMPTY_BUFFER);
998
- }
999
- retBuf = new ArrayBuffer(encodedBuf.byteLength + 1);
1000
- if (sizeOnly)
1001
- return retBuf;
1002
- const encodedView = new Uint8Array(encodedBuf);
1003
- retView = new Uint8Array(retBuf);
1004
- retView[0] = encodedBuf.byteLength | 0x80;
1005
- for (let i = 0; i < encodedBuf.byteLength; i++)
1006
- retView[i + 1] = encodedView[i];
1007
- return retBuf;
1008
- }
1009
- retBuf = new ArrayBuffer(1);
1010
- if (sizeOnly === false) {
1011
- retView = new Uint8Array(retBuf);
1012
- retView[0] = this.length;
1013
- }
1014
- return retBuf;
1015
- }
1016
- toJSON() {
1017
- return {
1018
- ...super.toJSON(),
1019
- isIndefiniteForm: this.isIndefiniteForm,
1020
- longFormUsed: this.longFormUsed,
1021
- length: this.length,
1022
- };
1023
- }
1024
- }
1025
- LocalLengthBlock.NAME = "lengthBlock";
1026
-
1027
- const typeStore = {};
1028
-
1029
- class BaseBlock extends LocalBaseBlock {
1030
- constructor({ name = EMPTY_STRING, optional = false, primitiveSchema, ...parameters } = {}, valueBlockType) {
1031
- super(parameters);
1032
- this.name = name;
1033
- this.optional = optional;
1034
- if (primitiveSchema) {
1035
- this.primitiveSchema = primitiveSchema;
1036
- }
1037
- this.idBlock = new LocalIdentificationBlock(parameters);
1038
- this.lenBlock = new LocalLengthBlock(parameters);
1039
- this.valueBlock = valueBlockType ? new valueBlockType(parameters) : new ValueBlock(parameters);
1040
- }
1041
- fromBER(inputBuffer, inputOffset, inputLength) {
1042
- const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm)
1043
- ? inputLength
1044
- : this.lenBlock.length);
1045
- if (resultOffset === -1) {
1046
- this.error = this.valueBlock.error;
1047
- return resultOffset;
1048
- }
1049
- if (!this.idBlock.error.length)
1050
- this.blockLength += this.idBlock.blockLength;
1051
- if (!this.lenBlock.error.length)
1052
- this.blockLength += this.lenBlock.blockLength;
1053
- if (!this.valueBlock.error.length)
1054
- this.blockLength += this.valueBlock.blockLength;
1055
- return resultOffset;
1056
- }
1057
- toBER(sizeOnly, writer) {
1058
- const _writer = writer || new ViewWriter();
1059
- if (!writer) {
1060
- prepareIndefiniteForm(this);
1061
- }
1062
- const idBlockBuf = this.idBlock.toBER(sizeOnly);
1063
- _writer.write(idBlockBuf);
1064
- if (this.lenBlock.isIndefiniteForm) {
1065
- _writer.write(new Uint8Array([0x80]).buffer);
1066
- this.valueBlock.toBER(sizeOnly, _writer);
1067
- _writer.write(new ArrayBuffer(2));
1068
- }
1069
- else {
1070
- const valueBlockBuf = this.valueBlock.toBER(sizeOnly);
1071
- this.lenBlock.length = valueBlockBuf.byteLength;
1072
- const lenBlockBuf = this.lenBlock.toBER(sizeOnly);
1073
- _writer.write(lenBlockBuf);
1074
- _writer.write(valueBlockBuf);
1075
- }
1076
- if (!writer) {
1077
- return _writer.final();
1078
- }
1079
- return EMPTY_BUFFER;
1080
- }
1081
- toJSON() {
1082
- const object = {
1083
- ...super.toJSON(),
1084
- idBlock: this.idBlock.toJSON(),
1085
- lenBlock: this.lenBlock.toJSON(),
1086
- valueBlock: this.valueBlock.toJSON(),
1087
- name: this.name,
1088
- optional: this.optional,
1089
- };
1090
- if (this.primitiveSchema)
1091
- object.primitiveSchema = this.primitiveSchema.toJSON();
1092
- return object;
1093
- }
1094
- toString(encoding = "ascii") {
1095
- if (encoding === "ascii") {
1096
- return this.onAsciiEncoding();
1097
- }
1098
- return Convert.ToHex(this.toBER());
1099
- }
1100
- onAsciiEncoding() {
1101
- const name = this.constructor.NAME;
1102
- const value = Convert.ToHex(this.valueBlock.valueBeforeDecodeView);
1103
- return `${name} : ${value}`;
1104
- }
1105
- isEqual(other) {
1106
- if (this === other) {
1107
- return true;
1108
- }
1109
- if (!(other instanceof this.constructor)) {
1110
- return false;
1111
- }
1112
- const thisRaw = this.toBER();
1113
- const otherRaw = other.toBER();
1114
- return isEqualBuffer(thisRaw, otherRaw);
1115
- }
1116
- }
1117
- BaseBlock.NAME = "BaseBlock";
1118
- function prepareIndefiniteForm(baseBlock) {
1119
- var _a;
1120
- if (baseBlock instanceof typeStore.Constructed) {
1121
- for (const value of baseBlock.valueBlock.value) {
1122
- if (prepareIndefiniteForm(value)) {
1123
- baseBlock.lenBlock.isIndefiniteForm = true;
1124
- }
1125
- }
1126
- }
1127
- return !!((_a = baseBlock.lenBlock) === null || _a === void 0 ? void 0 : _a.isIndefiniteForm);
1128
- }
1129
-
1130
- class BaseStringBlock extends BaseBlock {
1131
- getValue() {
1132
- return this.valueBlock.value;
1133
- }
1134
- setValue(value) {
1135
- this.valueBlock.value = value;
1136
- }
1137
- constructor({ value = EMPTY_STRING, ...parameters } = {}, stringValueBlockType) {
1138
- super(parameters, stringValueBlockType);
1139
- if (value) {
1140
- this.fromString(value);
1141
- }
1142
- }
1143
- fromBER(inputBuffer, inputOffset, inputLength) {
1144
- const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm)
1145
- ? inputLength
1146
- : this.lenBlock.length);
1147
- if (resultOffset === -1) {
1148
- this.error = this.valueBlock.error;
1149
- return resultOffset;
1150
- }
1151
- this.fromBuffer(this.valueBlock.valueHexView);
1152
- if (!this.idBlock.error.length)
1153
- this.blockLength += this.idBlock.blockLength;
1154
- if (!this.lenBlock.error.length)
1155
- this.blockLength += this.lenBlock.blockLength;
1156
- if (!this.valueBlock.error.length)
1157
- this.blockLength += this.valueBlock.blockLength;
1158
- return resultOffset;
1159
- }
1160
- onAsciiEncoding() {
1161
- return `${this.constructor.NAME} : '${this.valueBlock.value}'`;
1162
- }
1163
- }
1164
- BaseStringBlock.NAME = "BaseStringBlock";
1165
-
1166
- class LocalPrimitiveValueBlock extends HexBlock(ValueBlock) {
1167
- constructor({ isHexOnly = true, ...parameters } = {}) {
1168
- super(parameters);
1169
- this.isHexOnly = isHexOnly;
1170
- }
1171
- }
1172
- LocalPrimitiveValueBlock.NAME = "PrimitiveValueBlock";
1173
-
1174
- var _a$w;
1175
- class Primitive extends BaseBlock {
1176
- constructor(parameters = {}) {
1177
- super(parameters, LocalPrimitiveValueBlock);
1178
- this.idBlock.isConstructed = false;
1179
- }
1180
- }
1181
- _a$w = Primitive;
1182
- (() => {
1183
- typeStore.Primitive = _a$w;
1184
- })();
1185
- Primitive.NAME = "PRIMITIVE";
1186
-
1187
- function localChangeType(inputObject, newType) {
1188
- if (inputObject instanceof newType) {
1189
- return inputObject;
1190
- }
1191
- const newObject = new newType();
1192
- newObject.idBlock = inputObject.idBlock;
1193
- newObject.lenBlock = inputObject.lenBlock;
1194
- newObject.warnings = inputObject.warnings;
1195
- newObject.valueBeforeDecodeView = inputObject.valueBeforeDecodeView;
1196
- return newObject;
1197
- }
1198
- function localFromBER(inputBuffer, inputOffset = 0, inputLength = inputBuffer.length) {
1199
- const incomingOffset = inputOffset;
1200
- let returnObject = new BaseBlock({}, ValueBlock);
1201
- const baseBlock = new LocalBaseBlock();
1202
- if (!checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength)) {
1203
- returnObject.error = baseBlock.error;
1204
- return {
1205
- offset: -1,
1206
- result: returnObject,
1207
- };
1208
- }
1209
- const intBuffer = inputBuffer.subarray(inputOffset, inputOffset + inputLength);
1210
- if (!intBuffer.length) {
1211
- returnObject.error = "Zero buffer length";
1212
- return {
1213
- offset: -1,
1214
- result: returnObject,
1215
- };
1216
- }
1217
- let resultOffset = returnObject.idBlock.fromBER(inputBuffer, inputOffset, inputLength);
1218
- if (returnObject.idBlock.warnings.length) {
1219
- returnObject.warnings.concat(returnObject.idBlock.warnings);
1220
- }
1221
- if (resultOffset === -1) {
1222
- returnObject.error = returnObject.idBlock.error;
1223
- return {
1224
- offset: -1,
1225
- result: returnObject,
1226
- };
1227
- }
1228
- inputOffset = resultOffset;
1229
- inputLength -= returnObject.idBlock.blockLength;
1230
- resultOffset = returnObject.lenBlock.fromBER(inputBuffer, inputOffset, inputLength);
1231
- if (returnObject.lenBlock.warnings.length) {
1232
- returnObject.warnings.concat(returnObject.lenBlock.warnings);
1233
- }
1234
- if (resultOffset === -1) {
1235
- returnObject.error = returnObject.lenBlock.error;
1236
- return {
1237
- offset: -1,
1238
- result: returnObject,
1239
- };
1240
- }
1241
- inputOffset = resultOffset;
1242
- inputLength -= returnObject.lenBlock.blockLength;
1243
- if (!returnObject.idBlock.isConstructed
1244
- && returnObject.lenBlock.isIndefiniteForm) {
1245
- returnObject.error = "Indefinite length form used for primitive encoding form";
1246
- return {
1247
- offset: -1,
1248
- result: returnObject,
1249
- };
1250
- }
1251
- let newASN1Type = BaseBlock;
1252
- switch (returnObject.idBlock.tagClass) {
1253
- case 1:
1254
- if ((returnObject.idBlock.tagNumber >= 37)
1255
- && (returnObject.idBlock.isHexOnly === false)) {
1256
- returnObject.error = "UNIVERSAL 37 and upper tags are reserved by ASN.1 standard";
1257
- return {
1258
- offset: -1,
1259
- result: returnObject,
1260
- };
1261
- }
1262
- switch (returnObject.idBlock.tagNumber) {
1263
- case 0:
1264
- if ((returnObject.idBlock.isConstructed)
1265
- && (returnObject.lenBlock.length > 0)) {
1266
- returnObject.error = "Type [UNIVERSAL 0] is reserved";
1267
- return {
1268
- offset: -1,
1269
- result: returnObject,
1270
- };
1271
- }
1272
- newASN1Type = typeStore.EndOfContent;
1273
- break;
1274
- case 1:
1275
- newASN1Type = typeStore.Boolean;
1276
- break;
1277
- case 2:
1278
- newASN1Type = typeStore.Integer;
1279
- break;
1280
- case 3:
1281
- newASN1Type = typeStore.BitString;
1282
- break;
1283
- case 4:
1284
- newASN1Type = typeStore.OctetString;
1285
- break;
1286
- case 5:
1287
- newASN1Type = typeStore.Null;
1288
- break;
1289
- case 6:
1290
- newASN1Type = typeStore.ObjectIdentifier;
1291
- break;
1292
- case 10:
1293
- newASN1Type = typeStore.Enumerated;
1294
- break;
1295
- case 12:
1296
- newASN1Type = typeStore.Utf8String;
1297
- break;
1298
- case 13:
1299
- newASN1Type = typeStore.RelativeObjectIdentifier;
1300
- break;
1301
- case 14:
1302
- newASN1Type = typeStore.TIME;
1303
- break;
1304
- case 15:
1305
- returnObject.error = "[UNIVERSAL 15] is reserved by ASN.1 standard";
1306
- return {
1307
- offset: -1,
1308
- result: returnObject,
1309
- };
1310
- case 16:
1311
- newASN1Type = typeStore.Sequence;
1312
- break;
1313
- case 17:
1314
- newASN1Type = typeStore.Set;
1315
- break;
1316
- case 18:
1317
- newASN1Type = typeStore.NumericString;
1318
- break;
1319
- case 19:
1320
- newASN1Type = typeStore.PrintableString;
1321
- break;
1322
- case 20:
1323
- newASN1Type = typeStore.TeletexString;
1324
- break;
1325
- case 21:
1326
- newASN1Type = typeStore.VideotexString;
1327
- break;
1328
- case 22:
1329
- newASN1Type = typeStore.IA5String;
1330
- break;
1331
- case 23:
1332
- newASN1Type = typeStore.UTCTime;
1333
- break;
1334
- case 24:
1335
- newASN1Type = typeStore.GeneralizedTime;
1336
- break;
1337
- case 25:
1338
- newASN1Type = typeStore.GraphicString;
1339
- break;
1340
- case 26:
1341
- newASN1Type = typeStore.VisibleString;
1342
- break;
1343
- case 27:
1344
- newASN1Type = typeStore.GeneralString;
1345
- break;
1346
- case 28:
1347
- newASN1Type = typeStore.UniversalString;
1348
- break;
1349
- case 29:
1350
- newASN1Type = typeStore.CharacterString;
1351
- break;
1352
- case 30:
1353
- newASN1Type = typeStore.BmpString;
1354
- break;
1355
- case 31:
1356
- newASN1Type = typeStore.DATE;
1357
- break;
1358
- case 32:
1359
- newASN1Type = typeStore.TimeOfDay;
1360
- break;
1361
- case 33:
1362
- newASN1Type = typeStore.DateTime;
1363
- break;
1364
- case 34:
1365
- newASN1Type = typeStore.Duration;
1366
- break;
1367
- default: {
1368
- const newObject = returnObject.idBlock.isConstructed
1369
- ? new typeStore.Constructed()
1370
- : new typeStore.Primitive();
1371
- newObject.idBlock = returnObject.idBlock;
1372
- newObject.lenBlock = returnObject.lenBlock;
1373
- newObject.warnings = returnObject.warnings;
1374
- returnObject = newObject;
1375
- }
1376
- }
1377
- break;
1378
- case 2:
1379
- case 3:
1380
- case 4:
1381
- default: {
1382
- newASN1Type = returnObject.idBlock.isConstructed
1383
- ? typeStore.Constructed
1384
- : typeStore.Primitive;
1385
- }
1386
- }
1387
- returnObject = localChangeType(returnObject, newASN1Type);
1388
- resultOffset = returnObject.fromBER(inputBuffer, inputOffset, returnObject.lenBlock.isIndefiniteForm ? inputLength : returnObject.lenBlock.length);
1389
- returnObject.valueBeforeDecodeView = inputBuffer.subarray(incomingOffset, incomingOffset + returnObject.blockLength);
1390
- return {
1391
- offset: resultOffset,
1392
- result: returnObject,
1393
- };
1394
- }
1395
- function fromBER(inputBuffer) {
1396
- if (!inputBuffer.byteLength) {
1397
- const result = new BaseBlock({}, ValueBlock);
1398
- result.error = "Input buffer has zero length";
1399
- return {
1400
- offset: -1,
1401
- result,
1402
- };
1403
- }
1404
- return localFromBER(BufferSourceConverter.toUint8Array(inputBuffer).slice(), 0, inputBuffer.byteLength);
1405
- }
1406
-
1407
- function checkLen(indefiniteLength, length) {
1408
- if (indefiniteLength) {
1409
- return 1;
1410
- }
1411
- return length;
1412
- }
1413
- class LocalConstructedValueBlock extends ValueBlock {
1414
- constructor({ value = [], isIndefiniteForm = false, ...parameters } = {}) {
1415
- super(parameters);
1416
- this.value = value;
1417
- this.isIndefiniteForm = isIndefiniteForm;
1418
- }
1419
- fromBER(inputBuffer, inputOffset, inputLength) {
1420
- const view = BufferSourceConverter.toUint8Array(inputBuffer);
1421
- if (!checkBufferParams(this, view, inputOffset, inputLength)) {
1422
- return -1;
1423
- }
1424
- this.valueBeforeDecodeView = view.subarray(inputOffset, inputOffset + inputLength);
1425
- if (this.valueBeforeDecodeView.length === 0) {
1426
- this.warnings.push("Zero buffer length");
1427
- return inputOffset;
1428
- }
1429
- let currentOffset = inputOffset;
1430
- while (checkLen(this.isIndefiniteForm, inputLength) > 0) {
1431
- const returnObject = localFromBER(view, currentOffset, inputLength);
1432
- if (returnObject.offset === -1) {
1433
- this.error = returnObject.result.error;
1434
- this.warnings.concat(returnObject.result.warnings);
1435
- return -1;
1436
- }
1437
- currentOffset = returnObject.offset;
1438
- this.blockLength += returnObject.result.blockLength;
1439
- inputLength -= returnObject.result.blockLength;
1440
- this.value.push(returnObject.result);
1441
- if (this.isIndefiniteForm && returnObject.result.constructor.NAME === END_OF_CONTENT_NAME) {
1442
- break;
1443
- }
1444
- }
1445
- if (this.isIndefiniteForm) {
1446
- if (this.value[this.value.length - 1].constructor.NAME === END_OF_CONTENT_NAME) {
1447
- this.value.pop();
1448
- }
1449
- else {
1450
- this.warnings.push("No EndOfContent block encoded");
1451
- }
1452
- }
1453
- return currentOffset;
1454
- }
1455
- toBER(sizeOnly, writer) {
1456
- const _writer = writer || new ViewWriter();
1457
- for (let i = 0; i < this.value.length; i++) {
1458
- this.value[i].toBER(sizeOnly, _writer);
1459
- }
1460
- if (!writer) {
1461
- return _writer.final();
1462
- }
1463
- return EMPTY_BUFFER;
1464
- }
1465
- toJSON() {
1466
- const object = {
1467
- ...super.toJSON(),
1468
- isIndefiniteForm: this.isIndefiniteForm,
1469
- value: [],
1470
- };
1471
- for (const value of this.value) {
1472
- object.value.push(value.toJSON());
1473
- }
1474
- return object;
1475
- }
1476
- }
1477
- LocalConstructedValueBlock.NAME = "ConstructedValueBlock";
1478
-
1479
- var _a$v;
1480
- class Constructed extends BaseBlock {
1481
- constructor(parameters = {}) {
1482
- super(parameters, LocalConstructedValueBlock);
1483
- this.idBlock.isConstructed = true;
1484
- }
1485
- fromBER(inputBuffer, inputOffset, inputLength) {
1486
- this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;
1487
- const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, (this.lenBlock.isIndefiniteForm) ? inputLength : this.lenBlock.length);
1488
- if (resultOffset === -1) {
1489
- this.error = this.valueBlock.error;
1490
- return resultOffset;
1491
- }
1492
- if (!this.idBlock.error.length)
1493
- this.blockLength += this.idBlock.blockLength;
1494
- if (!this.lenBlock.error.length)
1495
- this.blockLength += this.lenBlock.blockLength;
1496
- if (!this.valueBlock.error.length)
1497
- this.blockLength += this.valueBlock.blockLength;
1498
- return resultOffset;
1499
- }
1500
- onAsciiEncoding() {
1501
- const values = [];
1502
- for (const value of this.valueBlock.value) {
1503
- values.push(value.toString("ascii").split("\n").map((o) => ` ${o}`).join("\n"));
1504
- }
1505
- const blockName = this.idBlock.tagClass === 3
1506
- ? `[${this.idBlock.tagNumber}]`
1507
- : this.constructor.NAME;
1508
- return values.length
1509
- ? `${blockName} :\n${values.join("\n")}`
1510
- : `${blockName} :`;
1511
- }
1512
- }
1513
- _a$v = Constructed;
1514
- (() => {
1515
- typeStore.Constructed = _a$v;
1516
- })();
1517
- Constructed.NAME = "CONSTRUCTED";
1518
-
1519
- class LocalEndOfContentValueBlock extends ValueBlock {
1520
- fromBER(inputBuffer, inputOffset, _inputLength) {
1521
- return inputOffset;
1522
- }
1523
- toBER(_sizeOnly) {
1524
- return EMPTY_BUFFER;
1525
- }
1526
- }
1527
- LocalEndOfContentValueBlock.override = "EndOfContentValueBlock";
1528
-
1529
- var _a$u;
1530
- class EndOfContent extends BaseBlock {
1531
- constructor(parameters = {}) {
1532
- super(parameters, LocalEndOfContentValueBlock);
1533
- this.idBlock.tagClass = 1;
1534
- this.idBlock.tagNumber = 0;
1535
- }
1536
- }
1537
- _a$u = EndOfContent;
1538
- (() => {
1539
- typeStore.EndOfContent = _a$u;
1540
- })();
1541
- EndOfContent.NAME = END_OF_CONTENT_NAME;
1542
-
1543
- var _a$t;
1544
- class Null extends BaseBlock {
1545
- constructor(parameters = {}) {
1546
- super(parameters, ValueBlock);
1547
- this.idBlock.tagClass = 1;
1548
- this.idBlock.tagNumber = 5;
1549
- }
1550
- fromBER(inputBuffer, inputOffset, inputLength) {
1551
- if (this.lenBlock.length > 0)
1552
- this.warnings.push("Non-zero length of value block for Null type");
1553
- if (!this.idBlock.error.length)
1554
- this.blockLength += this.idBlock.blockLength;
1555
- if (!this.lenBlock.error.length)
1556
- this.blockLength += this.lenBlock.blockLength;
1557
- this.blockLength += inputLength;
1558
- if ((inputOffset + inputLength) > inputBuffer.byteLength) {
1559
- this.error = "End of input reached before message was fully decoded (inconsistent offset and length values)";
1560
- return -1;
1561
- }
1562
- return (inputOffset + inputLength);
1563
- }
1564
- toBER(sizeOnly, writer) {
1565
- const retBuf = new ArrayBuffer(2);
1566
- if (!sizeOnly) {
1567
- const retView = new Uint8Array(retBuf);
1568
- retView[0] = 0x05;
1569
- retView[1] = 0x00;
1570
- }
1571
- if (writer) {
1572
- writer.write(retBuf);
1573
- }
1574
- return retBuf;
1575
- }
1576
- onAsciiEncoding() {
1577
- return `${this.constructor.NAME}`;
1578
- }
1579
- }
1580
- _a$t = Null;
1581
- (() => {
1582
- typeStore.Null = _a$t;
1583
- })();
1584
- Null.NAME = "NULL";
1585
-
1586
- class LocalBooleanValueBlock extends HexBlock(ValueBlock) {
1587
- get value() {
1588
- for (const octet of this.valueHexView) {
1589
- if (octet > 0) {
1590
- return true;
1591
- }
1592
- }
1593
- return false;
1594
- }
1595
- set value(value) {
1596
- this.valueHexView[0] = value ? 0xFF : 0x00;
1597
- }
1598
- constructor({ value, ...parameters } = {}) {
1599
- super(parameters);
1600
- if (parameters.valueHex) {
1601
- this.valueHexView = BufferSourceConverter.toUint8Array(parameters.valueHex);
1602
- }
1603
- else {
1604
- this.valueHexView = new Uint8Array(1);
1605
- }
1606
- if (value) {
1607
- this.value = value;
1608
- }
1609
- }
1610
- fromBER(inputBuffer, inputOffset, inputLength) {
1611
- const inputView = BufferSourceConverter.toUint8Array(inputBuffer);
1612
- if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
1613
- return -1;
1614
- }
1615
- this.valueHexView = inputView.subarray(inputOffset, inputOffset + inputLength);
1616
- if (inputLength > 1)
1617
- this.warnings.push("Boolean value encoded in more then 1 octet");
1618
- this.isHexOnly = true;
1619
- utilDecodeTC.call(this);
1620
- this.blockLength = inputLength;
1621
- return (inputOffset + inputLength);
1622
- }
1623
- toBER() {
1624
- return this.valueHexView.slice();
1625
- }
1626
- toJSON() {
1627
- return {
1628
- ...super.toJSON(),
1629
- value: this.value,
1630
- };
1631
- }
1632
- }
1633
- LocalBooleanValueBlock.NAME = "BooleanValueBlock";
1634
-
1635
- var _a$s;
1636
- class Boolean extends BaseBlock {
1637
- getValue() {
1638
- return this.valueBlock.value;
1639
- }
1640
- setValue(value) {
1641
- this.valueBlock.value = value;
1642
- }
1643
- constructor(parameters = {}) {
1644
- super(parameters, LocalBooleanValueBlock);
1645
- this.idBlock.tagClass = 1;
1646
- this.idBlock.tagNumber = 1;
1647
- }
1648
- onAsciiEncoding() {
1649
- return `${this.constructor.NAME} : ${this.getValue}`;
1650
- }
1651
- }
1652
- _a$s = Boolean;
1653
- (() => {
1654
- typeStore.Boolean = _a$s;
1655
- })();
1656
- Boolean.NAME = "BOOLEAN";
1657
-
1658
- class LocalOctetStringValueBlock extends HexBlock(LocalConstructedValueBlock) {
1659
- constructor({ isConstructed = false, ...parameters } = {}) {
1660
- super(parameters);
1661
- this.isConstructed = isConstructed;
1662
- }
1663
- fromBER(inputBuffer, inputOffset, inputLength) {
1664
- let resultOffset = 0;
1665
- if (this.isConstructed) {
1666
- this.isHexOnly = false;
1667
- resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength);
1668
- if (resultOffset === -1)
1669
- return resultOffset;
1670
- for (let i = 0; i < this.value.length; i++) {
1671
- const currentBlockName = this.value[i].constructor.NAME;
1672
- if (currentBlockName === END_OF_CONTENT_NAME) {
1673
- if (this.isIndefiniteForm)
1674
- break;
1675
- else {
1676
- this.error = "EndOfContent is unexpected, OCTET STRING may consists of OCTET STRINGs only";
1677
- return -1;
1678
- }
1679
- }
1680
- if (currentBlockName !== OCTET_STRING_NAME) {
1681
- this.error = "OCTET STRING may consists of OCTET STRINGs only";
1682
- return -1;
1683
- }
1684
- }
1685
- }
1686
- else {
1687
- this.isHexOnly = true;
1688
- resultOffset = super.fromBER(inputBuffer, inputOffset, inputLength);
1689
- this.blockLength = inputLength;
1690
- }
1691
- return resultOffset;
1692
- }
1693
- toBER(sizeOnly, writer) {
1694
- if (this.isConstructed)
1695
- return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer);
1696
- return sizeOnly
1697
- ? new ArrayBuffer(this.valueHexView.byteLength)
1698
- : this.valueHexView.slice().buffer;
1699
- }
1700
- toJSON() {
1701
- return {
1702
- ...super.toJSON(),
1703
- isConstructed: this.isConstructed,
1704
- };
1705
- }
1706
- }
1707
- LocalOctetStringValueBlock.NAME = "OctetStringValueBlock";
1708
-
1709
- var _a$r;
1710
- class OctetString extends BaseBlock {
1711
- constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) {
1712
- var _b, _c;
1713
- (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : (parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length));
1714
- super({
1715
- idBlock: {
1716
- isConstructed: parameters.isConstructed,
1717
- ...idBlock,
1718
- },
1719
- lenBlock: {
1720
- ...lenBlock,
1721
- isIndefiniteForm: !!parameters.isIndefiniteForm,
1722
- },
1723
- ...parameters,
1724
- }, LocalOctetStringValueBlock);
1725
- this.idBlock.tagClass = 1;
1726
- this.idBlock.tagNumber = 4;
1727
- }
1728
- fromBER(inputBuffer, inputOffset, inputLength) {
1729
- this.valueBlock.isConstructed = this.idBlock.isConstructed;
1730
- this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;
1731
- if (inputLength === 0) {
1732
- if (this.idBlock.error.length === 0)
1733
- this.blockLength += this.idBlock.blockLength;
1734
- if (this.lenBlock.error.length === 0)
1735
- this.blockLength += this.lenBlock.blockLength;
1736
- return inputOffset;
1737
- }
1738
- if (!this.valueBlock.isConstructed) {
1739
- const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer;
1740
- const buf = view.subarray(inputOffset, inputOffset + inputLength);
1741
- try {
1742
- if (buf.byteLength) {
1743
- const asn = localFromBER(buf, 0, buf.byteLength);
1744
- if (asn.offset !== -1 && asn.offset === inputLength) {
1745
- this.valueBlock.value = [asn.result];
1746
- }
1747
- }
1748
- }
1749
- catch {
1750
- }
1751
- }
1752
- return super.fromBER(inputBuffer, inputOffset, inputLength);
1753
- }
1754
- onAsciiEncoding() {
1755
- if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length)) {
1756
- return Constructed.prototype.onAsciiEncoding.call(this);
1757
- }
1758
- const name = this.constructor.NAME;
1759
- const value = Convert.ToHex(this.valueBlock.valueHexView);
1760
- return `${name} : ${value}`;
1761
- }
1762
- getValue() {
1763
- if (!this.idBlock.isConstructed) {
1764
- return this.valueBlock.valueHexView.slice().buffer;
1765
- }
1766
- const array = [];
1767
- for (const content of this.valueBlock.value) {
1768
- if (content instanceof _a$r) {
1769
- array.push(content.valueBlock.valueHexView);
1770
- }
1771
- }
1772
- return BufferSourceConverter.concat(array);
1773
- }
1774
- }
1775
- _a$r = OctetString;
1776
- (() => {
1777
- typeStore.OctetString = _a$r;
1778
- })();
1779
- OctetString.NAME = OCTET_STRING_NAME;
1780
-
1781
- class LocalBitStringValueBlock extends HexBlock(LocalConstructedValueBlock) {
1782
- constructor({ unusedBits = 0, isConstructed = false, ...parameters } = {}) {
1783
- super(parameters);
1784
- this.unusedBits = unusedBits;
1785
- this.isConstructed = isConstructed;
1786
- this.blockLength = this.valueHexView.byteLength;
1787
- }
1788
- fromBER(inputBuffer, inputOffset, inputLength) {
1789
- if (!inputLength) {
1790
- return inputOffset;
1791
- }
1792
- let resultOffset = -1;
1793
- if (this.isConstructed) {
1794
- resultOffset = LocalConstructedValueBlock.prototype.fromBER.call(this, inputBuffer, inputOffset, inputLength);
1795
- if (resultOffset === -1)
1796
- return resultOffset;
1797
- for (const value of this.value) {
1798
- const currentBlockName = value.constructor.NAME;
1799
- if (currentBlockName === END_OF_CONTENT_NAME) {
1800
- if (this.isIndefiniteForm)
1801
- break;
1802
- else {
1803
- this.error = "EndOfContent is unexpected, BIT STRING may consists of BIT STRINGs only";
1804
- return -1;
1805
- }
1806
- }
1807
- if (currentBlockName !== BIT_STRING_NAME) {
1808
- this.error = "BIT STRING may consists of BIT STRINGs only";
1809
- return -1;
1810
- }
1811
- const valueBlock = value.valueBlock;
1812
- if ((this.unusedBits > 0) && (valueBlock.unusedBits > 0)) {
1813
- this.error = "Using of \"unused bits\" inside constructive BIT STRING allowed for least one only";
1814
- return -1;
1815
- }
1816
- this.unusedBits = valueBlock.unusedBits;
1817
- }
1818
- return resultOffset;
1819
- }
1820
- const inputView = BufferSourceConverter.toUint8Array(inputBuffer);
1821
- if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
1822
- return -1;
1823
- }
1824
- const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
1825
- this.unusedBits = intBuffer[0];
1826
- if (this.unusedBits > 7) {
1827
- this.error = "Unused bits for BitString must be in range 0-7";
1828
- return -1;
1829
- }
1830
- if (!this.unusedBits) {
1831
- const buf = intBuffer.subarray(1);
1832
- try {
1833
- if (buf.byteLength) {
1834
- const asn = localFromBER(buf, 0, buf.byteLength);
1835
- if (asn.offset !== -1 && asn.offset === (inputLength - 1)) {
1836
- this.value = [asn.result];
1837
- }
1838
- }
1839
- }
1840
- catch {
1841
- }
1842
- }
1843
- this.valueHexView = intBuffer.subarray(1);
1844
- this.blockLength = intBuffer.length;
1845
- return (inputOffset + inputLength);
1846
- }
1847
- toBER(sizeOnly, writer) {
1848
- if (this.isConstructed) {
1849
- return LocalConstructedValueBlock.prototype.toBER.call(this, sizeOnly, writer);
1850
- }
1851
- if (sizeOnly) {
1852
- return new ArrayBuffer(this.valueHexView.byteLength + 1);
1853
- }
1854
- if (!this.valueHexView.byteLength) {
1855
- const empty = new Uint8Array(1);
1856
- empty[0] = 0;
1857
- return empty.buffer;
1858
- }
1859
- const retView = new Uint8Array(this.valueHexView.length + 1);
1860
- retView[0] = this.unusedBits;
1861
- retView.set(this.valueHexView, 1);
1862
- return retView.buffer;
1863
- }
1864
- toJSON() {
1865
- return {
1866
- ...super.toJSON(),
1867
- unusedBits: this.unusedBits,
1868
- isConstructed: this.isConstructed,
1869
- };
1870
- }
1871
- }
1872
- LocalBitStringValueBlock.NAME = "BitStringValueBlock";
1873
-
1874
- var _a$q;
1875
- class BitString extends BaseBlock {
1876
- constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) {
1877
- var _b, _c;
1878
- (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : (parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length));
1879
- super({
1880
- idBlock: {
1881
- isConstructed: parameters.isConstructed,
1882
- ...idBlock,
1883
- },
1884
- lenBlock: {
1885
- ...lenBlock,
1886
- isIndefiniteForm: !!parameters.isIndefiniteForm,
1887
- },
1888
- ...parameters,
1889
- }, LocalBitStringValueBlock);
1890
- this.idBlock.tagClass = 1;
1891
- this.idBlock.tagNumber = 3;
1892
- }
1893
- fromBER(inputBuffer, inputOffset, inputLength) {
1894
- this.valueBlock.isConstructed = this.idBlock.isConstructed;
1895
- this.valueBlock.isIndefiniteForm = this.lenBlock.isIndefiniteForm;
1896
- return super.fromBER(inputBuffer, inputOffset, inputLength);
1897
- }
1898
- onAsciiEncoding() {
1899
- if (this.valueBlock.isConstructed || (this.valueBlock.value && this.valueBlock.value.length)) {
1900
- return Constructed.prototype.onAsciiEncoding.call(this);
1901
- }
1902
- else {
1903
- const bits = [];
1904
- const valueHex = this.valueBlock.valueHexView;
1905
- for (const byte of valueHex) {
1906
- bits.push(byte.toString(2).padStart(8, "0"));
1907
- }
1908
- const bitsStr = bits.join("");
1909
- const name = this.constructor.NAME;
1910
- const value = bitsStr.substring(0, bitsStr.length - this.valueBlock.unusedBits);
1911
- return `${name} : ${value}`;
1912
- }
1913
- }
1914
- }
1915
- _a$q = BitString;
1916
- (() => {
1917
- typeStore.BitString = _a$q;
1918
- })();
1919
- BitString.NAME = BIT_STRING_NAME;
1920
-
1921
- var _a$p;
1922
- function viewAdd(first, second) {
1923
- const c = new Uint8Array([0]);
1924
- const firstView = new Uint8Array(first);
1925
- const secondView = new Uint8Array(second);
1926
- let firstViewCopy = firstView.slice(0);
1927
- const firstViewCopyLength = firstViewCopy.length - 1;
1928
- const secondViewCopy = secondView.slice(0);
1929
- const secondViewCopyLength = secondViewCopy.length - 1;
1930
- let value = 0;
1931
- const max = (secondViewCopyLength < firstViewCopyLength) ? firstViewCopyLength : secondViewCopyLength;
1932
- let counter = 0;
1933
- for (let i = max; i >= 0; i--, counter++) {
1934
- switch (true) {
1935
- case (counter < secondViewCopy.length):
1936
- value = firstViewCopy[firstViewCopyLength - counter] + secondViewCopy[secondViewCopyLength - counter] + c[0];
1937
- break;
1938
- default:
1939
- value = firstViewCopy[firstViewCopyLength - counter] + c[0];
1940
- }
1941
- c[0] = value / 10;
1942
- switch (true) {
1943
- case (counter >= firstViewCopy.length):
1944
- firstViewCopy = utilConcatView(new Uint8Array([value % 10]), firstViewCopy);
1945
- break;
1946
- default:
1947
- firstViewCopy[firstViewCopyLength - counter] = value % 10;
1948
- }
1949
- }
1950
- if (c[0] > 0)
1951
- firstViewCopy = utilConcatView(c, firstViewCopy);
1952
- return firstViewCopy;
1953
- }
1954
- function power2(n) {
1955
- if (n >= powers2.length) {
1956
- for (let p = powers2.length; p <= n; p++) {
1957
- const c = new Uint8Array([0]);
1958
- let digits = (powers2[p - 1]).slice(0);
1959
- for (let i = (digits.length - 1); i >= 0; i--) {
1960
- const newValue = new Uint8Array([(digits[i] << 1) + c[0]]);
1961
- c[0] = newValue[0] / 10;
1962
- digits[i] = newValue[0] % 10;
1963
- }
1964
- if (c[0] > 0)
1965
- digits = utilConcatView(c, digits);
1966
- powers2.push(digits);
1967
- }
1968
- }
1969
- return powers2[n];
1970
- }
1971
- function viewSub(first, second) {
1972
- let b = 0;
1973
- const firstView = new Uint8Array(first);
1974
- const secondView = new Uint8Array(second);
1975
- const firstViewCopy = firstView.slice(0);
1976
- const firstViewCopyLength = firstViewCopy.length - 1;
1977
- const secondViewCopy = secondView.slice(0);
1978
- const secondViewCopyLength = secondViewCopy.length - 1;
1979
- let value;
1980
- let counter = 0;
1981
- for (let i = secondViewCopyLength; i >= 0; i--, counter++) {
1982
- value = firstViewCopy[firstViewCopyLength - counter] - secondViewCopy[secondViewCopyLength - counter] - b;
1983
- switch (true) {
1984
- case (value < 0):
1985
- b = 1;
1986
- firstViewCopy[firstViewCopyLength - counter] = value + 10;
1987
- break;
1988
- default:
1989
- b = 0;
1990
- firstViewCopy[firstViewCopyLength - counter] = value;
1991
- }
1992
- }
1993
- if (b > 0) {
1994
- for (let i = (firstViewCopyLength - secondViewCopyLength + 1); i >= 0; i--, counter++) {
1995
- value = firstViewCopy[firstViewCopyLength - counter] - b;
1996
- if (value < 0) {
1997
- b = 1;
1998
- firstViewCopy[firstViewCopyLength - counter] = value + 10;
1999
- }
2000
- else {
2001
- b = 0;
2002
- firstViewCopy[firstViewCopyLength - counter] = value;
2003
- break;
2004
- }
2005
- }
2006
- }
2007
- return firstViewCopy.slice();
2008
- }
2009
- class LocalIntegerValueBlock extends HexBlock(ValueBlock) {
2010
- setValueHex() {
2011
- if (this.valueHexView.length >= 4) {
2012
- this.warnings.push("Too big Integer for decoding, hex only");
2013
- this.isHexOnly = true;
2014
- this._valueDec = 0;
2015
- }
2016
- else {
2017
- this.isHexOnly = false;
2018
- if (this.valueHexView.length > 0) {
2019
- this._valueDec = utilDecodeTC.call(this);
2020
- }
2021
- }
2022
- }
2023
- constructor({ value, ...parameters } = {}) {
2024
- super(parameters);
2025
- this._valueDec = 0;
2026
- if (parameters.valueHex) {
2027
- this.setValueHex();
2028
- }
2029
- if (value !== undefined) {
2030
- this.valueDec = value;
2031
- }
2032
- }
2033
- set valueDec(v) {
2034
- this._valueDec = v;
2035
- this.isHexOnly = false;
2036
- this.valueHexView = new Uint8Array(utilEncodeTC(v));
2037
- }
2038
- get valueDec() {
2039
- return this._valueDec;
2040
- }
2041
- fromDER(inputBuffer, inputOffset, inputLength, expectedLength = 0) {
2042
- const offset = this.fromBER(inputBuffer, inputOffset, inputLength);
2043
- if (offset === -1)
2044
- return offset;
2045
- const view = this.valueHexView;
2046
- if ((view[0] === 0x00) && ((view[1] & 0x80) !== 0)) {
2047
- this.valueHexView = view.subarray(1);
2048
- }
2049
- else {
2050
- if (expectedLength !== 0) {
2051
- if (view.length < expectedLength) {
2052
- if ((expectedLength - view.length) > 1)
2053
- expectedLength = view.length + 1;
2054
- this.valueHexView = view.subarray(expectedLength - view.length);
2055
- }
2056
- }
2057
- }
2058
- return offset;
2059
- }
2060
- toDER(sizeOnly = false) {
2061
- const view = this.valueHexView;
2062
- switch (true) {
2063
- case ((view[0] & 0x80) !== 0):
2064
- {
2065
- const updatedView = new Uint8Array(this.valueHexView.length + 1);
2066
- updatedView[0] = 0x00;
2067
- updatedView.set(view, 1);
2068
- this.valueHexView = updatedView;
2069
- }
2070
- break;
2071
- case ((view[0] === 0x00) && ((view[1] & 0x80) === 0)):
2072
- {
2073
- this.valueHexView = this.valueHexView.subarray(1);
2074
- }
2075
- break;
2076
- }
2077
- return this.toBER(sizeOnly);
2078
- }
2079
- fromBER(inputBuffer, inputOffset, inputLength) {
2080
- const resultOffset = super.fromBER(inputBuffer, inputOffset, inputLength);
2081
- if (resultOffset === -1) {
2082
- return resultOffset;
2083
- }
2084
- this.setValueHex();
2085
- return resultOffset;
2086
- }
2087
- toBER(sizeOnly) {
2088
- return sizeOnly
2089
- ? new ArrayBuffer(this.valueHexView.length)
2090
- : this.valueHexView.slice().buffer;
2091
- }
2092
- toJSON() {
2093
- return {
2094
- ...super.toJSON(),
2095
- valueDec: this.valueDec,
2096
- };
2097
- }
2098
- toString() {
2099
- const firstBit = (this.valueHexView.length * 8) - 1;
2100
- let digits = new Uint8Array((this.valueHexView.length * 8) / 3);
2101
- let bitNumber = 0;
2102
- let currentByte;
2103
- const asn1View = this.valueHexView;
2104
- let result = "";
2105
- let flag = false;
2106
- for (let byteNumber = (asn1View.byteLength - 1); byteNumber >= 0; byteNumber--) {
2107
- currentByte = asn1View[byteNumber];
2108
- for (let i = 0; i < 8; i++) {
2109
- if ((currentByte & 1) === 1) {
2110
- switch (bitNumber) {
2111
- case firstBit:
2112
- digits = viewSub(power2(bitNumber), digits);
2113
- result = "-";
2114
- break;
2115
- default:
2116
- digits = viewAdd(digits, power2(bitNumber));
2117
- }
2118
- }
2119
- bitNumber++;
2120
- currentByte >>= 1;
2121
- }
2122
- }
2123
- for (let i = 0; i < digits.length; i++) {
2124
- if (digits[i])
2125
- flag = true;
2126
- if (flag)
2127
- result += digitsString.charAt(digits[i]);
2128
- }
2129
- if (flag === false)
2130
- result += digitsString.charAt(0);
2131
- return result;
2132
- }
2133
- }
2134
- _a$p = LocalIntegerValueBlock;
2135
- LocalIntegerValueBlock.NAME = "IntegerValueBlock";
2136
- (() => {
2137
- Object.defineProperty(_a$p.prototype, "valueHex", {
2138
- set: function (v) {
2139
- this.valueHexView = new Uint8Array(v);
2140
- this.setValueHex();
2141
- },
2142
- get: function () {
2143
- return this.valueHexView.slice().buffer;
2144
- },
2145
- });
2146
- })();
2147
-
2148
- var _a$o;
2149
- class Integer extends BaseBlock {
2150
- constructor(parameters = {}) {
2151
- super(parameters, LocalIntegerValueBlock);
2152
- this.idBlock.tagClass = 1;
2153
- this.idBlock.tagNumber = 2;
2154
- }
2155
- toBigInt() {
2156
- assertBigInt();
2157
- return BigInt(this.valueBlock.toString());
2158
- }
2159
- static fromBigInt(value) {
2160
- assertBigInt();
2161
- const bigIntValue = BigInt(value);
2162
- const writer = new ViewWriter();
2163
- const hex = bigIntValue.toString(16).replace(/^-/, "");
2164
- const view = new Uint8Array(Convert.FromHex(hex));
2165
- if (bigIntValue < 0) {
2166
- const first = new Uint8Array(view.length + (view[0] & 0x80 ? 1 : 0));
2167
- first[0] |= 0x80;
2168
- const firstInt = BigInt(`0x${Convert.ToHex(first)}`);
2169
- const secondInt = firstInt + bigIntValue;
2170
- const second = BufferSourceConverter.toUint8Array(Convert.FromHex(secondInt.toString(16)));
2171
- second[0] |= 0x80;
2172
- writer.write(second);
2173
- }
2174
- else {
2175
- if (view[0] & 0x80) {
2176
- writer.write(new Uint8Array([0]));
2177
- }
2178
- writer.write(view);
2179
- }
2180
- const res = new _a$o({ valueHex: writer.final() });
2181
- return res;
2182
- }
2183
- convertToDER() {
2184
- const integer = new _a$o({ valueHex: this.valueBlock.valueHexView });
2185
- integer.valueBlock.toDER();
2186
- return integer;
2187
- }
2188
- convertFromDER() {
2189
- return new _a$o({
2190
- valueHex: this.valueBlock.valueHexView[0] === 0
2191
- ? this.valueBlock.valueHexView.subarray(1)
2192
- : this.valueBlock.valueHexView,
2193
- });
2194
- }
2195
- onAsciiEncoding() {
2196
- return `${this.constructor.NAME} : ${this.valueBlock.toString()}`;
2197
- }
2198
- }
2199
- _a$o = Integer;
2200
- (() => {
2201
- typeStore.Integer = _a$o;
2202
- })();
2203
- Integer.NAME = "INTEGER";
2204
-
2205
- var _a$n;
2206
- class Enumerated extends Integer {
2207
- constructor(parameters = {}) {
2208
- super(parameters);
2209
- this.idBlock.tagClass = 1;
2210
- this.idBlock.tagNumber = 10;
2211
- }
2212
- }
2213
- _a$n = Enumerated;
2214
- (() => {
2215
- typeStore.Enumerated = _a$n;
2216
- })();
2217
- Enumerated.NAME = "ENUMERATED";
2218
-
2219
- class LocalSidValueBlock extends HexBlock(ValueBlock) {
2220
- constructor({ valueDec = -1, isFirstSid = false, ...parameters } = {}) {
2221
- super(parameters);
2222
- this.valueDec = valueDec;
2223
- this.isFirstSid = isFirstSid;
2224
- }
2225
- fromBER(inputBuffer, inputOffset, inputLength) {
2226
- if (!inputLength) {
2227
- return inputOffset;
2228
- }
2229
- const inputView = BufferSourceConverter.toUint8Array(inputBuffer);
2230
- if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
2231
- return -1;
2232
- }
2233
- const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
2234
- this.valueHexView = new Uint8Array(inputLength);
2235
- for (let i = 0; i < inputLength; i++) {
2236
- this.valueHexView[i] = intBuffer[i] & 0x7F;
2237
- this.blockLength++;
2238
- if ((intBuffer[i] & 0x80) === 0x00)
2239
- break;
2240
- }
2241
- const tempView = new Uint8Array(this.blockLength);
2242
- for (let i = 0; i < this.blockLength; i++) {
2243
- tempView[i] = this.valueHexView[i];
2244
- }
2245
- this.valueHexView = tempView;
2246
- if ((intBuffer[this.blockLength - 1] & 0x80) !== 0x00) {
2247
- this.error = "End of input reached before message was fully decoded";
2248
- return -1;
2249
- }
2250
- if (this.valueHexView[0] === 0x00)
2251
- this.warnings.push("Needlessly long format of SID encoding");
2252
- if (this.blockLength <= 8)
2253
- this.valueDec = utilFromBase(this.valueHexView, 7);
2254
- else {
2255
- this.isHexOnly = true;
2256
- this.warnings.push("Too big SID for decoding, hex only");
2257
- }
2258
- return (inputOffset + this.blockLength);
2259
- }
2260
- set valueBigInt(value) {
2261
- assertBigInt();
2262
- let bits = BigInt(value).toString(2);
2263
- while (bits.length % 7) {
2264
- bits = "0" + bits;
2265
- }
2266
- const bytes = new Uint8Array(bits.length / 7);
2267
- for (let i = 0; i < bytes.length; i++) {
2268
- bytes[i] = parseInt(bits.slice(i * 7, i * 7 + 7), 2) + (i + 1 < bytes.length ? 0x80 : 0);
2269
- }
2270
- this.fromBER(bytes.buffer, 0, bytes.length);
2271
- }
2272
- toBER(sizeOnly) {
2273
- if (this.isHexOnly) {
2274
- if (sizeOnly)
2275
- return (new ArrayBuffer(this.valueHexView.byteLength));
2276
- const curView = this.valueHexView;
2277
- const retView = new Uint8Array(this.blockLength);
2278
- for (let i = 0; i < (this.blockLength - 1); i++)
2279
- retView[i] = curView[i] | 0x80;
2280
- retView[this.blockLength - 1] = curView[this.blockLength - 1];
2281
- return retView.buffer;
2282
- }
2283
- const encodedBuf = utilToBase(this.valueDec, 7);
2284
- if (encodedBuf.byteLength === 0) {
2285
- this.error = "Error during encoding SID value";
2286
- return EMPTY_BUFFER;
2287
- }
2288
- const retView = new Uint8Array(encodedBuf.byteLength);
2289
- if (!sizeOnly) {
2290
- const encodedView = new Uint8Array(encodedBuf);
2291
- const len = encodedBuf.byteLength - 1;
2292
- for (let i = 0; i < len; i++)
2293
- retView[i] = encodedView[i] | 0x80;
2294
- retView[len] = encodedView[len];
2295
- }
2296
- return retView;
2297
- }
2298
- toString() {
2299
- let result = "";
2300
- if (this.isHexOnly)
2301
- result = Convert.ToHex(this.valueHexView);
2302
- else {
2303
- if (this.isFirstSid) {
2304
- let sidValue = this.valueDec;
2305
- if (this.valueDec <= 39)
2306
- result = "0.";
2307
- else {
2308
- if (this.valueDec <= 79) {
2309
- result = "1.";
2310
- sidValue -= 40;
2311
- }
2312
- else {
2313
- result = "2.";
2314
- sidValue -= 80;
2315
- }
2316
- }
2317
- result += sidValue.toString();
2318
- }
2319
- else
2320
- result = this.valueDec.toString();
2321
- }
2322
- return result;
2323
- }
2324
- toJSON() {
2325
- return {
2326
- ...super.toJSON(),
2327
- valueDec: this.valueDec,
2328
- isFirstSid: this.isFirstSid,
2329
- };
2330
- }
2331
- }
2332
- LocalSidValueBlock.NAME = "sidBlock";
2333
-
2334
- class LocalObjectIdentifierValueBlock extends ValueBlock {
2335
- constructor({ value = EMPTY_STRING, ...parameters } = {}) {
2336
- super(parameters);
2337
- this.value = [];
2338
- if (value) {
2339
- this.fromString(value);
2340
- }
2341
- }
2342
- fromBER(inputBuffer, inputOffset, inputLength) {
2343
- let resultOffset = inputOffset;
2344
- while (inputLength > 0) {
2345
- const sidBlock = new LocalSidValueBlock();
2346
- resultOffset = sidBlock.fromBER(inputBuffer, resultOffset, inputLength);
2347
- if (resultOffset === -1) {
2348
- this.blockLength = 0;
2349
- this.error = sidBlock.error;
2350
- return resultOffset;
2351
- }
2352
- if (this.value.length === 0)
2353
- sidBlock.isFirstSid = true;
2354
- this.blockLength += sidBlock.blockLength;
2355
- inputLength -= sidBlock.blockLength;
2356
- this.value.push(sidBlock);
2357
- }
2358
- return resultOffset;
2359
- }
2360
- toBER(sizeOnly) {
2361
- const retBuffers = [];
2362
- for (let i = 0; i < this.value.length; i++) {
2363
- const valueBuf = this.value[i].toBER(sizeOnly);
2364
- if (valueBuf.byteLength === 0) {
2365
- this.error = this.value[i].error;
2366
- return EMPTY_BUFFER;
2367
- }
2368
- retBuffers.push(valueBuf);
2369
- }
2370
- return concat(retBuffers);
2371
- }
2372
- fromString(string) {
2373
- this.value = [];
2374
- let pos1 = 0;
2375
- let pos2 = 0;
2376
- let sid = "";
2377
- let flag = false;
2378
- do {
2379
- pos2 = string.indexOf(".", pos1);
2380
- if (pos2 === -1)
2381
- sid = string.substring(pos1);
2382
- else
2383
- sid = string.substring(pos1, pos2);
2384
- pos1 = pos2 + 1;
2385
- if (flag) {
2386
- const sidBlock = this.value[0];
2387
- let plus = 0;
2388
- switch (sidBlock.valueDec) {
2389
- case 0:
2390
- break;
2391
- case 1:
2392
- plus = 40;
2393
- break;
2394
- case 2:
2395
- plus = 80;
2396
- break;
2397
- default:
2398
- this.value = [];
2399
- return;
2400
- }
2401
- const parsedSID = parseInt(sid, 10);
2402
- if (isNaN(parsedSID))
2403
- return;
2404
- sidBlock.valueDec = parsedSID + plus;
2405
- flag = false;
2406
- }
2407
- else {
2408
- const sidBlock = new LocalSidValueBlock();
2409
- if (sid > Number.MAX_SAFE_INTEGER) {
2410
- assertBigInt();
2411
- const sidValue = BigInt(sid);
2412
- sidBlock.valueBigInt = sidValue;
2413
- }
2414
- else {
2415
- sidBlock.valueDec = parseInt(sid, 10);
2416
- if (isNaN(sidBlock.valueDec))
2417
- return;
2418
- }
2419
- if (!this.value.length) {
2420
- sidBlock.isFirstSid = true;
2421
- flag = true;
2422
- }
2423
- this.value.push(sidBlock);
2424
- }
2425
- } while (pos2 !== -1);
2426
- }
2427
- toString() {
2428
- let result = "";
2429
- let isHexOnly = false;
2430
- for (let i = 0; i < this.value.length; i++) {
2431
- isHexOnly = this.value[i].isHexOnly;
2432
- let sidStr = this.value[i].toString();
2433
- if (i !== 0)
2434
- result = `${result}.`;
2435
- if (isHexOnly) {
2436
- sidStr = `{${sidStr}}`;
2437
- if (this.value[i].isFirstSid)
2438
- result = `2.{${sidStr} - 80}`;
2439
- else
2440
- result += sidStr;
2441
- }
2442
- else
2443
- result += sidStr;
2444
- }
2445
- return result;
2446
- }
2447
- toJSON() {
2448
- const object = {
2449
- ...super.toJSON(),
2450
- value: this.toString(),
2451
- sidArray: [],
2452
- };
2453
- for (let i = 0; i < this.value.length; i++) {
2454
- object.sidArray.push(this.value[i].toJSON());
2455
- }
2456
- return object;
2457
- }
2458
- }
2459
- LocalObjectIdentifierValueBlock.NAME = "ObjectIdentifierValueBlock";
2460
-
2461
- var _a$m;
2462
- class ObjectIdentifier extends BaseBlock {
2463
- getValue() {
2464
- return this.valueBlock.toString();
2465
- }
2466
- setValue(value) {
2467
- this.valueBlock.fromString(value);
2468
- }
2469
- constructor(parameters = {}) {
2470
- super(parameters, LocalObjectIdentifierValueBlock);
2471
- this.idBlock.tagClass = 1;
2472
- this.idBlock.tagNumber = 6;
2473
- }
2474
- onAsciiEncoding() {
2475
- return `${this.constructor.NAME} : ${this.valueBlock.toString() || "empty"}`;
2476
- }
2477
- toJSON() {
2478
- return {
2479
- ...super.toJSON(),
2480
- value: this.getValue(),
2481
- };
2482
- }
2483
- }
2484
- _a$m = ObjectIdentifier;
2485
- (() => {
2486
- typeStore.ObjectIdentifier = _a$m;
2487
- })();
2488
- ObjectIdentifier.NAME = "OBJECT IDENTIFIER";
2489
-
2490
- class LocalRelativeSidValueBlock extends HexBlock(LocalBaseBlock) {
2491
- constructor({ valueDec = 0, ...parameters } = {}) {
2492
- super(parameters);
2493
- this.valueDec = valueDec;
2494
- }
2495
- fromBER(inputBuffer, inputOffset, inputLength) {
2496
- if (inputLength === 0)
2497
- return inputOffset;
2498
- const inputView = BufferSourceConverter.toUint8Array(inputBuffer);
2499
- if (!checkBufferParams(this, inputView, inputOffset, inputLength))
2500
- return -1;
2501
- const intBuffer = inputView.subarray(inputOffset, inputOffset + inputLength);
2502
- this.valueHexView = new Uint8Array(inputLength);
2503
- for (let i = 0; i < inputLength; i++) {
2504
- this.valueHexView[i] = intBuffer[i] & 0x7F;
2505
- this.blockLength++;
2506
- if ((intBuffer[i] & 0x80) === 0x00)
2507
- break;
2508
- }
2509
- const tempView = new Uint8Array(this.blockLength);
2510
- for (let i = 0; i < this.blockLength; i++)
2511
- tempView[i] = this.valueHexView[i];
2512
- this.valueHexView = tempView;
2513
- if ((intBuffer[this.blockLength - 1] & 0x80) !== 0x00) {
2514
- this.error = "End of input reached before message was fully decoded";
2515
- return -1;
2516
- }
2517
- if (this.valueHexView[0] === 0x00)
2518
- this.warnings.push("Needlessly long format of SID encoding");
2519
- if (this.blockLength <= 8)
2520
- this.valueDec = utilFromBase(this.valueHexView, 7);
2521
- else {
2522
- this.isHexOnly = true;
2523
- this.warnings.push("Too big SID for decoding, hex only");
2524
- }
2525
- return (inputOffset + this.blockLength);
2526
- }
2527
- toBER(sizeOnly) {
2528
- if (this.isHexOnly) {
2529
- if (sizeOnly)
2530
- return (new ArrayBuffer(this.valueHexView.byteLength));
2531
- const curView = this.valueHexView;
2532
- const retView = new Uint8Array(this.blockLength);
2533
- for (let i = 0; i < (this.blockLength - 1); i++)
2534
- retView[i] = curView[i] | 0x80;
2535
- retView[this.blockLength - 1] = curView[this.blockLength - 1];
2536
- return retView.buffer;
2537
- }
2538
- const encodedBuf = utilToBase(this.valueDec, 7);
2539
- if (encodedBuf.byteLength === 0) {
2540
- this.error = "Error during encoding SID value";
2541
- return EMPTY_BUFFER;
2542
- }
2543
- const retView = new Uint8Array(encodedBuf.byteLength);
2544
- if (!sizeOnly) {
2545
- const encodedView = new Uint8Array(encodedBuf);
2546
- const len = encodedBuf.byteLength - 1;
2547
- for (let i = 0; i < len; i++)
2548
- retView[i] = encodedView[i] | 0x80;
2549
- retView[len] = encodedView[len];
2550
- }
2551
- return retView.buffer;
2552
- }
2553
- toString() {
2554
- let result = "";
2555
- if (this.isHexOnly)
2556
- result = Convert.ToHex(this.valueHexView);
2557
- else {
2558
- result = this.valueDec.toString();
2559
- }
2560
- return result;
2561
- }
2562
- toJSON() {
2563
- return {
2564
- ...super.toJSON(),
2565
- valueDec: this.valueDec,
2566
- };
2567
- }
2568
- }
2569
- LocalRelativeSidValueBlock.NAME = "relativeSidBlock";
2570
-
2571
- class LocalRelativeObjectIdentifierValueBlock extends ValueBlock {
2572
- constructor({ value = EMPTY_STRING, ...parameters } = {}) {
2573
- super(parameters);
2574
- this.value = [];
2575
- if (value) {
2576
- this.fromString(value);
2577
- }
2578
- }
2579
- fromBER(inputBuffer, inputOffset, inputLength) {
2580
- let resultOffset = inputOffset;
2581
- while (inputLength > 0) {
2582
- const sidBlock = new LocalRelativeSidValueBlock();
2583
- resultOffset = sidBlock.fromBER(inputBuffer, resultOffset, inputLength);
2584
- if (resultOffset === -1) {
2585
- this.blockLength = 0;
2586
- this.error = sidBlock.error;
2587
- return resultOffset;
2588
- }
2589
- this.blockLength += sidBlock.blockLength;
2590
- inputLength -= sidBlock.blockLength;
2591
- this.value.push(sidBlock);
2592
- }
2593
- return resultOffset;
2594
- }
2595
- toBER(sizeOnly, _writer) {
2596
- const retBuffers = [];
2597
- for (let i = 0; i < this.value.length; i++) {
2598
- const valueBuf = this.value[i].toBER(sizeOnly);
2599
- if (valueBuf.byteLength === 0) {
2600
- this.error = this.value[i].error;
2601
- return EMPTY_BUFFER;
2602
- }
2603
- retBuffers.push(valueBuf);
2604
- }
2605
- return concat(retBuffers);
2606
- }
2607
- fromString(string) {
2608
- this.value = [];
2609
- let pos1 = 0;
2610
- let pos2 = 0;
2611
- let sid = "";
2612
- do {
2613
- pos2 = string.indexOf(".", pos1);
2614
- if (pos2 === -1)
2615
- sid = string.substring(pos1);
2616
- else
2617
- sid = string.substring(pos1, pos2);
2618
- pos1 = pos2 + 1;
2619
- const sidBlock = new LocalRelativeSidValueBlock();
2620
- sidBlock.valueDec = parseInt(sid, 10);
2621
- if (isNaN(sidBlock.valueDec))
2622
- return true;
2623
- this.value.push(sidBlock);
2624
- } while (pos2 !== -1);
2625
- return true;
2626
- }
2627
- toString() {
2628
- let result = "";
2629
- let isHexOnly = false;
2630
- for (let i = 0; i < this.value.length; i++) {
2631
- isHexOnly = this.value[i].isHexOnly;
2632
- let sidStr = this.value[i].toString();
2633
- if (i !== 0)
2634
- result = `${result}.`;
2635
- if (isHexOnly) {
2636
- sidStr = `{${sidStr}}`;
2637
- result += sidStr;
2638
- }
2639
- else
2640
- result += sidStr;
2641
- }
2642
- return result;
2643
- }
2644
- toJSON() {
2645
- const object = {
2646
- ...super.toJSON(),
2647
- value: this.toString(),
2648
- sidArray: [],
2649
- };
2650
- for (let i = 0; i < this.value.length; i++)
2651
- object.sidArray.push(this.value[i].toJSON());
2652
- return object;
2653
- }
2654
- }
2655
- LocalRelativeObjectIdentifierValueBlock.NAME = "RelativeObjectIdentifierValueBlock";
2656
-
2657
- var _a$l;
2658
- class RelativeObjectIdentifier extends BaseBlock {
2659
- getValue() {
2660
- return this.valueBlock.toString();
2661
- }
2662
- setValue(value) {
2663
- this.valueBlock.fromString(value);
2664
- }
2665
- constructor(parameters = {}) {
2666
- super(parameters, LocalRelativeObjectIdentifierValueBlock);
2667
- this.idBlock.tagClass = 1;
2668
- this.idBlock.tagNumber = 13;
2669
- }
2670
- onAsciiEncoding() {
2671
- return `${this.constructor.NAME} : ${this.valueBlock.toString() || "empty"}`;
2672
- }
2673
- toJSON() {
2674
- return {
2675
- ...super.toJSON(),
2676
- value: this.getValue(),
2677
- };
2678
- }
2679
- }
2680
- _a$l = RelativeObjectIdentifier;
2681
- (() => {
2682
- typeStore.RelativeObjectIdentifier = _a$l;
2683
- })();
2684
- RelativeObjectIdentifier.NAME = "RelativeObjectIdentifier";
2685
-
2686
- var _a$k;
2687
- class Sequence extends Constructed {
2688
- constructor(parameters = {}) {
2689
- super(parameters);
2690
- this.idBlock.tagClass = 1;
2691
- this.idBlock.tagNumber = 16;
2692
- }
2693
- }
2694
- _a$k = Sequence;
2695
- (() => {
2696
- typeStore.Sequence = _a$k;
2697
- })();
2698
- Sequence.NAME = "SEQUENCE";
2699
-
2700
- var _a$j;
2701
- class Set extends Constructed {
2702
- constructor(parameters = {}) {
2703
- super(parameters);
2704
- this.idBlock.tagClass = 1;
2705
- this.idBlock.tagNumber = 17;
2706
- }
2707
- }
2708
- _a$j = Set;
2709
- (() => {
2710
- typeStore.Set = _a$j;
2711
- })();
2712
- Set.NAME = "SET";
2713
-
2714
- class LocalStringValueBlock extends HexBlock(ValueBlock) {
2715
- constructor({ ...parameters } = {}) {
2716
- super(parameters);
2717
- this.isHexOnly = true;
2718
- this.value = EMPTY_STRING;
2719
- }
2720
- toJSON() {
2721
- return {
2722
- ...super.toJSON(),
2723
- value: this.value,
2724
- };
2725
- }
2726
- }
2727
- LocalStringValueBlock.NAME = "StringValueBlock";
2728
-
2729
- class LocalSimpleStringValueBlock extends LocalStringValueBlock {
2730
- }
2731
- LocalSimpleStringValueBlock.NAME = "SimpleStringValueBlock";
2732
-
2733
- class LocalSimpleStringBlock extends BaseStringBlock {
2734
- constructor({ ...parameters } = {}) {
2735
- super(parameters, LocalSimpleStringValueBlock);
2736
- }
2737
- fromBuffer(inputBuffer) {
2738
- this.valueBlock.value = String.fromCharCode.apply(null, BufferSourceConverter.toUint8Array(inputBuffer));
2739
- }
2740
- fromString(inputString) {
2741
- const strLen = inputString.length;
2742
- const view = this.valueBlock.valueHexView = new Uint8Array(strLen);
2743
- for (let i = 0; i < strLen; i++)
2744
- view[i] = inputString.charCodeAt(i);
2745
- this.valueBlock.value = inputString;
2746
- }
2747
- }
2748
- LocalSimpleStringBlock.NAME = "SIMPLE STRING";
2749
-
2750
- class LocalUtf8StringValueBlock extends LocalSimpleStringBlock {
2751
- fromBuffer(inputBuffer) {
2752
- this.valueBlock.valueHexView = BufferSourceConverter.toUint8Array(inputBuffer);
2753
- try {
2754
- this.valueBlock.value = Convert.ToUtf8String(inputBuffer);
2755
- }
2756
- catch (ex) {
2757
- this.warnings.push(`Error during "decodeURIComponent": ${ex}, using raw string`);
2758
- this.valueBlock.value = Convert.ToBinary(inputBuffer);
2759
- }
2760
- }
2761
- fromString(inputString) {
2762
- this.valueBlock.valueHexView = new Uint8Array(Convert.FromUtf8String(inputString));
2763
- this.valueBlock.value = inputString;
2764
- }
2765
- }
2766
- LocalUtf8StringValueBlock.NAME = "Utf8StringValueBlock";
2767
-
2768
- var _a$i;
2769
- class Utf8String extends LocalUtf8StringValueBlock {
2770
- constructor(parameters = {}) {
2771
- super(parameters);
2772
- this.idBlock.tagClass = 1;
2773
- this.idBlock.tagNumber = 12;
2774
- }
2775
- }
2776
- _a$i = Utf8String;
2777
- (() => {
2778
- typeStore.Utf8String = _a$i;
2779
- })();
2780
- Utf8String.NAME = "UTF8String";
2781
-
2782
- class LocalBmpStringValueBlock extends LocalSimpleStringBlock {
2783
- fromBuffer(inputBuffer) {
2784
- this.valueBlock.value = Convert.ToUtf16String(inputBuffer);
2785
- this.valueBlock.valueHexView = BufferSourceConverter.toUint8Array(inputBuffer);
2786
- }
2787
- fromString(inputString) {
2788
- this.valueBlock.value = inputString;
2789
- this.valueBlock.valueHexView = new Uint8Array(Convert.FromUtf16String(inputString));
2790
- }
2791
- }
2792
- LocalBmpStringValueBlock.NAME = "BmpStringValueBlock";
2793
-
2794
- var _a$h;
2795
- class BmpString extends LocalBmpStringValueBlock {
2796
- constructor({ ...parameters } = {}) {
2797
- super(parameters);
2798
- this.idBlock.tagClass = 1;
2799
- this.idBlock.tagNumber = 30;
2800
- }
2801
- }
2802
- _a$h = BmpString;
2803
- (() => {
2804
- typeStore.BmpString = _a$h;
2805
- })();
2806
- BmpString.NAME = "BMPString";
2807
-
2808
- class LocalUniversalStringValueBlock extends LocalSimpleStringBlock {
2809
- fromBuffer(inputBuffer) {
2810
- const copyBuffer = ArrayBuffer.isView(inputBuffer) ? inputBuffer.slice().buffer : inputBuffer.slice(0);
2811
- const valueView = new Uint8Array(copyBuffer);
2812
- for (let i = 0; i < valueView.length; i += 4) {
2813
- valueView[i] = valueView[i + 3];
2814
- valueView[i + 1] = valueView[i + 2];
2815
- valueView[i + 2] = 0x00;
2816
- valueView[i + 3] = 0x00;
2817
- }
2818
- this.valueBlock.value = String.fromCharCode.apply(null, new Uint32Array(copyBuffer));
2819
- }
2820
- fromString(inputString) {
2821
- const strLength = inputString.length;
2822
- const valueHexView = this.valueBlock.valueHexView = new Uint8Array(strLength * 4);
2823
- for (let i = 0; i < strLength; i++) {
2824
- const codeBuf = utilToBase(inputString.charCodeAt(i), 8);
2825
- const codeView = new Uint8Array(codeBuf);
2826
- if (codeView.length > 4)
2827
- continue;
2828
- const dif = 4 - codeView.length;
2829
- for (let j = (codeView.length - 1); j >= 0; j--)
2830
- valueHexView[i * 4 + j + dif] = codeView[j];
2831
- }
2832
- this.valueBlock.value = inputString;
2833
- }
2834
- }
2835
- LocalUniversalStringValueBlock.NAME = "UniversalStringValueBlock";
2836
-
2837
- var _a$g;
2838
- class UniversalString extends LocalUniversalStringValueBlock {
2839
- constructor({ ...parameters } = {}) {
2840
- super(parameters);
2841
- this.idBlock.tagClass = 1;
2842
- this.idBlock.tagNumber = 28;
2843
- }
2844
- }
2845
- _a$g = UniversalString;
2846
- (() => {
2847
- typeStore.UniversalString = _a$g;
2848
- })();
2849
- UniversalString.NAME = "UniversalString";
2850
-
2851
- var _a$f;
2852
- class NumericString extends LocalSimpleStringBlock {
2853
- constructor(parameters = {}) {
2854
- super(parameters);
2855
- this.idBlock.tagClass = 1;
2856
- this.idBlock.tagNumber = 18;
2857
- }
2858
- }
2859
- _a$f = NumericString;
2860
- (() => {
2861
- typeStore.NumericString = _a$f;
2862
- })();
2863
- NumericString.NAME = "NumericString";
2864
-
2865
- var _a$e;
2866
- class PrintableString extends LocalSimpleStringBlock {
2867
- constructor(parameters = {}) {
2868
- super(parameters);
2869
- this.idBlock.tagClass = 1;
2870
- this.idBlock.tagNumber = 19;
2871
- }
2872
- }
2873
- _a$e = PrintableString;
2874
- (() => {
2875
- typeStore.PrintableString = _a$e;
2876
- })();
2877
- PrintableString.NAME = "PrintableString";
2878
-
2879
- var _a$d;
2880
- class TeletexString extends LocalSimpleStringBlock {
2881
- constructor(parameters = {}) {
2882
- super(parameters);
2883
- this.idBlock.tagClass = 1;
2884
- this.idBlock.tagNumber = 20;
2885
- }
2886
- }
2887
- _a$d = TeletexString;
2888
- (() => {
2889
- typeStore.TeletexString = _a$d;
2890
- })();
2891
- TeletexString.NAME = "TeletexString";
2892
-
2893
- var _a$c;
2894
- class VideotexString extends LocalSimpleStringBlock {
2895
- constructor(parameters = {}) {
2896
- super(parameters);
2897
- this.idBlock.tagClass = 1;
2898
- this.idBlock.tagNumber = 21;
2899
- }
2900
- }
2901
- _a$c = VideotexString;
2902
- (() => {
2903
- typeStore.VideotexString = _a$c;
2904
- })();
2905
- VideotexString.NAME = "VideotexString";
2906
-
2907
- var _a$b;
2908
- class IA5String extends LocalSimpleStringBlock {
2909
- constructor(parameters = {}) {
2910
- super(parameters);
2911
- this.idBlock.tagClass = 1;
2912
- this.idBlock.tagNumber = 22;
2913
- }
2914
- }
2915
- _a$b = IA5String;
2916
- (() => {
2917
- typeStore.IA5String = _a$b;
2918
- })();
2919
- IA5String.NAME = "IA5String";
2920
-
2921
- var _a$a;
2922
- class GraphicString extends LocalSimpleStringBlock {
2923
- constructor(parameters = {}) {
2924
- super(parameters);
2925
- this.idBlock.tagClass = 1;
2926
- this.idBlock.tagNumber = 25;
2927
- }
2928
- }
2929
- _a$a = GraphicString;
2930
- (() => {
2931
- typeStore.GraphicString = _a$a;
2932
- })();
2933
- GraphicString.NAME = "GraphicString";
2934
-
2935
- var _a$9;
2936
- class VisibleString extends LocalSimpleStringBlock {
2937
- constructor(parameters = {}) {
2938
- super(parameters);
2939
- this.idBlock.tagClass = 1;
2940
- this.idBlock.tagNumber = 26;
2941
- }
2942
- }
2943
- _a$9 = VisibleString;
2944
- (() => {
2945
- typeStore.VisibleString = _a$9;
2946
- })();
2947
- VisibleString.NAME = "VisibleString";
2948
-
2949
- var _a$8;
2950
- class GeneralString extends LocalSimpleStringBlock {
2951
- constructor(parameters = {}) {
2952
- super(parameters);
2953
- this.idBlock.tagClass = 1;
2954
- this.idBlock.tagNumber = 27;
2955
- }
2956
- }
2957
- _a$8 = GeneralString;
2958
- (() => {
2959
- typeStore.GeneralString = _a$8;
2960
- })();
2961
- GeneralString.NAME = "GeneralString";
2962
-
2963
- var _a$7;
2964
- class CharacterString extends LocalSimpleStringBlock {
2965
- constructor(parameters = {}) {
2966
- super(parameters);
2967
- this.idBlock.tagClass = 1;
2968
- this.idBlock.tagNumber = 29;
2969
- }
2970
- }
2971
- _a$7 = CharacterString;
2972
- (() => {
2973
- typeStore.CharacterString = _a$7;
2974
- })();
2975
- CharacterString.NAME = "CharacterString";
2976
-
2977
- var _a$6;
2978
- class UTCTime extends VisibleString {
2979
- constructor({ value, valueDate, ...parameters } = {}) {
2980
- super(parameters);
2981
- this.year = 0;
2982
- this.month = 0;
2983
- this.day = 0;
2984
- this.hour = 0;
2985
- this.minute = 0;
2986
- this.second = 0;
2987
- if (value) {
2988
- this.fromString(value);
2989
- this.valueBlock.valueHexView = new Uint8Array(value.length);
2990
- for (let i = 0; i < value.length; i++)
2991
- this.valueBlock.valueHexView[i] = value.charCodeAt(i);
2992
- }
2993
- if (valueDate) {
2994
- this.fromDate(valueDate);
2995
- this.valueBlock.valueHexView = new Uint8Array(this.toBuffer());
2996
- }
2997
- this.idBlock.tagClass = 1;
2998
- this.idBlock.tagNumber = 23;
2999
- }
3000
- fromBuffer(inputBuffer) {
3001
- this.fromString(String.fromCharCode.apply(null, BufferSourceConverter.toUint8Array(inputBuffer)));
3002
- }
3003
- toBuffer() {
3004
- const str = this.toString();
3005
- const buffer = new ArrayBuffer(str.length);
3006
- const view = new Uint8Array(buffer);
3007
- for (let i = 0; i < str.length; i++)
3008
- view[i] = str.charCodeAt(i);
3009
- return buffer;
3010
- }
3011
- fromDate(inputDate) {
3012
- this.year = inputDate.getUTCFullYear();
3013
- this.month = inputDate.getUTCMonth() + 1;
3014
- this.day = inputDate.getUTCDate();
3015
- this.hour = inputDate.getUTCHours();
3016
- this.minute = inputDate.getUTCMinutes();
3017
- this.second = inputDate.getUTCSeconds();
3018
- }
3019
- toDate() {
3020
- return (new Date(Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second)));
3021
- }
3022
- fromString(inputString) {
3023
- const parser = /(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z/ig;
3024
- const parserArray = parser.exec(inputString);
3025
- if (parserArray === null) {
3026
- this.error = "Wrong input string for conversion";
3027
- return;
3028
- }
3029
- const year = parseInt(parserArray[1], 10);
3030
- if (year >= 50)
3031
- this.year = 1900 + year;
3032
- else
3033
- this.year = 2000 + year;
3034
- this.month = parseInt(parserArray[2], 10);
3035
- this.day = parseInt(parserArray[3], 10);
3036
- this.hour = parseInt(parserArray[4], 10);
3037
- this.minute = parseInt(parserArray[5], 10);
3038
- this.second = parseInt(parserArray[6], 10);
3039
- }
3040
- toString(encoding = "iso") {
3041
- if (encoding === "iso") {
3042
- const outputArray = new Array(7);
3043
- outputArray[0] = padNumber(((this.year < 2000) ? (this.year - 1900) : (this.year - 2000)), 2);
3044
- outputArray[1] = padNumber(this.month, 2);
3045
- outputArray[2] = padNumber(this.day, 2);
3046
- outputArray[3] = padNumber(this.hour, 2);
3047
- outputArray[4] = padNumber(this.minute, 2);
3048
- outputArray[5] = padNumber(this.second, 2);
3049
- outputArray[6] = "Z";
3050
- return outputArray.join("");
3051
- }
3052
- return super.toString(encoding);
3053
- }
3054
- onAsciiEncoding() {
3055
- return `${this.constructor.NAME} : ${this.toDate().toISOString()}`;
3056
- }
3057
- toJSON() {
3058
- return {
3059
- ...super.toJSON(),
3060
- year: this.year,
3061
- month: this.month,
3062
- day: this.day,
3063
- hour: this.hour,
3064
- minute: this.minute,
3065
- second: this.second,
3066
- };
3067
- }
3068
- }
3069
- _a$6 = UTCTime;
3070
- (() => {
3071
- typeStore.UTCTime = _a$6;
3072
- })();
3073
- UTCTime.NAME = "UTCTime";
3074
-
3075
- var _a$5;
3076
- class GeneralizedTime extends UTCTime {
3077
- constructor(parameters = {}) {
3078
- var _b;
3079
- super(parameters);
3080
- (_b = this.millisecond) !== null && _b !== void 0 ? _b : (this.millisecond = 0);
3081
- this.idBlock.tagClass = 1;
3082
- this.idBlock.tagNumber = 24;
3083
- }
3084
- fromDate(inputDate) {
3085
- super.fromDate(inputDate);
3086
- this.millisecond = inputDate.getUTCMilliseconds();
3087
- }
3088
- toDate() {
3089
- const utcDate = Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second, this.millisecond);
3090
- return (new Date(utcDate));
3091
- }
3092
- fromString(inputString) {
3093
- let isUTC = false;
3094
- let timeString = "";
3095
- let dateTimeString = "";
3096
- let fractionPart = 0;
3097
- let parser;
3098
- let hourDifference = 0;
3099
- let minuteDifference = 0;
3100
- if (inputString[inputString.length - 1] === "Z") {
3101
- timeString = inputString.substring(0, inputString.length - 1);
3102
- isUTC = true;
3103
- }
3104
- else {
3105
- const number = new Number(inputString[inputString.length - 1]);
3106
- if (isNaN(number.valueOf()))
3107
- throw new Error("Wrong input string for conversion");
3108
- timeString = inputString;
3109
- }
3110
- if (isUTC) {
3111
- if (timeString.indexOf("+") !== -1)
3112
- throw new Error("Wrong input string for conversion");
3113
- if (timeString.indexOf("-") !== -1)
3114
- throw new Error("Wrong input string for conversion");
3115
- }
3116
- else {
3117
- let multiplier = 1;
3118
- let differencePosition = timeString.indexOf("+");
3119
- let differenceString = "";
3120
- if (differencePosition === -1) {
3121
- differencePosition = timeString.indexOf("-");
3122
- multiplier = -1;
3123
- }
3124
- if (differencePosition !== -1) {
3125
- differenceString = timeString.substring(differencePosition + 1);
3126
- timeString = timeString.substring(0, differencePosition);
3127
- if ((differenceString.length !== 2) && (differenceString.length !== 4))
3128
- throw new Error("Wrong input string for conversion");
3129
- let number = parseInt(differenceString.substring(0, 2), 10);
3130
- if (isNaN(number.valueOf()))
3131
- throw new Error("Wrong input string for conversion");
3132
- hourDifference = multiplier * number;
3133
- if (differenceString.length === 4) {
3134
- number = parseInt(differenceString.substring(2, 4), 10);
3135
- if (isNaN(number.valueOf()))
3136
- throw new Error("Wrong input string for conversion");
3137
- minuteDifference = multiplier * number;
3138
- }
3139
- }
3140
- }
3141
- let fractionPointPosition = timeString.indexOf(".");
3142
- if (fractionPointPosition === -1)
3143
- fractionPointPosition = timeString.indexOf(",");
3144
- if (fractionPointPosition !== -1) {
3145
- const fractionPartCheck = new Number(`0${timeString.substring(fractionPointPosition)}`);
3146
- if (isNaN(fractionPartCheck.valueOf()))
3147
- throw new Error("Wrong input string for conversion");
3148
- fractionPart = fractionPartCheck.valueOf();
3149
- dateTimeString = timeString.substring(0, fractionPointPosition);
3150
- }
3151
- else
3152
- dateTimeString = timeString;
3153
- switch (true) {
3154
- case (dateTimeString.length === 8):
3155
- parser = /(\d{4})(\d{2})(\d{2})/ig;
3156
- if (fractionPointPosition !== -1)
3157
- throw new Error("Wrong input string for conversion");
3158
- break;
3159
- case (dateTimeString.length === 10):
3160
- parser = /(\d{4})(\d{2})(\d{2})(\d{2})/ig;
3161
- if (fractionPointPosition !== -1) {
3162
- let fractionResult = 60 * fractionPart;
3163
- this.minute = Math.floor(fractionResult);
3164
- fractionResult = 60 * (fractionResult - this.minute);
3165
- this.second = Math.floor(fractionResult);
3166
- fractionResult = 1000 * (fractionResult - this.second);
3167
- this.millisecond = Math.floor(fractionResult);
3168
- }
3169
- break;
3170
- case (dateTimeString.length === 12):
3171
- parser = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/ig;
3172
- if (fractionPointPosition !== -1) {
3173
- let fractionResult = 60 * fractionPart;
3174
- this.second = Math.floor(fractionResult);
3175
- fractionResult = 1000 * (fractionResult - this.second);
3176
- this.millisecond = Math.floor(fractionResult);
3177
- }
3178
- break;
3179
- case (dateTimeString.length === 14):
3180
- parser = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ig;
3181
- if (fractionPointPosition !== -1) {
3182
- const fractionResult = 1000 * fractionPart;
3183
- this.millisecond = Math.floor(fractionResult);
3184
- }
3185
- break;
3186
- default:
3187
- throw new Error("Wrong input string for conversion");
3188
- }
3189
- const parserArray = parser.exec(dateTimeString);
3190
- if (parserArray === null)
3191
- throw new Error("Wrong input string for conversion");
3192
- for (let j = 1; j < parserArray.length; j++) {
3193
- switch (j) {
3194
- case 1:
3195
- this.year = parseInt(parserArray[j], 10);
3196
- break;
3197
- case 2:
3198
- this.month = parseInt(parserArray[j], 10);
3199
- break;
3200
- case 3:
3201
- this.day = parseInt(parserArray[j], 10);
3202
- break;
3203
- case 4:
3204
- this.hour = parseInt(parserArray[j], 10) + hourDifference;
3205
- break;
3206
- case 5:
3207
- this.minute = parseInt(parserArray[j], 10) + minuteDifference;
3208
- break;
3209
- case 6:
3210
- this.second = parseInt(parserArray[j], 10);
3211
- break;
3212
- default:
3213
- throw new Error("Wrong input string for conversion");
3214
- }
3215
- }
3216
- if (isUTC === false) {
3217
- const tempDate = new Date(this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond);
3218
- this.year = tempDate.getUTCFullYear();
3219
- this.month = tempDate.getUTCMonth();
3220
- this.day = tempDate.getUTCDay();
3221
- this.hour = tempDate.getUTCHours();
3222
- this.minute = tempDate.getUTCMinutes();
3223
- this.second = tempDate.getUTCSeconds();
3224
- this.millisecond = tempDate.getUTCMilliseconds();
3225
- }
3226
- }
3227
- toString(encoding = "iso") {
3228
- if (encoding === "iso") {
3229
- const outputArray = [];
3230
- outputArray.push(padNumber(this.year, 4));
3231
- outputArray.push(padNumber(this.month, 2));
3232
- outputArray.push(padNumber(this.day, 2));
3233
- outputArray.push(padNumber(this.hour, 2));
3234
- outputArray.push(padNumber(this.minute, 2));
3235
- outputArray.push(padNumber(this.second, 2));
3236
- if (this.millisecond !== 0) {
3237
- outputArray.push(".");
3238
- outputArray.push(padNumber(this.millisecond, 3));
3239
- }
3240
- outputArray.push("Z");
3241
- return outputArray.join("");
3242
- }
3243
- return super.toString(encoding);
3244
- }
3245
- toJSON() {
3246
- return {
3247
- ...super.toJSON(),
3248
- millisecond: this.millisecond,
3249
- };
3250
- }
3251
- }
3252
- _a$5 = GeneralizedTime;
3253
- (() => {
3254
- typeStore.GeneralizedTime = _a$5;
3255
- })();
3256
- GeneralizedTime.NAME = "GeneralizedTime";
3257
-
3258
- var _a$4;
3259
- class DATE extends Utf8String {
3260
- constructor(parameters = {}) {
3261
- super(parameters);
3262
- this.idBlock.tagClass = 1;
3263
- this.idBlock.tagNumber = 31;
3264
- }
3265
- }
3266
- _a$4 = DATE;
3267
- (() => {
3268
- typeStore.DATE = _a$4;
3269
- })();
3270
- DATE.NAME = "DATE";
3271
-
3272
- var _a$3;
3273
- class TimeOfDay extends Utf8String {
3274
- constructor(parameters = {}) {
3275
- super(parameters);
3276
- this.idBlock.tagClass = 1;
3277
- this.idBlock.tagNumber = 32;
3278
- }
3279
- }
3280
- _a$3 = TimeOfDay;
3281
- (() => {
3282
- typeStore.TimeOfDay = _a$3;
3283
- })();
3284
- TimeOfDay.NAME = "TimeOfDay";
3285
-
3286
- var _a$2;
3287
- class DateTime extends Utf8String {
3288
- constructor(parameters = {}) {
3289
- super(parameters);
3290
- this.idBlock.tagClass = 1;
3291
- this.idBlock.tagNumber = 33;
3292
- }
3293
- }
3294
- _a$2 = DateTime;
3295
- (() => {
3296
- typeStore.DateTime = _a$2;
3297
- })();
3298
- DateTime.NAME = "DateTime";
3299
-
3300
- var _a$1;
3301
- class Duration extends Utf8String {
3302
- constructor(parameters = {}) {
3303
- super(parameters);
3304
- this.idBlock.tagClass = 1;
3305
- this.idBlock.tagNumber = 34;
3306
- }
3307
- }
3308
- _a$1 = Duration;
3309
- (() => {
3310
- typeStore.Duration = _a$1;
3311
- })();
3312
- Duration.NAME = "Duration";
3313
-
3314
- var _a;
3315
- class TIME extends Utf8String {
3316
- constructor(parameters = {}) {
3317
- super(parameters);
3318
- this.idBlock.tagClass = 1;
3319
- this.idBlock.tagNumber = 14;
3320
- }
3321
- }
3322
- _a = TIME;
3323
- (() => {
3324
- typeStore.TIME = _a;
3325
- })();
3326
- TIME.NAME = "TIME";
3327
-
3328
- /**
3329
- * Converts text to an array buffer.
3330
- * @param text The text.
3331
- * @returns An array buffer.
3332
- */
3333
- function textToBuffer(text) {
3334
- return Uint8Array.from(Array.from(text).map(function (letter) { return letter.charCodeAt(0); })).buffer;
3335
- }
3336
- /**
3337
- * Converts an array buffer to the corresponding text.
3338
- * @param buffer An array buffer.
3339
- * @returns The corresponding text.
3340
- */
3341
- function bufferToText(buffer) {
3342
- return String.fromCharCode.apply(null, new Uint8Array(buffer));
3343
- }
3344
- /**
3345
- * Encodes text as base 64.
3346
- * @param text The text.
3347
- * @returns The encoded base 64.
3348
- */
3349
- function textToBase64(text) {
3350
- return Buffer.from(text, 'binary').toString('base64');
3351
- }
3352
- /**
3353
- * Decodes base 64 to text.
3354
- * @param base64 The base 64.
3355
- * @returns The decoded text.
3356
- */
3357
- function base64ToText(base64) {
3358
- return Buffer.from(base64, 'base64').toString('binary');
3359
- }
3360
- /**
3361
- * Encodes an array buffer as base 64.
3362
- * @param buffer An array buffer.
3363
- * @returns The encoded base 64.
3364
- */
3365
- function bufferToBase64(buffer) {
3366
- return textToBase64(bufferToText(buffer));
3367
- }
3368
- /**
3369
- * Encodes an array buffer as base 64 url.
3370
- * @param buffer An array buffer.
3371
- * @returns The encoded base 64 url.
3372
- */
3373
- function bufferToBase64Url(buffer) {
3374
- return textToBase64Url(bufferToText(buffer));
3375
- }
3376
- /**
3377
- * Encodes text as base 64 url.
3378
- * @param text The text.
3379
- * @returns The encoded base 64 url.
3380
- */
3381
- function textToBase64Url(text) {
3382
- return textToBase64(text).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
3383
- }
3384
-
3385
- var crypto = require('node:crypto');
3386
- var pkijs = require('pkijs');
3387
- pkijs.setEngine('OpenSSL', crypto, new pkijs.CryptoEngine({ name: '', crypto: crypto, subtle: crypto.subtle }));
3388
- var DEF_ALGO = {
3389
- name: 'RSASSA-PKCS1-v1_5',
3390
- modulusLength: 2048,
3391
- publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
3392
- hash: { name: 'SHA-256' },
3393
- };
3394
- function randomString() {
3395
- return __awaiter(this, void 0, void 0, function () {
3396
- var bytes;
3397
- return __generator(this, function (_a) {
3398
- bytes = new Uint8Array(24);
3399
- crypto.getRandomValues(bytes);
3400
- return [2 /*return*/, bufferToBase64Url(bytes.buffer)];
3401
- });
3402
- });
3403
- }
3404
- function gen() {
3405
- return __awaiter(this, void 0, void 0, function () {
3406
- var keys;
3407
- var _a;
3408
- return __generator(this, function (_b) {
3409
- switch (_b.label) {
3410
- case 0: return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
3411
- case 1:
3412
- keys = _b.sent();
3413
- _a = {};
3414
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
3415
- case 2:
3416
- _a.publicJwk = _b.sent();
3417
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
3418
- case 3: return [2 /*return*/, (_a.privateJwk = _b.sent(),
3419
- _a)];
3420
- }
3421
- });
3422
- });
3423
- }
3424
- function sign(text, privateJwk) {
3425
- return __awaiter(this, void 0, void 0, function () {
3426
- var cKey, buffer, signed;
3427
- return __generator(this, function (_a) {
3428
- switch (_a.label) {
3429
- case 0: return [4 /*yield*/, crypto.subtle.importKey('jwk', privateJwk, DEF_ALGO, true, ['sign'])];
3430
- case 1:
3431
- cKey = _a.sent(), buffer = textToBuffer(text);
3432
- return [4 /*yield*/, crypto.subtle.sign(DEF_ALGO.name, cKey, buffer)];
3433
- case 2:
3434
- signed = _a.sent();
3435
- return [2 /*return*/, bufferToBase64Url(signed)];
3436
- }
3437
- });
3438
- });
3439
- }
3440
- function digest(text) {
3441
- return __awaiter(this, void 0, void 0, function () {
3442
- var buffer, digest;
3443
- return __generator(this, function (_a) {
3444
- switch (_a.label) {
3445
- case 0:
3446
- buffer = textToBuffer(text);
3447
- return [4 /*yield*/, crypto.subtle.digest('SHA-256', buffer)];
3448
- case 1:
3449
- digest = _a.sent();
3450
- return [2 /*return*/, bufferToBase64Url(digest)];
3451
- }
3452
- });
3453
- });
3454
- }
3455
- //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS10ComplexExample/es6.js
3456
- function csr(params) {
3457
- return __awaiter(this, void 0, void 0, function () {
3458
- var pkcs10, keys, publicKey, toDigest, pubkeyhash_sha1, privateKey, pkcs10_schema, pkcs10_encoded, exportedPkcs8;
3459
- var _a;
3460
- return __generator(this, function (_b) {
3461
- switch (_b.label) {
3462
- case 0:
3463
- pkcs10 = new pkijs.CertificationRequest();
3464
- pkcs10.version = 0;
3465
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3466
- type: '2.5.4.3', // CN=
3467
- value: new PrintableString({ value: params.domains[0] }),
3468
- }));
3469
- if (params.country) {
3470
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3471
- type: '2.5.4.6', // C=
3472
- value: new PrintableString({ value: params.country }),
3473
- }));
3474
- }
3475
- if (params.town) {
3476
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3477
- type: '2.5.4.7', // L=
3478
- value: new PrintableString({ value: params.town }),
3479
- }));
3480
- }
3481
- if (params.county) {
3482
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3483
- type: '2.5.4.8', // S=
3484
- value: new PrintableString({ value: params.county }),
3485
- }));
3486
- }
3487
- if (params.company) {
3488
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3489
- type: '2.5.4.10', // O=
3490
- value: new PrintableString({ value: params.company }),
3491
- }));
3492
- }
3493
- if (params.department) {
3494
- pkcs10.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({
3495
- type: '2.5.4.11', // OU=
3496
- value: new PrintableString({ value: params.department }),
3497
- }));
3498
- }
3499
- return [4 /*yield*/, crypto.subtle.generateKey(DEF_ALGO, true, ['sign'])];
3500
- case 1:
3501
- keys = _b.sent();
3502
- publicKey = keys.publicKey;
3503
- return [4 /*yield*/, pkcs10.subjectPublicKeyInfo.importKey(publicKey)];
3504
- case 2:
3505
- _b.sent();
3506
- toDigest = pkcs10.subjectPublicKeyInfo.subjectPublicKey.valueBlock.valueHex;
3507
- return [4 /*yield*/, crypto.subtle.digest('SHA-1', toDigest)];
3508
- case 3:
3509
- pubkeyhash_sha1 = _b.sent();
3510
- //pubkeyhash_sha256 = await crypto.subtle.digest('SHA-256', toDigest);
3511
- pkcs10.attributes = [];
3512
- pkcs10.attributes.push(new pkijs.Attribute({
3513
- type: '1.2.840.113549.1.9.14', // pkcs-9-at-extensionRequest
3514
- values: [
3515
- new pkijs.Extensions({
3516
- extensions: [
3517
- new pkijs.Extension({
3518
- extnID: '2.5.29.14',
3519
- critical: false,
3520
- extnValue: new OctetString({ valueHex: pubkeyhash_sha1 }).toBER(false),
3521
- }),
3522
- new pkijs.Extension({
3523
- extnID: '2.5.29.17',
3524
- critical: false,
3525
- extnValue: new pkijs.GeneralNames({
3526
- names: params.domains.map(function (dom) {
3527
- return new pkijs.GeneralName({
3528
- type: 2,
3529
- value: dom,
3530
- });
3531
- }),
3532
- })
3533
- .toSchema()
3534
- .toBER(false),
3535
- }),
3536
- ],
3537
- }).toSchema(),
3538
- ],
3539
- }));
3540
- privateKey = keys.privateKey;
3541
- return [4 /*yield*/, pkcs10.sign(privateKey, 'SHA-256')];
3542
- case 4:
3543
- _b.sent(), pkcs10_schema = pkcs10.toSchema(), pkcs10_encoded = pkcs10_schema.toBER(false);
3544
- return [4 /*yield*/, crypto.subtle.exportKey('pkcs8', keys.privateKey)];
3545
- case 5:
3546
- exportedPkcs8 = _b.sent();
3547
- _a = {
3548
- pem: bufferToPem(pkcs10_encoded, 'CERTIFICATE REQUEST'),
3549
- der: bufferToBase64Url(pkcs10_encoded),
3550
- pkcs8_pem: bufferToPem(exportedPkcs8, 'PRIVATE KEY'),
3551
- pkcs8_b64: bufferToBase64(exportedPkcs8)
3552
- };
3553
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.privateKey)];
3554
- case 6:
3555
- _a.privateJwk = _b.sent();
3556
- return [4 /*yield*/, crypto.subtle.exportKey('jwk', keys.publicKey)];
3557
- case 7: return [2 /*return*/, (_a.publicJwk = _b.sent(),
3558
- _a)];
3559
- }
3560
- });
3561
- });
3562
- }
3563
- //https://github.com/PeculiarVentures/PKI.js/blob/master/examples/PKCS12SimpleExample/es6.js
3564
- function pfx(friendlyName_1, cert_b64_1, key_b64_1, password_1) {
3565
- return __awaiter(this, arguments, void 0, function (friendlyName, cert_b64, key_b64, password, hash) {
3566
- var asn1_cer, asn1_key, objc_cer, objc_key, pkcs12;
3567
- if (hash === void 0) { hash = 'SHA-256'; }
3568
- return __generator(this, function (_a) {
3569
- switch (_a.label) {
3570
- case 0:
3571
- asn1_cer = fromBER(textToBuffer(base64ToText(cert_b64))), asn1_key = fromBER(textToBuffer(base64ToText(key_b64)));
3572
- objc_cer = new pkijs.Certificate({ schema: asn1_cer.result }), objc_key = new pkijs.PrivateKeyInfo({ schema: asn1_key.result });
3573
- pkcs12 = new pkijs.PFX({
3574
- parsedValue: {
3575
- integrityMode: 0, // Password-Based Integrity Mode
3576
- authenticatedSafe: new pkijs.AuthenticatedSafe({
3577
- parsedValue: {
3578
- safeContents: [
3579
- {
3580
- privacyMode: 0, // 'No Privacy' mode
3581
- value: new pkijs.SafeContents({
3582
- safeBags: [
3583
- new pkijs.SafeBag({
3584
- bagId: '1.2.840.113549.1.12.10.1.1',
3585
- bagValue: objc_key,
3586
- }),
3587
- new pkijs.SafeBag({
3588
- bagId: '1.2.840.113549.1.12.10.1.3',
3589
- bagValue: new pkijs.CertBag({
3590
- parsedValue: objc_cer,
3591
- }),
3592
- bagAttributes: [
3593
- new pkijs.Attribute({
3594
- type: '1.2.840.113549.1.9.20', // friendlyName
3595
- values: [new BmpString({ value: friendlyName })],
3596
- }),
3597
- ],
3598
- }),
3599
- ],
3600
- }),
3601
- },
3602
- ],
3603
- },
3604
- }),
3605
- },
3606
- });
3607
- return [4 /*yield*/, pkcs12.parsedValue.authenticatedSafe.makeInternalValues({
3608
- safeContents: [
3609
- {
3610
- /* Empty as 'No Privacy' for SafeContents */
3611
- },
3612
- ],
3613
- })];
3614
- case 1:
3615
- _a.sent();
3616
- return [4 /*yield*/, pkcs12.makeInternalValues({
3617
- password: textToBuffer(password),
3618
- iterations: 1000,
3619
- pbkdf2HashAlgorithm: hash,
3620
- hmacHashAlgorithm: hash,
3621
- })];
3622
- case 2:
3623
- _a.sent();
3624
- return [2 /*return*/, pkcs12.toSchema().toBER(false)];
3625
- }
3626
- });
3627
- });
3628
- }
3629
- function pemToBase64Parts(pem) {
3630
- return pem
3631
- .split(/-----[\w\s]+-----/)
3632
- .map(function (p) { return p.replace(/\s/g, ''); })
3633
- .filter(function (p) { return p; });
3634
- }
3635
- function base64ToPem(base64, title) {
3636
- var string_length = base64.length;
3637
- var result_string = '';
3638
- for (var i = 0, count = 0; i < string_length; i++, count++) {
3639
- if (count > 63) {
3640
- result_string += '\r\n';
3641
- count = 0;
3642
- }
3643
- result_string += base64[i];
3644
- }
3645
- title =
3646
- (title || '')
3647
- .replace(/[^\w\s]+/, '')
3648
- .trim()
3649
- .toUpperCase() || 'CERTIFICATE';
3650
- return "-----BEGIN ".concat(title, "-----\r\n").concat(result_string, "\r\n-----END ").concat(title, "-----\r\n");
3651
- }
3652
- function bufferToPem(pkcs10_buf, title) {
3653
- var base64 = bufferToBase64(pkcs10_buf);
3654
- return base64ToPem(base64, title);
3655
- }
3656
-
3657
- exports.base64ToPem = base64ToPem;
3658
- exports.bufferToPem = bufferToPem;
3659
- exports.csr = csr;
3660
- exports.digest = digest;
3661
- exports.gen = gen;
3662
- exports.pemToBase64Parts = pemToBase64Parts;
3663
- exports.pfx = pfx;
3664
- exports.randomString = randomString;
3665
- exports.sign = sign;
3666
-
3667
- }));