/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aws-lc-sys-0.39.1/aws-lc/crypto/mem.c
Line | Count | Source |
1 | | // Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 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 | 0 | #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 | 0 | static void __asan_poison_memory_region(const void *addr, size_t size) {} |
33 | 0 | static void __asan_unpoison_memory_region(const void *addr, size_t size) {} |
34 | | #endif |
35 | | |
36 | 0 | #define AWSLC_FILE "" |
37 | 0 | #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 | 0 | int CRYPTO_mem_ctrl(int mode) { |
76 | 0 | #if defined (OPENSSL_NO_CRYPTO_MDEBUG) |
77 | 0 | (void)mode; // Silence unused parameter warning |
78 | 0 | return 0; |
79 | | #else |
80 | | #error "Must compile with OPENSSL_NO_CRYPTO_MDEBUG defined." |
81 | | #endif |
82 | 0 | } |
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 | 0 | void (*f)(void *, const char *, int)) { |
93 | 0 | if (m == NULL || r == NULL || f == NULL) { |
94 | 0 | return 0; |
95 | 0 | } |
96 | | // |malloc_impl|, |realloc_impl| and |free_impl| can be set only once. |
97 | 0 | if (malloc_impl != NULL || realloc_impl != NULL || free_impl != NULL) { |
98 | 0 | return 0; |
99 | 0 | } |
100 | 0 | if (OPENSSL_memory_alloc != NULL || |
101 | 0 | OPENSSL_memory_free != NULL || |
102 | 0 | OPENSSL_memory_get_size != NULL || |
103 | 0 | OPENSSL_memory_realloc != NULL) { |
104 | | // |OPENSSL_malloc/free/realloc| are customized by overriding the symbols. |
105 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
106 | 0 | return 0; |
107 | 0 | } |
108 | 0 | malloc_impl = m; |
109 | 0 | realloc_impl = r; |
110 | 0 | free_impl = f; |
111 | 0 | return 1; |
112 | 0 | } |
113 | | |
114 | 0 | void *OPENSSL_malloc(size_t size) { |
115 | 0 | if (malloc_impl != NULL) { |
116 | 0 | assert(OPENSSL_memory_alloc == NULL); |
117 | 0 | assert(OPENSSL_memory_realloc == NULL); |
118 | 0 | assert(OPENSSL_memory_free == NULL); |
119 | 0 | assert(OPENSSL_memory_get_size == NULL); |
120 | 0 | assert(realloc_impl != NULL); |
121 | 0 | assert(free_impl != NULL); |
122 | 0 | return malloc_impl(size, AWSLC_FILE, AWSLC_LINE); |
123 | 0 | } |
124 | 0 | if (OPENSSL_memory_alloc != NULL) { |
125 | 0 | assert(OPENSSL_memory_free != NULL); |
126 | 0 | assert(OPENSSL_memory_get_size != NULL); |
127 | 0 | void *ptr = OPENSSL_memory_alloc(size); |
128 | 0 | if (ptr == NULL && size != 0) { |
129 | 0 | goto err; |
130 | 0 | } |
131 | 0 | return ptr; |
132 | 0 | } |
133 | | |
134 | 0 | if (size + OPENSSL_MALLOC_PREFIX < size) { |
135 | 0 | goto err; |
136 | 0 | } |
137 | | |
138 | 0 | void *ptr = malloc(size + OPENSSL_MALLOC_PREFIX); |
139 | 0 | if (ptr == NULL) { |
140 | 0 | goto err; |
141 | 0 | } |
142 | | |
143 | 0 | *(size_t *)ptr = size; |
144 | |
|
145 | 0 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
146 | 0 | return ((uint8_t *)ptr) + OPENSSL_MALLOC_PREFIX; |
147 | | |
148 | 0 | err: |
149 | | // This only works because ERR does not call OPENSSL_malloc. |
150 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
151 | 0 | return NULL; |
152 | 0 | } |
153 | | |
154 | 0 | void *OPENSSL_zalloc(size_t size) { |
155 | 0 | void *ret = OPENSSL_malloc(size); |
156 | 0 | if (ret != NULL) { |
157 | 0 | OPENSSL_memset(ret, 0, size); |
158 | 0 | } |
159 | 0 | return ret; |
160 | 0 | } |
161 | | |
162 | 0 | void *OPENSSL_calloc(size_t num, size_t size) { |
163 | 0 | if (size != 0 && num > SIZE_MAX / size) { |
164 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
165 | 0 | return NULL; |
166 | 0 | } |
167 | | |
168 | 0 | return OPENSSL_zalloc(num * size); |
169 | 0 | } |
170 | | |
171 | 0 | void OPENSSL_free(void *orig_ptr) { |
172 | 0 | if (orig_ptr == NULL) { |
173 | 0 | return; |
174 | 0 | } |
175 | 0 | if (free_impl != NULL) { |
176 | 0 | assert(OPENSSL_memory_alloc == NULL); |
177 | 0 | assert(OPENSSL_memory_realloc == NULL); |
178 | 0 | assert(OPENSSL_memory_free == NULL); |
179 | 0 | assert(OPENSSL_memory_get_size == NULL); |
180 | 0 | assert(malloc_impl != NULL); |
181 | 0 | assert(realloc_impl != NULL); |
182 | 0 | free_impl(orig_ptr, AWSLC_FILE, AWSLC_LINE); |
183 | 0 | return; |
184 | 0 | } |
185 | | |
186 | 0 | if (OPENSSL_memory_free != NULL) { |
187 | 0 | OPENSSL_memory_free(orig_ptr); |
188 | 0 | return; |
189 | 0 | } |
190 | | |
191 | 0 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
192 | 0 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
193 | |
|
194 | 0 | size_t size = *(size_t *)ptr; |
195 | 0 | 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 | 0 | if (sdallocx) { |
204 | 0 | sdallocx(ptr, size + OPENSSL_MALLOC_PREFIX, 0 /* flags */); |
205 | 0 | } else { |
206 | 0 | free(ptr); |
207 | 0 | } |
208 | 0 | #endif |
209 | 0 | } |
210 | | |
211 | 0 | void *OPENSSL_realloc(void *orig_ptr, size_t new_size) { |
212 | 0 | if (orig_ptr == NULL) { |
213 | 0 | return OPENSSL_malloc(new_size); |
214 | 0 | } |
215 | 0 | if (realloc_impl != NULL) { |
216 | 0 | assert(OPENSSL_memory_alloc == NULL); |
217 | 0 | assert(OPENSSL_memory_realloc == NULL); |
218 | 0 | assert(OPENSSL_memory_free == NULL); |
219 | 0 | assert(OPENSSL_memory_get_size == NULL); |
220 | 0 | assert(malloc_impl != NULL); |
221 | 0 | assert(free_impl != NULL); |
222 | 0 | return realloc_impl(orig_ptr, new_size, AWSLC_FILE, AWSLC_LINE); |
223 | 0 | } |
224 | 0 | if (OPENSSL_memory_realloc != NULL) { |
225 | 0 | assert(OPENSSL_memory_alloc != NULL); |
226 | 0 | assert(OPENSSL_memory_free != NULL); |
227 | 0 | assert(OPENSSL_memory_get_size != NULL); |
228 | 0 | return OPENSSL_memory_realloc(orig_ptr, new_size); |
229 | 0 | } |
230 | 0 | size_t old_size; |
231 | 0 | if (OPENSSL_memory_get_size != NULL) { |
232 | 0 | old_size = OPENSSL_memory_get_size(orig_ptr); |
233 | 0 | } else { |
234 | 0 | void *ptr = ((uint8_t *)orig_ptr) - OPENSSL_MALLOC_PREFIX; |
235 | 0 | __asan_unpoison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
236 | 0 | old_size = *(size_t *)ptr; |
237 | 0 | __asan_poison_memory_region(ptr, OPENSSL_MALLOC_PREFIX); |
238 | 0 | } |
239 | |
|
240 | 0 | void *ret = OPENSSL_malloc(new_size); |
241 | 0 | if (ret == NULL) { |
242 | 0 | return NULL; |
243 | 0 | } |
244 | | |
245 | 0 | size_t to_copy = new_size; |
246 | 0 | if (old_size < to_copy) { |
247 | 0 | to_copy = old_size; |
248 | 0 | } |
249 | |
|
250 | 0 | memcpy(ret, orig_ptr, to_copy); |
251 | 0 | OPENSSL_free(orig_ptr); |
252 | |
|
253 | 0 | return ret; |
254 | 0 | } |
255 | | |
256 | 0 | void OPENSSL_cleanse(void *ptr, size_t len) { |
257 | |
|
258 | 0 | if (ptr == NULL || len == 0) { |
259 | 0 | return; |
260 | 0 | } |
261 | | |
262 | | #if defined(OPENSSL_WINDOWS) |
263 | | SecureZeroMemory(ptr, len); |
264 | | #else |
265 | 0 | OPENSSL_memset(ptr, 0, len); |
266 | |
|
267 | 0 | #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 | 0 | __asm__ __volatile__("" : : "r"(ptr) : "memory"); |
272 | 0 | #endif |
273 | 0 | #endif // !OPENSSL_NO_ASM |
274 | 0 | } |
275 | | |
276 | 0 | void OPENSSL_clear_free(void *ptr, size_t unused) { OPENSSL_free(ptr); } |
277 | | |
278 | 0 | int CRYPTO_secure_malloc_init(size_t size, size_t min_size) { return 0; } |
279 | | |
280 | 0 | int CRYPTO_secure_malloc_initialized(void) { return 0; } |
281 | | |
282 | 0 | size_t CRYPTO_secure_used(void) { return 0; } |
283 | | |
284 | 0 | void *OPENSSL_secure_malloc(size_t size) { return OPENSSL_malloc(size); } |
285 | | |
286 | 0 | void *OPENSSL_secure_zalloc(size_t size) { return OPENSSL_zalloc(size); } |
287 | | |
288 | 0 | void OPENSSL_secure_clear_free(void *ptr, size_t len) { |
289 | 0 | OPENSSL_clear_free(ptr, len); |
290 | 0 | } |
291 | | |
292 | 0 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) { |
293 | 0 | const uint8_t *a = in_a; |
294 | 0 | const uint8_t *b = in_b; |
295 | 0 | uint8_t x = 0; |
296 | |
|
297 | 0 | for (size_t i = 0; i < len; i++) { |
298 | 0 | x |= a[i] ^ b[i]; |
299 | 0 | } |
300 | |
|
301 | 0 | return x; |
302 | 0 | } |
303 | | |
304 | 0 | uint32_t OPENSSL_hash32(const void *ptr, size_t len) { |
305 | | // These are the FNV-1a parameters for 32 bits. |
306 | 0 | static const uint32_t kPrime = 16777619u; |
307 | 0 | static const uint32_t kOffsetBasis = 2166136261u; |
308 | |
|
309 | 0 | const uint8_t *in = ptr; |
310 | 0 | uint32_t h = kOffsetBasis; |
311 | |
|
312 | 0 | for (size_t i = 0; i < len; i++) { |
313 | 0 | h ^= in[i]; |
314 | 0 | h *= kPrime; |
315 | 0 | } |
316 | |
|
317 | 0 | return h; |
318 | 0 | } |
319 | | |
320 | 0 | uint32_t OPENSSL_strhash(const char *s) { return OPENSSL_hash32(s, strlen(s)); } |
321 | | |
322 | 0 | size_t OPENSSL_strnlen(const char *s, size_t len) { |
323 | 0 | for (size_t i = 0; i < len; i++) { |
324 | 0 | if (s[i] == 0) { |
325 | 0 | return i; |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | 0 | return len; |
330 | 0 | } |
331 | | |
332 | 0 | char *OPENSSL_strdup(const char *s) { |
333 | 0 | if (s == NULL) { |
334 | 0 | return NULL; |
335 | 0 | } |
336 | | // Copy the NUL terminator. |
337 | 0 | return OPENSSL_memdup(s, strlen(s) + 1); |
338 | 0 | } |
339 | | |
340 | 0 | int OPENSSL_isalpha(int c) { |
341 | 0 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
342 | 0 | } |
343 | | |
344 | 0 | int OPENSSL_isdigit(int c) { return c >= '0' && c <= '9'; } |
345 | | |
346 | 0 | int OPENSSL_isxdigit(int c) { |
347 | 0 | return OPENSSL_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); |
348 | 0 | } |
349 | | |
350 | 0 | int OPENSSL_fromxdigit(uint8_t *out, int c) { |
351 | 0 | if (OPENSSL_isdigit(c)) { |
352 | 0 | *out = c - '0'; |
353 | 0 | return 1; |
354 | 0 | } |
355 | 0 | if ('a' <= c && c <= 'f') { |
356 | 0 | *out = c - 'a' + 10; |
357 | 0 | return 1; |
358 | 0 | } |
359 | 0 | if ('A' <= c && c <= 'F') { |
360 | 0 | *out = c - 'A' + 10; |
361 | 0 | return 1; |
362 | 0 | } |
363 | 0 | return 0; |
364 | 0 | } |
365 | | |
366 | 0 | uint8_t *OPENSSL_hexstr2buf(const char *str, size_t *len) { |
367 | 0 | if (str == NULL || len == NULL) { |
368 | 0 | return NULL; |
369 | 0 | } |
370 | | |
371 | 0 | const size_t slen = OPENSSL_strnlen(str, INT16_MAX); |
372 | 0 | if (slen % 2 != 0) { |
373 | 0 | return NULL; |
374 | 0 | } |
375 | | |
376 | 0 | const size_t buflen = slen / 2; |
377 | 0 | uint8_t *buf = OPENSSL_zalloc(buflen); |
378 | 0 | if (buf == NULL) { |
379 | 0 | return NULL; |
380 | 0 | } |
381 | | |
382 | 0 | for (size_t i = 0; i < buflen; i++) { |
383 | 0 | uint8_t hi, lo; |
384 | 0 | if (!OPENSSL_fromxdigit(&hi, str[2 * i]) || |
385 | 0 | !OPENSSL_fromxdigit(&lo, str[2 * i + 1])) { |
386 | 0 | OPENSSL_free(buf); |
387 | 0 | return NULL; |
388 | 0 | } |
389 | 0 | buf[i] = (hi << 4) | lo; |
390 | 0 | } |
391 | | |
392 | 0 | *len = buflen; |
393 | 0 | return buf; |
394 | 0 | } |
395 | | |
396 | 0 | int OPENSSL_isalnum(int c) { return OPENSSL_isalpha(c) || OPENSSL_isdigit(c); } |
397 | | |
398 | 0 | int OPENSSL_tolower(int c) { |
399 | 0 | if (c >= 'A' && c <= 'Z') { |
400 | 0 | return c + ('a' - 'A'); |
401 | 0 | } |
402 | 0 | return c; |
403 | 0 | } |
404 | | |
405 | 0 | int OPENSSL_isspace(int c) { |
406 | 0 | return c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || |
407 | 0 | c == ' '; |
408 | 0 | } |
409 | | |
410 | 0 | int OPENSSL_strcasecmp(const char *a, const char *b) { |
411 | 0 | for (size_t i = 0;; i++) { |
412 | 0 | const int aa = OPENSSL_tolower(a[i]); |
413 | 0 | const int bb = OPENSSL_tolower(b[i]); |
414 | |
|
415 | 0 | if (aa < bb) { |
416 | 0 | return -1; |
417 | 0 | } else if (aa > bb) { |
418 | 0 | return 1; |
419 | 0 | } else if (aa == 0) { |
420 | 0 | return 0; |
421 | 0 | } |
422 | 0 | } |
423 | 0 | } |
424 | | |
425 | 0 | int OPENSSL_strncasecmp(const char *a, const char *b, size_t n) { |
426 | 0 | for (size_t i = 0; i < n; i++) { |
427 | 0 | const int aa = OPENSSL_tolower(a[i]); |
428 | 0 | const int bb = OPENSSL_tolower(b[i]); |
429 | |
|
430 | 0 | if (aa < bb) { |
431 | 0 | return -1; |
432 | 0 | } else if (aa > bb) { |
433 | 0 | return 1; |
434 | 0 | } else if (aa == 0) { |
435 | 0 | return 0; |
436 | 0 | } |
437 | 0 | } |
438 | | |
439 | 0 | return 0; |
440 | 0 | } |
441 | | |
442 | 0 | int BIO_snprintf(char *buf, size_t n, const char *format, ...) { |
443 | 0 | va_list args; |
444 | 0 | va_start(args, format); |
445 | 0 | int ret = BIO_vsnprintf(buf, n, format, args); |
446 | 0 | va_end(args); |
447 | 0 | return ret; |
448 | 0 | } |
449 | | |
450 | 0 | int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args) { |
451 | 0 | return vsnprintf(buf, n, format, args); |
452 | 0 | } |
453 | | |
454 | | int OPENSSL_vasprintf_internal(char **str, const char *format, va_list args, |
455 | 0 | int system_malloc) { |
456 | 0 | void *(*allocate)(size_t) = system_malloc ? malloc : OPENSSL_malloc; |
457 | 0 | void (*deallocate)(void *) = system_malloc ? free : OPENSSL_free; |
458 | 0 | void *(*reallocate)(void *, size_t) = |
459 | 0 | system_malloc ? realloc : OPENSSL_realloc; |
460 | 0 | char *candidate = NULL; |
461 | 0 | size_t candidate_len = 64; // TODO(bbe) what's the best initial size? |
462 | |
|
463 | 0 | if ((candidate = allocate(candidate_len)) == NULL) { |
464 | 0 | goto err; |
465 | 0 | } |
466 | 0 | va_list args_copy; |
467 | 0 | va_copy(args_copy, args); |
468 | 0 | int ret = vsnprintf(candidate, candidate_len, format, args_copy); |
469 | 0 | va_end(args_copy); |
470 | 0 | if (ret < 0) { |
471 | 0 | goto err; |
472 | 0 | } |
473 | 0 | if ((size_t)ret >= candidate_len) { |
474 | | // Too big to fit in allocation. |
475 | 0 | char *tmp; |
476 | |
|
477 | 0 | candidate_len = (size_t)ret + 1; |
478 | 0 | if ((tmp = reallocate(candidate, candidate_len)) == NULL) { |
479 | 0 | goto err; |
480 | 0 | } |
481 | 0 | candidate = tmp; |
482 | 0 | ret = vsnprintf(candidate, candidate_len, format, args); |
483 | 0 | } |
484 | | // At this point this should not happen unless vsnprintf is insane. |
485 | 0 | if (ret < 0 || (size_t)ret >= candidate_len) { |
486 | 0 | goto err; |
487 | 0 | } |
488 | 0 | *str = candidate; |
489 | 0 | return ret; |
490 | | |
491 | 0 | err: |
492 | 0 | deallocate(candidate); |
493 | 0 | *str = NULL; |
494 | 0 | errno = ENOMEM; |
495 | 0 | return -1; |
496 | 0 | } |
497 | | |
498 | 0 | int OPENSSL_vasprintf(char **str, const char *format, va_list args) { |
499 | 0 | return OPENSSL_vasprintf_internal(str, format, args, /*system_malloc=*/0); |
500 | 0 | } |
501 | | |
502 | 0 | int OPENSSL_asprintf(char **str, const char *format, ...) { |
503 | 0 | va_list args; |
504 | 0 | va_start(args, format); |
505 | 0 | int ret = OPENSSL_vasprintf(str, format, args); |
506 | 0 | va_end(args); |
507 | 0 | return ret; |
508 | 0 | } |
509 | | |
510 | 0 | char *OPENSSL_strndup(const char *str, size_t size) { |
511 | 0 | size = OPENSSL_strnlen(str, size); |
512 | |
|
513 | 0 | size_t alloc_size = size + 1; |
514 | 0 | if (alloc_size < size) { |
515 | | // overflow |
516 | 0 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_MALLOC_FAILURE); |
517 | 0 | return NULL; |
518 | 0 | } |
519 | 0 | char *ret = OPENSSL_malloc(alloc_size); |
520 | 0 | if (ret == NULL) { |
521 | 0 | return NULL; |
522 | 0 | } |
523 | | |
524 | 0 | OPENSSL_memcpy(ret, str, size); |
525 | 0 | ret[size] = '\0'; |
526 | 0 | return ret; |
527 | 0 | } |
528 | | |
529 | 0 | size_t OPENSSL_strlcpy(char *dst, const char *src, size_t dst_size) { |
530 | 0 | size_t l = 0; |
531 | |
|
532 | 0 | for (; dst_size > 1 && *src; dst_size--) { |
533 | 0 | *dst++ = *src++; |
534 | 0 | l++; |
535 | 0 | } |
536 | |
|
537 | 0 | if (dst_size) { |
538 | 0 | *dst = 0; |
539 | 0 | } |
540 | |
|
541 | 0 | return l + strlen(src); |
542 | 0 | } |
543 | | |
544 | 0 | size_t OPENSSL_strlcat(char *dst, const char *src, size_t dst_size) { |
545 | 0 | size_t l = 0; |
546 | 0 | for (; dst_size > 0 && *dst; dst_size--, dst++) { |
547 | 0 | l++; |
548 | 0 | } |
549 | 0 | return l + OPENSSL_strlcpy(dst, src, dst_size); |
550 | 0 | } |
551 | | |
552 | 0 | void *OPENSSL_memdup(const void *data, size_t size) { |
553 | 0 | if (size == 0) { |
554 | 0 | return NULL; |
555 | 0 | } |
556 | | |
557 | 0 | void *ret = OPENSSL_malloc(size); |
558 | 0 | if (ret == NULL) { |
559 | 0 | return NULL; |
560 | 0 | } |
561 | | |
562 | 0 | OPENSSL_memcpy(ret, data, size); |
563 | 0 | return ret; |
564 | 0 | } |
565 | | |
566 | 0 | void *CRYPTO_malloc(size_t size, const char *file, int line) { |
567 | 0 | return OPENSSL_malloc(size); |
568 | 0 | } |
569 | | |
570 | 0 | void *CRYPTO_realloc(void *ptr, size_t new_size, const char *file, int line) { |
571 | 0 | return OPENSSL_realloc(ptr, new_size); |
572 | 0 | } |
573 | | |
574 | 0 | void CRYPTO_free(void *ptr, const char *file, int line) { OPENSSL_free(ptr); } |