2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "../ssl_local.h"
11 #include "record_local.h"
13 void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, size_t n)
22 * Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also
23 * retains the default_len setting
25 void SSL3_BUFFER_clear(SSL3_BUFFER *b)
31 void SSL3_BUFFER_release(SSL3_BUFFER *b)
37 int ssl3_setup_read_buffer(SSL *s)
40 size_t len, align = 0, headerlen;
43 b = RECORD_LAYER_get_rbuf(&s->rlayer);
46 headerlen = DTLS1_RT_HEADER_LENGTH;
48 headerlen = SSL3_RT_HEADER_LENGTH;
50 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
51 align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
55 len = SSL3_RT_MAX_PLAIN_LENGTH
56 + SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
57 #ifndef OPENSSL_NO_COMP
58 if (ssl_allow_compression(s))
59 len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
61 if (b->default_len > len)
63 if ((p = OPENSSL_malloc(len)) == NULL) {
65 * We've got a malloc failure, and we're still initialising buffers.
66 * We assume we're so doomed that we won't even be able to send an
69 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
79 int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len)
82 size_t align = 0, headerlen;
86 s->rlayer.numwpipes = numwpipes;
90 headerlen = DTLS1_RT_HEADER_LENGTH + 1;
92 headerlen = SSL3_RT_HEADER_LENGTH;
94 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
95 align = SSL3_ALIGN_PAYLOAD - 1;
98 len = ssl_get_max_send_fragment(s)
99 + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
100 #ifndef OPENSSL_NO_COMP
101 if (ssl_allow_compression(s))
102 len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
104 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
105 len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
108 wb = RECORD_LAYER_get_wbuf(&s->rlayer);
109 for (currpipe = 0; currpipe < numwpipes; currpipe++) {
110 SSL3_BUFFER *thiswb = &wb[currpipe];
112 if (thiswb->len != len) {
113 OPENSSL_free(thiswb->buf);
114 thiswb->buf = NULL; /* force reallocation */
117 if (thiswb->buf == NULL) {
118 if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
119 p = OPENSSL_malloc(len);
121 s->rlayer.numwpipes = currpipe;
123 * We've got a malloc failure, and we're still initialising
124 * buffers. We assume we're so doomed that we won't even be able
127 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
133 memset(thiswb, 0, sizeof(SSL3_BUFFER));
142 int ssl3_setup_buffers(SSL *s)
144 if (!ssl3_setup_read_buffer(s)) {
145 /* SSLfatal() already called */
148 if (!ssl3_setup_write_buffer(s, 1, 0)) {
149 /* SSLfatal() already called */
155 int ssl3_release_write_buffer(SSL *s)
160 pipes = s->rlayer.numwpipes;
162 wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
164 if (SSL3_BUFFER_is_app_buffer(wb))
165 SSL3_BUFFER_set_app_buffer(wb, 0);
167 OPENSSL_free(wb->buf);
171 s->rlayer.numwpipes = 0;
175 int ssl3_release_read_buffer(SSL *s)
179 b = RECORD_LAYER_get_rbuf(&s->rlayer);
180 if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
181 OPENSSL_cleanse(b->buf, b->len);
182 OPENSSL_free(b->buf);