blob: 9e81476d02afc4f425b33bc7e46eff3bc1127f20 [file] [log] [blame]
David Benjamin820731a2015-07-23 20:01:51 -04001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
Adam Langley95c29f32014-06-20 12:00:00 -07003 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
Adam Langley95c29f32014-06-20 12:00:00 -070057#include <openssl/mem.h>
58
59#include <assert.h>
Bob Beck350f8542023-02-07 16:11:58 -070060#include <errno.h>
61#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -070062#include <stdarg.h>
63#include <stdio.h>
Bob Beck350f8542023-02-07 16:11:58 -070064#include <stdlib.h>
Adam Langley95c29f32014-06-20 12:00:00 -070065
David Benjamin3ba95862019-10-21 16:14:33 -040066#include <openssl/err.h>
67
Adam Langleyded93582014-07-31 15:23:51 -070068#if defined(OPENSSL_WINDOWS)
David Benjamina353cdb2016-06-09 16:48:33 -040069OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -070070#include <windows.h>
David Benjamin054e5972016-06-16 12:08:26 -040071OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langleyded93582014-07-31 15:23:51 -070072#endif
73
David Benjamin582904f2023-02-04 18:30:36 -050074#if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
75#include <errno.h>
76#include <signal.h>
77#include <unistd.h>
78#endif
79
David Benjamin17cf2cb2016-12-13 01:07:13 -050080#include "internal.h"
81
Adam Langley95c29f32014-06-20 12:00:00 -070082
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070083#define OPENSSL_MALLOC_PREFIX 8
David Benjaminb7d63202022-07-26 13:25:02 -070084static_assert(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), "size_t too large");
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070085
David Benjaminda8bb842019-02-26 22:13:28 -060086#if defined(OPENSSL_ASAN)
87void __asan_poison_memory_region(const volatile void *addr, size_t size);
88void __asan_unpoison_memory_region(const volatile void *addr, size_t size);
89#else
90static void __asan_poison_memory_region(const void *addr, size_t size) {}
91static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
92#endif
93
John Sheu787b26c2019-05-03 12:08:12 -070094// Windows doesn't really support weak symbols as of May 2019, and Clang on
95// Windows will emit strong symbols instead. See
96// https://bugs.llvm.org/show_bug.cgi?id=37598
Adam Langley0cf14d32020-03-30 09:24:45 -070097#if defined(__ELF__) && defined(__GNUC__)
98#define WEAK_SYMBOL_FUNC(rettype, name, args) \
99 rettype name args __attribute__((weak));
Wiktor Garbacz9ae40ce2020-02-05 18:14:20 +0100100#else
Adam Langley0cf14d32020-03-30 09:24:45 -0700101#define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL;
Wiktor Garbacz9ae40ce2020-02-05 18:14:20 +0100102#endif
103
Chris Kennellyb5e4a222018-09-10 11:47:15 -0400104// sdallocx is a sized |free| function. By passing the size (which we happen to
Adam Langleyb49b78e2021-09-02 14:57:02 -0700105// always know in BoringSSL), the malloc implementation can save work. We cannot
106// depend on |sdallocx| being available, however, so it's a weak symbol.
Chris Kennellyb5e4a222018-09-10 11:47:15 -0400107//
Adam Langleyb49b78e2021-09-02 14:57:02 -0700108// This will always be safe, but will only be overridden if the malloc
109// implementation is statically linked with BoringSSL. So, if |sdallocx| is
110// provided in, say, libc.so, we still won't use it because that's dynamically
111// linked. This isn't an ideal result, but its helps in some cases.
112WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags));
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700113
Adam Langley0313b592020-06-10 14:38:02 -0700114// The following three functions can be defined to override default heap
115// allocation and freeing. If defined, it is the responsibility of
116// |OPENSSL_memory_free| to zero out the memory before returning it to the
117// system. |OPENSSL_memory_free| will not be passed NULL pointers.
David Benjamin20f7bba2021-03-24 02:31:33 -0400118//
119// WARNING: These functions are called on every allocation and free in
120// BoringSSL across the entire process. They may be called by any code in the
121// process which calls BoringSSL, including in process initializers and thread
122// destructors. When called, BoringSSL may hold pthreads locks. Any other code
123// in the process which, directly or indirectly, calls BoringSSL may be on the
124// call stack and may itself be using arbitrary synchronization primitives.
125//
126// As a result, these functions may not have the usual programming environment
127// available to most C or C++ code. In particular, they may not call into
128// BoringSSL, or any library which depends on BoringSSL. Any synchronization
129// primitives used must tolerate every other synchronization primitive linked
130// into the process, including pthreads locks. Failing to meet these constraints
131// may result in deadlocks, crashes, or memory corruption.
Bob Beck350f8542023-02-07 16:11:58 -0700132WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size));
Adam Langley0313b592020-06-10 14:38:02 -0700133WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr));
134WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr));
Wiktor Garbacz9ae40ce2020-02-05 18:14:20 +0100135
David Benjamin582904f2023-02-04 18:30:36 -0500136#if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
David Benjamin04c3d402023-06-03 01:26:29 -0400137static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT;
David Benjamin582904f2023-02-04 18:30:36 -0500138static uint64_t current_malloc_count = 0;
139static uint64_t malloc_number_to_fail = 0;
David Benjamin5e356a82023-02-04 19:44:34 -0500140static int malloc_failure_enabled = 0, break_on_malloc_fail = 0,
141 any_malloc_failed = 0;
David Benjamin582904f2023-02-04 18:30:36 -0500142
143static void malloc_exit_handler(void) {
David Benjamin04c3d402023-06-03 01:26:29 -0400144 CRYPTO_MUTEX_lock_read(&malloc_failure_lock);
David Benjamin5e356a82023-02-04 19:44:34 -0500145 if (any_malloc_failed) {
146 // Signal to the test driver that some allocation failed, so it knows to
147 // increment the counter and continue.
David Benjamin582904f2023-02-04 18:30:36 -0500148 _exit(88);
149 }
David Benjamin04c3d402023-06-03 01:26:29 -0400150 CRYPTO_MUTEX_unlock_read(&malloc_failure_lock);
David Benjamin582904f2023-02-04 18:30:36 -0500151}
152
153static void init_malloc_failure(void) {
154 const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
155 if (env != NULL && env[0] != 0) {
156 char *endptr;
157 malloc_number_to_fail = strtoull(env, &endptr, 10);
158 if (*endptr == 0) {
159 malloc_failure_enabled = 1;
160 atexit(malloc_exit_handler);
161 }
162 }
163 break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL;
164}
165
166// should_fail_allocation returns one if the current allocation should fail and
167// zero otherwise.
168static int should_fail_allocation() {
169 static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
170 CRYPTO_once(&once, init_malloc_failure);
171 if (!malloc_failure_enabled) {
172 return 0;
173 }
174
175 // We lock just so multi-threaded tests are still correct, but we won't test
176 // every malloc exhaustively.
David Benjamin04c3d402023-06-03 01:26:29 -0400177 CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
David Benjamin582904f2023-02-04 18:30:36 -0500178 int should_fail = current_malloc_count == malloc_number_to_fail;
179 current_malloc_count++;
David Benjamin5e356a82023-02-04 19:44:34 -0500180 any_malloc_failed = any_malloc_failed || should_fail;
David Benjamin04c3d402023-06-03 01:26:29 -0400181 CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
David Benjamin582904f2023-02-04 18:30:36 -0500182
183 if (should_fail && break_on_malloc_fail) {
184 raise(SIGTRAP);
185 }
186 if (should_fail) {
187 errno = ENOMEM;
188 }
189 return should_fail;
190}
191
David Benjamin5e356a82023-02-04 19:44:34 -0500192void OPENSSL_reset_malloc_counter_for_testing(void) {
David Benjamin04c3d402023-06-03 01:26:29 -0400193 CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
David Benjamin5e356a82023-02-04 19:44:34 -0500194 current_malloc_count = 0;
David Benjamin04c3d402023-06-03 01:26:29 -0400195 CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
David Benjamin5e356a82023-02-04 19:44:34 -0500196}
197
David Benjamin582904f2023-02-04 18:30:36 -0500198#else
199static int should_fail_allocation(void) { return 0; }
200#endif
201
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700202void *OPENSSL_malloc(size_t size) {
David Benjamin582904f2023-02-04 18:30:36 -0500203 if (should_fail_allocation()) {
Bob Beckdcabfe22023-02-07 19:06:08 -0700204 goto err;
David Benjamin582904f2023-02-04 18:30:36 -0500205 }
206
Adam Langley0313b592020-06-10 14:38:02 -0700207 if (OPENSSL_memory_alloc != NULL) {
208 assert(OPENSSL_memory_free != NULL);
209 assert(OPENSSL_memory_get_size != NULL);
Bob Beckdcabfe22023-02-07 19:06:08 -0700210 void *ptr = OPENSSL_memory_alloc(size);
211 if (ptr == NULL && size != 0) {
212 goto err;
213 }
214 return ptr;
Adam Langley0313b592020-06-10 14:38:02 -0700215 }
216
Adam Langley7964a1d2020-02-05 15:23:07 -0800217 if (size + OPENSSL_MALLOC_PREFIX < size) {
Bob Beckdcabfe22023-02-07 19:06:08 -0700218 goto err;
Adam Langley7964a1d2020-02-05 15:23:07 -0800219 }
220
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700221 void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
Adam Langley95c29f32014-06-20 12:00:00 -0700222 if (ptr == NULL) {
Bob Beckdcabfe22023-02-07 19:06:08 -0700223 goto err;
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700224 }
225
226 *(size_t *)ptr = size;
227
David Benjaminda8bb842019-02-26 22:13:28 -0600228 __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700229 return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
Bob Beckdcabfe22023-02-07 19:06:08 -0700230
231 err:
232 // This only works because ERR does not call OPENSSL_malloc.
233 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
234 return NULL;
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700235}
236
David Benjamindd68e4b2023-10-02 23:13:13 -0400237void *OPENSSL_zalloc(size_t size) {
238 void *ret = OPENSSL_malloc(size);
239 if (ret != NULL) {
240 OPENSSL_memset(ret, 0, size);
241 }
242 return ret;
243}
244
David Benjamin216db672023-10-05 10:42:15 -0400245void *OPENSSL_calloc(size_t num, size_t size) {
246 if (size != 0 && num > SIZE_MAX / size) {
247 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
248 return NULL;
249 }
250
251 return OPENSSL_zalloc(num * size);
252}
253
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700254void OPENSSL_free(void *orig_ptr) {
255 if (orig_ptr == NULL) {
256 return;
257 }
258
Adam Langley0313b592020-06-10 14:38:02 -0700259 if (OPENSSL_memory_free != NULL) {
260 OPENSSL_memory_free(orig_ptr);
261 return;
262 }
263
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700264 void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
David Benjaminda8bb842019-02-26 22:13:28 -0600265 __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700266
267 size_t size = *(size_t *)ptr;
268 OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
nieweif94a7ce2022-03-16 10:02:19 +0800269
270// ASan knows to intercept malloc and free, but not sdallocx.
271#if defined(OPENSSL_ASAN)
David Benjamin28883d42022-07-19 19:39:53 -0400272 (void)sdallocx;
nieweif94a7ce2022-03-16 10:02:19 +0800273 free(ptr);
274#else
Adam Langleyb49b78e2021-09-02 14:57:02 -0700275 if (sdallocx) {
276 sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
277 } else {
278 free(ptr);
279 }
nieweif94a7ce2022-03-16 10:02:19 +0800280#endif
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700281}
282
283void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
284 if (orig_ptr == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700285 return OPENSSL_malloc(new_size);
286 }
287
Adam Langley0313b592020-06-10 14:38:02 -0700288 size_t old_size;
289 if (OPENSSL_memory_get_size != NULL) {
290 old_size = OPENSSL_memory_get_size(orig_ptr);
291 } else {
292 void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
293 __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
294 old_size = *(size_t *)ptr;
295 __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
David Benjamin0ee31932016-07-11 19:38:56 -0400298 void *ret = OPENSSL_malloc(new_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700299 if (ret == NULL) {
300 return NULL;
301 }
302
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700303 size_t to_copy = new_size;
304 if (old_size < to_copy) {
305 to_copy = old_size;
306 }
307
308 memcpy(ret, orig_ptr, to_copy);
309 OPENSSL_free(orig_ptr);
310
Adam Langley95c29f32014-06-20 12:00:00 -0700311 return ret;
312}
313
Adam Langleyad1907f2014-07-30 11:55:17 -0700314void OPENSSL_cleanse(void *ptr, size_t len) {
Adam Langleyded93582014-07-31 15:23:51 -0700315#if defined(OPENSSL_WINDOWS)
David Benjaminc3774c12015-12-30 21:37:50 -0500316 SecureZeroMemory(ptr, len);
Adam Langleyded93582014-07-31 15:23:51 -0700317#else
David Benjamin17cf2cb2016-12-13 01:07:13 -0500318 OPENSSL_memset(ptr, 0, len);
Adam Langleyad1907f2014-07-30 11:55:17 -0700319
Adam Langleycf052cf2014-07-31 18:46:35 -0700320#if !defined(OPENSSL_NO_ASM)
Adam Langleyad1907f2014-07-30 11:55:17 -0700321 /* As best as we can tell, this is sufficient to break any optimisations that
322 might try to eliminate "superfluous" memsets. If there's an easy way to
323 detect memset_s, it would be better to use that. */
Adam Langleyad1907f2014-07-30 11:55:17 -0700324 __asm__ __volatile__("" : : "r"(ptr) : "memory");
325#endif
David Benjamin808f8322017-08-18 14:06:02 -0400326#endif // !OPENSSL_NO_ASM
Adam Langleyad1907f2014-07-30 11:55:17 -0700327}
328
Bob Beck350f8542023-02-07 16:11:58 -0700329void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); }
Jeremy Apthorp1fa5abc2019-03-04 11:09:13 -0800330
David Benjamin8a1542f2022-09-06 12:40:08 -0400331int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; }
332
333int CRYPTO_secure_malloc_initialized(void) { return 0; }
334
335size_t CRYPTO_secure_used(void) { return 0; }
336
337void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); }
338
339void OPENSSL_secure_clear_free(void *ptr, size_t len) {
340 OPENSSL_clear_free(ptr, len);
341}
342
Adam Langley95c29f32014-06-20 12:00:00 -0700343int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
Adam Langley95c29f32014-06-20 12:00:00 -0700344 const uint8_t *a = in_a;
345 const uint8_t *b = in_b;
346 uint8_t x = 0;
347
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400348 for (size_t i = 0; i < len; i++) {
Adam Langley95c29f32014-06-20 12:00:00 -0700349 x |= a[i] ^ b[i];
350 }
351
352 return x;
353}
354
355uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
David Benjamin808f8322017-08-18 14:06:02 -0400356 // These are the FNV-1a parameters for 32 bits.
Adam Langley95c29f32014-06-20 12:00:00 -0700357 static const uint32_t kPrime = 16777619u;
358 static const uint32_t kOffsetBasis = 2166136261u;
359
360 const uint8_t *in = ptr;
Adam Langley95c29f32014-06-20 12:00:00 -0700361 uint32_t h = kOffsetBasis;
362
David Benjamin0ee31932016-07-11 19:38:56 -0400363 for (size_t i = 0; i < len; i++) {
Adam Langley95c29f32014-06-20 12:00:00 -0700364 h ^= in[i];
365 h *= kPrime;
366 }
367
368 return h;
369}
370
David Benjaminec8c67d2021-06-21 17:10:53 -0400371uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); }
372
Adam Langley01797e32014-06-20 12:00:00 -0700373size_t OPENSSL_strnlen(const char *s, size_t len) {
David Benjamin0ee31932016-07-11 19:38:56 -0400374 for (size_t i = 0; i < len; i++) {
Adam Langley01797e32014-06-20 12:00:00 -0700375 if (s[i] == 0) {
376 return i;
377 }
378 }
379
380 return len;
381}
382
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700383char *OPENSSL_strdup(const char *s) {
David Benjamin3ba95862019-10-21 16:14:33 -0400384 if (s == NULL) {
385 return NULL;
386 }
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700387 const size_t len = strlen(s) + 1;
388 char *ret = OPENSSL_malloc(len);
389 if (ret == NULL) {
390 return NULL;
391 }
392 OPENSSL_memcpy(ret, s, len);
393 return ret;
394}
Adam Langleyccf80572017-07-25 14:49:30 -0700395
Bob Beck00c70b82023-02-01 12:41:49 -0700396int OPENSSL_isalpha(int c) {
397 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
398}
399
Bob Beck350f8542023-02-07 16:11:58 -0700400int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; }
Bob Beckf86a63c2023-01-30 12:17:39 -0700401
Bob Beck00c70b82023-02-01 12:41:49 -0700402int OPENSSL_isxdigit(int c) {
403 return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
404}
405
406int OPENSSL_fromxdigit(uint8_t *out, int c) {
407 if (OPENSSL_isdigit(c)) {
408 *out = c - '0';
409 return 1;
410 }
411 if ('a' <= c && c <= 'f') {
412 *out = c - 'a' + 10;
413 return 1;
414 }
415 if ('A' <= c && c <= 'F') {
416 *out = c - 'A' + 10;
417 return 1;
418 }
419 return 0;
420}
421
Bob Beck350f8542023-02-07 16:11:58 -0700422int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }
Bob Beck00c70b82023-02-01 12:41:49 -0700423
Adam Langleyccf80572017-07-25 14:49:30 -0700424int OPENSSL_tolower(int c) {
425 if (c >= 'A' && c <= 'Z') {
426 return c + ('a' - 'A');
427 }
428 return c;
429}
430
David Benjamin42b7b352023-01-27 21:02:34 -0500431int OPENSSL_isspace(int c) {
432 return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' ||
433 c == ' ';
434}
435
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700436int OPENSSL_strcasecmp(const char *a, const char *b) {
Adam Langleyccf80572017-07-25 14:49:30 -0700437 for (size_t i = 0;; i++) {
438 const int aa = OPENSSL_tolower(a[i]);
439 const int bb = OPENSSL_tolower(b[i]);
440
441 if (aa < bb) {
442 return -1;
443 } else if (aa > bb) {
444 return 1;
445 } else if (aa == 0) {
446 return 0;
447 }
448 }
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700449}
450
451int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
Adam Langleyccf80572017-07-25 14:49:30 -0700452 for (size_t i = 0; i < n; i++) {
453 const int aa = OPENSSL_tolower(a[i]);
454 const int bb = OPENSSL_tolower(b[i]);
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700455
Adam Langleyccf80572017-07-25 14:49:30 -0700456 if (aa < bb) {
457 return -1;
458 } else if (aa > bb) {
459 return 1;
460 } else if (aa == 0) {
461 return 0;
462 }
463 }
464
465 return 0;
466}
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700467
Adam Langley95c29f32014-06-20 12:00:00 -0700468int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
469 va_list args;
Adam Langley95c29f32014-06-20 12:00:00 -0700470 va_start(args, format);
David Benjamin0ee31932016-07-11 19:38:56 -0400471 int ret = BIO_vsnprintf(buf, n, format, args);
Adam Langley95c29f32014-06-20 12:00:00 -0700472 va_end(args);
473 return ret;
474}
475
476int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
477 return vsnprintf(buf, n, format, args);
478}
David Benjamin3ba95862019-10-21 16:14:33 -0400479
Bob Beck350f8542023-02-07 16:11:58 -0700480int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args,
481 int system_malloc) {
482 void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc;
483 void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free;
484 void *(*reallocate)(void *, size_t) =
485 system_malloc ? realloc : OPENSSL_realloc;
486 char *candidate = NULL;
487 size_t candidate_len = 64; // TODO(bbe) what's the best initial size?
488
489 if ((candidate = allocate(candidate_len)) == NULL) {
490 goto err;
491 }
492 va_list args_copy;
493 va_copy(args_copy, args);
494 int ret = vsnprintf(candidate, candidate_len, format, args_copy);
495 va_end(args_copy);
Bob Beckdcabfe22023-02-07 19:06:08 -0700496 if (ret < 0) {
Bob Beck350f8542023-02-07 16:11:58 -0700497 goto err;
498 }
499 if ((size_t)ret >= candidate_len) {
500 // Too big to fit in allocation.
501 char *tmp;
502
Bob Beckdcabfe22023-02-07 19:06:08 -0700503 candidate_len = (size_t)ret + 1;
Bob Beck350f8542023-02-07 16:11:58 -0700504 if ((tmp = reallocate(candidate, candidate_len)) == NULL) {
505 goto err;
506 }
507 candidate = tmp;
Bob Beckdcabfe22023-02-07 19:06:08 -0700508 ret = vsnprintf(candidate, candidate_len, format, args);
Bob Beck350f8542023-02-07 16:11:58 -0700509 }
Bob Beckdcabfe22023-02-07 19:06:08 -0700510 // At this point this should not happen unless vsnprintf is insane.
Bob Beck350f8542023-02-07 16:11:58 -0700511 if (ret < 0 || (size_t)ret >= candidate_len) {
512 goto err;
513 }
514 *str = candidate;
515 return ret;
516
517 err:
518 deallocate(candidate);
519 *str = NULL;
520 errno = ENOMEM;
521 return -1;
522}
523
524int OPENSSL_vasprintf(char **str, const char *format, va_list args) {
525 return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0);
526}
527
528int OPENSSL_asprintf(char **str, const char *format, ...) {
529 va_list args;
530 va_start(args, format);
531 int ret = OPENSSL_vasprintf(str, format, args);
532 va_end(args);
533 return ret;
534}
535
David Benjamin3ba95862019-10-21 16:14:33 -0400536char *OPENSSL_strndup(const char *str, size_t size) {
David Benjamin3ba95862019-10-21 16:14:33 -0400537 size = OPENSSL_strnlen(str, size);
538
David Benjamin5984cfe2021-08-24 16:03:34 -0400539 size_t alloc_size = size + 1;
David Benjamin3ba95862019-10-21 16:14:33 -0400540 if (alloc_size < size) {
541 // overflow
542 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
543 return NULL;
544 }
David Benjamin5984cfe2021-08-24 16:03:34 -0400545 char *ret = OPENSSL_malloc(alloc_size);
David Benjamin3ba95862019-10-21 16:14:33 -0400546 if (ret == NULL) {
David Benjamin3ba95862019-10-21 16:14:33 -0400547 return NULL;
548 }
549
550 OPENSSL_memcpy(ret, str, size);
551 ret[size] = '\0';
552 return ret;
553}
554
555size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
556 size_t l = 0;
557
558 for (; dst_size > 1 && *src; dst_size--) {
559 *dst++ = *src++;
560 l++;
561 }
562
563 if (dst_size) {
564 *dst = 0;
565 }
566
567 return l + strlen(src);
568}
569
570size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
571 size_t l = 0;
572 for (; dst_size > 0 && *dst; dst_size--, dst++) {
573 l++;
574 }
575 return l + OPENSSL_strlcpy(dst, src, dst_size);
576}
577
578void *OPENSSL_memdup(const void *data, size_t size) {
579 if (size == 0) {
580 return NULL;
581 }
582
583 void *ret = OPENSSL_malloc(size);
584 if (ret == NULL) {
David Benjamin3ba95862019-10-21 16:14:33 -0400585 return NULL;
586 }
587
588 OPENSSL_memcpy(ret, data, size);
589 return ret;
590}
David Benjamin551ccd72021-09-28 11:55:10 -0400591
592void *CRYPTO_malloc(size_t size, const char *file, int line) {
593 return OPENSSL_malloc(size);
594}
595
596void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) {
597 return OPENSSL_realloc(ptr, new_size);
598}
599
600void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); }