ssl3_buffer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright 1995-2021 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. #ifndef OPENSSL_NO_COMP
  87. if (ssl_allow_compression(s))
  88. len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  89. #endif
  90. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  91. len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  92. }
  93. wb = RECORD_LAYER_get_wbuf(&s->rlayer);
  94. for (currpipe = 0; currpipe < numwpipes; currpipe++) {
  95. SSL3_BUFFER *thiswb = &wb[currpipe];
  96. if (thiswb->buf != NULL && thiswb->len != len) {
  97. OPENSSL_free(thiswb->buf);
  98. thiswb->buf = NULL; /* force reallocation */
  99. }
  100. if (thiswb->buf == NULL) {
  101. p = OPENSSL_malloc(len);
  102. if (p == NULL) {
  103. s->rlayer.numwpipes = currpipe;
  104. /*
  105. * We've got a malloc failure, and we're still initialising
  106. * buffers. We assume we're so doomed that we won't even be able
  107. * to send an alert.
  108. */
  109. SSLfatal(s, SSL_AD_NO_ALERT,
  110. SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
  111. return 0;
  112. }
  113. memset(thiswb, 0, sizeof(SSL3_BUFFER));
  114. thiswb->buf = p;
  115. thiswb->len = len;
  116. }
  117. }
  118. return 1;
  119. }
  120. int ssl3_setup_buffers(SSL *s)
  121. {
  122. if (!ssl3_setup_read_buffer(s)) {
  123. /* SSLfatal() already called */
  124. return 0;
  125. }
  126. if (!ssl3_setup_write_buffer(s, 1, 0)) {
  127. /* SSLfatal() already called */
  128. return 0;
  129. }
  130. return 1;
  131. }
  132. int ssl3_release_write_buffer(SSL *s)
  133. {
  134. SSL3_BUFFER *wb;
  135. size_t pipes;
  136. pipes = s->rlayer.numwpipes;
  137. while (pipes > 0) {
  138. wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
  139. OPENSSL_free(wb->buf);
  140. wb->buf = NULL;
  141. pipes--;
  142. }
  143. s->rlayer.numwpipes = 0;
  144. return 1;
  145. }
  146. int ssl3_release_read_buffer(SSL *s)
  147. {
  148. SSL3_BUFFER *b;
  149. b = RECORD_LAYER_get_rbuf(&s->rlayer);
  150. OPENSSL_free(b->buf);
  151. b->buf = NULL;
  152. return 1;
  153. }