1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #include <asn_internal.h>
26 #define ADVANCE(num_bytes) do { \
27 size_t num = num_bytes; \
28 ptr = ((const char *)ptr) + num; \
30 consumed_myself += num; \
33 #define RETURN(_code) do { \
34 asn_dec_rval_t rval; \
36 if(opt_ctx) opt_ctx->step = step; /* Save context */ \
37 if(_code == RC_OK || opt_ctx) \
38 rval.consumed = consumed_myself; \
40 rval.consumed = 0; /* Context-free */ \
45 * The BER decoder of any type.
48 ber_decode(const asn_codec_ctx_t *opt_codec_ctx,
49 const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,
50 const void *ptr, size_t size) {
51 asn_codec_ctx_t s_codec_ctx;
54 * Stack checker requires that the codec context
55 * must be allocated on the stack.
58 if(opt_codec_ctx->max_stack_size) {
59 s_codec_ctx = *opt_codec_ctx;
60 opt_codec_ctx = &s_codec_ctx;
63 /* If context is not given, be security-conscious anyway */
64 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
65 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
66 opt_codec_ctx = &s_codec_ctx;
70 * Invoke type-specific decoder.
72 return type_descriptor->op->ber_decoder(opt_codec_ctx, type_descriptor,
73 struct_ptr, /* Pointer to the destination structure */
74 ptr, size, /* Buffer and its size */
75 0 /* Default tag mode is 0 */
80 * Check the set of <TL<TL<TL...>>> tags matches the definition.
83 ber_check_tags(const asn_codec_ctx_t *opt_codec_ctx,
84 const asn_TYPE_descriptor_t *td, asn_struct_ctx_t *opt_ctx,
85 const void *ptr, size_t size, int tag_mode, int last_tag_form,
86 ber_tlv_len_t *last_length, int *opt_tlv_form) {
87 ssize_t consumed_myself = 0;
90 ber_tlv_tag_t tlv_tag;
91 ber_tlv_len_t tlv_len;
92 ber_tlv_len_t limit_len = -1;
93 int expect_00_terminators = 0;
94 int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */
95 int step = opt_ctx ? opt_ctx->step : 0; /* Where we left previously */
99 * Make sure we didn't exceed the maximum stack size.
101 if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
105 * So what does all this implicit skip stuff mean?
107 * A ::= [5] IMPLICIT T
108 * B ::= [2] EXPLICIT T
109 * Where T is defined as
110 * T ::= [4] IMPLICIT SEQUENCE { ... }
112 * Let's say, we are starting to decode type A, given the
113 * following TLV stream: <5> <0>. What does this mean?
114 * It means that the type A contains type T which is,
116 * Remember though, that we are still in A. We cannot
117 * just pass control to the type T decoder. Why? Because
118 * the type T decoder expects <4> <0>, not <5> <0>.
119 * So, we must make sure we are going to receive <5> while
120 * still in A, then pass control to the T decoder, indicating
121 * that the tag <4> was implicitly skipped. The decoder of T
122 * hence will be prepared to treat <4> as valid tag, and decode
126 tagno = step /* Continuing where left previously */
129 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
130 td->name, (long)size, tag_mode, step, tagno);
131 /* assert(td->tags_count >= 1) May not be the case for CHOICE or ANY */
133 if(tag_mode == 0 && tagno == (int)td->tags_count) {
135 * This must be the _untagged_ ANY type,
136 * which outermost tag isn't known in advance.
137 * Fetch the tag and length separately.
139 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
141 case -1: RETURN(RC_FAIL);
142 case 0: RETURN(RC_WMORE);
144 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
145 len_len = ber_fetch_length(tlv_constr,
146 (const char *)ptr + tag_len, size - tag_len, &tlv_len);
148 case -1: RETURN(RC_FAIL);
149 case 0: RETURN(RC_WMORE);
151 ASN_DEBUG("Advancing %ld in ANY case",
152 (long)(tag_len + len_len));
153 ADVANCE(tag_len + len_len);
155 assert(tagno < (int)td->tags_count); /* At least one loop */
157 for((void)tagno; tagno < (int)td->tags_count; tagno++, step++) {
160 * Fetch and process T from TLV.
162 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
163 ASN_DEBUG("Fetching tag from {%p,%ld}: "
164 "len %ld, step %d, tagno %d got %s",
166 (long)tag_len, step, tagno,
167 ber_tlv_tag_string(tlv_tag));
169 case -1: RETURN(RC_FAIL);
170 case 0: RETURN(RC_WMORE);
173 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
176 * If {I}, don't check anything.
177 * If {I,B,C}, check B and C unless we're at I.
179 if(tag_mode != 0 && step == 0) {
181 * We don't expect tag to match here.
182 * It's just because we don't know how the tag
183 * is supposed to look like.
186 assert(tagno >= 0); /* Guaranteed by the code above */
187 if(tlv_tag != td->tags[tagno]) {
189 * Unexpected tag. Too bad.
191 ASN_DEBUG("Expected: %s, "
192 "expectation failed (tn=%d, tm=%d)",
193 ber_tlv_tag_string(td->tags[tagno]),
201 * Attention: if there are more tags expected,
202 * ensure that the current tag is presented
203 * in constructed form (it contains other tags!).
204 * If this one is the last one, check that the tag form
205 * matches the one given in descriptor.
207 if(tagno < ((int)td->tags_count - 1)) {
208 if(tlv_constr == 0) {
209 ASN_DEBUG("tlv_constr = %d, expfail",
214 if(last_tag_form != tlv_constr
215 && last_tag_form != -1) {
216 ASN_DEBUG("last_tag_form %d != %d",
217 last_tag_form, tlv_constr);
223 * Fetch and process L from TLV.
225 len_len = ber_fetch_length(tlv_constr,
226 (const char *)ptr + tag_len, size - tag_len, &tlv_len);
227 ASN_DEBUG("Fetching len = %ld", (long)len_len);
229 case -1: RETURN(RC_FAIL);
230 case 0: RETURN(RC_WMORE);
235 * As of today, the chain of tags
236 * must either contain several indefinite length TLVs,
237 * or several definite length ones.
238 * No mixing is allowed.
244 if(limit_len == -1) {
245 expect_00_terminators++;
247 ASN_DEBUG("Unexpected indefinite length "
248 "in a chain of definite lengths");
251 ADVANCE(tag_len + len_len);
254 if(expect_00_terminators) {
255 ASN_DEBUG("Unexpected definite length "
256 "in a chain of indefinite lengths");
262 * Check that multiple TLVs specify ever decreasing length,
263 * which is consistent.
265 if(limit_len == -1) {
266 limit_len = tlv_len + tag_len + len_len;
268 /* Too great tlv_len value? */
271 } else if(limit_len != tlv_len + tag_len + len_len) {
273 * Inner TLV specifies length which is inconsistent
274 * with the outer TLV's length value.
276 ASN_DEBUG("Outer TLV is %ld and inner is %ld",
277 (long)limit_len, (long)tlv_len);
281 ADVANCE(tag_len + len_len);
283 limit_len -= (tag_len + len_len);
284 if((ssize_t)size > limit_len) {
286 * Make sure that we won't consume more bytes
287 * from the parent frame than the inferred limit.
294 *opt_tlv_form = tlv_constr;
295 if(expect_00_terminators)
296 *last_length = -expect_00_terminators;
298 *last_length = tlv_len;