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 * @ans: string returned.
65 * @ans_len: number of meaningful bytes of ANS (ans[0]..ans[ans_len-1]).
67 * Creates the DER coding for the LEN parameter (only the length).
68 * The @ans buffer is pre-allocated and must have room for the output.
71 asn1_length_der (unsigned long int len
, unsigned char *ans
, int *ans_len
)
74 unsigned char temp
[SIZEOF_UNSIGNED_LONG_INT
];
80 ans
[0] = (unsigned char) len
;
89 temp
[k
++] = len
& 0xFF;
95 ans
[0] = ((unsigned char) k
& 0x7F) + 128;
97 ans
[*ans_len
- 1 - k
] = temp
[k
];
102 /******************************************************/
103 /* Function : _asn1_tag_der */
104 /* Description: creates the DER coding for the CLASS */
105 /* and TAG parameters. */
107 /* class: value to convert. */
108 /* tag_value: value to convert. */
109 /* ans: string returned. */
110 /* ans_len: number of meaningful bytes of ANS */
111 /* (ans[0]..ans[ans_len-1]). */
113 /******************************************************/
115 _asn1_tag_der (unsigned char class, unsigned int tag_value
,
116 unsigned char *ans
, int *ans_len
)
119 unsigned char temp
[SIZEOF_UNSIGNED_INT
];
124 ans
[0] = (class & 0xE0) + ((unsigned char) (tag_value
& 0x1F));
130 ans
[0] = (class & 0xE0) + 31;
134 temp
[k
++] = tag_value
& 0x7F;
135 tag_value
= tag_value
>> 7;
139 ans
[*ans_len
- 1 - k
] = temp
[k
] + 128;
140 ans
[*ans_len
- 1] -= 128;
146 * @str: OCTET string.
147 * @str_len: STR length (str[0]..str[str_len-1]).
148 * @der: string returned.
149 * @der_len: number of meaningful bytes of DER (der[0]..der[ans_len-1]).
151 * Creates the DER coding for an OCTET type (length included).
154 asn1_octet_der (const unsigned char *str
, int str_len
,
155 unsigned char *der
, int *der_len
)
159 if (der
== NULL
|| str_len
< 0)
161 asn1_length_der (str_len
, der
, &len_len
);
162 memcpy (der
+ len_len
, str
, str_len
);
163 *der_len
= str_len
+ len_len
;
166 /******************************************************/
167 /* Function : _asn1_time_der */
168 /* Description: creates the DER coding for a TIME */
169 /* type (length included). */
171 /* str: TIME null-terminated string. */
172 /* der: string returned. */
173 /* der_len: number of meaningful bytes of DER */
174 /* (der[0]..der[ans_len-1]). Initially it */
175 /* if must store the lenght of DER. */
177 /* ASN1_MEM_ERROR when DER isn't big enough */
178 /* ASN1_SUCCESS otherwise */
179 /******************************************************/
181 _asn1_time_der (unsigned char *str
, unsigned char *der
, int *der_len
)
185 int str_len
= _asn1_strlen (str
);
189 asn1_length_der (str_len
, (max_len
> 0) ? der
: NULL
, &len_len
);
191 if ((len_len
+ str_len
) <= max_len
)
192 memcpy (der
+ len_len
, str
, str_len
);
193 *der_len
= len_len
+ str_len
;
195 if ((*der_len
) > max_len
)
196 return ASN1_MEM_ERROR
;
204 _asn1_get_utctime_der(unsigned char *der,int *der_len,unsigned char *str)
209 if(str==NULL) return;
210 str_len=asn1_get_length_der(der,*der_len,&len_len);
211 if (str_len<0) return;
212 memcpy(temp,der+len_len,str_len);
213 *der_len=str_len+len_len;
217 strcat(temp,"00+0000");
221 strcat(temp,"+0000");
225 memmove(temp+12,temp+10,6);
226 temp[10]=temp[11]='0';
238 /******************************************************/
239 /* Function : _asn1_objectid_der */
240 /* Description: creates the DER coding for an */
241 /* OBJECT IDENTIFIER type (length included). */
243 /* str: OBJECT IDENTIFIER null-terminated string. */
244 /* der: string returned. */
245 /* der_len: number of meaningful bytes of DER */
246 /* (der[0]..der[ans_len-1]). Initially it */
247 /* must store the length of DER. */
249 /* ASN1_MEM_ERROR when DER isn't big enough */
250 /* ASN1_SUCCESS otherwise */
251 /******************************************************/
253 _asn1_objectid_der (unsigned char *str
, unsigned char *der
, int *der_len
)
255 int len_len
, counter
, k
, first
, max_len
;
256 char *temp
, *n_end
, *n_start
;
258 unsigned long val
, val1
= 0;
259 int str_len
= _asn1_strlen (str
);
263 temp
= malloc (str_len
+ 2);
265 return ASN1_MEM_ALLOC_ERROR
;
267 memcpy (temp
, str
, str_len
);
269 temp
[str_len
+ 1] = 0;
273 while ((n_end
= strchr (n_start
, '.')))
276 val
= strtoul (n_start
, NULL
, 10);
281 else if (counter
== 2)
284 der
[0] = 40 * val1
+ val
;
290 for (k
= 4; k
>= 0; k
--)
292 bit7
= (val
>> (k
* 7)) & 0x7F;
293 if (bit7
|| first
|| !k
)
297 if (max_len
> (*der_len
))
298 der
[*der_len
] = bit7
;
308 asn1_length_der (*der_len
, NULL
, &len_len
);
309 if (max_len
>= (*der_len
+ len_len
))
311 memmove (der
+ len_len
, der
, *der_len
);
312 asn1_length_der (*der_len
, der
, &len_len
);
318 if (max_len
< (*der_len
))
319 return ASN1_MEM_ERROR
;
325 static const unsigned char bit_mask
[] =
326 { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0x80 };
331 * @bit_len: number of meaningful bits in STR.
332 * @der: string returned.
333 * @der_len: number of meaningful bytes of DER
334 * (der[0]..der[ans_len-1]).
336 * Creates the DER coding for a BIT STRING type (length and pad
340 asn1_bit_der (const unsigned char *str
, int bit_len
,
341 unsigned char *der
, int *der_len
)
343 int len_len
, len_byte
, len_pad
;
347 len_byte
= bit_len
>> 3;
348 len_pad
= 8 - (bit_len
& 7);
353 asn1_length_der (len_byte
+ 1, der
, &len_len
);
354 der
[len_len
] = len_pad
;
355 memcpy (der
+ len_len
+ 1, str
, len_byte
);
356 der
[len_len
+ len_byte
] &= bit_mask
[len_pad
];
357 *der_len
= len_byte
+ len_len
+ 1;
361 /******************************************************/
362 /* Function : _asn1_complete_explicit_tag */
363 /* Description: add the length coding to the EXPLICIT */
366 /* node: pointer to the tree element. */
367 /* der: string with the DER coding of the whole tree*/
368 /* counter: number of meaningful bytes of DER */
369 /* (der[0]..der[*counter-1]). */
370 /* max_len: size of der vector */
372 /* ASN1_MEM_ERROR if der vector isn't big enough, */
373 /* otherwise ASN1_SUCCESS. */
374 /******************************************************/
376 _asn1_complete_explicit_tag (asn1_node node
, unsigned char *der
,
377 int *counter
, int *max_len
)
380 int is_tag_implicit
, len2
, len3
;
381 unsigned char temp
[SIZEOF_UNSIGNED_INT
];
385 if (node
->type
& CONST_TAG
)
388 /* When there are nested tags we must complete them reverse to
389 the order they were created. This is because completing a tag
390 modifies all data within it, including the incomplete tags
391 which store buffer positions -- simon@josefsson.org 2002-09-06
395 while (p
&& p
!= node
->down
->left
)
397 if (type_field (p
->type
) == TYPE_TAG
)
399 if (p
->type
& CONST_EXPLICIT
)
401 len2
= strtol (p
->name
, NULL
, 10);
402 _asn1_set_name (p
, NULL
);
403 asn1_length_der (*counter
- len2
, temp
, &len3
);
404 if (len3
<= (*max_len
))
406 memmove (der
+ len2
+ len3
, der
+ len2
,
408 memcpy (der
+ len2
, temp
, len3
);
415 { /* CONST_IMPLICIT */
416 if (!is_tag_implicit
)
427 return ASN1_MEM_ERROR
;
433 /******************************************************/
434 /* Function : _asn1_insert_tag_der */
435 /* Description: creates the DER coding of tags of one */
438 /* node: pointer to the tree element. */
439 /* der: string returned */
440 /* counter: number of meaningful bytes of DER */
441 /* (counter[0]..der[*counter-1]). */
442 /* max_len: size of der vector */
444 /* ASN1_GENERIC_ERROR if the type is unknown, */
445 /* ASN1_MEM_ERROR if der vector isn't big enough, */
446 /* otherwise ASN1_SUCCESS. */
447 /******************************************************/
449 _asn1_insert_tag_der (asn1_node node
, unsigned char *der
, int *counter
,
453 int tag_len
, is_tag_implicit
;
454 unsigned char class, class_implicit
= 0, temp
[SIZEOF_UNSIGNED_INT
* 3 + 1];
455 unsigned long tag_implicit
= 0;
456 unsigned char tag_der
[MAX_TAG_LEN
];
460 if (node
->type
& CONST_TAG
)
465 if (type_field (p
->type
) == TYPE_TAG
)
467 if (p
->type
& CONST_APPLICATION
)
468 class = ASN1_CLASS_APPLICATION
;
469 else if (p
->type
& CONST_UNIVERSAL
)
470 class = ASN1_CLASS_UNIVERSAL
;
471 else if (p
->type
& CONST_PRIVATE
)
472 class = ASN1_CLASS_PRIVATE
;
474 class = ASN1_CLASS_CONTEXT_SPECIFIC
;
476 if (p
->type
& CONST_EXPLICIT
)
479 _asn1_tag_der (class_implicit
, tag_implicit
, tag_der
,
482 _asn1_tag_der (class | ASN1_CLASS_STRUCTURED
,
483 _asn1_strtoul (p
->value
, NULL
, 10),
488 memcpy (der
+ *counter
, tag_der
, tag_len
);
491 _asn1_ltostr (*counter
, (char *) temp
);
492 _asn1_set_name (p
, (const char *) temp
);
497 { /* CONST_IMPLICIT */
498 if (!is_tag_implicit
)
500 if ((type_field (node
->type
) == TYPE_SEQUENCE
) ||
501 (type_field (node
->type
) == TYPE_SEQUENCE_OF
) ||
502 (type_field (node
->type
) == TYPE_SET
) ||
503 (type_field (node
->type
) == TYPE_SET_OF
))
504 class |= ASN1_CLASS_STRUCTURED
;
505 class_implicit
= class;
506 tag_implicit
= _asn1_strtoul (p
->value
, NULL
, 10);
517 _asn1_tag_der (class_implicit
, tag_implicit
, tag_der
, &tag_len
);
521 switch (type_field (node
->type
))
524 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_NULL
, tag_der
,
528 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_BOOLEAN
, tag_der
,
532 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_INTEGER
, tag_der
,
535 case TYPE_ENUMERATED
:
536 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_ENUMERATED
, tag_der
,
540 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_OBJECT_ID
, tag_der
,
544 if (node
->type
& CONST_UTC
)
546 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_UTCTime
, tag_der
,
550 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_GENERALIZEDTime
,
553 case TYPE_OCTET_STRING
:
554 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_OCTET_STRING
, tag_der
,
557 case TYPE_GENERALSTRING
:
558 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_GENERALSTRING
,
561 case TYPE_BIT_STRING
:
562 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
, ASN1_TAG_BIT_STRING
, tag_der
,
566 case TYPE_SEQUENCE_OF
:
567 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
,
568 ASN1_TAG_SEQUENCE
, tag_der
, &tag_len
);
572 _asn1_tag_der (ASN1_CLASS_UNIVERSAL
| ASN1_CLASS_STRUCTURED
,
573 ASN1_TAG_SET
, tag_der
, &tag_len
);
585 return ASN1_GENERIC_ERROR
;
591 memcpy (der
+ *counter
, tag_der
, tag_len
);
595 return ASN1_MEM_ERROR
;
600 /******************************************************/
601 /* Function : _asn1_ordering_set */
602 /* Description: puts the elements of a SET type in */
603 /* the correct order according to DER rules. */
605 /* der: string with the DER coding. */
606 /* node: pointer to the SET element. */
608 /******************************************************/
610 _asn1_ordering_set (unsigned char *der
, int der_len
, asn1_node node
)
616 struct vet
*next
, *prev
;
619 int counter
, len
, len2
;
620 struct vet
*first
, *last
, *p_vet
, *p2_vet
;
622 unsigned char class, *temp
;
627 if (type_field (node
->type
) != TYPE_SET
)
631 while ((type_field (p
->type
) == TYPE_TAG
)
632 || (type_field (p
->type
) == TYPE_SIZE
))
635 if ((p
== NULL
) || (p
->right
== NULL
))
641 p_vet
= malloc (sizeof (struct vet
));
653 /* tag value calculation */
655 (der
+ counter
, der_len
- counter
, &class, &len2
,
656 &tag
) != ASN1_SUCCESS
)
658 p_vet
->value
= (class << 24) | tag
;
661 /* extraction and length */
662 len2
= asn1_get_length_der (der
+ counter
, der_len
- counter
, &len
);
665 counter
+= len
+ len2
;
667 p_vet
->end
= counter
;
675 p2_vet
= p_vet
->next
;
679 if (p_vet
->value
> p2_vet
->value
)
681 /* change position */
682 temp
= malloc (p_vet
->end
- counter
);
686 memcpy (temp
, der
+ counter
, p_vet
->end
- counter
);
687 memcpy (der
+ counter
, der
+ p_vet
->end
,
688 p2_vet
->end
- p_vet
->end
);
689 memcpy (der
+ counter
+ p2_vet
->end
- p_vet
->end
, temp
,
690 p_vet
->end
- counter
);
694 p_vet
->value
= p2_vet
->value
;
697 p_vet
->end
= counter
+ (p2_vet
->end
- p_vet
->end
);
699 counter
= p_vet
->end
;
701 p2_vet
= p2_vet
->next
;
706 p_vet
->prev
->next
= NULL
;
714 /******************************************************/
715 /* Function : _asn1_ordering_set_of */
716 /* Description: puts the elements of a SET OF type in */
717 /* the correct order according to DER rules. */
719 /* der: string with the DER coding. */
720 /* node: pointer to the SET OF element. */
722 /******************************************************/
724 _asn1_ordering_set_of (unsigned char *der
, int der_len
, asn1_node node
)
729 struct vet
*next
, *prev
;
732 int counter
, len
, len2
, change
;
733 struct vet
*first
, *last
, *p_vet
, *p2_vet
;
735 unsigned char *temp
, class;
736 unsigned long k
, max
;
740 if (type_field (node
->type
) != TYPE_SET_OF
)
744 while ((type_field (p
->type
) == TYPE_TAG
)
745 || (type_field (p
->type
) == TYPE_SIZE
))
749 if ((p
== NULL
) || (p
->right
== NULL
))
755 p_vet
= malloc (sizeof (struct vet
));
767 /* extraction of tag and length */
768 if (der_len
- counter
> 0)
772 (der
+ counter
, der_len
- counter
, &class, &len
,
773 NULL
) != ASN1_SUCCESS
)
777 len2
= asn1_get_length_der (der
+ counter
, der_len
- counter
, &len
);
780 counter
+= len
+ len2
;
783 p_vet
->end
= counter
;
791 p2_vet
= p_vet
->next
;
795 if ((p_vet
->end
- counter
) > (p2_vet
->end
- p_vet
->end
))
796 max
= p_vet
->end
- counter
;
798 max
= p2_vet
->end
- p_vet
->end
;
801 for (k
= 0; k
< max
; k
++)
802 if (der
[counter
+ k
] > der
[p_vet
->end
+ k
])
807 else if (der
[counter
+ k
] < der
[p_vet
->end
+ k
])
814 && ((p_vet
->end
- counter
) > (p2_vet
->end
- p_vet
->end
)))
819 /* change position */
820 temp
= malloc (p_vet
->end
- counter
);
824 memcpy (temp
, der
+ counter
, (p_vet
->end
) - counter
);
825 memcpy (der
+ counter
, der
+ (p_vet
->end
),
826 (p2_vet
->end
) - (p_vet
->end
));
827 memcpy (der
+ counter
+ (p2_vet
->end
) - (p_vet
->end
), temp
,
828 (p_vet
->end
) - counter
);
831 p_vet
->end
= counter
+ (p2_vet
->end
- p_vet
->end
);
833 counter
= p_vet
->end
;
835 p2_vet
= p2_vet
->next
;
840 p_vet
->prev
->next
= NULL
;
850 * @element: pointer to an ASN1 element
851 * @name: the name of the structure you want to encode (it must be
853 * @ider: vector that will contain the DER encoding. DER must be a
854 * pointer to memory cells already allocated.
855 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1], Initialy
856 * holds the sizeof of der vector.
857 * @errorDescription : return the error description or an empty
860 * Creates the DER encoding for the NAME structure (inside *POINTER
863 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
864 * if @name is not a valid element, %ASN1_VALUE_NOT_FOUND if there
865 * is an element without a value, %ASN1_MEM_ERROR if the @ider
866 * vector isn't big enough and in this case @len will contain the
870 asn1_der_coding (asn1_node element
, const char *name
, void *ider
, int *len
,
871 char *ErrorDescription
)
873 asn1_node node
, p
, p2
;
874 unsigned char temp
[SIZEOF_UNSIGNED_LONG_INT
* 3 + 1];
875 int counter
, counter_old
, len2
, len3
, tlen
, move
, max_len
, max_len_old
;
877 unsigned char *der
= ider
;
879 node
= asn1_find_node (element
, name
);
881 return ASN1_ELEMENT_NOT_FOUND
;
883 /* Node is now a locally allocated variable.
884 * That is because in some point we modify the
885 * structure, and I don't know why! --nmav
887 node
= _asn1_copy_structure3 (node
);
889 return ASN1_ELEMENT_NOT_FOUND
;
899 counter_old
= counter
;
900 max_len_old
= max_len
;
903 err
= _asn1_insert_tag_der (p
, der
, &counter
, &max_len
);
904 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
907 switch (type_field (p
->type
))
917 if ((p
->type
& CONST_DEFAULT
) && (p
->value
== NULL
))
919 counter
= counter_old
;
920 max_len
= max_len_old
;
924 if (p
->value
== NULL
)
926 _asn1_error_description_value_not_found (p
,
928 err
= ASN1_VALUE_NOT_FOUND
;
935 if (p
->value
[0] == 'F')
938 der
[counter
++] = 0xFF;
946 case TYPE_ENUMERATED
:
947 if ((p
->type
& CONST_DEFAULT
) && (p
->value
== NULL
))
949 counter
= counter_old
;
950 max_len
= max_len_old
;
954 if (p
->value
== NULL
)
956 _asn1_error_description_value_not_found (p
,
958 err
= ASN1_VALUE_NOT_FOUND
;
961 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
964 err
= ASN1_DER_ERROR
;
967 max_len
-= len2
+ len3
;
969 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
970 counter
+= len3
+ len2
;
975 if ((p
->type
& CONST_DEFAULT
) && (p
->value
== NULL
))
977 counter
= counter_old
;
978 max_len
= max_len_old
;
982 if (p
->value
== NULL
)
984 _asn1_error_description_value_not_found (p
,
986 err
= ASN1_VALUE_NOT_FOUND
;
990 err
= _asn1_objectid_der (p
->value
, der
+ counter
, &len2
);
991 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1000 if (p
->value
== NULL
)
1002 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1003 err
= ASN1_VALUE_NOT_FOUND
;
1007 err
= _asn1_time_der (p
->value
, der
+ counter
, &len2
);
1008 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1015 case TYPE_OCTET_STRING
:
1016 if (p
->value
== NULL
)
1018 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1019 err
= ASN1_VALUE_NOT_FOUND
;
1022 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1025 err
= ASN1_DER_ERROR
;
1028 max_len
-= len2
+ len3
;
1030 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
1031 counter
+= len3
+ len2
;
1034 case TYPE_GENERALSTRING
:
1035 if (p
->value
== NULL
)
1037 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1038 err
= ASN1_VALUE_NOT_FOUND
;
1041 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1044 err
= ASN1_DER_ERROR
;
1047 max_len
-= len2
+ len3
;
1049 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
1050 counter
+= len3
+ len2
;
1053 case TYPE_BIT_STRING
:
1054 if (p
->value
== NULL
)
1056 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1057 err
= ASN1_VALUE_NOT_FOUND
;
1060 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1063 err
= ASN1_DER_ERROR
;
1066 max_len
-= len2
+ len3
;
1068 memcpy (der
+ counter
, p
->value
, len3
+ len2
);
1069 counter
+= len3
+ len2
;
1076 _asn1_ltostr (counter
, (char *) temp
);
1077 tlen
= _asn1_strlen (temp
);
1079 _asn1_set_value (p
, temp
, tlen
+ 1);
1080 if (p
->down
== NULL
)
1088 while (p2
&& (type_field (p2
->type
) == TYPE_TAG
))
1102 len2
= _asn1_strtol (p
->value
, NULL
, 10);
1103 _asn1_set_value (p
, NULL
, 0);
1104 if ((type_field (p
->type
) == TYPE_SET
) && (max_len
>= 0))
1105 _asn1_ordering_set (der
+ len2
, max_len
- len2
, p
);
1106 asn1_length_der (counter
- len2
, temp
, &len3
);
1110 memmove (der
+ len2
+ len3
, der
+ len2
, counter
- len2
);
1111 memcpy (der
+ len2
, temp
, len3
);
1117 case TYPE_SEQUENCE_OF
:
1121 _asn1_ltostr (counter
, (char *) temp
);
1122 tlen
= _asn1_strlen (temp
);
1125 _asn1_set_value (p
, temp
, tlen
+ 1);
1127 while ((type_field (p
->type
) == TYPE_TAG
)
1128 || (type_field (p
->type
) == TYPE_SIZE
))
1137 p
= _asn1_find_up (p
);
1142 len2
= _asn1_strtol (p
->value
, NULL
, 10);
1143 _asn1_set_value (p
, NULL
, 0);
1144 if ((type_field (p
->type
) == TYPE_SET_OF
)
1145 && (max_len
- len2
> 0))
1147 _asn1_ordering_set_of (der
+ len2
, max_len
- len2
, p
);
1149 asn1_length_der (counter
- len2
, temp
, &len3
);
1153 memmove (der
+ len2
+ len3
, der
+ len2
, counter
- len2
);
1154 memcpy (der
+ len2
, temp
, len3
);
1161 if (p
->value
== NULL
)
1163 _asn1_error_description_value_not_found (p
, ErrorDescription
);
1164 err
= ASN1_VALUE_NOT_FOUND
;
1167 len2
= asn1_get_length_der (p
->value
, p
->value_len
, &len3
);
1170 err
= ASN1_DER_ERROR
;
1175 memcpy (der
+ counter
, p
->value
+ len3
, len2
);
1180 move
= (move
== UP
) ? RIGHT
: DOWN
;
1184 if ((move
!= DOWN
) && (counter
!= counter_old
))
1186 err
= _asn1_complete_explicit_tag (p
, der
, &counter
, &max_len
);
1187 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1191 if (p
== node
&& move
!= DOWN
)
1209 p
= _asn1_find_up (p
);
1216 err
= ASN1_MEM_ERROR
;
1223 asn1_delete_structure (&node
);