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