OpenSSL 1.1.1-pre2
This commit is contained in:
+193
-395
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2005-2017 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
|
||||
@@ -9,18 +9,19 @@
|
||||
|
||||
#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"
|
||||
#include "../packet_locl.h"
|
||||
#include "internal/cryptlib.h"
|
||||
|
||||
int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
|
||||
{
|
||||
DTLS_RECORD_LAYER *d;
|
||||
|
||||
if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
rl->d = d;
|
||||
|
||||
@@ -35,7 +36,7 @@ int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
|
||||
pqueue_free(d->buffered_app_data.q);
|
||||
OPENSSL_free(d);
|
||||
rl->d = NULL;
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -108,19 +109,11 @@ void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
|
||||
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)
|
||||
{
|
||||
@@ -138,7 +131,7 @@ static int dtls1_copy_record(SSL *s, pitem *item)
|
||||
/* Set proper sequence number for mac calculation */
|
||||
memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
|
||||
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
|
||||
@@ -155,7 +148,8 @@ int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
|
||||
if (rdata == NULL || item == NULL) {
|
||||
OPENSSL_free(rdata);
|
||||
pitem_free(item);
|
||||
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -182,23 +176,24 @@ int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
|
||||
memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
|
||||
|
||||
if (!ssl3_setup_buffers(s)) {
|
||||
SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
|
||||
/* SSLfatal() already called */
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(rdata);
|
||||
pitem_free(item);
|
||||
return (-1);
|
||||
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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
OPENSSL_free(rdata->rbuf.buf);
|
||||
OPENSSL_free(rdata);
|
||||
pitem_free(item);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
|
||||
@@ -212,10 +207,10 @@ int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
|
||||
OPENSSL_free(item->data);
|
||||
pitem_free(item);
|
||||
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -265,8 +260,9 @@ int dtls1_process_buffered_records(SSL *s)
|
||||
* 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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR,
|
||||
SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
@@ -284,6 +280,10 @@ int dtls1_process_buffered_records(SSL *s)
|
||||
}
|
||||
|
||||
if (!replayok || !dtls1_process_record(s, bitmap)) {
|
||||
if (ossl_statem_in_error(s)) {
|
||||
/* dtls1_process_record called SSLfatal() */
|
||||
return -1;
|
||||
}
|
||||
/* dump this record */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
@@ -291,8 +291,10 @@ int dtls1_process_buffered_records(SSL *s)
|
||||
}
|
||||
|
||||
if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
|
||||
SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0)
|
||||
SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,49 +338,38 @@ int dtls1_process_buffered_records(SSL *s)
|
||||
* none of our business
|
||||
*/
|
||||
int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
int len, int peek)
|
||||
size_t len, int peek, size_t *readbytes)
|
||||
{
|
||||
int al, i, j, ret;
|
||||
unsigned int n;
|
||||
int i, j, iret;
|
||||
size_t 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 (!ssl3_setup_buffers(s)) {
|
||||
/* SSLfatal() already called */
|
||||
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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, 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);
|
||||
/* SSLfatal() already called if appropriate */
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
return i;
|
||||
if (i == 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
start:
|
||||
@@ -417,18 +408,25 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
}
|
||||
|
||||
/* Check for timeout */
|
||||
if (dtls1_handle_timeout(s) > 0)
|
||||
if (dtls1_handle_timeout(s) > 0) {
|
||||
goto start;
|
||||
} else if (ossl_statem_in_error(s)) {
|
||||
/* dtls1_handle_timeout() has failed with a fatal error */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
iret = dtls1_get_record(s);
|
||||
if (iret <= 0) {
|
||||
iret = dtls1_read_failed(s, iret);
|
||||
/*
|
||||
* Anything other than a timeout is an error. SSLfatal() already
|
||||
* called if appropriate.
|
||||
*/
|
||||
if (iret <= 0)
|
||||
return iret;
|
||||
else
|
||||
goto start;
|
||||
}
|
||||
@@ -454,7 +452,7 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
*/
|
||||
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);
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
@@ -468,7 +466,7 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (type == SSL3_RECORD_get_type(rr)
|
||||
@@ -485,21 +483,21 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
*/
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
|
||||
SSL_R_APP_DATA_IN_HANDSHAKE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (recvd_type != NULL)
|
||||
*recvd_type = SSL3_RECORD_get_type(rr);
|
||||
|
||||
if (len <= 0)
|
||||
return (len);
|
||||
if (len == 0)
|
||||
return 0;
|
||||
|
||||
if ((unsigned int)len > SSL3_RECORD_get_length(rr))
|
||||
if (len > SSL3_RECORD_get_length(rr))
|
||||
n = SSL3_RECORD_get_length(rr);
|
||||
else
|
||||
n = (unsigned int)len;
|
||||
n = len;
|
||||
|
||||
memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
|
||||
if (!peek) {
|
||||
@@ -520,10 +518,11 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
s->d1->shutdown_received
|
||||
&& !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return (n);
|
||||
*readbytes = n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -531,193 +530,23 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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_ALERT) {
|
||||
unsigned int alert_level, alert_descr;
|
||||
unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)
|
||||
+ SSL3_RECORD_get_off(rr);
|
||||
PACKET alert;
|
||||
|
||||
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;
|
||||
if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))
|
||||
|| !PACKET_get_1(&alert, &alert_level)
|
||||
|| !PACKET_get_1(&alert, &alert_descr)
|
||||
|| PACKET_remaining(&alert) != 0) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
|
||||
SSL_R_INVALID_ALERT);
|
||||
return -1;
|
||||
}
|
||||
#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(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
if (s->info_callback != NULL)
|
||||
@@ -735,9 +564,9 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
|
||||
SSL_R_TOO_MANY_WARN_ALERTS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
|
||||
@@ -757,51 +586,24 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
}
|
||||
#endif
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
return (0);
|
||||
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);
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, 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);
|
||||
return 0;
|
||||
} else {
|
||||
al = SSL_AD_ILLEGAL_PARAMETER;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_READ_BYTES,
|
||||
SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
goto start;
|
||||
@@ -811,7 +613,7 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* shutdown */
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
|
||||
@@ -826,43 +628,62 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
/*
|
||||
* 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)) {
|
||||
if ((SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) &&
|
||||
!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) {
|
||||
/*
|
||||
* This may just be a stale retransmit. Also sanity check that we have
|
||||
* at least enough record bytes for a message header
|
||||
*/
|
||||
if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch
|
||||
|| SSL3_RECORD_get_length(rr) < DTLS1_HM_HEADER_LENGTH) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
goto start;
|
||||
}
|
||||
|
||||
dtls1_get_message_header(rr->data, &msg_hdr);
|
||||
|
||||
/*
|
||||
* 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)
|
||||
if (dtls1_check_timeout_num(s) < 0) {
|
||||
/* SSLfatal) already called */
|
||||
return -1;
|
||||
}
|
||||
|
||||
dtls1_retransmit_buffered_messages(s);
|
||||
if (dtls1_retransmit_buffered_messages(s) <= 0) {
|
||||
/* Fail if we encountered a fatal error */
|
||||
if (ossl_statem_in_error(s))
|
||||
return -1;
|
||||
}
|
||||
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;
|
||||
/*
|
||||
* To get here we must be trying to read app data but found handshake
|
||||
* data. But if we're trying to read app data, and we're not in init
|
||||
* (which is tested for at the top of this function) then init must be
|
||||
* finished
|
||||
*/
|
||||
if (!ossl_assert(SSL_is_init_finished(s))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* We found handshake data, so we're going back into init */
|
||||
ossl_statem_set_in_init(s, 1);
|
||||
|
||||
i = s->handshake_func(s);
|
||||
/* SSLfatal() called if appropriate */
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
return i;
|
||||
if (i == 0)
|
||||
return -1;
|
||||
|
||||
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
||||
if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
|
||||
@@ -878,7 +699,7 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
bio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
goto start;
|
||||
@@ -886,14 +707,9 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
|
||||
SSL_R_UNEXPECTED_RECORD);
|
||||
return -1;
|
||||
case SSL3_RT_CHANGE_CIPHER_SPEC:
|
||||
case SSL3_RT_ALERT:
|
||||
case SSL3_RT_HANDSHAKE:
|
||||
@@ -902,9 +718,9 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
case SSL3_RT_APPLICATION_DATA:
|
||||
/*
|
||||
* At this point, we were expecting handshake data, but have
|
||||
@@ -917,73 +733,41 @@ int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
(s->s3->total_renegotiations != 0) &&
|
||||
ossl_statem_app_data_allowed(s)) {
|
||||
s->s3->in_read_app_data = 2;
|
||||
return (-1);
|
||||
return -1;
|
||||
} else {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
|
||||
SSL_R_UNEXPECTED_RECORD);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* 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 dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
|
||||
size_t *written)
|
||||
{
|
||||
int i;
|
||||
|
||||
OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
|
||||
if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_WRITE_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
s->rwstate = SSL_NOTHING;
|
||||
i = do_dtls1_write(s, type, buf, len, 0);
|
||||
i = do_dtls1_write(s, type, buf, len, 0, written);
|
||||
return i;
|
||||
}
|
||||
|
||||
int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int len, int create_empty_fragment)
|
||||
size_t len, int create_empty_fragment, size_t *written)
|
||||
{
|
||||
unsigned char *p, *pseq;
|
||||
int i, mac_size, clear = 0;
|
||||
int prefix_len = 0;
|
||||
size_t prefix_len = 0;
|
||||
int eivlen;
|
||||
SSL3_RECORD wr;
|
||||
SSL3_BUFFER *wb;
|
||||
@@ -995,24 +779,26 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
* 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 (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
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);
|
||||
if (len > ssl_get_max_send_fragment(s)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1026,8 +812,11 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
mac_size = 0;
|
||||
else {
|
||||
mac_size = EVP_MD_CTX_size(s->write_hash);
|
||||
if (mac_size < 0)
|
||||
goto err;
|
||||
if (mac_size < 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
p = SSL3_BUFFER_get_buf(wb) + prefix_len;
|
||||
@@ -1074,7 +863,7 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
|
||||
/* 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_length(&wr, len);
|
||||
SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
|
||||
|
||||
/*
|
||||
@@ -1084,8 +873,9 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
/* 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;
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
SSL_R_COMPRESSION_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
|
||||
@@ -1099,11 +889,14 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
* 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;
|
||||
if (!SSL_WRITE_ETM(s) && mac_size != 0) {
|
||||
if (!s->method->ssl3_enc->mac(s, &wr,
|
||||
&(p[SSL3_RECORD_get_length(&wr) + eivlen]),
|
||||
1)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
SSL3_RECORD_add_length(&wr, mac_size);
|
||||
}
|
||||
|
||||
@@ -1114,24 +907,30 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
if (eivlen)
|
||||
SSL3_RECORD_add_length(&wr, eivlen);
|
||||
|
||||
if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)
|
||||
goto err;
|
||||
if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1) {
|
||||
if (!ossl_statem_in_error(s)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (SSL_WRITE_ETM(s) && mac_size != 0) {
|
||||
if (!s->method->ssl3_enc->mac(s, &wr,
|
||||
&(p[SSL3_RECORD_get_length(&wr)]), 1)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
SSL3_RECORD_add_length(&wr, mac_size);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
@@ -1154,7 +953,8 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
* we are in a recursive call; just return the length, don't write
|
||||
* out anything here
|
||||
*/
|
||||
return wr.length;
|
||||
*written = wr.length;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* now let's set up wb */
|
||||
@@ -1170,10 +970,8 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *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;
|
||||
/* we now just need to write the buffer. Calls SSLfatal() as required. */
|
||||
return ssl3_write_pending(s, type, buf, len, written);
|
||||
}
|
||||
|
||||
DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
|
||||
|
||||
+560
-379
@@ -10,12 +10,12 @@
|
||||
#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"
|
||||
#include "../packet_locl.h"
|
||||
|
||||
#if defined(OPENSSL_SMALL_FOOTPRINT) || \
|
||||
!( defined(AES_ASM) && ( \
|
||||
@@ -46,8 +46,6 @@ void RECORD_LAYER_clear(RECORD_LAYER *rl)
|
||||
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;
|
||||
@@ -100,22 +98,6 @@ int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
|
||||
&& 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));
|
||||
@@ -126,10 +108,9 @@ void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl)
|
||||
memset(rl->write_sequence, 0, sizeof(rl->write_sequence));
|
||||
}
|
||||
|
||||
int ssl3_pending(const SSL *s)
|
||||
size_t ssl3_pending(const SSL *s)
|
||||
{
|
||||
unsigned int i;
|
||||
int num = 0;
|
||||
size_t i, num = 0;
|
||||
|
||||
if (s->rlayer.rstate == SSL_ST_READ_BODY)
|
||||
return 0;
|
||||
@@ -185,7 +166,8 @@ const char *SSL_rstate_string(const SSL *s)
|
||||
/*
|
||||
* Return values are as per SSL_read()
|
||||
*/
|
||||
int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
|
||||
size_t *readbytes)
|
||||
{
|
||||
/*
|
||||
* If extend == 0, obtain new n-byte packet; if extend == 1, increase
|
||||
@@ -196,18 +178,19 @@ int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
* 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;
|
||||
size_t len, left, align = 0;
|
||||
unsigned char *pkt;
|
||||
SSL3_BUFFER *rb;
|
||||
|
||||
if (n <= 0)
|
||||
return n;
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
rb = &s->rlayer.rbuf;
|
||||
if (rb->buf == NULL)
|
||||
if (!ssl3_setup_read_buffer(s))
|
||||
if (!ssl3_setup_read_buffer(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
|
||||
left = rb->left;
|
||||
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
||||
@@ -272,13 +255,16 @@ int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
s->rlayer.packet_length += n;
|
||||
rb->left = left - n;
|
||||
rb->offset += n;
|
||||
return (n);
|
||||
*readbytes = n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
if (n > rb->len - rb->offset) {
|
||||
/* does not happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_N,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -289,11 +275,14 @@ int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
else {
|
||||
if (max < n)
|
||||
max = n;
|
||||
if (max > (int)(rb->len - rb->offset))
|
||||
if (max > rb->len - rb->offset)
|
||||
max = rb->len - rb->offset;
|
||||
}
|
||||
|
||||
while (left < n) {
|
||||
size_t bioread = 0;
|
||||
int ret;
|
||||
|
||||
/*
|
||||
* 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
|
||||
@@ -303,20 +292,24 @@ int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
clear_sys_error();
|
||||
if (s->rbio != NULL) {
|
||||
s->rwstate = SSL_READING;
|
||||
i = BIO_read(s->rbio, pkt + len + left, max - left);
|
||||
/* TODO(size_t): Convert this function */
|
||||
ret = BIO_read(s->rbio, pkt + len + left, max - left);
|
||||
if (ret >= 0)
|
||||
bioread = ret;
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL3_READ_N, SSL_R_READ_BIO_NOT_SET);
|
||||
i = -1;
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_N,
|
||||
SSL_R_READ_BIO_NOT_SET);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
if (i <= 0) {
|
||||
if (ret <= 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;
|
||||
return ret;
|
||||
}
|
||||
left += i;
|
||||
left += bioread;
|
||||
/*
|
||||
* reads should *never* span multiple packets for DTLS because the
|
||||
* underlying transport protocol is message oriented as opposed to
|
||||
@@ -333,55 +326,65 @@ int ssl3_read_n(SSL *s, int n, int max, int extend, int clearold)
|
||||
rb->left = left - n;
|
||||
s->rlayer.packet_length += n;
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (n);
|
||||
*readbytes = n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
int ssl3_write_bytes(SSL *s, int type, const void *buf_, size_t len,
|
||||
size_t *written)
|
||||
{
|
||||
const unsigned char *buf = buf_;
|
||||
int tot;
|
||||
unsigned int n, split_send_fragment, maxpipes;
|
||||
size_t tot;
|
||||
size_t n, max_send_fragment, 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;
|
||||
size_t nw;
|
||||
#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;
|
||||
}
|
||||
size_t tmpwrit;
|
||||
|
||||
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
|
||||
* than 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);
|
||||
if ((len < s->rlayer.wnum)
|
||||
|| ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
|
||||
SSL_R_BAD_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->early_data_state == SSL_EARLY_DATA_WRITING
|
||||
&& !early_data_count_ok(s, len, 0, 1)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
|
||||
s->rlayer.wnum = 0;
|
||||
|
||||
if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)) {
|
||||
/*
|
||||
* When writing early data on the server side we could be "in_init" in
|
||||
* between receiving the EoED and the CF - but we don't want to handle those
|
||||
* messages yet.
|
||||
*/
|
||||
if (SSL_in_init(s) && !ossl_statem_get_in_handshake(s)
|
||||
&& s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) {
|
||||
i = s->handshake_func(s);
|
||||
/* SSLfatal() already called */
|
||||
if (i < 0)
|
||||
return (i);
|
||||
return i;
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -391,13 +394,15 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
* will happen with non blocking IO
|
||||
*/
|
||||
if (wb->left != 0) {
|
||||
i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot);
|
||||
/* SSLfatal() already called if appropriate */
|
||||
i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot,
|
||||
&tmpwrit);
|
||||
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 */
|
||||
tot += tmpwrit; /* this might be last fragment */
|
||||
}
|
||||
#if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
|
||||
/*
|
||||
@@ -407,14 +412,15 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
* compromise is considered worthy.
|
||||
*/
|
||||
if (type == SSL3_RT_APPLICATION_DATA &&
|
||||
u_len >= 4 * (max_send_fragment = s->max_send_fragment) &&
|
||||
len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s)) &&
|
||||
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;
|
||||
size_t packlen;
|
||||
int packleni;
|
||||
|
||||
/* minimize address aliasing conflicts */
|
||||
if ((max_send_fragment & 0xfff) == 0)
|
||||
@@ -425,21 +431,22 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
|
||||
packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
||||
EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
|
||||
max_send_fragment, NULL);
|
||||
(int)max_send_fragment, NULL);
|
||||
|
||||
if (u_len >= 8 * max_send_fragment)
|
||||
if (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);
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
} else if (tot == len) { /* done? */
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
return tot;
|
||||
*written = tot;
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = (len - tot);
|
||||
@@ -453,6 +460,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
if (s->s3->alert_dispatch) {
|
||||
i = s->method->ssl_dispatch_alert(s);
|
||||
if (i <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
@@ -473,11 +481,11 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
mb_param.inp = aad;
|
||||
mb_param.len = nw;
|
||||
|
||||
packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
|
||||
packleni = 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 */
|
||||
packlen = (size_t)packleni;
|
||||
if (packleni <= 0 || packlen > wb->len) { /* never happens */
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
break;
|
||||
@@ -506,8 +514,9 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
s->rlayer.wpend_type = type;
|
||||
s->rlayer.wpend_ret = nw;
|
||||
|
||||
i = ssl3_write_pending(s, type, &buf[tot], nw);
|
||||
i = ssl3_write_pending(s, type, &buf[tot], nw, &tmpwrit);
|
||||
if (i <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
@@ -515,26 +524,29 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
if (i == (int)n) {
|
||||
if (tmpwrit == n) {
|
||||
/* free jumbo buffer */
|
||||
ssl3_release_write_buffer(s);
|
||||
return tot + i;
|
||||
*written = tot + tmpwrit;
|
||||
return 1;
|
||||
}
|
||||
n -= i;
|
||||
tot += i;
|
||||
n -= tmpwrit;
|
||||
tot += tmpwrit;
|
||||
}
|
||||
} else
|
||||
#endif
|
||||
#endif /* !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK */
|
||||
if (tot == len) { /* done? */
|
||||
if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s))
|
||||
ssl3_release_write_buffer(s);
|
||||
|
||||
return tot;
|
||||
*written = tot;
|
||||
return 1;
|
||||
}
|
||||
|
||||
n = (len - tot);
|
||||
|
||||
split_send_fragment = s->split_send_fragment;
|
||||
max_send_fragment = ssl_get_max_send_fragment(s);
|
||||
split_send_fragment = ssl_get_split_send_fragment(s);
|
||||
/*
|
||||
* If max_pipelines is 0 then this means "undefined" and we default to
|
||||
* 1 pipeline. Similarly if the cipher does not support pipelined
|
||||
@@ -547,7 +559,8 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
* 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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
if (maxpipes == 0
|
||||
@@ -556,19 +569,20 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
& 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) {
|
||||
if (max_send_fragment == 0 || split_send_fragment == 0
|
||||
|| split_send_fragment > max_send_fragment) {
|
||||
/*
|
||||
* We should have prevented this when we set the split and max send
|
||||
* We should have prevented this when we set/get the split and max send
|
||||
* fragments so we shouldn't get here
|
||||
*/
|
||||
SSLerr(SSL_F_SSL3_WRITE_BYTES, ERR_R_INTERNAL_ERROR);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
unsigned int pipelens[SSL_MAX_PIPELINES], tmppipelen, remain;
|
||||
unsigned int numpipes, j;
|
||||
size_t pipelens[SSL_MAX_PIPELINES], tmppipelen, remain;
|
||||
size_t numpipes, j;
|
||||
|
||||
if (n == 0)
|
||||
numpipes = 1;
|
||||
@@ -577,13 +591,13 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
if (numpipes > maxpipes)
|
||||
numpipes = maxpipes;
|
||||
|
||||
if (n / numpipes >= s->max_send_fragment) {
|
||||
if (n / numpipes >= 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;
|
||||
pipelens[j] = max_send_fragment;
|
||||
}
|
||||
} else {
|
||||
/* We can partially fill all available pipelines */
|
||||
@@ -596,14 +610,16 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
}
|
||||
}
|
||||
|
||||
i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0);
|
||||
i = do_ssl3_write(s, type, &(buf[tot]), pipelens, numpipes, 0,
|
||||
&tmpwrit);
|
||||
if (i <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
/* XXX should we ssl3_release_write_buffer if i<0? */
|
||||
s->rlayer.wnum = tot;
|
||||
return i;
|
||||
}
|
||||
|
||||
if ((i == (int)n) ||
|
||||
if (tmpwrit == n ||
|
||||
(type == SSL3_RT_APPLICATION_DATA &&
|
||||
(s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
|
||||
/*
|
||||
@@ -616,28 +632,32 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
|
||||
!SSL_IS_DTLS(s))
|
||||
ssl3_release_write_buffer(s);
|
||||
|
||||
return tot + i;
|
||||
*written = tot + tmpwrit;
|
||||
return 1;
|
||||
}
|
||||
|
||||
n -= i;
|
||||
tot += i;
|
||||
n -= tmpwrit;
|
||||
tot += tmpwrit;
|
||||
}
|
||||
}
|
||||
|
||||
int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int *pipelens, unsigned int numpipes,
|
||||
int create_empty_fragment)
|
||||
size_t *pipelens, size_t numpipes,
|
||||
int create_empty_fragment, size_t *written)
|
||||
{
|
||||
unsigned char *outbuf[SSL_MAX_PIPELINES], *plen[SSL_MAX_PIPELINES];
|
||||
WPACKET pkt[SSL_MAX_PIPELINES];
|
||||
SSL3_RECORD wr[SSL_MAX_PIPELINES];
|
||||
WPACKET *thispkt;
|
||||
SSL3_RECORD *thiswr;
|
||||
unsigned char *recordstart;
|
||||
int i, mac_size, clear = 0;
|
||||
int prefix_len = 0;
|
||||
int eivlen;
|
||||
size_t prefix_len = 0;
|
||||
int eivlen = 0;
|
||||
size_t align = 0;
|
||||
SSL3_BUFFER *wb;
|
||||
SSL_SESSION *sess;
|
||||
unsigned int totlen = 0;
|
||||
unsigned int j;
|
||||
size_t totlen = 0, len, wpinited = 0;
|
||||
size_t j;
|
||||
|
||||
for (j = 0; j < numpipes; j++)
|
||||
totlen += pipelens[j];
|
||||
@@ -645,20 +665,27 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
* 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 (RECORD_LAYER_write_pending(&s->rlayer)) {
|
||||
/* Calls SSLfatal() as required */
|
||||
return ssl3_write_pending(s, type, buf, totlen, written);
|
||||
}
|
||||
|
||||
/* 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 (i <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
return i;
|
||||
}
|
||||
/* if it went, fall through and send more stuff */
|
||||
}
|
||||
|
||||
if (s->rlayer.numwpipes < numpipes)
|
||||
if (!ssl3_setup_write_buffer(s, numpipes, 0))
|
||||
if (s->rlayer.numwpipes < numpipes) {
|
||||
if (!ssl3_setup_write_buffer(s, numpipes, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (totlen == 0 && !create_empty_fragment)
|
||||
return 0;
|
||||
@@ -670,9 +697,13 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
clear = s->enc_write_ctx ? 0 : 1; /* must be AEAD cipher */
|
||||
mac_size = 0;
|
||||
} else {
|
||||
/* TODO(siz_t): Convert me */
|
||||
mac_size = EVP_MD_CTX_size(s->write_hash);
|
||||
if (mac_size < 0)
|
||||
if (mac_size < 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -691,16 +722,20 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
* 'prefix_len' bytes are sent out later together with the actual
|
||||
* payload)
|
||||
*/
|
||||
unsigned int tmppipelen = 0;
|
||||
size_t tmppipelen = 0;
|
||||
int ret;
|
||||
|
||||
prefix_len = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1);
|
||||
if (prefix_len <= 0)
|
||||
ret = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1, &prefix_len);
|
||||
if (ret <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
@@ -719,136 +754,318 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
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);
|
||||
if (!WPACKET_init_static_len(&pkt[0], SSL3_BUFFER_get_buf(wb),
|
||||
SSL3_BUFFER_get_len(wb), 0)
|
||||
|| !WPACKET_allocate_bytes(&pkt[0], align, NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
wpinited = 1;
|
||||
} else if (prefix_len) {
|
||||
wb = &s->rlayer.wbuf[0];
|
||||
outbuf[0] = SSL3_BUFFER_get_buf(wb) + SSL3_BUFFER_get_offset(wb)
|
||||
+ prefix_len;
|
||||
if (!WPACKET_init_static_len(&pkt[0],
|
||||
SSL3_BUFFER_get_buf(wb),
|
||||
SSL3_BUFFER_get_len(wb), 0)
|
||||
|| !WPACKET_allocate_bytes(&pkt[0], SSL3_BUFFER_get_offset(wb)
|
||||
+ prefix_len, NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
wpinited = 1;
|
||||
} else {
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
thispkt = &pkt[j];
|
||||
|
||||
wb = &s->rlayer.wbuf[j];
|
||||
#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
|
||||
#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);
|
||||
if (!WPACKET_init_static_len(thispkt, SSL3_BUFFER_get_buf(wb),
|
||||
SSL3_BUFFER_get_len(wb), 0)
|
||||
|| !WPACKET_allocate_bytes(thispkt, align, NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
wpinited++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Explicit IV length, block ciphers appropriate version flag */
|
||||
if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s)) {
|
||||
if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s) && !SSL_TREAT_AS_TLS13(s)) {
|
||||
int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
|
||||
if (mode == EVP_CIPH_CBC_MODE) {
|
||||
/* TODO(size_t): Convert me */
|
||||
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)
|
||||
} else if (mode == EVP_CIPH_GCM_MODE) {
|
||||
/* Need explicit part of IV for GCM mode */
|
||||
eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
else if (mode == EVP_CIPH_CCM_MODE)
|
||||
} 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);
|
||||
unsigned int version = SSL_TREAT_AS_TLS13(s) ? TLS1_2_VERSION
|
||||
: s->version;
|
||||
unsigned char *compressdata = NULL;
|
||||
size_t maxcomplen;
|
||||
unsigned int rectype;
|
||||
|
||||
*(outbuf[j]++) = (s->version >> 8);
|
||||
thispkt = &pkt[j];
|
||||
thiswr = &wr[j];
|
||||
|
||||
SSL3_RECORD_set_type(thiswr, type);
|
||||
/*
|
||||
* In TLSv1.3, once encrypting, we always use application data for the
|
||||
* record type
|
||||
*/
|
||||
if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL)
|
||||
rectype = SSL3_RT_APPLICATION_DATA;
|
||||
else
|
||||
rectype = type;
|
||||
/*
|
||||
* 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;
|
||||
&& !s->renegotiate
|
||||
&& TLS1_get_version(s) > TLS1_VERSION
|
||||
&& s->hello_retry_request == SSL_HRR_NONE)
|
||||
version = TLS1_VERSION;
|
||||
|
||||
/* field where we are to write out packet length */
|
||||
plen[j] = outbuf[j];
|
||||
outbuf[j] += 2;
|
||||
maxcomplen = pipelens[j];
|
||||
if (s->compress != NULL)
|
||||
maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
||||
|
||||
/* write the header */
|
||||
if (!WPACKET_put_bytes_u8(thispkt, rectype)
|
||||
|| !WPACKET_put_bytes_u16(thispkt, version)
|
||||
|| !WPACKET_start_sub_packet_u16(thispkt)
|
||||
|| (eivlen > 0
|
||||
&& !WPACKET_allocate_bytes(thispkt, eivlen, NULL))
|
||||
|| (maxcomplen > 0
|
||||
&& !WPACKET_reserve_bytes(thispkt, maxcomplen,
|
||||
&compressdata))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* 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]);
|
||||
SSL3_RECORD_set_data(thiswr, compressdata);
|
||||
SSL3_RECORD_set_length(thiswr, pipelens[j]);
|
||||
SSL3_RECORD_set_input(thiswr, (unsigned char *)&buf[totlen]);
|
||||
totlen += pipelens[j];
|
||||
|
||||
/*
|
||||
* we now 'read' from wr->input, wr->length bytes into wr->data
|
||||
* we now 'read' from thiswr->input, thiswr->length bytes into
|
||||
* thiswr->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);
|
||||
if (!ssl3_do_compress(s, thiswr)
|
||||
|| !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
SSL_R_COMPRESSION_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
memcpy(wr[j].data, wr[j].input, wr[j].length);
|
||||
if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
SSL3_RECORD_reset_input(&wr[j]);
|
||||
}
|
||||
|
||||
if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) {
|
||||
size_t rlen, max_send_fragment;
|
||||
|
||||
if (!WPACKET_put_bytes_u8(thispkt, type)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
SSL3_RECORD_add_length(thiswr, 1);
|
||||
|
||||
/* Add TLS1.3 padding */
|
||||
max_send_fragment = ssl_get_max_send_fragment(s);
|
||||
rlen = SSL3_RECORD_get_length(thiswr);
|
||||
if (rlen < max_send_fragment) {
|
||||
size_t padding = 0;
|
||||
size_t max_padding = max_send_fragment - rlen;
|
||||
if (s->record_padding_cb != NULL) {
|
||||
padding = s->record_padding_cb(s, type, rlen, s->record_padding_arg);
|
||||
} else if (s->block_padding > 0) {
|
||||
size_t mask = s->block_padding - 1;
|
||||
size_t remainder;
|
||||
|
||||
/* optimize for power of 2 */
|
||||
if ((s->block_padding & mask) == 0)
|
||||
remainder = rlen & mask;
|
||||
else
|
||||
remainder = rlen % s->block_padding;
|
||||
/* don't want to add a block of padding if we don't have to */
|
||||
if (remainder == 0)
|
||||
padding = 0;
|
||||
else
|
||||
padding = s->block_padding - remainder;
|
||||
}
|
||||
if (padding > 0) {
|
||||
/* do not allow the record to exceed max plaintext length */
|
||||
if (padding > max_padding)
|
||||
padding = max_padding;
|
||||
if (!WPACKET_memset(thispkt, 0, padding)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
SSL3_RECORD_add_length(thiswr, padding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
* we should still have the output to thiswr->data and the input from
|
||||
* wr->input. Length should be thiswr->length. thiswr->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)
|
||||
unsigned char *mac;
|
||||
|
||||
if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
|
||||
|| !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
SSL3_RECORD_add_length(&wr[j], mac_size);
|
||||
}
|
||||
}
|
||||
|
||||
SSL3_RECORD_set_data(&wr[j], outbuf[j]);
|
||||
SSL3_RECORD_reset_input(&wr[j]);
|
||||
/*
|
||||
* Reserve some bytes for any growth that may occur during encryption.
|
||||
* This will be at most one cipher block or the tag length if using
|
||||
* AEAD. SSL_RT_MAX_CIPHER_BLOCK_SIZE covers either case.
|
||||
*/
|
||||
if (!WPACKET_reserve_bytes(thispkt, SSL_RT_MAX_CIPHER_BLOCK_SIZE,
|
||||
NULL)
|
||||
/*
|
||||
* We also need next the amount of bytes written to this
|
||||
* sub-packet
|
||||
*/
|
||||
|| !WPACKET_get_length(thispkt, &len)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (eivlen) {
|
||||
/*
|
||||
* if (RAND_pseudo_bytes(p, eivlen) <= 0) goto err;
|
||||
*/
|
||||
SSL3_RECORD_add_length(&wr[j], eivlen);
|
||||
/* Get a pointer to the start of this record excluding header */
|
||||
recordstart = WPACKET_get_curr(thispkt) - len;
|
||||
|
||||
SSL3_RECORD_set_data(thiswr, recordstart);
|
||||
SSL3_RECORD_reset_input(thiswr);
|
||||
SSL3_RECORD_set_length(thiswr, len);
|
||||
}
|
||||
|
||||
if (s->early_data_state == SSL_EARLY_DATA_WRITING
|
||||
|| s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
|
||||
/*
|
||||
* We haven't actually negotiated the version yet, but we're trying to
|
||||
* send early data - so we need to use the tls13enc function.
|
||||
*/
|
||||
if (tls13_enc(s, wr, numpipes, 1) < 1) {
|
||||
if (!ossl_statem_in_error(s)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
}
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1) {
|
||||
if (!ossl_statem_in_error(s)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
}
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1)
|
||||
goto err;
|
||||
|
||||
for (j = 0; j < numpipes; j++) {
|
||||
size_t origlen;
|
||||
|
||||
thispkt = &pkt[j];
|
||||
thiswr = &wr[j];
|
||||
|
||||
/* Allocate bytes for the encryption overhead */
|
||||
if (!WPACKET_get_length(thispkt, &origlen)
|
||||
/* Encryption should never shrink the data! */
|
||||
|| origlen > thiswr->length
|
||||
|| (thiswr->length > origlen
|
||||
&& !WPACKET_allocate_bytes(thispkt,
|
||||
thiswr->length - origlen, NULL))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (SSL_WRITE_ETM(s) && mac_size != 0) {
|
||||
if (s->method->ssl3_enc->mac(s, &wr[j],
|
||||
outbuf[j] + wr[j].length, 1) < 0)
|
||||
unsigned char *mac;
|
||||
|
||||
if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)
|
||||
|| !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
SSL3_RECORD_add_length(&wr[j], mac_size);
|
||||
}
|
||||
SSL3_RECORD_add_length(thiswr, mac_size);
|
||||
}
|
||||
|
||||
/* record length after mac and block padding */
|
||||
s2n(SSL3_RECORD_get_length(&wr[j]), plen[j]);
|
||||
if (!WPACKET_get_length(thispkt, &len)
|
||||
|| !WPACKET_close(thispkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(1, 0, SSL3_RT_HEADER, plen[j] - 5, 5, s,
|
||||
if (s->msg_callback) {
|
||||
recordstart = WPACKET_get_curr(thispkt) - len
|
||||
- SSL3_RT_HEADER_LENGTH;
|
||||
s->msg_callback(1, 0, SSL3_RT_HEADER, recordstart,
|
||||
SSL3_RT_HEADER_LENGTH, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) {
|
||||
unsigned char ctype = type;
|
||||
|
||||
s->msg_callback(1, s->version, SSL3_RT_INNER_CONTENT_TYPE,
|
||||
&ctype, 1, s, s->msg_callback_arg);
|
||||
}
|
||||
}
|
||||
|
||||
if (!WPACKET_finish(thispkt)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* we should now have wr->data pointing to the encrypted data, which is
|
||||
* wr->length long
|
||||
* we should now have thiswr->data pointing to the encrypted data, which
|
||||
* is thiswr->length long
|
||||
*/
|
||||
SSL3_RECORD_set_type(&wr[j], type); /* not needed but helps for
|
||||
SSL3_RECORD_set_type(thiswr, type); /* not needed but helps for
|
||||
* debugging */
|
||||
SSL3_RECORD_add_length(&wr[j], SSL3_RT_HEADER_LENGTH);
|
||||
SSL3_RECORD_add_length(thiswr, SSL3_RT_HEADER_LENGTH);
|
||||
|
||||
if (create_empty_fragment) {
|
||||
/*
|
||||
@@ -857,15 +1074,17 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
*/
|
||||
if (j > 0) {
|
||||
/* We should never be pipelining an empty fragment!! */
|
||||
SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_SSL3_WRITE,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
return SSL3_RECORD_get_length(wr);
|
||||
*written = SSL3_RECORD_get_length(thiswr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* now let's set up wb */
|
||||
SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],
|
||||
prefix_len + SSL3_RECORD_get_length(&wr[j]));
|
||||
prefix_len + SSL3_RECORD_get_length(thiswr));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -878,8 +1097,10 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
s->rlayer.wpend_ret = totlen;
|
||||
|
||||
/* we now just need to write the buffer */
|
||||
return ssl3_write_pending(s, type, buf, totlen);
|
||||
return ssl3_write_pending(s, type, buf, totlen, written);
|
||||
err:
|
||||
for (j = 0; j < wpinited; j++)
|
||||
WPACKET_cleanup(&pkt[j]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -887,19 +1108,21 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
*
|
||||
* Return values are as per SSL_write()
|
||||
*/
|
||||
int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
||||
unsigned int len)
|
||||
int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
|
||||
size_t *written)
|
||||
{
|
||||
int i;
|
||||
SSL3_BUFFER *wb = s->rlayer.wbuf;
|
||||
unsigned int currbuf = 0;
|
||||
size_t currbuf = 0;
|
||||
size_t tmpwrit = 0;
|
||||
|
||||
if ((s->rlayer.wpend_tot > (int)len)
|
||||
if ((s->rlayer.wpend_tot > 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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,
|
||||
SSL_R_BAD_WRITE_RETRY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
@@ -912,21 +1135,26 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
||||
clear_sys_error();
|
||||
if (s->wbio != NULL) {
|
||||
s->rwstate = SSL_WRITING;
|
||||
/* TODO(size_t): Convert this call */
|
||||
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]));
|
||||
if (i >= 0)
|
||||
tmpwrit = i;
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BIO_NOT_SET);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_WRITE_PENDING,
|
||||
SSL_R_BIO_NOT_SET);
|
||||
i = -1;
|
||||
}
|
||||
if (i == SSL3_BUFFER_get_left(&wb[currbuf])) {
|
||||
if (i > 0 && tmpwrit == SSL3_BUFFER_get_left(&wb[currbuf])) {
|
||||
SSL3_BUFFER_set_left(&wb[currbuf], 0);
|
||||
SSL3_BUFFER_add_offset(&wb[currbuf], i);
|
||||
SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
|
||||
if (currbuf + 1 < s->rlayer.numwpipes)
|
||||
continue;
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (s->rlayer.wpend_ret);
|
||||
*written = s->rlayer.wpend_ret;
|
||||
return 1;
|
||||
} else if (i <= 0) {
|
||||
if (SSL_IS_DTLS(s)) {
|
||||
/*
|
||||
@@ -937,8 +1165,8 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
||||
}
|
||||
return i;
|
||||
}
|
||||
SSL3_BUFFER_add_offset(&wb[currbuf], i);
|
||||
SSL3_BUFFER_add_left(&wb[currbuf], -i);
|
||||
SSL3_BUFFER_add_offset(&wb[currbuf], tmpwrit);
|
||||
SSL3_BUFFER_sub_left(&wb[currbuf], tmpwrit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -972,10 +1200,10 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
|
||||
* none of our business
|
||||
*/
|
||||
int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
int len, int peek)
|
||||
size_t len, int peek, size_t *readbytes)
|
||||
{
|
||||
int al, i, j, ret;
|
||||
unsigned int n, curr_rec, num_recs, read_bytes;
|
||||
int i, j, ret;
|
||||
size_t n, curr_rec, num_recs, totalbytes;
|
||||
SSL3_RECORD *rr;
|
||||
SSL3_BUFFER *rbuf;
|
||||
void (*cb) (const SSL *ssl, int type2, int val) = NULL;
|
||||
@@ -984,15 +1212,18 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
|
||||
if (!SSL3_BUFFER_is_initialised(rbuf)) {
|
||||
/* Not initialized yet */
|
||||
if (!ssl3_setup_read_buffer(s))
|
||||
return (-1);
|
||||
if (!ssl3_setup_read_buffer(s)) {
|
||||
/* SSLfatal() already called */
|
||||
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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1018,7 +1249,8 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
if (recvd_type != NULL)
|
||||
*recvd_type = SSL3_RT_HANDSHAKE;
|
||||
|
||||
return n;
|
||||
*readbytes = n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1028,12 +1260,11 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) {
|
||||
/* type == SSL3_RT_APPLICATION_DATA */
|
||||
i = s->handshake_func(s);
|
||||
/* SSLfatal() already called */
|
||||
if (i < 0)
|
||||
return (i);
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
}
|
||||
return i;
|
||||
if (i == 0)
|
||||
return -1;
|
||||
}
|
||||
start:
|
||||
s->rwstate = SSL_NOTHING;
|
||||
@@ -1052,14 +1283,16 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
/* get new records if necessary */
|
||||
if (num_recs == 0) {
|
||||
ret = ssl3_get_record(s);
|
||||
if (ret <= 0)
|
||||
return (ret);
|
||||
if (ret <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Skip over any records we have already read */
|
||||
@@ -1087,9 +1320,9 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1099,12 +1332,13 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
s->rwstate = SSL_NOTHING;
|
||||
return (0);
|
||||
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)) {
|
||||
&& type == SSL3_RT_HANDSHAKE && recvd_type != NULL
|
||||
&& !SSL_IS_TLS13(s))) {
|
||||
/*
|
||||
* SSL3_RT_APPLICATION_DATA or
|
||||
* SSL3_RT_HANDSHAKE or
|
||||
@@ -1116,23 +1350,23 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
*/
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_APP_DATA_IN_HANDSHAKE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_CCS_RECEIVED_EARLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (recvd_type != NULL)
|
||||
*recvd_type = SSL3_RECORD_get_type(rr);
|
||||
|
||||
if (len <= 0) {
|
||||
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
|
||||
@@ -1140,15 +1374,15 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
*/
|
||||
if (SSL3_RECORD_get_length(rr) == 0)
|
||||
SSL3_RECORD_set_read(rr);
|
||||
return len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
read_bytes = 0;
|
||||
totalbytes = 0;
|
||||
do {
|
||||
if ((unsigned int)len - read_bytes > SSL3_RECORD_get_length(rr))
|
||||
if (len - totalbytes > SSL3_RECORD_get_length(rr))
|
||||
n = SSL3_RECORD_get_length(rr);
|
||||
else
|
||||
n = (unsigned int)len - read_bytes;
|
||||
n = len - totalbytes;
|
||||
|
||||
memcpy(buf, &(rr->data[rr->off]), n);
|
||||
buf += n;
|
||||
@@ -1170,10 +1404,10 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
curr_rec++;
|
||||
rr++;
|
||||
}
|
||||
read_bytes += n;
|
||||
totalbytes += n;
|
||||
} while (type == SSL3_RT_APPLICATION_DATA && curr_rec < num_recs
|
||||
&& read_bytes < (unsigned int)len);
|
||||
if (read_bytes == 0) {
|
||||
&& totalbytes < len);
|
||||
if (totalbytes == 0) {
|
||||
/* We must have read empty records. Get more data */
|
||||
goto start;
|
||||
}
|
||||
@@ -1181,7 +1415,8 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
&& (s->mode & SSL_MODE_RELEASE_BUFFERS)
|
||||
&& SSL3_BUFFER_get_left(rbuf) == 0)
|
||||
ssl3_release_read_buffer(s);
|
||||
return read_bytes;
|
||||
*readbytes = totalbytes;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1200,9 +1435,9 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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;
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_READ_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (s->method->version == TLS_ANY_VERSION
|
||||
@@ -1214,9 +1449,9 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_UNEXPECTED_MESSAGE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1224,18 +1459,14 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* that so that we can process the data at a fixed place.
|
||||
*/
|
||||
{
|
||||
unsigned int dest_maxlen = 0;
|
||||
size_t dest_maxlen = 0;
|
||||
unsigned char *dest = NULL;
|
||||
unsigned int *dest_len = NULL;
|
||||
size_t *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) {
|
||||
@@ -1244,117 +1475,41 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
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) {
|
||||
memcpy(dest + *dest_len,
|
||||
SSL3_RECORD_get_data(rr) + SSL3_RECORD_get_off(rr), n);
|
||||
SSL3_RECORD_add_off(rr, n);
|
||||
SSL3_RECORD_sub_length(rr, n);
|
||||
*dest_len += n;
|
||||
if (SSL3_RECORD_get_length(rr) == 0)
|
||||
SSL3_RECORD_set_read(rr);
|
||||
|
||||
if (*dest_len < dest_maxlen)
|
||||
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 (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
|
||||
unsigned int alert_level, alert_descr;
|
||||
unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)
|
||||
+ SSL3_RECORD_get_off(rr);
|
||||
PACKET alert;
|
||||
|
||||
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 (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))
|
||||
|| !PACKET_get_1(&alert, &alert_level)
|
||||
|| !PACKET_get_1(&alert, &alert_descr)
|
||||
|| PACKET_remaining(&alert) != 0) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_INVALID_ALERT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,
|
||||
s->msg_callback_arg);
|
||||
|
||||
if (s->info_callback != NULL)
|
||||
@@ -1373,14 +1528,23 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_TOO_MANY_WARN_ALERTS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
|
||||
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* Apart from close_notify the only other warning alert in TLSv1.3
|
||||
* is user_cancelled - which we just ignore.
|
||||
*/
|
||||
if (SSL_IS_TLS13(s) && alert_descr != SSL_AD_USER_CANCELLED) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
* This is a warning but we receive it if we requested
|
||||
@@ -1390,31 +1554,28 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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;
|
||||
if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
|
||||
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_NO_RENEGOTIATION);
|
||||
return -1;
|
||||
}
|
||||
#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);
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, 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);
|
||||
return 0;
|
||||
} else {
|
||||
al = SSL_AD_ILLEGAL_PARAMETER;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_UNKNOWN_ALERT_TYPE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
goto start;
|
||||
@@ -1425,34 +1586,42 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
s->rwstate = SSL_NOTHING;
|
||||
SSL3_RECORD_set_length(rr, 0);
|
||||
SSL3_RECORD_set_read(rr);
|
||||
return (0);
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_CCS_RECEIVED_EARLY);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Unexpected handshake message (Client Hello, or protocol violation)
|
||||
* Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) 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;
|
||||
}
|
||||
&& !ossl_statem_get_in_handshake(s)) {
|
||||
int ined = (s->early_data_state == SSL_EARLY_DATA_READING);
|
||||
|
||||
/* We found handshake data, so we're going back into init */
|
||||
ossl_statem_set_in_init(s, 1);
|
||||
|
||||
i = s->handshake_func(s);
|
||||
/* SSLfatal() already called if appropriate */
|
||||
if (i < 0)
|
||||
return (i);
|
||||
return i;
|
||||
if (i == 0) {
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we were actually trying to read early data and we found a
|
||||
* handshake message, then we don't want to continue to try and read
|
||||
* the application data any more. It won't be "early" now.
|
||||
*/
|
||||
if (ined)
|
||||
return -1;
|
||||
|
||||
if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
|
||||
if (SSL3_BUFFER_get_left(rbuf) == 0) {
|
||||
/* no read-ahead left? */
|
||||
@@ -1467,7 +1636,7 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
bio = SSL_get_rbio(s);
|
||||
BIO_clear_retry_flags(bio);
|
||||
BIO_set_retry_read(bio);
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
goto start;
|
||||
@@ -1482,9 +1651,9 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_UNEXPECTED_RECORD);
|
||||
return -1;
|
||||
case SSL3_RT_CHANGE_CIPHER_SPEC:
|
||||
case SSL3_RT_ALERT:
|
||||
case SSL3_RT_HANDSHAKE:
|
||||
@@ -1493,9 +1662,9 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
* 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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
case SSL3_RT_APPLICATION_DATA:
|
||||
/*
|
||||
* At this point, we were expecting handshake data, but have
|
||||
@@ -1506,18 +1675,30 @@ int ssl3_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
|
||||
*/
|
||||
if (ossl_statem_app_data_allowed(s)) {
|
||||
s->s3->in_read_app_data = 2;
|
||||
return (-1);
|
||||
return -1;
|
||||
} else if (ossl_statem_skip_early_data(s)) {
|
||||
/*
|
||||
* This can happen after a client sends a CH followed by early_data,
|
||||
* but the server responds with a HelloRetryRequest. The server
|
||||
* reads the next record from the client expecting to find a
|
||||
* plaintext ClientHello but gets a record which appears to be
|
||||
* application data. The trial decrypt "works" because null
|
||||
* decryption was applied. We just skip it and move on to the next
|
||||
* record.
|
||||
*/
|
||||
if (!early_data_count_ok(s, rr->length,
|
||||
EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
SSL3_RECORD_set_read(rr);
|
||||
goto start;
|
||||
} else {
|
||||
al = SSL_AD_UNEXPECTED_MESSAGE;
|
||||
SSLerr(SSL_F_SSL3_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_READ_BYTES,
|
||||
SSL_R_UNEXPECTED_RECORD);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* not reached */
|
||||
|
||||
f_err:
|
||||
ssl3_send_alert(s, SSL3_AL_FATAL, al);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void ssl3_record_sequence_update(unsigned char *seq)
|
||||
@@ -1543,7 +1724,7 @@ int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
|
||||
/*
|
||||
* Returns the length in bytes of the current rrec
|
||||
*/
|
||||
unsigned int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl)
|
||||
size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl)
|
||||
{
|
||||
return SSL3_RECORD_get_length(&rl->rrec[0]);
|
||||
}
|
||||
+34
-45
@@ -22,9 +22,9 @@ typedef struct ssl3_buffer_st {
|
||||
/* buffer size */
|
||||
size_t len;
|
||||
/* where to 'copy from' */
|
||||
int offset;
|
||||
size_t offset;
|
||||
/* how many bytes left */
|
||||
int left;
|
||||
size_t left;
|
||||
} SSL3_BUFFER;
|
||||
|
||||
#define SEQ_NUM_SIZE 8
|
||||
@@ -38,16 +38,16 @@ typedef struct ssl3_record_st {
|
||||
int type;
|
||||
/* How many bytes available */
|
||||
/* rw */
|
||||
unsigned int length;
|
||||
size_t 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;
|
||||
size_t orig_len;
|
||||
/* read/write offset into 'buf' */
|
||||
/* r */
|
||||
unsigned int off;
|
||||
size_t off;
|
||||
/* pointer to the record data */
|
||||
/* rw */
|
||||
unsigned char *data;
|
||||
@@ -82,7 +82,7 @@ typedef struct record_pqueue_st {
|
||||
|
||||
typedef struct dtls1_record_data_st {
|
||||
unsigned char *packet;
|
||||
unsigned int packet_length;
|
||||
size_t packet_length;
|
||||
SSL3_BUFFER rbuf;
|
||||
SSL3_RECORD rrec;
|
||||
#ifndef OPENSSL_NO_SCTP
|
||||
@@ -111,14 +111,6 @@ typedef struct dtls_record_layer_st {
|
||||
* 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];
|
||||
@@ -143,9 +135,9 @@ typedef struct record_layer_st {
|
||||
/* where we are when reading */
|
||||
int rstate;
|
||||
/* How many pipelines can be used to read data */
|
||||
unsigned int numrpipes;
|
||||
size_t numrpipes;
|
||||
/* How many pipelines can be used to write data */
|
||||
unsigned int numwpipes;
|
||||
size_t numwpipes;
|
||||
/* read IO goes into here */
|
||||
SSL3_BUFFER rbuf;
|
||||
/* write IO goes into here */
|
||||
@@ -154,25 +146,19 @@ typedef struct record_layer_st {
|
||||
SSL3_RECORD rrec[SSL_MAX_PIPELINES];
|
||||
/* used internally to point at a raw packet */
|
||||
unsigned char *packet;
|
||||
unsigned int packet_length;
|
||||
size_t 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;
|
||||
size_t wnum;
|
||||
unsigned char handshake_fragment[4];
|
||||
unsigned int handshake_fragment_len;
|
||||
size_t handshake_fragment_len;
|
||||
/* The number of consecutive empty records we have received */
|
||||
unsigned int empty_record_count;
|
||||
size_t empty_record_count;
|
||||
/* partial write - check the numbers match */
|
||||
/* number bytes written */
|
||||
int wpend_tot;
|
||||
size_t wpend_tot;
|
||||
int wpend_type;
|
||||
/* number of bytes submitted */
|
||||
int wpend_ret;
|
||||
size_t wpend_ret;
|
||||
const unsigned char *wpend_buf;
|
||||
unsigned char read_sequence[SEQ_NUM_SIZE];
|
||||
unsigned char write_sequence[SEQ_NUM_SIZE];
|
||||
@@ -209,35 +195,38 @@ 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);
|
||||
size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
|
||||
__owur size_t ssl3_pending(const SSL *s);
|
||||
__owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
|
||||
size_t *written);
|
||||
int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
size_t *pipelens, size_t numpipes,
|
||||
int create_empty_fragment, size_t *written);
|
||||
__owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
|
||||
unsigned char *buf, int len, int peek);
|
||||
unsigned char *buf, size_t len, int peek,
|
||||
size_t *readbytes);
|
||||
__owur int ssl3_setup_buffers(SSL *s);
|
||||
__owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send);
|
||||
__owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t 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 ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
|
||||
size_t *written);
|
||||
__owur int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send);
|
||||
__owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
|
||||
__owur int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, 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);
|
||||
unsigned char *buf, size_t len, int peek,
|
||||
size_t *readbytes);
|
||||
__owur int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
|
||||
size_t *written);
|
||||
int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
|
||||
size_t len, int create_empty_fragment, size_t *written);
|
||||
void dtls1_reset_seq_numbers(SSL *s, int rw);
|
||||
+12
-11
@@ -38,9 +38,9 @@
|
||||
#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);
|
||||
__owur int ssl3_read_n(SSL *s, size_t n, size_t max, int extend, int clearold,
|
||||
size_t *readbytes);
|
||||
|
||||
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);
|
||||
@@ -61,7 +61,7 @@ void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
|
||||
#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_sub_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))
|
||||
@@ -69,10 +69,10 @@ void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
|
||||
#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_set_data(SSL3_BUFFER *b, const unsigned char *d, size_t 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);
|
||||
__owur int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len);
|
||||
int ssl3_release_read_buffer(SSL *s);
|
||||
int ssl3_release_write_buffer(SSL *s);
|
||||
|
||||
@@ -99,18 +99,19 @@ int ssl3_release_write_buffer(SSL *s);
|
||||
#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_clear(SSL3_RECORD *r, size_t);
|
||||
void SSL3_RECORD_release(SSL3_RECORD *r, size_t 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);
|
||||
int ssl3_cbc_copy_mac(unsigned char *out,
|
||||
const SSL3_RECORD *rec, size_t md_size);
|
||||
__owur int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size);
|
||||
size_t block_size, size_t mac_size);
|
||||
__owur int tls1_cbc_remove_padding(const SSL *s,
|
||||
SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size);
|
||||
size_t block_size, size_t mac_size);
|
||||
int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap);
|
||||
__owur int dtls1_get_record(SSL *s);
|
||||
int early_data_count_ok(SSL *s, size_t length, size_t overhead, int send);
|
||||
+35
-19
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2017 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
|
||||
@@ -10,7 +10,7 @@
|
||||
#include "../ssl_locl.h"
|
||||
#include "record_locl.h"
|
||||
|
||||
void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, int n)
|
||||
void SSL3_BUFFER_set_data(SSL3_BUFFER *b, const unsigned char *d, size_t n)
|
||||
{
|
||||
if (d != NULL)
|
||||
memcpy(b->buf, d, n);
|
||||
@@ -60,26 +60,30 @@ int ssl3_setup_read_buffer(SSL *s)
|
||||
#endif
|
||||
if (b->default_len > len)
|
||||
len = b->default_len;
|
||||
if ((p = OPENSSL_malloc(len)) == NULL)
|
||||
goto err;
|
||||
if ((p = OPENSSL_malloc(len)) == NULL) {
|
||||
/*
|
||||
* We've got a malloc failure, and we're still initialising buffers.
|
||||
* We assume we're so doomed that we won't even be able to send an
|
||||
* alert.
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_SETUP_READ_BUFFER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
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)
|
||||
int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len)
|
||||
{
|
||||
unsigned char *p;
|
||||
size_t align = 0, headerlen;
|
||||
SSL3_BUFFER *wb;
|
||||
unsigned int currpipe;
|
||||
size_t currpipe;
|
||||
|
||||
s->rlayer.numwpipes = numwpipes;
|
||||
|
||||
@@ -93,7 +97,7 @@ int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t len)
|
||||
align = (-SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1);
|
||||
#endif
|
||||
|
||||
len = s->max_send_fragment
|
||||
len = ssl_get_max_send_fragment(s)
|
||||
+ SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD + headerlen + align;
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
if (ssl_allow_compression(s))
|
||||
@@ -107,11 +111,23 @@ int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t len)
|
||||
for (currpipe = 0; currpipe < numwpipes; currpipe++) {
|
||||
SSL3_BUFFER *thiswb = &wb[currpipe];
|
||||
|
||||
if (thiswb->buf != NULL && thiswb->len != len) {
|
||||
OPENSSL_free(thiswb->buf);
|
||||
thiswb->buf = NULL; /* force reallocation */
|
||||
}
|
||||
|
||||
if (thiswb->buf == NULL) {
|
||||
p = OPENSSL_malloc(len);
|
||||
if (p == NULL) {
|
||||
s->rlayer.numwpipes = currpipe;
|
||||
goto err;
|
||||
/*
|
||||
* We've got a malloc failure, and we're still initialising
|
||||
* buffers. We assume we're so doomed that we won't even be able
|
||||
* to send an alert.
|
||||
*/
|
||||
SSLfatal(s, SSL_AD_NO_ALERT,
|
||||
SSL_F_SSL3_SETUP_WRITE_BUFFER, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
memset(thiswb, 0, sizeof(SSL3_BUFFER));
|
||||
thiswb->buf = p;
|
||||
@@ -120,25 +136,25 @@ int ssl3_setup_write_buffer(SSL *s, unsigned int numwpipes, size_t 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))
|
||||
if (!ssl3_setup_read_buffer(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
if (!ssl3_setup_write_buffer(s, 1, 0))
|
||||
}
|
||||
if (!ssl3_setup_write_buffer(s, 1, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ssl3_release_write_buffer(SSL *s)
|
||||
{
|
||||
SSL3_BUFFER *wb;
|
||||
unsigned int pipes;
|
||||
size_t pipes;
|
||||
|
||||
pipes = s->rlayer.numwpipes;
|
||||
while (pipes > 0) {
|
||||
|
||||
+675
-313
@@ -7,11 +7,11 @@
|
||||
* 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"
|
||||
#include "internal/cryptlib.h"
|
||||
|
||||
static const unsigned char ssl3_pad_1[48] = {
|
||||
0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
|
||||
@@ -34,10 +34,10 @@ static const unsigned char ssl3_pad_2[48] = {
|
||||
/*
|
||||
* Clear the contents of an SSL3_RECORD but retain any memory allocated
|
||||
*/
|
||||
void SSL3_RECORD_clear(SSL3_RECORD *r, unsigned int num_recs)
|
||||
void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
|
||||
{
|
||||
unsigned char *comp;
|
||||
unsigned int i;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < num_recs; i++) {
|
||||
comp = r[i].comp;
|
||||
@@ -47,9 +47,9 @@ 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_release(SSL3_RECORD *r, size_t num_recs)
|
||||
{
|
||||
unsigned int i;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < num_recs; i++) {
|
||||
OPENSSL_free(r[i].comp);
|
||||
@@ -69,7 +69,7 @@ void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
|
||||
static int ssl3_record_app_data_waiting(SSL *s)
|
||||
{
|
||||
SSL3_BUFFER *rbuf;
|
||||
int left, len;
|
||||
size_t left, len;
|
||||
unsigned char *p;
|
||||
|
||||
rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
@@ -101,6 +101,48 @@ static int ssl3_record_app_data_waiting(SSL *s)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int early_data_count_ok(SSL *s, size_t length, size_t overhead, int send)
|
||||
{
|
||||
uint32_t max_early_data = s->max_early_data;
|
||||
SSL_SESSION *sess = s->session;
|
||||
|
||||
/*
|
||||
* If we are a client then we always use the max_early_data from the
|
||||
* session/psksession. Otherwise we go with the lowest out of the max early
|
||||
* data set in the session and the configured max_early_data.
|
||||
*/
|
||||
if (!s->server && sess->ext.max_early_data == 0) {
|
||||
if (!ossl_assert(s->psksession != NULL
|
||||
&& s->psksession->ext.max_early_data > 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_EARLY_DATA_COUNT_OK,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
sess = s->psksession;
|
||||
}
|
||||
if (!s->server
|
||||
|| (s->hit && sess->ext.max_early_data < s->max_early_data))
|
||||
max_early_data = sess->ext.max_early_data;
|
||||
|
||||
if (max_early_data == 0) {
|
||||
SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* If we are dealing with ciphertext we need to allow for the overhead */
|
||||
max_early_data += overhead;
|
||||
|
||||
if (s->early_data_count + length > max_early_data) {
|
||||
SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
|
||||
return 0;
|
||||
}
|
||||
s->early_data_count += length;
|
||||
|
||||
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
|
||||
@@ -125,19 +167,20 @@ static int ssl3_record_app_data_waiting(SSL *s)
|
||||
/* 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;
|
||||
int enc_err, rret;
|
||||
int i;
|
||||
size_t more, n;
|
||||
SSL3_RECORD *rr, *thisrr;
|
||||
SSL3_BUFFER *rbuf;
|
||||
SSL_SESSION *sess;
|
||||
unsigned char *p;
|
||||
unsigned char md[EVP_MAX_MD_SIZE];
|
||||
short version;
|
||||
unsigned mac_size;
|
||||
unsigned int version;
|
||||
size_t mac_size;
|
||||
int imac_size;
|
||||
unsigned int num_recs = 0;
|
||||
unsigned int max_recs;
|
||||
unsigned int j;
|
||||
size_t num_recs = 0, max_recs, j;
|
||||
PACKET pkt, sslv2pkt;
|
||||
size_t first_rec_len;
|
||||
|
||||
rr = RECORD_LAYER_get_rrec(&s->rlayer);
|
||||
rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
|
||||
@@ -147,24 +190,42 @@ int ssl3_get_record(SSL *s)
|
||||
sess = s->session;
|
||||
|
||||
do {
|
||||
thisrr = &rr[num_recs];
|
||||
|
||||
/* 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 */
|
||||
size_t sslv2len;
|
||||
unsigned int type;
|
||||
|
||||
rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
|
||||
SSL3_BUFFER_get_len(rbuf), 0,
|
||||
num_recs == 0 ? 1 : 0, &n);
|
||||
if (rret <= 0)
|
||||
return rret; /* error or non-blocking */
|
||||
RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
|
||||
|
||||
p = RECORD_LAYER_get_packet(&s->rlayer);
|
||||
|
||||
if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
|
||||
RECORD_LAYER_get_packet_length(&s->rlayer))) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
sslv2pkt = pkt;
|
||||
if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
|
||||
|| !PACKET_get_1(&sslv2pkt, &type)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
* 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)) {
|
||||
&& (sslv2len & 0x8000) != 0
|
||||
&& (type == SSL2_MT_CLIENT_HELLO)) {
|
||||
/*
|
||||
* SSLv2 style record
|
||||
*
|
||||
@@ -174,22 +235,22 @@ int ssl3_get_record(SSL *s)
|
||||
* 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;
|
||||
thisrr->type = SSL3_RT_HANDSHAKE;
|
||||
thisrr->rec_version = SSL2_VERSION;
|
||||
|
||||
rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];
|
||||
thisrr->length = sslv2len & 0x7fff;
|
||||
|
||||
if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
|
||||
if (thisrr->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;
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_PACKET_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
if (thisrr->length < MIN_SSL2_RECORD_LEN) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_LENGTH_TOO_SHORT);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
/* SSLv3+ style record */
|
||||
@@ -198,19 +259,28 @@ int ssl3_get_record(SSL *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);
|
||||
if (!PACKET_get_1(&pkt, &type)
|
||||
|| !PACKET_get_net_2(&pkt, &version)
|
||||
|| !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
thisrr->type = type;
|
||||
thisrr->rec_version = version;
|
||||
|
||||
/* Lets check version */
|
||||
if (!s->first_packet && version != s->version) {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
|
||||
/*
|
||||
* Lets check version. In TLSv1.3 we ignore this field. For the
|
||||
* ServerHello after an HRR we haven't actually selected TLSv1.3
|
||||
* yet, but we still treat it as TLSv1.3, so we must check for
|
||||
* that explicitly
|
||||
*/
|
||||
if (!s->first_packet && !SSL_IS_TLS13(s)
|
||||
&& s->hello_retry_request != SSL_HRR_PENDING
|
||||
&& version != (unsigned int)s->version) {
|
||||
if ((s->version & 0xFF00) == (version & 0xFF00)
|
||||
&& !s->enc_write_ctx && !s->write_hash) {
|
||||
if (rr->type == SSL3_RT_ALERT) {
|
||||
if (thisrr->type == SSL3_RT_ALERT) {
|
||||
/*
|
||||
* The record is using an incorrect version number,
|
||||
* but what we've got appears to be an alert. We
|
||||
@@ -219,15 +289,18 @@ int ssl3_get_record(SSL *s)
|
||||
* shouldn't send a fatal alert back. We'll just
|
||||
* end.
|
||||
*/
|
||||
goto err;
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
* Send back error using their minor version number :-)
|
||||
*/
|
||||
s->version = (unsigned short)version;
|
||||
}
|
||||
al = SSL_AD_PROTOCOL_VERSION;
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((version >> 8) != SSL3_VERSION_MAJOR) {
|
||||
@@ -239,97 +312,127 @@ int ssl3_get_record(SSL *s)
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_HTTP_REQUEST);
|
||||
return -1;
|
||||
} else if (strncmp((char *)p, "CONNE", 5) == 0) {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_HTTPS_PROXY_REQUEST);
|
||||
goto err;
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_HTTPS_PROXY_REQUEST);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Doesn't look like TLS - don't send an alert */
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
goto err;
|
||||
SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
return -1;
|
||||
} else {
|
||||
SSLerr(SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
al = SSL_AD_PROTOCOL_VERSION;
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
|
||||
SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_WRONG_VERSION_NUMBER);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (rr[num_recs].length >
|
||||
if (SSL_IS_TLS13(s)
|
||||
&& s->enc_read_ctx != NULL
|
||||
&& thisrr->type != SSL3_RT_APPLICATION_DATA
|
||||
&& (thisrr->type != SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
|| !SSL_IS_FIRST_HANDSHAKE(s))) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
|
||||
SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (thisrr->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;
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_PACKET_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* now s->rlayer.rstate == SSL_ST_READ_BODY */
|
||||
}
|
||||
|
||||
if (SSL_IS_TLS13(s)) {
|
||||
if (thisrr->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
|
||||
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
/*
|
||||
* If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
|
||||
* does not include the compression overhead anyway.
|
||||
*/
|
||||
if (s->expand == NULL)
|
||||
len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
|
||||
#endif
|
||||
|
||||
if (thisrr->length > len) {
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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
|
||||
if (thisrr->rec_version == SSL2_VERSION) {
|
||||
more = thisrr->length + SSL2_RT_HEADER_LENGTH
|
||||
- SSL3_RT_HEADER_LENGTH;
|
||||
} else {
|
||||
i = rr[num_recs].length;
|
||||
more = thisrr->length;
|
||||
}
|
||||
if (i > 0) {
|
||||
if (more > 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 */
|
||||
rret = ssl3_read_n(s, more, more, 1, 0, &n);
|
||||
if (rret <= 0)
|
||||
return rret; /* 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
|
||||
* At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
|
||||
* + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
|
||||
* + thisrr->length and we have that many bytes in s->packet
|
||||
*/
|
||||
if (rr[num_recs].rec_version == SSL2_VERSION) {
|
||||
rr[num_recs].input =
|
||||
if (thisrr->rec_version == SSL2_VERSION) {
|
||||
thisrr->input =
|
||||
&(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
|
||||
} else {
|
||||
rr[num_recs].input =
|
||||
thisrr->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
|
||||
* ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
|
||||
* points at thisrr->length bytes, which need to be copied into
|
||||
* thisrr->data by either the decryption or by the decompression When
|
||||
* the data is 'copied' into the thisrr->data buffer, thisrr->input will
|
||||
* be pointed at the new buffer
|
||||
*/
|
||||
|
||||
/*
|
||||
* We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
|
||||
* bytes of encrypted compressed stuff.
|
||||
* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
|
||||
* thisrr->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;
|
||||
/* decrypt in place in 'thisrr->input' */
|
||||
thisrr->data = thisrr->input;
|
||||
thisrr->orig_len = thisrr->length;
|
||||
|
||||
/* Mark this record as not read by upper layers yet */
|
||||
rr[num_recs].read = 0;
|
||||
thisrr->read = 0;
|
||||
|
||||
num_recs++;
|
||||
|
||||
@@ -337,65 +440,121 @@ int ssl3_get_record(SSL *s)
|
||||
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
|
||||
&& thisrr->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 (num_recs == 1
|
||||
&& thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
|
||||
&& (SSL_IS_TLS13(s) || s->hello_retry_request != SSL_HRR_NONE)
|
||||
&& SSL_IS_FIRST_HANDSHAKE(s)) {
|
||||
/*
|
||||
* CCS messages must be exactly 1 byte long, containing the value 0x01
|
||||
*/
|
||||
if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
|
||||
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_INVALID_CCS_MESSAGE);
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
* CCS messages are ignored in TLSv1.3. We treat it like an empty
|
||||
* handshake record
|
||||
*/
|
||||
thisrr->type = SSL3_RT_HANDSHAKE;
|
||||
RECORD_LAYER_inc_empty_record_count(&s->rlayer);
|
||||
if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
|
||||
> MAX_EMPTY_RECORDS) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_UNEXPECTED_CCS_MESSAGE);
|
||||
return -1;
|
||||
}
|
||||
thisrr->read = 1;
|
||||
RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/* TODO(size_t): convert this to do size_t properly */
|
||||
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;
|
||||
if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
ERR_LIB_EVP);
|
||||
return -1;
|
||||
}
|
||||
mac_size = (unsigned)imac_size;
|
||||
|
||||
mac_size = (size_t)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;
|
||||
thisrr = &rr[j];
|
||||
|
||||
if (thisrr->length < mac_size) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_LENGTH_TOO_SHORT);
|
||||
return -1;
|
||||
}
|
||||
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,
|
||||
thisrr->length -= mac_size;
|
||||
mac = thisrr->data + thisrr->length;
|
||||
i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
|
||||
if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
|
||||
SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
|
||||
goto f_err;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
first_rec_len = rr[0].length;
|
||||
|
||||
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.
|
||||
* 0: (in non-constant time) if the record is publicly 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;
|
||||
if (ossl_statem_in_error(s)) {
|
||||
/* SSLfatal() already got called */
|
||||
return -1;
|
||||
}
|
||||
if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
|
||||
/*
|
||||
* Valid early_data that we cannot decrypt might fail here as
|
||||
* publicly invalid. We treat it like an empty record.
|
||||
*/
|
||||
|
||||
thisrr = &rr[0];
|
||||
|
||||
if (!early_data_count_ok(s, thisrr->length,
|
||||
EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
|
||||
thisrr->length = 0;
|
||||
thisrr->read = 1;
|
||||
RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
|
||||
RECORD_LAYER_reset_read_sequence(&s->rlayer);
|
||||
return 1;
|
||||
}
|
||||
SSLfatal(s, SSL_AD_DECRYPTION_FAILED, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
|
||||
return -1;
|
||||
}
|
||||
#ifdef SSL_DEBUG
|
||||
printf("dec %d\n", rr->length);
|
||||
printf("dec %lu\n", (unsigned long)rr[0].length);
|
||||
{
|
||||
unsigned int z;
|
||||
for (z = 0; z < rr->length; z++)
|
||||
printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
|
||||
size_t z;
|
||||
for (z = 0; z < rr[0].length; z++)
|
||||
printf("%02X%c", rr[0].data[z], ((z + 1) % 16) ? ' ' : '\n');
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
@@ -409,22 +568,27 @@ int ssl3_get_record(SSL *s)
|
||||
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);
|
||||
if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (j = 0; j < num_recs; j++) {
|
||||
thisrr = &rr[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 ||
|
||||
if (thisrr->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;
|
||||
thisrr->orig_len < mac_size + 1)) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_LENGTH_TOO_SHORT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
|
||||
@@ -435,28 +599,59 @@ int ssl3_get_record(SSL *s)
|
||||
* contents of the padding bytes.
|
||||
*/
|
||||
mac = mac_tmp;
|
||||
ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
|
||||
rr[j].length -= mac_size;
|
||||
if (!ssl3_cbc_copy_mac(mac_tmp, thisrr, mac_size)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
thisrr->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];
|
||||
thisrr->length -= mac_size;
|
||||
mac = &thisrr->data[thisrr->length];
|
||||
}
|
||||
|
||||
i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
|
||||
if (i < 0 || mac == NULL
|
||||
i = s->method->ssl3_enc->mac(s, thisrr, 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)
|
||||
if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
|
||||
enc_err = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (enc_err < 0) {
|
||||
if (ossl_statem_in_error(s)) {
|
||||
/* We already called SSLfatal() */
|
||||
return -1;
|
||||
}
|
||||
if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
|
||||
/*
|
||||
* We assume this is unreadable early_data - we treat it like an
|
||||
* empty record
|
||||
*/
|
||||
|
||||
/*
|
||||
* The record length may have been modified by the mac check above
|
||||
* so we use the previously saved value
|
||||
*/
|
||||
if (!early_data_count_ok(s, first_rec_len,
|
||||
EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
|
||||
thisrr = &rr[0];
|
||||
thisrr->length = 0;
|
||||
thisrr->read = 1;
|
||||
RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
|
||||
RECORD_LAYER_reset_read_sequence(&s->rlayer);
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
* A separate 'decryption_failed' alert was introduced with TLS 1.0,
|
||||
* SSL 3.0 only has 'bad_record_mac'. But unless a decryption
|
||||
@@ -464,63 +659,118 @@ int ssl3_get_record(SSL *s)
|
||||
* 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;
|
||||
SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (j = 0; j < num_recs; j++) {
|
||||
/* rr[j].length is now just compressed */
|
||||
thisrr = &rr[j];
|
||||
|
||||
/* thisrr->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 (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_COMPRESSED_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
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 (!ssl3_do_uncompress(s, thisrr)) {
|
||||
SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_BAD_DECOMPRESSION);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (SSL_IS_TLS13(s) && s->enc_read_ctx != NULL) {
|
||||
size_t end;
|
||||
|
||||
if (thisrr->length == 0
|
||||
|| thisrr->type != SSL3_RT_APPLICATION_DATA) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_BAD_RECORD_TYPE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Strip trailing padding */
|
||||
for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
|
||||
end--)
|
||||
continue;
|
||||
|
||||
thisrr->length = end;
|
||||
thisrr->type = thisrr->data[end];
|
||||
if (thisrr->type != SSL3_RT_APPLICATION_DATA
|
||||
&& thisrr->type != SSL3_RT_ALERT
|
||||
&& thisrr->type != SSL3_RT_HANDSHAKE) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_BAD_RECORD_TYPE);
|
||||
return -1;
|
||||
}
|
||||
if (s->msg_callback)
|
||||
s->msg_callback(0, s->version, SSL3_RT_INNER_CONTENT_TYPE,
|
||||
&thisrr->data[end], 1, s, s->msg_callback_arg);
|
||||
}
|
||||
|
||||
rr[j].off = 0;
|
||||
/*
|
||||
* TLSv1.3 alert and handshake records are required to be non-zero in
|
||||
* length.
|
||||
*/
|
||||
if (SSL_IS_TLS13(s)
|
||||
&& (thisrr->type == SSL3_RT_HANDSHAKE
|
||||
|| thisrr->type == SSL3_RT_ALERT)
|
||||
&& thisrr->length == 0) {
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_BAD_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If received packet overflows current Max Fragment Length setting */
|
||||
if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
|
||||
&& thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
return -1;
|
||||
}
|
||||
|
||||
thisrr->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 :-).
|
||||
* thisrr->type is the type of record
|
||||
* thisrr->length == number of bytes in record
|
||||
* thisrr->off == offset to first valid byte
|
||||
* thisrr->data == where to take bytes from, increment after use :-).
|
||||
*/
|
||||
|
||||
/* just read a 0 length packet */
|
||||
if (rr[j].length == 0) {
|
||||
if (thisrr->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;
|
||||
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
|
||||
SSL_R_RECORD_TOO_SMALL);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
RECORD_LAYER_reset_empty_record_count(&s->rlayer);
|
||||
}
|
||||
}
|
||||
|
||||
if (s->early_data_state == SSL_EARLY_DATA_READING) {
|
||||
thisrr = &rr[0];
|
||||
if (thisrr->type == SSL3_RT_APPLICATION_DATA
|
||||
&& !early_data_count_ok(s, thisrr->length, 0, 0)) {
|
||||
/* SSLfatal already called */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -535,6 +785,7 @@ int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
|
||||
if (rr->comp == NULL)
|
||||
return 0;
|
||||
|
||||
/* TODO(size_t): Convert this call */
|
||||
i = COMP_expand_block(ssl->expand, rr->comp,
|
||||
SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
|
||||
if (i < 0)
|
||||
@@ -551,21 +802,23 @@ int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
int i;
|
||||
|
||||
/* TODO(size_t): Convert this call */
|
||||
i = COMP_compress_block(ssl->compress, wr->data,
|
||||
SSL3_RT_MAX_COMPRESSED_LENGTH,
|
||||
(int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
|
||||
wr->input, (int)wr->length);
|
||||
if (i < 0)
|
||||
return (0);
|
||||
return 0;
|
||||
else
|
||||
wr->length = i;
|
||||
|
||||
wr->input = wr->data;
|
||||
#endif
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-
|
||||
* ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
|
||||
* ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Will call
|
||||
* SSLfatal() for internal errors, but not otherwise.
|
||||
*
|
||||
* Returns:
|
||||
* 0: (in non-constant time) if the record is publically invalid (i.e. too
|
||||
@@ -574,12 +827,13 @@ int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
|
||||
* -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)
|
||||
int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending)
|
||||
{
|
||||
SSL3_RECORD *rec;
|
||||
EVP_CIPHER_CTX *ds;
|
||||
unsigned long l;
|
||||
int bs, i, mac_size = 0;
|
||||
size_t l, i;
|
||||
size_t bs, mac_size = 0;
|
||||
int imac_size;
|
||||
const EVP_CIPHER *enc;
|
||||
|
||||
rec = inrecs;
|
||||
@@ -607,12 +861,13 @@ int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int sending)
|
||||
rec->input = rec->data;
|
||||
} else {
|
||||
l = rec->length;
|
||||
/* TODO(size_t): Convert this call */
|
||||
bs = EVP_CIPHER_CTX_block_size(ds);
|
||||
|
||||
/* COMPRESS */
|
||||
|
||||
if ((bs != 1) && sending) {
|
||||
i = bs - ((int)l % bs);
|
||||
i = bs - (l % bs);
|
||||
|
||||
/* we need to add 'i-1' padding bytes */
|
||||
l += i;
|
||||
@@ -622,7 +877,7 @@ int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int sending)
|
||||
*/
|
||||
memset(&rec->input[rec->length], 0, i);
|
||||
rec->length += i;
|
||||
rec->input[l - 1] = (i - 1);
|
||||
rec->input[l - 1] = (unsigned char)(i - 1);
|
||||
}
|
||||
|
||||
if (!sending) {
|
||||
@@ -631,19 +886,30 @@ int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int sending)
|
||||
/* otherwise, rec->length >= bs */
|
||||
}
|
||||
|
||||
if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
|
||||
/* TODO(size_t): Convert this call */
|
||||
if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1)
|
||||
return -1;
|
||||
|
||||
if (EVP_MD_CTX_md(s->read_hash) != NULL)
|
||||
mac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if (EVP_MD_CTX_md(s->read_hash) != NULL) {
|
||||
/* TODO(size_t): convert me */
|
||||
imac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if (imac_size < 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
mac_size = (size_t)imac_size;
|
||||
}
|
||||
if ((bs != 1) && !sending)
|
||||
return ssl3_cbc_remove_padding(rec, bs, mac_size);
|
||||
}
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define MAX_PADDING 256
|
||||
/*-
|
||||
* tls1_enc encrypts/decrypts |n_recs| in |recs|.
|
||||
* tls1_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
|
||||
* internal errors, but not otherwise.
|
||||
*
|
||||
* Returns:
|
||||
* 0: (in non-constant time) if the record is publically invalid (i.e. too
|
||||
@@ -652,22 +918,31 @@ int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int sending)
|
||||
* -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)
|
||||
int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t 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;
|
||||
int i, pad = 0, ret, tmpr;
|
||||
size_t bs, mac_size = 0, ctr, padnum, loop;
|
||||
unsigned char padval;
|
||||
int imac_size;
|
||||
const EVP_CIPHER *enc;
|
||||
unsigned int ctr;
|
||||
|
||||
if (n_recs == 0)
|
||||
if (n_recs == 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
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);
|
||||
if (!ossl_assert(n >= 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
ds = s->enc_write_ctx;
|
||||
if (s->enc_write_ctx == NULL)
|
||||
@@ -688,10 +963,12 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
* we can't write into the input stream: Can this ever
|
||||
* happen?? (steve)
|
||||
*/
|
||||
SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, 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);
|
||||
} else if (ssl_randbytes(s, recs[ctr].input, ivlen) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -700,7 +977,11 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
} else {
|
||||
if (EVP_MD_CTX_md(s->read_hash)) {
|
||||
int n = EVP_MD_CTX_size(s->read_hash);
|
||||
OPENSSL_assert(n >= 0);
|
||||
if (!ossl_assert(n >= 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
ds = s->enc_read_ctx;
|
||||
if (s->enc_read_ctx == NULL)
|
||||
@@ -725,7 +1006,8 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
* 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);
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
SSL_R_PIPELINE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -759,12 +1041,15 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
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;
|
||||
buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
|
||||
buf[ctr][12] = (unsigned char)(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)
|
||||
if (pad <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sending) {
|
||||
reclen[ctr] += pad;
|
||||
@@ -772,16 +1057,21 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
}
|
||||
|
||||
} else if ((bs != 1) && sending) {
|
||||
i = bs - ((int)reclen[ctr] % bs);
|
||||
padnum = bs - (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 (padnum > MAX_PADDING) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
/* we need to add 'padnum' padding bytes of value padval */
|
||||
padval = (unsigned char)(padnum - 1);
|
||||
for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
|
||||
recs[ctr].input[loop] = padval;
|
||||
reclen[ctr] += padnum;
|
||||
recs[ctr].length += padnum;
|
||||
}
|
||||
|
||||
if (!sending) {
|
||||
@@ -797,28 +1087,34 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
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);
|
||||
(int)n_recs, data) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
SSL_R_PIPELINE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
/* 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
|
||||
(int)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);
|
||||
(int)n_recs, reclen) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
SSL_R_PIPELINE_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
i = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
|
||||
/* TODO(size_t): Convert this call */
|
||||
tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
|
||||
(unsigned int)reclen[0]);
|
||||
if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
|
||||
& EVP_CIPH_FLAG_CUSTOM_CIPHER)
|
||||
? (i < 0)
|
||||
: (i == 0))
|
||||
? (tmpr < 0)
|
||||
: (tmpr == 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++) {
|
||||
@@ -836,8 +1132,15 @@ int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int sending)
|
||||
}
|
||||
|
||||
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 (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
|
||||
imac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if (imac_size < 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
mac_size = (size_t)imac_size;
|
||||
}
|
||||
if ((bs != 1) && !sending) {
|
||||
int tmpret;
|
||||
for (ctr = 0; ctr < n_recs; ctr++) {
|
||||
@@ -868,7 +1171,7 @@ int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
const EVP_MD_CTX *hash;
|
||||
unsigned char *p, rec_char;
|
||||
size_t md_size;
|
||||
int npad;
|
||||
size_t npad;
|
||||
int t;
|
||||
|
||||
if (sending) {
|
||||
@@ -883,7 +1186,7 @@ int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
|
||||
t = EVP_MD_CTX_size(hash);
|
||||
if (t < 0)
|
||||
return -1;
|
||||
return 0;
|
||||
md_size = t;
|
||||
npad = (48 / md_size) * md_size;
|
||||
|
||||
@@ -905,7 +1208,7 @@ int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
* total size.
|
||||
*/
|
||||
unsigned char header[75];
|
||||
unsigned j = 0;
|
||||
size_t j = 0;
|
||||
memcpy(header + j, mac_sec, md_size);
|
||||
j += md_size;
|
||||
memcpy(header + j, ssl3_pad_1, npad);
|
||||
@@ -913,8 +1216,8 @@ int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
memcpy(header + j, seq, 8);
|
||||
j += 8;
|
||||
header[j++] = rec->type;
|
||||
header[j++] = rec->length >> 8;
|
||||
header[j++] = rec->length & 0xff;
|
||||
header[j++] = (unsigned char)(rec->length >> 8);
|
||||
header[j++] = (unsigned char)(rec->length & 0xff);
|
||||
|
||||
/* Final param == is SSLv3 */
|
||||
if (ssl3_cbc_digest_record(hash,
|
||||
@@ -922,14 +1225,14 @@ int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
header, rec->input,
|
||||
rec->length + md_size, rec->orig_len,
|
||||
mac_sec, md_size, 1) <= 0)
|
||||
return -1;
|
||||
return 0;
|
||||
} 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;
|
||||
return 0;
|
||||
|
||||
rec_char = rec->type;
|
||||
p = md;
|
||||
@@ -947,16 +1250,15 @@ int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
|| 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;
|
||||
EVP_MD_CTX_reset(md_ctx);
|
||||
return 0;
|
||||
}
|
||||
md_size = md_size_u;
|
||||
|
||||
EVP_MD_CTX_free(md_ctx);
|
||||
}
|
||||
|
||||
ssl3_record_sequence_update(seq);
|
||||
return (md_size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
@@ -980,7 +1282,8 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
}
|
||||
|
||||
t = EVP_MD_CTX_size(hash);
|
||||
OPENSSL_assert(t >= 0);
|
||||
if (!ossl_assert(t >= 0))
|
||||
return 0;
|
||||
md_size = t;
|
||||
|
||||
/* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
|
||||
@@ -988,10 +1291,8 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
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;
|
||||
}
|
||||
if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash))
|
||||
return 0;
|
||||
mac_ctx = hmac;
|
||||
}
|
||||
|
||||
@@ -1009,8 +1310,8 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
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;
|
||||
header[11] = (unsigned char)(rec->length >> 8);
|
||||
header[12] = (unsigned char)(rec->length & 0xff);
|
||||
|
||||
if (!sending && !SSL_READ_ETM(ssl) &&
|
||||
EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
@@ -1028,22 +1329,16 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
ssl->s3->read_mac_secret,
|
||||
ssl->s3->read_mac_secret_size, 0) <= 0) {
|
||||
EVP_MD_CTX_free(hmac);
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
/* TODO(size_t): Convert these calls */
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
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);
|
||||
@@ -1058,7 +1353,7 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
}
|
||||
fprintf(stderr, "rec=");
|
||||
{
|
||||
unsigned int z;
|
||||
size_t z;
|
||||
for (z = 0; z < rec->length; z++)
|
||||
fprintf(stderr, "%02X ", rec->data[z]);
|
||||
fprintf(stderr, "\n");
|
||||
@@ -1080,7 +1375,7 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
return (md_size);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-
|
||||
@@ -1094,10 +1389,11 @@ int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
|
||||
* -1: otherwise.
|
||||
*/
|
||||
int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size)
|
||||
size_t block_size, size_t mac_size)
|
||||
{
|
||||
unsigned padding_length, good;
|
||||
const unsigned overhead = 1 /* padding length byte */ + mac_size;
|
||||
size_t padding_length;
|
||||
size_t good;
|
||||
const size_t overhead = 1 /* padding length byte */ + mac_size;
|
||||
|
||||
/*
|
||||
* These lengths are all public so we can test them in non-constant time.
|
||||
@@ -1106,11 +1402,11 @@ int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
|
||||
return 0;
|
||||
|
||||
padding_length = rec->data[rec->length - 1];
|
||||
good = constant_time_ge(rec->length, padding_length + overhead);
|
||||
good = constant_time_ge_s(rec->length, padding_length + overhead);
|
||||
/* SSLv3 requires that the padding is minimal. */
|
||||
good &= constant_time_ge(block_size, padding_length + 1);
|
||||
good &= constant_time_ge_s(block_size, padding_length + 1);
|
||||
rec->length -= good & (padding_length + 1);
|
||||
return constant_time_select_int(good, 1, -1);
|
||||
return constant_time_select_int_s(good, 1, -1);
|
||||
}
|
||||
|
||||
/*-
|
||||
@@ -1128,10 +1424,11 @@ int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
|
||||
*/
|
||||
int tls1_cbc_remove_padding(const SSL *s,
|
||||
SSL3_RECORD *rec,
|
||||
unsigned block_size, unsigned mac_size)
|
||||
size_t block_size, size_t mac_size)
|
||||
{
|
||||
unsigned padding_length, good, to_check, i;
|
||||
const unsigned overhead = 1 /* padding length byte */ + mac_size;
|
||||
size_t good;
|
||||
size_t padding_length, to_check, i;
|
||||
const size_t overhead = 1 /* padding length byte */ + mac_size;
|
||||
/* Check if version requires explicit IV */
|
||||
if (SSL_USE_EXPLICIT_IV(s)) {
|
||||
/*
|
||||
@@ -1157,7 +1454,7 @@ int tls1_cbc_remove_padding(const SSL *s,
|
||||
return 1;
|
||||
}
|
||||
|
||||
good = constant_time_ge(rec->length, overhead + padding_length);
|
||||
good = constant_time_ge_s(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
|
||||
@@ -1172,7 +1469,7 @@ int tls1_cbc_remove_padding(const SSL *s,
|
||||
to_check = rec->length;
|
||||
|
||||
for (i = 0; i < to_check; i++) {
|
||||
unsigned char mask = constant_time_ge_8(padding_length, i);
|
||||
unsigned char mask = constant_time_ge_8_s(padding_length, i);
|
||||
unsigned char b = rec->data[rec->length - 1 - i];
|
||||
/*
|
||||
* The final |padding_length+1| bytes should all have the value
|
||||
@@ -1185,10 +1482,10 @@ int tls1_cbc_remove_padding(const SSL *s,
|
||||
* 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);
|
||||
good = constant_time_eq_s(0xff, good & 0xff);
|
||||
rec->length -= good & (padding_length + 1);
|
||||
|
||||
return constant_time_select_int(good, 1, -1);
|
||||
return constant_time_select_int_s(good, 1, -1);
|
||||
}
|
||||
|
||||
/*-
|
||||
@@ -1211,8 +1508,8 @@ int tls1_cbc_remove_padding(const SSL *s,
|
||||
*/
|
||||
#define CBC_MAC_ROTATE_IN_PLACE
|
||||
|
||||
void ssl3_cbc_copy_mac(unsigned char *out,
|
||||
const SSL3_RECORD *rec, unsigned md_size)
|
||||
int ssl3_cbc_copy_mac(unsigned char *out,
|
||||
const SSL3_RECORD *rec, size_t md_size)
|
||||
{
|
||||
#if defined(CBC_MAC_ROTATE_IN_PLACE)
|
||||
unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
|
||||
@@ -1224,19 +1521,20 @@ void ssl3_cbc_copy_mac(unsigned char *out,
|
||||
/*
|
||||
* 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;
|
||||
size_t mac_end = rec->length;
|
||||
size_t mac_start = mac_end - md_size;
|
||||
size_t 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;
|
||||
size_t scan_start = 0;
|
||||
size_t i, j;
|
||||
size_t rotate_offset;
|
||||
|
||||
OPENSSL_assert(rec->orig_len >= md_size);
|
||||
OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
|
||||
if (!ossl_assert(rec->orig_len >= md_size
|
||||
&& md_size <= EVP_MAX_MD_SIZE))
|
||||
return 0;
|
||||
|
||||
#if defined(CBC_MAC_ROTATE_IN_PLACE)
|
||||
rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
|
||||
@@ -1250,15 +1548,15 @@ void ssl3_cbc_copy_mac(unsigned char *out,
|
||||
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);
|
||||
size_t mac_started = constant_time_eq_s(i, mac_start);
|
||||
size_t mac_ended = constant_time_lt_s(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);
|
||||
j &= constant_time_lt_s(j, md_size);
|
||||
}
|
||||
|
||||
/* Now rotate the MAC */
|
||||
@@ -1268,28 +1566,31 @@ void ssl3_cbc_copy_mac(unsigned char *out,
|
||||
/* 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);
|
||||
rotate_offset &= constant_time_lt_s(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);
|
||||
rotate_offset &= constant_time_lt_s(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);
|
||||
out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
|
||||
rotate_offset++;
|
||||
rotate_offset &= constant_time_lt(rotate_offset, md_size);
|
||||
rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
|
||||
}
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
{
|
||||
int i, al;
|
||||
int i;
|
||||
int enc_err;
|
||||
SSL_SESSION *sess;
|
||||
SSL3_RECORD *rr;
|
||||
unsigned int mac_size;
|
||||
int imac_size;
|
||||
size_t mac_size;
|
||||
unsigned char md[EVP_MAX_MD_SIZE];
|
||||
|
||||
rr = RECORD_LAYER_get_rrec(&s->rlayer);
|
||||
@@ -1315,15 +1616,38 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
|
||||
/* 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;
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* decrypt in place in 'rr->input' */
|
||||
rr->data = rr->input;
|
||||
rr->orig_len = rr->length;
|
||||
|
||||
if (SSL_READ_ETM(s) && s->read_hash) {
|
||||
unsigned char *mac;
|
||||
mac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
if (rr->orig_len < mac_size) {
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_LENGTH_TOO_SHORT);
|
||||
return 0;
|
||||
}
|
||||
rr->length -= mac_size;
|
||||
mac = rr->data + rr->length;
|
||||
i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
|
||||
if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
|
||||
SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
|
||||
/*-
|
||||
* enc_err is:
|
||||
@@ -1332,15 +1656,19 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
* -1: if the padding is invalid
|
||||
*/
|
||||
if (enc_err == 0) {
|
||||
if (ossl_statem_in_error(s)) {
|
||||
/* SSLfatal() got called */
|
||||
return 0;
|
||||
}
|
||||
/* For DTLS we simply ignore bad packets. */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto err;
|
||||
return 0;
|
||||
}
|
||||
#ifdef SSL_DEBUG
|
||||
printf("dec %d\n", rr->length);
|
||||
printf("dec %ld\n", rr->length);
|
||||
{
|
||||
unsigned int z;
|
||||
size_t z;
|
||||
for (z = 0; z < rr->length; z++)
|
||||
printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
|
||||
}
|
||||
@@ -1348,13 +1676,25 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
#endif
|
||||
|
||||
/* r->length is now the compressed data plus mac */
|
||||
if ((sess != NULL) &&
|
||||
if ((sess != NULL) && !SSL_READ_ETM(s) &&
|
||||
(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);
|
||||
|
||||
/* TODO(size_t): Convert this to do size_t properly */
|
||||
imac_size = EVP_MD_CTX_size(s->read_hash);
|
||||
if (imac_size < 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
ERR_LIB_EVP);
|
||||
return 0;
|
||||
}
|
||||
mac_size = (size_t)imac_size;
|
||||
if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* orig_len is the length of the record before any padding was
|
||||
@@ -1366,9 +1706,9 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
/* 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;
|
||||
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_LENGTH_TOO_SHORT);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
|
||||
@@ -1379,7 +1719,11 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
* contents of the padding bytes.
|
||||
*/
|
||||
mac = mac_tmp;
|
||||
ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
|
||||
if (!ssl3_cbc_copy_mac(mac_tmp, rr, mac_size)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
rr->length -= mac_size;
|
||||
} else {
|
||||
/*
|
||||
@@ -1392,8 +1736,8 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
}
|
||||
|
||||
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)
|
||||
if (i == 0 || mac == NULL
|
||||
|| CRYPTO_memcmp(md, mac, mac_size) != 0)
|
||||
enc_err = -1;
|
||||
if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
|
||||
enc_err = -1;
|
||||
@@ -1403,28 +1747,27 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
/* decryption failed, silently discard message */
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto err;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_COMPRESSED_LENGTH_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
if (!ssl3_do_uncompress(s, rr)) {
|
||||
al = SSL_AD_DECOMPRESSION_FAILURE;
|
||||
SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
|
||||
goto f_err;
|
||||
SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
|
||||
SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
|
||||
SSL_R_DATA_LENGTH_TOO_LONG);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rr->off = 0;
|
||||
@@ -1443,12 +1786,7 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
/* 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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1471,7 +1809,8 @@ int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
|
||||
int dtls1_get_record(SSL *s)
|
||||
{
|
||||
int ssl_major, ssl_minor;
|
||||
int i, n;
|
||||
int rret;
|
||||
size_t more, n;
|
||||
SSL3_RECORD *rr;
|
||||
unsigned char *p = NULL;
|
||||
unsigned short version;
|
||||
@@ -1485,8 +1824,10 @@ int dtls1_get_record(SSL *s)
|
||||
* The epoch may have changed. If so, process all the pending records.
|
||||
* This is a non-blocking operation.
|
||||
*/
|
||||
if (!dtls1_process_buffered_records(s))
|
||||
if (!dtls1_process_buffered_records(s)) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* if we're renegotiating, then there may be buffered records */
|
||||
if (dtls1_get_processed_record(s))
|
||||
@@ -1497,11 +1838,13 @@ int dtls1_get_record(SSL *s)
|
||||
/* 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);
|
||||
rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
|
||||
SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
|
||||
/* read timeout is handled by dtls1_read_bytes */
|
||||
if (n <= 0)
|
||||
return (n); /* error or non-blocking */
|
||||
if (rret <= 0) {
|
||||
/* SSLfatal() already called if appropriate */
|
||||
return rret; /* error or non-blocking */
|
||||
}
|
||||
|
||||
/* this packet contained a partial record, dump it */
|
||||
if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
|
||||
@@ -1559,6 +1902,15 @@ int dtls1_get_record(SSL *s)
|
||||
goto again;
|
||||
}
|
||||
|
||||
/* If received packet overflows own-client Max Fragment Length setting */
|
||||
if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
|
||||
&& rr->length > GET_MAX_FRAGMENT_LENGTH(s->session)) {
|
||||
/* 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 */
|
||||
}
|
||||
|
||||
@@ -1567,10 +1919,14 @@ int dtls1_get_record(SSL *s)
|
||||
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);
|
||||
more = rr->length;
|
||||
rret = ssl3_read_n(s, more, more, 1, 1, &n);
|
||||
/* this packet contained a partial record, dump it */
|
||||
if (n != i) {
|
||||
if (rret <= 0 || n != more) {
|
||||
if (ossl_statem_in_error(s)) {
|
||||
/* ssl3_read_n() called SSLfatal() */
|
||||
return -1;
|
||||
}
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
goto again;
|
||||
@@ -1620,10 +1976,12 @@ int dtls1_get_record(SSL *s)
|
||||
*/
|
||||
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)
|
||||
if (dtls1_buffer_record (s,
|
||||
&(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
|
||||
rr->seq_num) < 0) {
|
||||
/* SSLfatal() already called */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer);
|
||||
@@ -1631,11 +1989,15 @@ int dtls1_get_record(SSL *s)
|
||||
}
|
||||
|
||||
if (!dtls1_process_record(s, bitmap)) {
|
||||
if (ossl_statem_in_error(s)) {
|
||||
/* dtls1_process_record() called SSLfatal */
|
||||
return -1;
|
||||
}
|
||||
rr->length = 0;
|
||||
RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
|
||||
goto again; /* get another record */
|
||||
}
|
||||
|
||||
return (1);
|
||||
return 1;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 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"
|
||||
#include "internal/cryptlib.h"
|
||||
|
||||
/*-
|
||||
* tls13_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
|
||||
* internal errors, but not otherwise.
|
||||
*
|
||||
* Returns:
|
||||
* 0: (in non-constant time) if the record is publically invalid (i.e. too
|
||||
* short etc).
|
||||
* 1: if the record encryption was successful.
|
||||
* -1: if the record's AEAD-authenticator is invalid or, if sending,
|
||||
* an internal error occurred.
|
||||
*/
|
||||
int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
|
||||
{
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
unsigned char iv[EVP_MAX_IV_LENGTH];
|
||||
size_t ivlen, taglen, offset, loop;
|
||||
unsigned char *staticiv;
|
||||
unsigned char *seq;
|
||||
int lenu, lenf;
|
||||
SSL3_RECORD *rec = &recs[0];
|
||||
uint32_t alg_enc;
|
||||
|
||||
if (n_recs != 1) {
|
||||
/* Should not happen */
|
||||
/* TODO(TLS1.3): Support pipelining */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sending) {
|
||||
ctx = s->enc_write_ctx;
|
||||
staticiv = s->write_iv;
|
||||
seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
|
||||
} else {
|
||||
ctx = s->enc_read_ctx;
|
||||
staticiv = s->read_iv;
|
||||
seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
|
||||
}
|
||||
|
||||
if (ctx == NULL) {
|
||||
memmove(rec->data, rec->input, rec->length);
|
||||
rec->input = rec->data;
|
||||
return 1;
|
||||
}
|
||||
|
||||
ivlen = EVP_CIPHER_CTX_iv_length(ctx);
|
||||
|
||||
if (s->early_data_state == SSL_EARLY_DATA_WRITING
|
||||
|| s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
|
||||
if (s->session != NULL && s->session->ext.max_early_data > 0) {
|
||||
alg_enc = s->session->cipher->algorithm_enc;
|
||||
} else {
|
||||
if (!ossl_assert(s->psksession != NULL
|
||||
&& s->psksession->ext.max_early_data > 0)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
alg_enc = s->psksession->cipher->algorithm_enc;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* To get here we must have selected a ciphersuite - otherwise ctx would
|
||||
* be NULL
|
||||
*/
|
||||
if (!ossl_assert(s->s3->tmp.new_cipher != NULL)) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
|
||||
}
|
||||
|
||||
if (alg_enc & SSL_AESCCM) {
|
||||
if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
|
||||
taglen = EVP_CCM8_TLS_TAG_LEN;
|
||||
else
|
||||
taglen = EVP_CCM_TLS_TAG_LEN;
|
||||
if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
|
||||
NULL) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
} else if (alg_enc & SSL_AESGCM) {
|
||||
taglen = EVP_GCM_TLS_TAG_LEN;
|
||||
} else if (alg_enc & SSL_CHACHA20) {
|
||||
taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
|
||||
} else {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!sending) {
|
||||
/*
|
||||
* Take off tag. There must be at least one byte of content type as
|
||||
* well as the tag
|
||||
*/
|
||||
if (rec->length < taglen + 1)
|
||||
return 0;
|
||||
rec->length -= taglen;
|
||||
}
|
||||
|
||||
/* Set up IV */
|
||||
if (ivlen < SEQ_NUM_SIZE) {
|
||||
/* Should not happen */
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
offset = ivlen - SEQ_NUM_SIZE;
|
||||
memcpy(iv, staticiv, offset);
|
||||
for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
|
||||
iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
|
||||
|
||||
/* Increment the sequence counter */
|
||||
for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
|
||||
++seq[loop - 1];
|
||||
if (seq[loop - 1] != 0)
|
||||
break;
|
||||
}
|
||||
if (loop == 0) {
|
||||
/* Sequence has wrapped */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
|
||||
|| (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
|
||||
taglen,
|
||||
rec->data + rec->length) <= 0)
|
||||
|| EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
|
||||
(unsigned int)rec->length) <= 0
|
||||
|| EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
|
||||
|| (size_t)(lenu + lenf) != rec->length) {
|
||||
return -1;
|
||||
}
|
||||
if (sending) {
|
||||
/* Add the tag */
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
|
||||
rec->data + rec->length) <= 0) {
|
||||
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
||||
ERR_R_INTERNAL_ERROR);
|
||||
return -1;
|
||||
}
|
||||
rec->length += taglen;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user