]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/record/ssl3_buffer.c
Remove some unnecessary function pointers from OSSL_RECORD_METHOD
[thirdparty/openssl.git] / ssl / record / ssl3_buffer.c
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
10 #include "../ssl_local.h"
11 #include "record_local.h"
12
13 void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, size_t n)
14 {
15 if (d != NULL)
16 memcpy(b->buf, d, n);
17 b->left = n;
18 b->offset = 0;
19 }
20
21 /*
22 * Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also
23 * retains the default_len setting
24 */
25 void SSL3_BUFFER_clear(SSL3_BUFFER *b)
26 {
27 b->offset = 0;
28 b->left = 0;
29 }
30
31 void SSL3_BUFFER_release(SSL3_BUFFER *b)
32 {
33 OPENSSL_free(b->buf);
34 b->buf = NULL;
35 }
36
37 int ssl3_setup_write_buffer(SSL_CONNECTION *s, size_t numwpipes, size_t len)
38 {
39 unsigned char *p;
40 size_t align = 0, headerlen;
41 SSL3_BUFFER *wb;
42 size_t currpipe;
43
44 s->rlayer.numwpipes = numwpipes;
45
46 if (len == 0) {
47 if (SSL_CONNECTION_IS_DTLS(s))
48 headerlen = DTLS1_RT_HEADER_LENGTH + 1;
49 else
50 headerlen = SSL3_RT_HEADER_LENGTH;
51
52 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
53 align = SSL3_ALIGN_PAYLOAD - 1;
54 #endif
55
56 len = ssl_get_max_send_fragment(s)
57 + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
58 #ifndef OPENSSL_NO_COMP
59 if (ssl_allow_compression(s))
60 len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
61 #endif
62 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
63 len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
64 }
65
66 wb = RECORD_LAYER_get_wbuf(&s->rlayer);
67 for (currpipe = 0; currpipe < numwpipes; currpipe++) {
68 SSL3_BUFFER *thiswb = &wb[currpipe];
69
70 if (thiswb->len != len) {
71 OPENSSL_free(thiswb->buf);
72 thiswb->buf = NULL; /* force reallocation */
73 }
74
75 if (thiswb->buf == NULL) {
76 if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
77 p = OPENSSL_malloc(len);
78 if (p == NULL) {
79 s->rlayer.numwpipes = currpipe;
80 /*
81 * We've got a malloc failure, and we're still initialising
82 * buffers. We assume we're so doomed that we won't even be able
83 * to send an alert.
84 */
85 SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
86 return 0;
87 }
88 } else {
89 p = NULL;
90 }
91 memset(thiswb, 0, sizeof(SSL3_BUFFER));
92 thiswb->buf = p;
93 thiswb->len = len;
94 }
95 }
96
97 return 1;
98 }
99
100 int ssl3_setup_buffers(SSL_CONNECTION *s)
101 {
102 if (!ssl3_setup_write_buffer(s, 1, 0)) {
103 /* SSLfatal() already called */
104 return 0;
105 }
106 return 1;
107 }
108
109 int ssl3_release_write_buffer(SSL_CONNECTION *s)
110 {
111 SSL3_BUFFER *wb;
112 size_t pipes;
113
114 pipes = s->rlayer.numwpipes;
115 while (pipes > 0) {
116 wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
117
118 if (SSL3_BUFFER_is_app_buffer(wb))
119 SSL3_BUFFER_set_app_buffer(wb, 0);
120 else
121 OPENSSL_free(wb->buf);
122 wb->buf = NULL;
123 pipes--;
124 }
125 s->rlayer.numwpipes = 0;
126 return 1;
127 }