1 /*
2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/ssl.h>
11
12 #include <assert.h>
13 #include <string.h>
14
15 #include <openssl/err.h>
16
17 #include "../crypto/internal.h"
18 #include "internal.h"
19
20
21 using namespace bssl;
22
dtls1_on_handshake_complete(SSL * ssl)23 static void dtls1_on_handshake_complete(SSL *ssl) {
24 if (ssl_protocol_version(ssl) <= TLS1_2_VERSION) {
25 // Stop the reply timer left by the last flight we sent. In DTLS 1.2, the
26 // retransmission timer ends when the handshake completes. If we sent the
27 // final flight, we may still need to retransmit it, but that is driven by
28 // messages from the peer.
29 dtls1_stop_timer(ssl);
30 // If the final flight had a reply, we know the peer has received it. If
31 // not, we must leave the flight around for post-handshake retransmission.
32 if (ssl->d1->flight_has_reply) {
33 dtls_clear_outgoing_messages(ssl);
34 }
35 }
36 }
37
next_epoch(const SSL * ssl,uint16_t * out,ssl_encryption_level_t level,uint16_t prev)38 static bool next_epoch(const SSL *ssl, uint16_t *out,
39 ssl_encryption_level_t level, uint16_t prev) {
40 switch (level) {
41 case ssl_encryption_initial:
42 case ssl_encryption_early_data:
43 case ssl_encryption_handshake:
44 *out = static_cast<uint16_t>(level);
45 return true;
46
47 case ssl_encryption_application:
48 if (prev < ssl_encryption_application &&
49 ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
50 *out = static_cast<uint16_t>(level);
51 return true;
52 }
53
54 if (prev == 0xffff) {
55 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES);
56 return false;
57 }
58 *out = prev + 1;
59 return true;
60 }
61
62 assert(0);
63 return false;
64 }
65
dtls1_set_read_state(SSL * ssl,ssl_encryption_level_t level,UniquePtr<SSLAEADContext> aead_ctx,Span<const uint8_t> traffic_secret)66 static bool dtls1_set_read_state(SSL *ssl, ssl_encryption_level_t level,
67 UniquePtr<SSLAEADContext> aead_ctx,
68 Span<const uint8_t> traffic_secret) {
69 // Cipher changes are forbidden if the current epoch has leftover data.
70 if (dtls_has_unprocessed_handshake_data(ssl)) {
71 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
72 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
73 return false;
74 }
75
76 DTLSReadEpoch new_epoch;
77 new_epoch.aead = std::move(aead_ctx);
78 if (!next_epoch(ssl, &new_epoch.epoch, level, ssl->d1->read_epoch.epoch)) {
79 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
80 return false;
81 }
82
83 if (ssl_protocol_version(ssl) > TLS1_2_VERSION) {
84 new_epoch.rn_encrypter =
85 RecordNumberEncrypter::Create(new_epoch.aead->cipher(), traffic_secret);
86 if (new_epoch.rn_encrypter == nullptr) {
87 return false;
88 }
89
90 // In DTLS 1.3, new read epochs are not applied immediately. In principle,
91 // we could do the same in DTLS 1.2, but we would ignore every record from
92 // the previous epoch anyway.
93 assert(ssl->d1->next_read_epoch == nullptr);
94 ssl->d1->next_read_epoch = MakeUnique<DTLSReadEpoch>(std::move(new_epoch));
95 if (ssl->d1->next_read_epoch == nullptr) {
96 return false;
97 }
98 } else {
99 ssl->d1->read_epoch = std::move(new_epoch);
100 ssl->d1->has_change_cipher_spec = false;
101 }
102 return true;
103 }
104
dtls1_set_write_state(SSL * ssl,ssl_encryption_level_t level,UniquePtr<SSLAEADContext> aead_ctx,Span<const uint8_t> traffic_secret)105 static bool dtls1_set_write_state(SSL *ssl, ssl_encryption_level_t level,
106 UniquePtr<SSLAEADContext> aead_ctx,
107 Span<const uint8_t> traffic_secret) {
108 uint16_t epoch;
109 if (!next_epoch(ssl, &epoch, level, ssl->d1->write_epoch.epoch())) {
110 return false;
111 }
112
113 DTLSWriteEpoch new_epoch;
114 new_epoch.aead = std::move(aead_ctx);
115 new_epoch.next_record = DTLSRecordNumber(epoch, 0);
116 if (ssl_protocol_version(ssl) > TLS1_2_VERSION) {
117 new_epoch.rn_encrypter =
118 RecordNumberEncrypter::Create(new_epoch.aead->cipher(), traffic_secret);
119 if (new_epoch.rn_encrypter == nullptr) {
120 return false;
121 }
122 }
123
124 auto current = MakeUnique<DTLSWriteEpoch>(std::move(ssl->d1->write_epoch));
125 if (current == nullptr) {
126 return false;
127 }
128
129 ssl->d1->write_epoch = std::move(new_epoch);
130 ssl->d1->extra_write_epochs.PushBack(std::move(current));
131 dtls_clear_unused_write_epochs(ssl);
132 return true;
133 }
134
135 static const SSL_PROTOCOL_METHOD kDTLSProtocolMethod = {
136 true /* is_dtls */,
137 dtls1_new,
138 dtls1_free,
139 dtls1_get_message,
140 dtls1_next_message,
141 dtls_has_unprocessed_handshake_data,
142 dtls1_open_handshake,
143 dtls1_open_change_cipher_spec,
144 dtls1_open_app_data,
145 dtls1_write_app_data,
146 dtls1_dispatch_alert,
147 dtls1_init_message,
148 dtls1_finish_message,
149 dtls1_add_message,
150 dtls1_add_change_cipher_spec,
151 dtls1_finish_flight,
152 dtls1_schedule_ack,
153 dtls1_flush,
154 dtls1_on_handshake_complete,
155 dtls1_set_read_state,
156 dtls1_set_write_state,
157 };
158
DTLS_method(void)159 const SSL_METHOD *DTLS_method(void) {
160 static const SSL_METHOD kMethod = {
161 0,
162 &kDTLSProtocolMethod,
163 &ssl_crypto_x509_method,
164 };
165 return &kMethod;
166 }
167
DTLS_with_buffers_method(void)168 const SSL_METHOD *DTLS_with_buffers_method(void) {
169 static const SSL_METHOD kMethod = {
170 0,
171 &kDTLSProtocolMethod,
172 &ssl_noop_x509_method,
173 };
174 return &kMethod;
175 }
176
177 // Legacy version-locked methods.
178
DTLSv1_2_method(void)179 const SSL_METHOD *DTLSv1_2_method(void) {
180 static const SSL_METHOD kMethod = {
181 DTLS1_2_VERSION,
182 &kDTLSProtocolMethod,
183 &ssl_crypto_x509_method,
184 };
185 return &kMethod;
186 }
187
DTLSv1_method(void)188 const SSL_METHOD *DTLSv1_method(void) {
189 static const SSL_METHOD kMethod = {
190 DTLS1_VERSION,
191 &kDTLSProtocolMethod,
192 &ssl_crypto_x509_method,
193 };
194 return &kMethod;
195 }
196
197 // Legacy side-specific methods.
198
DTLSv1_2_server_method(void)199 const SSL_METHOD *DTLSv1_2_server_method(void) { return DTLSv1_2_method(); }
200
DTLSv1_server_method(void)201 const SSL_METHOD *DTLSv1_server_method(void) { return DTLSv1_method(); }
202
DTLSv1_2_client_method(void)203 const SSL_METHOD *DTLSv1_2_client_method(void) { return DTLSv1_2_method(); }
204
DTLSv1_client_method(void)205 const SSL_METHOD *DTLSv1_client_method(void) { return DTLSv1_method(); }
206
DTLS_server_method(void)207 const SSL_METHOD *DTLS_server_method(void) { return DTLS_method(); }
208
DTLS_client_method(void)209 const SSL_METHOD *DTLS_client_method(void) { return DTLS_method(); }
210