Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 1 | #include <ber_decoder.c> |
| 2 | #include <ber_tlv_length.c> |
| 3 | #include <ber_tlv_tag.c> |
| 4 | #include <der_encoder.c> |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 5 | #include <xer_decoder.c> |
| 6 | #include <xer_support.c> |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 7 | #include <per_support.c> |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 8 | #include <constraints.c> |
| 9 | #undef ADVANCE |
| 10 | #undef RETURN |
| 11 | #undef LEFT |
| 12 | #include <OCTET_STRING.c> |
| 13 | |
| 14 | |
| 15 | uint8_t *buf; |
| 16 | size_t buf_size; |
| 17 | size_t buf_off; |
| 18 | |
| 19 | static int |
| 20 | write_to_buf(const void *buffer, size_t size, void *key) { |
| 21 | (void)key; |
| 22 | |
| 23 | if(buf_off + size > buf_size) { |
| 24 | size_t n = buf_size?:16; |
| 25 | while(n < buf_off + size) n <<= 2; |
| 26 | buf = realloc(buf, n); |
| 27 | assert(buf); |
| 28 | buf_size = n; |
| 29 | } |
| 30 | |
| 31 | memcpy(buf + buf_off, buffer, size); |
| 32 | |
| 33 | buf_off += size; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | static void |
| 39 | check(int size) { |
| 40 | OCTET_STRING_t *os; |
| 41 | OCTET_STRING_t *nos = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 42 | asn_enc_rval_t erval; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 43 | asn_dec_rval_t rval; |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 44 | int i; |
| 45 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 46 | os = OCTET_STRING_new_fromBuf(&asn_DEF_OCTET_STRING, 0, size); |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 47 | assert(os); |
| 48 | assert(os->size == 0); |
| 49 | |
| 50 | os->buf = malloc(size); |
| 51 | assert(os->buf); |
| 52 | os->size = size; |
| 53 | |
| 54 | for(i = 0; i < size; i++) { |
| 55 | os->buf[i] = i; |
| 56 | } |
| 57 | |
| 58 | buf_off = 0; |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 59 | erval = der_encode(&asn_DEF_OCTET_STRING, |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 60 | os, write_to_buf, 0); |
| 61 | assert(erval.encoded == buf_off); |
| 62 | assert(buf_off > size); |
| 63 | |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 64 | rval = ber_decode(0, &asn_DEF_OCTET_STRING, (void **)&nos, buf, buf_off); |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 65 | assert(rval.code == RC_OK); |
| 66 | assert(rval.consumed == buf_off); |
| 67 | |
| 68 | assert(os->size == nos->size); |
| 69 | |
| 70 | for(i = 0; i < size; i++) { |
| 71 | assert(os->buf[i] == nos->buf[i]); |
| 72 | } |
| 73 | |
| 74 | if(0) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 75 | fprintf(stderr, "new(%d):", size); |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 76 | for(i = 0; i < (buf_off<10?buf_off:10); i++) |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 77 | fprintf(stderr, " %02x", buf[i]); |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 78 | printf("\n"); |
| 79 | } |
| 80 | |
| 81 | |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 82 | asn_DEF_OCTET_STRING.free_struct(&asn_DEF_OCTET_STRING, os, 0); |
| 83 | asn_DEF_OCTET_STRING.free_struct(&asn_DEF_OCTET_STRING, nos, 0); |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int |
| 87 | main() { |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 88 | uint8_t buf1[] = { 0x85, 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 89 | uint8_t buf2[] = { 0x85, 0x00, 0x7f, 0xff, 0x03, 0x04 }; |
| 90 | uint8_t buf3[] = { 0x85, 0x00, 0x7f, 0xff, 0xff, 0x04 }; |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 91 | uint8_t buf4[] = { 0x89, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x04 }; |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 92 | ber_tlv_len_t tlv_len; |
| 93 | ssize_t ret; |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 94 | int i; |
| 95 | |
| 96 | for(i = 0; i < 66000; i++) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 97 | if(i == 4500) i = 64000; /* Jump */ |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 98 | check(i); |
| 99 | } |
| 100 | |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 101 | ret = ber_fetch_length(0, buf1, sizeof(buf1), &tlv_len); |
Lev Walkin | 6c45231 | 2004-10-26 08:02:01 +0000 | [diff] [blame] | 102 | printf("ret=%ld, len=%ld\n", (long)ret, (long)tlv_len); |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 103 | assert(ret == sizeof(buf1)); |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 104 | assert(tlv_len == 0x01020304); |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 105 | |
| 106 | ret = ber_fetch_length(0, buf2, sizeof(buf2), &tlv_len); |
Lev Walkin | 6c45231 | 2004-10-26 08:02:01 +0000 | [diff] [blame] | 107 | printf("ret=%ld, len=%ld\n", (long)ret, (long)tlv_len); |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 108 | assert(ret == sizeof(buf2)); |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 109 | assert(tlv_len == 0x7fff0304); |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 110 | |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Here although tlv_len is not greater than 2^31, |
| 113 | * we ought to hit an embedded length exploitation preventive check. |
| 114 | */ |
| 115 | if(sizeof(tlv_len) <= 4) { |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 116 | ret = ber_fetch_length(0, buf3, sizeof(buf3), &tlv_len); |
| 117 | printf("ret=%ld\n", (long)ret); |
| 118 | assert(ret == -1); |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 119 | } |
| 120 | if(sizeof(tlv_len) <= 8) { |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 121 | ret = ber_fetch_length(0, buf4, sizeof(buf4), &tlv_len); |
Lev Walkin | 8db9fab | 2006-07-13 09:22:34 +0000 | [diff] [blame] | 122 | printf("ret=%lld\n", (long long)ret); |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 123 | assert(ret == -1); |
| 124 | } |
Lev Walkin | 27d7049 | 2004-09-29 13:24:33 +0000 | [diff] [blame] | 125 | |
Lev Walkin | cb90bfc | 2004-08-19 16:42:54 +0000 | [diff] [blame] | 126 | return 0; |
| 127 | } |