… | |
… | |
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 | }; |
… | |
… | |
140 | { |
141 | { |
141 | if (!SvOK (profile)) |
142 | if (!SvOK (profile)) |
142 | return default_profile; |
143 | return default_profile; |
143 | |
144 | |
144 | if (!SvROK (profile)) |
145 | if (!SvROK (profile)) |
145 | croak ("invalid profile"); |
146 | croak ("Convert::BER::XS::Profile expected"); |
146 | |
147 | |
147 | profile = SvRV (profile); |
148 | profile = SvRV (profile); |
148 | |
149 | |
149 | if (SvSTASH (profile) != profile_stash) |
150 | if (SvSTASH (profile) != profile_stash) |
150 | croak ("invalid profile object"); |
151 | croak ("Convert::BER::XS::Profile expected"); |
151 | |
152 | |
152 | return (void *)profile; |
153 | return (void *)profile; |
153 | } |
154 | } |
154 | |
155 | |
155 | static int |
156 | static int |
… | |
… | |
227 | } |
228 | } |
228 | |
229 | |
229 | // get_* functions fetch something from the buffer |
230 | // get_* functions fetch something from the buffer |
230 | // decode_* functions use get_* fun ctions to decode ber values |
231 | // decode_* functions use get_* fun ctions to decode ber values |
231 | |
232 | |
|
|
233 | // get single octet |
|
|
234 | static U8 |
|
|
235 | get_u8 (void) |
|
|
236 | { |
|
|
237 | if (cur == end) |
|
|
238 | error ("unexpected end of message buffer"); |
|
|
239 | |
|
|
240 | return *cur++; |
|
|
241 | } |
|
|
242 | |
232 | // get n octets |
243 | // get n octets |
233 | static U8 * |
244 | static U8 * |
234 | get_n (UV count) |
245 | get_n (UV count) |
235 | { |
246 | { |
236 | want (count); |
247 | want (count); |
237 | U8 *res = cur; |
248 | U8 *res = cur; |
238 | cur += count; |
249 | cur += count; |
239 | return res; |
250 | return res; |
240 | } |
251 | } |
241 | |
252 | |
242 | // get single octet |
|
|
243 | static U8 |
|
|
244 | get_u8 (void) |
|
|
245 | { |
|
|
246 | if (cur == end) |
|
|
247 | error ("unexpected end of message buffer"); |
|
|
248 | |
|
|
249 | return *cur++; |
|
|
250 | } |
|
|
251 | |
|
|
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 length 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) |
… | |
… | |
276 | res = 0; |
285 | res = 0; |
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 BER value lengths not supported"); |
282 | return 0; |
291 | |
|
|
292 | case 0x7f: |
|
|
293 | error ("BER 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 ("BER value length too long (must fit into UV)"); |
286 | return 0; |
|
|
287 | |
297 | |
|
|
298 | #if UVSIZE > 4 |
288 | case 8: res = (res << 8) | get_u8 (); |
299 | case 8: res = (res << 8) | get_u8 (); |
289 | case 7: res = (res << 8) | get_u8 (); |
300 | case 7: res = (res << 8) | get_u8 (); |
290 | case 6: res = (res << 8) | get_u8 (); |
301 | case 6: res = (res << 8) | get_u8 (); |
291 | case 5: res = (res << 8) | get_u8 (); |
302 | case 5: res = (res << 8) | get_u8 (); |
|
|
303 | #endif |
292 | case 4: res = (res << 8) | get_u8 (); |
304 | case 4: res = (res << 8) | get_u8 (); |
293 | case 3: res = (res << 8) | get_u8 (); |
305 | case 3: res = (res << 8) | get_u8 (); |
294 | case 2: res = (res << 8) | get_u8 (); |
306 | case 2: res = (res << 8) | get_u8 (); |
295 | case 1: res = (res << 8) | get_u8 (); |
307 | case 1: res = (res << 8) | get_u8 (); |
296 | } |
308 | } |
… | |
… | |
303 | decode_int (void) |
315 | decode_int (void) |
304 | { |
316 | { |
305 | UV len = get_length (); |
317 | UV len = get_length (); |
306 | |
318 | |
307 | if (!len) |
319 | if (!len) |
308 | { |
320 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
309 | error ("invalid integer length equal to zero"); |
|
|
310 | return 0; |
|
|
311 | } |
|
|
312 | |
321 | |
313 | U8 *data = get_n (len); |
322 | U8 *data = get_n (len); |
314 | |
323 | |
|
|
324 | if (expect_false (len > 1)) |
|
|
325 | { |
|
|
326 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
|
|
327 | |
|
|
328 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
|
|
329 | error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)"); |
|
|
330 | } |
|
|
331 | |
315 | int negative = data [0] & 0x80; |
332 | int negative = data [0] & 0x80; |
316 | |
333 | |
317 | UV val = negative ? -1 : 0; // copy signbit to all bits |
334 | UV val = negative ? -1 : 0; // copy signbit to all bits |
|
|
335 | |
|
|
336 | if (len > UVSIZE + (!negative && !*data)) |
|
|
337 | error ("BER_TYPE_INT overflow"); |
318 | |
338 | |
319 | do |
339 | do |
320 | val = (val << 8) | *data++; |
340 | val = (val << 8) | *data++; |
321 | while (--len); |
341 | while (--len); |
322 | |
342 | |
… | |
… | |
370 | { |
390 | { |
371 | UV len = get_length (); |
391 | UV len = get_length (); |
372 | |
392 | |
373 | if (len <= 0) |
393 | if (len <= 0) |
374 | { |
394 | { |
375 | error ("OBJECT IDENTIFIER length equal to zero"); |
395 | error ("BER_TYPE_OID length must not be zero"); |
376 | return &PL_sv_undef; |
396 | return &PL_sv_undef; |
377 | } |
397 | } |
378 | |
398 | |
379 | U8 *end = cur + len; |
399 | U8 *end = cur + len; |
380 | UV w = get_w (); |
400 | UV w = get_w (); |
381 | |
401 | |
382 | static char oid[MAX_OID_STRLEN]; // static, becaueds too large for stack |
402 | static char oid[MAX_OID_STRLEN]; // static, because too large for stack |
383 | char *app = oid; |
403 | char *app = oid; |
384 | |
404 | |
385 | if (relative) |
405 | if (relative) |
386 | app = write_uv (app, w); |
406 | app = write_uv (app, w); |
387 | else |
407 | else if (w < 2 * 40) |
388 | { |
408 | { |
389 | app = write_uv (app, (U8)w / 40); |
409 | app = write_uv (app, (U8)w / 40); |
390 | *app++ = '.'; |
410 | *app++ = '.'; |
391 | app = write_uv (app, (U8)w % 40); |
411 | app = write_uv (app, (U8)w % 40); |
|
|
412 | } |
|
|
413 | else |
|
|
414 | { |
|
|
415 | app = write_uv (app, 2); |
|
|
416 | *app++ = '.'; |
|
|
417 | app = write_uv (app, w - 2 * 40); |
392 | } |
418 | } |
393 | |
419 | |
394 | while (cur < end) |
420 | while (cur < end) |
395 | { |
421 | { |
396 | // we assume an oid component is never > 64 digits |
422 | // we assume an oid component is never > 64 digits |
… | |
… | |
453 | int tag = identifier & ASN_TAG_MASK; |
479 | int tag = identifier & ASN_TAG_MASK; |
454 | |
480 | |
455 | if (tag == ASN_TAG_BER) |
481 | if (tag == ASN_TAG_BER) |
456 | tag = get_w (); |
482 | tag = get_w (); |
457 | |
483 | |
458 | if (tag == ASN_TAG_BER) |
|
|
459 | tag = get_w (); |
|
|
460 | |
|
|
461 | if (constructed) |
484 | if (constructed) |
462 | { |
485 | { |
463 | UV len = get_length (); |
486 | UV len = get_length (); |
464 | UV seqend = (cur - buf) + len; |
487 | UV seqend = (cur - buf) + len; |
465 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
488 | AV *av = (AV *)sv_2mortal ((SV *)newAV ()); |
466 | |
489 | |
467 | while (cur < buf + seqend) |
490 | while (cur < buf + seqend) |
468 | av_push (av, decode_ber ()); |
491 | av_push (av, decode_ber ()); |
469 | |
492 | |
470 | if (cur > buf + seqend) |
493 | if (cur > buf + seqend) |
471 | croak ("constructed type %02x overflow (%x %x)\n", identifier, cur - buf, seqend); |
494 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
472 | |
495 | |
473 | res = newRV_inc ((SV *)av); |
496 | res = newRV_inc ((SV *)av); |
474 | } |
497 | } |
475 | else |
498 | else |
476 | switch (profile_lookup (cur_profile, klass, tag)) |
499 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
478 | case BER_TYPE_NULL: |
501 | case BER_TYPE_NULL: |
479 | { |
502 | { |
480 | UV len = get_length (); |
503 | UV len = get_length (); |
481 | |
504 | |
482 | if (len) |
505 | if (len) |
483 | croak ("BER_TYPE_NULL value with non-zero length %d encountered", len); |
506 | croak ("BER_TYPE_NULL value with non-zero length %d encountered (X.690 8.8.2)", len); |
484 | |
507 | |
485 | res = &PL_sv_undef; |
508 | res = &PL_sv_undef; |
486 | } |
509 | } |
487 | break; |
510 | break; |
488 | |
511 | |
489 | case BER_TYPE_BOOL: |
512 | case BER_TYPE_BOOL: |
490 | { |
513 | { |
491 | UV len = get_length (); |
514 | UV len = get_length (); |
492 | |
515 | |
493 | if (len != 1) |
516 | if (len != 1) |
494 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered", len); |
517 | croak ("BER_TYPE_BOOLEAN value with invalid length %d encountered (X.690 8.2.1)", len); |
495 | |
518 | |
496 | res = newSVcacheint (!!get_u8 ()); |
519 | res = newSVcacheint (!!get_u8 ()); |
497 | } |
520 | } |
498 | break; |
521 | break; |
499 | |
522 | |
… | |
… | |
521 | case BER_TYPE_IPADDRESS: |
544 | case BER_TYPE_IPADDRESS: |
522 | { |
545 | { |
523 | UV len = get_length (); |
546 | UV len = get_length (); |
524 | |
547 | |
525 | if (len != 4) |
548 | if (len != 4) |
526 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered", len); |
549 | croak ("BER_TYPE_IPADDRESS type with invalid length %d encountered (RFC 2578 7.1.5)", len); |
527 | |
550 | |
528 | U8 c1 = get_u8 (); |
551 | U8 c1 = get_u8 (); |
529 | U8 c2 = get_u8 (); |
552 | U8 c2 = get_u8 (); |
530 | U8 c3 = get_u8 (); |
553 | U8 c3 = get_u8 (); |
531 | U8 c4 = get_u8 (); |
554 | U8 c4 = get_u8 (); |
… | |
… | |
541 | case BER_TYPE_UCS4: |
564 | case BER_TYPE_UCS4: |
542 | res = decode_ucs (4); |
565 | res = decode_ucs (4); |
543 | break; |
566 | break; |
544 | |
567 | |
545 | case BER_TYPE_REAL: |
568 | case BER_TYPE_REAL: |
|
|
569 | error ("BER_TYPE_REAL not implemented"); |
|
|
570 | |
546 | case BER_TYPE_CROAK: |
571 | case BER_TYPE_CROAK: |
|
|
572 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
573 | |
547 | default: |
574 | default: |
548 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
575 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
549 | } |
576 | } |
550 | |
577 | |
551 | AV *av = newAV (); |
578 | AV *av = newAV (); |
… | |
… | |
566 | strlen_sum (STRLEN l1, STRLEN l2) |
593 | strlen_sum (STRLEN l1, STRLEN l2) |
567 | { |
594 | { |
568 | size_t sum = l1 + l2; |
595 | size_t sum = l1 + l2; |
569 | |
596 | |
570 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
597 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
571 | croak ("JSON::XS: string size overflow"); |
598 | croak ("Convert::BER::XS: string size overflow"); |
572 | |
599 | |
573 | return sum; |
600 | return sum; |
574 | } |
601 | } |
575 | |
602 | |
576 | static void |
603 | static void |
… | |
… | |
902 | // and adjust later |
929 | // and adjust later |
903 | need (1); |
930 | need (1); |
904 | STRLEN mark = len_fixup_mark (); |
931 | STRLEN mark = len_fixup_mark (); |
905 | |
932 | |
906 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
933 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
907 | croak ("BER constructed data must be array-reference"); |
934 | croak ("BER CONSTRUCTED data must be array-reference"); |
908 | |
935 | |
909 | AV *av = (AV *)SvRV (data); |
936 | AV *av = (AV *)SvRV (data); |
910 | int fill = AvFILL (av); |
937 | int fill = AvFILL (av); |
911 | |
938 | |
912 | if (expect_false (SvRMAGICAL (av))) |
939 | if (expect_false (SvRMAGICAL (av))) |
913 | croak ("BER constructed data must not be tied"); |
940 | croak ("BER CONSTRUCTED data must not be tied"); |
914 | |
941 | |
915 | int i; |
942 | int i; |
916 | for (i = 0; i <= fill; ++i) |
943 | for (i = 0; i <= fill; ++i) |
917 | encode_ber (AvARRAY (av)[i]); |
944 | encode_ber (AvARRAY (av)[i]); |
918 | |
945 | |
… | |
… | |
973 | case BER_TYPE_UCS4: |
1000 | case BER_TYPE_UCS4: |
974 | encode_ucs (data, 4); |
1001 | encode_ucs (data, 4); |
975 | break; |
1002 | break; |
976 | |
1003 | |
977 | case BER_TYPE_REAL: |
1004 | case BER_TYPE_REAL: |
|
|
1005 | croak ("BER_TYPE_REAL not implemented"); |
|
|
1006 | |
978 | case BER_TYPE_CROAK: |
1007 | case BER_TYPE_CROAK: |
|
|
1008 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1009 | |
979 | default: |
1010 | default: |
980 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1011 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
981 | } |
1012 | } |
982 | |
1013 | |
983 | } |
1014 | } |
… | |
… | |
1055 | const_iv (BER_TYPE_IPADDRESS) |
1086 | const_iv (BER_TYPE_IPADDRESS) |
1056 | const_iv (BER_TYPE_CROAK) |
1087 | const_iv (BER_TYPE_CROAK) |
1057 | |
1088 | |
1058 | const_iv (SNMP_IPADDRESS) |
1089 | const_iv (SNMP_IPADDRESS) |
1059 | const_iv (SNMP_COUNTER32) |
1090 | const_iv (SNMP_COUNTER32) |
|
|
1091 | const_iv (SNMP_GAUGE32) |
1060 | const_iv (SNMP_UNSIGNED32) |
1092 | const_iv (SNMP_UNSIGNED32) |
1061 | const_iv (SNMP_TIMETICKS) |
1093 | const_iv (SNMP_TIMETICKS) |
1062 | const_iv (SNMP_OPAQUE) |
1094 | const_iv (SNMP_OPAQUE) |
1063 | const_iv (SNMP_COUNTER64) |
1095 | const_iv (SNMP_COUNTER64) |
1064 | }; |
1096 | }; |
1065 | |
1097 | |
1066 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ > const_iv; civ--) |
1098 | 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)); |
1099 | newCONSTSUB (stash, (char *)civ[-1].name, newSViv (civ[-1].iv)); |
1068 | } |
1100 | } |
1069 | |
1101 | |
1070 | SV * |
1102 | void |
1071 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1103 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
|
|
1104 | ALIAS: |
|
|
1105 | ber_decode_prefix = 1 |
1072 | CODE: |
1106 | PPCODE: |
1073 | { |
1107 | { |
1074 | cur_profile = SvPROFILE (profile); |
1108 | cur_profile = SvPROFILE (profile); |
1075 | STRLEN len; |
1109 | STRLEN len; |
1076 | buf = (U8 *)SvPVbyte (ber, len); |
1110 | buf = (U8 *)SvPVbyte (ber, len); |
1077 | cur = buf; |
1111 | cur = buf; |
1078 | end = buf + len; |
1112 | end = buf + len; |
1079 | |
1113 | |
1080 | RETVAL = decode_ber (); |
1114 | SV *tuple = decode_ber (); |
|
|
1115 | |
|
|
1116 | EXTEND (SP, 2); |
|
|
1117 | PUSHs (sv_2mortal (tuple)); |
|
|
1118 | |
|
|
1119 | if (ix) |
|
|
1120 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
|
|
1121 | else if (cur != end) |
|
|
1122 | error ("trailing garbage after BER value"); |
1081 | } |
1123 | } |
1082 | OUTPUT: RETVAL |
|
|
1083 | |
1124 | |
1084 | void |
1125 | 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) |
1126 | 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: |
1127 | PPCODE: |
1087 | { |
1128 | { |