| 1 |
// DTLS implementation written by Nagendra Modadugu ([email protected]) for the OpenSSL project 2005. |
| 2 |
// Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/ssl.h> |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/err.h> |
| 11 |
|
| 12 |
#include "../crypto/internal.h" |
| 13 |
#include "internal.h" |
| 14 |
|
| 15 |
|
| 16 |
using namespace bssl; |
| 17 |
|
| 18 |
static void dtls1_on_handshake_complete(SSL *ssl) { |
| 19 |
// Stop the reply timer left by the last flight we sent. |
| 20 |
dtls1_stop_timer(ssl); |
| 21 |
// If the final flight had a reply, we know the peer has received it. If not, |
| 22 |
// we must leave the flight around for post-handshake retransmission. |
| 23 |
if (ssl->d1->flight_has_reply) { |
| 24 |
dtls_clear_outgoing_messages(ssl); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
static bool dtls1_set_read_state(SSL *ssl, ssl_encryption_level_t level, |
| 29 |
UniquePtr<SSLAEADContext> aead_ctx, |
| 30 |
Span<const uint8_t> secret_for_quic) { |
| 31 |
assert(secret_for_quic.empty()); // QUIC does not use DTLS. |
| 32 |
// Cipher changes are forbidden if the current epoch has leftover data. |
| 33 |
if (dtls_has_unprocessed_handshake_data(ssl)) { |
| 34 |
OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 35 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
ssl->d1->r_epoch++; |
| 40 |
ssl->d1->bitmap = DTLS1_BITMAP(); |
| 41 |
OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence)); |
| 42 |
|
| 43 |
ssl->s3->aead_read_ctx = std::move(aead_ctx); |
| 44 |
ssl->s3->read_level = level; |
| 45 |
ssl->d1->has_change_cipher_spec = false; |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
static bool dtls1_set_write_state(SSL *ssl, ssl_encryption_level_t level, |
| 50 |
UniquePtr<SSLAEADContext> aead_ctx, |
| 51 |
Span<const uint8_t> secret_for_quic) { |
| 52 |
assert(secret_for_quic.empty()); // QUIC does not use DTLS. |
| 53 |
ssl->d1->w_epoch++; |
| 54 |
OPENSSL_memcpy(ssl->d1->last_write_sequence, ssl->s3->write_sequence, |
| 55 |
sizeof(ssl->s3->write_sequence)); |
| 56 |
OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence)); |
| 57 |
|
| 58 |
ssl->d1->last_aead_write_ctx = std::move(ssl->s3->aead_write_ctx); |
| 59 |
ssl->s3->aead_write_ctx = std::move(aead_ctx); |
| 60 |
ssl->s3->write_level = level; |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
static const SSL_PROTOCOL_METHOD kDTLSProtocolMethod = { |
| 65 |
true /* is_dtls */, |
| 66 |
dtls1_new, |
| 67 |
dtls1_free, |
| 68 |
dtls1_get_message, |
| 69 |
dtls1_next_message, |
| 70 |
dtls_has_unprocessed_handshake_data, |
| 71 |
dtls1_open_handshake, |
| 72 |
dtls1_open_change_cipher_spec, |
| 73 |
dtls1_open_app_data, |
| 74 |
dtls1_write_app_data, |
| 75 |
dtls1_dispatch_alert, |
| 76 |
dtls1_init_message, |
| 77 |
dtls1_finish_message, |
| 78 |
dtls1_add_message, |
| 79 |
dtls1_add_change_cipher_spec, |
| 80 |
dtls1_flush_flight, |
| 81 |
dtls1_on_handshake_complete, |
| 82 |
dtls1_set_read_state, |
| 83 |
dtls1_set_write_state, |
| 84 |
}; |
| 85 |
|
| 86 |
const SSL_METHOD *DTLS_method(void) { |
| 87 |
static const SSL_METHOD kMethod = { |
| 88 |
0, |
| 89 |
&kDTLSProtocolMethod, |
| 90 |
&ssl_crypto_x509_method, |
| 91 |
}; |
| 92 |
return &kMethod; |
| 93 |
} |
| 94 |
|
| 95 |
const SSL_METHOD *DTLS_with_buffers_method(void) { |
| 96 |
static const SSL_METHOD kMethod = { |
| 97 |
0, |
| 98 |
&kDTLSProtocolMethod, |
| 99 |
&ssl_noop_x509_method, |
| 100 |
}; |
| 101 |
return &kMethod; |
| 102 |
} |
| 103 |
|
| 104 |
// Legacy version-locked methods. |
| 105 |
|
| 106 |
const SSL_METHOD *DTLSv1_2_method(void) { |
| 107 |
static const SSL_METHOD kMethod = { |
| 108 |
DTLS1_2_VERSION, |
| 109 |
&kDTLSProtocolMethod, |
| 110 |
&ssl_crypto_x509_method, |
| 111 |
}; |
| 112 |
return &kMethod; |
| 113 |
} |
| 114 |
|
| 115 |
const SSL_METHOD *DTLSv1_method(void) { |
| 116 |
static const SSL_METHOD kMethod = { |
| 117 |
DTLS1_VERSION, |
| 118 |
&kDTLSProtocolMethod, |
| 119 |
&ssl_crypto_x509_method, |
| 120 |
}; |
| 121 |
return &kMethod; |
| 122 |
} |
| 123 |
|
| 124 |
// Legacy side-specific methods. |
| 125 |
|
| 126 |
const SSL_METHOD *DTLSv1_2_server_method(void) { |
| 127 |
return DTLSv1_2_method(); |
| 128 |
} |
| 129 |
|
| 130 |
const SSL_METHOD *DTLSv1_server_method(void) { |
| 131 |
return DTLSv1_method(); |
| 132 |
} |
| 133 |
|
| 134 |
const SSL_METHOD *DTLSv1_2_client_method(void) { |
| 135 |
return DTLSv1_2_method(); |
| 136 |
} |
| 137 |
|
| 138 |
const SSL_METHOD *DTLSv1_client_method(void) { |
| 139 |
return DTLSv1_method(); |
| 140 |
} |
| 141 |
|
| 142 |
const SSL_METHOD *DTLS_server_method(void) { |
| 143 |
return DTLS_method(); |
| 144 |
} |
| 145 |
|
| 146 |
const SSL_METHOD *DTLS_client_method(void) { |
| 147 |
return DTLS_method(); |
| 148 |
} |