Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <constr_TYPE.h> |
| 7 | #include <assert.h> |
| 8 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame^] | 9 | #undef ADVANCE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | #define ADVANCE(num_bytes) do { \ |
| 11 | size_t num = num_bytes; \ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 12 | ptr = ((char *)ptr) + num; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | size -= num; \ |
| 14 | consumed_myself += num; \ |
| 15 | } while(0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame^] | 16 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | #define RETURN(_code) do { \ |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame^] | 18 | fprintf(stderr, "-----%d", __LINE__); \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | ber_dec_rval_t rval; \ |
| 20 | rval.code = _code; \ |
| 21 | rval.consumed = consumed_myself; \ |
| 22 | return rval; \ |
| 23 | } while(0) |
| 24 | |
| 25 | /* |
| 26 | * The BER decoder of any type. |
| 27 | */ |
| 28 | ber_dec_rval_t |
| 29 | ber_decode(asn1_TYPE_descriptor_t *type_descriptor, |
| 30 | void **struct_ptr, void *ptr, size_t size) { |
| 31 | |
| 32 | /* |
| 33 | * Invoke type-specific decoder. |
| 34 | */ |
| 35 | return type_descriptor->ber_decoder(type_descriptor, |
| 36 | struct_ptr, /* Pointer to the destination structure */ |
| 37 | ptr, size, /* Buffer and its size */ |
| 38 | 0 /* Default tag mode is 0 */ |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Check the set of <TL<TL<TL...>>> tags matches the definition. |
| 44 | */ |
| 45 | ber_dec_rval_t |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 46 | ber_check_tags(asn1_TYPE_descriptor_t *td, ber_dec_ctx_t *ctx, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | void *ptr, size_t size, int tag_mode, |
| 48 | ber_tlv_len_t *last_length, int *opt_tlv_form) { |
| 49 | ssize_t consumed_myself = 0; |
| 50 | ssize_t tag_len; |
| 51 | ssize_t len_len; |
| 52 | ber_tlv_tag_t tlv_tag; |
| 53 | ber_tlv_len_t tlv_len; |
| 54 | ber_tlv_len_t limit_len = -1; |
| 55 | int expect_00_terminators = 0; |
| 56 | int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */ |
| 57 | int tagno; |
| 58 | |
| 59 | /* |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 60 | * So what does all this implicit skip stuff mean? |
Lev Walkin | 26e2222 | 2004-06-06 07:59:35 +0000 | [diff] [blame] | 61 | * Imagine two types, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | * A ::= [5] IMPLICIT T |
| 63 | * B ::= [2] EXPLICIT T |
| 64 | * Where T is defined as |
| 65 | * T ::= [4] IMPLICIT SEQUENCE { ... } |
| 66 | * |
| 67 | * Let's say, we are starting to decode type A, given the |
| 68 | * following TLV stream: <5> <0>. What does this mean? |
| 69 | * It means that the type A contains type T which is, |
| 70 | * in turn, empty. |
| 71 | * Remember though, that we are still in A. We cannot |
| 72 | * just pass control to the type T decoder. Why? Because |
| 73 | * the type T decoder expects <4> <0>, not <5> <0>. |
| 74 | * So, we must make sure we are going to receive <5> while |
| 75 | * still in A, then pass control to the T decoder, indicating |
| 76 | * that the tag <4> was implicitly skipped. The decoder of T |
| 77 | * hence will be prepared to treat <4> as valid tag, and decode |
| 78 | * it appropriately. |
| 79 | */ |
| 80 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | tagno = ctx->step /* Continuing where left previously */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | + (tag_mode==1?-1:0) |
| 83 | ; |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 84 | ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)", |
| 85 | td->name, (long)size, tag_mode, ctx->step, tagno); |
Lev Walkin | 7210fdb | 2004-09-04 04:44:30 +0000 | [diff] [blame] | 86 | //assert(td->tags_count >= 1); ?May not be the case for CHOICE or ANY. |
| 87 | |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 88 | if(tag_mode == 0 && tagno == td->tags_count) { |
Lev Walkin | 7210fdb | 2004-09-04 04:44:30 +0000 | [diff] [blame] | 89 | /* |
| 90 | * This must be the _untagged_ ANY type, |
| 91 | * which outermost tag isn't known in advance. |
| 92 | * Fetch the tag and length separately. |
| 93 | */ |
| 94 | tag_len = ber_fetch_tag(ptr, size, &tlv_tag); |
| 95 | switch(tag_len) { |
| 96 | case -1: RETURN(RC_FAIL); |
| 97 | case 0: RETURN(RC_WMORE); |
| 98 | } |
| 99 | tlv_constr = BER_TLV_CONSTRUCTED(ptr); |
| 100 | len_len = ber_fetch_length(tlv_constr, |
| 101 | (char *)ptr + tag_len, size - tag_len, &tlv_len); |
| 102 | switch(len_len) { |
| 103 | case -1: RETURN(RC_FAIL); |
| 104 | case 0: RETURN(RC_WMORE); |
| 105 | } |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame^] | 106 | ADVANCE(tag_len + len_len); |
Lev Walkin | 7210fdb | 2004-09-04 04:44:30 +0000 | [diff] [blame] | 107 | } else { |
| 108 | assert(tagno < td->tags_count); /* At least one loop */ |
| 109 | } |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 110 | for((void)tagno; tagno < td->tags_count; tagno++, ctx->step++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Fetch and process T from TLV. |
| 114 | */ |
| 115 | tag_len = ber_fetch_tag(ptr, size, &tlv_tag); |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 116 | ASN_DEBUG("Fetching tag from {%p,%ld} %02X..%02X: " |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | "len %ld, tag %s", |
| 118 | ptr, (long)size, |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 119 | size?*(uint8_t *)ptr:0, |
Lev Walkin | 9e12f2e | 2004-07-21 04:03:14 +0000 | [diff] [blame] | 120 | ((size_t)tag_len<size&&tag_len>0) |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 121 | ?*((uint8_t *)ptr + tag_len):0, |
| 122 | (long)tag_len, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | ber_tlv_tag_string(tlv_tag)); |
| 124 | switch(tag_len) { |
| 125 | case -1: RETURN(RC_FAIL); |
| 126 | case 0: RETURN(RC_WMORE); |
| 127 | } |
| 128 | |
| 129 | tlv_constr = BER_TLV_CONSTRUCTED(ptr); |
| 130 | |
| 131 | /* |
| 132 | * If {I}, don't check anything. |
| 133 | * If {I,B,C}, check B and C unless we're at I. |
| 134 | */ |
| 135 | if(tag_mode != 0 && ctx->step == 0) { |
| 136 | /* |
| 137 | * We don't expect tag to match here. |
| 138 | * It's just because we don't know how the tag |
| 139 | * is supposed to look like. |
| 140 | */ |
| 141 | } else { |
| 142 | assert(tagno >= 0); /* Guaranteed by the code above */ |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 143 | if(tlv_tag != td->tags[tagno]) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Unexpected tag. Too bad. |
| 146 | */ |
| 147 | ASN_DEBUG("Expected: %s, expectation failed", |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 148 | ber_tlv_tag_string(td->tags[tagno])); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | RETURN(RC_FAIL); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Attention: if there are more tags expected, |
| 155 | * ensure that the current tag is presented |
| 156 | * in constructed form (it contains other tags!). |
| 157 | * If this one is the last one, check that the tag form |
| 158 | * matches the one given in descriptor. |
| 159 | */ |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 160 | if(tagno < (td->tags_count - 1)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | if(tlv_constr == 0) { |
| 162 | RETURN(RC_FAIL); |
| 163 | } |
| 164 | } else { |
Lev Walkin | f7a6c6d | 2004-07-21 03:55:44 +0000 | [diff] [blame] | 165 | if(td->last_tag_form != tlv_constr |
| 166 | && td->last_tag_form != -1) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 167 | RETURN(RC_FAIL); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Fetch and process L from TLV. |
| 173 | */ |
| 174 | len_len = ber_fetch_length(tlv_constr, |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 175 | (char *)ptr + tag_len, size - tag_len, &tlv_len); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | switch(len_len) { |
| 177 | case -1: RETURN(RC_FAIL); |
| 178 | case 0: RETURN(RC_WMORE); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * FIXME |
| 183 | * As of today, the chain of tags |
| 184 | * must either contain several indefinite length TLVs, |
| 185 | * or several definite length ones. |
| 186 | * No mixing is allowed. |
| 187 | */ |
| 188 | if(tlv_len == -1) { |
| 189 | /* |
| 190 | * Indefinite length. |
| 191 | */ |
| 192 | if(limit_len == -1) { |
| 193 | expect_00_terminators++; |
| 194 | } else { |
| 195 | ASN_DEBUG("Unexpected indefinite length " |
| 196 | "in a chain of definite lengths"); |
| 197 | RETURN(RC_FAIL); |
| 198 | } |
| 199 | ADVANCE(tag_len + len_len); |
| 200 | continue; |
| 201 | } else { |
| 202 | if(expect_00_terminators) { |
| 203 | ASN_DEBUG("Unexpected definite length " |
| 204 | "in a chain of indefinite lengths"); |
| 205 | RETURN(RC_FAIL); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Check that multiple TLVs specify ever decreasing length, |
| 211 | * which is consistent. |
| 212 | */ |
| 213 | if(limit_len == -1) { |
| 214 | limit_len = tlv_len + tag_len + len_len; |
| 215 | } else if(limit_len != tlv_len + tag_len + len_len) { |
| 216 | /* |
| 217 | * Inner TLV specifies length which is inconsistent |
| 218 | * with the outer TLV's length value. |
| 219 | */ |
| 220 | ASN_DEBUG("Outer TLV is %d and inner is %d", |
| 221 | limit_len, tlv_len); |
| 222 | RETURN(RC_FAIL); |
| 223 | } |
| 224 | |
| 225 | ADVANCE(tag_len + len_len); |
| 226 | |
| 227 | limit_len -= (tag_len + len_len); |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 228 | if((ssize_t)size > limit_len) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 229 | /* |
| 230 | * Make sure that we won't consume more bytes |
| 231 | * from the large buffer than the inferred limit. |
| 232 | */ |
| 233 | size = limit_len; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if(opt_tlv_form) |
| 238 | *opt_tlv_form = tlv_constr; |
| 239 | if(expect_00_terminators) |
| 240 | *last_length = -expect_00_terminators; |
| 241 | else |
| 242 | *last_length = tlv_len; |
| 243 | |
| 244 | RETURN(RC_OK); |
| 245 | } |