diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/a_bitstr.c')
-rw-r--r-- | src/lib/libcrypto/asn1/a_bitstr.c | 244 |
1 files changed, 122 insertions, 122 deletions
diff --git a/src/lib/libcrypto/asn1/a_bitstr.c b/src/lib/libcrypto/asn1/a_bitstr.c index 68cefee4a2..207d80ab38 100644 --- a/src/lib/libcrypto/asn1/a_bitstr.c +++ b/src/lib/libcrypto/asn1/a_bitstr.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: a_bitstr.c,v 1.31 2021/12/15 18:00:31 jsing Exp $ */ | 1 | /* $OpenBSD: a_bitstr.c,v 1.32 2021/12/25 07:48:09 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -71,6 +71,127 @@ ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len) | |||
71 | } | 71 | } |
72 | 72 | ||
73 | int | 73 | int |
74 | ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) | ||
75 | { | ||
76 | int w, v, iv; | ||
77 | unsigned char *c; | ||
78 | |||
79 | w = n/8; | ||
80 | v = 1 << (7 - (n & 0x07)); | ||
81 | iv = ~v; | ||
82 | if (!value) | ||
83 | v = 0; | ||
84 | |||
85 | if (a == NULL) | ||
86 | return 0; | ||
87 | |||
88 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ | ||
89 | |||
90 | if ((a->length < (w + 1)) || (a->data == NULL)) { | ||
91 | if (!value) | ||
92 | return(1); /* Don't need to set */ | ||
93 | if ((c = recallocarray(a->data, a->length, w + 1, 1)) == NULL) { | ||
94 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
95 | return 0; | ||
96 | } | ||
97 | a->data = c; | ||
98 | a->length = w + 1; | ||
99 | } | ||
100 | a->data[w] = ((a->data[w]) & iv) | v; | ||
101 | while ((a->length > 0) && (a->data[a->length - 1] == 0)) | ||
102 | a->length--; | ||
103 | |||
104 | return (1); | ||
105 | } | ||
106 | |||
107 | int | ||
108 | ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) | ||
109 | { | ||
110 | int w, v; | ||
111 | |||
112 | w = n / 8; | ||
113 | v = 1 << (7 - (n & 0x07)); | ||
114 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) | ||
115 | return (0); | ||
116 | return ((a->data[w] & v) != 0); | ||
117 | } | ||
118 | |||
119 | /* | ||
120 | * Checks if the given bit string contains only bits specified by | ||
121 | * the flags vector. Returns 0 if there is at least one bit set in 'a' | ||
122 | * which is not specified in 'flags', 1 otherwise. | ||
123 | * 'len' is the length of 'flags'. | ||
124 | */ | ||
125 | int | ||
126 | ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, | ||
127 | int flags_len) | ||
128 | { | ||
129 | int i, ok; | ||
130 | |||
131 | /* Check if there is one bit set at all. */ | ||
132 | if (!a || !a->data) | ||
133 | return 1; | ||
134 | |||
135 | /* Check each byte of the internal representation of the bit string. */ | ||
136 | ok = 1; | ||
137 | for (i = 0; i < a->length && ok; ++i) { | ||
138 | unsigned char mask = i < flags_len ? ~flags[i] : 0xff; | ||
139 | /* We are done if there is an unneeded bit set. */ | ||
140 | ok = (a->data[i] & mask) == 0; | ||
141 | } | ||
142 | return ok; | ||
143 | } | ||
144 | |||
145 | int | ||
146 | ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs, | ||
147 | BIT_STRING_BITNAME *tbl, int indent) | ||
148 | { | ||
149 | BIT_STRING_BITNAME *bnam; | ||
150 | char first = 1; | ||
151 | |||
152 | BIO_printf(out, "%*s", indent, ""); | ||
153 | for (bnam = tbl; bnam->lname; bnam++) { | ||
154 | if (ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) { | ||
155 | if (!first) | ||
156 | BIO_puts(out, ", "); | ||
157 | BIO_puts(out, bnam->lname); | ||
158 | first = 0; | ||
159 | } | ||
160 | } | ||
161 | BIO_puts(out, "\n"); | ||
162 | return 1; | ||
163 | } | ||
164 | |||
165 | int | ||
166 | ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value, | ||
167 | BIT_STRING_BITNAME *tbl) | ||
168 | { | ||
169 | int bitnum; | ||
170 | |||
171 | bitnum = ASN1_BIT_STRING_num_asc(name, tbl); | ||
172 | if (bitnum < 0) | ||
173 | return 0; | ||
174 | if (bs) { | ||
175 | if (!ASN1_BIT_STRING_set_bit(bs, bitnum, value)) | ||
176 | return 0; | ||
177 | } | ||
178 | return 1; | ||
179 | } | ||
180 | |||
181 | int | ||
182 | ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl) | ||
183 | { | ||
184 | BIT_STRING_BITNAME *bnam; | ||
185 | |||
186 | for (bnam = tbl; bnam->lname; bnam++) { | ||
187 | if (!strcmp(bnam->sname, name) || | ||
188 | !strcmp(bnam->lname, name)) | ||
189 | return bnam->bitnum; | ||
190 | } | ||
191 | return -1; | ||
192 | } | ||
193 | |||
194 | int | ||
74 | i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp) | 195 | i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp) |
75 | { | 196 | { |
76 | int ret, j, bits, len; | 197 | int ret, j, bits, len; |
@@ -192,124 +313,3 @@ c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a, const unsigned char **pp, long len) | |||
192 | 313 | ||
193 | return (NULL); | 314 | return (NULL); |
194 | } | 315 | } |
195 | |||
196 | int | ||
197 | ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) | ||
198 | { | ||
199 | int w, v, iv; | ||
200 | unsigned char *c; | ||
201 | |||
202 | w = n/8; | ||
203 | v = 1 << (7 - (n & 0x07)); | ||
204 | iv = ~v; | ||
205 | if (!value) | ||
206 | v = 0; | ||
207 | |||
208 | if (a == NULL) | ||
209 | return 0; | ||
210 | |||
211 | a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */ | ||
212 | |||
213 | if ((a->length < (w + 1)) || (a->data == NULL)) { | ||
214 | if (!value) | ||
215 | return(1); /* Don't need to set */ | ||
216 | if ((c = recallocarray(a->data, a->length, w + 1, 1)) == NULL) { | ||
217 | ASN1error(ERR_R_MALLOC_FAILURE); | ||
218 | return 0; | ||
219 | } | ||
220 | a->data = c; | ||
221 | a->length = w + 1; | ||
222 | } | ||
223 | a->data[w] = ((a->data[w]) & iv) | v; | ||
224 | while ((a->length > 0) && (a->data[a->length - 1] == 0)) | ||
225 | a->length--; | ||
226 | |||
227 | return (1); | ||
228 | } | ||
229 | |||
230 | int | ||
231 | ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n) | ||
232 | { | ||
233 | int w, v; | ||
234 | |||
235 | w = n / 8; | ||
236 | v = 1 << (7 - (n & 0x07)); | ||
237 | if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL)) | ||
238 | return (0); | ||
239 | return ((a->data[w] & v) != 0); | ||
240 | } | ||
241 | |||
242 | /* | ||
243 | * Checks if the given bit string contains only bits specified by | ||
244 | * the flags vector. Returns 0 if there is at least one bit set in 'a' | ||
245 | * which is not specified in 'flags', 1 otherwise. | ||
246 | * 'len' is the length of 'flags'. | ||
247 | */ | ||
248 | int | ||
249 | ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a, const unsigned char *flags, | ||
250 | int flags_len) | ||
251 | { | ||
252 | int i, ok; | ||
253 | |||
254 | /* Check if there is one bit set at all. */ | ||
255 | if (!a || !a->data) | ||
256 | return 1; | ||
257 | |||
258 | /* Check each byte of the internal representation of the bit string. */ | ||
259 | ok = 1; | ||
260 | for (i = 0; i < a->length && ok; ++i) { | ||
261 | unsigned char mask = i < flags_len ? ~flags[i] : 0xff; | ||
262 | /* We are done if there is an unneeded bit set. */ | ||
263 | ok = (a->data[i] & mask) == 0; | ||
264 | } | ||
265 | return ok; | ||
266 | } | ||
267 | |||
268 | int | ||
269 | ASN1_BIT_STRING_name_print(BIO *out, ASN1_BIT_STRING *bs, | ||
270 | BIT_STRING_BITNAME *tbl, int indent) | ||
271 | { | ||
272 | BIT_STRING_BITNAME *bnam; | ||
273 | char first = 1; | ||
274 | |||
275 | BIO_printf(out, "%*s", indent, ""); | ||
276 | for (bnam = tbl; bnam->lname; bnam++) { | ||
277 | if (ASN1_BIT_STRING_get_bit(bs, bnam->bitnum)) { | ||
278 | if (!first) | ||
279 | BIO_puts(out, ", "); | ||
280 | BIO_puts(out, bnam->lname); | ||
281 | first = 0; | ||
282 | } | ||
283 | } | ||
284 | BIO_puts(out, "\n"); | ||
285 | return 1; | ||
286 | } | ||
287 | |||
288 | int | ||
289 | ASN1_BIT_STRING_set_asc(ASN1_BIT_STRING *bs, const char *name, int value, | ||
290 | BIT_STRING_BITNAME *tbl) | ||
291 | { | ||
292 | int bitnum; | ||
293 | |||
294 | bitnum = ASN1_BIT_STRING_num_asc(name, tbl); | ||
295 | if (bitnum < 0) | ||
296 | return 0; | ||
297 | if (bs) { | ||
298 | if (!ASN1_BIT_STRING_set_bit(bs, bitnum, value)) | ||
299 | return 0; | ||
300 | } | ||
301 | return 1; | ||
302 | } | ||
303 | |||
304 | int | ||
305 | ASN1_BIT_STRING_num_asc(const char *name, BIT_STRING_BITNAME *tbl) | ||
306 | { | ||
307 | BIT_STRING_BITNAME *bnam; | ||
308 | |||
309 | for (bnam = tbl; bnam->lname; bnam++) { | ||
310 | if (!strcmp(bnam->sname, name) || | ||
311 | !strcmp(bnam->lname, name)) | ||
312 | return bnam->bitnum; | ||
313 | } | ||
314 | return -1; | ||
315 | } | ||