ssl3_buffer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 1995-2023 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 "../ssl_local.h"
  10. #include "record_local.h"
  11. void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, size_t n)
  12. {
  13. if (d != NULL)
  14. memcpy(b->buf, d, n);
  15. b->left = n;
  16. b->offset = 0;
  17. }
  18. /*
  19. * Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also
  20. * retains the default_len setting
  21. */
  22. void SSL3_BUFFER_clear(SSL3_BUFFER *b)
  23. {
  24. b->offset = 0;
  25. b->left = 0;
  26. }
  27. void SSL3_BUFFER_release(SSL3_BUFFER *b)
  28. {
  29. OPENSSL_free(b->buf);
  30. b->buf = NULL;
  31. }
  32. int ssl3_setup_read_buffer(SSL *s)
  33. {
  34. unsigned char *p;
  35. size_t len, align = 0, headerlen;
  36. SSL3_BUFFER *b;
  37. b = RECORD_LAYER_get_rbuf(&s->rlayer);
  38. if (SSL_IS_DTLS(s))
  39. headerlen = DTLS1_RT_HEADER_LENGTH;
  40. else
  41. headerlen = SSL3_RT_HEADER_LENGTH;
  42. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  43. align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
  44. #endif
  45. if (b->buf == NULL) {
  46. len = SSL3_RT_MAX_PLAIN_LENGTH
  47. + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
  48. #ifndef OPENSSL_NO_COMP
  49. if (ssl_allow_compression(s))
  50. len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  51. #endif
  52. if (b->default_len > len)
  53. len = b->default_len;
  54. if ((p = OPENSSL_malloc(len)) == NULL) {
  55. /*
  56. * We've got a malloc failure, and we're still initialising buffers.
  57. * We assume we're so doomed that we won't even be able to send an
  58. * alert.
  59. */
  60. SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_SETUP_READ_BUFFER,
  61. ERR_R_MALLOC_FAILURE);
  62. return 0;
  63. }
  64. b->buf = p;
  65. b->len = len;
  66. }
  67. return 1;
  68. }
  69. int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len)
  70. {
  71. unsigned char *p;
  72. size_t align = 0, headerlen;
  73. SSL3_BUFFER *wb;
  74. size_t currpipe;
  75. s->rlayer.numwpipes = numwpipes;
  76. if (len == 0) {
  77. if (SSL_IS_DTLS(s))
  78. headerlen = DTLS1_RT_HEADER_LENGTH + 1;
  79. else
  80. headerlen = SSL3_RT_HEADER_LENGTH;
  81. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  82. align = SSL3_ALIGN_PAYLOAD - 1;
  83. #endif
  84. len = ssl_get_max_send_fragment(s)
  85. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align
  86. + SSL_RT_MAX_CIPHER_BLOCK_SIZE /* Explicit IV allowance */;
  87. #ifndef OPENSSL_NO_COMP
  88. if (ssl_allow_compression(s))
  89. len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  90. #endif
  91. /*
  92. * We don't need to add an allowance for eivlen here since empty
  93. * fragments only occur when we don't have an explicit IV
  94. */
  95. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  96. len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  97. }
  98. wb = RECORD_LAYER_get_wbuf(&s->rlayer);
  99. for (currpipe = 0; currpipe < numwpipes; currpipe++) {
  100. SSL3_BUFFER *thiswb = &wb[currpipe];
  101. if (thiswb->buf != NULL && thiswb->len != len) {
  102. OPENSSL_free(thiswb->buf);
  103. thiswb->buf = NULL; /* force reallocation */
  104. }
  105. if (thiswb->buf == NULL) {
  106. p = OPENSSL_malloc(len);
  107. if (p == NULL) {
  108. s->rlayer.numwpipes = currpipe;
  109. /*
  110. * We've got a malloc failure, and we're still initialising
  111. * buffers. We assume we're so doomed that we won't even be able
  112. * to send an alert.
  113. */
  114. SSLfatal(s, SSL_AD_NO_ALERT,
  115. SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
  116. return 0;
  117. }
  118. memset(thiswb, 0, sizeof(SSL3_BUFFER));
  119. thiswb->buf = p;
  120. thiswb->len = len;
  121. }
  122. }
  123. return 1;
  124. }
  125. int ssl3_setup_buffers(SSL *s)
  126. {
  127. if (!ssl3_setup_read_buffer(s)) {
  128. /* SSLfatal() already called */
  129. return 0;
  130. }
  131. if (!ssl3_setup_write_buffer(s, 1, 0)) {
  132. /* SSLfatal() already called */
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. int ssl3_release_write_buffer(SSL *s)
  138. {
  139. SSL3_BUFFER *wb;
  140. size_t pipes;
  141. pipes = s->rlayer.numwpipes;
  142. while (pipes > 0) {
  143. wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
  144. OPENSSL_free(wb->buf);
  145. wb->buf = NULL;
  146. pipes--;
  147. }
  148. s->rlayer.numwpipes = 0;
  149. return 1;
  150. }
  151. int ssl3_release_read_buffer(SSL *s)
  152. {
  153. SSL3_BUFFER *b;
  154. b = RECORD_LAYER_get_rbuf(&s->rlayer);
  155. OPENSSL_free(b->buf);
  156. b->buf = NULL;
  157. return 1;
  158. }