/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 | 663 | { |
21 | 663 | return ossl_asn1_string_set_internal(x, d, len, /*add_nul_byte=*/0); |
22 | 663 | } |
23 | | #endif |
24 | | |
25 | | int ossl_i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp) |
26 | 6.84M | { |
27 | 6.84M | int ret = 0, bits = 0, len; |
28 | 6.84M | unsigned char *p, *d; |
29 | | |
30 | 6.84M | if (a == NULL) |
31 | 0 | goto err; |
32 | | |
33 | 6.84M | len = a->length; |
34 | | |
35 | 6.84M | if (len > INT_MAX - 1) |
36 | 0 | goto err; |
37 | | |
38 | 6.84M | if ((len > 0) && (a->flags & ASN1_STRING_FLAG_BITS_LEFT)) |
39 | 4.61M | bits = (int)a->flags & 0x07; |
40 | | |
41 | 6.84M | if (pp == NULL) |
42 | 5.53M | goto done; |
43 | | |
44 | 1.30M | p = *pp; |
45 | | |
46 | 1.30M | *(p++) = (unsigned char)bits; |
47 | 1.30M | d = a->data; |
48 | 1.30M | if (len > 0) { |
49 | 859k | memcpy(p, d, len); |
50 | 859k | p += len; |
51 | 859k | p[-1] &= (0xff << bits); |
52 | 859k | } |
53 | 1.30M | *pp = p; |
54 | | |
55 | 6.84M | done: |
56 | 6.84M | ret = len + 1; |
57 | | |
58 | 6.84M | err: |
59 | 6.84M | return ret; |
60 | 6.84M | } |
61 | | |
62 | | ASN1_BIT_STRING *ossl_c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, |
63 | | const unsigned char **pp, long len) |
64 | 5.25M | { |
65 | 5.25M | ASN1_BIT_STRING *ret = NULL; |
66 | 5.25M | const unsigned char *p; |
67 | 5.25M | unsigned char *s; |
68 | 5.25M | int i = 0; |
69 | | |
70 | 5.25M | if (len < 1) { |
71 | 20.1k | i = ASN1_R_STRING_TOO_SHORT; |
72 | 20.1k | goto err; |
73 | 20.1k | } |
74 | | |
75 | 5.23M | if (len > INT_MAX) { |
76 | 0 | i = ASN1_R_STRING_TOO_LONG; |
77 | 0 | goto err; |
78 | 0 | } |
79 | | |
80 | 5.23M | if ((a == NULL) || ((*a) == NULL)) { |
81 | 1.31M | if ((ret = ASN1_BIT_STRING_new()) == NULL) |
82 | 0 | return NULL; |
83 | 1.31M | } else |
84 | 3.91M | ret = (*a); |
85 | | |
86 | 5.23M | p = *pp; |
87 | 5.23M | i = *(p++); |
88 | 5.23M | if (i > 7) { |
89 | 15.2k | i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT; |
90 | 15.2k | goto err; |
91 | 15.2k | } |
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 | 5.22M | ossl_asn1_bit_string_set_unused_bits(ret, i); |
97 | | |
98 | 5.22M | if (len-- > 1) { /* using one because of the bits left byte */ |
99 | 3.79M | s = OPENSSL_malloc((int)len); |
100 | 3.79M | if (s == NULL) { |
101 | 0 | goto err; |
102 | 0 | } |
103 | 3.79M | memcpy(s, p, (int)len); |
104 | 3.79M | s[len - 1] &= (0xff << i); |
105 | 3.79M | p += len; |
106 | 3.79M | } else |
107 | 1.42M | s = NULL; |
108 | | |
109 | 5.22M | ASN1_STRING_set0(ret, s, (int)len); |
110 | 5.22M | ret->type = V_ASN1_BIT_STRING; |
111 | 5.22M | if (a != NULL) |
112 | 5.22M | (*a) = ret; |
113 | 5.22M | *pp = p; |
114 | 5.22M | return ret; |
115 | 35.4k | err: |
116 | 35.4k | if (i != 0) |
117 | 35.4k | ERR_raise(ERR_LIB_ASN1, i); |
118 | 35.4k | if ((a == NULL) || (*a != ret)) |
119 | 25.7k | ASN1_BIT_STRING_free(ret); |
120 | 35.4k | return NULL; |
121 | 5.22M | } |
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 | 8.97k | { |
128 | 8.97k | int w, v, iv; |
129 | 8.97k | unsigned char *c; |
130 | | |
131 | 8.97k | if (n < 0) |
132 | 54 | return 0; |
133 | | |
134 | 8.91k | w = n / 8; |
135 | 8.91k | v = 1 << (7 - (n & 0x07)); |
136 | 8.91k | iv = ~v; |
137 | 8.91k | if (!value) |
138 | 0 | v = 0; |
139 | | |
140 | 8.91k | if (a == NULL) |
141 | 0 | return 0; |
142 | | |
143 | 8.91k | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ |
144 | | |
145 | 8.91k | if ((a->length < (w + 1)) || (a->data == NULL)) { |
146 | 6.86k | if (!value) |
147 | 0 | return 1; /* Don't need to set */ |
148 | 6.86k | c = OPENSSL_clear_realloc(a->data, a->length, w + 1); |
149 | 6.86k | if (c == NULL) |
150 | 0 | return 0; |
151 | 6.86k | if (w + 1 - a->length > 0) |
152 | 6.86k | memset(c + a->length, 0, w + 1 - a->length); |
153 | 6.86k | a->data = c; |
154 | 6.86k | a->length = w + 1; |
155 | 6.86k | } |
156 | 8.91k | a->data[w] = ((a->data[w]) & iv) | v; |
157 | | |
158 | 8.91k | while ((a->length > 0) && (a->data[a->length - 1] == 0)) |
159 | 0 | a->length--; |
160 | | |
161 | 8.91k | if (a->length > 0) { |
162 | 8.91k | uint8_t u8 = a->data[a->length - 1]; |
163 | 8.91k | uint8_t unused_bits = 7; |
164 | | |
165 | | /* Only keep least significant bit; count trailing zeroes. */ |
166 | 8.91k | u8 &= 0x100 - u8; |
167 | 8.91k | if ((u8 & 0x0f) != 0) |
168 | 911 | unused_bits -= 4; |
169 | 8.91k | if ((u8 & 0x33) != 0) |
170 | 7.56k | unused_bits -= 2; |
171 | 8.91k | if ((u8 & 0x55) != 0) |
172 | 1.30k | unused_bits -= 1; |
173 | 8.91k | ossl_asn1_bit_string_set_unused_bits(a, unused_bits); |
174 | 8.91k | } |
175 | 8.91k | return 1; |
176 | 8.91k | } |
177 | | |
178 | | int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) |
179 | 785k | { |
180 | 785k | int w, v; |
181 | | |
182 | 785k | if (n < 0) |
183 | 0 | return 0; |
184 | | |
185 | 785k | w = n / 8; |
186 | 785k | v = 1 << (7 - (n & 0x07)); |
187 | 785k | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) |
188 | 287k | return 0; |
189 | 497k | return ((a->data[w] & v) != 0); |
190 | 785k | } |
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 | 11 | { |
254 | 11 | if (abs == NULL) |
255 | 0 | return 0; |
256 | | |
257 | 11 | if (length > INT_MAX || unused_bits < 0 || unused_bits > 7) |
258 | 0 | return 0; |
259 | | |
260 | 11 | if (length == 0 && unused_bits != 0) |
261 | 0 | return 0; |
262 | | |
263 | 11 | if (length > 0 && (data[length - 1] & ((1 << unused_bits) - 1)) != 0) |
264 | 0 | return 0; |
265 | | |
266 | 11 | if (!ossl_asn1_string_set_internal(abs, data, (int)length, /*add_nul_byte=*/0)) |
267 | 0 | return 0; |
268 | | |
269 | 11 | abs->type = V_ASN1_BIT_STRING; |
270 | | |
271 | 11 | ossl_asn1_bit_string_set_unused_bits(abs, unused_bits); |
272 | | |
273 | 11 | return 1; |
274 | 11 | } |