@lapo/asn1js 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/asn1.js +41 -19
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ credits
|
|
|
62
62
|
- extended tag support added by [Péter Budai](https://www.peterbudai.eu/)
|
|
63
63
|
- patches by [Gergely Nagy](https://github.com/ngg)
|
|
64
64
|
- Relative OID support added by [Mistial Developer](https://github.com/mistial-dev)
|
|
65
|
-
- dark mode
|
|
65
|
+
- dark mode and other UI improvements by [Oliver Burgmaier](https://github.com/olibu/)
|
|
66
66
|
- patches by [Nicolai Søborg](https://github.com/NicolaiSoeborg)
|
|
67
67
|
|
|
68
68
|
links
|
package/asn1.js
CHANGED
|
@@ -72,17 +72,21 @@ class Stream {
|
|
|
72
72
|
if (pos === undefined)
|
|
73
73
|
pos = this.pos++;
|
|
74
74
|
if (pos >= this.enc.length)
|
|
75
|
-
throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length;
|
|
75
|
+
throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length);
|
|
76
76
|
return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos];
|
|
77
77
|
}
|
|
78
78
|
hexByte(b) {
|
|
79
79
|
return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
|
|
80
80
|
}
|
|
81
|
-
|
|
81
|
+
/** Hexadecimal dump.
|
|
82
|
+
* @param type 'raw', 'byte' or 'dump' */
|
|
83
|
+
hexDump(start, end, type = 'dump') {
|
|
82
84
|
let s = '';
|
|
83
85
|
for (let i = start; i < end; ++i) {
|
|
86
|
+
if (type == 'byte' && i > start)
|
|
87
|
+
s += ' ';
|
|
84
88
|
s += this.hexByte(this.get(i));
|
|
85
|
-
if (
|
|
89
|
+
if (type == 'dump')
|
|
86
90
|
switch (i & 0xF) {
|
|
87
91
|
case 0x7: s += ' '; break;
|
|
88
92
|
case 0xF: s += '\n'; break;
|
|
@@ -190,7 +194,7 @@ class Stream {
|
|
|
190
194
|
let s = this.parseStringISO(start, end).str,
|
|
191
195
|
m = (shortYear ? reTimeS : reTimeL).exec(s);
|
|
192
196
|
if (!m)
|
|
193
|
-
|
|
197
|
+
throw new Error('Unrecognized time: ' + s);
|
|
194
198
|
if (shortYear) {
|
|
195
199
|
// to avoid querying the timer, use the fixed range [1970, 2069]
|
|
196
200
|
// it will conform with ITU X.400 [-10, +40] sliding window until 2030
|
|
@@ -245,7 +249,7 @@ class Stream {
|
|
|
245
249
|
parseBitString(start, end, maxLength) {
|
|
246
250
|
let unusedBits = this.get(start);
|
|
247
251
|
if (unusedBits > 7)
|
|
248
|
-
throw 'Invalid BitString with unusedBits=' + unusedBits;
|
|
252
|
+
throw new Error('Invalid BitString with unusedBits=' + unusedBits);
|
|
249
253
|
let lenBit = ((end - start - 1) << 3) - unusedBits,
|
|
250
254
|
s = '';
|
|
251
255
|
for (let i = start + 1; i < end; ++i) {
|
|
@@ -368,7 +372,7 @@ class ASN1Tag {
|
|
|
368
372
|
|
|
369
373
|
export class ASN1 {
|
|
370
374
|
constructor(stream, header, length, tag, tagLen, sub) {
|
|
371
|
-
if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
|
|
375
|
+
if (!(tag instanceof ASN1Tag)) throw new Error('Invalid tag value.');
|
|
372
376
|
this.stream = stream;
|
|
373
377
|
this.header = header;
|
|
374
378
|
this.length = length;
|
|
@@ -485,7 +489,16 @@ export class ASN1 {
|
|
|
485
489
|
}
|
|
486
490
|
toPrettyString(indent) {
|
|
487
491
|
if (indent === undefined) indent = '';
|
|
488
|
-
let s = indent
|
|
492
|
+
let s = indent;
|
|
493
|
+
if (this.def) {
|
|
494
|
+
if (this.def.id)
|
|
495
|
+
s += this.def.id + ' ';
|
|
496
|
+
if (this.def.name && this.def.name != this.typeName().replace(/_/g, ' '))
|
|
497
|
+
s+= this.def.name + ' ';
|
|
498
|
+
if (this.def.mismatch)
|
|
499
|
+
s += '[?] ';
|
|
500
|
+
}
|
|
501
|
+
s += this.typeName() + ' @' + this.stream.pos;
|
|
489
502
|
if (this.length >= 0)
|
|
490
503
|
s += '+';
|
|
491
504
|
s += this.length;
|
|
@@ -517,9 +530,12 @@ export class ASN1 {
|
|
|
517
530
|
posLen() {
|
|
518
531
|
return this.stream.pos + this.tagLen;
|
|
519
532
|
}
|
|
520
|
-
|
|
521
|
-
|
|
533
|
+
/** Hexadecimal dump of the node.
|
|
534
|
+
* @param type 'raw', 'byte' or 'dump' */
|
|
535
|
+
toHexString(type = 'raw') {
|
|
536
|
+
return this.stream.hexDump(this.posStart(), this.posEnd(), type);
|
|
522
537
|
}
|
|
538
|
+
/** Base64 dump of the node. */
|
|
523
539
|
toB64String() {
|
|
524
540
|
return this.stream.b64Dump(this.posStart(), this.posEnd());
|
|
525
541
|
}
|
|
@@ -531,7 +547,7 @@ export class ASN1 {
|
|
|
531
547
|
if (len === 0) // long form with length 0 is a special case
|
|
532
548
|
return null; // undefined length
|
|
533
549
|
if (len > 6) // no reason to use Int10, as it would be a huge buffer anyways
|
|
534
|
-
throw 'Length over 48 bits not supported at position ' + (stream.pos - 1);
|
|
550
|
+
throw new Error('Length over 48 bits not supported at position ' + (stream.pos - 1));
|
|
535
551
|
buf = 0;
|
|
536
552
|
for (let i = 0; i < len; ++i)
|
|
537
553
|
buf = (buf * 256) + stream.get();
|
|
@@ -539,7 +555,7 @@ export class ASN1 {
|
|
|
539
555
|
}
|
|
540
556
|
static decode(stream, offset, type = ASN1) {
|
|
541
557
|
if (!(type == ASN1 || type.prototype instanceof ASN1))
|
|
542
|
-
throw 'Must pass a class that extends ASN1';
|
|
558
|
+
throw new Error('Must pass a class that extends ASN1');
|
|
543
559
|
if (!(stream instanceof Stream))
|
|
544
560
|
stream = new Stream(stream, offset || 0);
|
|
545
561
|
let streamStart = new Stream(stream),
|
|
@@ -555,11 +571,11 @@ export class ASN1 {
|
|
|
555
571
|
// definite length
|
|
556
572
|
let end = start + len;
|
|
557
573
|
if (end > stream.enc.length)
|
|
558
|
-
throw 'Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream';
|
|
574
|
+
throw new Error('Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream');
|
|
559
575
|
while (stream.pos < end)
|
|
560
576
|
sub[sub.length] = type.decode(stream);
|
|
561
577
|
if (stream.pos != end)
|
|
562
|
-
throw 'Content size is not correct for container at offset ' + start;
|
|
578
|
+
throw new Error('Content size is not correct for container at offset ' + start);
|
|
563
579
|
} else {
|
|
564
580
|
// undefined length
|
|
565
581
|
try {
|
|
@@ -571,7 +587,7 @@ export class ASN1 {
|
|
|
571
587
|
}
|
|
572
588
|
len = start - stream.pos; // undefined lengths are represented as negative values
|
|
573
589
|
} catch (e) {
|
|
574
|
-
throw 'Exception while decoding undefined length content at offset ' + start + ': ' + e;
|
|
590
|
+
throw new Error('Exception while decoding undefined length content at offset ' + start + ': ' + e);
|
|
575
591
|
}
|
|
576
592
|
}
|
|
577
593
|
};
|
|
@@ -583,11 +599,17 @@ export class ASN1 {
|
|
|
583
599
|
try {
|
|
584
600
|
if (tag.tagNumber == 0x03)
|
|
585
601
|
if (stream.get() != 0)
|
|
586
|
-
throw 'BIT STRINGs with unused bits cannot encapsulate.';
|
|
602
|
+
throw new Error('BIT STRINGs with unused bits cannot encapsulate.');
|
|
587
603
|
getSub();
|
|
588
|
-
for (let
|
|
589
|
-
if (
|
|
590
|
-
throw 'EOC is not supposed to be actual content.';
|
|
604
|
+
for (let s of sub) {
|
|
605
|
+
if (s.tag.isEOC())
|
|
606
|
+
throw new Error('EOC is not supposed to be actual content.');
|
|
607
|
+
try {
|
|
608
|
+
s.content();
|
|
609
|
+
} catch (e) {
|
|
610
|
+
throw new Error('Unable to parse content: ' + e);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
591
613
|
} catch (e) {
|
|
592
614
|
// but silently ignore when they don't
|
|
593
615
|
sub = null;
|
|
@@ -596,7 +618,7 @@ export class ASN1 {
|
|
|
596
618
|
}
|
|
597
619
|
if (sub === null) {
|
|
598
620
|
if (len === null)
|
|
599
|
-
throw "We can't skip over an invalid tag with undefined length at offset " + start;
|
|
621
|
+
throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
|
|
600
622
|
stream.pos = start + Math.abs(len);
|
|
601
623
|
}
|
|
602
624
|
return new type(streamStart, header, len, tag, tagLen, sub);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lapo/asn1js",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "asn1.js",
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
"homepage": "https://lapo.it/asn1js/",
|
|
16
16
|
"files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "oids.js" ],
|
|
17
17
|
"scripts": {
|
|
18
|
-
"lint": "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js index.js parseRFC.js dumpASN1.js",
|
|
18
|
+
"lint": "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js",
|
|
19
19
|
"lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
|
|
20
20
|
"serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
|
|
21
|
-
"test": "node test"
|
|
21
|
+
"test": "node test",
|
|
22
|
+
"testdefs": "node testDefs"
|
|
22
23
|
},
|
|
23
24
|
"engines": {
|
|
24
25
|
"node": ">=12.20.0"
|
|
@@ -81,6 +82,11 @@
|
|
|
81
82
|
"parserOptions": {
|
|
82
83
|
"ecmaVersion": 2021
|
|
83
84
|
}
|
|
85
|
+
}, {
|
|
86
|
+
"files": [ "testDefs.js" ],
|
|
87
|
+
"parserOptions": {
|
|
88
|
+
"ecmaVersion": 2022
|
|
89
|
+
}
|
|
84
90
|
}
|
|
85
91
|
]
|
|
86
92
|
}
|