• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
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 
57 #include <openssl/mem.h>
58 
59 #include <assert.h>
60 #include <errno.h>
61 #include <limits.h>
62 #include <stdarg.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 
66 #include <openssl/err.h>
67 
68 #if defined(OPENSSL_WINDOWS)
69 OPENSSL_MSVC_PRAGMA(warning(push, 3))
70 #include <windows.h>
71 OPENSSL_MSVC_PRAGMA(warning(pop))
72 #endif
73 
74 #if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
75 #include <errno.h>
76 #include <signal.h>
77 #include <unistd.h>
78 #endif
79 
80 #include "internal.h"
81 
82 
83 #define OPENSSL_MALLOC_PREFIX 8
84 static_assert(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), "size_t too large");
85 
86 #if defined(OPENSSL_ASAN)
87 extern "C" {
88 void __asan_poison_memory_region(const volatile void *addr, size_t size);
89 void __asan_unpoison_memory_region(const volatile void *addr, size_t size);
90 }
91 #else
__asan_poison_memory_region(const void * addr,size_t size)92 static void __asan_poison_memory_region(const void *addr, size_t size) {}
__asan_unpoison_memory_region(const void * addr,size_t size)93 static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
94 #endif
95 
96 // Windows doesn't really support weak symbols as of May 2019, and Clang on
97 // Windows will emit strong symbols instead. See
98 // https://bugs.llvm.org/show_bug.cgi?id=37598
99 //
100 // EDK2 targets UEFI but builds as ELF and then translates the binary to
101 // COFF(!). Thus it builds with __ELF__ defined but cannot actually cope with
102 // weak symbols.
103 #if !defined(__EDK2_BORINGSSL__) && defined(__ELF__) && defined(__GNUC__)
104 #define WEAK_SYMBOL_FUNC(rettype, name, args) \
105   extern "C" {                                \
106   rettype name args __attribute__((weak));    \
107   }
108 #else
109 #define WEAK_SYMBOL_FUNC(rettype, name, args) \
110   static rettype(*const name) args = NULL;
111 #endif
112 
113 #if defined(BORINGSSL_DETECT_SDALLOCX)
114 // sdallocx is a sized |free| function. By passing the size (which we happen to
115 // always know in BoringSSL), the malloc implementation can save work. We cannot
116 // depend on |sdallocx| being available, however, so it's a weak symbol.
117 //
118 // This mechanism is kept opt-in because it assumes that, when |sdallocx| is
119 // defined, it is part of the same allocator as |malloc|. This is usually true
120 // but may break if |malloc| does not implement |sdallocx|, but some other
121 // allocator with |sdallocx| is imported which does.
122 WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags))
123 #else
124 static void (*const sdallocx)(void *ptr, size_t size, int flags) = NULL;
125 #endif
126 
127 // The following three functions can be defined to override default heap
128 // allocation and freeing. If defined, it is the responsibility of
129 // |OPENSSL_memory_free| to zero out the memory before returning it to the
130 // system. |OPENSSL_memory_free| will not be passed NULL pointers.
131 //
132 // WARNING: These functions are called on every allocation and free in
133 // BoringSSL across the entire process. They may be called by any code in the
134 // process which calls BoringSSL, including in process initializers and thread
135 // destructors. When called, BoringSSL may hold pthreads locks. Any other code
136 // in the process which, directly or indirectly, calls BoringSSL may be on the
137 // call stack and may itself be using arbitrary synchronization primitives.
138 //
139 // As a result, these functions may not have the usual programming environment
140 // available to most C or C++ code. In particular, they may not call into
141 // BoringSSL, or any library which depends on BoringSSL. Any synchronization
142 // primitives used must tolerate every other synchronization primitive linked
143 // into the process, including pthreads locks. Failing to meet these constraints
144 // may result in deadlocks, crashes, or memory corruption.
145 WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size))
146 WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr))
147 WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr))
148 
149 #if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
150 static CRYPTO_MUTEX malloc_failure_lock = CRYPTO_MUTEX_INIT;
151 static uint64_t current_malloc_count = 0;
152 static uint64_t malloc_number_to_fail = 0;
153 static int malloc_failure_enabled = 0, break_on_malloc_fail = 0,
154            any_malloc_failed = 0, disable_malloc_failures = 0;
155 
malloc_exit_handler(void)156 static void malloc_exit_handler(void) {
157   CRYPTO_MUTEX_lock_read(&malloc_failure_lock);
158   if (any_malloc_failed) {
159     // Signal to the test driver that some allocation failed, so it knows to
160     // increment the counter and continue.
161     _exit(88);
162   }
163   CRYPTO_MUTEX_unlock_read(&malloc_failure_lock);
164 }
165 
init_malloc_failure(void)166 static void init_malloc_failure(void) {
167   const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
168   if (env != NULL && env[0] != 0) {
169     char *endptr;
170     malloc_number_to_fail = strtoull(env, &endptr, 10);
171     if (*endptr == 0) {
172       malloc_failure_enabled = 1;
173       atexit(malloc_exit_handler);
174     }
175   }
176   break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL;
177 }
178 
179 // should_fail_allocation returns one if the current allocation should fail and
180 // zero otherwise.
should_fail_allocation()181 static int should_fail_allocation() {
182   static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
183   CRYPTO_once(&once, init_malloc_failure);
184   if (!malloc_failure_enabled || disable_malloc_failures) {
185     return 0;
186   }
187 
188   // We lock just so multi-threaded tests are still correct, but we won't test
189   // every malloc exhaustively.
190   CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
191   int should_fail = current_malloc_count == malloc_number_to_fail;
192   current_malloc_count++;
193   any_malloc_failed = any_malloc_failed || should_fail;
194   CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
195 
196   if (should_fail && break_on_malloc_fail) {
197     raise(SIGTRAP);
198   }
199   if (should_fail) {
200     errno = ENOMEM;
201   }
202   return should_fail;
203 }
204 
OPENSSL_reset_malloc_counter_for_testing(void)205 void OPENSSL_reset_malloc_counter_for_testing(void) {
206   CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
207   current_malloc_count = 0;
208   CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
209 }
210 
OPENSSL_disable_malloc_failures_for_testing(void)211 void OPENSSL_disable_malloc_failures_for_testing(void) {
212   CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
213   BSSL_CHECK(!disable_malloc_failures);
214   disable_malloc_failures = 1;
215   CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
216 }
217 
OPENSSL_enable_malloc_failures_for_testing(void)218 void OPENSSL_enable_malloc_failures_for_testing(void) {
219   CRYPTO_MUTEX_lock_write(&malloc_failure_lock);
220   BSSL_CHECK(disable_malloc_failures);
221   disable_malloc_failures = 0;
222   CRYPTO_MUTEX_unlock_write(&malloc_failure_lock);
223 }
224 
225 #else
226 static int should_fail_allocation(void) { return 0; }
227 #endif
228 
OPENSSL_malloc(size_t size)229 void *OPENSSL_malloc(size_t size) {
230   void *ptr = nullptr;
231   if (should_fail_allocation()) {
232     goto err;
233   }
234 
235   if (OPENSSL_memory_alloc != NULL) {
236     assert(OPENSSL_memory_free != NULL);
237     assert(OPENSSL_memory_get_size != NULL);
238     void *ptr2 = OPENSSL_memory_alloc(size);
239     if (ptr2 == NULL && size != 0) {
240       goto err;
241     }
242     return ptr2;
243   }
244 
245   if (size + OPENSSL_MALLOC_PREFIX < size) {
246     goto err;
247   }
248 
249   ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
250   if (ptr == NULL) {
251     goto err;
252   }
253 
254   *(size_t *)ptr = size;
255 
256   __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
257   return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
258 
259 err:
260   // This only works because ERR does not call OPENSSL_malloc.
261   OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
262   return NULL;
263 }
264 
OPENSSL_zalloc(size_t size)265 void *OPENSSL_zalloc(size_t size) {
266   void *ret = OPENSSL_malloc(size);
267   if (ret != NULL) {
268     OPENSSL_memset(ret, 0, size);
269   }
270   return ret;
271 }
272 
OPENSSL_calloc(size_t num,size_t size)273 void *OPENSSL_calloc(size_t num, size_t size) {
274   if (size != 0 && num > SIZE_MAX / size) {
275     OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
276     return NULL;
277   }
278 
279   return OPENSSL_zalloc(num * size);
280 }
281 
OPENSSL_free(void * orig_ptr)282 void OPENSSL_free(void *orig_ptr) {
283   if (orig_ptr == NULL) {
284     return;
285   }
286 
287   if (OPENSSL_memory_free != NULL) {
288     OPENSSL_memory_free(orig_ptr);
289     return;
290   }
291 
292   void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
293   __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
294 
295   size_t size = *(size_t *)ptr;
296   OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
297 
298 // ASan knows to intercept malloc and free, but not sdallocx.
299 #if defined(OPENSSL_ASAN)
300   (void)sdallocx;
301   free(ptr);
302 #else
303   if (sdallocx) {
304     sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
305   } else {
306     free(ptr);
307   }
308 #endif
309 }
310 
OPENSSL_realloc(void * orig_ptr,size_t new_size)311 void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
312   if (orig_ptr == NULL) {
313     return OPENSSL_malloc(new_size);
314   }
315 
316   size_t old_size;
317   if (OPENSSL_memory_get_size != NULL) {
318     old_size = OPENSSL_memory_get_size(orig_ptr);
319   } else {
320     void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
321     __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
322     old_size = *(size_t *)ptr;
323     __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
324   }
325 
326   void *ret = OPENSSL_malloc(new_size);
327   if (ret == NULL) {
328     return NULL;
329   }
330 
331   size_t to_copy = new_size;
332   if (old_size < to_copy) {
333     to_copy = old_size;
334   }
335 
336   memcpy(ret, orig_ptr, to_copy);
337   OPENSSL_free(orig_ptr);
338 
339   return ret;
340 }
341 
OPENSSL_cleanse(void * ptr,size_t len)342 void OPENSSL_cleanse(void *ptr, size_t len) {
343 #if defined(OPENSSL_WINDOWS)
344   SecureZeroMemory(ptr, len);
345 #else
346   OPENSSL_memset(ptr, 0, len);
347 
348 #if !defined(OPENSSL_NO_ASM)
349   /* As best as we can tell, this is sufficient to break any optimisations that
350      might try to eliminate "superfluous" memsets. If there's an easy way to
351      detect memset_s, it would be better to use that. */
352   __asm__ __volatile__("" : : "r"(ptr) : "memory");
353 #endif
354 #endif  // !OPENSSL_NO_ASM
355 }
356 
OPENSSL_clear_free(void * ptr,size_t unused)357 void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); }
358 
CRYPTO_secure_malloc_init(size_t size,size_t min_size)359 int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; }
360 
CRYPTO_secure_malloc_initialized(void)361 int CRYPTO_secure_malloc_initialized(void) { return 0; }
362 
CRYPTO_secure_used(void)363 size_t CRYPTO_secure_used(void) { return 0; }
364 
OPENSSL_secure_malloc(size_t size)365 void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); }
366 
OPENSSL_secure_clear_free(void * ptr,size_t len)367 void OPENSSL_secure_clear_free(void *ptr, size_t len) {
368   OPENSSL_clear_free(ptr, len);
369 }
370 
CRYPTO_memcmp(const void * in_a,const void * in_b,size_t len)371 int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
372   const uint8_t *a = reinterpret_cast<const uint8_t *>(in_a);
373   const uint8_t *b = reinterpret_cast<const uint8_t *>(in_b);
374   uint8_t x = 0;
375 
376   for (size_t i = 0; i < len; i++) {
377     x |= a[i] ^ b[i];
378   }
379 
380   return x;
381 }
382 
OPENSSL_hash32(const void * ptr,size_t len)383 uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
384   // These are the FNV-1a parameters for 32 bits.
385   static const uint32_t kPrime = 16777619u;
386   static const uint32_t kOffsetBasis = 2166136261u;
387 
388   const uint8_t *in = reinterpret_cast<const uint8_t *>(ptr);
389   uint32_t h = kOffsetBasis;
390 
391   for (size_t i = 0; i < len; i++) {
392     h ^= in[i];
393     h *= kPrime;
394   }
395 
396   return h;
397 }
398 
OPENSSL_strhash(const char * s)399 uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); }
400 
OPENSSL_strnlen(const char * s,size_t len)401 size_t OPENSSL_strnlen(const char *s, size_t len) {
402   for (size_t i = 0; i < len; i++) {
403     if (s[i] == 0) {
404       return i;
405     }
406   }
407 
408   return len;
409 }
410 
OPENSSL_strdup(const char * s)411 char *OPENSSL_strdup(const char *s) {
412   if (s == NULL) {
413     return NULL;
414   }
415   // Copy the NUL terminator.
416   return reinterpret_cast<char *>(OPENSSL_memdup(s, strlen(s) + 1));
417 }
418 
OPENSSL_isalpha(int c)419 int OPENSSL_isalpha(int c) {
420   return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
421 }
422 
OPENSSL_isdigit(int c)423 int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; }
424 
OPENSSL_isxdigit(int c)425 int OPENSSL_isxdigit(int c) {
426   return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
427 }
428 
OPENSSL_fromxdigit(uint8_t * out,int c)429 int OPENSSL_fromxdigit(uint8_t *out, int c) {
430   if (OPENSSL_isdigit(c)) {
431     *out = c - '0';
432     return 1;
433   }
434   if ('a' <= c && c <= 'f') {
435     *out = c - 'a' + 10;
436     return 1;
437   }
438   if ('A' <= c && c <= 'F') {
439     *out = c - 'A' + 10;
440     return 1;
441   }
442   return 0;
443 }
444 
OPENSSL_isalnum(int c)445 int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }
446 
OPENSSL_tolower(int c)447 int OPENSSL_tolower(int c) {
448   if (c >= 'A' && c <= 'Z') {
449     return c + ('a' - 'A');
450   }
451   return c;
452 }
453 
OPENSSL_isspace(int c)454 int OPENSSL_isspace(int c) {
455   return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' ||
456          c == ' ';
457 }
458 
OPENSSL_strcasecmp(const char * a,const char * b)459 int OPENSSL_strcasecmp(const char *a, const char *b) {
460   for (size_t i = 0;; i++) {
461     const int aa = OPENSSL_tolower(a[i]);
462     const int bb = OPENSSL_tolower(b[i]);
463 
464     if (aa < bb) {
465       return -1;
466     } else if (aa > bb) {
467       return 1;
468     } else if (aa == 0) {
469       return 0;
470     }
471   }
472 }
473 
OPENSSL_strncasecmp(const char * a,const char * b,size_t n)474 int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
475   for (size_t i = 0; i < n; i++) {
476     const int aa = OPENSSL_tolower(a[i]);
477     const int bb = OPENSSL_tolower(b[i]);
478 
479     if (aa < bb) {
480       return -1;
481     } else if (aa > bb) {
482       return 1;
483     } else if (aa == 0) {
484       return 0;
485     }
486   }
487 
488   return 0;
489 }
490 
BIO_snprintf(char * buf,size_t n,const char * format,...)491 int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
492   va_list args;
493   va_start(args, format);
494   int ret = BIO_vsnprintf(buf, n, format, args);
495   va_end(args);
496   return ret;
497 }
498 
BIO_vsnprintf(char * buf,size_t n,const char * format,va_list args)499 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
500   return vsnprintf(buf, n, format, args);
501 }
502 
OPENSSL_vasprintf_internal(char ** str,const char * format,va_list args,int system_malloc)503 int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args,
504                                int system_malloc) {
505   void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc;
506   void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free;
507   void *(*reallocate)(void *, size_t) =
508       system_malloc ? realloc : OPENSSL_realloc;
509   char *candidate = NULL;
510   size_t candidate_len = 64;  // TODO(bbe) what's the best initial size?
511   int ret;
512 
513   if ((candidate = reinterpret_cast<char *>(allocate(candidate_len))) == NULL) {
514     goto err;
515   }
516   va_list args_copy;
517   va_copy(args_copy, args);
518   ret = vsnprintf(candidate, candidate_len, format, args_copy);
519   va_end(args_copy);
520   if (ret < 0) {
521     goto err;
522   }
523   if ((size_t)ret >= candidate_len) {
524     // Too big to fit in allocation.
525     char *tmp;
526 
527     candidate_len = (size_t)ret + 1;
528     if ((tmp = reinterpret_cast<char *>(
529              reallocate(candidate, candidate_len))) == NULL) {
530       goto err;
531     }
532     candidate = tmp;
533     ret = vsnprintf(candidate, candidate_len, format, args);
534   }
535   // At this point this should not happen unless vsnprintf is insane.
536   if (ret < 0 || (size_t)ret >= candidate_len) {
537     goto err;
538   }
539   *str = candidate;
540   return ret;
541 
542 err:
543   deallocate(candidate);
544   *str = NULL;
545   errno = ENOMEM;
546   return -1;
547 }
548 
OPENSSL_vasprintf(char ** str,const char * format,va_list args)549 int OPENSSL_vasprintf(char **str, const char *format, va_list args) {
550   return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0);
551 }
552 
OPENSSL_asprintf(char ** str,const char * format,...)553 int OPENSSL_asprintf(char **str, const char *format, ...) {
554   va_list args;
555   va_start(args, format);
556   int ret = OPENSSL_vasprintf(str, format, args);
557   va_end(args);
558   return ret;
559 }
560 
OPENSSL_strndup(const char * str,size_t size)561 char *OPENSSL_strndup(const char *str, size_t size) {
562   size = OPENSSL_strnlen(str, size);
563 
564   size_t alloc_size = size + 1;
565   if (alloc_size < size) {
566     // overflow
567     OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
568     return NULL;
569   }
570   char *ret = reinterpret_cast<char *>(OPENSSL_malloc(alloc_size));
571   if (ret == NULL) {
572     return NULL;
573   }
574 
575   OPENSSL_memcpy(ret, str, size);
576   ret[size] = '\0';
577   return ret;
578 }
579 
OPENSSL_strlcpy(char * dst,const char * src,size_t dst_size)580 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
581   size_t l = 0;
582 
583   for (; dst_size > 1 && *src; dst_size--) {
584     *dst++ = *src++;
585     l++;
586   }
587 
588   if (dst_size) {
589     *dst = 0;
590   }
591 
592   return l + strlen(src);
593 }
594 
OPENSSL_strlcat(char * dst,const char * src,size_t dst_size)595 size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
596   size_t l = 0;
597   for (; dst_size > 0 && *dst; dst_size--, dst++) {
598     l++;
599   }
600   return l + OPENSSL_strlcpy(dst, src, dst_size);
601 }
602 
OPENSSL_memdup(const void * data,size_t size)603 void *OPENSSL_memdup(const void *data, size_t size) {
604   if (size == 0) {
605     return NULL;
606   }
607 
608   void *ret = OPENSSL_malloc(size);
609   if (ret == NULL) {
610     return NULL;
611   }
612 
613   OPENSSL_memcpy(ret, data, size);
614   return ret;
615 }
616 
CRYPTO_malloc(size_t size,const char * file,int line)617 void *CRYPTO_malloc(size_t size, const char *file, int line) {
618   return OPENSSL_malloc(size);
619 }
620 
CRYPTO_realloc(void * ptr,size_t new_size,const char * file,int line)621 void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) {
622   return OPENSSL_realloc(ptr, new_size);
623 }
624 
CRYPTO_free(void * ptr,const char * file,int line)625 void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); }
626