buffer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. return NULL;
  32. return ret;
  33. }
  34. void BUF_MEM_free(BUF_MEM *a)
  35. {
  36. if (a == NULL)
  37. return;
  38. if (a->data != NULL) {
  39. if (a->flags & BUF_MEM_FLAG_SECURE)
  40. OPENSSL_secure_clear_free(a->data, a->max);
  41. else
  42. OPENSSL_clear_free(a->data, a->max);
  43. }
  44. OPENSSL_free(a);
  45. }
  46. /* Allocate a block of secure memory; copy over old data if there
  47. * was any, and then free it. */
  48. static char *sec_alloc_realloc(BUF_MEM *str, size_t len)
  49. {
  50. char *ret;
  51. ret = OPENSSL_secure_malloc(len);
  52. if (str->data != NULL) {
  53. if (ret != NULL) {
  54. memcpy(ret, str->data, str->length);
  55. OPENSSL_secure_clear_free(str->data, str->length);
  56. str->data = NULL;
  57. }
  58. }
  59. return ret;
  60. }
  61. size_t BUF_MEM_grow(BUF_MEM *str, size_t len)
  62. {
  63. char *ret;
  64. size_t n;
  65. if (str->length >= len) {
  66. str->length = len;
  67. return len;
  68. }
  69. if (str->max >= len) {
  70. if (str->data != NULL)
  71. memset(&str->data[str->length], 0, len - str->length);
  72. str->length = len;
  73. return len;
  74. }
  75. /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
  76. if (len > LIMIT_BEFORE_EXPANSION) {
  77. ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT);
  78. return 0;
  79. }
  80. n = (len + 3) / 3 * 4;
  81. if ((str->flags & BUF_MEM_FLAG_SECURE))
  82. ret = sec_alloc_realloc(str, n);
  83. else
  84. ret = OPENSSL_realloc(str->data, n);
  85. if (ret == NULL) {
  86. len = 0;
  87. } else {
  88. str->data = ret;
  89. str->max = n;
  90. memset(&str->data[str->length], 0, len - str->length);
  91. str->length = len;
  92. }
  93. return len;
  94. }
  95. size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)
  96. {
  97. char *ret;
  98. size_t n;
  99. if (str->length >= len) {
  100. if (str->data != NULL)
  101. memset(&str->data[len], 0, str->length - len);
  102. str->length = len;
  103. return len;
  104. }
  105. if (str->max >= len) {
  106. memset(&str->data[str->length], 0, len - str->length);
  107. str->length = len;
  108. return len;
  109. }
  110. /* This limit is sufficient to ensure (len+3)/3*4 < 2**31 */
  111. if (len > LIMIT_BEFORE_EXPANSION) {
  112. ERR_raise(ERR_LIB_BUF, ERR_R_PASSED_INVALID_ARGUMENT);
  113. return 0;
  114. }
  115. n = (len + 3) / 3 * 4;
  116. if ((str->flags & BUF_MEM_FLAG_SECURE))
  117. ret = sec_alloc_realloc(str, n);
  118. else
  119. ret = OPENSSL_clear_realloc(str->data, str->max, n);
  120. if (ret == NULL) {
  121. len = 0;
  122. } else {
  123. str->data = ret;
  124. str->max = n;
  125. memset(&str->data[str->length], 0, len - str->length);
  126. str->length = len;
  127. }
  128. return len;
  129. }
  130. void BUF_reverse(unsigned char *out, const unsigned char *in, size_t size)
  131. {
  132. size_t i;
  133. if (in) {
  134. out += size - 1;
  135. for (i = 0; i < size; i++)
  136. *out-- = *in++;
  137. } else {
  138. unsigned char *q;
  139. char c;
  140. q = out + size - 1;
  141. for (i = 0; i < size / 2; i++) {
  142. c = *q;
  143. *q-- = *out;
  144. *out++ = c;
  145. }
  146. }
  147. }