… | |
… | |
58 | ASN_CLASS_SHIFT = 6, |
58 | ASN_CLASS_SHIFT = 6, |
59 | |
59 | |
60 | // ASN_APPLICATION SNMP |
60 | // ASN_APPLICATION SNMP |
61 | SNMP_IPADDRESS = 0x00, |
61 | SNMP_IPADDRESS = 0x00, |
62 | SNMP_COUNTER32 = 0x01, |
62 | SNMP_COUNTER32 = 0x01, |
|
|
63 | SNMP_GAUGE32 = 0x02, |
63 | SNMP_UNSIGNED32 = 0x02, |
64 | SNMP_UNSIGNED32 = 0x02, |
64 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_TIMETICKS = 0x03, |
65 | SNMP_OPAQUE = 0x04, |
66 | SNMP_OPAQUE = 0x04, |
66 | SNMP_COUNTER64 = 0x06, |
67 | SNMP_COUNTER64 = 0x06, |
67 | }; |
68 | }; |
… | |
… | |
252 | // get ber-encoded integer (i.e. pack "w") |
253 | // get ber-encoded integer (i.e. pack "w") |
253 | static UV |
254 | static UV |
254 | get_w (void) |
255 | get_w (void) |
255 | { |
256 | { |
256 | UV res = 0; |
257 | UV res = 0; |
|
|
258 | U8 c = get_u8 (); |
|
|
259 | |
|
|
260 | if (expect_false (c == 0x80)) |
|
|
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
257 | |
262 | |
258 | for (;;) |
263 | for (;;) |
259 | { |
264 | { |
260 | U8 c = get_u8 (); |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
|
|
266 | error ("BER variable integer overflow"); |
|
|
267 | |
261 | res = (res << 7) | (c & 0x7f); |
268 | res = (res << 7) | (c & 0x7f); |
262 | |
269 | |
263 | if (!(c & 0x80)) |
270 | if (!(c & 0x80)) |
264 | return res; |
271 | return res; |
|
|
272 | |
|
|
273 | c = get_u8 (); |
265 | } |
274 | } |
266 | } |
275 | } |
267 | |
276 | |
268 | static UV |
277 | static UV |
269 | get_length (void) |
278 | get_length (void) |
… | |
… | |
277 | |
286 | |
278 | switch (cnt) |
287 | switch (cnt) |
279 | { |
288 | { |
280 | case 0: |
289 | case 0: |
281 | error ("indefinite ASN.1 lengths not supported"); |
290 | error ("indefinite ASN.1 lengths not supported"); |
282 | return 0; |
|
|
283 | |
291 | |
284 | //case 0x80: // indefinite length |
292 | case 0x7f: |
|
|
293 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
285 | |
294 | |
286 | //case 0xff: reserved |
|
|
287 | default: |
295 | default: |
288 | error ("ASN.1 length too long"); |
296 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
289 | return 0; |
|
|
290 | |
297 | |
291 | case 8: res = (res << 8) | get_u8 (); |
298 | case 8: res = (res << 8) | get_u8 (); |
292 | case 7: res = (res << 8) | get_u8 (); |
299 | case 7: res = (res << 8) | get_u8 (); |
293 | case 6: res = (res << 8) | get_u8 (); |
300 | case 6: res = (res << 8) | get_u8 (); |
294 | case 5: res = (res << 8) | get_u8 (); |
301 | case 5: res = (res << 8) | get_u8 (); |
… | |
… | |
306 | decode_int (void) |
313 | decode_int (void) |
307 | { |
314 | { |
308 | UV len = get_length (); |
315 | UV len = get_length (); |
309 | |
316 | |
310 | if (!len) |
317 | if (!len) |
311 | { |
|
|
312 | error ("invalid integer length equal to zero"); |
318 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
313 | return 0; |
|
|
314 | } |
|
|
315 | |
319 | |
316 | U8 *data = get_n (len); |
320 | U8 *data = get_n (len); |
317 | |
321 | |
|
|
322 | if (expect_false (len > 1)) |
|
|
323 | { |
|
|
324 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
325 | |
|
|
326 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
327 | error ("illegal padding in integer (X.690 8.3.2)"); |
|
|
328 | } |
|
|
329 | |
318 | int negative = data [0] & 0x80; |
330 | int negative = data [0] & 0x80; |
319 | |
331 | |
320 | UV val = negative ? -1 : 0; // copy signbit to all bits |
332 | UV val = negative ? -1 : 0; // copy signbit to all bits |
|
|
333 | |
|
|
334 | if (len > UVSIZE + (!negative && !*data)) |
|
|
335 | //printf ("len %d > %d + (!%d && !%d) = %d\n", len, UVSIZE, negative, *data, UVSIZE + (!negative && !*data));//D |
|
|
336 | error ("INTEGER overflow"); |
321 | |
337 | |
322 | do |
338 | do |
323 | val = (val << 8) | *data++; |
339 | val = (val << 8) | *data++; |
324 | while (--len); |
340 | while (--len); |
325 | |
341 | |
… | |
… | |
380 | } |
396 | } |
381 | |
397 | |
382 | U8 *end = cur + len; |
398 | U8 *end = cur + len; |
383 | UV w = get_w (); |
399 | UV w = get_w (); |
384 | |
400 | |
385 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
401 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
386 | char *app = oid; |
402 | char *app = oid; |
387 | |
403 | |
388 | if (relative) |
404 | if (relative) |
389 | app = write_uv (app, w); |
405 | app = write_uv (app, w); |
390 | else if (w < 2 * 40) |
406 | else if (w < 2 * 40) |
… | |
… | |
462 | int tag = identifier & ASN_TAG_MASK; |
478 | int tag = identifier & ASN_TAG_MASK; |
463 | |
479 | |
464 | if (tag == ASN_TAG_BER) |
480 | if (tag == ASN_TAG_BER) |
465 | tag = get_w (); |
481 | tag = get_w (); |
466 | |
482 | |
467 | if (tag == ASN_TAG_BER) |
|
|
468 | tag = get_w (); |
|
|
469 | |
|
|
470 | if (constructed) |
483 | if (constructed) |
471 | { |
484 | { |
472 | UV len = get_length (); |
485 | UV len = get_length (); |
473 | UV seqend = (cur - buf) + len; |
486 | UV seqend = (cur - buf) + len; |
474 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
487 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
475 | |
488 | |
476 | while (cur < buf + seqend) |
489 | while (cur < buf + seqend) |
477 | av_push (av, decode_ber ()); |
490 | av_push (av, decode_ber ()); |
478 | |
491 | |
479 | if (cur > buf + seqend) |
492 | if (cur > buf + seqend) |
480 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
493 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
481 | |
494 | |
482 | res = newRV_inc ((SV *)av); |
495 | res = newRV_inc ((SV *)av); |
483 | } |
496 | } |
484 | else |
497 | else |
485 | switch (profile_lookup (cur_profile, klass, tag)) |
498 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
487 | case BER_TYPE_NULL: |
500 | case BER_TYPE_NULL: |
488 | { |
501 | { |
489 | UV len = get_length (); |
502 | UV len = get_length (); |
490 | |
503 | |
491 | if (len) |
504 | if (len) |
492 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
505 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
493 | |
506 | |
494 | res = &PL_sv_undef; |
507 | res = &PL_sv_undef; |
495 | } |
508 | } |
496 | break; |
509 | break; |
497 | |
510 | |
498 | case BER_TYPE_BOOL: |
511 | case BER_TYPE_BOOL: |
499 | { |
512 | { |
500 | UV len = get_length (); |
513 | UV len = get_length (); |
501 | |
514 | |
502 | if (len != 1) |
515 | if (len != 1) |
503 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
516 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
504 | |
517 | |
505 | res = newSVcacheint (!!get_u8 ()); |
518 | res = newSVcacheint (!!get_u8 ()); |
506 | } |
519 | } |
507 | break; |
520 | break; |
508 | |
521 | |
… | |
… | |
530 | case BER_TYPE_IPADDRESS: |
543 | case BER_TYPE_IPADDRESS: |
531 | { |
544 | { |
532 | UV len = get_length (); |
545 | UV len = get_length (); |
533 | |
546 | |
534 | if (len != 4) |
547 | if (len != 4) |
535 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
548 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
536 | |
549 | |
537 | U8 c1 = get_u8 (); |
550 | U8 c1 = get_u8 (); |
538 | U8 c2 = get_u8 (); |
551 | U8 c2 = get_u8 (); |
539 | U8 c3 = get_u8 (); |
552 | U8 c3 = get_u8 (); |
540 | U8 c4 = get_u8 (); |
553 | U8 c4 = get_u8 (); |
… | |
… | |
575 | strlen_sum (STRLEN l1, STRLEN l2) |
588 | strlen_sum (STRLEN l1, STRLEN l2) |
576 | { |
589 | { |
577 | size_t sum = l1 + l2; |
590 | size_t sum = l1 + l2; |
578 | |
591 | |
579 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
592 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
580 | croak ("JSON::XS: string size overflow"); |
593 | croak ("Convert::BER::XS: string size overflow"); |
581 | |
594 | |
582 | return sum; |
595 | return sum; |
583 | } |
596 | } |
584 | |
597 | |
585 | static void |
598 | static void |
… | |
… | |
1064 | const_iv (BER_TYPE_IPADDRESS) |
1077 | const_iv (BER_TYPE_IPADDRESS) |
1065 | const_iv (BER_TYPE_CROAK) |
1078 | const_iv (BER_TYPE_CROAK) |
1066 | |
1079 | |
1067 | const_iv (SNMP_IPADDRESS) |
1080 | const_iv (SNMP_IPADDRESS) |
1068 | const_iv (SNMP_COUNTER32) |
1081 | const_iv (SNMP_COUNTER32) |
|
|
1082 | const_iv (SNMP_GAUGE32) |
1069 | const_iv (SNMP_UNSIGNED32) |
1083 | const_iv (SNMP_UNSIGNED32) |
1070 | const_iv (SNMP_TIMETICKS) |
1084 | const_iv (SNMP_TIMETICKS) |
1071 | const_iv (SNMP_OPAQUE) |
1085 | const_iv (SNMP_OPAQUE) |
1072 | const_iv (SNMP_COUNTER64) |
1086 | const_iv (SNMP_COUNTER64) |
1073 | }; |
1087 | }; |
1074 | |
1088 | |
1075 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1089 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1076 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1090 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1077 | } |
1091 | } |
1078 | |
1092 | |
1079 | SV * |
1093 | void |
1080 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1094 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1095 | ALIAS: |
|
|
1096 | ber_decode_prefix = 1 |
1081 | CODE: |
1097 | PPCODE: |
1082 | { |
1098 | { |
1083 | cur_profile = SvPROFILE (profile); |
1099 | cur_profile = SvPROFILE (profile); |
1084 | STRLEN len; |
1100 | STRLEN len; |
1085 | buf = (U8 *)SvPVbyte (ber, len); |
1101 | buf = (U8 *)SvPVbyte (ber, len); |
1086 | cur = buf; |
1102 | cur = buf; |
1087 | end = buf + len; |
1103 | end = buf + len; |
1088 | |
1104 | |
1089 | RETVAL = decode_ber (); |
1105 | SV *tuple = decode_ber (); |
|
|
1106 | |
|
|
1107 | EXTEND (SP, 2); |
|
|
1108 | PUSHs (sv_2mortal (tuple)); |
|
|
1109 | |
|
|
1110 | if (ix) |
|
|
1111 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1112 | else if (cur != end) |
|
|
1113 | error ("trailing garbage after BER data"); |
1090 | } |
1114 | } |
1091 | OUTPUT: RETVAL |
|
|
1092 | |
1115 | |
1093 | void |
1116 | void |
1094 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1117 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1095 | PPCODE: |
1118 | PPCODE: |
1096 | { |
1119 | { |