2 * Copyright (C) 2000-2014 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
22 /*****************************************************/
24 /* Description: Functions with the read and write */
26 /*****************************************************/
30 #include "parser_aux.h"
32 #include "structure.h"
37 _asn1_hierarchical_name (asn1_node node, char *name, int name_size)
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);
55 p = _asn1_find_up (p);
59 _asn1_str_cpy (name, name_size, "ROOT");
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. */
70 /* value: null terminated string to convert. */
71 /* value_out: convertion result (memory must be already */
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 /******************************************************************/
78 _asn1_convert_integer (const unsigned char *value, unsigned char *value_out,
79 int value_out_size, int *len)
82 unsigned char val[SIZEOF_UNSIGNED_LONG_INT];
86 valtmp = _asn1_strtol (value, NULL, 10);
88 for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT; k++)
90 val[SIZEOF_UNSIGNED_LONG_INT - k - 1] = (valtmp >> (8 * k)) & 0xFF;
98 for (k = 0; k < SIZEOF_UNSIGNED_LONG_INT - 1; k++)
100 if (negative && (val[k] != 0xFF))
102 else if (!negative && val[k])
106 if ((negative && !(val[k] & 0x80)) || (!negative && (val[k] & 0x80)))
109 *len = SIZEOF_UNSIGNED_LONG_INT - k;
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;
115 if (value_out != NULL)
117 for (k2 = k; k2 < SIZEOF_UNSIGNED_LONG_INT; k2++)
118 value_out[k2 - k] = val[k2];
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]);
133 _asn1_append_sequence_set (asn1_node node)
136 char temp[LTOSTR_MAX_SIZE];
139 if (!node || !(node->down))
140 return ASN1_GENERIC_ERROR;
143 while ((type_field (p->type) == ASN1_ETYPE_TAG)
144 || (type_field (p->type) == ASN1_ETYPE_SIZE))
146 p2 = _asn1_copy_structure3 (p);
149 _asn1_set_right (p, p2);
152 _asn1_str_cpy (temp, sizeof (temp), "?1");
155 n = strtol (p->name + 1, NULL, 0);
158 _asn1_ltostr (n, temp + 1);
160 _asn1_set_name (p2, temp);
161 /* p2->type |= CONST_OPTION; */
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
177 * Set the value of one element inside a structure.
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":
182 * result=asn1_write_value(cert, "tbsCertificate.issuerUniqueID",
185 * Description for each type:
187 * INTEGER: VALUE must contain a two's complement form integer.
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.
195 * ENUMERATED: As INTEGER (but only with not negative numbers).
197 * BOOLEAN: VALUE must be the null terminated string "TRUE" or
198 * "FALSE" and LEN != 0.
200 * value="TRUE" , len=1 -> boolean=TRUE.
201 * value="FALSE" , len=1 -> boolean=FALSE.
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.
206 * value="1 2 840 10040 4 3" , len=1 -> OID=dsa-with-sha.
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.
213 * value="9801011200Z" , len=1 -> time=Jannuary 1st, 1998
214 * at 12h 00m Greenwich Mean Time
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".
223 * value="2001010112001.12-0700" , len=1 -> time=Jannuary
224 * 1st, 2001 at 12h 00m 01.12s Pacific Daylight Time
226 * OCTET STRING: VALUE contains the octet string and LEN is the
229 * value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
230 * len=3 -> three bytes octet string
232 * GeneralString: VALUE contains the generalstring and LEN is the
235 * value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
236 * len=3 -> three bytes generalstring
238 * BIT STRING: VALUE contains the bit string organized by bytes and
239 * LEN is the number of bits.
241 * value="$\backslash$xCF" , len=6 -> bit string="110011" (six
244 * CHOICE: if NAME indicates a choice type, VALUE must specify one of
245 * the alternatives with a null terminated string. LEN != 0. Using
248 * result=asn1_write_value(cert,
249 * "certificate1.tbsCertificate.subject", "rdnSequence",
252 * ANY: VALUE indicates the der encoding of a structure. LEN != 0.
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.
261 * result=asn1_write_value(cert,
262 * "certificate1.tbsCertificate.subject.rdnSequence", "NEW", 1);
264 * SET OF: the same as SEQUENCE OF. Using "pkix.asn":
266 * result=asn1_write_value(cert,
267 * "tbsCertificate.subject.rdnSequence.?LAST", "NEW", 1);
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.
274 asn1_write_value (asn1_node node_root, const char *name,
275 const void *ivalue, int len)
277 asn1_node node, p, p2;
278 unsigned char *temp, *value_temp = NULL, *default_temp = NULL;
279 int len2, k, k2, negative;
281 const unsigned char *value = ivalue;
284 node = asn1_find_node (node_root, name);
286 return ASN1_ELEMENT_NOT_FOUND;
288 if ((node->type & CONST_OPTION) && (value == NULL) && (len == 0))
290 asn1_delete_structure (&node);
294 type = type_field (node->type);
296 if ((type == ASN1_ETYPE_SEQUENCE_OF || type == ASN1_ETYPE_SET_OF) && (value == NULL) && (len == 0))
299 while ((type_field (p->type) == ASN1_ETYPE_TAG)
300 || (type_field (p->type) == ASN1_ETYPE_SIZE))
304 asn1_delete_structure (&p->right);
309 /* Don't allow element deletion for other types */
312 return ASN1_VALUE_NOT_VALID;
317 case ASN1_ETYPE_BOOLEAN:
318 if (!_asn1_strcmp (value, "TRUE"))
320 if (node->type & CONST_DEFAULT)
323 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
325 if (p->type & CONST_TRUE)
326 _asn1_set_value (node, NULL, 0);
328 _asn1_set_value (node, "T", 1);
331 _asn1_set_value (node, "T", 1);
333 else if (!_asn1_strcmp (value, "FALSE"))
335 if (node->type & CONST_DEFAULT)
338 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
340 if (p->type & CONST_FALSE)
341 _asn1_set_value (node, NULL, 0);
343 _asn1_set_value (node, "F", 1);
346 _asn1_set_value (node, "F", 1);
349 return ASN1_VALUE_NOT_VALID;
351 case ASN1_ETYPE_INTEGER:
352 case ASN1_ETYPE_ENUMERATED:
355 if ((isdigit (value[0])) || (value[0] == '-'))
357 value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
358 if (value_temp == NULL)
359 return ASN1_MEM_ALLOC_ERROR;
361 _asn1_convert_integer (value, value_temp,
362 SIZEOF_UNSIGNED_LONG_INT, &len);
365 { /* is an identifier like v1 */
366 if (!(node->type & CONST_LIST))
367 return ASN1_VALUE_NOT_VALID;
371 if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
373 if (!_asn1_strcmp (p->name, value))
375 value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
376 if (value_temp == NULL)
377 return ASN1_MEM_ALLOC_ERROR;
379 _asn1_convert_integer (p->value,
381 SIZEOF_UNSIGNED_LONG_INT,
389 return ASN1_VALUE_NOT_VALID;
394 value_temp = malloc (len);
395 if (value_temp == NULL)
396 return ASN1_MEM_ALLOC_ERROR;
397 memcpy (value_temp, value, len);
400 if (value_temp[0] & 0x80)
405 if (negative && (type_field (node->type) == ASN1_ETYPE_ENUMERATED))
408 return ASN1_VALUE_NOT_VALID;
411 for (k = 0; k < len - 1; k++)
412 if (negative && (value_temp[k] != 0xFF))
414 else if (!negative && value_temp[k])
417 if ((negative && !(value_temp[k] & 0x80)) ||
418 (!negative && (value_temp[k] & 0x80)))
421 _asn1_set_value_lv (node, value_temp + k, len - k);
423 if (node->type & CONST_DEFAULT)
426 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
428 if ((isdigit (p->value[0])) || (p->value[0] == '-'))
430 default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
431 if (default_temp == NULL)
434 return ASN1_MEM_ALLOC_ERROR;
437 _asn1_convert_integer (p->value, default_temp,
438 SIZEOF_UNSIGNED_LONG_INT, &len2);
441 { /* is an identifier like v1 */
442 if (!(node->type & CONST_LIST))
445 return ASN1_VALUE_NOT_VALID;
450 if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
452 if (!_asn1_strcmp (p2->name, p->value))
454 default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
455 if (default_temp == NULL)
458 return ASN1_MEM_ALLOC_ERROR;
461 _asn1_convert_integer (p2->value,
463 SIZEOF_UNSIGNED_LONG_INT,
473 return ASN1_VALUE_NOT_VALID;
478 if ((len - k) == len2)
480 for (k2 = 0; k2 < len2; k2++)
481 if (value_temp[k + k2] != default_temp[k2])
486 _asn1_set_value (node, NULL, 0);
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)
499 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
501 if (!_asn1_strcmp (value, p->value))
503 _asn1_set_value (node, NULL, 0);
507 _asn1_set_value (node, value, _asn1_strlen (value) + 1);
509 case ASN1_ETYPE_UTC_TIME:
511 len = _asn1_strlen (value);
513 return ASN1_VALUE_NOT_VALID;
514 for (k = 0; k < 10; k++)
515 if (!isdigit (value[k]))
516 return ASN1_VALUE_NOT_VALID;
520 if (value[10] != 'Z')
521 return ASN1_VALUE_NOT_VALID;
524 if ((!isdigit (value[10])) || (!isdigit (value[11])) ||
526 return ASN1_VALUE_NOT_VALID;
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;
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;
545 return ASN1_VALUE_NOT_FOUND;
547 _asn1_set_value (node, value, len);
550 case ASN1_ETYPE_GENERALIZED_TIME:
551 len = _asn1_strlen (value);
552 _asn1_set_value (node, value, len);
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:
565 len = _asn1_strlen (value);
566 _asn1_set_value_lv (node, value, len);
568 case ASN1_ETYPE_BIT_STRING:
570 len = _asn1_strlen (value);
571 asn1_length_der ((len >> 3) + 2, NULL, &len2);
572 temp = malloc ((len >> 3) + 2 + len2);
574 return ASN1_MEM_ALLOC_ERROR;
576 asn1_bit_der (value, len, temp, &len2);
577 _asn1_set_value_m (node, temp, len2);
580 case ASN1_ETYPE_CHOICE:
584 if (!_asn1_strcmp (p->name, value))
591 asn1_delete_structure (&p2);
602 return ASN1_ELEMENT_NOT_FOUND;
605 _asn1_set_value_lv (node, value, len);
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);
614 return ASN1_ELEMENT_NOT_FOUND;
622 #define PUT_VALUE( ptr, ptr_size, data, data_size) \
624 if (ptr_size < data_size) { \
625 return ASN1_MEM_ERROR; \
627 if (ptr && data_size > 0) \
628 memcpy (ptr, data, data_size); \
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; \
636 /* this strcpy is checked */ \
638 _asn1_strcpy (ptr, data); \
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; \
647 /* this strcpy is checked */ \
650 memcpy (ptr, data, data_size); \
651 ptr[data_size] = 0; \
655 #define ADD_STR_VALUE( ptr, ptr_size, data) \
656 *len += _asn1_strlen(data); \
657 if (ptr_size < (int) *len) { \
659 return ASN1_MEM_ERROR; \
661 /* this strcat is checked */ \
662 if (ptr) _asn1_strcat (ptr, data); \
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.
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
681 * Note that there can be valid values with length zero. In these case
682 * this function will succeed and @len will be zero.
684 * INTEGER: VALUE will contain a two's complement form integer.
686 * integer=-1 -> value[0]=0xFF , len=1.
687 * integer=1 -> value[0]=0x01 , len=1.
689 * ENUMERATED: As INTEGER (but only with not negative numbers).
691 * BOOLEAN: VALUE will be the null terminated string "TRUE" or
692 * "FALSE" and LEN=5 or LEN=6.
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").
697 * LEN = strlen(VALUE)+1
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.
703 * GeneralizedTime: VALUE will be a null terminated string in the
704 * same format used to set the value.
706 * OCTET STRING: VALUE will contain the octet string and LEN will be
707 * the number of octets.
709 * GeneralString: VALUE will contain the generalstring and LEN will
710 * be the number of octets.
712 * BIT STRING: VALUE will contain the bit string organized by bytes
713 * and LEN will be the number of bits.
715 * CHOICE: If NAME indicates a choice type, VALUE will specify the
716 * alternative selected.
718 * ANY: If NAME indicates an any type, VALUE will indicate the DER
719 * encoding of the structure actually used.
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
729 asn1_read_value (asn1_node root, const char *name, void *ivalue, int *len)
731 return asn1_read_value_type (root, name, ivalue, len, NULL);
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)
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
751 * Note that there can be valid values with length zero. In these case
752 * this function will succeed and @len will be zero.
755 * INTEGER: VALUE will contain a two's complement form integer.
757 * integer=-1 -> value[0]=0xFF , len=1.
758 * integer=1 -> value[0]=0x01 , len=1.
760 * ENUMERATED: As INTEGER (but only with not negative numbers).
762 * BOOLEAN: VALUE will be the null terminated string "TRUE" or
763 * "FALSE" and LEN=5 or LEN=6.
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").
768 * LEN = strlen(VALUE)+1
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.
774 * GeneralizedTime: VALUE will be a null terminated string in the
775 * same format used to set the value.
777 * OCTET STRING: VALUE will contain the octet string and LEN will be
778 * the number of octets.
780 * GeneralString: VALUE will contain the generalstring and LEN will
781 * be the number of octets.
783 * BIT STRING: VALUE will contain the bit string organized by bytes
784 * and LEN will be the number of bits.
786 * CHOICE: If NAME indicates a choice type, VALUE will specify the
787 * alternative selected.
789 * ANY: If NAME indicates an any type, VALUE will indicate the DER
790 * encoding of the structure actually used.
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
800 asn1_read_value_type (asn1_node root, const char *name, void *ivalue,
801 int *len, unsigned int *etype)
803 asn1_node node, p, p2;
804 int len2, len3, result;
805 int value_size = *len;
806 unsigned char *value = ivalue;
809 node = asn1_find_node (root, name);
811 return ASN1_ELEMENT_NOT_FOUND;
813 type = type_field (node->type);
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;
825 case ASN1_ETYPE_NULL:
826 PUT_STR_VALUE (value, value_size, "NULL");
828 case ASN1_ETYPE_BOOLEAN:
829 if ((node->type & CONST_DEFAULT) && (node->value == NULL))
832 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
834 if (p->type & CONST_TRUE)
836 PUT_STR_VALUE (value, value_size, "TRUE");
840 PUT_STR_VALUE (value, value_size, "FALSE");
843 else if (node->value[0] == 'T')
845 PUT_STR_VALUE (value, value_size, "TRUE");
849 PUT_STR_VALUE (value, value_size, "FALSE");
852 case ASN1_ETYPE_INTEGER:
853 case ASN1_ETYPE_ENUMERATED:
854 if ((node->type & CONST_DEFAULT) && (node->value == NULL))
857 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
859 if ((isdigit (p->value[0])) || (p->value[0] == '-')
860 || (p->value[0] == '+'))
862 result = _asn1_convert_integer
863 (p->value, value, value_size, len);
864 if (result != ASN1_SUCCESS)
868 { /* is an identifier like v1 */
872 if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
874 if (!_asn1_strcmp (p2->name, p->value))
876 result = _asn1_convert_integer
877 (p2->value, value, value_size,
879 if (result != ASN1_SUCCESS)
891 result = asn1_get_octet_der
892 (node->value, node->value_len, &len2, value, value_size,
894 if (result != ASN1_SUCCESS)
898 case ASN1_ETYPE_OBJECT_ID:
899 if (node->type & CONST_ASSIGN)
907 if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
909 ADD_STR_VALUE (value, value_size, p->value);
912 ADD_STR_VALUE (value, value_size, ".");
919 else if ((node->type & CONST_DEFAULT) && (node->value == NULL))
922 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
924 PUT_STR_VALUE (value, value_size, p->value);
928 PUT_STR_VALUE (value, value_size, node->value);
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);
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:
946 result = asn1_get_octet_der
947 (node->value, node->value_len, &len2, value, value_size,
949 if (result != ASN1_SUCCESS)
952 case ASN1_ETYPE_BIT_STRING:
954 result = asn1_get_bit_der
955 (node->value, node->value_len, &len2, value, value_size,
957 if (result != ASN1_SUCCESS)
960 case ASN1_ETYPE_CHOICE:
961 PUT_STR_VALUE (value, value_size, node->down->name);
965 len2 = asn1_get_length_der (node->value, node->value_len, &len3);
967 return ASN1_DER_ERROR;
968 PUT_VALUE (value, value_size, node->value + len3, len2);
971 return ASN1_ELEMENT_NOT_FOUND;
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.
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.
990 * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
991 * @name is not a valid element.
994 asn1_read_tag (asn1_node root, const char *name, int *tagValue,
997 asn1_node node, p, pTag;
999 node = asn1_find_node (root, name);
1001 return ASN1_ELEMENT_NOT_FOUND;
1005 /* pTag will points to the IMPLICIT TAG */
1007 if (node->type & CONST_TAG)
1011 if (type_field (p->type) == ASN1_ETYPE_TAG)
1013 if ((p->type & CONST_IMPLICIT) && (pTag == NULL))
1015 else if (p->type & CONST_EXPLICIT)
1024 *tagValue = _asn1_strtoul (pTag->value, NULL, 10);
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;
1033 *classValue = ASN1_CLASS_CONTEXT_SPECIFIC;
1037 unsigned type = type_field (node->type);
1038 *classValue = ASN1_CLASS_UNIVERSAL;
1042 CASE_HANDLED_ETYPES:
1043 *tagValue = _asn1_tags[type].tag;
1045 case ASN1_ETYPE_TAG:
1046 case ASN1_ETYPE_CHOICE:
1047 case ASN1_ETYPE_ANY:
1055 return ASN1_SUCCESS;
1059 * asn1_read_node_value:
1060 * @node: pointer to a node.
1061 * @data: a point to a asn1_data_node_st
1063 * Returns the value a data node inside a asn1_node structure.
1064 * The data returned should be handled as constant values.
1066 * Returns: %ASN1_SUCCESS if the node exists.
1069 asn1_read_node_value (asn1_node node, asn1_data_node_st * data)
1071 data->name = node->name;
1072 data->value = node->value;
1073 data->value_len = node->value_len;
1074 data->type = type_field (node->type);
1076 return ASN1_SUCCESS;