@@ -6,16 +6,19 @@ function asn1_parse_oid(buf) {
6
6
if ( cur_octet < 40 ) {
7
7
oid . push ( 0 ) ;
8
8
oid . push ( cur_octet ) ;
9
+
9
10
} else if ( cur_octet < 80 ) {
10
11
oid . push ( 1 ) ;
11
12
oid . push ( cur_octet - 40 ) ;
13
+
12
14
} else {
13
15
oid . push ( 2 ) ;
14
16
oid . push ( cur_octet - 80 ) ;
15
17
}
16
18
17
19
for ( var n = 1 ; n < buf . length ; n ++ ) {
18
20
cur_octet = buf . charCodeAt ( n ) ;
21
+
19
22
if ( cur_octet < 0x80 ) {
20
23
sid += cur_octet ;
21
24
@@ -24,6 +27,7 @@ function asn1_parse_oid(buf) {
24
27
25
28
oid . push ( sid ) ;
26
29
sid = 0 ;
30
+
27
31
} else {
28
32
sid += cur_octet & 0x7f ; sid <<= 7 ;
29
33
@@ -52,6 +56,7 @@ function asn1_parse_integer(buf) {
52
56
is_negative = true ;
53
57
value = buf . charCodeAt ( 0 ) & 0x7f ;
54
58
var compl_int = 1 << ( 8 * buf . length - 1 )
59
+
55
60
} else {
56
61
value = buf . charCodeAt ( 0 ) ;
57
62
}
@@ -111,6 +116,7 @@ function asn1_parse_bit_string(buf) {
111
116
upper_bits = ( buf . charCodeAt ( n ) << shift ) & 0xff ;
112
117
value += symbol ;
113
118
}
119
+
114
120
return value ;
115
121
}
116
122
@@ -141,95 +147,98 @@ function asn1_read_length(buf, pointer) {
141
147
// length is less than 128
142
148
pointer ++ ;
143
149
return [ s , pointer ] ;
150
+
144
151
} else {
145
152
var l = s & 0x7f ;
146
153
if ( l > 7 )
147
154
throw "Too big length, exceeds MAX_SAFE_INTEGER: " + l ;
155
+
148
156
if ( ( pointer + l ) >= buf . length )
149
157
throw "Went out of buffer: " + ( pointer + l ) + " " + buf . length ;
158
+
150
159
var length = 0 ;
151
160
for ( var n = 0 ; n < l ; n ++ ) {
152
161
length += Math . pow ( 256 , l - n - 1 ) * buf . charCodeAt ( ++ pointer ) ;
153
162
if ( n == 6 && buf . charCodeAt ( pointer ) > 0x1f )
154
163
throw "Too big length, exceeds MAX_SAFE_INTEGER" ;
155
164
}
156
165
157
- return [ length , pointer + 1 ] ;
166
+ return [ length , pointer + 1 ] ;
158
167
}
159
168
}
160
169
161
170
function asn1_parse_primitive ( cls , tag , buf ) {
162
171
if ( cls == 0 ) {
163
172
switch ( tag ) {
164
- // INTEGER
165
- case 0x02 : return asn1_parse_integer ( buf ) ;
166
- // BIT STRING
167
- case 0x03 :
168
- try {
169
- return asn1_read ( buf ) ;
170
- } catch ( e ) {
171
- return asn1_parse_bit_string ( buf ) ;
172
- }
173
- // OCTET STRING
174
- case 0x04 :
175
- try {
176
- return asn1_read ( buf ) ;
177
- } catch ( e ) {
178
- return asn1_parse_octet_string ( buf ) ;
179
- }
180
- // OBJECT IDENTIFIER
181
- case 0x06 : return asn1_parse_oid ( buf ) ;
182
- // UTF8String
183
- case 0x0c : return asn1_parse_utf8_string ( buf ) ;
184
- // TIME
185
- case 0x0e :
186
- // NumericString
187
- case 0x12 :
188
- // PrintableString
189
- case 0x13 :
190
- // T61String
191
- case 0x14 :
192
- // VideotexString
193
- case 0x15 :
194
- return asn1_parse_ascii_string ( buf ) ;
195
- // IA5String
196
- case 0x16 : return asn1_parse_ia5_string ( buf ) ;
197
- // UTCTime
198
- case 0x17 :
199
- // GeneralizedTime
200
- case 0x18 :
201
- // GraphicString
202
- case 0x19 :
203
- // VisibleString
204
- case 0x1a :
205
- // GeneralString
206
- case 0x1b :
207
- return asn1_parse_ascii_string ( buf ) ;
208
- // UniversalString
209
- case 0x1c : return asn1_parse_universal_string ( buf ) ;
210
- // CHARACTER STRING
211
- case 0x1d : return asn1_parse_ascii_string ( buf ) ;
212
- // BMPString
213
- case 0x1e : return asn1_parse_bmp_string ( buf ) ;
214
- // DATE
215
- case 0x1f :
216
- // TIME-OF-DAY
217
- case 0x20 :
218
- // DATE-TIME
219
- case 0x21 :
220
- // DURATION
221
- case 0x22 :
222
- return asn1_parse_ascii_string ( buf ) ;
223
- default : return asn1_parse_any ( buf ) ;
173
+ // INTEGER
174
+ case 0x02 : return asn1_parse_integer ( buf ) ;
175
+ // BIT STRING
176
+ case 0x03 :
177
+ try {
178
+ return asn1_read ( buf ) ;
179
+ } catch ( e ) {
180
+ return asn1_parse_bit_string ( buf ) ;
181
+ }
182
+ // OCTET STRING
183
+ case 0x04 :
184
+ try {
185
+ return asn1_read ( buf ) ;
186
+ } catch ( e ) {
187
+ return asn1_parse_octet_string ( buf ) ;
188
+ }
189
+ // OBJECT IDENTIFIER
190
+ case 0x06 : return asn1_parse_oid ( buf ) ;
191
+ // UTF8String
192
+ case 0x0c : return asn1_parse_utf8_string ( buf ) ;
193
+ // TIME
194
+ case 0x0e :
195
+ // NumericString
196
+ case 0x12 :
197
+ // PrintableString
198
+ case 0x13 :
199
+ // T61String
200
+ case 0x14 :
201
+ // VideotexString
202
+ case 0x15 :
203
+ return asn1_parse_ascii_string ( buf ) ;
204
+ // IA5String
205
+ case 0x16 : return asn1_parse_ia5_string ( buf ) ;
206
+ // UTCTime
207
+ case 0x17 :
208
+ // GeneralizedTime
209
+ case 0x18 :
210
+ // GraphicString
211
+ case 0x19 :
212
+ // VisibleString
213
+ case 0x1a :
214
+ // GeneralString
215
+ case 0x1b :
216
+ return asn1_parse_ascii_string ( buf ) ;
217
+ // UniversalString
218
+ case 0x1c : return asn1_parse_universal_string ( buf ) ;
219
+ // CHARACTER STRING
220
+ case 0x1d : return asn1_parse_ascii_string ( buf ) ;
221
+ // BMPString
222
+ case 0x1e : return asn1_parse_bmp_string ( buf ) ;
223
+ // DATE
224
+ case 0x1f :
225
+ // TIME-OF-DAY
226
+ case 0x20 :
227
+ // DATE-TIME
228
+ case 0x21 :
229
+ // DURATION
230
+ case 0x22 :
231
+ return asn1_parse_ascii_string ( buf ) ;
232
+ default : return asn1_parse_any ( buf ) ;
224
233
}
225
234
226
235
} else if ( cls == 2 ) {
227
236
switch ( tag ) {
228
- case 0x00 : return asn1_parse_any ( buf ) ;
229
- case 0x01 : return asn1_parse_ascii_string ( buf ) ;
230
- case 0x02 : return asn1_parse_ascii_string ( buf ) ;
231
- case 0x06 : return asn1_parse_ascii_string ( buf ) ;
232
- default : return asn1_parse_any ( buf ) ;
237
+ case 0x00 : return asn1_parse_any ( buf ) ;
238
+ case 0x01 : return asn1_parse_ascii_string ( buf ) ;
239
+ case 0x02 : return asn1_parse_ascii_string ( buf ) ;
240
+ case 0x06 : return asn1_parse_ascii_string ( buf ) ;
241
+ default : return asn1_parse_any ( buf ) ;
233
242
}
234
243
}
235
244
@@ -255,32 +264,43 @@ function asn1_read(buf) {
255
264
if ( tag == 0x1f ) {
256
265
tag = 0 ;
257
266
var i = 0 ;
267
+
258
268
do {
259
269
if ( i > 3 )
260
270
throw "Too big tag value" + tag ;
271
+
261
272
i ++ ;
273
+
262
274
if ( ++ pointer >= buf . length )
263
275
throw "Went out of buffer: " + pointer + " " + buf . length ;
276
+
264
277
tag <<= 7 ;
265
278
tag += ( buf . charCodeAt ( pointer ) & 0x7f ) ;
279
+
266
280
} while ( buf . charCodeAt ( pointer ) > 0x80 )
267
281
}
268
282
269
283
if ( ++ pointer > buf . length )
270
284
throw "Went out of buffer: " + pointer + " " + buf . length ;
285
+
271
286
var lp = asn1_read_length ( buf , pointer ) ;
272
287
length = lp [ 0 ] ;
273
288
pointer = lp [ 1 ] ;
289
+
274
290
if ( ( pointer + length ) > buf . length )
275
291
throw "length exceeds buf side: " + length + " " + pointer + " "
276
292
+ buf . length ;
293
+
277
294
if ( is_constructed ) {
278
295
a . push ( asn1_read ( buf . slice ( pointer , pointer + length ) ) ) ;
296
+
279
297
} else {
280
298
a . push ( asn1_parse_primitive ( tag_class , tag , buf . slice ( pointer , pointer + length ) ) ) ;
281
299
}
300
+
282
301
pointer += length ;
283
302
}
303
+
284
304
return a ;
285
305
}
286
306
@@ -289,34 +309,36 @@ function is_oid_exist(cert, oid) {
289
309
if ( Array . isArray ( cert [ n ] ) ) {
290
310
if ( is_oid_exist ( cert [ n ] , oid ) )
291
311
return true ;
312
+
292
313
} else {
293
314
if ( cert [ n ] == oid )
294
315
return true ;
295
316
}
296
317
}
318
+
297
319
return false ;
298
320
}
299
321
300
322
// returns all the matching field with the specified 'oid' as a list
301
323
function get_oid_value_all ( cert , oid ) {
302
-
303
324
var values = [ ] ;
304
325
305
326
for ( var n = 0 ; n < cert . length ; n ++ ) {
306
327
if ( Array . isArray ( cert [ n ] ) ) {
307
328
var r = get_oid_value_all ( cert [ n ] , oid ) ;
308
- if ( r . length > 0 ) {
309
- values = values . concat ( r ) ;
310
- }
329
+ if ( r . length > 0 ) {
330
+ values = values . concat ( r ) ;
331
+ }
311
332
} else {
312
333
if ( cert [ n ] == oid ) {
313
334
if ( n < cert . length ) {
314
335
// push next element in array
315
- values . push ( cert [ n + 1 ] ) ;
336
+ values . push ( cert [ n + 1 ] ) ;
316
337
}
317
338
}
318
339
}
319
340
}
341
+
320
342
return values ;
321
343
}
322
344
@@ -326,6 +348,7 @@ function get_oid_value(cert, oid) {
326
348
var r = get_oid_value ( cert [ n ] , oid ) ;
327
349
if ( r !== false )
328
350
return r ;
351
+
329
352
} else {
330
353
if ( cert [ n ] == oid ) {
331
354
if ( n < cert . length ) {
@@ -335,6 +358,7 @@ function get_oid_value(cert, oid) {
335
358
}
336
359
}
337
360
}
361
+
338
362
return false ;
339
363
}
340
364
0 commit comments