… | |
… | |
252 | // get ber-encoded integer (i.e. pack "w") |
252 | // get ber-encoded integer (i.e. pack "w") |
253 | static UV |
253 | static UV |
254 | get_w (void) |
254 | get_w (void) |
255 | { |
255 | { |
256 | UV res = 0; |
256 | UV res = 0; |
|
|
257 | U8 c = get_u8 (); |
|
|
258 | |
|
|
259 | if (expect_false (c == 0x80)) |
|
|
260 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
257 | |
261 | |
258 | for (;;) |
262 | for (;;) |
259 | { |
263 | { |
260 | U8 c = get_u8 (); |
|
|
261 | res = (res << 7) | (c & 0x7f); |
264 | res = (res << 7) | (c & 0x7f); |
262 | |
265 | |
263 | if (!(c & 0x80)) |
266 | if (!(c & 0x80)) |
264 | return res; |
267 | return res; |
|
|
268 | |
|
|
269 | c = get_u8 (); |
265 | } |
270 | } |
266 | } |
271 | } |
267 | |
272 | |
268 | static UV |
273 | static UV |
269 | get_length (void) |
274 | get_length (void) |
… | |
… | |
277 | |
282 | |
278 | switch (cnt) |
283 | switch (cnt) |
279 | { |
284 | { |
280 | case 0: |
285 | case 0: |
281 | error ("indefinite ASN.1 lengths not supported"); |
286 | error ("indefinite ASN.1 lengths not supported"); |
282 | return 0; |
|
|
283 | |
287 | |
284 | //case 0x80: // indefinite length |
288 | case 0x7f: |
|
|
289 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
285 | |
290 | |
286 | //case 0xff: reserved |
|
|
287 | default: |
291 | default: |
288 | error ("ASN.1 length too long"); |
292 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
289 | return 0; |
|
|
290 | |
293 | |
291 | case 8: res = (res << 8) | get_u8 (); |
294 | case 8: res = (res << 8) | get_u8 (); |
292 | case 7: res = (res << 8) | get_u8 (); |
295 | case 7: res = (res << 8) | get_u8 (); |
293 | case 6: res = (res << 8) | get_u8 (); |
296 | case 6: res = (res << 8) | get_u8 (); |
294 | case 5: res = (res << 8) | get_u8 (); |
297 | case 5: res = (res << 8) | get_u8 (); |
… | |
… | |
306 | decode_int (void) |
309 | decode_int (void) |
307 | { |
310 | { |
308 | UV len = get_length (); |
311 | UV len = get_length (); |
309 | |
312 | |
310 | if (!len) |
313 | if (!len) |
311 | { |
|
|
312 | error ("invalid integer length equal to zero"); |
314 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
313 | return 0; |
|
|
314 | } |
|
|
315 | |
315 | |
316 | U8 *data = get_n (len); |
316 | U8 *data = get_n (len); |
|
|
317 | |
|
|
318 | if (expect_false (len > 1)) |
|
|
319 | { |
|
|
320 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
321 | |
|
|
322 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
323 | error ("illegal padding in integer (X.690 8.3.2)"); |
|
|
324 | } |
317 | |
325 | |
318 | int negative = data [0] & 0x80; |
326 | int negative = data [0] & 0x80; |
319 | |
327 | |
320 | UV val = negative ? -1 : 0; // copy signbit to all bits |
328 | UV val = negative ? -1 : 0; // copy signbit to all bits |
321 | |
329 | |
… | |
… | |
462 | int tag = identifier & ASN_TAG_MASK; |
470 | int tag = identifier & ASN_TAG_MASK; |
463 | |
471 | |
464 | if (tag == ASN_TAG_BER) |
472 | if (tag == ASN_TAG_BER) |
465 | tag = get_w (); |
473 | tag = get_w (); |
466 | |
474 | |
467 | if (tag == ASN_TAG_BER) |
|
|
468 | tag = get_w (); |
|
|
469 | |
|
|
470 | if (constructed) |
475 | if (constructed) |
471 | { |
476 | { |
472 | UV len = get_length (); |
477 | UV len = get_length (); |
473 | UV seqend = (cur - buf) + len; |
478 | UV seqend = (cur - buf) + len; |
474 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
479 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
475 | |
480 | |
476 | while (cur < buf + seqend) |
481 | while (cur < buf + seqend) |
477 | av_push (av, decode_ber ()); |
482 | av_push (av, decode_ber ()); |
478 | |
483 | |
479 | if (cur > buf + seqend) |
484 | if (cur > buf + seqend) |
480 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
485 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
481 | |
486 | |
482 | res = newRV_inc ((SV *)av); |
487 | res = newRV_inc ((SV *)av); |
483 | } |
488 | } |
484 | else |
489 | else |
485 | switch (profile_lookup (cur_profile, klass, tag)) |
490 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
487 | case BER_TYPE_NULL: |
492 | case BER_TYPE_NULL: |
488 | { |
493 | { |
489 | UV len = get_length (); |
494 | UV len = get_length (); |
490 | |
495 | |
491 | if (len) |
496 | if (len) |
492 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
497 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
493 | |
498 | |
494 | res = &PL_sv_undef; |
499 | res = &PL_sv_undef; |
495 | } |
500 | } |
496 | break; |
501 | break; |
497 | |
502 | |
498 | case BER_TYPE_BOOL: |
503 | case BER_TYPE_BOOL: |
499 | { |
504 | { |
500 | UV len = get_length (); |
505 | UV len = get_length (); |
501 | |
506 | |
502 | if (len != 1) |
507 | if (len != 1) |
503 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
508 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
504 | |
509 | |
505 | res = newSVcacheint (!!get_u8 ()); |
510 | res = newSVcacheint (!!get_u8 ()); |
506 | } |
511 | } |
507 | break; |
512 | break; |
508 | |
513 | |
… | |
… | |
530 | case BER_TYPE_IPADDRESS: |
535 | case BER_TYPE_IPADDRESS: |
531 | { |
536 | { |
532 | UV len = get_length (); |
537 | UV len = get_length (); |
533 | |
538 | |
534 | if (len != 4) |
539 | if (len != 4) |
535 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
540 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
536 | |
541 | |
537 | U8 c1 = get_u8 (); |
542 | U8 c1 = get_u8 (); |
538 | U8 c2 = get_u8 (); |
543 | U8 c2 = get_u8 (); |
539 | U8 c3 = get_u8 (); |
544 | U8 c3 = get_u8 (); |
540 | U8 c4 = get_u8 (); |
545 | U8 c4 = get_u8 (); |
… | |
… | |
575 | strlen_sum (STRLEN l1, STRLEN l2) |
580 | strlen_sum (STRLEN l1, STRLEN l2) |
576 | { |
581 | { |
577 | size_t sum = l1 + l2; |
582 | size_t sum = l1 + l2; |
578 | |
583 | |
579 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
584 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
580 | croak ("JSON::XS: string size overflow"); |
585 | croak ("Convert::BER::XS: string size overflow"); |
581 | |
586 | |
582 | return sum; |
587 | return sum; |
583 | } |
588 | } |
584 | |
589 | |
585 | static void |
590 | static void |