blob: 0491f150b4946bf02d594703bda4120c84c3826d [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>
60#include <stdarg.h>
61#include <stdio.h>
Adam Langley95c29f32014-06-20 12:00:00 -070062
David Benjamin3ba95862019-10-21 16:14:33 -040063#include <openssl/err.h>
64
Adam Langleyded93582014-07-31 15:23:51 -070065#if defined(OPENSSL_WINDOWS)
David Benjamina353cdb2016-06-09 16:48:33 -040066OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -070067#include <windows.h>
David Benjamin054e5972016-06-16 12:08:26 -040068OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langleyded93582014-07-31 15:23:51 -070069#endif
70
David Benjamin17cf2cb2016-12-13 01:07:13 -050071#include "internal.h"
72
Adam Langley95c29f32014-06-20 12:00:00 -070073
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070074#define OPENSSL_MALLOC_PREFIX 8
Adam Langley0313b592020-06-10 14:38:02 -070075OPENSSL_STATIC_ASSERT(OPENSSL_MALLOC_PREFIX >= sizeof(size_t),
76 "size_t too large");
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -070077
David Benjaminda8bb842019-02-26 22:13:28 -060078#if defined(OPENSSL_ASAN)
79void __asan_poison_memory_region(const volatile void *addr, size_t size);
80void __asan_unpoison_memory_region(const volatile void *addr, size_t size);
81#else
82static void __asan_poison_memory_region(const void *addr, size_t size) {}
83static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
84#endif
85
John Sheu787b26c2019-05-03 12:08:12 -070086// Windows doesn't really support weak symbols as of May 2019, and Clang on
87// Windows will emit strong symbols instead. See
88// https://bugs.llvm.org/show_bug.cgi?id=37598
Adam Langley0cf14d32020-03-30 09:24:45 -070089#if defined(__ELF__) && defined(__GNUC__)
90#define WEAK_SYMBOL_FUNC(rettype, name, args) \
91 rettype name args __attribute__((weak));
Wiktor Garbacz9ae40ce2020-02-05 18:14:20 +010092#else
Adam Langley0cf14d32020-03-30 09:24:45 -070093#define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL;
Wiktor Garbacz9ae40ce2020-02-05 18:14:20 +010094#endif
95
Chris Kennellyb5e4a222018-09-10 11:47:15 -040096// sdallocx is a sized |free| function. By passing the size (which we happen to
97// always know in BoringSSL), the malloc implementation can save work. We cannot
Adam Langley0cf14d32020-03-30 09:24:45 -070098// depend on |sdallocx| being available, however, so it's a weak symbol.
Chris Kennellyb5e4a222018-09-10 11:47:15 -040099//
100// This will always be safe, but will only be overridden if the malloc
101// implementation is statically linked with BoringSSL. So, if |sdallocx| is
102// provided in, say, libc.so, we still won't use it because that's dynamically
103// linked. This isn't an ideal result, but its helps in some cases.
Adam Langley0cf14d32020-03-30 09:24:45 -0700104WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags));
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700105
Adam Langley0313b592020-06-10 14:38:02 -0700106// The following three functions can be defined to override default heap
107// allocation and freeing. If defined, it is the responsibility of
108// |OPENSSL_memory_free| to zero out the memory before returning it to the
109// system. |OPENSSL_memory_free| will not be passed NULL pointers.
110WEAK_SYMBOL_FUNC(void*, OPENSSL_memory_alloc, (size_t size));
111WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr));
112WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr));
Wiktor Garbacz9ae40ce2020-02-05 18:14:20 +0100113
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700114void *OPENSSL_malloc(size_t size) {
Adam Langley0313b592020-06-10 14:38:02 -0700115 if (OPENSSL_memory_alloc != NULL) {
116 assert(OPENSSL_memory_free != NULL);
117 assert(OPENSSL_memory_get_size != NULL);
118 return OPENSSL_memory_alloc(size);
119 }
120
Adam Langley7964a1d2020-02-05 15:23:07 -0800121 if (size + OPENSSL_MALLOC_PREFIX < size) {
122 return NULL;
123 }
124
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700125 void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
Adam Langley95c29f32014-06-20 12:00:00 -0700126 if (ptr == NULL) {
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700127 return NULL;
128 }
129
130 *(size_t *)ptr = size;
131
David Benjaminda8bb842019-02-26 22:13:28 -0600132 __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700133 return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
134}
135
136void OPENSSL_free(void *orig_ptr) {
137 if (orig_ptr == NULL) {
138 return;
139 }
140
Adam Langley0313b592020-06-10 14:38:02 -0700141 if (OPENSSL_memory_free != NULL) {
142 OPENSSL_memory_free(orig_ptr);
143 return;
144 }
145
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700146 void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
David Benjaminda8bb842019-02-26 22:13:28 -0600147 __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700148
149 size_t size = *(size_t *)ptr;
150 OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
Adam Langley0cf14d32020-03-30 09:24:45 -0700151 if (sdallocx) {
152 sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
153 } else {
154 free(ptr);
155 }
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700156}
157
158void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
159 if (orig_ptr == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700160 return OPENSSL_malloc(new_size);
161 }
162
Adam Langley0313b592020-06-10 14:38:02 -0700163 size_t old_size;
164 if (OPENSSL_memory_get_size != NULL) {
165 old_size = OPENSSL_memory_get_size(orig_ptr);
166 } else {
167 void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
168 __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
169 old_size = *(size_t *)ptr;
170 __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
171 }
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjamin0ee31932016-07-11 19:38:56 -0400173 void *ret = OPENSSL_malloc(new_size);
Adam Langley95c29f32014-06-20 12:00:00 -0700174 if (ret == NULL) {
175 return NULL;
176 }
177
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700178 size_t to_copy = new_size;
179 if (old_size < to_copy) {
180 to_copy = old_size;
181 }
182
183 memcpy(ret, orig_ptr, to_copy);
184 OPENSSL_free(orig_ptr);
185
Adam Langley95c29f32014-06-20 12:00:00 -0700186 return ret;
187}
188
Adam Langleyad1907f2014-07-30 11:55:17 -0700189void OPENSSL_cleanse(void *ptr, size_t len) {
Adam Langleyded93582014-07-31 15:23:51 -0700190#if defined(OPENSSL_WINDOWS)
David Benjaminc3774c12015-12-30 21:37:50 -0500191 SecureZeroMemory(ptr, len);
Adam Langleyded93582014-07-31 15:23:51 -0700192#else
David Benjamin17cf2cb2016-12-13 01:07:13 -0500193 OPENSSL_memset(ptr, 0, len);
Adam Langleyad1907f2014-07-30 11:55:17 -0700194
Adam Langleycf052cf2014-07-31 18:46:35 -0700195#if !defined(OPENSSL_NO_ASM)
Adam Langleyad1907f2014-07-30 11:55:17 -0700196 /* As best as we can tell, this is sufficient to break any optimisations that
197 might try to eliminate "superfluous" memsets. If there's an easy way to
198 detect memset_s, it would be better to use that. */
Adam Langleyad1907f2014-07-30 11:55:17 -0700199 __asm__ __volatile__("" : : "r"(ptr) : "memory");
200#endif
David Benjamin808f8322017-08-18 14:06:02 -0400201#endif // !OPENSSL_NO_ASM
Adam Langleyad1907f2014-07-30 11:55:17 -0700202}
203
Jeremy Apthorp1fa5abc2019-03-04 11:09:13 -0800204void OPENSSL_clear_free(void *ptr, size_t unused) {
205 OPENSSL_free(ptr);
206}
207
Adam Langley95c29f32014-06-20 12:00:00 -0700208int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
Adam Langley95c29f32014-06-20 12:00:00 -0700209 const uint8_t *a = in_a;
210 const uint8_t *b = in_b;
211 uint8_t x = 0;
212
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400213 for (size_t i = 0; i < len; i++) {
Adam Langley95c29f32014-06-20 12:00:00 -0700214 x |= a[i] ^ b[i];
215 }
216
217 return x;
218}
219
220uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
David Benjamin808f8322017-08-18 14:06:02 -0400221 // These are the FNV-1a parameters for 32 bits.
Adam Langley95c29f32014-06-20 12:00:00 -0700222 static const uint32_t kPrime = 16777619u;
223 static const uint32_t kOffsetBasis = 2166136261u;
224
225 const uint8_t *in = ptr;
Adam Langley95c29f32014-06-20 12:00:00 -0700226 uint32_t h = kOffsetBasis;
227
David Benjamin0ee31932016-07-11 19:38:56 -0400228 for (size_t i = 0; i < len; i++) {
Adam Langley95c29f32014-06-20 12:00:00 -0700229 h ^= in[i];
230 h *= kPrime;
231 }
232
233 return h;
234}
235
Adam Langley01797e32014-06-20 12:00:00 -0700236size_t OPENSSL_strnlen(const char *s, size_t len) {
David Benjamin0ee31932016-07-11 19:38:56 -0400237 for (size_t i = 0; i < len; i++) {
Adam Langley01797e32014-06-20 12:00:00 -0700238 if (s[i] == 0) {
239 return i;
240 }
241 }
242
243 return len;
244}
245
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700246char *OPENSSL_strdup(const char *s) {
David Benjamin3ba95862019-10-21 16:14:33 -0400247 if (s == NULL) {
248 return NULL;
249 }
Martin Kreichgauerc0e15d12017-08-18 14:24:36 -0700250 const size_t len = strlen(s) + 1;
251 char *ret = OPENSSL_malloc(len);
252 if (ret == NULL) {
253 return NULL;
254 }
255 OPENSSL_memcpy(ret, s, len);
256 return ret;
257}
Adam Langleyccf80572017-07-25 14:49:30 -0700258
259int OPENSSL_tolower(int c) {
260 if (c >= 'A' && c <= 'Z') {
261 return c + ('a' - 'A');
262 }
263 return c;
264}
265
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700266int OPENSSL_strcasecmp(const char *a, const char *b) {
Adam Langleyccf80572017-07-25 14:49:30 -0700267 for (size_t i = 0;; i++) {
268 const int aa = OPENSSL_tolower(a[i]);
269 const int bb = OPENSSL_tolower(b[i]);
270
271 if (aa < bb) {
272 return -1;
273 } else if (aa > bb) {
274 return 1;
275 } else if (aa == 0) {
276 return 0;
277 }
278 }
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700279}
280
281int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
Adam Langleyccf80572017-07-25 14:49:30 -0700282 for (size_t i = 0; i < n; i++) {
283 const int aa = OPENSSL_tolower(a[i]);
284 const int bb = OPENSSL_tolower(b[i]);
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700285
Adam Langleyccf80572017-07-25 14:49:30 -0700286 if (aa < bb) {
287 return -1;
288 } else if (aa > bb) {
289 return 1;
290 } else if (aa == 0) {
291 return 0;
292 }
293 }
294
295 return 0;
296}
Adam Langleyb0d5fb62014-06-20 12:00:00 -0700297
Adam Langley95c29f32014-06-20 12:00:00 -0700298int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
299 va_list args;
Adam Langley95c29f32014-06-20 12:00:00 -0700300 va_start(args, format);
David Benjamin0ee31932016-07-11 19:38:56 -0400301 int ret = BIO_vsnprintf(buf, n, format, args);
Adam Langley95c29f32014-06-20 12:00:00 -0700302 va_end(args);
303 return ret;
304}
305
306int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
307 return vsnprintf(buf, n, format, args);
308}
David Benjamin3ba95862019-10-21 16:14:33 -0400309
310char *OPENSSL_strndup(const char *str, size_t size) {
311 char *ret;
312 size_t alloc_size;
313
314 if (str == NULL) {
315 return NULL;
316 }
317
318 size = OPENSSL_strnlen(str, size);
319
320 alloc_size = size + 1;
321 if (alloc_size < size) {
322 // overflow
323 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
324 return NULL;
325 }
326 ret = OPENSSL_malloc(alloc_size);
327 if (ret == NULL) {
328 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
329 return NULL;
330 }
331
332 OPENSSL_memcpy(ret, str, size);
333 ret[size] = '\0';
334 return ret;
335}
336
337size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
338 size_t l = 0;
339
340 for (; dst_size > 1 && *src; dst_size--) {
341 *dst++ = *src++;
342 l++;
343 }
344
345 if (dst_size) {
346 *dst = 0;
347 }
348
349 return l + strlen(src);
350}
351
352size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
353 size_t l = 0;
354 for (; dst_size > 0 && *dst; dst_size--, dst++) {
355 l++;
356 }
357 return l + OPENSSL_strlcpy(dst, src, dst_size);
358}
359
360void *OPENSSL_memdup(const void *data, size_t size) {
361 if (size == 0) {
362 return NULL;
363 }
364
365 void *ret = OPENSSL_malloc(size);
366 if (ret == NULL) {
367 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
368 return NULL;
369 }
370
371 OPENSSL_memcpy(ret, data, size);
372 return ret;
373}