blob: 0d57f82c71dac00c28050f21d3ddd92c5123260a [file] [log] [blame]
Lev Walkinf15320b2004-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 */
5#include <constr_TYPE.h>
6#include <assert.h>
7
8#define ADVANCE(num_bytes) do { \
9 size_t num = num_bytes; \
10 ptr += num; \
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 */
24ber_dec_rval_t
25ber_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 */
41ber_dec_rval_t
Lev Walkinf7a6c6d2004-07-21 03:55:44 +000042ber_check_tags(asn1_TYPE_descriptor_t *td, ber_dec_ctx_t *ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +000043 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?
Lev Walkin26e22222004-06-06 07:59:35 +000057 * Imagine two types,
Lev Walkinf15320b2004-06-03 03:38:44 +000058 * 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
Lev Walkinf7a6c6d2004-07-21 03:55:44 +000086 * by the ASN.1 compiler and placed into td->tags_impl_skip.
Lev Walkinf15320b2004-06-03 03:38:44 +000087 * So let's suppose the only tag left after implicit tagging is {I}.
Lev Walkinf7a6c6d2004-07-21 03:55:44 +000088 * Yet, the table we have is {A,B,C} and td->tags_impl_skip=3.
Lev Walkinf15320b2004-06-03 03:38:44 +000089 * 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 */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +000093 + (tag_mode==-1?(td->tags_impl_skip-1):0)
Lev Walkinf15320b2004-06-03 03:38:44 +000094 + (tag_mode==1?-1:0)
95 ;
Lev Walkinf7a6c6d2004-07-21 03:55:44 +000096 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
97 td->name, (long)size, tag_mode, ctx->step, tagno);
98 //assert(td->tags_count >= 1); ?May not be the case for CHOICE!
99 assert(tagno < td->tags_count); /* At least one loop */
100 for((void)tagno; tagno < td->tags_count; tagno++, ctx->step++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000101
102 /*
103 * Fetch and process T from TLV.
104 */
105 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000106 ASN_DEBUG("Fetching tag from {%p,%ld} %02X..%02X: "
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 "len %ld, tag %s",
108 ptr, (long)size,
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000109 size?*(uint8_t *)ptr:0,
110 (tag_len<size&&tag_len>0)
111 ?*((uint8_t *)ptr + tag_len):0,
112 (long)tag_len,
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 ber_tlv_tag_string(tlv_tag));
114 switch(tag_len) {
115 case -1: RETURN(RC_FAIL);
116 case 0: RETURN(RC_WMORE);
117 }
118
119 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
120
121 /*
122 * If {I}, don't check anything.
123 * If {I,B,C}, check B and C unless we're at I.
124 */
125 if(tag_mode != 0 && ctx->step == 0) {
126 /*
127 * We don't expect tag to match here.
128 * It's just because we don't know how the tag
129 * is supposed to look like.
130 */
131 } else {
132 assert(tagno >= 0); /* Guaranteed by the code above */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000133 if(tlv_tag != td->tags[tagno]) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 /*
135 * Unexpected tag. Too bad.
136 */
137 ASN_DEBUG("Expected: %s, expectation failed",
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000138 ber_tlv_tag_string(td->tags[tagno]));
Lev Walkinf15320b2004-06-03 03:38:44 +0000139 RETURN(RC_FAIL);
140 }
141 }
142
143 /*
144 * Attention: if there are more tags expected,
145 * ensure that the current tag is presented
146 * in constructed form (it contains other tags!).
147 * If this one is the last one, check that the tag form
148 * matches the one given in descriptor.
149 */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000150 if(tagno < (td->tags_count - 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 if(tlv_constr == 0) {
152 RETURN(RC_FAIL);
153 }
154 } else {
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000155 if(td->last_tag_form != tlv_constr
156 && td->last_tag_form != -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 RETURN(RC_FAIL);
158 }
159 }
160
161 /*
162 * Fetch and process L from TLV.
163 */
164 len_len = ber_fetch_length(tlv_constr,
165 ptr + tag_len, size - tag_len, &tlv_len);
166 switch(len_len) {
167 case -1: RETURN(RC_FAIL);
168 case 0: RETURN(RC_WMORE);
169 }
170
171 /*
172 * FIXME
173 * As of today, the chain of tags
174 * must either contain several indefinite length TLVs,
175 * or several definite length ones.
176 * No mixing is allowed.
177 */
178 if(tlv_len == -1) {
179 /*
180 * Indefinite length.
181 */
182 if(limit_len == -1) {
183 expect_00_terminators++;
184 } else {
185 ASN_DEBUG("Unexpected indefinite length "
186 "in a chain of definite lengths");
187 RETURN(RC_FAIL);
188 }
189 ADVANCE(tag_len + len_len);
190 continue;
191 } else {
192 if(expect_00_terminators) {
193 ASN_DEBUG("Unexpected definite length "
194 "in a chain of indefinite lengths");
195 RETURN(RC_FAIL);
196 }
197 }
198
199 /*
200 * Check that multiple TLVs specify ever decreasing length,
201 * which is consistent.
202 */
203 if(limit_len == -1) {
204 limit_len = tlv_len + tag_len + len_len;
205 } else if(limit_len != tlv_len + tag_len + len_len) {
206 /*
207 * Inner TLV specifies length which is inconsistent
208 * with the outer TLV's length value.
209 */
210 ASN_DEBUG("Outer TLV is %d and inner is %d",
211 limit_len, tlv_len);
212 RETURN(RC_FAIL);
213 }
214
215 ADVANCE(tag_len + len_len);
216
217 limit_len -= (tag_len + len_len);
Lev Walkind9bd7752004-06-05 08:17:50 +0000218 if((ssize_t)size > limit_len) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 /*
220 * Make sure that we won't consume more bytes
221 * from the large buffer than the inferred limit.
222 */
223 size = limit_len;
224 }
225 }
226
227 if(opt_tlv_form)
228 *opt_tlv_form = tlv_constr;
229 if(expect_00_terminators)
230 *last_length = -expect_00_terminators;
231 else
232 *last_length = tlv_len;
233
234 RETURN(RC_OK);
235}