*** empty log message ***
[shishi.git] / asn1 / element.c
blobc2a71d935b58f2fe47d3341886300c3f72df046e
1 /*
2 * Copyright (C) 2000,2001,2002,2003 Fabio Fiorina
4 * This file is part of LIBASN1.
6 * The LIBTASN1 library is free software; you can redistribute it and/or
7 * 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,
12 * but 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /*****************************************************/
22 /* File: element.c */
23 /* Description: Functions with the read and write */
24 /* functions. */
25 /*****************************************************/
28 #include <int.h>
29 #include <errors.h>
30 #include "parser_aux.h"
31 #include "der.h"
32 #include <gstr.h>
33 #include "structure.h"
35 void
36 _asn1_hierarchical_name(node_asn *node,char *name,int name_size)
38 node_asn *p;
39 char tmp_name[64];
41 p=node;
43 name[0]=0;
45 while(p != NULL){
46 if(p->name != NULL){
47 _asn1_str_cpy(tmp_name,sizeof(tmp_name),name),
49 _asn1_str_cpy(name,name_size,p->name);
50 _asn1_str_cat(name,name_size,".");
51 _asn1_str_cat(name,name_size,tmp_name);
53 p=_asn1_find_up(p);
56 if(name[0]==0) _asn1_str_cpy(name,name_size,"ROOT");
60 /******************************************************************/
61 /* Function : _asn1_convert_integer */
62 /* Description: converts an integer from a null terminated string */
63 /* to der decoding. The convertion from a null */
64 /* terminated string to an integer is made with */
65 /* the 'strtol' function. */
66 /* Parameters: */
67 /* value: null terminated string to convert. */
68 /* value_out: convertion result (memory must be already */
69 /* allocated). */
70 /* value_out_size: number of bytes of value_out. */
71 /* len: number of significant byte of value_out. */
72 /* Return: ASN1_MEM_ERROR or ASN1_SUCCESS */
73 /******************************************************************/
74 asn1_retCode
75 _asn1_convert_integer(const char *value,unsigned char *value_out,int value_out_size, int *len)
77 char negative;
78 unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
79 long valtmp;
80 int k,k2;
82 valtmp=strtol(value,NULL,10);
84 for(k=0;k<SIZEOF_UNSIGNED_LONG_INT;k++){
85 val[SIZEOF_UNSIGNED_LONG_INT-k-1]=(valtmp >> (8*k)) & 0xFF;
88 if(val[0]&0x80) negative=1;
89 else negative=0;
91 for(k=0;k<SIZEOF_UNSIGNED_LONG_INT-1;k++){
92 if(negative && (val[k]!=0xFF)) break;
93 else if(!negative && val[k]) break;
96 if((negative && !(val[k]&0x80)) ||
97 (!negative && (val[k]&0x80))) k--;
99 *len=SIZEOF_UNSIGNED_LONG_INT-k;
101 if (SIZEOF_UNSIGNED_LONG_INT-k> value_out_size)
102 /* VALUE_OUT is too short to contain the value convertion */
103 return ASN1_MEM_ERROR;
105 for(k2=k;k2<SIZEOF_UNSIGNED_LONG_INT;k2++)
106 value_out[k2-k]=val[k2];
109 #ifdef LIBTASN1_DEBUG_INTEGER
110 _libtasn1_log("_asn1_convert_integer: valueIn=%s, lenOut=%d",value,*len);
111 for(k=0;k<SIZEOF_UNSIGNED_LONG_INT;k++)
112 _libtasn1_log(", vOut[%d]=%d",k,value_out[k]);
113 _libtasn1_log("\n");
114 #endif
117 return ASN1_SUCCESS;
121 int
122 _asn1_append_sequence_set(node_asn *node)
124 node_asn *p,*p2;
125 char temp[10];
126 long n;
128 if(!node || !(node->down)) return ASN1_GENERIC_ERROR;
130 p=node->down;
131 while((type_field(p->type)==TYPE_TAG) || (type_field(p->type)==TYPE_SIZE)) p=p->right;
132 p2=_asn1_copy_structure3(p);
133 while(p->right) p=p->right;
134 _asn1_set_right(p,p2);
136 if(p->name==NULL) _asn1_str_cpy(temp,sizeof(temp),"?1");
137 else{
138 n=strtol(p->name+1,NULL,0);
139 n++;
140 temp[0]='?';
141 _asn1_ltostr(n,temp+1);
143 _asn1_set_name(p2,temp);
144 /* p2->type |= CONST_OPTION; */
146 return ASN1_SUCCESS;
151 * asn1_write_value - Set the value of one element inside a structure.
152 * @node_root: pointer to a structure
153 * @name: the name of the element inside the structure that you want to set.
154 * @value: vector used to specify the value to set. If len is >0,
155 * VALUE must be a two's complement form integer.
156 * if len=0 *VALUE must be a null terminated string with an integer value.
157 * @len: number of bytes of *value to use to set the value: value[0]..value[len-1]
158 * or 0 if value is a null terminated string
159 * Description:
161 * Set the value of one element inside a structure.
163 * Returns:
165 * ASN1_SUCCESS\: set value OK
167 * ASN1_ELEMENT_NOT_FOUND\: NAME is not a valid element.
169 * ASN1_VALUE_NOT_VALID\: VALUE has a wrong format.
171 * Examples:
172 * description for each type
174 *\begin{itemize}
175 * \item INTEGER\: VALUE must contain a two's complement form integer.
176 * value[0]=0xFF , len=1 -> integer=-1
177 * value[0]=0xFF value[1]=0xFF , len=2 -> integer=-1
178 * value[0]=0x01 , len=1 -> integer= 1
179 * value[0]=0x00 value[1]=0x01 , len=2 -> integer= 1
180 * value="123" , len=0 -> integer= 123
182 * \item ENUMERATED\: as INTEGER (but only with not negative numbers)
184 * \item BOOLEAN\: VALUE must be the null terminated string "TRUE" or "FALSE" and LEN != 0
185 * value="TRUE" , len=1 -> boolean=TRUE
186 * value="FALSE" , len=1 -> boolean=FALSE
188 * \item OBJECT IDENTIFIER\: VALUE must be a null terminated string with each number separated by
189 * a dot (e.g. "1.2.3.543.1").
190 * LEN != 0
191 * value="1 2 840 10040 4 3" , len=1 -> OID=dsa-with-sha
193 * \item UTCTime\: VALUE must be a null terminated string in one of these formats\:
194 * "YYMMDDhhmmssZ" "YYMMDDhhmmssZ" "YYMMDDhhmmss+hh'mm'" "YYMMDDhhmmss-hh'mm'"
195 * "YYMMDDhhmm+hh'mm'" "YYMMDDhhmm-hh'mm'".
196 * LEN != 0
197 * value="9801011200Z" , len=1 -> time=Jannuary 1st, 1998 at 12h 00m Greenwich Mean Time
199 * \item GeneralizedTime\: VALUE must be in one of this format\:
200 * "YYYYMMDDhhmmss.sZ" "YYYYMMDDhhmmss.sZ" "YYYYMMDDhhmmss.s+hh'mm'"
201 * "YYYYMMDDhhmmss.s-hh'mm'" "YYYYMMDDhhmm+hh'mm'" "YYYYMMDDhhmm-hh'mm'"
202 * where ss.s indicates the seconds with any precision like "10.1" or "01.02".
203 * LEN != 0
204 * value="2001010112001.12-0700" , len=1 -> time=Jannuary 1st, 2001 at 12h 00m 01.12s
205 * Pacific Daylight Time
207 * \item OCTET STRING\: VALUE contains the octet string and LEN is the number of octet.
208 * value="$\backslash$x01$\backslash$x02$\backslash$x03" , len=3 -> three bytes octet string
210 * \item GeneralString\: VALUE contains the generalstring and LEN is the number of octet.
211 * value="$\backslash$x01$\backslash$x02$\backslash$x03" , len=3 -> three bytes generalstring
213 * \item BIT STRING\: VALUE contains the bit string organized by bytes and LEN is the number of bits.
214 * value="$\backslash$xCF" , len=6 -> bit string="110011" (six bits)
216 * \item CHOICE\: if NAME indicates a choice type, VALUE must specify one of the alternatives with a
217 * null terminated string. LEN != 0
218 * Using "pkix.asn"\:
219 * result=asn1_write_value(cert,"certificate1.tbsCertificate.subject","rdnSequence",1);
221 * \item ANY\: VALUE indicates the der encoding of a structure.
222 * LEN != 0
224 * \item SEQUENCE OF\: VALUE must be the null terminated string "NEW" and LEN != 0. With this
225 * instruction another element is appended in the sequence. The name of this
226 * element will be "?1" if it's the first one, "?2" for the second and so on.
228 * Using "pkix.asn"\:
230 * result=asn1_write_value(cert,"certificate1.tbsCertificate.subject.rdnSequence","NEW",1);
232 * \item SET OF\: the same as SEQUENCE OF.
233 * Using "pkix.asn":
235 * result=asn1_write_value(cert,"tbsCertificate.subject.rdnSequence.?LAST","NEW",1);
236 *\end{itemize}
238 * If an element is OPTIONAL and you want to delete it, you must use the value=NULL and len=0.
240 * Using "pkix.asn"\:
242 * result=asn1_write_value(cert,"tbsCertificate.issuerUniqueID",NULL,0);
245 asn1_retCode
246 asn1_write_value(node_asn *node_root,const char *name,
247 const unsigned char *value,int len)
249 node_asn *node,*p,*p2;
250 unsigned char *temp,*value_temp=NULL,*default_temp=NULL;
251 int len2,k,k2,negative;
253 node=_asn1_find_node(node_root,name);
254 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
256 if((node->type & CONST_OPTION) && (value==NULL) && (len==0)){
257 asn1_delete_structure(&node);
258 return ASN1_SUCCESS;
261 if((type_field(node->type) == TYPE_SEQUENCE_OF) && (value == NULL) && (len==0)){
262 p=node->down;
263 while((type_field(p->type)==TYPE_TAG) || (type_field(p->type)==TYPE_SIZE)) p=p->right;
265 while(p->right)
266 asn1_delete_structure(&p->right);
268 return ASN1_SUCCESS;
271 switch(type_field(node->type)){
272 case TYPE_BOOLEAN:
273 if(!strcmp(value,"TRUE")){
274 if(node->type&CONST_DEFAULT){
275 p=node->down;
276 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
277 if(p->type&CONST_TRUE) _asn1_set_value(node,NULL,0);
278 else _asn1_set_value(node,"T",1);
280 else _asn1_set_value(node,"T",1);
282 else if(!strcmp(value,"FALSE")){
283 if(node->type&CONST_DEFAULT){
284 p=node->down;
285 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
286 if(p->type&CONST_FALSE) _asn1_set_value(node,NULL,0);
287 else _asn1_set_value(node,"F",1);
289 else _asn1_set_value(node,"F",1);
291 else return ASN1_VALUE_NOT_VALID;
292 break;
293 case TYPE_INTEGER: case TYPE_ENUMERATED:
294 if(len==0){
295 if((isdigit(value[0])) || (value[0]=='-')){
296 value_temp=(unsigned char *)_asn1_alloca(SIZEOF_UNSIGNED_LONG_INT);
297 if (value_temp==NULL) return ASN1_MEM_ALLOC_ERROR;
299 _asn1_convert_integer(value,value_temp,SIZEOF_UNSIGNED_LONG_INT, &len);
301 else{ /* is an identifier like v1 */
302 if(!(node->type&CONST_LIST)) return ASN1_VALUE_NOT_VALID;
303 p=node->down;
304 while(p){
305 if(type_field(p->type)==TYPE_CONSTANT){
306 if((p->name) && (!strcmp(p->name,value))){
307 value_temp=(unsigned char *)_asn1_alloca(SIZEOF_UNSIGNED_LONG_INT);
308 if (value_temp==NULL) return ASN1_MEM_ALLOC_ERROR;
310 _asn1_convert_integer(p->value,value_temp,SIZEOF_UNSIGNED_LONG_INT, &len);
311 break;
314 p=p->right;
316 if(p==NULL) return ASN1_VALUE_NOT_VALID;
319 else{ /* len != 0 */
320 value_temp=(unsigned char *)_asn1_alloca(len);
321 if (value_temp==NULL) return ASN1_MEM_ALLOC_ERROR;
322 memcpy(value_temp,value,len);
326 if(value_temp[0]&0x80) negative=1;
327 else negative=0;
329 if(negative && (type_field(node->type)==TYPE_ENUMERATED))
330 {_asn1_afree(value_temp);return ASN1_VALUE_NOT_VALID;}
332 for(k=0;k<len-1;k++)
333 if(negative && (value_temp[k]!=0xFF)) break;
334 else if(!negative && value_temp[k]) break;
336 if((negative && !(value_temp[k]&0x80)) ||
337 (!negative && (value_temp[k]&0x80))) k--;
339 _asn1_length_der(len-k,NULL,&len2);
340 temp=(unsigned char *)_asn1_alloca(len-k+len2);
341 if (temp==NULL) return ASN1_MEM_ALLOC_ERROR;
343 _asn1_octet_der(value_temp+k,len-k,temp,&len2);
344 _asn1_set_value(node,temp,len2);
346 _asn1_afree(temp);
349 if(node->type&CONST_DEFAULT){
350 p=node->down;
351 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
352 if((isdigit(p->value[0])) || (p->value[0]=='-')){
353 default_temp=(unsigned char *)_asn1_alloca(SIZEOF_UNSIGNED_LONG_INT);
354 if (default_temp==NULL) return ASN1_MEM_ALLOC_ERROR;
356 _asn1_convert_integer(p->value,default_temp,SIZEOF_UNSIGNED_LONG_INT,&len2);
358 else{ /* is an identifier like v1 */
359 if(!(node->type&CONST_LIST)) return ASN1_VALUE_NOT_VALID;
360 p2=node->down;
361 while(p2){
362 if(type_field(p2->type)==TYPE_CONSTANT){
363 if((p2->name) && (!strcmp(p2->name,p->value))){
364 default_temp=(unsigned char *)_asn1_alloca(SIZEOF_UNSIGNED_LONG_INT);
365 if (default_temp==NULL) return ASN1_MEM_ALLOC_ERROR;
367 _asn1_convert_integer(p2->value,default_temp,SIZEOF_UNSIGNED_LONG_INT,&len2);
368 break;
371 p2=p2->right;
373 if(p2==NULL) return ASN1_VALUE_NOT_VALID;
377 if((len-k)==len2){
378 for(k2=0;k2<len2;k2++)
379 if(value_temp[k+k2]!=default_temp[k2]){
380 break;
382 if(k2==len2) _asn1_set_value(node,NULL,0);
384 _asn1_afree(default_temp);
386 _asn1_afree(value_temp);
387 break;
388 case TYPE_OBJECT_ID:
389 for(k=0;k<strlen(value);k++)
390 if((!isdigit(value[k])) && (value[k]!='.') && (value[k]!='+'))
391 return ASN1_VALUE_NOT_VALID;
392 if(node->type&CONST_DEFAULT){
393 p=node->down;
394 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
395 if(!strcmp(value,p->value)){
396 _asn1_set_value(node,NULL,0);
397 break;
400 _asn1_set_value(node,value,strlen(value)+1);
401 break;
402 case TYPE_TIME:
403 if(node->type&CONST_UTC){
404 if(strlen(value)<11) return ASN1_VALUE_NOT_VALID;
405 for(k=0;k<10;k++)
406 if(!isdigit(value[k])) return ASN1_VALUE_NOT_VALID;
407 switch(strlen(value)){
408 case 11:
409 if(value[10]!='Z') return ASN1_VALUE_NOT_VALID;
410 break;
411 case 13:
412 if((!isdigit(value[10])) || (!isdigit(value[11])) ||
413 (value[12]!='Z')) return ASN1_VALUE_NOT_VALID;
414 break;
415 case 15:
416 if((value[10]!='+') && (value[10]!='-')) return ASN1_VALUE_NOT_VALID;
417 for(k=11;k<15;k++)
418 if(!isdigit(value[k])) return ASN1_VALUE_NOT_VALID;
419 break;
420 case 17:
421 if((!isdigit(value[10])) || (!isdigit(value[11])))
422 return ASN1_VALUE_NOT_VALID;
423 if((value[12]!='+') && (value[12]!='-')) return ASN1_VALUE_NOT_VALID;
424 for(k=13;k<17;k++)
425 if(!isdigit(value[k])) return ASN1_VALUE_NOT_VALID;
426 break;
427 default:
428 return ASN1_VALUE_NOT_FOUND;
430 _asn1_set_value(node,value,strlen(value)+1);
432 else{ /* GENERALIZED TIME */
433 if(value) _asn1_set_value(node,value,strlen(value)+1);
435 break;
436 case TYPE_OCTET_STRING:
437 if(len==0)
438 len=strlen(value);
439 _asn1_length_der(len,NULL,&len2);
440 temp=(unsigned char *)_asn1_alloca(len+len2);
441 if (temp==NULL) return ASN1_MEM_ALLOC_ERROR;
443 _asn1_octet_der(value,len,temp,&len2);
444 _asn1_set_value(node,temp,len2);
445 _asn1_afree(temp);
446 break;
447 case TYPE_GENERALSTRING:
448 if(len==0)
449 len=strlen(value);
450 _asn1_length_der(len,NULL,&len2);
451 temp=(unsigned char *)_asn1_alloca(len+len2);
452 if (temp==NULL) return ASN1_MEM_ALLOC_ERROR;
454 _asn1_octet_der(value,len,temp,&len2);
455 _asn1_set_value(node,temp,len2);
456 _asn1_afree(temp);
457 break;
458 case TYPE_BIT_STRING:
459 if(len==0)
460 len=strlen(value);
461 _asn1_length_der((len>>3)+2,NULL,&len2);
462 temp=(unsigned char *)_asn1_alloca((len>>3)+2+len2);
463 if (temp==NULL) return ASN1_MEM_ALLOC_ERROR;
465 _asn1_bit_der(value,len,temp,&len2);
466 _asn1_set_value(node,temp,len2);
467 _asn1_afree(temp);
468 break;
469 case TYPE_CHOICE:
470 p=node->down;
471 while(p){
472 if(!strcmp(p->name,value)){
473 p2=node->down;
474 while(p2){
475 if(p2!=p){asn1_delete_structure(&p2); p2=node->down;}
476 else p2=p2->right;
478 break;
480 p=p->right;
482 if(!p) return ASN1_ELEMENT_NOT_FOUND;
483 break;
484 case TYPE_ANY:
485 _asn1_length_der(len,NULL,&len2);
486 temp=(unsigned char *)_asn1_alloca(len+len2);
487 if (temp==NULL) return ASN1_MEM_ALLOC_ERROR;
489 _asn1_octet_der(value,len,temp,&len2);
490 _asn1_set_value(node,temp,len2);
491 _asn1_afree(temp);
492 break;
493 case TYPE_SEQUENCE_OF: case TYPE_SET_OF:
494 if(strcmp(value,"NEW")) return ASN1_VALUE_NOT_VALID;
495 _asn1_append_sequence_set(node);
496 break;
497 default:
498 return ASN1_ELEMENT_NOT_FOUND;
499 break;
502 return ASN1_SUCCESS;
506 #define PUT_VALUE( ptr, ptr_size, data, data_size) \
507 *len = data_size; \
508 if (ptr_size < data_size) { \
509 return ASN1_MEM_ERROR; \
510 } else { \
511 memcpy( ptr, data, data_size); \
514 #define PUT_STR_VALUE( ptr, ptr_size, data) \
515 *len = strlen(data) + 1; \
516 if (ptr_size < *len) { \
517 return ASN1_MEM_ERROR; \
518 } else { \
519 /* this strcpy is checked */ \
520 strcpy(ptr, data); \
523 #define ADD_STR_VALUE( ptr, ptr_size, data) \
524 *len = strlen(data) + 1; \
525 if (ptr_size < strlen(ptr)+(*len)) { \
526 return ASN1_MEM_ERROR; \
527 } else { \
528 /* this strcat is checked */ \
529 strcat(ptr, data); \
533 * asn1_read_value - Returns the value of one element inside a structure
534 * @root: pointer to a structure
535 * @name: the name of the element inside a structure that you want to read.
536 * @value: vector that will contain the element's content.
537 * VALUE must be a pointer to memory cells already allocated.
538 * @len: number of bytes of *value: value[0]..value[len-1]. Initialy holds the sizeof value.
540 * Description:
542 * Returns the value of one element inside a structure.
544 * Returns:
546 * ASN1_SUCCESS\: set value OK
548 * ASN1_ELEMENT_NOT_FOUND\: NAME is not a valid element.
550 * ASN1_VALUE_NOT_FOUND\: there isn't any value for the element selected.
552 * ASN1_MEM_ERROR\: the value vector isn't big enough to store the result.
553 * In this case LEN will contain the number of bytes needed.
555 * Examples:
556 * a description for each type
558 *\begin{itemize}
559 * \item INTEGER\: VALUE will contain a two's complement form integer.
560 * integer=-1 -> value[0]=0xFF , len=1
561 * integer=1 -> value[0]=0x01 , len=1
563 * \item ENUMERATED\: as INTEGER (but only with not negative numbers)
565 * \item BOOLEAN\: VALUE will be the null terminated string "TRUE" or "FALSE" and LEN=5 or LEN=6
567 * \item OBJECT IDENTIFIER\: VALUE will be a null terminated string with each number separated by
568 * a dot (i.e. "1.2.3.543.1").
569 * LEN = strlen(VALUE)+1
571 * \item UTCTime\: VALUE will be a null terminated string in one of these formats\:
572 * "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'"
573 * LEN=strlen(VALUE)+1
575 * \item GeneralizedTime\: VALUE will be a null terminated string in the same format used to set
576 * the value
578 * \item OCTET STRING\: VALUE will contain the octet string and LEN will be the number of octet.
580 * \item GeneralString\: VALUE will contain the generalstring and LEN will be the number of octet.
582 * \item BIT STRING\: VALUE will contain the bit string organized by bytes and LEN will be the
583 * number of bits.
585 * \item CHOICE\: if NAME indicates a choice type, VALUE will specify the alternative selected
587 * \item ANY\: if NAME indicates an any type, VALUE will indicate the DER encoding of the structure
588 * actually used.
589 *\end{itemize}
591 * If an element is OPTIONAL and the function "read_value" returns ASN1_ELEMENT_NOT_FOUND, it
592 * means that this element wasn't present in the der encoding that created the structure.
593 * The first element of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and so on.
596 asn1_retCode
597 asn1_read_value(node_asn *root,const char *name,unsigned char *value, int *len)
599 node_asn *node,*p,*p2;
600 int len2,len3;
601 int value_size = *len;
603 node=_asn1_find_node(root,name);
604 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
606 if((type_field(node->type)!=TYPE_NULL) &&
607 (type_field(node->type)!=TYPE_CHOICE) &&
608 !(node->type&CONST_DEFAULT) && !(node->type&CONST_ASSIGN) &&
609 (node->value==NULL))
610 return ASN1_VALUE_NOT_FOUND;
612 switch(type_field(node->type)){
613 case TYPE_NULL:
614 PUT_STR_VALUE( value, value_size, "NULL");
615 break;
616 case TYPE_BOOLEAN:
617 if((node->type&CONST_DEFAULT) && (node->value==NULL)){
618 p=node->down;
619 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
620 if(p->type&CONST_TRUE) {
621 PUT_STR_VALUE( value, value_size, "TRUE");
622 } else {
623 PUT_STR_VALUE(value, value_size, "FALSE");
626 else if(node->value[0]=='T') {
627 PUT_STR_VALUE(value, value_size, "TRUE");
629 else {
630 PUT_STR_VALUE(value, value_size, "FALSE");
632 break;
633 case TYPE_INTEGER: case TYPE_ENUMERATED:
634 if((node->type&CONST_DEFAULT) && (node->value==NULL)){
635 p=node->down;
636 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
637 if((isdigit(p->value[0])) || (p->value[0]=='-') || (p->value[0]=='+')){
638 if (_asn1_convert_integer(p->value,value,value_size, len) !=
639 ASN1_SUCCESS)
640 return ASN1_MEM_ERROR;
642 else{ /* is an identifier like v1 */
643 p2=node->down;
644 while(p2){
645 if(type_field(p2->type)==TYPE_CONSTANT){
646 if((p2->name) && (!strcmp(p2->name,p->value))){
647 if (_asn1_convert_integer(p2->value,value,value_size, len) !=
648 ASN1_SUCCESS)
649 return ASN1_MEM_ERROR;
650 break;
653 p2=p2->right;
657 else{
658 len2=-1;
659 if (_asn1_get_octet_der(node->value,&len2,value, value_size, len)!=ASN1_SUCCESS) return ASN1_MEM_ERROR;
661 break;
662 case TYPE_OBJECT_ID:
663 if(node->type&CONST_ASSIGN){
664 value[0]=0;
665 p=node->down;
666 while(p){
667 if(type_field(p->type)==TYPE_CONSTANT){
668 ADD_STR_VALUE(value,value_size,p->value);
669 if(p->right) {
670 ADD_STR_VALUE(value,value_size,".");
673 p=p->right;
675 *len = strlen(value) + 1;
677 else if((node->type&CONST_DEFAULT) && (node->value==NULL)){
678 p=node->down;
679 while(type_field(p->type)!=TYPE_DEFAULT) p=p->right;
680 PUT_STR_VALUE(value, value_size, p->value);
682 else {
683 PUT_STR_VALUE(value, value_size, node->value);
685 break;
686 case TYPE_TIME:
687 PUT_STR_VALUE( value, value_size, node->value);
688 break;
689 case TYPE_OCTET_STRING:
690 len2=-1;
691 if (_asn1_get_octet_der(node->value,&len2,value, value_size, len)!=ASN1_SUCCESS) return ASN1_MEM_ERROR;
692 break;
693 case TYPE_GENERALSTRING:
694 len2=-1;
695 if (_asn1_get_octet_der(node->value,&len2,value, value_size, len)!=ASN1_SUCCESS) return ASN1_MEM_ERROR;
696 break;
697 case TYPE_BIT_STRING:
698 len2=-1;
699 if (_asn1_get_bit_der(node->value,&len2,value,value_size,len)!=ASN1_SUCCESS) return ASN1_MEM_ERROR;
700 break;
701 case TYPE_CHOICE:
702 PUT_STR_VALUE( value, value_size, node->down->name);
703 break;
704 case TYPE_ANY:
705 len3=-1;
706 len2=_asn1_get_length_der(node->value,&len3);
707 PUT_VALUE( value, value_size, node->value+len3, len2);
708 break;
709 default:
710 return ASN1_ELEMENT_NOT_FOUND;
711 break;
713 return ASN1_SUCCESS;
718 * asn1_read_tag - Returns the TAG of one element inside a structure
719 * @root: pointer to a structure
720 * @name: the name of the element inside a structure.
721 * @tagValue: variable that will contain the TAG value.
722 * @classValue: variable that will specify the TAG type.
724 * Description:
726 * Returns the TAG and the CLASS of one element inside a structure.
727 * CLASS can have one of these constants: ASN1_CLASS_APPLICATION,
728 * ASN1_CLASS_UNIVERSAL, ASN1_CLASS_PRIVATE or ASN1_CLASS_CONTEXT_SPECIFIC.
730 * Returns:
732 * ASN1_SUCCESS\: set value OK
734 * ASN1_ELEMENT_NOT_FOUND\: NAME is not a valid element.
737 asn1_retCode
738 asn1_read_tag(node_asn *root,const char *name,int *tagValue, int *classValue)
740 node_asn *node,*p,*pTag;
742 node=_asn1_find_node(root,name);
743 if(node==NULL) return ASN1_ELEMENT_NOT_FOUND;
745 p=node->down;
747 /* pTag will points to the IMPLICIT TAG */
748 pTag=NULL;
749 if(node->type&CONST_TAG){
750 while(p){
751 if(type_field(p->type)==TYPE_TAG){
752 if((p->type&CONST_IMPLICIT) && (pTag==NULL))
753 pTag=p;
754 else if(p->type&CONST_EXPLICIT)
755 pTag=NULL;
757 p=p->right;
761 if(pTag){
762 *tagValue=strtoul(pTag->value,NULL,10);
764 if(pTag->type&CONST_APPLICATION) *classValue=ASN1_CLASS_APPLICATION;
765 else if(pTag->type&CONST_UNIVERSAL) *classValue=ASN1_CLASS_UNIVERSAL;
766 else if(pTag->type&CONST_PRIVATE) *classValue=ASN1_CLASS_PRIVATE;
767 else *classValue=ASN1_CLASS_CONTEXT_SPECIFIC;
769 else{
770 *classValue=ASN1_CLASS_UNIVERSAL;
772 switch(type_field(node->type)){
773 case TYPE_NULL:
774 *tagValue=ASN1_TAG_NULL;break;
775 case TYPE_BOOLEAN:
776 *tagValue=ASN1_TAG_BOOLEAN;break;
777 case TYPE_INTEGER:
778 *tagValue=ASN1_TAG_INTEGER;break;
779 case TYPE_ENUMERATED:
780 *tagValue=ASN1_TAG_ENUMERATED;break;
781 case TYPE_OBJECT_ID:
782 *tagValue=ASN1_TAG_OBJECT_ID;break;
783 case TYPE_TIME:
784 if(node->type&CONST_UTC){
785 *tagValue=ASN1_TAG_UTCTime;
787 else *tagValue=ASN1_TAG_GENERALIZEDTime;
788 break;
789 case TYPE_OCTET_STRING:
790 *tagValue=ASN1_TAG_OCTET_STRING;break;
791 case TYPE_GENERALSTRING:
792 *tagValue=ASN1_TAG_GENERALSTRING;break;
793 case TYPE_BIT_STRING:
794 *tagValue=ASN1_TAG_BIT_STRING;break;
795 case TYPE_SEQUENCE: case TYPE_SEQUENCE_OF:
796 *tagValue=ASN1_TAG_SEQUENCE;break;
797 case TYPE_SET: case TYPE_SET_OF:
798 *tagValue=ASN1_TAG_SET;break;
799 case TYPE_TAG:
800 case TYPE_CHOICE:
801 case TYPE_ANY:
802 break;
803 default:
804 break;
809 return ASN1_SUCCESS;