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