blob: 5ef1bf44b134837bc2d880c55d10363aa05e09c3 [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 <constr_TYPE.h>
7#include <assert.h>
8
vlmef1b4c02004-09-23 22:06:26 +00009#undef ADVANCE
vlmfa67ddc2004-06-03 03:38:44 +000010#define ADVANCE(num_bytes) do { \
11 size_t num = num_bytes; \
vlmd86c9252004-08-25 01:34:11 +000012 ptr = ((char *)ptr) + num; \
vlmfa67ddc2004-06-03 03:38:44 +000013 size -= num; \
14 consumed_myself += num; \
15 } while(0)
vlmef1b4c02004-09-23 22:06:26 +000016#undef RETURN
vlmfa67ddc2004-06-03 03:38:44 +000017#define RETURN(_code) do { \
vlmef1b4c02004-09-23 22:06:26 +000018 fprintf(stderr, "-----%d", __LINE__); \
vlmfa67ddc2004-06-03 03:38:44 +000019 ber_dec_rval_t rval; \
20 rval.code = _code; \
21 rval.consumed = consumed_myself; \
22 return rval; \
23 } while(0)
24
25/*
26 * The BER decoder of any type.
27 */
28ber_dec_rval_t
29ber_decode(asn1_TYPE_descriptor_t *type_descriptor,
30 void **struct_ptr, void *ptr, size_t size) {
31
32 /*
33 * Invoke type-specific decoder.
34 */
35 return type_descriptor->ber_decoder(type_descriptor,
36 struct_ptr, /* Pointer to the destination structure */
37 ptr, size, /* Buffer and its size */
38 0 /* Default tag mode is 0 */
39 );
40}
41
42/*
43 * Check the set of <TL<TL<TL...>>> tags matches the definition.
44 */
45ber_dec_rval_t
vlm796c1da2004-07-21 03:55:44 +000046ber_check_tags(asn1_TYPE_descriptor_t *td, ber_dec_ctx_t *ctx,
vlmfa67ddc2004-06-03 03:38:44 +000047 void *ptr, size_t size, int tag_mode,
48 ber_tlv_len_t *last_length, int *opt_tlv_form) {
49 ssize_t consumed_myself = 0;
50 ssize_t tag_len;
51 ssize_t len_len;
52 ber_tlv_tag_t tlv_tag;
53 ber_tlv_len_t tlv_len;
54 ber_tlv_len_t limit_len = -1;
55 int expect_00_terminators = 0;
56 int tlv_constr = -1; /* If CHOICE, opt_tlv_form is not given */
57 int tagno;
58
59 /*
vlm1308d2b2004-09-10 15:49:15 +000060 * So what does all this implicit skip stuff mean?
vlm9bb9a252004-06-06 07:59:35 +000061 * Imagine two types,
vlmfa67ddc2004-06-03 03:38:44 +000062 * A ::= [5] IMPLICIT T
63 * B ::= [2] EXPLICIT T
64 * Where T is defined as
65 * T ::= [4] IMPLICIT SEQUENCE { ... }
66 *
67 * Let's say, we are starting to decode type A, given the
68 * following TLV stream: <5> <0>. What does this mean?
69 * It means that the type A contains type T which is,
70 * in turn, empty.
71 * Remember though, that we are still in A. We cannot
72 * just pass control to the type T decoder. Why? Because
73 * the type T decoder expects <4> <0>, not <5> <0>.
74 * So, we must make sure we are going to receive <5> while
75 * still in A, then pass control to the T decoder, indicating
76 * that the tag <4> was implicitly skipped. The decoder of T
77 * hence will be prepared to treat <4> as valid tag, and decode
78 * it appropriately.
79 */
80
vlmfa67ddc2004-06-03 03:38:44 +000081 tagno = ctx->step /* Continuing where left previously */
vlmfa67ddc2004-06-03 03:38:44 +000082 + (tag_mode==1?-1:0)
83 ;
vlm796c1da2004-07-21 03:55:44 +000084 ASN_DEBUG("ber_check_tags(%s, size=%ld, tm=%d, step=%d, tagno=%d)",
85 td->name, (long)size, tag_mode, ctx->step, tagno);
vlm463dc5a2004-09-04 04:44:30 +000086 //assert(td->tags_count >= 1); ?May not be the case for CHOICE or ANY.
87
vlm1308d2b2004-09-10 15:49:15 +000088 if(tag_mode == 0 && tagno == td->tags_count) {
vlm463dc5a2004-09-04 04:44:30 +000089 /*
90 * This must be the _untagged_ ANY type,
91 * which outermost tag isn't known in advance.
92 * Fetch the tag and length separately.
93 */
94 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
95 switch(tag_len) {
96 case -1: RETURN(RC_FAIL);
97 case 0: RETURN(RC_WMORE);
98 }
99 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
100 len_len = ber_fetch_length(tlv_constr,
101 (char *)ptr + tag_len, size - tag_len, &tlv_len);
102 switch(len_len) {
103 case -1: RETURN(RC_FAIL);
104 case 0: RETURN(RC_WMORE);
105 }
vlmef1b4c02004-09-23 22:06:26 +0000106 ADVANCE(tag_len + len_len);
vlm463dc5a2004-09-04 04:44:30 +0000107 } else {
108 assert(tagno < td->tags_count); /* At least one loop */
109 }
vlm796c1da2004-07-21 03:55:44 +0000110 for((void)tagno; tagno < td->tags_count; tagno++, ctx->step++) {
vlmfa67ddc2004-06-03 03:38:44 +0000111
112 /*
113 * Fetch and process T from TLV.
114 */
115 tag_len = ber_fetch_tag(ptr, size, &tlv_tag);
vlm796c1da2004-07-21 03:55:44 +0000116 ASN_DEBUG("Fetching tag from {%p,%ld} %02X..%02X: "
vlmfa67ddc2004-06-03 03:38:44 +0000117 "len %ld, tag %s",
118 ptr, (long)size,
vlm796c1da2004-07-21 03:55:44 +0000119 size?*(uint8_t *)ptr:0,
vlm1e9e0b92004-07-21 04:03:14 +0000120 ((size_t)tag_len<size&&tag_len>0)
vlm796c1da2004-07-21 03:55:44 +0000121 ?*((uint8_t *)ptr + tag_len):0,
122 (long)tag_len,
vlmfa67ddc2004-06-03 03:38:44 +0000123 ber_tlv_tag_string(tlv_tag));
124 switch(tag_len) {
125 case -1: RETURN(RC_FAIL);
126 case 0: RETURN(RC_WMORE);
127 }
128
129 tlv_constr = BER_TLV_CONSTRUCTED(ptr);
130
131 /*
132 * If {I}, don't check anything.
133 * If {I,B,C}, check B and C unless we're at I.
134 */
135 if(tag_mode != 0 && ctx->step == 0) {
136 /*
137 * We don't expect tag to match here.
138 * It's just because we don't know how the tag
139 * is supposed to look like.
140 */
141 } else {
142 assert(tagno >= 0); /* Guaranteed by the code above */
vlm796c1da2004-07-21 03:55:44 +0000143 if(tlv_tag != td->tags[tagno]) {
vlmfa67ddc2004-06-03 03:38:44 +0000144 /*
145 * Unexpected tag. Too bad.
146 */
147 ASN_DEBUG("Expected: %s, expectation failed",
vlm796c1da2004-07-21 03:55:44 +0000148 ber_tlv_tag_string(td->tags[tagno]));
vlmfa67ddc2004-06-03 03:38:44 +0000149 RETURN(RC_FAIL);
150 }
151 }
152
153 /*
154 * Attention: if there are more tags expected,
155 * ensure that the current tag is presented
156 * in constructed form (it contains other tags!).
157 * If this one is the last one, check that the tag form
158 * matches the one given in descriptor.
159 */
vlm796c1da2004-07-21 03:55:44 +0000160 if(tagno < (td->tags_count - 1)) {
vlmfa67ddc2004-06-03 03:38:44 +0000161 if(tlv_constr == 0) {
162 RETURN(RC_FAIL);
163 }
164 } else {
vlm796c1da2004-07-21 03:55:44 +0000165 if(td->last_tag_form != tlv_constr
166 && td->last_tag_form != -1) {
vlmfa67ddc2004-06-03 03:38:44 +0000167 RETURN(RC_FAIL);
168 }
169 }
170
171 /*
172 * Fetch and process L from TLV.
173 */
174 len_len = ber_fetch_length(tlv_constr,
vlm1ff928d2004-08-11 08:10:13 +0000175 (char *)ptr + tag_len, size - tag_len, &tlv_len);
vlmfa67ddc2004-06-03 03:38:44 +0000176 switch(len_len) {
177 case -1: RETURN(RC_FAIL);
178 case 0: RETURN(RC_WMORE);
179 }
180
181 /*
182 * FIXME
183 * As of today, the chain of tags
184 * must either contain several indefinite length TLVs,
185 * or several definite length ones.
186 * No mixing is allowed.
187 */
188 if(tlv_len == -1) {
189 /*
190 * Indefinite length.
191 */
192 if(limit_len == -1) {
193 expect_00_terminators++;
194 } else {
195 ASN_DEBUG("Unexpected indefinite length "
196 "in a chain of definite lengths");
197 RETURN(RC_FAIL);
198 }
199 ADVANCE(tag_len + len_len);
200 continue;
201 } else {
202 if(expect_00_terminators) {
203 ASN_DEBUG("Unexpected definite length "
204 "in a chain of indefinite lengths");
205 RETURN(RC_FAIL);
206 }
207 }
208
209 /*
210 * Check that multiple TLVs specify ever decreasing length,
211 * which is consistent.
212 */
213 if(limit_len == -1) {
214 limit_len = tlv_len + tag_len + len_len;
215 } else if(limit_len != tlv_len + tag_len + len_len) {
216 /*
217 * Inner TLV specifies length which is inconsistent
218 * with the outer TLV's length value.
219 */
220 ASN_DEBUG("Outer TLV is %d and inner is %d",
221 limit_len, tlv_len);
222 RETURN(RC_FAIL);
223 }
224
225 ADVANCE(tag_len + len_len);
226
227 limit_len -= (tag_len + len_len);
vlmb42843a2004-06-05 08:17:50 +0000228 if((ssize_t)size > limit_len) {
vlmfa67ddc2004-06-03 03:38:44 +0000229 /*
230 * Make sure that we won't consume more bytes
231 * from the large buffer than the inferred limit.
232 */
233 size = limit_len;
234 }
235 }
236
237 if(opt_tlv_form)
238 *opt_tlv_form = tlv_constr;
239 if(expect_00_terminators)
240 *last_length = -expect_00_terminators;
241 else
242 *last_length = tlv_len;
243
244 RETURN(RC_OK);
245}