xref: /openbsd/lib/libcrypto/asn1/a_bitstr.c (revision d415bd75)
1 /* $OpenBSD: a_bitstr.c,v 1.41 2023/07/28 10:33:13 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 i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
167 {
168 	int ret, j, bits, len;
169 	unsigned char *p, *d;
170 
171 	if (a == NULL)
172 		return (0);
173 
174 	if (a->length == INT_MAX)
175 		return (0);
176 
177 	ret = a->length + 1;
178 
179 	if (pp == NULL)
180 		return (ret);
181 
182 	len = a->length;
183 
184 	if (len > 0) {
185 		if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
186 			bits = (int)a->flags & 0x07;
187 		} else {
188 			j = 0;
189 			for (; len > 0; len--) {
190 				if (a->data[len - 1])
191 					break;
192 			}
193 			if (len > 0)
194 				j = a->data[len - 1];
195 			if (j & 0x01)
196 				bits = 0;
197 			else if (j & 0x02)
198 				bits = 1;
199 			else if (j & 0x04)
200 				bits = 2;
201 			else if (j & 0x08)
202 				bits = 3;
203 			else if (j & 0x10)
204 				bits = 4;
205 			else if (j & 0x20)
206 				bits = 5;
207 			else if (j & 0x40)
208 				bits = 6;
209 			else if (j & 0x80)
210 				bits = 7;
211 			else
212 				bits = 0; /* should not happen */
213 		}
214 	} else
215 		bits = 0;
216 
217 	p= *pp;
218 
219 	*(p++) = (unsigned char)bits;
220 	d = a->data;
221 	if (len > 0) {
222 		memcpy(p, d, len);
223 		p += len;
224 		p[-1] &= 0xff << bits;
225 	}
226 	*pp = p;
227 	return (ret);
228 }
229 
230 int
231 c2i_ASN1_BIT_STRING_cbs(ASN1_BIT_STRING **out_abs, CBS *cbs)
232 {
233 	ASN1_BIT_STRING *abs = NULL;
234 	uint8_t *data = NULL;
235 	size_t data_len = 0;
236 	uint8_t unused_bits;
237 	int ret = 0;
238 
239 	if (out_abs == NULL)
240 		goto err;
241 
242 	if (*out_abs != NULL) {
243 		ASN1_BIT_STRING_free(*out_abs);
244 		*out_abs = NULL;
245 	}
246 
247 	if (!CBS_get_u8(cbs, &unused_bits)) {
248 		ASN1error(ASN1_R_STRING_TOO_SHORT);
249 		goto err;
250 	}
251 
252 	if (!CBS_stow(cbs, &data, &data_len))
253 		goto err;
254 	if (data_len > INT_MAX)
255 		goto err;
256 
257 	if ((abs = ASN1_BIT_STRING_new()) == NULL)
258 		goto err;
259 
260 	abs->data = data;
261 	abs->length = (int)data_len;
262 	data = NULL;
263 
264 	/*
265 	 * We do this to preserve the settings. If we modify the settings,
266 	 * via the _set_bit function, we will recalculate on output.
267 	 */
268 	if (!asn1_abs_set_unused_bits(abs, unused_bits)) {
269 		ASN1error(ASN1_R_INVALID_BIT_STRING_BITS_LEFT);
270 		goto err;
271 	}
272 	if (abs->length > 0)
273 		abs->data[abs->length - 1] &= 0xff << unused_bits;
274 
275 	*out_abs = abs;
276 	abs = NULL;
277 
278 	ret = 1;
279 
280  err:
281 	ASN1_BIT_STRING_free(abs);
282 	freezero(data, data_len);
283 
284 	return ret;
285 }
286 
287 ASN1_BIT_STRING *
288 c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **out_abs, const unsigned char **pp, long len)
289 {
290 	ASN1_BIT_STRING *abs = NULL;
291 	CBS content;
292 
293 	if (out_abs != NULL) {
294 		ASN1_BIT_STRING_free(*out_abs);
295 		*out_abs = NULL;
296 	}
297 
298 	if (len < 0) {
299 		ASN1error(ASN1_R_LENGTH_ERROR);
300 		return NULL;
301 	}
302 
303 	CBS_init(&content, *pp, len);
304 
305 	if (!c2i_ASN1_BIT_STRING_cbs(&abs, &content))
306 		return NULL;
307 
308 	*pp = CBS_data(&content);
309 
310 	if (out_abs != NULL)
311 		*out_abs = abs;
312 
313 	return abs;
314 }
315 
316 int
317 i2d_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **out)
318 {
319 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &ASN1_BIT_STRING_it);
320 }
321 LCRYPTO_ALIAS(i2d_ASN1_BIT_STRING);
322 
323 ASN1_BIT_STRING *
324 d2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **in, long len)
325 {
326 	return (ASN1_BIT_STRING *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
327 	    &ASN1_BIT_STRING_it);
328 }
329 LCRYPTO_ALIAS(d2i_ASN1_BIT_STRING);
330