blob: 24a47ac9f4a547d4d010da05a77507e9c903ed6a [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; \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000010 ptr = ((char *)ptr) + num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000011 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 /*
Lev Walkin906654e2004-09-10 15:49:15 +000056 * So what does all this implicit 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
Lev Walkinf15320b2004-06-03 03:38:44 +000077 tagno = ctx->step /* Continuing where left previously */
Lev Walkinf15320b2004-06-03 03:38:44 +000078 + (tag_mode==1?-1:0)
79 ;
Lev Walkinf7a6c6d2004-07-21 03:55:44 +000080 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
81 td->name, (long)size, tag_mode, ctx->step, tagno);
Lev Walkin7210fdb2004-09-04 04:44:30 +000082 //assert(td->tags_count >= 1); ?May not be the case for CHOICE or ANY.
83
Lev Walkin906654e2004-09-10 15:49:15 +000084 if(tag_mode == 0 && tagno == td->tags_count) {
Lev Walkin7210fdb2004-09-04 04:44:30 +000085 /*
86 * This must be the _untagged_ ANY type,
87 * which outermost tag isn't known in advance.
88 * Fetch the tag and length separately.
89 */
90 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
91 switch(tag_len) {
92 case -1: RETURN(RC_FAIL);
93 case 0: RETURN(RC_WMORE);
94 }
95 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
96 len_len = ber_fetch_length(tlv_constr,
97 (char *)ptr + tag_len, size - tag_len, &tlv_len);
98 switch(len_len) {
99 case -1: RETURN(RC_FAIL);
100 case 0: RETURN(RC_WMORE);
101 }
102 } else {
103 assert(tagno < td->tags_count); /* At least one loop */
104 }
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000105 for((void)tagno; tagno < td->tags_count; tagno++, ctx->step++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000106
107 /*
108 * Fetch and process T from TLV.
109 */
110 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000111 ASN_DEBUG("Fetching tag from {%p,%ld} %02X..%02X: "
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 "len %ld, tag %s",
113 ptr, (long)size,
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000114 size?*(uint8_t *)ptr:0,
Lev Walkin9e12f2e2004-07-21 04:03:14 +0000115 ((size_t)tag_len<size&&tag_len>0)
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000116 ?*((uint8_t *)ptr + tag_len):0,
117 (long)tag_len,
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 ber_tlv_tag_string(tlv_tag));
119 switch(tag_len) {
120 case -1: RETURN(RC_FAIL);
121 case 0: RETURN(RC_WMORE);
122 }
123
124 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
125
126 /*
127 * If {I}, don't check anything.
128 * If {I,B,C}, check B and C unless we're at I.
129 */
130 if(tag_mode != 0 && ctx->step == 0) {
131 /*
132 * We don't expect tag to match here.
133 * It's just because we don't know how the tag
134 * is supposed to look like.
135 */
136 } else {
137 assert(tagno >= 0); /* Guaranteed by the code above */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000138 if(tlv_tag != td->tags[tagno]) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000139 /*
140 * Unexpected tag. Too bad.
141 */
142 ASN_DEBUG("Expected: %s, expectation failed",
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000143 ber_tlv_tag_string(td->tags[tagno]));
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 RETURN(RC_FAIL);
145 }
146 }
147
148 /*
149 * Attention: if there are more tags expected,
150 * ensure that the current tag is presented
151 * in constructed form (it contains other tags!).
152 * If this one is the last one, check that the tag form
153 * matches the one given in descriptor.
154 */
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000155 if(tagno < (td->tags_count - 1)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 if(tlv_constr == 0) {
157 RETURN(RC_FAIL);
158 }
159 } else {
Lev Walkinf7a6c6d2004-07-21 03:55:44 +0000160 if(td->last_tag_form != tlv_constr
161 && td->last_tag_form != -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 RETURN(RC_FAIL);
163 }
164 }
165
166 /*
167 * Fetch and process L from TLV.
168 */
169 len_len = ber_fetch_length(tlv_constr,
Lev Walkin4d9528c2004-08-11 08:10:13 +0000170 (char *)ptr + tag_len, size - tag_len, &tlv_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 switch(len_len) {
172 case -1: RETURN(RC_FAIL);
173 case 0: RETURN(RC_WMORE);
174 }
175
176 /*
177 * FIXME
178 * As of today, the chain of tags
179 * must either contain several indefinite length TLVs,
180 * or several definite length ones.
181 * No mixing is allowed.
182 */
183 if(tlv_len == -1) {
184 /*
185 * Indefinite length.
186 */
187 if(limit_len == -1) {
188 expect_00_terminators++;
189 } else {
190 ASN_DEBUG("Unexpected indefinite length "
191 "in a chain of definite lengths");
192 RETURN(RC_FAIL);
193 }
194 ADVANCE(tag_len + len_len);
195 continue;
196 } else {
197 if(expect_00_terminators) {
198 ASN_DEBUG("Unexpected definite length "
199 "in a chain of indefinite lengths");
200 RETURN(RC_FAIL);
201 }
202 }
203
204 /*
205 * Check that multiple TLVs specify ever decreasing length,
206 * which is consistent.
207 */
208 if(limit_len == -1) {
209 limit_len = tlv_len + tag_len + len_len;
210 } else if(limit_len != tlv_len + tag_len + len_len) {
211 /*
212 * Inner TLV specifies length which is inconsistent
213 * with the outer TLV's length value.
214 */
215 ASN_DEBUG("Outer TLV is %d and inner is %d",
216 limit_len, tlv_len);
217 RETURN(RC_FAIL);
218 }
219
220 ADVANCE(tag_len + len_len);
221
222 limit_len -= (tag_len + len_len);
Lev Walkind9bd7752004-06-05 08:17:50 +0000223 if((ssize_t)size > limit_len) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 /*
225 * Make sure that we won't consume more bytes
226 * from the large buffer than the inferred limit.
227 */
228 size = limit_len;
229 }
230 }
231
232 if(opt_tlv_form)
233 *opt_tlv_form = tlv_constr;
234 if(expect_00_terminators)
235 *last_length = -expect_00_terminators;
236 else
237 *last_length = tlv_len;
238
239 RETURN(RC_OK);
240}