Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2002-2025 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, see |
18 | | * <https://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #ifndef INT_H |
22 | | # define INT_H |
23 | | |
24 | | # include <string.h> |
25 | | # include <stdlib.h> |
26 | | # include <stdio.h> |
27 | | # include <stdint.h> |
28 | | # include <sys/types.h> |
29 | | |
30 | | # include <libtasn1.h> |
31 | | |
32 | | # define ASN1_SMALL_VALUE_SIZE 16 |
33 | | |
34 | | struct asn1_node_array_st |
35 | | { |
36 | | asn1_node *nodes; |
37 | | size_t size; |
38 | | }; |
39 | | |
40 | | struct asn1_node_st |
41 | | { |
42 | | char name[ASN1_MAX_NAME_SIZE + 1]; /* Node name */ |
43 | | unsigned int name_hash; |
44 | | unsigned int type; /* Node type */ |
45 | | unsigned char *value; /* Node value */ |
46 | | int value_len; |
47 | | asn1_node down; /* Pointer to the son node */ |
48 | | asn1_node right; /* Pointer to the brother node */ |
49 | | asn1_node left; /* Pointer to the next list element */ |
50 | | unsigned char small_value[ASN1_SMALL_VALUE_SIZE]; /* For small values */ |
51 | | asn1_node parent; /* Pointer to the parent node */ |
52 | | struct asn1_node_array_st numbered_children; /* Array of unnamed child nodes for caching */ |
53 | | |
54 | | /* values used during decoding/coding */ |
55 | | int tmp_ival; |
56 | | unsigned start; /* the start of the DER sequence - if decoded */ |
57 | | unsigned end; /* the end of the DER sequence - if decoded */ |
58 | | }; |
59 | | |
60 | | typedef struct tag_and_class_st |
61 | | { |
62 | | unsigned tag; |
63 | | unsigned class; |
64 | | const char *desc; |
65 | | } tag_and_class_st; |
66 | | |
67 | | /* the types that are handled in _asn1_tags */ |
68 | | # define CASE_HANDLED_ETYPES \ |
69 | 0 | case ASN1_ETYPE_NULL: \ |
70 | 0 | case ASN1_ETYPE_BOOLEAN: \ |
71 | 0 | case ASN1_ETYPE_INTEGER: \ |
72 | 0 | case ASN1_ETYPE_ENUMERATED: \ |
73 | 564 | case ASN1_ETYPE_OBJECT_ID: \ |
74 | 564 | case ASN1_ETYPE_OCTET_STRING: \ |
75 | 564 | case ASN1_ETYPE_GENERALSTRING: \ |
76 | 564 | case ASN1_ETYPE_NUMERIC_STRING: \ |
77 | 564 | case ASN1_ETYPE_IA5_STRING: \ |
78 | 564 | case ASN1_ETYPE_TELETEX_STRING: \ |
79 | 564 | case ASN1_ETYPE_PRINTABLE_STRING: \ |
80 | 564 | case ASN1_ETYPE_UNIVERSAL_STRING: \ |
81 | 564 | case ASN1_ETYPE_BMP_STRING: \ |
82 | 564 | case ASN1_ETYPE_UTF8_STRING: \ |
83 | 564 | case ASN1_ETYPE_VISIBLE_STRING: \ |
84 | 564 | case ASN1_ETYPE_BIT_STRING: \ |
85 | 846 | case ASN1_ETYPE_SEQUENCE: \ |
86 | 846 | case ASN1_ETYPE_SEQUENCE_OF: \ |
87 | 846 | case ASN1_ETYPE_SET: \ |
88 | 846 | case ASN1_ETYPE_UTC_TIME: \ |
89 | 846 | case ASN1_ETYPE_GENERALIZED_TIME: \ |
90 | 846 | case ASN1_ETYPE_SET_OF |
91 | | |
92 | 0 | # define ETYPE_TAG(etype) (_asn1_tags[etype].tag) |
93 | 0 | # define ETYPE_CLASS(etype) (_asn1_tags[etype].class) |
94 | 0 | # define ETYPE_OK(etype) (((etype) != ASN1_ETYPE_INVALID && \ |
95 | 0 | (etype) < _asn1_tags_size && \ |
96 | 0 | _asn1_tags[(etype)].desc != NULL)?1:0) |
97 | | |
98 | 0 | # define ETYPE_IS_STRING(etype) ((etype == ASN1_ETYPE_GENERALSTRING || \ |
99 | 0 | etype == ASN1_ETYPE_NUMERIC_STRING || etype == ASN1_ETYPE_IA5_STRING || \ |
100 | 0 | etype == ASN1_ETYPE_TELETEX_STRING || etype == ASN1_ETYPE_PRINTABLE_STRING || \ |
101 | 0 | etype == ASN1_ETYPE_UNIVERSAL_STRING || etype == ASN1_ETYPE_BMP_STRING || \ |
102 | 0 | etype == ASN1_ETYPE_UTF8_STRING || etype == ASN1_ETYPE_VISIBLE_STRING || \ |
103 | 0 | etype == ASN1_ETYPE_OCTET_STRING)?1:0) |
104 | | |
105 | | extern unsigned int _asn1_tags_size; |
106 | | extern const tag_and_class_st _asn1_tags[]; |
107 | | |
108 | 24.8k | # define _asn1_strlen(s) strlen((const char *) s) |
109 | 0 | # define _asn1_strtol(n,e,b) strtol((const char *) n, e, b) |
110 | 0 | # define _asn1_strtoul(n,e,b) strtoul((const char *) n, e, b) |
111 | 0 | # define _asn1_strcmp(a,b) strcmp((const char *)a, (const char *)b) |
112 | 0 | # define _asn1_strcpy(a,b) strcpy((char *)a, (const char *)b) |
113 | 0 | # define _asn1_strcat(a,b) strcat((char *)a, (const char *)b) |
114 | | |
115 | | # if SIZEOF_UNSIGNED_LONG_INT == 8 |
116 | 8.98k | # define _asn1_strtou64(n,e,b) strtoul((const char *) n, e, b) |
117 | | # else |
118 | | # define _asn1_strtou64(n,e,b) strtoull((const char *) n, e, b) |
119 | | # endif |
120 | | |
121 | | # define MAX_LOG_SIZE 1024 /* maximum number of characters of a log message */ |
122 | | |
123 | | /* Define used for visiting trees. */ |
124 | 85.0k | # define UP 1 |
125 | 47.9k | # define RIGHT 2 |
126 | 61.3k | # define DOWN 3 |
127 | | |
128 | | /***********************************************************************/ |
129 | | /* List of constants to better specify the type of typedef asn1_node_st. */ |
130 | | /***********************************************************************/ |
131 | | /* Used with TYPE_TAG */ |
132 | 0 | # define CONST_UNIVERSAL (1U<<8) |
133 | 0 | # define CONST_PRIVATE (1U<<9) |
134 | 0 | # define CONST_APPLICATION (1U<<10) |
135 | 0 | # define CONST_EXPLICIT (1U<<11) |
136 | 0 | # define CONST_IMPLICIT (1U<<12) |
137 | | |
138 | 6.71k | # define CONST_TAG (1U<<13) /* Used in ASN.1 assignment */ |
139 | 16.7k | # define CONST_OPTION (1U<<14) |
140 | 14.1k | # define CONST_DEFAULT (1U<<15) |
141 | 0 | # define CONST_TRUE (1U<<16) |
142 | 0 | # define CONST_FALSE (1U<<17) |
143 | | |
144 | 0 | # define CONST_LIST (1U<<18) /* Used with TYPE_INTEGER and TYPE_BIT_STRING */ |
145 | 0 | # define CONST_MIN_MAX (1U<<19) |
146 | | |
147 | 0 | # define CONST_1_PARAM (1U<<20) |
148 | | |
149 | 0 | # define CONST_SIZE (1U<<21) |
150 | | |
151 | 0 | # define CONST_DEFINED_BY (1U<<22) |
152 | | |
153 | | /* Those two are deprecated and used for backwards compatibility */ |
154 | 0 | # define CONST_GENERALIZED (1U<<23) |
155 | 0 | # define CONST_UTC (1U<<24) |
156 | | |
157 | | /* #define CONST_IMPORTS (1U<<25) */ |
158 | | |
159 | 646 | # define CONST_NOT_USED (1U<<26) |
160 | 6.68k | # define CONST_SET (1U<<27) |
161 | 48 | # define CONST_ASSIGN (1U<<28) |
162 | | |
163 | 180 | # define CONST_DOWN (1U<<29) |
164 | 113 | # define CONST_RIGHT (1U<<30) |
165 | | |
166 | | |
167 | 90 | # define ASN1_ETYPE_TIME 17 |
168 | | /****************************************/ |
169 | | /* Returns the first 8 bits. */ |
170 | | /* Used with the field type of asn1_node_st */ |
171 | | /****************************************/ |
172 | | inline static unsigned int |
173 | | type_field (unsigned int ntype) |
174 | 44.6k | { |
175 | 44.6k | return (ntype & 0xff); |
176 | 44.6k | } Line | Count | Source | 174 | 2.50k | { | 175 | 2.50k | return (ntype & 0xff); | 176 | 2.50k | } |
Line | Count | Source | 174 | 19.9k | { | 175 | 19.9k | return (ntype & 0xff); | 176 | 19.9k | } |
Line | Count | Source | 174 | 1.27k | { | 175 | 1.27k | return (ntype & 0xff); | 176 | 1.27k | } |
Unexecuted instantiation: gstr.c:type_field Line | Count | Source | 174 | 455 | { | 175 | 455 | return (ntype & 0xff); | 176 | 455 | } |
Line | Count | Source | 174 | 20.4k | { | 175 | 20.4k | return (ntype & 0xff); | 176 | 20.4k | } |
|
177 | | |
178 | | /* To convert old types from a static structure */ |
179 | | inline static unsigned int |
180 | | convert_old_type (unsigned int ntype) |
181 | 90 | { |
182 | 90 | unsigned int type = ntype & 0xff; |
183 | 90 | if (type == ASN1_ETYPE_TIME) |
184 | 0 | { |
185 | 0 | if (ntype & CONST_UTC) |
186 | 0 | type = ASN1_ETYPE_UTC_TIME; |
187 | 0 | else |
188 | 0 | type = ASN1_ETYPE_GENERALIZED_TIME; |
189 | |
|
190 | 0 | ntype &= ~(CONST_UTC | CONST_GENERALIZED); |
191 | 0 | ntype &= 0xffffff00; |
192 | 0 | ntype |= type; |
193 | |
|
194 | 0 | return ntype; |
195 | 0 | } |
196 | 90 | else |
197 | 90 | return ntype; |
198 | 90 | } Unexecuted instantiation: coding.c:convert_old_type Unexecuted instantiation: decoding.c:convert_old_type Unexecuted instantiation: element.c:convert_old_type Unexecuted instantiation: gstr.c:convert_old_type Unexecuted instantiation: parser_aux.c:convert_old_type structure.c:convert_old_type Line | Count | Source | 181 | 90 | { | 182 | 90 | unsigned int type = ntype & 0xff; | 183 | 90 | if (type == ASN1_ETYPE_TIME) | 184 | 0 | { | 185 | 0 | if (ntype & CONST_UTC) | 186 | 0 | type = ASN1_ETYPE_UTC_TIME; | 187 | 0 | else | 188 | 0 | type = ASN1_ETYPE_GENERALIZED_TIME; | 189 | |
| 190 | 0 | ntype &= ~(CONST_UTC | CONST_GENERALIZED); | 191 | 0 | ntype &= 0xffffff00; | 192 | 0 | ntype |= type; | 193 | |
| 194 | 0 | return ntype; | 195 | 0 | } | 196 | 90 | else | 197 | 90 | return ntype; | 198 | 90 | } |
|
199 | | |
200 | | static inline void * |
201 | | _asn1_realloc (void *ptr, size_t size) |
202 | 0 | { |
203 | 0 | void *ret; |
204 | |
|
205 | 0 | if (size == 0) |
206 | 0 | return ptr; |
207 | | |
208 | 0 | ret = realloc (ptr, size); |
209 | 0 | if (ret == NULL) |
210 | 0 | { |
211 | 0 | free (ptr); |
212 | 0 | } |
213 | 0 | return ret; |
214 | 0 | } Unexecuted instantiation: coding.c:_asn1_realloc Unexecuted instantiation: decoding.c:_asn1_realloc Unexecuted instantiation: element.c:_asn1_realloc Unexecuted instantiation: gstr.c:_asn1_realloc Unexecuted instantiation: parser_aux.c:_asn1_realloc Unexecuted instantiation: structure.c:_asn1_realloc |
215 | | |
216 | | #endif /* INT_H */ |