1
1
function asn1_parse_oid ( buf ) {
2
2
var oid = [ ] ;
3
3
var sid = 0 ;
4
- var cur_octet = buf . charCodeAt ( 0 )
4
+ var cur_octet = buf [ 0 ] ;
5
5
6
6
if ( cur_octet < 40 ) {
7
7
oid . push ( 0 ) ;
@@ -17,7 +17,7 @@ function asn1_parse_oid(buf) {
17
17
}
18
18
19
19
for ( var n = 1 ; n < buf . length ; n ++ ) {
20
- cur_octet = buf . charCodeAt ( n ) ;
20
+ cur_octet = buf [ n ] ;
21
21
22
22
if ( cur_octet < 0x80 ) {
23
23
sid += cur_octet ;
@@ -36,7 +36,7 @@ function asn1_parse_oid(buf) {
36
36
}
37
37
}
38
38
39
- if ( buf . slice ( - 1 ) . charCodeAt ( 0 ) >= 0x80 )
39
+ if ( buf . slice ( - 1 ) [ 0 ] >= 0x80 )
40
40
throw "Last octet in oid buffer has highest bit set to 1" ;
41
41
42
42
return oid . join ( '.' )
@@ -52,19 +52,19 @@ function asn1_parse_integer(buf) {
52
52
var value = 0 ;
53
53
var is_negative = false ;
54
54
55
- if ( buf . charCodeAt ( 0 ) & 0x80 ) {
55
+ if ( buf [ 0 ] & 0x80 ) {
56
56
is_negative = true ;
57
- value = buf . charCodeAt ( 0 ) & 0x7f ;
57
+ value = buf [ 0 ] & 0x7f ;
58
58
var compl_int = 1 << ( 8 * buf . length - 1 )
59
59
60
60
} else {
61
- value = buf . charCodeAt ( 0 ) ;
61
+ value = buf [ 0 ] ;
62
62
}
63
63
64
64
if ( buf . length > 1 ) {
65
65
for ( var n = 1 ; n < buf . length ; n ++ ) {
66
66
value <<= 8 ;
67
- value += buf . charCodeAt ( n ) ;
67
+ value += buf [ n ] ;
68
68
}
69
69
}
70
70
@@ -75,18 +75,18 @@ function asn1_parse_integer(buf) {
75
75
}
76
76
77
77
function asn1_parse_ascii_string ( buf ) {
78
- return buf ;
78
+ return buf . toString ( ) ;
79
79
}
80
80
81
81
function asn1_parse_ia5_string ( buf ) {
82
82
if ( is_ia5 ( buf ) )
83
- return buf ;
83
+ return buf . toString ( ) ;
84
84
else
85
85
throw "Not a IA5String: " + buf ;
86
86
}
87
87
88
88
function asn1_parse_utf8_string ( buf ) {
89
- return buf . fromUTF8 ( )
89
+ return buf . toString ( 'utf8' ) ;
90
90
}
91
91
92
92
function asn1_parse_bmp_string ( buf ) {
@@ -98,10 +98,10 @@ function asn1_parse_universal_string(buf) {
98
98
}
99
99
100
100
function asn1_parse_bit_string ( buf ) {
101
- if ( buf . charCodeAt ( 0 ) == 0 )
101
+ if ( buf [ 0 ] == 0 )
102
102
return buf . slice ( 1 ) . toString ( "hex" ) ;
103
103
104
- var shift = buf . charCodeAt ( 0 )
104
+ var shift = buf [ 0 ]
105
105
if ( shift > 7 )
106
106
throw "Incorrect shift in bitstring: " + shift ;
107
107
@@ -110,10 +110,10 @@ function asn1_parse_bit_string(buf) {
110
110
var symbol = "" ;
111
111
112
112
// 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 ;
117
117
value += symbol ;
118
118
}
119
119
@@ -130,7 +130,7 @@ function asn1_parse_any(buf) {
130
130
131
131
function is_ia5 ( buf ) {
132
132
for ( var n = 0 ; n < buf . length ; n ++ ) {
133
- var s = buf . charCodeAt ( n ) ;
133
+ var s = buf [ n ] ;
134
134
if ( s > 0x7e )
135
135
return false ;
136
136
}
@@ -139,7 +139,7 @@ function is_ia5(buf) {
139
139
}
140
140
141
141
function asn1_read_length ( buf , pointer ) {
142
- var s = buf . charCodeAt ( pointer ) ;
142
+ var s = buf [ pointer ] ;
143
143
if ( s == 0x80 || s == 0xff )
144
144
throw "indefinite length is not supported"
145
145
@@ -158,8 +158,8 @@ function asn1_read_length(buf, pointer) {
158
158
159
159
var length = 0 ;
160
160
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 )
163
163
throw "Too big length, exceeds MAX_SAFE_INTEGER" ;
164
164
}
165
165
@@ -256,7 +256,7 @@ function asn1_read(buf) {
256
256
257
257
while ( pointer < buf . length ) {
258
258
// read type: 7 & 8 bits define class, 6 bit if it is constructed
259
- s = buf . charCodeAt ( pointer ) ;
259
+ s = buf [ pointer ] ;
260
260
tag_class = s >> 6 ;
261
261
is_constructed = s & 0x20 ;
262
262
tag = s & 0x1f ;
@@ -275,9 +275,9 @@ function asn1_read(buf) {
275
275
throw "Went out of buffer: " + pointer + " " + buf . length ;
276
276
277
277
tag <<= 7 ;
278
- tag += ( buf . charCodeAt ( pointer ) & 0x7f ) ;
278
+ tag += ( buf [ pointer ] & 0x7f ) ;
279
279
280
- } while ( buf . charCodeAt ( pointer ) > 0x80 )
280
+ } while ( buf [ pointer ] > 0x80 )
281
281
}
282
282
283
283
if ( ++ pointer > buf . length )
@@ -369,9 +369,7 @@ function parse_pem_cert(pem) {
369
369
der = der . slice ( 1 , - 2 ) ;
370
370
}
371
371
372
- der = atob ( der . join ( '' ) ) ;
373
-
374
- return asn1_read ( der ) ;
372
+ return asn1_read ( Buffer . from ( der . join ( '' ) , 'base64' ) ) ;
375
373
}
376
374
377
375
export default { asn1_read, parse_pem_cert, is_oid_exist, get_oid_value, get_oid_value_all} ;
0 commit comments