2 * Copyright (C) 2002-2012 Free Software Foundation, Inc.
4 * This file is part of LIBTASN1.
6 * The LIBTASN1 library is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 /*****************************************************/
24 /* File: decoding.c */
25 /* Description: Functions to manage DER decoding */
26 /*****************************************************/
29 #include "parser_aux.h"
31 #include "structure.h"
36 _asn1_get_indefinite_length_string (const unsigned char *der
, int *len
);
39 _asn1_error_description_tag_error (asn1_node node
, char *ErrorDescription
)
42 Estrcpy (ErrorDescription
, ":: tag error near element '");
43 _asn1_hierarchical_name (node
, ErrorDescription
+ strlen (ErrorDescription
),
44 ASN1_MAX_ERROR_DESCRIPTION_SIZE
- 40);
45 Estrcat (ErrorDescription
, "'");
50 * asn1_get_length_der:
51 * @der: DER data to decode.
52 * @der_len: Length of DER data to decode.
53 * @len: Output variable containing the length of the DER length field.
55 * Extract a length field from DER data.
57 * Returns: Return the decoded length value, or -1 on indefinite
58 * length, or -2 when the value was too big to fit in a int, or -4
59 * when the decoded length value plus @len would exceed @der_len.
62 asn1_get_length_der (const unsigned char *der
, int der_len
, int *len
)
64 unsigned int ans
, sum
, last
;
83 { /* definite length method */
85 while (punt
<= k
&& punt
< der_len
)
89 ans
= (ans
*256) + der
[punt
++];
91 /* we wrapped around, no bignum support... */
96 { /* indefinite length method */
106 /* check for overflow as well INT_MAX as a maximum upper
107 * limit for length */
108 if (sum
>= INT_MAX
|| sum
< ans
)
111 if (((int) sum
) > der_len
)
119 * @der: DER data to decode.
120 * @der_len: Length of DER data to decode.
121 * @cls: Output variable containing decoded class.
122 * @len: Output variable containing the length of the DER TAG data.
123 * @tag: Output variable containing the decoded tag.
125 * Decode the class and TAG from DER code.
127 * Returns: Returns %ASN1_SUCCESS on success, or an error.
130 asn1_get_tag_der (const unsigned char *der
, int der_len
,
131 unsigned char *cls
, int *len
, unsigned long *tag
)
137 if (der
== NULL
|| der_len
< 2 || len
== NULL
)
138 return ASN1_DER_ERROR
;
140 *cls
= der
[0] & 0xE0;
141 if ((der
[0] & 0x1F) != 0x1F)
152 while (punt
<= der_len
&& der
[punt
] & 128)
156 ris
= (ris
* 128) + (der
[punt
++] & 0x7F);
158 /* wrapped around, and no bignums... */
159 return ASN1_DER_ERROR
;
163 return ASN1_DER_ERROR
;
167 ris
= (ris
* 128) + (der
[punt
++] & 0x7F);
169 return ASN1_DER_ERROR
;
179 * asn1_get_length_ber:
180 * @ber: BER data to decode.
181 * @ber_len: Length of BER data to decode.
182 * @len: Output variable containing the length of the BER length field.
184 * Extract a length field from BER data. The difference to
185 * asn1_get_length_der() is that this function will return a length
186 * even if the value has indefinite encoding.
188 * Returns: Return the decoded length value, or negative value when
189 * the value was too big.
194 asn1_get_length_ber (const unsigned char *ber
, int ber_len
, int *len
)
199 ret
= asn1_get_length_der (ber
, ber_len
, len
);
201 { /* indefinite length method */
203 err
= _asn1_get_indefinite_length_string (ber
+ 1, &ret
);
204 if (err
!= ASN1_SUCCESS
)
212 * asn1_get_octet_der:
213 * @der: DER data to decode containing the OCTET SEQUENCE.
214 * @der_len: Length of DER data to decode.
215 * @ret_len: Output variable containing the length of the DER data.
216 * @str: Pre-allocated output buffer to put decoded OCTET SEQUENCE in.
217 * @str_size: Length of pre-allocated output buffer.
218 * @str_len: Output variable containing the length of the OCTET SEQUENCE.
220 * Extract an OCTET SEQUENCE from DER data.
222 * Returns: Returns %ASN1_SUCCESS on success, or an error.
225 asn1_get_octet_der (const unsigned char *der
, int der_len
,
226 int *ret_len
, unsigned char *str
, int str_size
,
232 return ASN1_GENERIC_ERROR
;
234 /* if(str==NULL) return ASN1_SUCCESS; */
235 *str_len
= asn1_get_length_der (der
, der_len
, &len_len
);
238 return ASN1_DER_ERROR
;
240 *ret_len
= *str_len
+ len_len
;
241 if (str_size
>= *str_len
)
242 memcpy (str
, der
+ len_len
, *str_len
);
245 return ASN1_MEM_ERROR
;
251 /* Returns ASN1_SUCCESS on success or an error code on error.
254 _asn1_get_time_der (const unsigned char *der
, int der_len
, int *ret_len
,
255 char *str
, int str_size
)
257 int len_len
, str_len
;
259 if (der_len
<= 0 || str
== NULL
)
260 return ASN1_DER_ERROR
;
261 str_len
= asn1_get_length_der (der
, der_len
, &len_len
);
262 if (str_len
< 0 || str_size
< str_len
)
263 return ASN1_DER_ERROR
;
264 memcpy (str
, der
+ len_len
, str_len
);
266 *ret_len
= str_len
+ len_len
;
272 _asn1_get_objectid_der (const unsigned char *der
, int der_len
, int *ret_len
,
273 char *str
, int str_size
)
278 unsigned long val
, val1
, prev_val
;
281 if (str
&& str_size
> 0)
282 str
[0] = 0; /* no oid */
284 if (str
== NULL
|| der_len
<= 0)
285 return ASN1_GENERIC_ERROR
;
286 len
= asn1_get_length_der (der
, der_len
, &len_len
);
288 if (len
< 0 || len
> der_len
|| len_len
> der_len
)
289 return ASN1_DER_ERROR
;
291 val1
= der
[len_len
] / 40;
292 val
= der
[len_len
] - val1
* 40;
294 _asn1_str_cpy (str
, str_size
, _asn1_ltostr (val1
, temp
));
295 _asn1_str_cat (str
, str_size
, ".");
296 _asn1_str_cat (str
, str_size
, _asn1_ltostr (val
, temp
));
301 for (k
= 1; k
< len
; k
++)
303 /* X.690 mandates that the leading byte must never be 0x80
305 if (leading
!= 0 && der
[len_len
+ k
] == 0x80)
306 return ASN1_DER_ERROR
;
309 /* check for wrap around */
311 val
|= der
[len_len
+ k
] & 0x7F;
314 return ASN1_DER_ERROR
;
318 if (!(der
[len_len
+ k
] & 0x80))
320 _asn1_str_cat (str
, str_size
, ".");
321 _asn1_str_cat (str
, str_size
, _asn1_ltostr (val
, temp
));
327 *ret_len
= len
+ len_len
;
334 * @der: DER data to decode containing the BIT SEQUENCE.
335 * @der_len: Length of DER data to decode.
336 * @ret_len: Output variable containing the length of the DER data.
337 * @str: Pre-allocated output buffer to put decoded BIT SEQUENCE in.
338 * @str_size: Length of pre-allocated output buffer.
339 * @bit_len: Output variable containing the size of the BIT SEQUENCE.
341 * Extract a BIT SEQUENCE from DER data.
343 * Returns: Return %ASN1_SUCCESS on success, or an error.
346 asn1_get_bit_der (const unsigned char *der
, int der_len
,
347 int *ret_len
, unsigned char *str
, int str_size
,
350 int len_len
, len_byte
;
353 return ASN1_GENERIC_ERROR
;
354 len_byte
= asn1_get_length_der (der
, der_len
, &len_len
) - 1;
356 return ASN1_DER_ERROR
;
358 *ret_len
= len_byte
+ len_len
+ 1;
359 *bit_len
= len_byte
* 8 - der
[len_len
];
361 if (str_size
>= len_byte
)
362 memcpy (str
, der
+ len_len
+ 1, len_byte
);
365 return ASN1_MEM_ERROR
;
372 _asn1_extract_tag_der (asn1_node node
, const unsigned char *der
, int der_len
,
376 int counter
, len2
, len3
, is_tag_implicit
;
377 unsigned long tag
, tag_implicit
= 0;
378 unsigned char class, class2
, class_implicit
= 0;
381 return ASN1_GENERIC_ERROR
;
383 counter
= is_tag_implicit
= 0;
385 if (node
->type
& CONST_TAG
)
390 if (type_field (p
->type
) == TYPE_TAG
)
392 if (p
->type
& CONST_APPLICATION
)
393 class2
= ASN1_CLASS_APPLICATION
;
394 else if (p
->type
& CONST_UNIVERSAL
)
395 class2
= ASN1_CLASS_UNIVERSAL
;
396 else if (p
->type
& CONST_PRIVATE
)
397 class2
= ASN1_CLASS_PRIVATE
;
399 class2
= ASN1_CLASS_CONTEXT_SPECIFIC
;
401 if (p
->type
& CONST_EXPLICIT
)
404 (der
+ counter
, der_len
- counter
, &class, &len2
,
405 &tag
) != ASN1_SUCCESS
)
406 return ASN1_DER_ERROR
;
408 if (counter
+ len2
> der_len
)
409 return ASN1_DER_ERROR
;
413 asn1_get_length_ber (der
+ counter
, der_len
- counter
,
416 return ASN1_DER_ERROR
;
419 if (counter
> der_len
)
420 return ASN1_DER_ERROR
;
422 if (!is_tag_implicit
)
424 if ((class != (class2
| ASN1_CLASS_STRUCTURED
)) ||
425 (tag
!= strtoul ((char *) p
->value
, NULL
, 10)))
426 return ASN1_TAG_ERROR
;
429 { /* ASN1_TAG_IMPLICIT */
430 if ((class != class_implicit
) || (tag
!= tag_implicit
))
431 return ASN1_TAG_ERROR
;
436 { /* ASN1_TAG_IMPLICIT */
437 if (!is_tag_implicit
)
439 if ((type_field (node
->type
) == TYPE_SEQUENCE
) ||
440 (type_field (node
->type
) == TYPE_SEQUENCE_OF
) ||
441 (type_field (node
->type
) == TYPE_SET
) ||
442 (type_field (node
->type
) == TYPE_SET_OF
))
443 class2
|= ASN1_CLASS_STRUCTURED
;
444 class_implicit
= class2
;
445 tag_implicit
= strtoul ((char *) p
->value
, NULL
, 10);
457 (der
+ counter
, der_len
- counter
, &class, &len2
,
458 &tag
) != ASN1_SUCCESS
)
459 return ASN1_DER_ERROR
;
460 if (counter
+ len2
> der_len
)
461 return ASN1_DER_ERROR
;
463 if ((class != class_implicit
) || (tag
!= tag_implicit
))
465 if (type_field (node
->type
) == TYPE_OCTET_STRING
)
467 class_implicit
|= ASN1_CLASS_STRUCTURED
;
468 if ((class != class_implicit
) || (tag
!= tag_implicit
))
469 return ASN1_TAG_ERROR
;
472 return ASN1_TAG_ERROR
;
477 if (type_field (node
->type
) == TYPE_TAG
)
485 (der
+ counter
, der_len
- counter
, &class, &len2
,
486 &tag
) != ASN1_SUCCESS
)
487 return ASN1_DER_ERROR
;
489 if (counter
+ len2
> der_len
)
490 return ASN1_DER_ERROR
;
492 switch (type_field (node
->type
))
495 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_NULL
))
496 return ASN1_DER_ERROR
;
499 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_BOOLEAN
))
500 return ASN1_DER_ERROR
;
503 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_INTEGER
))
504 return ASN1_DER_ERROR
;
506 case TYPE_ENUMERATED
:
507 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_ENUMERATED
))
508 return ASN1_DER_ERROR
;
511 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_OBJECT_ID
))
512 return ASN1_DER_ERROR
;
515 if (node
->type
& CONST_UTC
)
517 if ((class != ASN1_CLASS_UNIVERSAL
)
518 || (tag
!= ASN1_TAG_UTCTime
))
519 return ASN1_DER_ERROR
;
523 if ((class != ASN1_CLASS_UNIVERSAL
)
524 || (tag
!= ASN1_TAG_GENERALIZEDTime
))
525 return ASN1_DER_ERROR
;
528 case TYPE_OCTET_STRING
:
529 if (((class != ASN1_CLASS_UNIVERSAL
)
530 && (class != (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
)))
531 || (tag
!= ASN1_TAG_OCTET_STRING
))
532 return ASN1_DER_ERROR
;
534 case TYPE_GENERALSTRING
:
535 if ((class != ASN1_CLASS_UNIVERSAL
)
536 || (tag
!= ASN1_TAG_GENERALSTRING
))
537 return ASN1_DER_ERROR
;
539 case TYPE_BIT_STRING
:
540 if ((class != ASN1_CLASS_UNIVERSAL
) || (tag
!= ASN1_TAG_BIT_STRING
))
541 return ASN1_DER_ERROR
;
544 case TYPE_SEQUENCE_OF
:
545 if ((class != (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
))
546 || (tag
!= ASN1_TAG_SEQUENCE
))
547 return ASN1_DER_ERROR
;
551 if ((class != (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
))
552 || (tag
!= ASN1_TAG_SET
))
553 return ASN1_DER_ERROR
;
559 return ASN1_DER_ERROR
;
570 _asn1_delete_not_used (asn1_node node
)
575 return ASN1_ELEMENT_NOT_FOUND
;
580 if (p
->type
& CONST_NOT_USED
)
585 p2
= _asn1_find_left (p
);
587 p2
= _asn1_find_up (p
);
589 asn1_delete_structure (&p
);
594 break; /* reach node */
610 p
= _asn1_find_up (p
);
629 _asn1_extract_der_octet (asn1_node node
, const unsigned char *der
,
633 int counter2
, counter_end
;
635 len2
= asn1_get_length_der (der
, der_len
, &len3
);
637 return ASN1_DER_ERROR
;
642 counter_end
= der_len
- 2;
644 counter_end
= der_len
;
646 while (counter2
< counter_end
)
648 len2
= asn1_get_length_der (der
+ counter2
, der_len
- counter2
, &len3
);
651 return ASN1_DER_ERROR
;
655 _asn1_append_value (node
, der
+ counter2
+ len3
, len2
);
661 _asn1_extract_der_octet (node
, der
+ counter2
+ len3
,
662 der_len
- counter2
- len3
);
667 counter2
+= len2
+ len3
+ 1;
674 _asn1_get_octet_string (const unsigned char *der
, asn1_node node
, int *len
)
676 int len2
, len3
, counter
, tot_len
, indefinite
;
680 if (*(der
- 1) & ASN1_CLASS_STRUCTURED
)
683 indefinite
= asn1_get_length_der (der
, *len
, &len3
);
685 return ASN1_DER_ERROR
;
693 if (counter
> (*len
))
694 return ASN1_DER_ERROR
;
696 if (indefinite
== -1)
698 if ((der
[counter
] == 0) && (der
[counter
+ 1] == 0))
704 else if (counter
>= indefinite
)
707 if (der
[counter
] != ASN1_TAG_OCTET_STRING
)
708 return ASN1_DER_ERROR
;
712 len2
= asn1_get_length_der (der
+ counter
, *len
- counter
, &len3
);
714 return ASN1_DER_ERROR
;
716 counter
+= len3
+ len2
;
723 unsigned char temp
[DER_LEN
];
726 len2
= sizeof (temp
);
728 asn1_length_der (tot_len
, temp
, &len2
);
729 _asn1_set_value (node
, temp
, len2
);
731 ret
= _asn1_extract_der_octet (node
, der
, *len
);
732 if (ret
!= ASN1_SUCCESS
)
738 { /* NOT STRUCTURED */
739 len2
= asn1_get_length_der (der
, *len
, &len3
);
741 return ASN1_DER_ERROR
;
743 counter
= len3
+ len2
;
745 _asn1_set_value (node
, der
, counter
);
754 _asn1_get_indefinite_length_string (const unsigned char *der
, int *len
)
756 int len2
, len3
, counter
, indefinite
;
760 counter
= indefinite
= 0;
764 if ((*len
) < counter
)
765 return ASN1_DER_ERROR
;
767 if ((der
[counter
] == 0) && (der
[counter
+ 1] == 0))
778 (der
+ counter
, *len
- counter
, &class, &len2
,
779 &tag
) != ASN1_SUCCESS
)
780 return ASN1_DER_ERROR
;
781 if (counter
+ len2
> *len
)
782 return ASN1_DER_ERROR
;
784 len2
= asn1_get_length_der (der
+ counter
, *len
- counter
, &len3
);
786 return ASN1_DER_ERROR
;
794 counter
+= len2
+ len3
;
805 * @element: pointer to an ASN1 structure.
806 * @ider: vector that contains the DER encoding.
807 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1].
808 * @errorDescription: null-terminated string contains details when an
811 * Fill the structure *@ELEMENT with values of a DER encoding
812 * string. The structure must just be created with function
813 * asn1_create_element(). If an error occurs during the decoding
814 * procedure, the *@ELEMENT is deleted and set equal to
817 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
818 * if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
819 * %ASN1_DER_ERROR if the der encoding doesn't match the structure
820 * name (*@ELEMENT deleted).
823 asn1_der_decoding (asn1_node
* element
, const void *ider
, int len
,
824 char *errorDescription
)
826 asn1_node node
, p
, p2
, p3
;
828 int counter
, len2
, len3
, len4
, move
, ris
, tlen
;
831 int indefinite
, result
;
832 const unsigned char *der
= ider
;
836 if (errorDescription
!= NULL
)
837 errorDescription
[0] = 0;
840 return ASN1_ELEMENT_NOT_FOUND
;
842 if (node
->type
& CONST_OPTION
)
844 result
= ASN1_GENERIC_ERROR
;
856 if (p
->type
& CONST_SET
)
858 p2
= _asn1_find_up (p
);
859 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
862 if (!der
[counter
] && !der
[counter
+ 1])
870 else if (counter
== len2
)
876 else if (counter
> len2
)
878 result
= ASN1_DER_ERROR
;
884 if ((p2
->type
& CONST_SET
) && (p2
->type
& CONST_NOT_USED
))
886 if (type_field (p2
->type
) != TYPE_CHOICE
)
888 _asn1_extract_tag_der (p2
, der
+ counter
,
889 len
- counter
, &len2
);
896 _asn1_extract_tag_der (p3
, der
+ counter
,
897 len
- counter
, &len2
);
898 if (ris
== ASN1_SUCCESS
)
903 if (ris
== ASN1_SUCCESS
)
905 p2
->type
&= ~CONST_NOT_USED
;
914 result
= ASN1_DER_ERROR
;
919 if ((p
->type
& CONST_OPTION
) || (p
->type
& CONST_DEFAULT
))
921 p2
= _asn1_find_up (p
);
922 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
933 if (p
->type
& CONST_OPTION
)
934 asn1_delete_structure (&p
);
941 if (type_field (p
->type
) == TYPE_CHOICE
)
947 _asn1_extract_tag_der (p
->down
, der
+ counter
,
948 len
- counter
, &len2
);
950 ris
= ASN1_DER_ERROR
;
951 if (ris
== ASN1_SUCCESS
)
953 while (p
->down
->right
)
956 asn1_delete_structure (&p2
);
960 else if (ris
== ASN1_ERROR_TYPE_ANY
)
962 result
= ASN1_ERROR_TYPE_ANY
;
968 asn1_delete_structure (&p2
);
974 if (!(p
->type
& CONST_OPTION
))
976 result
= ASN1_DER_ERROR
;
984 if ((p
->type
& CONST_OPTION
) || (p
->type
& CONST_DEFAULT
))
986 p2
= _asn1_find_up (p
);
987 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
988 if ((len2
!= -1) && (counter
> len2
))
989 ris
= ASN1_TAG_ERROR
;
992 if (ris
== ASN1_SUCCESS
)
994 _asn1_extract_tag_der (p
, der
+ counter
, len
- counter
, &len2
);
995 if (ris
!= ASN1_SUCCESS
)
997 if (p
->type
& CONST_OPTION
)
999 p
->type
|= CONST_NOT_USED
;
1002 else if (p
->type
& CONST_DEFAULT
)
1004 _asn1_set_value (p
, NULL
, 0);
1009 if (errorDescription
!= NULL
)
1010 _asn1_error_description_tag_error (p
, errorDescription
);
1012 result
= ASN1_TAG_ERROR
;
1020 if (ris
== ASN1_SUCCESS
)
1022 switch (type_field (p
->type
))
1027 result
= ASN1_DER_ERROR
;
1034 if (der
[counter
++] != 1)
1036 result
= ASN1_DER_ERROR
;
1039 if (der
[counter
++] == 0)
1040 _asn1_set_value (p
, "F", 1);
1042 _asn1_set_value (p
, "T", 1);
1046 case TYPE_ENUMERATED
:
1048 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1051 result
= ASN1_DER_ERROR
;
1055 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1056 counter
+= len3
+ len2
;
1059 case TYPE_OBJECT_ID
:
1061 _asn1_get_objectid_der (der
+ counter
, len
- counter
, &len2
,
1062 temp
, sizeof (temp
));
1063 if (result
!= ASN1_SUCCESS
)
1066 tlen
= strlen (temp
);
1068 _asn1_set_value (p
, temp
, tlen
+ 1);
1074 _asn1_get_time_der (der
+ counter
, len
- counter
, &len2
, temp
,
1076 if (result
!= ASN1_SUCCESS
)
1079 tlen
= strlen (temp
);
1081 _asn1_set_value (p
, temp
, tlen
+ 1);
1085 case TYPE_OCTET_STRING
:
1086 len3
= len
- counter
;
1087 result
= _asn1_get_octet_string (der
+ counter
, p
, &len3
);
1088 if (result
!= ASN1_SUCCESS
)
1094 case TYPE_GENERALSTRING
:
1096 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1099 result
= ASN1_DER_ERROR
;
1103 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1104 counter
+= len3
+ len2
;
1107 case TYPE_BIT_STRING
:
1109 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1112 result
= ASN1_DER_ERROR
;
1116 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1117 counter
+= len3
+ len2
;
1124 len2
= _asn1_strtol (p
->value
, NULL
, 10);
1125 _asn1_set_value (p
, NULL
, 0);
1127 { /* indefinite length method */
1128 if (len
- counter
+ 1 > 0)
1130 if ((der
[counter
]) || der
[counter
+ 1])
1132 result
= ASN1_DER_ERROR
;
1138 result
= ASN1_DER_ERROR
;
1144 { /* definite length method */
1145 if (len2
!= counter
)
1147 result
= ASN1_DER_ERROR
;
1154 { /* move==DOWN || move==RIGHT */
1156 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
1159 result
= ASN1_DER_ERROR
;
1165 _asn1_ltostr (counter
+ len3
, temp
);
1166 tlen
= strlen (temp
);
1168 _asn1_set_value (p
, temp
, tlen
+ 1);
1176 if (type_field (p2
->type
) != TYPE_TAG
)
1179 asn1_delete_structure (&p2
);
1188 { /* indefinite length method */
1189 _asn1_set_value (p
, "-1", 3);
1194 case TYPE_SEQUENCE_OF
:
1198 len2
= _asn1_strtol (p
->value
, NULL
, 10);
1200 { /* indefinite length method */
1201 if ((counter
+ 2) > len
)
1203 result
= ASN1_DER_ERROR
;
1207 if ((der
[counter
]) || der
[counter
+ 1])
1209 _asn1_append_sequence_set (p
);
1216 _asn1_set_value (p
, NULL
, 0);
1220 { /* definite length method */
1223 _asn1_append_sequence_set (p
);
1230 _asn1_set_value (p
, NULL
, 0);
1231 if (len2
!= counter
)
1233 result
= ASN1_DER_ERROR
;
1239 { /* move==DOWN || move==RIGHT */
1241 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
1244 result
= ASN1_DER_ERROR
;
1251 { /* definite length method */
1252 _asn1_ltostr (counter
+ len3
, temp
);
1253 tlen
= strlen (temp
);
1256 _asn1_set_value (p
, temp
, tlen
+ 1);
1259 { /* indefinite length method */
1260 _asn1_set_value (p
, "-1", 3);
1263 while ((type_field (p2
->type
) == TYPE_TAG
)
1264 || (type_field (p2
->type
) == TYPE_SIZE
))
1266 if (p2
->right
== NULL
)
1267 _asn1_append_sequence_set (p
);
1274 if (asn1_get_tag_der
1275 (der
+ counter
, len
- counter
, &class, &len2
,
1276 &tag
) != ASN1_SUCCESS
)
1278 result
= ASN1_DER_ERROR
;
1282 if (counter
+ len2
> len
)
1284 result
= ASN1_DER_ERROR
;
1288 asn1_get_length_der (der
+ counter
+ len2
,
1289 len
- counter
- len2
, &len3
);
1292 result
= ASN1_DER_ERROR
;
1298 _asn1_set_value_octet (p
, der
+ counter
, len2
+ len3
);
1299 counter
+= len2
+ len3
;
1302 { /* indefinite length */
1303 /* Check indefinite lenth method in an EXPLICIT TAG */
1304 if ((p
->type
& CONST_TAG
) && (der
[counter
- 1] == 0x80))
1309 len2
= len
- counter
;
1311 _asn1_get_indefinite_length_string (der
+ counter
, &len2
);
1312 if (result
!= ASN1_SUCCESS
)
1315 _asn1_set_value_octet (p
, der
+ counter
, len2
);
1318 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1319 an indefinite length method. */
1322 if (!der
[counter
] && !der
[counter
+ 1])
1328 result
= ASN1_DER_ERROR
;
1336 move
= (move
== UP
) ? RIGHT
: DOWN
;
1341 if (p
== node
&& move
!= DOWN
)
1351 if ((move
== RIGHT
) && !(p
->type
& CONST_SET
))
1359 p
= _asn1_find_up (p
);
1362 _asn1_delete_not_used (*element
);
1366 result
= ASN1_DER_ERROR
;
1370 return ASN1_SUCCESS
;
1373 asn1_delete_structure (element
);
1378 #define SAME_BRANCH 2
1379 #define OTHER_BRANCH 3
1383 * asn1_der_decoding_element:
1384 * @structure: pointer to an ASN1 structure
1385 * @elementName: name of the element to fill
1386 * @ider: vector that contains the DER encoding of the whole structure.
1387 * @len: number of bytes of *der: der[0]..der[len-1]
1388 * @errorDescription: null-terminated string contains details when an
1391 * Fill the element named @ELEMENTNAME with values of a DER encoding
1392 * string. The structure must just be created with function
1393 * asn1_create_element(). The DER vector must contain the encoding
1394 * string of the whole @STRUCTURE. If an error occurs during the
1395 * decoding procedure, the *@STRUCTURE is deleted and set equal to
1398 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1399 * if ELEMENT is %NULL or @elementName == NULL, and
1400 * %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding doesn't
1401 * match the structure @structure (*ELEMENT deleted).
1404 asn1_der_decoding_element (asn1_node
* structure
, const char *elementName
,
1405 const void *ider
, int len
, char *errorDescription
)
1407 asn1_node node
, p
, p2
, p3
, nodeFound
= NULL
;
1408 char temp
[128], currentName
[ASN1_MAX_NAME_SIZE
* 10], *dot_p
, *char_p
;
1409 int nameLen
= ASN1_MAX_NAME_SIZE
* 10 - 1, state
;
1410 int counter
, len2
, len3
, len4
, move
, ris
, tlen
;
1411 unsigned char class;
1413 int indefinite
, result
;
1414 const unsigned char *der
= ider
;
1419 return ASN1_ELEMENT_NOT_FOUND
;
1421 if (elementName
== NULL
)
1423 result
= ASN1_ELEMENT_NOT_FOUND
;
1427 if (node
->type
& CONST_OPTION
)
1429 result
= ASN1_GENERIC_ERROR
;
1433 if ((*structure
)->name
[0] != 0)
1434 { /* Has *structure got a name? */
1435 nameLen
-= strlen ((*structure
)->name
);
1437 strcpy (currentName
, (*structure
)->name
);
1440 result
= ASN1_MEM_ERROR
;
1443 if (!(strcmp (currentName
, elementName
)))
1446 nodeFound
= *structure
;
1448 else if (!memcmp (currentName
, elementName
, strlen (currentName
)))
1449 state
= SAME_BRANCH
;
1451 state
= OTHER_BRANCH
;
1454 { /* *structure doesn't have a name? */
1456 if (elementName
[0] == 0)
1459 nodeFound
= *structure
;
1463 state
= SAME_BRANCH
;
1477 if (p
->type
& CONST_SET
)
1479 p2
= _asn1_find_up (p
);
1480 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
1481 if (counter
== len2
)
1487 else if (counter
> len2
)
1489 result
= ASN1_DER_ERROR
;
1495 if ((p2
->type
& CONST_SET
) && (p2
->type
& CONST_NOT_USED
))
1497 if (type_field (p2
->type
) != TYPE_CHOICE
)
1499 _asn1_extract_tag_der (p2
, der
+ counter
,
1500 len
- counter
, &len2
);
1507 _asn1_extract_tag_der (p3
, der
+ counter
,
1508 len
- counter
, &len2
);
1509 if (ris
== ASN1_SUCCESS
)
1514 if (ris
== ASN1_SUCCESS
)
1516 p2
->type
&= ~CONST_NOT_USED
;
1525 result
= ASN1_DER_ERROR
;
1530 if ((p
->type
& CONST_OPTION
) || (p
->type
& CONST_DEFAULT
))
1532 p2
= _asn1_find_up (p
);
1533 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
1534 if (counter
== len2
)
1544 if (p
->type
& CONST_OPTION
)
1545 asn1_delete_structure (&p
);
1552 if (type_field (p
->type
) == TYPE_CHOICE
)
1558 _asn1_extract_tag_der (p
->down
, der
+ counter
,
1559 len
- counter
, &len2
);
1561 ris
= ASN1_DER_ERROR
;
1562 if (ris
== ASN1_SUCCESS
)
1564 while (p
->down
->right
)
1566 p2
= p
->down
->right
;
1567 asn1_delete_structure (&p2
);
1571 else if (ris
== ASN1_ERROR_TYPE_ANY
)
1573 result
= ASN1_ERROR_TYPE_ANY
;
1579 asn1_delete_structure (&p2
);
1583 if (p
->down
== NULL
)
1585 if (!(p
->type
& CONST_OPTION
))
1587 result
= ASN1_DER_ERROR
;
1595 if ((p
->type
& CONST_OPTION
) || (p
->type
& CONST_DEFAULT
))
1597 p2
= _asn1_find_up (p
);
1598 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
1600 ris
= ASN1_TAG_ERROR
;
1603 if (ris
== ASN1_SUCCESS
)
1605 _asn1_extract_tag_der (p
, der
+ counter
, len
- counter
, &len2
);
1606 if (ris
!= ASN1_SUCCESS
)
1608 if (p
->type
& CONST_OPTION
)
1610 p
->type
|= CONST_NOT_USED
;
1613 else if (p
->type
& CONST_DEFAULT
)
1615 _asn1_set_value (p
, NULL
, 0);
1620 if (errorDescription
!= NULL
)
1621 _asn1_error_description_tag_error (p
, errorDescription
);
1623 result
= ASN1_TAG_ERROR
;
1631 if (ris
== ASN1_SUCCESS
)
1633 switch (type_field (p
->type
))
1638 result
= ASN1_DER_ERROR
;
1649 if (der
[counter
++] != 1)
1651 result
= ASN1_DER_ERROR
;
1657 if (der
[counter
++] == 0)
1658 _asn1_set_value (p
, "F", 1);
1660 _asn1_set_value (p
, "T", 1);
1672 case TYPE_ENUMERATED
:
1674 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1677 result
= ASN1_DER_ERROR
;
1683 if (len3
+ len2
> len
- counter
)
1685 result
= ASN1_DER_ERROR
;
1688 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1693 counter
+= len3
+ len2
;
1696 case TYPE_OBJECT_ID
:
1700 _asn1_get_objectid_der (der
+ counter
, len
- counter
,
1701 &len2
, temp
, sizeof (temp
));
1702 if (result
!= ASN1_SUCCESS
)
1705 tlen
= strlen (temp
);
1708 _asn1_set_value (p
, temp
, tlen
+ 1);
1716 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1719 result
= ASN1_DER_ERROR
;
1732 _asn1_get_time_der (der
+ counter
, len
- counter
, &len2
,
1733 temp
, sizeof (temp
) - 1);
1734 if (result
!= ASN1_SUCCESS
)
1737 tlen
= strlen (temp
);
1739 _asn1_set_value (p
, temp
, tlen
+ 1);
1747 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1750 result
= ASN1_DER_ERROR
;
1759 case TYPE_OCTET_STRING
:
1760 len3
= len
- counter
;
1763 result
= _asn1_get_octet_string (der
+ counter
, p
, &len3
);
1768 result
= _asn1_get_octet_string (der
+ counter
, NULL
, &len3
);
1770 if (result
!= ASN1_SUCCESS
)
1776 case TYPE_GENERALSTRING
:
1778 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1781 result
= ASN1_DER_ERROR
;
1787 if (len3
+ len2
> len
- counter
)
1789 result
= ASN1_DER_ERROR
;
1792 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1797 counter
+= len3
+ len2
;
1800 case TYPE_BIT_STRING
:
1802 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
1805 result
= ASN1_DER_ERROR
;
1810 if (len3
+ len2
> len
- counter
)
1812 result
= ASN1_DER_ERROR
;
1815 _asn1_set_value (p
, der
+ counter
, len3
+ len2
);
1820 counter
+= len3
+ len2
;
1827 len2
= _asn1_strtol (p
->value
, NULL
, 10);
1828 _asn1_set_value (p
, NULL
, 0);
1830 { /* indefinite length method */
1831 if ((der
[counter
]) || der
[counter
+ 1])
1833 result
= ASN1_DER_ERROR
;
1839 { /* definite length method */
1840 if (len2
!= counter
)
1842 result
= ASN1_DER_ERROR
;
1851 { /* move==DOWN || move==RIGHT */
1852 if (state
== OTHER_BRANCH
)
1855 asn1_get_length_der (der
+ counter
, len
- counter
,
1859 result
= ASN1_DER_ERROR
;
1862 counter
+= len2
+ len3
;
1866 { /* state==SAME_BRANCH or state==FOUND */
1868 asn1_get_length_der (der
+ counter
, len
- counter
,
1872 result
= ASN1_DER_ERROR
;
1878 _asn1_ltostr (counter
+ len3
, temp
);
1879 tlen
= strlen (temp
);
1882 _asn1_set_value (p
, temp
, tlen
+ 1);
1890 if (type_field (p2
->type
) != TYPE_TAG
)
1893 asn1_delete_structure (&p2
);
1902 { /* indefinite length method */
1903 _asn1_set_value (p
, "-1", 3);
1909 case TYPE_SEQUENCE_OF
:
1913 len2
= _asn1_strtol (p
->value
, NULL
, 10);
1916 _asn1_append_sequence_set (p
);
1923 _asn1_set_value (p
, NULL
, 0);
1924 if (len2
!= counter
)
1926 result
= ASN1_DER_ERROR
;
1934 { /* move==DOWN || move==RIGHT */
1935 if (state
== OTHER_BRANCH
)
1938 asn1_get_length_der (der
+ counter
, len
- counter
,
1942 result
= ASN1_DER_ERROR
;
1945 counter
+= len2
+ len3
;
1949 { /* state==FOUND or state==SAME_BRANCH */
1951 asn1_get_length_der (der
+ counter
, len
- counter
,
1955 result
= ASN1_DER_ERROR
;
1961 _asn1_ltostr (counter
+ len3
, temp
);
1962 tlen
= strlen (temp
);
1965 _asn1_set_value (p
, temp
, tlen
+ 1);
1967 while ((type_field (p2
->type
) == TYPE_TAG
)
1968 || (type_field (p2
->type
) == TYPE_SIZE
))
1970 if (p2
->right
== NULL
)
1971 _asn1_append_sequence_set (p
);
1980 if (asn1_get_tag_der
1981 (der
+ counter
, len
- counter
, &class, &len2
,
1982 &tag
) != ASN1_SUCCESS
)
1984 result
= ASN1_DER_ERROR
;
1988 if (counter
+ len2
> len
)
1990 result
= ASN1_DER_ERROR
;
1995 asn1_get_length_der (der
+ counter
+ len2
,
1996 len
- counter
- len2
, &len3
);
1999 result
= ASN1_DER_ERROR
;
2008 _asn1_set_value_octet (p
, der
+ counter
, len2
+ len3
);
2013 counter
+= len2
+ len3
;
2016 { /* indefinite length */
2017 /* Check indefinite lenth method in an EXPLICIT TAG */
2018 if ((p
->type
& CONST_TAG
) && (der
[counter
- 1] == 0x80))
2023 len2
= len
- counter
;
2025 _asn1_get_indefinite_length_string (der
+ counter
, &len2
);
2026 if (result
!= ASN1_SUCCESS
)
2031 _asn1_set_value_octet (p
, der
+ counter
, len2
);
2039 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2040 an indefinite length method. */
2043 if (!der
[counter
] && !der
[counter
+ 1])
2049 result
= ASN1_DER_ERROR
;
2058 move
= (move
== UP
) ? RIGHT
: DOWN
;
2063 if ((p
== node
&& move
!= DOWN
) || (state
== EXIT
))
2074 nameLen
-= strlen (p
->name
) + 1;
2078 strcat (currentName
, ".");
2079 strcat (currentName
, p
->name
);
2083 result
= ASN1_MEM_ERROR
;
2086 if (!(strcmp (currentName
, elementName
)))
2093 (currentName
, elementName
, strlen (currentName
)))
2094 state
= SAME_BRANCH
;
2096 state
= OTHER_BRANCH
;
2103 if ((move
== RIGHT
) && !(p
->type
& CONST_SET
))
2111 dot_p
= char_p
= currentName
;
2112 while ((char_p
= strchr (char_p
, '.')))
2118 nameLen
+= strlen (currentName
) - (dot_p
- currentName
);
2121 nameLen
-= strlen (p
->name
);
2123 strcat (currentName
, p
->name
);
2126 result
= ASN1_MEM_ERROR
;
2130 if (!(strcmp (currentName
, elementName
)))
2137 (currentName
, elementName
, strlen (currentName
)))
2138 state
= SAME_BRANCH
;
2140 state
= OTHER_BRANCH
;
2149 p
= _asn1_find_up (p
);
2153 dot_p
= char_p
= currentName
;
2154 while ((char_p
= strchr (char_p
, '.')))
2160 nameLen
+= strlen (currentName
) - (dot_p
- currentName
);
2163 if (!(strcmp (currentName
, elementName
)))
2169 if (!memcmp (currentName
, elementName
, strlen (currentName
)))
2170 state
= SAME_BRANCH
;
2172 state
= OTHER_BRANCH
;
2177 _asn1_delete_not_used (*structure
);
2181 result
= ASN1_DER_ERROR
;
2185 return ASN1_SUCCESS
;
2188 asn1_delete_structure (structure
);
2193 * asn1_der_decoding_startEnd:
2194 * @element: pointer to an ASN1 element
2195 * @ider: vector that contains the DER encoding.
2196 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1]
2197 * @name_element: an element of NAME structure.
2198 * @start: the position of the first byte of NAME_ELEMENT decoding
2200 * @end: the position of the last byte of NAME_ELEMENT decoding
2203 * Find the start and end point of an element in a DER encoding
2204 * string. I mean that if you have a der encoding and you have already
2205 * used the function asn1_der_decoding() to fill a structure, it may
2206 * happen that you want to find the piece of string concerning an
2207 * element of the structure.
2209 * One example is the sequence "tbsCertificate" inside an X509
2212 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
2213 * if ELEMENT is %asn1_node EMPTY or @name_element is not a valid
2214 * element, %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding
2215 * doesn't match the structure ELEMENT.
2218 asn1_der_decoding_startEnd (asn1_node element
, const void *ider
, int len
,
2219 const char *name_element
, int *start
, int *end
)
2221 asn1_node node
, node_to_find
, p
, p2
, p3
;
2222 int counter
, len2
, len3
, len4
, move
, ris
;
2223 unsigned char class;
2226 const unsigned char *der
= ider
;
2231 return ASN1_ELEMENT_NOT_FOUND
;
2233 node_to_find
= asn1_find_node (node
, name_element
);
2235 if (node_to_find
== NULL
)
2236 return ASN1_ELEMENT_NOT_FOUND
;
2238 if (node_to_find
== node
)
2242 return ASN1_SUCCESS
;
2245 if (node
->type
& CONST_OPTION
)
2246 return ASN1_GENERIC_ERROR
;
2254 return ASN1_DER_ERROR
;
2260 if (p
->type
& CONST_SET
)
2262 p2
= _asn1_find_up (p
);
2264 return ASN1_DER_ERROR
;
2266 len2
= _asn1_strtol (p2
->value
, NULL
, 10);
2269 if (!der
[counter
] && !der
[counter
+ 1])
2277 else if (counter
== len2
)
2283 else if (counter
> len2
)
2284 return ASN1_DER_ERROR
;
2290 if ((p2
->type
& CONST_SET
) && (p2
->type
& CONST_NOT_USED
))
2292 if (type_field (p2
->type
) != TYPE_CHOICE
)
2294 _asn1_extract_tag_der (p2
, der
+ counter
,
2295 len
- counter
, &len2
);
2300 return ASN1_DER_ERROR
;
2303 _asn1_extract_tag_der (p3
, der
+ counter
,
2304 len
- counter
, &len2
);
2306 if (ris
== ASN1_SUCCESS
)
2308 p2
->type
&= ~CONST_NOT_USED
;
2316 return ASN1_DER_ERROR
;
2319 if (p
== node_to_find
)
2322 if (type_field (p
->type
) == TYPE_CHOICE
)
2326 return ASN1_DER_ERROR
;
2329 _asn1_extract_tag_der (p
, der
+ counter
, len
- counter
,
2331 if (p
== node_to_find
)
2335 if (ris
== ASN1_SUCCESS
)
2337 _asn1_extract_tag_der (p
, der
+ counter
, len
- counter
, &len2
);
2338 if (ris
!= ASN1_SUCCESS
)
2340 if (p
->type
& CONST_OPTION
)
2342 p
->type
|= CONST_NOT_USED
;
2345 else if (p
->type
& CONST_DEFAULT
)
2351 return ASN1_TAG_ERROR
;
2358 if (ris
== ASN1_SUCCESS
)
2360 switch (type_field (p
->type
))
2364 return ASN1_DER_ERROR
;
2369 if (der
[counter
++] != 1)
2370 return ASN1_DER_ERROR
;
2375 case TYPE_ENUMERATED
:
2377 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2379 return ASN1_DER_ERROR
;
2380 counter
+= len3
+ len2
;
2383 case TYPE_OBJECT_ID
:
2385 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2387 return ASN1_DER_ERROR
;
2388 counter
+= len2
+ len3
;
2393 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2395 return ASN1_DER_ERROR
;
2396 counter
+= len2
+ len3
;
2399 case TYPE_OCTET_STRING
:
2400 len3
= len
- counter
;
2401 ris
= _asn1_get_octet_string (der
+ counter
, NULL
, &len3
);
2402 if (ris
!= ASN1_SUCCESS
)
2407 case TYPE_GENERALSTRING
:
2409 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2411 return ASN1_DER_ERROR
;
2412 counter
+= len3
+ len2
;
2415 case TYPE_BIT_STRING
:
2417 asn1_get_length_der (der
+ counter
, len
- counter
, &len3
);
2419 return ASN1_DER_ERROR
;
2420 counter
+= len3
+ len2
;
2428 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
2430 return ASN1_DER_ERROR
;
2439 if (!der
[counter
] && !der
[counter
+ 1]) /* indefinite length method */
2444 case TYPE_SEQUENCE_OF
:
2449 asn1_get_length_der (der
+ counter
, len
- counter
, &len2
);
2451 return ASN1_DER_ERROR
;
2453 if ((len3
== -1) && !der
[counter
] && !der
[counter
+ 1])
2458 while ((type_field (p2
->type
) == TYPE_TAG
) ||
2459 (type_field (p2
->type
) == TYPE_SIZE
))
2466 if (!der
[counter
] && !der
[counter
+ 1]) /* indefinite length method */
2472 if (asn1_get_tag_der
2473 (der
+ counter
, len
- counter
, &class, &len2
,
2474 &tag
) != ASN1_SUCCESS
)
2475 return ASN1_DER_ERROR
;
2476 if (counter
+ len2
> len
)
2477 return ASN1_DER_ERROR
;
2480 asn1_get_length_der (der
+ counter
+ len2
,
2481 len
- counter
- len2
, &len3
);
2483 return ASN1_DER_ERROR
;
2487 counter
+= len2
+ len4
+ len3
;
2490 { /* indefinite length */
2491 /* Check indefinite lenth method in an EXPLICIT TAG */
2492 if ((p
->type
& CONST_TAG
) && (der
[counter
- 1] == 0x80))
2497 len2
= len
- counter
;
2499 _asn1_get_indefinite_length_string (der
+ counter
, &len2
);
2500 if (ris
!= ASN1_SUCCESS
)
2504 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2505 an indefinite length method. */
2508 if (!der
[counter
] && !der
[counter
+ 1])
2511 return ASN1_DER_ERROR
;
2517 move
= (move
== UP
) ? RIGHT
: DOWN
;
2522 if ((p
== node_to_find
) && (move
== RIGHT
))
2525 return ASN1_SUCCESS
;
2528 if (p
== node
&& move
!= DOWN
)
2538 if ((move
== RIGHT
) && !(p
->type
& CONST_SET
))
2546 p
= _asn1_find_up (p
);
2549 return ASN1_ELEMENT_NOT_FOUND
;
2553 * asn1_expand_any_defined_by:
2554 * @definitions: ASN1 definitions
2555 * @element: pointer to an ASN1 structure
2557 * Expands every "ANY DEFINED BY" element of a structure created from
2558 * a DER decoding process (asn1_der_decoding function). The element
2559 * ANY must be defined by an OBJECT IDENTIFIER. The type used to
2560 * expand the element ANY is the first one following the definition of
2561 * the actual value of the OBJECT IDENTIFIER.
2563 * Returns: %ASN1_SUCCESS if Substitution OK, %ASN1_ERROR_TYPE_ANY if
2564 * some "ANY DEFINED BY" element couldn't be expanded due to a
2565 * problem in OBJECT_ID -> TYPE association, or other error codes
2566 * depending on DER decoding.
2569 asn1_expand_any_defined_by (asn1_node definitions
, asn1_node
* element
)
2571 char definitionsName
[ASN1_MAX_NAME_SIZE
], name
[2 * ASN1_MAX_NAME_SIZE
+ 1],
2572 value
[ASN1_MAX_NAME_SIZE
];
2573 int retCode
= ASN1_SUCCESS
, result
;
2574 int len
, len2
, len3
;
2575 asn1_node p
, p2
, p3
, aux
= NULL
;
2576 char errorDescription
[ASN1_MAX_ERROR_DESCRIPTION_SIZE
];
2578 if ((definitions
== NULL
) || (*element
== NULL
))
2579 return ASN1_ELEMENT_NOT_FOUND
;
2581 strcpy (definitionsName
, definitions
->name
);
2582 strcat (definitionsName
, ".");
2588 switch (type_field (p
->type
))
2591 if ((p
->type
& CONST_DEFINED_BY
) && (p
->value
))
2593 /* search the "DEF_BY" element */
2595 while ((p2
) && (type_field (p2
->type
) != TYPE_CONSTANT
))
2600 retCode
= ASN1_ERROR_TYPE_ANY
;
2604 p3
= _asn1_find_up (p
);
2608 retCode
= ASN1_ERROR_TYPE_ANY
;
2615 if (!(strcmp (p3
->name
, p2
->name
)))
2620 if ((!p3
) || (type_field (p3
->type
) != TYPE_OBJECT_ID
) ||
2621 (p3
->value
== NULL
))
2624 p3
= _asn1_find_up (p
);
2625 p3
= _asn1_find_up (p3
);
2629 retCode
= ASN1_ERROR_TYPE_ANY
;
2637 if (!(strcmp (p3
->name
, p2
->name
)))
2642 if ((!p3
) || (type_field (p3
->type
) != TYPE_OBJECT_ID
) ||
2643 (p3
->value
== NULL
))
2645 retCode
= ASN1_ERROR_TYPE_ANY
;
2650 /* search the OBJECT_ID into definitions */
2651 p2
= definitions
->down
;
2654 if ((type_field (p2
->type
) == TYPE_OBJECT_ID
) &&
2655 (p2
->type
& CONST_ASSIGN
))
2657 strcpy (name
, definitionsName
);
2658 strcat (name
, p2
->name
);
2660 len
= ASN1_MAX_NAME_SIZE
;
2662 asn1_read_value (definitions
, name
, value
, &len
);
2664 if ((result
== ASN1_SUCCESS
)
2665 && (!_asn1_strcmp (p3
->value
, value
)))
2667 p2
= p2
->right
; /* pointer to the structure to
2668 use for expansion */
2669 while ((p2
) && (p2
->type
& CONST_ASSIGN
))
2674 strcpy (name
, definitionsName
);
2675 strcat (name
, p2
->name
);
2678 asn1_create_element (definitions
, name
, &aux
);
2679 if (result
== ASN1_SUCCESS
)
2681 _asn1_cpy_name (aux
, p
);
2683 asn1_get_length_der (p
->value
,
2684 p
->value_len
, &len3
);
2686 return ASN1_DER_ERROR
;
2689 asn1_der_decoding (&aux
, p
->value
+ len3
,
2692 if (result
== ASN1_SUCCESS
)
2695 _asn1_set_right (aux
, p
->right
);
2696 _asn1_set_right (p
, aux
);
2698 result
= asn1_delete_structure (&p
);
2699 if (result
== ASN1_SUCCESS
)
2706 { /* error with asn1_delete_structure */
2707 asn1_delete_structure (&aux
);
2713 { /* error with asn1_der_decoding */
2719 { /* error with asn1_create_element */
2725 { /* error with the pointer to the structure to exapand */
2726 retCode
= ASN1_ERROR_TYPE_ANY
;
2736 retCode
= ASN1_ERROR_TYPE_ANY
;
2751 else if (p
== *element
)
2762 p
= _asn1_find_up (p
);
2781 * asn1_expand_octet_string:
2782 * @definitions: ASN1 definitions
2783 * @element: pointer to an ASN1 structure
2784 * @octetName: name of the OCTECT STRING field to expand.
2785 * @objectName: name of the OBJECT IDENTIFIER field to use to define
2786 * the type for expansion.
2788 * Expands an "OCTET STRING" element of a structure created from a DER
2789 * decoding process (the asn1_der_decoding() function). The type used
2790 * for expansion is the first one following the definition of the
2791 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
2793 * Returns: %ASN1_SUCCESS if substitution OK, %ASN1_ELEMENT_NOT_FOUND
2794 * if @objectName or @octetName are not correct,
2795 * %ASN1_VALUE_NOT_VALID if it wasn't possible to find the type to
2796 * use for expansion, or other errors depending on DER decoding.
2799 asn1_expand_octet_string (asn1_node definitions
, asn1_node
* element
,
2800 const char *octetName
, const char *objectName
)
2802 char name
[2 * ASN1_MAX_NAME_SIZE
+ 1], value
[ASN1_MAX_NAME_SIZE
];
2803 int retCode
= ASN1_SUCCESS
, result
;
2804 int len
, len2
, len3
;
2805 asn1_node p2
, aux
= NULL
;
2806 asn1_node octetNode
= NULL
, objectNode
= NULL
;
2807 char errorDescription
[ASN1_MAX_ERROR_DESCRIPTION_SIZE
];
2809 if ((definitions
== NULL
) || (*element
== NULL
))
2810 return ASN1_ELEMENT_NOT_FOUND
;
2812 octetNode
= asn1_find_node (*element
, octetName
);
2813 if (octetNode
== NULL
)
2814 return ASN1_ELEMENT_NOT_FOUND
;
2815 if (type_field (octetNode
->type
) != TYPE_OCTET_STRING
)
2816 return ASN1_ELEMENT_NOT_FOUND
;
2817 if (octetNode
->value
== NULL
)
2818 return ASN1_VALUE_NOT_FOUND
;
2820 objectNode
= asn1_find_node (*element
, objectName
);
2821 if (objectNode
== NULL
)
2822 return ASN1_ELEMENT_NOT_FOUND
;
2824 if (type_field (objectNode
->type
) != TYPE_OBJECT_ID
)
2825 return ASN1_ELEMENT_NOT_FOUND
;
2827 if (objectNode
->value
== NULL
)
2828 return ASN1_VALUE_NOT_FOUND
;
2831 /* search the OBJECT_ID into definitions */
2832 p2
= definitions
->down
;
2835 if ((type_field (p2
->type
) == TYPE_OBJECT_ID
) &&
2836 (p2
->type
& CONST_ASSIGN
))
2838 strcpy (name
, definitions
->name
);
2840 strcat (name
, p2
->name
);
2842 len
= sizeof (value
);
2843 result
= asn1_read_value (definitions
, name
, value
, &len
);
2845 if ((result
== ASN1_SUCCESS
)
2846 && (!_asn1_strcmp (objectNode
->value
, value
)))
2849 p2
= p2
->right
; /* pointer to the structure to
2850 use for expansion */
2851 while ((p2
) && (p2
->type
& CONST_ASSIGN
))
2856 strcpy (name
, definitions
->name
);
2858 strcat (name
, p2
->name
);
2860 result
= asn1_create_element (definitions
, name
, &aux
);
2861 if (result
== ASN1_SUCCESS
)
2863 _asn1_cpy_name (aux
, octetNode
);
2865 asn1_get_length_der (octetNode
->value
,
2866 octetNode
->value_len
, &len3
);
2868 return ASN1_DER_ERROR
;
2871 asn1_der_decoding (&aux
, octetNode
->value
+ len3
,
2872 len2
, errorDescription
);
2873 if (result
== ASN1_SUCCESS
)
2876 _asn1_set_right (aux
, octetNode
->right
);
2877 _asn1_set_right (octetNode
, aux
);
2879 result
= asn1_delete_structure (&octetNode
);
2880 if (result
== ASN1_SUCCESS
)
2886 { /* error with asn1_delete_structure */
2887 asn1_delete_structure (&aux
);
2893 { /* error with asn1_der_decoding */
2899 { /* error with asn1_create_element */
2905 { /* error with the pointer to the structure to exapand */
2906 retCode
= ASN1_VALUE_NOT_VALID
;
2917 retCode
= ASN1_VALUE_NOT_VALID
;