… | |
… | |
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; |
291 | |
|
|
292 | case 0x7f: |
|
|
293 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
283 | |
294 | |
284 | default: |
295 | default: |
285 | error ("ASN.1 length too long"); |
296 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
286 | return 0; |
|
|
287 | |
297 | |
288 | case 8: res = (res << 8) | get_u8 (); |
298 | case 8: res = (res << 8) | get_u8 (); |
289 | case 7: res = (res << 8) | get_u8 (); |
299 | case 7: res = (res << 8) | get_u8 (); |
290 | case 6: res = (res << 8) | get_u8 (); |
300 | case 6: res = (res << 8) | get_u8 (); |
291 | case 5: res = (res << 8) | get_u8 (); |
301 | case 5: res = (res << 8) | get_u8 (); |
… | |
… | |
303 | decode_int (void) |
313 | decode_int (void) |
304 | { |
314 | { |
305 | UV len = get_length (); |
315 | UV len = get_length (); |
306 | |
316 | |
307 | if (!len) |
317 | if (!len) |
308 | { |
|
|
309 | error ("invalid integer length equal to zero"); |
318 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
310 | return 0; |
|
|
311 | } |
|
|
312 | |
319 | |
313 | U8 *data = get_n (len); |
320 | U8 *data = get_n (len); |
314 | |
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 | |
315 | int negative = data [0] & 0x80; |
330 | int negative = data [0] & 0x80; |
316 | |
331 | |
317 | 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 | error ("INTEGER overflow"); |
318 | |
336 | |
319 | do |
337 | do |
320 | val = (val << 8) | *data++; |
338 | val = (val << 8) | *data++; |
321 | while (--len); |
339 | while (--len); |
322 | |
340 | |
… | |
… | |
377 | } |
395 | } |
378 | |
396 | |
379 | U8 *end = cur + len; |
397 | U8 *end = cur + len; |
380 | UV w = get_w (); |
398 | UV w = get_w (); |
381 | |
399 | |
382 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
400 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
383 | char *app = oid; |
401 | char *app = oid; |
384 | |
402 | |
385 | if (relative) |
403 | if (relative) |
386 | app = write_uv (app, w); |
404 | app = write_uv (app, w); |
387 | else |
405 | else if (w < 2 * 40) |
388 | { |
406 | { |
389 | app = write_uv (app, (U8)w / 40); |
407 | app = write_uv (app, (U8)w / 40); |
390 | *app++ = '.'; |
408 | *app++ = '.'; |
391 | app = write_uv (app, (U8)w % 40); |
409 | app = write_uv (app, (U8)w % 40); |
|
|
410 | } |
|
|
411 | else |
|
|
412 | { |
|
|
413 | app = write_uv (app, 2); |
|
|
414 | *app++ = '.'; |
|
|
415 | app = write_uv (app, w - 2 * 40); |
392 | } |
416 | } |
393 | |
417 | |
394 | while (cur < end) |
418 | while (cur < end) |
395 | { |
419 | { |
396 | // we assume an oid component is never > 64 digits |
420 | // we assume an oid component is never > 64 digits |
… | |
… | |
453 | int tag = identifier & ASN_TAG_MASK; |
477 | int tag = identifier & ASN_TAG_MASK; |
454 | |
478 | |
455 | if (tag == ASN_TAG_BER) |
479 | if (tag == ASN_TAG_BER) |
456 | tag = get_w (); |
480 | tag = get_w (); |
457 | |
481 | |
458 | if (tag == ASN_TAG_BER) |
|
|
459 | tag = get_w (); |
|
|
460 | |
|
|
461 | if (constructed) |
482 | if (constructed) |
462 | { |
483 | { |
463 | UV len = get_length (); |
484 | UV len = get_length (); |
464 | UV seqend = (cur - buf) + len; |
485 | UV seqend = (cur - buf) + len; |
465 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
486 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
466 | |
487 | |
467 | while (cur < buf + seqend) |
488 | while (cur < buf + seqend) |
468 | av_push (av, decode_ber ()); |
489 | av_push (av, decode_ber ()); |
469 | |
490 | |
470 | if (cur > buf + seqend) |
491 | if (cur > buf + seqend) |
471 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
492 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
472 | |
493 | |
473 | res = newRV_inc ((SV *)av); |
494 | res = newRV_inc ((SV *)av); |
474 | } |
495 | } |
475 | else |
496 | else |
476 | switch (profile_lookup (cur_profile, klass, tag)) |
497 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
478 | case BER_TYPE_NULL: |
499 | case BER_TYPE_NULL: |
479 | { |
500 | { |
480 | UV len = get_length (); |
501 | UV len = get_length (); |
481 | |
502 | |
482 | if (len) |
503 | if (len) |
483 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
504 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
484 | |
505 | |
485 | res = &PL_sv_undef; |
506 | res = &PL_sv_undef; |
486 | } |
507 | } |
487 | break; |
508 | break; |
488 | |
509 | |
489 | case BER_TYPE_BOOL: |
510 | case BER_TYPE_BOOL: |
490 | { |
511 | { |
491 | UV len = get_length (); |
512 | UV len = get_length (); |
492 | |
513 | |
493 | if (len != 1) |
514 | if (len != 1) |
494 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
515 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
495 | |
516 | |
496 | res = newSVcacheint (!!get_u8 ()); |
517 | res = newSVcacheint (!!get_u8 ()); |
497 | } |
518 | } |
498 | break; |
519 | break; |
499 | |
520 | |
… | |
… | |
521 | case BER_TYPE_IPADDRESS: |
542 | case BER_TYPE_IPADDRESS: |
522 | { |
543 | { |
523 | UV len = get_length (); |
544 | UV len = get_length (); |
524 | |
545 | |
525 | if (len != 4) |
546 | if (len != 4) |
526 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
547 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
527 | |
548 | |
528 | U8 c1 = get_u8 (); |
549 | U8 c1 = get_u8 (); |
529 | U8 c2 = get_u8 (); |
550 | U8 c2 = get_u8 (); |
530 | U8 c3 = get_u8 (); |
551 | U8 c3 = get_u8 (); |
531 | U8 c4 = get_u8 (); |
552 | U8 c4 = get_u8 (); |
… | |
… | |
566 | strlen_sum (STRLEN l1, STRLEN l2) |
587 | strlen_sum (STRLEN l1, STRLEN l2) |
567 | { |
588 | { |
568 | size_t sum = l1 + l2; |
589 | size_t sum = l1 + l2; |
569 | |
590 | |
570 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
591 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
571 | croak ("JSON::XS: string size overflow"); |
592 | croak ("Convert::BER::XS: string size overflow"); |
572 | |
593 | |
573 | return sum; |
594 | return sum; |
574 | } |
595 | } |
575 | |
596 | |
576 | static void |
597 | static void |
… | |
… | |
925 | put_length (0); |
946 | put_length (0); |
926 | break; |
947 | break; |
927 | |
948 | |
928 | case BER_TYPE_BOOL: |
949 | case BER_TYPE_BOOL: |
929 | put_length (1); |
950 | put_length (1); |
930 | *cur++ = SvTRUE (data) ? 0xff : 0x00; |
951 | *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER |
931 | break; |
952 | break; |
932 | |
953 | |
933 | case BER_TYPE_OID: |
954 | case BER_TYPE_OID: |
934 | encode_oid (data, 0); |
955 | encode_oid (data, 0); |
935 | break; |
956 | break; |
… | |
… | |
1055 | const_iv (BER_TYPE_IPADDRESS) |
1076 | const_iv (BER_TYPE_IPADDRESS) |
1056 | const_iv (BER_TYPE_CROAK) |
1077 | const_iv (BER_TYPE_CROAK) |
1057 | |
1078 | |
1058 | const_iv (SNMP_IPADDRESS) |
1079 | const_iv (SNMP_IPADDRESS) |
1059 | const_iv (SNMP_COUNTER32) |
1080 | const_iv (SNMP_COUNTER32) |
|
|
1081 | const_iv (SNMP_GAUGE32) |
1060 | const_iv (SNMP_UNSIGNED32) |
1082 | const_iv (SNMP_UNSIGNED32) |
1061 | const_iv (SNMP_TIMETICKS) |
1083 | const_iv (SNMP_TIMETICKS) |
1062 | const_iv (SNMP_OPAQUE) |
1084 | const_iv (SNMP_OPAQUE) |
1063 | const_iv (SNMP_COUNTER64) |
1085 | const_iv (SNMP_COUNTER64) |
1064 | }; |
1086 | }; |
1065 | |
1087 | |
1066 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1088 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1067 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1089 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1068 | } |
1090 | } |
1069 | |
1091 | |
1070 | SV * |
1092 | void |
1071 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1093 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1094 | ALIAS: |
|
|
1095 | ber_decode_prefix = 1 |
1072 | CODE: |
1096 | PPCODE: |
1073 | { |
1097 | { |
1074 | cur_profile = SvPROFILE (profile); |
1098 | cur_profile = SvPROFILE (profile); |
1075 | STRLEN len; |
1099 | STRLEN len; |
1076 | buf = (U8 *)SvPVbyte (ber, len); |
1100 | buf = (U8 *)SvPVbyte (ber, len); |
1077 | cur = buf; |
1101 | cur = buf; |
1078 | end = buf + len; |
1102 | end = buf + len; |
1079 | |
1103 | |
1080 | RETVAL = decode_ber (); |
1104 | SV *tuple = decode_ber (); |
|
|
1105 | |
|
|
1106 | EXTEND (SP, 2); |
|
|
1107 | PUSHs (sv_2mortal (tuple)); |
|
|
1108 | |
|
|
1109 | if (ix) |
|
|
1110 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1111 | else if (cur != end) |
|
|
1112 | error ("trailing garbage after BER data"); |
1081 | } |
1113 | } |
1082 | OUTPUT: RETVAL |
|
|
1083 | |
1114 | |
1084 | void |
1115 | void |
1085 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1116 | ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *flags = &PL_sv_undef, SV *data = &PL_sv_undef) |
1086 | PPCODE: |
1117 | PPCODE: |
1087 | { |
1118 | { |