Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #undef NDEBUG |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <string.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include <T1.h> |
| 9 | |
| 10 | uint8_t buf1[] = { |
| 11 | 32 | 16, /* [UNIVERSAL 16], constructed */ |
| 12 | 12, /* L */ |
| 13 | /* INTEGER a */ |
| 14 | ((2 << 6) + 0), /* [0], primitive */ |
| 15 | 2, /* L */ |
| 16 | 150, |
| 17 | 70, |
| 18 | /* b [1] EXPLICIT CHOICE */ |
| 19 | 32 | ((2 << 6) + 1), /* [1] */ |
| 20 | 3, /* L */ |
| 21 | ((2 << 6) + 1), /* [1] */ |
| 22 | 1, |
| 23 | 'i', |
| 24 | /* UTF8String c */ |
| 25 | ((2 << 6) + 2), /* [2], primitive */ |
| 26 | 1, /* L */ |
| 27 | 'x' |
| 28 | }; |
| 29 | |
| 30 | uint8_t buf2[128]; |
| 31 | int buf2_pos; |
| 32 | |
| 33 | static int |
| 34 | buf2_fill(const void *buffer, size_t size, void *app_key) { |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 35 | |
| 36 | (void)app_key; |
| 37 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | if(buf2_pos + size > sizeof(buf2)) |
| 39 | return -1; |
| 40 | |
| 41 | memcpy(buf2 + buf2_pos, buffer, size); |
| 42 | buf2_pos += size; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 48 | check(int is_ok, uint8_t *buf, size_t size, size_t consumed) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | T1_t t, *tp; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 50 | void *tpp = &tp; |
Lev Walkin | 90bf7ae | 2004-10-20 15:46:56 +0000 | [diff] [blame] | 51 | asn_dec_rval_t rval; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 52 | asn_enc_rval_t erval; |
| 53 | int ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 54 | int i; |
| 55 | |
| 56 | tp = memset(&t, 0, sizeof(t)); |
| 57 | |
| 58 | fprintf(stderr, "Buf %p\n", buf); |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 59 | rval = ber_decode(0, &asn_DEF_T1, (void **)tpp, buf, size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | fprintf(stderr, "Returned code %d, consumed %d\n", |
| 61 | (int)rval.code, (int)rval.consumed); |
| 62 | |
| 63 | if(is_ok) { |
| 64 | assert(rval.code == RC_OK); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 65 | assert(rval.consumed == (size_t)consumed); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | assert(t.a.size == 2); |
| 67 | assert(t.b.present == b_PR_n); |
| 68 | assert(t.b.choice.n.size == 1); |
| 69 | assert(t.b.choice.n.buf[0] == 'i'); |
| 70 | assert(t.c.size == 1); |
| 71 | assert(t.c.buf[0] == 'x'); |
| 72 | |
| 73 | } else { |
| 74 | if(rval.code == RC_OK) { |
| 75 | assert(t.a.size != 2 |
| 76 | || t.b.present != b_PR_n |
| 77 | || t.b.choice.n.size != 1 |
| 78 | || t.c.size != 1 |
| 79 | ); |
| 80 | } |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 81 | assert(rval.consumed <= (size_t)consumed); |
Vasil Velichkov | cef21e0 | 2017-10-09 23:40:17 +0300 | [diff] [blame^] | 82 | ASN_STRUCT_RESET(asn_DEF_T1, tp); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | fprintf(stderr, "=> Re-creating using DER encoder <=\n"); |
| 87 | |
| 88 | /* |
| 89 | * Try to re-create using DER encoding. |
| 90 | */ |
| 91 | buf2_pos = 0; |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 92 | erval = der_encode(&asn_DEF_T1, tp, buf2_fill, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | assert(erval.encoded != -1); |
| 94 | if(erval.encoded != sizeof(buf1)) { |
| 95 | printf("%d != %d\n", (int)erval.encoded, (int)sizeof(buf1)); |
| 96 | } |
Lev Walkin | 5b63acf | 2014-01-14 01:48:37 -0800 | [diff] [blame] | 97 | assert(erval.encoded == (ssize_t)sizeof(buf1)); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 98 | for(i = 0; i < (ssize_t)sizeof(buf1); i++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | if(buf1[i] != buf2[i]) { |
| 100 | fprintf(stderr, "Recreated buffer content mismatch:\n"); |
| 101 | fprintf(stderr, "Byte %d, %x != %x (%d != %d)\n", |
| 102 | i, |
| 103 | buf1[i], buf2[i], |
| 104 | buf1[i], buf2[i] |
| 105 | ); |
| 106 | } |
| 107 | assert(buf1[i] == buf2[i]); |
| 108 | } |
| 109 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 110 | fprintf(stderr, "=== asn_fprint() ===\n"); |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 111 | ret = asn_fprint(stderr, &asn_DEF_T1, tp); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 112 | assert(ret == 0); |
| 113 | fprintf(stderr, "=== xer_fprint() ===\n"); |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 114 | ret = xer_fprint(stderr, &asn_DEF_T1, tp); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 115 | assert(ret == 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | fprintf(stderr, "=== EOF ===\n"); |
Vasil Velichkov | cef21e0 | 2017-10-09 23:40:17 +0300 | [diff] [blame^] | 117 | |
| 118 | ASN_STRUCT_RESET(asn_DEF_T1, tp); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 122 | try_corrupt(uint8_t *buf, size_t size) { |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 123 | uint8_t tmp[size]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 124 | |
| 125 | fprintf(stderr, "\nCorrupting...\n"); |
| 126 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 127 | for(int i = 0; i < 1000; i++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | int loc; |
| 129 | memcpy(tmp, buf, size); |
| 130 | |
| 131 | /* Corrupt random _non-value_ location. */ |
| 132 | do { loc = random() % size; } while(tmp[loc] >= 70); |
| 133 | do { tmp[loc] ^= random(); } while(tmp[loc] == buf[loc]); |
| 134 | |
| 135 | fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n", |
| 136 | i, loc, buf[loc], tmp[loc]); |
| 137 | |
| 138 | check(0, tmp, size, size); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | int |
| 143 | main(int ac, char **av) { |
| 144 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 145 | (void)ac; /* Unused argument */ |
| 146 | (void)av; /* Unused argument */ |
| 147 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 148 | check(1, buf1, sizeof(buf1), sizeof(buf1)); |
| 149 | try_corrupt(buf1, sizeof(buf1)); |
| 150 | check(1, buf1, sizeof(buf1) + 10, sizeof(buf1)); |
| 151 | |
| 152 | return 0; |
| 153 | } |