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