2 * Copyright (C) 2004, 2006, 2008 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * The LIBTASN1 library is free software; you can redistribute it
8 * and/or modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 /*****************************************************/
26 /* Description: Functions to create a DER coding of */
28 /*****************************************************/
32 #include "parser_aux.h"
35 #include <structure.h>
37 #define MAX_TAG_LEN 16
39 /******************************************************/
40 /* Function : _asn1_error_description_value_not_found */
41 /* Description: creates the ErrorDescription string */
42 /* for the ASN1_VALUE_NOT_FOUND error. */
44 /* node: node of the tree where the value is NULL. */
45 /* ErrorDescription: string returned. */
47 /******************************************************/
49 _asn1_error_description_value_not_found (node_asn
* node
,
50 char *ErrorDescription
)
53 if (ErrorDescription
== NULL
)
56 Estrcpy (ErrorDescription
, ":: value of element '");
57 _asn1_hierarchical_name (node
, ErrorDescription
+ strlen (ErrorDescription
),
58 MAX_ERROR_DESCRIPTION_SIZE
- 40);
59 Estrcat (ErrorDescription
, "' not found");
65 * @len: value to convert.
66 * @ans: string returned.
67 * @ans_len: number of meaningful bytes of ANS (ans[0]..ans[ans_len-1]).
69 * Creates the DER coding for the LEN parameter (only the length).
70 * The @ans buffer is pre-allocated and must have room for the output.
73 asn1_length_der (unsigned long int len
, unsigned char *ans
, int *ans_len
)
76 unsigned char temp
[SIZEOF_UNSIGNED_LONG_INT
];
82 ans
[0] = (unsigned char) len
;
91 temp
[k
++] = len
& 0xFF;
97 ans
[0] = ((unsigned char) k
& 0x7F) + 128;
99 ans
[*ans_len
- 1 - k
] = temp
[k
];
104 /******************************************************/
105 /* Function : _asn1_tag_der */
106 /* Description: creates the DER coding for the CLASS */
107 /* and TAG parameters. */
109 /* class: value to convert. */
110 /* tag_value: value to convert. */
111 /* ans: string returned. */
112 /* ans_len: number of meaningful bytes of ANS */
113 /* (ans[0]..ans[ans_len-1]). */
115 /******************************************************/
117 _asn1_tag_der (unsigned char class, unsigned int tag_value
,
118 unsigned char *ans
, int *ans_len
)
121 unsigned char temp
[SIZEOF_UNSIGNED_INT
];
126 ans
[0] = (class & 0xE0) + ((unsigned char) (tag_value
& 0x1F));
132 ans
[0] = (class & 0xE0) + 31;
136 temp
[k
++] = tag_value
& 0x7F;
137 tag_value
= tag_value
>> 7;
141 ans
[*ans_len
- 1 - k
] = temp
[k
] + 128;
142 ans
[*ans_len
- 1] -= 128;
148 * @str: OCTET string.
149 * @str_len: STR length (str[0]..str[str_len-1]).
150 * @der: string returned.
151 * @der_len: number of meaningful bytes of DER (der[0]..der[ans_len-1]).
153 * Creates the DER coding for an OCTET type (length included).
156 asn1_octet_der (const unsigned char *str
, int str_len
,
157 unsigned char *der
, int *der_len
)
161 if (der
== NULL
|| str_len
< 0)
163 asn1_length_der (str_len
, der
, &len_len
);
164 memcpy (der
+ len_len
, str
, str_len
);
165 *der_len
= str_len
+ len_len
;
168 /******************************************************/
169 /* Function : _asn1_time_der */
170 /* Description: creates the DER coding for a TIME */
171 /* type (length included). */
173 /* str: TIME null-terminated string. */
174 /* der: string returned. */
175 /* der_len: number of meaningful bytes of DER */
176 /* (der[0]..der[ans_len-1]). Initially it */
177 /* if must store the lenght of DER. */
179 /* ASN1_MEM_ERROR when DER isn't big enough */
180 /* ASN1_SUCCESS otherwise */
181 /******************************************************/
183 _asn1_time_der (unsigned char *str
, unsigned char *der
, int *der_len
)
190 asn1_length_der (strlen (str
), (max_len
> 0) ? der
: NULL
, &len_len
);
192 if ((len_len
+ (int) strlen (str
)) <= max_len
)
193 memcpy (der
+ len_len
, str
, strlen (str
));
194 *der_len
= len_len
+ strlen (str
);
196 if ((*der_len
) > max_len
)
197 return ASN1_MEM_ERROR
;
205 _asn1_get_utctime_der(unsigned char *der,int *der_len,unsigned char *str)
210 if(str==NULL) return;
211 str_len=asn1_get_length_der(der,*der_len,&len_len);
212 if (str_len<0) return;
213 memcpy(temp,der+len_len,str_len);
214 *der_len=str_len+len_len;
218 strcat(temp,"00+0000");
222 strcat(temp,"+0000");
226 memmove(temp+12,temp+10,6);
227 temp[10]=temp[11]='0';
239 /******************************************************/
240 /* Function : _asn1_objectid_der */
241 /* Description: creates the DER coding for an */
242 /* OBJECT IDENTIFIER type (length included). */
244 /* str: OBJECT IDENTIFIER null-terminated string. */
245 /* der: string returned. */
246 /* der_len: number of meaningful bytes of DER */
247 /* (der[0]..der[ans_len-1]). Initially it */
248 /* must store the length of DER. */
250 /* ASN1_MEM_ERROR when DER isn't big enough */
251 /* ASN1_SUCCESS otherwise */
252 /******************************************************/
254 _asn1_objectid_der (unsigned char *str
, unsigned char *der
, int *der_len
)
256 int len_len
, counter
, k
, first
, max_len
;
257 char *temp
, *n_end
, *n_start
;
259 unsigned long val
, val1
= 0;
263 temp
= (char *) _asn1_malloc (strlen (str
) + 2);
265 return ASN1_MEM_ALLOC_ERROR
;
272 while ((n_end
= strchr (n_start
, '.')))
275 val
= strtoul (n_start
, NULL
, 10);
280 else if (counter
== 2)
283 der
[0] = 40 * val1
+ val
;
289 for (k
= 4; k
>= 0; k
--)
291 bit7
= (val
>> (k
* 7)) & 0x7F;
292 if (bit7
|| first
|| !k
)
296 if (max_len
> (*der_len
))
297 der
[*der_len
] = bit7
;
307 asn1_length_der (*der_len
, NULL
, &len_len
);
308 if (max_len
>= (*der_len
+ len_len
))
310 memmove (der
+ len_len
, der
, *der_len
);
311 asn1_length_der (*der_len
, der
, &len_len
);
317 if (max_len
< (*der_len
))
318 return ASN1_MEM_ERROR
;
324 const char bit_mask
[] = { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
329 * @bit_len: number of meaningful bits in STR.
330 * @der: string returned.
331 * @der_len: number of meaningful bytes of DER
332 * (der[0]..der[ans_len-1]).
334 * Creates the DER coding for a BIT STRING type (length and pad
338 asn1_bit_der (const unsigned char *str
, int bit_len
,
339 unsigned char *der
, int *der_len
)
341 int len_len
, len_byte
, len_pad
;
345 len_byte
= bit_len
>> 3;
346 len_pad
= 8 - (bit_len
& 7);
351 asn1_length_der (len_byte
+ 1, der
, &len_len
);
352 der
[len_len
] = len_pad
;
353 memcpy (der
+ len_len
+ 1, str
, len_byte
);
354 der
[len_len
+ len_byte
] &= bit_mask
[len_pad
];
355 *der_len
= len_byte
+ len_len
+ 1;
359 /******************************************************/
360 /* Function : _asn1_complete_explicit_tag */
361 /* Description: add the length coding to the EXPLICIT */
364 /* node: pointer to the tree element. */
365 /* der: string with the DER coding of the whole tree*/
366 /* counter: number of meaningful bytes of DER */
367 /* (der[0]..der[*counter-1]). */
368 /* max_len: size of der vector */
370 /* ASN1_MEM_ERROR if der vector isn't big enough, */
371 /* otherwise ASN1_SUCCESS. */
372 /******************************************************/
374 _asn1_complete_explicit_tag (node_asn
* node
, unsigned char *der
,
375 int *counter
, int *max_len
)
378 int is_tag_implicit
, len2
, len3
;
379 unsigned char temp
[SIZEOF_UNSIGNED_INT
];
383 if (node
->type
& CONST_TAG
)
386 /* When there are nested tags we must complete them reverse to
387 the order they were created. This is because completing a tag
388 modifies all data within it, including the incomplete tags
389 which store buffer positions -- simon@josefsson.org 2002-09-06
393 while (p
&& p
!= node
->down
->left
)
395 if (type_field (p
->type
) == TYPE_TAG
)
397 if (p
->type
& CONST_EXPLICIT
)
399 len2
= strtol (p
->name
, NULL
, 10);
400 _asn1_set_name (p
, NULL
);
401 asn1_length_der (*counter
- len2
, temp
, &len3
);
402 if (len3
<= (*max_len
))
404 memmove (der
+ len2
+ len3
, der
+ len2
,
406 memcpy (der
+ len2
, temp
, len3
);
413 { /* CONST_IMPLICIT */
414 if (!is_tag_implicit
)
425 return ASN1_MEM_ERROR
;
431 /******************************************************/
432 /* Function : _asn1_insert_tag_der */
433 /* Description: creates the DER coding of tags of one */
436 /* node: pointer to the tree element. */
437 /* der: string returned */
438 /* counter: number of meaningful bytes of DER */
439 /* (counter[0]..der[*counter-1]). */
440 /* max_len: size of der vector */
442 /* ASN1_GENERIC_ERROR if the type is unknown, */
443 /* ASN1_MEM_ERROR if der vector isn't big enough, */
444 /* otherwise ASN1_SUCCESS. */
445 /******************************************************/
447 _asn1_insert_tag_der (node_asn
* node
, unsigned char *der
, int *counter
,
451 int tag_len
, is_tag_implicit
;
452 unsigned char class, class_implicit
= 0, temp
[SIZEOF_UNSIGNED_INT
* 3 + 1];
453 unsigned long tag_implicit
= 0;
454 char tag_der
[MAX_TAG_LEN
];
458 if (node
->type
& CONST_TAG
)
463 if (type_field (p
->type
) == TYPE_TAG
)
465 if (p
->type
& CONST_APPLICATION
)
466 class = ASN1_CLASS_APPLICATION
;
467 else if (p
->type
& CONST_UNIVERSAL
)
468 class = ASN1_CLASS_UNIVERSAL
;
469 else if (p
->type
& CONST_PRIVATE
)
470 class = ASN1_CLASS_PRIVATE
;
472 class = ASN1_CLASS_CONTEXT_SPECIFIC
;
474 if (p
->type
& CONST_EXPLICIT
)
477 _asn1_tag_der (class_implicit
, tag_implicit
, tag_der
,
480 _asn1_tag_der (class | ASN1_CLASS_STRUCTURED
,
481 strtoul (p
->value
, NULL
, 10), tag_der
,
486 memcpy (der
+ *counter
, tag_der
, tag_len
);
489 _asn1_ltostr (*counter
, temp
);
490 _asn1_set_name (p
, temp
);
495 { /* CONST_IMPLICIT */
496 if (!is_tag_implicit
)
498 if ((type_field (node
->type
) == TYPE_SEQUENCE
) ||
499 (type_field (node
->type
) == TYPE_SEQUENCE_OF
) ||
500 (type_field (node
->type
) == TYPE_SET
) ||
501 (type_field (node
->type
) == TYPE_SET_OF
))
502 class |= ASN1_CLASS_STRUCTURED
;
503 class_implicit
= class;
504 tag_implicit
= strtoul (p
->value
, NULL
, 10);
515 _asn1_tag_der (class_implicit
, tag_implicit
, tag_der
, &tag_len
);
519 switch (type_field (node
->type
))
522 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_NULL
, tag_der
,
526 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_BOOLEAN
, tag_der
,
530 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_INTEGER
, tag_der
,
533 case TYPE_ENUMERATED
:
534 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_ENUMERATED
, tag_der
,
538 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_OBJECT_ID
, tag_der
,
542 if (node
->type
& CONST_UTC
)
544 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_UTCTime
, tag_der
,
548 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_GENERALIZEDTime
,
551 case TYPE_OCTET_STRING
:
552 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_OCTET_STRING
, tag_der
,
555 case TYPE_GENERALSTRING
:
556 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_GENERALSTRING
,
559 case TYPE_BIT_STRING
:
560 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_BIT_STRING
, tag_der
,
564 case TYPE_SEQUENCE_OF
:
565 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
,
566 ASN1_TAG_SEQUENCE
, tag_der
, &tag_len
);
570 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
,
571 ASN1_TAG_SET
, tag_der
, &tag_len
);
583 return ASN1_GENERIC_ERROR
;
589 memcpy (der
+ *counter
, tag_der
, tag_len
);
593 return ASN1_MEM_ERROR
;
598 /******************************************************/
599 /* Function : _asn1_ordering_set */
600 /* Description: puts the elements of a SET type in */
601 /* the correct order according to DER rules. */
603 /* der: string with the DER coding. */
604 /* node: pointer to the SET element. */
606 /******************************************************/
608 _asn1_ordering_set (unsigned char *der
, int der_len
, node_asn
* node
)
614 struct vet
*next
, *prev
;
617 int counter
, len
, len2
;
618 struct vet
*first
, *last
, *p_vet
, *p2_vet
;
620 unsigned char class, *temp
;
625 if (type_field (node
->type
) != TYPE_SET
)
629 while ((type_field (p
->type
) == TYPE_TAG
)
630 || (type_field (p
->type
) == TYPE_SIZE
))
633 if ((p
== NULL
) || (p
->right
== NULL
))
639 p_vet
= (struct vet
*) _asn1_malloc (sizeof (struct vet
));
651 /* tag value calculation */
653 (der
+ counter
, der_len
- counter
, &class, &len2
,
654 &tag
) != ASN1_SUCCESS
)
656 p_vet
->value
= (class << 24) | tag
;
659 /* extraction and length */
660 len2
= asn1_get_length_der (der
+ counter
, der_len
- counter
, &len
);
663 counter
+= len
+ len2
;
665 p_vet
->end
= counter
;
673 p2_vet
= p_vet
->next
;
677 if (p_vet
->value
> p2_vet
->value
)
679 /* change position */
680 temp
= (unsigned char *) _asn1_malloc (p_vet
->end
- counter
);
684 memcpy (temp
, der
+ counter
, p_vet
->end
- counter
);
685 memcpy (der
+ counter
, der
+ p_vet
->end
,
686 p2_vet
->end
- p_vet
->end
);
687 memcpy (der
+ counter
+ p2_vet
->end
- p_vet
->end
, temp
,
688 p_vet
->end
- counter
);
692 p_vet
->value
= p2_vet
->value
;
695 p_vet
->end
= counter
+ (p2_vet
->end
- p_vet
->end
);
697 counter
= p_vet
->end
;
699 p2_vet
= p2_vet
->next
;
704 p_vet
->prev
->next
= NULL
;
712 /******************************************************/
713 /* Function : _asn1_ordering_set_of */
714 /* Description: puts the elements of a SET OF type in */
715 /* the correct order according to DER rules. */
717 /* der: string with the DER coding. */
718 /* node: pointer to the SET OF element. */
720 /******************************************************/
722 _asn1_ordering_set_of (unsigned char *der
, int der_len
, node_asn
* node
)
727 struct vet
*next
, *prev
;
730 int counter
, len
, len2
, change
;
731 struct vet
*first
, *last
, *p_vet
, *p2_vet
;
733 unsigned char *temp
, class;
734 unsigned long k
, max
;
738 if (type_field (node
->type
) != TYPE_SET_OF
)
742 while ((type_field (p
->type
) == TYPE_TAG
)
743 || (type_field (p
->type
) == TYPE_SIZE
))
747 if ((p
== NULL
) || (p
->right
== NULL
))
753 p_vet
= (struct vet
*) _asn1_malloc (sizeof (struct vet
));
765 /* extraction of tag and length */
766 if (der_len
- counter
> 0)
770 (der
+ counter
, der_len
- counter
, &class, &len
,
771 NULL
) != ASN1_SUCCESS
)
775 len2
= asn1_get_length_der (der
+ counter
, der_len
- counter
, &len
);
778 counter
+= len
+ len2
;
781 p_vet
->end
= counter
;
789 p2_vet
= p_vet
->next
;
793 if ((p_vet
->end
- counter
) > (p2_vet
->end
- p_vet
->end
))
794 max
= p_vet
->end
- counter
;
796 max
= p2_vet
->end
- p_vet
->end
;
799 for (k
= 0; k
< max
; k
++)
800 if (der
[counter
+ k
] > der
[p_vet
->end
+ k
])
805 else if (der
[counter
+ k
] < der
[p_vet
->end
+ k
])
812 && ((p_vet
->end
- counter
) > (p2_vet
->end
- p_vet
->end
)))
817 /* change position */
818 temp
= (unsigned char *) _asn1_malloc (p_vet
->end
- counter
);
822 memcpy (temp
, der
+ counter
, (p_vet
->end
) - counter
);
823 memcpy (der
+ counter
, der
+ (p_vet
->end
),
824 (p2_vet
->end
) - (p_vet
->end
));
825 memcpy (der
+ counter
+ (p2_vet
->end
) - (p_vet
->end
), temp
,
826 (p_vet
->end
) - counter
);
829 p_vet
->end
= counter
+ (p2_vet
->end
- p_vet
->end
);
831 counter
= p_vet
->end
;
833 p2_vet
= p2_vet
->next
;
838 p_vet
->prev
->next
= NULL
;
847 * asn1_der_coding - Creates the DER encoding for the NAME structure
848 * @element: pointer to an ASN1 element
849 * @name: the name of the structure you want to encode (it must be
851 * @ider: vector that will contain the DER encoding. DER must be a
852 * pointer to memory cells already allocated.
853 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1], Initialy
854 * holds the sizeof of der vector.
855 * @errorDescription : return the error description or an empty
858 * Creates the DER encoding for the NAME structure (inside *POINTER
863 * ASN1_SUCCESS: DER encoding OK.
865 * ASN1_ELEMENT_NOT_FOUND: NAME is not a valid element.
867 * ASN1_VALUE_NOT_FOUND: There is an element without a value.
869 * ASN1_MEM_ERROR: @ider vector isn't big enough. Also in this case
870 * LEN will contain the length needed.
874 asn1_der_coding (ASN1_TYPE element
, const char *name
, void *ider
, int *len
,
875 char *ErrorDescription
)
877 node_asn
*node
, *p
, *p2
;
878 char temp
[SIZEOF_UNSIGNED_LONG_INT
* 3 + 1];
879 int counter
, counter_old
, len2
, len3
, tlen
, move
, max_len
, max_len_old
;
881 unsigned char *der
= ider
;
883 node
= asn1_find_node (element
, name
);
885 return ASN1_ELEMENT_NOT_FOUND
;
887 /* Node is now a locally allocated variable.
888 * That is because in some point we modify the
889 * structure, and I don't know why! --nmav
891 node
= _asn1_copy_structure3 (node
);
893 return ASN1_ELEMENT_NOT_FOUND
;
903 counter_old
= counter
;
904 max_len_old
= max_len
;
907 err
= _asn1_insert_tag_der (p
, der
, &counter
, &max_len
);
908 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
911 switch (type_field (p
->type
))
921 if ((p
->type
& CONST_DEFAULT
) && (p
->value
== NULL
))
923 counter
= counter_old
;
924 max_len
= max_len_old
;
928 if (p
->value
== NULL
)
930 _asn1_error_description_value_not_found (p
,
932 err
= ASN1_VALUE_NOT_FOUND
;
939 if (p
->value
[0] == 'F')
942 der
[counter
++] = 0xFF;
950 case TYPE_ENUMERATED
:
951 if ((p
->type
& CONST_DEFAULT
) && (p
->value
== NULL
))
953 counter
= counter_old
;
954 max_len
= max_len_old
;
958 if (p
->value
== NULL
)
960 _asn1_error_description_value_not_found (p
,
962 err
= ASN1_VALUE_NOT_FOUND
;
965 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
968 err
= ASN1_DER_ERROR
;
971 max_len
-= len2
+ len3
;
973 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
974 counter
+= len3
+ len2
;
979 if ((p
->type
& CONST_DEFAULT
) && (p
->value
== NULL
))
981 counter
= counter_old
;
982 max_len
= max_len_old
;
986 if (p
->value
== NULL
)
988 _asn1_error_description_value_not_found (p
,
990 err
= ASN1_VALUE_NOT_FOUND
;
994 err
= _asn1_objectid_der (p
->value
, der
+ counter
, &len2
);
995 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1004 if (p
->value
== NULL
)
1006 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1007 err
= ASN1_VALUE_NOT_FOUND
;
1011 err
= _asn1_time_der (p
->value
, der
+ counter
, &len2
);
1012 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1019 case TYPE_OCTET_STRING
:
1020 if (p
->value
== NULL
)
1022 _asn1_error_description_value_not_found (p
, ErrorDescription
);
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
;
1038 case TYPE_GENERALSTRING
:
1039 if (p
->value
== NULL
)
1041 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1042 err
= ASN1_VALUE_NOT_FOUND
;
1045 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1048 err
= ASN1_DER_ERROR
;
1051 max_len
-= len2
+ len3
;
1053 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
1054 counter
+= len3
+ len2
;
1057 case TYPE_BIT_STRING
:
1058 if (p
->value
== NULL
)
1060 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1061 err
= ASN1_VALUE_NOT_FOUND
;
1064 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1067 err
= ASN1_DER_ERROR
;
1070 max_len
-= len2
+ len3
;
1072 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
1073 counter
+= len3
+ len2
;
1080 _asn1_ltostr (counter
, temp
);
1081 tlen
= strlen (temp
);
1083 _asn1_set_value (p
, temp
, tlen
+ 1);
1084 if (p
->down
== NULL
)
1092 while (p2
&& (type_field (p2
->type
) == TYPE_TAG
))
1106 len2
= strtol (p
->value
, NULL
, 10);
1107 _asn1_set_value (p
, NULL
, 0);
1108 if ((type_field (p
->type
) == TYPE_SET
) && (max_len
>= 0))
1109 _asn1_ordering_set (der
+ len2
, max_len
- len2
, p
);
1110 asn1_length_der (counter
- len2
, temp
, &len3
);
1114 memmove (der
+ len2
+ len3
, der
+ len2
, counter
- len2
);
1115 memcpy (der
+ len2
, temp
, len3
);
1121 case TYPE_SEQUENCE_OF
:
1125 _asn1_ltostr (counter
, temp
);
1126 tlen
= strlen (temp
);
1129 _asn1_set_value (p
, temp
, tlen
+ 1);
1131 while ((type_field (p
->type
) == TYPE_TAG
)
1132 || (type_field (p
->type
) == TYPE_SIZE
))
1141 p
= _asn1_find_up (p
);
1146 len2
= strtol (p
->value
, NULL
, 10);
1147 _asn1_set_value (p
, NULL
, 0);
1148 if ((type_field (p
->type
) == TYPE_SET_OF
)
1149 && (max_len
- len2
> 0))
1151 _asn1_ordering_set_of (der
+ len2
, max_len
- len2
, p
);
1153 asn1_length_der (counter
- len2
, temp
, &len3
);
1157 memmove (der
+ len2
+ len3
, der
+ len2
, counter
- len2
);
1158 memcpy (der
+ len2
, temp
, len3
);
1165 if (p
->value
== NULL
)
1167 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1168 err
= ASN1_VALUE_NOT_FOUND
;
1171 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1174 err
= ASN1_DER_ERROR
;
1179 memcpy (der
+ counter
, p
->value
+ len3
, len2
);
1184 move
= (move
== UP
) ? RIGHT
: DOWN
;
1188 if ((move
!= DOWN
) && (counter
!= counter_old
))
1190 err
= _asn1_complete_explicit_tag (p
, der
, &counter
, &max_len
);
1191 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1195 if (p
== node
&& move
!= DOWN
)
1213 p
= _asn1_find_up (p
);
1220 err
= ASN1_MEM_ERROR
;
1227 asn1_delete_structure (&node
);