]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
cf72c757 | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
28d59af8 | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
28d59af8 MC |
8 | */ |
9 | ||
706457b7 DMSP |
10 | #include "../ssl_local.h" |
11 | #include "record_local.h" | |
28d59af8 | 12 | |
eda75751 | 13 | void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, size_t n) |
28d59af8 | 14 | { |
61986d32 | 15 | if (d != NULL) |
28d59af8 MC |
16 | memcpy(b->buf, d, n); |
17 | b->left = n; | |
18 | b->offset = 0; | |
19 | } | |
20 | ||
6b41b3f5 | 21 | /* |
dad78fb1 MC |
22 | * Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also |
23 | * retains the default_len setting | |
6b41b3f5 MC |
24 | */ |
25 | void SSL3_BUFFER_clear(SSL3_BUFFER *b) | |
26 | { | |
dad78fb1 MC |
27 | b->offset = 0; |
28 | b->left = 0; | |
6b41b3f5 MC |
29 | } |
30 | ||
28d59af8 MC |
31 | void SSL3_BUFFER_release(SSL3_BUFFER *b) |
32 | { | |
b548a1f1 | 33 | OPENSSL_free(b->buf); |
28d59af8 MC |
34 | b->buf = NULL; |
35 | } | |
76042020 MC |
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; | |
0485d540 | 42 | |
76042020 MC |
43 | b = RECORD_LAYER_get_rbuf(&s->rlayer); |
44 | ||
246b52f3 | 45 | if (SSL_IS_DTLS(s)) |
76042020 MC |
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; | |
76042020 MC |
57 | #ifndef OPENSSL_NO_COMP |
58 | if (ssl_allow_compression(s)) | |
59 | len += SSL3_RT_MAX_COMPRESSED_OVERHEAD; | |
60 | #endif | |
dad78fb1 MC |
61 | if (b->default_len > len) |
62 | len = b->default_len; | |
196f2cbb MC |
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, SSL_F_SSL3_SETUP_READ_BUFFER, | |
70 | ERR_R_MALLOC_FAILURE); | |
71 | return 0; | |
72 | } | |
76042020 MC |
73 | b->buf = p; |
74 | b->len = len; | |
75 | } | |
76 | ||
7a7048af | 77 | RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0])); |
76042020 | 78 | return 1; |
76042020 MC |
79 | } |
80 | ||
7ee8627f | 81 | int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len) |
76042020 MC |
82 | { |
83 | unsigned char *p; | |
58c27c20 | 84 | size_t align = 0, headerlen; |
76042020 | 85 | SSL3_BUFFER *wb; |
7ee8627f | 86 | size_t currpipe; |
d102d9df MC |
87 | |
88 | s->rlayer.numwpipes = numwpipes; | |
76042020 | 89 | |
58c27c20 MC |
90 | if (len == 0) { |
91 | if (SSL_IS_DTLS(s)) | |
92 | headerlen = DTLS1_RT_HEADER_LENGTH + 1; | |
93 | else | |
94 | headerlen = SSL3_RT_HEADER_LENGTH; | |
76042020 MC |
95 | |
96 | #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0 | |
58c27c20 | 97 | align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1); |
76042020 MC |
98 | #endif |
99 | ||
cf72c757 | 100 | len = ssl_get_max_send_fragment(s) |
58c27c20 | 101 | + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align; |
76042020 | 102 | #ifndef OPENSSL_NO_COMP |
58c27c20 MC |
103 | if (ssl_allow_compression(s)) |
104 | len += SSL3_RT_MAX_COMPRESSED_OVERHEAD; | |
76042020 | 105 | #endif |
58c27c20 MC |
106 | if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)) |
107 | len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD; | |
108 | } | |
76042020 | 109 | |
d102d9df MC |
110 | wb = RECORD_LAYER_get_wbuf(&s->rlayer); |
111 | for (currpipe = 0; currpipe < numwpipes; currpipe++) { | |
4bf08600 MC |
112 | SSL3_BUFFER *thiswb = &wb[currpipe]; |
113 | ||
50ec7505 | 114 | if (thiswb->len != len) { |
cf72c757 F |
115 | OPENSSL_free(thiswb->buf); |
116 | thiswb->buf = NULL; /* force reallocation */ | |
117 | } | |
118 | ||
4bf08600 | 119 | if (thiswb->buf == NULL) { |
50ec7505 BP |
120 | if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) { |
121 | p = OPENSSL_malloc(len); | |
122 | if (p == NULL) { | |
123 | s->rlayer.numwpipes = currpipe; | |
124 | /* | |
125 | * We've got a malloc failure, and we're still initialising | |
126 | * buffers. We assume we're so doomed that we won't even be able | |
127 | * to send an alert. | |
128 | */ | |
129 | SSLfatal(s, SSL_AD_NO_ALERT, | |
130 | SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE); | |
131 | return 0; | |
132 | } | |
133 | } else { | |
134 | p = NULL; | |
d102d9df | 135 | } |
4bf08600 MC |
136 | memset(thiswb, 0, sizeof(SSL3_BUFFER)); |
137 | thiswb->buf = p; | |
138 | thiswb->len = len; | |
d102d9df | 139 | } |
76042020 MC |
140 | } |
141 | ||
142 | return 1; | |
76042020 MC |
143 | } |
144 | ||
145 | int ssl3_setup_buffers(SSL *s) | |
146 | { | |
196f2cbb MC |
147 | if (!ssl3_setup_read_buffer(s)) { |
148 | /* SSLfatal() already called */ | |
76042020 | 149 | return 0; |
196f2cbb | 150 | } |
f63a17d6 MC |
151 | if (!ssl3_setup_write_buffer(s, 1, 0)) { |
152 | /* SSLfatal() already called */ | |
76042020 | 153 | return 0; |
f63a17d6 | 154 | } |
76042020 MC |
155 | return 1; |
156 | } | |
157 | ||
158 | int ssl3_release_write_buffer(SSL *s) | |
159 | { | |
160 | SSL3_BUFFER *wb; | |
8b0e934a | 161 | size_t pipes; |
76042020 | 162 | |
d102d9df MC |
163 | pipes = s->rlayer.numwpipes; |
164 | while (pipes > 0) { | |
165 | wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1]; | |
76042020 | 166 | |
50ec7505 BP |
167 | if (s->wbio == NULL || !BIO_get_ktls_send(s->wbio)) |
168 | OPENSSL_free(wb->buf); | |
d102d9df MC |
169 | wb->buf = NULL; |
170 | pipes--; | |
171 | } | |
172 | s->rlayer.numwpipes = 0; | |
76042020 MC |
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); | |
b548a1f1 RS |
181 | OPENSSL_free(b->buf); |
182 | b->buf = NULL; | |
76042020 MC |
183 | return 1; |
184 | } |