@lapo/asn1js 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/asn1.js +22 -16
  2. package/base64.js +6 -5
  3. package/package.json +2 -2
package/asn1.js CHANGED
@@ -21,7 +21,8 @@ const
21
21
  reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
22
22
  reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|(-(?:0\d|1[0-2])|[+](?:0\d|1[0-4]))([0-5]\d)?)?$/,
23
23
  hexDigits = '0123456789ABCDEF',
24
- b64Safe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
24
+ b64Std = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
25
+ b64URL = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
25
26
  tableT61 = [
26
27
  ['', ''],
27
28
  ['AEIOUaeiou', 'ÀÈÌÒÙàèìòù'], // Grave
@@ -58,7 +59,7 @@ function checkPrintable(s) {
58
59
 
59
60
  /** Class to manage a stream of bytes, with a zero-copy approach.
60
61
  * It uses an existing array or binary string and advances a position index. */
61
- class Stream {
62
+ export class Stream {
62
63
 
63
64
  /**
64
65
  * @param {Stream|array|string} enc data (will not be copied)
@@ -98,7 +99,7 @@ class Stream {
98
99
  /** Hexadecimal dump of a specified region of the stream.
99
100
  * @param {number} start starting position (included)
100
101
  * @param {number} end ending position (excluded)
101
- * @param {string} type 'raw', 'byte' or 'dump' */
102
+ * @param {string} type 'raw', 'byte' or 'dump' (default) */
102
103
  hexDump(start, end, type = 'dump') {
103
104
  let s = '';
104
105
  for (let i = start; i < end; ++i) {
@@ -114,26 +115,29 @@ class Stream {
114
115
  }
115
116
  return s;
116
117
  }
117
- /** Base-64 dump of a specified region of the stream.
118
+ /** Base64url dump of a specified region of the stream (according to RFC 4648 section 5).
118
119
  * @param {number} start starting position (included)
119
- * @param {number} end ending position (excluded) */
120
- b64Dump(start, end) {
120
+ * @param {number} end ending position (excluded)
121
+ * @param {string} type 'url' (default, section 5 without padding) or 'std' (section 4 with padding) */
122
+ b64Dump(start, end, type = 'url') {
123
+ const b64 = type === 'url' ? b64URL : b64Std;
121
124
  let extra = (end - start) % 3,
122
125
  s = '',
123
126
  i, c;
124
127
  for (i = start; i + 2 < end; i += 3) {
125
128
  c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
126
- s += b64Safe.charAt(c >> 18 & 0x3F);
127
- s += b64Safe.charAt(c >> 12 & 0x3F);
128
- s += b64Safe.charAt(c >> 6 & 0x3F);
129
- s += b64Safe.charAt(c & 0x3F);
129
+ s += b64.charAt(c >> 18 & 0x3F);
130
+ s += b64.charAt(c >> 12 & 0x3F);
131
+ s += b64.charAt(c >> 6 & 0x3F);
132
+ s += b64.charAt(c & 0x3F);
130
133
  }
131
134
  if (extra > 0) {
132
135
  c = this.get(i) << 16;
133
136
  if (extra > 1) c |= this.get(i + 1) << 8;
134
- s += b64Safe.charAt(c >> 18 & 0x3F);
135
- s += b64Safe.charAt(c >> 12 & 0x3F);
136
- if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
137
+ s += b64.charAt(c >> 18 & 0x3F);
138
+ s += b64.charAt(c >> 12 & 0x3F);
139
+ if (extra == 2) s += b64.charAt(c >> 6 & 0x3F);
140
+ if (b64 === b64Std) s += '==='.slice(0, 3 - extra);
137
141
  }
138
142
  return s;
139
143
  }
@@ -557,9 +561,11 @@ export class ASN1 {
557
561
  toHexString(type = 'raw') {
558
562
  return this.stream.hexDump(this.posStart(), this.posEnd(), type);
559
563
  }
560
- /** Base64 dump of the node. */
561
- toB64String() {
562
- return this.stream.b64Dump(this.posStart(), this.posEnd());
564
+ /** Base64url dump of the node (according to RFC 4648 section 5).
565
+ * @param {string} type 'url' (default, section 5 without padding) or 'std' (section 4 with padding)
566
+ */
567
+ toB64String(type = 'url') {
568
+ return this.stream.b64Dump(this.posStart(), this.posEnd(), type);
563
569
  }
564
570
  static decodeLength(stream) {
565
571
  let buf = stream.get(),
package/base64.js CHANGED
@@ -31,7 +31,7 @@ export class Base64 {
31
31
  decoder[b64.charCodeAt(i)] = i;
32
32
  for (i = 0; i < ignore.length; ++i)
33
33
  decoder[ignore.charCodeAt(i)] = -1;
34
- // RFC 3548 URL & file safe encoding
34
+ // also support decoding Base64url (RFC 4648 section 5)
35
35
  decoder['-'.charCodeAt(0)] = decoder['+'.charCodeAt(0)];
36
36
  decoder['_'.charCodeAt(0)] = decoder['/'.charCodeAt(0)];
37
37
  }
@@ -75,12 +75,13 @@ export class Base64 {
75
75
 
76
76
  static pretty(str) {
77
77
  // fix padding
78
- if (str.length % 4 > 0)
79
- str = (str + '===').slice(0, str.length + str.length % 4);
80
- // convert RFC 3548 to standard Base64
78
+ let pad = 4 - str.length % 4;
79
+ if (pad < 4)
80
+ str += '==='.slice(0, pad);
81
+ // convert Base64url (RFC 4648 section 5) to standard Base64 (RFC 4648 section 4)
81
82
  str = str.replace(/-/g, '+').replace(/_/g, '/');
82
83
  // 80 column width
83
- return str.replace(/(.{80})/g, '$1\n');
84
+ return str.replace(/.{80}/g, '$&\n');
84
85
  }
85
86
 
86
87
  static unarmor(a) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lapo/asn1js",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "Generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.",
5
5
  "type": "module",
6
6
  "main": "asn1.js",
@@ -18,7 +18,7 @@
18
18
  "lint": "npx eslint asn1.js base64.js hex.js int10.js dom.js defs.js oids.js rfcdef.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js vite.config.js theme.js",
19
19
  "lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
20
20
  "build": "vite build",
21
- "serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
21
+ "serve": "npx -p local-web-server ws",
22
22
  "test": "node test",
23
23
  "testdefs": "node testDefs"
24
24
  },