2 * Copyright (C) 2004, 2006 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
,char *ErrorDescription
)
52 if (ErrorDescription
== NULL
) return;
54 Estrcpy(ErrorDescription
,":: value of element '");
55 _asn1_hierarchical_name(node
,ErrorDescription
+strlen(ErrorDescription
),
56 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
];
78 if(ans
!=NULL
) ans
[0]=(unsigned char)len
;
90 ans
[0]=((unsigned char)k
&0x7F)+128;
91 while(k
--) ans
[*ans_len
-1-k
]=temp
[k
];
96 /******************************************************/
97 /* Function : _asn1_tag_der */
98 /* Description: creates the DER coding for the CLASS */
99 /* and TAG parameters. */
101 /* class: value to convert. */
102 /* tag_value: value to convert. */
103 /* ans: string returned. */
104 /* ans_len: number of meaningful bytes of ANS */
105 /* (ans[0]..ans[ans_len-1]). */
107 /******************************************************/
109 _asn1_tag_der(unsigned char class,unsigned int tag_value
,unsigned char *ans
,int *ans_len
)
112 unsigned char temp
[SIZEOF_UNSIGNED_INT
];
116 ans
[0]=(class&0xE0) + ((unsigned char)(tag_value
&0x1F));
121 ans
[0]=(class&0xE0) + 31;
124 temp
[k
++]=tag_value
&0x7F;
125 tag_value
=tag_value
>>7;
128 while(k
--) ans
[*ans_len
-1-k
]=temp
[k
]+128;
129 ans
[*ans_len
-1]-=128;
135 * @str: OCTET string.
136 * @str_len: STR length (str[0]..str[str_len-1]).
137 * @der: string returned.
138 * @der_len: number of meaningful bytes of DER (der[0]..der[ans_len-1]).
140 * Creates the DER coding for an OCTET type (length included).
143 asn1_octet_der(const unsigned char *str
,int str_len
,
144 unsigned char *der
,int *der_len
)
148 if(der
==NULL
|| str_len
<= 0) return;
149 asn1_length_der(str_len
,der
,&len_len
);
150 memcpy(der
+len_len
,str
,str_len
);
151 *der_len
=str_len
+len_len
;
154 /******************************************************/
155 /* Function : _asn1_time_der */
156 /* Description: creates the DER coding for a TIME */
157 /* type (length included). */
159 /* str: TIME null-terminated string. */
160 /* der: string returned. */
161 /* der_len: number of meaningful bytes of DER */
162 /* (der[0]..der[ans_len-1]). Initially it */
163 /* if must store the lenght of DER. */
165 /* ASN1_MEM_ERROR when DER isn't big enough */
166 /* ASN1_SUCCESS otherwise */
167 /******************************************************/
169 _asn1_time_der(unsigned char *str
,unsigned char *der
,int *der_len
)
176 asn1_length_der(strlen(str
),(max_len
>0)?der
:NULL
,&len_len
);
178 if((len_len
+(int)strlen(str
))<=max_len
)
179 memcpy(der
+len_len
,str
,strlen(str
));
180 *der_len
=len_len
+strlen(str
);
182 if((*der_len
)>max_len
) return ASN1_MEM_ERROR
;
190 _asn1_get_utctime_der(unsigned char *der,int *der_len,unsigned char *str)
195 if(str==NULL) return;
196 str_len=asn1_get_length_der(der,*der_len,&len_len);
197 if (str_len<0) return;
198 memcpy(temp,der+len_len,str_len);
199 *der_len=str_len+len_len;
203 strcat(temp,"00+0000");
207 strcat(temp,"+0000");
211 memmove(temp+12,temp+10,6);
212 temp[10]=temp[11]='0';
224 /******************************************************/
225 /* Function : _asn1_objectid_der */
226 /* Description: creates the DER coding for an */
227 /* OBJECT IDENTIFIER type (length included). */
229 /* str: OBJECT IDENTIFIER null-terminated string. */
230 /* der: string returned. */
231 /* der_len: number of meaningful bytes of DER */
232 /* (der[0]..der[ans_len-1]). Initially it */
233 /* must store the length of DER. */
235 /* ASN1_MEM_ERROR when DER isn't big enough */
236 /* ASN1_SUCCESS otherwise */
237 /******************************************************/
239 _asn1_objectid_der(unsigned char *str
,unsigned char *der
,int *der_len
)
241 int len_len
,counter
,k
,first
,max_len
;
242 char *temp
,*n_end
,*n_start
;
244 unsigned long val
,val1
=0;
248 temp
= (char *) _asn1_alloca(strlen(str
)+2);
249 if(temp
==NULL
) return ASN1_MEM_ALLOC_ERROR
;
256 while((n_end
=strchr(n_start
,'.'))){
258 val
=strtoul(n_start
,NULL
,10);
261 if(counter
==1) val1
=val
;
270 bit7
=(val
>>(k
*7))&0x7F;
271 if(bit7
|| first
|| !k
){
273 if(max_len
>(*der_len
))
284 asn1_length_der(*der_len
,NULL
,&len_len
);
285 if(max_len
>=(*der_len
+len_len
)){
286 memmove(der
+len_len
,der
,*der_len
);
287 asn1_length_der(*der_len
,der
,&len_len
);
293 if(max_len
<(*der_len
)) return ASN1_MEM_ERROR
;
299 const char bit_mask
[]={0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80};
304 * @bit_len: number of meaningful bits in STR.
305 * @der: string returned.
306 * @der_len: number of meaningful bytes of DER
307 * (der[0]..der[ans_len-1]).
309 * Creates the DER coding for a BIT STRING type (length and pad
313 asn1_bit_der(const unsigned char *str
, int bit_len
,
314 unsigned char *der
, int *der_len
)
316 int len_len
,len_byte
,len_pad
;
318 if(der
==NULL
) return;
320 len_pad
=8-(bit_len
&7);
321 if(len_pad
==8) len_pad
=0;
323 asn1_length_der(len_byte
+1,der
,&len_len
);
324 der
[len_len
]=len_pad
;
325 memcpy(der
+len_len
+1,str
,len_byte
);
326 der
[len_len
+len_byte
]&=bit_mask
[len_pad
];
327 *der_len
=len_byte
+len_len
+1;
331 /******************************************************/
332 /* Function : _asn1_complete_explicit_tag */
333 /* Description: add the length coding to the EXPLICIT */
336 /* node: pointer to the tree element. */
337 /* der: string with the DER coding of the whole tree*/
338 /* counter: number of meaningful bytes of DER */
339 /* (der[0]..der[*counter-1]). */
340 /* max_len: size of der vector */
342 /* ASN1_MEM_ERROR if der vector isn't big enough, */
343 /* otherwise ASN1_SUCCESS. */
344 /******************************************************/
346 _asn1_complete_explicit_tag(node_asn
*node
,unsigned char *der
,int *counter
,int *max_len
)
349 int is_tag_implicit
,len2
,len3
;
350 unsigned char temp
[SIZEOF_UNSIGNED_INT
];
354 if(node
->type
&CONST_TAG
){
356 /* When there are nested tags we must complete them reverse to
357 the order they were created. This is because completing a tag
358 modifies all data within it, including the incomplete tags
359 which store buffer positions -- simon@josefsson.org 2002-09-06
363 while(p
&& p
!=node
->down
->left
){
364 if(type_field(p
->type
)==TYPE_TAG
){
365 if(p
->type
&CONST_EXPLICIT
){
366 len2
=strtol(p
->name
,NULL
,10);
367 _asn1_set_name(p
,NULL
);
368 asn1_length_der(*counter
-len2
,temp
,&len3
);
369 if(len3
<=(*max_len
)){
370 memmove(der
+len2
+len3
,der
+len2
,*counter
-len2
);
371 memcpy(der
+len2
,temp
,len3
);
377 else{ /* CONST_IMPLICIT */
378 if(!is_tag_implicit
){
387 if(*max_len
<0) return ASN1_MEM_ERROR
;
393 /******************************************************/
394 /* Function : _asn1_insert_tag_der */
395 /* Description: creates the DER coding of tags of one */
398 /* node: pointer to the tree element. */
399 /* der: string returned */
400 /* counter: number of meaningful bytes of DER */
401 /* (counter[0]..der[*counter-1]). */
402 /* max_len: size of der vector */
404 /* ASN1_GENERIC_ERROR if the type is unknown, */
405 /* ASN1_MEM_ERROR if der vector isn't big enough, */
406 /* otherwise ASN1_SUCCESS. */
407 /******************************************************/
409 _asn1_insert_tag_der(node_asn
*node
,unsigned char *der
,int *counter
,int *max_len
)
412 int tag_len
,is_tag_implicit
;
413 unsigned char class,class_implicit
=0,temp
[SIZEOF_UNSIGNED_INT
*3+1];
414 unsigned long tag_implicit
=0;
415 char tag_der
[MAX_TAG_LEN
];
419 if(node
->type
&CONST_TAG
){
422 if(type_field(p
->type
)==TYPE_TAG
){
423 if(p
->type
&CONST_APPLICATION
) class=ASN1_CLASS_APPLICATION
;
424 else if(p
->type
&CONST_UNIVERSAL
) class=ASN1_CLASS_UNIVERSAL
;
425 else if(p
->type
&CONST_PRIVATE
) class=ASN1_CLASS_PRIVATE
;
426 else class=ASN1_CLASS_CONTEXT_SPECIFIC
;
428 if(p
->type
&CONST_EXPLICIT
){
430 _asn1_tag_der(class_implicit
,tag_implicit
,tag_der
,&tag_len
);
432 _asn1_tag_der(class|ASN1_CLASS_STRUCTURED
,strtoul(p
->value
,NULL
,10),tag_der
,&tag_len
);
436 memcpy(der
+*counter
,tag_der
,tag_len
);
439 _asn1_ltostr(*counter
,temp
);
440 _asn1_set_name(p
,temp
);
444 else{ /* CONST_IMPLICIT */
445 if(!is_tag_implicit
){
446 if((type_field(node
->type
)==TYPE_SEQUENCE
) ||
447 (type_field(node
->type
)==TYPE_SEQUENCE_OF
) ||
448 (type_field(node
->type
)==TYPE_SET
) ||
449 (type_field(node
->type
)==TYPE_SET_OF
)) class|=ASN1_CLASS_STRUCTURED
;
450 class_implicit
=class;
451 tag_implicit
=strtoul(p
->value
,NULL
,10);
461 _asn1_tag_der(class_implicit
,tag_implicit
,tag_der
,&tag_len
);
464 switch(type_field(node
->type
)){
466 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_NULL
,tag_der
,&tag_len
);
469 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_BOOLEAN
,tag_der
,&tag_len
);
472 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_INTEGER
,tag_der
,&tag_len
);
474 case TYPE_ENUMERATED
:
475 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_ENUMERATED
,tag_der
,&tag_len
);
478 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_OBJECT_ID
,tag_der
,&tag_len
);
481 if(node
->type
&CONST_UTC
){
482 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_UTCTime
,tag_der
,&tag_len
);
484 else _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_GENERALIZEDTime
,tag_der
,&tag_len
);
486 case TYPE_OCTET_STRING
:
487 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_OCTET_STRING
,tag_der
,&tag_len
);
489 case TYPE_GENERALSTRING
:
490 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_GENERALSTRING
,tag_der
,&tag_len
);
492 case TYPE_BIT_STRING
:
493 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
,ASN1_TAG_BIT_STRING
,tag_der
,&tag_len
);
495 case TYPE_SEQUENCE
: case TYPE_SEQUENCE_OF
:
496 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
|ASN1_CLASS_STRUCTURED
,ASN1_TAG_SEQUENCE
,tag_der
,&tag_len
);
498 case TYPE_SET
: case TYPE_SET_OF
:
499 _asn1_tag_der(ASN1_CLASS_UNIVERSAL
|ASN1_CLASS_STRUCTURED
,ASN1_TAG_SET
,tag_der
,&tag_len
);
511 return ASN1_GENERIC_ERROR
;
517 memcpy(der
+*counter
,tag_der
,tag_len
);
520 if(*max_len
<0) return ASN1_MEM_ERROR
;
525 /******************************************************/
526 /* Function : _asn1_ordering_set */
527 /* Description: puts the elements of a SET type in */
528 /* the correct order according to DER rules. */
530 /* der: string with the DER coding. */
531 /* node: pointer to the SET element. */
533 /******************************************************/
535 _asn1_ordering_set(unsigned char *der
, int der_len
, node_asn
*node
)
540 struct vet
*next
,*prev
;
543 int counter
,len
,len2
;
544 struct vet
*first
,*last
,*p_vet
,*p2_vet
;
546 unsigned char class,*temp
;
551 if(type_field(node
->type
)!=TYPE_SET
) return;
554 while((type_field(p
->type
)==TYPE_TAG
) || (type_field(p
->type
)==TYPE_SIZE
)) p
=p
->right
;
556 if((p
==NULL
) || (p
->right
==NULL
)) return;
560 p_vet
=(struct vet
*)_asn1_alloca( sizeof(struct vet
));
561 if (p_vet
==NULL
) return;
565 if(first
==NULL
) first
=p_vet
;
566 else last
->next
=p_vet
;
569 /* tag value calculation */
570 if (asn1_get_tag_der(der
+counter
, der_len
-counter
,&class,&len2
, &tag
)!=ASN1_SUCCESS
)
572 p_vet
->value
=(class<<24)|tag
;
575 /* extraction and length */
576 len2
=asn1_get_length_der(der
+counter
,der_len
-counter
,&len
);
590 if(p_vet
->value
>p2_vet
->value
){
591 /* change position */
592 temp
=(unsigned char *)_asn1_alloca( p_vet
->end
-counter
);
593 if (temp
==NULL
) return;
595 memcpy(temp
,der
+counter
,p_vet
->end
-counter
);
596 memcpy(der
+counter
,der
+p_vet
->end
,p2_vet
->end
-p_vet
->end
);
597 memcpy(der
+counter
+p2_vet
->end
-p_vet
->end
,temp
,p_vet
->end
-counter
);
601 p_vet
->value
=p2_vet
->value
;
604 p_vet
->end
=counter
+(p2_vet
->end
-p_vet
->end
);
612 if(p_vet
!=first
) p_vet
->prev
->next
=NULL
;
619 /******************************************************/
620 /* Function : _asn1_ordering_set_of */
621 /* Description: puts the elements of a SET OF type in */
622 /* the correct order according to DER rules. */
624 /* der: string with the DER coding. */
625 /* node: pointer to the SET OF element. */
627 /******************************************************/
629 _asn1_ordering_set_of(unsigned char *der
, int der_len
, node_asn
*node
)
633 struct vet
*next
,*prev
;
636 int counter
,len
,len2
,change
;
637 struct vet
*first
,*last
,*p_vet
,*p2_vet
;
639 unsigned char *temp
,class;
644 if(type_field(node
->type
)!=TYPE_SET_OF
) return;
647 while((type_field(p
->type
)==TYPE_TAG
) || (type_field(p
->type
)==TYPE_SIZE
)) p
=p
->right
;
650 if((p
==NULL
) || (p
->right
==NULL
)) return;
654 p_vet
=(struct vet
*)_asn1_alloca(sizeof(struct vet
));
655 if (p_vet
==NULL
) return;
659 if(first
==NULL
) first
=p_vet
;
660 else last
->next
=p_vet
;
663 /* extraction of tag and length */
664 if (der_len
-counter
> 0) {
666 if (asn1_get_tag_der(der
+counter
, der_len
- counter
, &class,&len
,NULL
)!=ASN1_SUCCESS
)
670 len2
=asn1_get_length_der(der
+counter
,der_len
-counter
,&len
);
685 if((p_vet
->end
-counter
)>(p2_vet
->end
-p_vet
->end
))
686 max
=p_vet
->end
-counter
;
688 max
=p2_vet
->end
-p_vet
->end
;
692 if(der
[counter
+k
]>der
[p_vet
->end
+k
]){change
=1;break;}
693 else if(der
[counter
+k
]<der
[p_vet
->end
+k
]){change
=0;break;}
695 if((change
==-1) && ((p_vet
->end
-counter
)>(p2_vet
->end
-p_vet
->end
)))
699 /* change position */
700 temp
=(unsigned char *)_asn1_alloca(p_vet
->end
-counter
);
701 if (temp
==NULL
) return;
703 memcpy(temp
,der
+counter
,(p_vet
->end
)-counter
);
704 memcpy(der
+counter
,der
+(p_vet
->end
),(p2_vet
->end
)-(p_vet
->end
));
705 memcpy(der
+counter
+(p2_vet
->end
)-(p_vet
->end
),temp
,(p_vet
->end
)-counter
);
708 p_vet
->end
=counter
+(p2_vet
->end
-p_vet
->end
);
716 if(p_vet
!=first
) p_vet
->prev
->next
=NULL
;
724 * asn1_der_coding - Creates the DER encoding for the NAME structure
725 * @element: pointer to an ASN1 element
726 * @name: the name of the structure you want to encode (it must be
728 * @ider: vector that will contain the DER encoding. DER must be a
729 * pointer to memory cells already allocated.
730 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1], Initialy
731 * holds the sizeof of der vector.
732 * @errorDescription : return the error description or an empty
735 * Creates the DER encoding for the NAME structure (inside *POINTER
740 * ASN1_SUCCESS: DER encoding OK.
742 * ASN1_ELEMENT_NOT_FOUND: NAME is not a valid element.
744 * ASN1_VALUE_NOT_FOUND: There is an element without a value.
746 * ASN1_MEM_ERROR: @ider vector isn't big enough. Also in this case
747 * LEN will contain the length needed.
751 asn1_der_coding(ASN1_TYPE element
,const char *name
,void *ider
,int *len
,
752 char *ErrorDescription
)
754 node_asn
*node
,*p
,*p2
;
755 char temp
[SIZEOF_UNSIGNED_LONG_INT
*3+1];
756 int counter
,counter_old
,len2
,len3
,tlen
,move
,max_len
,max_len_old
;
758 unsigned char* der
= ider
;
760 node
=asn1_find_node(element
,name
);
761 if(node
==NULL
) return ASN1_ELEMENT_NOT_FOUND
;
763 /* Node is now a locally allocated variable.
764 * That is because in some point we modify the
765 * structure, and I don't know why! --nmav
767 node
= _asn1_copy_structure3( node
);
769 return ASN1_ELEMENT_NOT_FOUND
;
781 err
= _asn1_insert_tag_der(p
,der
,&counter
,&max_len
);
782 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
785 switch(type_field(p
->type
)){
793 if((p
->type
&CONST_DEFAULT
) && (p
->value
==NULL
)){
799 _asn1_error_description_value_not_found(p
,ErrorDescription
);
800 err
= ASN1_VALUE_NOT_FOUND
;
806 if(p
->value
[0]=='F') der
[counter
++]=0;
807 else der
[counter
++]=0xFF;
814 case TYPE_INTEGER
: case TYPE_ENUMERATED
:
815 if((p
->type
&CONST_DEFAULT
) && (p
->value
==NULL
)){
821 _asn1_error_description_value_not_found(p
,ErrorDescription
);
822 err
= ASN1_VALUE_NOT_FOUND
;
825 len2
=asn1_get_length_der(p
->value
,p
->value_len
, &len3
);
827 err
= ASN1_DER_ERROR
;
830 max_len
-= len2
+len3
;
832 memcpy(der
+counter
,p
->value
,len3
+len2
);
838 if((p
->type
&CONST_DEFAULT
) && (p
->value
==NULL
)){
844 _asn1_error_description_value_not_found(p
,ErrorDescription
);
845 err
= ASN1_VALUE_NOT_FOUND
;
849 err
= _asn1_objectid_der(p
->value
,der
+counter
,&len2
);
850 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
860 _asn1_error_description_value_not_found(p
,ErrorDescription
);
861 err
= ASN1_VALUE_NOT_FOUND
;
865 err
= _asn1_time_der(p
->value
,der
+counter
,&len2
);
866 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
873 case TYPE_OCTET_STRING
:
875 _asn1_error_description_value_not_found(p
,ErrorDescription
);
876 err
= ASN1_VALUE_NOT_FOUND
;
879 len2
=asn1_get_length_der(p
->value
,p
->value_len
,&len3
);
881 err
= ASN1_DER_ERROR
;
886 memcpy(der
+counter
,p
->value
,len3
+len2
);
890 case TYPE_GENERALSTRING
:
892 _asn1_error_description_value_not_found(p
,ErrorDescription
);
893 err
= ASN1_VALUE_NOT_FOUND
;
896 len2
=asn1_get_length_der(p
->value
,p
->value_len
,&len3
);
898 err
= ASN1_DER_ERROR
;
903 memcpy(der
+counter
,p
->value
,len3
+len2
);
907 case TYPE_BIT_STRING
:
909 _asn1_error_description_value_not_found(p
,ErrorDescription
);
910 err
= ASN1_VALUE_NOT_FOUND
;
913 len2
=asn1_get_length_der(p
->value
,p
->value_len
,&len3
);
915 err
= ASN1_DER_ERROR
;\
920 memcpy(der
+counter
,p
->value
,len3
+len2
);
924 case TYPE_SEQUENCE
: case TYPE_SET
:
926 _asn1_ltostr(counter
,temp
);
929 _asn1_set_value(p
,temp
,tlen
+1);
936 while(p2
&& (type_field(p2
->type
)==TYPE_TAG
)) p2
=p2
->right
;
947 len2
=strtol(p
->value
,NULL
,10);
948 _asn1_set_value(p
,NULL
,0);
949 if((type_field(p
->type
)==TYPE_SET
) && (max_len
>=0))
950 _asn1_ordering_set(der
+len2
, max_len
-len2
,p
);
951 asn1_length_der(counter
-len2
,temp
,&len3
);
954 memmove(der
+len2
+len3
,der
+len2
,counter
-len2
);
955 memcpy(der
+len2
,temp
,len3
);
961 case TYPE_SEQUENCE_OF
: case TYPE_SET_OF
:
963 _asn1_ltostr(counter
,temp
);
967 _asn1_set_value(p
,temp
,tlen
+1);
969 while((type_field(p
->type
)==TYPE_TAG
) || (type_field(p
->type
)==TYPE_SIZE
)) p
=p
->right
;
975 else p
=_asn1_find_up(p
);
979 len2
=strtol(p
->value
,NULL
,10);
980 _asn1_set_value(p
,NULL
,0);
981 if((type_field(p
->type
)==TYPE_SET_OF
) && (max_len
-len2
>0)) {
982 _asn1_ordering_set_of(der
+len2
, max_len
-len2
,p
);
984 asn1_length_der(counter
-len2
,temp
,&len3
);
987 memmove(der
+len2
+len3
,der
+len2
,counter
-len2
);
988 memcpy(der
+len2
,temp
,len3
);
996 _asn1_error_description_value_not_found(p
,ErrorDescription
);
997 err
= ASN1_VALUE_NOT_FOUND
;
1000 len2
=asn1_get_length_der(p
->value
,p
->value_len
,&len3
);
1002 err
= ASN1_DER_ERROR
;
1007 memcpy(der
+counter
,p
->value
+len3
,len2
);
1012 move
=(move
==UP
)?RIGHT
:DOWN
;
1016 if((move
!=DOWN
) && (counter
!=counter_old
)){
1017 err
=_asn1_complete_explicit_tag(p
,der
,&counter
,&max_len
);
1018 if (err
!= ASN1_SUCCESS
&& err
!= ASN1_MEM_ERROR
)
1022 if(p
==node
&& move
!=DOWN
) break;
1025 if(p
->down
) p
=p
->down
;
1029 if(p
->right
) p
=p
->right
;
1032 if(move
==UP
) p
=_asn1_find_up(p
);
1038 err
= ASN1_MEM_ERROR
;
1045 asn1_delete_structure(&node
);