• 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 void __asan_poison_memory_region(const volatile void *addr, size_t size);
88 void __asan_unpoison_memory_region(const volatile void *addr, size_t size);
89 #else
__asan_poison_memory_region(const void * addr,size_t size)90 static void __asan_poison_memory_region(const void *addr, size_t size) {}
__asan_unpoison_memory_region(const void * addr,size_t size)91 static void __asan_unpoison_memory_region(const void *addr, size_t size) {}
92 #endif
93 
94 // 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
97 #if defined(__ELF__) && defined(__GNUC__)
98 #define WEAK_SYMBOL_FUNC(rettype, name, args) \
99   rettype name args __attribute__((weak));
100 #else
101 #define WEAK_SYMBOL_FUNC(rettype, name, args) static rettype(*name) args = NULL;
102 #endif
103 
104 // sdallocx is a sized |free| function. By passing the size (which we happen to
105 // 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.
107 //
108 // 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.
112 WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags));
113 
114 // 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.
118 //
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.
132 WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size));
133 WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr));
134 WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr));
135 
136 // kBoringSSLBinaryTag is a distinctive byte sequence to identify binaries that
137 // are linking in BoringSSL and, roughly, what version they are using.
138 static const uint8_t kBoringSSLBinaryTag[18] = {
139     // 16 bytes of magic tag.
140     0x8c,
141     0x62,
142     0x20,
143     0x0b,
144     0xd2,
145     0xa0,
146     0x72,
147     0x58,
148     0x44,
149     0xa8,
150     0x96,
151     0x69,
152     0xad,
153     0x55,
154     0x7e,
155     0xec,
156     // Current source iteration. Incremented ~monthly.
157     3,
158     0,
159 };
160 
161 #if defined(BORINGSSL_MALLOC_FAILURE_TESTING)
162 static struct CRYPTO_STATIC_MUTEX malloc_failure_lock =
163     CRYPTO_STATIC_MUTEX_INIT;
164 static uint64_t current_malloc_count = 0;
165 static uint64_t malloc_number_to_fail = 0;
166 static int malloc_failure_enabled = 0, break_on_malloc_fail = 0,
167            any_malloc_failed = 0;
168 
malloc_exit_handler(void)169 static void malloc_exit_handler(void) {
170   CRYPTO_STATIC_MUTEX_lock_read(&malloc_failure_lock);
171   if (any_malloc_failed) {
172     // Signal to the test driver that some allocation failed, so it knows to
173     // increment the counter and continue.
174     _exit(88);
175   }
176   CRYPTO_STATIC_MUTEX_unlock_read(&malloc_failure_lock);
177 }
178 
init_malloc_failure(void)179 static void init_malloc_failure(void) {
180   const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
181   if (env != NULL && env[0] != 0) {
182     char *endptr;
183     malloc_number_to_fail = strtoull(env, &endptr, 10);
184     if (*endptr == 0) {
185       malloc_failure_enabled = 1;
186       atexit(malloc_exit_handler);
187     }
188   }
189   break_on_malloc_fail = getenv("MALLOC_BREAK_ON_FAIL") != NULL;
190 }
191 
192 // should_fail_allocation returns one if the current allocation should fail and
193 // zero otherwise.
should_fail_allocation()194 static int should_fail_allocation() {
195   static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
196   CRYPTO_once(&once, init_malloc_failure);
197   if (!malloc_failure_enabled) {
198     return 0;
199   }
200 
201   // We lock just so multi-threaded tests are still correct, but we won't test
202   // every malloc exhaustively.
203   CRYPTO_STATIC_MUTEX_lock_write(&malloc_failure_lock);
204   int should_fail = current_malloc_count == malloc_number_to_fail;
205   current_malloc_count++;
206   any_malloc_failed = any_malloc_failed || should_fail;
207   CRYPTO_STATIC_MUTEX_unlock_write(&malloc_failure_lock);
208 
209   if (should_fail && break_on_malloc_fail) {
210     raise(SIGTRAP);
211   }
212   if (should_fail) {
213     errno = ENOMEM;
214   }
215   return should_fail;
216 }
217 
OPENSSL_reset_malloc_counter_for_testing(void)218 void OPENSSL_reset_malloc_counter_for_testing(void) {
219   CRYPTO_STATIC_MUTEX_lock_write(&malloc_failure_lock);
220   current_malloc_count = 0;
221   CRYPTO_STATIC_MUTEX_unlock_write(&malloc_failure_lock);
222 }
223 
224 #else
should_fail_allocation(void)225 static int should_fail_allocation(void) { return 0; }
226 #endif
227 
OPENSSL_malloc(size_t size)228 void *OPENSSL_malloc(size_t size) {
229   if (should_fail_allocation()) {
230     goto err;
231   }
232 
233   if (OPENSSL_memory_alloc != NULL) {
234     assert(OPENSSL_memory_free != NULL);
235     assert(OPENSSL_memory_get_size != NULL);
236     void *ptr = OPENSSL_memory_alloc(size);
237     if (ptr == NULL && size != 0) {
238       goto err;
239     }
240     return ptr;
241   }
242 
243   if (size + OPENSSL_MALLOC_PREFIX < size) {
244     // |OPENSSL_malloc| is a central function in BoringSSL thus a reference to
245     // |kBoringSSLBinaryTag| is created here so that the tag isn't discarded by
246     // the linker. The following is sufficient to stop GCC, Clang, and MSVC
247     // optimising away the reference at the time of writing. Since this
248     // probably results in an actual memory reference, it is put in this very
249     // rare code path.
250     uint8_t unused = *(volatile uint8_t *)kBoringSSLBinaryTag;
251     (void) unused;
252     goto err;
253   }
254 
255   void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX);
256   if (ptr == NULL) {
257     goto err;
258   }
259 
260   *(size_t *)ptr = size;
261 
262   __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
263   return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX;
264 
265  err:
266   // This only works because ERR does not call OPENSSL_malloc.
267   OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
268   return NULL;
269 }
270 
OPENSSL_free(void * orig_ptr)271 void OPENSSL_free(void *orig_ptr) {
272   if (orig_ptr == NULL) {
273     return;
274   }
275 
276   if (OPENSSL_memory_free != NULL) {
277     OPENSSL_memory_free(orig_ptr);
278     return;
279   }
280 
281   void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
282   __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
283 
284   size_t size = *(size_t *)ptr;
285   OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX);
286 
287 // ASan knows to intercept malloc and free, but not sdallocx.
288 #if defined(OPENSSL_ASAN)
289   (void)sdallocx;
290   free(ptr);
291 #else
292   if (sdallocx) {
293     sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */);
294   } else {
295     free(ptr);
296   }
297 #endif
298 }
299 
OPENSSL_realloc(void * orig_ptr,size_t new_size)300 void *OPENSSL_realloc(void *orig_ptr, size_t new_size) {
301   if (orig_ptr == NULL) {
302     return OPENSSL_malloc(new_size);
303   }
304 
305   size_t old_size;
306   if (OPENSSL_memory_get_size != NULL) {
307     old_size = OPENSSL_memory_get_size(orig_ptr);
308   } else {
309     void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX;
310     __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
311     old_size = *(size_t *)ptr;
312     __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX);
313   }
314 
315   void *ret = OPENSSL_malloc(new_size);
316   if (ret == NULL) {
317     return NULL;
318   }
319 
320   size_t to_copy = new_size;
321   if (old_size < to_copy) {
322     to_copy = old_size;
323   }
324 
325   memcpy(ret, orig_ptr, to_copy);
326   OPENSSL_free(orig_ptr);
327 
328   return ret;
329 }
330 
OPENSSL_cleanse(void * ptr,size_t len)331 void OPENSSL_cleanse(void *ptr, size_t len) {
332 #if defined(OPENSSL_WINDOWS)
333   SecureZeroMemory(ptr, len);
334 #else
335   OPENSSL_memset(ptr, 0, len);
336 
337 #if !defined(OPENSSL_NO_ASM)
338   /* As best as we can tell, this is sufficient to break any optimisations that
339      might try to eliminate "superfluous" memsets. If there's an easy way to
340      detect memset_s, it would be better to use that. */
341   __asm__ __volatile__("" : : "r"(ptr) : "memory");
342 #endif
343 #endif  // !OPENSSL_NO_ASM
344 }
345 
OPENSSL_clear_free(void * ptr,size_t unused)346 void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); }
347 
CRYPTO_secure_malloc_init(size_t size,size_t min_size)348 int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; }
349 
CRYPTO_secure_malloc_initialized(void)350 int CRYPTO_secure_malloc_initialized(void) { return 0; }
351 
CRYPTO_secure_used(void)352 size_t CRYPTO_secure_used(void) { return 0; }
353 
OPENSSL_secure_malloc(size_t size)354 void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); }
355 
OPENSSL_secure_clear_free(void * ptr,size_t len)356 void OPENSSL_secure_clear_free(void *ptr, size_t len) {
357   OPENSSL_clear_free(ptr, len);
358 }
359 
CRYPTO_memcmp(const void * in_a,const void * in_b,size_t len)360 int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
361   const uint8_t *a = in_a;
362   const uint8_t *b = in_b;
363   uint8_t x = 0;
364 
365   for (size_t i = 0; i < len; i++) {
366     x |= a[i] ^ b[i];
367   }
368 
369   return x;
370 }
371 
OPENSSL_hash32(const void * ptr,size_t len)372 uint32_t OPENSSL_hash32(const void *ptr, size_t len) {
373   // These are the FNV-1a parameters for 32 bits.
374   static const uint32_t kPrime = 16777619u;
375   static const uint32_t kOffsetBasis = 2166136261u;
376 
377   const uint8_t *in = ptr;
378   uint32_t h = kOffsetBasis;
379 
380   for (size_t i = 0; i < len; i++) {
381     h ^= in[i];
382     h *= kPrime;
383   }
384 
385   return h;
386 }
387 
OPENSSL_strhash(const char * s)388 uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); }
389 
OPENSSL_strnlen(const char * s,size_t len)390 size_t OPENSSL_strnlen(const char *s, size_t len) {
391   for (size_t i = 0; i < len; i++) {
392     if (s[i] == 0) {
393       return i;
394     }
395   }
396 
397   return len;
398 }
399 
OPENSSL_strdup(const char * s)400 char *OPENSSL_strdup(const char *s) {
401   if (s == NULL) {
402     return NULL;
403   }
404   const size_t len = strlen(s) + 1;
405   char *ret = OPENSSL_malloc(len);
406   if (ret == NULL) {
407     return NULL;
408   }
409   OPENSSL_memcpy(ret, s, len);
410   return ret;
411 }
412 
OPENSSL_isalpha(int c)413 int OPENSSL_isalpha(int c) {
414   return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
415 }
416 
OPENSSL_isdigit(int c)417 int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; }
418 
OPENSSL_isxdigit(int c)419 int OPENSSL_isxdigit(int c) {
420   return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
421 }
422 
OPENSSL_fromxdigit(uint8_t * out,int c)423 int OPENSSL_fromxdigit(uint8_t *out, int c) {
424   if (OPENSSL_isdigit(c)) {
425     *out = c - '0';
426     return 1;
427   }
428   if ('a' <= c && c <= 'f') {
429     *out = c - 'a' + 10;
430     return 1;
431   }
432   if ('A' <= c && c <= 'F') {
433     *out = c - 'A' + 10;
434     return 1;
435   }
436   return 0;
437 }
438 
OPENSSL_isalnum(int c)439 int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); }
440 
OPENSSL_tolower(int c)441 int OPENSSL_tolower(int c) {
442   if (c >= 'A' && c <= 'Z') {
443     return c + ('a' - 'A');
444   }
445   return c;
446 }
447 
OPENSSL_isspace(int c)448 int OPENSSL_isspace(int c) {
449   return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' ||
450          c == ' ';
451 }
452 
OPENSSL_strcasecmp(const char * a,const char * b)453 int OPENSSL_strcasecmp(const char *a, const char *b) {
454   for (size_t i = 0;; i++) {
455     const int aa = OPENSSL_tolower(a[i]);
456     const int bb = OPENSSL_tolower(b[i]);
457 
458     if (aa < bb) {
459       return -1;
460     } else if (aa > bb) {
461       return 1;
462     } else if (aa == 0) {
463       return 0;
464     }
465   }
466 }
467 
OPENSSL_strncasecmp(const char * a,const char * b,size_t n)468 int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) {
469   for (size_t i = 0; i < n; i++) {
470     const int aa = OPENSSL_tolower(a[i]);
471     const int bb = OPENSSL_tolower(b[i]);
472 
473     if (aa < bb) {
474       return -1;
475     } else if (aa > bb) {
476       return 1;
477     } else if (aa == 0) {
478       return 0;
479     }
480   }
481 
482   return 0;
483 }
484 
BIO_snprintf(char * buf,size_t n,const char * format,...)485 int BIO_snprintf(char *buf, size_t n, const char *format, ...) {
486   va_list args;
487   va_start(args, format);
488   int ret = BIO_vsnprintf(buf, n, format, args);
489   va_end(args);
490   return ret;
491 }
492 
BIO_vsnprintf(char * buf,size_t n,const char * format,va_list args)493 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) {
494   return vsnprintf(buf, n, format, args);
495 }
496 
OPENSSL_vasprintf_internal(char ** str,const char * format,va_list args,int system_malloc)497 int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args,
498                                int system_malloc) {
499   void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc;
500   void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free;
501   void *(*reallocate)(void *, size_t) =
502       system_malloc ? realloc : OPENSSL_realloc;
503   char *candidate = NULL;
504   size_t candidate_len = 64;  // TODO(bbe) what's the best initial size?
505 
506   if ((candidate = allocate(candidate_len)) == NULL) {
507     goto err;
508   }
509   va_list args_copy;
510   va_copy(args_copy, args);
511   int ret = vsnprintf(candidate, candidate_len, format, args_copy);
512   va_end(args_copy);
513   if (ret < 0) {
514     goto err;
515   }
516   if ((size_t)ret >= candidate_len) {
517     // Too big to fit in allocation.
518     char *tmp;
519 
520     candidate_len = (size_t)ret + 1;
521     if ((tmp = reallocate(candidate, candidate_len)) == NULL) {
522       goto err;
523     }
524     candidate = tmp;
525     ret = vsnprintf(candidate, candidate_len, format, args);
526   }
527   // At this point this should not happen unless vsnprintf is insane.
528   if (ret < 0 || (size_t)ret >= candidate_len) {
529     goto err;
530   }
531   *str = candidate;
532   return ret;
533 
534  err:
535   deallocate(candidate);
536   *str = NULL;
537   errno = ENOMEM;
538   return -1;
539 }
540 
OPENSSL_vasprintf(char ** str,const char * format,va_list args)541 int OPENSSL_vasprintf(char **str, const char *format, va_list args) {
542   return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0);
543 }
544 
OPENSSL_asprintf(char ** str,const char * format,...)545 int OPENSSL_asprintf(char **str, const char *format, ...) {
546   va_list args;
547   va_start(args, format);
548   int ret = OPENSSL_vasprintf(str, format, args);
549   va_end(args);
550   return ret;
551 }
552 
OPENSSL_strndup(const char * str,size_t size)553 char *OPENSSL_strndup(const char *str, size_t size) {
554   size = OPENSSL_strnlen(str, size);
555 
556   size_t alloc_size = size + 1;
557   if (alloc_size < size) {
558     // overflow
559     OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE);
560     return NULL;
561   }
562   char *ret = OPENSSL_malloc(alloc_size);
563   if (ret == NULL) {
564     return NULL;
565   }
566 
567   OPENSSL_memcpy(ret, str, size);
568   ret[size] = '\0';
569   return ret;
570 }
571 
OPENSSL_strlcpy(char * dst,const char * src,size_t dst_size)572 size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) {
573   size_t l = 0;
574 
575   for (; dst_size > 1 && *src; dst_size--) {
576     *dst++ = *src++;
577     l++;
578   }
579 
580   if (dst_size) {
581     *dst = 0;
582   }
583 
584   return l + strlen(src);
585 }
586 
OPENSSL_strlcat(char * dst,const char * src,size_t dst_size)587 size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) {
588   size_t l = 0;
589   for (; dst_size > 0 && *dst; dst_size--, dst++) {
590     l++;
591   }
592   return l + OPENSSL_strlcpy(dst, src, dst_size);
593 }
594 
OPENSSL_memdup(const void * data,size_t size)595 void *OPENSSL_memdup(const void *data, size_t size) {
596   if (size == 0) {
597     return NULL;
598   }
599 
600   void *ret = OPENSSL_malloc(size);
601   if (ret == NULL) {
602     return NULL;
603   }
604 
605   OPENSSL_memcpy(ret, data, size);
606   return ret;
607 }
608 
CRYPTO_malloc(size_t size,const char * file,int line)609 void *CRYPTO_malloc(size_t size, const char *file, int line) {
610   return OPENSSL_malloc(size);
611 }
612 
CRYPTO_realloc(void * ptr,size_t new_size,const char * file,int line)613 void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) {
614   return OPENSSL_realloc(ptr, new_size);
615 }
616 
CRYPTO_free(void * ptr,const char * file,int line)617 void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); }
618