… | |
… | |
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; |
265 | } |
|
|
266 | } |
|
|
267 | |
268 | |
268 | // get_w, but disallow padding |
269 | c = get_u8 (); |
269 | static UV |
270 | } |
270 | get_w_nopad (void) |
|
|
271 | { |
|
|
272 | U8 first = get_u8 (); |
|
|
273 | |
|
|
274 | if (first == 0x80) |
|
|
275 | error ("illegal BER padding"); |
|
|
276 | |
|
|
277 | --cur; |
|
|
278 | |
|
|
279 | return get_w (); |
|
|
280 | } |
271 | } |
281 | |
272 | |
282 | static UV |
273 | static UV |
283 | get_length (void) |
274 | get_length (void) |
284 | { |
275 | { |
… | |
… | |
291 | |
282 | |
292 | switch (cnt) |
283 | switch (cnt) |
293 | { |
284 | { |
294 | case 0: |
285 | case 0: |
295 | error ("indefinite ASN.1 lengths not supported"); |
286 | error ("indefinite ASN.1 lengths not supported"); |
296 | return 0; |
|
|
297 | |
287 | |
298 | //case 0x80: // indefinite length |
288 | case 0x7f: |
|
|
289 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
299 | |
290 | |
300 | //case 0xff: reserved |
|
|
301 | default: |
291 | default: |
302 | error ("ASN.1 length too long"); |
292 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
303 | return 0; |
|
|
304 | |
293 | |
305 | case 8: res = (res << 8) | get_u8 (); |
294 | case 8: res = (res << 8) | get_u8 (); |
306 | case 7: res = (res << 8) | get_u8 (); |
295 | case 7: res = (res << 8) | get_u8 (); |
307 | case 6: res = (res << 8) | get_u8 (); |
296 | case 6: res = (res << 8) | get_u8 (); |
308 | case 5: res = (res << 8) | get_u8 (); |
297 | case 5: res = (res << 8) | get_u8 (); |
… | |
… | |
320 | decode_int (void) |
309 | decode_int (void) |
321 | { |
310 | { |
322 | UV len = get_length (); |
311 | UV len = get_length (); |
323 | |
312 | |
324 | if (!len) |
313 | if (!len) |
325 | { |
|
|
326 | error ("invalid integer length equal to zero"); |
314 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
327 | return 0; |
|
|
328 | } |
|
|
329 | |
315 | |
330 | 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 | } |
331 | |
325 | |
332 | int negative = data [0] & 0x80; |
326 | int negative = data [0] & 0x80; |
333 | |
327 | |
334 | UV val = negative ? -1 : 0; // copy signbit to all bits |
328 | UV val = negative ? -1 : 0; // copy signbit to all bits |
335 | |
329 | |
… | |
… | |
392 | error ("OBJECT IDENTIFIER length equal to zero"); |
386 | error ("OBJECT IDENTIFIER length equal to zero"); |
393 | return &PL_sv_undef; |
387 | return &PL_sv_undef; |
394 | } |
388 | } |
395 | |
389 | |
396 | U8 *end = cur + len; |
390 | U8 *end = cur + len; |
397 | UV w = get_w_nopad (); |
391 | UV w = get_w (); |
398 | |
392 | |
399 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
393 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
400 | char *app = oid; |
394 | char *app = oid; |
401 | |
395 | |
402 | if (relative) |
396 | if (relative) |
… | |
… | |
418 | { |
412 | { |
419 | // we assume an oid component is never > 64 digits |
413 | // we assume an oid component is never > 64 digits |
420 | if (oid + sizeof (oid) - app < 64) |
414 | if (oid + sizeof (oid) - app < 64) |
421 | croak ("BER_TYPE_OID to long to decode"); |
415 | croak ("BER_TYPE_OID to long to decode"); |
422 | |
416 | |
423 | w = get_w_nopad (); |
417 | w = get_w (); |
424 | *app++ = '.'; |
418 | *app++ = '.'; |
425 | app = write_uv (app, w); |
419 | app = write_uv (app, w); |
426 | } |
420 | } |
427 | |
421 | |
428 | return newSVpvn (oid, app - oid); |
422 | return newSVpvn (oid, app - oid); |
… | |
… | |
476 | int tag = identifier & ASN_TAG_MASK; |
470 | int tag = identifier & ASN_TAG_MASK; |
477 | |
471 | |
478 | if (tag == ASN_TAG_BER) |
472 | if (tag == ASN_TAG_BER) |
479 | tag = get_w (); |
473 | tag = get_w (); |
480 | |
474 | |
481 | if (tag == ASN_TAG_BER) |
|
|
482 | tag = get_w (); |
|
|
483 | |
|
|
484 | if (constructed) |
475 | if (constructed) |
485 | { |
476 | { |
486 | UV len = get_length (); |
477 | UV len = get_length (); |
487 | UV seqend = (cur - buf) + len; |
478 | UV seqend = (cur - buf) + len; |
488 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
479 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
489 | |
480 | |
490 | while (cur < buf + seqend) |
481 | while (cur < buf + seqend) |
491 | av_push (av, decode_ber ()); |
482 | av_push (av, decode_ber ()); |
492 | |
483 | |
493 | if (cur > buf + seqend) |
484 | if (cur > buf + seqend) |
494 | 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); |
495 | |
486 | |
496 | res = newRV_inc ((SV *)av); |
487 | res = newRV_inc ((SV *)av); |
497 | } |
488 | } |
498 | else |
489 | else |
499 | switch (profile_lookup (cur_profile, klass, tag)) |
490 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
501 | case BER_TYPE_NULL: |
492 | case BER_TYPE_NULL: |
502 | { |
493 | { |
503 | UV len = get_length (); |
494 | UV len = get_length (); |
504 | |
495 | |
505 | if (len) |
496 | if (len) |
506 | 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); |
507 | |
498 | |
508 | res = &PL_sv_undef; |
499 | res = &PL_sv_undef; |
509 | } |
500 | } |
510 | break; |
501 | break; |
511 | |
502 | |
512 | case BER_TYPE_BOOL: |
503 | case BER_TYPE_BOOL: |
513 | { |
504 | { |
514 | UV len = get_length (); |
505 | UV len = get_length (); |
515 | |
506 | |
516 | if (len != 1) |
507 | if (len != 1) |
517 | 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); |
518 | |
509 | |
519 | res = newSVcacheint (!!get_u8 ()); |
510 | res = newSVcacheint (!!get_u8 ()); |
520 | } |
511 | } |
521 | break; |
512 | break; |
522 | |
513 | |
… | |
… | |
544 | case BER_TYPE_IPADDRESS: |
535 | case BER_TYPE_IPADDRESS: |
545 | { |
536 | { |
546 | UV len = get_length (); |
537 | UV len = get_length (); |
547 | |
538 | |
548 | if (len != 4) |
539 | if (len != 4) |
549 | 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); |
550 | |
541 | |
551 | U8 c1 = get_u8 (); |
542 | U8 c1 = get_u8 (); |
552 | U8 c2 = get_u8 (); |
543 | U8 c2 = get_u8 (); |
553 | U8 c3 = get_u8 (); |
544 | U8 c3 = get_u8 (); |
554 | U8 c4 = get_u8 (); |
545 | U8 c4 = get_u8 (); |
… | |
… | |
589 | strlen_sum (STRLEN l1, STRLEN l2) |
580 | strlen_sum (STRLEN l1, STRLEN l2) |
590 | { |
581 | { |
591 | size_t sum = l1 + l2; |
582 | size_t sum = l1 + l2; |
592 | |
583 | |
593 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
584 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
594 | croak ("JSON::XS: string size overflow"); |
585 | croak ("Convert::BER::XS: string size overflow"); |
595 | |
586 | |
596 | return sum; |
587 | return sum; |
597 | } |
588 | } |
598 | |
589 | |
599 | static void |
590 | static void |