blob: 3672b6867fe9111e3e80020ea001e73106de62a5 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <assert.h>
7
vlmef1b4c02004-09-23 22:06:26 +00008#undef ADVANCE
vlm63dd5a12004-09-24 20:58:47 +00009#define ADVANCE(num_bytes) do { \
10 size_t num = num_bytes; \
11 ptr = ((char *)ptr) + num; \
12 size -= num; \
13 consumed_myself += num; \
vlmfa67ddc2004-06-03 03:38:44 +000014 } while(0)
vlmef1b4c02004-09-23 22:06:26 +000015#undef RETURN
vlm63dd5a12004-09-24 20:58:47 +000016#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; \
vlmfa67ddc2004-06-03 03:38:44 +000025 } while(0)
26
27/*
28 * The BER decoder of any type.
29 */
30ber_dec_rval_t
31ber_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 */
47ber_dec_rval_t
vlm63dd5a12004-09-24 20:58:47 +000048ber_check_tags(asn1_TYPE_descriptor_t *td, ber_dec_ctx_t *opt_ctx,
vlmfa67ddc2004-06-03 03:38:44 +000049 void *ptr, size_t size, int tag_mode,
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 */
vlm63dd5a12004-09-24 20:58:47 +000059 int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */
vlmfa67ddc2004-06-03 03:38:44 +000060 int tagno;
61
62 /*
vlm1308d2b2004-09-10 15:49:15 +000063 * So what does all this implicit skip stuff mean?
vlm9bb9a252004-06-06 07:59:35 +000064 * Imagine two types,
vlmfa67ddc2004-06-03 03:38:44 +000065 * 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
vlm63dd5a12004-09-24 20:58:47 +000084 tagno = step /* Continuing where left previously */
vlmfa67ddc2004-06-03 03:38:44 +000085 + (tag_mode==1?-1:0)
86 ;
vlm796c1da2004-07-21 03:55:44 +000087 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
vlm63dd5a12004-09-24 20:58:47 +000088 td->name, (long)size, tag_mode, step, tagno);
vlm463dc5a2004-09-04 04:44:30 +000089 //assert(td->tags_count >= 1); ?May not be the case for CHOICE or ANY.
90
vlm1308d2b2004-09-10 15:49:15 +000091 if(tag_mode == 0 && tagno == td->tags_count) {
vlm463dc5a2004-09-04 04:44:30 +000092 /*
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 }
vlm63dd5a12004-09-24 20:58:47 +0000109 ASN_DEBUG("Advancing %ld in ANY case",
110 (long)(tag_len + len_len));
vlmef1b4c02004-09-23 22:06:26 +0000111 ADVANCE(tag_len + len_len);
vlm463dc5a2004-09-04 04:44:30 +0000112 } else {
113 assert(tagno < td->tags_count); /* At least one loop */
114 }
vlm63dd5a12004-09-24 20:58:47 +0000115 for((void)tagno; tagno < td->tags_count; tagno++, step++) {
vlmfa67ddc2004-06-03 03:38:44 +0000116
117 /*
118 * Fetch and process T from TLV.
119 */
120 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
vlm796c1da2004-07-21 03:55:44 +0000121 ASN_DEBUG("Fetching tag from {%p,%ld} %02X..%02X: "
vlm63dd5a12004-09-24 20:58:47 +0000122 "len %ld, step %d, tag %s",
vlmfa67ddc2004-06-03 03:38:44 +0000123 ptr, (long)size,
vlm796c1da2004-07-21 03:55:44 +0000124 size?*(uint8_t *)ptr:0,
vlm1e9e0b92004-07-21 04:03:14 +0000125 ((size_t)tag_len<size&&tag_len>0)
vlm796c1da2004-07-21 03:55:44 +0000126 ?*((uint8_t *)ptr + tag_len):0,
vlm63dd5a12004-09-24 20:58:47 +0000127 (long)tag_len, step,
vlmfa67ddc2004-06-03 03:38:44 +0000128 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 */
vlm63dd5a12004-09-24 20:58:47 +0000140 if(tag_mode != 0 && step == 0) {
vlmfa67ddc2004-06-03 03:38:44 +0000141 /*
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 */
vlm796c1da2004-07-21 03:55:44 +0000148 if(tlv_tag != td->tags[tagno]) {
vlmfa67ddc2004-06-03 03:38:44 +0000149 /*
150 * Unexpected tag. Too bad.
151 */
152 ASN_DEBUG("Expected: %s, expectation failed",
vlm796c1da2004-07-21 03:55:44 +0000153 ber_tlv_tag_string(td->tags[tagno]));
vlmfa67ddc2004-06-03 03:38:44 +0000154 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 */
vlm796c1da2004-07-21 03:55:44 +0000165 if(tagno < (td->tags_count - 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000166 if(tlv_constr == 0) {
vlm63dd5a12004-09-24 20:58:47 +0000167 ASN_DEBUG("tlv_constr = %d, expfail",
168 tlv_constr);
vlmfa67ddc2004-06-03 03:38:44 +0000169 RETURN(RC_FAIL);
170 }
171 } else {
vlm796c1da2004-07-21 03:55:44 +0000172 if(td->last_tag_form != tlv_constr
173 && td->last_tag_form != -1) {
vlm63dd5a12004-09-24 20:58:47 +0000174 ASN_DEBUG("last_tag_form %d != %d",
175 td->last_tag_form, tlv_constr);
vlmfa67ddc2004-06-03 03:38:44 +0000176 RETURN(RC_FAIL);
177 }
178 }
179
180 /*
181 * Fetch and process L from TLV.
182 */
183 len_len = ber_fetch_length(tlv_constr,
vlm1ff928d2004-08-11 08:10:13 +0000184 (char *)ptr + tag_len, size - tag_len, &tlv_len);
vlm63dd5a12004-09-24 20:58:47 +0000185 ASN_DEBUG("Fetchinig len = %ld", (long)len_len);
vlmfa67ddc2004-06-03 03:38:44 +0000186 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);
vlmb42843a2004-06-05 08:17:50 +0000238 if((ssize_t)size > limit_len) {
vlmfa67ddc2004-06-03 03:38:44 +0000239 /*
240 * Make sure that we won't consume more bytes
vlm63dd5a12004-09-24 20:58:47 +0000241 * from the parent frame than the inferred limit.
vlmfa67ddc2004-06-03 03:38:44 +0000242 */
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}