blob: b3a6329e063f67c9855ffaf3e6a48d2292ec637d [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin99414dc2004-09-29 13:21:23 +00002 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006
Lev Walkincc6a9102004-09-23 22:06:26 +00007#undef ADVANCE
Lev Walkin5ccf1eb2004-09-24 20:58:47 +00008#define ADVANCE(num_bytes) do { \
9 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +000010 ptr = ((const char *)ptr) + num; \
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000011 size -= num; \
12 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000013 } while(0)
Lev Walkincc6a9102004-09-23 22:06:26 +000014#undef RETURN
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000015#define RETURN(_code) do { \
Lev Walkindc06f6b2004-10-20 15:50:55 +000016 asn_dec_rval_t rval; \
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000017 rval.code = _code; \
18 if(opt_ctx) opt_ctx->step = step; /* Save context */ \
19 if(_code == RC_OK || opt_ctx) \
20 rval.consumed = consumed_myself; \
21 else \
22 rval.consumed = 0; /* Context-free */ \
23 return rval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000024 } while(0)
25
26/*
27 * The BER decoder of any type.
28 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000029asn_dec_rval_t
Lev Walkin99414dc2004-09-29 13:21:23 +000030ber_decode(asn_codec_ctx_t *opt_codec_ctx,
31 asn_TYPE_descriptor_t *type_descriptor,
Lev Walkin8c3b8542005-03-10 18:52:02 +000032 void **struct_ptr, const void *ptr, size_t size) {
Lev Walkin99414dc2004-09-29 13:21:23 +000033 asn_codec_ctx_t s_codec_ctx;
34
35 /*
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000036 * Stack checker requires that the codec context
Lev Walkin99414dc2004-09-29 13:21:23 +000037 * must be allocated on the stack.
38 */
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000039 if(opt_codec_ctx) {
40 if(opt_codec_ctx->max_stack_size) {
41 s_codec_ctx = *opt_codec_ctx;
42 opt_codec_ctx = &s_codec_ctx;
43 }
44 } else {
45 /* If context is not given, be security-conscious anyway */
46 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
Lev Walkin7c1dc052016-03-14 03:08:15 -070047 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
Lev Walkin99414dc2004-09-29 13:21:23 +000048 opt_codec_ctx = &s_codec_ctx;
49 }
Lev Walkinf15320b2004-06-03 03:38:44 +000050
51 /*
52 * Invoke type-specific decoder.
53 */
Lev Walkin99414dc2004-09-29 13:21:23 +000054 return type_descriptor->ber_decoder(opt_codec_ctx, type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000055 struct_ptr, /* Pointer to the destination structure */
56 ptr, size, /* Buffer and its size */
57 0 /* Default tag mode is 0 */
58 );
59}
60
61/*
62 * Check the set of <TL<TL<TL...>>> tags matches the definition.
63 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000064asn_dec_rval_t
Lev Walkin99414dc2004-09-29 13:21:23 +000065ber_check_tags(asn_codec_ctx_t *opt_codec_ctx,
66 asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,
Lev Walkin8c3b8542005-03-10 18:52:02 +000067 const void *ptr, size_t size, int tag_mode, int last_tag_form,
Lev Walkinf15320b2004-06-03 03:38:44 +000068 ber_tlv_len_t *last_length, int *opt_tlv_form) {
69 ssize_t consumed_myself = 0;
70 ssize_t tag_len;
71 ssize_t len_len;
72 ber_tlv_tag_t tlv_tag;
73 ber_tlv_len_t tlv_len;
74 ber_tlv_len_t limit_len = -1;
75 int expect_00_terminators = 0;
76 int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000077 int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */
Lev Walkinf15320b2004-06-03 03:38:44 +000078 int tagno;
79
Lev Walkinbaaa24f2004-09-29 14:19:14 +000080 /*
81 * Make sure we didn't exceed the maximum stack size.
82 */
Lev Walkin7c1dc052016-03-14 03:08:15 -070083 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000084 RETURN(RC_FAIL);
Lev Walkin99414dc2004-09-29 13:21:23 +000085
Lev Walkinf15320b2004-06-03 03:38:44 +000086 /*
Lev Walkin906654e2004-09-10 15:49:15 +000087 * So what does all this implicit skip stuff mean?
Lev Walkin26e22222004-06-06 07:59:35 +000088 * Imagine two types,
Lev Walkinf15320b2004-06-03 03:38:44 +000089 * A ::= [5] IMPLICIT T
90 * B ::= [2] EXPLICIT T
91 * Where T is defined as
92 * T ::= [4] IMPLICIT SEQUENCE { ... }
93 *
94 * Let's say, we are starting to decode type A, given the
95 * following TLV stream: <5> <0>. What does this mean?
96 * It means that the type A contains type T which is,
97 * in turn, empty.
98 * Remember though, that we are still in A. We cannot
99 * just pass control to the type T decoder. Why? Because
100 * the type T decoder expects <4> <0>, not <5> <0>.
101 * So, we must make sure we are going to receive <5> while
102 * still in A, then pass control to the T decoder, indicating
103 * that the tag <4> was implicitly skipped. The decoder of T
104 * hence will be prepared to treat <4> as valid tag, and decode
105 * it appropriately.
106 */
107
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000108 tagno = step /* Continuing where left previously */
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 + (tag_mode==1?-1:0)
110 ;
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000111 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000112 td->name, (long)size, tag_mode, step, tagno);
Lev Walkin99414dc2004-09-29 13:21:23 +0000113 /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */
Lev Walkin7210fdb2004-09-04 04:44:30 +0000114
Lev Walkin906654e2004-09-10 15:49:15 +0000115 if(tag_mode == 0 && tagno == td->tags_count) {
Lev Walkin7210fdb2004-09-04 04:44:30 +0000116 /*
117 * This must be the _untagged_ ANY type,
118 * which outermost tag isn't known in advance.
119 * Fetch the tag and length separately.
120 */
121 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
122 switch(tag_len) {
123 case -1: RETURN(RC_FAIL);
124 case 0: RETURN(RC_WMORE);
125 }
126 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
127 len_len = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000128 (const char *)ptr + tag_len, size - tag_len, &tlv_len);
Lev Walkin7210fdb2004-09-04 04:44:30 +0000129 switch(len_len) {
130 case -1: RETURN(RC_FAIL);
131 case 0: RETURN(RC_WMORE);
132 }
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000133 ASN_DEBUG("Advancing %ld in ANY case",
134 (long)(tag_len + len_len));
Lev Walkincc6a9102004-09-23 22:06:26 +0000135 ADVANCE(tag_len + len_len);
Lev Walkin7210fdb2004-09-04 04:44:30 +0000136 } else {
137 assert(tagno < td->tags_count); /* At least one loop */
138 }
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000139 for((void)tagno; tagno < td->tags_count; tagno++, step++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000140
141 /*
142 * Fetch and process T from TLV.
143 */
144 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
Lev Walkin00d36022004-10-05 06:35:43 +0000145 ASN_DEBUG("Fetching tag from {%p,%ld}: "
146 "len %ld, step %d, tagno %d got %s",
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 ptr, (long)size,
Lev Walkin00d36022004-10-05 06:35:43 +0000148 (long)tag_len, step, tagno,
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 ber_tlv_tag_string(tlv_tag));
150 switch(tag_len) {
151 case -1: RETURN(RC_FAIL);
152 case 0: RETURN(RC_WMORE);
153 }
154
155 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
156
157 /*
158 * If {I}, don't check anything.
159 * If {I,B,C}, check B and C unless we're at I.
160 */
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000161 if(tag_mode != 0 && step == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 /*
163 * We don't expect tag to match here.
164 * It's just because we don't know how the tag
165 * is supposed to look like.
166 */
167 } else {
168 assert(tagno >= 0); /* Guaranteed by the code above */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000169 if(tlv_tag != td->tags[tagno]) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 /*
171 * Unexpected tag. Too bad.
172 */
Lev Walkin302f9fd2004-10-28 13:02:29 +0000173 ASN_DEBUG("Expected: %s, "
174 "expectation failed (tn=%d, tm=%d)",
175 ber_tlv_tag_string(td->tags[tagno]),
176 tagno, tag_mode
177 );
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 RETURN(RC_FAIL);
179 }
180 }
181
182 /*
183 * Attention: if there are more tags expected,
184 * ensure that the current tag is presented
185 * in constructed form (it contains other tags!).
186 * If this one is the last one, check that the tag form
187 * matches the one given in descriptor.
188 */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000189 if(tagno < (td->tags_count - 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 if(tlv_constr == 0) {
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000191 ASN_DEBUG("tlv_constr = %d, expfail",
192 tlv_constr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 RETURN(RC_FAIL);
194 }
195 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000196 if(last_tag_form != tlv_constr
197 && last_tag_form != -1) {
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000198 ASN_DEBUG("last_tag_form %d != %d",
Lev Walkin8e8078a2004-09-26 13:10:40 +0000199 last_tag_form, tlv_constr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 RETURN(RC_FAIL);
201 }
202 }
203
204 /*
205 * Fetch and process L from TLV.
206 */
207 len_len = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000208 (const char *)ptr + tag_len, size - tag_len, &tlv_len);
Lev Walkin85325922013-12-07 10:52:28 -0800209 ASN_DEBUG("Fetching len = %ld", (long)len_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 switch(len_len) {
211 case -1: RETURN(RC_FAIL);
212 case 0: RETURN(RC_WMORE);
213 }
214
215 /*
216 * FIXME
217 * As of today, the chain of tags
218 * must either contain several indefinite length TLVs,
219 * or several definite length ones.
220 * No mixing is allowed.
221 */
222 if(tlv_len == -1) {
223 /*
224 * Indefinite length.
225 */
226 if(limit_len == -1) {
227 expect_00_terminators++;
228 } else {
229 ASN_DEBUG("Unexpected indefinite length "
230 "in a chain of definite lengths");
231 RETURN(RC_FAIL);
232 }
233 ADVANCE(tag_len + len_len);
234 continue;
235 } else {
236 if(expect_00_terminators) {
237 ASN_DEBUG("Unexpected definite length "
238 "in a chain of indefinite lengths");
239 RETURN(RC_FAIL);
240 }
241 }
242
243 /*
244 * Check that multiple TLVs specify ever decreasing length,
245 * which is consistent.
246 */
247 if(limit_len == -1) {
248 limit_len = tlv_len + tag_len + len_len;
Lev Walkin99414dc2004-09-29 13:21:23 +0000249 if(limit_len < 0) {
250 /* Too great tlv_len value? */
251 RETURN(RC_FAIL);
252 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 } else if(limit_len != tlv_len + tag_len + len_len) {
254 /*
255 * Inner TLV specifies length which is inconsistent
256 * with the outer TLV's length value.
257 */
Lev Walkin33700162004-10-26 09:03:31 +0000258 ASN_DEBUG("Outer TLV is %ld and inner is %ld",
259 (long)limit_len, (long)tlv_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000260 RETURN(RC_FAIL);
261 }
262
263 ADVANCE(tag_len + len_len);
264
265 limit_len -= (tag_len + len_len);
Lev Walkind9bd7752004-06-05 08:17:50 +0000266 if((ssize_t)size > limit_len) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000267 /*
268 * Make sure that we won't consume more bytes
Lev Walkin5ccf1eb2004-09-24 20:58:47 +0000269 * from the parent frame than the inferred limit.
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 */
271 size = limit_len;
272 }
273 }
274
275 if(opt_tlv_form)
276 *opt_tlv_form = tlv_constr;
277 if(expect_00_terminators)
278 *last_length = -expect_00_terminators;
279 else
280 *last_length = tlv_len;
281
282 RETURN(RC_OK);
283}