xref: /openbsd-src/lib/libcrypto/asn1/a_bitstr.c (revision 1411e9ef2a4ec93e44d8b2cd207dd80b45d88efe)
1 /* $OpenBSD: a_bitstr.c,v 1.40 2023/07/28 10:30:16 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 <stdio.h>
61 #include <string.h>
62 
63 #include <openssl/asn1.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/conf.h>
66 #include <openssl/err.h>
67 #include <openssl/x509v3.h>
68 
69 #include "bytestring.h"
70 
71 const ASN1_ITEM ASN1_BIT_STRING_it = {
72 	.itype = ASN1_ITYPE_PRIMITIVE,
73 	.utype = V_ASN1_BIT_STRING,
74 	.sname = "ASN1_BIT_STRING",
75 };
76 
77 ASN1_BIT_STRING *
78 ASN1_BIT_STRING_new(void)
79 {
80 	return (ASN1_BIT_STRING *)ASN1_item_new(&ASN1_BIT_STRING_it);
81 }
82 LCRYPTO_ALIAS(ASN1_BIT_STRING_new);
83 
84 void
85 ASN1_BIT_STRING_free(ASN1_BIT_STRING *a)
86 {
87 	ASN1_item_free((ASN1_VALUE *)a, &ASN1_BIT_STRING_it);
88 }
89 LCRYPTO_ALIAS(ASN1_BIT_STRING_free);
90 
91 static void
92 asn1_abs_clear_unused_bits(ASN1_BIT_STRING *abs)
93 {
94 	abs->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
95 }
96 
97 int
98 asn1_abs_set_unused_bits(ASN1_BIT_STRING *abs, uint8_t unused_bits)
99 {
100 	if (unused_bits > 7)
101 		return 0;
102 
103 	asn1_abs_clear_unused_bits(abs);
104 
105 	abs->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
106 
107 	return 1;
108 }
109 
110 int
111 ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
112 {
113 	return ASN1_STRING_set(x, d, len);
114 }
115 LCRYPTO_ALIAS(ASN1_BIT_STRING_set);
116 
117 int
118 ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
119 {
120 	int w, v, iv;
121 	unsigned char *c;
122 
123 	w = n/8;
124 	v = 1 << (7 - (n & 0x07));
125 	iv = ~v;
126 	if (!value)
127 		v = 0;
128 
129 	if (a == NULL)
130 		return 0;
131 
132 	asn1_abs_clear_unused_bits(a);
133 
134 	if ((a->length < (w + 1)) || (a->data == NULL)) {
135 		if (!value)
136 			return(1); /* Don't need to set */
137 		if ((c = recallocarray(a->data, a->length, w + 1, 1)) == NULL) {
138 			ASN1error(ERR_R_MALLOC_FAILURE);
139 			return 0;
140 		}
141 		a->data = c;
142 		a->length = w + 1;
143 	}
144 	a->data[w] = ((a->data[w]) & iv) | v;
145 	while ((a->length > 0) && (a->data[a->length - 1] == 0))
146 		a->length--;
147 
148 	return (1);
149 }
150 LCRYPTO_ALIAS(ASN1_BIT_STRING_set_bit);
151 
152 int
153 ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
154 {
155 	int w, v;
156 
157 	w = n / 8;
158 	v = 1 << (7 - (n & 0x07));
159 	if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
160 		return (0);
161 	return ((a->data[w] & v) != 0);
162 }
163 LCRYPTO_ALIAS(ASN1_BIT_STRING_get_bit);
164 
165 int
166 ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs,
167     BIT_STRING_BITNAME *tbl, int indent)
168 {
169 	BIT_STRING_BITNAME *bnam;
170 	char first = 1;
171 
172 	BIO_printf(out, "%*s", indent, "");
173 	for (bnam = tbl; bnam->lname; bnam++) {
174 		if (ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) {
175 			if (!first)
176 				BIO_puts(out, ", ");
177 			BIO_puts(out, bnam->lname);
178 			first = 0;
179 		}
180 	}
181 	BIO_puts(out, "\n");
182 	return 1;
183 }
184 LCRYPTO_ALIAS(ASN1_BIT_STRING_name_print);
185 
186 int
187 ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value,
188     BIT_STRING_BITNAME *tbl)
189 {
190 	int bitnum;
191 
192 	bitnum = ASN1_BIT_STRING_num_asc(name, tbl);
193 	if (bitnum < 0)
194 		return 0;
195 	if (bs) {
196 		if (!ASN1_BIT_STRING_set_bit(bs, bitnum, value))
197 			return 0;
198 	}
199 	return 1;
200 }
201 LCRYPTO_ALIAS(ASN1_BIT_STRING_set_asc);
202 
203 int
204 ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl)
205 {
206 	BIT_STRING_BITNAME *bnam;
207 
208 	for (bnam = tbl; bnam->lname; bnam++) {
209 		if (!strcmp(bnam->sname, name) ||
210 		    !strcmp(bnam->lname, name))
211 			return bnam->bitnum;
212 	}
213 	return -1;
214 }
215 LCRYPTO_ALIAS(ASN1_BIT_STRING_num_asc);
216 
217 int
218 i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
219 {
220 	int ret, j, bits, len;
221 	unsigned char *p, *d;
222 
223 	if (a == NULL)
224 		return (0);
225 
226 	if (a->length == INT_MAX)
227 		return (0);
228 
229 	ret = a->length + 1;
230 
231 	if (pp == NULL)
232 		return (ret);
233 
234 	len = a->length;
235 
236 	if (len > 0) {
237 		if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
238 			bits = (int)a->flags & 0x07;
239 		} else {
240 			j = 0;
241 			for (; len > 0; len--) {
242 				if (a->data[len - 1])
243 					break;
244 			}
245 			if (len > 0)
246 				j = a->data[len - 1];
247 			if (j & 0x01)
248 				bits = 0;
249 			else if (j & 0x02)
250 				bits = 1;
251 			else if (j & 0x04)
252 				bits = 2;
253 			else if (j & 0x08)
254 				bits = 3;
255 			else if (j & 0x10)
256 				bits = 4;
257 			else if (j & 0x20)
258 				bits = 5;
259 			else if (j & 0x40)
260 				bits = 6;
261 			else if (j & 0x80)
262 				bits = 7;
263 			else
264 				bits = 0; /* should not happen */
265 		}
266 	} else
267 		bits = 0;
268 
269 	p= *pp;
270 
271 	*(p++) = (unsigned char)bits;
272 	d = a->data;
273 	if (len > 0) {
274 		memcpy(p, d, len);
275 		p += len;
276 		p[-1] &= 0xff << bits;
277 	}
278 	*pp = p;
279 	return (ret);
280 }
281 
282 int
283 c2i_ASN1_BIT_STRING_cbs(ASN1_BIT_STRING **out_abs, CBS *cbs)
284 {
285 	ASN1_BIT_STRING *abs = NULL;
286 	uint8_t *data = NULL;
287 	size_t data_len = 0;
288 	uint8_t unused_bits;
289 	int ret = 0;
290 
291 	if (out_abs == NULL)
292 		goto err;
293 
294 	if (*out_abs != NULL) {
295 		ASN1_BIT_STRING_free(*out_abs);
296 		*out_abs = NULL;
297 	}
298 
299 	if (!CBS_get_u8(cbs, &unused_bits)) {
300 		ASN1error(ASN1_R_STRING_TOO_SHORT);
301 		goto err;
302 	}
303 
304 	if (!CBS_stow(cbs, &data, &data_len))
305 		goto err;
306 	if (data_len > INT_MAX)
307 		goto err;
308 
309 	if ((abs = ASN1_BIT_STRING_new()) == NULL)
310 		goto err;
311 
312 	abs->data = data;
313 	abs->length = (int)data_len;
314 	data = NULL;
315 
316 	/*
317 	 * We do this to preserve the settings. If we modify the settings,
318 	 * via the _set_bit function, we will recalculate on output.
319 	 */
320 	if (!asn1_abs_set_unused_bits(abs, unused_bits)) {
321 		ASN1error(ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
322 		goto err;
323 	}
324 	if (abs->length > 0)
325 		abs->data[abs->length - 1] &= 0xff << unused_bits;
326 
327 	*out_abs = abs;
328 	abs = NULL;
329 
330 	ret = 1;
331 
332  err:
333 	ASN1_BIT_STRING_free(abs);
334 	freezero(data, data_len);
335 
336 	return ret;
337 }
338 
339 ASN1_BIT_STRING *
340 c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out_abs, const unsigned char **pp, long len)
341 {
342 	ASN1_BIT_STRING *abs = NULL;
343 	CBS content;
344 
345 	if (out_abs != NULL) {
346 		ASN1_BIT_STRING_free(*out_abs);
347 		*out_abs = NULL;
348 	}
349 
350 	if (len < 0) {
351 		ASN1error(ASN1_R_LENGTH_ERROR);
352 		return NULL;
353 	}
354 
355 	CBS_init(&content, *pp, len);
356 
357 	if (!c2i_ASN1_BIT_STRING_cbs(&abs, &content))
358 		return NULL;
359 
360 	*pp = CBS_data(&content);
361 
362 	if (out_abs != NULL)
363 		*out_abs = abs;
364 
365 	return abs;
366 }
367 
368 int
369 i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **out)
370 {
371 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_BIT_STRING_it);
372 }
373 LCRYPTO_ALIAS(i2d_ASN1_BIT_STRING);
374 
375 ASN1_BIT_STRING *
376 d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **in, long len)
377 {
378 	return (ASN1_BIT_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
379 	    &ASN1_BIT_STRING_it);
380 }
381 LCRYPTO_ALIAS(d2i_ASN1_BIT_STRING);
382