ssl3_buffer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 1995-2017 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, 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. RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0]));
  68. return 1;
  69. }
  70. int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len)
  71. {
  72. unsigned char *p;
  73. size_t align = 0, headerlen;
  74. SSL3_BUFFER *wb;
  75. size_t currpipe;
  76. s->rlayer.numwpipes = numwpipes;
  77. if (len == 0) {
  78. if (SSL_IS_DTLS(s))
  79. headerlen = DTLS1_RT_HEADER_LENGTH + 1;
  80. else
  81. headerlen = SSL3_RT_HEADER_LENGTH;
  82. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  83. align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
  84. #endif
  85. len = ssl_get_max_send_fragment(s)
  86. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
  87. #ifndef OPENSSL_NO_COMP
  88. if (ssl_allow_compression(s))
  89. len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  90. #endif
  91. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  92. len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  93. }
  94. wb = RECORD_LAYER_get_wbuf(&s->rlayer);
  95. for (currpipe = 0; currpipe < numwpipes; currpipe++) {
  96. SSL3_BUFFER *thiswb = &wb[currpipe];
  97. if (thiswb->len != len) {
  98. OPENSSL_free(thiswb->buf);
  99. thiswb->buf = NULL; /* force reallocation */
  100. }
  101. if (thiswb->buf == NULL) {
  102. if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
  103. p = OPENSSL_malloc(len);
  104. if (p == NULL) {
  105. s->rlayer.numwpipes = currpipe;
  106. /*
  107. * We've got a malloc failure, and we're still initialising
  108. * buffers. We assume we're so doomed that we won't even be able
  109. * to send an alert.
  110. */
  111. SSLfatal(s, SSL_AD_NO_ALERT,
  112. SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
  113. return 0;
  114. }
  115. } else {
  116. p = NULL;
  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. if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio))
  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. OPENSSL_free(b->buf);
  157. b->buf = NULL;
  158. return 1;
  159. }