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 /*****************************************************/
25 /* Description: Functions to create a DER coding of */
27 /*****************************************************/
30 #include "parser_aux.h"
33 #include <structure.h>
35 #define MAX_TAG_LEN 16
37 /******************************************************/
38 /* Function : _asn1_error_description_value_not_found */
39 /* Description: creates the ErrorDescription string */
40 /* for the ASN1_VALUE_NOT_FOUND error. */
42 /* node: node of the tree where the value is NULL. */
43 /* ErrorDescription: string returned. */
45 /******************************************************/
47 _asn1_error_description_value_not_found (asn1_node node,
48 char *ErrorDescription)
51 if (ErrorDescription == NULL)
54 Estrcpy (ErrorDescription, ":: value of element '");
55 _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
56 ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
57 Estrcat (ErrorDescription, "' not found");
63 * @len: value to convert.
64 * @der: the encoding (may be %NULL).
65 * @der_len: number of meaningful bytes of ANS (der[0]..der[der_len-1]).
67 * Creates the DER encoding of the provided length value.
68 * The @der buffer must have enough room for the output. The maximum
69 * length this function will encode is %ASN1_MAX_LENGTH_SIZE.
71 * To know the size of the DER encoding use a %NULL value for @der.
74 asn1_length_der (unsigned long int len, unsigned char *der, int *der_len)
77 unsigned char temp[ASN1_MAX_LENGTH_SIZE];
78 #if SIZEOF_UNSIGNED_LONG_INT > 8
79 len &= 0xFFFFFFFFFFFFFFFF;
86 der[0] = (unsigned char) len;
95 temp[k++] = len & 0xFF;
101 der[0] = ((unsigned char) k & 0x7F) + 128;
103 der[*der_len - 1 - k] = temp[k];
108 /******************************************************/
109 /* Function : _asn1_tag_der */
110 /* Description: creates the DER coding for the CLASS */
111 /* and TAG parameters. */
112 /* It is limited by the ASN1_MAX_TAG_SIZE variable */
114 /* class: value to convert. */
115 /* tag_value: value to convert. */
116 /* ans: string returned. */
117 /* ans_len: number of meaningful bytes of ANS */
118 /* (ans[0]..ans[ans_len-1]). */
120 /******************************************************/
122 _asn1_tag_der (unsigned char class, unsigned int tag_value,
123 unsigned char *ans, int *ans_len)
126 unsigned char temp[ASN1_MAX_TAG_SIZE];
131 ans[0] = (class & 0xE0) + ((unsigned char) (tag_value & 0x1F));
137 ans[0] = (class & 0xE0) + 31;
139 while (tag_value != 0)
141 temp[k++] = tag_value & 0x7F;
144 if (k > ASN1_MAX_TAG_SIZE-1)
145 break; /* will not encode larger tags */
149 ans[*ans_len - 1 - k] = temp[k] + 128;
150 ans[*ans_len - 1] -= 128;
156 * @str: the input data.
157 * @str_len: STR length (str[0]..str[*str_len-1]).
158 * @der: encoded string returned.
159 * @der_len: number of meaningful bytes of DER (der[0]..der[der_len-1]).
161 * Creates a length-value DER encoding for the input data.
162 * The DER encoding of the input data will be placed in the @der variable.
164 * Note that the OCTET STRING tag is not included in the output.
166 * This function does not return any value because it is expected
167 * that @der_len will contain enough bytes to store the string
168 * plus the DER encoding. The DER encoding size can be obtained using
172 asn1_octet_der (const unsigned char *str, int str_len,
173 unsigned char *der, int *der_len)
177 if (der == NULL || str_len < 0)
180 asn1_length_der (str_len, der, &len_len);
181 memcpy (der + len_len, str, str_len);
182 *der_len = str_len + len_len;
187 * asn1_encode_simple_der:
188 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
189 * @str: the string data.
190 * @str_len: the string length
191 * @tl: the encoded tag and length
192 * @tl_len: the bytes of the @tl field
194 * Creates the DER encoding for various simple ASN.1 types like strings etc.
195 * It stores the tag and length in @tl, which should have space for at least
196 * %ASN1_MAX_TL_SIZE bytes. Initially @tl_len should contain the size of @tl.
198 * The complete DER encoding should consist of the value in @tl appended
199 * with the provided @str.
201 * Returns: %ASN1_SUCCESS if successful or an error value.
204 asn1_encode_simple_der (unsigned int etype, const unsigned char *str, unsigned int str_len,
205 unsigned char *tl, unsigned int *tl_len)
207 int tag_len, len_len;
209 unsigned char der_tag[ASN1_MAX_TAG_SIZE];
210 unsigned char der_length[ASN1_MAX_LENGTH_SIZE];
214 return ASN1_VALUE_NOT_VALID;
216 if (ETYPE_OK(etype) == 0)
217 return ASN1_VALUE_NOT_VALID;
219 /* doesn't handle constructed classes */
220 if (ETYPE_CLASS(etype) != ASN1_CLASS_UNIVERSAL)
221 return ASN1_VALUE_NOT_VALID;
223 _asn1_tag_der (ETYPE_CLASS(etype), ETYPE_TAG(etype),
226 asn1_length_der(str_len, der_length, &len_len);
228 if (tag_len <= 0 || len_len <= 0)
229 return ASN1_VALUE_NOT_VALID;
231 tlen = tag_len + len_len;
234 return ASN1_MEM_ERROR;
237 memcpy(p, der_tag, tag_len);
239 memcpy(p, der_length, len_len);
246 /******************************************************/
247 /* Function : _asn1_time_der */
248 /* Description: creates the DER coding for a TIME */
249 /* type (length included). */
251 /* str: TIME null-terminated string. */
252 /* der: string returned. */
253 /* der_len: number of meaningful bytes of DER */
254 /* (der[0]..der[ans_len-1]). Initially it */
255 /* if must store the lenght of DER. */
257 /* ASN1_MEM_ERROR when DER isn't big enough */
258 /* ASN1_SUCCESS otherwise */
259 /******************************************************/
261 _asn1_time_der (unsigned char *str, unsigned char *der, int *der_len)
265 int str_len = _asn1_strlen (str);
269 asn1_length_der (str_len, (max_len > 0) ? der : NULL, &len_len);
271 if ((len_len + str_len) <= max_len)
272 memcpy (der + len_len, str, str_len);
273 *der_len = len_len + str_len;
275 if ((*der_len) > max_len)
276 return ASN1_MEM_ERROR;
284 _asn1_get_utctime_der(unsigned char *der,int *der_len,unsigned char *str)
289 if(str==NULL) return;
290 str_len=asn1_get_length_der(der,*der_len,&len_len);
291 if (str_len<0) return;
292 memcpy(temp,der+len_len,str_len);
293 *der_len=str_len+len_len;
297 strcat(temp,"00+0000");
301 strcat(temp,"+0000");
305 memmove(temp+12,temp+10,6);
306 temp[10]=temp[11]='0';
318 /******************************************************/
319 /* Function : _asn1_objectid_der */
320 /* Description: creates the DER coding for an */
321 /* OBJECT IDENTIFIER type (length included). */
323 /* str: OBJECT IDENTIFIER null-terminated string. */
324 /* der: string returned. */
325 /* der_len: number of meaningful bytes of DER */
326 /* (der[0]..der[ans_len-1]). Initially it */
327 /* must store the length of DER. */
329 /* ASN1_MEM_ERROR when DER isn't big enough */
330 /* ASN1_SUCCESS otherwise */
331 /******************************************************/
333 _asn1_objectid_der (unsigned char *str, unsigned char *der, int *der_len)
335 int len_len, counter, k, first, max_len;
336 char *temp, *n_end, *n_start;
338 unsigned long val, val1 = 0;
339 int str_len = _asn1_strlen (str);
343 temp = malloc (str_len + 2);
345 return ASN1_MEM_ALLOC_ERROR;
347 memcpy (temp, str, str_len);
349 temp[str_len + 1] = 0;
353 while ((n_end = strchr (n_start, '.')))
356 val = strtoul (n_start, NULL, 10);
361 else if (counter == 2)
364 der[0] = 40 * val1 + val;
370 for (k = 4; k >= 0; k--)
372 bit7 = (val >> (k * 7)) & 0x7F;
373 if (bit7 || first || !k)
377 if (max_len > (*der_len))
378 der[*der_len] = bit7;
388 asn1_length_der (*der_len, NULL, &len_len);
389 if (max_len >= (*der_len + len_len))
391 memmove (der + len_len, der, *der_len);
392 asn1_length_der (*der_len, der, &len_len);
398 if (max_len < (*der_len))
399 return ASN1_MEM_ERROR;
405 static const unsigned char bit_mask[] =
406 { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
411 * @bit_len: number of meaningful bits in STR.
412 * @der: string returned.
413 * @der_len: number of meaningful bytes of DER
414 * (der[0]..der[ans_len-1]).
416 * Creates a length-value DER encoding for the input data
417 * as it would have been for a BIT STRING.
418 * The DER encoded data will be copied in @der.
420 * Note that the BIT STRING tag is not included in the output.
422 * This function does not return any value because it is expected
423 * that @der_len will contain enough bytes to store the string
424 * plus the DER encoding. The DER encoding size can be obtained using
428 asn1_bit_der (const unsigned char *str, int bit_len,
429 unsigned char *der, int *der_len)
431 int len_len, len_byte, len_pad;
436 len_byte = bit_len >> 3;
437 len_pad = 8 - (bit_len & 7);
442 asn1_length_der (len_byte + 1, der, &len_len);
443 der[len_len] = len_pad;
444 memcpy (der + len_len + 1, str, len_byte);
445 der[len_len + len_byte] &= bit_mask[len_pad];
446 *der_len = len_byte + len_len + 1;
450 /******************************************************/
451 /* Function : _asn1_complete_explicit_tag */
452 /* Description: add the length coding to the EXPLICIT */
455 /* node: pointer to the tree element. */
456 /* der: string with the DER coding of the whole tree*/
457 /* counter: number of meaningful bytes of DER */
458 /* (der[0]..der[*counter-1]). */
459 /* max_len: size of der vector */
461 /* ASN1_MEM_ERROR if der vector isn't big enough, */
462 /* otherwise ASN1_SUCCESS. */
463 /******************************************************/
465 _asn1_complete_explicit_tag (asn1_node node, unsigned char *der,
466 int *counter, int *max_len)
469 int is_tag_implicit, len2, len3;
470 unsigned char temp[SIZEOF_UNSIGNED_INT];
474 if (node->type & CONST_TAG)
477 /* When there are nested tags we must complete them reverse to
478 the order they were created. This is because completing a tag
479 modifies all data within it, including the incomplete tags
480 which store buffer positions -- simon@josefsson.org 2002-09-06
484 while (p && p != node->down->left)
486 if (type_field (p->type) == ASN1_ETYPE_TAG)
488 if (p->type & CONST_EXPLICIT)
490 len2 = strtol (p->name, NULL, 10);
491 _asn1_set_name (p, NULL);
492 asn1_length_der (*counter - len2, temp, &len3);
493 if (len3 <= (*max_len))
495 memmove (der + len2 + len3, der + len2,
497 memcpy (der + len2, temp, len3);
504 { /* CONST_IMPLICIT */
505 if (!is_tag_implicit)
516 return ASN1_MEM_ERROR;
521 const tag_and_class_st _asn1_tags[] =
523 [ASN1_ETYPE_GENERALSTRING] = {ASN1_TAG_GENERALSTRING, ASN1_CLASS_UNIVERSAL, "type:GENERALSTRING"},
524 [ASN1_ETYPE_NUMERIC_STRING] = {ASN1_TAG_NUMERIC_STRING, ASN1_CLASS_UNIVERSAL, "type:NUMERIC_STR"},
525 [ASN1_ETYPE_IA5_STRING] = {ASN1_TAG_IA5_STRING, ASN1_CLASS_UNIVERSAL, "type:IA5_STR"},
526 [ASN1_ETYPE_TELETEX_STRING] = {ASN1_TAG_TELETEX_STRING, ASN1_CLASS_UNIVERSAL, "type:TELETEX_STR"},
527 [ASN1_ETYPE_PRINTABLE_STRING] = {ASN1_TAG_PRINTABLE_STRING, ASN1_CLASS_UNIVERSAL, "type:PRINTABLE_STR"},
528 [ASN1_ETYPE_UNIVERSAL_STRING] = {ASN1_TAG_UNIVERSAL_STRING, ASN1_CLASS_UNIVERSAL, "type:UNIVERSAL_STR"},
529 [ASN1_ETYPE_BMP_STRING] = {ASN1_TAG_BMP_STRING, ASN1_CLASS_UNIVERSAL, "type:BMP_STR"},
530 [ASN1_ETYPE_UTF8_STRING] = {ASN1_TAG_UTF8_STRING, ASN1_CLASS_UNIVERSAL, "type:UTF8_STR"},
531 [ASN1_ETYPE_VISIBLE_STRING] = {ASN1_TAG_VISIBLE_STRING, ASN1_CLASS_UNIVERSAL, "type:VISIBLE_STR"},
532 [ASN1_ETYPE_OCTET_STRING] = {ASN1_TAG_OCTET_STRING, ASN1_CLASS_UNIVERSAL, "type:OCT_STR"},
533 [ASN1_ETYPE_BIT_STRING] = {ASN1_TAG_BIT_STRING, ASN1_CLASS_UNIVERSAL, "type:BIT_STR"},
534 [ASN1_ETYPE_OBJECT_ID] = {ASN1_TAG_OBJECT_ID, ASN1_CLASS_UNIVERSAL, "type:OBJ_STR"},
535 [ASN1_ETYPE_NULL] = {ASN1_TAG_NULL, ASN1_CLASS_UNIVERSAL, "type:NULL"},
536 [ASN1_ETYPE_BOOLEAN] = {ASN1_TAG_BOOLEAN, ASN1_CLASS_UNIVERSAL, "type:BOOLEAN"},
537 [ASN1_ETYPE_INTEGER] = {ASN1_TAG_INTEGER, ASN1_CLASS_UNIVERSAL, "type:INTEGER"},
538 [ASN1_ETYPE_ENUMERATED] = {ASN1_TAG_ENUMERATED, ASN1_CLASS_UNIVERSAL, "type:ENUMERATED"},
539 [ASN1_ETYPE_SEQUENCE] = {ASN1_TAG_SEQUENCE, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED, "type:SEQUENCE"},
540 [ASN1_ETYPE_SEQUENCE_OF] ={ASN1_TAG_SEQUENCE, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED, "type:SEQ_OF"},
541 [ASN1_ETYPE_SET] = {ASN1_TAG_SET, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED, "type:SET"},
542 [ASN1_ETYPE_SET_OF] = {ASN1_TAG_SET, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED, "type:SET_OF"},
543 [ASN1_ETYPE_GENERALIZED_TIME] = {ASN1_TAG_GENERALIZEDTime, ASN1_CLASS_UNIVERSAL, "type:GENERALIZED_TIME"},
544 [ASN1_ETYPE_UTC_TIME] = {ASN1_TAG_UTCTime, ASN1_CLASS_UNIVERSAL, "type:UTC_TIME"},
547 unsigned int _asn1_tags_size = sizeof(_asn1_tags)/sizeof(_asn1_tags[0]);
549 /******************************************************/
550 /* Function : _asn1_insert_tag_der */
551 /* Description: creates the DER coding of tags of one */
554 /* node: pointer to the tree element. */
555 /* der: string returned */
556 /* counter: number of meaningful bytes of DER */
557 /* (counter[0]..der[*counter-1]). */
558 /* max_len: size of der vector */
560 /* ASN1_GENERIC_ERROR if the type is unknown, */
561 /* ASN1_MEM_ERROR if der vector isn't big enough, */
562 /* otherwise ASN1_SUCCESS. */
563 /******************************************************/
565 _asn1_insert_tag_der (asn1_node node, unsigned char *der, int *counter,
569 int tag_len, is_tag_implicit;
570 unsigned char class, class_implicit = 0, temp[SIZEOF_UNSIGNED_INT * 3 + 1];
571 unsigned long tag_implicit = 0;
572 unsigned char tag_der[MAX_TAG_LEN];
576 if (node->type & CONST_TAG)
581 if (type_field (p->type) == ASN1_ETYPE_TAG)
583 if (p->type & CONST_APPLICATION)
584 class = ASN1_CLASS_APPLICATION;
585 else if (p->type & CONST_UNIVERSAL)
586 class = ASN1_CLASS_UNIVERSAL;
587 else if (p->type & CONST_PRIVATE)
588 class = ASN1_CLASS_PRIVATE;
590 class = ASN1_CLASS_CONTEXT_SPECIFIC;
592 if (p->type & CONST_EXPLICIT)
595 _asn1_tag_der (class_implicit, tag_implicit, tag_der,
598 _asn1_tag_der (class | ASN1_CLASS_STRUCTURED,
599 _asn1_strtoul (p->value, NULL, 10),
604 memcpy (der + *counter, tag_der, tag_len);
607 _asn1_ltostr (*counter, (char *) temp);
608 _asn1_set_name (p, (const char *) temp);
613 { /* CONST_IMPLICIT */
614 if (!is_tag_implicit)
616 if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
617 (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF) ||
618 (type_field (node->type) == ASN1_ETYPE_SET) ||
619 (type_field (node->type) == ASN1_ETYPE_SET_OF))
620 class |= ASN1_CLASS_STRUCTURED;
621 class_implicit = class;
622 tag_implicit = _asn1_strtoul (p->value, NULL, 10);
633 _asn1_tag_der (class_implicit, tag_implicit, tag_der, &tag_len);
637 unsigned type = type_field (node->type);
641 _asn1_tag_der (_asn1_tags[type].class, _asn1_tags[type].tag,
645 case ASN1_ETYPE_CHOICE:
650 return ASN1_GENERIC_ERROR;
656 memcpy (der + *counter, tag_der, tag_len);
660 return ASN1_MEM_ERROR;
665 /******************************************************/
666 /* Function : _asn1_ordering_set */
667 /* Description: puts the elements of a SET type in */
668 /* the correct order according to DER rules. */
670 /* der: string with the DER coding. */
671 /* node: pointer to the SET element. */
673 /******************************************************/
675 _asn1_ordering_set (unsigned char *der, int der_len, asn1_node node)
681 struct vet *next, *prev;
684 int counter, len, len2;
685 struct vet *first, *last, *p_vet, *p2_vet;
687 unsigned char class, *temp;
692 if (type_field (node->type) != ASN1_ETYPE_SET)
696 while ((type_field (p->type) == ASN1_ETYPE_TAG)
697 || (type_field (p->type) == ASN1_ETYPE_SIZE))
700 if ((p == NULL) || (p->right == NULL))
706 p_vet = malloc (sizeof (struct vet));
718 /* tag value calculation */
720 (der + counter, der_len - counter, &class, &len2,
721 &tag) != ASN1_SUCCESS)
723 p_vet->value = (class << 24) | tag;
726 /* extraction and length */
727 len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
730 counter += len + len2;
732 p_vet->end = counter;
740 p2_vet = p_vet->next;
744 if (p_vet->value > p2_vet->value)
746 /* change position */
747 temp = malloc (p_vet->end - counter);
751 memcpy (temp, der + counter, p_vet->end - counter);
752 memcpy (der + counter, der + p_vet->end,
753 p2_vet->end - p_vet->end);
754 memcpy (der + counter + p2_vet->end - p_vet->end, temp,
755 p_vet->end - counter);
759 p_vet->value = p2_vet->value;
762 p_vet->end = counter + (p2_vet->end - p_vet->end);
764 counter = p_vet->end;
766 p2_vet = p2_vet->next;
771 p_vet->prev->next = NULL;
779 /******************************************************/
780 /* Function : _asn1_ordering_set_of */
781 /* Description: puts the elements of a SET OF type in */
782 /* the correct order according to DER rules. */
784 /* der: string with the DER coding. */
785 /* node: pointer to the SET OF element. */
787 /******************************************************/
789 _asn1_ordering_set_of (unsigned char *der, int der_len, asn1_node node)
794 struct vet *next, *prev;
797 int counter, len, len2, change;
798 struct vet *first, *last, *p_vet, *p2_vet;
800 unsigned char *temp, class;
801 unsigned long k, max;
805 if (type_field (node->type) != ASN1_ETYPE_SET_OF)
809 while ((type_field (p->type) == ASN1_ETYPE_TAG)
810 || (type_field (p->type) == ASN1_ETYPE_SIZE))
814 if ((p == NULL) || (p->right == NULL))
820 p_vet = malloc (sizeof (struct vet));
832 /* extraction of tag and length */
833 if (der_len - counter > 0)
837 (der + counter, der_len - counter, &class, &len,
838 NULL) != ASN1_SUCCESS)
842 len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
845 counter += len + len2;
848 p_vet->end = counter;
856 p2_vet = p_vet->next;
860 if ((p_vet->end - counter) > (p2_vet->end - p_vet->end))
861 max = p_vet->end - counter;
863 max = p2_vet->end - p_vet->end;
866 for (k = 0; k < max; k++)
867 if (der[counter + k] > der[p_vet->end + k])
872 else if (der[counter + k] < der[p_vet->end + k])
879 && ((p_vet->end - counter) > (p2_vet->end - p_vet->end)))
884 /* change position */
885 temp = malloc (p_vet->end - counter);
889 memcpy (temp, der + counter, (p_vet->end) - counter);
890 memcpy (der + counter, der + (p_vet->end),
891 (p2_vet->end) - (p_vet->end));
892 memcpy (der + counter + (p2_vet->end) - (p_vet->end), temp,
893 (p_vet->end) - counter);
896 p_vet->end = counter + (p2_vet->end - p_vet->end);
898 counter = p_vet->end;
900 p2_vet = p2_vet->next;
905 p_vet->prev->next = NULL;
915 * @element: pointer to an ASN1 element
916 * @name: the name of the structure you want to encode (it must be
918 * @ider: vector that will contain the DER encoding. DER must be a
919 * pointer to memory cells already allocated.
920 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1], Initialy
921 * holds the sizeof of der vector.
922 * @errorDescription : return the error description or an empty
925 * Creates the DER encoding for the NAME structure (inside *POINTER
928 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
929 * if @name is not a valid element, %ASN1_VALUE_NOT_FOUND if there
930 * is an element without a value, %ASN1_MEM_ERROR if the @ider
931 * vector isn't big enough and in this case @len will contain the
935 asn1_der_coding (asn1_node element, const char *name, void *ider, int *len,
936 char *ErrorDescription)
938 asn1_node node, p, p2;
939 unsigned char temp[SIZEOF_UNSIGNED_LONG_INT * 3 + 1];
940 int counter, counter_old, len2, len3, tlen, move, max_len, max_len_old;
942 unsigned char *der = ider;
944 node = asn1_find_node (element, name);
946 return ASN1_ELEMENT_NOT_FOUND;
948 /* Node is now a locally allocated variable.
949 * That is because in some point we modify the
950 * structure, and I don't know why! --nmav
952 node = _asn1_copy_structure3 (node);
954 return ASN1_ELEMENT_NOT_FOUND;
964 counter_old = counter;
965 max_len_old = max_len;
968 err = _asn1_insert_tag_der (p, der, &counter, &max_len);
969 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
972 switch (type_field (p->type))
974 case ASN1_ETYPE_NULL:
981 case ASN1_ETYPE_BOOLEAN:
982 if ((p->type & CONST_DEFAULT) && (p->value == NULL))
984 counter = counter_old;
985 max_len = max_len_old;
989 if (p->value == NULL)
991 _asn1_error_description_value_not_found (p,
993 err = ASN1_VALUE_NOT_FOUND;
1000 if (p->value[0] == 'F')
1003 der[counter++] = 0xFF;
1010 case ASN1_ETYPE_INTEGER:
1011 case ASN1_ETYPE_ENUMERATED:
1012 if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1014 counter = counter_old;
1015 max_len = max_len_old;
1019 if (p->value == NULL)
1021 _asn1_error_description_value_not_found (p,
1023 err = ASN1_VALUE_NOT_FOUND;
1026 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1029 err = ASN1_DER_ERROR;
1032 max_len -= len2 + len3;
1034 memcpy (der + counter, p->value, len3 + len2);
1035 counter += len3 + len2;
1039 case ASN1_ETYPE_OBJECT_ID:
1040 if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1042 counter = counter_old;
1043 max_len = max_len_old;
1047 if (p->value == NULL)
1049 _asn1_error_description_value_not_found (p,
1051 err = ASN1_VALUE_NOT_FOUND;
1055 err = _asn1_objectid_der (p->value, der + counter, &len2);
1056 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1064 case ASN1_ETYPE_GENERALIZED_TIME:
1065 case ASN1_ETYPE_UTC_TIME:
1066 if (p->value == NULL)
1068 _asn1_error_description_value_not_found (p, ErrorDescription);
1069 err = ASN1_VALUE_NOT_FOUND;
1073 err = _asn1_time_der (p->value, der + counter, &len2);
1074 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1081 case ASN1_ETYPE_OCTET_STRING:
1082 case ASN1_ETYPE_GENERALSTRING:
1083 case ASN1_ETYPE_NUMERIC_STRING:
1084 case ASN1_ETYPE_IA5_STRING:
1085 case ASN1_ETYPE_TELETEX_STRING:
1086 case ASN1_ETYPE_PRINTABLE_STRING:
1087 case ASN1_ETYPE_UNIVERSAL_STRING:
1088 case ASN1_ETYPE_BMP_STRING:
1089 case ASN1_ETYPE_UTF8_STRING:
1090 case ASN1_ETYPE_VISIBLE_STRING:
1091 case ASN1_ETYPE_BIT_STRING:
1092 if (p->value == NULL)
1094 _asn1_error_description_value_not_found (p, ErrorDescription);
1095 err = ASN1_VALUE_NOT_FOUND;
1098 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1101 err = ASN1_DER_ERROR;
1104 max_len -= len2 + len3;
1106 memcpy (der + counter, p->value, len3 + len2);
1107 counter += len3 + len2;
1110 case ASN1_ETYPE_SEQUENCE:
1111 case ASN1_ETYPE_SET:
1114 _asn1_ltostr (counter, (char *) temp);
1115 tlen = _asn1_strlen (temp);
1117 _asn1_set_value (p, temp, tlen + 1);
1118 if (p->down == NULL)
1126 while (p2 && (type_field (p2->type) == ASN1_ETYPE_TAG))
1140 len2 = _asn1_strtol (p->value, NULL, 10);
1141 _asn1_set_value (p, NULL, 0);
1142 if ((type_field (p->type) == ASN1_ETYPE_SET) && (max_len >= 0))
1143 _asn1_ordering_set (der + len2, max_len - len2, p);
1144 asn1_length_der (counter - len2, temp, &len3);
1148 memmove (der + len2 + len3, der + len2, counter - len2);
1149 memcpy (der + len2, temp, len3);
1155 case ASN1_ETYPE_SEQUENCE_OF:
1156 case ASN1_ETYPE_SET_OF:
1159 _asn1_ltostr (counter, (char *) temp);
1160 tlen = _asn1_strlen (temp);
1163 _asn1_set_value (p, temp, tlen + 1);
1165 while ((type_field (p->type) == ASN1_ETYPE_TAG)
1166 || (type_field (p->type) == ASN1_ETYPE_SIZE))
1175 p = _asn1_find_up (p);
1180 len2 = _asn1_strtol (p->value, NULL, 10);
1181 _asn1_set_value (p, NULL, 0);
1182 if ((type_field (p->type) == ASN1_ETYPE_SET_OF)
1183 && (max_len - len2 > 0))
1185 _asn1_ordering_set_of (der + len2, max_len - len2, p);
1187 asn1_length_der (counter - len2, temp, &len3);
1191 memmove (der + len2 + len3, der + len2, counter - len2);
1192 memcpy (der + len2, temp, len3);
1198 case ASN1_ETYPE_ANY:
1199 if (p->value == NULL)
1201 _asn1_error_description_value_not_found (p, ErrorDescription);
1202 err = ASN1_VALUE_NOT_FOUND;
1205 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1208 err = ASN1_DER_ERROR;
1213 memcpy (der + counter, p->value + len3, len2);
1218 move = (move == UP) ? RIGHT : DOWN;
1222 if ((move != DOWN) && (counter != counter_old))
1224 err = _asn1_complete_explicit_tag (p, der, &counter, &max_len);
1225 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1229 if (p == node && move != DOWN)
1247 p = _asn1_find_up (p);
1254 err = ASN1_MEM_ERROR;
1261 asn1_delete_structure (&node);