2015-02-04 16:02:37 +00:00
|
|
|
/* ssl/record/rec_layer_d1.c */
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2005-04-26 16:02:40 +00:00
|
|
|
* DTLS implementation written by Nagendra Modadugu
|
2015-01-22 03:40:55 +00:00
|
|
|
* (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
|
2005-04-26 16:02:40 +00:00
|
|
|
*/
|
|
|
|
/* ====================================================================
|
|
|
|
* Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2015-01-22 03:40:55 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2005-04-26 16:02:40 +00:00
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* 3. All advertising materials mentioning features or use of this
|
|
|
|
* software must display the following acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
|
|
|
* endorse or promote products derived from this software without
|
|
|
|
* prior written permission. For written permission, please contact
|
|
|
|
* openssl-core@openssl.org.
|
|
|
|
*
|
|
|
|
* 5. Products derived from this software may not be called "OpenSSL"
|
|
|
|
* nor may "OpenSSL" appear in their names without prior written
|
|
|
|
* permission of the OpenSSL Project.
|
|
|
|
*
|
|
|
|
* 6. Redistributions of any form whatsoever must retain the following
|
|
|
|
* acknowledgment:
|
|
|
|
* "This product includes software developed by the OpenSSL Project
|
|
|
|
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
|
|
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
|
|
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* ====================================================================
|
|
|
|
*
|
|
|
|
* This product includes cryptographic software written by Eric Young
|
|
|
|
* (eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
* Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2005-04-26 16:02:40 +00:00
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2005-04-26 16:02:40 +00:00
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2005-04-26 16:02:40 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
2015-01-22 03:40:55 +00:00
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
2005-04-26 16:02:40 +00:00
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2005-04-26 16:02:40 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2005-04-26 16:02:40 +00:00
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.]
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#define USE_SOCKETS
|
2015-02-02 10:38:12 +00:00
|
|
|
#include "../ssl_locl.h"
|
2005-04-26 16:02:40 +00:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/pqueue.h>
|
2007-09-30 22:01:36 +00:00
|
|
|
#include <openssl/rand.h>
|
2015-02-04 16:02:37 +00:00
|
|
|
#include "record_locl.h"
|
2015-02-03 14:54:13 +00:00
|
|
|
|
|
|
|
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
|
|
|
|
{
|
|
|
|
DTLS_RECORD_LAYER *d;
|
|
|
|
|
2015-05-02 03:10:31 +00:00
|
|
|
if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
|
2015-02-03 14:54:13 +00:00
|
|
|
return (0);
|
|
|
|
|
2015-02-03 16:05:28 +00:00
|
|
|
|
2015-02-03 14:54:13 +00:00
|
|
|
rl->d = d;
|
2015-02-03 16:11:49 +00:00
|
|
|
|
2015-02-03 16:05:28 +00:00
|
|
|
d->unprocessed_rcds.q = pqueue_new();
|
|
|
|
d->processed_rcds.q = pqueue_new();
|
2015-02-04 10:27:43 +00:00
|
|
|
d->buffered_app_data.q = pqueue_new();
|
2015-02-03 16:05:28 +00:00
|
|
|
|
2015-02-04 10:27:43 +00:00
|
|
|
if (!d->unprocessed_rcds.q || !d->processed_rcds.q
|
|
|
|
|| !d->buffered_app_data.q) {
|
2015-05-01 18:37:16 +00:00
|
|
|
pqueue_free(d->unprocessed_rcds.q);
|
|
|
|
pqueue_free(d->processed_rcds.q);
|
|
|
|
pqueue_free(d->buffered_app_data.q);
|
2015-02-03 16:05:28 +00:00
|
|
|
OPENSSL_free(d);
|
|
|
|
rl->d = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
2015-02-03 14:54:13 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
|
|
|
|
{
|
2015-02-03 16:05:28 +00:00
|
|
|
DTLS_RECORD_LAYER_clear(rl);
|
|
|
|
pqueue_free(rl->d->unprocessed_rcds.q);
|
|
|
|
pqueue_free(rl->d->processed_rcds.q);
|
2015-02-04 10:27:43 +00:00
|
|
|
pqueue_free(rl->d->buffered_app_data.q);
|
2015-02-03 14:54:13 +00:00
|
|
|
OPENSSL_free(rl->d);
|
|
|
|
rl->d = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
|
|
|
|
{
|
|
|
|
DTLS_RECORD_LAYER *d;
|
2015-02-03 16:05:28 +00:00
|
|
|
pitem *item = NULL;
|
|
|
|
DTLS1_RECORD_DATA *rdata;
|
|
|
|
pqueue unprocessed_rcds;
|
|
|
|
pqueue processed_rcds;
|
2015-02-04 10:27:43 +00:00
|
|
|
pqueue buffered_app_data;
|
2015-02-03 16:05:28 +00:00
|
|
|
|
2015-02-03 14:54:13 +00:00
|
|
|
d = rl->d;
|
2015-02-03 16:05:28 +00:00
|
|
|
|
|
|
|
while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
|
|
|
|
rdata = (DTLS1_RECORD_DATA *)item->data;
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(rdata->rbuf.buf);
|
2015-02-03 16:05:28 +00:00
|
|
|
OPENSSL_free(item->data);
|
|
|
|
pitem_free(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
|
|
|
|
rdata = (DTLS1_RECORD_DATA *)item->data;
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(rdata->rbuf.buf);
|
2015-02-03 16:05:28 +00:00
|
|
|
OPENSSL_free(item->data);
|
|
|
|
pitem_free(item);
|
|
|
|
}
|
|
|
|
|
2015-02-04 10:27:43 +00:00
|
|
|
while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
|
|
|
|
rdata = (DTLS1_RECORD_DATA *)item->data;
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(rdata->rbuf.buf);
|
2015-02-04 10:27:43 +00:00
|
|
|
OPENSSL_free(item->data);
|
|
|
|
pitem_free(item);
|
|
|
|
}
|
|
|
|
|
2015-02-03 16:05:28 +00:00
|
|
|
unprocessed_rcds = d->unprocessed_rcds.q;
|
|
|
|
processed_rcds = d->processed_rcds.q;
|
2015-02-04 10:27:43 +00:00
|
|
|
buffered_app_data = d->buffered_app_data.q;
|
2015-05-02 03:10:31 +00:00
|
|
|
memset(d, 0, sizeof(*d));
|
2015-02-03 16:05:28 +00:00
|
|
|
d->unprocessed_rcds.q = unprocessed_rcds;
|
|
|
|
d->processed_rcds.q = processed_rcds;
|
2015-02-04 10:27:43 +00:00
|
|
|
d->buffered_app_data.q = buffered_app_data;
|
2015-02-03 14:54:13 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 11:57:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-04 15:44:12 +00:00
|
|
|
void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl)
|
|
|
|
{
|
|
|
|
memcpy(rl->write_sequence, rl->read_sequence, sizeof(rl->write_sequence));
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
|
|
|
|
int len, int peek);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
|
|
|
/* copy buffered record into SSL structure */
|
2015-01-22 03:40:55 +00:00
|
|
|
static int dtls1_copy_record(SSL *s, pitem *item)
|
|
|
|
{
|
2005-04-26 16:02:40 +00:00
|
|
|
DTLS1_RECORD_DATA *rdata;
|
|
|
|
|
|
|
|
rdata = (DTLS1_RECORD_DATA *)item->data;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-02-02 16:02:55 +00:00
|
|
|
SSL3_BUFFER_release(&s->rlayer.rbuf);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-02-02 20:55:15 +00:00
|
|
|
s->rlayer.packet = rdata->packet;
|
|
|
|
s->rlayer.packet_length = rdata->packet_length;
|
2015-02-02 16:02:55 +00:00
|
|
|
memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
|
|
|
|
memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Set proper sequence number for mac calculation */
|
2015-02-03 14:26:50 +00:00
|
|
|
memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-03-26 15:14:42 +00:00
|
|
|
int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
DTLS1_RECORD_DATA *rdata;
|
|
|
|
pitem *item;
|
|
|
|
|
|
|
|
/* Limit the size of the queue to prevent DOS attacks */
|
|
|
|
if (pqueue_size(queue->q) >= 100)
|
|
|
|
return 0;
|
|
|
|
|
2015-05-02 03:10:31 +00:00
|
|
|
rdata = OPENSSL_malloc(sizeof(*rdata));
|
2015-01-22 03:40:55 +00:00
|
|
|
item = pitem_new(priority, rdata);
|
|
|
|
if (rdata == NULL || item == NULL) {
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(rdata);
|
2015-05-01 18:37:16 +00:00
|
|
|
pitem_free(item);
|
2015-01-22 03:40:55 +00:00
|
|
|
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
2015-03-04 21:05:53 +00:00
|
|
|
return -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 20:55:15 +00:00
|
|
|
rdata->packet = s->rlayer.packet;
|
|
|
|
rdata->packet_length = s->rlayer.packet_length;
|
2015-02-02 16:02:55 +00:00
|
|
|
memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
|
|
|
|
memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
item->data = rdata;
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2011-12-25 14:45:15 +00:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Store bio_dgram_sctp_rcvinfo struct */
|
|
|
|
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
|
|
|
|
(s->state == SSL3_ST_SR_FINISHED_A
|
|
|
|
|| s->state == SSL3_ST_CR_FINISHED_A)) {
|
|
|
|
BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
|
|
|
|
sizeof(rdata->recordinfo), &rdata->recordinfo);
|
|
|
|
}
|
2011-12-25 14:45:15 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-02 20:55:15 +00:00
|
|
|
s->rlayer.packet = NULL;
|
|
|
|
s->rlayer.packet_length = 0;
|
2015-05-04 22:00:15 +00:00
|
|
|
memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
|
|
|
|
memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (!ssl3_setup_buffers(s)) {
|
|
|
|
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(rdata->rbuf.buf);
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_free(rdata);
|
|
|
|
pitem_free(item);
|
|
|
|
return (-1);
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* 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);
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(rdata->rbuf.buf);
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_free(rdata);
|
|
|
|
pitem_free(item);
|
|
|
|
return (-1);
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2015-02-01 16:47:15 +00:00
|
|
|
int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2005-04-26 16:02:40 +00:00
|
|
|
pitem *item;
|
|
|
|
|
|
|
|
item = pqueue_pop(queue->q);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (item) {
|
2005-04-26 16:02:40 +00:00
|
|
|
dtls1_copy_record(s, item);
|
|
|
|
|
|
|
|
OPENSSL_free(item->data);
|
2015-01-22 03:40:55 +00:00
|
|
|
pitem_free(item);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return (1);
|
2005-04-26 16:02:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* retrieve a buffered record that belongs to the new epoch, i.e., not
|
|
|
|
* processed yet
|
|
|
|
*/
|
2005-04-26 16:02:40 +00:00
|
|
|
#define dtls1_get_unprocessed_record(s) \
|
|
|
|
dtls1_retrieve_buffered_record((s), \
|
2015-02-03 16:05:28 +00:00
|
|
|
&((s)->rlayer.d->unprocessed_rcds))
|
2005-04-26 16:02:40 +00:00
|
|
|
|
|
|
|
|
2015-02-01 16:47:15 +00:00
|
|
|
int dtls1_process_buffered_records(SSL *s)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2005-04-26 16:02:40 +00:00
|
|
|
pitem *item;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-02-03 16:05:28 +00:00
|
|
|
item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (item) {
|
2005-04-26 16:02:40 +00:00
|
|
|
/* Check if epoch is current. */
|
2015-02-03 16:05:28 +00:00
|
|
|
if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
|
2015-01-22 03:40:55 +00:00
|
|
|
return (1); /* Nothing to do. */
|
|
|
|
|
2005-04-26 16:02:40 +00:00
|
|
|
/* Process all the records. */
|
2015-02-03 16:05:28 +00:00
|
|
|
while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
|
2005-04-26 16:02:40 +00:00
|
|
|
dtls1_get_unprocessed_record(s);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!dtls1_process_record(s))
|
|
|
|
return (0);
|
2015-02-03 16:05:28 +00:00
|
|
|
if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
|
2015-02-02 16:02:55 +00:00
|
|
|
SSL3_RECORD_get_seq_num(&s->rlayer.rrec)) < 0)
|
2015-01-07 14:18:13 +00:00
|
|
|
return -1;
|
2005-04-26 16:02:40 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* sync epoch numbers once all the unprocessed records have been
|
|
|
|
* processed
|
|
|
|
*/
|
2015-02-03 16:05:28 +00:00
|
|
|
s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;
|
|
|
|
s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
|
|
|
|
2014-12-28 02:48:40 +00:00
|
|
|
/*-
|
|
|
|
* Return up to 'len' payload bytes received in 'type' records.
|
2005-04-26 16:02:40 +00:00
|
|
|
* '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), ChangeCipherSpec records (not really
|
|
|
|
* a surprise, but handled as if it were), or renegotiation requests.
|
|
|
|
* 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, unsigned char *buf, int len, int peek)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int al, i, j, ret;
|
|
|
|
unsigned int n;
|
|
|
|
SSL3_RECORD *rr;
|
|
|
|
void (*cb) (const SSL *ssl, int type2, int val) = NULL;
|
|
|
|
|
2015-02-02 16:02:55 +00:00
|
|
|
if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
|
2015-01-30 14:57:54 +00:00
|
|
|
/* Not initialized yet */
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!ssl3_setup_buffers(s))
|
|
|
|
return (-1);
|
2015-01-30 14:57:54 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
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, peek)))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/*
|
2015-02-04 10:14:36 +00:00
|
|
|
* Now s->rlayer.d->handshake_fragment_len == 0 if
|
|
|
|
* type == SSL3_RT_HANDSHAKE.
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2011-12-25 14:45:15 +00:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Continue handshake if it had to be interrupted to read app data with
|
|
|
|
* SCTP.
|
|
|
|
*/
|
|
|
|
if ((!s->in_handshake && SSL_in_init(s)) ||
|
|
|
|
(BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
|
|
|
|
(s->state == DTLS1_SCTP_ST_SR_READ_SOCK
|
|
|
|
|| s->state == DTLS1_SCTP_ST_CR_READ_SOCK)
|
|
|
|
&& s->s3->in_read_app_data != 2))
|
2011-12-25 14:45:15 +00:00
|
|
|
#else
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!s->in_handshake && SSL_in_init(s))
|
2011-12-25 14:45:15 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
2015-01-05 11:30:03 +00:00
|
|
|
/*-
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-02 16:02:55 +00:00
|
|
|
rr = &s->rlayer.rrec;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We are not handshaking and have no data yet, so process data buffered
|
|
|
|
* during the last handshake in advance, if any.
|
|
|
|
*/
|
2015-03-28 00:33:05 +00:00
|
|
|
if (s->state == SSL_ST_OK && SSL3_RECORD_get_length(rr) == 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
pitem *item;
|
2015-02-04 10:27:43 +00:00
|
|
|
item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (item) {
|
2011-12-25 14:45:15 +00:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-01-22 03:40:55 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
2011-12-25 14:45:15 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
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 */
|
2015-03-28 00:33:05 +00:00
|
|
|
if ((SSL3_RECORD_get_length(rr) == 0)
|
|
|
|
|| (s->rlayer.rstate == SSL_ST_READ_BODY)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->d1->listen && rr->type != SSL3_RT_HANDSHAKE) {
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2015-03-28 00:33:05 +00:00
|
|
|
&& (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-04 10:27:43 +00:00
|
|
|
if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_get_seq_num(rr)) < 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the other end has shut down, throw anything we read away (even in
|
|
|
|
* 'peek' mode)
|
|
|
|
*/
|
|
|
|
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
s->rwstate = SSL_NOTHING;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
if (type == SSL3_RECORD_get_type(rr)) {
|
|
|
|
/* SSL3_RT_APPLICATION_DATA or
|
|
|
|
* SSL3_RT_HANDSHAKE */
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
2011-12-25 14:45:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (len <= 0)
|
|
|
|
return (len);
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
if ((unsigned int)len > SSL3_RECORD_get_length(rr))
|
|
|
|
n = SSL3_RECORD_get_length(rr);
|
2015-01-22 03:40:55 +00:00
|
|
|
else
|
|
|
|
n = (unsigned int)len;
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!peek) {
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_add_length(rr, -n);
|
|
|
|
SSL3_RECORD_add_off(rr, n);
|
|
|
|
if (SSL3_RECORD_get_length(rr) == 0) {
|
2015-02-03 10:48:28 +00:00
|
|
|
s->rlayer.rstate = SSL_ST_READ_HEADER;
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_off(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-25 14:45:15 +00:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* We were about to renegotiate but had to read belated application
|
|
|
|
* data first, so retry.
|
|
|
|
*/
|
|
|
|
if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA &&
|
2015-01-22 03:40:55 +00:00
|
|
|
(s->state == DTLS1_SCTP_ST_SR_READ_SOCK
|
|
|
|
|| s->state == DTLS1_SCTP_ST_CR_READ_SOCK)) {
|
|
|
|
s->rwstate = SSL_READING;
|
|
|
|
BIO_clear_retry_flags(SSL_get_rbio(s));
|
|
|
|
BIO_set_retry_read(SSL_get_rbio(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
|
2015-02-04 10:14:36 +00:00
|
|
|
dest_maxlen = sizeof s->rlayer.d->handshake_fragment;
|
|
|
|
dest = s->rlayer.d->handshake_fragment;
|
|
|
|
dest_len = &s->rlayer.d->handshake_fragment_len;
|
2015-03-28 00:33:05 +00:00
|
|
|
} else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
|
2015-02-04 10:14:36 +00:00
|
|
|
dest_maxlen = sizeof(s->rlayer.d->alert_fragment);
|
|
|
|
dest = s->rlayer.d->alert_fragment;
|
|
|
|
dest_len = &s->rlayer.d->alert_fragment_len;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2011-12-31 22:59:57 +00:00
|
|
|
#ifndef OPENSSL_NO_HEARTBEATS
|
2015-03-28 00:33:05 +00:00
|
|
|
else if (SSL3_RECORD_get_type(rr) == TLS1_RT_HEARTBEAT) {
|
2015-03-06 14:37:17 +00:00
|
|
|
/* We allow a 0 return */
|
2015-04-16 05:50:03 +00:00
|
|
|
if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_get_length(rr)) < 0) {
|
2015-03-06 14:37:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Exit and notify application to read again */
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
s->rwstate = SSL_READING;
|
|
|
|
BIO_clear_retry_flags(SSL_get_rbio(s));
|
|
|
|
BIO_set_retry_read(SSL_get_rbio(s));
|
|
|
|
return (-1);
|
|
|
|
}
|
2011-12-31 22:59:57 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
/* else it's a CCS message, or application data or wrong */
|
2015-03-28 00:33:05 +00:00
|
|
|
else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Application data while renegotiating is allowed. Try again
|
|
|
|
* reading.
|
|
|
|
*/
|
2015-03-28 00:33:05 +00:00
|
|
|
if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {
|
2015-01-22 03:40:55 +00:00
|
|
|
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 pathalogical case, the Client Hello may be
|
|
|
|
* fragmented--don't always expect dest_maxlen bytes
|
|
|
|
*/
|
2015-03-28 00:33:05 +00:00
|
|
|
if (SSL3_RECORD_get_length(rr) < dest_maxlen) {
|
2008-09-13 18:24:38 +00:00
|
|
|
#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* for normal alerts rr->length is 2, while
|
|
|
|
* dest_maxlen is 7 if we were to handle this
|
|
|
|
* non-existing alert...
|
|
|
|
*/
|
|
|
|
FIX ME
|
2008-09-13 18:24:38 +00:00
|
|
|
#endif
|
2015-03-28 00:33:05 +00:00
|
|
|
s->rlayer.rstate = SSL_ST_READ_HEADER;
|
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now move 'n' bytes: */
|
|
|
|
for (k = 0; k < dest_maxlen; k++) {
|
2015-03-28 00:33:05 +00:00
|
|
|
dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
|
|
|
|
SSL3_RECORD_add_off(rr, 1);
|
|
|
|
SSL3_RECORD_add_length(rr, -1);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
*dest_len = dest_maxlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 00:06:54 +00:00
|
|
|
/*-
|
2015-02-04 10:14:36 +00:00
|
|
|
* 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.
|
2015-01-17 00:06:54 +00:00
|
|
|
* (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
|
|
|
|
*/
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/* If we are a client, check for an incoming 'Hello Request': */
|
|
|
|
if ((!s->server) &&
|
2015-02-04 10:14:36 +00:00
|
|
|
(s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
|
|
|
|
(s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
|
2015-01-22 03:40:55 +00:00
|
|
|
(s->session != NULL) && (s->session->cipher != NULL)) {
|
2015-02-04 10:14:36 +00:00
|
|
|
s->rlayer.d->handshake_fragment_len = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-02-04 10:14:36 +00:00
|
|
|
if ((s->rlayer.d->handshake_fragment[1] != 0) ||
|
|
|
|
(s->rlayer.d->handshake_fragment[2] != 0) ||
|
|
|
|
(s->rlayer.d->handshake_fragment[3] != 0)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
al = SSL_AD_DECODE_ERROR;
|
|
|
|
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* no need to check sequence number on HELLO REQUEST messages
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (s->msg_callback)
|
|
|
|
s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
|
2015-02-04 10:14:36 +00:00
|
|
|
s->rlayer.d->handshake_fragment, 4, s,
|
2015-01-22 03:40:55 +00:00
|
|
|
s->msg_callback_arg);
|
|
|
|
|
|
|
|
if (SSL_is_init_finished(s) &&
|
|
|
|
!(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)) {
|
2015-02-02 16:02:55 +00:00
|
|
|
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
|
2015-01-30 14:57:54 +00:00
|
|
|
/* no read-ahead left? */
|
2015-01-22 03:40:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* we either finished a handshake or ignored the request, now try
|
|
|
|
* again to obtain the (application) data we were asked for
|
|
|
|
*/
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
2015-02-04 10:14:36 +00:00
|
|
|
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];
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-02-04 10:14:36 +00:00
|
|
|
s->rlayer.d->alert_fragment_len = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (s->msg_callback)
|
|
|
|
s->msg_callback(0, s->version, SSL3_RT_ALERT,
|
2015-02-04 10:14:36 +00:00
|
|
|
s->rlayer.d->alert_fragment, 2, s,
|
|
|
|
s->msg_callback_arg);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:57:45 +00:00
|
|
|
if (alert_level == SSL3_AL_WARNING) {
|
2015-01-22 03:40:55 +00:00
|
|
|
s->s3->warn_alert = alert_descr;
|
|
|
|
if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
|
2011-12-25 14:45:15 +00:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
2011-12-25 14:45:15 +00:00
|
|
|
#endif
|
2015-01-22 03:40:55 +00:00
|
|
|
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
|
|
|
return (0);
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
#if 0
|
|
|
|
/* XXX: this is a possible improvement in the future */
|
2015-01-22 03:40:55 +00:00
|
|
|
/* now check if it's a missing record */
|
|
|
|
if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
|
|
|
|
unsigned short seq;
|
|
|
|
unsigned int frag_off;
|
2015-02-04 10:14:36 +00:00
|
|
|
unsigned char *p = &(s->rlayer.d->alert_fragment[2]);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
#endif
|
2015-03-04 10:57:45 +00:00
|
|
|
} else if (alert_level == SSL3_AL_FATAL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
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->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;
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
|
2015-01-22 03:40:55 +00:00
|
|
|
struct ccs_header_st ccs_hdr;
|
|
|
|
unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH;
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
dtls1_get_ccs_header(SSL3_RECORD_get_data(rr), &ccs_hdr);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (s->version == DTLS1_BAD_VER)
|
|
|
|
ccs_hdr_len = 3;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'Change Cipher Spec' is just a single byte, so we know exactly
|
|
|
|
* what the record payload has to look like
|
|
|
|
*/
|
|
|
|
/* XDTLS: check that epoch is consistent */
|
2015-03-28 00:33:05 +00:00
|
|
|
if ((SSL3_RECORD_get_length(rr) != ccs_hdr_len)
|
|
|
|
|| (SSL3_RECORD_get_off(rr) != 0)
|
|
|
|
|| (SSL3_RECORD_get_data(rr)[0] != SSL3_MT_CCS)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
i = SSL_AD_ILLEGAL_PARAMETER;
|
|
|
|
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_CHANGE_CIPHER_SPEC);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (s->msg_callback)
|
|
|
|
s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_get_data(rr), 1, s, s->msg_callback_arg);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't process a CCS now, because previous handshake messages
|
|
|
|
* are still missing, so just drop it.
|
|
|
|
*/
|
|
|
|
if (!s->d1->change_cipher_spec_ok) {
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->d1->change_cipher_spec_ok = 0;
|
|
|
|
|
|
|
|
s->s3->change_cipher_spec = 1;
|
|
|
|
if (!ssl3_do_change_cipher_spec(s))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* do this whenever CCS is processed */
|
|
|
|
dtls1_reset_seq_numbers(s, SSL3_CC_READ);
|
|
|
|
|
|
|
|
if (s->version == DTLS1_BAD_VER)
|
|
|
|
s->d1->handshake_read_seq++;
|
2009-04-20 11:33:12 +00:00
|
|
|
|
2011-12-25 14:45:15 +00:00
|
|
|
#ifndef OPENSSL_NO_SCTP
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Remember that a CCS has been received, so that an old key of
|
|
|
|
* SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
|
|
|
|
* SCTP is used
|
|
|
|
*/
|
|
|
|
BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
|
2011-12-25 14:45:15 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unexpected handshake message (Client Hello, or protocol violation)
|
|
|
|
*/
|
2015-02-04 10:14:36 +00:00
|
|
|
if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
|
2015-01-22 03:40:55 +00:00
|
|
|
!s->in_handshake) {
|
|
|
|
struct hm_header_st msg_hdr;
|
|
|
|
|
|
|
|
/* this may just be a stale retransmit */
|
|
|
|
dtls1_get_message_header(rr->data, &msg_hdr);
|
2015-03-28 00:33:05 +00:00
|
|
|
if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {
|
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
|
2015-03-24 15:10:15 +00:00
|
|
|
dtls1_retransmit_buffered_messages(s);
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((s->state & SSL_ST_MASK) == SSL_ST_OK) &&
|
|
|
|
!(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
|
|
|
|
s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
|
|
|
|
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)) {
|
2015-02-02 16:02:55 +00:00
|
|
|
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
|
2015-01-30 14:57:54 +00:00
|
|
|
/* no read-ahead left? */
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
switch (SSL3_RECORD_get_type(rr)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
default:
|
|
|
|
/* TLS just ignores unknown message types */
|
|
|
|
if (s->version == TLS1_VERSION) {
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_length(rr, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
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 s->in_handshake is set, 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) &&
|
|
|
|
(((s->state & SSL_ST_CONNECT) &&
|
|
|
|
(s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
|
|
|
|
(s->state <= SSL3_ST_CR_SRVR_HELLO_A)
|
|
|
|
) || ((s->state & SSL_ST_ACCEPT) &&
|
|
|
|
(s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
|
|
|
|
(s->state >= SSL3_ST_SR_CLNT_HELLO_A)
|
|
|
|
)
|
|
|
|
)) {
|
|
|
|
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);
|
|
|
|
err:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this only happens when a client hello is received and a handshake
|
|
|
|
* is started.
|
|
|
|
*/
|
2015-03-26 15:14:42 +00:00
|
|
|
static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
|
|
|
|
int len, int peek)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
|
2015-02-04 10:14:36 +00:00
|
|
|
if ((type == SSL3_RT_HANDSHAKE)
|
|
|
|
&& (s->rlayer.d->handshake_fragment_len > 0))
|
2015-01-22 03:40:55 +00:00
|
|
|
/* (partially) satisfy request from storage */
|
|
|
|
{
|
2015-02-04 10:14:36 +00:00
|
|
|
unsigned char *src = s->rlayer.d->handshake_fragment;
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *dst = buf;
|
|
|
|
unsigned int k, n;
|
|
|
|
|
|
|
|
/* peek == 0 */
|
|
|
|
n = 0;
|
2015-02-04 10:14:36 +00:00
|
|
|
while ((len > 0) && (s->rlayer.d->handshake_fragment_len > 0)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
*dst++ = *src++;
|
|
|
|
len--;
|
2015-02-04 10:14:36 +00:00
|
|
|
s->rlayer.d->handshake_fragment_len--;
|
2015-01-22 03:40:55 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
/* move any remaining fragment bytes: */
|
2015-02-04 10:14:36 +00:00
|
|
|
for (k = 0; k < s->rlayer.d->handshake_fragment_len; k++)
|
|
|
|
s->rlayer.d->handshake_fragment[k] = *src++;
|
2015-01-22 03:40:55 +00:00
|
|
|
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.
|
2005-04-26 16:02:40 +00:00
|
|
|
*/
|
2009-08-26 11:51:57 +00:00
|
|
|
int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2015-02-02 16:02:55 +00:00
|
|
|
wb = &s->rlayer.wbuf;
|
2015-01-30 16:05:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* first check if there is a SSL3_BUFFER still being written out. This
|
|
|
|
* will happen with non blocking IO
|
|
|
|
*/
|
2015-01-30 16:05:47 +00:00
|
|
|
if (SSL3_BUFFER_get_left(wb) != 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
|
2015-02-02 16:02:55 +00:00
|
|
|
wr = &s->rlayer.wrec;
|
2015-01-22 03:40:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-28 00:33:05 +00:00
|
|
|
p = SSL3_BUFFER_get_buf(wb) + prefix_len;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/* write the header */
|
|
|
|
|
|
|
|
*(p++) = type & 0xff;
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_type(wr, type);
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
*(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
|
|
|
|
eivlen = 0;
|
|
|
|
} else
|
|
|
|
eivlen = 0;
|
|
|
|
|
|
|
|
/* lets setup the record stuff. */
|
2015-03-28 00:33:05 +00:00
|
|
|
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);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we now 'read' from wr->input, wr->length bytes into wr->data
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* first we compress */
|
|
|
|
if (s->compress != NULL) {
|
|
|
|
if (!ssl3_do_compress(s)) {
|
|
|
|
SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-28 00:33:05 +00:00
|
|
|
memcpy(SSL3_RECORD_get_data(wr), SSL3_RECORD_get_input(wr),
|
|
|
|
SSL3_RECORD_get_length(wr));
|
|
|
|
SSL3_RECORD_reset_input(wr);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (mac_size != 0) {
|
2015-03-28 00:33:05 +00:00
|
|
|
if (s->method->ssl3_enc->mac(s,
|
|
|
|
&(p[SSL3_RECORD_get_length(wr) + eivlen]), 1) < 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_add_length(wr, mac_size);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* this is true regardless of mac size */
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_data(wr, p);
|
|
|
|
SSL3_RECORD_reset_input(wr);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (eivlen)
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_add_length(wr, eivlen);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (s->method->ssl3_enc->enc(s, 1) < 1)
|
|
|
|
goto err;
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* record length after mac and block padding */
|
|
|
|
/*
|
|
|
|
* if (type == SSL3_RT_APPLICATION_DATA || (type == SSL3_RT_ALERT && !
|
|
|
|
* SSL_in_init(s)))
|
|
|
|
*/
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* there's only one epoch between handshake and app data */
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-02-03 15:14:24 +00:00
|
|
|
s2n(s->rlayer.d->w_epoch, pseq);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* XDTLS: ?? */
|
|
|
|
/*
|
|
|
|
* else s2n(s->d1->handshake_epoch, pseq);
|
|
|
|
*/
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-02-03 14:26:50 +00:00
|
|
|
memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
|
2015-01-22 03:40:55 +00:00
|
|
|
pseq += 6;
|
2015-03-28 00:33:05 +00:00
|
|
|
s2n(SSL3_RECORD_get_length(wr), pseq);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
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);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* we should now have wr->data pointing to the encrypted data, which is
|
|
|
|
* wr->length long
|
|
|
|
*/
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_RECORD_set_type(wr, type); /* not needed but helps for debugging */
|
|
|
|
SSL3_RECORD_add_length(wr, DTLS1_RT_HEADER_LENGTH);
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-02-03 14:26:50 +00:00
|
|
|
ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (create_empty_fragment) {
|
|
|
|
/*
|
|
|
|
* we are in a recursive call; just return the length, don't write
|
|
|
|
* out anything here
|
|
|
|
*/
|
|
|
|
return wr->length;
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* now let's set up wb */
|
2015-03-28 00:33:05 +00:00
|
|
|
SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(wr));
|
|
|
|
SSL3_BUFFER_set_offset(wb, 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* memorize arguments so that ssl3_write_pending can detect bad write
|
|
|
|
* retries later
|
|
|
|
*/
|
2015-02-03 13:22:12 +00:00
|
|
|
s->rlayer.wpend_tot = len;
|
|
|
|
s->rlayer.wpend_buf = buf;
|
|
|
|
s->rlayer.wpend_type = type;
|
|
|
|
s->rlayer.wpend_ret = len;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/* we now just need to write the buffer */
|
|
|
|
return ssl3_write_pending(s, type, buf, len);
|
|
|
|
err:
|
|
|
|
return -1;
|
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-02-01 16:47:15 +00:00
|
|
|
DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned int *is_next_epoch)
|
|
|
|
{
|
|
|
|
|
2005-04-26 16:02:40 +00:00
|
|
|
*is_next_epoch = 0;
|
|
|
|
|
|
|
|
/* In current epoch, accept HM, CCS, DATA, & ALERT */
|
2015-02-03 15:14:24 +00:00
|
|
|
if (rr->epoch == s->rlayer.d->r_epoch)
|
2015-02-03 15:39:06 +00:00
|
|
|
return &s->rlayer.d->bitmap;
|
2005-04-26 16:02:40 +00:00
|
|
|
|
|
|
|
/* Only HM and ALERT messages can be from the next epoch */
|
2015-02-03 15:14:24 +00:00
|
|
|
else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
|
|
|
|
(rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
|
2005-04-26 16:02:40 +00:00
|
|
|
*is_next_epoch = 1;
|
2015-02-03 15:39:06 +00:00
|
|
|
return &s->rlayer.d->next_bitmap;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2005-04-26 16:02:40 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
void dtls1_reset_seq_numbers(SSL *s, int rw)
|
|
|
|
{
|
|
|
|
unsigned char *seq;
|
2015-02-03 14:26:50 +00:00
|
|
|
unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (rw & SSL3_CC_READ) {
|
2015-02-03 14:26:50 +00:00
|
|
|
seq = s->rlayer.read_sequence;
|
2015-02-03 15:14:24 +00:00
|
|
|
s->rlayer.d->r_epoch++;
|
2015-05-04 22:00:15 +00:00
|
|
|
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));
|
2015-01-22 03:40:55 +00:00
|
|
|
} else {
|
2015-02-03 14:26:50 +00:00
|
|
|
seq = s->rlayer.write_sequence;
|
2015-02-04 11:57:34 +00:00
|
|
|
memcpy(s->rlayer.d->last_write_sequence, seq,
|
2015-02-03 14:26:50 +00:00
|
|
|
sizeof(s->rlayer.write_sequence));
|
2015-02-03 15:14:24 +00:00
|
|
|
s->rlayer.d->w_epoch++;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 22:00:15 +00:00
|
|
|
memset(seq, 0, seq_bytes);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|