ssl3_buffer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "../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, ERR_R_MALLOC_FAILURE);
  61. return 0;
  62. }
  63. b->buf = p;
  64. b->len = len;
  65. }
  66. RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0]));
  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->len != len) {
  97. OPENSSL_free(thiswb->buf);
  98. thiswb->buf = NULL; /* force reallocation */
  99. }
  100. if (thiswb->buf == NULL) {
  101. if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
  102. p = OPENSSL_malloc(len);
  103. if (p == NULL) {
  104. s->rlayer.numwpipes = currpipe;
  105. /*
  106. * We've got a malloc failure, and we're still initialising
  107. * buffers. We assume we're so doomed that we won't even be able
  108. * to send an alert.
  109. */
  110. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
  111. return 0;
  112. }
  113. } else {
  114. p = NULL;
  115. }
  116. memset(thiswb, 0, sizeof(SSL3_BUFFER));
  117. thiswb->buf = p;
  118. thiswb->len = len;
  119. }
  120. }
  121. return 1;
  122. }
  123. int ssl3_setup_buffers(SSL *s)
  124. {
  125. if (!ssl3_setup_read_buffer(s)) {
  126. /* SSLfatal() already called */
  127. return 0;
  128. }
  129. if (!ssl3_setup_write_buffer(s, 1, 0)) {
  130. /* SSLfatal() already called */
  131. return 0;
  132. }
  133. return 1;
  134. }
  135. int ssl3_release_write_buffer(SSL *s)
  136. {
  137. SSL3_BUFFER *wb;
  138. size_t pipes;
  139. pipes = s->rlayer.numwpipes;
  140. while (pipes > 0) {
  141. wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
  142. if (SSL3_BUFFER_is_app_buffer(wb))
  143. SSL3_BUFFER_set_app_buffer(wb, 0);
  144. else
  145. OPENSSL_free(wb->buf);
  146. wb->buf = NULL;
  147. pipes--;
  148. }
  149. s->rlayer.numwpipes = 0;
  150. return 1;
  151. }
  152. int ssl3_release_read_buffer(SSL *s)
  153. {
  154. SSL3_BUFFER *b;
  155. b = RECORD_LAYER_get_rbuf(&s->rlayer);
  156. if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
  157. OPENSSL_cleanse(b->buf, b->len);
  158. OPENSSL_free(b->buf);
  159. b->buf = NULL;
  160. return 1;
  161. }