… | |
… | |
228 | } |
228 | } |
229 | |
229 | |
230 | // get_* functions fetch something from the buffer |
230 | // get_* functions fetch something from the buffer |
231 | // decode_* functions use get_* fun ctions to decode ber values |
231 | // decode_* functions use get_* fun ctions to decode ber values |
232 | |
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 | |
233 | // get n octets |
243 | // get n octets |
234 | static U8 * |
244 | static U8 * |
235 | get_n (UV count) |
245 | get_n (UV count) |
236 | { |
246 | { |
237 | want (count); |
247 | want (count); |
238 | U8 *res = cur; |
248 | U8 *res = cur; |
239 | cur += count; |
249 | cur += count; |
240 | return res; |
250 | return res; |
241 | } |
251 | } |
242 | |
252 | |
243 | // get single octet |
|
|
244 | static U8 |
|
|
245 | get_u8 (void) |
|
|
246 | { |
|
|
247 | if (cur == end) |
|
|
248 | error ("unexpected end of message buffer"); |
|
|
249 | |
|
|
250 | return *cur++; |
|
|
251 | } |
|
|
252 | |
|
|
253 | // get ber-encoded integer (i.e. pack "w") |
253 | // get ber-encoded integer (i.e. pack "w") |
254 | static UV |
254 | static UV |
255 | get_w (void) |
255 | get_w (void) |
256 | { |
256 | { |
257 | UV res = 0; |
257 | UV res = 0; |
… | |
… | |
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
261 | error ("illegal BER padding (X.690 8.1.2.4.2, 8.19.2)"); |
262 | |
262 | |
263 | for (;;) |
263 | for (;;) |
264 | { |
264 | { |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
265 | if (expect_false (res >> UVSIZE * 8 - 7)) |
266 | error ("BER variable integer overflow"); |
266 | error ("BER variable length integer overflow"); |
267 | |
267 | |
268 | res = (res << 7) | (c & 0x7f); |
268 | res = (res << 7) | (c & 0x7f); |
269 | |
269 | |
270 | if (!(c & 0x80)) |
270 | if (!(c & 0x80)) |
271 | return res; |
271 | return res; |
… | |
… | |
285 | res = 0; |
285 | res = 0; |
286 | |
286 | |
287 | switch (cnt) |
287 | switch (cnt) |
288 | { |
288 | { |
289 | case 0: |
289 | case 0: |
290 | error ("indefinite ASN.1 lengths not supported"); |
290 | error ("indefinite BER value lengths not supported"); |
291 | |
291 | |
292 | case 0x7f: |
292 | case 0x7f: |
293 | error ("ASN.1 reserved value in length (X.690 8.1.3.5)"); |
293 | error ("BER reserved value in length (X.690 8.1.3.5)"); |
294 | |
294 | |
295 | default: |
295 | default: |
296 | error ("ASN.1 length too long (only up to 2**64 octets supported)"); |
296 | error ("BER value length too long (must fit into UV)"); |
297 | |
297 | |
|
|
298 | #if UVSIZE > 4 |
298 | case 8: res = (res << 8) | get_u8 (); |
299 | case 8: res = (res << 8) | get_u8 (); |
299 | case 7: res = (res << 8) | get_u8 (); |
300 | case 7: res = (res << 8) | get_u8 (); |
300 | case 6: res = (res << 8) | get_u8 (); |
301 | case 6: res = (res << 8) | get_u8 (); |
301 | case 5: res = (res << 8) | get_u8 (); |
302 | case 5: res = (res << 8) | get_u8 (); |
|
|
303 | #endif |
302 | case 4: res = (res << 8) | get_u8 (); |
304 | case 4: res = (res << 8) | get_u8 (); |
303 | case 3: res = (res << 8) | get_u8 (); |
305 | case 3: res = (res << 8) | get_u8 (); |
304 | case 2: res = (res << 8) | get_u8 (); |
306 | case 2: res = (res << 8) | get_u8 (); |
305 | case 1: res = (res << 8) | get_u8 (); |
307 | case 1: res = (res << 8) | get_u8 (); |
306 | } |
308 | } |
… | |
… | |
313 | decode_int (void) |
315 | decode_int (void) |
314 | { |
316 | { |
315 | UV len = get_length (); |
317 | UV len = get_length (); |
316 | |
318 | |
317 | if (!len) |
319 | if (!len) |
318 | error ("invalid integer length equal to zero (X.690 8.3.1)"); |
320 | error ("invalid BER_TYPE_INT length zero (X.690 8.3.1)"); |
319 | |
321 | |
320 | U8 *data = get_n (len); |
322 | U8 *data = get_n (len); |
321 | |
323 | |
322 | if (expect_false (len > 1)) |
324 | if (expect_false (len > 1)) |
323 | { |
325 | { |
324 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
326 | U16 mask = (data [0] << 8) | data [1] & 0xff80; |
325 | |
327 | |
326 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
328 | if (expect_false (mask == 0xff80 || mask == 0x0000)) |
327 | error ("illegal padding in integer (X.690 8.3.2)"); |
329 | error ("illegal padding in BER_TYPE_INT (X.690 8.3.2)"); |
328 | } |
330 | } |
329 | |
331 | |
330 | int negative = data [0] & 0x80; |
332 | int negative = data [0] & 0x80; |
331 | |
333 | |
332 | UV val = negative ? -1 : 0; // copy signbit to all bits |
334 | UV val = negative ? -1 : 0; // copy signbit to all bits |
333 | |
335 | |
334 | if (len > UVSIZE + (!negative && !*data)) |
336 | if (len > UVSIZE + (!negative && !*data)) |
335 | error ("INTEGER overflow"); |
337 | error ("BER_TYPE_INT overflow"); |
336 | |
338 | |
337 | do |
339 | do |
338 | val = (val << 8) | *data++; |
340 | val = (val << 8) | *data++; |
339 | while (--len); |
341 | while (--len); |
340 | |
342 | |
… | |
… | |
388 | { |
390 | { |
389 | UV len = get_length (); |
391 | UV len = get_length (); |
390 | |
392 | |
391 | if (len <= 0) |
393 | if (len <= 0) |
392 | { |
394 | { |
393 | error ("OBJECT IDENTIFIER length equal to zero"); |
395 | error ("BER_TYPE_OID length must not be zero"); |
394 | return &PL_sv_undef; |
396 | return &PL_sv_undef; |
395 | } |
397 | } |
396 | |
398 | |
397 | U8 *end = cur + len; |
399 | U8 *end = cur + len; |
398 | UV w = get_w (); |
400 | UV w = get_w (); |
… | |
… | |
487 | |
489 | |
488 | while (cur < buf + seqend) |
490 | while (cur < buf + seqend) |
489 | av_push (av, decode_ber ()); |
491 | av_push (av, decode_ber ()); |
490 | |
492 | |
491 | if (cur > buf + seqend) |
493 | if (cur > buf + seqend) |
492 | croak ("constructed type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
494 | croak ("CONSTRUCTED type %02x length overflow (0x%x 0x%x)\n", identifier, (int)(cur - buf), (int)seqend); |
493 | |
495 | |
494 | res = newRV_inc ((SV *)av); |
496 | res = newRV_inc ((SV *)av); |
495 | } |
497 | } |
496 | else |
498 | else |
497 | switch (profile_lookup (cur_profile, klass, tag)) |
499 | switch (profile_lookup (cur_profile, klass, tag)) |
… | |
… | |
562 | case BER_TYPE_UCS4: |
564 | case BER_TYPE_UCS4: |
563 | res = decode_ucs (4); |
565 | res = decode_ucs (4); |
564 | break; |
566 | break; |
565 | |
567 | |
566 | case BER_TYPE_REAL: |
568 | case BER_TYPE_REAL: |
|
|
569 | error ("BER_TYPE_REAL not implemented"); |
|
|
570 | |
567 | case BER_TYPE_CROAK: |
571 | case BER_TYPE_CROAK: |
|
|
572 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
573 | |
568 | default: |
574 | default: |
569 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
575 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
570 | } |
576 | } |
571 | |
577 | |
572 | AV *av = newAV (); |
578 | AV *av = newAV (); |
… | |
… | |
923 | // and adjust later |
929 | // and adjust later |
924 | need (1); |
930 | need (1); |
925 | STRLEN mark = len_fixup_mark (); |
931 | STRLEN mark = len_fixup_mark (); |
926 | |
932 | |
927 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
933 | if (expect_false (!SvROK (data) || SvTYPE (SvRV (data)) != SVt_PVAV)) |
928 | croak ("BER constructed data must be array-reference"); |
934 | croak ("BER CONSTRUCTED data must be array-reference"); |
929 | |
935 | |
930 | AV *av = (AV *)SvRV (data); |
936 | AV *av = (AV *)SvRV (data); |
931 | int fill = AvFILL (av); |
937 | int fill = AvFILL (av); |
932 | |
938 | |
933 | if (expect_false (SvRMAGICAL (av))) |
939 | if (expect_false (SvRMAGICAL (av))) |
934 | croak ("BER constructed data must not be tied"); |
940 | croak ("BER CONSTRUCTED data must not be tied"); |
935 | |
941 | |
936 | int i; |
942 | int i; |
937 | for (i = 0; i <= fill; ++i) |
943 | for (i = 0; i <= fill; ++i) |
938 | encode_ber (AvARRAY (av)[i]); |
944 | encode_ber (AvARRAY (av)[i]); |
939 | |
945 | |
… | |
… | |
994 | case BER_TYPE_UCS4: |
1000 | case BER_TYPE_UCS4: |
995 | encode_ucs (data, 4); |
1001 | encode_ucs (data, 4); |
996 | break; |
1002 | break; |
997 | |
1003 | |
998 | case BER_TYPE_REAL: |
1004 | case BER_TYPE_REAL: |
|
|
1005 | croak ("BER_TYPE_REAL not implemented"); |
|
|
1006 | |
999 | case BER_TYPE_CROAK: |
1007 | case BER_TYPE_CROAK: |
|
|
1008 | croak ("class/tag %d/%d mapped to BER_TYPE_CROAK", klass, tag); |
|
|
1009 | |
1000 | default: |
1010 | default: |
1001 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1011 | croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); |
1002 | } |
1012 | } |
1003 | |
1013 | |
1004 | } |
1014 | } |
… | |
… | |
1107 | PUSHs (sv_2mortal (tuple)); |
1117 | PUSHs (sv_2mortal (tuple)); |
1108 | |
1118 | |
1109 | if (ix) |
1119 | if (ix) |
1110 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
1120 | PUSHs (sv_2mortal (newSViv (cur - buf))); |
1111 | else if (cur != end) |
1121 | else if (cur != end) |
1112 | error ("trailing garbage after BER data"); |
1122 | error ("trailing garbage after BER value"); |
1113 | } |
1123 | } |
1114 | |
1124 | |
1115 | void |
1125 | void |
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) |
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) |
1117 | PPCODE: |
1127 | PPCODE: |