@lapo/asn1js 2.0.4 → 2.0.6

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  ISC License
2
2
 
3
- Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
3
+ Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any
6
6
  purpose with or without fee is hereby granted, provided that the above
package/README.md CHANGED
@@ -3,7 +3,7 @@ asn1js
3
3
 
4
4
  asn1js is a JavaScript generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.
5
5
 
6
- An example page that can decode Base64-encoded (raw base64, PEM armoring and `begin-base64` are recognized) or Hex-encoded (or local files with some browsers) is included and can be used both [online on the official website](https://lapo.it/asn1js/) or [offline (ZIP file)](https://lapo.it/asn1js/asn1js.zip).
6
+ An example page that can decode Base64-encoded (raw base64, PEM armoring and `begin-base64` are recognized) or Hex-encoded (or local files with some browsers) is included and can be used both [online on the official website](https://asn1js.eu/) or [offline (ZIP file)](https://lapo.it/asn1js/asn1js.zip) by opening `index-local.html`.
7
7
 
8
8
  Usage with `nodejs`
9
9
  -------------------
@@ -67,7 +67,7 @@ document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
67
67
  Local usage
68
68
  --------------------
69
69
 
70
- Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser. ([known bug](https://github.com/lapo-luchini/asn1js/issues/89): dark mode is currently broken in this mode)
70
+ Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser.
71
71
 
72
72
  Usage from CLI
73
73
  --------------------
@@ -81,7 +81,7 @@ npx @lapo/asn1js ed25519.cer
81
81
  ISC license
82
82
  -----------
83
83
 
84
- ASN.1 JavaScript decoder Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
84
+ ASN.1 JavaScript decoder Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
85
85
 
86
86
  Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
87
87
 
package/asn1.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // ASN.1 JavaScript decoder
2
- // Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3
3
 
4
4
  // Permission to use, copy, modify, and/or distribute this software for any
5
5
  // purpose with or without fee is hereby granted, provided that the above
6
6
  // copyright notice and this permission notice appear in all copies.
7
- //
7
+ //
8
8
  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
9
  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
10
  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@@ -21,7 +21,8 @@ const
21
21
  reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
22
22
  reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
23
23
  hexDigits = '0123456789ABCDEF',
24
- b64Safe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
24
+ b64Std = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
25
+ b64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
25
26
  tableT61 = [
26
27
  ['', ''],
27
28
  ['AEIOUaeiou', 'ÀÈÌÒÙàèìòù'], // Grave
@@ -56,36 +57,55 @@ function checkPrintable(s) {
56
57
  }
57
58
  }
58
59
 
59
- class Stream {
60
+ /** Class to manage a stream of bytes, with a zero-copy approach.
61
+ * It uses an existing array or binary string and advances a position index. */
62
+ export class Stream {
60
63
 
64
+ /**
65
+ * @param {Stream|array|string} enc data (will not be copied)
66
+ * @param {?number} pos starting position (mandatory when `end` is not a Stream)
67
+ */
61
68
  constructor(enc, pos) {
62
69
  if (enc instanceof Stream) {
63
70
  this.enc = enc.enc;
64
71
  this.pos = enc.pos;
65
72
  } else {
66
- // enc should be an array or a binary string
67
73
  this.enc = enc;
68
74
  this.pos = pos;
69
75
  }
70
- }
76
+ if (typeof this.pos != 'number')
77
+ throw new Error('"pos" must be a numeric value');
78
+ if (typeof this.enc == 'string')
79
+ this.getRaw = pos => this.enc.charCodeAt(pos);
80
+ else if (typeof this.enc[0] == 'number')
81
+ this.getRaw = pos => this.enc[pos];
82
+ else
83
+ throw new Error('"enc" must be a numeric array or a string');
84
+ }
85
+ /** Get the byte at current position (and increment it) or at a specified position (and avoid moving current position).
86
+ * @param {?number} pos read position if specified, else current position (and increment it) */
71
87
  get(pos) {
72
88
  if (pos === undefined)
73
89
  pos = this.pos++;
74
90
  if (pos >= this.enc.length)
75
91
  throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length);
76
- return (typeof this.enc == 'string') ? this.enc.charCodeAt(pos) : this.enc[pos];
92
+ return this.getRaw(pos);
77
93
  }
78
- hexByte(b) {
94
+ /** Convert a single byte to an hexadcimal string (of length 2).
95
+ * @param {number} b */
96
+ static hexByte(b) {
79
97
  return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
80
98
  }
81
- /** Hexadecimal dump.
82
- * @param type 'raw', 'byte' or 'dump' */
99
+ /** Hexadecimal dump of a specified region of the stream.
100
+ * @param {number} start starting position (included)
101
+ * @param {number} end ending position (excluded)
102
+ * @param {string} type 'raw', 'byte' or 'dump' (default) */
83
103
  hexDump(start, end, type = 'dump') {
84
104
  let s = '';
85
105
  for (let i = start; i < end; ++i) {
86
106
  if (type == 'byte' && i > start)
87
107
  s += ' ';
88
- s += this.hexByte(this.get(i));
108
+ s += Stream.hexByte(this.get(i));
89
109
  if (type == 'dump')
90
110
  switch (i & 0xF) {
91
111
  case 0x7: s += ' '; break;
@@ -95,23 +115,29 @@ class Stream {
95
115
  }
96
116
  return s;
97
117
  }
98
- b64Dump(start, end) {
118
+ /** Base64url dump of a specified region of the stream (according to RFC 4648 section 5).
119
+ * @param {number} start starting position (included)
120
+ * @param {number} end ending position (excluded)
121
+ * @param {string} type 'url' (default, section 5 without padding) or 'std' (section 4 with padding) */
122
+ b64Dump(start, end, type = 'url') {
123
+ const b64 = type === 'url' ? b64URL : b64Std;
99
124
  let extra = (end - start) % 3,
100
125
  s = '',
101
126
  i, c;
102
127
  for (i = start; i + 2 < end; i += 3) {
103
128
  c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
104
- s += b64Safe.charAt(c >> 18 & 0x3F);
105
- s += b64Safe.charAt(c >> 12 & 0x3F);
106
- s += b64Safe.charAt(c >> 6 & 0x3F);
107
- s += b64Safe.charAt(c & 0x3F);
129
+ s += b64.charAt(c >> 18 & 0x3F);
130
+ s += b64.charAt(c >> 12 & 0x3F);
131
+ s += b64.charAt(c >> 6 & 0x3F);
132
+ s += b64.charAt(c & 0x3F);
108
133
  }
109
134
  if (extra > 0) {
110
135
  c = this.get(i) << 16;
111
136
  if (extra > 1) c |= this.get(i + 1) << 8;
112
- s += b64Safe.charAt(c >> 18 & 0x3F);
113
- s += b64Safe.charAt(c >> 12 & 0x3F);
114
- if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
137
+ s += b64.charAt(c >> 18 & 0x3F);
138
+ s += b64.charAt(c >> 12 & 0x3F);
139
+ if (extra == 2) s += b64.charAt(c >> 6 & 0x3F);
140
+ if (b64 === b64Std) s += '==='.slice(0, 3 - extra);
115
141
  }
116
142
  return s;
117
143
  }
@@ -277,7 +303,7 @@ class Stream {
277
303
  end = start + maxLength;
278
304
  s = '';
279
305
  for (let i = start; i < end; ++i)
280
- s += this.hexByte(this.get(i));
306
+ s += Stream.hexByte(this.get(i));
281
307
  if (len > maxLength)
282
308
  s += ellipsis;
283
309
  return { size: len, str: s };
@@ -535,9 +561,11 @@ export class ASN1 {
535
561
  toHexString(type = 'raw') {
536
562
  return this.stream.hexDump(this.posStart(), this.posEnd(), type);
537
563
  }
538
- /** Base64 dump of the node. */
539
- toB64String() {
540
- return this.stream.b64Dump(this.posStart(), this.posEnd());
564
+ /** Base64url dump of the node (according to RFC 4648 section 5).
565
+ * @param {string} type 'url' (default, section 5 without padding) or 'std' (section 4 with padding)
566
+ */
567
+ toB64String(type = 'url') {
568
+ return this.stream.b64Dump(this.posStart(), this.posEnd(), type);
541
569
  }
542
570
  static decodeLength(stream) {
543
571
  let buf = stream.get(),
package/base64.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // Base64 JavaScript decoder
2
- // Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3
3
 
4
4
  // Permission to use, copy, modify, and/or distribute this software for any
5
5
  // purpose with or without fee is hereby granted, provided that the above
6
6
  // copyright notice and this permission notice appear in all copies.
7
- //
7
+ //
8
8
  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
9
  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
10
  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@@ -31,7 +31,7 @@ export class Base64 {
31
31
  decoder[b64.charCodeAt(i)] = i;
32
32
  for (i = 0; i < ignore.length; ++i)
33
33
  decoder[ignore.charCodeAt(i)] = -1;
34
- // RFC 3548 URL & file safe encoding
34
+ // also support decoding Base64url (RFC 4648 section 5)
35
35
  decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
36
36
  decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
37
37
  }
@@ -75,12 +75,13 @@ export class Base64 {
75
75
 
76
76
  static pretty(str) {
77
77
  // fix padding
78
- if (str.length % 4 > 0)
79
- str = (str + '===').slice(0, str.length + str.length % 4);
80
- // convert RFC 3548 to standard Base64
78
+ let pad = 4 - str.length % 4;
79
+ if (pad < 4)
80
+ str += '==='.slice(0, pad);
81
+ // convert Base64url (RFC 4648 section 5) to standard Base64 (RFC 4648 section 4)
81
82
  str = str.replace(/-/g, '+').replace(/_/g, '/');
82
83
  // 80 column width
83
- return str.replace(/(.{80})/g, '$1\n');
84
+ return str.replace(/.{80}/g, '$&\n');
84
85
  }
85
86
 
86
87
  static unarmor(a) {
package/defs.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // ASN.1 RFC definitions matcher
2
- // Copyright (c) 2023-2024 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2023 Lapo Luchini <lapo@lapo.it>
3
3
 
4
4
  // Permission to use, copy, modify, and/or distribute this software for any
5
5
  // purpose with or without fee is hereby granted, provided that the above
6
6
  // copyright notice and this permission notice appear in all copies.
7
- //
7
+ //
8
8
  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
9
  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
10
  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@@ -29,12 +29,13 @@ function translate(def, tn, stats) {
29
29
  def = Object.assign({}, def);
30
30
  def.type = Defs.searchType(name).type;
31
31
  }
32
- if (def?.type?.name == 'CHOICE') {
33
- for (let c of def.type.content) {
32
+ if (def?.name == 'CHOICE' || def?.type?.name == 'CHOICE') {
33
+ for (let c of def.content ?? def.type.content) {
34
34
  if (tn != c.type.name && tn != c.name)
35
35
  c = translate(c);
36
36
  if (tn == c.type.name || tn == c.name) {
37
37
  def = Object.assign({}, def);
38
+ if (c.id) def.id = c.id;
38
39
  def.type = c.type.name ? c.type : c;
39
40
  break;
40
41
  }
@@ -127,12 +128,14 @@ export class Defs {
127
128
  Defs.RFC = rfcdef;
128
129
 
129
130
  Defs.commonTypes = [
130
- [ 'X.509 certificate', '1.3.6.1.5.5.7.0.18', 'Certificate' ],
131
+ [ 'X.509 certificate', '1.3.6.1.5.5.7.0.18', 'Certificate' ],
131
132
  [ 'X.509 public key info', '1.3.6.1.5.5.7.0.18', 'SubjectPublicKeyInfo' ],
133
+ [ 'X.509 certificate revocation list', '1.3.6.1.5.5.7.0.18', 'CertificateList' ],
132
134
  [ 'CMS / PKCS#7 envelope', '1.2.840.113549.1.9.16.0.14', 'ContentInfo' ],
133
135
  [ 'PKCS#1 RSA private key', '1.2.840.113549.1.1.0.1', 'RSAPrivateKey' ],
134
136
  [ 'PKCS#8 encrypted private key', '1.2.840.113549.1.8.1.1', 'EncryptedPrivateKeyInfo' ],
135
137
  [ 'PKCS#8 private key', '1.2.840.113549.1.8.1.1', 'PrivateKeyInfo' ],
136
138
  [ 'PKCS#10 certification request', '1.2.840.113549.1.10.1.1', 'CertificationRequest' ],
137
139
  [ 'CMP PKI Message', '1.3.6.1.5.5.7.0.16', 'PKIMessage' ],
140
+ [ 'LDAP Message', '1.3.6.1.1.18', 'LDAPMessage' ],
138
141
  ].map(arr => ({ description: arr[0], ...Defs.moduleAndType(rfcdef[arr[1]], arr[2]) }));
package/dom.js ADDED
@@ -0,0 +1,248 @@
1
+ // ASN.1 JavaScript decoder
2
+ // Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3
+
4
+ // Permission to use, copy, modify, and/or distribute this software for any
5
+ // purpose with or without fee is hereby granted, provided that the above
6
+ // copyright notice and this permission notice appear in all copies.
7
+ //
8
+ // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
+ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
+
16
+ import { ASN1 } from './asn1.js';
17
+ import { oids } from './oids.js';
18
+ import { bindContextMenu } from './context.js';
19
+
20
+ const
21
+ lineLength = 80,
22
+ contentLength = 8 * lineLength,
23
+ DOM = {
24
+ ellipsis: '\u2026',
25
+ tag: function (tagName, className, text) {
26
+ let t = document.createElement(tagName);
27
+ if (className) t.className = className;
28
+ if (text) t.innerText = text;
29
+ return t;
30
+ },
31
+ text: function (str) {
32
+ return document.createTextNode(str);
33
+ },
34
+ space: function () {
35
+ return DOM.tag('span', 'spaces', ' ');
36
+ },
37
+ breakLines: function (str, length) {
38
+ let lines = str.split(/\r?\n/),
39
+ o = '';
40
+ for (let i = 0; i < lines.length; ++i) {
41
+ let line = lines[i];
42
+ if (i > 0) o += '\n';
43
+ while (line.length > length) {
44
+ o += line.substring(0, length);
45
+ o += '\n';
46
+ line = line.substring(length);
47
+ }
48
+ o += line;
49
+ }
50
+ return o;
51
+ },
52
+ };
53
+
54
+ export class ASN1DOM extends ASN1 {
55
+
56
+ toDOM(spaces) {
57
+ spaces = spaces || '';
58
+ let isOID = (typeof oids === 'object') && (this.tag.isUniversal() && (this.tag.tagNumber == 0x06) || (this.tag.tagNumber == 0x0D));
59
+ let node = DOM.tag('li');
60
+ node.asn1 = this;
61
+ let head = DOM.tag('span', 'head');
62
+ const typeName = this.typeName().replace(/_/g, ' ');
63
+ if (this.def) {
64
+ if (this.def.id) {
65
+ head.appendChild(DOM.tag('span', 'name id', this.def.id));
66
+ head.appendChild(DOM.space());
67
+ }
68
+ if (this.def.name && this.def.name != typeName) {
69
+ head.appendChild(DOM.tag('span', 'name type', this.def.name));
70
+ head.appendChild(DOM.space());
71
+ }
72
+ if (this.def.mismatch) {
73
+ head.appendChild(DOM.tag('span', 'name type', '[?]'));
74
+ head.appendChild(DOM.space());
75
+ }
76
+ }
77
+ head.appendChild(DOM.text(typeName));
78
+ let content;
79
+ try {
80
+ content = this.content(contentLength);
81
+ } catch (e) {
82
+ content = 'Cannot decode: ' + e;
83
+ }
84
+ let oid;
85
+ if (content !== null) {
86
+ let preview = DOM.tag('span', 'preview'),
87
+ shortContent;
88
+ if (isOID)
89
+ content = content.split('\n', 1)[0];
90
+ shortContent = (content.length > lineLength) ? content.substring(0, lineLength) + DOM.ellipsis : content;
91
+ preview.appendChild(DOM.space());
92
+ preview.appendChild(DOM.text(shortContent));
93
+ if (isOID) {
94
+ oid = oids[content];
95
+ if (oid) {
96
+ if (oid.d) {
97
+ preview.appendChild(DOM.space());
98
+ let oidd = DOM.tag('span', 'oid description', oid.d);
99
+ preview.appendChild(oidd);
100
+ }
101
+ if (oid.c) {
102
+ preview.appendChild(DOM.space());
103
+ let oidc = DOM.tag('span', 'oid comment', '(' + oid.c + ')');
104
+ preview.appendChild(oidc);
105
+ }
106
+ }
107
+ }
108
+ head.appendChild(preview);
109
+ content = DOM.breakLines(content, lineLength);
110
+ content = content.replace(/</g, '&lt;');
111
+ content = content.replace(/\n/g, '<br>');
112
+ }
113
+ // add the li and details section for this node
114
+ let contentNode;
115
+ let childNode;
116
+ if (this.sub !== null) {
117
+ let details = DOM.tag('details');
118
+ details.setAttribute('open', '');
119
+ node.appendChild(details);
120
+ let summary = DOM.tag('summary', 'node');
121
+ details.appendChild(summary);
122
+ summary.appendChild(head);
123
+ contentNode = summary;
124
+ childNode = details;
125
+ } else {
126
+ contentNode = node;
127
+ contentNode.classList.add('node');
128
+ contentNode.appendChild(head);
129
+ }
130
+ this.node = contentNode;
131
+ this.head = head;
132
+ let value = DOM.tag('div', 'value');
133
+ let s = 'Offset: ' + this.stream.pos + '<br>';
134
+ s += 'Length: ' + this.header + '+';
135
+ if (this.length >= 0)
136
+ s += this.length;
137
+ else
138
+ s += (-this.length) + ' (undefined)';
139
+ if (this.tag.tagConstructed)
140
+ s += '<br>(constructed)';
141
+ else if ((this.tag.isUniversal() && ((this.tag.tagNumber == 0x03) || (this.tag.tagNumber == 0x04))) && (this.sub !== null))
142
+ s += '<br>(encapsulates)';
143
+ //TODO if (this.tag.isUniversal() && this.tag.tagNumber == 0x03) s += "Unused bits: "
144
+ if (content !== null) {
145
+ s += '<br>Value:<br><b>' + content + '</b>';
146
+ if (isOID && oid) {
147
+ if (oid.d) s += '<br>' + oid.d;
148
+ if (oid.c) s += '<br>' + oid.c;
149
+ if (oid.w) s += '<br>(warning!)';
150
+ }
151
+ }
152
+ value.innerHTML = s;
153
+ contentNode.appendChild(value);
154
+ if (this.sub !== null) {
155
+ let sub = DOM.tag('ul');
156
+ childNode.appendChild(sub);
157
+ spaces += '\xA0 ';
158
+ for (let i = 0, max = this.sub.length; i < max; ++i)
159
+ sub.appendChild(this.sub[i].toDOM(spaces));
160
+ }
161
+ bindContextMenu(node);
162
+ return node;
163
+ }
164
+ fakeHover(current) {
165
+ this.node.classList.add('hover');
166
+ if (current)
167
+ this.head.classList.add('hover');
168
+ }
169
+ fakeOut(current) {
170
+ this.node.classList.remove('hover');
171
+ if (current)
172
+ this.head.classList.remove('hover');
173
+ }
174
+ toHexDOM_sub(node, className, stream, start, end) {
175
+ if (start >= end)
176
+ return;
177
+ let sub = DOM.tag('span', className, stream.hexDump(start, end));
178
+ node.appendChild(sub);
179
+ }
180
+ toHexDOM(root, trim=true) {
181
+ let node = DOM.tag('span', 'hex');
182
+ if (root === undefined) root = node;
183
+ this.head.hexNode = node;
184
+ this.head.onmouseover = function () { this.hexNode.className = 'hexCurrent'; };
185
+ this.head.onmouseout = function () { this.hexNode.className = 'hex'; };
186
+ node.asn1 = this;
187
+ node.onmouseover = function (event) {
188
+ let current = !root.selected;
189
+ if (current) {
190
+ root.selected = this.asn1;
191
+ this.className = 'hexCurrent';
192
+ }
193
+ this.asn1.fakeHover(current);
194
+ event.stopPropagation();
195
+ };
196
+ node.onmouseout = function () {
197
+ let current = (root.selected == this.asn1);
198
+ this.asn1.fakeOut(current);
199
+ if (current) {
200
+ root.selected = null;
201
+ this.className = 'hex';
202
+ }
203
+ };
204
+ bindContextMenu(node);
205
+ if (root == node) {
206
+ let lineStart = this.posStart() & 0xF;
207
+ if (lineStart != 0) {
208
+ let skip = DOM.tag('span', 'skip');
209
+ let skipStr = '';
210
+ for (let j = lineStart; j > 0; --j)
211
+ skipStr += ' ';
212
+ if (lineStart >= 8)
213
+ skipStr += ' ';
214
+ skip.innerText = skipStr;
215
+ node.appendChild(skip);
216
+ }
217
+ }
218
+ this.toHexDOM_sub(node, 'tag', this.stream, this.posStart(), this.posLen());
219
+ this.toHexDOM_sub(node, (this.length >= 0) ? 'dlen' : 'ulen', this.stream, this.posLen(), this.posContent());
220
+ if (this.sub === null) {
221
+ let start = this.posContent();
222
+ let end = this.posEnd();
223
+ if (!trim || end - start < 10 * 16)
224
+ node.appendChild(DOM.text(
225
+ this.stream.hexDump(start, end)));
226
+ else {
227
+ let end1 = start + 5 * 16 - (start & 0xF);
228
+ let start2 = end - 16 - (end & 0xF);
229
+ node.appendChild(DOM.text(this.stream.hexDump(start, end1)));
230
+ node.appendChild(DOM.tag('span', 'skip', '\u2026 skipping ' + (start2 - end1) + ' bytes \u2026\n'));
231
+ node.appendChild(DOM.text(this.stream.hexDump(start2, end)));
232
+ }
233
+ } else if (this.sub.length > 0) {
234
+ let first = this.sub[0];
235
+ let last = this.sub[this.sub.length - 1];
236
+ this.toHexDOM_sub(node, 'intro', this.stream, this.posContent(), first.posStart());
237
+ for (let i = 0, max = this.sub.length; i < max; ++i)
238
+ node.appendChild(this.sub[i].toHexDOM(root, trim));
239
+ this.toHexDOM_sub(node, 'outro', this.stream, last.posEnd(), this.posEnd());
240
+ } else
241
+ this.toHexDOM_sub(node, 'outro', this.stream, this.posContent(), this.posEnd());
242
+ return node;
243
+ }
244
+ static decode(stream, offset) {
245
+ return ASN1.decode(stream, offset, ASN1DOM);
246
+ }
247
+
248
+ }
package/dumpASN1.js CHANGED
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ // usage:
4
+ // ./dumpASN1.js filename
5
+ // ./dumpASN1.js data:base64,MDMCAQFjLgQACgEACgEAAgEAAgEAAQEAoA+jDQQFTnRWZXIEBAEAAAAwCgQITmV0bG9nb24===
6
+
3
7
  import * as fs from 'node:fs';
4
8
  import { Base64 } from './base64.js';
5
9
  import { ASN1 } from './asn1.js';
package/hex.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // Hex JavaScript decoder
2
- // Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3
3
 
4
4
  // Permission to use, copy, modify, and/or distribute this software for any
5
5
  // purpose with or without fee is hereby granted, provided that the above
6
6
  // copyright notice and this permission notice appear in all copies.
7
- //
7
+ //
8
8
  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
9
  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
10
  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
package/int10.js CHANGED
@@ -1,10 +1,10 @@
1
1
  // Big integer base-10 printing library
2
- // Copyright (c) 2008-2024 Lapo Luchini <lapo@lapo.it>
2
+ // Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
3
3
 
4
4
  // Permission to use, copy, modify, and/or distribute this software for any
5
5
  // purpose with or without fee is hereby granted, provided that the above
6
6
  // copyright notice and this permission notice appear in all copies.
7
- //
7
+ //
8
8
  // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
9
  // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
10
  // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lapo/asn1js",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
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",
@@ -13,12 +13,12 @@
13
13
  "license": "ISC",
14
14
  "bugs": { "url": "https://github.com/lapo-luchini/asn1js/issues" },
15
15
  "homepage": "https://lapo.it/asn1js/",
16
- "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "defs.js", "oids.js", "rfcdef.js", "dumpASN1.js" ],
16
+ "files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "dom.js", "defs.js", "oids.js", "rfcdef.js", "dumpASN1.js" ],
17
17
  "scripts": {
18
- "lint": "npx eslint asn1.js base64.js hex.js int10.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js",
18
+ "lint": "npx eslint asn1.js base64.js hex.js int10.js dom.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js theme.js",
19
19
  "lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
20
20
  "build": "vite build",
21
- "serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
21
+ "serve": "npx -p local-web-server ws",
22
22
  "test": "node test",
23
23
  "testdefs": "node testDefs"
24
24
  },
@@ -29,11 +29,12 @@
29
29
  "node": ">=12.20.0"
30
30
  },
31
31
  "devDependencies": {
32
- "eslint": "^8.34.0",
32
+ "@rollup/wasm-node": "^4.25.0",
33
+ "eslint": "^8.57.1",
33
34
  "htmlparser2": "^9.1.0",
34
- "vite": "^5.2.10",
35
- "vite-plugin-dom": "^1.0.3",
36
- "vite-plugin-singlefile": "^2.0.1"
35
+ "vite": "^5.4.10",
36
+ "vite-plugin-dom": "^1.0.4",
37
+ "vite-plugin-singlefile": "^2.0.3"
37
38
  },
38
39
  "overrides": {
39
40
  "rollup": "npm:@rollup/wasm-node"
@@ -60,7 +61,9 @@
60
61
  "rules": {
61
62
  "strict": [ "error", "function" ],
62
63
  "indent": [ "error", 4 ],
64
+ "no-trailing-spaces": [ "error" ],
63
65
  "linebreak-style": [ "error", "unix" ],
66
+ "eol-last": [ "error", "always" ],
64
67
  "semi": [ "warn", "always" ],
65
68
  "quotes": [ "error", "single", { "avoidEscape": true } ],
66
69
  "no-var": [ "warn" ],