ssl3_buffer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 1995-2021 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_write_buffer(SSL_CONNECTION *s, size_t numwpipes, size_t len)
  33. {
  34. unsigned char *p;
  35. size_t align = 0, headerlen;
  36. SSL3_BUFFER *wb;
  37. size_t currpipe;
  38. s->rlayer.numwpipes = numwpipes;
  39. if (len == 0) {
  40. if (SSL_CONNECTION_IS_DTLS(s))
  41. headerlen = DTLS1_RT_HEADER_LENGTH + 1;
  42. else
  43. headerlen = SSL3_RT_HEADER_LENGTH;
  44. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
  45. align = SSL3_ALIGN_PAYLOAD - 1;
  46. #endif
  47. len = ssl_get_max_send_fragment(s)
  48. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
  49. #ifndef OPENSSL_NO_COMP
  50. if (ssl_allow_compression(s))
  51. len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
  52. #endif
  53. if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  54. len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
  55. }
  56. wb = RECORD_LAYER_get_wbuf(&s->rlayer);
  57. for (currpipe = 0; currpipe < numwpipes; currpipe++) {
  58. SSL3_BUFFER *thiswb = &wb[currpipe];
  59. if (thiswb->len != len) {
  60. OPENSSL_free(thiswb->buf);
  61. thiswb->buf = NULL; /* force reallocation */
  62. }
  63. if (thiswb->buf == NULL) {
  64. if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
  65. p = OPENSSL_malloc(len);
  66. if (p == NULL) {
  67. s->rlayer.numwpipes = currpipe;
  68. /*
  69. * We've got a malloc failure, and we're still initialising
  70. * buffers. We assume we're so doomed that we won't even be able
  71. * to send an alert.
  72. */
  73. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
  74. return 0;
  75. }
  76. } else {
  77. p = NULL;
  78. }
  79. memset(thiswb, 0, sizeof(SSL3_BUFFER));
  80. thiswb->buf = p;
  81. thiswb->len = len;
  82. }
  83. }
  84. return 1;
  85. }
  86. int ssl3_setup_buffers(SSL_CONNECTION *s)
  87. {
  88. if (!ssl3_setup_write_buffer(s, 1, 0)) {
  89. /* SSLfatal() already called */
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. int ssl3_release_write_buffer(SSL_CONNECTION *s)
  95. {
  96. SSL3_BUFFER *wb;
  97. size_t pipes;
  98. pipes = s->rlayer.numwpipes;
  99. while (pipes > 0) {
  100. wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
  101. if (SSL3_BUFFER_is_app_buffer(wb))
  102. SSL3_BUFFER_set_app_buffer(wb, 0);
  103. else
  104. OPENSSL_free(wb->buf);
  105. wb->buf = NULL;
  106. pipes--;
  107. }
  108. s->rlayer.numwpipes = 0;
  109. return 1;
  110. }