/src/openssl/crypto/asn1/asn1_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 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 <stdio.h> |
11 | | #include <limits.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/asn1.h> |
14 | | #include "asn1_local.h" |
15 | | |
16 | | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
17 | | long max); |
18 | | static void asn1_put_length(unsigned char **pp, int length); |
19 | | |
20 | | static int _asn1_check_infinite_end(const unsigned char **p, long len) |
21 | 0 | { |
22 | | /* |
23 | | * If there is 0 or 1 byte left, the length check should pick things up |
24 | | */ |
25 | 0 | if (len <= 0) { |
26 | 0 | return 1; |
27 | 0 | } else { |
28 | 0 | if ((len >= 2) && ((*p)[0] == 0) && ((*p)[1] == 0)) { |
29 | 0 | (*p) += 2; |
30 | 0 | return 1; |
31 | 0 | } |
32 | 0 | } |
33 | 0 | return 0; |
34 | 0 | } |
35 | | |
36 | | int ASN1_check_infinite_end(unsigned char **p, long len) |
37 | 0 | { |
38 | 0 | return _asn1_check_infinite_end((const unsigned char **)p, len); |
39 | 0 | } |
40 | | |
41 | | int ASN1_const_check_infinite_end(const unsigned char **p, long len) |
42 | 0 | { |
43 | 0 | return _asn1_check_infinite_end(p, len); |
44 | 0 | } |
45 | | |
46 | | int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag, |
47 | | int *pclass, long omax) |
48 | 24.0k | { |
49 | 24.0k | int i, ret; |
50 | 24.0k | long len; |
51 | 24.0k | const unsigned char *p = *pp; |
52 | 24.0k | int tag, xclass, inf; |
53 | 24.0k | long max = omax; |
54 | | |
55 | 24.0k | if (omax <= 0) { |
56 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL); |
57 | 0 | return 0x80; |
58 | 0 | } |
59 | 24.0k | ret = (*p & V_ASN1_CONSTRUCTED); |
60 | 24.0k | xclass = (*p & V_ASN1_PRIVATE); |
61 | 24.0k | i = *p & V_ASN1_PRIMITIVE_TAG; |
62 | 24.0k | if (i == V_ASN1_PRIMITIVE_TAG) { /* high-tag */ |
63 | 297 | p++; |
64 | 297 | if (--max == 0) |
65 | 7 | goto err; |
66 | 290 | len = 0; |
67 | 820 | while (*p & 0x80) { |
68 | 538 | len <<= 7L; |
69 | 538 | len |= *(p++) & 0x7f; |
70 | 538 | if (--max == 0) |
71 | 3 | goto err; |
72 | 535 | if (len > (INT_MAX >> 7L)) |
73 | 5 | goto err; |
74 | 535 | } |
75 | 282 | len <<= 7L; |
76 | 282 | len |= *(p++) & 0x7f; |
77 | 282 | tag = (int)len; |
78 | 282 | if (--max == 0) |
79 | 3 | goto err; |
80 | 23.7k | } else { |
81 | 23.7k | tag = i; |
82 | 23.7k | p++; |
83 | 23.7k | if (--max == 0) |
84 | 19 | goto err; |
85 | 23.7k | } |
86 | 24.0k | *ptag = tag; |
87 | 24.0k | *pclass = xclass; |
88 | 24.0k | if (!asn1_get_length(&p, &inf, plength, max)) |
89 | 97 | goto err; |
90 | | |
91 | 23.9k | if (inf && !(ret & V_ASN1_CONSTRUCTED)) |
92 | 3 | goto err; |
93 | | |
94 | 23.9k | if (*plength > (omax - (p - *pp))) { |
95 | 129 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
96 | | /* |
97 | | * Set this so that even if things are not long enough the values are |
98 | | * set correctly |
99 | | */ |
100 | 129 | ret |= 0x80; |
101 | 129 | } |
102 | 23.9k | *pp = p; |
103 | 23.9k | return ret | inf; |
104 | 137 | err: |
105 | 137 | ERR_raise(ERR_LIB_ASN1, ASN1_R_HEADER_TOO_LONG); |
106 | 137 | return 0x80; |
107 | 23.9k | } |
108 | | |
109 | | /* |
110 | | * Decode a length field. |
111 | | * The short form is a single byte defining a length 0 - 127. |
112 | | * The long form is a byte 0 - 127 with the top bit set and this indicates |
113 | | * the number of following octets that contain the length. These octets |
114 | | * are stored most significant digit first. |
115 | | */ |
116 | | static int asn1_get_length(const unsigned char **pp, int *inf, long *rl, |
117 | | long max) |
118 | 24.0k | { |
119 | 24.0k | const unsigned char *p = *pp; |
120 | 24.0k | unsigned long ret = 0; |
121 | 24.0k | int i; |
122 | | |
123 | 24.0k | if (max-- < 1) |
124 | 0 | return 0; |
125 | 24.0k | if (*p == 0x80) { |
126 | 1.27k | *inf = 1; |
127 | 1.27k | p++; |
128 | 22.7k | } else { |
129 | 22.7k | *inf = 0; |
130 | 22.7k | i = *p & 0x7f; |
131 | 22.7k | if (*p++ & 0x80) { |
132 | 839 | if (max < i + 1) |
133 | 18 | return 0; |
134 | | /* Skip leading zeroes */ |
135 | 1.54k | while (i > 0 && *p == 0) { |
136 | 720 | p++; |
137 | 720 | i--; |
138 | 720 | } |
139 | 821 | if (i > (int)sizeof(long)) |
140 | 19 | return 0; |
141 | 2.94k | while (i > 0) { |
142 | 2.14k | ret <<= 8; |
143 | 2.14k | ret |= *p++; |
144 | 2.14k | i--; |
145 | 2.14k | } |
146 | 802 | if (ret > LONG_MAX) |
147 | 60 | return 0; |
148 | 21.9k | } else { |
149 | 21.9k | ret = i; |
150 | 21.9k | } |
151 | 22.7k | } |
152 | 23.9k | *pp = p; |
153 | 23.9k | *rl = (long)ret; |
154 | 23.9k | return 1; |
155 | 24.0k | } |
156 | | |
157 | | /* |
158 | | * constructed == 2 for indefinite length constructed |
159 | | */ |
160 | | void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag, |
161 | | int xclass) |
162 | 13.0k | { |
163 | 13.0k | unsigned char *p = *pp; |
164 | 13.0k | int i, ttag; |
165 | | |
166 | 13.0k | i = (constructed) ? V_ASN1_CONSTRUCTED : 0; |
167 | 13.0k | i |= (xclass & V_ASN1_PRIVATE); |
168 | 13.0k | if (tag < 31) { |
169 | 13.0k | *(p++) = i | (tag & V_ASN1_PRIMITIVE_TAG); |
170 | 13.0k | } else { |
171 | 0 | *(p++) = i | V_ASN1_PRIMITIVE_TAG; |
172 | 0 | for (i = 0, ttag = tag; ttag > 0; i++) |
173 | 0 | ttag >>= 7; |
174 | 0 | ttag = i; |
175 | 0 | while (i-- > 0) { |
176 | 0 | p[i] = tag & 0x7f; |
177 | 0 | if (i != (ttag - 1)) |
178 | 0 | p[i] |= 0x80; |
179 | 0 | tag >>= 7; |
180 | 0 | } |
181 | 0 | p += ttag; |
182 | 0 | } |
183 | 13.0k | if (constructed == 2) |
184 | 0 | *(p++) = 0x80; |
185 | 13.0k | else |
186 | 13.0k | asn1_put_length(&p, length); |
187 | 13.0k | *pp = p; |
188 | 13.0k | } |
189 | | |
190 | | int ASN1_put_eoc(unsigned char **pp) |
191 | 0 | { |
192 | 0 | unsigned char *p = *pp; |
193 | |
|
194 | 0 | *p++ = 0; |
195 | 0 | *p++ = 0; |
196 | 0 | *pp = p; |
197 | 0 | return 2; |
198 | 0 | } |
199 | | |
200 | | static void asn1_put_length(unsigned char **pp, int length) |
201 | 13.0k | { |
202 | 13.0k | unsigned char *p = *pp; |
203 | 13.0k | int i, len; |
204 | | |
205 | 13.0k | if (length <= 127) { |
206 | 13.0k | *(p++) = (unsigned char)length; |
207 | 13.0k | } else { |
208 | 0 | len = length; |
209 | 0 | for (i = 0; len > 0; i++) |
210 | 0 | len >>= 8; |
211 | 0 | *(p++) = i | 0x80; |
212 | 0 | len = i; |
213 | 0 | while (i-- > 0) { |
214 | 0 | p[i] = length & 0xff; |
215 | 0 | length >>= 8; |
216 | 0 | } |
217 | 0 | p += len; |
218 | 0 | } |
219 | 13.0k | *pp = p; |
220 | 13.0k | } |
221 | | |
222 | | int ASN1_object_size(int constructed, int length, int tag) |
223 | 22.7k | { |
224 | 22.7k | int ret = 1; |
225 | | |
226 | 22.7k | if (length < 0) |
227 | 0 | return -1; |
228 | 22.7k | if (tag >= 31) { |
229 | 0 | while (tag > 0) { |
230 | 0 | tag >>= 7; |
231 | 0 | ret++; |
232 | 0 | } |
233 | 0 | } |
234 | 22.7k | if (constructed == 2) { |
235 | 0 | ret += 3; |
236 | 22.7k | } else { |
237 | 22.7k | ret++; |
238 | 22.7k | if (length > 127) { |
239 | 0 | int tmplen = length; |
240 | 0 | while (tmplen > 0) { |
241 | 0 | tmplen >>= 8; |
242 | 0 | ret++; |
243 | 0 | } |
244 | 0 | } |
245 | 22.7k | } |
246 | 22.7k | if (ret >= INT_MAX - length) |
247 | 0 | return -1; |
248 | 22.7k | return ret + length; |
249 | 22.7k | } |
250 | | |
251 | | void ossl_asn1_string_set_bits_left(ASN1_STRING *str, unsigned int num) |
252 | 8 | { |
253 | 8 | str->flags &= ~0x07; |
254 | 8 | str->flags |= ASN1_STRING_FLAG_BITS_LEFT | (num & 0x07); |
255 | 8 | } |
256 | | |
257 | | int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str) |
258 | 537 | { |
259 | 537 | if (str == NULL) |
260 | 0 | return 0; |
261 | 537 | dst->type = str->type; |
262 | 537 | if (!ASN1_STRING_set(dst, str->data, str->length)) |
263 | 0 | return 0; |
264 | | /* Copy flags but preserve embed value */ |
265 | 537 | dst->flags &= ASN1_STRING_FLAG_EMBED; |
266 | 537 | dst->flags |= str->flags & ~ASN1_STRING_FLAG_EMBED; |
267 | 537 | return 1; |
268 | 537 | } |
269 | | |
270 | | ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) |
271 | 0 | { |
272 | 0 | ASN1_STRING *ret; |
273 | |
|
274 | 0 | if (!str) |
275 | 0 | return NULL; |
276 | 0 | ret = ASN1_STRING_new(); |
277 | 0 | if (ret == NULL) |
278 | 0 | return NULL; |
279 | 0 | if (!ASN1_STRING_copy(ret, str)) { |
280 | 0 | ASN1_STRING_free(ret); |
281 | 0 | return NULL; |
282 | 0 | } |
283 | 0 | return ret; |
284 | 0 | } |
285 | | |
286 | | int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len_in) |
287 | 4.34k | { |
288 | 4.34k | unsigned char *c; |
289 | 4.34k | const char *data = _data; |
290 | 4.34k | size_t len; |
291 | | |
292 | 4.34k | if (len_in < 0) { |
293 | 0 | if (data == NULL) |
294 | 0 | return 0; |
295 | 0 | len = strlen(data); |
296 | 4.34k | } else { |
297 | 4.34k | len = (size_t)len_in; |
298 | 4.34k | } |
299 | | /* |
300 | | * Verify that the length fits within an integer for assignment to |
301 | | * str->length below. The additional 1 is subtracted to allow for the |
302 | | * '\0' terminator even though this isn't strictly necessary. |
303 | | */ |
304 | 4.34k | if (len > INT_MAX - 1) { |
305 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE); |
306 | 0 | return 0; |
307 | 0 | } |
308 | 4.34k | if ((size_t)str->length <= len || str->data == NULL) { |
309 | 4.34k | c = str->data; |
310 | 4.34k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
311 | | /* No NUL terminator in fuzzing builds */ |
312 | 4.34k | str->data = OPENSSL_realloc(c, len != 0 ? len : 1); |
313 | | #else |
314 | | str->data = OPENSSL_realloc(c, len + 1); |
315 | | #endif |
316 | 4.34k | if (str->data == NULL) { |
317 | 0 | str->data = c; |
318 | 0 | return 0; |
319 | 0 | } |
320 | 4.34k | } |
321 | 4.34k | str->length = len; |
322 | 4.34k | if (data != NULL) { |
323 | 3.60k | memcpy(str->data, data, len); |
324 | 3.60k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
325 | | /* Set the unused byte to something non NUL and printable. */ |
326 | 3.60k | if (len == 0) |
327 | 10 | str->data[len] = '~'; |
328 | | #else |
329 | | /* |
330 | | * Add a NUL terminator. This should not be necessary - but we add it as |
331 | | * a safety precaution |
332 | | */ |
333 | | str->data[len] = '\0'; |
334 | | #endif |
335 | 3.60k | } |
336 | 4.34k | return 1; |
337 | 4.34k | } |
338 | | |
339 | | void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) |
340 | 1.08k | { |
341 | 1.08k | OPENSSL_free(str->data); |
342 | 1.08k | str->data = data; |
343 | 1.08k | str->length = len; |
344 | 1.08k | } |
345 | | |
346 | | ASN1_STRING *ASN1_STRING_new(void) |
347 | 0 | { |
348 | 0 | return ASN1_STRING_type_new(V_ASN1_OCTET_STRING); |
349 | 0 | } |
350 | | |
351 | | ASN1_STRING *ASN1_STRING_type_new(int type) |
352 | 6.23k | { |
353 | 6.23k | ASN1_STRING *ret; |
354 | | |
355 | 6.23k | ret = OPENSSL_zalloc(sizeof(*ret)); |
356 | 6.23k | if (ret == NULL) |
357 | 0 | return NULL; |
358 | 6.23k | ret->type = type; |
359 | 6.23k | return ret; |
360 | 6.23k | } |
361 | | |
362 | | void ossl_asn1_string_embed_free(ASN1_STRING *a, int embed) |
363 | 7.57k | { |
364 | 7.57k | if (a == NULL) |
365 | 0 | return; |
366 | 7.57k | if (!(a->flags & ASN1_STRING_FLAG_NDEF)) |
367 | 7.57k | OPENSSL_free(a->data); |
368 | 7.57k | if (embed == 0) |
369 | 6.23k | OPENSSL_free(a); |
370 | 7.57k | } |
371 | | |
372 | | void ASN1_STRING_free(ASN1_STRING *a) |
373 | 3.10k | { |
374 | 3.10k | if (a == NULL) |
375 | 2.43k | return; |
376 | 675 | ossl_asn1_string_embed_free(a, a->flags & ASN1_STRING_FLAG_EMBED); |
377 | 675 | } |
378 | | |
379 | | void ASN1_STRING_clear_free(ASN1_STRING *a) |
380 | 0 | { |
381 | 0 | if (a == NULL) |
382 | 0 | return; |
383 | 0 | if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF)) |
384 | 0 | OPENSSL_cleanse(a->data, a->length); |
385 | 0 | ASN1_STRING_free(a); |
386 | 0 | } |
387 | | |
388 | | int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b) |
389 | 0 | { |
390 | 0 | int i; |
391 | |
|
392 | 0 | i = (a->length - b->length); |
393 | 0 | if (i == 0) { |
394 | 0 | if (a->length != 0) |
395 | 0 | i = memcmp(a->data, b->data, a->length); |
396 | 0 | if (i == 0) |
397 | 0 | return a->type - b->type; |
398 | 0 | else |
399 | 0 | return i; |
400 | 0 | } else { |
401 | 0 | return i; |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | | int ASN1_STRING_length(const ASN1_STRING *x) |
406 | 0 | { |
407 | 0 | return x->length; |
408 | 0 | } |
409 | | |
410 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
411 | | void ASN1_STRING_length_set(ASN1_STRING *x, int len) |
412 | 0 | { |
413 | 0 | x->length = len; |
414 | 0 | } |
415 | | #endif |
416 | | |
417 | | int ASN1_STRING_type(const ASN1_STRING *x) |
418 | 0 | { |
419 | 0 | return x->type; |
420 | 0 | } |
421 | | |
422 | | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) |
423 | 0 | { |
424 | 0 | return x->data; |
425 | 0 | } |
426 | | |
427 | | #ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
428 | | unsigned char *ASN1_STRING_data(ASN1_STRING *x) |
429 | 0 | { |
430 | 0 | return x->data; |
431 | 0 | } |
432 | | #endif |
433 | | |
434 | | /* |max_len| excludes NUL terminator and may be 0 to indicate no restriction */ |
435 | | char *ossl_sk_ASN1_UTF8STRING2text(STACK_OF(ASN1_UTF8STRING) *text, |
436 | | const char *sep, size_t max_len) |
437 | 0 | { |
438 | 0 | int i; |
439 | 0 | ASN1_UTF8STRING *current; |
440 | 0 | size_t length = 0, sep_len; |
441 | 0 | char *result = NULL; |
442 | 0 | char *p; |
443 | |
|
444 | 0 | if (sep == NULL) |
445 | 0 | sep = ""; |
446 | 0 | sep_len = strlen(sep); |
447 | |
|
448 | 0 | for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) { |
449 | 0 | current = sk_ASN1_UTF8STRING_value(text, i); |
450 | 0 | if (i > 0) |
451 | 0 | length += sep_len; |
452 | 0 | length += ASN1_STRING_length(current); |
453 | 0 | if (max_len != 0 && length > max_len) |
454 | 0 | return NULL; |
455 | 0 | } |
456 | 0 | if ((result = OPENSSL_malloc(length + 1)) == NULL) |
457 | 0 | return NULL; |
458 | | |
459 | 0 | p = result; |
460 | 0 | for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) { |
461 | 0 | current = sk_ASN1_UTF8STRING_value(text, i); |
462 | 0 | length = ASN1_STRING_length(current); |
463 | 0 | if (i > 0 && sep_len > 0) { |
464 | 0 | strncpy(p, sep, sep_len + 1); /* using + 1 to silence gcc warning */ |
465 | 0 | p += sep_len; |
466 | 0 | } |
467 | 0 | strncpy(p, (const char *)ASN1_STRING_get0_data(current), length); |
468 | 0 | p += length; |
469 | 0 | } |
470 | 0 | *p = '\0'; |
471 | |
|
472 | 0 | return result; |
473 | 0 | } |