buffer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. /*
  13. * LIMIT_BEFORE_EXPANSION is the maximum n such that (n+3)/3*4 < 2**31. That
  14. * function is applied in several functions in this file and this limit
  15. * ensures that the result fits in an int.
  16. */
  17. #define LIMIT_BEFORE_EXPANSION 0x5ffffffc
  18. BUF_MEM *BUF_MEM_new_ex(unsigned long flags)
  19. {
  20. BUF_MEM *ret;
  21. ret = BUF_MEM_new();
  22. if (ret != NULL)
  23. ret->flags = flags;
  24. return ret;
  25. }
  26. BUF_MEM *BUF_MEM_new(void)
  27. {
  28. BUF_MEM *ret;
  29. ret = OPENSSL_zalloc(sizeof(*ret));
  30. if (ret == NULL) {
  31. ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
  32. return NULL;
  33. }
  34. return ret;
  35. }
  36. void BUF_MEM_free(BUF_MEM *a)
  37. {
  38. if (a == NULL)
  39. return;
  40. if (a->data != NULL) {
  41. if (a->flags & BUF_MEM_FLAG_SECURE)
  42. OPENSSL_secure_clear_free(a->data, a->max);
  43. else
  44. OPENSSL_clear_free(a->data, a->max);
  45. }
  46. OPENSSL_free(a);
  47. }
  48. /* Allocate a block of secure memory; copy over old data if there
  49. * was any, and then free it. */
  50. static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
  51. {
  52. char *ret;
  53. ret = OPENSSL_secure_malloc(len);
  54. if (str->data != NULL) {
  55. if (ret != NULL) {
  56. memcpy(ret, str->data, str->length);
  57. OPENSSL_secure_clear_free(str->data, str->length);
  58. str->data = NULL;
  59. }
  60. }
  61. return ret;
  62. }
  63. size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
  64. {
  65. char *ret;
  66. size_t n;
  67. if (str->length >= len) {
  68. str->length = len;
  69. return len;
  70. }
  71. if (str->max >= len) {
  72. if (str->data != NULL)
  73. memset(&str->data[str->length], 0, len - str->length);
  74. str->length = len;
  75. return len;
  76. }
  77. /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
  78. if (len > LIMIT_BEFORE_EXPANSION) {
  79. ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
  80. return 0;
  81. }
  82. n = (len + 3) / 3 * 4;
  83. if ((str->flags & BUF_MEM_FLAG_SECURE))
  84. ret = sec_alloc_realloc(str, n);
  85. else
  86. ret = OPENSSL_realloc(str->data, n);
  87. if (ret == NULL) {
  88. ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
  89. len = 0;
  90. } else {
  91. str->data = ret;
  92. str->max = n;
  93. memset(&str->data[str->length], 0, len - str->length);
  94. str->length = len;
  95. }
  96. return len;
  97. }
  98. size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
  99. {
  100. char *ret;
  101. size_t n;
  102. if (str->length >= len) {
  103. if (str->data != NULL)
  104. memset(&str->data[len], 0, str->length - len);
  105. str->length = len;
  106. return len;
  107. }
  108. if (str->max >= len) {
  109. memset(&str->data[str->length], 0, len - str->length);
  110. str->length = len;
  111. return len;
  112. }
  113. /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
  114. if (len > LIMIT_BEFORE_EXPANSION) {
  115. ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
  116. return 0;
  117. }
  118. n = (len + 3) / 3 * 4;
  119. if ((str->flags & BUF_MEM_FLAG_SECURE))
  120. ret = sec_alloc_realloc(str, n);
  121. else
  122. ret = OPENSSL_clear_realloc(str->data, str->max, n);
  123. if (ret == NULL) {
  124. ERR_raise(ERR_LIB_BUF, ERR_R_MALLOC_FAILURE);
  125. len = 0;
  126. } else {
  127. str->data = ret;
  128. str->max = n;
  129. memset(&str->data[str->length], 0, len - str->length);
  130. str->length = len;
  131. }
  132. return len;
  133. }
  134. void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
  135. {
  136. size_t i;
  137. if (in) {
  138. out += size - 1;
  139. for (i = 0; i < size; i++)
  140. *out-- = *in++;
  141. } else {
  142. unsigned char *q;
  143. char c;
  144. q = out + size - 1;
  145. for (i = 0; i < size / 2; i++) {
  146. c = *q;
  147. *q-- = *out;
  148. *out++ = c;
  149. }
  150. }
  151. }