• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/ssl.h>
16 
17 #include <assert.h>
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <openssl/bio.h>
23 #include <openssl/err.h>
24 #include <openssl/mem.h>
25 
26 #include "../crypto/internal.h"
27 #include "internal.h"
28 
29 
30 namespace bssl {
31 
32 // BIO uses int instead of size_t. No lengths will exceed uint16_t, so this will
33 // not overflow.
34 static_assert(0xffff <= INT_MAX, "uint16_t does not fit in int");
35 
36 static_assert((SSL3_ALIGN_PAYLOAD & (SSL3_ALIGN_PAYLOAD - 1)) == 0,
37               "SSL3_ALIGN_PAYLOAD must be a power of 2");
38 
Clear()39 void SSLBuffer::Clear() {
40   free(buf_);  // Allocated with malloc().
41   buf_ = nullptr;
42   offset_ = 0;
43   size_ = 0;
44   cap_ = 0;
45 }
46 
EnsureCap(size_t header_len,size_t new_cap)47 bool SSLBuffer::EnsureCap(size_t header_len, size_t new_cap) {
48   if (new_cap > 0xffff) {
49     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
50     return false;
51   }
52 
53   if (cap_ >= new_cap) {
54     return true;
55   }
56 
57   // Add up to |SSL3_ALIGN_PAYLOAD| - 1 bytes of slack for alignment.
58   //
59   // Since this buffer gets allocated quite frequently and doesn't contain any
60   // sensitive data, we allocate with malloc rather than |OPENSSL_malloc| and
61   // avoid zeroing on free.
62   uint8_t *new_buf = (uint8_t *)malloc(new_cap + SSL3_ALIGN_PAYLOAD - 1);
63   if (new_buf == NULL) {
64     OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
65     return false;
66   }
67 
68   // Offset the buffer such that the record body is aligned.
69   size_t new_offset =
70       (0 - header_len - (uintptr_t)new_buf) & (SSL3_ALIGN_PAYLOAD - 1);
71 
72   if (buf_ != NULL) {
73     OPENSSL_memcpy(new_buf + new_offset, buf_ + offset_, size_);
74     free(buf_);  // Allocated with malloc().
75   }
76 
77   buf_ = new_buf;
78   offset_ = new_offset;
79   cap_ = new_cap;
80   return true;
81 }
82 
DidWrite(size_t new_size)83 void SSLBuffer::DidWrite(size_t new_size) {
84   if (new_size > cap() - size()) {
85     abort();
86   }
87   size_ += new_size;
88 }
89 
Consume(size_t len)90 void SSLBuffer::Consume(size_t len) {
91   if (len > size_) {
92     abort();
93   }
94   offset_ += (uint16_t)len;
95   size_ -= (uint16_t)len;
96   cap_ -= (uint16_t)len;
97 }
98 
DiscardConsumed()99 void SSLBuffer::DiscardConsumed() {
100   if (size_ == 0) {
101     Clear();
102   }
103 }
104 
dtls_read_buffer_next_packet(SSL * ssl)105 static int dtls_read_buffer_next_packet(SSL *ssl) {
106   SSLBuffer *buf = &ssl->s3->read_buffer;
107 
108   if (!buf->empty()) {
109     // It is an error to call |dtls_read_buffer_extend| when the read buffer is
110     // not empty.
111     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
112     return -1;
113   }
114 
115   // Read a single packet from |ssl->rbio|. |buf->cap()| must fit in an int.
116   int ret = BIO_read(ssl->rbio, buf->data(), static_cast<int>(buf->cap()));
117   if (ret <= 0) {
118     ssl->s3->rwstate = SSL_READING;
119     return ret;
120   }
121   buf->DidWrite(static_cast<size_t>(ret));
122   return 1;
123 }
124 
tls_read_buffer_extend_to(SSL * ssl,size_t len)125 static int tls_read_buffer_extend_to(SSL *ssl, size_t len) {
126   SSLBuffer *buf = &ssl->s3->read_buffer;
127 
128   if (len > buf->cap()) {
129     OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
130     return -1;
131   }
132 
133   // Read until the target length is reached.
134   while (buf->size() < len) {
135     // The amount of data to read is bounded by |buf->cap|, which must fit in an
136     // int.
137     int ret = BIO_read(ssl->rbio, buf->data() + buf->size(),
138                        static_cast<int>(len - buf->size()));
139     if (ret <= 0) {
140       ssl->s3->rwstate = SSL_READING;
141       return ret;
142     }
143     buf->DidWrite(static_cast<size_t>(ret));
144   }
145 
146   return 1;
147 }
148 
ssl_read_buffer_extend_to(SSL * ssl,size_t len)149 int ssl_read_buffer_extend_to(SSL *ssl, size_t len) {
150   // |ssl_read_buffer_extend_to| implicitly discards any consumed data.
151   ssl->s3->read_buffer.DiscardConsumed();
152 
153   if (SSL_is_dtls(ssl)) {
154     static_assert(
155         DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH <= 0xffff,
156         "DTLS read buffer is too large");
157 
158     // The |len| parameter is ignored in DTLS.
159     len = DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
160   }
161 
162   if (!ssl->s3->read_buffer.EnsureCap(ssl_record_prefix_len(ssl), len)) {
163     return -1;
164   }
165 
166   if (ssl->rbio == NULL) {
167     OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
168     return -1;
169   }
170 
171   int ret;
172   if (SSL_is_dtls(ssl)) {
173     // |len| is ignored for a datagram transport.
174     ret = dtls_read_buffer_next_packet(ssl);
175   } else {
176     ret = tls_read_buffer_extend_to(ssl, len);
177   }
178 
179   if (ret <= 0) {
180     // If the buffer was empty originally and remained empty after attempting to
181     // extend it, release the buffer until the next attempt.
182     ssl->s3->read_buffer.DiscardConsumed();
183   }
184   return ret;
185 }
186 
ssl_handle_open_record(SSL * ssl,bool * out_retry,ssl_open_record_t ret,size_t consumed,uint8_t alert)187 int ssl_handle_open_record(SSL *ssl, bool *out_retry, ssl_open_record_t ret,
188                            size_t consumed, uint8_t alert) {
189   *out_retry = false;
190   if (ret != ssl_open_record_partial) {
191     ssl->s3->read_buffer.Consume(consumed);
192   }
193   if (ret != ssl_open_record_success) {
194     // Nothing was returned to the caller, so discard anything marked consumed.
195     ssl->s3->read_buffer.DiscardConsumed();
196   }
197   switch (ret) {
198     case ssl_open_record_success:
199       return 1;
200 
201     case ssl_open_record_partial: {
202       int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
203       if (read_ret <= 0) {
204         return read_ret;
205       }
206       *out_retry = true;
207       return 1;
208     }
209 
210     case ssl_open_record_discard:
211       *out_retry = true;
212       return 1;
213 
214     case ssl_open_record_close_notify:
215       return 0;
216 
217     case ssl_open_record_error:
218       if (alert != 0) {
219         ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
220       }
221       return -1;
222   }
223   assert(0);
224   return -1;
225 }
226 
227 
228 static_assert(SSL3_RT_HEADER_LENGTH * 2 +
229                       SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD * 2 +
230                       SSL3_RT_MAX_PLAIN_LENGTH <=
231                   0xffff,
232               "maximum TLS write buffer is too large");
233 
234 static_assert(DTLS1_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD +
235                       SSL3_RT_MAX_PLAIN_LENGTH <=
236                   0xffff,
237               "maximum DTLS write buffer is too large");
238 
tls_write_buffer_flush(SSL * ssl)239 static int tls_write_buffer_flush(SSL *ssl) {
240   SSLBuffer *buf = &ssl->s3->write_buffer;
241 
242   while (!buf->empty()) {
243     int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
244     if (ret <= 0) {
245       ssl->s3->rwstate = SSL_WRITING;
246       return ret;
247     }
248     buf->Consume(static_cast<size_t>(ret));
249   }
250   buf->Clear();
251   return 1;
252 }
253 
dtls_write_buffer_flush(SSL * ssl)254 static int dtls_write_buffer_flush(SSL *ssl) {
255   SSLBuffer *buf = &ssl->s3->write_buffer;
256   if (buf->empty()) {
257     return 1;
258   }
259 
260   int ret = BIO_write(ssl->wbio, buf->data(), buf->size());
261   if (ret <= 0) {
262     ssl->s3->rwstate = SSL_WRITING;
263     // If the write failed, drop the write buffer anyway. Datagram transports
264     // can't write half a packet, so the caller is expected to retry from the
265     // top.
266     buf->Clear();
267     return ret;
268   }
269   buf->Clear();
270   return 1;
271 }
272 
ssl_write_buffer_flush(SSL * ssl)273 int ssl_write_buffer_flush(SSL *ssl) {
274   if (ssl->wbio == NULL) {
275     OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET);
276     return -1;
277   }
278 
279   if (SSL_is_dtls(ssl)) {
280     return dtls_write_buffer_flush(ssl);
281   } else {
282     return tls_write_buffer_flush(ssl);
283   }
284 }
285 
286 }  // namespace bssl
287