SSL: refactor all SSLfatal() calls
[openssl.git] / ssl / record / ssl3_buffer.c
1 /*
2  * Copyright 1995-2020 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_read_buffer(SSL *s)
38 {
39     unsigned char *p;
40     size_t len, align = 0, headerlen;
41     SSL3_BUFFER *b;
42
43     b = RECORD_LAYER_get_rbuf(&s->rlayer);
44
45     if (SSL_IS_DTLS(s))
46         headerlen = DTLS1_RT_HEADER_LENGTH;
47     else
48         headerlen = SSL3_RT_HEADER_LENGTH;
49
50 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
51     align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
52 #endif
53
54     if (b->buf == NULL) {
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;
60 #endif
61         if (b->default_len > len)
62             len = b->default_len;
63         if ((p = OPENSSL_malloc(len)) == NULL) {
64             /*
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
67              * alert.
68              */
69             SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
70             return 0;
71         }
72         b->buf = p;
73         b->len = len;
74     }
75
76     RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0]));
77     return 1;
78 }
79
80 int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len)
81 {
82     unsigned char *p;
83     size_t align = 0, headerlen;
84     SSL3_BUFFER *wb;
85     size_t currpipe;
86
87     s->rlayer.numwpipes = numwpipes;
88
89     if (len == 0) {
90         if (SSL_IS_DTLS(s))
91             headerlen = DTLS1_RT_HEADER_LENGTH + 1;
92         else
93             headerlen = SSL3_RT_HEADER_LENGTH;
94
95 #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
96         align = SSL3_ALIGN_PAYLOAD - 1;
97 #endif
98
99         len = ssl_get_max_send_fragment(s)
100             + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
101 #ifndef OPENSSL_NO_COMP
102         if (ssl_allow_compression(s))
103             len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
104 #endif
105         if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
106             len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
107     }
108
109     wb = RECORD_LAYER_get_wbuf(&s->rlayer);
110     for (currpipe = 0; currpipe < numwpipes; currpipe++) {
111         SSL3_BUFFER *thiswb = &wb[currpipe];
112
113         if (thiswb->len != len) {
114             OPENSSL_free(thiswb->buf);
115             thiswb->buf = NULL;         /* force reallocation */
116         }
117
118         if (thiswb->buf == NULL) {
119             if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) {
120                 p = OPENSSL_malloc(len);
121                 if (p == NULL) {
122                     s->rlayer.numwpipes = currpipe;
123                     /*
124                      * We've got a malloc failure, and we're still initialising
125                      * buffers. We assume we're so doomed that we won't even be able
126                      * to send an alert.
127                      */
128                     SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_MALLOC_FAILURE);
129                     return 0;
130                 }
131             } else {
132                 p = NULL;
133             }
134             memset(thiswb, 0, sizeof(SSL3_BUFFER));
135             thiswb->buf = p;
136             thiswb->len = len;
137         }
138     }
139
140     return 1;
141 }
142
143 int ssl3_setup_buffers(SSL *s)
144 {
145     if (!ssl3_setup_read_buffer(s)) {
146         /* SSLfatal() already called */
147         return 0;
148     }
149     if (!ssl3_setup_write_buffer(s, 1, 0)) {
150         /* SSLfatal() already called */
151         return 0;
152     }
153     return 1;
154 }
155
156 int ssl3_release_write_buffer(SSL *s)
157 {
158     SSL3_BUFFER *wb;
159     size_t pipes;
160
161     pipes = s->rlayer.numwpipes;
162     while (pipes > 0) {
163         wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
164
165         if (SSL3_BUFFER_is_app_buffer(wb))
166             SSL3_BUFFER_set_app_buffer(wb, 0);
167         else
168             OPENSSL_free(wb->buf);
169         wb->buf = NULL;
170         pipes--;
171     }
172     s->rlayer.numwpipes = 0;
173     return 1;
174 }
175
176 int ssl3_release_read_buffer(SSL *s)
177 {
178     SSL3_BUFFER *b;
179
180     b = RECORD_LAYER_get_rbuf(&s->rlayer);
181     if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
182         OPENSSL_cleanse(b->buf, b->len);
183     OPENSSL_free(b->buf);
184     b->buf = NULL;
185     return 1;
186 }