2 * Copyright (C) 2002-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
35 #ifdef HAVE_SYS_TYPES_H
36 #include <sys/types.h>
41 #define ASN1_SMALL_VALUE_SIZE 16
43 /* This structure is also in libtasn1.h, but then contains less
44 fields. You cannot make any modifications to these first fields
45 without breaking ABI. */
49 char name[ASN1_MAX_NAME_SIZE + 1]; /* Node name */
50 unsigned int name_hash;
51 unsigned int type; /* Node type */
52 unsigned char *value; /* Node value */
54 asn1_node down; /* Pointer to the son node */
55 asn1_node right; /* Pointer to the brother node */
56 asn1_node left; /* Pointer to the next list element */
58 unsigned char small_value[ASN1_SMALL_VALUE_SIZE]; /* For small values */
60 /* values used during decoding/coding */
62 unsigned start; /* the start of the DER sequence - if decoded */
63 unsigned end; /* the end of the DER sequence - if decoded */
66 typedef struct tag_and_class_st
73 /* the types that are handled in _asn1_tags */
74 #define CASE_HANDLED_ETYPES \
75 case ASN1_ETYPE_NULL: \
76 case ASN1_ETYPE_BOOLEAN: \
77 case ASN1_ETYPE_INTEGER: \
78 case ASN1_ETYPE_ENUMERATED: \
79 case ASN1_ETYPE_OBJECT_ID: \
80 case ASN1_ETYPE_OCTET_STRING: \
81 case ASN1_ETYPE_GENERALSTRING: \
82 case ASN1_ETYPE_NUMERIC_STRING: \
83 case ASN1_ETYPE_IA5_STRING: \
84 case ASN1_ETYPE_TELETEX_STRING: \
85 case ASN1_ETYPE_PRINTABLE_STRING: \
86 case ASN1_ETYPE_UNIVERSAL_STRING: \
87 case ASN1_ETYPE_BMP_STRING: \
88 case ASN1_ETYPE_UTF8_STRING: \
89 case ASN1_ETYPE_VISIBLE_STRING: \
90 case ASN1_ETYPE_BIT_STRING: \
91 case ASN1_ETYPE_SEQUENCE: \
92 case ASN1_ETYPE_SEQUENCE_OF: \
93 case ASN1_ETYPE_SET: \
94 case ASN1_ETYPE_UTC_TIME: \
95 case ASN1_ETYPE_GENERALIZED_TIME: \
96 case ASN1_ETYPE_SET_OF
98 #define ETYPE_TAG(etype) (_asn1_tags[etype].tag)
99 #define ETYPE_CLASS(etype) (_asn1_tags[etype].class)
100 #define ETYPE_OK(etype) (((etype) != ASN1_ETYPE_INVALID && \
101 (etype) <= _asn1_tags_size && \
102 _asn1_tags[(etype)].desc != NULL)?1:0)
104 #define ETYPE_IS_STRING(etype) ((etype == ASN1_ETYPE_GENERALSTRING || \
105 etype == ASN1_ETYPE_NUMERIC_STRING || etype == ASN1_ETYPE_IA5_STRING || \
106 etype == ASN1_ETYPE_TELETEX_STRING || etype == ASN1_ETYPE_PRINTABLE_STRING || \
107 etype == ASN1_ETYPE_UNIVERSAL_STRING || etype == ASN1_ETYPE_BMP_STRING || \
108 etype == ASN1_ETYPE_UTF8_STRING || etype == ASN1_ETYPE_VISIBLE_STRING || \
109 etype == ASN1_ETYPE_OCTET_STRING)?1:0)
111 extern unsigned int _asn1_tags_size;
112 extern const tag_and_class_st _asn1_tags[];
114 #define _asn1_strlen(s) strlen((const char *) s)
115 #define _asn1_strtol(n,e,b) strtol((const char *) n, e, b)
116 #define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b)
117 #define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b)
118 #define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b)
119 #define _asn1_strcat(a,b) strcat((char *)a, (const char *)b)
121 #if SIZEOF_UNSIGNED_LONG_INT == 8
122 # define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b)
124 # define _asn1_strtou64(n,e,b) strtoull((const char *) n, e, b)
127 #define MAX_LOG_SIZE 1024 /* maximum number of characters of a log message */
129 /* Define used for visiting trees. */
134 /***********************************************************************/
135 /* List of constants to better specify the type of typedef asn1_node_st. */
136 /***********************************************************************/
137 /* Used with TYPE_TAG */
138 #define CONST_UNIVERSAL (1<<8)
139 #define CONST_PRIVATE (1<<9)
140 #define CONST_APPLICATION (1<<10)
141 #define CONST_EXPLICIT (1<<11)
142 #define CONST_IMPLICIT (1<<12)
144 #define CONST_TAG (1<<13) /* Used in ASN.1 assignement */
145 #define CONST_OPTION (1<<14)
146 #define CONST_DEFAULT (1<<15)
147 #define CONST_TRUE (1<<16)
148 #define CONST_FALSE (1<<17)
150 #define CONST_LIST (1<<18) /* Used with TYPE_INTEGER and TYPE_BIT_STRING */
151 #define CONST_MIN_MAX (1<<19)
153 #define CONST_1_PARAM (1<<20)
155 #define CONST_SIZE (1<<21)
157 #define CONST_DEFINED_BY (1<<22)
159 /* Those two are deprecated and used for backwards compatibility */
160 #define CONST_GENERALIZED (1<<23)
161 #define CONST_UTC (1<<24)
163 /* #define CONST_IMPORTS (1<<25) */
165 #define CONST_NOT_USED (1<<26)
166 #define CONST_SET (1<<27)
167 #define CONST_ASSIGN (1<<28)
169 #define CONST_DOWN (1<<29)
170 #define CONST_RIGHT (1<<30)
173 #define ASN1_ETYPE_TIME 17
174 /****************************************/
175 /* Returns the first 8 bits. */
176 /* Used with the field type of asn1_node_st */
177 /****************************************/
178 inline static unsigned int
179 type_field (unsigned int ntype)
181 return (ntype & 0xff);
184 /* To convert old types from a static structure */
185 inline static unsigned int
186 convert_old_type (unsigned int ntype)
188 unsigned int type = ntype & 0xff;
189 if (type == ASN1_ETYPE_TIME)
191 if (ntype & CONST_UTC)
192 type = ASN1_ETYPE_UTC_TIME;
194 type = ASN1_ETYPE_GENERALIZED_TIME;
196 ntype &= ~(CONST_UTC | CONST_GENERALIZED);
207 void *_asn1_realloc(void *ptr, size_t size)
214 ret = realloc(ptr, size);