2 * Copyright (C) 2002-2014 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"
34 #include <structure.h>
36 #define MAX_TAG_LEN 16
38 /******************************************************/
39 /* Function : _asn1_error_description_value_not_found */
40 /* Description: creates the ErrorDescription string */
41 /* for the ASN1_VALUE_NOT_FOUND error. */
43 /* node: node of the tree where the value is NULL. */
44 /* ErrorDescription: string returned. */
46 /******************************************************/
48 _asn1_error_description_value_not_found (asn1_node node,
49 char *ErrorDescription)
52 if (ErrorDescription == NULL)
55 Estrcpy (ErrorDescription, ":: value of element '");
56 _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
57 ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
58 Estrcat (ErrorDescription, "' not found");
64 * @len: value to convert.
65 * @der: buffer to hold the returned encoding (may be %NULL).
66 * @der_len: number of meaningful bytes of ANS (der[0]..der[der_len-1]).
68 * Creates the DER encoding of the provided length value.
69 * The @der buffer must have enough room for the output. The maximum
70 * length this function will encode is %ASN1_MAX_LENGTH_SIZE.
72 * To know the size of the DER encoding use a %NULL value for @der.
75 asn1_length_der (unsigned long int len, unsigned char *der, int *der_len)
78 unsigned char temp[ASN1_MAX_LENGTH_SIZE];
79 #if SIZEOF_UNSIGNED_LONG_INT > 8
80 len &= 0xFFFFFFFFFFFFFFFF;
87 der[0] = (unsigned char) len;
96 temp[k++] = len & 0xFF;
102 der[0] = ((unsigned char) k & 0x7F) + 128;
104 der[*der_len - 1 - k] = temp[k];
109 /******************************************************/
110 /* Function : _asn1_tag_der */
111 /* Description: creates the DER coding for the CLASS */
112 /* and TAG parameters. */
113 /* It is limited by the ASN1_MAX_TAG_SIZE variable */
115 /* class: value to convert. */
116 /* tag_value: value to convert. */
117 /* ans: string returned. */
118 /* ans_len: number of meaningful bytes of ANS */
119 /* (ans[0]..ans[ans_len-1]). */
121 /******************************************************/
123 _asn1_tag_der (unsigned char class, unsigned int tag_value,
124 unsigned char *ans, int *ans_len)
127 unsigned char temp[ASN1_MAX_TAG_SIZE];
132 ans[0] = (class & 0xE0) + ((unsigned char) (tag_value & 0x1F));
138 ans[0] = (class & 0xE0) + 31;
140 while (tag_value != 0)
142 temp[k++] = tag_value & 0x7F;
145 if (k > ASN1_MAX_TAG_SIZE - 1)
146 break; /* will not encode larger tags */
150 ans[*ans_len - 1 - k] = temp[k] + 128;
151 ans[*ans_len - 1] -= 128;
157 * @str: the input data.
158 * @str_len: STR length (str[0]..str[*str_len-1]).
159 * @der: encoded string returned.
160 * @der_len: number of meaningful bytes of DER (der[0]..der[der_len-1]).
162 * Creates a length-value DER encoding for the input data.
163 * The DER encoding of the input data will be placed in the @der variable.
165 * Note that the OCTET STRING tag is not included in the output.
167 * This function does not return any value because it is expected
168 * that @der_len will contain enough bytes to store the string
169 * plus the DER encoding. The DER encoding size can be obtained using
173 asn1_octet_der (const unsigned char *str, int str_len,
174 unsigned char *der, int *der_len)
178 if (der == NULL || str_len < 0)
181 asn1_length_der (str_len, der, &len_len);
182 memcpy (der + len_len, str, str_len);
183 *der_len = str_len + len_len;
188 * asn1_encode_simple_der:
189 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
190 * @str: the string data.
191 * @str_len: the string length
192 * @tl: the encoded tag and length
193 * @tl_len: the bytes of the @tl field
195 * Creates the DER encoding for various simple ASN.1 types like strings etc.
196 * It stores the tag and length in @tl, which should have space for at least
197 * %ASN1_MAX_TL_SIZE bytes. Initially @tl_len should contain the size of @tl.
199 * The complete DER encoding should consist of the value in @tl appended
200 * with the provided @str.
202 * Returns: %ASN1_SUCCESS if successful or an error value.
205 asn1_encode_simple_der (unsigned int etype, const unsigned char *str,
206 unsigned int str_len, unsigned char *tl,
207 unsigned int *tl_len)
209 int tag_len, len_len;
211 unsigned char der_tag[ASN1_MAX_TAG_SIZE];
212 unsigned char der_length[ASN1_MAX_LENGTH_SIZE];
216 return ASN1_VALUE_NOT_VALID;
218 if (ETYPE_OK (etype) == 0)
219 return ASN1_VALUE_NOT_VALID;
221 /* doesn't handle constructed classes */
222 if (ETYPE_CLASS (etype) != ASN1_CLASS_UNIVERSAL)
223 return ASN1_VALUE_NOT_VALID;
225 _asn1_tag_der (ETYPE_CLASS (etype), ETYPE_TAG (etype), der_tag, &tag_len);
227 asn1_length_der (str_len, der_length, &len_len);
229 if (tag_len <= 0 || len_len <= 0)
230 return ASN1_VALUE_NOT_VALID;
232 tlen = tag_len + len_len;
235 return ASN1_MEM_ERROR;
238 memcpy (p, der_tag, tag_len);
240 memcpy (p, der_length, len_len);
247 /******************************************************/
248 /* Function : _asn1_time_der */
249 /* Description: creates the DER coding for a TIME */
250 /* type (length included). */
252 /* str: TIME null-terminated string. */
253 /* der: string returned. */
254 /* der_len: number of meaningful bytes of DER */
255 /* (der[0]..der[ans_len-1]). Initially it */
256 /* if must store the lenght of DER. */
258 /* ASN1_MEM_ERROR when DER isn't big enough */
259 /* ASN1_SUCCESS otherwise */
260 /******************************************************/
262 _asn1_time_der (unsigned char *str, int str_len, unsigned char *der,
270 asn1_length_der (str_len, (max_len > 0) ? der : NULL, &len_len);
272 if ((len_len + str_len) <= max_len)
273 memcpy (der + len_len, str, str_len);
274 *der_len = len_len + str_len;
276 if ((*der_len) > max_len)
277 return ASN1_MEM_ERROR;
285 _asn1_get_utctime_der(unsigned char *der,int *der_len,unsigned char *str)
290 if(str==NULL) return;
291 str_len=asn1_get_length_der(der,*der_len,&len_len);
292 if (str_len<0) return;
293 memcpy(temp,der+len_len,str_len);
294 *der_len=str_len+len_len;
298 strcat(temp,"00+0000");
302 strcat(temp,"+0000");
306 memmove(temp+12,temp+10,6);
307 temp[10]=temp[11]='0';
319 /******************************************************/
320 /* Function : _asn1_objectid_der */
321 /* Description: creates the DER coding for an */
322 /* OBJECT IDENTIFIER type (length included). */
324 /* str: OBJECT IDENTIFIER null-terminated string. */
325 /* der: string returned. */
326 /* der_len: number of meaningful bytes of DER */
327 /* (der[0]..der[ans_len-1]). Initially it */
328 /* must store the length of DER. */
330 /* ASN1_MEM_ERROR when DER isn't big enough */
331 /* ASN1_SUCCESS if succesful */
332 /* or an error value. */
333 /******************************************************/
335 _asn1_objectid_der (unsigned char *str, unsigned char *der, int *der_len)
337 int len_len, counter, k, first, max_len;
338 char *temp, *n_end, *n_start;
340 unsigned long val, val1 = 0;
341 int str_len = _asn1_strlen (str);
345 temp = malloc (str_len + 2);
347 return ASN1_MEM_ALLOC_ERROR;
349 memcpy (temp, str, str_len);
351 temp[str_len + 1] = 0;
355 while ((n_end = strchr (n_start, '.')))
358 val = strtoul (n_start, NULL, 10);
363 else if (counter == 2)
366 der[0] = 40 * val1 + val;
372 for (k = 4; k >= 0; k--)
374 bit7 = (val >> (k * 7)) & 0x7F;
375 if (bit7 || first || !k)
379 if (max_len > (*der_len))
380 der[*der_len] = bit7;
390 asn1_length_der (*der_len, NULL, &len_len);
391 if (max_len >= (*der_len + len_len))
393 memmove (der + len_len, der, *der_len);
394 asn1_length_der (*der_len, der, &len_len);
400 if (max_len < (*der_len))
401 return ASN1_MEM_ERROR;
407 static const unsigned char bit_mask[] =
408 { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
413 * @bit_len: number of meaningful bits in STR.
414 * @der: string returned.
415 * @der_len: number of meaningful bytes of DER
416 * (der[0]..der[ans_len-1]).
418 * Creates a length-value DER encoding for the input data
419 * as it would have been for a BIT STRING.
420 * The DER encoded data will be copied in @der.
422 * Note that the BIT STRING tag is not included in the output.
424 * This function does not return any value because it is expected
425 * that @der_len will contain enough bytes to store the string
426 * plus the DER encoding. The DER encoding size can be obtained using
430 asn1_bit_der (const unsigned char *str, int bit_len,
431 unsigned char *der, int *der_len)
433 int len_len, len_byte, len_pad;
438 len_byte = bit_len >> 3;
439 len_pad = 8 - (bit_len & 7);
444 asn1_length_der (len_byte + 1, der, &len_len);
445 der[len_len] = len_pad;
446 memcpy (der + len_len + 1, str, len_byte);
447 der[len_len + len_byte] &= bit_mask[len_pad];
448 *der_len = len_byte + len_len + 1;
452 /******************************************************/
453 /* Function : _asn1_complete_explicit_tag */
454 /* Description: add the length coding to the EXPLICIT */
457 /* node: pointer to the tree element. */
458 /* der: string with the DER coding of the whole tree*/
459 /* counter: number of meaningful bytes of DER */
460 /* (der[0]..der[*counter-1]). */
461 /* max_len: size of der vector */
463 /* ASN1_MEM_ERROR if der vector isn't big enough, */
464 /* otherwise ASN1_SUCCESS. */
465 /******************************************************/
467 _asn1_complete_explicit_tag (asn1_node node, unsigned char *der,
468 int *counter, int *max_len)
471 int is_tag_implicit, len2, len3;
472 unsigned char temp[SIZEOF_UNSIGNED_INT];
476 if (node->type & CONST_TAG)
480 return ASN1_DER_ERROR;
481 /* When there are nested tags we must complete them reverse to
482 the order they were created. This is because completing a tag
483 modifies all data within it, including the incomplete tags
484 which store buffer positions -- simon@josefsson.org 2002-09-06
488 while (p && p != node->down->left)
490 if (type_field (p->type) == ASN1_ETYPE_TAG)
492 if (p->type & CONST_EXPLICIT)
494 len2 = strtol (p->name, NULL, 10);
495 _asn1_set_name (p, NULL);
497 asn1_length_der (*counter - len2, temp, &len3);
498 if (len3 <= (*max_len))
500 memmove (der + len2 + len3, der + len2,
502 memcpy (der + len2, temp, len3);
509 { /* CONST_IMPLICIT */
510 if (!is_tag_implicit)
521 return ASN1_MEM_ERROR;
526 const tag_and_class_st _asn1_tags[] = {
527 [ASN1_ETYPE_GENERALSTRING] =
528 {ASN1_TAG_GENERALSTRING, ASN1_CLASS_UNIVERSAL, "type:GENERALSTRING"},
529 [ASN1_ETYPE_NUMERIC_STRING] =
530 {ASN1_TAG_NUMERIC_STRING, ASN1_CLASS_UNIVERSAL, "type:NUMERIC_STR"},
531 [ASN1_ETYPE_IA5_STRING] =
532 {ASN1_TAG_IA5_STRING, ASN1_CLASS_UNIVERSAL, "type:IA5_STR"},
533 [ASN1_ETYPE_TELETEX_STRING] =
534 {ASN1_TAG_TELETEX_STRING, ASN1_CLASS_UNIVERSAL, "type:TELETEX_STR"},
535 [ASN1_ETYPE_PRINTABLE_STRING] =
536 {ASN1_TAG_PRINTABLE_STRING, ASN1_CLASS_UNIVERSAL, "type:PRINTABLE_STR"},
537 [ASN1_ETYPE_UNIVERSAL_STRING] =
538 {ASN1_TAG_UNIVERSAL_STRING, ASN1_CLASS_UNIVERSAL, "type:UNIVERSAL_STR"},
539 [ASN1_ETYPE_BMP_STRING] =
540 {ASN1_TAG_BMP_STRING, ASN1_CLASS_UNIVERSAL, "type:BMP_STR"},
541 [ASN1_ETYPE_UTF8_STRING] =
542 {ASN1_TAG_UTF8_STRING, ASN1_CLASS_UNIVERSAL, "type:UTF8_STR"},
543 [ASN1_ETYPE_VISIBLE_STRING] =
544 {ASN1_TAG_VISIBLE_STRING, ASN1_CLASS_UNIVERSAL, "type:VISIBLE_STR"},
545 [ASN1_ETYPE_OCTET_STRING] =
546 {ASN1_TAG_OCTET_STRING, ASN1_CLASS_UNIVERSAL, "type:OCT_STR"},
547 [ASN1_ETYPE_BIT_STRING] =
548 {ASN1_TAG_BIT_STRING, ASN1_CLASS_UNIVERSAL, "type:BIT_STR"},
549 [ASN1_ETYPE_OBJECT_ID] =
550 {ASN1_TAG_OBJECT_ID, ASN1_CLASS_UNIVERSAL, "type:OBJ_ID"},
551 [ASN1_ETYPE_NULL] = {ASN1_TAG_NULL, ASN1_CLASS_UNIVERSAL, "type:NULL"},
552 [ASN1_ETYPE_BOOLEAN] =
553 {ASN1_TAG_BOOLEAN, ASN1_CLASS_UNIVERSAL, "type:BOOLEAN"},
554 [ASN1_ETYPE_INTEGER] =
555 {ASN1_TAG_INTEGER, ASN1_CLASS_UNIVERSAL, "type:INTEGER"},
556 [ASN1_ETYPE_ENUMERATED] =
557 {ASN1_TAG_ENUMERATED, ASN1_CLASS_UNIVERSAL, "type:ENUMERATED"},
558 [ASN1_ETYPE_SEQUENCE] =
559 {ASN1_TAG_SEQUENCE, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED,
561 [ASN1_ETYPE_SEQUENCE_OF] =
562 {ASN1_TAG_SEQUENCE, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED,
565 {ASN1_TAG_SET, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED, "type:SET"},
566 [ASN1_ETYPE_SET_OF] =
567 {ASN1_TAG_SET, ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED,
569 [ASN1_ETYPE_GENERALIZED_TIME] =
570 {ASN1_TAG_GENERALIZEDTime, ASN1_CLASS_UNIVERSAL, "type:GENERALIZED_TIME"},
571 [ASN1_ETYPE_UTC_TIME] =
572 {ASN1_TAG_UTCTime, ASN1_CLASS_UNIVERSAL, "type:UTC_TIME"},
575 unsigned int _asn1_tags_size = sizeof (_asn1_tags) / sizeof (_asn1_tags[0]);
577 /******************************************************/
578 /* Function : _asn1_insert_tag_der */
579 /* Description: creates the DER coding of tags of one */
582 /* node: pointer to the tree element. */
583 /* der: string returned */
584 /* counter: number of meaningful bytes of DER */
585 /* (counter[0]..der[*counter-1]). */
586 /* max_len: size of der vector */
588 /* ASN1_GENERIC_ERROR if the type is unknown, */
589 /* ASN1_MEM_ERROR if der vector isn't big enough, */
590 /* otherwise ASN1_SUCCESS. */
591 /******************************************************/
593 _asn1_insert_tag_der (asn1_node node, unsigned char *der, int *counter,
597 int tag_len, is_tag_implicit;
598 unsigned char class, class_implicit = 0, temp[MAX(SIZEOF_UNSIGNED_INT * 3 + 1, LTOSTR_MAX_SIZE)];
599 unsigned long tag_implicit = 0;
600 unsigned char tag_der[MAX_TAG_LEN];
604 if (node->type & CONST_TAG)
609 if (type_field (p->type) == ASN1_ETYPE_TAG)
611 if (p->type & CONST_APPLICATION)
612 class = ASN1_CLASS_APPLICATION;
613 else if (p->type & CONST_UNIVERSAL)
614 class = ASN1_CLASS_UNIVERSAL;
615 else if (p->type & CONST_PRIVATE)
616 class = ASN1_CLASS_PRIVATE;
618 class = ASN1_CLASS_CONTEXT_SPECIFIC;
620 if (p->type & CONST_EXPLICIT)
623 _asn1_tag_der (class_implicit, tag_implicit, tag_der,
626 _asn1_tag_der (class | ASN1_CLASS_STRUCTURED,
627 _asn1_strtoul (p->value, NULL, 10),
632 memcpy (der + *counter, tag_der, tag_len);
635 _asn1_ltostr (*counter, (char *) temp);
636 _asn1_set_name (p, (const char *) temp);
641 { /* CONST_IMPLICIT */
642 if (!is_tag_implicit)
644 if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
645 (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
646 || (type_field (node->type) == ASN1_ETYPE_SET)
647 || (type_field (node->type) == ASN1_ETYPE_SET_OF))
648 class |= ASN1_CLASS_STRUCTURED;
649 class_implicit = class;
650 tag_implicit = _asn1_strtoul (p->value, NULL, 10);
661 _asn1_tag_der (class_implicit, tag_implicit, tag_der, &tag_len);
665 unsigned type = type_field (node->type);
669 _asn1_tag_der (_asn1_tags[type].class, _asn1_tags[type].tag,
673 case ASN1_ETYPE_CHOICE:
678 return ASN1_GENERIC_ERROR;
684 memcpy (der + *counter, tag_der, tag_len);
688 return ASN1_MEM_ERROR;
693 /******************************************************/
694 /* Function : _asn1_ordering_set */
695 /* Description: puts the elements of a SET type in */
696 /* the correct order according to DER rules. */
698 /* der: string with the DER coding. */
699 /* node: pointer to the SET element. */
701 /* ASN1_SUCCESS if successful */
702 /* or an error value. */
703 /******************************************************/
705 _asn1_ordering_set (unsigned char *der, int der_len, asn1_node node)
711 struct vet *next, *prev;
714 int counter, len, len2;
715 struct vet *first, *last, *p_vet, *p2_vet;
717 unsigned char class, *temp;
718 unsigned long tag, t;
723 if (type_field (node->type) != ASN1_ETYPE_SET)
724 return ASN1_VALUE_NOT_VALID;
727 while (p && ((type_field (p->type) == ASN1_ETYPE_TAG) ||
728 (type_field (p->type) == ASN1_ETYPE_SIZE)))
731 if ((p == NULL) || (p->right == NULL))
737 p_vet = malloc (sizeof (struct vet));
740 err = ASN1_MEM_ALLOC_ERROR;
752 /* tag value calculation */
753 err = asn1_get_tag_der (der + counter, der_len - counter, &class, &len2,
755 if (err != ASN1_SUCCESS)
759 p_vet->value = t | tag;
762 /* extraction and length */
763 len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
766 err = ASN1_DER_ERROR;
769 counter += len + len2;
771 p_vet->end = counter;
779 p2_vet = p_vet->next;
783 if (p_vet->value > p2_vet->value)
785 /* change position */
786 temp = malloc (p_vet->end - counter);
789 err = ASN1_MEM_ALLOC_ERROR;
793 memcpy (temp, der + counter, p_vet->end - counter);
794 memcpy (der + counter, der + p_vet->end,
795 p2_vet->end - p_vet->end);
796 memcpy (der + counter + p2_vet->end - p_vet->end, temp,
797 p_vet->end - counter);
801 p_vet->value = p2_vet->value;
804 p_vet->end = counter + (p2_vet->end - p_vet->end);
806 counter = p_vet->end;
808 p2_vet = p2_vet->next;
813 p_vet->prev->next = NULL;
822 while (first != NULL)
831 /******************************************************/
832 /* Function : _asn1_ordering_set_of */
833 /* Description: puts the elements of a SET OF type in */
834 /* the correct order according to DER rules. */
836 /* der: string with the DER coding. */
837 /* node: pointer to the SET OF element. */
839 /* ASN1_SUCCESS if successful */
840 /* or an error value. */
841 /******************************************************/
843 _asn1_ordering_set_of (unsigned char *der, int der_len, asn1_node node)
848 struct vet *next, *prev;
851 int counter, len, len2, change;
852 struct vet *first, *last, *p_vet, *p2_vet;
854 unsigned char *temp, class;
855 unsigned long k, length;
860 if (type_field (node->type) != ASN1_ETYPE_SET_OF)
861 return ASN1_VALUE_NOT_VALID;
864 while (p && ((type_field (p->type) == ASN1_ETYPE_TAG) ||
865 (type_field (p->type) == ASN1_ETYPE_SIZE)))
868 return ASN1_VALUE_NOT_VALID;
871 if ((p == NULL) || (p->right == NULL))
877 p_vet = malloc (sizeof (struct vet));
880 err = ASN1_MEM_ALLOC_ERROR;
892 /* extraction of tag and length */
893 if (der_len - counter > 0)
896 err = asn1_get_tag_der (der + counter, der_len - counter, &class,
898 if (err != ASN1_SUCCESS)
902 len2 = asn1_get_length_der (der + counter, der_len - counter, &len);
905 err = ASN1_DER_ERROR;
908 counter += len + len2;
912 err = ASN1_DER_ERROR;
916 p_vet->end = counter;
924 p2_vet = p_vet->next;
928 length = MIN(p_vet->end - counter, p2_vet->end - p_vet->end);
930 for (k = 0; k < length; k++)
931 if (der[counter + k] > der[p_vet->end + k])
936 else if (der[counter + k] < der[p_vet->end + k])
943 && ((p_vet->end - counter) > (p2_vet->end - p_vet->end)))
948 /* change position */
949 temp = malloc (p_vet->end - counter);
952 err = ASN1_MEM_ALLOC_ERROR;
956 memcpy (temp, der + counter, (p_vet->end) - counter);
957 memcpy (der + counter, der + (p_vet->end),
958 (p2_vet->end) - (p_vet->end));
959 memcpy (der + counter + (p2_vet->end) - (p_vet->end), temp,
960 (p_vet->end) - counter);
963 p_vet->end = counter + (p2_vet->end - p_vet->end);
965 counter = p_vet->end;
967 p2_vet = p2_vet->next;
972 p_vet->prev->next = NULL;
981 while (first != NULL)
992 * @element: pointer to an ASN1 element
993 * @name: the name of the structure you want to encode (it must be
995 * @ider: vector that will contain the DER encoding. DER must be a
996 * pointer to memory cells already allocated.
997 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1], Initialy
998 * holds the sizeof of der vector.
999 * @ErrorDescription: return the error description or an empty
1000 * string if success.
1002 * Creates the DER encoding for the NAME structure (inside *POINTER
1005 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1006 * if @name is not a valid element, %ASN1_VALUE_NOT_FOUND if there
1007 * is an element without a value, %ASN1_MEM_ERROR if the @ider
1008 * vector isn't big enough and in this case @len will contain the
1012 asn1_der_coding (asn1_node element, const char *name, void *ider, int *len,
1013 char *ErrorDescription)
1015 asn1_node node, p, p2;
1016 unsigned char temp[MAX(LTOSTR_MAX_SIZE, SIZEOF_UNSIGNED_LONG_INT * 3 + 1)];
1017 int counter, counter_old, len2, len3, move, max_len, max_len_old;
1019 unsigned char *der = ider;
1021 node = asn1_find_node (element, name);
1023 return ASN1_ELEMENT_NOT_FOUND;
1025 /* Node is now a locally allocated variable.
1026 * That is because in some point we modify the
1027 * structure, and I don't know why! --nmav
1029 node = _asn1_copy_structure3 (node);
1031 return ASN1_ELEMENT_NOT_FOUND;
1041 counter_old = counter;
1042 max_len_old = max_len;
1045 err = _asn1_insert_tag_der (p, der, &counter, &max_len);
1046 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1049 switch (type_field (p->type))
1051 case ASN1_ETYPE_NULL:
1058 case ASN1_ETYPE_BOOLEAN:
1059 if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1061 counter = counter_old;
1062 max_len = max_len_old;
1066 if (p->value == NULL)
1068 _asn1_error_description_value_not_found (p,
1070 err = ASN1_VALUE_NOT_FOUND;
1077 if (p->value[0] == 'F')
1080 der[counter++] = 0xFF;
1087 case ASN1_ETYPE_INTEGER:
1088 case ASN1_ETYPE_ENUMERATED:
1089 if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1091 counter = counter_old;
1092 max_len = max_len_old;
1096 if (p->value == NULL)
1098 _asn1_error_description_value_not_found (p,
1100 err = ASN1_VALUE_NOT_FOUND;
1103 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1106 err = ASN1_DER_ERROR;
1109 max_len -= len2 + len3;
1111 memcpy (der + counter, p->value, len3 + len2);
1112 counter += len3 + len2;
1116 case ASN1_ETYPE_OBJECT_ID:
1117 if ((p->type & CONST_DEFAULT) && (p->value == NULL))
1119 counter = counter_old;
1120 max_len = max_len_old;
1124 if (p->value == NULL)
1126 _asn1_error_description_value_not_found (p,
1128 err = ASN1_VALUE_NOT_FOUND;
1132 err = _asn1_objectid_der (p->value, der + counter, &len2);
1133 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1141 case ASN1_ETYPE_GENERALIZED_TIME:
1142 case ASN1_ETYPE_UTC_TIME:
1143 if (p->value == NULL)
1145 _asn1_error_description_value_not_found (p, ErrorDescription);
1146 err = ASN1_VALUE_NOT_FOUND;
1150 err = _asn1_time_der (p->value, p->value_len, der + counter, &len2);
1151 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1158 case ASN1_ETYPE_OCTET_STRING:
1159 case ASN1_ETYPE_GENERALSTRING:
1160 case ASN1_ETYPE_NUMERIC_STRING:
1161 case ASN1_ETYPE_IA5_STRING:
1162 case ASN1_ETYPE_TELETEX_STRING:
1163 case ASN1_ETYPE_PRINTABLE_STRING:
1164 case ASN1_ETYPE_UNIVERSAL_STRING:
1165 case ASN1_ETYPE_BMP_STRING:
1166 case ASN1_ETYPE_UTF8_STRING:
1167 case ASN1_ETYPE_VISIBLE_STRING:
1168 case ASN1_ETYPE_BIT_STRING:
1169 if (p->value == NULL)
1171 _asn1_error_description_value_not_found (p, ErrorDescription);
1172 err = ASN1_VALUE_NOT_FOUND;
1175 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1178 err = ASN1_DER_ERROR;
1181 max_len -= len2 + len3;
1183 memcpy (der + counter, p->value, len3 + len2);
1184 counter += len3 + len2;
1187 case ASN1_ETYPE_SEQUENCE:
1188 case ASN1_ETYPE_SET:
1191 p->tmp_ival = counter;
1192 if (p->down == NULL)
1200 while (p2 && (type_field (p2->type) == ASN1_ETYPE_TAG))
1216 if ((type_field (p->type) == ASN1_ETYPE_SET) && (max_len >= 0))
1218 err = _asn1_ordering_set (der + len2, counter - len2, p);
1219 if (err != ASN1_SUCCESS)
1222 asn1_length_der (counter - len2, temp, &len3);
1226 memmove (der + len2 + len3, der + len2, counter - len2);
1227 memcpy (der + len2, temp, len3);
1233 case ASN1_ETYPE_SEQUENCE_OF:
1234 case ASN1_ETYPE_SET_OF:
1237 p->tmp_ival = counter;
1239 while ((type_field (p->type) == ASN1_ETYPE_TAG)
1240 || (type_field (p->type) == ASN1_ETYPE_SIZE))
1249 p = _asn1_get_up (p);
1256 if ((type_field (p->type) == ASN1_ETYPE_SET_OF)
1257 && (counter - len2 > 0) && (max_len >= 0))
1259 err = _asn1_ordering_set_of (der + len2, counter - len2, p);
1260 if (err != ASN1_SUCCESS)
1263 asn1_length_der (counter - len2, temp, &len3);
1267 memmove (der + len2 + len3, der + len2, counter - len2);
1268 memcpy (der + len2, temp, len3);
1274 case ASN1_ETYPE_ANY:
1275 if (p->value == NULL)
1277 _asn1_error_description_value_not_found (p, ErrorDescription);
1278 err = ASN1_VALUE_NOT_FOUND;
1281 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
1284 err = ASN1_DER_ERROR;
1289 memcpy (der + counter, p->value + len3, len2);
1294 move = (move == UP) ? RIGHT : DOWN;
1298 if ((move != DOWN) && (counter != counter_old))
1300 err = _asn1_complete_explicit_tag (p, der, &counter, &max_len);
1301 if (err != ASN1_SUCCESS && err != ASN1_MEM_ERROR)
1305 if (p == node && move != DOWN)
1323 p = _asn1_get_up (p);
1330 err = ASN1_MEM_ERROR;
1337 asn1_delete_structure (&node);