A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.4 KiB

  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <limits.h>
  12. #include <openssl/crypto.h>
  13. #include "internal/cryptlib.h"
  14. /*
  15. * the following pointers may be changed as long as 'allow_customize' is set
  16. */
  17. static int allow_customize = 1;
  18. static void *(*malloc_impl)(size_t, const char *, int)
  19. = CRYPTO_malloc;
  20. static void *(*realloc_impl)(void *, size_t, const char *, int)
  21. = CRYPTO_realloc;
  22. static void (*free_impl)(void *, const char *, int)
  23. = CRYPTO_free;
  24. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  25. static int call_malloc_debug = 1;
  26. #else
  27. static int call_malloc_debug = 0;
  28. #endif
  29. int CRYPTO_set_mem_functions(
  30. void *(*m)(size_t, const char *, int),
  31. void *(*r)(void *, size_t, const char *, int),
  32. void (*f)(void *, const char *, int))
  33. {
  34. if (!allow_customize)
  35. return 0;
  36. if (m)
  37. malloc_impl = m;
  38. if (r)
  39. realloc_impl = r;
  40. if (f)
  41. free_impl = f;
  42. return 1;
  43. }
  44. int CRYPTO_set_mem_debug(int flag)
  45. {
  46. if (!allow_customize)
  47. return 0;
  48. call_malloc_debug = flag;
  49. return 1;
  50. }
  51. void CRYPTO_get_mem_functions(
  52. void *(**m)(size_t, const char *, int),
  53. void *(**r)(void *, size_t, const char *, int),
  54. void (**f)(void *, const char *, int))
  55. {
  56. if (m != NULL)
  57. *m = malloc_impl;
  58. if (r != NULL)
  59. *r = realloc_impl;
  60. if (f != NULL)
  61. *f = free_impl;
  62. }
  63. void *CRYPTO_malloc(size_t num, const char *file, int line)
  64. {
  65. void *ret = NULL;
  66. if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
  67. return malloc_impl(num, file, line);
  68. if (num <= 0)
  69. return NULL;
  70. allow_customize = 0;
  71. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  72. if (call_malloc_debug) {
  73. CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
  74. ret = malloc(num);
  75. CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
  76. } else {
  77. ret = malloc(num);
  78. }
  79. #else
  80. osslargused(file); osslargused(line);
  81. ret = malloc(num);
  82. #endif
  83. return ret;
  84. }
  85. void *CRYPTO_zalloc(size_t num, const char *file, int line)
  86. {
  87. void *ret = CRYPTO_malloc(num, file, line);
  88. if (ret != NULL)
  89. memset(ret, 0, num);
  90. return ret;
  91. }
  92. void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
  93. {
  94. if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
  95. return realloc_impl(str, num, file, line);
  96. if (str == NULL)
  97. return CRYPTO_malloc(num, file, line);
  98. if (num == 0) {
  99. CRYPTO_free(str, file, line);
  100. return NULL;
  101. }
  102. allow_customize = 0;
  103. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  104. if (call_malloc_debug) {
  105. void *ret;
  106. CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
  107. ret = realloc(str, num);
  108. CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
  109. return ret;
  110. }
  111. #else
  112. osslargused(file); osslargused(line);
  113. #endif
  114. return realloc(str, num);
  115. }
  116. void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
  117. const char *file, int line)
  118. {
  119. void *ret = NULL;
  120. if (str == NULL)
  121. return CRYPTO_malloc(num, file, line);
  122. if (num == 0) {
  123. CRYPTO_clear_free(str, old_len, file, line);
  124. return NULL;
  125. }
  126. /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
  127. if (num < old_len) {
  128. memset((char*)str + num, 0, old_len - num);
  129. return str;
  130. }
  131. ret = CRYPTO_malloc(num, file, line);
  132. if (ret != NULL) {
  133. memcpy(ret, str, old_len);
  134. CRYPTO_clear_free(str, old_len, file, line);
  135. }
  136. return ret;
  137. }
  138. void CRYPTO_free(void *str, const char *file, int line)
  139. {
  140. if (free_impl != NULL && free_impl != &CRYPTO_free) {
  141. free_impl(str, file, line);
  142. return;
  143. }
  144. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  145. if (call_malloc_debug) {
  146. CRYPTO_mem_debug_free(str, 0, file, line);
  147. free(str);
  148. CRYPTO_mem_debug_free(str, 1, file, line);
  149. } else {
  150. free(str);
  151. }
  152. #else
  153. free(str);
  154. #endif
  155. }
  156. void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
  157. {
  158. if (str == NULL)
  159. return;
  160. if (num)
  161. OPENSSL_cleanse(str, num);
  162. CRYPTO_free(str, file, line);
  163. }