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