Openssl 1.1.0h
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
Record Layer Design
|
||||
===================
|
||||
|
||||
This file provides some guidance on the thinking behind the design of the
|
||||
record layer code to aid future maintenance.
|
||||
|
||||
The record layer is divided into a number of components. At the time of writing
|
||||
there are four: SSL3_RECORD, SSL3_BUFFER, DLTS1_BITMAP and RECORD_LAYER. Each
|
||||
of these components is defined by:
|
||||
1) A struct definition of the same name as the component
|
||||
2) A set of source files that define the functions for that component
|
||||
3) A set of accessor macros
|
||||
|
||||
All struct definitions are in record.h. The functions and macros are either
|
||||
defined in record.h or record_locl.h dependent on whether they are intended to
|
||||
be private to the record layer, or whether they form part of the API to the rest
|
||||
of libssl.
|
||||
|
||||
The source files map to components as follows:
|
||||
|
||||
dtls1_bitmap.c -> DTLS1_BITMAP component
|
||||
ssl3_buffer.c -> SSL3_BUFFER component
|
||||
ssl3_record.c -> SSL3_RECORD component
|
||||
rec_layer_s3.c, rec_layer_d1.c -> RECORD_LAYER component
|
||||
|
||||
The RECORD_LAYER component is a facade pattern, i.e. it provides a simplified
|
||||
interface to the record layer for the rest of libssl. The other 3 components are
|
||||
entirely private to the record layer and therefore should never be accessed
|
||||
directly by libssl.
|
||||
|
||||
Any component can directly access its own members - they are private to that
|
||||
component, e.g. ssl3_buffer.c can access members of the SSL3_BUFFER struct
|
||||
without using a macro. No component can directly access the members of another
|
||||
component, e.g. ssl3_buffer cannot reach inside the RECORD_LAYER component to
|
||||
directly access its members. Instead components use accessor macros, so if code
|
||||
in ssl3_buffer.c wants to access the members of the RECORD_LAYER it uses the
|
||||
RECORD_LAYER_* macros.
|
||||
|
||||
Conceptually it looks like this:
|
||||
|
||||
libssl
|
||||
|
|
||||
---------------------------|-----record.h--------------------------------------
|
||||
|
|
||||
_______V______________
|
||||
| |
|
||||
| RECORD_LAYER |
|
||||
| |
|
||||
| rec_layer_s3.c |
|
||||
| ^ |
|
||||
| _________|__________ |
|
||||
|| ||
|
||||
|| DTLS1_RECORD_LAYER ||
|
||||
|| ||
|
||||
|| rec_layer_d1.c ||
|
||||
||____________________||
|
||||
|______________________|
|
||||
record_locl.h ^ ^ ^
|
||||
_________________| | |_________________
|
||||
| | |
|
||||
_____V_________ ______V________ _______V________
|
||||
| | | | | |
|
||||
| SSL3_BUFFER | | SSL3_RECORD | | DTLS1_BITMAP |
|
||||
| |--->| | | |
|
||||
| ssl3_buffer.c | | ssl3_record.c | | dtls1_bitmap.c |
|
||||
|_______________| |_______________| |________________|
|
||||
|
||||
|
||||
The two RECORD_LAYER source files build on each other, i.e.
|
||||
the main one is rec_layer_s3.c which provides the core SSL/TLS layer. The second
|
||||
one is rec_layer_d1.c which builds off of the SSL/TLS code to provide DTLS
|
||||
specific capabilities. It uses some DTLS specific RECORD_LAYER component members
|
||||
which should only be accessed from rec_layer_d1.c. These are held in the
|
||||
DTLS1_RECORD_LAYER struct.
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "../ssl_locl.h"
|
||||
#include "record_locl.h"
|
||||
|
||||
/* mod 128 saturating subtract of two 64-bit values in big-endian order */
|
||||
static int satsub64be(const unsigned char *v1, const unsigned char *v2)
|
||||
{
|
||||
int64_t ret;
|
||||
uint64_t l1, l2;
|
||||
|
||||
n2l8(v1, l1);
|
||||
n2l8(v2, l2);
|
||||
|
||||
ret = l1 - l2;
|
||||
|
||||
/* We do not permit wrap-around */
|
||||
if (l1 > l2 && ret < 0)
|
||||
return 128;
|
||||
else if (l2 > l1 && ret > 0)
|
||||
return -128;
|
||||
|
||||
if (ret > 128)
|
||||
return 128;
|
||||
else if (ret < -128)
|
||||
return -128;
|
||||
else
|
||||
return (int)ret;
|
||||
}
|
||||
|
||||
int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
{
|
||||
int cmp;
|
||||
unsigned int shift;
|
||||
const unsigned char *seq = s->rlayer.read_sequence;
|
||||
|
||||
cmp = satsub64be(seq, bitmap->max_seq_num);
|
||||
if (cmp > 0) {
|
||||
SSL3_RECORD_set_seq_num(RECORD_LAYER_get_rrec(&s->rlayer), seq);
|
||||
return 1; /* this record in new */
|
||||
}
|
||||
shift = -cmp;
|
||||
if (shift >= sizeof(bitmap->map) * 8)
|
||||
return 0; /* stale, outside the window */
|
||||
else if (bitmap->map & (1UL << shift))
|
||||
return 0; /* record previously received */
|
||||
|
||||
SSL3_RECORD_set_seq_num(RECORD_LAYER_get_rrec(&s->rlayer), seq);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
{
|
||||
int cmp;
|
||||
unsigned int shift;
|
||||
const unsigned char *seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
|
||||
|
||||
cmp = satsub64be(seq, bitmap->max_seq_num);
|
||||
if (cmp > 0) {
|
||||
shift = cmp;
|
||||
if (shift < sizeof(bitmap->map) * 8)
|
||||
bitmap->map <<= shift, bitmap->map |= 1UL;
|
||||
else
|
||||
bitmap->map = 1UL;
|
||||
memcpy(bitmap->max_seq_num, seq, SEQ_NUM_SIZE);
|
||||
} else {
|
||||
shift = -cmp;
|
||||
if (shift < sizeof(bitmap->map) * 8)
|
||||
bitmap->map |= 1UL << shift;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,1229 @@
|
||||
/*
|
||||
* Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#define USE_SOCKETS
|
||||
#include "../ssl_locl.h"
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include "record_locl.h"
|
||||
|
||||
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
|
||||
{
|
||||
DTLS_RECORD_LAYER *d;
|
||||
|
||||
if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
|
||||
return (0);
|
||||
|
||||
rl->d = d;
|
||||
|
||||
d->unprocessed_rcds.q = pqueue_new();
|
||||
d->processed_rcds.q = pqueue_new();
|
||||
d->buffered_app_data.q = pqueue_new();
|
||||
|
||||
if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL
|
||||
|| d->buffered_app_data.q == NULL) {
|
||||
pqueue_free(d->unprocessed_rcds.q);
|
||||
pqueue_free(d->processed_rcds.q);
|
||||
pqueue_free(d->buffered_app_data.q);
|
||||
OPENSSL_free(d);
|
||||
rl->d = NULL;
|
||||
return (0);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
|
||||
{
|
||||
DTLS_RECORD_LAYER_clear(rl);
|
||||
pqueue_free(rl->d->unprocessed_rcds.q);
|
||||
pqueue_free(rl->d->processed_rcds.q);
|
||||
pqueue_free(rl->d->buffered_app_data.q);
|
||||
OPENSSL_free(rl->d);
|
||||
rl->d = NULL;
|
||||
}
|
||||
|
||||
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
|
||||
{
|
||||
DTLS_RECORD_LAYER *d;
|
||||
pitem *item = NULL;
|
||||
DTLS1_RECORD_DATA *rdata;
|
||||
pqueue *unprocessed_rcds;
|
||||
pqueue *processed_rcds;
|
||||
pqueue *buffered_app_data;
|
||||
|
||||
d = rl->d;
|
||||
|
||||
while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
|
||||
rdata = (DTLS1_RECORD_DATA *)item->data;
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(item->data);
|
||||
pitem_free(item);
|
||||
}
|
||||
|
||||
while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
|
||||
rdata = (DTLS1_RECORD_DATA *)item->data;
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(item->data);
|
||||
pitem_free(item);
|
||||
}
|
||||
|
||||
while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
|
||||
rdata = (DTLS1_RECORD_DATA *)item->data;
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(item->data);
|
||||
pitem_free(item);
|
||||
}
|
||||
|
||||
unprocessed_rcds = d->unprocessed_rcds.q;
|
||||
processed_rcds = d->processed_rcds.q;
|
||||
buffered_app_data = d->buffered_app_data.q;
|
||||
memset(d, 0, sizeof(*d));
|
||||
d->unprocessed_rcds.q = unprocessed_rcds;
|
||||
d->processed_rcds.q = processed_rcds;
|
||||
d->buffered_app_data.q = buffered_app_data;
|
||||
}
|
||||
|
||||
void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
|
||||
{
|
||||
if (e == rl->d->w_epoch - 1) {
|
||||
memcpy(rl->d->curr_write_sequence,
|
||||
rl->write_sequence, sizeof(rl->write_sequence));
|
||||
memcpy(rl->write_sequence,
|
||||
rl->d->last_write_sequence, sizeof(rl->write_sequence));
|
||||
} else if (e == rl->d->w_epoch + 1) {
|
||||
memcpy(rl->d->last_write_sequence,
|
||||
rl->write_sequence, sizeof(unsigned char[8]));
|
||||
memcpy(rl->write_sequence,
|
||||
rl->d->curr_write_sequence, sizeof(rl->write_sequence));
|
||||
}
|
||||
rl->d->w_epoch = e;
|
||||
}
|
||||
|
||||
void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl)
|
||||
{
|
||||
memcpy(rl->write_sequence, rl->read_sequence, sizeof(rl->write_sequence));
|
||||
}
|
||||
|
||||
void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
|
||||
{
|
||||
memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
|
||||
}
|
||||
|
||||
static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
|
||||
int len);
|
||||
|
||||
/* copy buffered record into SSL structure */
|
||||
static int dtls1_copy_record(SSL *s, pitem *item)
|
||||
{
|
||||
DTLS1_RECORD_DATA *rdata;
|
||||
|
||||
rdata = (DTLS1_RECORD_DATA *)item->data;
|
||||
|
||||
SSL3_BUFFER_release(&s->rlayer.rbuf);
|
||||
|
||||
s->rlayer.packet = rdata->packet;
|
||||
s->rlayer.packet_length = rdata->packet_length;
|
||||
memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
|
||||
memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
|
||||
|
||||
/* Set proper sequence number for mac calculation */
|
||||
memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
|
||||
{
|
||||
DTLS1_RECORD_DATA *rdata;
|
||||
pitem *item;
|
||||
|
||||
/* Limit the size of the queue to prevent DOS attacks */
|
||||
if (pqueue_size(queue->q) >= 100)
|
||||
return 0;
|
||||
|
||||
rdata = OPENSSL_malloc(sizeof(*rdata));
|
||||
item = pitem_new(priority, rdata);
|
||||
if (rdata == NULL || item == NULL) {
|
||||
OPENSSL_free(rdata);
|
||||
pitem_free(item);
|
||||
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rdata->packet = s->rlayer.packet;
|
||||
rdata->packet_length = s->rlayer.packet_length;
|
||||
memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
|
||||
memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
|
||||
|
||||
item->data = rdata;
|
||||
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
/* Store bio_dgram_sctp_rcvinfo struct */
|
||||
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
|
||||
(SSL_get_state(s) == TLS_ST_SR_FINISHED
|
||||
|| SSL_get_state(s) == TLS_ST_CR_FINISHED)) {
|
||||
BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
|
||||
sizeof(rdata->recordinfo), &rdata->recordinfo);
|
||||
}
|
||||
#endif
|
||||
|
||||
s->rlayer.packet = NULL;
|
||||
s->rlayer.packet_length = 0;
|
||||
memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
|
||||
memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
|
||||
|
||||
if (!ssl3_setup_buffers(s)) {
|
||||
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(rdata);
|
||||
pitem_free(item);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* insert should not fail, since duplicates are dropped */
|
||||
if (pqueue_insert(queue->q, item) == NULL) {
|
||||
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(rdata);
|
||||
pitem_free(item);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
|
||||
{
|
||||
pitem *item;
|
||||
|
||||
item = pqueue_pop(queue->q);
|
||||
if (item) {
|
||||
dtls1_copy_record(s, item);
|
||||
|
||||
OPENSSL_free(item->data);
|
||||
pitem_free(item);
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* retrieve a buffered record that belongs to the new epoch, i.e., not
|
||||
* processed yet
|
||||
*/
|
||||
#define dtls1_get_unprocessed_record(s) \
|
||||
dtls1_retrieve_buffered_record((s), \
|
||||
&((s)->rlayer.d->unprocessed_rcds))
|
||||
|
||||
int dtls1_process_buffered_records(SSL *s)
|
||||
{
|
||||
pitem *item;
|
||||
SSL3_BUFFER *rb;
|
||||
SSL3_RECORD *rr;
|
||||
DTLS1_BITMAP *bitmap;
|
||||
unsigned int is_next_epoch;
|
||||
int replayok = 1;
|
||||
|
||||
item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
|
||||
if (item) {
|
||||
/* Check if epoch is current. */
|
||||
if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
|
||||
return 1; /* Nothing to do. */
|
||||
|
||||
rr = RECORD_LAYER_get_rrec(&s->rlayer);
|
||||
|
||||
rb = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
|
||||
if (SSL3_BUFFER_get_left(rb) > 0) {
|
||||
/*
|
||||
* We've still got data from the current packet to read. There could
|
||||
* be a record from the new epoch in it - so don't overwrite it
|
||||
* with the unprocessed records yet (we'll do it when we've
|
||||
* finished reading the current packet).
|
||||
*/
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Process all the records. */
|
||||
while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
|
||||
dtls1_get_unprocessed_record(s);
|
||||
bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
|
||||
if (bitmap == NULL) {
|
||||
/*
|
||||
* Should not happen. This will only ever be NULL when the
|
||||
* current record is from a different epoch. But that cannot
|
||||
* be the case because we already checked the epoch above
|
||||
*/
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
/* Only do replay check if no SCTP bio */
|
||||
if (!BIO_dgram_is_sctp(SSL_get_rbio(s)))
|
||||
#endif
|
||||
{
|
||||
/*
|
||||
* Check whether this is a repeat, or aged record. We did this
|
||||
* check once already when we first received the record - but
|
||||
* we might have updated the window since then due to
|
||||
* records we subsequently processed.
|
||||
*/
|
||||
replayok = dtls1_record_replay_check(s, bitmap);
|
||||
}
|
||||
|
||||
if (!replayok || !dtls1_process_record(s, bitmap)) {
|
||||
/* dump this record */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
|
||||
SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* sync epoch numbers once all the unprocessed records have been
|
||||
* processed
|
||||
*/
|
||||
s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;
|
||||
s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-
|
||||
* Return up to 'len' payload bytes received in 'type' records.
|
||||
* 'type' is one of the following:
|
||||
*
|
||||
* - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
|
||||
* - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
|
||||
* - 0 (during a shutdown, no data has to be returned)
|
||||
*
|
||||
* If we don't have stored data to work from, read a SSL/TLS record first
|
||||
* (possibly multiple records if we still don't have anything to return).
|
||||
*
|
||||
* This function must handle any surprises the peer may have for us, such as
|
||||
* Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
|
||||
* messages are treated as if they were handshake messages *if* the |recd_type|
|
||||
* argument is non NULL.
|
||||
* Also if record payloads contain fragments too small to process, we store
|
||||
* them until there is enough for the respective protocol (the record protocol
|
||||
* may use arbitrary fragmentation and even interleaving):
|
||||
* Change cipher spec protocol
|
||||
* just 1 byte needed, no need for keeping anything stored
|
||||
* Alert protocol
|
||||
* 2 bytes needed (AlertLevel, AlertDescription)
|
||||
* Handshake protocol
|
||||
* 4 bytes needed (HandshakeType, uint24 length) -- we just have
|
||||
* to detect unexpected Client Hello and Hello Request messages
|
||||
* here, anything else is handled by higher layers
|
||||
* Application data protocol
|
||||
* none of our business
|
||||
*/
|
||||
int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
int len, int peek)
|
||||
{
|
||||
int al, i, j, ret;
|
||||
unsigned int n;
|
||||
SSL3_RECORD *rr;
|
||||
void (*cb) (const SSL *ssl, int type2, int val) = NULL;
|
||||
|
||||
if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
|
||||
/* Not initialized yet */
|
||||
if (!ssl3_setup_buffers(s))
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
|
||||
(type != SSL3_RT_HANDSHAKE)) ||
|
||||
(peek && (type != SSL3_RT_APPLICATION_DATA))) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* check whether there's a handshake message (client hello?) waiting
|
||||
*/
|
||||
if ((ret = have_handshake_fragment(s, type, buf, len))) {
|
||||
*recvd_type = SSL3_RT_HANDSHAKE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now s->rlayer.d->handshake_fragment_len == 0 if
|
||||
* type == SSL3_RT_HANDSHAKE.
|
||||
*/
|
||||
|
||||
if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))
|
||||
{
|
||||
/* type == SSL3_RT_APPLICATION_DATA */
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
start:
|
||||
s->rwstate = SSL_NOTHING;
|
||||
|
||||
/*-
|
||||
* s->s3->rrec.type - is the type of record
|
||||
* s->s3->rrec.data, - data
|
||||
* s->s3->rrec.off, - offset into 'data' for next read
|
||||
* s->s3->rrec.length, - number of bytes.
|
||||
*/
|
||||
rr = s->rlayer.rrec;
|
||||
|
||||
/*
|
||||
* We are not handshaking and have no data yet, so process data buffered
|
||||
* during the last handshake in advance, if any.
|
||||
*/
|
||||
if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {
|
||||
pitem *item;
|
||||
item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
|
||||
if (item) {
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
/* Restore bio_dgram_sctp_rcvinfo struct */
|
||||
if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
|
||||
DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
|
||||
BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
|
||||
sizeof(rdata->recordinfo), &rdata->recordinfo);
|
||||
}
|
||||
#endif
|
||||
|
||||
dtls1_copy_record(s, item);
|
||||
|
||||
OPENSSL_free(item->data);
|
||||
pitem_free(item);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for timeout */
|
||||
if (dtls1_handle_timeout(s) > 0)
|
||||
goto start;
|
||||
|
||||
/* get new packet if necessary */
|
||||
if ((SSL3_RECORD_get_length(rr) == 0)
|
||||
|| (s->rlayer.rstate == SSL_ST_READ_BODY)) {
|
||||
ret = dtls1_get_record(s);
|
||||
if (ret <= 0) {
|
||||
ret = dtls1_read_failed(s, ret);
|
||||
/* anything other than a timeout is an error */
|
||||
if (ret <= 0)
|
||||
return (ret);
|
||||
else
|
||||
goto start;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Reset the count of consecutive warning alerts if we've got a non-empty
|
||||
* record that isn't an alert.
|
||||
*/
|
||||
if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
|
||||
&& SSL3_RECORD_get_length(rr) != 0)
|
||||
s->rlayer.alert_count = 0;
|
||||
|
||||
/* we now have a packet which can be read and processed */
|
||||
|
||||
if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
|
||||
* reset by ssl3_get_finished */
|
||||
&& (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
|
||||
/*
|
||||
* We now have application data between CCS and Finished. Most likely
|
||||
* the packets were reordered on their way, so buffer the application
|
||||
* data for later processing rather than dropping the connection.
|
||||
*/
|
||||
if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
|
||||
SSL3_RECORD_get_seq_num(rr)) < 0) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the other end has shut down, throw anything we read away (even in
|
||||
* 'peek' mode)
|
||||
*/
|
||||
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (type == SSL3_RECORD_get_type(rr)
|
||||
|| (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
&& type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
|
||||
/*
|
||||
* SSL3_RT_APPLICATION_DATA or
|
||||
* SSL3_RT_HANDSHAKE or
|
||||
* SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
*/
|
||||
/*
|
||||
* make sure that we are not getting application data when we are
|
||||
* doing a handshake for the first time
|
||||
*/
|
||||
if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
|
||||
(s->enc_read_ctx == NULL)) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (recvd_type != NULL)
|
||||
*recvd_type = SSL3_RECORD_get_type(rr);
|
||||
|
||||
if (len <= 0)
|
||||
return (len);
|
||||
|
||||
if ((unsigned int)len > SSL3_RECORD_get_length(rr))
|
||||
n = SSL3_RECORD_get_length(rr);
|
||||
else
|
||||
n = (unsigned int)len;
|
||||
|
||||
memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
|
||||
if (!peek) {
|
||||
SSL3_RECORD_sub_length(rr, n);
|
||||
SSL3_RECORD_add_off(rr, n);
|
||||
if (SSL3_RECORD_get_length(rr) == 0) {
|
||||
s->rlayer.rstate = SSL_ST_READ_HEADER;
|
||||
SSL3_RECORD_set_off(rr, 0);
|
||||
}
|
||||
}
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
/*
|
||||
* We might had to delay a close_notify alert because of reordered
|
||||
* app data. If there was an alert and there is no message to read
|
||||
* anymore, finally set shutdown.
|
||||
*/
|
||||
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
|
||||
s->d1->shutdown_received
|
||||
&& !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
return (0);
|
||||
}
|
||||
#endif
|
||||
return (n);
|
||||
}
|
||||
|
||||
/*
|
||||
* If we get here, then type != rr->type; if we have a handshake message,
|
||||
* then it was unexpected (Hello Request or Client Hello).
|
||||
*/
|
||||
|
||||
/*
|
||||
* In case of record types for which we have 'fragment' storage, fill
|
||||
* that so that we can process the data at a fixed place.
|
||||
*/
|
||||
{
|
||||
unsigned int k, dest_maxlen = 0;
|
||||
unsigned char *dest = NULL;
|
||||
unsigned int *dest_len = NULL;
|
||||
|
||||
if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
|
||||
dest_maxlen = sizeof(s->rlayer.d->handshake_fragment);
|
||||
dest = s->rlayer.d->handshake_fragment;
|
||||
dest_len = &s->rlayer.d->handshake_fragment_len;
|
||||
} else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
|
||||
dest_maxlen = sizeof(s->rlayer.d->alert_fragment);
|
||||
dest = s->rlayer.d->alert_fragment;
|
||||
dest_len = &s->rlayer.d->alert_fragment_len;
|
||||
}
|
||||
#ifndef OPENSSL_NO_HEARTBEATS
|
||||
else if (SSL3_RECORD_get_type(rr) == DTLS1_RT_HEARTBEAT) {
|
||||
/* We allow a 0 return */
|
||||
if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),
|
||||
SSL3_RECORD_get_length(rr)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
/* Exit and notify application to read again */
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
s->rwstate = SSL_READING;
|
||||
BIO_clear_retry_flags(SSL_get_rbio(s));
|
||||
BIO_set_retry_read(SSL_get_rbio(s));
|
||||
return (-1);
|
||||
}
|
||||
#endif
|
||||
/* else it's a CCS message, or application data or wrong */
|
||||
else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {
|
||||
/*
|
||||
* Application data while renegotiating is allowed. Try again
|
||||
* reading.
|
||||
*/
|
||||
if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {
|
||||
BIO *bio;
|
||||
s->s3->in_read_app_data = 2;
|
||||
bio = SSL_get_rbio(s);
|
||||
s->rwstate = SSL_READING;
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/* Not certain if this is the right error handling */
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (dest_maxlen > 0) {
|
||||
/*
|
||||
* XDTLS: In a pathological case, the Client Hello may be
|
||||
* fragmented--don't always expect dest_maxlen bytes
|
||||
*/
|
||||
if (SSL3_RECORD_get_length(rr) < dest_maxlen) {
|
||||
#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
|
||||
/*
|
||||
* for normal alerts rr->length is 2, while
|
||||
* dest_maxlen is 7 if we were to handle this
|
||||
* non-existing alert...
|
||||
*/
|
||||
FIX ME;
|
||||
#endif
|
||||
s->rlayer.rstate = SSL_ST_READ_HEADER;
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
|
||||
/* now move 'n' bytes: */
|
||||
for (k = 0; k < dest_maxlen; k++) {
|
||||
dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
|
||||
SSL3_RECORD_add_off(rr, 1);
|
||||
SSL3_RECORD_add_length(rr, -1);
|
||||
}
|
||||
*dest_len = dest_maxlen;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* s->rlayer.d->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
|
||||
* s->rlayer.d->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
|
||||
* (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
|
||||
*/
|
||||
|
||||
/* If we are a client, check for an incoming 'Hello Request': */
|
||||
if ((!s->server) &&
|
||||
(s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
|
||||
(s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
|
||||
(s->session != NULL) && (s->session->cipher != NULL)) {
|
||||
s->rlayer.d->handshake_fragment_len = 0;
|
||||
|
||||
if ((s->rlayer.d->handshake_fragment[1] != 0) ||
|
||||
(s->rlayer.d->handshake_fragment[2] != 0) ||
|
||||
(s->rlayer.d->handshake_fragment[3] != 0)) {
|
||||
al = SSL_AD_DECODE_ERROR;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
/*
|
||||
* no need to check sequence number on HELLO REQUEST messages
|
||||
*/
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
|
||||
s->rlayer.d->handshake_fragment, 4, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
if (SSL_is_init_finished(s) &&
|
||||
(s->options & SSL_OP_NO_RENEGOTIATION) == 0 &&
|
||||
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
|
||||
!s->s3->renegotiate) {
|
||||
s->d1->handshake_read_seq++;
|
||||
s->new_session = 1;
|
||||
ssl3_renegotiate(s);
|
||||
if (ssl3_renegotiate_check(s)) {
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
||||
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
|
||||
/* no read-ahead left? */
|
||||
BIO *bio;
|
||||
/*
|
||||
* In the case where we try to read application data,
|
||||
* but we trigger an SSL handshake, we return -1 with
|
||||
* the retry option set. Otherwise renegotiation may
|
||||
* cause nasty problems in the blocking world
|
||||
*/
|
||||
s->rwstate = SSL_READING;
|
||||
bio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
|
||||
}
|
||||
/*
|
||||
* we either finished a handshake or ignored the request, now try
|
||||
* again to obtain the (application) data we were asked for
|
||||
*/
|
||||
goto start;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we are a server and get a client hello when renegotiation isn't
|
||||
* allowed send back a no renegotiation alert and carry on.
|
||||
*/
|
||||
if (s->server
|
||||
&& SSL_is_init_finished(s)
|
||||
&& s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH
|
||||
&& s->rlayer.d->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO
|
||||
&& s->s3->previous_client_finished_len != 0
|
||||
&& ((!s->s3->send_connection_binding
|
||||
&& (s->options
|
||||
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)
|
||||
|| (s->options & SSL_OP_NO_RENEGOTIATION) != 0)) {
|
||||
s->rlayer.d->handshake_fragment_len = 0;
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
|
||||
goto start;
|
||||
}
|
||||
|
||||
if (s->rlayer.d->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {
|
||||
int alert_level = s->rlayer.d->alert_fragment[0];
|
||||
int alert_descr = s->rlayer.d->alert_fragment[1];
|
||||
|
||||
s->rlayer.d->alert_fragment_len = 0;
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, s->version, SSL3_RT_ALERT,
|
||||
s->rlayer.d->alert_fragment, 2, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
if (s->info_callback != NULL)
|
||||
cb = s->info_callback;
|
||||
else if (s->ctx->info_callback != NULL)
|
||||
cb = s->ctx->info_callback;
|
||||
|
||||
if (cb != NULL) {
|
||||
j = (alert_level << 8) | alert_descr;
|
||||
cb(s, SSL_CB_READ_ALERT, j);
|
||||
}
|
||||
|
||||
if (alert_level == SSL3_AL_WARNING) {
|
||||
s->s3->warn_alert = alert_descr;
|
||||
|
||||
s->rlayer.alert_count++;
|
||||
if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
/*
|
||||
* With SCTP and streams the socket may deliver app data
|
||||
* after a close_notify alert. We have to check this first so
|
||||
* that nothing gets discarded.
|
||||
*/
|
||||
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
|
||||
BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
|
||||
s->d1->shutdown_received = 1;
|
||||
s->rwstate = SSL_READING;
|
||||
BIO_clear_retry_flags(SSL_get_rbio(s));
|
||||
BIO_set_retry_read(SSL_get_rbio(s));
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
return (0);
|
||||
}
|
||||
#if 0
|
||||
/* XXX: this is a possible improvement in the future */
|
||||
/* now check if it's a missing record */
|
||||
if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
|
||||
unsigned short seq;
|
||||
unsigned int frag_off;
|
||||
unsigned char *p = &(s->rlayer.d->alert_fragment[2]);
|
||||
|
||||
n2s(p, seq);
|
||||
n2l3(p, frag_off);
|
||||
|
||||
dtls1_retransmit_message(s,
|
||||
dtls1_get_queue_priority
|
||||
(frag->msg_header.seq, 0), frag_off,
|
||||
&found);
|
||||
if (!found && SSL_in_init(s)) {
|
||||
/*
|
||||
* fprintf( stderr,"in init = %d\n", SSL_in_init(s));
|
||||
*/
|
||||
/*
|
||||
* requested a message not yet sent, send an alert
|
||||
* ourselves
|
||||
*/
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING,
|
||||
DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else if (alert_level == SSL3_AL_FATAL) {
|
||||
char tmp[16];
|
||||
|
||||
s->rwstate = SSL_NOTHING;
|
||||
s->s3->fatal_alert = alert_descr;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
|
||||
BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
|
||||
ERR_add_error_data(2, "SSL alert number ", tmp);
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
SSL_CTX_remove_session(s->session_ctx, s->session);
|
||||
return (0);
|
||||
} else {
|
||||
al = SSL_AD_ILLEGAL_PARAMETER;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
goto start;
|
||||
}
|
||||
|
||||
if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
|
||||
* shutdown */
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
|
||||
/*
|
||||
* We can't process a CCS now, because previous handshake messages
|
||||
* are still missing, so just drop it.
|
||||
*/
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unexpected handshake message (Client Hello, or protocol violation)
|
||||
*/
|
||||
if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
|
||||
!ossl_statem_get_in_handshake(s)) {
|
||||
struct hm_header_st msg_hdr;
|
||||
|
||||
/* this may just be a stale retransmit */
|
||||
dtls1_get_message_header(rr->data, &msg_hdr);
|
||||
if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we are server, we may have a repeated FINISHED of the client
|
||||
* here, then retransmit our CCS and FINISHED.
|
||||
*/
|
||||
if (msg_hdr.type == SSL3_MT_FINISHED) {
|
||||
if (dtls1_check_timeout_num(s) < 0)
|
||||
return -1;
|
||||
|
||||
dtls1_retransmit_buffered_messages(s);
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
|
||||
if (SSL_is_init_finished(s) &&
|
||||
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
|
||||
ossl_statem_set_in_init(s, 1);
|
||||
s->renegotiate = 1;
|
||||
s->new_session = 1;
|
||||
}
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
||||
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
|
||||
/* no read-ahead left? */
|
||||
BIO *bio;
|
||||
/*
|
||||
* In the case where we try to read application data, but we
|
||||
* trigger an SSL handshake, we return -1 with the retry
|
||||
* option set. Otherwise renegotiation may cause nasty
|
||||
* problems in the blocking world
|
||||
*/
|
||||
s->rwstate = SSL_READING;
|
||||
bio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
goto start;
|
||||
}
|
||||
|
||||
switch (SSL3_RECORD_get_type(rr)) {
|
||||
default:
|
||||
/* TLS just ignores unknown message types */
|
||||
if (s->version == TLS1_VERSION) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
case SSL3_RT_CHANGE_CIPHER_SPEC:
|
||||
case SSL3_RT_ALERT:
|
||||
case SSL3_RT_HANDSHAKE:
|
||||
/*
|
||||
* we already handled all of these, with the possible exception of
|
||||
* SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
|
||||
* that should not happen when type != rr->type
|
||||
*/
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
goto f_err;
|
||||
case SSL3_RT_APPLICATION_DATA:
|
||||
/*
|
||||
* At this point, we were expecting handshake data, but have
|
||||
* application data. If the library was running inside ssl3_read()
|
||||
* (i.e. in_read_app_data is set) and it makes sense to read
|
||||
* application data at this point (session renegotiation not yet
|
||||
* started), we will indulge it.
|
||||
*/
|
||||
if (s->s3->in_read_app_data &&
|
||||
(s->s3->total_renegotiations != 0) &&
|
||||
ossl_statem_app_data_allowed(s)) {
|
||||
s->s3->in_read_app_data = 2;
|
||||
return (-1);
|
||||
} else {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
/* not reached */
|
||||
|
||||
f_err:
|
||||
ssl3_send_alert(s, SSL3_AL_FATAL, al);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
/*
|
||||
* this only happens when a client hello is received and a handshake
|
||||
* is started.
|
||||
*/
|
||||
static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
|
||||
int len)
|
||||
{
|
||||
|
||||
if ((type == SSL3_RT_HANDSHAKE)
|
||||
&& (s->rlayer.d->handshake_fragment_len > 0))
|
||||
/* (partially) satisfy request from storage */
|
||||
{
|
||||
unsigned char *src = s->rlayer.d->handshake_fragment;
|
||||
unsigned char *dst = buf;
|
||||
unsigned int k, n;
|
||||
|
||||
/* peek == 0 */
|
||||
n = 0;
|
||||
while ((len > 0) && (s->rlayer.d->handshake_fragment_len > 0)) {
|
||||
*dst++ = *src++;
|
||||
len--;
|
||||
s->rlayer.d->handshake_fragment_len--;
|
||||
n++;
|
||||
}
|
||||
/* move any remaining fragment bytes: */
|
||||
for (k = 0; k < s->rlayer.d->handshake_fragment_len; k++)
|
||||
s->rlayer.d->handshake_fragment[k] = *src++;
|
||||
return n;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this to write data in records of type 'type' It will return <= 0 if
|
||||
* not all data has been sent or non-blocking IO.
|
||||
*/
|
||||
int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
|
||||
s->rwstate = SSL_NOTHING;
|
||||
i = do_dtls1_write(s, type, buf, len, 0);
|
||||
return i;
|
||||
}
|
||||
|
||||
int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int len, int create_empty_fragment)
|
||||
{
|
||||
unsigned char *p, *pseq;
|
||||
int i, mac_size, clear = 0;
|
||||
int prefix_len = 0;
|
||||
int eivlen;
|
||||
SSL3_RECORD wr;
|
||||
SSL3_BUFFER *wb;
|
||||
SSL_SESSION *sess;
|
||||
|
||||
wb = &s->rlayer.wbuf[0];
|
||||
|
||||
/*
|
||||
* first check if there is a SSL3_BUFFER still being written out. This
|
||||
* will happen with non blocking IO
|
||||
*/
|
||||
if (SSL3_BUFFER_get_left(wb) != 0) {
|
||||
OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
|
||||
return (ssl3_write_pending(s, type, buf, len));
|
||||
}
|
||||
|
||||
/* If we have an alert to send, lets send it */
|
||||
if (s->s3->alert_dispatch) {
|
||||
i = s->method->ssl_dispatch_alert(s);
|
||||
if (i <= 0)
|
||||
return (i);
|
||||
/* if it went, fall through and send more stuff */
|
||||
}
|
||||
|
||||
if (len == 0 && !create_empty_fragment)
|
||||
return 0;
|
||||
|
||||
if (len > s->max_send_fragment) {
|
||||
SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
sess = s->session;
|
||||
|
||||
if ((sess == NULL) ||
|
||||
(s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
|
||||
clear = 1;
|
||||
|
||||
if (clear)
|
||||
mac_size = 0;
|
||||
else {
|
||||
mac_size = EVP_MD_CTX_size(s->write_hash);
|
||||
if (mac_size < 0)
|
||||
goto err;
|
||||
}
|
||||
|
||||
p = SSL3_BUFFER_get_buf(wb) + prefix_len;
|
||||
|
||||
/* write the header */
|
||||
|
||||
*(p++) = type & 0xff;
|
||||
SSL3_RECORD_set_type(&wr, type);
|
||||
/*
|
||||
* Special case: for hello verify request, client version 1.0 and we
|
||||
* haven't decided which version to use yet send back using version 1.0
|
||||
* header: otherwise some clients will ignore it.
|
||||
*/
|
||||
if (s->method->version == DTLS_ANY_VERSION &&
|
||||
s->max_proto_version != DTLS1_BAD_VER) {
|
||||
*(p++) = DTLS1_VERSION >> 8;
|
||||
*(p++) = DTLS1_VERSION & 0xff;
|
||||
} else {
|
||||
*(p++) = s->version >> 8;
|
||||
*(p++) = s->version & 0xff;
|
||||
}
|
||||
|
||||
/* field where we are to write out packet epoch, seq num and len */
|
||||
pseq = p;
|
||||
p += 10;
|
||||
|
||||
/* Explicit IV length, block ciphers appropriate version flag */
|
||||
if (s->enc_write_ctx) {
|
||||
int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
|
||||
if (mode == EVP_CIPH_CBC_MODE) {
|
||||
eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
|
||||
if (eivlen <= 1)
|
||||
eivlen = 0;
|
||||
}
|
||||
/* Need explicit part of IV for GCM mode */
|
||||
else if (mode == EVP_CIPH_GCM_MODE)
|
||||
eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
else if (mode == EVP_CIPH_CCM_MODE)
|
||||
eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
else
|
||||
eivlen = 0;
|
||||
} else
|
||||
eivlen = 0;
|
||||
|
||||
/* lets setup the record stuff. */
|
||||
SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
|
||||
SSL3_RECORD_set_length(&wr, (int)len);
|
||||
SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
|
||||
|
||||
/*
|
||||
* we now 'read' from wr.input, wr.length bytes into wr.data
|
||||
*/
|
||||
|
||||
/* first we compress */
|
||||
if (s->compress != NULL) {
|
||||
if (!ssl3_do_compress(s, &wr)) {
|
||||
SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
|
||||
SSL3_RECORD_get_length(&wr));
|
||||
SSL3_RECORD_reset_input(&wr);
|
||||
}
|
||||
|
||||
/*
|
||||
* we should still have the output to wr.data and the input from
|
||||
* wr.input. Length should be wr.length. wr.data still points in the
|
||||
* wb->buf
|
||||
*/
|
||||
|
||||
if (mac_size != 0) {
|
||||
if (s->method->ssl3_enc->mac(s, &wr,
|
||||
&(p[SSL3_RECORD_get_length(&wr) + eivlen]),
|
||||
1) < 0)
|
||||
goto err;
|
||||
SSL3_RECORD_add_length(&wr, mac_size);
|
||||
}
|
||||
|
||||
/* this is true regardless of mac size */
|
||||
SSL3_RECORD_set_data(&wr, p);
|
||||
SSL3_RECORD_reset_input(&wr);
|
||||
|
||||
if (eivlen)
|
||||
SSL3_RECORD_add_length(&wr, eivlen);
|
||||
|
||||
if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)
|
||||
goto err;
|
||||
|
||||
/* record length after mac and block padding */
|
||||
/*
|
||||
* if (type == SSL3_RT_APPLICATION_DATA || (type == SSL3_RT_ALERT && !
|
||||
* SSL_in_init(s)))
|
||||
*/
|
||||
|
||||
/* there's only one epoch between handshake and app data */
|
||||
|
||||
s2n(s->rlayer.d->w_epoch, pseq);
|
||||
|
||||
/* XDTLS: ?? */
|
||||
/*
|
||||
* else s2n(s->d1->handshake_epoch, pseq);
|
||||
*/
|
||||
|
||||
memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
|
||||
pseq += 6;
|
||||
s2n(SSL3_RECORD_get_length(&wr), pseq);
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
|
||||
DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
|
||||
|
||||
/*
|
||||
* we should now have wr.data pointing to the encrypted data, which is
|
||||
* wr->length long
|
||||
*/
|
||||
SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
|
||||
SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
|
||||
|
||||
ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
|
||||
|
||||
if (create_empty_fragment) {
|
||||
/*
|
||||
* we are in a recursive call; just return the length, don't write
|
||||
* out anything here
|
||||
*/
|
||||
return wr.length;
|
||||
}
|
||||
|
||||
/* now let's set up wb */
|
||||
SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
|
||||
SSL3_BUFFER_set_offset(wb, 0);
|
||||
|
||||
/*
|
||||
* memorize arguments so that ssl3_write_pending can detect bad write
|
||||
* retries later
|
||||
*/
|
||||
s->rlayer.wpend_tot = len;
|
||||
s->rlayer.wpend_buf = buf;
|
||||
s->rlayer.wpend_type = type;
|
||||
s->rlayer.wpend_ret = len;
|
||||
|
||||
/* we now just need to write the buffer */
|
||||
return ssl3_write_pending(s, type, buf, len);
|
||||
err:
|
||||
return -1;
|
||||
}
|
||||
|
||||
DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
|
||||
unsigned int *is_next_epoch)
|
||||
{
|
||||
|
||||
*is_next_epoch = 0;
|
||||
|
||||
/* In current epoch, accept HM, CCS, DATA, & ALERT */
|
||||
if (rr->epoch == s->rlayer.d->r_epoch)
|
||||
return &s->rlayer.d->bitmap;
|
||||
|
||||
/*
|
||||
* Only HM and ALERT messages can be from the next epoch and only if we
|
||||
* have already processed all of the unprocessed records from the last
|
||||
* epoch
|
||||
*/
|
||||
else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
|
||||
s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch &&
|
||||
(rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
|
||||
*is_next_epoch = 1;
|
||||
return &s->rlayer.d->next_bitmap;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void dtls1_reset_seq_numbers(SSL *s, int rw)
|
||||
{
|
||||
unsigned char *seq;
|
||||
unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
|
||||
|
||||
if (rw & SSL3_CC_READ) {
|
||||
seq = s->rlayer.read_sequence;
|
||||
s->rlayer.d->r_epoch++;
|
||||
memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
|
||||
sizeof(s->rlayer.d->bitmap));
|
||||
memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap));
|
||||
|
||||
/*
|
||||
* We must not use any buffered messages received from the previous
|
||||
* epoch
|
||||
*/
|
||||
dtls1_clear_received_buffer(s);
|
||||
} else {
|
||||
seq = s->rlayer.write_sequence;
|
||||
memcpy(s->rlayer.d->last_write_sequence, seq,
|
||||
sizeof(s->rlayer.write_sequence));
|
||||
s->rlayer.d->w_epoch++;
|
||||
}
|
||||
|
||||
memset(seq, 0, seq_bytes);
|
||||
}
|
||||
@@ -0,0 +1,1549 @@
|
||||
/*
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
#define USE_SOCKETS
|
||||
#include "../ssl_locl.h"
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include <openssl/rand.h>
|
||||
#include "record_locl.h"
|
||||
|
||||
#if defined(OPENSSL_SMALL_FOOTPRINT) || \
|
||||
!( defined(AES_ASM) && ( \
|
||||
defined(__x86_64) || defined(__x86_64__) || \
|
||||
defined(_M_AMD64) || defined(_M_X64) ) \
|
||||
)
|
||||
# undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
||||
# define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
|
||||
#endif
|
||||
|
||||
void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s)
|
||||
{
|
||||
rl->s = s;
|
||||
RECORD_LAYER_set_first_record(&s->rlayer);
|
||||
SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);
|
||||
}
|
||||
|
||||
void RECORD_LAYER_clear(RECORD_LAYER *rl)
|
||||
{
|
||||
rl->rstate = SSL_ST_READ_HEADER;
|
||||
|
||||
/*
|
||||
* Do I need to clear read_ahead? As far as I can tell read_ahead did not
|
||||
* previously get reset by SSL_clear...so I'll keep it that way..but is
|
||||
* that right?
|
||||
*/
|
||||
|
||||
rl->packet = NULL;
|
||||
rl->packet_length = 0;
|
||||
rl->wnum = 0;
|
||||
memset(rl->alert_fragment, 0, sizeof(rl->alert_fragment));
|
||||
rl->alert_fragment_len = 0;
|
||||
memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
|
||||
rl->handshake_fragment_len = 0;
|
||||
rl->wpend_tot = 0;
|
||||
rl->wpend_type = 0;
|
||||
rl->wpend_ret = 0;
|
||||
rl->wpend_buf = NULL;
|
||||
|
||||
SSL3_BUFFER_clear(&rl->rbuf);
|
||||
ssl3_release_write_buffer(rl->s);
|
||||
rl->numrpipes = 0;
|
||||
SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);
|
||||
|
||||
RECORD_LAYER_reset_read_sequence(rl);
|
||||
RECORD_LAYER_reset_write_sequence(rl);
|
||||
|
||||
if (rl->d)
|
||||
DTLS_RECORD_LAYER_clear(rl);
|
||||
}
|
||||
|
||||
void RECORD_LAYER_release(RECORD_LAYER *rl)
|
||||
{
|
||||
if (SSL3_BUFFER_is_initialised(&rl->rbuf))
|
||||
ssl3_release_read_buffer(rl->s);
|
||||
if (rl->numwpipes > 0)
|
||||
ssl3_release_write_buffer(rl->s);
|
||||
SSL3_RECORD_release(rl->rrec, SSL_MAX_PIPELINES);
|
||||
}
|
||||
|
||||
/* Checks if we have unprocessed read ahead data pending */
|
||||
int RECORD_LAYER_read_pending(const RECORD_LAYER *rl)
|
||||
{
|
||||
return SSL3_BUFFER_get_left(&rl->rbuf) != 0;
|
||||
}
|
||||
|
||||
/* Checks if we have decrypted unread record data pending */
|
||||
int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl)
|
||||
{
|
||||
size_t curr_rec = 0, num_recs = RECORD_LAYER_get_numrpipes(rl);
|
||||
const SSL3_RECORD *rr = rl->rrec;
|
||||
|
||||
while (curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]))
|
||||
curr_rec++;
|
||||
|
||||
return curr_rec < num_recs;
|
||||
}
|
||||
|
||||
int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
|
||||
{
|
||||
return (rl->numwpipes > 0)
|
||||
&& SSL3_BUFFER_get_left(&rl->wbuf[rl->numwpipes - 1]) != 0;
|
||||
}
|
||||
|
||||
int RECORD_LAYER_set_data(RECORD_LAYER *rl, const unsigned char *buf, int len)
|
||||
{
|
||||
rl->packet_length = len;
|
||||
if (len != 0) {
|
||||
rl->rstate = SSL_ST_READ_HEADER;
|
||||
if (!SSL3_BUFFER_is_initialised(&rl->rbuf))
|
||||
if (!ssl3_setup_read_buffer(rl->s))
|
||||
return 0;
|
||||
}
|
||||
|
||||
rl->packet = SSL3_BUFFER_get_buf(&rl->rbuf);
|
||||
SSL3_BUFFER_set_data(&rl->rbuf, buf, len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl)
|
||||
{
|
||||
memset(rl->read_sequence, 0, sizeof(rl->read_sequence));
|
||||
}
|
||||
|
||||
void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl)
|
||||
{
|
||||
memset(rl->write_sequence, 0, sizeof(rl->write_sequence));
|
||||
}
|
||||
|
||||
int ssl3_pending(const SSL *s)
|
||||
{
|
||||
unsigned int i;
|
||||
int num = 0;
|
||||
|
||||
if (s->rlayer.rstate == SSL_ST_READ_BODY)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < RECORD_LAYER_get_numrpipes(&s->rlayer); i++) {
|
||||
if (SSL3_RECORD_get_type(&s->rlayer.rrec[i])
|
||||
!= SSL3_RT_APPLICATION_DATA)
|
||||
return 0;
|
||||
num += SSL3_RECORD_get_length(&s->rlayer.rrec[i]);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
|
||||
{
|
||||
ctx->default_read_buf_len = len;
|
||||
}
|
||||
|
||||
void SSL_set_default_read_buffer_len(SSL *s, size_t len)
|
||||
{
|
||||
SSL3_BUFFER_set_default_len(RECORD_LAYER_get_rbuf(&s->rlayer), len);
|
||||
}
|
||||
|
||||
const char *SSL_rstate_string_long(const SSL *s)
|
||||
{
|
||||
switch (s->rlayer.rstate) {
|
||||
case SSL_ST_READ_HEADER:
|
||||
return "read header";
|
||||
case SSL_ST_READ_BODY:
|
||||
return "read body";
|
||||
case SSL_ST_READ_DONE:
|
||||
return "read done";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
const char *SSL_rstate_string(const SSL *s)
|
||||
{
|
||||
switch (s->rlayer.rstate) {
|
||||
case SSL_ST_READ_HEADER:
|
||||
return "RH";
|
||||
case SSL_ST_READ_BODY:
|
||||
return "RB";
|
||||
case SSL_ST_READ_DONE:
|
||||
return "RD";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Return values are as per SSL_read()
|
||||
*/
|
||||
int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
{
|
||||
/*
|
||||
* If extend == 0, obtain new n-byte packet; if extend == 1, increase
|
||||
* packet by another n bytes. The packet will be in the sub-array of
|
||||
* s->s3->rbuf.buf specified by s->packet and s->packet_length. (If
|
||||
* s->rlayer.read_ahead is set, 'max' bytes may be stored in rbuf [plus
|
||||
* s->packet_length bytes if extend == 1].)
|
||||
* if clearold == 1, move the packet to the start of the buffer; if
|
||||
* clearold == 0 then leave any old packets where they were
|
||||
*/
|
||||
int i, len, left;
|
||||
size_t align = 0;
|
||||
unsigned char *pkt;
|
||||
SSL3_BUFFER *rb;
|
||||
|
||||
if (n <= 0)
|
||||
return n;
|
||||
|
||||
rb = &s->rlayer.rbuf;
|
||||
if (rb->buf == NULL)
|
||||
if (!ssl3_setup_read_buffer(s))
|
||||
return -1;
|
||||
|
||||
left = rb->left;
|
||||
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
||||
align = (size_t)rb->buf + SSL3_RT_HEADER_LENGTH;
|
||||
align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
|
||||
#endif
|
||||
|
||||
if (!extend) {
|
||||
/* start with empty packet ... */
|
||||
if (left == 0)
|
||||
rb->offset = align;
|
||||
else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
|
||||
/*
|
||||
* check if next packet length is large enough to justify payload
|
||||
* alignment...
|
||||
*/
|
||||
pkt = rb->buf + rb->offset;
|
||||
if (pkt[0] == SSL3_RT_APPLICATION_DATA
|
||||
&& (pkt[3] << 8 | pkt[4]) >= 128) {
|
||||
/*
|
||||
* Note that even if packet is corrupted and its length field
|
||||
* is insane, we can only be led to wrong decision about
|
||||
* whether memmove will occur or not. Header values has no
|
||||
* effect on memmove arguments and therefore no buffer
|
||||
* overrun can be triggered.
|
||||
*/
|
||||
memmove(rb->buf + align, pkt, left);
|
||||
rb->offset = align;
|
||||
}
|
||||
}
|
||||
s->rlayer.packet = rb->buf + rb->offset;
|
||||
s->rlayer.packet_length = 0;
|
||||
/* ... now we can act as if 'extend' was set */
|
||||
}
|
||||
|
||||
len = s->rlayer.packet_length;
|
||||
pkt = rb->buf + align;
|
||||
/*
|
||||
* Move any available bytes to front of buffer: 'len' bytes already
|
||||
* pointed to by 'packet', 'left' extra ones at the end
|
||||
*/
|
||||
if (s->rlayer.packet != pkt && clearold == 1) {
|
||||
memmove(pkt, s->rlayer.packet, len + left);
|
||||
s->rlayer.packet = pkt;
|
||||
rb->offset = len + align;
|
||||
}
|
||||
|
||||
/*
|
||||
* For DTLS/UDP reads should not span multiple packets because the read
|
||||
* operation returns the whole packet at once (as long as it fits into
|
||||
* the buffer).
|
||||
*/
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
if (left == 0 && extend)
|
||||
return 0;
|
||||
if (left > 0 && n > left)
|
||||
n = left;
|
||||
}
|
||||
|
||||
/* if there is enough in the buffer from a previous read, take some */
|
||||
if (left >= n) {
|
||||
s->rlayer.packet_length += n;
|
||||
rb->left = left - n;
|
||||
rb->offset += n;
|
||||
return (n);
|
||||
}
|
||||
|
||||
/* else we need to read more data */
|
||||
|
||||
if (n > (int)(rb->len - rb->offset)) { /* does not happen */
|
||||
SSLerr(SSL_F_SSL3_READ_N, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* We always act like read_ahead is set for DTLS */
|
||||
if (!s->rlayer.read_ahead && !SSL_IS_DTLS(s))
|
||||
/* ignore max parameter */
|
||||
max = n;
|
||||
else {
|
||||
if (max < n)
|
||||
max = n;
|
||||
if (max > (int)(rb->len - rb->offset))
|
||||
max = rb->len - rb->offset;
|
||||
}
|
||||
|
||||
while (left < n) {
|
||||
/*
|
||||
* Now we have len+left bytes at the front of s->s3->rbuf.buf and
|
||||
* need to read in more until we have len+n (up to len+max if
|
||||
* possible)
|
||||
*/
|
||||
|
||||
clear_sys_error();
|
||||
if (s->rbio != NULL) {
|
||||
s->rwstate = SSL_READING;
|
||||
i = BIO_read(s->rbio, pkt + len + left, max - left);
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
|
||||
i = -1;
|
||||
}
|
||||
|
||||
if (i <= 0) {
|
||||
rb->left = left;
|
||||
if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
|
||||
if (len + left == 0)
|
||||
ssl3_release_read_buffer(s);
|
||||
return i;
|
||||
}
|
||||
left += i;
|
||||
/*
|
||||
* reads should *never* span multiple packets for DTLS because the
|
||||
* underlying transport protocol is message oriented as opposed to
|
||||
* byte oriented as in the TLS case.
|
||||
*/
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
if (n > left)
|
||||
n = left; /* makes the while condition false */
|
||||
}
|
||||
}
|
||||
|
||||
/* done reading, now the book-keeping */
|
||||
rb->offset += n;
|
||||
rb->left = left - n;
|
||||
s->rlayer.packet_length += n;
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this to write data in records of type 'type' It will return <= 0 if
|
||||
* not all data has been sent or non-blocking IO.
|
||||
*/
|
||||
int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
{
|
||||
const unsigned char *buf = buf_;
|
||||
int tot;
|
||||
unsigned int n, split_send_fragment, maxpipes;
|
||||
#if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
||||
unsigned int max_send_fragment, nw;
|
||||
unsigned int u_len = (unsigned int)len;
|
||||
#endif
|
||||
SSL3_BUFFER *wb = &s->rlayer.wbuf[0];
|
||||
int i;
|
||||
|
||||
if (len < 0) {
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_NEGATIVE_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
s->rwstate = SSL_NOTHING;
|
||||
tot = s->rlayer.wnum;
|
||||
/*
|
||||
* ensure that if we end up with a smaller value of data to write out
|
||||
* than the the original len from a write which didn't complete for
|
||||
* non-blocking I/O and also somehow ended up avoiding the check for
|
||||
* this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
|
||||
* possible to end up with (len-tot) as a large number that will then
|
||||
* promptly send beyond the end of the users buffer ... so we trap and
|
||||
* report the error in a way the user will notice
|
||||
*/
|
||||
if (((unsigned int)len < s->rlayer.wnum)
|
||||
|| ((wb->left != 0) && ((unsigned int)len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
s->rlayer.wnum = 0;
|
||||
|
||||
if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) {
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* first check if there is a SSL3_BUFFER still being written out. This
|
||||
* will happen with non blocking IO
|
||||
*/
|
||||
if (wb->left != 0) {
|
||||
i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot);
|
||||
if (i <= 0) {
|
||||
/* XXX should we ssl3_release_write_buffer if i<0? */
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
tot += i; /* this might be last fragment */
|
||||
}
|
||||
#if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
||||
/*
|
||||
* Depending on platform multi-block can deliver several *times*
|
||||
* better performance. Downside is that it has to allocate
|
||||
* jumbo buffer to accommodate up to 8 records, but the
|
||||
* compromise is considered worthy.
|
||||
*/
|
||||
if (type == SSL3_RT_APPLICATION_DATA &&
|
||||
u_len >= 4 * (max_send_fragment = s->max_send_fragment) &&
|
||||
s->compress == NULL && s->msg_callback == NULL &&
|
||||
!SSL_WRITE_ETM(s) && SSL_USE_EXPLICIT_IV(s) &&
|
||||
EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &
|
||||
EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
|
||||
unsigned char aad[13];
|
||||
EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
|
||||
int packlen;
|
||||
|
||||
/* minimize address aliasing conflicts */
|
||||
if ((max_send_fragment & 0xfff) == 0)
|
||||
max_send_fragment -= 512;
|
||||
|
||||
if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
|
||||
packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
||||
EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
|
||||
max_send_fragment, NULL);
|
||||
|
||||
if (u_len >= 8 * max_send_fragment)
|
||||
packlen *= 8;
|
||||
else
|
||||
packlen *= 4;
|
||||
|
||||
if (!ssl3_setup_write_buffer(s, 1, packlen)) {
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else if (tot == len) { /* done? */
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
return tot;
|
||||
}
|
||||
|
||||
n = (len - tot);
|
||||
for (;;) {
|
||||
if (n < 4 * max_send_fragment) {
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
break;
|
||||
}
|
||||
|
||||
if (s->s3->alert_dispatch) {
|
||||
i = s->method->ssl_dispatch_alert(s);
|
||||
if (i <= 0) {
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
if (n >= 8 * max_send_fragment)
|
||||
nw = max_send_fragment * (mb_param.interleave = 8);
|
||||
else
|
||||
nw = max_send_fragment * (mb_param.interleave = 4);
|
||||
|
||||
memcpy(aad, s->rlayer.write_sequence, 8);
|
||||
aad[8] = type;
|
||||
aad[9] = (unsigned char)(s->version >> 8);
|
||||
aad[10] = (unsigned char)(s->version);
|
||||
aad[11] = 0;
|
||||
aad[12] = 0;
|
||||
mb_param.out = NULL;
|
||||
mb_param.inp = aad;
|
||||
mb_param.len = nw;
|
||||
|
||||
packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
||||
EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
|
||||
sizeof(mb_param), &mb_param);
|
||||
|
||||
if (packlen <= 0 || packlen > (int)wb->len) { /* never happens */
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
break;
|
||||
}
|
||||
|
||||
mb_param.out = wb->buf;
|
||||
mb_param.inp = &buf[tot];
|
||||
mb_param.len = nw;
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
||||
EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
|
||||
sizeof(mb_param), &mb_param) <= 0)
|
||||
return -1;
|
||||
|
||||
s->rlayer.write_sequence[7] += mb_param.interleave;
|
||||
if (s->rlayer.write_sequence[7] < mb_param.interleave) {
|
||||
int j = 6;
|
||||
while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ;
|
||||
}
|
||||
|
||||
wb->offset = 0;
|
||||
wb->left = packlen;
|
||||
|
||||
s->rlayer.wpend_tot = nw;
|
||||
s->rlayer.wpend_buf = &buf[tot];
|
||||
s->rlayer.wpend_type = type;
|
||||
s->rlayer.wpend_ret = nw;
|
||||
|
||||
i = ssl3_write_pending(s, type, &buf[tot], nw);
|
||||
if (i <= 0) {
|
||||
if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
}
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
if (i == (int)n) {
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
return tot + i;
|
||||
}
|
||||
n -= i;
|
||||
tot += i;
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
if (tot == len) { /* done? */
|
||||
if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
|
||||
ssl3_release_write_buffer(s);
|
||||
|
||||
return tot;
|
||||
}
|
||||
|
||||
n = (len - tot);
|
||||
|
||||
split_send_fragment = s->split_send_fragment;
|
||||
/*
|
||||
* If max_pipelines is 0 then this means "undefined" and we default to
|
||||
* 1 pipeline. Similarly if the cipher does not support pipelined
|
||||
* processing then we also only use 1 pipeline, or if we're not using
|
||||
* explicit IVs
|
||||
*/
|
||||
maxpipes = s->max_pipelines;
|
||||
if (maxpipes > SSL_MAX_PIPELINES) {
|
||||
/*
|
||||
* We should have prevented this when we set max_pipelines so we
|
||||
* shouldn't get here
|
||||
*/
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
if (maxpipes == 0
|
||||
|| s->enc_write_ctx == NULL
|
||||
|| !(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx))
|
||||
& EVP_CIPH_FLAG_PIPELINE)
|
||||
|| !SSL_USE_EXPLICIT_IV(s))
|
||||
maxpipes = 1;
|
||||
if (s->max_send_fragment == 0 || split_send_fragment > s->max_send_fragment
|
||||
|| split_send_fragment == 0) {
|
||||
/*
|
||||
* We should have prevented this when we set the split and max send
|
||||
* fragments so we shouldn't get here
|
||||
*/
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
unsigned int pipelens[SSL_MAX_PIPELINES], tmppipelen, remain;
|
||||
unsigned int numpipes, j;
|
||||
|
||||
if (n == 0)
|
||||
numpipes = 1;
|
||||
else
|
||||
numpipes = ((n - 1) / split_send_fragment) + 1;
|
||||
if (numpipes > maxpipes)
|
||||
numpipes = maxpipes;
|
||||
|
||||
if (n / numpipes >= s->max_send_fragment) {
|
||||
/*
|
||||
* We have enough data to completely fill all available
|
||||
* pipelines
|
||||
*/
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
pipelens[j] = s->max_send_fragment;
|
||||
}
|
||||
} else {
|
||||
/* We can partially fill all available pipelines */
|
||||
tmppipelen = n / numpipes;
|
||||
remain = n % numpipes;
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
pipelens[j] = tmppipelen;
|
||||
if (j < remain)
|
||||
pipelens[j]++;
|
||||
}
|
||||
}
|
||||
|
||||
i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0);
|
||||
if (i <= 0) {
|
||||
/* XXX should we ssl3_release_write_buffer if i<0? */
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
|
||||
if ((i == (int)n) ||
|
||||
(type == SSL3_RT_APPLICATION_DATA &&
|
||||
(s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
|
||||
/*
|
||||
* next chunk of data should get another prepended empty fragment
|
||||
* in ciphersuites with known-IV weakness:
|
||||
*/
|
||||
s->s3->empty_fragment_done = 0;
|
||||
|
||||
if ((i == (int)n) && s->mode & SSL_MODE_RELEASE_BUFFERS &&
|
||||
!SSL_IS_DTLS(s))
|
||||
ssl3_release_write_buffer(s);
|
||||
|
||||
return tot + i;
|
||||
}
|
||||
|
||||
n -= i;
|
||||
tot += i;
|
||||
}
|
||||
}
|
||||
|
||||
int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int *pipelens, unsigned int numpipes,
|
||||
int create_empty_fragment)
|
||||
{
|
||||
unsigned char *outbuf[SSL_MAX_PIPELINES], *plen[SSL_MAX_PIPELINES];
|
||||
SSL3_RECORD wr[SSL_MAX_PIPELINES];
|
||||
int i, mac_size, clear = 0;
|
||||
int prefix_len = 0;
|
||||
int eivlen;
|
||||
size_t align = 0;
|
||||
SSL3_BUFFER *wb;
|
||||
SSL_SESSION *sess;
|
||||
unsigned int totlen = 0;
|
||||
unsigned int j;
|
||||
|
||||
for (j = 0; j < numpipes; j++)
|
||||
totlen += pipelens[j];
|
||||
/*
|
||||
* first check if there is a SSL3_BUFFER still being written out. This
|
||||
* will happen with non blocking IO
|
||||
*/
|
||||
if (RECORD_LAYER_write_pending(&s->rlayer))
|
||||
return (ssl3_write_pending(s, type, buf, totlen));
|
||||
|
||||
/* If we have an alert to send, lets send it */
|
||||
if (s->s3->alert_dispatch) {
|
||||
i = s->method->ssl_dispatch_alert(s);
|
||||
if (i <= 0)
|
||||
return (i);
|
||||
/* if it went, fall through and send more stuff */
|
||||
}
|
||||
|
||||
if (s->rlayer.numwpipes < numpipes)
|
||||
if (!ssl3_setup_write_buffer(s, numpipes, 0))
|
||||
return -1;
|
||||
|
||||
if (totlen == 0 && !create_empty_fragment)
|
||||
return 0;
|
||||
|
||||
sess = s->session;
|
||||
|
||||
if ((sess == NULL) ||
|
||||
(s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) {
|
||||
clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
|
||||
mac_size = 0;
|
||||
} else {
|
||||
mac_size = EVP_MD_CTX_size(s->write_hash);
|
||||
if (mac_size < 0)
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* 'create_empty_fragment' is true only when this function calls itself
|
||||
*/
|
||||
if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {
|
||||
/*
|
||||
* countermeasure against known-IV weakness in CBC ciphersuites (see
|
||||
* http://www.openssl.org/~bodo/tls-cbc.txt)
|
||||
*/
|
||||
|
||||
if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {
|
||||
/*
|
||||
* recursive function call with 'create_empty_fragment' set; this
|
||||
* prepares and buffers the data for an empty fragment (these
|
||||
* 'prefix_len' bytes are sent out later together with the actual
|
||||
* payload)
|
||||
*/
|
||||
unsigned int tmppipelen = 0;
|
||||
|
||||
prefix_len = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1);
|
||||
if (prefix_len <= 0)
|
||||
goto err;
|
||||
|
||||
if (prefix_len >
|
||||
(SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
|
||||
/* insufficient space */
|
||||
SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
s->s3->empty_fragment_done = 1;
|
||||
}
|
||||
|
||||
if (create_empty_fragment) {
|
||||
wb = &s->rlayer.wbuf[0];
|
||||
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
||||
/*
|
||||
* extra fragment would be couple of cipher blocks, which would be
|
||||
* multiple of SSL3_ALIGN_PAYLOAD, so if we want to align the real
|
||||
* payload, then we can just pretend we simply have two headers.
|
||||
*/
|
||||
align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;
|
||||
align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
|
||||
#endif
|
||||
outbuf[0] = SSL3_BUFFER_get_buf(wb) + align;
|
||||
SSL3_BUFFER_set_offset(wb, align);
|
||||
} else if (prefix_len) {
|
||||
wb = &s->rlayer.wbuf[0];
|
||||
outbuf[0] = SSL3_BUFFER_get_buf(wb) + SSL3_BUFFER_get_offset(wb)
|
||||
+ prefix_len;
|
||||
} else {
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
wb = &s->rlayer.wbuf[j];
|
||||
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
||||
align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;
|
||||
align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
|
||||
#endif
|
||||
outbuf[j] = SSL3_BUFFER_get_buf(wb) + align;
|
||||
SSL3_BUFFER_set_offset(wb, align);
|
||||
}
|
||||
}
|
||||
|
||||
/* Explicit IV length, block ciphers appropriate version flag */
|
||||
if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {
|
||||
int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
|
||||
if (mode == EVP_CIPH_CBC_MODE) {
|
||||
eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
|
||||
if (eivlen <= 1)
|
||||
eivlen = 0;
|
||||
}
|
||||
/* Need explicit part of IV for GCM mode */
|
||||
else if (mode == EVP_CIPH_GCM_MODE)
|
||||
eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
else if (mode == EVP_CIPH_CCM_MODE)
|
||||
eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
else
|
||||
eivlen = 0;
|
||||
} else
|
||||
eivlen = 0;
|
||||
|
||||
totlen = 0;
|
||||
/* Clear our SSL3_RECORD structures */
|
||||
memset(wr, 0, sizeof(wr));
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
/* write the header */
|
||||
*(outbuf[j]++) = type & 0xff;
|
||||
SSL3_RECORD_set_type(&wr[j], type);
|
||||
|
||||
*(outbuf[j]++) = (s->version >> 8);
|
||||
/*
|
||||
* Some servers hang if initial client hello is larger than 256 bytes
|
||||
* and record version number > TLS 1.0
|
||||
*/
|
||||
if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO
|
||||
&& !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)
|
||||
*(outbuf[j]++) = 0x1;
|
||||
else
|
||||
*(outbuf[j]++) = s->version & 0xff;
|
||||
|
||||
/* field where we are to write out packet length */
|
||||
plen[j] = outbuf[j];
|
||||
outbuf[j] += 2;
|
||||
|
||||
/* lets setup the record stuff. */
|
||||
SSL3_RECORD_set_data(&wr[j], outbuf[j] + eivlen);
|
||||
SSL3_RECORD_set_length(&wr[j], (int)pipelens[j]);
|
||||
SSL3_RECORD_set_input(&wr[j], (unsigned char *)&buf[totlen]);
|
||||
totlen += pipelens[j];
|
||||
|
||||
/*
|
||||
* we now 'read' from wr->input, wr->length bytes into wr->data
|
||||
*/
|
||||
|
||||
/* first we compress */
|
||||
if (s->compress != NULL) {
|
||||
if (!ssl3_do_compress(s, &wr[j])) {
|
||||
SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
memcpy(wr[j].data, wr[j].input, wr[j].length);
|
||||
SSL3_RECORD_reset_input(&wr[j]);
|
||||
}
|
||||
|
||||
/*
|
||||
* we should still have the output to wr->data and the input from
|
||||
* wr->input. Length should be wr->length. wr->data still points in the
|
||||
* wb->buf
|
||||
*/
|
||||
|
||||
if (!SSL_WRITE_ETM(s) && mac_size != 0) {
|
||||
if (s->method->ssl3_enc->mac(s, &wr[j],
|
||||
&(outbuf[j][wr[j].length + eivlen]),
|
||||
1) < 0)
|
||||
goto err;
|
||||
SSL3_RECORD_add_length(&wr[j], mac_size);
|
||||
}
|
||||
|
||||
SSL3_RECORD_set_data(&wr[j], outbuf[j]);
|
||||
SSL3_RECORD_reset_input(&wr[j]);
|
||||
|
||||
if (eivlen) {
|
||||
/*
|
||||
* if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
|
||||
*/
|
||||
SSL3_RECORD_add_length(&wr[j], eivlen);
|
||||
}
|
||||
}
|
||||
|
||||
if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1)
|
||||
goto err;
|
||||
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
if (SSL_WRITE_ETM(s) && mac_size != 0) {
|
||||
if (s->method->ssl3_enc->mac(s, &wr[j],
|
||||
outbuf[j] + wr[j].length, 1) < 0)
|
||||
goto err;
|
||||
SSL3_RECORD_add_length(&wr[j], mac_size);
|
||||
}
|
||||
|
||||
/* record length after mac and block padding */
|
||||
s2n(SSL3_RECORD_get_length(&wr[j]), plen[j]);
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(1, 0, SSL3_RT_HEADER, plen[j] - 5, 5, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
/*
|
||||
* we should now have wr->data pointing to the encrypted data, which is
|
||||
* wr->length long
|
||||
*/
|
||||
SSL3_RECORD_set_type(&wr[j], type); /* not needed but helps for
|
||||
* debugging */
|
||||
SSL3_RECORD_add_length(&wr[j], SSL3_RT_HEADER_LENGTH);
|
||||
|
||||
if (create_empty_fragment) {
|
||||
/*
|
||||
* we are in a recursive call; just return the length, don't write
|
||||
* out anything here
|
||||
*/
|
||||
if (j > 0) {
|
||||
/* We should never be pipelining an empty fragment!! */
|
||||
SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
return SSL3_RECORD_get_length(wr);
|
||||
}
|
||||
|
||||
/* now let's set up wb */
|
||||
SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],
|
||||
prefix_len + SSL3_RECORD_get_length(&wr[j]));
|
||||
}
|
||||
|
||||
/*
|
||||
* memorize arguments so that ssl3_write_pending can detect bad write
|
||||
* retries later
|
||||
*/
|
||||
s->rlayer.wpend_tot = totlen;
|
||||
s->rlayer.wpend_buf = buf;
|
||||
s->rlayer.wpend_type = type;
|
||||
s->rlayer.wpend_ret = totlen;
|
||||
|
||||
/* we now just need to write the buffer */
|
||||
return ssl3_write_pending(s, type, buf, totlen);
|
||||
err:
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* if s->s3->wbuf.left != 0, we need to call this
|
||||
*
|
||||
* Return values are as per SSL_write()
|
||||
*/
|
||||
int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int len)
|
||||
{
|
||||
int i;
|
||||
SSL3_BUFFER *wb = s->rlayer.wbuf;
|
||||
unsigned int currbuf = 0;
|
||||
|
||||
if ((s->rlayer.wpend_tot > (int)len)
|
||||
|| (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
|
||||
&& (s->rlayer.wpend_buf != buf))
|
||||
|| (s->rlayer.wpend_type != type)) {
|
||||
SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* Loop until we find a buffer we haven't written out yet */
|
||||
if (SSL3_BUFFER_get_left(&wb[currbuf]) == 0
|
||||
&& currbuf < s->rlayer.numwpipes - 1) {
|
||||
currbuf++;
|
||||
continue;
|
||||
}
|
||||
clear_sys_error();
|
||||
if (s->wbio != NULL) {
|
||||
s->rwstate = SSL_WRITING;
|
||||
i = BIO_write(s->wbio, (char *)
|
||||
&(SSL3_BUFFER_get_buf(&wb[currbuf])
|
||||
[SSL3_BUFFER_get_offset(&wb[currbuf])]),
|
||||
(unsigned int)SSL3_BUFFER_get_left(&wb[currbuf]));
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
|
||||
i = -1;
|
||||
}
|
||||
if (i == SSL3_BUFFER_get_left(&wb[currbuf])) {
|
||||
SSL3_BUFFER_set_left(&wb[currbuf], 0);
|
||||
SSL3_BUFFER_add_offset(&wb[currbuf], i);
|
||||
if (currbuf + 1 < s->rlayer.numwpipes)
|
||||
continue;
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (s->rlayer.wpend_ret);
|
||||
} else if (i <= 0) {
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
/*
|
||||
* For DTLS, just drop it. That's kind of the whole point in
|
||||
* using a datagram service
|
||||
*/
|
||||
SSL3_BUFFER_set_left(&wb[currbuf], 0);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
SSL3_BUFFER_add_offset(&wb[currbuf], i);
|
||||
SSL3_BUFFER_add_left(&wb[currbuf], -i);
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* Return up to 'len' payload bytes received in 'type' records.
|
||||
* 'type' is one of the following:
|
||||
*
|
||||
* - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
|
||||
* - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
|
||||
* - 0 (during a shutdown, no data has to be returned)
|
||||
*
|
||||
* If we don't have stored data to work from, read a SSL/TLS record first
|
||||
* (possibly multiple records if we still don't have anything to return).
|
||||
*
|
||||
* This function must handle any surprises the peer may have for us, such as
|
||||
* Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
|
||||
* messages are treated as if they were handshake messages *if* the |recd_type|
|
||||
* argument is non NULL.
|
||||
* Also if record payloads contain fragments too small to process, we store
|
||||
* them until there is enough for the respective protocol (the record protocol
|
||||
* may use arbitrary fragmentation and even interleaving):
|
||||
* Change cipher spec protocol
|
||||
* just 1 byte needed, no need for keeping anything stored
|
||||
* Alert protocol
|
||||
* 2 bytes needed (AlertLevel, AlertDescription)
|
||||
* Handshake protocol
|
||||
* 4 bytes needed (HandshakeType, uint24 length) -- we just have
|
||||
* to detect unexpected Client Hello and Hello Request messages
|
||||
* here, anything else is handled by higher layers
|
||||
* Application data protocol
|
||||
* none of our business
|
||||
*/
|
||||
int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
int len, int peek)
|
||||
{
|
||||
int al, i, j, ret;
|
||||
unsigned int n, curr_rec, num_recs, read_bytes;
|
||||
SSL3_RECORD *rr;
|
||||
SSL3_BUFFER *rbuf;
|
||||
void (*cb) (const SSL *ssl, int type2, int val) = NULL;
|
||||
|
||||
rbuf = &s->rlayer.rbuf;
|
||||
|
||||
if (!SSL3_BUFFER_is_initialised(rbuf)) {
|
||||
/* Not initialized yet */
|
||||
if (!ssl3_setup_read_buffer(s))
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if ((type && (type != SSL3_RT_APPLICATION_DATA)
|
||||
&& (type != SSL3_RT_HANDSHAKE)) || (peek
|
||||
&& (type !=
|
||||
SSL3_RT_APPLICATION_DATA))) {
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
|
||||
/* (partially) satisfy request from storage */
|
||||
{
|
||||
unsigned char *src = s->rlayer.handshake_fragment;
|
||||
unsigned char *dst = buf;
|
||||
unsigned int k;
|
||||
|
||||
/* peek == 0 */
|
||||
n = 0;
|
||||
while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
|
||||
*dst++ = *src++;
|
||||
len--;
|
||||
s->rlayer.handshake_fragment_len--;
|
||||
n++;
|
||||
}
|
||||
/* move any remaining fragment bytes: */
|
||||
for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
|
||||
s->rlayer.handshake_fragment[k] = *src++;
|
||||
|
||||
if (recvd_type != NULL)
|
||||
*recvd_type = SSL3_RT_HANDSHAKE;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
|
||||
*/
|
||||
|
||||
if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {
|
||||
/* type == SSL3_RT_APPLICATION_DATA */
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
start:
|
||||
s->rwstate = SSL_NOTHING;
|
||||
|
||||
/*-
|
||||
* For each record 'i' up to |num_recs]
|
||||
* rr[i].type - is the type of record
|
||||
* rr[i].data, - data
|
||||
* rr[i].off, - offset into 'data' for next read
|
||||
* rr[i].length, - number of bytes.
|
||||
*/
|
||||
rr = s->rlayer.rrec;
|
||||
num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);
|
||||
|
||||
do {
|
||||
/* get new records if necessary */
|
||||
if (num_recs == 0) {
|
||||
ret = ssl3_get_record(s);
|
||||
if (ret <= 0)
|
||||
return (ret);
|
||||
num_recs = RECORD_LAYER_get_numrpipes(&s->rlayer);
|
||||
if (num_recs == 0) {
|
||||
/* Shouldn't happen */
|
||||
al = SSL_AD_INTERNAL_ERROR;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
/* Skip over any records we have already read */
|
||||
for (curr_rec = 0;
|
||||
curr_rec < num_recs && SSL3_RECORD_is_read(&rr[curr_rec]);
|
||||
curr_rec++) ;
|
||||
if (curr_rec == num_recs) {
|
||||
RECORD_LAYER_set_numrpipes(&s->rlayer, 0);
|
||||
num_recs = 0;
|
||||
curr_rec = 0;
|
||||
}
|
||||
} while (num_recs == 0);
|
||||
rr = &rr[curr_rec];
|
||||
|
||||
/*
|
||||
* Reset the count of consecutive warning alerts if we've got a non-empty
|
||||
* record that isn't an alert.
|
||||
*/
|
||||
if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
|
||||
&& SSL3_RECORD_get_length(rr) != 0)
|
||||
s->rlayer.alert_count = 0;
|
||||
|
||||
/* we now have a packet which can be read and processed */
|
||||
|
||||
if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
|
||||
* reset by ssl3_get_finished */
|
||||
&& (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the other end has shut down, throw anything we read away (even in
|
||||
* 'peek' mode)
|
||||
*/
|
||||
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (type == SSL3_RECORD_get_type(rr)
|
||||
|| (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
&& type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
|
||||
/*
|
||||
* SSL3_RT_APPLICATION_DATA or
|
||||
* SSL3_RT_HANDSHAKE or
|
||||
* SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
*/
|
||||
/*
|
||||
* make sure that we are not getting application data when we are
|
||||
* doing a handshake for the first time
|
||||
*/
|
||||
if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
|
||||
(s->enc_read_ctx == NULL)) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (type == SSL3_RT_HANDSHAKE
|
||||
&& SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
&& s->rlayer.handshake_fragment_len > 0) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (recvd_type != NULL)
|
||||
*recvd_type = SSL3_RECORD_get_type(rr);
|
||||
|
||||
if (len <= 0) {
|
||||
/*
|
||||
* Mark a zero length record as read. This ensures multiple calls to
|
||||
* SSL_read() with a zero length buffer will eventually cause
|
||||
* SSL_pending() to report data as being available.
|
||||
*/
|
||||
if (SSL3_RECORD_get_length(rr) == 0)
|
||||
SSL3_RECORD_set_read(rr);
|
||||
return len;
|
||||
}
|
||||
|
||||
read_bytes = 0;
|
||||
do {
|
||||
if ((unsigned int)len - read_bytes > SSL3_RECORD_get_length(rr))
|
||||
n = SSL3_RECORD_get_length(rr);
|
||||
else
|
||||
n = (unsigned int)len - read_bytes;
|
||||
|
||||
memcpy(buf, &(rr->data[rr->off]), n);
|
||||
buf += n;
|
||||
if (peek) {
|
||||
/* Mark any zero length record as consumed CVE-2016-6305 */
|
||||
if (SSL3_RECORD_get_length(rr) == 0)
|
||||
SSL3_RECORD_set_read(rr);
|
||||
} else {
|
||||
SSL3_RECORD_sub_length(rr, n);
|
||||
SSL3_RECORD_add_off(rr, n);
|
||||
if (SSL3_RECORD_get_length(rr) == 0) {
|
||||
s->rlayer.rstate = SSL_ST_READ_HEADER;
|
||||
SSL3_RECORD_set_off(rr, 0);
|
||||
SSL3_RECORD_set_read(rr);
|
||||
}
|
||||
}
|
||||
if (SSL3_RECORD_get_length(rr) == 0
|
||||
|| (peek && n == SSL3_RECORD_get_length(rr))) {
|
||||
curr_rec++;
|
||||
rr++;
|
||||
}
|
||||
read_bytes += n;
|
||||
} while (type == SSL3_RT_APPLICATION_DATA && curr_rec < num_recs
|
||||
&& read_bytes < (unsigned int)len);
|
||||
if (read_bytes == 0) {
|
||||
/* We must have read empty records. Get more data */
|
||||
goto start;
|
||||
}
|
||||
if (!peek && curr_rec == num_recs
|
||||
&& (s->mode & SSL_MODE_RELEASE_BUFFERS)
|
||||
&& SSL3_BUFFER_get_left(rbuf) == 0)
|
||||
ssl3_release_read_buffer(s);
|
||||
return read_bytes;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we get here, then type != rr->type; if we have a handshake message,
|
||||
* then it was unexpected (Hello Request or Client Hello) or invalid (we
|
||||
* were actually expecting a CCS).
|
||||
*/
|
||||
|
||||
/*
|
||||
* Lets just double check that we've not got an SSLv2 record
|
||||
*/
|
||||
if (rr->rec_version == SSL2_VERSION) {
|
||||
/*
|
||||
* Should never happen. ssl3_get_record() should only give us an SSLv2
|
||||
* record back if this is the first packet and we are looking for an
|
||||
* initial ClientHello. Therefore |type| should always be equal to
|
||||
* |rr->type|. If not then something has gone horribly wrong
|
||||
*/
|
||||
al = SSL_AD_INTERNAL_ERROR;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (s->method->version == TLS_ANY_VERSION
|
||||
&& (s->server || rr->type != SSL3_RT_ALERT)) {
|
||||
/*
|
||||
* If we've got this far and still haven't decided on what version
|
||||
* we're using then this must be a client side alert we're dealing with
|
||||
* (we don't allow heartbeats yet). We shouldn't be receiving anything
|
||||
* other than a ClientHello if we are a server.
|
||||
*/
|
||||
s->version = rr->rec_version;
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_MESSAGE);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
/*
|
||||
* In case of record types for which we have 'fragment' storage, fill
|
||||
* that so that we can process the data at a fixed place.
|
||||
*/
|
||||
{
|
||||
unsigned int dest_maxlen = 0;
|
||||
unsigned char *dest = NULL;
|
||||
unsigned int *dest_len = NULL;
|
||||
|
||||
if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
|
||||
dest_maxlen = sizeof(s->rlayer.handshake_fragment);
|
||||
dest = s->rlayer.handshake_fragment;
|
||||
dest_len = &s->rlayer.handshake_fragment_len;
|
||||
} else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
|
||||
dest_maxlen = sizeof(s->rlayer.alert_fragment);
|
||||
dest = s->rlayer.alert_fragment;
|
||||
dest_len = &s->rlayer.alert_fragment_len;
|
||||
}
|
||||
|
||||
if (dest_maxlen > 0) {
|
||||
n = dest_maxlen - *dest_len; /* available space in 'dest' */
|
||||
if (SSL3_RECORD_get_length(rr) < n)
|
||||
n = SSL3_RECORD_get_length(rr); /* available bytes */
|
||||
|
||||
/* now move 'n' bytes: */
|
||||
while (n-- > 0) {
|
||||
dest[(*dest_len)++] =
|
||||
SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
|
||||
SSL3_RECORD_add_off(rr, 1);
|
||||
SSL3_RECORD_add_length(rr, -1);
|
||||
}
|
||||
|
||||
if (*dest_len < dest_maxlen) {
|
||||
SSL3_RECORD_set_read(rr);
|
||||
goto start; /* fragment was too small */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
|
||||
* s->rlayer.alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
|
||||
* (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
|
||||
*/
|
||||
|
||||
/* If we are a client, check for an incoming 'Hello Request': */
|
||||
if ((!s->server) &&
|
||||
(s->rlayer.handshake_fragment_len >= 4) &&
|
||||
(s->rlayer.handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
|
||||
(s->session != NULL) && (s->session->cipher != NULL)) {
|
||||
s->rlayer.handshake_fragment_len = 0;
|
||||
|
||||
if ((s->rlayer.handshake_fragment[1] != 0) ||
|
||||
(s->rlayer.handshake_fragment[2] != 0) ||
|
||||
(s->rlayer.handshake_fragment[3] != 0)) {
|
||||
al = SSL_AD_DECODE_ERROR;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
|
||||
s->rlayer.handshake_fragment, 4, s,
|
||||
s->msg_callback_arg);
|
||||
if (SSL_is_init_finished(s) &&
|
||||
(s->options & SSL_OP_NO_RENEGOTIATION) == 0 &&
|
||||
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
|
||||
!s->s3->renegotiate) {
|
||||
ssl3_renegotiate(s);
|
||||
if (ssl3_renegotiate_check(s)) {
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
||||
if (SSL3_BUFFER_get_left(rbuf) == 0) {
|
||||
/* no read-ahead left? */
|
||||
BIO *bio;
|
||||
/*
|
||||
* In the case where we try to read application data,
|
||||
* but we trigger an SSL handshake, we return -1 with
|
||||
* the retry option set. Otherwise renegotiation may
|
||||
* cause nasty problems in the blocking world
|
||||
*/
|
||||
s->rwstate = SSL_READING;
|
||||
bio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SSL3_RECORD_set_read(rr);
|
||||
}
|
||||
} else {
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
|
||||
SSL3_RECORD_set_read(rr);
|
||||
}
|
||||
/*
|
||||
* we either finished a handshake or ignored the request, now try
|
||||
* again to obtain the (application) data we were asked for
|
||||
*/
|
||||
goto start;
|
||||
}
|
||||
/*
|
||||
* If we are a server and get a client hello when renegotiation isn't
|
||||
* allowed send back a no renegotiation alert and carry on.
|
||||
*/
|
||||
if (s->server
|
||||
&& SSL_is_init_finished(s)
|
||||
&& s->version > SSL3_VERSION
|
||||
&& s->rlayer.handshake_fragment_len >= SSL3_HM_HEADER_LENGTH
|
||||
&& s->rlayer.handshake_fragment[0] == SSL3_MT_CLIENT_HELLO
|
||||
&& s->s3->previous_client_finished_len != 0
|
||||
&& ((!s->s3->send_connection_binding
|
||||
&& (s->options
|
||||
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)
|
||||
|| (s->options & SSL_OP_NO_RENEGOTIATION) != 0)) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
SSL3_RECORD_set_read(rr);
|
||||
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
|
||||
goto start;
|
||||
}
|
||||
if (s->rlayer.alert_fragment_len >= 2) {
|
||||
int alert_level = s->rlayer.alert_fragment[0];
|
||||
int alert_descr = s->rlayer.alert_fragment[1];
|
||||
|
||||
s->rlayer.alert_fragment_len = 0;
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, s->version, SSL3_RT_ALERT,
|
||||
s->rlayer.alert_fragment, 2, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
if (s->info_callback != NULL)
|
||||
cb = s->info_callback;
|
||||
else if (s->ctx->info_callback != NULL)
|
||||
cb = s->ctx->info_callback;
|
||||
|
||||
if (cb != NULL) {
|
||||
j = (alert_level << 8) | alert_descr;
|
||||
cb(s, SSL_CB_READ_ALERT, j);
|
||||
}
|
||||
|
||||
if (alert_level == SSL3_AL_WARNING) {
|
||||
s->s3->warn_alert = alert_descr;
|
||||
SSL3_RECORD_set_read(rr);
|
||||
|
||||
s->rlayer.alert_count++;
|
||||
if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_TOO_MANY_WARN_ALERTS);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
return (0);
|
||||
}
|
||||
/*
|
||||
* This is a warning but we receive it if we requested
|
||||
* renegotiation and the peer denied it. Terminate with a fatal
|
||||
* alert because if application tried to renegotiate it
|
||||
* presumably had a good reason and expects it to succeed. In
|
||||
* future we might have a renegotiation where we don't care if
|
||||
* the peer refused it where we carry on.
|
||||
*/
|
||||
else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
|
||||
al = SSL_AD_HANDSHAKE_FAILURE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_NO_RENEGOTIATION);
|
||||
goto f_err;
|
||||
}
|
||||
#ifdef SSL_AD_MISSING_SRP_USERNAME
|
||||
else if (alert_descr == SSL_AD_MISSING_SRP_USERNAME)
|
||||
return (0);
|
||||
#endif
|
||||
} else if (alert_level == SSL3_AL_FATAL) {
|
||||
char tmp[16];
|
||||
|
||||
s->rwstate = SSL_NOTHING;
|
||||
s->s3->fatal_alert = alert_descr;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
|
||||
BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
|
||||
ERR_add_error_data(2, "SSL alert number ", tmp);
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
SSL3_RECORD_set_read(rr);
|
||||
SSL_CTX_remove_session(s->session_ctx, s->session);
|
||||
return (0);
|
||||
} else {
|
||||
al = SSL_AD_ILLEGAL_PARAMETER;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
goto start;
|
||||
}
|
||||
|
||||
if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
|
||||
* shutdown */
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
SSL3_RECORD_set_read(rr);
|
||||
return (0);
|
||||
}
|
||||
|
||||
if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_CCS_RECEIVED_EARLY);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unexpected handshake message (Client Hello, or protocol violation)
|
||||
*/
|
||||
if ((s->rlayer.handshake_fragment_len >= 4)
|
||||
&& !ossl_statem_get_in_handshake(s)) {
|
||||
if (SSL_is_init_finished(s) &&
|
||||
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
|
||||
ossl_statem_set_in_init(s, 1);
|
||||
s->renegotiate = 1;
|
||||
s->new_session = 1;
|
||||
}
|
||||
i = s->handshake_func(s);
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
||||
if (SSL3_BUFFER_get_left(rbuf) == 0) {
|
||||
/* no read-ahead left? */
|
||||
BIO *bio;
|
||||
/*
|
||||
* In the case where we try to read application data, but we
|
||||
* trigger an SSL handshake, we return -1 with the retry
|
||||
* option set. Otherwise renegotiation may cause nasty
|
||||
* problems in the blocking world
|
||||
*/
|
||||
s->rwstate = SSL_READING;
|
||||
bio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
goto start;
|
||||
}
|
||||
|
||||
switch (SSL3_RECORD_get_type(rr)) {
|
||||
default:
|
||||
/*
|
||||
* TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
|
||||
* TLS 1.2 says you MUST send an unexpected message alert. We use the
|
||||
* TLS 1.2 behaviour for all protocol versions to prevent issues where
|
||||
* no progress is being made and the peer continually sends unrecognised
|
||||
* record types, using up resources processing them.
|
||||
*/
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
case SSL3_RT_CHANGE_CIPHER_SPEC:
|
||||
case SSL3_RT_ALERT:
|
||||
case SSL3_RT_HANDSHAKE:
|
||||
/*
|
||||
* we already handled all of these, with the possible exception of
|
||||
* SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
|
||||
* that should not happen when type != rr->type
|
||||
*/
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
goto f_err;
|
||||
case SSL3_RT_APPLICATION_DATA:
|
||||
/*
|
||||
* At this point, we were expecting handshake data, but have
|
||||
* application data. If the library was running inside ssl3_read()
|
||||
* (i.e. in_read_app_data is set) and it makes sense to read
|
||||
* application data at this point (session renegotiation not yet
|
||||
* started), we will indulge it.
|
||||
*/
|
||||
if (ossl_statem_app_data_allowed(s)) {
|
||||
s->s3->in_read_app_data = 2;
|
||||
return (-1);
|
||||
} else {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
/* not reached */
|
||||
|
||||
f_err:
|
||||
ssl3_send_alert(s, SSL3_AL_FATAL, al);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void ssl3_record_sequence_update(unsigned char *seq)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 7; i >= 0; i--) {
|
||||
++seq[i];
|
||||
if (seq[i] != 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns true if the current rrec was sent in SSLv2 backwards compatible
|
||||
* format and false otherwise.
|
||||
*/
|
||||
int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
|
||||
{
|
||||
return SSL3_RECORD_is_sslv2_record(&rl->rrec[0]);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the length in bytes of the current rrec
|
||||
*/
|
||||
unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl)
|
||||
{
|
||||
return SSL3_RECORD_get_length(&rl->rrec[0]);
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* These structures should be considered PRIVATE to the record layer. No *
|
||||
* non-record layer code should be using these structures in any way. *
|
||||
* *
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct ssl3_buffer_st {
|
||||
/* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
|
||||
unsigned char *buf;
|
||||
/* default buffer size (or 0 if no default set) */
|
||||
size_t default_len;
|
||||
/* buffer size */
|
||||
size_t len;
|
||||
/* where to 'copy from' */
|
||||
int offset;
|
||||
/* how many bytes left */
|
||||
int left;
|
||||
} SSL3_BUFFER;
|
||||
|
||||
#define SEQ_NUM_SIZE 8
|
||||
|
||||
typedef struct ssl3_record_st {
|
||||
/* Record layer version */
|
||||
/* r */
|
||||
int rec_version;
|
||||
/* type of record */
|
||||
/* r */
|
||||
int type;
|
||||
/* How many bytes available */
|
||||
/* rw */
|
||||
unsigned int length;
|
||||
/*
|
||||
* How many bytes were available before padding was removed? This is used
|
||||
* to implement the MAC check in constant time for CBC records.
|
||||
*/
|
||||
/* rw */
|
||||
unsigned int orig_len;
|
||||
/* read/write offset into 'buf' */
|
||||
/* r */
|
||||
unsigned int off;
|
||||
/* pointer to the record data */
|
||||
/* rw */
|
||||
unsigned char *data;
|
||||
/* where the decode bytes are */
|
||||
/* rw */
|
||||
unsigned char *input;
|
||||
/* only used with decompression - malloc()ed */
|
||||
/* r */
|
||||
unsigned char *comp;
|
||||
/* Whether the data from this record has already been read or not */
|
||||
/* r */
|
||||
unsigned int read;
|
||||
/* epoch number, needed by DTLS1 */
|
||||
/* r */
|
||||
unsigned long epoch;
|
||||
/* sequence number, needed by DTLS1 */
|
||||
/* r */
|
||||
unsigned char seq_num[SEQ_NUM_SIZE];
|
||||
} SSL3_RECORD;
|
||||
|
||||
typedef struct dtls1_bitmap_st {
|
||||
/* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
|
||||
unsigned long map;
|
||||
/* Max record number seen so far, 64-bit value in big-endian encoding */
|
||||
unsigned char max_seq_num[SEQ_NUM_SIZE];
|
||||
} DTLS1_BITMAP;
|
||||
|
||||
typedef struct record_pqueue_st {
|
||||
unsigned short epoch;
|
||||
struct pqueue_st *q;
|
||||
} record_pqueue;
|
||||
|
||||
typedef struct dtls1_record_data_st {
|
||||
unsigned char *packet;
|
||||
unsigned int packet_length;
|
||||
SSL3_BUFFER rbuf;
|
||||
SSL3_RECORD rrec;
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
struct bio_dgram_sctp_rcvinfo recordinfo;
|
||||
#endif
|
||||
} DTLS1_RECORD_DATA;
|
||||
|
||||
typedef struct dtls_record_layer_st {
|
||||
/*
|
||||
* The current data and handshake epoch. This is initially
|
||||
* undefined, and starts at zero once the initial handshake is
|
||||
* completed
|
||||
*/
|
||||
unsigned short r_epoch;
|
||||
unsigned short w_epoch;
|
||||
/* records being received in the current epoch */
|
||||
DTLS1_BITMAP bitmap;
|
||||
/* renegotiation starts a new set of sequence numbers */
|
||||
DTLS1_BITMAP next_bitmap;
|
||||
/* Received handshake records (processed and unprocessed) */
|
||||
record_pqueue unprocessed_rcds;
|
||||
record_pqueue processed_rcds;
|
||||
/*
|
||||
* Buffered application records. Only for records between CCS and
|
||||
* Finished to prevent either protocol violation or unnecessary message
|
||||
* loss.
|
||||
*/
|
||||
record_pqueue buffered_app_data;
|
||||
/*
|
||||
* storage for Alert/Handshake protocol data received but not yet
|
||||
* processed by ssl3_read_bytes:
|
||||
*/
|
||||
unsigned char alert_fragment[DTLS1_AL_HEADER_LENGTH];
|
||||
unsigned int alert_fragment_len;
|
||||
unsigned char handshake_fragment[DTLS1_HM_HEADER_LENGTH];
|
||||
unsigned int handshake_fragment_len;
|
||||
/* save last and current sequence numbers for retransmissions */
|
||||
unsigned char last_write_sequence[8];
|
||||
unsigned char curr_write_sequence[8];
|
||||
} DTLS_RECORD_LAYER;
|
||||
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* This structure should be considered "opaque" to anything outside of the *
|
||||
* record layer. No non-record layer code should be accessing the members of *
|
||||
* this structure. *
|
||||
* *
|
||||
*****************************************************************************/
|
||||
|
||||
typedef struct record_layer_st {
|
||||
/* The parent SSL structure */
|
||||
SSL *s;
|
||||
/*
|
||||
* Read as many input bytes as possible (for
|
||||
* non-blocking reads)
|
||||
*/
|
||||
int read_ahead;
|
||||
/* where we are when reading */
|
||||
int rstate;
|
||||
/* How many pipelines can be used to read data */
|
||||
unsigned int numrpipes;
|
||||
/* How many pipelines can be used to write data */
|
||||
unsigned int numwpipes;
|
||||
/* read IO goes into here */
|
||||
SSL3_BUFFER rbuf;
|
||||
/* write IO goes into here */
|
||||
SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
|
||||
/* each decoded record goes in here */
|
||||
SSL3_RECORD rrec[SSL_MAX_PIPELINES];
|
||||
/* used internally to point at a raw packet */
|
||||
unsigned char *packet;
|
||||
unsigned int packet_length;
|
||||
/* number of bytes sent so far */
|
||||
unsigned int wnum;
|
||||
/*
|
||||
* storage for Alert/Handshake protocol data received but not yet
|
||||
* processed by ssl3_read_bytes:
|
||||
*/
|
||||
unsigned char alert_fragment[2];
|
||||
unsigned int alert_fragment_len;
|
||||
unsigned char handshake_fragment[4];
|
||||
unsigned int handshake_fragment_len;
|
||||
/* The number of consecutive empty records we have received */
|
||||
unsigned int empty_record_count;
|
||||
/* partial write - check the numbers match */
|
||||
/* number bytes written */
|
||||
int wpend_tot;
|
||||
int wpend_type;
|
||||
/* number of bytes submitted */
|
||||
int wpend_ret;
|
||||
const unsigned char *wpend_buf;
|
||||
unsigned char read_sequence[SEQ_NUM_SIZE];
|
||||
unsigned char write_sequence[SEQ_NUM_SIZE];
|
||||
/* Set to true if this is the first record in a connection */
|
||||
unsigned int is_first_record;
|
||||
/* Count of the number of consecutive warning alerts received */
|
||||
unsigned int alert_count;
|
||||
DTLS_RECORD_LAYER *d;
|
||||
} RECORD_LAYER;
|
||||
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* The following macros/functions represent the libssl internal API to the *
|
||||
* record layer. Any libssl code may call these functions/macros *
|
||||
* *
|
||||
*****************************************************************************/
|
||||
|
||||
#define MIN_SSL2_RECORD_LEN 9
|
||||
|
||||
#define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
|
||||
#define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
|
||||
#define RECORD_LAYER_get_packet(rl) ((rl)->packet)
|
||||
#define RECORD_LAYER_get_packet_length(rl) ((rl)->packet_length)
|
||||
#define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
|
||||
#define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
|
||||
#define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
|
||||
((rl)->d->processed_rcds)
|
||||
#define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
|
||||
((rl)->d->unprocessed_rcds)
|
||||
|
||||
void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s);
|
||||
void RECORD_LAYER_clear(RECORD_LAYER *rl);
|
||||
void RECORD_LAYER_release(RECORD_LAYER *rl);
|
||||
int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
|
||||
int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
|
||||
int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
|
||||
int RECORD_LAYER_set_data(RECORD_LAYER *rl, const unsigned char *buf, int len);
|
||||
void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
|
||||
void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
|
||||
int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
|
||||
unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
|
||||
__owur int ssl3_pending(const SSL *s);
|
||||
__owur int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
|
||||
__owur int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int *pipelens, unsigned int numpipes,
|
||||
int create_empty_fragment);
|
||||
__owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
|
||||
unsigned char *buf, int len, int peek);
|
||||
__owur int ssl3_setup_buffers(SSL *s);
|
||||
__owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send);
|
||||
__owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
|
||||
__owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int len);
|
||||
__owur int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int send);
|
||||
__owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
|
||||
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
|
||||
void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
|
||||
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
|
||||
void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
|
||||
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
|
||||
void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl);
|
||||
void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
|
||||
__owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
|
||||
unsigned char *buf, int len, int peek);
|
||||
__owur int dtls1_write_bytes(SSL *s, int type, const void *buf, int len);
|
||||
__owur int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int len, int create_empty_fragement);
|
||||
void dtls1_reset_seq_numbers(SSL *s, int rw);
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*****************************************************************************
|
||||
* *
|
||||
* The following macros/functions are PRIVATE to the record layer. They *
|
||||
* should NOT be used outside of the record layer. *
|
||||
* *
|
||||
*****************************************************************************/
|
||||
|
||||
#define MAX_WARN_ALERT_COUNT 5
|
||||
|
||||
/* Functions/macros provided by the RECORD_LAYER component */
|
||||
|
||||
#define RECORD_LAYER_get_rbuf(rl) (&(rl)->rbuf)
|
||||
#define RECORD_LAYER_get_wbuf(rl) ((rl)->wbuf)
|
||||
#define RECORD_LAYER_get_rrec(rl) ((rl)->rrec)
|
||||
#define RECORD_LAYER_set_packet(rl, p) ((rl)->packet = (p))
|
||||
#define RECORD_LAYER_reset_packet_length(rl) ((rl)->packet_length = 0)
|
||||
#define RECORD_LAYER_get_rstate(rl) ((rl)->rstate)
|
||||
#define RECORD_LAYER_set_rstate(rl, st) ((rl)->rstate = (st))
|
||||
#define RECORD_LAYER_get_read_sequence(rl) ((rl)->read_sequence)
|
||||
#define RECORD_LAYER_get_write_sequence(rl) ((rl)->write_sequence)
|
||||
#define RECORD_LAYER_get_numrpipes(rl) ((rl)->numrpipes)
|
||||
#define RECORD_LAYER_set_numrpipes(rl, n) ((rl)->numrpipes = (n))
|
||||
#define RECORD_LAYER_inc_empty_record_count(rl) ((rl)->empty_record_count++)
|
||||
#define RECORD_LAYER_reset_empty_record_count(rl) \
|
||||
((rl)->empty_record_count = 0)
|
||||
#define RECORD_LAYER_get_empty_record_count(rl) ((rl)->empty_record_count)
|
||||
#define RECORD_LAYER_is_first_record(rl) ((rl)->is_first_record)
|
||||
#define RECORD_LAYER_set_first_record(rl) ((rl)->is_first_record = 1)
|
||||
#define RECORD_LAYER_clear_first_record(rl) ((rl)->is_first_record = 0)
|
||||
#define DTLS_RECORD_LAYER_get_r_epoch(rl) ((rl)->d->r_epoch)
|
||||
|
||||
__owur int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold);
|
||||
|
||||
void RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, const unsigned char *ws);
|
||||
DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
|
||||
unsigned int *is_next_epoch);
|
||||
int dtls1_process_buffered_records(SSL *s);
|
||||
int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue);
|
||||
int dtls1_buffer_record(SSL *s, record_pqueue *q, unsigned char *priority);
|
||||
void ssl3_record_sequence_update(unsigned char *seq);
|
||||
|
||||
/* Functions provided by the DTLS1_BITMAP component */
|
||||
|
||||
int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap);
|
||||
void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
|
||||
|
||||
/* Macros/functions provided by the SSL3_BUFFER component */
|
||||
|
||||
#define SSL3_BUFFER_get_buf(b) ((b)->buf)
|
||||
#define SSL3_BUFFER_set_buf(b, n) ((b)->buf = (n))
|
||||
#define SSL3_BUFFER_get_len(b) ((b)->len)
|
||||
#define SSL3_BUFFER_set_len(b, l) ((b)->len = (l))
|
||||
#define SSL3_BUFFER_get_left(b) ((b)->left)
|
||||
#define SSL3_BUFFER_set_left(b, l) ((b)->left = (l))
|
||||
#define SSL3_BUFFER_add_left(b, l) ((b)->left += (l))
|
||||
#define SSL3_BUFFER_get_offset(b) ((b)->offset)
|
||||
#define SSL3_BUFFER_set_offset(b, o) ((b)->offset = (o))
|
||||
#define SSL3_BUFFER_add_offset(b, o) ((b)->offset += (o))
|
||||
#define SSL3_BUFFER_is_initialised(b) ((b)->buf != NULL)
|
||||
#define SSL3_BUFFER_set_default_len(b, l) ((b)->default_len = (l))
|
||||
|
||||
void SSL3_BUFFER_clear(SSL3_BUFFER *b);
|
||||
void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, int n);
|
||||
void SSL3_BUFFER_release(SSL3_BUFFER *b);
|
||||
__owur int ssl3_setup_read_buffer(SSL *s);
|
||||
__owur int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t len);
|
||||
int ssl3_release_read_buffer(SSL *s);
|
||||
int ssl3_release_write_buffer(SSL *s);
|
||||
|
||||
/* Macros/functions provided by the SSL3_RECORD component */
|
||||
|
||||
#define SSL3_RECORD_get_type(r) ((r)->type)
|
||||
#define SSL3_RECORD_set_type(r, t) ((r)->type = (t))
|
||||
#define SSL3_RECORD_get_length(r) ((r)->length)
|
||||
#define SSL3_RECORD_set_length(r, l) ((r)->length = (l))
|
||||
#define SSL3_RECORD_add_length(r, l) ((r)->length += (l))
|
||||
#define SSL3_RECORD_sub_length(r, l) ((r)->length -= (l))
|
||||
#define SSL3_RECORD_get_data(r) ((r)->data)
|
||||
#define SSL3_RECORD_set_data(r, d) ((r)->data = (d))
|
||||
#define SSL3_RECORD_get_input(r) ((r)->input)
|
||||
#define SSL3_RECORD_set_input(r, i) ((r)->input = (i))
|
||||
#define SSL3_RECORD_reset_input(r) ((r)->input = (r)->data)
|
||||
#define SSL3_RECORD_get_seq_num(r) ((r)->seq_num)
|
||||
#define SSL3_RECORD_get_off(r) ((r)->off)
|
||||
#define SSL3_RECORD_set_off(r, o) ((r)->off = (o))
|
||||
#define SSL3_RECORD_add_off(r, o) ((r)->off += (o))
|
||||
#define SSL3_RECORD_get_epoch(r) ((r)->epoch)
|
||||
#define SSL3_RECORD_is_sslv2_record(r) \
|
||||
((r)->rec_version == SSL2_VERSION)
|
||||
#define SSL3_RECORD_is_read(r) ((r)->read)
|
||||
#define SSL3_RECORD_set_read(r) ((r)->read = 1)
|
||||
|
||||
void SSL3_RECORD_clear(SSL3_RECORD *r, unsigned int num_recs);
|
||||
void SSL3_RECORD_release(SSL3_RECORD *r, unsigned int num_recs);
|
||||
void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num);
|
||||
int ssl3_get_record(SSL *s);
|
||||
__owur int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr);
|
||||
__owur int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr);
|
||||
void ssl3_cbc_copy_mac(unsigned char *out,
|
||||
const SSL3_RECORD *rec, unsigned md_size);
|
||||
__owur int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size);
|
||||
__owur int tls1_cbc_remove_padding(const SSL *s,
|
||||
SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size);
|
||||
int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap);
|
||||
__owur int dtls1_get_record(SSL *s);
|
||||
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "../ssl_locl.h"
|
||||
#include "record_locl.h"
|
||||
|
||||
void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, int n)
|
||||
{
|
||||
if (d != NULL)
|
||||
memcpy(b->buf, d, n);
|
||||
b->left = n;
|
||||
b->offset = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clear the contents of an SSL3_BUFFER but retain any memory allocated. Also
|
||||
* retains the default_len setting
|
||||
*/
|
||||
void SSL3_BUFFER_clear(SSL3_BUFFER *b)
|
||||
{
|
||||
b->offset = 0;
|
||||
b->left = 0;
|
||||
}
|
||||
|
||||
void SSL3_BUFFER_release(SSL3_BUFFER *b)
|
||||
{
|
||||
OPENSSL_free(b->buf);
|
||||
b->buf = NULL;
|
||||
}
|
||||
|
||||
int ssl3_setup_read_buffer(SSL *s)
|
||||
{
|
||||
unsigned char *p;
|
||||
size_t len, align = 0, headerlen;
|
||||
SSL3_BUFFER *b;
|
||||
|
||||
b = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
|
||||
if (SSL_IS_DTLS(s))
|
||||
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
|
||||
if (b->default_len > len)
|
||||
len = b->default_len;
|
||||
if ((p = OPENSSL_malloc(len)) == NULL)
|
||||
goto err;
|
||||
b->buf = p;
|
||||
b->len = len;
|
||||
}
|
||||
|
||||
RECORD_LAYER_set_packet(&s->rlayer, &(b->buf[0]));
|
||||
return 1;
|
||||
|
||||
err:
|
||||
SSLerr(SSL_F_SSL3_SETUP_READ_BUFFER, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t len)
|
||||
{
|
||||
unsigned char *p;
|
||||
size_t align = 0, headerlen;
|
||||
SSL3_BUFFER *wb;
|
||||
unsigned int currpipe;
|
||||
|
||||
s->rlayer.numwpipes = numwpipes;
|
||||
|
||||
if (len == 0) {
|
||||
if (SSL_IS_DTLS(s))
|
||||
headerlen = DTLS1_RT_HEADER_LENGTH + 1;
|
||||
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
|
||||
|
||||
len = s->max_send_fragment
|
||||
+ SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
if (ssl_allow_compression(s))
|
||||
len += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
||||
#endif
|
||||
if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
|
||||
len += headerlen + align + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD;
|
||||
}
|
||||
|
||||
wb = RECORD_LAYER_get_wbuf(&s->rlayer);
|
||||
for (currpipe = 0; currpipe < numwpipes; currpipe++) {
|
||||
SSL3_BUFFER *thiswb = &wb[currpipe];
|
||||
|
||||
if (thiswb->buf == NULL) {
|
||||
p = OPENSSL_malloc(len);
|
||||
if (p == NULL) {
|
||||
s->rlayer.numwpipes = currpipe;
|
||||
goto err;
|
||||
}
|
||||
memset(thiswb, 0, sizeof(SSL3_BUFFER));
|
||||
thiswb->buf = p;
|
||||
thiswb->len = len;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (!ssl3_setup_write_buffer(s, 1, 0))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ssl3_release_write_buffer(SSL *s)
|
||||
{
|
||||
SSL3_BUFFER *wb;
|
||||
unsigned int pipes;
|
||||
|
||||
pipes = s->rlayer.numwpipes;
|
||||
while (pipes > 0) {
|
||||
wb = &RECORD_LAYER_get_wbuf(&s->rlayer)[pipes - 1];
|
||||
|
||||
OPENSSL_free(wb->buf);
|
||||
wb->buf = NULL;
|
||||
pipes--;
|
||||
}
|
||||
s->rlayer.numwpipes = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ssl3_release_read_buffer(SSL *s)
|
||||
{
|
||||
SSL3_BUFFER *b;
|
||||
|
||||
b = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
OPENSSL_free(b->buf);
|
||||
b->buf = NULL;
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,1641 @@
|
||||
/*
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include "../ssl_locl.h"
|
||||
#include "internal/constant_time_locl.h"
|
||||
#include <openssl/rand.h>
|
||||
#include "record_locl.h"
|
||||
|
||||
static const unsigned char ssl3_pad_1[48] = {
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
|
||||
};
|
||||
|
||||
static const unsigned char ssl3_pad_2[48] = {
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
|
||||
0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
|
||||
};
|
||||
|
||||
/*
|
||||
* Clear the contents of an SSL3_RECORD but retain any memory allocated
|
||||
*/
|
||||
void SSL3_RECORD_clear(SSL3_RECORD *r, unsigned int num_recs)
|
||||
{
|
||||
unsigned char *comp;
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < num_recs; i++) {
|
||||
comp = r[i].comp;
|
||||
|
||||
memset(&r[i], 0, sizeof(*r));
|
||||
r[i].comp = comp;
|
||||
}
|
||||
}
|
||||
|
||||
void SSL3_RECORD_release(SSL3_RECORD *r, unsigned int num_recs)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < num_recs; i++) {
|
||||
OPENSSL_free(r[i].comp);
|
||||
r[i].comp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
|
||||
{
|
||||
memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
|
||||
}
|
||||
|
||||
/*
|
||||
* Peeks ahead into "read_ahead" data to see if we have a whole record waiting
|
||||
* for us in the buffer.
|
||||
*/
|
||||
static int ssl3_record_app_data_waiting(SSL *s)
|
||||
{
|
||||
SSL3_BUFFER *rbuf;
|
||||
int left, len;
|
||||
unsigned char *p;
|
||||
|
||||
rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
|
||||
p = SSL3_BUFFER_get_buf(rbuf);
|
||||
if (p == NULL)
|
||||
return 0;
|
||||
|
||||
left = SSL3_BUFFER_get_left(rbuf);
|
||||
|
||||
if (left < SSL3_RT_HEADER_LENGTH)
|
||||
return 0;
|
||||
|
||||
p += SSL3_BUFFER_get_offset(rbuf);
|
||||
|
||||
/*
|
||||
* We only check the type and record length, we will sanity check version
|
||||
* etc later
|
||||
*/
|
||||
if (*p != SSL3_RT_APPLICATION_DATA)
|
||||
return 0;
|
||||
|
||||
p += 3;
|
||||
n2s(p, len);
|
||||
|
||||
if (left < SSL3_RT_HEADER_LENGTH + len)
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
|
||||
* will be processed per call to ssl3_get_record. Without this limit an
|
||||
* attacker could send empty records at a faster rate than we can process and
|
||||
* cause ssl3_get_record to loop forever.
|
||||
*/
|
||||
#define MAX_EMPTY_RECORDS 32
|
||||
|
||||
#define SSL2_RT_HEADER_LENGTH 2
|
||||
/*-
|
||||
* Call this to get new input records.
|
||||
* It will return <= 0 if more data is needed, normally due to an error
|
||||
* or non-blocking IO.
|
||||
* When it finishes, |numrpipes| records have been decoded. For each record 'i':
|
||||
* rr[i].type - is the type of record
|
||||
* rr[i].data, - data
|
||||
* rr[i].length, - number of bytes
|
||||
* Multiple records will only be returned if the record types are all
|
||||
* SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
|
||||
* |max_pipelines|
|
||||
*/
|
||||
/* used only by ssl3_read_bytes */
|
||||
int ssl3_get_record(SSL *s)
|
||||
{
|
||||
int ssl_major, ssl_minor, al;
|
||||
int enc_err, n, i, ret = -1;
|
||||
SSL3_RECORD *rr;
|
||||
SSL3_BUFFER *rbuf;
|
||||
SSL_SESSION *sess;
|
||||
unsigned char *p;
|
||||
unsigned char md[EVP_MAX_MD_SIZE];
|
||||
short version;
|
||||
unsigned mac_size;
|
||||
int imac_size;
|
||||
unsigned int num_recs = 0;
|
||||
unsigned int max_recs;
|
||||
unsigned int j;
|
||||
|
||||
rr = RECORD_LAYER_get_rrec(&s->rlayer);
|
||||
rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
max_recs = s->max_pipelines;
|
||||
if (max_recs == 0)
|
||||
max_recs = 1;
|
||||
sess = s->session;
|
||||
|
||||
do {
|
||||
/* check if we have the header */
|
||||
if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
|
||||
(RECORD_LAYER_get_packet_length(&s->rlayer)
|
||||
< SSL3_RT_HEADER_LENGTH)) {
|
||||
n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
|
||||
SSL3_BUFFER_get_len(rbuf), 0,
|
||||
num_recs == 0 ? 1 : 0);
|
||||
if (n <= 0)
|
||||
return (n); /* error or non-blocking */
|
||||
RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
|
||||
|
||||
p = RECORD_LAYER_get_packet(&s->rlayer);
|
||||
|
||||
/*
|
||||
* The first record received by the server may be a V2ClientHello.
|
||||
*/
|
||||
if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
|
||||
&& (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
|
||||
/*
|
||||
* SSLv2 style record
|
||||
*
|
||||
* |num_recs| here will actually always be 0 because
|
||||
* |num_recs > 0| only ever occurs when we are processing
|
||||
* multiple app data records - which we know isn't the case here
|
||||
* because it is an SSLv2ClientHello. We keep it using
|
||||
* |num_recs| for the sake of consistency
|
||||
*/
|
||||
rr[num_recs].type = SSL3_RT_HANDSHAKE;
|
||||
rr[num_recs].rec_version = SSL2_VERSION;
|
||||
|
||||
rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];
|
||||
|
||||
if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
|
||||
- SSL2_RT_HEADER_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {
|
||||
al = SSL_AD_HANDSHAKE_FAILURE;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
|
||||
goto f_err;
|
||||
}
|
||||
} else {
|
||||
/* SSLv3+ style record */
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
/* Pull apart the header into the SSL3_RECORD */
|
||||
rr[num_recs].type = *(p++);
|
||||
ssl_major = *(p++);
|
||||
ssl_minor = *(p++);
|
||||
version = (ssl_major << 8) | ssl_minor;
|
||||
rr[num_recs].rec_version = version;
|
||||
n2s(p, rr[num_recs].length);
|
||||
|
||||
/* Lets check version */
|
||||
if (!s->first_packet && version != s->version) {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
|
||||
if ((s->version & 0xFF00) == (version & 0xFF00)
|
||||
&& !s->enc_write_ctx && !s->write_hash) {
|
||||
if (rr->type == SSL3_RT_ALERT) {
|
||||
/*
|
||||
* The record is using an incorrect version number,
|
||||
* but what we've got appears to be an alert. We
|
||||
* haven't read the body yet to check whether its a
|
||||
* fatal or not - but chances are it is. We probably
|
||||
* shouldn't send a fatal alert back. We'll just
|
||||
* end.
|
||||
*/
|
||||
goto err;
|
||||
}
|
||||
/*
|
||||
* Send back error using their minor version number :-)
|
||||
*/
|
||||
s->version = (unsigned short)version;
|
||||
}
|
||||
al = SSL_AD_PROTOCOL_VERSION;
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if ((version >> 8) != SSL3_VERSION_MAJOR) {
|
||||
if (RECORD_LAYER_is_first_record(&s->rlayer)) {
|
||||
/* Go back to start of packet, look at the five bytes
|
||||
* that we have. */
|
||||
p = RECORD_LAYER_get_packet(&s->rlayer);
|
||||
if (strncmp((char *)p, "GET ", 4) == 0 ||
|
||||
strncmp((char *)p, "POST ", 5) == 0 ||
|
||||
strncmp((char *)p, "HEAD ", 5) == 0 ||
|
||||
strncmp((char *)p, "PUT ", 4) == 0) {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
|
||||
goto err;
|
||||
} else if (strncmp((char *)p, "CONNE", 5) == 0) {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_HTTPS_PROXY_REQUEST);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* Doesn't look like TLS - don't send an alert */
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
goto err;
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
al = SSL_AD_PROTOCOL_VERSION;
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
|
||||
if (rr[num_recs].length >
|
||||
SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
|
||||
/* now s->rlayer.rstate == SSL_ST_READ_BODY */
|
||||
}
|
||||
|
||||
/*
|
||||
* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
|
||||
* Calculate how much more data we need to read for the rest of the
|
||||
* record
|
||||
*/
|
||||
if (rr[num_recs].rec_version == SSL2_VERSION) {
|
||||
i = rr[num_recs].length + SSL2_RT_HEADER_LENGTH
|
||||
- SSL3_RT_HEADER_LENGTH;
|
||||
} else {
|
||||
i = rr[num_recs].length;
|
||||
}
|
||||
if (i > 0) {
|
||||
/* now s->packet_length == SSL3_RT_HEADER_LENGTH */
|
||||
|
||||
n = ssl3_read_n(s, i, i, 1, 0);
|
||||
if (n <= 0)
|
||||
return (n); /* error or non-blocking io */
|
||||
}
|
||||
|
||||
/* set state for later operations */
|
||||
RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
|
||||
|
||||
/*
|
||||
* At this point, s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length,
|
||||
* or s->packet_length == SSL2_RT_HEADER_LENGTH + rr->length
|
||||
* and we have that many bytes in s->packet
|
||||
*/
|
||||
if (rr[num_recs].rec_version == SSL2_VERSION) {
|
||||
rr[num_recs].input =
|
||||
&(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
|
||||
} else {
|
||||
rr[num_recs].input =
|
||||
&(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
|
||||
}
|
||||
|
||||
/*
|
||||
* ok, we can now read from 's->packet' data into 'rr' rr->input points
|
||||
* at rr->length bytes, which need to be copied into rr->data by either
|
||||
* the decryption or by the decompression When the data is 'copied' into
|
||||
* the rr->data buffer, rr->input will be pointed at the new buffer
|
||||
*/
|
||||
|
||||
/*
|
||||
* We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
|
||||
* bytes of encrypted compressed stuff.
|
||||
*/
|
||||
|
||||
/* check is not needed I believe */
|
||||
if (rr[num_recs].length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
/* decrypt in place in 'rr->input' */
|
||||
rr[num_recs].data = rr[num_recs].input;
|
||||
rr[num_recs].orig_len = rr[num_recs].length;
|
||||
|
||||
/* Mark this record as not read by upper layers yet */
|
||||
rr[num_recs].read = 0;
|
||||
|
||||
num_recs++;
|
||||
|
||||
/* we have pulled in a full packet so zero things */
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
RECORD_LAYER_clear_first_record(&s->rlayer);
|
||||
} while (num_recs < max_recs
|
||||
&& rr[num_recs - 1].type == SSL3_RT_APPLICATION_DATA
|
||||
&& SSL_USE_EXPLICIT_IV(s)
|
||||
&& s->enc_read_ctx != NULL
|
||||
&& (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
|
||||
& EVP_CIPH_FLAG_PIPELINE)
|
||||
&& ssl3_record_app_data_waiting(s));
|
||||
|
||||
/*
|
||||
* If in encrypt-then-mac mode calculate mac from encrypted record. All
|
||||
* the details below are public so no timing details can leak.
|
||||
*/
|
||||
if (SSL_READ_ETM(s) && s->read_hash) {
|
||||
unsigned char *mac;
|
||||
|
||||
imac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE);
|
||||
if (imac_size < 0 || imac_size > EVP_MAX_MD_SIZE) {
|
||||
al = SSL_AD_INTERNAL_ERROR;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, ERR_LIB_EVP);
|
||||
goto f_err;
|
||||
}
|
||||
mac_size = (unsigned)imac_size;
|
||||
|
||||
for (j = 0; j < num_recs; j++) {
|
||||
if (rr[j].length < mac_size) {
|
||||
al = SSL_AD_DECODE_ERROR;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
|
||||
goto f_err;
|
||||
}
|
||||
rr[j].length -= mac_size;
|
||||
mac = rr[j].data + rr[j].length;
|
||||
i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
|
||||
if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
|
||||
al = SSL_AD_BAD_RECORD_MAC;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
|
||||
/*-
|
||||
* enc_err is:
|
||||
* 0: (in non-constant time) if the record is publically invalid.
|
||||
* 1: if the padding is valid
|
||||
* -1: if the padding is invalid
|
||||
*/
|
||||
if (enc_err == 0) {
|
||||
al = SSL_AD_DECRYPTION_FAILED;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
|
||||
goto f_err;
|
||||
}
|
||||
#ifdef SSL_DEBUG
|
||||
printf("dec %d\n", rr->length);
|
||||
{
|
||||
unsigned int z;
|
||||
for (z = 0; z < rr->length; z++)
|
||||
printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
/* r->length is now the compressed data plus mac */
|
||||
if ((sess != NULL) &&
|
||||
(s->enc_read_ctx != NULL) &&
|
||||
(!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
|
||||
/* s->read_hash != NULL => mac_size != -1 */
|
||||
unsigned char *mac = NULL;
|
||||
unsigned char mac_tmp[EVP_MAX_MD_SIZE];
|
||||
|
||||
mac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
|
||||
|
||||
for (j = 0; j < num_recs; j++) {
|
||||
/*
|
||||
* orig_len is the length of the record before any padding was
|
||||
* removed. This is public information, as is the MAC in use,
|
||||
* therefore we can safely process the record in a different amount
|
||||
* of time if it's too short to possibly contain a MAC.
|
||||
*/
|
||||
if (rr[j].orig_len < mac_size ||
|
||||
/* CBC records must have a padding length byte too. */
|
||||
(EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
rr[j].orig_len < mac_size + 1)) {
|
||||
al = SSL_AD_DECODE_ERROR;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
|
||||
/*
|
||||
* We update the length so that the TLS header bytes can be
|
||||
* constructed correctly but we need to extract the MAC in
|
||||
* constant time from within the record, without leaking the
|
||||
* contents of the padding bytes.
|
||||
*/
|
||||
mac = mac_tmp;
|
||||
ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
|
||||
rr[j].length -= mac_size;
|
||||
} else {
|
||||
/*
|
||||
* In this case there's no padding, so |rec->orig_len| equals
|
||||
* |rec->length| and we checked that there's enough bytes for
|
||||
* |mac_size| above.
|
||||
*/
|
||||
rr[j].length -= mac_size;
|
||||
mac = &rr[j].data[rr[j].length];
|
||||
}
|
||||
|
||||
i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
|
||||
if (i < 0 || mac == NULL
|
||||
|| CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
|
||||
enc_err = -1;
|
||||
if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
|
||||
enc_err = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (enc_err < 0) {
|
||||
/*
|
||||
* A separate 'decryption_failed' alert was introduced with TLS 1.0,
|
||||
* SSL 3.0 only has 'bad_record_mac'. But unless a decryption
|
||||
* failure is directly visible from the ciphertext anyway, we should
|
||||
* not reveal which kind of error occurred -- this might become
|
||||
* visible to an attacker (e.g. via a logfile)
|
||||
*/
|
||||
al = SSL_AD_BAD_RECORD_MAC;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
for (j = 0; j < num_recs; j++) {
|
||||
/* rr[j].length is now just compressed */
|
||||
if (s->expand != NULL) {
|
||||
if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
if (!ssl3_do_uncompress(s, &rr[j])) {
|
||||
al = SSL_AD_DECOMPRESSION_FAILURE;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
|
||||
if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
rr[j].off = 0;
|
||||
/*-
|
||||
* So at this point the following is true
|
||||
* rr[j].type is the type of record
|
||||
* rr[j].length == number of bytes in record
|
||||
* rr[j].off == offset to first valid byte
|
||||
* rr[j].data == where to take bytes from, increment after use :-).
|
||||
*/
|
||||
|
||||
/* just read a 0 length packet */
|
||||
if (rr[j].length == 0) {
|
||||
RECORD_LAYER_inc_empty_record_count(&s->rlayer);
|
||||
if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
|
||||
> MAX_EMPTY_RECORDS) {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
|
||||
goto f_err;
|
||||
}
|
||||
} else {
|
||||
RECORD_LAYER_reset_empty_record_count(&s->rlayer);
|
||||
}
|
||||
}
|
||||
|
||||
RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
|
||||
return 1;
|
||||
|
||||
f_err:
|
||||
ssl3_send_alert(s, SSL3_AL_FATAL, al);
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
|
||||
{
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
int i;
|
||||
|
||||
if (rr->comp == NULL) {
|
||||
rr->comp = (unsigned char *)
|
||||
OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
|
||||
}
|
||||
if (rr->comp == NULL)
|
||||
return 0;
|
||||
|
||||
i = COMP_expand_block(ssl->expand, rr->comp,
|
||||
SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
|
||||
if (i < 0)
|
||||
return 0;
|
||||
else
|
||||
rr->length = i;
|
||||
rr->data = rr->comp;
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
|
||||
{
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
int i;
|
||||
|
||||
i = COMP_compress_block(ssl->compress, wr->data,
|
||||
SSL3_RT_MAX_COMPRESSED_LENGTH,
|
||||
wr->input, (int)wr->length);
|
||||
if (i < 0)
|
||||
return (0);
|
||||
else
|
||||
wr->length = i;
|
||||
|
||||
wr->input = wr->data;
|
||||
#endif
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*-
|
||||
* ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
|
||||
*
|
||||
* Returns:
|
||||
* 0: (in non-constant time) if the record is publically invalid (i.e. too
|
||||
* short etc).
|
||||
* 1: if the record's padding is valid / the encryption was successful.
|
||||
* -1: if the record's padding is invalid or, if sending, an internal error
|
||||
* occurred.
|
||||
*/
|
||||
int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int sending)
|
||||
{
|
||||
SSL3_RECORD *rec;
|
||||
EVP_CIPHER_CTX *ds;
|
||||
unsigned long l;
|
||||
int bs, i, mac_size = 0;
|
||||
const EVP_CIPHER *enc;
|
||||
|
||||
rec = inrecs;
|
||||
/*
|
||||
* We shouldn't ever be called with more than one record in the SSLv3 case
|
||||
*/
|
||||
if (n_recs != 1)
|
||||
return 0;
|
||||
if (sending) {
|
||||
ds = s->enc_write_ctx;
|
||||
if (s->enc_write_ctx == NULL)
|
||||
enc = NULL;
|
||||
else
|
||||
enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
|
||||
} else {
|
||||
ds = s->enc_read_ctx;
|
||||
if (s->enc_read_ctx == NULL)
|
||||
enc = NULL;
|
||||
else
|
||||
enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
|
||||
}
|
||||
|
||||
if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
|
||||
memmove(rec->data, rec->input, rec->length);
|
||||
rec->input = rec->data;
|
||||
} else {
|
||||
l = rec->length;
|
||||
bs = EVP_CIPHER_CTX_block_size(ds);
|
||||
|
||||
/* COMPRESS */
|
||||
|
||||
if ((bs != 1) && sending) {
|
||||
i = bs - ((int)l % bs);
|
||||
|
||||
/* we need to add 'i-1' padding bytes */
|
||||
l += i;
|
||||
/*
|
||||
* the last of these zero bytes will be overwritten with the
|
||||
* padding length.
|
||||
*/
|
||||
memset(&rec->input[rec->length], 0, i);
|
||||
rec->length += i;
|
||||
rec->input[l - 1] = (i - 1);
|
||||
}
|
||||
|
||||
if (!sending) {
|
||||
if (l == 0 || l % bs != 0)
|
||||
return 0;
|
||||
/* otherwise, rec->length >= bs */
|
||||
}
|
||||
|
||||
if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
|
||||
return -1;
|
||||
|
||||
if (EVP_MD_CTX_md(s->read_hash) != NULL)
|
||||
mac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if ((bs != 1) && !sending)
|
||||
return ssl3_cbc_remove_padding(rec, bs, mac_size);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*-
|
||||
* tls1_enc encrypts/decrypts |n_recs| in |recs|.
|
||||
*
|
||||
* Returns:
|
||||
* 0: (in non-constant time) if the record is publically invalid (i.e. too
|
||||
* short etc).
|
||||
* 1: if the record's padding is valid / the encryption was successful.
|
||||
* -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
|
||||
* an internal error occurred.
|
||||
*/
|
||||
int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
{
|
||||
EVP_CIPHER_CTX *ds;
|
||||
size_t reclen[SSL_MAX_PIPELINES];
|
||||
unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
|
||||
int bs, i, j, k, pad = 0, ret, mac_size = 0;
|
||||
const EVP_CIPHER *enc;
|
||||
unsigned int ctr;
|
||||
|
||||
if (n_recs == 0)
|
||||
return 0;
|
||||
|
||||
if (sending) {
|
||||
if (EVP_MD_CTX_md(s->write_hash)) {
|
||||
int n = EVP_MD_CTX_size(s->write_hash);
|
||||
OPENSSL_assert(n >= 0);
|
||||
}
|
||||
ds = s->enc_write_ctx;
|
||||
if (s->enc_write_ctx == NULL)
|
||||
enc = NULL;
|
||||
else {
|
||||
int ivlen;
|
||||
enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
|
||||
/* For TLSv1.1 and later explicit IV */
|
||||
if (SSL_USE_EXPLICIT_IV(s)
|
||||
&& EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
|
||||
ivlen = EVP_CIPHER_iv_length(enc);
|
||||
else
|
||||
ivlen = 0;
|
||||
if (ivlen > 1) {
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
if (recs[ctr].data != recs[ctr].input) {
|
||||
/*
|
||||
* we can't write into the input stream: Can this ever
|
||||
* happen?? (steve)
|
||||
*/
|
||||
SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
} else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
|
||||
SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (EVP_MD_CTX_md(s->read_hash)) {
|
||||
int n = EVP_MD_CTX_size(s->read_hash);
|
||||
OPENSSL_assert(n >= 0);
|
||||
}
|
||||
ds = s->enc_read_ctx;
|
||||
if (s->enc_read_ctx == NULL)
|
||||
enc = NULL;
|
||||
else
|
||||
enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
|
||||
}
|
||||
|
||||
if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
|
||||
recs[ctr].input = recs[ctr].data;
|
||||
}
|
||||
ret = 1;
|
||||
} else {
|
||||
bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
|
||||
|
||||
if (n_recs > 1) {
|
||||
if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
|
||||
& EVP_CIPH_FLAG_PIPELINE)) {
|
||||
/*
|
||||
* We shouldn't have been called with pipeline data if the
|
||||
* cipher doesn't support pipelining
|
||||
*/
|
||||
SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
reclen[ctr] = recs[ctr].length;
|
||||
|
||||
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
|
||||
& EVP_CIPH_FLAG_AEAD_CIPHER) {
|
||||
unsigned char *seq;
|
||||
|
||||
seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
|
||||
: RECORD_LAYER_get_read_sequence(&s->rlayer);
|
||||
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
/* DTLS does not support pipelining */
|
||||
unsigned char dtlsseq[9], *p = dtlsseq;
|
||||
|
||||
s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
|
||||
DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
|
||||
memcpy(p, &seq[2], 6);
|
||||
memcpy(buf[ctr], dtlsseq, 8);
|
||||
} else {
|
||||
memcpy(buf[ctr], seq, 8);
|
||||
for (i = 7; i >= 0; i--) { /* increment */
|
||||
++seq[i];
|
||||
if (seq[i] != 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
buf[ctr][8] = recs[ctr].type;
|
||||
buf[ctr][9] = (unsigned char)(s->version >> 8);
|
||||
buf[ctr][10] = (unsigned char)(s->version);
|
||||
buf[ctr][11] = recs[ctr].length >> 8;
|
||||
buf[ctr][12] = recs[ctr].length & 0xff;
|
||||
pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
|
||||
EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
|
||||
if (pad <= 0)
|
||||
return -1;
|
||||
|
||||
if (sending) {
|
||||
reclen[ctr] += pad;
|
||||
recs[ctr].length += pad;
|
||||
}
|
||||
|
||||
} else if ((bs != 1) && sending) {
|
||||
i = bs - ((int)reclen[ctr] % bs);
|
||||
|
||||
/* Add weird padding of upto 256 bytes */
|
||||
|
||||
/* we need to add 'i' padding bytes of value j */
|
||||
j = i - 1;
|
||||
for (k = (int)reclen[ctr]; k < (int)(reclen[ctr] + i); k++)
|
||||
recs[ctr].input[k] = j;
|
||||
reclen[ctr] += i;
|
||||
recs[ctr].length += i;
|
||||
}
|
||||
|
||||
if (!sending) {
|
||||
if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (n_recs > 1) {
|
||||
unsigned char *data[SSL_MAX_PIPELINES];
|
||||
|
||||
/* Set the output buffers */
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
data[ctr] = recs[ctr].data;
|
||||
}
|
||||
if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
|
||||
n_recs, data) <= 0) {
|
||||
SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
|
||||
}
|
||||
/* Set the input buffers */
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
data[ctr] = recs[ctr].input;
|
||||
}
|
||||
if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
|
||||
n_recs, data) <= 0
|
||||
|| EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
|
||||
n_recs, reclen) <= 0) {
|
||||
SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
i = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
|
||||
if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
|
||||
& EVP_CIPH_FLAG_CUSTOM_CIPHER)
|
||||
? (i < 0)
|
||||
: (i == 0))
|
||||
return -1; /* AEAD can fail to verify MAC */
|
||||
if (sending == 0) {
|
||||
if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
}
|
||||
} else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
if (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)
|
||||
mac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if ((bs != 1) && !sending) {
|
||||
int tmpret;
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
|
||||
/*
|
||||
* If tmpret == 0 then this means publicly invalid so we can
|
||||
* short circuit things here. Otherwise we must respect constant
|
||||
* time behaviour.
|
||||
*/
|
||||
if (tmpret == 0)
|
||||
return 0;
|
||||
ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
|
||||
ret, -1);
|
||||
}
|
||||
}
|
||||
if (pad && !sending) {
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
recs[ctr].length -= pad;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
{
|
||||
unsigned char *mac_sec, *seq;
|
||||
const EVP_MD_CTX *hash;
|
||||
unsigned char *p, rec_char;
|
||||
size_t md_size;
|
||||
int npad;
|
||||
int t;
|
||||
|
||||
if (sending) {
|
||||
mac_sec = &(ssl->s3->write_mac_secret[0]);
|
||||
seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
|
||||
hash = ssl->write_hash;
|
||||
} else {
|
||||
mac_sec = &(ssl->s3->read_mac_secret[0]);
|
||||
seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
|
||||
hash = ssl->read_hash;
|
||||
}
|
||||
|
||||
t = EVP_MD_CTX_size(hash);
|
||||
if (t < 0)
|
||||
return -1;
|
||||
md_size = t;
|
||||
npad = (48 / md_size) * md_size;
|
||||
|
||||
if (!sending &&
|
||||
EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
ssl3_cbc_record_digest_supported(hash)) {
|
||||
/*
|
||||
* This is a CBC-encrypted record. We must avoid leaking any
|
||||
* timing-side channel information about how many blocks of data we
|
||||
* are hashing because that gives an attacker a timing-oracle.
|
||||
*/
|
||||
|
||||
/*-
|
||||
* npad is, at most, 48 bytes and that's with MD5:
|
||||
* 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
|
||||
*
|
||||
* With SHA-1 (the largest hash speced for SSLv3) the hash size
|
||||
* goes up 4, but npad goes down by 8, resulting in a smaller
|
||||
* total size.
|
||||
*/
|
||||
unsigned char header[75];
|
||||
unsigned j = 0;
|
||||
memcpy(header + j, mac_sec, md_size);
|
||||
j += md_size;
|
||||
memcpy(header + j, ssl3_pad_1, npad);
|
||||
j += npad;
|
||||
memcpy(header + j, seq, 8);
|
||||
j += 8;
|
||||
header[j++] = rec->type;
|
||||
header[j++] = rec->length >> 8;
|
||||
header[j++] = rec->length & 0xff;
|
||||
|
||||
/* Final param == is SSLv3 */
|
||||
if (ssl3_cbc_digest_record(hash,
|
||||
md, &md_size,
|
||||
header, rec->input,
|
||||
rec->length + md_size, rec->orig_len,
|
||||
mac_sec, md_size, 1) <= 0)
|
||||
return -1;
|
||||
} else {
|
||||
unsigned int md_size_u;
|
||||
/* Chop the digest off the end :-) */
|
||||
EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
|
||||
|
||||
if (md_ctx == NULL)
|
||||
return -1;
|
||||
|
||||
rec_char = rec->type;
|
||||
p = md;
|
||||
s2n(rec->length, p);
|
||||
if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, seq, 8) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, md, 2) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
|
||||
|| EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
|
||||
|| EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
|
||||
|| EVP_DigestUpdate(md_ctx, md, md_size) <= 0
|
||||
|| EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
|
||||
EVP_MD_CTX_free(md_ctx);
|
||||
return -1;
|
||||
}
|
||||
md_size = md_size_u;
|
||||
|
||||
EVP_MD_CTX_free(md_ctx);
|
||||
}
|
||||
|
||||
ssl3_record_sequence_update(seq);
|
||||
return (md_size);
|
||||
}
|
||||
|
||||
int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
{
|
||||
unsigned char *seq;
|
||||
EVP_MD_CTX *hash;
|
||||
size_t md_size;
|
||||
int i;
|
||||
EVP_MD_CTX *hmac = NULL, *mac_ctx;
|
||||
unsigned char header[13];
|
||||
int stream_mac = (sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
|
||||
: (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
|
||||
int t;
|
||||
|
||||
if (sending) {
|
||||
seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
|
||||
hash = ssl->write_hash;
|
||||
} else {
|
||||
seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
|
||||
hash = ssl->read_hash;
|
||||
}
|
||||
|
||||
t = EVP_MD_CTX_size(hash);
|
||||
OPENSSL_assert(t >= 0);
|
||||
md_size = t;
|
||||
|
||||
/* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
|
||||
if (stream_mac) {
|
||||
mac_ctx = hash;
|
||||
} else {
|
||||
hmac = EVP_MD_CTX_new();
|
||||
if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
|
||||
EVP_MD_CTX_free(hmac);
|
||||
return -1;
|
||||
}
|
||||
mac_ctx = hmac;
|
||||
}
|
||||
|
||||
if (SSL_IS_DTLS(ssl)) {
|
||||
unsigned char dtlsseq[8], *p = dtlsseq;
|
||||
|
||||
s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
|
||||
DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
|
||||
memcpy(p, &seq[2], 6);
|
||||
|
||||
memcpy(header, dtlsseq, 8);
|
||||
} else
|
||||
memcpy(header, seq, 8);
|
||||
|
||||
header[8] = rec->type;
|
||||
header[9] = (unsigned char)(ssl->version >> 8);
|
||||
header[10] = (unsigned char)(ssl->version);
|
||||
header[11] = (rec->length) >> 8;
|
||||
header[12] = (rec->length) & 0xff;
|
||||
|
||||
if (!sending && !SSL_READ_ETM(ssl) &&
|
||||
EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
ssl3_cbc_record_digest_supported(mac_ctx)) {
|
||||
/*
|
||||
* This is a CBC-encrypted record. We must avoid leaking any
|
||||
* timing-side channel information about how many blocks of data we
|
||||
* are hashing because that gives an attacker a timing-oracle.
|
||||
*/
|
||||
/* Final param == not SSLv3 */
|
||||
if (ssl3_cbc_digest_record(mac_ctx,
|
||||
md, &md_size,
|
||||
header, rec->input,
|
||||
rec->length + md_size, rec->orig_len,
|
||||
ssl->s3->read_mac_secret,
|
||||
ssl->s3->read_mac_secret_size, 0) <= 0) {
|
||||
EVP_MD_CTX_free(hmac);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
|
||||
|| EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
|
||||
|| EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
|
||||
EVP_MD_CTX_free(hmac);
|
||||
return -1;
|
||||
}
|
||||
if (!sending && !SSL_READ_ETM(ssl) && FIPS_mode())
|
||||
if (!tls_fips_digest_extra(ssl->enc_read_ctx,
|
||||
mac_ctx, rec->input,
|
||||
rec->length, rec->orig_len)) {
|
||||
EVP_MD_CTX_free(hmac);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
EVP_MD_CTX_free(hmac);
|
||||
|
||||
#ifdef SSL_DEBUG
|
||||
fprintf(stderr, "seq=");
|
||||
{
|
||||
int z;
|
||||
for (z = 0; z < 8; z++)
|
||||
fprintf(stderr, "%02X ", seq[z]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
fprintf(stderr, "rec=");
|
||||
{
|
||||
unsigned int z;
|
||||
for (z = 0; z < rec->length; z++)
|
||||
fprintf(stderr, "%02X ", rec->data[z]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!SSL_IS_DTLS(ssl)) {
|
||||
for (i = 7; i >= 0; i--) {
|
||||
++seq[i];
|
||||
if (seq[i] != 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
#ifdef SSL_DEBUG
|
||||
{
|
||||
unsigned int z;
|
||||
for (z = 0; z < md_size; z++)
|
||||
fprintf(stderr, "%02X ", md[z]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
return (md_size);
|
||||
}
|
||||
|
||||
/*-
|
||||
* ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
|
||||
* record in |rec| by updating |rec->length| in constant time.
|
||||
*
|
||||
* block_size: the block size of the cipher used to encrypt the record.
|
||||
* returns:
|
||||
* 0: (in non-constant time) if the record is publicly invalid.
|
||||
* 1: if the padding was valid
|
||||
* -1: otherwise.
|
||||
*/
|
||||
int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size)
|
||||
{
|
||||
unsigned padding_length, good;
|
||||
const unsigned overhead = 1 /* padding length byte */ + mac_size;
|
||||
|
||||
/*
|
||||
* These lengths are all public so we can test them in non-constant time.
|
||||
*/
|
||||
if (overhead > rec->length)
|
||||
return 0;
|
||||
|
||||
padding_length = rec->data[rec->length - 1];
|
||||
good = constant_time_ge(rec->length, padding_length + overhead);
|
||||
/* SSLv3 requires that the padding is minimal. */
|
||||
good &= constant_time_ge(block_size, padding_length + 1);
|
||||
rec->length -= good & (padding_length + 1);
|
||||
return constant_time_select_int(good, 1, -1);
|
||||
}
|
||||
|
||||
/*-
|
||||
* tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
|
||||
* record in |rec| in constant time and returns 1 if the padding is valid and
|
||||
* -1 otherwise. It also removes any explicit IV from the start of the record
|
||||
* without leaking any timing about whether there was enough space after the
|
||||
* padding was removed.
|
||||
*
|
||||
* block_size: the block size of the cipher used to encrypt the record.
|
||||
* returns:
|
||||
* 0: (in non-constant time) if the record is publicly invalid.
|
||||
* 1: if the padding was valid
|
||||
* -1: otherwise.
|
||||
*/
|
||||
int tls1_cbc_remove_padding(const SSL *s,
|
||||
SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size)
|
||||
{
|
||||
unsigned padding_length, good, to_check, i;
|
||||
const unsigned overhead = 1 /* padding length byte */ + mac_size;
|
||||
/* Check if version requires explicit IV */
|
||||
if (SSL_USE_EXPLICIT_IV(s)) {
|
||||
/*
|
||||
* These lengths are all public so we can test them in non-constant
|
||||
* time.
|
||||
*/
|
||||
if (overhead + block_size > rec->length)
|
||||
return 0;
|
||||
/* We can now safely skip explicit IV */
|
||||
rec->data += block_size;
|
||||
rec->input += block_size;
|
||||
rec->length -= block_size;
|
||||
rec->orig_len -= block_size;
|
||||
} else if (overhead > rec->length)
|
||||
return 0;
|
||||
|
||||
padding_length = rec->data[rec->length - 1];
|
||||
|
||||
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
|
||||
EVP_CIPH_FLAG_AEAD_CIPHER) {
|
||||
/* padding is already verified */
|
||||
rec->length -= padding_length + 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
good = constant_time_ge(rec->length, overhead + padding_length);
|
||||
/*
|
||||
* The padding consists of a length byte at the end of the record and
|
||||
* then that many bytes of padding, all with the same value as the length
|
||||
* byte. Thus, with the length byte included, there are i+1 bytes of
|
||||
* padding. We can't check just |padding_length+1| bytes because that
|
||||
* leaks decrypted information. Therefore we always have to check the
|
||||
* maximum amount of padding possible. (Again, the length of the record
|
||||
* is public information so we can use it.)
|
||||
*/
|
||||
to_check = 256; /* maximum amount of padding, inc length byte. */
|
||||
if (to_check > rec->length)
|
||||
to_check = rec->length;
|
||||
|
||||
for (i = 0; i < to_check; i++) {
|
||||
unsigned char mask = constant_time_ge_8(padding_length, i);
|
||||
unsigned char b = rec->data[rec->length - 1 - i];
|
||||
/*
|
||||
* The final |padding_length+1| bytes should all have the value
|
||||
* |padding_length|. Therefore the XOR should be zero.
|
||||
*/
|
||||
good &= ~(mask & (padding_length ^ b));
|
||||
}
|
||||
|
||||
/*
|
||||
* If any of the final |padding_length+1| bytes had the wrong value, one
|
||||
* or more of the lower eight bits of |good| will be cleared.
|
||||
*/
|
||||
good = constant_time_eq(0xff, good & 0xff);
|
||||
rec->length -= good & (padding_length + 1);
|
||||
|
||||
return constant_time_select_int(good, 1, -1);
|
||||
}
|
||||
|
||||
/*-
|
||||
* ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
|
||||
* constant time (independent of the concrete value of rec->length, which may
|
||||
* vary within a 256-byte window).
|
||||
*
|
||||
* ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
|
||||
* this function.
|
||||
*
|
||||
* On entry:
|
||||
* rec->orig_len >= md_size
|
||||
* md_size <= EVP_MAX_MD_SIZE
|
||||
*
|
||||
* If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
|
||||
* variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
|
||||
* a single or pair of cache-lines, then the variable memory accesses don't
|
||||
* actually affect the timing. CPUs with smaller cache-lines [if any] are
|
||||
* not multi-core and are not considered vulnerable to cache-timing attacks.
|
||||
*/
|
||||
#define CBC_MAC_ROTATE_IN_PLACE
|
||||
|
||||
void ssl3_cbc_copy_mac(unsigned char *out,
|
||||
const SSL3_RECORD *rec, unsigned md_size)
|
||||
{
|
||||
#if defined(CBC_MAC_ROTATE_IN_PLACE)
|
||||
unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
|
||||
unsigned char *rotated_mac;
|
||||
#else
|
||||
unsigned char rotated_mac[EVP_MAX_MD_SIZE];
|
||||
#endif
|
||||
|
||||
/*
|
||||
* mac_end is the index of |rec->data| just after the end of the MAC.
|
||||
*/
|
||||
unsigned mac_end = rec->length;
|
||||
unsigned mac_start = mac_end - md_size;
|
||||
unsigned in_mac;
|
||||
/*
|
||||
* scan_start contains the number of bytes that we can ignore because the
|
||||
* MAC's position can only vary by 255 bytes.
|
||||
*/
|
||||
unsigned scan_start = 0;
|
||||
unsigned i, j;
|
||||
unsigned rotate_offset;
|
||||
|
||||
OPENSSL_assert(rec->orig_len >= md_size);
|
||||
OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
|
||||
|
||||
#if defined(CBC_MAC_ROTATE_IN_PLACE)
|
||||
rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
|
||||
#endif
|
||||
|
||||
/* This information is public so it's safe to branch based on it. */
|
||||
if (rec->orig_len > md_size + 255 + 1)
|
||||
scan_start = rec->orig_len - (md_size + 255 + 1);
|
||||
|
||||
in_mac = 0;
|
||||
rotate_offset = 0;
|
||||
memset(rotated_mac, 0, md_size);
|
||||
for (i = scan_start, j = 0; i < rec->orig_len; i++) {
|
||||
unsigned mac_started = constant_time_eq(i, mac_start);
|
||||
unsigned mac_ended = constant_time_lt(i, mac_end);
|
||||
unsigned char b = rec->data[i];
|
||||
|
||||
in_mac |= mac_started;
|
||||
in_mac &= mac_ended;
|
||||
rotate_offset |= j & mac_started;
|
||||
rotated_mac[j++] |= b & in_mac;
|
||||
j &= constant_time_lt(j, md_size);
|
||||
}
|
||||
|
||||
/* Now rotate the MAC */
|
||||
#if defined(CBC_MAC_ROTATE_IN_PLACE)
|
||||
j = 0;
|
||||
for (i = 0; i < md_size; i++) {
|
||||
/* in case cache-line is 32 bytes, touch second line */
|
||||
((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
|
||||
out[j++] = rotated_mac[rotate_offset++];
|
||||
rotate_offset &= constant_time_lt(rotate_offset, md_size);
|
||||
}
|
||||
#else
|
||||
memset(out, 0, md_size);
|
||||
rotate_offset = md_size - rotate_offset;
|
||||
rotate_offset &= constant_time_lt(rotate_offset, md_size);
|
||||
for (i = 0; i < md_size; i++) {
|
||||
for (j = 0; j < md_size; j++)
|
||||
out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
|
||||
rotate_offset++;
|
||||
rotate_offset &= constant_time_lt(rotate_offset, md_size);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
{
|
||||
int i, al;
|
||||
int enc_err;
|
||||
SSL_SESSION *sess;
|
||||
SSL3_RECORD *rr;
|
||||
unsigned int mac_size;
|
||||
unsigned char md[EVP_MAX_MD_SIZE];
|
||||
|
||||
rr = RECORD_LAYER_get_rrec(&s->rlayer);
|
||||
sess = s->session;
|
||||
|
||||
/*
|
||||
* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
|
||||
* and we have that many bytes in s->packet
|
||||
*/
|
||||
rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
|
||||
|
||||
/*
|
||||
* ok, we can now read from 's->packet' data into 'rr' rr->input points
|
||||
* at rr->length bytes, which need to be copied into rr->data by either
|
||||
* the decryption or by the decompression When the data is 'copied' into
|
||||
* the rr->data buffer, rr->input will be pointed at the new buffer
|
||||
*/
|
||||
|
||||
/*
|
||||
* We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
|
||||
* bytes of encrypted compressed stuff.
|
||||
*/
|
||||
|
||||
/* check is not needed I believe */
|
||||
if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
/* decrypt in place in 'rr->input' */
|
||||
rr->data = rr->input;
|
||||
rr->orig_len = rr->length;
|
||||
|
||||
enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
|
||||
/*-
|
||||
* enc_err is:
|
||||
* 0: (in non-constant time) if the record is publically invalid.
|
||||
* 1: if the padding is valid
|
||||
* -1: if the padding is invalid
|
||||
*/
|
||||
if (enc_err == 0) {
|
||||
/* For DTLS we simply ignore bad packets. */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto err;
|
||||
}
|
||||
#ifdef SSL_DEBUG
|
||||
printf("dec %d\n", rr->length);
|
||||
{
|
||||
unsigned int z;
|
||||
for (z = 0; z < rr->length; z++)
|
||||
printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
/* r->length is now the compressed data plus mac */
|
||||
if ((sess != NULL) &&
|
||||
(s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
|
||||
/* s->read_hash != NULL => mac_size != -1 */
|
||||
unsigned char *mac = NULL;
|
||||
unsigned char mac_tmp[EVP_MAX_MD_SIZE];
|
||||
mac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
|
||||
|
||||
/*
|
||||
* orig_len is the length of the record before any padding was
|
||||
* removed. This is public information, as is the MAC in use,
|
||||
* therefore we can safely process the record in a different amount
|
||||
* of time if it's too short to possibly contain a MAC.
|
||||
*/
|
||||
if (rr->orig_len < mac_size ||
|
||||
/* CBC records must have a padding length byte too. */
|
||||
(EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
rr->orig_len < mac_size + 1)) {
|
||||
al = SSL_AD_DECODE_ERROR;
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
|
||||
/*
|
||||
* We update the length so that the TLS header bytes can be
|
||||
* constructed correctly but we need to extract the MAC in
|
||||
* constant time from within the record, without leaking the
|
||||
* contents of the padding bytes.
|
||||
*/
|
||||
mac = mac_tmp;
|
||||
ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
|
||||
rr->length -= mac_size;
|
||||
} else {
|
||||
/*
|
||||
* In this case there's no padding, so |rec->orig_len| equals
|
||||
* |rec->length| and we checked that there's enough bytes for
|
||||
* |mac_size| above.
|
||||
*/
|
||||
rr->length -= mac_size;
|
||||
mac = &rr->data[rr->length];
|
||||
}
|
||||
|
||||
i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
|
||||
if (i < 0 || mac == NULL
|
||||
|| CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
|
||||
enc_err = -1;
|
||||
if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
|
||||
enc_err = -1;
|
||||
}
|
||||
|
||||
if (enc_err < 0) {
|
||||
/* decryption failed, silently discard message */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* r->length is now just compressed */
|
||||
if (s->expand != NULL) {
|
||||
if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_COMPRESSED_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
if (!ssl3_do_uncompress(s, rr)) {
|
||||
al = SSL_AD_DECOMPRESSION_FAILURE;
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
|
||||
goto f_err;
|
||||
}
|
||||
}
|
||||
|
||||
if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
|
||||
al = SSL_AD_RECORD_OVERFLOW;
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
goto f_err;
|
||||
}
|
||||
|
||||
rr->off = 0;
|
||||
/*-
|
||||
* So at this point the following is true
|
||||
* ssl->s3->rrec.type is the type of record
|
||||
* ssl->s3->rrec.length == number of bytes in record
|
||||
* ssl->s3->rrec.off == offset to first valid byte
|
||||
* ssl->s3->rrec.data == where to take bytes from, increment
|
||||
* after use :-).
|
||||
*/
|
||||
|
||||
/* we have pulled in a full packet so zero things */
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
|
||||
/* Mark receipt of record. */
|
||||
dtls1_record_bitmap_update(s, bitmap);
|
||||
|
||||
return (1);
|
||||
|
||||
f_err:
|
||||
ssl3_send_alert(s, SSL3_AL_FATAL, al);
|
||||
err:
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve a buffered record that belongs to the current epoch, i.e. processed
|
||||
*/
|
||||
#define dtls1_get_processed_record(s) \
|
||||
dtls1_retrieve_buffered_record((s), \
|
||||
&(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
|
||||
|
||||
/*-
|
||||
* Call this to get a new input record.
|
||||
* It will return <= 0 if more data is needed, normally due to an error
|
||||
* or non-blocking IO.
|
||||
* When it finishes, one packet has been decoded and can be found in
|
||||
* ssl->s3->rrec.type - is the type of record
|
||||
* ssl->s3->rrec.data, - data
|
||||
* ssl->s3->rrec.length, - number of bytes
|
||||
*/
|
||||
/* used only by dtls1_read_bytes */
|
||||
int dtls1_get_record(SSL *s)
|
||||
{
|
||||
int ssl_major, ssl_minor;
|
||||
int i, n;
|
||||
SSL3_RECORD *rr;
|
||||
unsigned char *p = NULL;
|
||||
unsigned short version;
|
||||
DTLS1_BITMAP *bitmap;
|
||||
unsigned int is_next_epoch;
|
||||
|
||||
rr = RECORD_LAYER_get_rrec(&s->rlayer);
|
||||
|
||||
again:
|
||||
/*
|
||||
* The epoch may have changed. If so, process all the pending records.
|
||||
* This is a non-blocking operation.
|
||||
*/
|
||||
if (!dtls1_process_buffered_records(s))
|
||||
return -1;
|
||||
|
||||
/* if we're renegotiating, then there may be buffered records */
|
||||
if (dtls1_get_processed_record(s))
|
||||
return 1;
|
||||
|
||||
/* get something from the wire */
|
||||
|
||||
/* check if we have the header */
|
||||
if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
|
||||
(RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
|
||||
n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
|
||||
SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1);
|
||||
/* read timeout is handled by dtls1_read_bytes */
|
||||
if (n <= 0)
|
||||
return (n); /* error or non-blocking */
|
||||
|
||||
/* this packet contained a partial record, dump it */
|
||||
if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
|
||||
DTLS1_RT_HEADER_LENGTH) {
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
}
|
||||
|
||||
RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
|
||||
|
||||
p = RECORD_LAYER_get_packet(&s->rlayer);
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
|
||||
s, s->msg_callback_arg);
|
||||
|
||||
/* Pull apart the header into the DTLS1_RECORD */
|
||||
rr->type = *(p++);
|
||||
ssl_major = *(p++);
|
||||
ssl_minor = *(p++);
|
||||
version = (ssl_major << 8) | ssl_minor;
|
||||
|
||||
/* sequence number is 64 bits, with top 2 bytes = epoch */
|
||||
n2s(p, rr->epoch);
|
||||
|
||||
memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
|
||||
p += 6;
|
||||
|
||||
n2s(p, rr->length);
|
||||
|
||||
/*
|
||||
* Lets check the version. We tolerate alerts that don't have the exact
|
||||
* version number (e.g. because of protocol version errors)
|
||||
*/
|
||||
if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
|
||||
if (version != s->version) {
|
||||
/* unexpected version, silently discard */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
}
|
||||
}
|
||||
|
||||
if ((version & 0xff00) != (s->version & 0xff00)) {
|
||||
/* wrong version, silently discard record */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
}
|
||||
|
||||
if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
|
||||
/* record too long, silently discard it */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
}
|
||||
|
||||
/* now s->rlayer.rstate == SSL_ST_READ_BODY */
|
||||
}
|
||||
|
||||
/* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
|
||||
|
||||
if (rr->length >
|
||||
RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
|
||||
/* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
|
||||
i = rr->length;
|
||||
n = ssl3_read_n(s, i, i, 1, 1);
|
||||
/* this packet contained a partial record, dump it */
|
||||
if (n != i) {
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
}
|
||||
|
||||
/*
|
||||
* now n == rr->length, and s->packet_length ==
|
||||
* DTLS1_RT_HEADER_LENGTH + rr->length
|
||||
*/
|
||||
}
|
||||
/* set state for later operations */
|
||||
RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
|
||||
|
||||
/* match epochs. NULL means the packet is dropped on the floor */
|
||||
bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
|
||||
if (bitmap == NULL) {
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
|
||||
goto again; /* get another record */
|
||||
}
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
/* Only do replay check if no SCTP bio */
|
||||
if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
|
||||
#endif
|
||||
/* Check whether this is a repeat, or aged record. */
|
||||
/*
|
||||
* TODO: Does it make sense to have replay protection in epoch 0 where
|
||||
* we have no integrity negotiated yet?
|
||||
*/
|
||||
if (!dtls1_record_replay_check(s, bitmap)) {
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
|
||||
goto again; /* get another record */
|
||||
}
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
}
|
||||
#endif
|
||||
|
||||
/* just read a 0 length packet */
|
||||
if (rr->length == 0)
|
||||
goto again;
|
||||
|
||||
/*
|
||||
* If this record is from the next epoch (either HM or ALERT), and a
|
||||
* handshake is currently in progress, buffer it since it cannot be
|
||||
* processed at this time.
|
||||
*/
|
||||
if (is_next_epoch) {
|
||||
if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
|
||||
if (dtls1_buffer_record
|
||||
(s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
|
||||
rr->seq_num) < 0)
|
||||
return -1;
|
||||
}
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
}
|
||||
|
||||
if (!dtls1_process_record(s, bitmap)) {
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
|
||||
goto again; /* get another record */
|
||||
}
|
||||
|
||||
return (1);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user