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) == ASN1_ETYPE_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) == ASN1_ETYPE_SEQUENCE) ||
440 (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF) ||
441 (type_field (node->type) == ASN1_ETYPE_SET) ||
442 (type_field (node->type) == ASN1_ETYPE_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) == ASN1_ETYPE_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 unsigned type = type_field (node->type);
478 if (type == ASN1_ETYPE_TAG)
486 (der + counter, der_len - counter, &class, &len2,
487 &tag) != ASN1_SUCCESS)
488 return ASN1_DER_ERROR;
490 if (counter + len2 > der_len)
491 return ASN1_DER_ERROR;
495 case ASN1_ETYPE_NULL:
496 case ASN1_ETYPE_BOOLEAN:
497 case ASN1_ETYPE_INTEGER:
498 case ASN1_ETYPE_ENUMERATED:
499 case ASN1_ETYPE_OBJECT_ID:
500 case ASN1_ETYPE_GENERALSTRING:
501 case ASN1_ETYPE_NUMERIC_STRING:
502 case ASN1_ETYPE_IA5_STRING:
503 case ASN1_ETYPE_TELETEX_STRING:
504 case ASN1_ETYPE_PRINTABLE_STRING:
505 case ASN1_ETYPE_UNIVERSAL_STRING:
506 case ASN1_ETYPE_BMP_STRING:
507 case ASN1_ETYPE_UTF8_STRING:
508 case ASN1_ETYPE_VISIBLE_STRING:
509 case ASN1_ETYPE_BIT_STRING:
510 case ASN1_ETYPE_SEQUENCE:
511 case ASN1_ETYPE_SEQUENCE_OF:
513 case ASN1_ETYPE_SET_OF:
514 case ASN1_ETYPE_GENERALIZED_TIME:
515 case ASN1_ETYPE_UTC_TIME:
516 if ((class != _asn1_tags[type].class) || (tag != _asn1_tags[type].tag))
517 return ASN1_DER_ERROR;
520 case ASN1_ETYPE_OCTET_STRING:
521 /* OCTET STRING is handled differently to allow
522 * BER encodings (structured class). */
523 if (((class != ASN1_CLASS_UNIVERSAL)
524 && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
525 || (tag != ASN1_TAG_OCTET_STRING))
526 return ASN1_DER_ERROR;
532 return ASN1_DER_ERROR;
543 _asn1_delete_not_used (asn1_node node)
548 return ASN1_ELEMENT_NOT_FOUND;
553 if (p->type & CONST_NOT_USED)
558 p2 = _asn1_find_left (p);
560 p2 = _asn1_find_up (p);
562 asn1_delete_structure (&p);
567 break; /* reach node */
583 p = _asn1_find_up (p);
602 _asn1_extract_der_octet (asn1_node node, const unsigned char *der,
606 int counter2, counter_end;
608 len2 = asn1_get_length_der (der, der_len, &len3);
610 return ASN1_DER_ERROR;
615 counter_end = der_len - 2;
617 counter_end = der_len;
619 while (counter2 < counter_end)
621 len2 = asn1_get_length_der (der + counter2, der_len - counter2, &len3);
624 return ASN1_DER_ERROR;
628 _asn1_append_value (node, der + counter2 + len3, len2);
634 _asn1_extract_der_octet (node, der + counter2 + len3,
635 der_len - counter2 - len3);
640 counter2 += len2 + len3 + 1;
647 _asn1_get_octet_string (const unsigned char *der, asn1_node node, int *len)
649 int len2, len3, counter, tot_len, indefinite;
653 if (*(der - 1) & ASN1_CLASS_STRUCTURED)
656 indefinite = asn1_get_length_der (der, *len, &len3);
658 return ASN1_DER_ERROR;
666 if (counter > (*len))
667 return ASN1_DER_ERROR;
669 if (indefinite == -1)
671 if ((der[counter] == 0) && (der[counter + 1] == 0))
677 else if (counter >= indefinite)
680 if (der[counter] != ASN1_TAG_OCTET_STRING)
681 return ASN1_DER_ERROR;
685 len2 = asn1_get_length_der (der + counter, *len - counter, &len3);
687 return ASN1_DER_ERROR;
689 counter += len3 + len2;
696 unsigned char temp[DER_LEN];
699 len2 = sizeof (temp);
701 asn1_length_der (tot_len, temp, &len2);
702 _asn1_set_value (node, temp, len2);
704 ret = _asn1_extract_der_octet (node, der, *len);
705 if (ret != ASN1_SUCCESS)
711 { /* NOT STRUCTURED */
712 len2 = asn1_get_length_der (der, *len, &len3);
714 return ASN1_DER_ERROR;
716 counter = len3 + len2;
718 _asn1_set_value (node, der, counter);
727 _asn1_get_indefinite_length_string (const unsigned char *der, int *len)
729 int len2, len3, counter, indefinite;
733 counter = indefinite = 0;
737 if ((*len) < counter)
738 return ASN1_DER_ERROR;
740 if ((der[counter] == 0) && (der[counter + 1] == 0))
751 (der + counter, *len - counter, &class, &len2,
752 &tag) != ASN1_SUCCESS)
753 return ASN1_DER_ERROR;
754 if (counter + len2 > *len)
755 return ASN1_DER_ERROR;
757 len2 = asn1_get_length_der (der + counter, *len - counter, &len3);
759 return ASN1_DER_ERROR;
767 counter += len2 + len3;
778 * @element: pointer to an ASN1 structure.
779 * @ider: vector that contains the DER encoding.
780 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1].
781 * @errorDescription: null-terminated string contains details when an
784 * Fill the structure *@ELEMENT with values of a DER encoding
785 * string. The structure must just be created with function
786 * asn1_create_element(). If an error occurs during the decoding
787 * procedure, the *@ELEMENT is deleted and set equal to
790 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
791 * if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
792 * %ASN1_DER_ERROR if the der encoding doesn't match the structure
793 * name (*@ELEMENT deleted).
796 asn1_der_decoding (asn1_node * element, const void *ider, int len,
797 char *errorDescription)
799 asn1_node node, p, p2, p3;
801 int counter, len2, len3, len4, move, ris, tlen;
804 int indefinite, result;
805 const unsigned char *der = ider;
809 if (errorDescription != NULL)
810 errorDescription[0] = 0;
813 return ASN1_ELEMENT_NOT_FOUND;
815 if (node->type & CONST_OPTION)
817 result = ASN1_GENERIC_ERROR;
829 if (p->type & CONST_SET)
831 p2 = _asn1_find_up (p);
832 len2 = _asn1_strtol (p2->value, NULL, 10);
835 if (!der[counter] && !der[counter + 1])
843 else if (counter == len2)
849 else if (counter > len2)
851 result = ASN1_DER_ERROR;
857 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
859 if (type_field (p2->type) != ASN1_ETYPE_CHOICE)
861 _asn1_extract_tag_der (p2, der + counter,
862 len - counter, &len2);
869 _asn1_extract_tag_der (p3, der + counter,
870 len - counter, &len2);
871 if (ris == ASN1_SUCCESS)
876 if (ris == ASN1_SUCCESS)
878 p2->type &= ~CONST_NOT_USED;
887 result = ASN1_DER_ERROR;
892 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
894 p2 = _asn1_find_up (p);
895 len2 = _asn1_strtol (p2->value, NULL, 10);
906 if (p->type & CONST_OPTION)
907 asn1_delete_structure (&p);
914 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
920 _asn1_extract_tag_der (p->down, der + counter,
921 len - counter, &len2);
923 ris = ASN1_DER_ERROR;
924 if (ris == ASN1_SUCCESS)
926 while (p->down->right)
929 asn1_delete_structure (&p2);
933 else if (ris == ASN1_ERROR_TYPE_ANY)
935 result = ASN1_ERROR_TYPE_ANY;
941 asn1_delete_structure (&p2);
947 if (!(p->type & CONST_OPTION))
949 result = ASN1_DER_ERROR;
957 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
959 p2 = _asn1_find_up (p);
960 len2 = _asn1_strtol (p2->value, NULL, 10);
961 if ((len2 != -1) && (counter > len2))
962 ris = ASN1_TAG_ERROR;
965 if (ris == ASN1_SUCCESS)
967 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
968 if (ris != ASN1_SUCCESS)
970 if (p->type & CONST_OPTION)
972 p->type |= CONST_NOT_USED;
975 else if (p->type & CONST_DEFAULT)
977 _asn1_set_value (p, NULL, 0);
982 if (errorDescription != NULL)
983 _asn1_error_description_tag_error (p, errorDescription);
985 result = ASN1_TAG_ERROR;
993 if (ris == ASN1_SUCCESS)
995 switch (type_field (p->type))
997 case ASN1_ETYPE_NULL:
1000 result = ASN1_DER_ERROR;
1006 case ASN1_ETYPE_BOOLEAN:
1007 if (der[counter++] != 1)
1009 result = ASN1_DER_ERROR;
1012 if (der[counter++] == 0)
1013 _asn1_set_value (p, "F", 1);
1015 _asn1_set_value (p, "T", 1);
1018 case ASN1_ETYPE_INTEGER:
1019 case ASN1_ETYPE_ENUMERATED:
1021 asn1_get_length_der (der + counter, len - counter, &len3);
1024 result = ASN1_DER_ERROR;
1028 _asn1_set_value (p, der + counter, len3 + len2);
1029 counter += len3 + len2;
1032 case ASN1_ETYPE_OBJECT_ID:
1034 _asn1_get_objectid_der (der + counter, len - counter, &len2,
1035 temp, sizeof (temp));
1036 if (result != ASN1_SUCCESS)
1039 tlen = strlen (temp);
1041 _asn1_set_value (p, temp, tlen + 1);
1045 case ASN1_ETYPE_GENERALIZED_TIME:
1046 case ASN1_ETYPE_UTC_TIME:
1048 _asn1_get_time_der (der + counter, len - counter, &len2, temp,
1050 if (result != ASN1_SUCCESS)
1053 tlen = strlen (temp);
1055 _asn1_set_value (p, temp, tlen + 1);
1059 case ASN1_ETYPE_OCTET_STRING:
1060 len3 = len - counter;
1061 result = _asn1_get_octet_string (der + counter, p, &len3);
1062 if (result != ASN1_SUCCESS)
1068 case ASN1_ETYPE_GENERALSTRING:
1069 case ASN1_ETYPE_NUMERIC_STRING:
1070 case ASN1_ETYPE_IA5_STRING:
1071 case ASN1_ETYPE_TELETEX_STRING:
1072 case ASN1_ETYPE_PRINTABLE_STRING:
1073 case ASN1_ETYPE_UNIVERSAL_STRING:
1074 case ASN1_ETYPE_BMP_STRING:
1075 case ASN1_ETYPE_UTF8_STRING:
1076 case ASN1_ETYPE_VISIBLE_STRING:
1077 case ASN1_ETYPE_BIT_STRING:
1079 asn1_get_length_der (der + counter, len - counter, &len3);
1082 result = ASN1_DER_ERROR;
1086 _asn1_set_value (p, der + counter, len3 + len2);
1087 counter += len3 + len2;
1090 case ASN1_ETYPE_SEQUENCE:
1091 case ASN1_ETYPE_SET:
1094 len2 = _asn1_strtol (p->value, NULL, 10);
1095 _asn1_set_value (p, NULL, 0);
1097 { /* indefinite length method */
1098 if (len - counter + 1 > 0)
1100 if ((der[counter]) || der[counter + 1])
1102 result = ASN1_DER_ERROR;
1108 result = ASN1_DER_ERROR;
1114 { /* definite length method */
1115 if (len2 != counter)
1117 result = ASN1_DER_ERROR;
1124 { /* move==DOWN || move==RIGHT */
1126 asn1_get_length_der (der + counter, len - counter, &len2);
1129 result = ASN1_DER_ERROR;
1135 _asn1_ltostr (counter + len3, temp);
1136 tlen = strlen (temp);
1138 _asn1_set_value (p, temp, tlen + 1);
1146 if (type_field (p2->type) != ASN1_ETYPE_TAG)
1149 asn1_delete_structure (&p2);
1158 { /* indefinite length method */
1159 _asn1_set_value (p, "-1", 3);
1164 case ASN1_ETYPE_SEQUENCE_OF:
1165 case ASN1_ETYPE_SET_OF:
1168 len2 = _asn1_strtol (p->value, NULL, 10);
1170 { /* indefinite length method */
1171 if ((counter + 2) > len)
1173 result = ASN1_DER_ERROR;
1177 if ((der[counter]) || der[counter + 1])
1179 _asn1_append_sequence_set (p);
1186 _asn1_set_value (p, NULL, 0);
1190 { /* definite length method */
1193 _asn1_append_sequence_set (p);
1200 _asn1_set_value (p, NULL, 0);
1201 if (len2 != counter)
1203 result = ASN1_DER_ERROR;
1209 { /* move==DOWN || move==RIGHT */
1211 asn1_get_length_der (der + counter, len - counter, &len2);
1214 result = ASN1_DER_ERROR;
1221 { /* definite length method */
1222 _asn1_ltostr (counter + len3, temp);
1223 tlen = strlen (temp);
1226 _asn1_set_value (p, temp, tlen + 1);
1229 { /* indefinite length method */
1230 _asn1_set_value (p, "-1", 3);
1233 while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1234 || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1236 if (p2->right == NULL)
1237 _asn1_append_sequence_set (p);
1243 case ASN1_ETYPE_ANY:
1244 if (asn1_get_tag_der
1245 (der + counter, len - counter, &class, &len2,
1246 &tag) != ASN1_SUCCESS)
1248 result = ASN1_DER_ERROR;
1252 if (counter + len2 > len)
1254 result = ASN1_DER_ERROR;
1258 asn1_get_length_der (der + counter + len2,
1259 len - counter - len2, &len3);
1262 result = ASN1_DER_ERROR;
1268 _asn1_set_value_lv (p, der + counter, len2 + len3);
1269 counter += len2 + len3;
1272 { /* indefinite length */
1273 /* Check indefinite lenth method in an EXPLICIT TAG */
1274 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1279 len2 = len - counter;
1281 _asn1_get_indefinite_length_string (der + counter, &len2);
1282 if (result != ASN1_SUCCESS)
1285 _asn1_set_value_lv (p, der + counter, len2);
1288 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1289 an indefinite length method. */
1292 if (!der[counter] && !der[counter + 1])
1298 result = ASN1_DER_ERROR;
1306 move = (move == UP) ? RIGHT : DOWN;
1311 if (p == node && move != DOWN)
1321 if ((move == RIGHT) && !(p->type & CONST_SET))
1329 p = _asn1_find_up (p);
1332 _asn1_delete_not_used (*element);
1336 result = ASN1_DER_ERROR;
1340 return ASN1_SUCCESS;
1343 asn1_delete_structure (element);
1348 #define SAME_BRANCH 2
1349 #define OTHER_BRANCH 3
1353 * asn1_der_decoding_element:
1354 * @structure: pointer to an ASN1 structure
1355 * @elementName: name of the element to fill
1356 * @ider: vector that contains the DER encoding of the whole structure.
1357 * @len: number of bytes of *der: der[0]..der[len-1]
1358 * @errorDescription: null-terminated string contains details when an
1361 * Fill the element named @ELEMENTNAME with values of a DER encoding
1362 * string. The structure must just be created with function
1363 * asn1_create_element(). The DER vector must contain the encoding
1364 * string of the whole @STRUCTURE. If an error occurs during the
1365 * decoding procedure, the *@STRUCTURE is deleted and set equal to
1368 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1369 * if ELEMENT is %NULL or @elementName == NULL, and
1370 * %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding doesn't
1371 * match the structure @structure (*ELEMENT deleted).
1374 asn1_der_decoding_element (asn1_node * structure, const char *elementName,
1375 const void *ider, int len, char *errorDescription)
1377 asn1_node node, p, p2, p3, nodeFound = NULL;
1378 char temp[128], currentName[ASN1_MAX_NAME_SIZE * 10], *dot_p, *char_p;
1379 int nameLen = ASN1_MAX_NAME_SIZE * 10 - 1, state;
1380 int counter, len2, len3, len4, move, ris, tlen;
1381 unsigned char class;
1383 int indefinite, result;
1384 const unsigned char *der = ider;
1389 return ASN1_ELEMENT_NOT_FOUND;
1391 if (elementName == NULL)
1393 result = ASN1_ELEMENT_NOT_FOUND;
1397 if (node->type & CONST_OPTION)
1399 result = ASN1_GENERIC_ERROR;
1403 if ((*structure)->name[0] != 0)
1404 { /* Has *structure got a name? */
1405 nameLen -= strlen ((*structure)->name);
1407 strcpy (currentName, (*structure)->name);
1410 result = ASN1_MEM_ERROR;
1413 if (!(strcmp (currentName, elementName)))
1416 nodeFound = *structure;
1418 else if (!memcmp (currentName, elementName, strlen (currentName)))
1419 state = SAME_BRANCH;
1421 state = OTHER_BRANCH;
1424 { /* *structure doesn't have a name? */
1426 if (elementName[0] == 0)
1429 nodeFound = *structure;
1433 state = SAME_BRANCH;
1447 if (p->type & CONST_SET)
1449 p2 = _asn1_find_up (p);
1450 len2 = _asn1_strtol (p2->value, NULL, 10);
1451 if (counter == len2)
1457 else if (counter > len2)
1459 result = ASN1_DER_ERROR;
1465 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
1467 if (type_field (p2->type) != ASN1_ETYPE_CHOICE)
1469 _asn1_extract_tag_der (p2, der + counter,
1470 len - counter, &len2);
1477 _asn1_extract_tag_der (p3, der + counter,
1478 len - counter, &len2);
1479 if (ris == ASN1_SUCCESS)
1484 if (ris == ASN1_SUCCESS)
1486 p2->type &= ~CONST_NOT_USED;
1495 result = ASN1_DER_ERROR;
1500 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1502 p2 = _asn1_find_up (p);
1503 len2 = _asn1_strtol (p2->value, NULL, 10);
1504 if (counter == len2)
1514 if (p->type & CONST_OPTION)
1515 asn1_delete_structure (&p);
1522 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
1528 _asn1_extract_tag_der (p->down, der + counter,
1529 len - counter, &len2);
1531 ris = ASN1_DER_ERROR;
1532 if (ris == ASN1_SUCCESS)
1534 while (p->down->right)
1536 p2 = p->down->right;
1537 asn1_delete_structure (&p2);
1541 else if (ris == ASN1_ERROR_TYPE_ANY)
1543 result = ASN1_ERROR_TYPE_ANY;
1549 asn1_delete_structure (&p2);
1553 if (p->down == NULL)
1555 if (!(p->type & CONST_OPTION))
1557 result = ASN1_DER_ERROR;
1565 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1567 p2 = _asn1_find_up (p);
1568 len2 = _asn1_strtol (p2->value, NULL, 10);
1570 ris = ASN1_TAG_ERROR;
1573 if (ris == ASN1_SUCCESS)
1575 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
1576 if (ris != ASN1_SUCCESS)
1578 if (p->type & CONST_OPTION)
1580 p->type |= CONST_NOT_USED;
1583 else if (p->type & CONST_DEFAULT)
1585 _asn1_set_value (p, NULL, 0);
1590 if (errorDescription != NULL)
1591 _asn1_error_description_tag_error (p, errorDescription);
1593 result = ASN1_TAG_ERROR;
1601 if (ris == ASN1_SUCCESS)
1603 switch (type_field (p->type))
1605 case ASN1_ETYPE_NULL:
1608 result = ASN1_DER_ERROR;
1618 case ASN1_ETYPE_BOOLEAN:
1619 if (der[counter++] != 1)
1621 result = ASN1_DER_ERROR;
1627 if (der[counter++] == 0)
1628 _asn1_set_value (p, "F", 1);
1630 _asn1_set_value (p, "T", 1);
1641 case ASN1_ETYPE_INTEGER:
1642 case ASN1_ETYPE_ENUMERATED:
1644 asn1_get_length_der (der + counter, len - counter, &len3);
1647 result = ASN1_DER_ERROR;
1653 if (len3 + len2 > len - counter)
1655 result = ASN1_DER_ERROR;
1658 _asn1_set_value (p, der + counter, len3 + len2);
1663 counter += len3 + len2;
1666 case ASN1_ETYPE_OBJECT_ID:
1670 _asn1_get_objectid_der (der + counter, len - counter,
1671 &len2, temp, sizeof (temp));
1672 if (result != ASN1_SUCCESS)
1675 tlen = strlen (temp);
1678 _asn1_set_value (p, temp, tlen + 1);
1686 asn1_get_length_der (der + counter, len - counter, &len3);
1689 result = ASN1_DER_ERROR;
1698 case ASN1_ETYPE_GENERALIZED_TIME:
1699 case ASN1_ETYPE_UTC_TIME:
1703 _asn1_get_time_der (der + counter, len - counter, &len2,
1704 temp, sizeof (temp) - 1);
1705 if (result != ASN1_SUCCESS)
1708 tlen = strlen (temp);
1710 _asn1_set_value (p, temp, tlen + 1);
1718 asn1_get_length_der (der + counter, len - counter, &len3);
1721 result = ASN1_DER_ERROR;
1730 case ASN1_ETYPE_OCTET_STRING:
1731 len3 = len - counter;
1734 result = _asn1_get_octet_string (der + counter, p, &len3);
1739 result = _asn1_get_octet_string (der + counter, NULL, &len3);
1741 if (result != ASN1_SUCCESS)
1747 case ASN1_ETYPE_GENERALSTRING:
1748 case ASN1_ETYPE_NUMERIC_STRING:
1749 case ASN1_ETYPE_IA5_STRING:
1750 case ASN1_ETYPE_TELETEX_STRING:
1751 case ASN1_ETYPE_PRINTABLE_STRING:
1752 case ASN1_ETYPE_UNIVERSAL_STRING:
1753 case ASN1_ETYPE_BMP_STRING:
1754 case ASN1_ETYPE_UTF8_STRING:
1755 case ASN1_ETYPE_VISIBLE_STRING:
1756 case ASN1_ETYPE_BIT_STRING:
1758 asn1_get_length_der (der + counter, len - counter, &len3);
1761 result = ASN1_DER_ERROR;
1767 if (len3 + len2 > len - counter)
1769 result = ASN1_DER_ERROR;
1772 _asn1_set_value (p, der + counter, len3 + len2);
1777 counter += len3 + len2;
1780 case ASN1_ETYPE_SEQUENCE:
1781 case ASN1_ETYPE_SET:
1784 len2 = _asn1_strtol (p->value, NULL, 10);
1785 _asn1_set_value (p, NULL, 0);
1787 { /* indefinite length method */
1788 if ((der[counter]) || der[counter + 1])
1790 result = ASN1_DER_ERROR;
1796 { /* definite length method */
1797 if (len2 != counter)
1799 result = ASN1_DER_ERROR;
1808 { /* move==DOWN || move==RIGHT */
1809 if (state == OTHER_BRANCH)
1812 asn1_get_length_der (der + counter, len - counter,
1816 result = ASN1_DER_ERROR;
1819 counter += len2 + len3;
1823 { /* state==SAME_BRANCH or state==FOUND */
1825 asn1_get_length_der (der + counter, len - counter,
1829 result = ASN1_DER_ERROR;
1835 _asn1_ltostr (counter + len3, temp);
1836 tlen = strlen (temp);
1839 _asn1_set_value (p, temp, tlen + 1);
1847 if (type_field (p2->type) != ASN1_ETYPE_TAG)
1850 asn1_delete_structure (&p2);
1859 { /* indefinite length method */
1860 _asn1_set_value (p, "-1", 3);
1866 case ASN1_ETYPE_SEQUENCE_OF:
1867 case ASN1_ETYPE_SET_OF:
1870 len2 = _asn1_strtol (p->value, NULL, 10);
1873 _asn1_append_sequence_set (p);
1880 _asn1_set_value (p, NULL, 0);
1881 if (len2 != counter)
1883 result = ASN1_DER_ERROR;
1891 { /* move==DOWN || move==RIGHT */
1892 if (state == OTHER_BRANCH)
1895 asn1_get_length_der (der + counter, len - counter,
1899 result = ASN1_DER_ERROR;
1902 counter += len2 + len3;
1906 { /* state==FOUND or state==SAME_BRANCH */
1908 asn1_get_length_der (der + counter, len - counter,
1912 result = ASN1_DER_ERROR;
1918 _asn1_ltostr (counter + len3, temp);
1919 tlen = strlen (temp);
1922 _asn1_set_value (p, temp, tlen + 1);
1924 while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1925 || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1927 if (p2->right == NULL)
1928 _asn1_append_sequence_set (p);
1936 case ASN1_ETYPE_ANY:
1937 if (asn1_get_tag_der
1938 (der + counter, len - counter, &class, &len2,
1939 &tag) != ASN1_SUCCESS)
1941 result = ASN1_DER_ERROR;
1945 if (counter + len2 > len)
1947 result = ASN1_DER_ERROR;
1952 asn1_get_length_der (der + counter + len2,
1953 len - counter - len2, &len3);
1956 result = ASN1_DER_ERROR;
1965 _asn1_set_value_lv (p, der + counter, len2 + len3);
1970 counter += len2 + len3;
1973 { /* indefinite length */
1974 /* Check indefinite lenth method in an EXPLICIT TAG */
1975 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1980 len2 = len - counter;
1982 _asn1_get_indefinite_length_string (der + counter, &len2);
1983 if (result != ASN1_SUCCESS)
1988 _asn1_set_value_lv (p, der + counter, len2);
1996 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1997 an indefinite length method. */
2000 if (!der[counter] && !der[counter + 1])
2006 result = ASN1_DER_ERROR;
2015 move = (move == UP) ? RIGHT : DOWN;
2020 if ((p == node && move != DOWN) || (state == EXIT))
2031 nameLen -= strlen (p->name) + 1;
2035 strcat (currentName, ".");
2036 strcat (currentName, p->name);
2040 result = ASN1_MEM_ERROR;
2043 if (!(strcmp (currentName, elementName)))
2050 (currentName, elementName, strlen (currentName)))
2051 state = SAME_BRANCH;
2053 state = OTHER_BRANCH;
2060 if ((move == RIGHT) && !(p->type & CONST_SET))
2068 dot_p = char_p = currentName;
2069 while ((char_p = strchr (char_p, '.')))
2075 nameLen += strlen (currentName) - (dot_p - currentName);
2078 nameLen -= strlen (p->name);
2080 strcat (currentName, p->name);
2083 result = ASN1_MEM_ERROR;
2087 if (!(strcmp (currentName, elementName)))
2094 (currentName, elementName, strlen (currentName)))
2095 state = SAME_BRANCH;
2097 state = OTHER_BRANCH;
2106 p = _asn1_find_up (p);
2110 dot_p = char_p = currentName;
2111 while ((char_p = strchr (char_p, '.')))
2117 nameLen += strlen (currentName) - (dot_p - currentName);
2120 if (!(strcmp (currentName, elementName)))
2126 if (!memcmp (currentName, elementName, strlen (currentName)))
2127 state = SAME_BRANCH;
2129 state = OTHER_BRANCH;
2134 _asn1_delete_not_used (*structure);
2138 result = ASN1_DER_ERROR;
2142 return ASN1_SUCCESS;
2145 asn1_delete_structure (structure);
2150 * asn1_der_decoding_startEnd:
2151 * @element: pointer to an ASN1 element
2152 * @ider: vector that contains the DER encoding.
2153 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1]
2154 * @name_element: an element of NAME structure.
2155 * @start: the position of the first byte of NAME_ELEMENT decoding
2157 * @end: the position of the last byte of NAME_ELEMENT decoding
2160 * Find the start and end point of an element in a DER encoding
2161 * string. I mean that if you have a der encoding and you have already
2162 * used the function asn1_der_decoding() to fill a structure, it may
2163 * happen that you want to find the piece of string concerning an
2164 * element of the structure.
2166 * One example is the sequence "tbsCertificate" inside an X509
2169 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
2170 * if ELEMENT is %asn1_node EMPTY or @name_element is not a valid
2171 * element, %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding
2172 * doesn't match the structure ELEMENT.
2175 asn1_der_decoding_startEnd (asn1_node element, const void *ider, int len,
2176 const char *name_element, int *start, int *end)
2178 asn1_node node, node_to_find, p, p2, p3;
2179 int counter, len2, len3, len4, move, ris;
2180 unsigned char class;
2183 const unsigned char *der = ider;
2188 return ASN1_ELEMENT_NOT_FOUND;
2190 node_to_find = asn1_find_node (node, name_element);
2192 if (node_to_find == NULL)
2193 return ASN1_ELEMENT_NOT_FOUND;
2195 if (node_to_find == node)
2199 return ASN1_SUCCESS;
2202 if (node->type & CONST_OPTION)
2203 return ASN1_GENERIC_ERROR;
2211 return ASN1_DER_ERROR;
2217 if (p->type & CONST_SET)
2219 p2 = _asn1_find_up (p);
2221 return ASN1_DER_ERROR;
2223 len2 = _asn1_strtol (p2->value, NULL, 10);
2226 if (!der[counter] && !der[counter + 1])
2234 else if (counter == len2)
2240 else if (counter > len2)
2241 return ASN1_DER_ERROR;
2247 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
2249 if (type_field (p2->type) != ASN1_ETYPE_CHOICE)
2251 _asn1_extract_tag_der (p2, der + counter,
2252 len - counter, &len2);
2257 return ASN1_DER_ERROR;
2260 _asn1_extract_tag_der (p3, der + counter,
2261 len - counter, &len2);
2263 if (ris == ASN1_SUCCESS)
2265 p2->type &= ~CONST_NOT_USED;
2273 return ASN1_DER_ERROR;
2276 if (p == node_to_find)
2279 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
2283 return ASN1_DER_ERROR;
2286 _asn1_extract_tag_der (p, der + counter, len - counter,
2288 if (p == node_to_find)
2292 if (ris == ASN1_SUCCESS)
2294 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
2295 if (ris != ASN1_SUCCESS)
2297 if (p->type & CONST_OPTION)
2299 p->type |= CONST_NOT_USED;
2302 else if (p->type & CONST_DEFAULT)
2308 return ASN1_TAG_ERROR;
2315 if (ris == ASN1_SUCCESS)
2317 switch (type_field (p->type))
2319 case ASN1_ETYPE_NULL:
2321 return ASN1_DER_ERROR;
2325 case ASN1_ETYPE_BOOLEAN:
2326 if (der[counter++] != 1)
2327 return ASN1_DER_ERROR;
2331 case ASN1_ETYPE_OCTET_STRING:
2332 len3 = len - counter;
2333 ris = _asn1_get_octet_string (der + counter, NULL, &len3);
2334 if (ris != ASN1_SUCCESS)
2339 case ASN1_ETYPE_UTC_TIME:
2340 case ASN1_ETYPE_GENERALIZED_TIME:
2341 case ASN1_ETYPE_OBJECT_ID:
2342 case ASN1_ETYPE_INTEGER:
2343 case ASN1_ETYPE_ENUMERATED:
2344 case ASN1_ETYPE_GENERALSTRING:
2345 case ASN1_ETYPE_NUMERIC_STRING:
2346 case ASN1_ETYPE_IA5_STRING:
2347 case ASN1_ETYPE_TELETEX_STRING:
2348 case ASN1_ETYPE_PRINTABLE_STRING:
2349 case ASN1_ETYPE_UNIVERSAL_STRING:
2350 case ASN1_ETYPE_BMP_STRING:
2351 case ASN1_ETYPE_UTF8_STRING:
2352 case ASN1_ETYPE_VISIBLE_STRING:
2353 case ASN1_ETYPE_BIT_STRING:
2355 asn1_get_length_der (der + counter, len - counter, &len3);
2357 return ASN1_DER_ERROR;
2358 counter += len3 + len2;
2361 case ASN1_ETYPE_SEQUENCE:
2362 case ASN1_ETYPE_SET:
2366 asn1_get_length_der (der + counter, len - counter, &len2);
2368 return ASN1_DER_ERROR;
2377 if (!der[counter] && !der[counter + 1]) /* indefinite length method */
2382 case ASN1_ETYPE_SEQUENCE_OF:
2383 case ASN1_ETYPE_SET_OF:
2387 asn1_get_length_der (der + counter, len - counter, &len2);
2389 return ASN1_DER_ERROR;
2391 if ((len3 == -1) && !der[counter] && !der[counter + 1])
2396 while ((type_field (p2->type) == ASN1_ETYPE_TAG) ||
2397 (type_field (p2->type) == ASN1_ETYPE_SIZE))
2404 if (!der[counter] && !der[counter + 1]) /* indefinite length method */
2409 case ASN1_ETYPE_ANY:
2410 if (asn1_get_tag_der
2411 (der + counter, len - counter, &class, &len2,
2412 &tag) != ASN1_SUCCESS)
2413 return ASN1_DER_ERROR;
2414 if (counter + len2 > len)
2415 return ASN1_DER_ERROR;
2418 asn1_get_length_der (der + counter + len2,
2419 len - counter - len2, &len3);
2421 return ASN1_DER_ERROR;
2425 counter += len2 + len4 + len3;
2428 { /* indefinite length */
2429 /* Check indefinite lenth method in an EXPLICIT TAG */
2430 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
2435 len2 = len - counter;
2437 _asn1_get_indefinite_length_string (der + counter, &len2);
2438 if (ris != ASN1_SUCCESS)
2442 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2443 an indefinite length method. */
2446 if (!der[counter] && !der[counter + 1])
2449 return ASN1_DER_ERROR;
2455 move = (move == UP) ? RIGHT : DOWN;
2460 if ((p == node_to_find) && (move == RIGHT))
2463 return ASN1_SUCCESS;
2466 if (p == node && move != DOWN)
2476 if ((move == RIGHT) && !(p->type & CONST_SET))
2484 p = _asn1_find_up (p);
2487 return ASN1_ELEMENT_NOT_FOUND;
2491 * asn1_expand_any_defined_by:
2492 * @definitions: ASN1 definitions
2493 * @element: pointer to an ASN1 structure
2495 * Expands every "ANY DEFINED BY" element of a structure created from
2496 * a DER decoding process (asn1_der_decoding function). The element
2497 * ANY must be defined by an OBJECT IDENTIFIER. The type used to
2498 * expand the element ANY is the first one following the definition of
2499 * the actual value of the OBJECT IDENTIFIER.
2501 * Returns: %ASN1_SUCCESS if Substitution OK, %ASN1_ERROR_TYPE_ANY if
2502 * some "ANY DEFINED BY" element couldn't be expanded due to a
2503 * problem in OBJECT_ID -> TYPE association, or other error codes
2504 * depending on DER decoding.
2507 asn1_expand_any_defined_by (asn1_node definitions, asn1_node * element)
2509 char definitionsName[ASN1_MAX_NAME_SIZE], name[2 * ASN1_MAX_NAME_SIZE + 1],
2510 value[ASN1_MAX_NAME_SIZE];
2511 int retCode = ASN1_SUCCESS, result;
2512 int len, len2, len3;
2513 asn1_node p, p2, p3, aux = NULL;
2514 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
2516 if ((definitions == NULL) || (*element == NULL))
2517 return ASN1_ELEMENT_NOT_FOUND;
2519 strcpy (definitionsName, definitions->name);
2520 strcat (definitionsName, ".");
2526 switch (type_field (p->type))
2528 case ASN1_ETYPE_ANY:
2529 if ((p->type & CONST_DEFINED_BY) && (p->value))
2531 /* search the "DEF_BY" element */
2533 while ((p2) && (type_field (p2->type) != ASN1_ETYPE_CONSTANT))
2538 retCode = ASN1_ERROR_TYPE_ANY;
2542 p3 = _asn1_find_up (p);
2546 retCode = ASN1_ERROR_TYPE_ANY;
2553 if (!(strcmp (p3->name, p2->name)))
2558 if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
2559 (p3->value == NULL))
2562 p3 = _asn1_find_up (p);
2563 p3 = _asn1_find_up (p3);
2567 retCode = ASN1_ERROR_TYPE_ANY;
2575 if (!(strcmp (p3->name, p2->name)))
2580 if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
2581 (p3->value == NULL))
2583 retCode = ASN1_ERROR_TYPE_ANY;
2588 /* search the OBJECT_ID into definitions */
2589 p2 = definitions->down;
2592 if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
2593 (p2->type & CONST_ASSIGN))
2595 strcpy (name, definitionsName);
2596 strcat (name, p2->name);
2598 len = ASN1_MAX_NAME_SIZE;
2600 asn1_read_value (definitions, name, value, &len);
2602 if ((result == ASN1_SUCCESS)
2603 && (!_asn1_strcmp (p3->value, value)))
2605 p2 = p2->right; /* pointer to the structure to
2606 use for expansion */
2607 while ((p2) && (p2->type & CONST_ASSIGN))
2612 strcpy (name, definitionsName);
2613 strcat (name, p2->name);
2616 asn1_create_element (definitions, name, &aux);
2617 if (result == ASN1_SUCCESS)
2619 _asn1_cpy_name (aux, p);
2621 asn1_get_length_der (p->value,
2622 p->value_len, &len3);
2624 return ASN1_DER_ERROR;
2627 asn1_der_decoding (&aux, p->value + len3,
2630 if (result == ASN1_SUCCESS)
2633 _asn1_set_right (aux, p->right);
2634 _asn1_set_right (p, aux);
2636 result = asn1_delete_structure (&p);
2637 if (result == ASN1_SUCCESS)
2644 { /* error with asn1_delete_structure */
2645 asn1_delete_structure (&aux);
2651 { /* error with asn1_der_decoding */
2657 { /* error with asn1_create_element */
2663 { /* error with the pointer to the structure to exapand */
2664 retCode = ASN1_ERROR_TYPE_ANY;
2674 retCode = ASN1_ERROR_TYPE_ANY;
2689 else if (p == *element)
2700 p = _asn1_find_up (p);
2719 * asn1_expand_octet_string:
2720 * @definitions: ASN1 definitions
2721 * @element: pointer to an ASN1 structure
2722 * @octetName: name of the OCTECT STRING field to expand.
2723 * @objectName: name of the OBJECT IDENTIFIER field to use to define
2724 * the type for expansion.
2726 * Expands an "OCTET STRING" element of a structure created from a DER
2727 * decoding process (the asn1_der_decoding() function). The type used
2728 * for expansion is the first one following the definition of the
2729 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
2731 * Returns: %ASN1_SUCCESS if substitution OK, %ASN1_ELEMENT_NOT_FOUND
2732 * if @objectName or @octetName are not correct,
2733 * %ASN1_VALUE_NOT_VALID if it wasn't possible to find the type to
2734 * use for expansion, or other errors depending on DER decoding.
2737 asn1_expand_octet_string (asn1_node definitions, asn1_node * element,
2738 const char *octetName, const char *objectName)
2740 char name[2 * ASN1_MAX_NAME_SIZE + 1], value[ASN1_MAX_NAME_SIZE];
2741 int retCode = ASN1_SUCCESS, result;
2742 int len, len2, len3;
2743 asn1_node p2, aux = NULL;
2744 asn1_node octetNode = NULL, objectNode = NULL;
2745 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
2747 if ((definitions == NULL) || (*element == NULL))
2748 return ASN1_ELEMENT_NOT_FOUND;
2750 octetNode = asn1_find_node (*element, octetName);
2751 if (octetNode == NULL)
2752 return ASN1_ELEMENT_NOT_FOUND;
2753 if (type_field (octetNode->type) != ASN1_ETYPE_OCTET_STRING)
2754 return ASN1_ELEMENT_NOT_FOUND;
2755 if (octetNode->value == NULL)
2756 return ASN1_VALUE_NOT_FOUND;
2758 objectNode = asn1_find_node (*element, objectName);
2759 if (objectNode == NULL)
2760 return ASN1_ELEMENT_NOT_FOUND;
2762 if (type_field (objectNode->type) != ASN1_ETYPE_OBJECT_ID)
2763 return ASN1_ELEMENT_NOT_FOUND;
2765 if (objectNode->value == NULL)
2766 return ASN1_VALUE_NOT_FOUND;
2769 /* search the OBJECT_ID into definitions */
2770 p2 = definitions->down;
2773 if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
2774 (p2->type & CONST_ASSIGN))
2776 strcpy (name, definitions->name);
2778 strcat (name, p2->name);
2780 len = sizeof (value);
2781 result = asn1_read_value (definitions, name, value, &len);
2783 if ((result == ASN1_SUCCESS)
2784 && (!_asn1_strcmp (objectNode->value, value)))
2787 p2 = p2->right; /* pointer to the structure to
2788 use for expansion */
2789 while ((p2) && (p2->type & CONST_ASSIGN))
2794 strcpy (name, definitions->name);
2796 strcat (name, p2->name);
2798 result = asn1_create_element (definitions, name, &aux);
2799 if (result == ASN1_SUCCESS)
2801 _asn1_cpy_name (aux, octetNode);
2803 asn1_get_length_der (octetNode->value,
2804 octetNode->value_len, &len3);
2806 return ASN1_DER_ERROR;
2809 asn1_der_decoding (&aux, octetNode->value + len3,
2810 len2, errorDescription);
2811 if (result == ASN1_SUCCESS)
2814 _asn1_set_right (aux, octetNode->right);
2815 _asn1_set_right (octetNode, aux);
2817 result = asn1_delete_structure (&octetNode);
2818 if (result == ASN1_SUCCESS)
2824 { /* error with asn1_delete_structure */
2825 asn1_delete_structure (&aux);
2831 { /* error with asn1_der_decoding */
2837 { /* error with asn1_create_element */
2843 { /* error with the pointer to the structure to exapand */
2844 retCode = ASN1_VALUE_NOT_VALID;
2855 retCode = ASN1_VALUE_NOT_VALID;
2861 * asn1_decode_simple_der:
2862 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2863 * @der: the encoded string
2864 * @der_len: the bytes of the encoded string
2865 * @str: a pointer to the data
2866 * @str_len: the length of the data
2868 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2869 * The output is a pointer inside the @der.
2871 * Returns: %ASN1_SUCCESS if successful or an error value.
2874 asn1_decode_simple_der (unsigned int etype, const unsigned char *der, unsigned int der_len,
2875 const unsigned char **str, unsigned int *str_len)
2877 int tag_len, len_len;
2878 const unsigned char* p;
2879 unsigned char class;
2883 if (der == NULL || der_len == 0)
2884 return ASN1_VALUE_NOT_VALID;
2886 if (ETYPE_OK(etype) == 0)
2887 return ASN1_VALUE_NOT_VALID;
2889 /* doesn't handle constructed classes */
2890 if (ETYPE_CLASS(etype) != ASN1_CLASS_UNIVERSAL)
2891 return ASN1_VALUE_NOT_VALID;
2894 ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2895 if (ret != ASN1_SUCCESS)
2898 if (class != ETYPE_CLASS(etype) || tag != ETYPE_TAG(etype))
2899 return ASN1_DER_ERROR;
2904 ret = asn1_get_length_der (p, der_len, &len_len);
2906 return ASN1_DER_ERROR;
2914 return ASN1_SUCCESS;