Add GPLv3, since automake installs GPLv2.
[shishi.git] / asn1 / decoding.c
blob3df85318e6164482b7c3dd4c290e2c6ffed2b209
1 /*
2 * Copyright (C) 2004, 2006 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * The LIBTASN1 library is free software; you can redistribute it
8 * and/or modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA
24 /*****************************************************/
25 /* File: decoding.c */
26 /* Description: Functions to manage DER decoding */
27 /*****************************************************/
29 #include <int.h>
30 #include <errors.h>
31 #include "parser_aux.h"
32 #include <gstr.h>
33 #include "structure.h"
34 #include "element.h"
37 void
38 _asn1_error_description_tag_error (node_asn * node, char *ErrorDescription)
41 Estrcpy (ErrorDescription, ":: tag error near element '");
42 _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
43 MAX_ERROR_DESCRIPTION_SIZE - 40);
44 Estrcat (ErrorDescription, "'");
48 /**
49 * asn1_get_length_der:
50 * @der: DER data to decode.
51 * @der_len: Length of DER data to decode.
52 * @len: Output variable containing the length of the DER length field.
54 * Extract a length field from DER data.
56 * Return value: Return the decoded length value, or -1 on indefinite
57 * length, or -2 when the value was too big.
58 **/
59 signed long
60 asn1_get_length_der (const unsigned char *der, int der_len, int *len)
62 unsigned long ans;
63 int k, punt;
65 *len = 0;
66 if (der_len <= 0)
67 return 0;
69 if (!(der[0] & 128))
71 /* short form */
72 *len = 1;
73 return der[0];
75 else
77 /* Long form */
78 k = der[0] & 0x7F;
79 punt = 1;
80 if (k)
81 { /* definite length method */
82 ans = 0;
83 while (punt <= k && punt < der_len)
85 unsigned long last = ans;
87 ans = ans * 256 + der[punt++];
88 if (ans < last)
89 /* we wrapped around, no bignum support... */
90 return -2;
93 else
94 { /* indefinite length method */
95 ans = -1;
98 *len = punt;
99 return ans;
107 * asn1_get_tag_der:
108 * @der: DER data to decode.
109 * @der_len: Length of DER data to decode.
110 * @class: Output variable containing decoded class.
111 * @len: Output variable containing the length of the DER TAG data.
112 * @tag: Output variable containing the decoded tag.
114 * Decode the class and TAG from DER code.
116 * Return value: Returns ASN1_SUCCESS on success, or an error.
119 asn1_get_tag_der (const unsigned char *der, int der_len,
120 unsigned char *class, int *len, unsigned long *tag)
122 int punt, ris;
124 if (der == NULL || der_len <= 0 || len == NULL)
125 return ASN1_DER_ERROR;
127 *class = der[0] & 0xE0;
128 if ((der[0] & 0x1F) != 0x1F)
130 /* short form */
131 *len = 1;
132 ris = der[0] & 0x1F;
134 else
136 /* Long form */
137 punt = 1;
138 ris = 0;
139 while (punt <= der_len && der[punt] & 128)
141 int last = ris;
142 ris = ris * 128 + (der[punt++] & 0x7F);
143 if (ris < last)
144 /* wrapper around, and no bignums... */
145 return ASN1_DER_ERROR;
147 if (punt >= der_len)
148 return ASN1_DER_ERROR;
150 int last = ris;
151 ris = ris * 128 + (der[punt++] & 0x7F);
152 if (ris < last)
153 /* wrapper around, and no bignums... */
154 return ASN1_DER_ERROR;
156 *len = punt;
158 if (tag)
159 *tag = ris;
160 return ASN1_SUCCESS;
167 * asn1_get_octet_der:
168 * @der: DER data to decode containing the OCTET SEQUENCE.
169 * @der_len: Length of DER data to decode.
170 * @ret_len: Output variable containing the length of the DER data.
171 * @str: Pre-allocated output buffer to put decoded OCTET SEQUENCE in.
172 * @str_size: Length of pre-allocated output buffer.
173 * @str_len: Output variable containing the length of the OCTET SEQUENCE.
175 * Extract an OCTET SEQUENCE from DER data.
177 * Return value: Returns ASN1_SUCCESS on success, or an error.
180 asn1_get_octet_der (const unsigned char *der, int der_len,
181 int *ret_len, unsigned char *str, int str_size,
182 int *str_len)
184 int len_len;
186 if (der_len <= 0)
187 return ASN1_GENERIC_ERROR;
189 /* if(str==NULL) return ASN1_SUCCESS; */
190 *str_len = asn1_get_length_der (der, der_len, &len_len);
192 if (*str_len < 0)
193 return ASN1_DER_ERROR;
195 *ret_len = *str_len + len_len;
196 if (str_size >= *str_len)
197 memcpy (str, der + len_len, *str_len);
198 else
200 return ASN1_MEM_ERROR;
203 return ASN1_SUCCESS;
208 /* Returns ASN1_SUCCESS on success or an error code on error.
211 _asn1_get_time_der (const unsigned char *der, int der_len, int *ret_len,
212 char *str, int str_size)
214 int len_len, str_len;
216 if (der_len <= 0 || str == NULL)
217 return ASN1_DER_ERROR;
218 str_len = asn1_get_length_der (der, der_len, &len_len);
219 if (str_len < 0 || str_size < str_len)
220 return ASN1_DER_ERROR;
221 memcpy (str, der + len_len, str_len);
222 str[str_len] = 0;
223 *ret_len = str_len + len_len;
225 return ASN1_SUCCESS;
230 void
231 _asn1_get_objectid_der (const unsigned char *der, int der_len, int *ret_len,
232 char *str, int str_size)
234 int len_len, len, k;
235 char temp[20];
236 unsigned long val, val1;
238 *ret_len = 0;
239 if (str && str_size > 0)
240 str[0] = 0; /* no oid */
242 if (str == NULL || der_len <= 0)
243 return;
244 len = asn1_get_length_der (der, der_len, &len_len);
246 if (len < 0 || len > der_len || len_len > der_len)
247 return;
249 val1 = der[len_len] / 40;
250 val = der[len_len] - val1 * 40;
252 _asn1_str_cpy (str, str_size, _asn1_ltostr (val1, temp));
253 _asn1_str_cat (str, str_size, ".");
254 _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
256 val = 0;
257 for (k = 1; k < len; k++)
259 val = val << 7;
260 val |= der[len_len + k] & 0x7F;
261 if (!(der[len_len + k] & 0x80))
263 _asn1_str_cat (str, str_size, ".");
264 _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
265 val = 0;
268 *ret_len = len + len_len;
275 * asn1_get_bit_der:
276 * @der: DER data to decode containing the BIT SEQUENCE.
277 * @der_len: Length of DER data to decode.
278 * @ret_len: Output variable containing the length of the DER data.
279 * @str: Pre-allocated output buffer to put decoded BIT SEQUENCE in.
280 * @str_size: Length of pre-allocated output buffer.
281 * @bit_len: Output variable containing the size of the BIT SEQUENCE.
283 * Extract a BIT SEQUENCE from DER data.
285 * Return value: Return ASN1_SUCCESS on success, or an error.
288 asn1_get_bit_der (const unsigned char *der, int der_len,
289 int *ret_len, unsigned char *str, int str_size,
290 int *bit_len)
292 int len_len, len_byte;
294 if (der_len <= 0)
295 return ASN1_GENERIC_ERROR;
296 len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
297 if (len_byte < 0)
298 return ASN1_DER_ERROR;
300 *ret_len = len_byte + len_len + 1;
301 *bit_len = len_byte * 8 - der[len_len];
303 if (str_size >= len_byte)
304 memcpy (str, der + len_len + 1, len_byte);
305 else
307 return ASN1_MEM_ERROR;
310 return ASN1_SUCCESS;
317 _asn1_extract_tag_der (node_asn * node, const unsigned char *der, int der_len,
318 int *ret_len)
320 node_asn *p;
321 int counter, len2, len3, is_tag_implicit;
322 unsigned long tag, tag_implicit = 0;
323 unsigned char class, class2, class_implicit = 0;
325 if (der_len <= 0)
326 return ASN1_GENERIC_ERROR;
328 counter = is_tag_implicit = 0;
330 if (node->type & CONST_TAG)
332 p = node->down;
333 while (p)
335 if (type_field (p->type) == TYPE_TAG)
337 if (p->type & CONST_APPLICATION)
338 class2 = ASN1_CLASS_APPLICATION;
339 else if (p->type & CONST_UNIVERSAL)
340 class2 = ASN1_CLASS_UNIVERSAL;
341 else if (p->type & CONST_PRIVATE)
342 class2 = ASN1_CLASS_PRIVATE;
343 else
344 class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
346 if (p->type & CONST_EXPLICIT)
348 if (asn1_get_tag_der
349 (der + counter, der_len - counter, &class, &len2,
350 &tag) != ASN1_SUCCESS)
351 return ASN1_DER_ERROR;
352 if (counter + len2 > der_len)
353 return ASN1_DER_ERROR;
354 counter += len2;
355 len3 =
356 asn1_get_length_der (der + counter, der_len - counter,
357 &len2);
358 if (len3 < 0)
359 return ASN1_DER_ERROR;
360 counter += len2;
361 if (!is_tag_implicit)
363 if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
364 (tag != strtoul ((char *) p->value, NULL, 10)))
365 return ASN1_TAG_ERROR;
367 else
368 { /* ASN1_TAG_IMPLICIT */
369 if ((class != class_implicit) || (tag != tag_implicit))
370 return ASN1_TAG_ERROR;
373 is_tag_implicit = 0;
375 else
376 { /* ASN1_TAG_IMPLICIT */
377 if (!is_tag_implicit)
379 if ((type_field (node->type) == TYPE_SEQUENCE) ||
380 (type_field (node->type) == TYPE_SEQUENCE_OF) ||
381 (type_field (node->type) == TYPE_SET) ||
382 (type_field (node->type) == TYPE_SET_OF))
383 class2 |= ASN1_CLASS_STRUCTURED;
384 class_implicit = class2;
385 tag_implicit = strtoul ((char *) p->value, NULL, 10);
386 is_tag_implicit = 1;
390 p = p->right;
394 if (is_tag_implicit)
396 if (asn1_get_tag_der
397 (der + counter, der_len - counter, &class, &len2,
398 &tag) != ASN1_SUCCESS)
399 return ASN1_DER_ERROR;
400 if (counter + len2 > der_len)
401 return ASN1_DER_ERROR;
403 if ((class != class_implicit) || (tag != tag_implicit))
405 if (type_field (node->type) == TYPE_OCTET_STRING)
407 class_implicit |= ASN1_CLASS_STRUCTURED;
408 if ((class != class_implicit) || (tag != tag_implicit))
409 return ASN1_TAG_ERROR;
411 else
412 return ASN1_TAG_ERROR;
415 else
417 if (type_field (node->type) == TYPE_TAG)
419 counter = 0;
420 *ret_len = counter;
421 return ASN1_SUCCESS;
424 if (asn1_get_tag_der
425 (der + counter, der_len - counter, &class, &len2,
426 &tag) != ASN1_SUCCESS)
427 return ASN1_DER_ERROR;
428 if (counter + len2 > der_len)
429 return ASN1_DER_ERROR;
431 switch (type_field (node->type))
433 case TYPE_NULL:
434 if ((class != ASN1_CLASS_UNIVERSAL) || (tag != ASN1_TAG_NULL))
435 return ASN1_DER_ERROR;
436 break;
437 case TYPE_BOOLEAN:
438 if ((class != ASN1_CLASS_UNIVERSAL) || (tag != ASN1_TAG_BOOLEAN))
439 return ASN1_DER_ERROR;
440 break;
441 case TYPE_INTEGER:
442 if ((class != ASN1_CLASS_UNIVERSAL) || (tag != ASN1_TAG_INTEGER))
443 return ASN1_DER_ERROR;
444 break;
445 case TYPE_ENUMERATED:
446 if ((class != ASN1_CLASS_UNIVERSAL) || (tag != ASN1_TAG_ENUMERATED))
447 return ASN1_DER_ERROR;
448 break;
449 case TYPE_OBJECT_ID:
450 if ((class != ASN1_CLASS_UNIVERSAL) || (tag != ASN1_TAG_OBJECT_ID))
451 return ASN1_DER_ERROR;
452 break;
453 case TYPE_TIME:
454 if (node->type & CONST_UTC)
456 if ((class != ASN1_CLASS_UNIVERSAL)
457 || (tag != ASN1_TAG_UTCTime))
458 return ASN1_DER_ERROR;
460 else
462 if ((class != ASN1_CLASS_UNIVERSAL)
463 || (tag != ASN1_TAG_GENERALIZEDTime))
464 return ASN1_DER_ERROR;
466 break;
467 case TYPE_OCTET_STRING:
468 if (((class != ASN1_CLASS_UNIVERSAL)
469 && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
470 || (tag != ASN1_TAG_OCTET_STRING))
471 return ASN1_DER_ERROR;
472 break;
473 case TYPE_GENERALSTRING:
474 if ((class != ASN1_CLASS_UNIVERSAL)
475 || (tag != ASN1_TAG_GENERALSTRING))
476 return ASN1_DER_ERROR;
477 break;
478 case TYPE_BIT_STRING:
479 if ((class != ASN1_CLASS_UNIVERSAL) || (tag != ASN1_TAG_BIT_STRING))
480 return ASN1_DER_ERROR;
481 break;
482 case TYPE_SEQUENCE:
483 case TYPE_SEQUENCE_OF:
484 if ((class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED))
485 || (tag != ASN1_TAG_SEQUENCE))
486 return ASN1_DER_ERROR;
487 break;
488 case TYPE_SET:
489 case TYPE_SET_OF:
490 if ((class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED))
491 || (tag != ASN1_TAG_SET))
492 return ASN1_DER_ERROR;
493 break;
494 case TYPE_ANY:
495 counter -= len2;
496 break;
497 default:
498 return ASN1_DER_ERROR;
499 break;
503 counter += len2;
504 *ret_len = counter;
505 return ASN1_SUCCESS;
510 _asn1_delete_not_used (node_asn * node)
512 node_asn *p, *p2;
514 if (node == NULL)
515 return ASN1_ELEMENT_NOT_FOUND;
517 p = node;
518 while (p)
520 if (p->type & CONST_NOT_USED)
522 p2 = NULL;
523 if (p != node)
525 p2 = _asn1_find_left (p);
526 if (!p2)
527 p2 = _asn1_find_up (p);
529 asn1_delete_structure (&p);
530 p = p2;
533 if (!p)
534 break; /* reach node */
536 if (p->down)
538 p = p->down;
540 else
542 if (p == node)
543 p = NULL;
544 else if (p->right)
545 p = p->right;
546 else
548 while (1)
550 p = _asn1_find_up (p);
551 if (p == node)
553 p = NULL;
554 break;
556 if (p->right)
558 p = p->right;
559 break;
565 return ASN1_SUCCESS;
569 asn1_retCode
570 _asn1_get_octet_string (const unsigned char *der, node_asn * node, int *len)
572 int len2, len3, counter, counter2, counter_end, tot_len, indefinite;
573 unsigned char *temp, *temp2;
575 counter = 0;
577 if (*(der - 1) & ASN1_CLASS_STRUCTURED)
579 tot_len = 0;
580 indefinite = asn1_get_length_der (der, *len, &len3);
581 if (indefinite < -1)
582 return ASN1_DER_ERROR;
584 counter += len3;
585 if (indefinite >= 0)
586 indefinite += len3;
588 while (1)
590 if (counter > (*len))
591 return ASN1_DER_ERROR;
593 if (indefinite == -1)
595 if ((der[counter] == 0) && (der[counter + 1] == 0))
597 counter += 2;
598 break;
601 else if (counter >= indefinite)
602 break;
604 if (der[counter] != ASN1_TAG_OCTET_STRING)
605 return ASN1_DER_ERROR;
607 counter++;
609 len2 = asn1_get_length_der (der + counter, *len - counter, &len3);
610 if (len2 <= 0)
611 return ASN1_DER_ERROR;
613 counter += len3 + len2;
614 tot_len += len2;
617 /* copy */
618 if (node)
620 asn1_length_der (tot_len, NULL, &len2);
621 temp = _asn1_alloca (len2 + tot_len);
622 if (temp == NULL)
624 return ASN1_MEM_ALLOC_ERROR;
627 asn1_length_der (tot_len, temp, &len2);
628 tot_len += len2;
629 temp2 = temp + len2;
630 len2 = asn1_get_length_der (der, *len, &len3);
631 if (len2 < -1)
632 return ASN1_DER_ERROR;
633 counter2 = len3 + 1;
635 if (indefinite == -1)
636 counter_end = counter - 2;
637 else
638 counter_end = counter;
640 while (counter2 < counter_end)
642 len2 =
643 asn1_get_length_der (der + counter2, *len - counter, &len3);
644 if (len2 < -1)
645 return ASN1_DER_ERROR;
647 /* FIXME: to be checked. Is this ok? Has the
648 * size been checked before?
650 memcpy (temp2, der + counter2 + len3, len2);
651 temp2 += len2;
652 counter2 += len2 + len3 + 1;
655 _asn1_set_value (node, temp, tot_len);
656 _asn1_afree (temp);
659 else
660 { /* NOT STRUCTURED */
661 len2 = asn1_get_length_der (der, *len, &len3);
662 if (len2 < 0)
663 return ASN1_DER_ERROR;
664 if (len3 + len2 > *len)
665 return ASN1_DER_ERROR;
666 if (node)
667 _asn1_set_value (node, der, len3 + len2);
668 counter = len3 + len2;
671 *len = counter;
672 return ASN1_SUCCESS;
677 asn1_retCode
678 _asn1_get_indefinite_length_string (const unsigned char *der, int *len)
680 int len2, len3, counter, indefinite;
681 unsigned long tag;
682 unsigned char class;
684 counter = indefinite = 0;
686 while (1)
688 if ((*len) < counter)
689 return ASN1_DER_ERROR;
691 if ((der[counter] == 0) && (der[counter + 1] == 0))
693 counter += 2;
694 indefinite--;
695 if (indefinite <= 0)
696 break;
697 else
698 continue;
701 if (asn1_get_tag_der
702 (der + counter, *len - counter, &class, &len2,
703 &tag) != ASN1_SUCCESS)
704 return ASN1_DER_ERROR;
705 if (counter + len2 > *len)
706 return ASN1_DER_ERROR;
707 counter += len2;
708 len2 = asn1_get_length_der (der + counter, *len - counter, &len3);
709 if (len2 < -1)
710 return ASN1_DER_ERROR;
711 if (len2 == -1)
713 indefinite++;
714 counter += 1;
716 else
718 counter += len2 + len3;
722 *len = counter;
723 return ASN1_SUCCESS;
729 * asn1_der_decoding - Fill the structure *ELEMENT with values of a DER encoding string.
730 * @element: pointer to an ASN1 structure.
731 * @ider: vector that contains the DER encoding.
732 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1].
733 * @errorDescription: null-terminated string contains details when an
734 * error occurred.
736 * Fill the structure *ELEMENT with values of a DER encoding
737 * string. The sructure must just be created with function
738 * 'create_stucture'. If an error occurs during the decoding
739 * procedure, the *ELEMENT is deleted and set equal to
740 * %ASN1_TYPE_EMPTY.
742 * Returns:
744 * ASN1_SUCCESS: DER encoding OK.
746 * ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE_EMPTY.
748 * ASN1_TAG_ERROR,ASN1_DER_ERROR: The der encoding doesn't match
749 * the structure NAME. *ELEMENT deleted.
752 asn1_retCode
753 asn1_der_decoding (ASN1_TYPE * element, const void *ider, int len,
754 char *errorDescription)
756 node_asn *node, *p, *p2, *p3;
757 char temp[128];
758 int counter, len2, len3, len4, move, ris, tlen;
759 unsigned char class, *temp2;
760 unsigned long tag;
761 int indefinite, result;
762 const unsigned char *der = ider;
764 node = *element;
766 if (node == ASN1_TYPE_EMPTY)
767 return ASN1_ELEMENT_NOT_FOUND;
769 if (node->type & CONST_OPTION)
771 asn1_delete_structure (element);
772 return ASN1_GENERIC_ERROR;
775 counter = 0;
776 move = DOWN;
777 p = node;
778 while (1)
780 ris = ASN1_SUCCESS;
781 if (move != UP)
783 if (p->type & CONST_SET)
785 p2 = _asn1_find_up (p);
786 len2 = strtol (p2->value, NULL, 10);
787 if (len2 == -1)
789 if (!der[counter] && !der[counter + 1])
791 p = p2;
792 move = UP;
793 counter += 2;
794 continue;
797 else if (counter == len2)
799 p = p2;
800 move = UP;
801 continue;
803 else if (counter > len2)
805 asn1_delete_structure (element);
806 return ASN1_DER_ERROR;
808 p2 = p2->down;
809 while (p2)
811 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
813 if (type_field (p2->type) != TYPE_CHOICE)
814 ris =
815 _asn1_extract_tag_der (p2, der + counter,
816 len - counter, &len2);
817 else
819 p3 = p2->down;
820 while (p3)
822 ris =
823 _asn1_extract_tag_der (p3, der + counter,
824 len - counter, &len2);
825 if (ris == ASN1_SUCCESS)
826 break;
827 p3 = p3->right;
830 if (ris == ASN1_SUCCESS)
832 p2->type &= ~CONST_NOT_USED;
833 p = p2;
834 break;
837 p2 = p2->right;
839 if (p2 == NULL)
841 asn1_delete_structure (element);
842 return ASN1_DER_ERROR;
846 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
848 p2 = _asn1_find_up (p);
849 len2 = strtol (p2->value, NULL, 10);
850 if (counter == len2)
852 if (p->right)
854 p2 = p->right;
855 move = RIGHT;
857 else
858 move = UP;
860 if (p->type & CONST_OPTION)
861 asn1_delete_structure (&p);
863 p = p2;
864 continue;
868 if (type_field (p->type) == TYPE_CHOICE)
870 while (p->down)
872 if (counter < len)
873 ris =
874 _asn1_extract_tag_der (p->down, der + counter,
875 len - counter, &len2);
876 else
877 ris = ASN1_DER_ERROR;
878 if (ris == ASN1_SUCCESS)
880 while (p->down->right)
882 p2 = p->down->right;
883 asn1_delete_structure (&p2);
885 break;
887 else if (ris == ASN1_ERROR_TYPE_ANY)
889 asn1_delete_structure (element);
890 return ASN1_ERROR_TYPE_ANY;
892 else
894 p2 = p->down;
895 asn1_delete_structure (&p2);
899 if (p->down == NULL)
901 if (!(p->type & CONST_OPTION))
903 asn1_delete_structure (element);
904 return ASN1_DER_ERROR;
907 else
908 p = p->down;
911 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
913 p2 = _asn1_find_up (p);
914 len2 = strtol (p2->value, NULL, 10);
915 if ((len2 != -1) && (counter > len2))
916 ris = ASN1_TAG_ERROR;
919 if (ris == ASN1_SUCCESS)
920 ris =
921 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
922 if (ris != ASN1_SUCCESS)
924 if (p->type & CONST_OPTION)
926 p->type |= CONST_NOT_USED;
927 move = RIGHT;
929 else if (p->type & CONST_DEFAULT)
931 _asn1_set_value (p, NULL, 0);
932 move = RIGHT;
934 else
936 if (errorDescription != NULL)
937 _asn1_error_description_tag_error (p, errorDescription);
939 asn1_delete_structure (element);
940 return ASN1_TAG_ERROR;
943 else
944 counter += len2;
947 if (ris == ASN1_SUCCESS)
949 switch (type_field (p->type))
951 case TYPE_NULL:
952 if (der[counter])
954 asn1_delete_structure (element);
955 return ASN1_DER_ERROR;
957 counter++;
958 move = RIGHT;
959 break;
960 case TYPE_BOOLEAN:
961 if (der[counter++] != 1)
963 asn1_delete_structure (element);
964 return ASN1_DER_ERROR;
966 if (der[counter++] == 0)
967 _asn1_set_value (p, "F", 1);
968 else
969 _asn1_set_value (p, "T", 1);
970 move = RIGHT;
971 break;
972 case TYPE_INTEGER:
973 case TYPE_ENUMERATED:
974 len2 =
975 asn1_get_length_der (der + counter, len - counter, &len3);
976 if (len2 < 0)
977 return ASN1_DER_ERROR;
978 if (len2 + len3 > len - counter)
979 return ASN1_DER_ERROR;
980 _asn1_set_value (p, der + counter, len3 + len2);
981 counter += len3 + len2;
982 move = RIGHT;
983 break;
984 case TYPE_OBJECT_ID:
985 _asn1_get_objectid_der (der + counter, len - counter, &len2,
986 temp, sizeof (temp));
987 tlen = strlen (temp);
988 if (tlen > 0)
989 _asn1_set_value (p, temp, tlen + 1);
990 counter += len2;
991 move = RIGHT;
992 break;
993 case TYPE_TIME:
994 result =
995 _asn1_get_time_der (der + counter, len - counter, &len2, temp,
996 sizeof (temp) - 1);
997 if (result != ASN1_SUCCESS)
999 asn1_delete_structure (element);
1000 return result;
1002 tlen = strlen (temp);
1003 if (tlen > 0)
1004 _asn1_set_value (p, temp, tlen + 1);
1005 counter += len2;
1006 move = RIGHT;
1007 break;
1008 case TYPE_OCTET_STRING:
1009 len3 = len - counter;
1010 ris = _asn1_get_octet_string (der + counter, p, &len3);
1011 if (ris != ASN1_SUCCESS)
1012 return ris;
1013 counter += len3;
1014 move = RIGHT;
1015 break;
1016 case TYPE_GENERALSTRING:
1017 len2 =
1018 asn1_get_length_der (der + counter, len - counter, &len3);
1019 if (len2 < 0)
1020 return ASN1_DER_ERROR;
1021 if (len3 + len2 > len - counter)
1022 return ASN1_DER_ERROR;
1023 _asn1_set_value (p, der + counter, len3 + len2);
1024 counter += len3 + len2;
1025 move = RIGHT;
1026 break;
1027 case TYPE_BIT_STRING:
1028 len2 =
1029 asn1_get_length_der (der + counter, len - counter, &len3);
1030 if (len2 < 0)
1031 return ASN1_DER_ERROR;
1032 if (len3 + len2 > len - counter)
1033 return ASN1_DER_ERROR;
1034 _asn1_set_value (p, der + counter, len3 + len2);
1035 counter += len3 + len2;
1036 move = RIGHT;
1037 break;
1038 case TYPE_SEQUENCE:
1039 case TYPE_SET:
1040 if (move == UP)
1042 len2 = strtol (p->value, NULL, 10);
1043 _asn1_set_value (p, NULL, 0);
1044 if (len2 == -1)
1045 { /* indefinite length method */
1046 if (len - counter + 1 > 0)
1048 if ((der[counter]) || der[counter + 1])
1050 asn1_delete_structure (element);
1051 return ASN1_DER_ERROR;
1054 else
1055 return ASN1_DER_ERROR;
1056 counter += 2;
1058 else
1059 { /* definite length method */
1060 if (len2 != counter)
1062 asn1_delete_structure (element);
1063 return ASN1_DER_ERROR;
1066 move = RIGHT;
1068 else
1069 { /* move==DOWN || move==RIGHT */
1070 len3 =
1071 asn1_get_length_der (der + counter, len - counter, &len2);
1072 if (len3 < -1)
1073 return ASN1_DER_ERROR;
1074 counter += len2;
1075 if (len3 > 0)
1077 _asn1_ltostr (counter + len3, temp);
1078 tlen = strlen (temp);
1079 if (tlen > 0)
1080 _asn1_set_value (p, temp, tlen + 1);
1081 move = DOWN;
1083 else if (len3 == 0)
1085 p2 = p->down;
1086 while (p2)
1088 if (type_field (p2->type) != TYPE_TAG)
1090 p3 = p2->right;
1091 asn1_delete_structure (&p2);
1092 p2 = p3;
1094 else
1095 p2 = p2->right;
1097 move = RIGHT;
1099 else
1100 { /* indefinite length method */
1101 _asn1_set_value (p, "-1", 3);
1102 move = DOWN;
1105 break;
1106 case TYPE_SEQUENCE_OF:
1107 case TYPE_SET_OF:
1108 if (move == UP)
1110 len2 = strtol (p->value, NULL, 10);
1111 if (len2 == -1)
1112 { /* indefinite length method */
1113 if ((counter + 2) > len)
1114 return ASN1_DER_ERROR;
1115 if ((der[counter]) || der[counter + 1])
1117 _asn1_append_sequence_set (p);
1118 p = p->down;
1119 while (p->right)
1120 p = p->right;
1121 move = RIGHT;
1122 continue;
1124 _asn1_set_value (p, NULL, 0);
1125 counter += 2;
1127 else
1128 { /* definite length method */
1129 if (len2 > counter)
1131 _asn1_append_sequence_set (p);
1132 p = p->down;
1133 while (p->right)
1134 p = p->right;
1135 move = RIGHT;
1136 continue;
1138 _asn1_set_value (p, NULL, 0);
1139 if (len2 != counter)
1141 asn1_delete_structure (element);
1142 return ASN1_DER_ERROR;
1146 else
1147 { /* move==DOWN || move==RIGHT */
1148 len3 =
1149 asn1_get_length_der (der + counter, len - counter, &len2);
1150 if (len3 < -1)
1151 return ASN1_DER_ERROR;
1152 counter += len2;
1153 if (len3)
1155 if (len3 > 0)
1156 { /* definite length method */
1157 _asn1_ltostr (counter + len3, temp);
1158 tlen = strlen (temp);
1160 if (tlen > 0)
1161 _asn1_set_value (p, temp, tlen + 1);
1163 else
1164 { /* indefinite length method */
1165 _asn1_set_value (p, "-1", 3);
1167 p2 = p->down;
1168 while ((type_field (p2->type) == TYPE_TAG)
1169 || (type_field (p2->type) == TYPE_SIZE))
1170 p2 = p2->right;
1171 if (p2->right == NULL)
1172 _asn1_append_sequence_set (p);
1173 p = p2;
1176 move = RIGHT;
1177 break;
1178 case TYPE_ANY:
1179 if (asn1_get_tag_der
1180 (der + counter, len - counter, &class, &len2,
1181 &tag) != ASN1_SUCCESS)
1182 return ASN1_DER_ERROR;
1183 if (counter + len2 > len)
1184 return ASN1_DER_ERROR;
1185 len4 =
1186 asn1_get_length_der (der + counter + len2,
1187 len - counter - len2, &len3);
1188 if (len4 < -1)
1189 return ASN1_DER_ERROR;
1190 if (len4 > len - counter + len2 + len3)
1191 return ASN1_DER_ERROR;
1192 if (len4 != -1)
1194 len2 += len4;
1195 asn1_length_der (len2 + len3, NULL, &len4);
1196 temp2 = (unsigned char *) _asn1_alloca (len2 + len3 + len4);
1197 if (temp2 == NULL)
1199 asn1_delete_structure (element);
1200 return ASN1_MEM_ALLOC_ERROR;
1203 asn1_octet_der (der + counter, len2 + len3, temp2, &len4);
1204 _asn1_set_value (p, temp2, len4);
1205 _asn1_afree (temp2);
1206 counter += len2 + len3;
1208 else
1209 { /* indefinite length */
1210 /* Check indefinite lenth method in an EXPLICIT TAG */
1211 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1212 indefinite = 1;
1213 else
1214 indefinite = 0;
1216 len2 = len - counter;
1217 ris =
1218 _asn1_get_indefinite_length_string (der + counter, &len2);
1219 if (ris != ASN1_SUCCESS)
1221 asn1_delete_structure (element);
1222 return ris;
1224 asn1_length_der (len2, NULL, &len4);
1225 temp2 = (unsigned char *) _asn1_alloca (len2 + len4);
1226 if (temp2 == NULL)
1228 asn1_delete_structure (element);
1229 return ASN1_MEM_ALLOC_ERROR;
1232 asn1_octet_der (der + counter, len2, temp2, &len4);
1233 _asn1_set_value (p, temp2, len4);
1234 _asn1_afree (temp2);
1235 counter += len2;
1237 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1238 an indefinite length method. */
1239 if (indefinite)
1241 if (!der[counter] && !der[counter + 1])
1243 counter += 2;
1245 else
1247 asn1_delete_structure (element);
1248 return ASN1_DER_ERROR;
1252 move = RIGHT;
1253 break;
1254 default:
1255 move = (move == UP) ? RIGHT : DOWN;
1256 break;
1260 if (p == node && move != DOWN)
1261 break;
1263 if (move == DOWN)
1265 if (p->down)
1266 p = p->down;
1267 else
1268 move = RIGHT;
1270 if ((move == RIGHT) && !(p->type & CONST_SET))
1272 if (p->right)
1273 p = p->right;
1274 else
1275 move = UP;
1277 if (move == UP)
1278 p = _asn1_find_up (p);
1281 _asn1_delete_not_used (*element);
1283 if (counter != len)
1285 asn1_delete_structure (element);
1286 return ASN1_DER_ERROR;
1289 return ASN1_SUCCESS;
1293 #define FOUND 1
1294 #define SAME_BRANCH 2
1295 #define OTHER_BRANCH 3
1296 #define EXIT 4
1299 * asn1_der_decoding_element - Fill the element named ELEMENTNAME of the structure STRUCTURE with values of a DER encoding string.
1300 * @structure: pointer to an ASN1 structure
1301 * @elementName: name of the element to fill
1302 * @ider: vector that contains the DER encoding of the whole structure.
1303 * @len: number of bytes of *der: der[0]..der[len-1]
1304 * @errorDescription: null-terminated string contains details when an
1305 * error occurred.
1307 * Fill the element named ELEMENTNAME with values of a DER encoding
1308 * string. The sructure must just be created with function
1309 * 'create_stucture'. The DER vector must contain the encoding
1310 * string of the whole STRUCTURE. If an error occurs during the
1311 * decoding procedure, the *STRUCTURE is deleted and set equal to
1312 * %ASN1_TYPE_EMPTY.
1314 * Returns:
1316 * ASN1_SUCCESS: DER encoding OK.
1318 * ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE_EMPTY or
1319 * elementName == NULL.
1321 * ASN1_TAG_ERROR,ASN1_DER_ERROR: The der encoding doesn't match
1322 * the structure STRUCTURE. *ELEMENT deleted.
1325 asn1_retCode
1326 asn1_der_decoding_element (ASN1_TYPE * structure, const char *elementName,
1327 const void *ider, int len, char *errorDescription)
1329 node_asn *node, *p, *p2, *p3, *nodeFound = ASN1_TYPE_EMPTY;
1330 char temp[128], currentName[MAX_NAME_SIZE * 10], *dot_p, *char_p;
1331 int nameLen = MAX_NAME_SIZE * 10 - 1, state;
1332 int counter, len2, len3, len4, move, ris, tlen;
1333 unsigned char class, *temp2;
1334 unsigned long tag;
1335 int indefinite, result;
1336 const unsigned char *der = ider;
1338 node = *structure;
1340 if (node == ASN1_TYPE_EMPTY)
1341 return ASN1_ELEMENT_NOT_FOUND;
1343 if (elementName == NULL)
1345 asn1_delete_structure (structure);
1346 return ASN1_ELEMENT_NOT_FOUND;
1349 if (node->type & CONST_OPTION)
1351 asn1_delete_structure (structure);
1352 return ASN1_GENERIC_ERROR;
1355 if ((*structure)->name)
1356 { /* Has *structure got a name? */
1357 nameLen -= strlen ((*structure)->name);
1358 if (nameLen > 0)
1359 strcpy (currentName, (*structure)->name);
1360 else
1362 asn1_delete_structure (structure);
1363 return ASN1_MEM_ERROR;
1365 if (!(strcmp (currentName, elementName)))
1367 state = FOUND;
1368 nodeFound = *structure;
1370 else if (!memcmp (currentName, elementName, strlen (currentName)))
1371 state = SAME_BRANCH;
1372 else
1373 state = OTHER_BRANCH;
1375 else
1376 { /* *structure doesn't have a name? */
1377 currentName[0] = 0;
1378 if (elementName[0] == 0)
1380 state = FOUND;
1381 nodeFound = *structure;
1383 else
1385 state = SAME_BRANCH;
1389 counter = 0;
1390 move = DOWN;
1391 p = node;
1392 while (1)
1395 ris = ASN1_SUCCESS;
1397 if (move != UP)
1399 if (p->type & CONST_SET)
1401 p2 = _asn1_find_up (p);
1402 len2 = strtol (p2->value, NULL, 10);
1403 if (counter == len2)
1405 p = p2;
1406 move = UP;
1407 continue;
1409 else if (counter > len2)
1411 asn1_delete_structure (structure);
1412 return ASN1_DER_ERROR;
1414 p2 = p2->down;
1415 while (p2)
1417 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
1419 if (type_field (p2->type) != TYPE_CHOICE)
1420 ris =
1421 _asn1_extract_tag_der (p2, der + counter,
1422 len - counter, &len2);
1423 else
1425 p3 = p2->down;
1426 while (p3)
1428 ris =
1429 _asn1_extract_tag_der (p3, der + counter,
1430 len - counter, &len2);
1431 if (ris == ASN1_SUCCESS)
1432 break;
1433 p3 = p3->right;
1436 if (ris == ASN1_SUCCESS)
1438 p2->type &= ~CONST_NOT_USED;
1439 p = p2;
1440 break;
1443 p2 = p2->right;
1445 if (p2 == NULL)
1447 asn1_delete_structure (structure);
1448 return ASN1_DER_ERROR;
1452 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1454 p2 = _asn1_find_up (p);
1455 len2 = strtol (p2->value, NULL, 10);
1456 if (counter == len2)
1458 if (p->right)
1460 p2 = p->right;
1461 move = RIGHT;
1463 else
1464 move = UP;
1466 if (p->type & CONST_OPTION)
1467 asn1_delete_structure (&p);
1469 p = p2;
1470 continue;
1474 if (type_field (p->type) == TYPE_CHOICE)
1476 while (p->down)
1478 if (counter < len)
1479 ris =
1480 _asn1_extract_tag_der (p->down, der + counter,
1481 len - counter, &len2);
1482 else
1483 ris = ASN1_DER_ERROR;
1484 if (ris == ASN1_SUCCESS)
1486 while (p->down->right)
1488 p2 = p->down->right;
1489 asn1_delete_structure (&p2);
1491 break;
1493 else if (ris == ASN1_ERROR_TYPE_ANY)
1495 asn1_delete_structure (structure);
1496 return ASN1_ERROR_TYPE_ANY;
1498 else
1500 p2 = p->down;
1501 asn1_delete_structure (&p2);
1505 if (p->down == NULL)
1507 if (!(p->type & CONST_OPTION))
1509 asn1_delete_structure (structure);
1510 return ASN1_DER_ERROR;
1513 else
1514 p = p->down;
1517 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1519 p2 = _asn1_find_up (p);
1520 len2 = strtol (p2->value, NULL, 10);
1521 if (counter > len2)
1522 ris = ASN1_TAG_ERROR;
1525 if (ris == ASN1_SUCCESS)
1526 ris =
1527 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
1528 if (ris != ASN1_SUCCESS)
1530 if (p->type & CONST_OPTION)
1532 p->type |= CONST_NOT_USED;
1533 move = RIGHT;
1535 else if (p->type & CONST_DEFAULT)
1537 _asn1_set_value (p, NULL, 0);
1538 move = RIGHT;
1540 else
1542 if (errorDescription != NULL)
1543 _asn1_error_description_tag_error (p, errorDescription);
1545 asn1_delete_structure (structure);
1546 return ASN1_TAG_ERROR;
1549 else
1550 counter += len2;
1553 if (ris == ASN1_SUCCESS)
1555 switch (type_field (p->type))
1557 case TYPE_NULL:
1558 if (der[counter])
1560 asn1_delete_structure (structure);
1561 return ASN1_DER_ERROR;
1564 if (p == nodeFound)
1565 state = EXIT;
1567 counter++;
1568 move = RIGHT;
1569 break;
1570 case TYPE_BOOLEAN:
1571 if (der[counter++] != 1)
1573 asn1_delete_structure (structure);
1574 return ASN1_DER_ERROR;
1577 if (state == FOUND)
1579 if (der[counter++] == 0)
1580 _asn1_set_value (p, "F", 1);
1581 else
1582 _asn1_set_value (p, "T", 1);
1584 if (p == nodeFound)
1585 state = EXIT;
1588 else
1589 counter++;
1591 move = RIGHT;
1592 break;
1593 case TYPE_INTEGER:
1594 case TYPE_ENUMERATED:
1595 len2 =
1596 asn1_get_length_der (der + counter, len - counter, &len3);
1597 if (len2 < 0)
1598 return ASN1_DER_ERROR;
1599 if (state == FOUND)
1601 if (len3 + len2 > len - counter)
1602 return ASN1_DER_ERROR;
1603 _asn1_set_value (p, der + counter, len3 + len2);
1605 if (p == nodeFound)
1606 state = EXIT;
1608 counter += len3 + len2;
1609 move = RIGHT;
1610 break;
1611 case TYPE_OBJECT_ID:
1612 if (state == FOUND)
1614 _asn1_get_objectid_der (der + counter, len - counter, &len2,
1615 temp, sizeof (temp));
1616 tlen = strlen (temp);
1618 if (tlen > 0)
1619 _asn1_set_value (p, temp, tlen + 1);
1621 if (p == nodeFound)
1622 state = EXIT;
1624 else
1626 len2 =
1627 asn1_get_length_der (der + counter, len - counter, &len3);
1628 if (len2 < 0)
1629 return ASN1_DER_ERROR;
1630 len2 += len3;
1633 counter += len2;
1634 move = RIGHT;
1635 break;
1636 case TYPE_TIME:
1637 if (state == FOUND)
1639 result =
1640 _asn1_get_time_der (der + counter, len - counter, &len2,
1641 temp, sizeof (temp) - 1);
1642 if (result != ASN1_SUCCESS)
1644 asn1_delete_structure (structure);
1645 return result;
1648 tlen = strlen (temp);
1649 if (tlen > 0)
1650 _asn1_set_value (p, temp, tlen + 1);
1652 if (p == nodeFound)
1653 state = EXIT;
1655 else
1657 len2 =
1658 asn1_get_length_der (der + counter, len - counter, &len3);
1659 if (len2 < 0)
1660 return ASN1_DER_ERROR;
1661 len2 += len3;
1664 counter += len2;
1665 move = RIGHT;
1666 break;
1667 case TYPE_OCTET_STRING:
1668 len3 = len - counter;
1669 if (state == FOUND)
1671 ris = _asn1_get_octet_string (der + counter, p, &len3);
1672 if (p == nodeFound)
1673 state = EXIT;
1675 else
1676 ris = _asn1_get_octet_string (der + counter, NULL, &len3);
1678 if (ris != ASN1_SUCCESS)
1679 return ris;
1680 counter += len3;
1681 move = RIGHT;
1682 break;
1683 case TYPE_GENERALSTRING:
1684 len2 =
1685 asn1_get_length_der (der + counter, len - counter, &len3);
1686 if (len2 < 0)
1687 return ASN1_DER_ERROR;
1688 if (state == FOUND)
1690 if (len3 + len2 > len - counter)
1691 return ASN1_DER_ERROR;
1692 _asn1_set_value (p, der + counter, len3 + len2);
1694 if (p == nodeFound)
1695 state = EXIT;
1697 counter += len3 + len2;
1698 move = RIGHT;
1699 break;
1700 case TYPE_BIT_STRING:
1701 len2 =
1702 asn1_get_length_der (der + counter, len - counter, &len3);
1703 if (len2 < 0)
1704 return ASN1_DER_ERROR;
1705 if (state == FOUND)
1707 if (len3 + len2 > len - counter)
1708 return ASN1_DER_ERROR;
1709 _asn1_set_value (p, der + counter, len3 + len2);
1711 if (p == nodeFound)
1712 state = EXIT;
1714 counter += len3 + len2;
1715 move = RIGHT;
1716 break;
1717 case TYPE_SEQUENCE:
1718 case TYPE_SET:
1719 if (move == UP)
1721 len2 = strtol (p->value, NULL, 10);
1722 _asn1_set_value (p, NULL, 0);
1723 if (len2 == -1)
1724 { /* indefinite length method */
1725 if ((der[counter]) || der[counter + 1])
1727 asn1_delete_structure (structure);
1728 return ASN1_DER_ERROR;
1730 counter += 2;
1732 else
1733 { /* definite length method */
1734 if (len2 != counter)
1736 asn1_delete_structure (structure);
1737 return ASN1_DER_ERROR;
1740 if (p == nodeFound)
1741 state = EXIT;
1742 move = RIGHT;
1744 else
1745 { /* move==DOWN || move==RIGHT */
1746 if (state == OTHER_BRANCH)
1748 len3 =
1749 asn1_get_length_der (der + counter, len - counter,
1750 &len2);
1751 if (len3 < 0)
1752 return ASN1_DER_ERROR;
1753 counter += len2 + len3;
1754 move = RIGHT;
1756 else
1757 { /* state==SAME_BRANCH or state==FOUND */
1758 len3 =
1759 asn1_get_length_der (der + counter, len - counter,
1760 &len2);
1761 if (len3 < 0)
1762 return ASN1_DER_ERROR;
1763 counter += len2;
1764 if (len3 > 0)
1766 _asn1_ltostr (counter + len3, temp);
1767 tlen = strlen (temp);
1769 if (tlen > 0)
1770 _asn1_set_value (p, temp, tlen + 1);
1771 move = DOWN;
1773 else if (len3 == 0)
1775 p2 = p->down;
1776 while (p2)
1778 if (type_field (p2->type) != TYPE_TAG)
1780 p3 = p2->right;
1781 asn1_delete_structure (&p2);
1782 p2 = p3;
1784 else
1785 p2 = p2->right;
1787 move = RIGHT;
1789 else
1790 { /* indefinite length method */
1791 _asn1_set_value (p, "-1", 3);
1792 move = DOWN;
1796 break;
1797 case TYPE_SEQUENCE_OF:
1798 case TYPE_SET_OF:
1799 if (move == UP)
1801 len2 = strtol (p->value, NULL, 10);
1802 if (len2 > counter)
1804 _asn1_append_sequence_set (p);
1805 p = p->down;
1806 while (p->right)
1807 p = p->right;
1808 move = RIGHT;
1809 continue;
1811 _asn1_set_value (p, NULL, 0);
1812 if (len2 != counter)
1814 asn1_delete_structure (structure);
1815 return ASN1_DER_ERROR;
1818 if (p == nodeFound)
1819 state = EXIT;
1821 else
1822 { /* move==DOWN || move==RIGHT */
1823 if (state == OTHER_BRANCH)
1825 len3 =
1826 asn1_get_length_der (der + counter, len - counter,
1827 &len2);
1828 if (len3 < 0)
1829 return ASN1_DER_ERROR;
1830 counter += len2 + len3;
1831 move = RIGHT;
1833 else
1834 { /* state==FOUND or state==SAME_BRANCH */
1835 len3 =
1836 asn1_get_length_der (der + counter, len - counter,
1837 &len2);
1838 if (len3 < 0)
1839 return ASN1_DER_ERROR;
1840 counter += len2;
1841 if (len3)
1843 _asn1_ltostr (counter + len3, temp);
1844 tlen = strlen (temp);
1846 if (tlen > 0)
1847 _asn1_set_value (p, temp, tlen + 1);
1848 p2 = p->down;
1849 while ((type_field (p2->type) == TYPE_TAG)
1850 || (type_field (p2->type) == TYPE_SIZE))
1851 p2 = p2->right;
1852 if (p2->right == NULL)
1853 _asn1_append_sequence_set (p);
1854 p = p2;
1855 state = FOUND;
1860 break;
1861 case TYPE_ANY:
1862 if (asn1_get_tag_der
1863 (der + counter, len - counter, &class, &len2,
1864 &tag) != ASN1_SUCCESS)
1865 return ASN1_DER_ERROR;
1866 if (counter + len2 > len)
1867 return ASN1_DER_ERROR;
1869 len4 =
1870 asn1_get_length_der (der + counter + len2,
1871 len - counter - len2, &len3);
1872 if (len4 < -1)
1873 return ASN1_DER_ERROR;
1875 if (len4 != -1)
1877 len2 += len4;
1878 if (state == FOUND)
1880 asn1_length_der (len2 + len3, NULL, &len4);
1881 temp2 =
1882 (unsigned char *) _asn1_alloca (len2 + len3 + len4);
1883 if (temp2 == NULL)
1885 asn1_delete_structure (structure);
1886 return ASN1_MEM_ALLOC_ERROR;
1889 asn1_octet_der (der + counter, len2 + len3, temp2,
1890 &len4);
1891 _asn1_set_value (p, temp2, len4);
1892 _asn1_afree (temp2);
1894 if (p == nodeFound)
1895 state = EXIT;
1897 counter += len2 + len3;
1899 else
1900 { /* indefinite length */
1901 /* Check indefinite lenth method in an EXPLICIT TAG */
1902 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1903 indefinite = 1;
1904 else
1905 indefinite = 0;
1907 len2 = len - counter;
1908 ris =
1909 _asn1_get_indefinite_length_string (der + counter, &len2);
1910 if (ris != ASN1_SUCCESS)
1912 asn1_delete_structure (structure);
1913 return ris;
1916 if (state == FOUND)
1918 asn1_length_der (len2, NULL, &len4);
1919 temp2 = (unsigned char *) _asn1_alloca (len2 + len4);
1920 if (temp2 == NULL)
1922 asn1_delete_structure (structure);
1923 return ASN1_MEM_ALLOC_ERROR;
1926 asn1_octet_der (der + counter, len2, temp2, &len4);
1927 _asn1_set_value (p, temp2, len4);
1928 _asn1_afree (temp2);
1930 if (p == nodeFound)
1931 state = EXIT;
1934 counter += len2;
1936 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1937 an indefinite length method. */
1938 if (indefinite)
1940 if (!der[counter] && !der[counter + 1])
1942 counter += 2;
1944 else
1946 asn1_delete_structure (structure);
1947 return ASN1_DER_ERROR;
1951 move = RIGHT;
1952 break;
1954 default:
1955 move = (move == UP) ? RIGHT : DOWN;
1956 break;
1960 if ((p == node && move != DOWN) || (state == EXIT))
1961 break;
1963 if (move == DOWN)
1965 if (p->down)
1967 p = p->down;
1969 if (state != FOUND)
1971 nameLen -= strlen (p->name) + 1;
1972 if (nameLen > 0)
1974 if (currentName[0])
1975 strcat (currentName, ".");
1976 strcat (currentName, p->name);
1978 else
1980 asn1_delete_structure (structure);
1981 return ASN1_MEM_ERROR;
1983 if (!(strcmp (currentName, elementName)))
1985 state = FOUND;
1986 nodeFound = p;
1988 else
1989 if (!memcmp
1990 (currentName, elementName, strlen (currentName)))
1991 state = SAME_BRANCH;
1992 else
1993 state = OTHER_BRANCH;
1996 else
1997 move = RIGHT;
2000 if ((move == RIGHT) && !(p->type & CONST_SET))
2002 if (p->right)
2004 p = p->right;
2006 if (state != FOUND)
2008 dot_p = char_p = currentName;
2009 while ((char_p = strchr (char_p, '.')))
2011 dot_p = char_p++;
2012 dot_p++;
2015 nameLen += strlen (currentName) - (dot_p - currentName);
2016 *dot_p = 0;
2018 nameLen -= strlen (p->name);
2019 if (nameLen > 0)
2020 strcat (currentName, p->name);
2021 else
2023 asn1_delete_structure (structure);
2024 return ASN1_MEM_ERROR;
2027 if (!(strcmp (currentName, elementName)))
2029 state = FOUND;
2030 nodeFound = p;
2032 else
2033 if (!memcmp
2034 (currentName, elementName, strlen (currentName)))
2035 state = SAME_BRANCH;
2036 else
2037 state = OTHER_BRANCH;
2040 else
2041 move = UP;
2044 if (move == UP)
2046 p = _asn1_find_up (p);
2048 if (state != FOUND)
2050 dot_p = char_p = currentName;
2051 while ((char_p = strchr (char_p, '.')))
2053 dot_p = char_p++;
2054 dot_p++;
2057 nameLen += strlen (currentName) - (dot_p - currentName);
2058 *dot_p = 0;
2060 if (!(strcmp (currentName, elementName)))
2062 state = FOUND;
2063 nodeFound = p;
2065 else
2066 if (!memcmp (currentName, elementName, strlen (currentName)))
2067 state = SAME_BRANCH;
2068 else
2069 state = OTHER_BRANCH;
2074 _asn1_delete_not_used (*structure);
2076 if (counter > len)
2078 asn1_delete_structure (structure);
2079 return ASN1_DER_ERROR;
2082 return ASN1_SUCCESS;
2088 * asn1_der_decoding_startEnd - Find the start and end point of an element in a DER encoding string.
2089 * @element: pointer to an ASN1 element
2090 * @ider: vector that contains the DER encoding.
2091 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1]
2092 * @name_element: an element of NAME structure.
2093 * @start: the position of the first byte of NAME_ELEMENT decoding
2094 * (@ider[*start])
2095 * @end: the position of the last byte of NAME_ELEMENT decoding
2096 * (@ider[*end])
2098 * Find the start and end point of an element in a DER encoding
2099 * string. I mean that if you have a der encoding and you have
2100 * already used the function "asn1_der_decoding" to fill a structure,
2101 * it may happen that you want to find the piece of string concerning
2102 * an element of the structure.
2104 * Example: the sequence "tbsCertificate" inside an X509 certificate.
2106 * Returns:
2108 * ASN1_SUCCESS: DER encoding OK.
2110 * ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE EMPTY or
2111 * NAME_ELEMENT is not a valid element.
2113 * ASN1_TAG_ERROR,ASN1_DER_ERROR: the der encoding doesn't match
2114 * the structure ELEMENT.
2117 asn1_retCode
2118 asn1_der_decoding_startEnd (ASN1_TYPE element, const void *ider, int len,
2119 const char *name_element, int *start, int *end)
2121 node_asn *node, *node_to_find, *p, *p2, *p3;
2122 int counter, len2, len3, len4, move, ris;
2123 unsigned char class;
2124 unsigned long tag;
2125 int indefinite;
2126 const unsigned char *der = ider;
2128 node = element;
2130 if (node == ASN1_TYPE_EMPTY)
2131 return ASN1_ELEMENT_NOT_FOUND;
2133 node_to_find = asn1_find_node (node, name_element);
2135 if (node_to_find == NULL)
2136 return ASN1_ELEMENT_NOT_FOUND;
2138 if (node_to_find == node)
2140 *start = 0;
2141 *end = len - 1;
2142 return ASN1_SUCCESS;
2145 if (node->type & CONST_OPTION)
2146 return ASN1_GENERIC_ERROR;
2148 counter = 0;
2149 move = DOWN;
2150 p = node;
2151 while (1)
2153 ris = ASN1_SUCCESS;
2155 if (move != UP)
2157 if (p->type & CONST_SET)
2159 p2 = _asn1_find_up (p);
2160 len2 = strtol (p2->value, NULL, 10);
2161 if (len2 == -1)
2163 if (!der[counter] && !der[counter + 1])
2165 p = p2;
2166 move = UP;
2167 counter += 2;
2168 continue;
2171 else if (counter == len2)
2173 p = p2;
2174 move = UP;
2175 continue;
2177 else if (counter > len2)
2178 return ASN1_DER_ERROR;
2179 p2 = p2->down;
2180 while (p2)
2182 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
2183 { /* CONTROLLARE */
2184 if (type_field (p2->type) != TYPE_CHOICE)
2185 ris =
2186 _asn1_extract_tag_der (p2, der + counter,
2187 len - counter, &len2);
2188 else
2190 p3 = p2->down;
2191 ris =
2192 _asn1_extract_tag_der (p3, der + counter,
2193 len - counter, &len2);
2195 if (ris == ASN1_SUCCESS)
2197 p2->type &= ~CONST_NOT_USED;
2198 p = p2;
2199 break;
2202 p2 = p2->right;
2204 if (p2 == NULL)
2205 return ASN1_DER_ERROR;
2208 if (p == node_to_find)
2209 *start = counter;
2211 if (type_field (p->type) == TYPE_CHOICE)
2213 p = p->down;
2214 ris =
2215 _asn1_extract_tag_der (p, der + counter, len - counter,
2216 &len2);
2217 if (p == node_to_find)
2218 *start = counter;
2221 if (ris == ASN1_SUCCESS)
2222 ris =
2223 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
2224 if (ris != ASN1_SUCCESS)
2226 if (p->type & CONST_OPTION)
2228 p->type |= CONST_NOT_USED;
2229 move = RIGHT;
2231 else if (p->type & CONST_DEFAULT)
2233 move = RIGHT;
2235 else
2237 return ASN1_TAG_ERROR;
2240 else
2241 counter += len2;
2244 if (ris == ASN1_SUCCESS)
2246 switch (type_field (p->type))
2248 case TYPE_NULL:
2249 if (der[counter])
2250 return ASN1_DER_ERROR;
2251 counter++;
2252 move = RIGHT;
2253 break;
2254 case TYPE_BOOLEAN:
2255 if (der[counter++] != 1)
2256 return ASN1_DER_ERROR;
2257 counter++;
2258 move = RIGHT;
2259 break;
2260 case TYPE_INTEGER:
2261 case TYPE_ENUMERATED:
2262 len2 =
2263 asn1_get_length_der (der + counter, len - counter, &len3);
2264 if (len2 < 0)
2265 return ASN1_DER_ERROR;
2266 counter += len3 + len2;
2267 move = RIGHT;
2268 break;
2269 case TYPE_OBJECT_ID:
2270 len2 =
2271 asn1_get_length_der (der + counter, len - counter, &len3);
2272 if (len2 < 0)
2273 return ASN1_DER_ERROR;
2274 counter += len2 + len3;
2275 move = RIGHT;
2276 break;
2277 case TYPE_TIME:
2278 len2 =
2279 asn1_get_length_der (der + counter, len - counter, &len3);
2280 if (len2 < 0)
2281 return ASN1_DER_ERROR;
2282 counter += len2 + len3;
2283 move = RIGHT;
2284 break;
2285 case TYPE_OCTET_STRING:
2286 len3 = len - counter;
2287 ris = _asn1_get_octet_string (der + counter, NULL, &len3);
2288 if (ris != ASN1_SUCCESS)
2289 return ris;
2290 counter += len3;
2291 move = RIGHT;
2292 break;
2293 case TYPE_GENERALSTRING:
2294 len2 =
2295 asn1_get_length_der (der + counter, len - counter, &len3);
2296 if (len2 < 0)
2297 return ASN1_DER_ERROR;
2298 counter += len3 + len2;
2299 move = RIGHT;
2300 break;
2301 case TYPE_BIT_STRING:
2302 len2 =
2303 asn1_get_length_der (der + counter, len - counter, &len3);
2304 if (len2 < 0)
2305 return ASN1_DER_ERROR;
2306 counter += len3 + len2;
2307 move = RIGHT;
2308 break;
2309 case TYPE_SEQUENCE:
2310 case TYPE_SET:
2311 if (move != UP)
2313 len3 =
2314 asn1_get_length_der (der + counter, len - counter, &len2);
2315 if (len3 < -1)
2316 return ASN1_DER_ERROR;
2317 counter += len2;
2318 if (len3 == 0)
2319 move = RIGHT;
2320 else
2321 move = DOWN;
2323 else
2325 if (!der[counter] && !der[counter + 1]) /* indefinite length method */
2326 counter += 2;
2327 move = RIGHT;
2329 break;
2330 case TYPE_SEQUENCE_OF:
2331 case TYPE_SET_OF:
2332 if (move != UP)
2334 len3 =
2335 asn1_get_length_der (der + counter, len - counter, &len2);
2336 if (len3 < -1)
2337 return ASN1_DER_ERROR;
2338 counter += len2;
2339 if ((len3 == -1) && !der[counter] && !der[counter + 1])
2340 counter += 2;
2341 else if (len3)
2343 p2 = p->down;
2344 while ((type_field (p2->type) == TYPE_TAG) ||
2345 (type_field (p2->type) == TYPE_SIZE))
2346 p2 = p2->right;
2347 p = p2;
2350 else
2352 if (!der[counter] && !der[counter + 1]) /* indefinite length method */
2353 counter += 2;
2355 move = RIGHT;
2356 break;
2357 case TYPE_ANY:
2358 if (asn1_get_tag_der
2359 (der + counter, len - counter, &class, &len2,
2360 &tag) != ASN1_SUCCESS)
2361 return ASN1_DER_ERROR;
2362 if (counter + len2 > len)
2363 return ASN1_DER_ERROR;
2365 len4 =
2366 asn1_get_length_der (der + counter + len2,
2367 len - counter - len2, &len3);
2368 if (len4 < -1)
2369 return ASN1_DER_ERROR;
2371 if (len4 != -1)
2373 counter += len2 + len4 + len3;
2375 else
2376 { /* indefinite length */
2377 /* Check indefinite lenth method in an EXPLICIT TAG */
2378 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
2379 indefinite = 1;
2380 else
2381 indefinite = 0;
2383 len2 = len - counter;
2384 ris =
2385 _asn1_get_indefinite_length_string (der + counter, &len2);
2386 if (ris != ASN1_SUCCESS)
2387 return ris;
2388 counter += len2;
2390 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2391 an indefinite length method. */
2392 if (indefinite)
2394 if (!der[counter] && !der[counter + 1])
2395 counter += 2;
2396 else
2397 return ASN1_DER_ERROR;
2400 move = RIGHT;
2401 break;
2402 default:
2403 move = (move == UP) ? RIGHT : DOWN;
2404 break;
2408 if ((p == node_to_find) && (move == RIGHT))
2410 *end = counter - 1;
2411 return ASN1_SUCCESS;
2414 if (p == node && move != DOWN)
2415 break;
2417 if (move == DOWN)
2419 if (p->down)
2420 p = p->down;
2421 else
2422 move = RIGHT;
2424 if ((move == RIGHT) && !(p->type & CONST_SET))
2426 if (p->right)
2427 p = p->right;
2428 else
2429 move = UP;
2431 if (move == UP)
2432 p = _asn1_find_up (p);
2435 return ASN1_ELEMENT_NOT_FOUND;
2440 * asn1_expand_any_defined_by - Expand "ANY DEFINED BY" fields in structure.
2441 * @definitions: ASN1 definitions
2442 * @element: pointer to an ASN1 structure
2444 * Expands every "ANY DEFINED BY" element of a structure created from
2445 * a DER decoding process (asn1_der_decoding function). The element ANY
2446 * must be defined by an OBJECT IDENTIFIER. The type used to expand
2447 * the element ANY is the first one following the definition of
2448 * the actual value of the OBJECT IDENTIFIER.
2451 * Returns:
2453 * ASN1_SUCCESS: Substitution OK.
2455 * ASN1_ERROR_TYPE_ANY: Some "ANY DEFINED BY" element couldn't be
2456 * expanded due to a problem in OBJECT_ID -> TYPE association.
2458 * other errors: Result of der decoding process.
2461 asn1_retCode
2462 asn1_expand_any_defined_by (ASN1_TYPE definitions, ASN1_TYPE * element)
2464 char definitionsName[MAX_NAME_SIZE], name[2 * MAX_NAME_SIZE + 1],
2465 value[MAX_NAME_SIZE];
2466 asn1_retCode retCode = ASN1_SUCCESS, result;
2467 int len, len2, len3;
2468 ASN1_TYPE p, p2, p3, aux = ASN1_TYPE_EMPTY;
2469 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
2471 if ((definitions == ASN1_TYPE_EMPTY) || (*element == ASN1_TYPE_EMPTY))
2472 return ASN1_ELEMENT_NOT_FOUND;
2474 strcpy (definitionsName, definitions->name);
2475 strcat (definitionsName, ".");
2477 p = *element;
2478 while (p)
2481 switch (type_field (p->type))
2483 case TYPE_ANY:
2484 if ((p->type & CONST_DEFINED_BY) && (p->value))
2486 /* search the "DEF_BY" element */
2487 p2 = p->down;
2488 while ((p2) && (type_field (p2->type) != TYPE_CONSTANT))
2489 p2 = p2->right;
2491 if (!p2)
2493 retCode = ASN1_ERROR_TYPE_ANY;
2494 break;
2497 p3 = _asn1_find_up (p);
2499 if (!p3)
2501 retCode = ASN1_ERROR_TYPE_ANY;
2502 break;
2505 p3 = p3->down;
2506 while (p3)
2508 if ((p3->name) && !(strcmp (p3->name, p2->name)))
2509 break;
2510 p3 = p3->right;
2513 if ((!p3) || (type_field (p3->type) != TYPE_OBJECT_ID) ||
2514 (p3->value == NULL))
2517 p3 = _asn1_find_up (p);
2518 p3 = _asn1_find_up (p3);
2520 if (!p3)
2522 retCode = ASN1_ERROR_TYPE_ANY;
2523 break;
2526 p3 = p3->down;
2528 while (p3)
2530 if ((p3->name) && !(strcmp (p3->name, p2->name)))
2531 break;
2532 p3 = p3->right;
2535 if ((!p3) || (type_field (p3->type) != TYPE_OBJECT_ID) ||
2536 (p3->value == NULL))
2538 retCode = ASN1_ERROR_TYPE_ANY;
2539 break;
2543 /* search the OBJECT_ID into definitions */
2544 p2 = definitions->down;
2545 while (p2)
2547 if ((type_field (p2->type) == TYPE_OBJECT_ID) &&
2548 (p2->type & CONST_ASSIGN))
2550 strcpy (name, definitionsName);
2551 strcat (name, p2->name);
2553 len = MAX_NAME_SIZE;
2554 result =
2555 asn1_read_value (definitions, name, value, &len);
2557 if ((result == ASN1_SUCCESS)
2558 && (!strcmp (p3->value, value)))
2560 p2 = p2->right; /* pointer to the structure to
2561 use for expansion */
2562 while ((p2) && (p2->type & CONST_ASSIGN))
2563 p2 = p2->right;
2565 if (p2)
2567 strcpy (name, definitionsName);
2568 strcat (name, p2->name);
2570 result =
2571 asn1_create_element (definitions, name, &aux);
2572 if (result == ASN1_SUCCESS)
2574 _asn1_set_name (aux, p->name);
2575 len2 =
2576 asn1_get_length_der (p->value,
2577 p->value_len, &len3);
2578 if (len2 < 0)
2579 return ASN1_DER_ERROR;
2581 result =
2582 asn1_der_decoding (&aux, p->value + len3,
2583 len2,
2584 errorDescription);
2585 if (result == ASN1_SUCCESS)
2588 _asn1_set_right (aux, p->right);
2589 _asn1_set_right (p, aux);
2591 result = asn1_delete_structure (&p);
2592 if (result == ASN1_SUCCESS)
2594 p = aux;
2595 aux = ASN1_TYPE_EMPTY;
2596 break;
2598 else
2599 { /* error with asn1_delete_structure */
2600 asn1_delete_structure (&aux);
2601 retCode = result;
2602 break;
2605 else
2606 { /* error with asn1_der_decoding */
2607 retCode = result;
2608 break;
2611 else
2612 { /* error with asn1_create_element */
2613 retCode = result;
2614 break;
2617 else
2618 { /* error with the pointer to the structure to exapand */
2619 retCode = ASN1_ERROR_TYPE_ANY;
2620 break;
2624 p2 = p2->right;
2625 } /* end while */
2627 if (!p2)
2629 retCode = ASN1_ERROR_TYPE_ANY;
2630 break;
2634 break;
2635 default:
2636 break;
2640 if (p->down)
2642 p = p->down;
2644 else if (p == *element)
2646 p = NULL;
2647 break;
2649 else if (p->right)
2650 p = p->right;
2651 else
2653 while (1)
2655 p = _asn1_find_up (p);
2656 if (p == *element)
2658 p = NULL;
2659 break;
2661 if (p->right)
2663 p = p->right;
2664 break;
2670 return retCode;
2676 * asn1_expand_octet_string - Expand "OCTET STRING" fields in structure.
2677 * @definitions: ASN1 definitions
2678 * @element: pointer to an ASN1 structure
2679 * @octetName: name of the OCTECT STRING field to expand.
2680 * @objectName: name of the OBJECT IDENTIFIER field to use to define
2681 * the type for expansion.
2683 * Expands an "OCTET STRING" element of a structure created from a
2684 * DER decoding process (asn1_der_decoding function). The type used
2685 * for expansion is the first one following the definition of the
2686 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
2688 * Returns:
2690 * ASN1_SUCCESS: Substitution OK.
2692 * ASN1_ELEMENT_NOT_FOUND: OBJECTNAME or OCTETNAME are not correct.
2694 * ASN1_VALUE_NOT_VALID: Wasn't possible to find the type to use
2695 * for expansion.
2697 * other errors: result of der decoding process.
2699 asn1_retCode
2700 asn1_expand_octet_string (ASN1_TYPE definitions, ASN1_TYPE * element,
2701 const char *octetName, const char *objectName)
2703 char name[2 * MAX_NAME_SIZE + 1], value[MAX_NAME_SIZE];
2704 asn1_retCode retCode = ASN1_SUCCESS, result;
2705 int len, len2, len3;
2706 ASN1_TYPE p2, aux = ASN1_TYPE_EMPTY;
2707 ASN1_TYPE octetNode = ASN1_TYPE_EMPTY, objectNode = ASN1_TYPE_EMPTY;
2708 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
2710 if ((definitions == ASN1_TYPE_EMPTY) || (*element == ASN1_TYPE_EMPTY))
2711 return ASN1_ELEMENT_NOT_FOUND;
2713 octetNode = asn1_find_node (*element, octetName);
2714 if (octetNode == ASN1_TYPE_EMPTY)
2715 return ASN1_ELEMENT_NOT_FOUND;
2716 if (type_field (octetNode->type) != TYPE_OCTET_STRING)
2717 return ASN1_ELEMENT_NOT_FOUND;
2718 if (octetNode->value == NULL)
2719 return ASN1_VALUE_NOT_FOUND;
2721 objectNode = asn1_find_node (*element, objectName);
2722 if (objectNode == ASN1_TYPE_EMPTY)
2723 return ASN1_ELEMENT_NOT_FOUND;
2725 if (type_field (objectNode->type) != TYPE_OBJECT_ID)
2726 return ASN1_ELEMENT_NOT_FOUND;
2728 if (objectNode->value == NULL)
2729 return ASN1_VALUE_NOT_FOUND;
2732 /* search the OBJECT_ID into definitions */
2733 p2 = definitions->down;
2734 while (p2)
2736 if ((type_field (p2->type) == TYPE_OBJECT_ID) &&
2737 (p2->type & CONST_ASSIGN))
2739 strcpy (name, definitions->name);
2740 strcat (name, ".");
2741 strcat (name, p2->name);
2743 len = sizeof (value);
2744 result = asn1_read_value (definitions, name, value, &len);
2746 if ((result == ASN1_SUCCESS)
2747 && (!strcmp (objectNode->value, value)))
2750 p2 = p2->right; /* pointer to the structure to
2751 use for expansion */
2752 while ((p2) && (p2->type & CONST_ASSIGN))
2753 p2 = p2->right;
2755 if (p2)
2757 strcpy (name, definitions->name);
2758 strcat (name, ".");
2759 strcat (name, p2->name);
2761 result = asn1_create_element (definitions, name, &aux);
2762 if (result == ASN1_SUCCESS)
2764 _asn1_set_name (aux, octetNode->name);
2765 len2 =
2766 asn1_get_length_der (octetNode->value,
2767 octetNode->value_len, &len3);
2768 if (len2 < 0)
2769 return ASN1_DER_ERROR;
2771 result =
2772 asn1_der_decoding (&aux, octetNode->value + len3,
2773 len2, errorDescription);
2774 if (result == ASN1_SUCCESS)
2777 _asn1_set_right (aux, octetNode->right);
2778 _asn1_set_right (octetNode, aux);
2780 result = asn1_delete_structure (&octetNode);
2781 if (result == ASN1_SUCCESS)
2783 aux = ASN1_TYPE_EMPTY;
2784 break;
2786 else
2787 { /* error with asn1_delete_structure */
2788 asn1_delete_structure (&aux);
2789 retCode = result;
2790 break;
2793 else
2794 { /* error with asn1_der_decoding */
2795 retCode = result;
2796 break;
2799 else
2800 { /* error with asn1_create_element */
2801 retCode = result;
2802 break;
2805 else
2806 { /* error with the pointer to the structure to exapand */
2807 retCode = ASN1_VALUE_NOT_VALID;
2808 break;
2813 p2 = p2->right;
2817 if (!p2)
2818 retCode = ASN1_VALUE_NOT_VALID;
2820 return retCode;