fixes in length calculation in _asn1_extract_der_octet().
[platform/upstream/libtasn1.git] / lib / element.c
1 /*
2  * Copyright (C) 2000-2014 Free Software Foundation, Inc.
3  *
4  * This file is part of LIBTASN1.
5  *
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.
10  *
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.
15  *
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
19  * 02110-1301, USA
20  */
21
22 /*****************************************************/
23 /* File: element.c                                   */
24 /* Description: Functions with the read and write    */
25 /*   functions.                                      */
26 /*****************************************************/
27
28
29 #include <int.h>
30 #include "parser_aux.h"
31 #include <gstr.h>
32 #include "structure.h"
33
34 #include "element.h"
35
36 void
37 _asn1_hierarchical_name (asn1_node node, char *name, int name_size)
38 {
39   asn1_node p;
40   char tmp_name[64];
41
42   p = node;
43
44   name[0] = 0;
45
46   while (p != NULL)
47     {
48       if (p->name[0] != 0)
49         {
50           _asn1_str_cpy (tmp_name, sizeof (tmp_name), name),
51             _asn1_str_cpy (name, name_size, p->name);
52           _asn1_str_cat (name, name_size, ".");
53           _asn1_str_cat (name, name_size, tmp_name);
54         }
55       p = _asn1_find_up (p);
56     }
57
58   if (name[0] == 0)
59     _asn1_str_cpy (name, name_size, "ROOT");
60 }
61
62
63 /******************************************************************/
64 /* Function : _asn1_convert_integer                               */
65 /* Description: converts an integer from a null terminated string */
66 /*              to der decoding. The convertion from a null       */
67 /*              terminated string to an integer is made with      */
68 /*              the 'strtol' function.                            */
69 /* Parameters:                                                    */
70 /*   value: null terminated string to convert.                    */
71 /*   value_out: convertion result (memory must be already         */
72 /*              allocated).                                       */
73 /*   value_out_size: number of bytes of value_out.                */
74 /*   len: number of significant byte of value_out.                */
75 /* Return: ASN1_MEM_ERROR or ASN1_SUCCESS                         */
76 /******************************************************************/
77 int
78 _asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
79                        int value_out_size, int *len)
80 {
81   char negative;
82   unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
83   long valtmp;
84   int k, k2;
85
86   valtmp = _asn1_strtol (value, NULL, 10);
87
88   for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
89     {
90       val[SIZEOF_UNSIGNED_LONG_INT - k - 1] = (valtmp >> (8 * k)) & 0xFF;
91     }
92
93   if (val[0] & 0x80)
94     negative = 1;
95   else
96     negative = 0;
97
98   for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT - 1; k++)
99     {
100       if (negative && (val[k] != 0xFF))
101         break;
102       else if (!negative && val[k])
103         break;
104     }
105
106   if ((negative && !(val[k] & 0x80)) || (!negative && (val[k] & 0x80)))
107     k--;
108
109   *len = SIZEOF_UNSIGNED_LONG_INT - k;
110
111   if (SIZEOF_UNSIGNED_LONG_INT - k > value_out_size)
112     /* VALUE_OUT is too short to contain the value conversion */
113     return ASN1_MEM_ERROR;
114
115   if (value_out != NULL)
116     {
117       for (k2 = k; k2 < SIZEOF_UNSIGNED_LONG_INT; k2++)
118         value_out[k2 - k] = val[k2];
119     }
120
121 #if 0
122   printf ("_asn1_convert_integer: valueIn=%s, lenOut=%d", value, *len);
123   for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
124     printf (", vOut[%d]=%d", k, value_out[k]);
125   printf ("\n");
126 #endif
127
128   return ASN1_SUCCESS;
129 }
130
131
132 int
133 _asn1_append_sequence_set (asn1_node node)
134 {
135   asn1_node p, p2;
136   char temp[LTOSTR_MAX_SIZE];
137   long n;
138
139   if (!node || !(node->down))
140     return ASN1_GENERIC_ERROR;
141
142   p = node->down;
143   while ((type_field (p->type) == ASN1_ETYPE_TAG)
144          || (type_field (p->type) == ASN1_ETYPE_SIZE))
145     p = p->right;
146   p2 = _asn1_copy_structure3 (p);
147   while (p->right)
148     p = p->right;
149   _asn1_set_right (p, p2);
150
151   if (p->name[0] == 0)
152     _asn1_str_cpy (temp, sizeof (temp), "?1");
153   else
154     {
155       n = strtol (p->name + 1, NULL, 0);
156       n++;
157       temp[0] = '?';
158       _asn1_ltostr (n, temp + 1);
159     }
160   _asn1_set_name (p2, temp);
161   /*  p2->type |= CONST_OPTION; */
162
163   return ASN1_SUCCESS;
164 }
165
166
167 /**
168  * asn1_write_value:
169  * @node_root: pointer to a structure
170  * @name: the name of the element inside the structure that you want to set.
171  * @ivalue: vector used to specify the value to set. If len is >0,
172  *   VALUE must be a two's complement form integer.  if len=0 *VALUE
173  *   must be a null terminated string with an integer value.
174  * @len: number of bytes of *value to use to set the value:
175  *   value[0]..value[len-1] or 0 if value is a null terminated string
176  *
177  * Set the value of one element inside a structure.
178  *
179  * If an element is OPTIONAL and you want to delete it, you must use
180  * the value=NULL and len=0.  Using "pkix.asn":
181  *
182  * result=asn1_write_value(cert, "tbsCertificate.issuerUniqueID",
183  * NULL, 0);
184  *
185  * Description for each type:
186  *
187  * INTEGER: VALUE must contain a two's complement form integer.
188  *
189  *            value[0]=0xFF ,               len=1 -> integer=-1.
190  *            value[0]=0xFF value[1]=0xFF , len=2 -> integer=-1.
191  *            value[0]=0x01 ,               len=1 -> integer= 1.
192  *            value[0]=0x00 value[1]=0x01 , len=2 -> integer= 1.
193  *            value="123"                 , len=0 -> integer= 123.
194  *
195  * ENUMERATED: As INTEGER (but only with not negative numbers).
196  *
197  * BOOLEAN: VALUE must be the null terminated string "TRUE" or
198  *   "FALSE" and LEN != 0.
199  *
200  *            value="TRUE" , len=1 -> boolean=TRUE.
201  *            value="FALSE" , len=1 -> boolean=FALSE.
202  *
203  * OBJECT IDENTIFIER: VALUE must be a null terminated string with
204  *   each number separated by a dot (e.g. "1.2.3.543.1").  LEN != 0.
205  *
206  *            value="1 2 840 10040 4 3" , len=1 -> OID=dsa-with-sha.
207  *
208  * UTCTime: VALUE must be a null terminated string in one of these
209  *   formats: "YYMMDDhhmmssZ", "YYMMDDhhmmssZ",
210  *   "YYMMDDhhmmss+hh'mm'", "YYMMDDhhmmss-hh'mm'",
211  *   "YYMMDDhhmm+hh'mm'", or "YYMMDDhhmm-hh'mm'".  LEN != 0.
212  *
213  *            value="9801011200Z" , len=1 -> time=Jannuary 1st, 1998
214  *            at 12h 00m Greenwich Mean Time
215  *
216  * GeneralizedTime: VALUE must be in one of this format:
217  *   "YYYYMMDDhhmmss.sZ", "YYYYMMDDhhmmss.sZ",
218  *   "YYYYMMDDhhmmss.s+hh'mm'", "YYYYMMDDhhmmss.s-hh'mm'",
219  *   "YYYYMMDDhhmm+hh'mm'", or "YYYYMMDDhhmm-hh'mm'" where ss.s
220  *   indicates the seconds with any precision like "10.1" or "01.02".
221  *   LEN != 0
222  *
223  *            value="2001010112001.12-0700" , len=1 -> time=Jannuary
224  *            1st, 2001 at 12h 00m 01.12s Pacific Daylight Time
225  *
226  * OCTET STRING: VALUE contains the octet string and LEN is the
227  *   number of octets.
228  *
229  *            value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
230  *            len=3 -> three bytes octet string
231  *
232  * GeneralString: VALUE contains the generalstring and LEN is the
233  *   number of octets.
234  *
235  *            value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
236  *            len=3 -> three bytes generalstring
237  *
238  * BIT STRING: VALUE contains the bit string organized by bytes and
239  *   LEN is the number of bits.
240  *
241  *   value="$\backslash$xCF" , len=6 -> bit string="110011" (six
242  *   bits)
243  *
244  * CHOICE: if NAME indicates a choice type, VALUE must specify one of
245  *   the alternatives with a null terminated string. LEN != 0. Using
246  *   "pkix.asn"\:
247  *
248  *           result=asn1_write_value(cert,
249  *           "certificate1.tbsCertificate.subject", "rdnSequence",
250  *           1);
251  *
252  * ANY: VALUE indicates the der encoding of a structure.  LEN != 0.
253  *
254  * SEQUENCE OF: VALUE must be the null terminated string "NEW" and
255  *   LEN != 0. With this instruction another element is appended in
256  *   the sequence. The name of this element will be "?1" if it's the
257  *   first one, "?2" for the second and so on.
258  *
259  *   Using "pkix.asn"\:
260  *
261  *   result=asn1_write_value(cert,
262  *   "certificate1.tbsCertificate.subject.rdnSequence", "NEW", 1);
263  *
264  * SET OF: the same as SEQUENCE OF.  Using "pkix.asn":
265  *
266  *           result=asn1_write_value(cert,
267  *           "tbsCertificate.subject.rdnSequence.?LAST", "NEW", 1);
268  *
269  * Returns: %ASN1_SUCCESS if the value was set,
270  *   %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element, and
271  *   %ASN1_VALUE_NOT_VALID if @ivalue has a wrong format.
272  **/
273 int
274 asn1_write_value (asn1_node node_root, const char *name,
275                   const void *ivalue, int len)
276 {
277   asn1_node node, p, p2;
278   unsigned char *temp, *value_temp = NULL, *default_temp = NULL;
279   int len2, k, k2, negative;
280   size_t i;
281   const unsigned char *value = ivalue;
282   unsigned int type;
283
284   node = asn1_find_node (node_root, name);
285   if (node == NULL)
286     return ASN1_ELEMENT_NOT_FOUND;
287
288   if ((node->type & CONST_OPTION) && (value == NULL) && (len == 0))
289     {
290       asn1_delete_structure (&node);
291       return ASN1_SUCCESS;
292     }
293
294   type = type_field (node->type);
295
296   if ((type == ASN1_ETYPE_SEQUENCE_OF || type == ASN1_ETYPE_SET_OF) && (value == NULL) && (len == 0))
297     {
298       p = node->down;
299       while ((type_field (p->type) == ASN1_ETYPE_TAG)
300              || (type_field (p->type) == ASN1_ETYPE_SIZE))
301         p = p->right;
302
303       while (p->right)
304         asn1_delete_structure (&p->right);
305
306       return ASN1_SUCCESS;
307     }
308
309   /* Don't allow element deletion for other types */
310   if (value == NULL)
311     {
312       return ASN1_VALUE_NOT_VALID;
313     }
314
315   switch (type)
316     {
317     case ASN1_ETYPE_BOOLEAN:
318       if (!_asn1_strcmp (value, "TRUE"))
319         {
320           if (node->type & CONST_DEFAULT)
321             {
322               p = node->down;
323               while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
324                 p = p->right;
325               if (p->type & CONST_TRUE)
326                 _asn1_set_value (node, NULL, 0);
327               else
328                 _asn1_set_value (node, "T", 1);
329             }
330           else
331             _asn1_set_value (node, "T", 1);
332         }
333       else if (!_asn1_strcmp (value, "FALSE"))
334         {
335           if (node->type & CONST_DEFAULT)
336             {
337               p = node->down;
338               while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
339                 p = p->right;
340               if (p->type & CONST_FALSE)
341                 _asn1_set_value (node, NULL, 0);
342               else
343                 _asn1_set_value (node, "F", 1);
344             }
345           else
346             _asn1_set_value (node, "F", 1);
347         }
348       else
349         return ASN1_VALUE_NOT_VALID;
350       break;
351     case ASN1_ETYPE_INTEGER:
352     case ASN1_ETYPE_ENUMERATED:
353       if (len == 0)
354         {
355           if ((isdigit (value[0])) || (value[0] == '-'))
356             {
357               value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
358               if (value_temp == NULL)
359                 return ASN1_MEM_ALLOC_ERROR;
360
361               _asn1_convert_integer (value, value_temp,
362                                      SIZEOF_UNSIGNED_LONG_INT, &len);
363             }
364           else
365             {                   /* is an identifier like v1 */
366               if (!(node->type & CONST_LIST))
367                 return ASN1_VALUE_NOT_VALID;
368               p = node->down;
369               while (p)
370                 {
371                   if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
372                     {
373                       if (!_asn1_strcmp (p->name, value))
374                         {
375                           value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
376                           if (value_temp == NULL)
377                             return ASN1_MEM_ALLOC_ERROR;
378
379                           _asn1_convert_integer (p->value,
380                                                  value_temp,
381                                                  SIZEOF_UNSIGNED_LONG_INT,
382                                                  &len);
383                           break;
384                         }
385                     }
386                   p = p->right;
387                 }
388               if (p == NULL)
389                 return ASN1_VALUE_NOT_VALID;
390             }
391         }
392       else
393         {                       /* len != 0 */
394           value_temp = malloc (len);
395           if (value_temp == NULL)
396             return ASN1_MEM_ALLOC_ERROR;
397           memcpy (value_temp, value, len);
398         }
399
400       if (value_temp[0] & 0x80)
401         negative = 1;
402       else
403         negative = 0;
404
405       if (negative && (type_field (node->type) == ASN1_ETYPE_ENUMERATED))
406         {
407           free (value_temp);
408           return ASN1_VALUE_NOT_VALID;
409         }
410
411       for (k = 0; k < len - 1; k++)
412         if (negative && (value_temp[k] != 0xFF))
413           break;
414         else if (!negative && value_temp[k])
415           break;
416
417       if ((negative && !(value_temp[k] & 0x80)) ||
418           (!negative && (value_temp[k] & 0x80)))
419         k--;
420
421       _asn1_set_value_lv (node, value_temp + k, len - k);
422
423       if (node->type & CONST_DEFAULT)
424         {
425           p = node->down;
426           while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
427             p = p->right;
428           if ((isdigit (p->value[0])) || (p->value[0] == '-'))
429             {
430               default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
431               if (default_temp == NULL)
432                 {
433                   free (value_temp);
434                   return ASN1_MEM_ALLOC_ERROR;
435                 }
436
437               _asn1_convert_integer (p->value, default_temp,
438                                      SIZEOF_UNSIGNED_LONG_INT, &len2);
439             }
440           else
441             {                   /* is an identifier like v1 */
442               if (!(node->type & CONST_LIST))
443                 {
444                   free (value_temp);
445                   return ASN1_VALUE_NOT_VALID;
446                 }
447               p2 = node->down;
448               while (p2)
449                 {
450                   if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
451                     {
452                       if (!_asn1_strcmp (p2->name, p->value))
453                         {
454                           default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
455                           if (default_temp == NULL)
456                             {
457                               free (value_temp);
458                               return ASN1_MEM_ALLOC_ERROR;
459                             }
460
461                           _asn1_convert_integer (p2->value,
462                                                  default_temp,
463                                                  SIZEOF_UNSIGNED_LONG_INT,
464                                                  &len2);
465                           break;
466                         }
467                     }
468                   p2 = p2->right;
469                 }
470               if (p2 == NULL)
471                 {
472                   free (value_temp);
473                   return ASN1_VALUE_NOT_VALID;
474                 }
475             }
476
477
478           if ((len - k) == len2)
479             {
480               for (k2 = 0; k2 < len2; k2++)
481                 if (value_temp[k + k2] != default_temp[k2])
482                   {
483                     break;
484                   }
485               if (k2 == len2)
486                 _asn1_set_value (node, NULL, 0);
487             }
488           free (default_temp);
489         }
490       free (value_temp);
491       break;
492     case ASN1_ETYPE_OBJECT_ID:
493       for (i = 0; i < _asn1_strlen (value); i++)
494         if ((!isdigit (value[i])) && (value[i] != '.') && (value[i] != '+'))
495           return ASN1_VALUE_NOT_VALID;
496       if (node->type & CONST_DEFAULT)
497         {
498           p = node->down;
499           while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
500             p = p->right;
501           if (!_asn1_strcmp (value, p->value))
502             {
503               _asn1_set_value (node, NULL, 0);
504               break;
505             }
506         }
507       _asn1_set_value (node, value, _asn1_strlen (value) + 1);
508       break;
509     case ASN1_ETYPE_UTC_TIME:
510       {
511         len = _asn1_strlen (value);
512         if (len < 11)
513           return ASN1_VALUE_NOT_VALID;
514         for (k = 0; k < 10; k++)
515           if (!isdigit (value[k]))
516             return ASN1_VALUE_NOT_VALID;
517         switch (len)
518           {
519           case 11:
520             if (value[10] != 'Z')
521               return ASN1_VALUE_NOT_VALID;
522             break;
523           case 13:
524             if ((!isdigit (value[10])) || (!isdigit (value[11])) ||
525                 (value[12] != 'Z'))
526               return ASN1_VALUE_NOT_VALID;
527             break;
528           case 15:
529             if ((value[10] != '+') && (value[10] != '-'))
530               return ASN1_VALUE_NOT_VALID;
531             for (k = 11; k < 15; k++)
532               if (!isdigit (value[k]))
533                 return ASN1_VALUE_NOT_VALID;
534             break;
535           case 17:
536             if ((!isdigit (value[10])) || (!isdigit (value[11])))
537               return ASN1_VALUE_NOT_VALID;
538             if ((value[12] != '+') && (value[12] != '-'))
539               return ASN1_VALUE_NOT_VALID;
540             for (k = 13; k < 17; k++)
541               if (!isdigit (value[k]))
542                 return ASN1_VALUE_NOT_VALID;
543             break;
544           default:
545             return ASN1_VALUE_NOT_FOUND;
546           }
547         _asn1_set_value (node, value, len);
548       }
549       break;
550     case ASN1_ETYPE_GENERALIZED_TIME:
551       len = _asn1_strlen (value);
552       _asn1_set_value (node, value, len);
553       break;
554     case ASN1_ETYPE_OCTET_STRING:
555     case ASN1_ETYPE_GENERALSTRING:
556     case ASN1_ETYPE_NUMERIC_STRING:
557     case ASN1_ETYPE_IA5_STRING:
558     case ASN1_ETYPE_TELETEX_STRING:
559     case ASN1_ETYPE_PRINTABLE_STRING:
560     case ASN1_ETYPE_UNIVERSAL_STRING:
561     case ASN1_ETYPE_BMP_STRING:
562     case ASN1_ETYPE_UTF8_STRING:
563     case ASN1_ETYPE_VISIBLE_STRING:
564       if (len == 0)
565         len = _asn1_strlen (value);
566       _asn1_set_value_lv (node, value, len);
567       break;
568     case ASN1_ETYPE_BIT_STRING:
569       if (len == 0)
570         len = _asn1_strlen (value);
571       asn1_length_der ((len >> 3) + 2, NULL, &len2);
572       temp = malloc ((len >> 3) + 2 + len2);
573       if (temp == NULL)
574         return ASN1_MEM_ALLOC_ERROR;
575
576       asn1_bit_der (value, len, temp, &len2);
577       _asn1_set_value_m (node, temp, len2);
578       temp = NULL;
579       break;
580     case ASN1_ETYPE_CHOICE:
581       p = node->down;
582       while (p)
583         {
584           if (!_asn1_strcmp (p->name, value))
585             {
586               p2 = node->down;
587               while (p2)
588                 {
589                   if (p2 != p)
590                     {
591                       asn1_delete_structure (&p2);
592                       p2 = node->down;
593                     }
594                   else
595                     p2 = p2->right;
596                 }
597               break;
598             }
599           p = p->right;
600         }
601       if (!p)
602         return ASN1_ELEMENT_NOT_FOUND;
603       break;
604     case ASN1_ETYPE_ANY:
605       _asn1_set_value_lv (node, value, len);
606       break;
607     case ASN1_ETYPE_SEQUENCE_OF:
608     case ASN1_ETYPE_SET_OF:
609       if (_asn1_strcmp (value, "NEW"))
610         return ASN1_VALUE_NOT_VALID;
611       _asn1_append_sequence_set (node);
612       break;
613     default:
614       return ASN1_ELEMENT_NOT_FOUND;
615       break;
616     }
617
618   return ASN1_SUCCESS;
619 }
620
621
622 #define PUT_VALUE( ptr, ptr_size, data, data_size) \
623         *len = data_size; \
624         if (ptr_size < data_size) { \
625                 return ASN1_MEM_ERROR; \
626         } else { \
627                 if (ptr && data_size > 0) \
628                   memcpy (ptr, data, data_size); \
629         }
630
631 #define PUT_STR_VALUE( ptr, ptr_size, data) \
632         *len = _asn1_strlen (data) + 1; \
633         if (ptr_size < *len) { \
634                 return ASN1_MEM_ERROR; \
635         } else { \
636                 /* this strcpy is checked */ \
637                 if (ptr) { \
638                   _asn1_strcpy (ptr, data); \
639                 } \
640         }
641
642 #define PUT_AS_STR_VALUE( ptr, ptr_size, data, data_size) \
643         *len = data_size + 1; \
644         if (ptr_size < *len) { \
645                 return ASN1_MEM_ERROR; \
646         } else { \
647                 /* this strcpy is checked */ \
648                 if (ptr) { \
649                   if (data_size > 0) \
650                     memcpy (ptr, data, data_size); \
651                   ptr[data_size] = 0; \
652                 } \
653         }
654
655 #define ADD_STR_VALUE( ptr, ptr_size, data) \
656         *len += _asn1_strlen(data); \
657         if (ptr_size < (int) *len) { \
658                 (*len)++; \
659                 return ASN1_MEM_ERROR; \
660         } else { \
661                 /* this strcat is checked */ \
662                 if (ptr) _asn1_strcat (ptr, data); \
663         }
664
665 /**
666  * asn1_read_value:
667  * @root: pointer to a structure.
668  * @name: the name of the element inside a structure that you want to read.
669  * @ivalue: vector that will contain the element's content, must be a
670  *   pointer to memory cells already allocated (may be %NULL).
671  * @len: number of bytes of *value: value[0]..value[len-1]. Initialy
672  *   holds the sizeof value.
673  *
674  * Returns the value of one element inside a structure. 
675  * If an element is OPTIONAL and this returns
676  * %ASN1_ELEMENT_NOT_FOUND, it means that this element wasn't present
677  * in the der encoding that created the structure.  The first element
678  * of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and
679  * so on.
680  *
681  * Note that there can be valid values with length zero. In these case
682  * this function will succeed and @len will be zero.
683  *
684  * INTEGER: VALUE will contain a two's complement form integer.
685  *
686  *            integer=-1  -> value[0]=0xFF , len=1.
687  *            integer=1   -> value[0]=0x01 , len=1.
688  *
689  * ENUMERATED: As INTEGER (but only with not negative numbers).
690  *
691  * BOOLEAN: VALUE will be the null terminated string "TRUE" or
692  *   "FALSE" and LEN=5 or LEN=6.
693  *
694  * OBJECT IDENTIFIER: VALUE will be a null terminated string with
695  *   each number separated by a dot (i.e. "1.2.3.543.1").
696  *
697  *                      LEN = strlen(VALUE)+1
698  *
699  * UTCTime: VALUE will be a null terminated string in one of these
700  *   formats: "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'".
701  *   LEN=strlen(VALUE)+1.
702  *
703  * GeneralizedTime: VALUE will be a null terminated string in the
704  *   same format used to set the value.
705  *
706  * OCTET STRING: VALUE will contain the octet string and LEN will be
707  *   the number of octets.
708  *
709  * GeneralString: VALUE will contain the generalstring and LEN will
710  *   be the number of octets.
711  *
712  * BIT STRING: VALUE will contain the bit string organized by bytes
713  *   and LEN will be the number of bits.
714  *
715  * CHOICE: If NAME indicates a choice type, VALUE will specify the
716  *   alternative selected.
717  *
718  * ANY: If NAME indicates an any type, VALUE will indicate the DER
719  *   encoding of the structure actually used.
720  *
721  * Returns: %ASN1_SUCCESS if value is returned,
722  *   %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element,
723  *   %ASN1_VALUE_NOT_FOUND if there isn't any value for the element
724  *   selected, and %ASN1_MEM_ERROR if The value vector isn't big enough
725  *   to store the result, and in this case @len will contain the number of
726  *   bytes needed.
727  **/
728 int
729 asn1_read_value (asn1_node root, const char *name, void *ivalue, int *len)
730 {
731   return asn1_read_value_type (root, name, ivalue, len, NULL);
732 }
733
734 /**
735  * asn1_read_value_type:
736  * @root: pointer to a structure.
737  * @name: the name of the element inside a structure that you want to read.
738  * @ivalue: vector that will contain the element's content, must be a
739  *   pointer to memory cells already allocated (may be %NULL).
740  * @len: number of bytes of *value: value[0]..value[len-1]. Initialy
741  *   holds the sizeof value.
742  * @etype: The type of the value read (ASN1_ETYPE)
743  *
744  * Returns the value of one element inside a structure. 
745  * If an element is OPTIONAL and this returns
746  * %ASN1_ELEMENT_NOT_FOUND, it means that this element wasn't present
747  * in the der encoding that created the structure.  The first element
748  * of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and
749  * so on.
750  *
751  * Note that there can be valid values with length zero. In these case
752  * this function will succeed and @len will be zero.
753  *
754  *
755  * INTEGER: VALUE will contain a two's complement form integer.
756  *
757  *            integer=-1  -> value[0]=0xFF , len=1.
758  *            integer=1   -> value[0]=0x01 , len=1.
759  *
760  * ENUMERATED: As INTEGER (but only with not negative numbers).
761  *
762  * BOOLEAN: VALUE will be the null terminated string "TRUE" or
763  *   "FALSE" and LEN=5 or LEN=6.
764  *
765  * OBJECT IDENTIFIER: VALUE will be a null terminated string with
766  *   each number separated by a dot (i.e. "1.2.3.543.1").
767  *
768  *                      LEN = strlen(VALUE)+1
769  *
770  * UTCTime: VALUE will be a null terminated string in one of these
771  *   formats: "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'".
772  *   LEN=strlen(VALUE)+1.
773  *
774  * GeneralizedTime: VALUE will be a null terminated string in the
775  *   same format used to set the value.
776  *
777  * OCTET STRING: VALUE will contain the octet string and LEN will be
778  *   the number of octets.
779  *
780  * GeneralString: VALUE will contain the generalstring and LEN will
781  *   be the number of octets.
782  *
783  * BIT STRING: VALUE will contain the bit string organized by bytes
784  *   and LEN will be the number of bits.
785  *
786  * CHOICE: If NAME indicates a choice type, VALUE will specify the
787  *   alternative selected.
788  *
789  * ANY: If NAME indicates an any type, VALUE will indicate the DER
790  *   encoding of the structure actually used.
791  *
792  * Returns: %ASN1_SUCCESS if value is returned,
793  *   %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element,
794  *   %ASN1_VALUE_NOT_FOUND if there isn't any value for the element
795  *   selected, and %ASN1_MEM_ERROR if The value vector isn't big enough
796  *   to store the result, and in this case @len will contain the number of
797  *   bytes needed.
798  **/
799 int
800 asn1_read_value_type (asn1_node root, const char *name, void *ivalue,
801                       int *len, unsigned int *etype)
802 {
803   asn1_node node, p, p2;
804   int len2, len3, result;
805   int value_size = *len;
806   unsigned char *value = ivalue;
807   unsigned type;
808
809   node = asn1_find_node (root, name);
810   if (node == NULL)
811     return ASN1_ELEMENT_NOT_FOUND;
812
813   type = type_field (node->type);
814
815   if ((type != ASN1_ETYPE_NULL) &&
816       (type != ASN1_ETYPE_CHOICE) &&
817       !(node->type & CONST_DEFAULT) && !(node->type & CONST_ASSIGN) &&
818       (node->value == NULL))
819     return ASN1_VALUE_NOT_FOUND;
820
821   if (etype)
822     *etype = type;
823   switch (type)
824     {
825     case ASN1_ETYPE_NULL:
826       PUT_STR_VALUE (value, value_size, "NULL");
827       break;
828     case ASN1_ETYPE_BOOLEAN:
829       if ((node->type & CONST_DEFAULT) && (node->value == NULL))
830         {
831           p = node->down;
832           while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
833             p = p->right;
834           if (p->type & CONST_TRUE)
835             {
836               PUT_STR_VALUE (value, value_size, "TRUE");
837             }
838           else
839             {
840               PUT_STR_VALUE (value, value_size, "FALSE");
841             }
842         }
843       else if (node->value[0] == 'T')
844         {
845           PUT_STR_VALUE (value, value_size, "TRUE");
846         }
847       else
848         {
849           PUT_STR_VALUE (value, value_size, "FALSE");
850         }
851       break;
852     case ASN1_ETYPE_INTEGER:
853     case ASN1_ETYPE_ENUMERATED:
854       if ((node->type & CONST_DEFAULT) && (node->value == NULL))
855         {
856           p = node->down;
857           while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
858             p = p->right;
859           if ((isdigit (p->value[0])) || (p->value[0] == '-')
860               || (p->value[0] == '+'))
861             {
862               result = _asn1_convert_integer
863                   (p->value, value, value_size, len);
864               if (result != ASN1_SUCCESS)
865                 return result;
866             }
867           else
868             {                   /* is an identifier like v1 */
869               p2 = node->down;
870               while (p2)
871                 {
872                   if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
873                     {
874                       if (!_asn1_strcmp (p2->name, p->value))
875                         {
876                           result = _asn1_convert_integer
877                               (p2->value, value, value_size,
878                                len);
879                           if (result != ASN1_SUCCESS)
880                             return result;
881                           break;
882                         }
883                     }
884                   p2 = p2->right;
885                 }
886             }
887         }
888       else
889         {
890           len2 = -1;
891           result = asn1_get_octet_der
892               (node->value, node->value_len, &len2, value, value_size,
893                len);
894           if (result != ASN1_SUCCESS)
895             return result;
896         }
897       break;
898     case ASN1_ETYPE_OBJECT_ID:
899       if (node->type & CONST_ASSIGN)
900         {
901           *len = 0;
902           if (value)
903                 value[0] = 0;
904           p = node->down;
905           while (p)
906             {
907               if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
908                 {
909                   ADD_STR_VALUE (value, value_size, p->value);
910                   if (p->right)
911                     {
912                       ADD_STR_VALUE (value, value_size, ".");
913                     }
914                 }
915               p = p->right;
916             }
917           (*len)++;
918         }
919       else if ((node->type & CONST_DEFAULT) && (node->value == NULL))
920         {
921           p = node->down;
922           while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
923             p = p->right;
924           PUT_STR_VALUE (value, value_size, p->value);
925         }
926       else
927         {
928           PUT_STR_VALUE (value, value_size, node->value);
929         }
930       break;
931     case ASN1_ETYPE_GENERALIZED_TIME:
932     case ASN1_ETYPE_UTC_TIME:
933       PUT_AS_STR_VALUE (value, value_size, node->value, node->value_len);
934       break;
935     case ASN1_ETYPE_OCTET_STRING:
936     case ASN1_ETYPE_GENERALSTRING:
937     case ASN1_ETYPE_NUMERIC_STRING:
938     case ASN1_ETYPE_IA5_STRING:
939     case ASN1_ETYPE_TELETEX_STRING:
940     case ASN1_ETYPE_PRINTABLE_STRING:
941     case ASN1_ETYPE_UNIVERSAL_STRING:
942     case ASN1_ETYPE_BMP_STRING:
943     case ASN1_ETYPE_UTF8_STRING:
944     case ASN1_ETYPE_VISIBLE_STRING:
945       len2 = -1;
946       result = asn1_get_octet_der
947           (node->value, node->value_len, &len2, value, value_size,
948            len);
949       if (result != ASN1_SUCCESS)
950         return result;
951       break;
952     case ASN1_ETYPE_BIT_STRING:
953       len2 = -1;
954       result = asn1_get_bit_der
955           (node->value, node->value_len, &len2, value, value_size,
956            len);
957       if (result != ASN1_SUCCESS)
958         return result;
959       break;
960     case ASN1_ETYPE_CHOICE:
961       PUT_STR_VALUE (value, value_size, node->down->name);
962       break;
963     case ASN1_ETYPE_ANY:
964       len3 = -1;
965       len2 = asn1_get_length_der (node->value, node->value_len, &len3);
966       if (len2 < 0)
967         return ASN1_DER_ERROR;
968       PUT_VALUE (value, value_size, node->value + len3, len2);
969       break;
970     default:
971       return ASN1_ELEMENT_NOT_FOUND;
972       break;
973     }
974   return ASN1_SUCCESS;
975 }
976
977
978 /**
979  * asn1_read_tag:
980  * @root: pointer to a structure
981  * @name: the name of the element inside a structure.
982  * @tagValue:  variable that will contain the TAG value.
983  * @classValue: variable that will specify the TAG type.
984  *
985  * Returns the TAG and the CLASS of one element inside a structure.
986  * CLASS can have one of these constants: %ASN1_CLASS_APPLICATION,
987  * %ASN1_CLASS_UNIVERSAL, %ASN1_CLASS_PRIVATE or
988  * %ASN1_CLASS_CONTEXT_SPECIFIC.
989  *
990  * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
991  *   @name is not a valid element.
992  **/
993 int
994 asn1_read_tag (asn1_node root, const char *name, int *tagValue,
995                int *classValue)
996 {
997   asn1_node node, p, pTag;
998
999   node = asn1_find_node (root, name);
1000   if (node == NULL)
1001     return ASN1_ELEMENT_NOT_FOUND;
1002
1003   p = node->down;
1004
1005   /* pTag will points to the IMPLICIT TAG */
1006   pTag = NULL;
1007   if (node->type & CONST_TAG)
1008     {
1009       while (p)
1010         {
1011           if (type_field (p->type) == ASN1_ETYPE_TAG)
1012             {
1013               if ((p->type & CONST_IMPLICIT) && (pTag == NULL))
1014                 pTag = p;
1015               else if (p->type & CONST_EXPLICIT)
1016                 pTag = NULL;
1017             }
1018           p = p->right;
1019         }
1020     }
1021
1022   if (pTag)
1023     {
1024       *tagValue = _asn1_strtoul (pTag->value, NULL, 10);
1025
1026       if (pTag->type & CONST_APPLICATION)
1027         *classValue = ASN1_CLASS_APPLICATION;
1028       else if (pTag->type & CONST_UNIVERSAL)
1029         *classValue = ASN1_CLASS_UNIVERSAL;
1030       else if (pTag->type & CONST_PRIVATE)
1031         *classValue = ASN1_CLASS_PRIVATE;
1032       else
1033         *classValue = ASN1_CLASS_CONTEXT_SPECIFIC;
1034     }
1035   else
1036     {
1037       unsigned type = type_field (node->type);
1038       *classValue = ASN1_CLASS_UNIVERSAL;
1039
1040       switch (type)
1041         {
1042         CASE_HANDLED_ETYPES:
1043           *tagValue = _asn1_tags[type].tag;
1044           break;
1045         case ASN1_ETYPE_TAG:
1046         case ASN1_ETYPE_CHOICE:
1047         case ASN1_ETYPE_ANY:
1048           *tagValue = -1;
1049           break;
1050         default:
1051           break;
1052         }
1053     }
1054
1055   return ASN1_SUCCESS;
1056 }
1057
1058 /**
1059  * asn1_read_node_value:
1060  * @node: pointer to a node.
1061  * @data: a point to a asn1_data_node_st
1062  *
1063  * Returns the value a data node inside a asn1_node structure.
1064  * The data returned should be handled as constant values.
1065  *
1066  * Returns: %ASN1_SUCCESS if the node exists.
1067  **/
1068 int
1069 asn1_read_node_value (asn1_node node, asn1_data_node_st * data)
1070 {
1071   data->name = node->name;
1072   data->value = node->value;
1073   data->value_len = node->value_len;
1074   data->type = type_field (node->type);
1075
1076   return ASN1_SUCCESS;
1077 }