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