ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-BER-XS/XS.xs
(Generate patch)

Comparing Convert-BER-XS/XS.xs (file contents):
Revision 1.15 by root, Sat Apr 20 15:23:26 2019 UTC vs.
Revision 1.20 by root, Sat Apr 20 17:04:35 2019 UTC

80 BER_TYPE_IPADDRESS, 80 BER_TYPE_IPADDRESS,
81 BER_TYPE_CROAK, 81 BER_TYPE_CROAK,
82}; 82};
83 83
84enum { 84enum {
85 BER_CLASS = 0, 85 BER_CLASS = 0,
86 BER_TAG = 1, 86 BER_TAG = 1,
87 BER_CONSTRUCTED = 2, 87 BER_FLAGS = 2,
88 BER_DATA = 3, 88 BER_DATA = 3,
89 BER_ARRAYSIZE 89 BER_ARRAYSIZE
90}; 90};
91 91
92#define MAX_OID_STRLEN 4096 92#define MAX_OID_STRLEN 4096
93 93
252// get ber-encoded integer (i.e. pack "w") 252// get ber-encoded integer (i.e. pack "w")
253static UV 253static UV
254get_w (void) 254get_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;
268
269 c = get_u8 ();
265 } 270 }
266} 271}
267 272
268static UV 273static UV
269get_length (void) 274get_length (void)
277 282
278 switch (cnt) 283 switch (cnt)
279 { 284 {
280 case 0: 285 case 0:
281 error ("indefinite ASN.1 lengths not supported"); 286 error ("indefinite ASN.1 lengths not supported");
282 return 0; 287
288 case 0x7f:
289 error ("ASN.1 reserved value in length (X.690 8.1.3.5)");
283 290
284 default: 291 default:
285 error ("ASN.1 length too long"); 292 error ("ASN.1 length too long (only up to 2**64 octets supported)");
286 return 0;
287 293
288 case 8: res = (res << 8) | get_u8 (); 294 case 8: res = (res << 8) | get_u8 ();
289 case 7: res = (res << 8) | get_u8 (); 295 case 7: res = (res << 8) | get_u8 ();
290 case 6: res = (res << 8) | get_u8 (); 296 case 6: res = (res << 8) | get_u8 ();
291 case 5: res = (res << 8) | get_u8 (); 297 case 5: res = (res << 8) | get_u8 ();
303decode_int (void) 309decode_int (void)
304{ 310{
305 UV len = get_length (); 311 UV len = get_length ();
306 312
307 if (!len) 313 if (!len)
308 {
309 error ("invalid integer length equal to zero"); 314 error ("invalid integer length equal to zero (X.690 8.3.1)");
310 return 0;
311 }
312 315
313 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 }
314 325
315 int negative = data [0] & 0x80; 326 int negative = data [0] & 0x80;
316 327
317 UV val = negative ? -1 : 0; // copy signbit to all bits 328 UV val = negative ? -1 : 0; // copy signbit to all bits
318 329
382 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
383 char *app = oid; 394 char *app = oid;
384 395
385 if (relative) 396 if (relative)
386 app = write_uv (app, w); 397 app = write_uv (app, w);
387 else 398 else if (w < 2 * 40)
388 { 399 {
389 app = write_uv (app, (U8)w / 40); 400 app = write_uv (app, (U8)w / 40);
390 *app++ = '.'; 401 *app++ = '.';
391 app = write_uv (app, (U8)w % 40); 402 app = write_uv (app, (U8)w % 40);
403 }
404 else
405 {
406 app = write_uv (app, 2);
407 *app++ = '.';
408 app = write_uv (app, w - 2 * 40);
392 } 409 }
393 410
394 while (cur < end) 411 while (cur < end)
395 { 412 {
396 // we assume an oid component is never > 64 digits 413 // we assume an oid component is never > 64 digits
453 int tag = identifier & ASN_TAG_MASK; 470 int tag = identifier & ASN_TAG_MASK;
454 471
455 if (tag == ASN_TAG_BER) 472 if (tag == ASN_TAG_BER)
456 tag = get_w (); 473 tag = get_w ();
457 474
458 if (tag == ASN_TAG_BER)
459 tag = get_w ();
460
461 if (constructed) 475 if (constructed)
462 { 476 {
463 UV len = get_length (); 477 UV len = get_length ();
464 UV seqend = (cur - buf) + len; 478 UV seqend = (cur - buf) + len;
465 AV *av = (AV *)sv_2mortal ((SV *)newAV ()); 479 AV *av = (AV *)sv_2mortal ((SV *)newAV ());
466 480
467 while (cur < buf + seqend) 481 while (cur < buf + seqend)
468 av_push (av, decode_ber ()); 482 av_push (av, decode_ber ());
469 483
470 if (cur > buf + seqend) 484 if (cur > buf + seqend)
471 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);
472 486
473 res = newRV_inc ((SV *)av); 487 res = newRV_inc ((SV *)av);
474 } 488 }
475 else 489 else
476 switch (profile_lookup (cur_profile, klass, tag)) 490 switch (profile_lookup (cur_profile, klass, tag))
478 case BER_TYPE_NULL: 492 case BER_TYPE_NULL:
479 { 493 {
480 UV len = get_length (); 494 UV len = get_length ();
481 495
482 if (len) 496 if (len)
483 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);
484 498
485 res = &PL_sv_undef; 499 res = &PL_sv_undef;
486 } 500 }
487 break; 501 break;
488 502
489 case BER_TYPE_BOOL: 503 case BER_TYPE_BOOL:
490 { 504 {
491 UV len = get_length (); 505 UV len = get_length ();
492 506
493 if (len != 1) 507 if (len != 1)
494 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);
495 509
496 res = newSVcacheint (!!get_u8 ()); 510 res = newSVcacheint (!!get_u8 ());
497 } 511 }
498 break; 512 break;
499 513
521 case BER_TYPE_IPADDRESS: 535 case BER_TYPE_IPADDRESS:
522 { 536 {
523 UV len = get_length (); 537 UV len = get_length ();
524 538
525 if (len != 4) 539 if (len != 4)
526 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);
527 541
528 U8 c1 = get_u8 (); 542 U8 c1 = get_u8 ();
529 U8 c2 = get_u8 (); 543 U8 c2 = get_u8 ();
530 U8 c3 = get_u8 (); 544 U8 c3 = get_u8 ();
531 U8 c4 = get_u8 (); 545 U8 c4 = get_u8 ();
548 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag); 562 croak ("unconfigured/unsupported class/tag %d/%d", klass, tag);
549 } 563 }
550 564
551 AV *av = newAV (); 565 AV *av = newAV ();
552 av_fill (av, BER_ARRAYSIZE - 1); 566 av_fill (av, BER_ARRAYSIZE - 1);
553 AvARRAY (av)[BER_CLASS ] = newSVcacheint (klass); 567 AvARRAY (av)[BER_CLASS] = newSVcacheint (klass);
554 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag); 568 AvARRAY (av)[BER_TAG ] = newSVcacheint (tag);
555 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (constructed ? 1 : 0); 569 AvARRAY (av)[BER_FLAGS] = newSVcacheint (constructed ? 1 : 0);
556 AvARRAY (av)[BER_DATA ] = res; 570 AvARRAY (av)[BER_DATA ] = res;
557 571
558 return newRV_noinc ((SV *)av); 572 return newRV_noinc ((SV *)av);
559} 573}
560 574
561///////////////////////////////////////////////////////////////////////////// 575/////////////////////////////////////////////////////////////////////////////
566strlen_sum (STRLEN l1, STRLEN l2) 580strlen_sum (STRLEN l1, STRLEN l2)
567{ 581{
568 size_t sum = l1 + l2; 582 size_t sum = l1 + l2;
569 583
570 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) 584 if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum)
571 croak ("JSON::XS: string size overflow"); 585 croak ("Convert::BER::XS: string size overflow");
572 586
573 return sum; 587 return sum;
574} 588}
575 589
576static void 590static void
881{ 895{
882 AV *av = ber_tuple (tuple); 896 AV *av = ber_tuple (tuple);
883 897
884 int klass = SvIV (AvARRAY (av)[BER_CLASS]); 898 int klass = SvIV (AvARRAY (av)[BER_CLASS]);
885 int tag = SvIV (AvARRAY (av)[BER_TAG]); 899 int tag = SvIV (AvARRAY (av)[BER_TAG]);
886 int constructed = SvIV (AvARRAY (av)[BER_CONSTRUCTED]) ? ASN_CONSTRUCTED : 0; 900 int constructed = SvIV (AvARRAY (av)[BER_FLAGS]) & 1 ? ASN_CONSTRUCTED : 0;
887 SV *data = AvARRAY (av)[BER_DATA]; 901 SV *data = AvARRAY (av)[BER_DATA];
888 902
889 int identifier = (klass << ASN_CLASS_SHIFT) | constructed; 903 int identifier = (klass << ASN_CLASS_SHIFT) | constructed;
890 904
891 if (expect_false (tag >= ASN_TAG_BER)) 905 if (expect_false (tag >= ASN_TAG_BER))
925 put_length (0); 939 put_length (0);
926 break; 940 break;
927 941
928 case BER_TYPE_BOOL: 942 case BER_TYPE_BOOL:
929 put_length (1); 943 put_length (1);
930 *cur++ = SvTRUE (data) ? 0xff : 0x00; 944 *cur++ = SvTRUE (data) ? 0xff : 0x00; // 0xff = DER/CER
931 break; 945 break;
932 946
933 case BER_TYPE_OID: 947 case BER_TYPE_OID:
934 encode_oid (data, 0); 948 encode_oid (data, 0);
935 break; 949 break;
1037 const_iv (ASN_CONTEXT) 1051 const_iv (ASN_CONTEXT)
1038 const_iv (ASN_PRIVATE) 1052 const_iv (ASN_PRIVATE)
1039 1053
1040 const_iv (BER_CLASS) 1054 const_iv (BER_CLASS)
1041 const_iv (BER_TAG) 1055 const_iv (BER_TAG)
1042 const_iv (BER_CONSTRUCTED) 1056 const_iv (BER_FLAGS)
1043 const_iv (BER_DATA) 1057 const_iv (BER_DATA)
1044 1058
1045 const_iv (BER_TYPE_BYTES) 1059 const_iv (BER_TYPE_BYTES)
1046 const_iv (BER_TYPE_UTF8) 1060 const_iv (BER_TYPE_UTF8)
1047 const_iv (BER_TYPE_UCS2) 1061 const_iv (BER_TYPE_UCS2)
1080 RETVAL = decode_ber (); 1094 RETVAL = decode_ber ();
1081} 1095}
1082 OUTPUT: RETVAL 1096 OUTPUT: RETVAL
1083 1097
1084void 1098void
1085ber_is (SV *tuple, SV *klass = &PL_sv_undef, SV *tag = &PL_sv_undef, SV *constructed = &PL_sv_undef, SV *data = &PL_sv_undef) 1099ber_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: 1100 PPCODE:
1087{ 1101{
1088 if (!SvOK (tuple)) 1102 if (!SvOK (tuple))
1089 XSRETURN_NO; 1103 XSRETURN_NO;
1090 1104
1092 croak ("ber_is: tuple must be BER tuple (array-ref)"); 1106 croak ("ber_is: tuple must be BER tuple (array-ref)");
1093 1107
1094 AV *av = (AV *)SvRV (tuple); 1108 AV *av = (AV *)SvRV (tuple);
1095 1109
1096 XPUSHs ( 1110 XPUSHs (
1097 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS ]) == SvIV (klass)) 1111 (!SvOK (klass) || SvIV (AvARRAY (av)[BER_CLASS]) == SvIV (klass))
1098 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag)) 1112 && (!SvOK (tag) || SvIV (AvARRAY (av)[BER_TAG ]) == SvIV (tag))
1099 && (!SvOK (constructed) || !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) == !SvIV (constructed)) 1113 && (!SvOK (flags) || !SvIV (AvARRAY (av)[BER_FLAGS]) == !SvIV (flags))
1100 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data)) 1114 && (!SvOK (data) || sv_eq (AvARRAY (av)[BER_DATA ], data))
1101 ? &PL_sv_yes : &PL_sv_undef); 1115 ? &PL_sv_yes : &PL_sv_undef);
1102} 1116}
1103 1117
1104void 1118void
1105ber_is_seq (SV *tuple) 1119ber_is_seq (SV *tuple)
1109 XSRETURN_UNDEF; 1123 XSRETURN_UNDEF;
1110 1124
1111 AV *av = ber_tuple (tuple); 1125 AV *av = ber_tuple (tuple);
1112 1126
1113 XPUSHs ( 1127 XPUSHs (
1114 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1128 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1115 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE 1129 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_SEQUENCE
1116 && SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1130 && SvIV (AvARRAY (av)[BER_FLAGS])
1117 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef); 1131 ? AvARRAY (av)[BER_DATA] : &PL_sv_undef);
1118} 1132}
1119 1133
1120void 1134void
1121ber_is_int (SV *tuple, SV *value = &PL_sv_undef) 1135ber_is_int (SV *tuple, SV *value = &PL_sv_undef)
1127 AV *av = ber_tuple (tuple); 1141 AV *av = ber_tuple (tuple);
1128 1142
1129 UV data = SvUV (AvARRAY (av)[BER_DATA]); 1143 UV data = SvUV (AvARRAY (av)[BER_DATA]);
1130 1144
1131 XPUSHs ( 1145 XPUSHs (
1132 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1146 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1133 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER 1147 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_INTEGER
1134 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1148 && !SvIV (AvARRAY (av)[BER_FLAGS])
1135 && (!SvOK (value) || data == SvUV (value)) 1149 && (!SvOK (value) || data == SvUV (value))
1136 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0)) 1150 ? sv_2mortal (data ? newSVsv (AvARRAY (av)[BER_DATA]) : newSVpv ("0 but true", 0))
1137 : &PL_sv_undef); 1151 : &PL_sv_undef);
1138} 1152}
1139 1153
1145 XSRETURN_NO; 1159 XSRETURN_NO;
1146 1160
1147 AV *av = ber_tuple (tuple); 1161 AV *av = ber_tuple (tuple);
1148 1162
1149 XPUSHs ( 1163 XPUSHs (
1150 SvIV (AvARRAY (av)[BER_CLASS ]) == ASN_UNIVERSAL 1164 SvIV (AvARRAY (av)[BER_CLASS]) == ASN_UNIVERSAL
1151 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER 1165 && SvIV (AvARRAY (av)[BER_TAG ]) == ASN_OBJECT_IDENTIFIER
1152 && !SvIV (AvARRAY (av)[BER_CONSTRUCTED]) 1166 && !SvIV (AvARRAY (av)[BER_FLAGS])
1153 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid)) 1167 && (!SvOK (oid) || sv_eq (AvARRAY (av)[BER_DATA], oid))
1154 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef); 1168 ? newSVsv (AvARRAY (av)[BER_DATA]) : &PL_sv_undef);
1155} 1169}
1156 1170
1157############################################################################# 1171#############################################################################
1175ber_int (SV *sv) 1189ber_int (SV *sv)
1176 CODE: 1190 CODE:
1177{ 1191{
1178 AV *av = newAV (); 1192 AV *av = newAV ();
1179 av_fill (av, BER_ARRAYSIZE - 1); 1193 av_fill (av, BER_ARRAYSIZE - 1);
1180 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1194 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1181 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER); 1195 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_INTEGER);
1182 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (0); 1196 AvARRAY (av)[BER_FLAGS] = newSVcacheint (0);
1183 AvARRAY (av)[BER_DATA ] = newSVsv (sv); 1197 AvARRAY (av)[BER_DATA ] = newSVsv (sv);
1184 RETVAL = newRV_noinc ((SV *)av); 1198 RETVAL = newRV_noinc ((SV *)av);
1185} 1199}
1186 OUTPUT: RETVAL 1200 OUTPUT: RETVAL
1187 1201
1188# TODO: not arrayref, but elements? 1202# TODO: not arrayref, but elements?
1190ber_seq (SV *arrayref) 1204ber_seq (SV *arrayref)
1191 CODE: 1205 CODE:
1192{ 1206{
1193 AV *av = newAV (); 1207 AV *av = newAV ();
1194 av_fill (av, BER_ARRAYSIZE - 1); 1208 av_fill (av, BER_ARRAYSIZE - 1);
1195 AvARRAY (av)[BER_CLASS ] = newSVcacheint (ASN_UNIVERSAL); 1209 AvARRAY (av)[BER_CLASS] = newSVcacheint (ASN_UNIVERSAL);
1196 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE); 1210 AvARRAY (av)[BER_TAG ] = newSVcacheint (ASN_SEQUENCE);
1197 AvARRAY (av)[BER_CONSTRUCTED] = newSVcacheint (1); 1211 AvARRAY (av)[BER_FLAGS] = newSVcacheint (1);
1198 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref); 1212 AvARRAY (av)[BER_DATA ] = newSVsv (arrayref);
1199 RETVAL = newRV_noinc ((SV *)av); 1213 RETVAL = newRV_noinc ((SV *)av);
1200} 1214}
1201 OUTPUT: RETVAL 1215 OUTPUT: RETVAL
1202 1216
1203MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile 1217MODULE = Convert::BER::XS PACKAGE = Convert::BER::XS::Profile

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines