xref: /openbsd-src/lib/libcrypto/asn1/a_enum.c (revision 365421c136ad5d375d027b8beea9a8a273cedbb9)
1 /* $OpenBSD: a_enum.c,v 1.24 2022/06/25 16:15:18 jsing 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 {
85 	return (ASN1_ENUMERATED *)ASN1_item_new(&ASN1_ENUMERATED_it);
86 }
87 
88 static void
89 asn1_aenum_clear(ASN1_ENUMERATED *aenum)
90 {
91 	freezero(aenum->data, aenum->length);
92 
93 	memset(aenum, 0, sizeof(*aenum));
94 
95 	aenum->type = V_ASN1_ENUMERATED;
96 }
97 
98 void
99 ASN1_ENUMERATED_free(ASN1_ENUMERATED *a)
100 {
101 	ASN1_item_free((ASN1_VALUE *)a, &ASN1_ENUMERATED_it);
102 }
103 
104 int
105 ASN1_ENUMERATED_get_int64(int64_t *out_val, const ASN1_ENUMERATED *aenum)
106 {
107 	CBS cbs;
108 
109 	*out_val = 0;
110 
111 	if (aenum == NULL || aenum->length < 0)
112 		return 0;
113 
114 	if (aenum->type != V_ASN1_ENUMERATED &&
115 	    aenum->type != V_ASN1_NEG_ENUMERATED) {
116 		ASN1error(ASN1_R_WRONG_INTEGER_TYPE);
117 		return 0;
118 	}
119 
120 	CBS_init(&cbs, aenum->data, aenum->length);
121 
122 	return asn1_aint_get_int64(&cbs, (aenum->type == V_ASN1_NEG_ENUMERATED),
123 	    out_val);
124 }
125 
126 int
127 ASN1_ENUMERATED_set_int64(ASN1_ENUMERATED *aenum, int64_t val)
128 {
129 	asn1_aenum_clear(aenum);
130 
131 	if (val < 0) {
132 		aenum->type = V_ASN1_NEG_ENUMERATED;
133 		val = -val;
134 	}
135 
136 	return asn1_aint_set_uint64((uint64_t)val, &aenum->data, &aenum->length);
137 }
138 
139 long
140 ASN1_ENUMERATED_get(const ASN1_ENUMERATED *aenum)
141 {
142 	int64_t val;
143 
144 	if (!ASN1_ENUMERATED_get_int64(&val, aenum))
145 		return -1;
146 	if (val < LONG_MIN || val > LONG_MAX) {
147 		/* hmm... a bit ugly, return all ones */
148 		return -1;
149 	}
150 
151 	return (long)val;
152 }
153 
154 int
155 ASN1_ENUMERATED_set(ASN1_ENUMERATED *aenum, long val)
156 {
157 	return ASN1_ENUMERATED_set_int64(aenum, val);
158 }
159 
160 ASN1_ENUMERATED *
161 BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
162 {
163 	ASN1_ENUMERATED *ret;
164 	int len, j;
165 
166 	if (ai == NULL)
167 		ret = ASN1_ENUMERATED_new();
168 	else
169 		ret = ai;
170 	if (ret == NULL) {
171 		ASN1error(ERR_R_NESTED_ASN1_ERROR);
172 		goto err;
173 	}
174 	if (BN_is_negative(bn))
175 		ret->type = V_ASN1_NEG_ENUMERATED;
176 	else
177 		ret->type = V_ASN1_ENUMERATED;
178 	j = BN_num_bits(bn);
179 	len = ((j == 0) ? 0 : ((j / 8) + 1));
180 	if (ret->length < len + 4) {
181 		unsigned char *new_data = realloc(ret->data, len + 4);
182 		if (!new_data) {
183 			ASN1error(ERR_R_MALLOC_FAILURE);
184 			goto err;
185 		}
186 		ret->data = new_data;
187 	}
188 	ret->length = BN_bn2bin(bn, ret->data);
189 
190 	/* Correct zero case */
191 	if (!ret->length) {
192 		ret->data[0] = 0;
193 		ret->length = 1;
194 	}
195 	return (ret);
196 
197  err:
198 	if (ret != ai)
199 		ASN1_ENUMERATED_free(ret);
200 	return (NULL);
201 }
202 
203 BIGNUM *
204 ASN1_ENUMERATED_to_BN(const ASN1_ENUMERATED *ai, BIGNUM *bn)
205 {
206 	BIGNUM *ret;
207 
208 	if ((ret = BN_bin2bn(ai->data, ai->length, bn)) == NULL)
209 		ASN1error(ASN1_R_BN_LIB);
210 	else if (ai->type == V_ASN1_NEG_ENUMERATED)
211 		BN_set_negative(ret, 1);
212 	return (ret);
213 }
214 
215 /* Based on a_int.c: equivalent ENUMERATED functions */
216 
217 int
218 i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
219 {
220 	int i, n = 0;
221 	static const char h[] = "0123456789ABCDEF";
222 	char buf[2];
223 
224 	if (a == NULL)
225 		return (0);
226 
227 	if (a->length == 0) {
228 		if (BIO_write(bp, "00", 2) != 2)
229 			goto err;
230 		n = 2;
231 	} else {
232 		for (i = 0; i < a->length; i++) {
233 			if ((i != 0) && (i % 35 == 0)) {
234 				if (BIO_write(bp, "\\\n", 2) != 2)
235 					goto err;
236 				n += 2;
237 			}
238 			buf[0] = h[((unsigned char)a->data[i] >> 4) & 0x0f];
239 			buf[1] = h[((unsigned char)a->data[i]) & 0x0f];
240 			if (BIO_write(bp, buf, 2) != 2)
241 				goto err;
242 			n += 2;
243 		}
244 	}
245 	return (n);
246 
247  err:
248 	return (-1);
249 }
250 
251 int
252 a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
253 {
254 	int ret = 0;
255 	int i, j,k, m,n, again, bufsize;
256 	unsigned char *s = NULL, *sp;
257 	unsigned char *bufp;
258 	int first = 1;
259 	size_t num = 0, slen = 0;
260 
261 	bs->type = V_ASN1_ENUMERATED;
262 
263 	bufsize = BIO_gets(bp, buf, size);
264 	for (;;) {
265 		if (bufsize < 1)
266 			goto err_sl;
267 		i = bufsize;
268 		if (buf[i-1] == '\n')
269 			buf[--i] = '\0';
270 		if (i == 0)
271 			goto err_sl;
272 		if (buf[i-1] == '\r')
273 			buf[--i] = '\0';
274 		if (i == 0)
275 			goto err_sl;
276 		if (buf[i - 1] == '\\') {
277 			i--;
278 			again = 1;
279 		} else
280 			again = 0;
281 		buf[i] = '\0';
282 		if (i < 2)
283 			goto err_sl;
284 
285 		bufp = (unsigned char *)buf;
286 		if (first) {
287 			first = 0;
288 			if ((bufp[0] == '0') && (buf[1] == '0')) {
289 				bufp += 2;
290 				i -= 2;
291 			}
292 		}
293 		k = 0;
294 		if (i % 2 != 0) {
295 			ASN1error(ASN1_R_ODD_NUMBER_OF_CHARS);
296 			goto err;
297 		}
298 		i /= 2;
299 		if (num + i > slen) {
300 			sp = realloc(s, num + i);
301 			if (sp == NULL) {
302 				ASN1error(ERR_R_MALLOC_FAILURE);
303 				goto err;
304 			}
305 			s = sp;
306 			slen = num + i;
307 		}
308 		for (j = 0; j < i; j++, k += 2) {
309 			for (n = 0; n < 2; n++) {
310 				m = bufp[k + n];
311 				if ((m >= '0') && (m <= '9'))
312 					m -= '0';
313 				else if ((m >= 'a') && (m <= 'f'))
314 					m = m - 'a' + 10;
315 				else if ((m >= 'A') && (m <= 'F'))
316 					m = m - 'A' + 10;
317 				else {
318 					ASN1error(ASN1_R_NON_HEX_CHARACTERS);
319 					goto err;
320 				}
321 				s[num + j] <<= 4;
322 				s[num + j] |= m;
323 			}
324 		}
325 		num += i;
326 		if (again)
327 			bufsize = BIO_gets(bp, buf, size);
328 		else
329 			break;
330 	}
331 	bs->length = num;
332 	bs->data = s;
333 	return (1);
334 
335  err_sl:
336 	ASN1error(ASN1_R_SHORT_LINE);
337  err:
338 	free(s);
339 	return (ret);
340 }
341 
342 int
343 i2d_ASN1_ENUMERATED(ASN1_ENUMERATED *a, unsigned char **out)
344 {
345 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_ENUMERATED_it);
346 }
347 
348 ASN1_ENUMERATED *
349 d2i_ASN1_ENUMERATED(ASN1_ENUMERATED **a, const unsigned char **in, long len)
350 {
351 	return (ASN1_ENUMERATED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
352 	    &ASN1_ENUMERATED_it);
353 }
354