/src/openssl/crypto/asn1/a_bitstr.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <limits.h> |
11 | | #include <stdio.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/asn1.h> |
14 | | #include "asn1_local.h" |
15 | | |
16 | | #include <crypto/asn1.h> |
17 | | |
18 | | #ifndef OPENSSL_NO_DEPRECATED_4_1 |
19 | | int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len) |
20 | 0 | { |
21 | 0 | return ASN1_STRING_set(x, d, len); |
22 | 0 | } |
23 | | #endif |
24 | | |
25 | | int ossl_i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp) |
26 | 0 | { |
27 | 0 | int ret = 0, bits = 0, len; |
28 | 0 | unsigned char *p, *d; |
29 | |
|
30 | 0 | if (a == NULL) |
31 | 0 | goto err; |
32 | | |
33 | 0 | len = a->length; |
34 | |
|
35 | 0 | if (len > INT_MAX - 1) |
36 | 0 | goto err; |
37 | | |
38 | 0 | if ((len > 0) && (a->flags & ASN1_STRING_FLAG_BITS_LEFT)) |
39 | 0 | bits = (int)a->flags & 0x07; |
40 | |
|
41 | 0 | if (pp == NULL) |
42 | 0 | goto done; |
43 | | |
44 | 0 | p = *pp; |
45 | |
|
46 | 0 | *(p++) = (unsigned char)bits; |
47 | 0 | d = a->data; |
48 | 0 | if (len > 0) { |
49 | 0 | memcpy(p, d, len); |
50 | 0 | p += len; |
51 | 0 | p[-1] &= (0xff << bits); |
52 | 0 | } |
53 | 0 | *pp = p; |
54 | |
|
55 | 0 | done: |
56 | 0 | ret = len + 1; |
57 | |
|
58 | 0 | err: |
59 | 0 | return ret; |
60 | 0 | } |
61 | | |
62 | | ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, |
63 | | const unsigned char **pp, long len) |
64 | 0 | { |
65 | 0 | ASN1_BIT_STRING *ret = NULL; |
66 | 0 | const unsigned char *p; |
67 | 0 | unsigned char *s; |
68 | 0 | int i = 0; |
69 | |
|
70 | 0 | if (len < 1) { |
71 | 0 | i = ASN1_R_STRING_TOO_SHORT; |
72 | 0 | goto err; |
73 | 0 | } |
74 | | |
75 | 0 | if (len > INT_MAX) { |
76 | 0 | i = ASN1_R_STRING_TOO_LONG; |
77 | 0 | goto err; |
78 | 0 | } |
79 | | |
80 | 0 | if ((a == NULL) || ((*a) == NULL)) { |
81 | 0 | if ((ret = ASN1_BIT_STRING_new()) == NULL) |
82 | 0 | return NULL; |
83 | 0 | } else |
84 | 0 | ret = (*a); |
85 | | |
86 | 0 | p = *pp; |
87 | 0 | i = *(p++); |
88 | 0 | if (i > 7) { |
89 | 0 | i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT; |
90 | 0 | goto err; |
91 | 0 | } |
92 | | /* |
93 | | * We do this to preserve the settings. If we modify the settings, via |
94 | | * the _set_bit function, we will recalculate on output |
95 | | */ |
96 | 0 | ossl_asn1_bit_string_set_unused_bits(ret, i); |
97 | |
|
98 | 0 | if (len-- > 1) { /* using one because of the bits left byte */ |
99 | 0 | s = OPENSSL_malloc((int)len); |
100 | 0 | if (s == NULL) { |
101 | 0 | goto err; |
102 | 0 | } |
103 | 0 | memcpy(s, p, (int)len); |
104 | 0 | s[len - 1] &= (0xff << i); |
105 | 0 | p += len; |
106 | 0 | } else |
107 | 0 | s = NULL; |
108 | | |
109 | 0 | ASN1_STRING_set0(ret, s, (int)len); |
110 | 0 | ret->type = V_ASN1_BIT_STRING; |
111 | 0 | if (a != NULL) |
112 | 0 | (*a) = ret; |
113 | 0 | *pp = p; |
114 | 0 | return ret; |
115 | 0 | err: |
116 | 0 | if (i != 0) |
117 | 0 | ERR_raise(ERR_LIB_ASN1, i); |
118 | 0 | if ((a == NULL) || (*a != ret)) |
119 | 0 | ASN1_BIT_STRING_free(ret); |
120 | 0 | return NULL; |
121 | 0 | } |
122 | | |
123 | | /* |
124 | | * These next 2 functions from Goetz Babin-Ebell. |
125 | | */ |
126 | | int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) |
127 | 0 | { |
128 | 0 | int w, v, iv; |
129 | 0 | unsigned char *c; |
130 | |
|
131 | 0 | if (n < 0) |
132 | 0 | return 0; |
133 | | |
134 | 0 | w = n / 8; |
135 | 0 | v = 1 << (7 - (n & 0x07)); |
136 | 0 | iv = ~v; |
137 | 0 | if (!value) |
138 | 0 | v = 0; |
139 | |
|
140 | 0 | if (a == NULL) |
141 | 0 | return 0; |
142 | | |
143 | 0 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ |
144 | |
|
145 | 0 | if ((a->length < (w + 1)) || (a->data == NULL)) { |
146 | 0 | if (!value) |
147 | 0 | return 1; /* Don't need to set */ |
148 | 0 | c = OPENSSL_clear_realloc(a->data, a->length, w + 1); |
149 | 0 | if (c == NULL) |
150 | 0 | return 0; |
151 | 0 | if (w + 1 - a->length > 0) |
152 | 0 | memset(c + a->length, 0, w + 1 - a->length); |
153 | 0 | a->data = c; |
154 | 0 | a->length = w + 1; |
155 | 0 | } |
156 | 0 | a->data[w] = ((a->data[w]) & iv) | v; |
157 | |
|
158 | 0 | while ((a->length > 0) && (a->data[a->length - 1] == 0)) |
159 | 0 | a->length--; |
160 | |
|
161 | 0 | if (a->length > 0) { |
162 | 0 | uint8_t u8 = a->data[a->length - 1]; |
163 | 0 | uint8_t unused_bits = 7; |
164 | | |
165 | | /* Only keep least significant bit; count trailing zeroes. */ |
166 | 0 | u8 &= 0x100 - u8; |
167 | 0 | if ((u8 & 0x0f) != 0) |
168 | 0 | unused_bits -= 4; |
169 | 0 | if ((u8 & 0x33) != 0) |
170 | 0 | unused_bits -= 2; |
171 | 0 | if ((u8 & 0x55) != 0) |
172 | 0 | unused_bits -= 1; |
173 | 0 | ossl_asn1_bit_string_set_unused_bits(a, unused_bits); |
174 | 0 | } |
175 | 0 | return 1; |
176 | 0 | } |
177 | | |
178 | | int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) |
179 | 0 | { |
180 | 0 | int w, v; |
181 | |
|
182 | 0 | if (n < 0) |
183 | 0 | return 0; |
184 | | |
185 | 0 | w = n / 8; |
186 | 0 | v = 1 << (7 - (n & 0x07)); |
187 | 0 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) |
188 | 0 | return 0; |
189 | 0 | return ((a->data[w] & v) != 0); |
190 | 0 | } |
191 | | |
192 | | /* |
193 | | * Checks if the given bit string contains only bits specified by |
194 | | * the flags vector. Returns 0 if there is at least one bit set in 'a' |
195 | | * which is not specified in 'flags', 1 otherwise. |
196 | | * 'len' is the length of 'flags'. |
197 | | */ |
198 | | int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, |
199 | | const unsigned char *flags, int flags_len) |
200 | 0 | { |
201 | 0 | int i, ok; |
202 | | /* Check if there is one bit set at all. */ |
203 | 0 | if (!a || !a->data) |
204 | 0 | return 1; |
205 | | |
206 | | /* |
207 | | * Check each byte of the internal representation of the bit string. |
208 | | */ |
209 | 0 | ok = 1; |
210 | 0 | for (i = 0; i < a->length && ok; ++i) { |
211 | 0 | unsigned char mask = i < flags_len ? ~flags[i] : 0xff; |
212 | | /* We are done if there is an unneeded bit set. */ |
213 | 0 | ok = (a->data[i] & mask) == 0; |
214 | 0 | } |
215 | 0 | return ok; |
216 | 0 | } |
217 | | |
218 | | int ASN1_BIT_STRING_get_length(const ASN1_BIT_STRING *abs, size_t *out_length, |
219 | | int *out_unused_bits) |
220 | 0 | { |
221 | 0 | size_t length; |
222 | 0 | int unused_bits; |
223 | |
|
224 | 0 | if (abs == NULL || abs->type != V_ASN1_BIT_STRING) |
225 | 0 | return 0; |
226 | | |
227 | 0 | if (out_length == NULL || out_unused_bits == NULL) |
228 | 0 | return 0; |
229 | | |
230 | 0 | length = abs->length; |
231 | 0 | unused_bits = 0; |
232 | |
|
233 | 0 | if ((abs->flags & ASN1_STRING_FLAG_BITS_LEFT) != 0) |
234 | 0 | unused_bits = abs->flags & 0x07; |
235 | |
|
236 | 0 | if (length == 0 && unused_bits != 0) |
237 | 0 | return 0; |
238 | | |
239 | 0 | if (unused_bits != 0) { |
240 | 0 | unsigned char mask = (1 << unused_bits) - 1; |
241 | 0 | if ((abs->data[length - 1] & mask) != 0) |
242 | 0 | return 0; |
243 | 0 | } |
244 | | |
245 | 0 | *out_length = length; |
246 | 0 | *out_unused_bits = unused_bits; |
247 | |
|
248 | 0 | return 1; |
249 | 0 | } |
250 | | |
251 | | int ASN1_BIT_STRING_set1(ASN1_BIT_STRING *abs, const uint8_t *data, size_t length, |
252 | | int unused_bits) |
253 | 0 | { |
254 | 0 | if (abs == NULL) |
255 | 0 | return 0; |
256 | | |
257 | 0 | if (length > INT_MAX || unused_bits < 0 || unused_bits > 7) |
258 | 0 | return 0; |
259 | | |
260 | 0 | if (length == 0 && unused_bits != 0) |
261 | 0 | return 0; |
262 | | |
263 | 0 | if (length > 0 && (data[length - 1] & ((1 << unused_bits) - 1)) != 0) |
264 | 0 | return 0; |
265 | | |
266 | | /* |
267 | | * XXX - ASN1_STRING_set() and asn1_bit_string_set_unused_bits() preserve the |
268 | | * state of flags irrelevant to ASN1_BIT_STRING. Should we explicitly |
269 | | * clear them? |
270 | | */ |
271 | | |
272 | 0 | if (!ASN1_STRING_set(abs, data, (int)length)) |
273 | 0 | return 0; |
274 | 0 | abs->type = V_ASN1_BIT_STRING; |
275 | |
|
276 | 0 | ossl_asn1_bit_string_set_unused_bits(abs, unused_bits); |
277 | |
|
278 | 0 | return 1; |
279 | 0 | } |