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
24 /*****************************************************/
25 /* File: decoding.c */
26 /* Description: Functions to manage DER decoding */
27 /*****************************************************/
31 #include "parser_aux.h"
33 #include "structure.h"
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
, "'");
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.
60 asn1_get_length_der (const unsigned char *der
, int der_len
, int *len
)
81 { /* definite length method */
83 while (punt
<= k
&& punt
< der_len
)
85 unsigned long last
= ans
;
87 ans
= ans
* 256 + der
[punt
++];
89 /* we wrapped around, no bignum support... */
94 { /* indefinite length method */
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
)
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)
139 while (punt
<= der_len
&& der
[punt
] & 128)
142 ris
= ris
* 128 + (der
[punt
++] & 0x7F);
144 /* wrapper around, and no bignums... */
145 return ASN1_DER_ERROR
;
148 return ASN1_DER_ERROR
;
151 ris
= ris
* 128 + (der
[punt
++] & 0x7F);
153 /* wrapper around, and no bignums... */
154 return ASN1_DER_ERROR
;
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
,
187 return ASN1_GENERIC_ERROR
;
189 /* if(str==NULL) return ASN1_SUCCESS; */
190 *str_len
= asn1_get_length_der (der
, der_len
, &len_len
);
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
);
200 return ASN1_MEM_ERROR
;
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
);
223 *ret_len
= str_len
+ len_len
;
231 _asn1_get_objectid_der (const unsigned char *der
, int der_len
, int *ret_len
,
232 char *str
, int str_size
)
236 unsigned long val
, val1
;
239 if (str
&& str_size
> 0)
240 str
[0] = 0; /* no oid */
242 if (str
== NULL
|| der_len
<= 0)
244 len
= asn1_get_length_der (der
, der_len
, &len_len
);
246 if (len
< 0 || len
> der_len
|| len_len
> der_len
)
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
));
257 for (k
= 1; k
< len
; k
++)
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
));
268 *ret_len
= len
+ len_len
;
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
,
292 int len_len
, len_byte
;
295 return ASN1_GENERIC_ERROR
;
296 len_byte
= asn1_get_length_der (der
, der_len
, &len_len
) - 1;
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
);
307 return ASN1_MEM_ERROR
;
317 _asn1_extract_tag_der (node_asn
* node
, const unsigned char *der
, int der_len
,
321 int counter
, len2
, len3
, is_tag_implicit
;
322 unsigned long tag
, tag_implicit
= 0;
323 unsigned char class, class2
, class_implicit
= 0;
326 return ASN1_GENERIC_ERROR
;
328 counter
= is_tag_implicit
= 0;
330 if (node
->type
& CONST_TAG
)
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
;
344 class2
= ASN1_CLASS_CONTEXT_SPECIFIC
;
346 if (p
->type
& CONST_EXPLICIT
)
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
;
356 asn1_get_length_der (der
+ counter
, der_len
- counter
,
359 return ASN1_DER_ERROR
;
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
;
368 { /* ASN1_TAG_IMPLICIT */
369 if ((class != class_implicit
) || (tag
!= tag_implicit
))
370 return ASN1_TAG_ERROR
;
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);
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
;
412 return ASN1_TAG_ERROR
;
417 if (type_field (node
->type
) == TYPE_TAG
)
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
))
434 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_NULL
))
435 return ASN1_DER_ERROR
;
438 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_BOOLEAN
))
439 return ASN1_DER_ERROR
;
442 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_INTEGER
))
443 return ASN1_DER_ERROR
;
445 case TYPE_ENUMERATED
:
446 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_ENUMERATED
))
447 return ASN1_DER_ERROR
;
450 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_OBJECT_ID
))
451 return ASN1_DER_ERROR
;
454 if (node
->type
& CONST_UTC
)
456 if ((class != ASN1_CLASS_UNIVERSAL
)
457 || (tag
!= ASN1_TAG_UTCTime
))
458 return ASN1_DER_ERROR
;
462 if ((class != ASN1_CLASS_UNIVERSAL
)
463 || (tag
!= ASN1_TAG_GENERALIZEDTime
))
464 return ASN1_DER_ERROR
;
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
;
473 case TYPE_GENERALSTRING
:
474 if ((class != ASN1_CLASS_UNIVERSAL
)
475 || (tag
!= ASN1_TAG_GENERALSTRING
))
476 return ASN1_DER_ERROR
;
478 case TYPE_BIT_STRING
:
479 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_BIT_STRING
))
480 return ASN1_DER_ERROR
;
483 case TYPE_SEQUENCE_OF
:
484 if ((class != (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
))
485 || (tag
!= ASN1_TAG_SEQUENCE
))
486 return ASN1_DER_ERROR
;
490 if ((class != (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
))
491 || (tag
!= ASN1_TAG_SET
))
492 return ASN1_DER_ERROR
;
498 return ASN1_DER_ERROR
;
510 _asn1_delete_not_used (node_asn
* node
)
515 return ASN1_ELEMENT_NOT_FOUND
;
520 if (p
->type
& CONST_NOT_USED
)
525 p2
= _asn1_find_left (p
);
527 p2
= _asn1_find_up (p
);
529 asn1_delete_structure (&p
);
534 break; /* reach node */
550 p
= _asn1_find_up (p
);
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
;
577 if (*(der
- 1) & ASN1_CLASS_STRUCTURED
)
580 indefinite
= asn1_get_length_der (der
, *len
, &len3
);
582 return ASN1_DER_ERROR
;
590 if (counter
> (*len
))
591 return ASN1_DER_ERROR
;
593 if (indefinite
== -1)
595 if ((der
[counter
] == 0) && (der
[counter
+ 1] == 0))
601 else if (counter
>= indefinite
)
604 if (der
[counter
] != ASN1_TAG_OCTET_STRING
)
605 return ASN1_DER_ERROR
;
609 len2
= asn1_get_length_der (der
+ counter
, *len
- counter
, &len3
);
611 return ASN1_DER_ERROR
;
613 counter
+= len3
+ len2
;
620 asn1_length_der (tot_len
, NULL
, &len2
);
621 temp
= _asn1_alloca (len2
+ tot_len
);
624 return ASN1_MEM_ALLOC_ERROR
;
627 asn1_length_der (tot_len
, temp
, &len2
);
630 len2
= asn1_get_length_der (der
, *len
, &len3
);
632 return ASN1_DER_ERROR
;
635 if (indefinite
== -1)
636 counter_end
= counter
- 2;
638 counter_end
= counter
;
640 while (counter2
< counter_end
)
643 asn1_get_length_der (der
+ counter2
, *len
- counter
, &len3
);
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
);
652 counter2
+= len2
+ len3
+ 1;
655 _asn1_set_value (node
, temp
, tot_len
);
660 { /* NOT STRUCTURED */
661 len2
= asn1_get_length_der (der
, *len
, &len3
);
663 return ASN1_DER_ERROR
;
664 if (len3
+ len2
> *len
)
665 return ASN1_DER_ERROR
;
667 _asn1_set_value (node
, der
, len3
+ len2
);
668 counter
= len3
+ len2
;
678 _asn1_get_indefinite_length_string (const unsigned char *der
, int *len
)
680 int len2
, len3
, counter
, indefinite
;
684 counter
= indefinite
= 0;
688 if ((*len
) < counter
)
689 return ASN1_DER_ERROR
;
691 if ((der
[counter
] == 0) && (der
[counter
+ 1] == 0))
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
;
708 len2
= asn1_get_length_der (der
+ counter
, *len
- counter
, &len3
);
710 return ASN1_DER_ERROR
;
718 counter
+= len2
+ len3
;
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
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
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.
753 asn1_der_decoding (ASN1_TYPE
* element
, const void *ider
, int len
,
754 char *errorDescription
)
756 node_asn
*node
, *p
, *p2
, *p3
;
758 int counter
, len2
, len3
, len4
, move
, ris
, tlen
;
759 unsigned char class, *temp2
;
761 int indefinite
, result
;
762 const unsigned char *der
= ider
;
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
;
783 if (p
->type
& CONST_SET
)
785 p2
= _asn1_find_up (p
);
786 len2
= strtol (p2
->value
, NULL
, 10);
789 if (!der
[counter
] && !der
[counter
+ 1])
797 else if (counter
== len2
)
803 else if (counter
> len2
)
805 asn1_delete_structure (element
);
806 return ASN1_DER_ERROR
;
811 if ((p2
->type
& CONST_SET
) && (p2
->type
& CONST_NOT_USED
))
813 if (type_field (p2
->type
) != TYPE_CHOICE
)
815 _asn1_extract_tag_der (p2
, der
+ counter
,
816 len
- counter
, &len2
);
823 _asn1_extract_tag_der (p3
, der
+ counter
,
824 len
- counter
, &len2
);
825 if (ris
== ASN1_SUCCESS
)
830 if (ris
== ASN1_SUCCESS
)
832 p2
->type
&= ~CONST_NOT_USED
;
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);
860 if (p
->type
& CONST_OPTION
)
861 asn1_delete_structure (&p
);
868 if (type_field (p
->type
) == TYPE_CHOICE
)
874 _asn1_extract_tag_der (p
->down
, der
+ counter
,
875 len
- counter
, &len2
);
877 ris
= ASN1_DER_ERROR
;
878 if (ris
== ASN1_SUCCESS
)
880 while (p
->down
->right
)
883 asn1_delete_structure (&p2
);
887 else if (ris
== ASN1_ERROR_TYPE_ANY
)
889 asn1_delete_structure (element
);
890 return ASN1_ERROR_TYPE_ANY
;
895 asn1_delete_structure (&p2
);
901 if (!(p
->type
& CONST_OPTION
))
903 asn1_delete_structure (element
);
904 return ASN1_DER_ERROR
;
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
)
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
;
929 else if (p
->type
& CONST_DEFAULT
)
931 _asn1_set_value (p
, NULL
, 0);
936 if (errorDescription
!= NULL
)
937 _asn1_error_description_tag_error (p
, errorDescription
);
939 asn1_delete_structure (element
);
940 return ASN1_TAG_ERROR
;
947 if (ris
== ASN1_SUCCESS
)
949 switch (type_field (p
->type
))
954 asn1_delete_structure (element
);
955 return ASN1_DER_ERROR
;
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);
969 _asn1_set_value (p
, "T", 1);
973 case TYPE_ENUMERATED
:
975 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
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
;
985 _asn1_get_objectid_der (der
+ counter
, len
- counter
, &len2
,
986 temp
, sizeof (temp
));
987 tlen
= strlen (temp
);
989 _asn1_set_value (p
, temp
, tlen
+ 1);
995 _asn1_get_time_der (der
+ counter
, len
- counter
, &len2
, temp
,
997 if (result
!= ASN1_SUCCESS
)
999 asn1_delete_structure (element
);
1002 tlen
= strlen (temp
);
1004 _asn1_set_value (p
, temp
, tlen
+ 1);
1008 case TYPE_OCTET_STRING
:
1009 len3
= len
- counter
;
1010 ris
= _asn1_get_octet_string (der
+ counter
, p
, &len3
);
1011 if (ris
!= ASN1_SUCCESS
)
1016 case TYPE_GENERALSTRING
:
1018 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
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
;
1027 case TYPE_BIT_STRING
:
1029 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
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
;
1042 len2
= strtol (p
->value
, NULL
, 10);
1043 _asn1_set_value (p
, NULL
, 0);
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
;
1055 return ASN1_DER_ERROR
;
1059 { /* definite length method */
1060 if (len2
!= counter
)
1062 asn1_delete_structure (element
);
1063 return ASN1_DER_ERROR
;
1069 { /* move==DOWN || move==RIGHT */
1071 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
1073 return ASN1_DER_ERROR
;
1077 _asn1_ltostr (counter
+ len3
, temp
);
1078 tlen
= strlen (temp
);
1080 _asn1_set_value (p
, temp
, tlen
+ 1);
1088 if (type_field (p2
->type
) != TYPE_TAG
)
1091 asn1_delete_structure (&p2
);
1100 { /* indefinite length method */
1101 _asn1_set_value (p
, "-1", 3);
1106 case TYPE_SEQUENCE_OF
:
1110 len2
= strtol (p
->value
, NULL
, 10);
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
);
1124 _asn1_set_value (p
, NULL
, 0);
1128 { /* definite length method */
1131 _asn1_append_sequence_set (p
);
1138 _asn1_set_value (p
, NULL
, 0);
1139 if (len2
!= counter
)
1141 asn1_delete_structure (element
);
1142 return ASN1_DER_ERROR
;
1147 { /* move==DOWN || move==RIGHT */
1149 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
1151 return ASN1_DER_ERROR
;
1156 { /* definite length method */
1157 _asn1_ltostr (counter
+ len3
, temp
);
1158 tlen
= strlen (temp
);
1161 _asn1_set_value (p
, temp
, tlen
+ 1);
1164 { /* indefinite length method */
1165 _asn1_set_value (p
, "-1", 3);
1168 while ((type_field (p2
->type
) == TYPE_TAG
)
1169 || (type_field (p2
->type
) == TYPE_SIZE
))
1171 if (p2
->right
== NULL
)
1172 _asn1_append_sequence_set (p
);
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
;
1186 asn1_get_length_der (der
+ counter
+ len2
,
1187 len
- counter
- len2
, &len3
);
1189 return ASN1_DER_ERROR
;
1190 if (len4
> len
- counter
+ len2
+ len3
)
1191 return ASN1_DER_ERROR
;
1195 asn1_length_der (len2
+ len3
, NULL
, &len4
);
1196 temp2
= (unsigned char *) _asn1_alloca (len2
+ len3
+ len4
);
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
;
1209 { /* indefinite length */
1210 /* Check indefinite lenth method in an EXPLICIT TAG */
1211 if ((p
->type
& CONST_TAG
) && (der
[counter
- 1] == 0x80))
1216 len2
= len
- counter
;
1218 _asn1_get_indefinite_length_string (der
+ counter
, &len2
);
1219 if (ris
!= ASN1_SUCCESS
)
1221 asn1_delete_structure (element
);
1224 asn1_length_der (len2
, NULL
, &len4
);
1225 temp2
= (unsigned char *) _asn1_alloca (len2
+ len4
);
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
);
1237 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1238 an indefinite length method. */
1241 if (!der
[counter
] && !der
[counter
+ 1])
1247 asn1_delete_structure (element
);
1248 return ASN1_DER_ERROR
;
1255 move
= (move
== UP
) ? RIGHT
: DOWN
;
1260 if (p
== node
&& move
!= DOWN
)
1270 if ((move
== RIGHT
) && !(p
->type
& CONST_SET
))
1278 p
= _asn1_find_up (p
);
1281 _asn1_delete_not_used (*element
);
1285 asn1_delete_structure (element
);
1286 return ASN1_DER_ERROR
;
1289 return ASN1_SUCCESS
;
1294 #define SAME_BRANCH 2
1295 #define OTHER_BRANCH 3
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
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
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.
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
;
1335 int indefinite
, result
;
1336 const unsigned char *der
= ider
;
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
);
1359 strcpy (currentName
, (*structure
)->name
);
1362 asn1_delete_structure (structure
);
1363 return ASN1_MEM_ERROR
;
1365 if (!(strcmp (currentName
, elementName
)))
1368 nodeFound
= *structure
;
1370 else if (!memcmp (currentName
, elementName
, strlen (currentName
)))
1371 state
= SAME_BRANCH
;
1373 state
= OTHER_BRANCH
;
1376 { /* *structure doesn't have a name? */
1378 if (elementName
[0] == 0)
1381 nodeFound
= *structure
;
1385 state
= SAME_BRANCH
;
1399 if (p
->type
& CONST_SET
)
1401 p2
= _asn1_find_up (p
);
1402 len2
= strtol (p2
->value
, NULL
, 10);
1403 if (counter
== len2
)
1409 else if (counter
> len2
)
1411 asn1_delete_structure (structure
);
1412 return ASN1_DER_ERROR
;
1417 if ((p2
->type
& CONST_SET
) && (p2
->type
& CONST_NOT_USED
))
1419 if (type_field (p2
->type
) != TYPE_CHOICE
)
1421 _asn1_extract_tag_der (p2
, der
+ counter
,
1422 len
- counter
, &len2
);
1429 _asn1_extract_tag_der (p3
, der
+ counter
,
1430 len
- counter
, &len2
);
1431 if (ris
== ASN1_SUCCESS
)
1436 if (ris
== ASN1_SUCCESS
)
1438 p2
->type
&= ~CONST_NOT_USED
;
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
)
1466 if (p
->type
& CONST_OPTION
)
1467 asn1_delete_structure (&p
);
1474 if (type_field (p
->type
) == TYPE_CHOICE
)
1480 _asn1_extract_tag_der (p
->down
, der
+ counter
,
1481 len
- counter
, &len2
);
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
);
1493 else if (ris
== ASN1_ERROR_TYPE_ANY
)
1495 asn1_delete_structure (structure
);
1496 return ASN1_ERROR_TYPE_ANY
;
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
;
1517 if ((p
->type
& CONST_OPTION
) || (p
->type
& CONST_DEFAULT
))
1519 p2
= _asn1_find_up (p
);
1520 len2
= strtol (p2
->value
, NULL
, 10);
1522 ris
= ASN1_TAG_ERROR
;
1525 if (ris
== ASN1_SUCCESS
)
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
;
1535 else if (p
->type
& CONST_DEFAULT
)
1537 _asn1_set_value (p
, NULL
, 0);
1542 if (errorDescription
!= NULL
)
1543 _asn1_error_description_tag_error (p
, errorDescription
);
1545 asn1_delete_structure (structure
);
1546 return ASN1_TAG_ERROR
;
1553 if (ris
== ASN1_SUCCESS
)
1555 switch (type_field (p
->type
))
1560 asn1_delete_structure (structure
);
1561 return ASN1_DER_ERROR
;
1571 if (der
[counter
++] != 1)
1573 asn1_delete_structure (structure
);
1574 return ASN1_DER_ERROR
;
1579 if (der
[counter
++] == 0)
1580 _asn1_set_value (p
, "F", 1);
1582 _asn1_set_value (p
, "T", 1);
1594 case TYPE_ENUMERATED
:
1596 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1598 return ASN1_DER_ERROR
;
1601 if (len3
+ len2
> len
- counter
)
1602 return ASN1_DER_ERROR
;
1603 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1608 counter
+= len3
+ len2
;
1611 case TYPE_OBJECT_ID
:
1614 _asn1_get_objectid_der (der
+ counter
, len
- counter
, &len2
,
1615 temp
, sizeof (temp
));
1616 tlen
= strlen (temp
);
1619 _asn1_set_value (p
, temp
, tlen
+ 1);
1627 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1629 return ASN1_DER_ERROR
;
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
);
1648 tlen
= strlen (temp
);
1650 _asn1_set_value (p
, temp
, tlen
+ 1);
1658 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1660 return ASN1_DER_ERROR
;
1667 case TYPE_OCTET_STRING
:
1668 len3
= len
- counter
;
1671 ris
= _asn1_get_octet_string (der
+ counter
, p
, &len3
);
1676 ris
= _asn1_get_octet_string (der
+ counter
, NULL
, &len3
);
1678 if (ris
!= ASN1_SUCCESS
)
1683 case TYPE_GENERALSTRING
:
1685 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1687 return ASN1_DER_ERROR
;
1690 if (len3
+ len2
> len
- counter
)
1691 return ASN1_DER_ERROR
;
1692 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1697 counter
+= len3
+ len2
;
1700 case TYPE_BIT_STRING
:
1702 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1704 return ASN1_DER_ERROR
;
1707 if (len3
+ len2
> len
- counter
)
1708 return ASN1_DER_ERROR
;
1709 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1714 counter
+= len3
+ len2
;
1721 len2
= strtol (p
->value
, NULL
, 10);
1722 _asn1_set_value (p
, NULL
, 0);
1724 { /* indefinite length method */
1725 if ((der
[counter
]) || der
[counter
+ 1])
1727 asn1_delete_structure (structure
);
1728 return ASN1_DER_ERROR
;
1733 { /* definite length method */
1734 if (len2
!= counter
)
1736 asn1_delete_structure (structure
);
1737 return ASN1_DER_ERROR
;
1745 { /* move==DOWN || move==RIGHT */
1746 if (state
== OTHER_BRANCH
)
1749 asn1_get_length_der (der
+ counter
, len
- counter
,
1752 return ASN1_DER_ERROR
;
1753 counter
+= len2
+ len3
;
1757 { /* state==SAME_BRANCH or state==FOUND */
1759 asn1_get_length_der (der
+ counter
, len
- counter
,
1762 return ASN1_DER_ERROR
;
1766 _asn1_ltostr (counter
+ len3
, temp
);
1767 tlen
= strlen (temp
);
1770 _asn1_set_value (p
, temp
, tlen
+ 1);
1778 if (type_field (p2
->type
) != TYPE_TAG
)
1781 asn1_delete_structure (&p2
);
1790 { /* indefinite length method */
1791 _asn1_set_value (p
, "-1", 3);
1797 case TYPE_SEQUENCE_OF
:
1801 len2
= strtol (p
->value
, NULL
, 10);
1804 _asn1_append_sequence_set (p
);
1811 _asn1_set_value (p
, NULL
, 0);
1812 if (len2
!= counter
)
1814 asn1_delete_structure (structure
);
1815 return ASN1_DER_ERROR
;
1822 { /* move==DOWN || move==RIGHT */
1823 if (state
== OTHER_BRANCH
)
1826 asn1_get_length_der (der
+ counter
, len
- counter
,
1829 return ASN1_DER_ERROR
;
1830 counter
+= len2
+ len3
;
1834 { /* state==FOUND or state==SAME_BRANCH */
1836 asn1_get_length_der (der
+ counter
, len
- counter
,
1839 return ASN1_DER_ERROR
;
1843 _asn1_ltostr (counter
+ len3
, temp
);
1844 tlen
= strlen (temp
);
1847 _asn1_set_value (p
, temp
, tlen
+ 1);
1849 while ((type_field (p2
->type
) == TYPE_TAG
)
1850 || (type_field (p2
->type
) == TYPE_SIZE
))
1852 if (p2
->right
== NULL
)
1853 _asn1_append_sequence_set (p
);
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
;
1870 asn1_get_length_der (der
+ counter
+ len2
,
1871 len
- counter
- len2
, &len3
);
1873 return ASN1_DER_ERROR
;
1880 asn1_length_der (len2
+ len3
, NULL
, &len4
);
1882 (unsigned char *) _asn1_alloca (len2
+ len3
+ len4
);
1885 asn1_delete_structure (structure
);
1886 return ASN1_MEM_ALLOC_ERROR
;
1889 asn1_octet_der (der
+ counter
, len2
+ len3
, temp2
,
1891 _asn1_set_value (p
, temp2
, len4
);
1892 _asn1_afree (temp2
);
1897 counter
+= len2
+ len3
;
1900 { /* indefinite length */
1901 /* Check indefinite lenth method in an EXPLICIT TAG */
1902 if ((p
->type
& CONST_TAG
) && (der
[counter
- 1] == 0x80))
1907 len2
= len
- counter
;
1909 _asn1_get_indefinite_length_string (der
+ counter
, &len2
);
1910 if (ris
!= ASN1_SUCCESS
)
1912 asn1_delete_structure (structure
);
1918 asn1_length_der (len2
, NULL
, &len4
);
1919 temp2
= (unsigned char *) _asn1_alloca (len2
+ len4
);
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
);
1936 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1937 an indefinite length method. */
1940 if (!der
[counter
] && !der
[counter
+ 1])
1946 asn1_delete_structure (structure
);
1947 return ASN1_DER_ERROR
;
1955 move
= (move
== UP
) ? RIGHT
: DOWN
;
1960 if ((p
== node
&& move
!= DOWN
) || (state
== EXIT
))
1971 nameLen
-= strlen (p
->name
) + 1;
1975 strcat (currentName
, ".");
1976 strcat (currentName
, p
->name
);
1980 asn1_delete_structure (structure
);
1981 return ASN1_MEM_ERROR
;
1983 if (!(strcmp (currentName
, elementName
)))
1990 (currentName
, elementName
, strlen (currentName
)))
1991 state
= SAME_BRANCH
;
1993 state
= OTHER_BRANCH
;
2000 if ((move
== RIGHT
) && !(p
->type
& CONST_SET
))
2008 dot_p
= char_p
= currentName
;
2009 while ((char_p
= strchr (char_p
, '.')))
2015 nameLen
+= strlen (currentName
) - (dot_p
- currentName
);
2018 nameLen
-= strlen (p
->name
);
2020 strcat (currentName
, p
->name
);
2023 asn1_delete_structure (structure
);
2024 return ASN1_MEM_ERROR
;
2027 if (!(strcmp (currentName
, elementName
)))
2034 (currentName
, elementName
, strlen (currentName
)))
2035 state
= SAME_BRANCH
;
2037 state
= OTHER_BRANCH
;
2046 p
= _asn1_find_up (p
);
2050 dot_p
= char_p
= currentName
;
2051 while ((char_p
= strchr (char_p
, '.')))
2057 nameLen
+= strlen (currentName
) - (dot_p
- currentName
);
2060 if (!(strcmp (currentName
, elementName
)))
2066 if (!memcmp (currentName
, elementName
, strlen (currentName
)))
2067 state
= SAME_BRANCH
;
2069 state
= OTHER_BRANCH
;
2074 _asn1_delete_not_used (*structure
);
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
2095 * @end: the position of the last byte of NAME_ELEMENT decoding
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.
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.
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;
2126 const unsigned char *der
= ider
;
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
)
2142 return ASN1_SUCCESS
;
2145 if (node
->type
& CONST_OPTION
)
2146 return ASN1_GENERIC_ERROR
;
2157 if (p
->type
& CONST_SET
)
2159 p2
= _asn1_find_up (p
);
2160 len2
= strtol (p2
->value
, NULL
, 10);
2163 if (!der
[counter
] && !der
[counter
+ 1])
2171 else if (counter
== len2
)
2177 else if (counter
> len2
)
2178 return ASN1_DER_ERROR
;
2182 if ((p2
->type
& CONST_SET
) && (p2
->type
& CONST_NOT_USED
))
2184 if (type_field (p2
->type
) != TYPE_CHOICE
)
2186 _asn1_extract_tag_der (p2
, der
+ counter
,
2187 len
- counter
, &len2
);
2192 _asn1_extract_tag_der (p3
, der
+ counter
,
2193 len
- counter
, &len2
);
2195 if (ris
== ASN1_SUCCESS
)
2197 p2
->type
&= ~CONST_NOT_USED
;
2205 return ASN1_DER_ERROR
;
2208 if (p
== node_to_find
)
2211 if (type_field (p
->type
) == TYPE_CHOICE
)
2215 _asn1_extract_tag_der (p
, der
+ counter
, len
- counter
,
2217 if (p
== node_to_find
)
2221 if (ris
== ASN1_SUCCESS
)
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
;
2231 else if (p
->type
& CONST_DEFAULT
)
2237 return ASN1_TAG_ERROR
;
2244 if (ris
== ASN1_SUCCESS
)
2246 switch (type_field (p
->type
))
2250 return ASN1_DER_ERROR
;
2255 if (der
[counter
++] != 1)
2256 return ASN1_DER_ERROR
;
2261 case TYPE_ENUMERATED
:
2263 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2265 return ASN1_DER_ERROR
;
2266 counter
+= len3
+ len2
;
2269 case TYPE_OBJECT_ID
:
2271 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2273 return ASN1_DER_ERROR
;
2274 counter
+= len2
+ len3
;
2279 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2281 return ASN1_DER_ERROR
;
2282 counter
+= len2
+ len3
;
2285 case TYPE_OCTET_STRING
:
2286 len3
= len
- counter
;
2287 ris
= _asn1_get_octet_string (der
+ counter
, NULL
, &len3
);
2288 if (ris
!= ASN1_SUCCESS
)
2293 case TYPE_GENERALSTRING
:
2295 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2297 return ASN1_DER_ERROR
;
2298 counter
+= len3
+ len2
;
2301 case TYPE_BIT_STRING
:
2303 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2305 return ASN1_DER_ERROR
;
2306 counter
+= len3
+ len2
;
2314 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
2316 return ASN1_DER_ERROR
;
2325 if (!der
[counter
] && !der
[counter
+ 1]) /* indefinite length method */
2330 case TYPE_SEQUENCE_OF
:
2335 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
2337 return ASN1_DER_ERROR
;
2339 if ((len3
== -1) && !der
[counter
] && !der
[counter
+ 1])
2344 while ((type_field (p2
->type
) == TYPE_TAG
) ||
2345 (type_field (p2
->type
) == TYPE_SIZE
))
2352 if (!der
[counter
] && !der
[counter
+ 1]) /* indefinite length method */
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
;
2366 asn1_get_length_der (der
+ counter
+ len2
,
2367 len
- counter
- len2
, &len3
);
2369 return ASN1_DER_ERROR
;
2373 counter
+= len2
+ len4
+ len3
;
2376 { /* indefinite length */
2377 /* Check indefinite lenth method in an EXPLICIT TAG */
2378 if ((p
->type
& CONST_TAG
) && (der
[counter
- 1] == 0x80))
2383 len2
= len
- counter
;
2385 _asn1_get_indefinite_length_string (der
+ counter
, &len2
);
2386 if (ris
!= ASN1_SUCCESS
)
2390 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2391 an indefinite length method. */
2394 if (!der
[counter
] && !der
[counter
+ 1])
2397 return ASN1_DER_ERROR
;
2403 move
= (move
== UP
) ? RIGHT
: DOWN
;
2408 if ((p
== node_to_find
) && (move
== RIGHT
))
2411 return ASN1_SUCCESS
;
2414 if (p
== node
&& move
!= DOWN
)
2424 if ((move
== RIGHT
) && !(p
->type
& CONST_SET
))
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.
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.
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
, ".");
2481 switch (type_field (p
->type
))
2484 if ((p
->type
& CONST_DEFINED_BY
) && (p
->value
))
2486 /* search the "DEF_BY" element */
2488 while ((p2
) && (type_field (p2
->type
) != TYPE_CONSTANT
))
2493 retCode
= ASN1_ERROR_TYPE_ANY
;
2497 p3
= _asn1_find_up (p
);
2501 retCode
= ASN1_ERROR_TYPE_ANY
;
2508 if ((p3
->name
) && !(strcmp (p3
->name
, p2
->name
)))
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
);
2522 retCode
= ASN1_ERROR_TYPE_ANY
;
2530 if ((p3
->name
) && !(strcmp (p3
->name
, p2
->name
)))
2535 if ((!p3
) || (type_field (p3
->type
) != TYPE_OBJECT_ID
) ||
2536 (p3
->value
== NULL
))
2538 retCode
= ASN1_ERROR_TYPE_ANY
;
2543 /* search the OBJECT_ID into definitions */
2544 p2
= definitions
->down
;
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
;
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
))
2567 strcpy (name
, definitionsName
);
2568 strcat (name
, p2
->name
);
2571 asn1_create_element (definitions
, name
, &aux
);
2572 if (result
== ASN1_SUCCESS
)
2574 _asn1_set_name (aux
, p
->name
);
2576 asn1_get_length_der (p
->value
,
2577 p
->value_len
, &len3
);
2579 return ASN1_DER_ERROR
;
2582 asn1_der_decoding (&aux
, p
->value
+ len3
,
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
)
2595 aux
= ASN1_TYPE_EMPTY
;
2599 { /* error with asn1_delete_structure */
2600 asn1_delete_structure (&aux
);
2606 { /* error with asn1_der_decoding */
2612 { /* error with asn1_create_element */
2618 { /* error with the pointer to the structure to exapand */
2619 retCode
= ASN1_ERROR_TYPE_ANY
;
2629 retCode
= ASN1_ERROR_TYPE_ANY
;
2644 else if (p
== *element
)
2655 p
= _asn1_find_up (p
);
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.
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
2697 * other errors: result of der decoding process.
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
;
2736 if ((type_field (p2
->type
) == TYPE_OBJECT_ID
) &&
2737 (p2
->type
& CONST_ASSIGN
))
2739 strcpy (name
, definitions
->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
))
2757 strcpy (name
, definitions
->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
);
2766 asn1_get_length_der (octetNode
->value
,
2767 octetNode
->value_len
, &len3
);
2769 return ASN1_DER_ERROR
;
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
;
2787 { /* error with asn1_delete_structure */
2788 asn1_delete_structure (&aux
);
2794 { /* error with asn1_der_decoding */
2800 { /* error with asn1_create_element */
2806 { /* error with the pointer to the structure to exapand */
2807 retCode
= ASN1_VALUE_NOT_VALID
;
2818 retCode
= ASN1_VALUE_NOT_VALID
;