···1+# ChangeLog
2+3+## 2.1.1 - 2025-10-24
4+5+### Changed
6+7+- update dev dependencies
8+- fix test suite that was reporting no error with empty responses
9+10+### Added
11+12+- add content length check for BOOLEAN, INTEGER, OID ([GitHub #104](https://github.com/lapo-luchini/asn1js/pull/104))
13+14+## 2.1.0 - 2025-08-03
15+16+### Changed
17+18+- when fields are CHOICEs now both the field name and the choice name are shown (fixes [GitHub #102](https://github.com/lapo-luchini/asn1js/issues/102))
19+- upgrade minimum NodeJS version supported from 12.20.0 to 14.6.0 due to usage of ?. and ?? operators in defs.js (ECMAScript 2020); older code is still linted against ECMAScript 2015 for now
20+21+### Added
22+23+- add tests to check expected decoding
24+25+## 2.0.6 - 2025-07-29
26+27+### Added
28+29+- add proper support for standard Base64 (we previously only supported Base64url) (fixes [GitHub #99](https://github.com/lapo-luchini/asn1js/pull/99))
30+- improve test harness
31+32+## 2.0.5 - 2025-04-12
33+34+### Added
35+36+- add `index-local.html` for local `file://` usage without needing a web server
37+- add definitions support for `LDAPMessage`
38+- #TODO continue producing old ChangeLog entries
+3-2
LICENSE
···1-ASN.1 JavaScript decoder Copyright (c) 2008-2020 Lapo Luchini <lapo@lapo.it>
0023Permission to use, copy, modify, and/or distribute this software for any
4purpose with or without fee is hereby granted, provided that the above
···11WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14-
···1+ISC License
2+3+Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
45Permission to use, copy, modify, and/or distribute this software for any
6purpose with or without fee is hereby granted, provided that the above
···13WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0
+67-15
README.md
···34asn1js is a JavaScript generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.
56-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).
78-Usage with `npm` / `yarn`
9--------------------------
1011This package can be installed with either npm or yarn via the following commands:
1213-```
14npm install @lapo/asn1js
15-# or with yarn
016yarn add @lapo/asn1js
17```
1819-Assuming a standard javascript bundler is setup you can import it like so:
2021```js
22-const ASN1 = require('@lapo/asn1js');
23-// or with ES modules
24-import ASN1 from '@lapo/asn1js';
25```
2627A submodule of this package can also be imported:
2829```js
30-const ASN1 = require('@lapo/asn1js/hex');
31-// or with ES modules
32-import Hex from '@lapo/asn1js/hex';
0000000000000000000000000000000000000000000000033```
3435ISC license
36-----------
3738-ASN.1 JavaScript decoder Copyright (c) 2008-2020 Lapo Luchini <lapo@lapo.it>
3940Permission 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.
41···48- BMPString support added by [Felipe Gasper](https://github.com/FGasper)
49- extended tag support added by [Pรฉter Budai](https://www.peterbudai.eu/)
50- patches by [Gergely Nagy](https://github.com/ngg)
0005152links
53-----
5455-- [official website](https://lapo.it/asn1js/)
56-- [InDefero tracker](http://idf.lapo.it/p/asn1js/)
0057- [GitHub mirror](https://github.com/lapo-luchini/asn1js)
058- [Ohloh code stats](https://www.openhub.net/p/asn1js)
···34asn1js is a JavaScript generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.
56+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`.
78+Usage with `nodejs`
9+-------------------
1011This package can be installed with either npm or yarn via the following commands:
1213+```sh
14npm install @lapo/asn1js
15+# or other tools
16+pnpm install @lapo/asn1js
17yarn add @lapo/asn1js
18```
1920+You can import the classes like this:
2122```js
23+import { ASN1 } from '@lapo/asn1js';
0024```
2526A submodule of this package can also be imported:
2728```js
29+import { Hex } from '@lapo/asn1js/hex.js';
30+```
31+32+If your code is still not using ES6 Modules (and is using CommonJS) you can `require` it normally [since NodeJS 22](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) (with parameter `--experimental-require-module`):
33+34+```js
35+const
36+ { ASN1 } = require('@lapo/asn1js'),
37+ { Hex } = require('@lapo/asn1js/hex.js');
38+console.log(ASN1.decode(Hex.decode('06032B6570')).content());
39+```
40+41+On older NodeJS you instead need to use async `import`:
42+43+```js
44+async function main() {
45+ const
46+ { ASN1 } = await import('@lapo/asn1js'),
47+ { Hex } = await import('@lapo/asn1js/hex.js');
48+ console.log(ASN1.decode(Hex.decode('06032B6570')).content());
49+}
50+main();
51+```
52+53+Usage on the web
54+--------------------
55+56+Can be [tested on JSFiddle](https://jsfiddle.net/lapo/y6t2wo7q/).
57+58+```html
59+<script>
60+import { ASN1 } from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
61+import { Hex } from 'https://unpkg.com/@lapo/asn1js@2.0.0/hex.js';
62+63+document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
64+</script>
65+```
66+67+Local usage
68+--------------------
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.
71+72+Usage from CLI
73+--------------------
74+75+You can dump an ASN.1 structure from the command line using the following command (no need to even install it):
76+77+```sh
78+npx @lapo/asn1js ed25519.cer
79```
8081ISC license
82-----------
8384+ASN.1 JavaScript decoder Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
8586Permission 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···94- BMPString support added by [Felipe Gasper](https://github.com/FGasper)
95- extended tag support added by [Pรฉter Budai](https://www.peterbudai.eu/)
96- patches by [Gergely Nagy](https://github.com/ngg)
97+- Relative OID support added by [Mistial Developer](https://github.com/mistial-dev)
98+- dark mode and other UI improvements by [Oliver Burgmaier](https://github.com/olibu/)
99+- patches by [Nicolai Sรธborg](https://github.com/NicolaiSoeborg)
100101links
102-----
103104+- [official website](https://asn1js.eu/)
105+- [alternate website](https://lapo.it/asn1js/)
106+- [single-file version working locally](https://asn1js.eu/index-local.html) (just save this link)
107+- [InDefero tracker](http://idf.lapo.it/p/asn1js/) (currently offline)
108- [GitHub mirror](https://github.com/lapo-luchini/asn1js)
109+- [ChangeLog on GitHub](https://github.com/lapo-luchini/asn1js/blob/trunk/CHANGELOG.md)
110- [Ohloh code stats](https://www.openhub.net/p/asn1js)
+800-460
asn1.js
···1// ASN.1 JavaScript decoder
2-// Copyright (c) 2008-2020 Lapo Luchini <lapo@lapo.it>
34// 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
···13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1516-(typeof define != 'undefined' ? define : function (factory) { 'use strict';
17- if (typeof module == 'object') module.exports = factory(function (name) { return require('./' + name); });
18- else window.asn1 = factory(function (name) { return window[name]; });
19-})(function (require) {
20-"use strict";
2122-var Int10 = require('int10'),
23- oids = require('oids'),
24- ellipsis = "\u2026",
25- 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-5]\d)?)?$/,
26- 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-5]\d)?)?$/;
000000000000000000002700000028function stringCut(str, len) {
29 if (str.length > len)
30 str = str.substring(0, len) + ellipsis;
31 return str;
32}
3334-function Stream(enc, pos) {
35- if (enc instanceof Stream) {
36- this.enc = enc.enc;
37- this.pos = enc.pos;
38- } else {
39- // enc should be an array or a binary string
40- this.enc = enc;
41- this.pos = pos;
00042 }
43}
44-Stream.prototype.get = function (pos) {
45- if (pos === undefined)
46- pos = this.pos++;
47- if (pos >= this.enc.length)
48- throw 'Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length;
49- return (typeof this.enc == "string") ? this.enc.charCodeAt(pos) : this.enc[pos];
50-};
51-Stream.prototype.hexDigits = "0123456789ABCDEF";
52-Stream.prototype.hexByte = function (b) {
53- return this.hexDigits.charAt((b >> 4) & 0xF) + this.hexDigits.charAt(b & 0xF);
54-};
55-Stream.prototype.hexDump = function (start, end, raw) {
56- var s = "";
57- for (var i = start; i < end; ++i) {
58- s += this.hexByte(this.get(i));
59- if (raw !== true)
60- switch (i & 0xF) {
61- case 0x7: s += " "; break;
62- case 0xF: s += "\n"; break;
63- default: s += " ";
64- }
0000000065 }
66- return s;
67-};
68-var b64Safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
69-Stream.prototype.b64Dump = function (start, end) {
70- var extra = (end - start) % 3,
71- s = '',
72- i, c;
73- for (i = start; i + 2 < end; i += 3) {
74- c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
75- s += b64Safe.charAt(c >> 18 & 0x3F);
76- s += b64Safe.charAt(c >> 12 & 0x3F);
77- s += b64Safe.charAt(c >> 6 & 0x3F);
78- s += b64Safe.charAt(c & 0x3F);
79 }
80- if (extra > 0) {
81- c = this.get(i) << 16;
82- if (extra > 1) c |= this.get(i + 1) << 8;
83- s += b64Safe.charAt(c >> 18 & 0x3F);
84- s += b64Safe.charAt(c >> 12 & 0x3F);
85- if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
0086 }
87- return s;
88-};
89-Stream.prototype.isASCII = function (start, end) {
90- for (var i = start; i < end; ++i) {
91- var c = this.get(i);
92- if (c < 32 || c > 176)
93- return false;
00000000000000094 }
95- return true;
96-};
97-Stream.prototype.parseStringISO = function (start, end) {
98- var s = "";
99- for (var i = start; i < end; ++i)
100- s += String.fromCharCode(this.get(i));
101- return s;
102-};
103-Stream.prototype.parseStringUTF = function (start, end) {
104- function ex(c) { // must be 10xxxxxx
105- if ((c < 0x80) || (c >= 0xC0))
106- throw new Error('Invalid UTF-8 continuation byte: ' + c);
107- return (c & 0x3F);
0000000000000000108 }
109- function surrogate(cp) {
110- if (cp < 0x10000)
111- throw new Error('UTF-8 overlong encoding, codepoint encoded in 4 bytes: ' + cp);
112- // we could use String.fromCodePoint(cp) but let's be nice to older browsers and use surrogate pairs
113- cp -= 0x10000;
114- return String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
00000000115 }
116- var s = "";
117- for (var i = start; i < end; ) {
118- var c = this.get(i++);
119- if (c < 0x80) // 0xxxxxxx (7 bit)
120- s += String.fromCharCode(c);
121- else if (c < 0xC0)
122- throw new Error('Invalid UTF-8 starting byte: ' + c);
123- else if (c < 0xE0) // 110xxxxx 10xxxxxx (11 bit)
124- s += String.fromCharCode(((c & 0x1F) << 6) | ex(this.get(i++)));
125- else if (c < 0xF0) // 1110xxxx 10xxxxxx 10xxxxxx (16 bit)
126- s += String.fromCharCode(((c & 0x0F) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
127- else if (c < 0xF8) // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (21 bit)
128- s += surrogate(((c & 0x07) << 18) | (ex(this.get(i++)) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
129- else
130- throw new Error('Invalid UTF-8 starting byte (since 2003 it is restricted to 4 bytes): ' + c);
131 }
132- return s;
133-};
134-Stream.prototype.parseStringBMP = function (start, end) {
135- var str = "", hi, lo;
136- for (var i = start; i < end; ) {
137- hi = this.get(i++);
138- lo = this.get(i++);
139- str += String.fromCharCode((hi << 8) | lo);
00000000000000000000140 }
141- return str;
142-};
143-Stream.prototype.parseTime = function (start, end, shortYear) {
144- var s = this.parseStringISO(start, end),
145- m = (shortYear ? reTimeS : reTimeL).exec(s);
146- if (!m)
147- return "Unrecognized time: " + s;
148- if (shortYear) {
149- // to avoid querying the timer, use the fixed range [1970, 2069]
150- // it will conform with ITU X.400 [-10, +40] sliding window until 2030
151- m[1] = +m[1];
152- m[1] += (m[1] < 70) ? 2000 : 1900;
000000000000000000000000000000000000153 }
154- s = m[1] + "-" + m[2] + "-" + m[3] + " " + m[4];
155- if (m[5]) {
156- s += ":" + m[5];
157- if (m[6]) {
158- s += ":" + m[6];
159- if (m[7])
160- s += "." + m[7];
0000000161 }
0162 }
163- if (m[8]) {
164- s += " UTC";
165- if (m[8] != 'Z') {
166- s += m[8];
00000000000000000000000000167 if (m[9])
168- s += ":" + m[9];
169 }
0170 }
171- return s;
172-};
173-Stream.prototype.parseInteger = function (start, end) {
174- var v = this.get(start),
175- neg = (v > 127),
176- pad = neg ? 255 : 0,
177- len,
178- s = '';
179- // skip unuseful bits (not allowed in DER)
180- while (v == pad && ++start < end)
181- v = this.get(start);
182- len = end - start;
183- if (len === 0)
184- return neg ? '-1' : '0';
185- // show bit length of huge integers
186- if (len > 4) {
187- s = v;
188- len <<= 3;
189- while (((s ^ pad) & 0x80) == 0) {
190- s <<= 1;
191- --len;
000000192 }
193- s = "(" + len + " bit)\n";
00000194 }
195- // decode the integer
196- if (neg) v = v - 256;
197- var n = new Int10(v);
198- for (var i = start + 1; i < end; ++i)
199- n.mulAdd(256, this.get(i));
200- return s + n.toString();
201-};
202-Stream.prototype.parseBitString = function (start, end, maxLength) {
203- var unusedBits = this.get(start);
204- if (unusedBits > 7)
205- throw 'Invalid BitString with unusedBits=' + unusedBits;
206- var lenBit = ((end - start - 1) << 3) - unusedBits,
207- intro = "(" + lenBit + " bit)\n",
208- s = "";
209- for (var i = start + 1; i < end; ++i) {
210- var b = this.get(i),
211- skip = (i == end - 1) ? unusedBits : 0;
212- for (var j = 7; j >= skip; --j)
213- s += (b >> j) & 1 ? "1" : "0";
214- if (s.length > maxLength)
215- return intro + stringCut(s, maxLength);
216- }
217- return intro + s;
218-};
219-Stream.prototype.parseOctetString = function (start, end, maxLength) {
220- if (this.isASCII(start, end))
221- return stringCut(this.parseStringISO(start, end), maxLength);
222- var len = end - start,
223- s = "(" + len + " byte)\n";
224- maxLength /= 2; // we work in bytes
225- if (len > maxLength)
226- end = start + maxLength;
227- for (var i = start; i < end; ++i)
228- s += this.hexByte(this.get(i));
229- if (len > maxLength)
230- s += ellipsis;
231- return s;
232-};
233-Stream.prototype.parseOID = function (start, end, maxLength) {
234- var s = '',
235- n = new Int10(),
236- bits = 0;
237- for (var i = start; i < end; ++i) {
238- var v = this.get(i);
239- n.mulAdd(128, v & 0x7F);
240- bits += 7;
241- if (!(v & 0x80)) { // finished
242- if (s === '') {
243- n = n.simplify();
244- if (n instanceof Int10) {
245- n.sub(80);
246- s = "2." + n.toString();
247- } else {
248- var m = n < 80 ? n < 40 ? 0 : 1 : 2;
249- s = m + "." + (n - m * 40);
250- }
251- } else
252- s += "." + n.toString();
253 if (s.length > maxLength)
254- return stringCut(s, maxLength);
255- n = new Int10();
256- bits = 0;
257 }
0258 }
259- if (bits > 0)
260- s += ".incomplete";
261- if (typeof oids === 'object') {
262- var oid = oids[s];
263- if (oid) {
264- if (oid.d) s += "\n" + oid.d;
265- if (oid.c) s += "\n" + oid.c;
266- if (oid.w) s += "\n(warning!)";
0000000267 }
0000000000268 }
269- return s;
270-};
271272-function ASN1(stream, header, length, tag, tagLen, sub) {
273- if (!(tag instanceof ASN1Tag)) throw 'Invalid tag value.';
274- this.stream = stream;
275- this.header = header;
276- this.length = length;
277- this.tag = tag;
278- this.tagLen = tagLen;
279- this.sub = sub;
000000000000000000000000000000000000000000000000000280}
281-ASN1.prototype.typeName = function () {
282- switch (this.tag.tagClass) {
283- case 0: // universal
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000284 switch (this.tag.tagNumber) {
285- case 0x00: return "EOC";
286- case 0x01: return "BOOLEAN";
287- case 0x02: return "INTEGER";
288- case 0x03: return "BIT_STRING";
289- case 0x04: return "OCTET_STRING";
290- case 0x05: return "NULL";
291- case 0x06: return "OBJECT_IDENTIFIER";
292- case 0x07: return "ObjectDescriptor";
293- case 0x08: return "EXTERNAL";
294- case 0x09: return "REAL";
295- case 0x0A: return "ENUMERATED";
296- case 0x0B: return "EMBEDDED_PDV";
297- case 0x0C: return "UTF8String";
298- case 0x10: return "SEQUENCE";
299- case 0x11: return "SET";
300- case 0x12: return "NumericString";
301- case 0x13: return "PrintableString"; // ASCII subset
302- case 0x14: return "TeletexString"; // aka T61String
303- case 0x15: return "VideotexString";
304- case 0x16: return "IA5String"; // ASCII
305- case 0x17: return "UTCTime";
306- case 0x18: return "GeneralizedTime";
307- case 0x19: return "GraphicString";
308- case 0x1A: return "VisibleString"; // ASCII subset
309- case 0x1B: return "GeneralString";
310- case 0x1C: return "UniversalString";
311- case 0x1E: return "BMPString";
00000000000000000000000312 }
313- return "Universal_" + this.tag.tagNumber.toString();
314- case 1: return "Application_" + this.tag.tagNumber.toString();
315- case 2: return "[" + this.tag.tagNumber.toString() + "]"; // Context
316- case 3: return "Private_" + this.tag.tagNumber.toString();
317- }
318-};
319-ASN1.prototype.content = function (maxLength) { // a preview of the content (intended for humans)
320- if (this.tag === undefined)
321 return null;
322- if (maxLength === undefined)
323- maxLength = Infinity;
324- var content = this.posContent(),
325- len = Math.abs(this.length);
326- if (!this.tag.isUniversal()) {
327- if (this.sub !== null)
328- return "(" + this.sub.length + " elem)";
329- return this.stream.parseOctetString(content, content + len, maxLength);
330 }
331- switch (this.tag.tagNumber) {
332- case 0x01: // BOOLEAN
333- return (this.stream.get(content) === 0) ? "false" : "true";
334- case 0x02: // INTEGER
335- return this.stream.parseInteger(content, content + len);
336- case 0x03: // BIT_STRING
337- return this.sub ? "(" + this.sub.length + " elem)" :
338- this.stream.parseBitString(content, content + len, maxLength);
339- case 0x04: // OCTET_STRING
340- return this.sub ? "(" + this.sub.length + " elem)" :
341- this.stream.parseOctetString(content, content + len, maxLength);
342- //case 0x05: // NULL
343- case 0x06: // OBJECT_IDENTIFIER
344- return this.stream.parseOID(content, content + len, maxLength);
345- //case 0x07: // ObjectDescriptor
346- //case 0x08: // EXTERNAL
347- //case 0x09: // REAL
348- case 0x0A: // ENUMERATED
349- return this.stream.parseInteger(content, content + len);
350- //case 0x0B: // EMBEDDED_PDV
351- case 0x10: // SEQUENCE
352- case 0x11: // SET
353- if (this.sub !== null)
354- return "(" + this.sub.length + " elem)";
355- else
356- return "(no elem)";
357- case 0x0C: // UTF8String
358- return stringCut(this.stream.parseStringUTF(content, content + len), maxLength);
359- case 0x12: // NumericString
360- case 0x13: // PrintableString
361- case 0x14: // TeletexString
362- case 0x15: // VideotexString
363- case 0x16: // IA5String
364- //case 0x19: // GraphicString
365- case 0x1A: // VisibleString
366- case 0x1B: // GeneralString
367- //case 0x1C: // UniversalString
368- return stringCut(this.stream.parseStringISO(content, content + len), maxLength);
369- case 0x1E: // BMPString
370- return stringCut(this.stream.parseStringBMP(content, content + len), maxLength);
371- case 0x17: // UTCTime
372- case 0x18: // GeneralizedTime
373- return this.stream.parseTime(content, content + len, (this.tag.tagNumber == 0x17));
374 }
375- return null;
376-};
377-ASN1.prototype.toString = function () {
378- return this.typeName() + "@" + this.stream.pos + "[header:" + this.header + ",length:" + this.length + ",sub:" + ((this.sub === null) ? 'null' : this.sub.length) + "]";
379-};
380-ASN1.prototype.toPrettyString = function (indent) {
381- if (indent === undefined) indent = '';
382- var s = indent + this.typeName() + " @" + this.stream.pos;
383- if (this.length >= 0)
384- s += "+";
385- s += this.length;
386- if (this.tag.tagConstructed)
387- s += " (constructed)";
388- else if ((this.tag.isUniversal() && ((this.tag.tagNumber == 0x03) || (this.tag.tagNumber == 0x04))) && (this.sub !== null))
389- s += " (encapsulates)";
390- var content = this.content();
391- if (content)
392- s += ": " + content.replace(/\n/g, '|');
393- s += "\n";
394- if (this.sub !== null) {
395- indent += ' ';
396- for (var i = 0, max = this.sub.length; i < max; ++i)
397- s += this.sub[i].toPrettyString(indent);
000000000000398 }
399- return s;
400-};
401-ASN1.prototype.posStart = function () {
402- return this.stream.pos;
403-};
404-ASN1.prototype.posContent = function () {
405- return this.stream.pos + this.header;
406-};
407-ASN1.prototype.posEnd = function () {
408- return this.stream.pos + this.header + Math.abs(this.length);
409-};
410-/** Position of the length. */
411-ASN1.prototype.posLen = function() {
412- return this.stream.pos + this.tagLen;
413-};
414-ASN1.prototype.toHexString = function () {
415- return this.stream.hexDump(this.posStart(), this.posEnd(), true);
416-};
417-ASN1.prototype.toB64String = function () {
418- return this.stream.b64Dump(this.posStart(), this.posEnd());
419-};
420-ASN1.decodeLength = function (stream) {
421- var buf = stream.get(),
422- len = buf & 0x7F;
423- if (len == buf)
424- return len;
425- if (len > 6) // no reason to use Int10, as it would be a huge buffer anyways
426- throw "Length over 48 bits not supported at position " + (stream.pos - 1);
427- if (len === 0)
428- return null; // undefined
429- buf = 0;
430- for (var i = 0; i < len; ++i)
431- buf = (buf * 256) + stream.get();
432- return buf;
433-};
434-function ASN1Tag(stream) {
435- var buf = stream.get();
436- this.tagClass = buf >> 6;
437- this.tagConstructed = ((buf & 0x20) !== 0);
438- this.tagNumber = buf & 0x1F;
439- if (this.tagNumber == 0x1F) { // long tag
440- var n = new Int10();
441- do {
442- buf = stream.get();
443- n.mulAdd(128, buf & 0x7F);
444- } while (buf & 0x80);
445- this.tagNumber = n.simplify();
00000000000000000000000446 }
447-}
448-ASN1Tag.prototype.isUniversal = function () {
449- return this.tagClass === 0x00;
450-};
451-ASN1Tag.prototype.isEOC = function () {
452- return this.tagClass === 0x00 && this.tagNumber === 0x00;
453-};
454-ASN1.decode = function (stream, offset) {
455- if (!(stream instanceof Stream))
456- stream = new Stream(stream, offset || 0);
457- var streamStart = new Stream(stream),
458- tag = new ASN1Tag(stream),
459- tagLen = stream.pos - streamStart.pos,
460- len = ASN1.decodeLength(stream),
461- start = stream.pos,
462- header = start - streamStart.pos,
463- sub = null,
464- getSub = function () {
465- sub = [];
466- if (len !== null) {
467- // definite length
468- var end = start + len;
469- if (end > stream.enc.length)
470- throw 'Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream';
471- while (stream.pos < end)
472- sub[sub.length] = ASN1.decode(stream);
473- if (stream.pos != end)
474- throw 'Content size is not correct for container at offset ' + start;
475- } else {
476- // undefined length
477- try {
478- for (;;) {
479- var s = ASN1.decode(stream);
480- if (s.tag.isEOC())
481- break;
482- sub[sub.length] = s;
0000000000000000000000000000483 }
484- len = start - stream.pos; // undefined lengths are represented as negative values
485- } catch (e) {
486- throw 'Exception while decoding undefined length content at offset ' + start + ': ' + e;
487 }
0000488 }
489- };
490- if (tag.tagConstructed) {
491- // must have valid content
492- getSub();
493- } else if (tag.isUniversal() && ((tag.tagNumber == 0x03) || (tag.tagNumber == 0x04))) {
494- // sometimes BitString and OctetString are used to encapsulate ASN.1
495- try {
496- if (tag.tagNumber == 0x03)
497- if (stream.get() != 0)
498- throw "BIT STRINGs with unused bits cannot encapsulate.";
499- getSub();
500- for (var i = 0; i < sub.length; ++i)
501- if (sub[i].tag.isEOC())
502- throw 'EOC is not supposed to be actual content.';
503- } catch (e) {
504- // but silently ignore when they don't
505- sub = null;
506- //DEBUG console.log('Could not decode structure at ' + start + ':', e);
507 }
0508 }
509- if (sub === null) {
510- if (len === null)
511- throw "We can't skip over an invalid tag with undefined length at offset " + start;
512- stream.pos = start + Math.abs(len);
513- }
514- return new ASN1(streamStart, header, len, tag, tagLen, sub);
515-};
516517-return ASN1;
518-519-});
···1// ASN.1 JavaScript decoder
2+// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
34// 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
···13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1516+import { oids } from './oids.js';
00001718+const
19+ ellipsis = '\u2026',
20+ 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)?)?$/,
21+ 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)?)?$/,
22+ hexDigits = '0123456789ABCDEF',
23+ b64Std = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
24+ b64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
25+ tableT61 = [
26+ ['', ''],
27+ ['AEIOUaeiou', 'รรรรรร รจรฌรฒรน'], // Grave
28+ ['ACEILNORSUYZacegilnorsuyz', 'รฤรรฤนลรลลรรลนรกฤรฉฤฃรญฤบลรณลลรบรฝลบ'], // Acute
29+ ['ACEGHIJOSUWYaceghijosuwy', 'รฤรฤฤครฤดรลรลดลถรขฤรชฤฤฅรฎฤตรดลรปลตลท'], // Circumflex
30+ ['AINOUainou', 'รฤจรรลจรฃฤฉรฑรตลฉ'], // Tilde
31+ ['AEIOUaeiou', 'ฤฤฤชลลชฤฤฤซลลซ'], // Macron
32+ ['AGUagu', 'ฤฤลฌฤฤลญ'], // Breve
33+ ['CEGIZcegz', 'ฤฤฤ ฤฐลปฤฤฤกลผ'], // Dot
34+ ['AEIOUYaeiouy', 'รรรรรลธรครซรฏรถรผรฟ'], // Umlaut or diรฆresis
35+ ['', ''],
36+ ['AUau', 'ร ลฎรฅลฏ'], // Ring
37+ ['CGKLNRSTcklnrst', 'รฤขฤถฤปล ลลลขรงฤทฤผลลลลฃ'], // Cedilla
38+ ['', ''],
39+ ['OUou', 'ลลฐลลฑ'], // Double Acute
40+ ['AEIUaeiu', 'ฤฤฤฎลฒฤ ฤฤฏลณ'], // Ogonek
41+ ['CDELNRSTZcdelnrstz', 'ฤฤฤฤฝลลล ลคลฝฤฤฤฤพลลลกลฅลพ'], // Caron
42+ ];
4344+/**
45+ * Truncates a string to a specified length and adds an ellipsis if needed.
46+ * @param {string} str - The input string to truncate
47+ * @param {number} len - The maximum length of the string
48+ * @returns {string} The truncated string
49+ */
50function stringCut(str, len) {
51 if (str.length > len)
52 str = str.substring(0, len) + ellipsis;
53 return str;
54}
5556+/**
57+ * Checks if a string contains only printable characters (ASCII 32-126, plus tab, newline, carriage return)
58+ * @param {string} s - The string to check
59+ * @throws {Error} If an unprintable character is found
60+ */
61+function checkPrintable(s) {
62+ let i, v;
63+ for (i = 0; i < s.length; ++i) {
64+ v = s.charCodeAt(i);
65+ if (v < 32 && v != 9 && v != 10 && v != 13) // [\t\r\n] are (kinda) printable
66+ throw new Error('Unprintable character at index ' + i + ' (code ' + s.str.charCodeAt(i) + ')');
67 }
68}
69+70+/**
71+ * Class to manage a stream of bytes, with a zero-copy approach.
72+ * It uses an existing array or binary string and advances a position index.
73+ */
74+export class Stream {
75+76+ /**
77+ * Creates a new Stream object.
78+ * @param {Stream|array|string} enc data (will not be copied)
79+ * @param {?number} pos starting position (mandatory when `end` is not a Stream)
80+ */
81+ constructor(enc, pos) {
82+ if (enc instanceof Stream) {
83+ this.enc = enc.enc;
84+ this.pos = enc.pos;
85+ } else {
86+ this.enc = enc;
87+ this.pos = pos;
88+ }
89+ if (typeof this.pos != 'number')
90+ throw new Error('"pos" must be a numeric value');
91+ // Set up the raw byte access function based on the type of data
92+ if (typeof this.enc == 'string')
93+ this.getRaw = pos => this.enc.charCodeAt(pos);
94+ else if (typeof this.enc[0] == 'number')
95+ this.getRaw = pos => this.enc[pos];
96+ else
97+ throw new Error('"enc" must be a numeric array or a string');
98 }
99+100+ /**
101+ * Get the byte at current position (and increment it) or at a specified position (and avoid moving current position).
102+ * @param {?number} pos read position if specified, else current position (and increment it)
103+ * @returns {number} The byte value at the specified position
104+ */
105+ get(pos) {
106+ if (pos === undefined)
107+ pos = this.pos++;
108+ if (pos >= this.enc.length)
109+ throw new Error('Requesting byte offset ' + pos + ' on a stream of length ' + this.enc.length);
110+ return this.getRaw(pos);
0111 }
112+113+ /**
114+ * Convert a single byte to a hexadecimal string (of length 2).
115+ * @param {number} b - The byte to convert
116+ * @returns {string} Hexadecimal representation of the byte
117+ */
118+ static hexByte(b) {
119+ return hexDigits.charAt((b >> 4) & 0xF) + hexDigits.charAt(b & 0xF);
120 }
121+122+ /**
123+ * Hexadecimal dump of a specified region of the stream.
124+ * @param {number} start - starting position (included)
125+ * @param {number} end - ending position (excluded)
126+ * @param {string} type - 'raw', 'byte' or 'dump' (default)
127+ * @returns {string} Hexadecimal representation of the data
128+ */
129+ hexDump(start, end, type = 'dump') {
130+ let s = '';
131+ for (let i = start; i < end; ++i) {
132+ if (type == 'byte' && i > start)
133+ s += ' ';
134+ s += Stream.hexByte(this.get(i));
135+ if (type == 'dump')
136+ switch (i & 0xF) {
137+ case 0x7: s += ' '; break;
138+ case 0xF: s += '\n'; break;
139+ default: s += ' ';
140+ }
141+ }
142+ return s;
143 }
144+145+ /**
146+ * Base64url dump of a specified region of the stream (according to RFC 4648 section 5).
147+ * @param {number} start - starting position (included)
148+ * @param {number} end - ending position (excluded)
149+ * @param {string} type - 'url' (default, section 5 without padding) or 'std' (section 4 with padding)
150+ * @returns {string} Base64 encoded representation of the data
151+ */
152+ b64Dump(start, end, type = 'url') {
153+ const b64 = type === 'url' ? b64URL : b64Std,
154+ extra = (end - start) % 3;
155+ let s = '',
156+ i, c;
157+ for (i = start; i + 2 < end; i += 3) {
158+ c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
159+ s += b64.charAt(c >> 18 & 0x3F);
160+ s += b64.charAt(c >> 12 & 0x3F);
161+ s += b64.charAt(c >> 6 & 0x3F);
162+ s += b64.charAt(c & 0x3F);
163+ }
164+ if (extra > 0) {
165+ c = this.get(i) << 16;
166+ if (extra > 1) c |= this.get(i + 1) << 8;
167+ s += b64.charAt(c >> 18 & 0x3F);
168+ s += b64.charAt(c >> 12 & 0x3F);
169+ if (extra == 2) s += b64.charAt(c >> 6 & 0x3F);
170+ if (b64 === b64Std) s += '==='.slice(0, 3 - extra);
171+ }
172+ return s;
173 }
174+175+ /**
176+ * Check if a region of the stream contains only ASCII characters (32-176)
177+ * @param {number} start - starting position (included)
178+ * @param {number} end - ending position (excluded)
179+ * @returns {boolean} True if all characters are ASCII, false otherwise
180+ */
181+ isASCII(start, end) {
182+ for (let i = start; i < end; ++i) {
183+ let c = this.get(i);
184+ if (c < 32 || c > 176)
185+ return false;
186+ }
187+ return true;
188 }
189+190+ /**
191+ * Parse a region of the stream as an ISO string
192+ * @param {number} start - starting position (included)
193+ * @param {number} end - ending position (excluded)
194+ * @param {number} maxLength - maximum length of the output string
195+ * @returns {Object} Object with size and str properties
196+ */
197+ parseStringISO(start, end, maxLength) {
198+ let s = '';
199+ for (let i = start; i < end; ++i)
200+ s += String.fromCharCode(this.get(i));
201+ return { size: s.length, str: stringCut(s, maxLength) };
00202 }
203+204+ /**
205+ * Parse a region of the stream as a T.61 string
206+ * @param {number} start - starting position (included)
207+ * @param {number} end - ending position (excluded)
208+ * @param {number} maxLength - maximum length of the output string
209+ * @returns {Object} Object with size and str properties
210+ */
211+ parseStringT61(start, end, maxLength) {
212+ // warning: this code is not very well tested so far
213+ function merge(c, d) {
214+ const t = tableT61[c - 0xC0];
215+ const i = t[0].indexOf(String.fromCharCode(d));
216+ return (i < 0) ? '\0' : t[1].charAt(i);
217+ }
218+ let s = '', c;
219+ for (let i = start; i < end; ++i) {
220+ c = this.get(i);
221+ if (c >= 0xA4 && c <= 0xBF)
222+ s += '$ยฅ#ยงยค\0\0ยซ\0\0\0\0ยฐยฑยฒยณรยตยถยทรท\0\0ยปยผยฝยพยฟ'.charAt(c - 0xA4);
223+ else if (c >= 0xE0 && c <= 0xFF)
224+ s += 'โฆรรยชฤฆ\0ฤฒฤฟลรลยบรลฆลลฤธรฆฤรฐฤงฤฑฤณลลรธลรรพลงล\0'.charAt(c - 0xE0);
225+ else if (c >= 0xC0 && c <= 0xCF)
226+ s += merge(c, this.get(++i));
227+ else // using ISO 8859-1 for characters undefined (or equal) in T.61
228+ s += String.fromCharCode(c);
229+ }
230+ return { size: s.length, str: stringCut(s, maxLength) };
231 }
232+233+ /**
234+ * Parse a region of the stream as a UTF-8 string
235+ * @param {number} start - starting position (included)
236+ * @param {number} end - ending position (excluded)
237+ * @param {number} maxLength - maximum length of the output string
238+ * @returns {Object} Object with size and str properties
239+ */
240+ parseStringUTF(start, end, maxLength) {
241+ /**
242+ * Helper function to process UTF-8 continuation bytes
243+ * @param {number} c - The continuation byte
244+ * @returns {number} The extracted data bits
245+ */
246+ function ex(c) { // must be 10xxxxxx
247+ if ((c < 0x80) || (c >= 0xC0))
248+ throw new Error('Invalid UTF-8 continuation byte: ' + c);
249+ return (c & 0x3F);
250+ }
251+ /**
252+ * Helper function to convert a code point to a surrogate pair
253+ * @param {number} cp - The code point to convert
254+ * @returns {string} The surrogate pair as a string
255+ */
256+ function surrogate(cp) {
257+ if (cp < 0x10000)
258+ throw new Error('UTF-8 overlong encoding, codepoint encoded in 4 bytes: ' + cp);
259+ // we could use String.fromCodePoint(cp) but let's be nice to older browsers and use surrogate pairs
260+ cp -= 0x10000;
261+ return String.fromCharCode((cp >> 10) + 0xD800, (cp & 0x3FF) + 0xDC00);
262+ }
263+ let s = '';
264+ for (let i = start; i < end; ) {
265+ const c = this.get(i++);
266+ if (c < 0x80) // 0xxxxxxx (7 bit)
267+ s += String.fromCharCode(c);
268+ else if (c < 0xC0)
269+ throw new Error('Invalid UTF-8 starting byte: ' + c);
270+ else if (c < 0xE0) // 110xxxxx 10xxxxxx (11 bit)
271+ s += String.fromCharCode(((c & 0x1F) << 6) | ex(this.get(i++)));
272+ else if (c < 0xF0) // 1110xxxx 10xxxxxx 10xxxxxx (16 bit)
273+ s += String.fromCharCode(((c & 0x0F) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
274+ else if (c < 0xF8) // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx (21 bit)
275+ s += surrogate(((c & 0x07) << 18) | (ex(this.get(i++)) << 12) | (ex(this.get(i++)) << 6) | ex(this.get(i++)));
276+ else
277+ throw new Error('Invalid UTF-8 starting byte (since 2003 it is restricted to 4 bytes): ' + c);
278+ }
279+ return { size: s.length, str: stringCut(s, maxLength) };
280 }
281+282+ /**
283+ * Parse a region of the stream as a BMP (Basic Multilingual Plane) string
284+ * @param {number} start - starting position (included)
285+ * @param {number} end - ending position (excluded)
286+ * @param {number} maxLength - maximum length of the output string
287+ * @returns {Object} Object with size and str properties
288+ */
289+ parseStringBMP(start, end, maxLength) {
290+ let s = '', hi, lo;
291+ for (let i = start; i < end; ) {
292+ hi = this.get(i++);
293+ lo = this.get(i++);
294+ s += String.fromCharCode((hi << 8) | lo);
295 }
296+ return { size: s.length, str: stringCut(s, maxLength) };
297 }
298+299+ /**
300+ * Parse a region of the stream as a time string
301+ * @param {number} start - starting position (included)
302+ * @param {number} end - ending position (excluded)
303+ * @param {boolean} shortYear - Whether to parse as short year (2-digit)
304+ * @returns {string} Formatted time string
305+ */
306+ parseTime(start, end, shortYear) {
307+ let s = this.parseStringISO(start, end).str,
308+ m = (shortYear ? reTimeS : reTimeL).exec(s);
309+ if (!m)
310+ throw new Error('Unrecognized time: ' + s);
311+ if (shortYear) {
312+ // to avoid querying the timer, use the fixed range [1970, 2069]
313+ // it will conform with ITU X.400 [-10, +40] sliding window until 2030
314+ m[1] = +m[1];
315+ m[1] += (m[1] < 70) ? 2000 : 1900;
316+ }
317+ s = m[1] + '-' + m[2] + '-' + m[3] + ' ' + m[4];
318+ if (m[5]) {
319+ s += ':' + m[5];
320+ if (m[6]) {
321+ s += ':' + m[6];
322+ if (m[7])
323+ s += '.' + m[7];
324+ }
325+ }
326+ if (m[8]) {
327+ s += ' UTC';
328 if (m[9])
329+ s += m[9] + ':' + (m[10] || '00');
330 }
331+ return s;
332 }
333+334+ /**
335+ * Parse a region of the stream as an integer
336+ * @param {number} start - starting position (included)
337+ * @param {number} end - ending position (excluded)
338+ * @returns {string} Formatted integer string
339+ */
340+ parseInteger(start, end) {
341+ let v = this.get(start),
342+ s = '';
343+ const neg = (v > 127),
344+ pad = neg ? 255 : 0;
345+ // skip unuseful bits (not allowed in DER)
346+ while (v == pad && ++start < end)
347+ v = this.get(start);
348+ const len = end - start;
349+ if (len === 0)
350+ return neg ? '-1' : '0';
351+ // show bit length of huge integers
352+ if (len > 4) {
353+ let v2 = v,
354+ lenBit = len << 3;
355+ while (((v2 ^ pad) & 0x80) == 0) {
356+ v2 <<= 1;
357+ --lenBit;
358+ }
359+ s = '(' + lenBit + ' bit)\n';
360 }
361+ // decode the integer
362+ if (neg) v = v - 256;
363+ let n = BigInt(v);
364+ for (let i = start + 1; i < end; ++i)
365+ n = (n << 8n) | BigInt(this.get(i));
366+ return s + n;
367 }
368+369+ /**
370+ * Parse a region of the stream as a bit string.
371+ * @param {number} start - starting position (included)
372+ * @param {number} end - ending position (excluded)
373+ * @param {number} maxLength - maximum length of the output string
374+ * @returns {Object} Object with size and str properties
375+ */
376+ parseBitString(start, end, maxLength) {
377+ const unusedBits = this.get(start);
378+ if (unusedBits > 7)
379+ throw new Error('Invalid BitString with unusedBits=' + unusedBits);
380+ const lenBit = ((end - start - 1) << 3) - unusedBits;
381+ let s = '';
382+ for (let i = start + 1; i < end; ++i) {
383+ let b = this.get(i),
384+ skip = (i == end - 1) ? unusedBits : 0;
385+ for (let j = 7; j >= skip; --j)
386+ s += (b >> j) & 1 ? '1' : '0';
000000000000000000000000000000000000000387 if (s.length > maxLength)
388+ s = stringCut(s, maxLength);
00389 }
390+ return { size: lenBit, str: s };
391 }
392+393+ /**
394+ * Parse a region of the stream as an octet string.
395+ * @param {number} start - starting position (included)
396+ * @param {number} end - ending position (excluded)
397+ * @param {number} maxLength - maximum length of the output string
398+ * @returns {Object} Object with size and str properties
399+ */
400+ parseOctetString(start, end, maxLength) {
401+ try {
402+ let s = this.parseStringUTF(start, end, maxLength);
403+ checkPrintable(s.str);
404+ return { size: end - start, str: s.str };
405+ } catch (ignore) {
406+ // If UTF-8 parsing fails, fall back to hexadecimal dump
407 }
408+ const len = end - start;
409+ maxLength /= 2; // we work in bytes
410+ if (len > maxLength)
411+ end = start + maxLength;
412+ let s = '';
413+ for (let i = start; i < end; ++i)
414+ s += Stream.hexByte(this.get(i));
415+ if (len > maxLength)
416+ s += ellipsis;
417+ return { size: len, str: s };
418 }
00419420+ /**
421+ * Parse a region of the stream as an OID (Object Identifier).
422+ * @param {number} start - starting position (included)
423+ * @param {number} end - ending position (excluded)
424+ * @param {number} maxLength - maximum length of the output string
425+ * @param {boolean} isRelative - Whether the OID is relative
426+ * @returns {string} Formatted OID string
427+ */
428+ parseOID(start, end, maxLength, isRelative) {
429+ let s = '',
430+ n = 0n,
431+ bits = 0;
432+ for (let i = start; i < end; ++i) {
433+ let v = this.get(i);
434+ // Shift bits and add the lower 7 bits of the byte
435+ n = (n << 7n) | BigInt(v & 0x7F);
436+ bits += 7;
437+ // If the most significant bit is 0, this is the last byte of the OID component
438+ if (!(v & 0x80)) { // finished
439+ // If this is the first component, handle it specially
440+ if (s === '') {
441+ if (isRelative) {
442+ s = n.toString();
443+ } else {
444+ let m = n < 80 ? n < 40 ? 0n : 1n : 2n;
445+ s = m + '.' + (n - m * 40n);
446+ }
447+ } else
448+ s += '.' + n;
449+ if (s.length > maxLength)
450+ return stringCut(s, maxLength);
451+ n = 0n;
452+ bits = 0;
453+ }
454+ }
455+ if (bits > 0)
456+ s += '.incomplete';
457+ // If OIDs mapping is available and the OID is absolute, try to resolve it
458+ if (typeof oids === 'object' && !isRelative) {
459+ let oid = oids[s];
460+ if (oid) {
461+ if (oid.d) s += '\n' + oid.d;
462+ if (oid.c) s += '\n' + oid.c;
463+ if (oid.w) s += '\n(warning!)';
464+ }
465+ }
466+ return s;
467+ }
468+469+ /**
470+ * Parse a region of the stream as a relative OID (Object Identifier).
471+ * @param {number} start - starting position (included)
472+ * @param {number} end - ending position (excluded)
473+ * @param {number} maxLength - maximum length of the output string
474+ * @returns {string} Formatted relative OID string
475+ */
476+ parseRelativeOID(start, end, maxLength) {
477+ return this.parseOID(start, end, maxLength, true);
478+ }
479}
480+481+function recurse(el, parser, maxLength) {
482+ let avoidRecurse = true;
483+ if (el.tag.tagConstructed && el.sub) {
484+ avoidRecurse = false;
485+ el.sub.forEach(function (e1) {
486+ if (e1.tag.tagClass != el.tag.tagClass || e1.tag.tagNumber != el.tag.tagNumber)
487+ avoidRecurse = true;
488+ });
489+ }
490+ if (avoidRecurse)
491+ return el.stream[parser](el.posContent(), el.posContent() + Math.abs(el.length), maxLength);
492+ let d = { size: 0, str: '' };
493+ el.sub.forEach(function (el) {
494+ let d1 = recurse(el, parser, maxLength - d.str.length);
495+ d.size += d1.size;
496+ d.str += d1.str;
497+ });
498+ return d;
499+}
500+501+class ASN1Tag {
502+ constructor(stream) {
503+ let buf = stream.get();
504+ this.tagClass = buf >> 6;
505+ this.tagConstructed = ((buf & 0x20) !== 0);
506+ this.tagNumber = buf & 0x1F;
507+ if (this.tagNumber == 0x1F) { // long tag
508+ let n = 0n;
509+ do {
510+ buf = stream.get();
511+ n = (n << 7n) | BigInt(buf & 0x7F);
512+ } while (buf & 0x80);
513+ this.tagNumber = n <= Number.MAX_SAFE_INTEGER ? Number(n) : n;
514+ }
515+ }
516+ isUniversal() {
517+ return this.tagClass === 0x00;
518+ }
519+ isEOC() {
520+ return this.tagClass === 0x00 && this.tagNumber === 0x00;
521+ }
522+}
523+524+/**
525+ * ASN1 class for parsing ASN.1 encoded data.
526+ * Instances of this class represent an ASN.1 element and provides methods to parse and display its content.
527+ */
528+export class ASN1 {
529+ /**
530+ * Creates an ASN1 parser object.
531+ * @param {Stream} stream - The stream containing the ASN.1 data.
532+ * @param {number} header - The header length.
533+ * @param {number} length - The length of the data.
534+ * @param {ASN1Tag} tag - The ASN.1 tag.
535+ * @param {number} tagLen - The length of the tag.
536+ * @param {Array} sub - The sub-elements.
537+ */
538+ constructor(stream, header, length, tag, tagLen, sub) {
539+ if (!(tag instanceof ASN1Tag)) throw new Error('Invalid tag value.');
540+ this.stream = stream;
541+ this.header = header;
542+ this.length = length;
543+ this.tag = tag;
544+ this.tagLen = tagLen;
545+ this.sub = sub;
546+ }
547+548+ /**
549+ * Get the type name of the ASN.1 element.
550+ * @returns {string} The type name.
551+ */
552+ typeName() {
553+ switch (this.tag.tagClass) {
554+ case 0: // universal
555+ switch (this.tag.tagNumber) {
556+ case 0x00: return 'EOC';
557+ case 0x01: return 'BOOLEAN';
558+ case 0x02: return 'INTEGER';
559+ case 0x03: return 'BIT_STRING';
560+ case 0x04: return 'OCTET_STRING';
561+ case 0x05: return 'NULL';
562+ case 0x06: return 'OBJECT_IDENTIFIER';
563+ case 0x07: return 'ObjectDescriptor';
564+ case 0x08: return 'EXTERNAL';
565+ case 0x09: return 'REAL';
566+ case 0x0A: return 'ENUMERATED';
567+ case 0x0B: return 'EMBEDDED_PDV';
568+ case 0x0C: return 'UTF8String';
569+ case 0x0D: return 'RELATIVE_OID';
570+ case 0x10: return 'SEQUENCE';
571+ case 0x11: return 'SET';
572+ case 0x12: return 'NumericString';
573+ case 0x13: return 'PrintableString'; // ASCII subset
574+ case 0x14: return 'TeletexString'; // aka T61String
575+ case 0x15: return 'VideotexString';
576+ case 0x16: return 'IA5String'; // ASCII
577+ case 0x17: return 'UTCTime';
578+ case 0x18: return 'GeneralizedTime';
579+ case 0x19: return 'GraphicString';
580+ case 0x1A: return 'VisibleString'; // ASCII subset
581+ case 0x1B: return 'GeneralString';
582+ case 0x1C: return 'UniversalString';
583+ case 0x1E: return 'BMPString';
584+ }
585+ return 'Universal_' + this.tag.tagNumber.toString();
586+ case 1: return 'Application_' + this.tag.tagNumber.toString();
587+ case 2: return '[' + this.tag.tagNumber.toString() + ']'; // Context
588+ case 3: return 'Private_' + this.tag.tagNumber.toString();
589+ }
590+ }
591+592+ /**
593+ * Get a string preview of the content (intended for humans).
594+ * @param {number} maxLength - The maximum length of the content.
595+ * @returns {string|null} The content preview or null if not supported.
596+ */
597+ content(maxLength) {
598+ if (this.tag === undefined)
599+ return null;
600+ if (maxLength === undefined)
601+ maxLength = Infinity;
602+ const content = this.posContent(),
603+ len = Math.abs(this.length);
604+ if (!this.tag.isUniversal()) {
605+ if (this.sub !== null)
606+ return '(' + this.sub.length + ' elem)';
607+ let d1 = this.stream.parseOctetString(content, content + len, maxLength);
608+ return '(' + d1.size + ' byte)\n' + d1.str;
609+ }
610 switch (this.tag.tagNumber) {
611+ case 0x01: // BOOLEAN
612+ if (len != 1) return 'invalid length ' + len;
613+ return (this.stream.get(content) === 0) ? 'false' : 'true';
614+ case 0x02: // INTEGER
615+ if (len < 1) return 'invalid length ' + len;
616+ return this.stream.parseInteger(content, content + len);
617+ case 0x03: { // BIT_STRING
618+ let d = recurse(this, 'parseBitString', maxLength);
619+ return '(' + d.size + ' bit)\n' + d.str;
620+ }
621+ case 0x04: { // OCTET_STRING
622+ let d = recurse(this, 'parseOctetString', maxLength);
623+ return '(' + d.size + ' byte)\n' + d.str;
624+ }
625+ //case 0x05: // NULL
626+ case 0x06: // OBJECT_IDENTIFIER
627+ if (len < 1) return 'invalid length ' + len; // pgut001's dumpasn1.c enforces a minimum lenght of 3
628+ return this.stream.parseOID(content, content + len, maxLength);
629+ //case 0x07: // ObjectDescriptor
630+ //case 0x08: // EXTERNAL
631+ //case 0x09: // REAL
632+ case 0x0A: // ENUMERATED
633+ return this.stream.parseInteger(content, content + len);
634+ //case 0x0B: // EMBEDDED_PDV
635+ case 0x0D: // RELATIVE-OID
636+ return this.stream.parseRelativeOID(content, content + len, maxLength);
637+ case 0x10: // SEQUENCE
638+ case 0x11: // SET
639+ if (this.sub !== null)
640+ return '(' + this.sub.length + ' elem)';
641+ else
642+ return '(no elem)';
643+ case 0x0C: // UTF8String
644+ return recurse(this, 'parseStringUTF', maxLength).str;
645+ case 0x14: // TeletexString
646+ return recurse(this, 'parseStringT61', maxLength).str;
647+ case 0x12: // NumericString
648+ case 0x13: // PrintableString
649+ case 0x15: // VideotexString
650+ case 0x16: // IA5String
651+ case 0x1A: // VisibleString
652+ case 0x1B: // GeneralString
653+ //case 0x19: // GraphicString
654+ //case 0x1C: // UniversalString
655+ return recurse(this, 'parseStringISO', maxLength).str;
656+ case 0x1E: // BMPString
657+ return recurse(this, 'parseStringBMP', maxLength).str;
658+ case 0x17: // UTCTime
659+ case 0x18: // GeneralizedTime
660+ return this.stream.parseTime(content, content + len, (this.tag.tagNumber == 0x17));
661 }
00000000662 return null;
00000000663 }
664+665+ /**
666+ * Get a string representation of the ASN.1 element.
667+ * @returns {string} The string representation.
668+ */
669+ toString() {
670+ return this.typeName() + '@' + this.stream.pos + '[header:' + this.header + ',length:' + this.length + ',sub:' + ((this.sub === null) ? 'null' : this.sub.length) + ']';
000000000000000000000000000000000000671 }
672+673+ /**
674+ * Get a pretty string representation of the ASN.1 element.
675+ * @param {string} indent - The indentation string.
676+ * @returns {string} The pretty string representation.
677+ */
678+ toPrettyString(indent) {
679+ if (indent === undefined) indent = '';
680+ let s = indent;
681+ if (this.def) {
682+ if (this.def.id)
683+ s += this.def.id + ' ';
684+ if (this.def.name && this.def.name != this.typeName().replace(/_/g, ' '))
685+ s+= this.def.name + ' ';
686+ if (this.def.mismatch)
687+ s += '[?] ';
688+ }
689+ s += this.typeName() + ' @' + this.stream.pos;
690+ if (this.length >= 0)
691+ s += '+';
692+ s += this.length;
693+ if (this.tag.tagConstructed)
694+ s += ' (constructed)';
695+ else if ((this.tag.isUniversal() && ((this.tag.tagNumber == 0x03) || (this.tag.tagNumber == 0x04))) && (this.sub !== null))
696+ s += ' (encapsulates)';
697+ let content = this.content();
698+ if (content)
699+ s += ': ' + content.replace(/\n/g, '|');
700+ s += '\n';
701+ if (this.sub !== null) {
702+ indent += ' ';
703+ for (let i = 0, max = this.sub.length; i < max; ++i)
704+ s += this.sub[i].toPrettyString(indent);
705+ }
706+ return s;
707 }
708+709+ /**
710+ * Get the starting position of the element in the stream.
711+ * @returns {number} The starting position.
712+ */
713+ posStart() {
714+ return this.stream.pos;
715+ }
716+717+ /**
718+ * Get the position of the content in the stream.
719+ * @returns {number} The content position.
720+ */
721+ posContent() {
722+ return this.stream.pos + this.header;
723+ }
724+725+ /**
726+ * Get the ending position of the element in the stream.
727+ * @returns {number} The ending position.
728+ */
729+ posEnd() {
730+ return this.stream.pos + this.header + Math.abs(this.length);
731+ }
732+733+ /**
734+ * Get the position of the length in the stream.
735+ * @returns {number} The length position.
736+ */
737+ posLen() {
738+ return this.stream.pos + this.tagLen;
739+ }
740+741+ /**
742+ * Get a hexadecimal dump of the node.
743+ * @param {string} [type='raw'] - The dump type: 'raw', 'byte', or 'dump'.
744+ * @returns {string} The hexadecimal dump.
745+ */
746+ toHexString(type = 'raw') {
747+ return this.stream.hexDump(this.posStart(), this.posEnd(), type);
748+ }
749+750+ /**
751+ * Get a base64url dump of the node (according to RFC 4648 section 5).
752+ * @param {string} [type='url'] - The dump type: 'url' (section 5 without padding) or 'std' (section 4 with padding).
753+ * @returns {string} The base64 encoded representation.
754+ */
755+ toB64String(type = 'url') {
756+ return this.stream.b64Dump(this.posStart(), this.posEnd(), type);
757+ }
758+759+ /**
760+ * Decode the length field of an ASN.1 element.
761+ * @param {Stream} stream - The stream to read from.
762+ * @returns {number|null} The decoded length, or null for indefinite length.
763+ * @throws {Error} If the length is invalid or exceeds 48 bits.
764+ */
765+ static decodeLength(stream) {
766+ const buf = stream.get(),
767+ len = buf & 0x7F;
768+ if (len == buf) // first bit was 0, short form
769+ return len;
770+ if (len === 0) // long form with length 0 is a special case
771+ return null; // undefined length
772+ if (len > 6) // no reason to use BigInt, as it would be a huge buffer anyways
773+ throw new Error('Length over 48 bits not supported at position ' + (stream.pos - 1));
774+ let value = 0;
775+ for (let i = 0; i < len; ++i)
776+ value = (value << 8) | stream.get();
777+ return value;
778 }
779+780+ /**
781+ * Decode an ASN.1 element from a stream.
782+ * @param {Stream|array|string} stream - The input data.
783+ * @param {number} [offset=0] - The offset to start decoding from.
784+ * @param {Function} [type=ASN1] - The class to instantiate.
785+ * @returns {ASN1} The decoded ASN.1 element.
786+ * @throws {Error} If the decoding fails.
787+ */
788+ static decode(stream, offset, type = ASN1) {
789+ if (!(type == ASN1 || type.prototype instanceof ASN1))
790+ throw new Error('Must pass a class that extends ASN1');
791+ if (!(stream instanceof Stream))
792+ stream = new Stream(stream, offset || 0);
793+ let streamStart = new Stream(stream),
794+ tag = new ASN1Tag(stream),
795+ tagLen = stream.pos - streamStart.pos,
796+ len = ASN1.decodeLength(stream),
797+ start = stream.pos,
798+ header = start - streamStart.pos,
799+ sub = null,
800+ getSub = function () {
801+ sub = [];
802+ if (len !== null) {
803+ // definite length
804+ let end = start + len;
805+ if (end > stream.enc.length)
806+ throw new Error('Container at offset ' + start + ' has a length of ' + len + ', which is past the end of the stream');
807+ while (stream.pos < end)
808+ sub[sub.length] = type.decode(stream);
809+ if (stream.pos != end)
810+ throw new Error('Content size is not correct for container at offset ' + start);
811+ } else {
812+ // undefined length
813+ try {
814+ for (;;) {
815+ let s = type.decode(stream);
816+ if (s.tag.isEOC())
817+ break;
818+ sub[sub.length] = s;
819+ }
820+ len = start - stream.pos; // undefined lengths are represented as negative values
821+ } catch (e) {
822+ throw new Error('Exception while decoding undefined length content at offset ' + start + ': ' + e);
823+ }
824+ }
825+ };
826+ if (tag.tagConstructed) {
827+ // must have valid content
828+ getSub();
829+ } else if (tag.isUniversal() && ((tag.tagNumber == 0x03) || (tag.tagNumber == 0x04))) {
830+ // sometimes BitString and OctetString are used to encapsulate ASN.1
831+ try {
832+ if (tag.tagNumber == 0x03)
833+ if (stream.get() != 0)
834+ throw new Error('BIT STRINGs with unused bits cannot encapsulate.');
835+ getSub();
836+ for (let s of sub) {
837+ if (s.tag.isEOC())
838+ throw new Error('EOC is not supposed to be actual content.');
839+ try {
840+ s.content();
841+ } catch (e) {
842+ throw new Error('Unable to parse content: ' + e);
843 }
000844 }
845+ } catch (ignore) {
846+ // but silently ignore when they don't
847+ sub = null;
848+ //DEBUG console.log('Could not decode structure at ' + start + ':', e);
849 }
850+ }
851+ if (sub === null) {
852+ if (len === null)
853+ throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
854+ stream.pos = start + Math.abs(len);
0000000000000855 }
856+ return new type(streamStart, header, len, tag, tagLen, sub);
857 }
0000000858859+}
00
+79-80
base64.js
···1// Base64 JavaScript decoder
2-// Copyright (c) 2008-2020 Lapo Luchini <lapo@lapo.it>
34// 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
···13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1516-(typeof define != 'undefined' ? define : function (factory) { 'use strict';
17- if (typeof module == 'object') module.exports = factory();
18- else window.base64 = factory();
19-})(function () {
20-"use strict";
21-22-var Base64 = {},
23- decoder, // populated on first usage
24 haveU8 = (typeof Uint8Array == 'function');
2526-Base64.decode = function (a) {
27- var isString = (typeof a == 'string');
28- var i;
29- if (decoder === undefined) {
30- var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
31- ignore = "= \f\n\r\t\u00A0\u2028\u2029";
32- decoder = [];
33- for (i = 0; i < 64; ++i)
34- decoder[b64.charCodeAt(i)] = i;
35- for (i = 0; i < ignore.length; ++i)
36- decoder[ignore.charCodeAt(i)] = -1;
37- // RFC 3548 URL & file safe encoding
38- decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
39- decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
40- }
41- var out = haveU8 ? new Uint8Array(a.length * 3 >> 2) : [];
42- var bits = 0, char_count = 0, len = 0;
43- for (i = 0; i < a.length; ++i) {
44- var c = isString ? a.charCodeAt(i) : a[i];
45- if (c == 61) // '='.charCodeAt(0)
0000000000000000000000000046 break;
47- c = decoder[c];
48- if (c == -1)
49- continue;
50- if (c === undefined)
51- throw 'Illegal character at offset ' + i;
52- bits |= c;
53- if (++char_count >= 4) {
54 out[len++] = (bits >> 16);
55 out[len++] = (bits >> 8) & 0xFF;
56- out[len++] = bits & 0xFF;
57- bits = 0;
58- char_count = 0;
59- } else {
60- bits <<= 6;
61 }
00062 }
63- switch (char_count) {
64- case 1:
65- throw "Base64 encoding incomplete: at least 2 bits missing";
66- case 2:
67- out[len++] = (bits >> 10);
68- break;
69- case 3:
70- out[len++] = (bits >> 16);
71- out[len++] = (bits >> 8) & 0xFF;
72- break;
73 }
74- if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters
75- out = out.subarray(0, len);
76- return out;
77-};
7879-Base64.pretty = function (str) {
80- // fix padding
81- if (str.length % 4 > 0)
82- str = (str + '===').slice(0, str.length + str.length % 4);
83- // convert RFC 3548 to standard Base64
84- str = str.replace(/-/g, '+').replace(/_/g, '/');
85- // 80 column width
86- return str.replace(/(.{80})/g, '$1\n');
87-};
88-89-Base64.re = /-----BEGIN [^-]+-----([A-Za-z0-9+\/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/;
90-Base64.unarmor = function (a) {
91- var m = Base64.re.exec(a);
92- if (m) {
93- if (m[1])
94- a = m[1];
95- else if (m[2])
96- a = m[2];
97- else
98- throw "RegExp out of sync";
99 }
100- return Base64.decode(a);
101-};
102103-return Base64;
104105-});
···1// Base64 JavaScript decoder
2+// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
34// 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
···13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1516+const
000000017 haveU8 = (typeof Uint8Array == 'function');
1819+let decoder; // populated on first usage
20+21+export class Base64 {
22+23+ static decode(a) {
24+ let isString = (typeof a == 'string');
25+ let i;
26+ if (decoder === undefined) {
27+ let b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
28+ ignore = '= \f\n\r\t\u00A0\u2028\u2029';
29+ decoder = [];
30+ for (i = 0; i < 64; ++i)
31+ decoder[b64.charCodeAt(i)] = i;
32+ for (i = 0; i < ignore.length; ++i)
33+ decoder[ignore.charCodeAt(i)] = -1;
34+ // also support decoding Base64url (RFC 4648 section 5)
35+ decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
36+ decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
37+ }
38+ let out = haveU8 ? new Uint8Array(a.length * 3 >> 2) : [];
39+ let bits = 0, char_count = 0, len = 0;
40+ for (i = 0; i < a.length; ++i) {
41+ let c = isString ? a.charCodeAt(i) : a[i];
42+ if (c == 61) // '='.charCodeAt(0)
43+ break;
44+ c = decoder[c];
45+ if (c == -1)
46+ continue;
47+ if (c === undefined)
48+ throw 'Illegal character at offset ' + i;
49+ bits |= c;
50+ if (++char_count >= 4) {
51+ out[len++] = (bits >> 16);
52+ out[len++] = (bits >> 8) & 0xFF;
53+ out[len++] = bits & 0xFF;
54+ bits = 0;
55+ char_count = 0;
56+ } else {
57+ bits <<= 6;
58+ }
59+ }
60+ switch (char_count) {
61+ case 1:
62+ throw 'Base64 encoding incomplete: at least 2 bits missing';
63+ case 2:
64+ out[len++] = (bits >> 10);
65 break;
66+ case 3:
00000067 out[len++] = (bits >> 16);
68 out[len++] = (bits >> 8) & 0xFF;
69+ break;
000070 }
71+ if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters
72+ out = out.subarray(0, len);
73+ return out;
74 }
75+76+ static pretty(str) {
77+ // fix padding
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)
82+ str = str.replace(/-/g, '+').replace(/_/g, '/');
83+ // 80 column width
84+ return str.replace(/.{80}/g, '$&\n');
85 }
00008687+ static unarmor(a) {
88+ let m = Base64.re.exec(a);
89+ if (m) {
90+ if (m[1])
91+ a = m[1];
92+ else if (m[2])
93+ a = m[2];
94+ else if (m[3])
95+ a = m[3];
96+ else
97+ throw 'RegExp out of sync';
98+ }
99+ return Base64.decode(a);
0000000100 }
00101102+}
103104+Base64.re = /-----BEGIN [^-]+-----([A-Za-z0-9+/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+/=\s]+)====|^([A-Za-z0-9+/=\s]+)$/;
···1+CMPv2 example as found on Wireshark page.
2+3+Original link:
4+https://wiki.wireshark.org/CMP
5+6+Attachment found moved here:
7+https://wiki.wireshark.org/uploads/__moin_import__/attachments/SampleCaptures/cmp_IR_sequence_OpenSSL-Cryptlib.pcap
8+9+begin-base64 644 cmpv2.der
10+MIICPjCB1QIBAqQCMACkRjBEMQswCQYDVQQGEwJERTEMMAoGA1UEChMDTlNOMREwDwYDVQQLEwhQ
11+RyBSREUgMzEUMBIGA1UEAxMLTWFydGluJ3MgQ0GgERgPMjAxMDA3MDUwNzM1MzhaoTwwOgYJKoZI
12+hvZ9B0INMC0EEJ5EpSD3zKjvmzHEK5+aoAAwCQYFKw4DAhoFAAICAfQwCgYIKwYBBQUIAQKiCwQJ
13+b/KGO0ILNJqApBIEEJGOKFG/9crkwU+z/I5ICa6lEgQQnnbd7EB2QjRCwOHt9QWdBKCCAUkwggFF
14+MIIBQTCBqAIBADCBoqaBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAqVTOtjEEYELkomc3sMOy
15+Too5a9YeC91IMn52cVx7doY4AeO6J9e8p+CtWNbzVF8aRgHUhh31m+/X3MkQOaY5i8nF33uxAxDL
16+MDXttHjsqrF/tsgYuuHSs/Znz4PA1kLkdhKE9DLiGlCFaJH5QY5Hzl6bcS3ApuWCny0RRzIA1/cC
17+AwEAAaGBkzANBgkqhkiG9w0BAQUFAAOBgQArOldjg75fDx7BaFp0oAknLDREvB1KyE+BV96R+lB+
18+tRRhwv3dyc/GTvRw4GtaeDjWCjNPaDCl9ZvvVljaR2aMZvhaQV+DUmCMjFSP3DPiGuszBA6R2azX
19+NKtnpJ3SGx2vk0+Iv05tXLhdnqQJZs5a3S3R30kn4Vw+4WQm3kb0fKAXAxUA9K8u+7hv5Rg6GDn6
20+aoPxbUo6fpU=
21+====
+9
examples/cms-password.p7m
···000000000
···1+This is a PKCS#7/CMS encrypted with passwod.
2+$ echo content | openssl cms -encrypt -pwri_password test -aes256 -outform pem -out examples/cms-password.p7m
3+-----BEGIN CMS-----
4+MIHYBgkqhkiG9w0BBwOggcowgccCAQMxgYOjgYACAQCgGwYJKoZIhvcNAQUMMA4E
5+CED/DSxXMtH6AgIIADAsBgsqhkiG9w0BCRADCTAdBglghkgBZQMEASoEEDIQbJMC
6+Sfb3LpwHduj/meQEMKwrwq5M4V0stztm6OUTAsFY2zKDY20SApwSEeEcAh9TM42E
7+1palnHeqHTBpC8pIpjA8BgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBByt+scPrdM
8+giR7WUOJyB3hgBDcD3UDMtZSep8X/3yy1/Yq
9+-----END CMS-----
+12
examples/crl-rfc5280.b64
···000000000000
···1+CRL example from RFC5280 as found here:
2+https://csrc.nist.gov/projects/pki-testing/sample-certificates-and-crls
3+4+begin-base64 644 crl-rfc5280.der
5+MIIBYDCBygIBATANBgkqhkiG9w0BAQUFADBDMRMwEQYKCZImiZPyLGQBGRYDY29tMRcwFQYKCZIm
6+iZPyLGQBGRYHZXhhbXBsZTETMBEGA1UEAxMKRXhhbXBsZSBDQRcNMDUwMjA1MTIwMDAwWhcNMDUw
7+MjA2MTIwMDAwWjAiMCACARIXDTA0MTExOTE1NTcwM1owDDAKBgNVHRUEAwoBAaAvMC0wHwYDVR0j
8+BBgwFoAUCGivhTPIOUp6+IKTjnBqSiCELDIwCgYDVR0UBAMCAQwwDQYJKoZIhvcNAQEFBQADgYEA
9+ItwYffcIzsx10NBqm60Q9HYjtIFutW2+DvsVFGzIF20f7pAXom9g5L2qjFXejoRvkvifEBInr0rU
10+L4XiNkR9qqNMJTgV/wD9Pn7uPSYS69jnK2LiK8NGgO94gtEVxtCccmrLznrtZ5mLbnCBfUNCdMGm
11+r8FVF6IzTNYGmCuk/C4=
12+====
+45
examples/crl-rfc5280.b64.dump
···000000000000000000000000000000000000000000000
···1+CertificateList SEQUENCE @0+352 (constructed): (3 elem)
2+ tbsCertList TBSCertList SEQUENCE @4+202 (constructed): (7 elem)
3+ version Version INTEGER @7+1: 1
4+ signature AlgorithmIdentifier SEQUENCE @10+13 (constructed): (2 elem)
5+ algorithm OBJECT_IDENTIFIER @12+9: 1.2.840.113549.1.1.5|sha1WithRSAEncryption|PKCS #1
6+ parameters ANY NULL @23+0
7+ issuer rdnSequence Name SEQUENCE @25+67 (constructed): (3 elem)
8+ RelativeDistinguishedName SET @27+19 (constructed): (1 elem)
9+ AttributeTypeAndValue SEQUENCE @29+17 (constructed): (2 elem)
10+ type AttributeType OBJECT_IDENTIFIER @31+10: 0.9.2342.19200300.100.1.25|domainComponent|Men are from Mars, this OID is from Pluto
11+ value AttributeValue [?] IA5String @43+3: com
12+ RelativeDistinguishedName SET @48+23 (constructed): (1 elem)
13+ AttributeTypeAndValue SEQUENCE @50+21 (constructed): (2 elem)
14+ type AttributeType OBJECT_IDENTIFIER @52+10: 0.9.2342.19200300.100.1.25|domainComponent|Men are from Mars, this OID is from Pluto
15+ value AttributeValue [?] IA5String @64+7: example
16+ RelativeDistinguishedName SET @73+19 (constructed): (1 elem)
17+ AttributeTypeAndValue SEQUENCE @75+17 (constructed): (2 elem)
18+ type AttributeType OBJECT_IDENTIFIER @77+3: 2.5.4.3|commonName|X.520 DN component
19+ value AttributeValue [?] PrintableString @82+10: Example CA
20+ thisUpdate utcTime Time UTCTime @94+13: 2005-02-05 12:00:00 UTC
21+ nextUpdate utcTime Time UTCTime @109+13: 2005-02-06 12:00:00 UTC
22+ revokedCertificates SEQUENCE @124+34 (constructed): (1 elem)
23+ SEQUENCE @126+32 (constructed): (3 elem)
24+ userCertificate CertificateSerialNumber INTEGER @128+1: 18
25+ revocationDate utcTime Time UTCTime @131+13: 2004-11-19 15:57:03 UTC
26+ crlEntryExtensions Extensions SEQUENCE @146+12 (constructed): (1 elem)
27+ Extension SEQUENCE @148+10 (constructed): (2 elem)
28+ extnID OBJECT_IDENTIFIER @150+3: 2.5.29.21|cRLReason|X.509 extension
29+ extnValue OCTET_STRING @155+3 (encapsulates): (3 byte)|0A0101
30+ ENUMERATED @157+1: 1
31+ crlExtensions [0] @160+47 (constructed): (1 elem)
32+ Extensions SEQUENCE @162+45 (constructed): (2 elem)
33+ Extension SEQUENCE @164+31 (constructed): (2 elem)
34+ extnID OBJECT_IDENTIFIER @166+3: 2.5.29.35|authorityKeyIdentifier|X.509 extension
35+ extnValue OCTET_STRING @171+24 (encapsulates): (24 byte)|301680140868AF8533C8394A7AF882938E706A4A20842C32
36+ SEQUENCE @173+22 (constructed): (1 elem)
37+ [0] @175+20: (20 byte)|0868AF8533C8394A7AF882938E706A4A20842C32
38+ Extension SEQUENCE @197+10 (constructed): (2 elem)
39+ extnID OBJECT_IDENTIFIER @199+3: 2.5.29.20|cRLNumber|X.509 extension
40+ extnValue OCTET_STRING @204+3 (encapsulates): (3 byte)|02010C
41+ INTEGER @206+1: 12
42+ signatureAlgorithm AlgorithmIdentifier SEQUENCE @209+13 (constructed): (2 elem)
43+ algorithm OBJECT_IDENTIFIER @211+9: 1.2.840.113549.1.1.5|sha1WithRSAEncryption|PKCS #1
44+ parameters ANY NULL @222+0
45+ signature BIT_STRING @224+129: (1024 bit)|0010001011011100000110000111110111110111000010001100111011001100011101011101000011010000011010101001101110101101000100001111010001110110001000111011010010000001011011101011010101101101101111100000111011111011000101010001010001101100110010000001011101101101000111111110111010010000000101111010001001101111011000001110010010111101101010101000110001010101110111101000111010000100011011111001001011111000100111110001000000010010001001111010111101001010110101000010111110000101111000100011011001000100011111011010101010100011010011000010010100111000000101011111111100000000111111010011111001111110111011100011110100100110000100101110101111011000111001110010101101100010111000100010101111000011010001101000000011101111011110001000001011010001000101011100011011010000100111000111001001101010110010111100111001111010111011010110011110011001100010110110111001110000100000010111110101000011010000100111010011000001101001101010111111000001010101010001011110100010001100110100110011010110000001101001100000101011101001001111110000101110
+13
examples/ed25519.cer
···0000000000000
···1+X.509 certificate based on Daniel J. Bernsteinโs Curve25519 (as per RFC 8410).
2+$ openssl req -x509 -newkey ed25519 -keyout test.key -out test.cer -days 3652 -subj '/C=IT/L=Milano/CN=Test ed25519'
3+-----BEGIN CERTIFICATE-----
4+MIIBfzCCATGgAwIBAgIUfI5kSdcO2S0+LkpdL3b2VUJG10YwBQYDK2VwMDUxCzAJ
5+BgNVBAYTAklUMQ8wDQYDVQQHDAZNaWxhbm8xFTATBgNVBAMMDFRlc3QgZWQyNTUx
6+OTAeFw0yMDA5MDIxMzI1MjZaFw0zMDA5MDIxMzI1MjZaMDUxCzAJBgNVBAYTAklU
7+MQ8wDQYDVQQHDAZNaWxhbm8xFTATBgNVBAMMDFRlc3QgZWQyNTUxOTAqMAUGAytl
8+cAMhADupL/3LF2beQKKS95PeMPgKI6gxIV3QB9hjJC7/aCGFo1MwUTAdBgNVHQ4E
9+FgQUa6W9z536I1l4EmQXrh5y2JqASugwHwYDVR0jBBgwFoAUa6W9z536I1l4EmQX
10+rh5y2JqASugwDwYDVR0TAQH/BAUwAwEB/zAFBgMrZXADQQBvc3e+KJZaMzbX5TT9
11+kPP9QH8fAvkAV/IWDxZrBL9lhLaY0tDSv0zWbw624uidBKPgmVD5wm3ec60dNVeF
12+ZYYG
13+-----END CERTIFICATE-----
···1+LDAPMessage example as found on ldap.com.
2+3+Original link:
4+https://ldap.com/ldapv3-wire-protocol-reference-ldap-message/
5+6+begin-base64 644 ldapmessage.der
7+MDUCAQVKEWRjPWV4YW1wbGUsZGM9Y29toB0wGwQWMS4yLjg0MC4xMTM1NTYuMS40LjgwNQEB/w==
8+====
···1+This is a PKCS#7/CMS detached digital signature.
2+It is an old example generated in 2008 and as such uses obsolete cryptography: RSA1024 with SHA1.
3+-----BEGIN PKCS7-----
4+MIIDUAYJKoZIhvcNAQcCoIIDQTCCAz0CAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3
5+DQEHAaCCAfMwggHvMIIBWKADAgECAhAvoXazbunwSfREtACZZhlFMA0GCSqGSIb3
6+DQEBBQUAMAwxCjAIBgNVBAMMAWEwHhcNMDgxMDE1MTUwMzQxWhcNMDkxMDE1MTUw
7+MzQxWjAMMQowCAYDVQQDDAFhMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ
8+Uwlwhu5hR8X01f+vG0mKPRHsVRjpZNxSEmsmFPdDiD9kylE3ertTDf0gRkpIvWfN
9+J+eymuxoXF0Qgl5gXAVuSrjupGD6J+VapixJiwLXJHokmDihLs3zfGARz08O3qnO
10+5ofBy0pRxq5isu/bAAcjoByZ1sI/g0iAuotC1UFObwIDAQABo1IwUDAOBgNVHQ8B
11+Af8EBAMCBPAwHQYDVR0OBBYEFEIGXQB4h+04Z3y/n7Nv94+CqPitMB8GA1UdIwQY
12+MBaAFEIGXQB4h+04Z3y/n7Nv94+CqPitMA0GCSqGSIb3DQEBBQUAA4GBAE0G7tAi
13+aacJxvP3fhEj+yP9VDxL0omrRRAEaMXwWaBf/Ggk1T/u+8/CDAdjuGNCiF6ctooK
14+c8u8KpnZJsGqnpGQ4n6L2KjTtRUDh+hija0eJRBFdirPQe2HAebQGFnmOk6Mn7Ki
15+QfBIsOzXim/bFqaBSbf06bLTQNwFouSO+jwOMYIBJTCCASECAQEwIDAMMQowCAYD
16+VQQDDAFhAhAvoXazbunwSfREtACZZhlFMAkGBSsOAwIaBQCgXTAYBgkqhkiG9w0B
17+CQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3DQEJBTEPFw0wODEwMTUxNTAzNDNaMCMG
18+CSqGSIb3DQEJBDEWBBQAAAAAAAAAAAAAAAAAAAAAAAAAADANBgkqhkiG9w0BAQEF
19+AASBgHQe0ocjBn+ZVXWbb8CpZ2CxzFKiVrgZxZO2kMBwJoNyZx+jnICHdhfsX4Or
20+cF5vIYVAZIRz5RxqFmuZELTfZ/K89zaK873DP9V7/ftBGpezWEp9h29AtAzI9lzS
21+GB9gugiyB5JstXoM1L87KJmT05MeZxg1pvvFhwo1m/QOpcqz
22+-----END PKCS7-----
···1// Hex JavaScript decoder
2-// Copyright (c) 2008-2020 Lapo Luchini <lapo@lapo.it>
34// 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
···13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1516-(typeof define != 'undefined' ? define : function (factory) { 'use strict';
17- if (typeof module == 'object') module.exports = factory();
18- else window.hex = factory();
19-})(function () {
20-"use strict";
21-22-var Hex = {},
23- decoder, // populated on first usage
24 haveU8 = (typeof Uint8Array == 'function');
2526-Hex.decode = function(a) {
27- var isString = (typeof a == 'string');
28- var i;
29- if (decoder === undefined) {
30- var hex = "0123456789ABCDEF",
31- ignore = " \f\n\r\t\u00A0\u2028\u2029";
32- decoder = [];
33- for (i = 0; i < 16; ++i)
34- decoder[hex.charCodeAt(i)] = i;
35- hex = hex.toLowerCase();
36- for (i = 10; i < 16; ++i)
37- decoder[hex.charCodeAt(i)] = i;
38- for (i = 0; i < ignore.length; ++i)
39- decoder[ignore.charCodeAt(i)] = -1;
40- }
41- var out = haveU8 ? new Uint8Array(a.length >> 1) : [],
42- bits = 0,
43- char_count = 0,
44- len = 0;
45- for (i = 0; i < a.length; ++i) {
46- var c = isString ? a.charCodeAt(i) : a[i];
47- c = decoder[c];
48- if (c == -1)
49- continue;
50- if (c === undefined)
51- throw 'Illegal character at offset ' + i;
52- bits |= c;
53- if (++char_count >= 2) {
54- out[len++] = bits;
55- bits = 0;
56- char_count = 0;
57- } else {
58- bits <<= 4;
00000000059 }
0000060 }
61- if (char_count)
62- throw "Hex encoding incomplete: 4 bits missing";
63- if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters
64- out = out.subarray(0, len);
65- return out;
66-};
6768-return Hex;
69-70-});
···1// Hex JavaScript decoder
2+// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it>
34// 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
···13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1516+const
000000017 haveU8 = (typeof Uint8Array == 'function');
1819+let decoder; // populated on first usage
20+21+export class Hex {
22+23+ /**
24+ * Decodes an hexadecimal value.
25+ * @param {string|Array|Uint8Array} a - a string representing hexadecimal data, or an array representation of its charcodes
26+ */
27+ static decode(a) {
28+ let isString = (typeof a == 'string');
29+ let i;
30+ if (decoder === undefined) {
31+ let hex = '0123456789ABCDEF',
32+ ignore = ' \f\n\r\t\u00A0\u2028\u2029';
33+ decoder = [];
34+ for (i = 0; i < 16; ++i)
35+ decoder[hex.charCodeAt(i)] = i;
36+ hex = hex.toLowerCase();
37+ for (i = 10; i < 16; ++i)
38+ decoder[hex.charCodeAt(i)] = i;
39+ for (i = 0; i < ignore.length; ++i)
40+ decoder[ignore.charCodeAt(i)] = -1;
41+ }
42+ let out = haveU8 ? new Uint8Array(a.length >> 1) : [],
43+ bits = 0,
44+ char_count = 0,
45+ len = 0;
46+ for (i = 0; i < a.length; ++i) {
47+ let c = isString ? a.charCodeAt(i) : a[i];
48+ c = decoder[c];
49+ if (c == -1)
50+ continue;
51+ if (c === undefined)
52+ throw 'Illegal character at offset ' + i;
53+ bits |= c;
54+ if (++char_count >= 2) {
55+ out[len++] = bits;
56+ bits = 0;
57+ char_count = 0;
58+ } else {
59+ bits <<= 4;
60+ }
61 }
62+ if (char_count)
63+ throw 'Hex encoding incomplete: 4 bits missing';
64+ if (haveU8 && out.length > len) // in case it was originally longer because of ignored characters
65+ out = out.subarray(0, len);
66+ return out;
67 }
0000006869+}
00
···1-(typeof define != 'undefined' ? define : function (factory) { 'use strict';
2- if (typeof module == 'object') factory(function (name) { return require('./' + name); });
3- else factory(function (name) { return window[name]; });
4-})(function (require) {
5-"use strict";
067-var ASN1 = require('asn1'),
8- Base64 = require('base64'),
9- Hex = require('hex'),
10 maxLength = 10240,
11 reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/,
12 tree = id('tree'),
13 dump = id('dump'),
14- wantHex = id('wantHex'),
0015 area = id('area'),
16 file = id('file'),
17- hash = null;
001819-require('dom'); // side effect: augment ASN1
00020function id(elem) {
21 return document.getElementById(elem);
22}
23function text(el, string) {
24- if ('textContent' in el)
25- el.textContent = string;
26- else
27- el.innerText = string;
00000028}
29-function decode(der, offset) {
30- offset = offset || 0;
31 tree.innerHTML = '';
32 dump.innerHTML = '';
0000000033 try {
34- var asn1 = ASN1.decode(der, offset);
35- tree.appendChild(asn1.toDOM());
36- if (wantHex.checked)
37- dump.appendChild(asn1.toHexDOM());
38- var b64 = (der.length < maxLength) ? asn1.toB64String() : '';
39- if (area.value === '')
40- area.value = Base64.pretty(b64);
00000000000000000000000000041 try {
42 window.location.hash = hash = '#' + b64;
43- } catch (e) { // fails with "Access Denied" on IE with URLs longer than ~2048 chars
044 window.location.hash = hash = '#';
45 }
46- var endOffset = asn1.posEnd();
47 if (endOffset < der.length) {
48- var p = document.createElement('p');
49 p.innerText = 'Input contains ' + (der.length - endOffset) + ' more bytes to decode.';
50- var button = document.createElement('button');
51 button.innerText = 'try to decode';
52 button.onclick = function () {
53 decode(der, endOffset);
···59 text(tree, e);
60 }
61}
62-function decodeText(val) {
63 try {
64- var der = reHex.test(val) ? Hex.decode(val) : Base64.unarmor(val);
65 decode(der);
66 } catch (e) {
67 text(tree, e);
68 dump.innerHTML = '';
69 }
70}
71-function decodeBinaryString(str) {
72- var der;
73 try {
74- if (reHex.test(str))
75- der = Hex.decode(str);
76- else if (Base64.re.test(str))
77- der = Base64.unarmor(str);
78- else
79- der = str;
80 decode(der);
81- } catch (e) {
82 text(tree, 'Cannot decode file.');
83 dump.innerHTML = '';
84 }
85}
86// set up buttons
87-id('butDecode').onclick = function () { decodeText(area.value); };
88-id('butClear').onclick = function () {
89- area.value = '';
90- tree.innerHTML = '';
91- dump.innerHTML = '';
92- hash = window.location.hash = '';
93-};
94-id('butExample').onclick = function () {
95- var demo = 'MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIAwggHvMIIBWKADAgECAhAvoXazbunwSfREtACZZhlFMA0GCSqGSIb3DQEBBQUAMAwxCjAIBgNVBAMMAWEwHhcNMDgxMDE1MTUwMzQxWhcNMDkxMDE1MTUwMzQxWjAMMQowCAYDVQQDDAFhMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJUwlwhu5hR8X01f-vG0mKPRHsVRjpZNxSEmsmFPdDiD9kylE3ertTDf0gRkpIvWfNJ-eymuxoXF0Qgl5gXAVuSrjupGD6J-VapixJiwLXJHokmDihLs3zfGARz08O3qnO5ofBy0pRxq5isu_bAAcjoByZ1sI_g0iAuotC1UFObwIDAQABo1IwUDAOBgNVHQ8BAf8EBAMCBPAwHQYDVR0OBBYEFEIGXQB4h-04Z3y_n7Nv94-CqPitMB8GA1UdIwQYMBaAFEIGXQB4h-04Z3y_n7Nv94-CqPitMA0GCSqGSIb3DQEBBQUAA4GBAE0G7tAiaacJxvP3fhEj-yP9VDxL0omrRRAEaMXwWaBf_Ggk1T_u-8_CDAdjuGNCiF6ctooKc8u8KpnZJsGqnpGQ4n6L2KjTtRUDh-hija0eJRBFdirPQe2HAebQGFnmOk6Mn7KiQfBIsOzXim_bFqaBSbf06bLTQNwFouSO-jwOAAAxggElMIIBIQIBATAgMAwxCjAIBgNVBAMMAWECEC-hdrNu6fBJ9ES0AJlmGUUwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTA4MTAxNTE1MDM0M1owIwYJKoZIhvcNAQkEMRYEFAAAAAAAAAAAAAAAAAAAAAAAAAAAMA0GCSqGSIb3DQEBAQUABIGAdB7ShyMGf5lVdZtvwKlnYLHMUqJWuBnFk7aQwHAmg3JnH6OcgId2F-xfg6twXm8hhUBkhHPlHGoWa5kQtN9n8rz3NorzvcM_1Xv9-0Eal7NYSn2Hb0C0DMj2XNIYH2C6CLIHkmy1egzUvzsomZPTkx5nGDWm-8WHCjWb9A6lyrMAAAAAAAA';
96- decodeText(demo);
0000000000000000097};
0000098// this is only used if window.FileReader
99function read(f) {
100 area.value = ''; // clear text area, will get b64 content
101- var r = new FileReader();
102 r.onloadend = function () {
103- if (r.error)
104- alert("Your browser couldn't read the specified file (error code " + r.error.code + ").");
105- else
106- decodeBinaryString(r.result);
107 };
108 r.readAsBinaryString(f);
109}
110function load() {
111- if (file.files.length === 0)
112- alert("Select a file to load first.");
113- else
114- read(file.files[0]);
115}
116function loadFromHash() {
117 if (window.location.hash && window.location.hash != hash) {
···119 // Firefox is not consistent with other browsers and returns an
120 // already-decoded hash string so we risk double-decoding here,
121 // but since % is not allowed in base64 nor hexadecimal, it's ok
122- var val = decodeURIComponent(hash.substr(1));
123- if (val.length)
124- decodeText(val);
125 }
126}
127function stop(e) {
···130}
131function dragAccept(e) {
132 stop(e);
133- if (e.dataTransfer.files.length > 0)
134- read(e.dataTransfer.files[0]);
135}
136// main
137-if ('onhashchange' in window)
138- window.onhashchange = loadFromHash;
139loadFromHash();
140document.ondragover = stop;
141document.ondragleave = stop;
142-if ('FileReader' in window && 'readAsBinaryString' in (new FileReader())) {
143 file.style.display = 'block';
144 file.onchange = load;
145 document.ondrop = dragAccept;
146}
147-148-});
000000000
···1+import './theme.js';
2+import { ASN1DOM } from './dom.js';
3+import { Base64 } from './base64.js';
4+import { Hex } from './hex.js';
5+import { Defs } from './defs.js';
6+import { tags } from './tags.js';
78+const
009 maxLength = 10240,
10 reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/,
11 tree = id('tree'),
12 dump = id('dump'),
13+ wantHex = checkbox('wantHex'),
14+ trimHex = checkbox('trimHex'),
15+ wantDef = checkbox('wantDef'),
16 area = id('area'),
17 file = id('file'),
18+ examples = id('examples'),
19+ selectDefs = id('definitions'),
20+ selectTag = id('tags');
2122+let hash = null;
23+24+if (!window.console || !window.console.log) // IE8 with closed developer tools
25+ window.console = { log: function () {} };
26function id(elem) {
27 return document.getElementById(elem);
28}
29function text(el, string) {
30+ if ('textContent' in el) el.textContent = string;
31+ else el.innerText = string;
32+}
33+function checkbox(name) {
34+ const el = id(name);
35+ const cfg = localStorage.getItem(name);
36+ if (cfg === 'false')
37+ el.checked = false;
38+ el.onchange = () => localStorage.setItem(name, el.checked);
39+ return el;
40}
41+function show(asn1) {
042 tree.innerHTML = '';
43 dump.innerHTML = '';
44+ let ul = document.createElement('ul');
45+ ul.className = 'treecollapse';
46+ tree.appendChild(ul);
47+ ul.appendChild(asn1.toDOM());
48+ if (wantHex.checked) dump.appendChild(asn1.toHexDOM(undefined, trimHex.checked));
49+}
50+export function decode(der, offset) {
51+ offset = offset || 0;
52 try {
53+ const asn1 = ASN1DOM.decode(der, offset);
54+ if (wantDef.checked) {
55+ selectDefs.innerHTML = '';
56+ const types = Defs.commonTypes
57+ .map(type => {
58+ const stats = Defs.match(asn1, type);
59+ return { type, match: stats.recognized / stats.total };
60+ })
61+ .sort((a, b) => b.match - a.match);
62+ for (const t of types) {
63+ t.element = document.createElement('option');
64+ t.element.innerText = (t.match * 100).toFixed(1) + '% ' + t.type.description;
65+ selectDefs.appendChild(t.element);
66+ }
67+ let not = document.createElement('option');
68+ not.innerText = 'no definition';
69+ selectDefs.appendChild(not);
70+ Defs.match(asn1, types[0].type);
71+ selectDefs.onchange = () => {
72+ for (const t of types) {
73+ if (t.element == selectDefs.selectedOptions[0]) {
74+ Defs.match(asn1, t.type);
75+ show(asn1);
76+ return;
77+ }
78+ }
79+ Defs.match(asn1, null);
80+ show(asn1);
81+ };
82+ } else
83+ selectDefs.innerHTML = '<option>no definition</option>';
84+ show(asn1);
85+ let b64 = der.length < maxLength ? asn1.toB64String() : '';
86+ if (area.value === '') area.value = Base64.pretty(b64);
87 try {
88 window.location.hash = hash = '#' + b64;
89+ } catch (ignore) {
90+ // fails with "Access Denied" on IE with URLs longer than ~2048 chars
91 window.location.hash = hash = '#';
92 }
93+ let endOffset = asn1.posEnd();
94 if (endOffset < der.length) {
95+ let p = document.createElement('p');
96 p.innerText = 'Input contains ' + (der.length - endOffset) + ' more bytes to decode.';
97+ let button = document.createElement('button');
98 button.innerText = 'try to decode';
99 button.onclick = function () {
100 decode(der, endOffset);
···106 text(tree, e);
107 }
108}
109+export function decodeText(val) {
110 try {
111+ let der = reHex.test(val) ? Hex.decode(val) : Base64.unarmor(val);
112 decode(der);
113 } catch (e) {
114 text(tree, e);
115 dump.innerHTML = '';
116 }
117}
118+export function decodeBinaryString(str) {
119+ let der;
120 try {
121+ if (reHex.test(str)) der = Hex.decode(str);
122+ else if (Base64.re.test(str)) der = Base64.unarmor(str);
123+ else der = str;
000124 decode(der);
125+ } catch (ignore) {
126 text(tree, 'Cannot decode file.');
127 dump.innerHTML = '';
128 }
129}
130// set up buttons
131+const butClickHandlers = {
132+ butDecode: () => {
133+ decodeText(area.value);
134+ },
135+ butClear: () => {
136+ area.value = '';
137+ file.value = '';
138+ tree.innerHTML = '';
139+ dump.innerHTML = '';
140+ selectDefs.innerHTML = '';
141+ hash = window.location.hash = '';
142+ },
143+ butExample: () => {
144+ console.log('Loading example:', examples.value);
145+ let request = new XMLHttpRequest();
146+ request.open('GET', 'examples/' + examples.value, true);
147+ request.onreadystatechange = function () {
148+ if (this.readyState !== 4) return;
149+ if (this.status >= 200 && this.status < 400) {
150+ area.value = this.responseText;
151+ decodeText(this.responseText);
152+ } else {
153+ console.log('Error loading example.');
154+ }
155+ };
156+ request.send();
157+ },
158};
159+for (const [name, onClick] of Object.entries(butClickHandlers)) {
160+ let elem = id(name);
161+ if (elem)
162+ elem.onclick = onClick;
163+}
164// this is only used if window.FileReader
165function read(f) {
166 area.value = ''; // clear text area, will get b64 content
167+ let r = new FileReader();
168 r.onloadend = function () {
169+ if (r.error) alert("Your browser couldn't read the specified file (error code " + r.error.code + ').');
170+ else decodeBinaryString(r.result);
00171 };
172 r.readAsBinaryString(f);
173}
174function load() {
175+ if (file.files.length === 0) alert('Select a file to load first.');
176+ else read(file.files[0]);
00177}
178function loadFromHash() {
179 if (window.location.hash && window.location.hash != hash) {
···181 // Firefox is not consistent with other browsers and returns an
182 // already-decoded hash string so we risk double-decoding here,
183 // but since % is not allowed in base64 nor hexadecimal, it's ok
184+ let val = decodeURIComponent(hash.substr(1));
185+ if (val.length) decodeText(val);
0186 }
187}
188function stop(e) {
···191}
192function dragAccept(e) {
193 stop(e);
194+ if (e.dataTransfer.files.length > 0) read(e.dataTransfer.files[0]);
0195}
196// main
197+if ('onhashchange' in window) window.onhashchange = loadFromHash;
0198loadFromHash();
199document.ondragover = stop;
200document.ondragleave = stop;
201+if ('FileReader' in window && 'readAsBinaryString' in new FileReader()) {
202 file.style.display = 'block';
203 file.onchange = load;
204 document.ondrop = dragAccept;
205}
206+for (let tag in tags) {
207+ let date = tags[tag];
208+ let el = document.createElement('option');
209+ el.value = tag;
210+ el.innerText = date + ' ' + tag;
211+ selectTag.appendChild(el);
212+}
213+selectTag.onchange = function (ev) {
214+ let tag = ev.target.selectedOptions[0].value;
215+ window.location.href = 'https://rawcdn.githack.com/lapo-luchini/asn1js/' + tag + '/index.html';
216+};
-90
int10.js
···1-// Big integer base-10 printing library
2-// Copyright (c) 2008-2020 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-(typeof define != 'undefined' ? define : function (factory) { 'use strict';
17- if (typeof module == 'object') module.exports = factory();
18- else window.int10 = factory();
19-})(function () {
20-"use strict";
21-22-var max = 10000000000000; // biggest 10^n integer that can still fit 2^53 when multiplied by 256
23-24-function Int10(value) {
25- this.buf = [+value || 0];
26-}
27-28-Int10.prototype.mulAdd = function (m, c) {
29- // assert(m <= 256)
30- var b = this.buf,
31- l = b.length,
32- i, t;
33- for (i = 0; i < l; ++i) {
34- t = b[i] * m + c;
35- if (t < max)
36- c = 0;
37- else {
38- c = 0|(t / max);
39- t -= c * max;
40- }
41- b[i] = t;
42- }
43- if (c > 0)
44- b[i] = c;
45-};
46-47-Int10.prototype.sub = function (c) {
48- // assert(m <= 256)
49- var b = this.buf,
50- l = b.length,
51- i, t;
52- for (i = 0; i < l; ++i) {
53- t = b[i] - c;
54- if (t < 0) {
55- t += max;
56- c = 1;
57- } else
58- c = 0;
59- b[i] = t;
60- }
61- while (b[b.length - 1] === 0)
62- b.pop();
63-};
64-65-Int10.prototype.toString = function (base) {
66- if ((base || 10) != 10)
67- throw 'only base 10 is supported';
68- var b = this.buf,
69- s = b[b.length - 1].toString();
70- for (var i = b.length - 2; i >= 0; --i)
71- s += (max + b[i]).toString().substring(1);
72- return s;
73-};
74-75-Int10.prototype.valueOf = function () {
76- var b = this.buf,
77- v = 0;
78- for (var i = b.length - 1; i >= 0; --i)
79- v = v * max + b[i];
80- return v;
81-};
82-83-Int10.prototype.simplify = function () {
84- var b = this.buf;
85- return (b.length == 1) ? b[0] : this;
86-};
87-88-return Int10;
89-90-});
···1+#/bin/sh
2+URL='https://www.cs.auckland.ac.nz/~pgut001/dumpasn1.cfg'
3+if [ -x /usr/bin/fetch ]; then
4+ /usr/bin/fetch -m --no-verify-peer $URL
5+elif [ -x /usr/bin/wget ]; then
6+ /usr/bin/wget -N --no-check-certificate $URL
7+elif [ ! -r dumpasn1.cfg ]; then
8+ echo Please download $URL in this directory.
9+ exit 1
10+fi
11+cat dumpasn1.cfg | \
12+tr -d '\r' | \
13+awk -v apos="'" -v q='"' -v url="$URL" '
14+ function clean() {
15+ oid = "";
16+ comment = "";
17+ description = "";
18+ warning = "";
19+ }
20+ BEGIN {
21+ FS = "= *";
22+ clean();
23+ print "// Converted from: " url;
24+ print "// which is made by Peter Gutmann and whose license states:";
25+ print "// You can use this code in whatever way you want,";
26+ print "// as long as you don" apos "t try to claim you wrote it.";
27+ print "export const oids = {";
28+ }
29+ /^OID/ { oid = $2; }
30+ /^Comment/ { comment = $2; }
31+ /^Description/ { description = $2; }
32+ /^Warning/ { warning = ", \"w\": true"; }
33+ /^$/ {
34+ if (length(oid) > 0) {
35+ gsub(" ", ".", oid);
36+ gsub("\"", "\\\"", description);
37+ gsub("\"", "\\\"", comment);
38+ if (++seen[oid] > 1)
39+ print "Duplicate OID in line " NR ": " oid > "/dev/stderr";
40+ else
41+ printf "\"%s\": { \"d\": \"%s\", \"c\": \"%s\"%s },\n", oid, description, comment, warning;
42+ clean();
43+ }
44+ }
45+ END {
46+ print "};"
47+ }
48+' >oids.js
49+echo Conversion completed.
+32
updateRFC.sh
···00000000000000000000000000000000
···1+#/bin/sh
2+RFCs="5280 5208 3369 3161 2986 4211 4210 8017 4511"
3+downloadRFC() {
4+ URL="https://www.ietf.org/rfc/rfc$1.txt"
5+ if [ -x /usr/bin/fetch ]; then
6+ /usr/bin/fetch -m --no-verify-peer $URL
7+ elif [ -x /usr/bin/wget ]; then
8+ /usr/bin/wget -N --no-check-certificate $URL
9+ elif [ ! -r dumpasn1.cfg ]; then
10+ echo Please download $URL in this directory.
11+ exit 1
12+ fi
13+}
14+echo '{}' > rfcdef.json # start from scratch
15+mkdir -p rfc
16+cd rfc
17+for n in $RFCs; do
18+ downloadRFC $n
19+ ../parseRFC.js rfc$n.txt ../rfcdef.json
20+done
21+cd ..
22+{
23+ echo "// content parsed from ASN.1 definitions as found in the following RFCs: $RFCs"
24+ echo "// Copyright (C) The IETF Trust (2008)"
25+ echo "// as far as I can tell this file is allowed under the following clause:"
26+ echo "// It is acceptable under the current IETF rules (RFC 5378) to modify extracted code if necessary."
27+ echo "// https://trustee.ietf.org/about/faq/#reproducing-rfcs"
28+ echo -n "export const rfcdef = "
29+ cat rfcdef.json
30+ echo ";"
31+} > rfcdef.js
32+echo Conversion completed.