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);
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, asn1_node *ptail)
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);
148 if (ptail == NULL || *ptail == NULL || (*ptail)->up != p->up)
157 _asn1_set_right (p, p2);
162 _asn1_str_cpy (temp, sizeof (temp), "?1");
165 n = strtol (p->name + 1, NULL, 0);
168 _asn1_ltostr (n, temp + 1);
170 _asn1_set_name (p2, temp);
171 /* p2->type |= CONST_OPTION; */
179 * @node_root: pointer to a structure
180 * @name: the name of the element inside the structure that you want to set.
181 * @ivalue: vector used to specify the value to set. If len is >0,
182 * VALUE must be a two's complement form integer. if len=0 *VALUE
183 * must be a null terminated string with an integer value.
184 * @len: number of bytes of *value to use to set the value:
185 * value[0]..value[len-1] or 0 if value is a null terminated string
187 * Set the value of one element inside a structure.
189 * If an element is OPTIONAL and you want to delete it, you must use
190 * the value=NULL and len=0. Using "pkix.asn":
192 * result=asn1_write_value(cert, "tbsCertificate.issuerUniqueID",
195 * Description for each type:
197 * INTEGER: VALUE must contain a two's complement form integer.
199 * value[0]=0xFF , len=1 -> integer=-1.
200 * value[0]=0xFF value[1]=0xFF , len=2 -> integer=-1.
201 * value[0]=0x01 , len=1 -> integer= 1.
202 * value[0]=0x00 value[1]=0x01 , len=2 -> integer= 1.
203 * value="123" , len=0 -> integer= 123.
205 * ENUMERATED: As INTEGER (but only with not negative numbers).
207 * BOOLEAN: VALUE must be the null terminated string "TRUE" or
208 * "FALSE" and LEN != 0.
210 * value="TRUE" , len=1 -> boolean=TRUE.
211 * value="FALSE" , len=1 -> boolean=FALSE.
213 * OBJECT IDENTIFIER: VALUE must be a null terminated string with
214 * each number separated by a dot (e.g. "1.2.3.543.1"). LEN != 0.
216 * value="1 2 840 10040 4 3" , len=1 -> OID=dsa-with-sha.
218 * UTCTime: VALUE must be a null terminated string in one of these
219 * formats: "YYMMDDhhmmssZ", "YYMMDDhhmmssZ",
220 * "YYMMDDhhmmss+hh'mm'", "YYMMDDhhmmss-hh'mm'",
221 * "YYMMDDhhmm+hh'mm'", or "YYMMDDhhmm-hh'mm'". LEN != 0.
223 * value="9801011200Z" , len=1 -> time=Jannuary 1st, 1998
224 * at 12h 00m Greenwich Mean Time
226 * GeneralizedTime: VALUE must be in one of this format:
227 * "YYYYMMDDhhmmss.sZ", "YYYYMMDDhhmmss.sZ",
228 * "YYYYMMDDhhmmss.s+hh'mm'", "YYYYMMDDhhmmss.s-hh'mm'",
229 * "YYYYMMDDhhmm+hh'mm'", or "YYYYMMDDhhmm-hh'mm'" where ss.s
230 * indicates the seconds with any precision like "10.1" or "01.02".
233 * value="2001010112001.12-0700" , len=1 -> time=Jannuary
234 * 1st, 2001 at 12h 00m 01.12s Pacific Daylight Time
236 * OCTET STRING: VALUE contains the octet string and LEN is the
239 * value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
240 * len=3 -> three bytes octet string
242 * GeneralString: VALUE contains the generalstring and LEN is the
245 * value="$\backslash$x01$\backslash$x02$\backslash$x03" ,
246 * len=3 -> three bytes generalstring
248 * BIT STRING: VALUE contains the bit string organized by bytes and
249 * LEN is the number of bits.
251 * value="$\backslash$xCF" , len=6 -> bit string="110011" (six
254 * CHOICE: if NAME indicates a choice type, VALUE must specify one of
255 * the alternatives with a null terminated string. LEN != 0. Using
258 * result=asn1_write_value(cert,
259 * "certificate1.tbsCertificate.subject", "rdnSequence",
262 * ANY: VALUE indicates the der encoding of a structure. LEN != 0.
264 * SEQUENCE OF: VALUE must be the null terminated string "NEW" and
265 * LEN != 0. With this instruction another element is appended in
266 * the sequence. The name of this element will be "?1" if it's the
267 * first one, "?2" for the second and so on.
271 * result=asn1_write_value(cert,
272 * "certificate1.tbsCertificate.subject.rdnSequence", "NEW", 1);
274 * SET OF: the same as SEQUENCE OF. Using "pkix.asn":
276 * result=asn1_write_value(cert,
277 * "tbsCertificate.subject.rdnSequence.?LAST", "NEW", 1);
279 * Returns: %ASN1_SUCCESS if the value was set,
280 * %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element, and
281 * %ASN1_VALUE_NOT_VALID if @ivalue has a wrong format.
284 asn1_write_value (asn1_node node_root, const char *name,
285 const void *ivalue, int len)
287 asn1_node node, p, p2;
288 unsigned char *temp, *value_temp = NULL, *default_temp = NULL;
289 int len2, k, k2, negative;
291 const unsigned char *value = ivalue;
294 node = asn1_find_node (node_root, name);
296 return ASN1_ELEMENT_NOT_FOUND;
298 if ((node->type & CONST_OPTION) && (value == NULL) && (len == 0))
300 asn1_delete_structure (&node);
304 type = type_field (node->type);
306 if ((type == ASN1_ETYPE_SEQUENCE_OF || type == ASN1_ETYPE_SET_OF) && (value == NULL) && (len == 0))
309 while ((type_field (p->type) == ASN1_ETYPE_TAG)
310 || (type_field (p->type) == ASN1_ETYPE_SIZE))
314 asn1_delete_structure (&p->right);
319 /* Don't allow element deletion for other types */
322 return ASN1_VALUE_NOT_VALID;
327 case ASN1_ETYPE_BOOLEAN:
328 if (!_asn1_strcmp (value, "TRUE"))
330 if (node->type & CONST_DEFAULT)
333 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
335 if (p->type & CONST_TRUE)
336 _asn1_set_value (node, NULL, 0);
338 _asn1_set_value (node, "T", 1);
341 _asn1_set_value (node, "T", 1);
343 else if (!_asn1_strcmp (value, "FALSE"))
345 if (node->type & CONST_DEFAULT)
348 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
350 if (p->type & CONST_FALSE)
351 _asn1_set_value (node, NULL, 0);
353 _asn1_set_value (node, "F", 1);
356 _asn1_set_value (node, "F", 1);
359 return ASN1_VALUE_NOT_VALID;
361 case ASN1_ETYPE_INTEGER:
362 case ASN1_ETYPE_ENUMERATED:
365 if ((isdigit (value[0])) || (value[0] == '-'))
367 value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
368 if (value_temp == NULL)
369 return ASN1_MEM_ALLOC_ERROR;
371 _asn1_convert_integer (value, value_temp,
372 SIZEOF_UNSIGNED_LONG_INT, &len);
375 { /* is an identifier like v1 */
376 if (!(node->type & CONST_LIST))
377 return ASN1_VALUE_NOT_VALID;
381 if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
383 if (!_asn1_strcmp (p->name, value))
385 value_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
386 if (value_temp == NULL)
387 return ASN1_MEM_ALLOC_ERROR;
389 _asn1_convert_integer (p->value,
391 SIZEOF_UNSIGNED_LONG_INT,
399 return ASN1_VALUE_NOT_VALID;
404 value_temp = malloc (len);
405 if (value_temp == NULL)
406 return ASN1_MEM_ALLOC_ERROR;
407 memcpy (value_temp, value, len);
410 if (value_temp[0] & 0x80)
415 if (negative && (type_field (node->type) == ASN1_ETYPE_ENUMERATED))
418 return ASN1_VALUE_NOT_VALID;
421 for (k = 0; k < len - 1; k++)
422 if (negative && (value_temp[k] != 0xFF))
424 else if (!negative && value_temp[k])
427 if ((negative && !(value_temp[k] & 0x80)) ||
428 (!negative && (value_temp[k] & 0x80)))
431 _asn1_set_value_lv (node, value_temp + k, len - k);
433 if (node->type & CONST_DEFAULT)
436 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
438 if ((isdigit (p->value[0])) || (p->value[0] == '-'))
440 default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
441 if (default_temp == NULL)
444 return ASN1_MEM_ALLOC_ERROR;
447 _asn1_convert_integer (p->value, default_temp,
448 SIZEOF_UNSIGNED_LONG_INT, &len2);
451 { /* is an identifier like v1 */
452 if (!(node->type & CONST_LIST))
455 return ASN1_VALUE_NOT_VALID;
460 if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
462 if (!_asn1_strcmp (p2->name, p->value))
464 default_temp = malloc (SIZEOF_UNSIGNED_LONG_INT);
465 if (default_temp == NULL)
468 return ASN1_MEM_ALLOC_ERROR;
471 _asn1_convert_integer (p2->value,
473 SIZEOF_UNSIGNED_LONG_INT,
483 return ASN1_VALUE_NOT_VALID;
488 if ((len - k) == len2)
490 for (k2 = 0; k2 < len2; k2++)
491 if (value_temp[k + k2] != default_temp[k2])
496 _asn1_set_value (node, NULL, 0);
502 case ASN1_ETYPE_OBJECT_ID:
503 for (i = 0; i < _asn1_strlen (value); i++)
504 if ((!isdigit (value[i])) && (value[i] != '.') && (value[i] != '+'))
505 return ASN1_VALUE_NOT_VALID;
506 if (node->type & CONST_DEFAULT)
509 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
511 if (!_asn1_strcmp (value, p->value))
513 _asn1_set_value (node, NULL, 0);
517 _asn1_set_value (node, value, _asn1_strlen (value) + 1);
519 case ASN1_ETYPE_UTC_TIME:
521 len = _asn1_strlen (value);
523 return ASN1_VALUE_NOT_VALID;
524 for (k = 0; k < 10; k++)
525 if (!isdigit (value[k]))
526 return ASN1_VALUE_NOT_VALID;
530 if (value[10] != 'Z')
531 return ASN1_VALUE_NOT_VALID;
534 if ((!isdigit (value[10])) || (!isdigit (value[11])) ||
536 return ASN1_VALUE_NOT_VALID;
539 if ((value[10] != '+') && (value[10] != '-'))
540 return ASN1_VALUE_NOT_VALID;
541 for (k = 11; k < 15; k++)
542 if (!isdigit (value[k]))
543 return ASN1_VALUE_NOT_VALID;
546 if ((!isdigit (value[10])) || (!isdigit (value[11])))
547 return ASN1_VALUE_NOT_VALID;
548 if ((value[12] != '+') && (value[12] != '-'))
549 return ASN1_VALUE_NOT_VALID;
550 for (k = 13; k < 17; k++)
551 if (!isdigit (value[k]))
552 return ASN1_VALUE_NOT_VALID;
555 return ASN1_VALUE_NOT_FOUND;
557 _asn1_set_value (node, value, len);
560 case ASN1_ETYPE_GENERALIZED_TIME:
561 len = _asn1_strlen (value);
562 _asn1_set_value (node, value, len);
564 case ASN1_ETYPE_OCTET_STRING:
565 case ASN1_ETYPE_GENERALSTRING:
566 case ASN1_ETYPE_NUMERIC_STRING:
567 case ASN1_ETYPE_IA5_STRING:
568 case ASN1_ETYPE_TELETEX_STRING:
569 case ASN1_ETYPE_PRINTABLE_STRING:
570 case ASN1_ETYPE_UNIVERSAL_STRING:
571 case ASN1_ETYPE_BMP_STRING:
572 case ASN1_ETYPE_UTF8_STRING:
573 case ASN1_ETYPE_VISIBLE_STRING:
575 len = _asn1_strlen (value);
576 _asn1_set_value_lv (node, value, len);
578 case ASN1_ETYPE_BIT_STRING:
580 len = _asn1_strlen (value);
581 asn1_length_der ((len >> 3) + 2, NULL, &len2);
582 temp = malloc ((len >> 3) + 2 + len2);
584 return ASN1_MEM_ALLOC_ERROR;
586 asn1_bit_der (value, len, temp, &len2);
587 _asn1_set_value_m (node, temp, len2);
590 case ASN1_ETYPE_CHOICE:
594 if (!_asn1_strcmp (p->name, value))
601 asn1_delete_structure (&p2);
612 return ASN1_ELEMENT_NOT_FOUND;
615 _asn1_set_value_lv (node, value, len);
617 case ASN1_ETYPE_SEQUENCE_OF:
618 case ASN1_ETYPE_SET_OF:
619 if (_asn1_strcmp (value, "NEW"))
620 return ASN1_VALUE_NOT_VALID;
621 _asn1_append_sequence_set (node, NULL);
624 return ASN1_ELEMENT_NOT_FOUND;
632 #define PUT_VALUE( ptr, ptr_size, data, data_size) \
634 if (ptr_size < data_size) { \
635 return ASN1_MEM_ERROR; \
637 if (ptr && data_size > 0) \
638 memcpy (ptr, data, data_size); \
641 #define PUT_STR_VALUE( ptr, ptr_size, data) \
642 *len = _asn1_strlen (data) + 1; \
643 if (ptr_size < *len) { \
644 return ASN1_MEM_ERROR; \
646 /* this strcpy is checked */ \
648 _asn1_strcpy (ptr, data); \
652 #define PUT_AS_STR_VALUE( ptr, ptr_size, data, data_size) \
653 *len = data_size + 1; \
654 if (ptr_size < *len) { \
655 return ASN1_MEM_ERROR; \
657 /* this strcpy is checked */ \
660 memcpy (ptr, data, data_size); \
661 ptr[data_size] = 0; \
665 #define ADD_STR_VALUE( ptr, ptr_size, data) \
666 *len += _asn1_strlen(data); \
667 if (ptr_size < (int) *len) { \
669 return ASN1_MEM_ERROR; \
671 /* this strcat is checked */ \
672 if (ptr) _asn1_strcat (ptr, data); \
677 * @root: pointer to a structure.
678 * @name: the name of the element inside a structure that you want to read.
679 * @ivalue: vector that will contain the element's content, must be a
680 * pointer to memory cells already allocated (may be %NULL).
681 * @len: number of bytes of *value: value[0]..value[len-1]. Initialy
682 * holds the sizeof value.
684 * Returns the value of one element inside a structure.
685 * If an element is OPTIONAL and this returns
686 * %ASN1_ELEMENT_NOT_FOUND, it means that this element wasn't present
687 * in the der encoding that created the structure. The first element
688 * of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and
689 * so on. If the @root provided is a node to specific sequence element,
690 * then the keyword "?CURRENT" is also acceptable and indicates the
691 * current sequence element of this node.
693 * Note that there can be valid values with length zero. In these case
694 * this function will succeed and @len will be zero.
696 * INTEGER: VALUE will contain a two's complement form integer.
698 * integer=-1 -> value[0]=0xFF , len=1.
699 * integer=1 -> value[0]=0x01 , len=1.
701 * ENUMERATED: As INTEGER (but only with not negative numbers).
703 * BOOLEAN: VALUE will be the null terminated string "TRUE" or
704 * "FALSE" and LEN=5 or LEN=6.
706 * OBJECT IDENTIFIER: VALUE will be a null terminated string with
707 * each number separated by a dot (i.e. "1.2.3.543.1").
709 * LEN = strlen(VALUE)+1
711 * UTCTime: VALUE will be a null terminated string in one of these
712 * formats: "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'".
713 * LEN=strlen(VALUE)+1.
715 * GeneralizedTime: VALUE will be a null terminated string in the
716 * same format used to set the value.
718 * OCTET STRING: VALUE will contain the octet string and LEN will be
719 * the number of octets.
721 * GeneralString: VALUE will contain the generalstring and LEN will
722 * be the number of octets.
724 * BIT STRING: VALUE will contain the bit string organized by bytes
725 * and LEN will be the number of bits.
727 * CHOICE: If NAME indicates a choice type, VALUE will specify the
728 * alternative selected.
730 * ANY: If NAME indicates an any type, VALUE will indicate the DER
731 * encoding of the structure actually used.
733 * Returns: %ASN1_SUCCESS if value is returned,
734 * %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element,
735 * %ASN1_VALUE_NOT_FOUND if there isn't any value for the element
736 * selected, and %ASN1_MEM_ERROR if The value vector isn't big enough
737 * to store the result, and in this case @len will contain the number of
741 asn1_read_value (asn1_node root, const char *name, void *ivalue, int *len)
743 return asn1_read_value_type (root, name, ivalue, len, NULL);
747 * asn1_read_value_type:
748 * @root: pointer to a structure.
749 * @name: the name of the element inside a structure that you want to read.
750 * @ivalue: vector that will contain the element's content, must be a
751 * pointer to memory cells already allocated (may be %NULL).
752 * @len: number of bytes of *value: value[0]..value[len-1]. Initialy
753 * holds the sizeof value.
754 * @etype: The type of the value read (ASN1_ETYPE)
756 * Returns the type and value of one element inside a structure.
757 * If an element is OPTIONAL and this returns
758 * %ASN1_ELEMENT_NOT_FOUND, it means that this element wasn't present
759 * in the der encoding that created the structure. The first element
760 * of a SEQUENCE_OF or SET_OF is named "?1". The second one "?2" and
761 * so on. If the @root provided is a node to specific sequence element,
762 * then the keyword "?CURRENT" is also acceptable and indicates the
763 * current sequence element of this node.
765 * Note that there can be valid values with length zero. In these case
766 * this function will succeed and @len will be zero.
769 * INTEGER: VALUE will contain a two's complement form integer.
771 * integer=-1 -> value[0]=0xFF , len=1.
772 * integer=1 -> value[0]=0x01 , len=1.
774 * ENUMERATED: As INTEGER (but only with not negative numbers).
776 * BOOLEAN: VALUE will be the null terminated string "TRUE" or
777 * "FALSE" and LEN=5 or LEN=6.
779 * OBJECT IDENTIFIER: VALUE will be a null terminated string with
780 * each number separated by a dot (i.e. "1.2.3.543.1").
782 * LEN = strlen(VALUE)+1
784 * UTCTime: VALUE will be a null terminated string in one of these
785 * formats: "YYMMDDhhmmss+hh'mm'" or "YYMMDDhhmmss-hh'mm'".
786 * LEN=strlen(VALUE)+1.
788 * GeneralizedTime: VALUE will be a null terminated string in the
789 * same format used to set the value.
791 * OCTET STRING: VALUE will contain the octet string and LEN will be
792 * the number of octets.
794 * GeneralString: VALUE will contain the generalstring and LEN will
795 * be the number of octets.
797 * BIT STRING: VALUE will contain the bit string organized by bytes
798 * and LEN will be the number of bits.
800 * CHOICE: If NAME indicates a choice type, VALUE will specify the
801 * alternative selected.
803 * ANY: If NAME indicates an any type, VALUE will indicate the DER
804 * encoding of the structure actually used.
806 * Returns: %ASN1_SUCCESS if value is returned,
807 * %ASN1_ELEMENT_NOT_FOUND if @name is not a valid element,
808 * %ASN1_VALUE_NOT_FOUND if there isn't any value for the element
809 * selected, and %ASN1_MEM_ERROR if The value vector isn't big enough
810 * to store the result, and in this case @len will contain the number of
814 asn1_read_value_type (asn1_node root, const char *name, void *ivalue,
815 int *len, unsigned int *etype)
817 asn1_node node, p, p2;
818 int len2, len3, result;
819 int value_size = *len;
820 unsigned char *value = ivalue;
823 node = asn1_find_node (root, name);
825 return ASN1_ELEMENT_NOT_FOUND;
827 type = type_field (node->type);
829 if ((type != ASN1_ETYPE_NULL) &&
830 (type != ASN1_ETYPE_CHOICE) &&
831 !(node->type & CONST_DEFAULT) && !(node->type & CONST_ASSIGN) &&
832 (node->value == NULL))
833 return ASN1_VALUE_NOT_FOUND;
839 case ASN1_ETYPE_NULL:
840 PUT_STR_VALUE (value, value_size, "NULL");
842 case ASN1_ETYPE_BOOLEAN:
843 if ((node->type & CONST_DEFAULT) && (node->value == NULL))
846 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
848 if (p->type & CONST_TRUE)
850 PUT_STR_VALUE (value, value_size, "TRUE");
854 PUT_STR_VALUE (value, value_size, "FALSE");
857 else if (node->value[0] == 'T')
859 PUT_STR_VALUE (value, value_size, "TRUE");
863 PUT_STR_VALUE (value, value_size, "FALSE");
866 case ASN1_ETYPE_INTEGER:
867 case ASN1_ETYPE_ENUMERATED:
868 if ((node->type & CONST_DEFAULT) && (node->value == NULL))
871 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
873 if ((isdigit (p->value[0])) || (p->value[0] == '-')
874 || (p->value[0] == '+'))
876 result = _asn1_convert_integer
877 (p->value, value, value_size, len);
878 if (result != ASN1_SUCCESS)
882 { /* is an identifier like v1 */
886 if (type_field (p2->type) == ASN1_ETYPE_CONSTANT)
888 if (!_asn1_strcmp (p2->name, p->value))
890 result = _asn1_convert_integer
891 (p2->value, value, value_size,
893 if (result != ASN1_SUCCESS)
905 result = asn1_get_octet_der
906 (node->value, node->value_len, &len2, value, value_size,
908 if (result != ASN1_SUCCESS)
912 case ASN1_ETYPE_OBJECT_ID:
913 if (node->type & CONST_ASSIGN)
921 if (type_field (p->type) == ASN1_ETYPE_CONSTANT)
923 ADD_STR_VALUE (value, value_size, p->value);
926 ADD_STR_VALUE (value, value_size, ".");
933 else if ((node->type & CONST_DEFAULT) && (node->value == NULL))
936 while (type_field (p->type) != ASN1_ETYPE_DEFAULT)
938 PUT_STR_VALUE (value, value_size, p->value);
942 PUT_STR_VALUE (value, value_size, node->value);
945 case ASN1_ETYPE_GENERALIZED_TIME:
946 case ASN1_ETYPE_UTC_TIME:
947 PUT_AS_STR_VALUE (value, value_size, node->value, node->value_len);
949 case ASN1_ETYPE_OCTET_STRING:
950 case ASN1_ETYPE_GENERALSTRING:
951 case ASN1_ETYPE_NUMERIC_STRING:
952 case ASN1_ETYPE_IA5_STRING:
953 case ASN1_ETYPE_TELETEX_STRING:
954 case ASN1_ETYPE_PRINTABLE_STRING:
955 case ASN1_ETYPE_UNIVERSAL_STRING:
956 case ASN1_ETYPE_BMP_STRING:
957 case ASN1_ETYPE_UTF8_STRING:
958 case ASN1_ETYPE_VISIBLE_STRING:
960 result = asn1_get_octet_der
961 (node->value, node->value_len, &len2, value, value_size,
963 if (result != ASN1_SUCCESS)
966 case ASN1_ETYPE_BIT_STRING:
968 result = asn1_get_bit_der
969 (node->value, node->value_len, &len2, value, value_size,
971 if (result != ASN1_SUCCESS)
974 case ASN1_ETYPE_CHOICE:
975 PUT_STR_VALUE (value, value_size, node->down->name);
979 len2 = asn1_get_length_der (node->value, node->value_len, &len3);
981 return ASN1_DER_ERROR;
982 PUT_VALUE (value, value_size, node->value + len3, len2);
985 return ASN1_ELEMENT_NOT_FOUND;
994 * @root: pointer to a structure
995 * @name: the name of the element inside a structure.
996 * @tagValue: variable that will contain the TAG value.
997 * @classValue: variable that will specify the TAG type.
999 * Returns the TAG and the CLASS of one element inside a structure.
1000 * CLASS can have one of these constants: %ASN1_CLASS_APPLICATION,
1001 * %ASN1_CLASS_UNIVERSAL, %ASN1_CLASS_PRIVATE or
1002 * %ASN1_CLASS_CONTEXT_SPECIFIC.
1004 * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
1005 * @name is not a valid element.
1008 asn1_read_tag (asn1_node root, const char *name, int *tagValue,
1011 asn1_node node, p, pTag;
1013 node = asn1_find_node (root, name);
1015 return ASN1_ELEMENT_NOT_FOUND;
1019 /* pTag will points to the IMPLICIT TAG */
1021 if (node->type & CONST_TAG)
1025 if (type_field (p->type) == ASN1_ETYPE_TAG)
1027 if ((p->type & CONST_IMPLICIT) && (pTag == NULL))
1029 else if (p->type & CONST_EXPLICIT)
1038 *tagValue = _asn1_strtoul (pTag->value, NULL, 10);
1040 if (pTag->type & CONST_APPLICATION)
1041 *classValue = ASN1_CLASS_APPLICATION;
1042 else if (pTag->type & CONST_UNIVERSAL)
1043 *classValue = ASN1_CLASS_UNIVERSAL;
1044 else if (pTag->type & CONST_PRIVATE)
1045 *classValue = ASN1_CLASS_PRIVATE;
1047 *classValue = ASN1_CLASS_CONTEXT_SPECIFIC;
1051 unsigned type = type_field (node->type);
1052 *classValue = ASN1_CLASS_UNIVERSAL;
1056 CASE_HANDLED_ETYPES:
1057 *tagValue = _asn1_tags[type].tag;
1059 case ASN1_ETYPE_TAG:
1060 case ASN1_ETYPE_CHOICE:
1061 case ASN1_ETYPE_ANY:
1069 return ASN1_SUCCESS;
1073 * asn1_read_node_value:
1074 * @node: pointer to a node.
1075 * @data: a point to a asn1_data_node_st
1077 * Returns the value a data node inside a asn1_node structure.
1078 * The data returned should be handled as constant values.
1080 * Returns: %ASN1_SUCCESS if the node exists.
1083 asn1_read_node_value (asn1_node node, asn1_data_node_st * data)
1085 data->name = node->name;
1086 data->value = node->value;
1087 data->value_len = node->value_len;
1088 data->type = type_field (node->type);
1090 return ASN1_SUCCESS;