| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/mem.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <errno.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <stdarg.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
|
| 13 |
#include <openssl/err.h> |
| 14 |
|
| 15 |
#if defined(OPENSSL_WINDOWS) |
| 16 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 17 |
#include <windows.h> |
| 18 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 19 |
#endif |
| 20 |
|
| 21 |
#include "internal.h" |
| 22 |
|
| 23 |
|
| 24 |
#define OPENSSL_MALLOC_PREFIX 8 |
| 25 |
OPENSSL_STATIC_ASSERT(OPENSSL_MALLOC_PREFIX >= sizeof(size_t), |
| 26 |
size_t_too_large) |
| 27 |
|
| 28 |
#if defined(OPENSSL_ASAN) |
| 29 |
void __asan_poison_memory_region(const volatile void *addr, size_t size); |
| 30 |
void __asan_unpoison_memory_region(const volatile void *addr, size_t size); |
| 31 |
#else |
| 32 |
static void __asan_poison_memory_region(const void *addr, size_t size) {} |
| 33 |
static void __asan_unpoison_memory_region(const void *addr, size_t size) {} |
| 34 |
#endif |
| 35 |
|
| 36 |
#define AWSLC_FILE "" |
| 37 |
#define AWSLC_LINE 0 |
| 38 |
|
| 39 |
// sdallocx is a sized |free| function. By passing the size (which we happen to |
| 40 |
// always know in BoringSSL), the malloc implementation can save work. We cannot |
| 41 |
// depend on |sdallocx| being available, however, so it's a weak symbol. |
| 42 |
// |
| 43 |
// This will always be safe, but will only be overridden if the malloc |
| 44 |
// implementation is statically linked with BoringSSL. So, if |sdallocx| is |
| 45 |
// provided in, say, libc.so, we still won't use it because that's dynamically |
| 46 |
// linked. This isn't an ideal result, but its helps in some cases. |
| 47 |
WEAK_SYMBOL_FUNC(void, sdallocx, (void *ptr, size_t size, int flags)) |
| 48 |
|
| 49 |
// The following four functions can be defined to override default heap |
| 50 |
// allocation and freeing. If defined, it is the responsibility of |
| 51 |
// |OPENSSL_memory_free| to zero out the memory before returning it to the |
| 52 |
// system. |OPENSSL_memory_free| will not be passed NULL pointers. |
| 53 |
// |
| 54 |
// WARNING: These functions are called on every allocation and free in |
| 55 |
// BoringSSL across the entire process. They may be called by any code in the |
| 56 |
// process which calls BoringSSL, including in process initializers and thread |
| 57 |
// destructors. When called, BoringSSL may hold pthreads locks. Any other code |
| 58 |
// in the process which, directly or indirectly, calls BoringSSL may be on the |
| 59 |
// call stack and may itself be using arbitrary synchronization primitives. |
| 60 |
// |
| 61 |
// As a result, these functions may not have the usual programming environment |
| 62 |
// available to most C or C++ code. In particular, they may not call into |
| 63 |
// BoringSSL, or any library which depends on BoringSSL. Any synchronization |
| 64 |
// primitives used must tolerate every other synchronization primitive linked |
| 65 |
// into the process, including pthreads locks. Failing to meet these constraints |
| 66 |
// may result in deadlocks, crashes, or memory corruption. |
| 67 |
WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_alloc, (size_t size)) |
| 68 |
WEAK_SYMBOL_FUNC(void, OPENSSL_memory_free, (void *ptr)) |
| 69 |
WEAK_SYMBOL_FUNC(size_t, OPENSSL_memory_get_size, (void *ptr)) |
| 70 |
WEAK_SYMBOL_FUNC(void *, OPENSSL_memory_realloc, (void *ptr, size_t size)) |
| 71 |
|
| 72 |
// Control function for crypto memory debugging Always returns 0 in AWS-LC |
| 73 |
// |mode| Unused parameter AWS-LC doesn't support |CRYPTO_MDEBUG| |
| 74 |
// Always returns 0 |
| 75 |
int CRYPTO_mem_ctrl(int mode) { |
| 76 |
#if defined (OPENSSL_NO_CRYPTO_MDEBUG) |
| 77 |
(void)mode; // Silence unused parameter warning |
| 78 |
return 0; |
| 79 |
#else |
| 80 |
#error "Must compile with OPENSSL_NO_CRYPTO_MDEBUG defined." |
| 81 |
#endif |
| 82 |
} |
| 83 |
|
| 84 |
// Below can be customized by |CRYPTO_set_mem_functions| only once. |
| 85 |
static void *(*malloc_impl)(size_t, const char *, int) = NULL; |
| 86 |
static void *(*realloc_impl)(void *, size_t, const char *, int) = NULL; |
| 87 |
static void (*free_impl)(void *, const char *, int) = NULL; |
| 88 |
|
| 89 |
int CRYPTO_set_mem_functions( |
| 90 |
void *(*m)(size_t, const char *, int), |
| 91 |
void *(*r)(void *, size_t, const char *, int), |
| 92 |
void (*f)(void *, const char *, int)) { |
| 93 |
if (m == NULL || r == NULL || f == NULL) { |
| 94 |
return 0; |
| 95 |
} |
| 96 |
// |malloc_impl|, |realloc_impl| and |free_impl| can be set only once. |
| 97 |
if (malloc_impl != NULL || realloc_impl != NULL || free_impl != NULL) { |
| 98 |
return 0; |
| 99 |
} |
| 100 |
if (OPENSSL_memory_alloc != NULL || |
| 101 |
OPENSSL_memory_free != NULL || |
| 102 |
OPENSSL_memory_get_size != NULL || |
| 103 |
OPENSSL_memory_realloc != NULL) { |
| 104 |
// |OPENSSL_malloc/free/realloc| are customized by overriding the symbols. |
| 105 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
malloc_impl = m; |
| 109 |
realloc_impl = r; |
| 110 |
free_impl = f; |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
void *OPENSSL_malloc(size_t size) { |
| 115 |
if (malloc_impl != NULL) { |
| 116 |
assert(OPENSSL_memory_alloc == NULL); |
| 117 |
assert(OPENSSL_memory_realloc == NULL); |
| 118 |
assert(OPENSSL_memory_free == NULL); |
| 119 |
assert(OPENSSL_memory_get_size == NULL); |
| 120 |
assert(realloc_impl != NULL); |
| 121 |
assert(free_impl != NULL); |
| 122 |
return malloc_impl(size, AWSLC_FILE, AWSLC_LINE); |
| 123 |
} |
| 124 |
if (OPENSSL_memory_alloc != NULL) { |
| 125 |
assert(OPENSSL_memory_free != NULL); |
| 126 |
assert(OPENSSL_memory_get_size != NULL); |
| 127 |
void *ptr = OPENSSL_memory_alloc(size); |
| 128 |
if (ptr == NULL && size != 0) { |
| 129 |
goto err; |
| 130 |
} |
| 131 |
return ptr; |
| 132 |
} |
| 133 |
|
| 134 |
if (size + OPENSSL_MALLOC_PREFIX < size) { |
| 135 |
goto err; |
| 136 |
} |
| 137 |
|
| 138 |
void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
| 139 |
if (ptr == NULL) { |
| 140 |
goto err; |
| 141 |
} |
| 142 |
|
| 143 |
*(size_t *)ptr = size; |
| 144 |
|
| 145 |
__asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
| 146 |
return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
| 147 |
|
| 148 |
err: |
| 149 |
// This only works because ERR does not call OPENSSL_malloc. |
| 150 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
void *OPENSSL_zalloc(size_t size) { |
| 155 |
void *ret = OPENSSL_malloc(size); |
| 156 |
if (ret != NULL) { |
| 157 |
OPENSSL_memset(ret, 0, size); |
| 158 |
} |
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
void *OPENSSL_calloc(size_t num, size_t size) { |
| 163 |
if (size != 0 && num > SIZE_MAX / size) { |
| 164 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
return OPENSSL_zalloc(num * size); |
| 169 |
} |
| 170 |
|
| 171 |
void OPENSSL_free(void *orig_ptr) { |
| 172 |
if (orig_ptr == NULL) { |
| 173 |
return; |
| 174 |
} |
| 175 |
if (free_impl != NULL) { |
| 176 |
assert(OPENSSL_memory_alloc == NULL); |
| 177 |
assert(OPENSSL_memory_realloc == NULL); |
| 178 |
assert(OPENSSL_memory_free == NULL); |
| 179 |
assert(OPENSSL_memory_get_size == NULL); |
| 180 |
assert(malloc_impl != NULL); |
| 181 |
assert(realloc_impl != NULL); |
| 182 |
free_impl(orig_ptr, AWSLC_FILE, AWSLC_LINE); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
if (OPENSSL_memory_free != NULL) { |
| 187 |
OPENSSL_memory_free(orig_ptr); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
| 192 |
__asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
| 193 |
|
| 194 |
size_t size = *(size_t *)ptr; |
| 195 |
OPENSSL_cleanse(ptr, size + OPENSSL_MALLOC_PREFIX); |
| 196 |
|
| 197 |
// ASan knows to intercept malloc and free, but not sdallocx. |
| 198 |
#if defined(OPENSSL_ASAN) |
| 199 |
(void)sdallocx; |
| 200 |
free(ptr); |
| 201 |
(void) sdallocx; |
| 202 |
#else |
| 203 |
if (sdallocx) { |
| 204 |
sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
| 205 |
} else { |
| 206 |
free(ptr); |
| 207 |
} |
| 208 |
#endif |
| 209 |
} |
| 210 |
|
| 211 |
void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
| 212 |
if (orig_ptr == NULL) { |
| 213 |
return OPENSSL_malloc(new_size); |
| 214 |
} |
| 215 |
if (realloc_impl != NULL) { |
| 216 |
assert(OPENSSL_memory_alloc == NULL); |
| 217 |
assert(OPENSSL_memory_realloc == NULL); |
| 218 |
assert(OPENSSL_memory_free == NULL); |
| 219 |
assert(OPENSSL_memory_get_size == NULL); |
| 220 |
assert(malloc_impl != NULL); |
| 221 |
assert(free_impl != NULL); |
| 222 |
return realloc_impl(orig_ptr, new_size, AWSLC_FILE, AWSLC_LINE); |
| 223 |
} |
| 224 |
if (OPENSSL_memory_realloc != NULL) { |
| 225 |
assert(OPENSSL_memory_alloc != NULL); |
| 226 |
assert(OPENSSL_memory_free != NULL); |
| 227 |
assert(OPENSSL_memory_get_size != NULL); |
| 228 |
return OPENSSL_memory_realloc(orig_ptr, new_size); |
| 229 |
} |
| 230 |
size_t old_size; |
| 231 |
if (OPENSSL_memory_get_size != NULL) { |
| 232 |
old_size = OPENSSL_memory_get_size(orig_ptr); |
| 233 |
} else { |
| 234 |
void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
| 235 |
__asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
| 236 |
old_size = *(size_t *)ptr; |
| 237 |
__asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
| 238 |
} |
| 239 |
|
| 240 |
void *ret = OPENSSL_malloc(new_size); |
| 241 |
if (ret == NULL) { |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
|
| 245 |
size_t to_copy = new_size; |
| 246 |
if (old_size < to_copy) { |
| 247 |
to_copy = old_size; |
| 248 |
} |
| 249 |
|
| 250 |
memcpy(ret, orig_ptr, to_copy); |
| 251 |
OPENSSL_free(orig_ptr); |
| 252 |
|
| 253 |
return ret; |
| 254 |
} |
| 255 |
|
| 256 |
void OPENSSL_cleanse(void *ptr, size_t len) { |
| 257 |
|
| 258 |
if (ptr == NULL || len == 0) { |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
#if defined(OPENSSL_WINDOWS) |
| 263 |
SecureZeroMemory(ptr, len); |
| 264 |
#else |
| 265 |
OPENSSL_memset(ptr, 0, len); |
| 266 |
|
| 267 |
#if !defined(OPENSSL_NO_ASM) |
| 268 |
/* As best as we can tell, this is sufficient to break any optimisations that |
| 269 |
might try to eliminate "superfluous" memsets. If there's an easy way to |
| 270 |
detect memset_s, it would be better to use that. */ |
| 271 |
__asm__ __volatile__("" : : "r"(ptr) : "memory"); |
| 272 |
#endif |
| 273 |
#endif // !OPENSSL_NO_ASM |
| 274 |
} |
| 275 |
|
| 276 |
void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
| 277 |
|
| 278 |
int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
| 279 |
|
| 280 |
int CRYPTO_secure_malloc_initialized(void) { return 0; } |
| 281 |
|
| 282 |
size_t CRYPTO_secure_used(void) { return 0; } |
| 283 |
|
| 284 |
void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
| 285 |
|
| 286 |
void *OPENSSL_secure_zalloc(size_t size) { return OPENSSL_zalloc(size); } |
| 287 |
|
| 288 |
void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
| 289 |
OPENSSL_clear_free(ptr, len); |
| 290 |
} |
| 291 |
|
| 292 |
int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
| 293 |
const uint8_t *a = in_a; |
| 294 |
const uint8_t *b = in_b; |
| 295 |
uint8_t x = 0; |
| 296 |
|
| 297 |
for (size_t i = 0; i < len; i++) { |
| 298 |
x |= a[i] ^ b[i]; |
| 299 |
} |
| 300 |
|
| 301 |
return x; |
| 302 |
} |
| 303 |
|
| 304 |
uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
| 305 |
// These are the FNV-1a parameters for 32 bits. |
| 306 |
static const uint32_t kPrime = 16777619u; |
| 307 |
static const uint32_t kOffsetBasis = 2166136261u; |
| 308 |
|
| 309 |
const uint8_t *in = ptr; |
| 310 |
uint32_t h = kOffsetBasis; |
| 311 |
|
| 312 |
for (size_t i = 0; i < len; i++) { |
| 313 |
h ^= in[i]; |
| 314 |
h *= kPrime; |
| 315 |
} |
| 316 |
|
| 317 |
return h; |
| 318 |
} |
| 319 |
|
| 320 |
uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
| 321 |
|
| 322 |
size_t OPENSSL_strnlen(const char *s, size_t len) { |
| 323 |
for (size_t i = 0; i < len; i++) { |
| 324 |
if (s[i] == 0) { |
| 325 |
return i; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
return len; |
| 330 |
} |
| 331 |
|
| 332 |
char *OPENSSL_strdup(const char *s) { |
| 333 |
if (s == NULL) { |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
// Copy the NUL terminator. |
| 337 |
return OPENSSL_memdup(s, strlen(s) + 1); |
| 338 |
} |
| 339 |
|
| 340 |
int OPENSSL_isalpha(int c) { |
| 341 |
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
| 342 |
} |
| 343 |
|
| 344 |
int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
| 345 |
|
| 346 |
int OPENSSL_isxdigit(int c) { |
| 347 |
return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
| 348 |
} |
| 349 |
|
| 350 |
int OPENSSL_fromxdigit(uint8_t *out, int c) { |
| 351 |
if (OPENSSL_isdigit(c)) { |
| 352 |
*out = c - '0'; |
| 353 |
return 1; |
| 354 |
} |
| 355 |
if ('a' <= c && c <= 'f') { |
| 356 |
*out = c - 'a' + 10; |
| 357 |
return 1; |
| 358 |
} |
| 359 |
if ('A' <= c && c <= 'F') { |
| 360 |
*out = c - 'A' + 10; |
| 361 |
return 1; |
| 362 |
} |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len) { |
| 367 |
if (str == NULL || len == NULL) { |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
|
| 371 |
const size_t slen = OPENSSL_strnlen(str, INT16_MAX); |
| 372 |
if (slen % 2 != 0) { |
| 373 |
return NULL; |
| 374 |
} |
| 375 |
|
| 376 |
const size_t buflen = slen / 2; |
| 377 |
uint8_t *buf = OPENSSL_zalloc(buflen); |
| 378 |
if (buf == NULL) { |
| 379 |
return NULL; |
| 380 |
} |
| 381 |
|
| 382 |
for (size_t i = 0; i < buflen; i++) { |
| 383 |
uint8_t hi, lo; |
| 384 |
if (!OPENSSL_fromxdigit(&hi, str[2 * i]) || |
| 385 |
!OPENSSL_fromxdigit(&lo, str[2 * i + 1])) { |
| 386 |
OPENSSL_free(buf); |
| 387 |
return NULL; |
| 388 |
} |
| 389 |
buf[i] = (hi << 4) | lo; |
| 390 |
} |
| 391 |
|
| 392 |
*len = buflen; |
| 393 |
return buf; |
| 394 |
} |
| 395 |
|
| 396 |
int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
| 397 |
|
| 398 |
int OPENSSL_tolower(int c) { |
| 399 |
if (c >= 'A' && c <= 'Z') { |
| 400 |
return c + ('a' - 'A'); |
| 401 |
} |
| 402 |
return c; |
| 403 |
} |
| 404 |
|
| 405 |
int OPENSSL_isspace(int c) { |
| 406 |
return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
| 407 |
c == ' '; |
| 408 |
} |
| 409 |
|
| 410 |
int OPENSSL_strcasecmp(const char *a, const char *b) { |
| 411 |
for (size_t i = 0;; i++) { |
| 412 |
const int aa = OPENSSL_tolower(a[i]); |
| 413 |
const int bb = OPENSSL_tolower(b[i]); |
| 414 |
|
| 415 |
if (aa < bb) { |
| 416 |
return -1; |
| 417 |
} else if (aa > bb) { |
| 418 |
return 1; |
| 419 |
} else if (aa == 0) { |
| 420 |
return 0; |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
| 426 |
for (size_t i = 0; i < n; i++) { |
| 427 |
const int aa = OPENSSL_tolower(a[i]); |
| 428 |
const int bb = OPENSSL_tolower(b[i]); |
| 429 |
|
| 430 |
if (aa < bb) { |
| 431 |
return -1; |
| 432 |
} else if (aa > bb) { |
| 433 |
return 1; |
| 434 |
} else if (aa == 0) { |
| 435 |
return 0; |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
| 443 |
va_list args; |
| 444 |
va_start(args, format); |
| 445 |
int ret = BIO_vsnprintf(buf, n, format, args); |
| 446 |
va_end(args); |
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
| 451 |
return vsnprintf(buf, n, format, args); |
| 452 |
} |
| 453 |
|
| 454 |
int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
| 455 |
int system_malloc) { |
| 456 |
void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
| 457 |
void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
| 458 |
void *(*reallocate)(void *, size_t) = |
| 459 |
system_malloc ? realloc : OPENSSL_realloc; |
| 460 |
char *candidate = NULL; |
| 461 |
size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
| 462 |
|
| 463 |
if ((candidate = allocate(candidate_len)) == NULL) { |
| 464 |
goto err; |
| 465 |
} |
| 466 |
va_list args_copy; |
| 467 |
va_copy(args_copy, args); |
| 468 |
int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
| 469 |
va_end(args_copy); |
| 470 |
if (ret < 0) { |
| 471 |
goto err; |
| 472 |
} |
| 473 |
if ((size_t)ret >= candidate_len) { |
| 474 |
// Too big to fit in allocation. |
| 475 |
char *tmp; |
| 476 |
|
| 477 |
candidate_len = (size_t)ret + 1; |
| 478 |
if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
| 479 |
goto err; |
| 480 |
} |
| 481 |
candidate = tmp; |
| 482 |
ret = vsnprintf(candidate, candidate_len, format, args); |
| 483 |
} |
| 484 |
// At this point this should not happen unless vsnprintf is insane. |
| 485 |
if (ret < 0 || (size_t)ret >= candidate_len) { |
| 486 |
goto err; |
| 487 |
} |
| 488 |
*str = candidate; |
| 489 |
return ret; |
| 490 |
|
| 491 |
err: |
| 492 |
deallocate(candidate); |
| 493 |
*str = NULL; |
| 494 |
errno = ENOMEM; |
| 495 |
return -1; |
| 496 |
} |
| 497 |
|
| 498 |
int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
| 499 |
return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
| 500 |
} |
| 501 |
|
| 502 |
int OPENSSL_asprintf(char **str, const char *format, ...) { |
| 503 |
va_list args; |
| 504 |
va_start(args, format); |
| 505 |
int ret = OPENSSL_vasprintf(str, format, args); |
| 506 |
va_end(args); |
| 507 |
return ret; |
| 508 |
} |
| 509 |
|
| 510 |
char *OPENSSL_strndup(const char *str, size_t size) { |
| 511 |
size = OPENSSL_strnlen(str, size); |
| 512 |
|
| 513 |
size_t alloc_size = size + 1; |
| 514 |
if (alloc_size < size) { |
| 515 |
// overflow |
| 516 |
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
| 517 |
return NULL; |
| 518 |
} |
| 519 |
char *ret = OPENSSL_malloc(alloc_size); |
| 520 |
if (ret == NULL) { |
| 521 |
return NULL; |
| 522 |
} |
| 523 |
|
| 524 |
OPENSSL_memcpy(ret, str, size); |
| 525 |
ret[size] = '\0'; |
| 526 |
return ret; |
| 527 |
} |
| 528 |
|
| 529 |
size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
| 530 |
size_t l = 0; |
| 531 |
|
| 532 |
for (; dst_size > 1 && *src; dst_size--) { |
| 533 |
*dst++ = *src++; |
| 534 |
l++; |
| 535 |
} |
| 536 |
|
| 537 |
if (dst_size) { |
| 538 |
*dst = 0; |
| 539 |
} |
| 540 |
|
| 541 |
return l + strlen(src); |
| 542 |
} |
| 543 |
|
| 544 |
size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
| 545 |
size_t l = 0; |
| 546 |
for (; dst_size > 0 && *dst; dst_size--, dst++) { |
| 547 |
l++; |
| 548 |
} |
| 549 |
return l + OPENSSL_strlcpy(dst, src, dst_size); |
| 550 |
} |
| 551 |
|
| 552 |
void *OPENSSL_memdup(const void *data, size_t size) { |
| 553 |
if (size == 0) { |
| 554 |
return NULL; |
| 555 |
} |
| 556 |
|
| 557 |
void *ret = OPENSSL_malloc(size); |
| 558 |
if (ret == NULL) { |
| 559 |
return NULL; |
| 560 |
} |
| 561 |
|
| 562 |
OPENSSL_memcpy(ret, data, size); |
| 563 |
return ret; |
| 564 |
} |
| 565 |
|
| 566 |
void *CRYPTO_malloc(size_t size, const char *file, int line) { |
| 567 |
return OPENSSL_malloc(size); |
| 568 |
} |
| 569 |
|
| 570 |
void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
| 571 |
return OPENSSL_realloc(ptr, new_size); |
| 572 |
} |
| 573 |
|
| 574 |
void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |