2
0

ssl3_buffer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 1995-2017 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_locl.h"
  10. #include "record_locl.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->buf != NULL && thiswb->len != len) {
  98. OPENSSL_free(thiswb->buf);
  99. thiswb->buf = NULL; /* force reallocation */
  100. }
  101. if (thiswb->buf == NULL) {
  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,
  111. SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
  112. return 0;
  113. }
  114. memset(thiswb, 0, sizeof(SSL3_BUFFER));
  115. thiswb->buf = p;
  116. thiswb->len = len;
  117. }
  118. }
  119. return 1;
  120. }
  121. int ssl3_setup_buffers(SSL *s)
  122. {
  123. if (!ssl3_setup_read_buffer(s)) {
  124. /* SSLfatal() already called */
  125. return 0;
  126. }
  127. if (!ssl3_setup_write_buffer(s, 1, 0)) {
  128. /* SSLfatal() already called */
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. int ssl3_release_write_buffer(SSL *s)
  134. {
  135. SSL3_BUFFER *wb;
  136. size_t pipes;
  137. pipes = s->rlayer.numwpipes;
  138. while (pipes > 0) {
  139. wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
  140. OPENSSL_free(wb->buf);
  141. wb->buf = NULL;
  142. pipes--;
  143. }
  144. s->rlayer.numwpipes = 0;
  145. return 1;
  146. }
  147. int ssl3_release_read_buffer(SSL *s)
  148. {
  149. SSL3_BUFFER *b;
  150. b = RECORD_LAYER_get_rbuf(&s->rlayer);
  151. OPENSSL_free(b->buf);
  152. b->buf = NULL;
  153. return 1;
  154. }