/src/libressl/crypto/asn1/a_enum.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: a_enum.c,v 1.26 2022/08/10 12:06:28 tb Exp $ */ |
2 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | | * All rights reserved. |
4 | | * |
5 | | * This package is an SSL implementation written |
6 | | * by Eric Young (eay@cryptsoft.com). |
7 | | * The implementation was written so as to conform with Netscapes SSL. |
8 | | * |
9 | | * This library is free for commercial and non-commercial use as long as |
10 | | * the following conditions are aheared to. The following conditions |
11 | | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | | * included with this distribution is covered by the same copyright terms |
14 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | | * |
16 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | | * the code are not to be removed. |
18 | | * If this package is used in a product, Eric Young should be given attribution |
19 | | * as the author of the parts of the library used. |
20 | | * This can be in the form of a textual message at program startup or |
21 | | * in documentation (online or textual) provided with the package. |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions |
25 | | * are met: |
26 | | * 1. Redistributions of source code must retain the copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * 2. Redistributions in binary form must reproduce the above copyright |
29 | | * notice, this list of conditions and the following disclaimer in the |
30 | | * documentation and/or other materials provided with the distribution. |
31 | | * 3. All advertising materials mentioning features or use of this software |
32 | | * must display the following acknowledgement: |
33 | | * "This product includes cryptographic software written by |
34 | | * Eric Young (eay@cryptsoft.com)" |
35 | | * The word 'cryptographic' can be left out if the rouines from the library |
36 | | * being used are not cryptographic related :-). |
37 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | | * the apps directory (application code) you must include an acknowledgement: |
39 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | | * SUCH DAMAGE. |
52 | | * |
53 | | * The licence and distribution terms for any publically available version or |
54 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | | * copied and put under another distribution licence |
56 | | * [including the GNU Public Licence.] |
57 | | */ |
58 | | |
59 | | #include <limits.h> |
60 | | #include <string.h> |
61 | | |
62 | | #include <openssl/asn1.h> |
63 | | #include <openssl/asn1t.h> |
64 | | #include <openssl/bn.h> |
65 | | #include <openssl/buffer.h> |
66 | | #include <openssl/err.h> |
67 | | |
68 | | #include "asn1_locl.h" |
69 | | #include "bytestring.h" |
70 | | |
71 | | /* |
72 | | * Code for ENUMERATED type: identical to INTEGER apart from a different tag. |
73 | | * for comments on encoding see a_int.c |
74 | | */ |
75 | | |
76 | | const ASN1_ITEM ASN1_ENUMERATED_it = { |
77 | | .itype = ASN1_ITYPE_PRIMITIVE, |
78 | | .utype = V_ASN1_ENUMERATED, |
79 | | .sname = "ASN1_ENUMERATED", |
80 | | }; |
81 | | |
82 | | ASN1_ENUMERATED * |
83 | | ASN1_ENUMERATED_new(void) |
84 | 0 | { |
85 | 0 | return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it); |
86 | 0 | } |
87 | | |
88 | | static void |
89 | | asn1_aenum_clear(ASN1_ENUMERATED *aenum) |
90 | 0 | { |
91 | 0 | freezero(aenum->data, aenum->length); |
92 | |
|
93 | 0 | memset(aenum, 0, sizeof(*aenum)); |
94 | |
|
95 | 0 | aenum->type = V_ASN1_ENUMERATED; |
96 | 0 | } |
97 | | |
98 | | void |
99 | | ASN1_ENUMERATED_free(ASN1_ENUMERATED *a) |
100 | 9.74k | { |
101 | 9.74k | ASN1_item_free((ASN1_VALUE *)a, &ASN1_ENUMERATED_it); |
102 | 9.74k | } |
103 | | |
104 | | int |
105 | | ASN1_ENUMERATED_get_int64(int64_t *out_val, const ASN1_ENUMERATED *aenum) |
106 | 0 | { |
107 | 0 | CBS cbs; |
108 | |
|
109 | 0 | *out_val = 0; |
110 | |
|
111 | 0 | if (aenum == NULL || aenum->length < 0) |
112 | 0 | return 0; |
113 | | |
114 | 0 | if (aenum->type != V_ASN1_ENUMERATED && |
115 | 0 | aenum->type != V_ASN1_NEG_ENUMERATED) { |
116 | 0 | ASN1error(ASN1_R_WRONG_INTEGER_TYPE); |
117 | 0 | return 0; |
118 | 0 | } |
119 | | |
120 | 0 | CBS_init(&cbs, aenum->data, aenum->length); |
121 | |
|
122 | 0 | return asn1_aint_get_int64(&cbs, (aenum->type == V_ASN1_NEG_ENUMERATED), |
123 | 0 | out_val); |
124 | 0 | } |
125 | | |
126 | | int |
127 | | ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *aenum, int64_t val) |
128 | 0 | { |
129 | 0 | uint64_t uval; |
130 | |
|
131 | 0 | asn1_aenum_clear(aenum); |
132 | |
|
133 | 0 | uval = (uint64_t)val; |
134 | |
|
135 | 0 | if (val < 0) { |
136 | 0 | aenum->type = V_ASN1_NEG_ENUMERATED; |
137 | 0 | uval = -uval; |
138 | 0 | } |
139 | |
|
140 | 0 | return asn1_aint_set_uint64(uval, &aenum->data, &aenum->length); |
141 | 0 | } |
142 | | |
143 | | long |
144 | | ASN1_ENUMERATED_get(const ASN1_ENUMERATED *aenum) |
145 | 0 | { |
146 | 0 | int64_t val; |
147 | |
|
148 | 0 | if (aenum == NULL) |
149 | 0 | return 0; |
150 | 0 | if (!ASN1_ENUMERATED_get_int64(&val, aenum)) |
151 | 0 | return -1; |
152 | 0 | if (val < LONG_MIN || val > LONG_MAX) { |
153 | | /* hmm... a bit ugly, return all ones */ |
154 | 0 | return -1; |
155 | 0 | } |
156 | | |
157 | 0 | return (long)val; |
158 | 0 | } |
159 | | |
160 | | int |
161 | | ASN1_ENUMERATED_set(ASN1_ENUMERATED *aenum, long val) |
162 | 0 | { |
163 | 0 | return ASN1_ENUMERATED_set_int64(aenum, val); |
164 | 0 | } |
165 | | |
166 | | ASN1_ENUMERATED * |
167 | | BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai) |
168 | 0 | { |
169 | 0 | ASN1_ENUMERATED *ret; |
170 | 0 | int len, j; |
171 | |
|
172 | 0 | if (ai == NULL) |
173 | 0 | ret = ASN1_ENUMERATED_new(); |
174 | 0 | else |
175 | 0 | ret = ai; |
176 | 0 | if (ret == NULL) { |
177 | 0 | ASN1error(ERR_R_NESTED_ASN1_ERROR); |
178 | 0 | goto err; |
179 | 0 | } |
180 | 0 | if (BN_is_negative(bn)) |
181 | 0 | ret->type = V_ASN1_NEG_ENUMERATED; |
182 | 0 | else |
183 | 0 | ret->type = V_ASN1_ENUMERATED; |
184 | 0 | j = BN_num_bits(bn); |
185 | 0 | len = ((j == 0) ? 0 : ((j / 8) + 1)); |
186 | 0 | if (ret->length < len + 4) { |
187 | 0 | unsigned char *new_data = realloc(ret->data, len + 4); |
188 | 0 | if (!new_data) { |
189 | 0 | ASN1error(ERR_R_MALLOC_FAILURE); |
190 | 0 | goto err; |
191 | 0 | } |
192 | 0 | ret->data = new_data; |
193 | 0 | } |
194 | 0 | ret->length = BN_bn2bin(bn, ret->data); |
195 | | |
196 | | /* Correct zero case */ |
197 | 0 | if (!ret->length) { |
198 | 0 | ret->data[0] = 0; |
199 | 0 | ret->length = 1; |
200 | 0 | } |
201 | 0 | return (ret); |
202 | | |
203 | 0 | err: |
204 | 0 | if (ret != ai) |
205 | 0 | ASN1_ENUMERATED_free(ret); |
206 | 0 | return (NULL); |
207 | 0 | } |
208 | | |
209 | | BIGNUM * |
210 | | ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn) |
211 | 0 | { |
212 | 0 | BIGNUM *ret; |
213 | |
|
214 | 0 | if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL) |
215 | 0 | ASN1error(ASN1_R_BN_LIB); |
216 | 0 | else if (ai->type == V_ASN1_NEG_ENUMERATED) |
217 | 0 | BN_set_negative(ret, 1); |
218 | 0 | return (ret); |
219 | 0 | } |
220 | | |
221 | | /* Based on a_int.c: equivalent ENUMERATED functions */ |
222 | | |
223 | | int |
224 | | i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a) |
225 | 0 | { |
226 | 0 | int i, n = 0; |
227 | 0 | static const char h[] = "0123456789ABCDEF"; |
228 | 0 | char buf[2]; |
229 | |
|
230 | 0 | if (a == NULL) |
231 | 0 | return (0); |
232 | | |
233 | 0 | if (a->length == 0) { |
234 | 0 | if (BIO_write(bp, "00", 2) != 2) |
235 | 0 | goto err; |
236 | 0 | n = 2; |
237 | 0 | } else { |
238 | 0 | for (i = 0; i < a->length; i++) { |
239 | 0 | if ((i != 0) && (i % 35 == 0)) { |
240 | 0 | if (BIO_write(bp, "\\\n", 2) != 2) |
241 | 0 | goto err; |
242 | 0 | n += 2; |
243 | 0 | } |
244 | 0 | buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f]; |
245 | 0 | buf[1] = h[((unsigned char)a->data[i]) & 0x0f]; |
246 | 0 | if (BIO_write(bp, buf, 2) != 2) |
247 | 0 | goto err; |
248 | 0 | n += 2; |
249 | 0 | } |
250 | 0 | } |
251 | 0 | return (n); |
252 | | |
253 | 0 | err: |
254 | 0 | return (-1); |
255 | 0 | } |
256 | | |
257 | | int |
258 | | a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size) |
259 | 0 | { |
260 | 0 | int ret = 0; |
261 | 0 | int i, j,k, m,n, again, bufsize; |
262 | 0 | unsigned char *s = NULL, *sp; |
263 | 0 | unsigned char *bufp; |
264 | 0 | int first = 1; |
265 | 0 | size_t num = 0, slen = 0; |
266 | |
|
267 | 0 | bs->type = V_ASN1_ENUMERATED; |
268 | |
|
269 | 0 | bufsize = BIO_gets(bp, buf, size); |
270 | 0 | for (;;) { |
271 | 0 | if (bufsize < 1) |
272 | 0 | goto err_sl; |
273 | 0 | i = bufsize; |
274 | 0 | if (buf[i-1] == '\n') |
275 | 0 | buf[--i] = '\0'; |
276 | 0 | if (i == 0) |
277 | 0 | goto err_sl; |
278 | 0 | if (buf[i-1] == '\r') |
279 | 0 | buf[--i] = '\0'; |
280 | 0 | if (i == 0) |
281 | 0 | goto err_sl; |
282 | 0 | if (buf[i - 1] == '\\') { |
283 | 0 | i--; |
284 | 0 | again = 1; |
285 | 0 | } else |
286 | 0 | again = 0; |
287 | 0 | buf[i] = '\0'; |
288 | 0 | if (i < 2) |
289 | 0 | goto err_sl; |
290 | | |
291 | 0 | bufp = (unsigned char *)buf; |
292 | 0 | if (first) { |
293 | 0 | first = 0; |
294 | 0 | if ((bufp[0] == '0') && (buf[1] == '0')) { |
295 | 0 | bufp += 2; |
296 | 0 | i -= 2; |
297 | 0 | } |
298 | 0 | } |
299 | 0 | k = 0; |
300 | 0 | if (i % 2 != 0) { |
301 | 0 | ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS); |
302 | 0 | goto err; |
303 | 0 | } |
304 | 0 | i /= 2; |
305 | 0 | if (num + i > slen) { |
306 | 0 | sp = realloc(s, num + i); |
307 | 0 | if (sp == NULL) { |
308 | 0 | ASN1error(ERR_R_MALLOC_FAILURE); |
309 | 0 | goto err; |
310 | 0 | } |
311 | 0 | s = sp; |
312 | 0 | slen = num + i; |
313 | 0 | } |
314 | 0 | for (j = 0; j < i; j++, k += 2) { |
315 | 0 | for (n = 0; n < 2; n++) { |
316 | 0 | m = bufp[k + n]; |
317 | 0 | if ((m >= '0') && (m <= '9')) |
318 | 0 | m -= '0'; |
319 | 0 | else if ((m >= 'a') && (m <= 'f')) |
320 | 0 | m = m - 'a' + 10; |
321 | 0 | else if ((m >= 'A') && (m <= 'F')) |
322 | 0 | m = m - 'A' + 10; |
323 | 0 | else { |
324 | 0 | ASN1error(ASN1_R_NON_HEX_CHARACTERS); |
325 | 0 | goto err; |
326 | 0 | } |
327 | 0 | s[num + j] <<= 4; |
328 | 0 | s[num + j] |= m; |
329 | 0 | } |
330 | 0 | } |
331 | 0 | num += i; |
332 | 0 | if (again) |
333 | 0 | bufsize = BIO_gets(bp, buf, size); |
334 | 0 | else |
335 | 0 | break; |
336 | 0 | } |
337 | 0 | bs->length = num; |
338 | 0 | bs->data = s; |
339 | 0 | return (1); |
340 | | |
341 | 0 | err_sl: |
342 | 0 | ASN1error(ASN1_R_SHORT_LINE); |
343 | 0 | err: |
344 | 0 | free(s); |
345 | 0 | return (ret); |
346 | 0 | } |
347 | | |
348 | | int |
349 | | i2d_ASN1_ENUMERATED(ASN1_ENUMERATED *a, unsigned char **out) |
350 | 0 | { |
351 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ENUMERATED_it); |
352 | 0 | } |
353 | | |
354 | | ASN1_ENUMERATED * |
355 | | d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **a, const unsigned char **in, long len) |
356 | 1.54k | { |
357 | 1.54k | return (ASN1_ENUMERATED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
358 | 1.54k | &ASN1_ENUMERATED_it); |
359 | 1.54k | } |