| 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 |
| 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 |
| 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 |
| 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 | if (!next_epoch(ssl, &new_epoch.epoch, level, ssl->d1->read_epoch.epoch)) { |
| 84 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 85 | return false; |
| 86 | } |
| 87 |
| 88 | if (ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
| 89 | new_epoch.rn_encrypter = |
| 90 | RecordNumberEncrypter::Create(new_epoch.aead->cipher(), traffic_secret); |
| 91 | if (new_epoch.rn_encrypter == nullptr) { |
| 92 | return false; |
| 93 | } |
| 94 |
| 95 | // In DTLS 1.3, new read epochs are not applied immediately. In principle, |
| 96 | // we could do the same in DTLS 1.2, but we would ignore every record from |
| 97 | // the previous epoch anyway. |
| 98 | assert(ssl->d1->next_read_epoch == nullptr); |
| 99 | ssl->d1->next_read_epoch = MakeUnique<DTLSReadEpoch>(std::move(new_epoch)); |
| 100 | if (ssl->d1->next_read_epoch == nullptr) { |
| 101 | return false; |
| 102 | } |
| 103 | } else { |
| 104 | ssl->d1->read_epoch = std::move(new_epoch); |
| 105 | ssl->d1->has_change_cipher_spec = false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 |
| 110 | static bool dtls1_set_write_state(SSL *ssl, ssl_encryption_level_t level, |
| 111 | UniquePtr<SSLAEADContext> aead_ctx, |
| 112 | Span<const uint8_t> traffic_secret) { |
| 113 | uint16_t epoch; |
| 114 | if (!next_epoch(ssl, &epoch, level, ssl->d1->write_epoch.epoch())) { |
| 115 | return false; |
| 116 | } |
| 117 |
| 118 | DTLSWriteEpoch new_epoch; |
| 119 | new_epoch.aead = std::move(aead_ctx); |
| 120 | new_epoch.next_record = DTLSRecordNumber(epoch, 0); |
| 121 | if (ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
| 122 | new_epoch.rn_encrypter = |
| 123 | RecordNumberEncrypter::Create(new_epoch.aead->cipher(), traffic_secret); |
| 124 | if (new_epoch.rn_encrypter == nullptr) { |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 |
| 129 | auto current = MakeUnique<DTLSWriteEpoch>(std::move(ssl->d1->write_epoch)); |
| 130 | if (current == nullptr) { |
| 131 | return false; |
| 132 | } |
| 133 |
| 134 | ssl->d1->write_epoch = std::move(new_epoch); |
| 135 | ssl->d1->extra_write_epochs.PushBack(std::move(current)); |
| 136 | dtls_clear_unused_write_epochs(ssl); |
| 137 | return true; |
| 138 | } |
| 139 |
| 140 | static const SSL_PROTOCOL_METHOD kDTLSProtocolMethod = { |
| 141 | true /* is_dtls */, |
| 142 | dtls1_new, |
| 143 | dtls1_free, |
| 144 | dtls1_get_message, |
| 145 | dtls1_next_message, |
| 146 | dtls_has_unprocessed_handshake_data, |
| 147 | dtls1_open_handshake, |
| 148 | dtls1_open_change_cipher_spec, |
| 149 | dtls1_open_app_data, |
| 150 | dtls1_write_app_data, |
| 151 | dtls1_dispatch_alert, |
| 152 | dtls1_init_message, |
| 153 | dtls1_finish_message, |
| 154 | dtls1_add_message, |
| 155 | dtls1_add_change_cipher_spec, |
| 156 | dtls1_finish_flight, |
| 157 | dtls1_schedule_ack, |
| 158 | dtls1_flush, |
| 159 | dtls1_on_handshake_complete, |
| 160 | dtls1_set_read_state, |
| 161 | dtls1_set_write_state, |
| 162 | }; |
| 163 |
| 164 | const SSL_METHOD *DTLS_method(void) { |
| 165 | static const SSL_METHOD kMethod = { |
| 166 | 0, |
| 167 | &kDTLSProtocolMethod, |
| 168 | &ssl_crypto_x509_method, |
| 169 | }; |
| 170 | return &kMethod; |
| 171 | } |
| 172 |
| 173 | const SSL_METHOD *DTLS_with_buffers_method(void) { |
| 174 | static const SSL_METHOD kMethod = { |
| 175 | 0, |
| 176 | &kDTLSProtocolMethod, |
| 177 | &ssl_noop_x509_method, |
| 178 | }; |
| 179 | return &kMethod; |
| 180 | } |
| 181 |
| 182 | // Legacy version-locked methods. |
| 183 |
| 184 | const SSL_METHOD *DTLSv1_2_method(void) { |
| 185 | static const SSL_METHOD kMethod = { |
| 186 | DTLS1_2_VERSION, |
| 187 | &kDTLSProtocolMethod, |
| 188 | &ssl_crypto_x509_method, |
| 189 | }; |
| 190 | return &kMethod; |
| 191 | } |
| 192 |
| 193 | const SSL_METHOD *DTLSv1_method(void) { |
| 194 | static const SSL_METHOD kMethod = { |
| 195 | DTLS1_VERSION, |
| 196 | &kDTLSProtocolMethod, |
| 197 | &ssl_crypto_x509_method, |
| 198 | }; |
| 199 | return &kMethod; |
| 200 | } |
| 201 |
| 202 | // Legacy side-specific methods. |
| 203 |
| 204 | const SSL_METHOD *DTLSv1_2_server_method(void) { return DTLSv1_2_method(); } |
| 205 |
| 206 | const SSL_METHOD *DTLSv1_server_method(void) { return DTLSv1_method(); } |
| 207 |
| 208 | const SSL_METHOD *DTLSv1_2_client_method(void) { return DTLSv1_2_method(); } |
| 209 |
| 210 | const SSL_METHOD *DTLSv1_client_method(void) { return DTLSv1_method(); } |
| 211 |
| 212 | const SSL_METHOD *DTLS_server_method(void) { return DTLS_method(); } |
| 213 |
| 214 | const SSL_METHOD *DTLS_client_method(void) { return DTLS_method(); } |
| 215 |