Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2018, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/ssl.h> |
| 16 | |
| 17 | #include <openssl/bytestring.h> |
| 18 | |
| 19 | #include "internal.h" |
| 20 | |
| 21 | |
| 22 | namespace bssl { |
| 23 | |
| 24 | constexpr int kHandoffVersion = 0; |
| 25 | constexpr int kHandbackVersion = 0; |
| 26 | |
| 27 | bool SSL_serialize_handoff(const SSL *ssl, CBB *out) { |
| 28 | const SSL3_STATE *const s3 = ssl->s3; |
| 29 | if (!ssl->server || |
| 30 | s3->hs == nullptr || |
| 31 | s3->rwstate != SSL_HANDOFF) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | CBB seq; |
| 36 | Span<const uint8_t> transcript = s3->hs->transcript.buffer(); |
| 37 | if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 38 | !CBB_add_asn1_uint64(&seq, kHandoffVersion) || |
| 39 | !CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) || |
| 40 | !CBB_add_asn1_octet_string(&seq, |
| 41 | reinterpret_cast<uint8_t *>(s3->hs_buf->data), |
| 42 | s3->hs_buf->length) || |
| 43 | !CBB_flush(out)) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool SSL_decline_handoff(SSL *ssl) { |
| 51 | const SSL3_STATE *const s3 = ssl->s3; |
| 52 | if (!ssl->server || |
| 53 | s3->hs == nullptr || |
| 54 | s3->rwstate != SSL_HANDOFF) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | ssl->handoff = false; |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool SSL_apply_handoff(SSL *ssl, Span<const uint8_t> handoff) { |
| 63 | if (ssl->method->is_dtls) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | CBS seq, handoff_cbs(handoff); |
| 68 | uint64_t handoff_version; |
| 69 | if (!CBS_get_asn1(&handoff_cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 70 | !CBS_get_asn1_uint64(&seq, &handoff_version) || |
| 71 | handoff_version != kHandoffVersion) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | CBS transcript, hs_buf; |
| 76 | if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) || |
| 77 | !CBS_get_asn1(&seq, &hs_buf, CBS_ASN1_OCTETSTRING)) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | SSL_set_accept_state(ssl); |
| 82 | |
| 83 | SSL3_STATE *const s3 = ssl->s3; |
| 84 | s3->v2_hello_done = true; |
| 85 | s3->has_message = true; |
| 86 | |
| 87 | s3->hs_buf.reset(BUF_MEM_new()); |
| 88 | if (!s3->hs_buf || |
| 89 | !BUF_MEM_append(s3->hs_buf.get(), CBS_data(&hs_buf), CBS_len(&hs_buf))) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (CBS_len(&transcript) != 0) { |
| 94 | s3->hs->transcript.Update(transcript); |
| 95 | s3->is_v2_hello = true; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 96 | } |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 97 | ssl->handback = true; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool SSL_serialize_handback(const SSL *ssl, CBB *out) { |
| 103 | if (!ssl->server || |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 104 | (ssl->s3->hs->state != state12_finish_server_handshake && |
| 105 | ssl->s3->hs->state != state12_read_client_certificate) || |
| 106 | ssl->method->is_dtls || ssl->version < TLS1_VERSION) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
| 110 | const SSL3_STATE *const s3 = ssl->s3; |
| 111 | size_t hostname_len = 0; |
| 112 | if (s3->hostname) { |
| 113 | hostname_len = strlen(s3->hostname.get()); |
| 114 | } |
| 115 | |
| 116 | size_t iv_len = 0; |
| 117 | const uint8_t *read_iv = nullptr, *write_iv = nullptr; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 118 | Span<const uint8_t> transcript; |
| 119 | if (ssl->s3->hs->state == state12_finish_server_handshake) { |
| 120 | if (ssl->version == TLS1_VERSION && |
| 121 | SSL_CIPHER_is_block_cipher(s3->aead_read_ctx->cipher()) && |
| 122 | (!s3->aead_read_ctx->GetIV(&read_iv, &iv_len) || |
| 123 | !s3->aead_write_ctx->GetIV(&write_iv, &iv_len))) { |
| 124 | return false; |
| 125 | } |
| 126 | } else { |
| 127 | transcript = s3->hs->transcript.buffer(); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 130 | // TODO(mab): make sure everything is serialized. |
| 131 | CBB seq, key_share; |
| 132 | SSL_SESSION *session = |
| 133 | s3->session_reused ? ssl->session : s3->hs->new_session.get(); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 134 | if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 135 | !CBB_add_asn1_uint64(&seq, kHandbackVersion) || |
| 136 | !CBB_add_asn1_uint64(&seq, ssl->version) || |
| 137 | !CBB_add_asn1_uint64(&seq, ssl->conf_max_version) || |
| 138 | !CBB_add_asn1_uint64(&seq, ssl->conf_min_version) || |
| 139 | !CBB_add_asn1_uint64(&seq, ssl->max_send_fragment) || |
| 140 | !CBB_add_asn1_octet_string(&seq, s3->read_sequence, |
| 141 | sizeof(s3->read_sequence)) || |
| 142 | !CBB_add_asn1_octet_string(&seq, s3->write_sequence, |
| 143 | sizeof(s3->write_sequence)) || |
| 144 | !CBB_add_asn1_octet_string(&seq, s3->server_random, |
| 145 | sizeof(s3->server_random)) || |
| 146 | !CBB_add_asn1_octet_string(&seq, s3->client_random, |
| 147 | sizeof(s3->client_random)) || |
| 148 | !CBB_add_asn1_octet_string(&seq, read_iv, iv_len) || |
| 149 | !CBB_add_asn1_octet_string(&seq, write_iv, iv_len) || |
| 150 | !CBB_add_asn1_bool(&seq, s3->session_reused) || |
| 151 | !CBB_add_asn1_bool(&seq, s3->send_connection_binding) || |
| 152 | !CBB_add_asn1_bool(&seq, s3->tlsext_channel_id_valid) || |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 153 | !ssl_session_serialize(session, &seq) || |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 154 | !CBB_add_asn1_octet_string(&seq, s3->next_proto_negotiated.data(), |
| 155 | s3->next_proto_negotiated.size()) || |
| 156 | !CBB_add_asn1_octet_string(&seq, s3->alpn_selected.data(), |
| 157 | s3->alpn_selected.size()) || |
| 158 | !CBB_add_asn1_octet_string( |
| 159 | &seq, reinterpret_cast<uint8_t *>(s3->hostname.get()), |
| 160 | hostname_len) || |
| 161 | !CBB_add_asn1_octet_string(&seq, s3->tlsext_channel_id, |
| 162 | sizeof(s3->tlsext_channel_id)) || |
| 163 | !CBB_add_asn1_uint64(&seq, ssl->options) || |
| 164 | !CBB_add_asn1_uint64(&seq, ssl->mode) || |
| 165 | !CBB_add_asn1_uint64(&seq, ssl->max_cert_list) || |
| 166 | !CBB_add_asn1_bool(&seq, ssl->quiet_shutdown) || |
| 167 | !CBB_add_asn1_bool(&seq, ssl->tlsext_channel_id_enabled) || |
| 168 | !CBB_add_asn1_bool(&seq, ssl->retain_only_sha256_of_client_certs) || |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 169 | !CBB_add_asn1_bool(&seq, ssl->token_binding_negotiated) || |
| 170 | !CBB_add_asn1_uint64(&seq, ssl->negotiated_token_binding_param) || |
| 171 | !CBB_add_asn1_bool(&seq, s3->hs->next_proto_neg_seen) || |
| 172 | !CBB_add_asn1_bool(&seq, s3->hs->cert_request) || |
| 173 | !CBB_add_asn1_bool(&seq, s3->hs->extended_master_secret) || |
| 174 | !CBB_add_asn1_bool(&seq, s3->hs->ticket_expected) || |
| 175 | !CBB_add_asn1_uint64(&seq, SSL_CIPHER_get_id(s3->hs->new_cipher)) || |
| 176 | !CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) || |
| 177 | !CBB_add_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 178 | return false; |
| 179 | } |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 180 | if (ssl->s3->hs->state == state12_read_client_certificate && |
| 181 | !s3->hs->key_share->Serialize(&key_share)) { |
| 182 | return false; |
| 183 | } |
| 184 | return CBB_flush(out); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool SSL_apply_handback(SSL *ssl, Span<const uint8_t> handback) { |
| 188 | if (ssl->do_handshake != nullptr || |
| 189 | ssl->method->is_dtls) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | SSL3_STATE *const s3 = ssl->s3; |
| 194 | uint64_t handback_version, version, conf_max_version, conf_min_version, |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 195 | max_send_fragment, options, mode, max_cert_list, |
| 196 | negotiated_token_binding_param, cipher; |
| 197 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 198 | CBS seq, read_seq, write_seq, server_rand, client_rand, read_iv, write_iv, |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 199 | next_proto, alpn, hostname, channel_id, transcript, key_share; |
| 200 | int session_reused, send_connection_binding, channel_id_valid, quiet_shutdown, |
| 201 | channel_id_enabled, retain_only_sha256, cert_request, |
| 202 | extended_master_secret, ticket_expected, token_binding_negotiated, |
| 203 | next_proto_neg_seen; |
| 204 | SSL_SESSION *session = nullptr; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 205 | |
| 206 | CBS handback_cbs(handback); |
| 207 | if (!CBS_get_asn1(&handback_cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 208 | !CBS_get_asn1_uint64(&seq, &handback_version) || |
| 209 | handback_version != kHandbackVersion) { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | if (!CBS_get_asn1_uint64(&seq, &version) || |
| 214 | !CBS_get_asn1_uint64(&seq, &conf_max_version) || |
| 215 | !CBS_get_asn1_uint64(&seq, &conf_min_version) || |
| 216 | !CBS_get_asn1_uint64(&seq, &max_send_fragment) || |
| 217 | !CBS_get_asn1(&seq, &read_seq, CBS_ASN1_OCTETSTRING) || |
| 218 | CBS_len(&read_seq) != sizeof(s3->read_sequence) || |
| 219 | !CBS_get_asn1(&seq, &write_seq, CBS_ASN1_OCTETSTRING) || |
| 220 | CBS_len(&write_seq) != sizeof(s3->write_sequence) || |
| 221 | !CBS_get_asn1(&seq, &server_rand, CBS_ASN1_OCTETSTRING) || |
| 222 | CBS_len(&server_rand) != sizeof(s3->server_random) || |
| 223 | !CBS_copy_bytes(&server_rand, s3->server_random, |
| 224 | sizeof(s3->server_random)) || |
| 225 | !CBS_get_asn1(&seq, &client_rand, CBS_ASN1_OCTETSTRING) || |
| 226 | CBS_len(&client_rand) != sizeof(s3->client_random) || |
| 227 | !CBS_copy_bytes(&client_rand, s3->client_random, |
| 228 | sizeof(s3->client_random)) || |
| 229 | !CBS_get_asn1(&seq, &read_iv, CBS_ASN1_OCTETSTRING) || |
| 230 | !CBS_get_asn1(&seq, &write_iv, CBS_ASN1_OCTETSTRING) || |
| 231 | !CBS_get_asn1_bool(&seq, &session_reused) || |
| 232 | !CBS_get_asn1_bool(&seq, &send_connection_binding) || |
| 233 | !CBS_get_asn1_bool(&seq, &channel_id_valid)) { |
| 234 | return false; |
| 235 | } |
| 236 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 237 | s3->hs = ssl_handshake_new(ssl); |
| 238 | if (session_reused) { |
| 239 | ssl->session = |
| 240 | SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool) |
| 241 | .release(); |
| 242 | session = ssl->session; |
| 243 | } else { |
| 244 | s3->hs->new_session = |
| 245 | SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool); |
| 246 | session = s3->hs->new_session.get(); |
| 247 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 248 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 249 | if (!session || !CBS_get_asn1(&seq, &next_proto, CBS_ASN1_OCTETSTRING) || |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 250 | !CBS_get_asn1(&seq, &alpn, CBS_ASN1_OCTETSTRING) || |
| 251 | !CBS_get_asn1(&seq, &hostname, CBS_ASN1_OCTETSTRING) || |
| 252 | !CBS_get_asn1(&seq, &channel_id, CBS_ASN1_OCTETSTRING) || |
| 253 | CBS_len(&channel_id) != sizeof(s3->tlsext_channel_id) || |
| 254 | !CBS_copy_bytes(&channel_id, s3->tlsext_channel_id, |
| 255 | sizeof(s3->tlsext_channel_id)) || |
| 256 | !CBS_get_asn1_uint64(&seq, &options) || |
| 257 | !CBS_get_asn1_uint64(&seq, &mode) || |
| 258 | !CBS_get_asn1_uint64(&seq, &max_cert_list) || |
| 259 | !CBS_get_asn1_bool(&seq, &quiet_shutdown) || |
| 260 | !CBS_get_asn1_bool(&seq, &channel_id_enabled) || |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 261 | !CBS_get_asn1_bool(&seq, &retain_only_sha256) || |
| 262 | !CBS_get_asn1_bool(&seq, &token_binding_negotiated) || |
| 263 | !CBS_get_asn1_uint64(&seq, &negotiated_token_binding_param) || |
| 264 | !CBS_get_asn1_bool(&seq, &next_proto_neg_seen) || |
| 265 | !CBS_get_asn1_bool(&seq, &cert_request) || |
| 266 | !CBS_get_asn1_bool(&seq, &extended_master_secret) || |
| 267 | !CBS_get_asn1_bool(&seq, &ticket_expected) || |
| 268 | !CBS_get_asn1_uint64(&seq, &cipher)) { |
| 269 | return false; |
| 270 | } |
| 271 | if ((s3->hs->new_cipher = |
| 272 | SSL_get_cipher_by_value(static_cast<uint16_t>(cipher))) == nullptr) { |
| 273 | return false; |
| 274 | } |
| 275 | if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) || |
| 276 | !CBS_get_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | ssl->version = version; |
| 281 | ssl->conf_max_version = conf_max_version; |
| 282 | ssl->conf_min_version = conf_min_version; |
| 283 | ssl->max_send_fragment = max_send_fragment; |
| 284 | ssl->do_handshake = ssl_server_handshake; |
| 285 | ssl->server = true; |
| 286 | ssl->options = options; |
| 287 | ssl->mode = mode; |
| 288 | ssl->max_cert_list = max_cert_list; |
| 289 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 290 | s3->have_version = true; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 291 | s3->hs->state = CBS_len(&transcript) == 0 ? state12_finish_server_handshake |
| 292 | : state12_read_client_certificate; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 293 | s3->session_reused = session_reused; |
| 294 | s3->send_connection_binding = send_connection_binding; |
| 295 | s3->tlsext_channel_id_valid = channel_id_valid; |
| 296 | s3->next_proto_negotiated.CopyFrom(next_proto); |
| 297 | s3->alpn_selected.CopyFrom(alpn); |
| 298 | |
| 299 | const size_t hostname_len = CBS_len(&hostname); |
| 300 | if (hostname_len == 0) { |
| 301 | s3->hostname.reset(); |
| 302 | } else { |
| 303 | char *hostname_str = nullptr; |
| 304 | if (!CBS_strdup(&hostname, &hostname_str)) { |
| 305 | return false; |
| 306 | } |
| 307 | s3->hostname.reset(hostname_str); |
| 308 | } |
| 309 | |
| 310 | ssl->quiet_shutdown = quiet_shutdown; |
| 311 | ssl->tlsext_channel_id_enabled = channel_id_enabled; |
| 312 | ssl->retain_only_sha256_of_client_certs = retain_only_sha256; |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 313 | ssl->token_binding_negotiated = token_binding_negotiated; |
| 314 | ssl->negotiated_token_binding_param = |
| 315 | static_cast<uint8_t>(negotiated_token_binding_param); |
| 316 | s3->hs->next_proto_neg_seen = next_proto_neg_seen; |
| 317 | s3->hs->wait = ssl_hs_flush; |
| 318 | s3->hs->extended_master_secret = extended_master_secret; |
| 319 | s3->hs->ticket_expected = ticket_expected; |
| 320 | s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
| 321 | s3->hs->cert_request = cert_request; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 322 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 323 | if (s3->hs->state == state12_finish_server_handshake) { |
| 324 | Array<uint8_t> key_block; |
| 325 | if (!tls1_configure_aead(ssl, evp_aead_open, &key_block, session->cipher, |
| 326 | read_iv) || |
| 327 | !tls1_configure_aead(ssl, evp_aead_seal, &key_block, session->cipher, |
| 328 | write_iv)) { |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | if (!CBS_copy_bytes(&read_seq, s3->read_sequence, |
| 333 | sizeof(s3->read_sequence)) || |
| 334 | !CBS_copy_bytes(&write_seq, s3->write_sequence, |
| 335 | sizeof(s3->write_sequence))) { |
| 336 | return false; |
| 337 | } |
| 338 | } else { |
| 339 | if (!s3->hs->transcript.Init() || |
| 340 | !s3->hs->transcript.InitHash(ssl_protocol_version(ssl), |
| 341 | s3->hs->new_cipher) || |
| 342 | !s3->hs->transcript.Update(transcript)) { |
| 343 | return false; |
| 344 | } |
| 345 | if ((s3->hs->key_share = SSLKeyShare::Create(&key_share)) == nullptr) { |
| 346 | return false; |
| 347 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Robert Sloan | dc2f609 | 2018-04-10 10:22:33 -0700 | [diff] [blame^] | 350 | return CBS_len(&seq) == 0; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | } // namespace bssl |