1/* $OpenBSD: a_bitstr.c,v 1.42 2023/12/25 22:02:59 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
71const ASN1_ITEM ASN1_BIT_STRING_it = {
72	.itype = ASN1_ITYPE_PRIMITIVE,
73	.utype = V_ASN1_BIT_STRING,
74	.sname = "ASN1_BIT_STRING",
75};
76
77ASN1_BIT_STRING *
78ASN1_BIT_STRING_new(void)
79{
80	return (ASN1_BIT_STRING *)ASN1_item_new(&ASN1_BIT_STRING_it);
81}
82LCRYPTO_ALIAS(ASN1_BIT_STRING_new);
83
84void
85ASN1_BIT_STRING_free(ASN1_BIT_STRING *a)
86{
87	ASN1_item_free((ASN1_VALUE *)a, &ASN1_BIT_STRING_it);
88}
89LCRYPTO_ALIAS(ASN1_BIT_STRING_free);
90
91static void
92asn1_abs_clear_unused_bits(ASN1_BIT_STRING *abs)
93{
94	abs->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
95}
96
97int
98asn1_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
110int
111ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
112{
113	return ASN1_STRING_set(x, d, len);
114}
115LCRYPTO_ALIAS(ASN1_BIT_STRING_set);
116
117int
118ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
119{
120	int w, v, iv;
121	unsigned char *c;
122
123	if (a == NULL)
124		return 0;
125	if (n < 0)
126		return 0;
127
128	w = n / 8;
129	v = 1 << (7 - (n & 0x07));
130	iv = ~v;
131
132	if (value == 0)
133		v = 0;
134
135	asn1_abs_clear_unused_bits(a);
136
137	if (a->length < w + 1 || a->data == NULL) {
138		/* Don't expand if there's no bit to set. */
139		if (value == 0)
140			return 1;
141		if ((c = recallocarray(a->data, a->length, w + 1, 1)) == NULL) {
142			ASN1error(ERR_R_MALLOC_FAILURE);
143			return 0;
144		}
145		a->data = c;
146		a->length = w + 1;
147	}
148
149	a->data[w] = ((a->data[w]) & iv) | v;
150	while (a->length > 0 && a->data[a->length - 1] == 0)
151		a->length--;
152
153	return 1;
154}
155LCRYPTO_ALIAS(ASN1_BIT_STRING_set_bit);
156
157int
158ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
159{
160	int w, v;
161
162	if (a == NULL)
163		return 0;
164	if (n < 0)
165		return 0;
166
167	w = n / 8;
168	v = 1 << (7 - (n & 0x07));
169
170	if (a->length < w + 1 || a->data == NULL)
171		return 0;
172
173	return (a->data[w] & v) != 0;
174}
175LCRYPTO_ALIAS(ASN1_BIT_STRING_get_bit);
176
177int
178i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
179{
180	int ret, j, bits, len;
181	unsigned char *p, *d;
182
183	if (a == NULL)
184		return (0);
185
186	if (a->length == INT_MAX)
187		return (0);
188
189	ret = a->length + 1;
190
191	if (pp == NULL)
192		return (ret);
193
194	len = a->length;
195
196	if (len > 0) {
197		if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
198			bits = (int)a->flags & 0x07;
199		} else {
200			j = 0;
201			for (; len > 0; len--) {
202				if (a->data[len - 1])
203					break;
204			}
205			if (len > 0)
206				j = a->data[len - 1];
207			if (j & 0x01)
208				bits = 0;
209			else if (j & 0x02)
210				bits = 1;
211			else if (j & 0x04)
212				bits = 2;
213			else if (j & 0x08)
214				bits = 3;
215			else if (j & 0x10)
216				bits = 4;
217			else if (j & 0x20)
218				bits = 5;
219			else if (j & 0x40)
220				bits = 6;
221			else if (j & 0x80)
222				bits = 7;
223			else
224				bits = 0; /* should not happen */
225		}
226	} else
227		bits = 0;
228
229	p= *pp;
230
231	*(p++) = (unsigned char)bits;
232	d = a->data;
233	if (len > 0) {
234		memcpy(p, d, len);
235		p += len;
236		p[-1] &= 0xff << bits;
237	}
238	*pp = p;
239	return (ret);
240}
241
242int
243c2i_ASN1_BIT_STRING_cbs(ASN1_BIT_STRING **out_abs, CBS *cbs)
244{
245	ASN1_BIT_STRING *abs = NULL;
246	uint8_t *data = NULL;
247	size_t data_len = 0;
248	uint8_t unused_bits;
249	int ret = 0;
250
251	if (out_abs == NULL)
252		goto err;
253
254	if (*out_abs != NULL) {
255		ASN1_BIT_STRING_free(*out_abs);
256		*out_abs = NULL;
257	}
258
259	if (!CBS_get_u8(cbs, &unused_bits)) {
260		ASN1error(ASN1_R_STRING_TOO_SHORT);
261		goto err;
262	}
263
264	if (!CBS_stow(cbs, &data, &data_len))
265		goto err;
266	if (data_len > INT_MAX)
267		goto err;
268
269	if ((abs = ASN1_BIT_STRING_new()) == NULL)
270		goto err;
271
272	abs->data = data;
273	abs->length = (int)data_len;
274	data = NULL;
275
276	/*
277	 * We do this to preserve the settings. If we modify the settings,
278	 * via the _set_bit function, we will recalculate on output.
279	 */
280	if (!asn1_abs_set_unused_bits(abs, unused_bits)) {
281		ASN1error(ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
282		goto err;
283	}
284	if (abs->length > 0)
285		abs->data[abs->length - 1] &= 0xff << unused_bits;
286
287	*out_abs = abs;
288	abs = NULL;
289
290	ret = 1;
291
292 err:
293	ASN1_BIT_STRING_free(abs);
294	freezero(data, data_len);
295
296	return ret;
297}
298
299ASN1_BIT_STRING *
300c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out_abs, const unsigned char **pp, long len)
301{
302	ASN1_BIT_STRING *abs = NULL;
303	CBS content;
304
305	if (out_abs != NULL) {
306		ASN1_BIT_STRING_free(*out_abs);
307		*out_abs = NULL;
308	}
309
310	if (len < 0) {
311		ASN1error(ASN1_R_LENGTH_ERROR);
312		return NULL;
313	}
314
315	CBS_init(&content, *pp, len);
316
317	if (!c2i_ASN1_BIT_STRING_cbs(&abs, &content))
318		return NULL;
319
320	*pp = CBS_data(&content);
321
322	if (out_abs != NULL)
323		*out_abs = abs;
324
325	return abs;
326}
327
328int
329i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **out)
330{
331	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_BIT_STRING_it);
332}
333LCRYPTO_ALIAS(i2d_ASN1_BIT_STRING);
334
335ASN1_BIT_STRING *
336d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **in, long len)
337{
338	return (ASN1_BIT_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
339	    &ASN1_BIT_STRING_it);
340}
341LCRYPTO_ALIAS(d2i_ASN1_BIT_STRING);
342