blob: 0385722c5bf41f2d9e05c312cba916b15f75a203 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <constr_SET.h>
6#include <netinet/in.h> /* for ntohl() */
7#include <assert.h> /* for assert() */
8
9/*
10 * Number of bytes left for this structure.
11 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
12 * (size) contains the number of bytes in the buffer passed.
13 */
Lev Walkind9bd7752004-06-05 08:17:50 +000014#define LEFT ((size<(size_t)ctx->left)?size:ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16/*
17 * If the subprocessor function returns with an indication that it wants
18 * more data, it may well be a fatal decoding problem, because the
19 * size is constrained by the <TLV>'s L, even if the buffer size allows
20 * reading more data.
21 * For example, consider the buffer containing the following TLVs:
22 * <T:5><L:1><V> <T:6>...
23 * The TLV length clearly indicates that one byte is expected in V, but
24 * if the V processor returns with "want more data" even if the buffer
25 * contains way more data than the V processor have seen.
26 */
Lev Walkind9bd7752004-06-05 08:17:50 +000027#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000028
29/*
30 * This macro "eats" the part of the buffer which is definitely "consumed",
31 * i.e. was correctly converted into local representation or rightfully skipped.
32 */
33#define ADVANCE(num_bytes) do { \
34 size_t num = num_bytes; \
35 ptr += num; \
36 size -= num; \
37 if(ctx->left >= 0) \
38 ctx->left -= num; \
39 consumed_myself += num; \
40 } while(0)
41
42/*
43 * Switch to the next phase of parsing.
44 */
45#define NEXT_PHASE(ctx) do { \
46 ctx->phase++; \
47 ctx->step = 0; \
48 } while(0)
49
50/*
51 * Return a standardized complex structure.
52 */
53#define RETURN(_code) do { \
54 rval.code = _code; \
55 rval.consumed = consumed_myself;\
56 return rval; \
57 } while(0)
58
59/*
60 * Tags are canonically sorted in the tag2element map.
61 */
62static int
63_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin4c36e302004-06-06 07:20:02 +000064 const asn1_TYPE_tag2member_t *a = ap;
65 const asn1_TYPE_tag2member_t *b = bp;
Lev Walkinf15320b2004-06-03 03:38:44 +000066 int a_class = BER_TAG_CLASS(a->el_tag);
67 int b_class = BER_TAG_CLASS(b->el_tag);
68
69 if(a_class == b_class) {
70 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
71 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
72
73 if(a_value == b_value)
74 return 0;
75 else if(a_value < b_value)
76 return -1;
77 else
78 return 1;
79 } else if(a_class < b_class) {
80 return -1;
81 } else {
82 return 1;
83 }
84}
85
86/*
87 * The decoder of the SET type.
88 */
89ber_dec_rval_t
90SET_decode_ber(asn1_TYPE_descriptor_t *sd,
91 void **struct_ptr, void *ptr, size_t size, int tag_mode) {
92 /*
93 * Bring closer parts of structure description.
94 */
95 asn1_SET_specifics_t *specs = sd->specifics;
96 asn1_SET_element_t *elements = specs->elements;
97
98 /*
99 * Parts of the structure being constructed.
100 */
101 void *st = *struct_ptr; /* Target structure. */
102 ber_dec_ctx_t *ctx; /* Decoder context */
103
104 ber_tlv_tag_t tlv_tag; /* T from TLV */
105 //ber_tlv_len_t tlv_len; /* L from TLV */
106 ber_dec_rval_t rval; /* Return code from subparsers */
107
108 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
109 int edx; /* SET element's index */
110
111 ASN_DEBUG("Decoding %s as SET", sd->name);
112
113 /*
114 * Create the target structure if it is not present already.
115 */
116 if(st == 0) {
117 st = *struct_ptr = CALLOC(1, specs->struct_size);
118 if(st == 0) {
119 RETURN(RC_FAIL);
120 }
121 }
122
123 /*
124 * Restore parsing context.
125 */
126 ctx = (st + specs->ctx_offset);
127
128 /*
129 * Start to parse where left previously
130 */
131 switch(ctx->phase) {
132 case 0:
133 /*
134 * PHASE 0.
135 * Check that the set of tags associated with given structure
136 * perfectly fits our expectations.
137 */
138
139 rval = ber_check_tags(sd, ctx, ptr, size,
140 tag_mode, &ctx->left, 0);
141 if(rval.code != RC_OK) {
142 ASN_DEBUG("%s tagging check failed: %d",
143 sd->name, rval.code);
144 consumed_myself += rval.consumed;
145 RETURN(rval.code);
146 }
147
148 if(ctx->left >= 0)
149 ctx->left += rval.consumed; /* ?Substracted below! */
150 ADVANCE(rval.consumed);
151
152 NEXT_PHASE(ctx);
153
154 ASN_DEBUG("Structure advertised %ld bytes, "
155 "buffer contains %ld", (long)ctx->left, (long)size);
156
157 /* Fall through */
158 case 1:
159 /*
160 * PHASE 1.
161 * From the place where we've left it previously,
162 * try to decode the next member from the list of
163 * this structure's elements.
164 * (ctx->step) stores the member being processed
165 * between invocations and the microphase {0,1} of parsing
166 * that member:
167 * step = (2 * <member_number> + <microphase>).
168 * Note, however, that the elements in BER may arrive out of
169 * order, yet DER mandates that they shall arive in the
170 * canonical order of their tags. So, there is a room
171 * for optimization.
172 */
173 for(edx = (ctx->step >> 1); edx < specs->elements_count;
174 ctx->step = (ctx->step & ~1) + 2,
175 edx = (ctx->step >> 1)) {
176 void *memb_ptr; /* Pointer to the member */
177 void *memb_ptr2; /* Pointer to that pointer */
178 ssize_t tag_len; /* Length of TLV's T */
179
180 if(ctx->step & 1)
181 goto microphase2;
182
183 /*
184 * MICROPHASE 1: Synchronize decoding.
185 */
186
187 if(ctx->left == 0)
188 /*
189 * No more things to decode.
190 * Exit out of here and check whether all mandatory
191 * elements have been received (in the next phase).
192 */
193 break;
194
195 /*
196 * Fetch the T from TLV.
197 */
198 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
199 switch(tag_len) {
200 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
201 /* Fall through */
202 case -1: RETURN(RC_FAIL);
203 }
204
205 if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
206 if(LEFT < 2) {
207 if(SIZE_VIOLATION)
208 RETURN(RC_FAIL);
209 else
210 RETURN(RC_WMORE);
211 } else if(((uint8_t *)ptr)[1] == 0) {
212 /*
213 * Found the terminator of the
214 * indefinite length structure.
215 * Invoke the generic finalization function.
216 */
217 goto phase3;
218 }
219 }
220
221 if(BER_TAGS_EQUAL(tlv_tag, elements[edx].tag)) {
222 /*
223 * The elements seem to go in order.
224 * This is not particularly strange,
225 * but is not strongly anticipated either.
226 */
227 } else {
Lev Walkin4c36e302004-06-06 07:20:02 +0000228 asn1_TYPE_tag2member_t *t2m;
229 asn1_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000230
231 key.el_tag = tlv_tag;
232 t2m = bsearch(&key, specs->tag2el, specs->tag2el_count,
233 sizeof(specs->tag2el[0]), _t2e_cmp);
234 if(t2m) {
235 /*
236 * Found the element corresponding to the tag.
237 */
238 edx = t2m->el_no;
239 ctx->step = 2 * edx;
240 } else if(specs->extensible == 0) {
241 ASN_DEBUG("Unexpected tag %s "
242 "in non-extensible SET %s",
243 ber_tlv_tag_string(tlv_tag), sd->name);
244 RETURN(RC_FAIL);
245 } else {
246 /* Skip this tag */
247 ssize_t skip;
248
249 ASN_DEBUG("Skipping unknown tag %s",
250 ber_tlv_tag_string(tlv_tag));
251
252 skip = ber_skip_length(
253 BER_TLV_CONSTRUCTED(ptr),
254 ptr + tag_len, LEFT - tag_len);
255
256 switch(skip) {
257 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
258 /* Fall through */
259 case -1: RETURN(RC_FAIL);
260 }
261
262 ADVANCE(skip + tag_len);
263 ctx->step -= 2;
264 edx--;
265 continue; /* Try again with the next tag */
266 }
267 }
268
269 /*
270 * MICROPHASE 2: Invoke the member-specific decoder.
271 */
272 ctx->step |= 1; /* Confirm entering next microphase */
273 microphase2:
274
275 /*
276 * Check for duplications: must not overwrite
277 * already decoded elements.
278 */
279 if(ASN_SET_ISPRESENT2(st + specs->pres_offset, edx)) {
280 ASN_DEBUG("Duplicate element %d", edx);
281 RETURN(RC_FAIL);
282 }
283
284 /*
285 * Compute the position of the member inside a structure,
286 * and also a type of containment (it may be contained
287 * as pointer or using inline inclusion).
288 */
289 if(elements[edx].optional) {
290 /* Optional member, hereby, a simple pointer */
291 memb_ptr2 = (char *)st + elements[edx].memb_offset;
292 } else {
293 /*
294 * A pointer to a pointer
295 * holding the start of the structure
296 */
297 memb_ptr = (char *)st + elements[edx].memb_offset;
298 memb_ptr2 = &memb_ptr;
299 }
300 /*
301 * Invoke the member fetch routine according to member's type
302 */
303 rval = elements[edx].type->ber_decoder(
304 (void *)elements[edx].type,
305 memb_ptr2, ptr, LEFT,
306 elements[edx].tag_mode);
307 switch(rval.code) {
308 case RC_OK:
309 ASN_SET_MKPRESENT(st + specs->pres_offset, edx);
310 break;
311 case RC_WMORE: /* More data expected */
312 if(!SIZE_VIOLATION) {
313 ADVANCE(rval.consumed);
314 RETURN(RC_WMORE);
315 }
316 /* Fail through */
317 case RC_FAIL: /* Fatal error */
318 RETURN(RC_FAIL);
319 } /* switch(rval) */
320
321 ADVANCE(rval.consumed);
322 } /* for(all structure members) */
323
324 phase3:
325 ctx->phase = 3;
326 /* Fall through */
327 case 3:
328 case 4: /* Only 00 is expected */
329 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
330 sd->name, (long)ctx->left, (long)size);
331
332 /*
333 * Skip everything until the end of the SET.
334 */
335 while(ctx->left) {
336 ssize_t tl, ll;
337
338 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
339 switch(tl) {
340 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
341 /* Fall through */
342 case -1: RETURN(RC_FAIL);
343 }
344
345 /*
346 * If expected <0><0>...
347 */
348 if(ctx->left < 0
349 && ((uint8_t *)ptr)[0] == 0) {
350 if(LEFT < 2) {
351 if(SIZE_VIOLATION)
352 RETURN(RC_FAIL);
353 else
354 RETURN(RC_WMORE);
355 } else if(((uint8_t *)ptr)[1] == 0) {
356 /*
357 * Correctly finished with <0><0>.
358 */
359 ADVANCE(2);
360 ctx->left++;
361 ctx->phase = 4;
362 continue;
363 }
364 }
365
366 if(specs->extensible == 0 || ctx->phase == 4) {
367 ASN_DEBUG("Unexpected continuation "
368 "of a non-extensible type %s",
369 sd->name);
370 RETURN(RC_FAIL);
371 }
372
373 ll = ber_skip_length(
374 BER_TLV_CONSTRUCTED(ptr),
375 ptr + tl, LEFT - tl);
376 switch(ll) {
377 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
378 /* Fall through */
379 case -1: RETURN(RC_FAIL);
380 }
381
382 ADVANCE(tl + ll);
383 }
384
385 ctx->phase = 5;
386 case 5:
387 /*
388 * Check that all mandatory elements are present.
389 */
390 for(edx = 0; edx < specs->elements_count;
391 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
392 unsigned int midx, pres, must;
393
394 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
395 pres = ((unsigned int *)(st+specs->pres_offset))[midx];
396 must = ntohl(specs->_mandatory_elements[midx]);
397
398 if((pres & must) == must) {
399 /*
400 * Yes, everything seems to be in place.
401 */
402 } else {
403 ASN_DEBUG("One or more mandatory elements "
404 "of a SET %s %d (%08x.%08x)=%08x "
405 "are not present",
406 sd->name,
407 midx,
408 pres,
409 must,
410 (~(pres & must) & must)
411 );
412 RETURN(RC_FAIL);
413 }
414 }
415
416 NEXT_PHASE(ctx);
417 }
418
419 RETURN(RC_OK);
420}
421
422/*
423 * The DER encoder of the SET type.
424 */
425der_enc_rval_t
426SET_encode_der(asn1_TYPE_descriptor_t *sd,
427 void *ptr, int tag_mode, ber_tlv_tag_t tag,
428 asn_app_consume_bytes_f *cb, void *app_key) {
429 asn1_SET_specifics_t *specs = sd->specifics;
430 size_t computed_size = 0;
431 der_enc_rval_t my_erval;
432 int t2m_build_own = (specs->tag2el_count != specs->elements_count);
Lev Walkin4c36e302004-06-06 07:20:02 +0000433 asn1_TYPE_tag2member_t *t2m;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 int t2m_count;
435 ssize_t ret;
436 int edx;
437
438 /*
439 * Use existing, or build our own tags map.
440 */
441 if(t2m_build_own) {
442 t2m = alloca(specs->elements_count * sizeof(t2m[0]));
443 t2m_count = 0;
444 } else {
445 /*
446 * There is no untagged CHOICE in this SET.
447 * Employ existing table.
448 */
449 t2m = specs->tag2el;
450 t2m_count = specs->tag2el_count;
451 }
452
453 /*
454 * Gather the length of the underlying members sequence.
455 */
456 for(edx = 0; edx < specs->elements_count; edx++) {
457 asn1_SET_element_t *elm = &specs->elements[edx];
458 der_enc_rval_t erval;
459 void *memb_ptr;
460
461 /*
462 * Compute the length of the encoding of this member.
463 */
464 if(elm->optional) {
465 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
466 if(!memb_ptr) {
467 if(t2m_build_own) {
468 t2m[t2m_count].el_no = edx;
469 t2m[t2m_count].el_tag = 0;
470 t2m_count++;
471 }
472 continue;
473 }
474 } else {
475 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
476 }
477 erval = elm->type->der_encoder(elm->type, memb_ptr,
478 elm->tag_mode, elm->tag,
479 0, 0);
480 if(erval.encoded == -1)
481 return erval;
482 computed_size += erval.encoded;
483
484 /*
485 * Remember the outmost tag of this member.
486 */
487 if(t2m_build_own) {
488 t2m[t2m_count].el_no = edx;
489 t2m[t2m_count].el_tag = asn1_TYPE_outmost_tag(
490 elm->type, memb_ptr, elm->tag_mode, elm->tag);
491 t2m_count++;
492 } else {
493 /*
494 * No dynamic sorting is necessary.
495 */
496 }
497 }
498
499 /*
500 * Finalize order of the components.
501 */
502 assert(t2m_count == specs->elements_count);
503 if(t2m_build_own) {
504 /*
505 * Sort the underlying members according to their
506 * canonical tags order. DER encoding mandates it.
507 */
508 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
509 } else {
510 /*
511 * Tags are already sorted by the compiler.
512 */
513 }
514
515 /*
516 * Encode the TLV for the sequence itself.
517 */
518 ret = der_write_tags(sd, computed_size, tag_mode, tag, cb, app_key);
519 if(ret == -1) {
520 my_erval.encoded = -1;
521 my_erval.failed_type = sd;
522 my_erval.structure_ptr = ptr;
523 return my_erval;
524 }
525 my_erval.encoded = computed_size + ret;
526
527 if(!cb) return my_erval;
528
529 /*
530 * Encode all members.
531 */
532 for(edx = 0; edx < specs->elements_count; edx++) {
533 asn1_SET_element_t *elm;
534 der_enc_rval_t erval;
535 void *memb_ptr;
536
537 /* Encode according to the tag order */
538 elm = &specs->elements[t2m[edx].el_no];
539
540 if(elm->optional) {
541 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
542 if(!memb_ptr) continue;
543 } else {
544 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
545 }
546 erval = elm->type->der_encoder(elm->type, memb_ptr,
547 elm->tag_mode, elm->tag,
548 cb, app_key);
549 if(erval.encoded == -1)
550 return erval;
551 computed_size -= erval.encoded;
552 }
553
554 if(computed_size != 0) {
555 /*
556 * Encoded size is not equal to the computed size.
557 */
558 my_erval.encoded = -1;
559 my_erval.failed_type = sd;
560 my_erval.structure_ptr = ptr;
561 }
562
563 return my_erval;
564}
565
566int
567SET_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
568 asn_app_consume_bytes_f *cb, void *app_key) {
569 asn1_SET_specifics_t *specs = td->specifics;
570 int edx;
571 int ret;
572
573 if(!sptr) return cb("<absent>", 8, app_key);
574
575 /* Dump preamble */
576 if(cb(td->name, strlen(td->name), app_key)
577 || cb(" ::= {\n", 7, app_key))
578 return -1;
579
580 for(edx = 0; edx < specs->elements_count; edx++) {
581 asn1_SET_element_t *elm = &specs->elements[edx];
582 const void *memb_ptr;
583
584 if(elm->optional) {
585 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
586 if(!memb_ptr) continue;
587 } else {
588 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
589 }
590
591 /* Indentation */
592 for(ret = 0; ret < ilevel; ret++) cb(" ", 1, app_key);
593
594 /* Print the member's name and stuff */
595 if(cb(elm->name, strlen(elm->name), app_key)
596 || cb(": ", 2, app_key))
597 return -1;
598
599 /* Print the member itself */
600 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 4,
601 cb, app_key);
602 if(ret) return ret;
603
604 ret = cb("\n", 1, app_key);
605 if(ret) return ret;
606 }
607
608 /* Indentation */
609 for(ret = 0; ret < ilevel - 4; ret++) cb(" ", 1, app_key);
610
611 return cb("}", 1, app_key);
612}
613
614void
615SET_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
616 asn1_SET_specifics_t *specs = td->specifics;
617 int edx;
618
619 if(!td || !ptr)
620 return;
621
622 ASN_DEBUG("Freeing %s as SET", td->name);
623
624 for(edx = 0; edx < specs->elements_count; edx++) {
625 asn1_SET_element_t *elm = &specs->elements[edx];
626 void *memb_ptr;
627 if(elm->optional) {
628 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
629 if(memb_ptr)
630 elm->type->free_struct(elm->type, memb_ptr, 0);
631 } else {
632 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
633 elm->type->free_struct(elm->type, memb_ptr, 1);
634 }
635 }
636
637 if(!contents_only) {
638 FREEMEM(ptr);
639 }
640}
641
642int
643SET_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
644 asn_app_consume_bytes_f *app_errlog, void *app_key) {
645 asn1_SET_specifics_t *specs = td->specifics;
646 int edx;
647
648 if(!sptr) {
649 _ASN_ERRLOG("%s: value not given", td->name);
650 return -1;
651 }
652
653 /*
654 * Iterate over structure members and check their validity.
655 */
656 for(edx = 0; edx < specs->elements_count; edx++) {
657 asn1_SET_element_t *elm = &specs->elements[edx];
658 const void *memb_ptr;
659
660 if(elm->optional) {
661 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
662 if(!memb_ptr) {
663 if(ASN_SET_ISPRESENT2(
664 &(specs->_mandatory_elements), edx)) {
665 _ASN_ERRLOG(
666 "%s: mandatory element "
667 "%s absent",
668 td->name, elm->name);
669 return -1;
670 }
671 continue;
672 }
673 } else {
674 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
675 }
676
677 return elm->type->check_constraints(elm->type, memb_ptr,
678 app_errlog, app_key);
679 }
680
681 return 0;
682}