2016-05-17 18:18:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2015-01-30 14:57:54 +00:00
|
|
|
*
|
2016-05-17 18:18:30 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-01-30 14:57:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../ssl_locl.h"
|
2015-02-04 15:52:05 +00:00
|
|
|
#include "record_locl.h"
|
2015-01-30 14:57:54 +00:00
|
|
|
|
2015-02-02 13:43:38 +00:00
|
|
|
void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, int n)
|
2015-01-30 14:57:54 +00:00
|
|
|
{
|
2015-04-16 05:50:03 +00:00
|
|
|
if (d != NULL)
|
2015-01-30 14:57:54 +00:00
|
|
|
memcpy(b->buf, d, n);
|
|
|
|
b->left = n;
|
|
|
|
b->offset = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-21 13:06:52 +00:00
|
|
|
/*
|
2016-01-13 14:20:25 +00:00
|
|
|
* Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also
|
|
|
|
* retains the default_len setting
|
2015-05-21 13:06:52 +00:00
|
|
|
*/
|
|
|
|
void SSL3_BUFFER_clear(SSL3_BUFFER *b)
|
|
|
|
{
|
2016-01-13 14:20:25 +00:00
|
|
|
b->offset = 0;
|
|
|
|
b->left = 0;
|
2015-05-21 13:06:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 14:57:54 +00:00
|
|
|
void SSL3_BUFFER_release(SSL3_BUFFER *b)
|
|
|
|
{
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(b->buf);
|
2015-01-30 14:57:54 +00:00
|
|
|
b->buf = NULL;
|
|
|
|
}
|
2015-02-01 16:03:18 +00:00
|
|
|
|
|
|
|
int ssl3_setup_read_buffer(SSL *s)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
|
|
|
size_t len, align = 0, headerlen;
|
|
|
|
SSL3_BUFFER *b;
|
2016-06-28 20:51:27 +00:00
|
|
|
|
2015-02-01 16:03:18 +00:00
|
|
|
b = RECORD_LAYER_get_rbuf(&s->rlayer);
|
|
|
|
|
2015-08-26 13:45:40 +00:00
|
|
|
if (SSL_IS_DTLS(s))
|
2015-02-01 16:03:18 +00:00
|
|
|
headerlen = DTLS1_RT_HEADER_LENGTH;
|
|
|
|
else
|
|
|
|
headerlen = SSL3_RT_HEADER_LENGTH;
|
|
|
|
|
|
|
|
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
|
|
|
align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (b->buf == NULL) {
|
|
|
|
len = SSL3_RT_MAX_PLAIN_LENGTH
|
|
|
|
+ SSL3_RT_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
|
|
|
|
#ifndef OPENSSL_NO_COMP
|
|
|
|
if (ssl_allow_compression(s))
|
|
|
|
len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
|
|
|
#endif
|
2016-01-13 14:20:25 +00:00
|
|
|
if (b->default_len > len)
|
|
|
|
len = b->default_len;
|
2015-02-01 16:03:18 +00:00
|
|
|
if ((p = OPENSSL_malloc(len)) == NULL)
|
|
|
|
goto err;
|
|
|
|
b->buf = p;
|
|
|
|
b->len = len;
|
|
|
|
}
|
|
|
|
|
2015-02-02 20:55:15 +00:00
|
|
|
RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0]));
|
2015-02-01 16:03:18 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
SSLerr(SSL_F_SSL3_SETUP_READ_BUFFER, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-25 09:36:57 +00:00
|
|
|
int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t len)
|
2015-02-01 16:03:18 +00:00
|
|
|
{
|
|
|
|
unsigned char *p;
|
2016-07-25 09:36:57 +00:00
|
|
|
size_t align = 0, headerlen;
|
2015-02-01 16:03:18 +00:00
|
|
|
SSL3_BUFFER *wb;
|
2015-09-22 10:12:50 +00:00
|
|
|
unsigned int currpipe;
|
|
|
|
|
|
|
|
s->rlayer.numwpipes = numwpipes;
|
2015-02-01 16:03:18 +00:00
|
|
|
|
2016-07-25 09:36:57 +00:00
|
|
|
if (len == 0) {
|
|
|
|
if (SSL_IS_DTLS(s))
|
|
|
|
headerlen = DTLS1_RT_HEADER_LENGTH + 1;
|
|
|
|
else
|
|
|
|
headerlen = SSL3_RT_HEADER_LENGTH;
|
2015-02-01 16:03:18 +00:00
|
|
|
|
|
|
|
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
2016-07-25 09:36:57 +00:00
|
|
|
align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
|
2015-02-01 16:03:18 +00:00
|
|
|
#endif
|
|
|
|
|
2016-07-25 09:36:57 +00:00
|
|
|
len = s->max_send_fragment
|
|
|
|
+ SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
|
2015-02-01 16:03:18 +00:00
|
|
|
#ifndef OPENSSL_NO_COMP
|
2016-07-25 09:36:57 +00:00
|
|
|
if (ssl_allow_compression(s))
|
|
|
|
len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
2015-02-01 16:03:18 +00:00
|
|
|
#endif
|
2016-07-25 09:36:57 +00:00
|
|
|
if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
|
|
|
|
len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
|
|
|
|
}
|
2015-02-01 16:03:18 +00:00
|
|
|
|
2015-09-22 10:12:50 +00:00
|
|
|
wb = RECORD_LAYER_get_wbuf(&s->rlayer);
|
|
|
|
for (currpipe = 0; currpipe < numwpipes; currpipe++) {
|
|
|
|
if (wb[currpipe].buf == NULL) {
|
|
|
|
if ((p = OPENSSL_malloc(len)) == NULL) {
|
|
|
|
s->rlayer.numwpipes = currpipe;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
wb[currpipe].buf = p;
|
|
|
|
wb[currpipe].len = len;
|
|
|
|
}
|
2015-02-01 16:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
SSLerr(SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl3_setup_buffers(SSL *s)
|
|
|
|
{
|
|
|
|
if (!ssl3_setup_read_buffer(s))
|
|
|
|
return 0;
|
2016-07-25 09:36:57 +00:00
|
|
|
if (!ssl3_setup_write_buffer(s, 1, 0))
|
2015-02-01 16:03:18 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl3_release_write_buffer(SSL *s)
|
|
|
|
{
|
|
|
|
SSL3_BUFFER *wb;
|
2015-09-22 10:12:50 +00:00
|
|
|
unsigned int pipes;
|
2015-02-01 16:03:18 +00:00
|
|
|
|
2015-09-22 10:12:50 +00:00
|
|
|
pipes = s->rlayer.numwpipes;
|
|
|
|
while (pipes > 0) {
|
|
|
|
wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
|
2015-02-01 16:03:18 +00:00
|
|
|
|
2015-09-22 10:12:50 +00:00
|
|
|
OPENSSL_free(wb->buf);
|
|
|
|
wb->buf = NULL;
|
|
|
|
pipes--;
|
|
|
|
}
|
|
|
|
s->rlayer.numwpipes = 0;
|
2015-02-01 16:03:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ssl3_release_read_buffer(SSL *s)
|
|
|
|
{
|
|
|
|
SSL3_BUFFER *b;
|
|
|
|
|
|
|
|
b = RECORD_LAYER_get_rbuf(&s->rlayer);
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(b->buf);
|
|
|
|
b->buf = NULL;
|
2015-02-01 16:03:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|