Skip to content

Commit 9d47ef0

Browse files
committed
[enh] rewritten x509.js without deprecated String methods
1 parent 3d7b78c commit 9d47ef0

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

‎njs/http/certs/js/x509.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function asn1_parse_oid(buf) {
22
var oid = [];
33
var sid = 0;
4-
var cur_octet = buf.charCodeAt(0)
4+
var cur_octet = buf[0];
55

66
if (cur_octet < 40) {
77
oid.push(0);
@@ -17,7 +17,7 @@ function asn1_parse_oid(buf) {
1717
}
1818

1919
for (var n = 1; n < buf.length; n++) {
20-
cur_octet = buf.charCodeAt(n);
20+
cur_octet = buf[n];
2121

2222
if (cur_octet < 0x80) {
2323
sid += cur_octet;
@@ -36,7 +36,7 @@ function asn1_parse_oid(buf) {
3636
}
3737
}
3838

39-
if (buf.slice(-1).charCodeAt(0) >= 0x80)
39+
if (buf.slice(-1)[0] >= 0x80)
4040
throw "Last octet in oid buffer has highest bit set to 1";
4141

4242
return oid.join('.')
@@ -52,19 +52,19 @@ function asn1_parse_integer(buf) {
5252
var value = 0;
5353
var is_negative = false;
5454

55-
if (buf.charCodeAt(0) & 0x80) {
55+
if (buf[0] & 0x80) {
5656
is_negative = true;
57-
value = buf.charCodeAt(0) & 0x7f;
57+
value = buf[0] & 0x7f;
5858
var compl_int = 1 << (8 * buf.length - 1)
5959

6060
} else {
61-
value = buf.charCodeAt(0);
61+
value = buf[0];
6262
}
6363

6464
if (buf.length > 1) {
6565
for (var n = 1; n < buf.length; n++) {
6666
value <<= 8;
67-
value += buf.charCodeAt(n);
67+
value += buf[n];
6868
}
6969
}
7070

@@ -75,18 +75,18 @@ function asn1_parse_integer(buf) {
7575
}
7676

7777
function asn1_parse_ascii_string(buf) {
78-
return buf;
78+
return buf.toString();
7979
}
8080

8181
function asn1_parse_ia5_string(buf) {
8282
if (is_ia5(buf))
83-
return buf;
83+
return buf.toString();
8484
else
8585
throw "Not a IA5String: " + buf;
8686
}
8787

8888
function asn1_parse_utf8_string(buf) {
89-
return buf.fromUTF8()
89+
return buf.toString('utf8');
9090
}
9191

9292
function asn1_parse_bmp_string(buf) {
@@ -98,10 +98,10 @@ function asn1_parse_universal_string(buf) {
9898
}
9999

100100
function asn1_parse_bit_string(buf) {
101-
if (buf.charCodeAt(0) == 0)
101+
if (buf[0] == 0)
102102
return buf.slice(1).toString("hex");
103103

104-
var shift = buf.charCodeAt(0)
104+
var shift = buf[0]
105105
if (shift > 7)
106106
throw "Incorrect shift in bitstring: " + shift;
107107

@@ -110,10 +110,10 @@ function asn1_parse_bit_string(buf) {
110110
var symbol = "";
111111

112112
// shift string right and convert to hex
113-
for (n = 1; n < buf.length; n++) {
114-
var char_code = buf.charCodeAt(n) >> shift + upper_bits;
115-
symbol = String.fromCharCode(char_code).toBytes().toString("hex");
116-
upper_bits = (buf.charCodeAt(n) << shift) & 0xff;
113+
for (var n = 1; n < buf.length; n++) {
114+
var char_code = buf[n] >> shift + upper_bits;
115+
symbol = char_code.toString(16);
116+
upper_bits = (buf[n] << shift) & 0xff;
117117
value += symbol;
118118
}
119119

@@ -130,7 +130,7 @@ function asn1_parse_any(buf) {
130130

131131
function is_ia5(buf) {
132132
for (var n = 0; n < buf.length; n++) {
133-
var s = buf.charCodeAt(n);
133+
var s = buf[n];
134134
if (s > 0x7e)
135135
return false;
136136
}
@@ -139,7 +139,7 @@ function is_ia5(buf) {
139139
}
140140

141141
function asn1_read_length(buf, pointer) {
142-
var s = buf.charCodeAt(pointer);
142+
var s = buf[pointer];
143143
if (s == 0x80 || s == 0xff)
144144
throw "indefinite length is not supported"
145145

@@ -158,8 +158,8 @@ function asn1_read_length(buf, pointer) {
158158

159159
var length = 0;
160160
for (var n = 0; n < l; n++) {
161-
length += Math.pow(256, l - n - 1) * buf.charCodeAt(++pointer);
162-
if (n == 6 && buf.charCodeAt(pointer) > 0x1f)
161+
length += Math.pow(256, l - n - 1) * buf[++pointer];
162+
if (n == 6 && buf[pointer] > 0x1f)
163163
throw "Too big length, exceeds MAX_SAFE_INTEGER";
164164
}
165165

@@ -256,7 +256,7 @@ function asn1_read(buf) {
256256

257257
while (pointer < buf.length) {
258258
// read type: 7 & 8 bits define class, 6 bit if it is constructed
259-
s = buf.charCodeAt(pointer);
259+
s = buf[pointer];
260260
tag_class = s >> 6;
261261
is_constructed = s & 0x20;
262262
tag = s & 0x1f;
@@ -275,9 +275,9 @@ function asn1_read(buf) {
275275
throw "Went out of buffer: " + pointer + " " + buf.length;
276276

277277
tag <<= 7;
278-
tag += (buf.charCodeAt(pointer) & 0x7f);
278+
tag += (buf[pointer] & 0x7f);
279279

280-
} while (buf.charCodeAt(pointer) > 0x80)
280+
} while (buf[pointer] > 0x80)
281281
}
282282

283283
if (++pointer > buf.length)
@@ -369,9 +369,7 @@ function parse_pem_cert(pem) {
369369
der = der.slice(1, -2);
370370
}
371371

372-
der = atob(der.join(''));
373-
374-
return asn1_read(der);
372+
return asn1_read(Buffer.from(der.join(''), 'base64'));
375373
}
376374

377375
export default {asn1_read, parse_pem_cert, is_oid_exist, get_oid_value, get_oid_value_all};

0 commit comments

Comments
 (0)