| 1 |
// DTLS implementation written by Nagendra Modadugu ([email protected]) for the OpenSSL project 2005. |
| 2 |
// Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
#include <openssl/ssl.h> |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <openssl/bio.h> |
| 12 |
#include <openssl/bytestring.h> |
| 13 |
#include <openssl/mem.h> |
| 14 |
#include <openssl/evp.h> |
| 15 |
#include <openssl/err.h> |
| 16 |
#include <openssl/rand.h> |
| 17 |
|
| 18 |
#include "../crypto/internal.h" |
| 19 |
#include "internal.h" |
| 20 |
|
| 21 |
|
| 22 |
BSSL_NAMESPACE_BEGIN |
| 23 |
|
| 24 |
ssl_open_record_t dtls1_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 25 |
size_t *out_consumed, uint8_t *out_alert, |
| 26 |
Span<uint8_t> in) { |
| 27 |
assert(!SSL_in_init(ssl)); |
| 28 |
|
| 29 |
uint8_t type; |
| 30 |
Span<uint8_t> record; |
| 31 |
auto ret = dtls_open_record(ssl, &type, &record, out_consumed, out_alert, in); |
| 32 |
if (ret != ssl_open_record_success) { |
| 33 |
return ret; |
| 34 |
} |
| 35 |
|
| 36 |
if (type == SSL3_RT_HANDSHAKE) { |
| 37 |
// Parse the first fragment header to determine if this is a pre-CCS or |
| 38 |
// post-CCS handshake record. DTLS resets handshake message numbers on each |
| 39 |
// handshake, so renegotiations and retransmissions are ambiguous. |
| 40 |
CBS cbs, body; |
| 41 |
struct hm_header_st msg_hdr; |
| 42 |
CBS_init(&cbs, record.data(), record.size()); |
| 43 |
if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) { |
| 44 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD); |
| 45 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 46 |
return ssl_open_record_error; |
| 47 |
} |
| 48 |
|
| 49 |
if (msg_hdr.type == SSL3_MT_FINISHED && |
| 50 |
msg_hdr.seq == ssl->d1->handshake_read_seq - 1) { |
| 51 |
if (msg_hdr.frag_off == 0) { |
| 52 |
// Retransmit our last flight of messages. If the peer sends the second |
| 53 |
// Finished, they may not have received ours. Only do this for the |
| 54 |
// first fragment, in case the Finished was fragmented. |
| 55 |
if (!dtls1_check_timeout_num(ssl)) { |
| 56 |
*out_alert = 0; // TODO(davidben): Send an alert? |
| 57 |
return ssl_open_record_error; |
| 58 |
} |
| 59 |
|
| 60 |
dtls1_retransmit_outgoing_messages(ssl); |
| 61 |
} |
| 62 |
return ssl_open_record_discard; |
| 63 |
} |
| 64 |
|
| 65 |
// Otherwise, this is a pre-CCS handshake message from an unsupported |
| 66 |
// renegotiation attempt. Fall through to the error path. |
| 67 |
} |
| 68 |
|
| 69 |
if (type != SSL3_RT_APPLICATION_DATA) { |
| 70 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 71 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 72 |
return ssl_open_record_error; |
| 73 |
} |
| 74 |
|
| 75 |
if (record.empty()) { |
| 76 |
return ssl_open_record_discard; |
| 77 |
} |
| 78 |
|
| 79 |
*out = record; |
| 80 |
return ssl_open_record_success; |
| 81 |
} |
| 82 |
|
| 83 |
int dtls1_write_app_data(SSL *ssl, bool *out_needs_handshake, |
| 84 |
size_t *out_bytes_written, Span<const uint8_t> in) { |
| 85 |
assert(!SSL_in_init(ssl)); |
| 86 |
*out_needs_handshake = false; |
| 87 |
|
| 88 |
if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 89 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
// DTLS does not split the input across records. |
| 94 |
if (in.size() > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 95 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
|
| 99 |
if (in.empty()) { |
| 100 |
*out_bytes_written = 0; |
| 101 |
return 1; |
| 102 |
} |
| 103 |
|
| 104 |
int ret = dtls1_write_record(ssl, SSL3_RT_APPLICATION_DATA, in, |
| 105 |
dtls1_use_current_epoch); |
| 106 |
if (ret <= 0) { |
| 107 |
return ret; |
| 108 |
} |
| 109 |
*out_bytes_written = in.size(); |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
int dtls1_write_record(SSL *ssl, int type, Span<const uint8_t> in, |
| 114 |
enum dtls1_use_epoch_t use_epoch) { |
| 115 |
SSLBuffer *buf = &ssl->s3->write_buffer; |
| 116 |
assert(in.size() <= SSL3_RT_MAX_PLAIN_LENGTH); |
| 117 |
// There should never be a pending write buffer in DTLS. One can't write half |
| 118 |
// a datagram, so the write buffer is always dropped in |
| 119 |
// |ssl_write_buffer_flush|. |
| 120 |
assert(buf->empty()); |
| 121 |
|
| 122 |
if (in.size() > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 123 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
size_t ciphertext_len; |
| 128 |
if (!buf->EnsureCap(ssl_seal_align_prefix_len(ssl), |
| 129 |
in.size() + SSL_max_seal_overhead(ssl)) || |
| 130 |
!dtls_seal_record(ssl, buf->remaining().data(), &ciphertext_len, |
| 131 |
buf->remaining().size(), type, in.data(), in.size(), |
| 132 |
use_epoch)) { |
| 133 |
buf->Clear(); |
| 134 |
return -1; |
| 135 |
} |
| 136 |
buf->DidWrite(ciphertext_len); |
| 137 |
|
| 138 |
int ret = ssl_write_buffer_flush(ssl); |
| 139 |
if (ret <= 0) { |
| 140 |
return ret; |
| 141 |
} |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
int dtls1_dispatch_alert(SSL *ssl) { |
| 146 |
int ret = dtls1_write_record(ssl, SSL3_RT_ALERT, ssl->s3->send_alert, |
| 147 |
dtls1_use_current_epoch); |
| 148 |
if (ret <= 0) { |
| 149 |
return ret; |
| 150 |
} |
| 151 |
ssl->s3->alert_dispatch = false; |
| 152 |
|
| 153 |
// If the alert is fatal, flush the BIO now. |
| 154 |
if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) { |
| 155 |
BIO_flush(ssl->wbio.get()); |
| 156 |
} |
| 157 |
|
| 158 |
ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert); |
| 159 |
|
| 160 |
int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1]; |
| 161 |
ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert); |
| 162 |
|
| 163 |
return 1; |
| 164 |
} |
| 165 |
|
| 166 |
BSSL_NAMESPACE_END |