Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. |
| 148 | */ |
| 149 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 150 | #include <openssl/ssl.h> |
| 151 | |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame] | 152 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 153 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 155 | #include <utility> |
| 156 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 157 | #include <openssl/aead.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 158 | #include <openssl/bn.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 160 | #include <openssl/bytestring.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 161 | #include <openssl/ec_key.h> |
| 162 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 163 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | #include <openssl/evp.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | #include <openssl/md5.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 166 | #include <openssl/mem.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 167 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 169 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 170 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 173 | namespace bssl { |
| 174 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 175 | static int ssl3_send_client_hello(SSL_HANDSHAKE *hs); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 176 | static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 177 | static int ssl3_get_server_hello(SSL_HANDSHAKE *hs); |
| 178 | static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs); |
| 179 | static int ssl3_get_cert_status(SSL_HANDSHAKE *hs); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 180 | static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs); |
| 181 | static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs); |
| 182 | static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs); |
| 183 | static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs); |
| 184 | static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs); |
| 185 | static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs); |
| 186 | static int ssl3_send_next_proto(SSL_HANDSHAKE *hs); |
| 187 | static int ssl3_send_channel_id(SSL_HANDSHAKE *hs); |
| 188 | static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 189 | |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 190 | int ssl3_connect(SSL_HANDSHAKE *hs) { |
| 191 | SSL *const ssl = hs->ssl; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 192 | int ret = -1; |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 193 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 194 | assert(ssl->handshake_func == ssl3_connect); |
| 195 | assert(!ssl->server); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 196 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 197 | for (;;) { |
David Benjamin | 16b1b1d | 2017-02-10 23:41:14 -0500 | [diff] [blame] | 198 | int state = hs->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 200 | switch (hs->state) { |
David Benjamin | fc0c9d9 | 2016-09-01 01:33:33 -0400 | [diff] [blame] | 201 | case SSL_ST_INIT: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 202 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 203 | hs->state = SSL3_ST_CW_CLNT_HELLO_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 204 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 206 | case SSL3_ST_CW_CLNT_HELLO_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 207 | ret = ssl3_send_client_hello(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 208 | if (ret <= 0) { |
| 209 | goto end; |
| 210 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 211 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 212 | if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 213 | if (hs->early_data_offered) { |
| 214 | if (!tls13_init_early_key_schedule(hs) || |
| 215 | !tls13_advance_key_schedule(hs, ssl->session->master_key, |
| 216 | ssl->session->master_key_length) || |
| 217 | !tls13_derive_early_secrets(hs) || |
| 218 | !tls13_set_traffic_key(ssl, evp_aead_seal, |
| 219 | hs->early_traffic_secret, |
| 220 | hs->hash_len)) { |
| 221 | ret = -1; |
| 222 | goto end; |
| 223 | } |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 224 | hs->next_state = SSL3_ST_WRITE_EARLY_DATA; |
| 225 | } else { |
| 226 | hs->next_state = SSL3_ST_CR_SRVR_HELLO_A; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 227 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 228 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 229 | hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 230 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 231 | hs->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 232 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 234 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 235 | assert(SSL_is_dtls(ssl)); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 236 | ret = dtls1_get_hello_verify_request(hs); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 237 | if (ret <= 0) { |
| 238 | goto end; |
| 239 | } |
| 240 | if (ssl->d1->send_cookie) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 241 | hs->state = SSL3_ST_CW_CLNT_HELLO_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 242 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 243 | hs->state = SSL3_ST_CR_SRVR_HELLO_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 244 | } |
| 245 | break; |
| 246 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 247 | case SSL3_ST_WRITE_EARLY_DATA: |
| 248 | /* Stash the early data session, so connection properties may be queried |
| 249 | * out of it. */ |
| 250 | hs->in_early_data = 1; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 251 | SSL_SESSION_up_ref(ssl->session); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 252 | hs->early_session.reset(ssl->session); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 253 | |
| 254 | hs->state = SSL3_ST_CR_SRVR_HELLO_A; |
| 255 | hs->can_early_write = 1; |
| 256 | ret = 1; |
| 257 | goto end; |
| 258 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 259 | case SSL3_ST_CR_SRVR_HELLO_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 260 | ret = ssl3_get_server_hello(hs); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 261 | if (hs->state == SSL_ST_TLS13) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 262 | break; |
| 263 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 264 | if (ret <= 0) { |
| 265 | goto end; |
| 266 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 268 | if (ssl->session != NULL) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 269 | hs->state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 270 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 271 | hs->state = SSL3_ST_CR_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 272 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 273 | break; |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 274 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 275 | case SSL3_ST_CR_CERT_A: |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 276 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 277 | ret = ssl3_get_server_certificate(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 278 | if (ret <= 0) { |
| 279 | goto end; |
| 280 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 281 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 282 | hs->state = SSL3_ST_CR_CERT_STATUS_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 283 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 285 | case SSL3_ST_CR_CERT_STATUS_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 286 | if (hs->certificate_status_expected) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 287 | ret = ssl3_get_cert_status(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 288 | if (ret <= 0) { |
| 289 | goto end; |
| 290 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 291 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 292 | hs->state = SSL3_ST_VERIFY_SERVER_CERT; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 293 | break; |
| 294 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 295 | case SSL3_ST_VERIFY_SERVER_CERT: |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 296 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 297 | switch (ssl_verify_peer_cert(hs)) { |
| 298 | case ssl_verify_ok: |
| 299 | break; |
| 300 | case ssl_verify_invalid: |
| 301 | ret = -1; |
| 302 | goto end; |
| 303 | case ssl_verify_retry: |
| 304 | ssl->rwstate = SSL_CERTIFICATE_VERIFY; |
| 305 | ret = -1; |
| 306 | goto end; |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 307 | } |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 308 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 309 | hs->state = SSL3_ST_CR_KEY_EXCH_A; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 310 | break; |
| 311 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 312 | case SSL3_ST_CR_KEY_EXCH_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 313 | ret = ssl3_get_server_key_exchange(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 314 | if (ret <= 0) { |
| 315 | goto end; |
| 316 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 317 | hs->state = SSL3_ST_CR_CERT_REQ_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 318 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 320 | case SSL3_ST_CR_CERT_REQ_A: |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 321 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 322 | ret = ssl3_get_certificate_request(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 323 | if (ret <= 0) { |
| 324 | goto end; |
| 325 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 326 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 327 | hs->state = SSL3_ST_CR_SRVR_DONE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 328 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 330 | case SSL3_ST_CR_SRVR_DONE_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 331 | ret = ssl3_get_server_hello_done(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 332 | if (ret <= 0) { |
| 333 | goto end; |
| 334 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 335 | hs->state = SSL3_ST_CW_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 336 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 338 | case SSL3_ST_CW_CERT_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 339 | if (hs->cert_request) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 340 | ret = ssl3_send_client_certificate(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 341 | if (ret <= 0) { |
| 342 | goto end; |
| 343 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 344 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 345 | hs->state = SSL3_ST_CW_KEY_EXCH_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 346 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 348 | case SSL3_ST_CW_KEY_EXCH_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 349 | ret = ssl3_send_client_key_exchange(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 350 | if (ret <= 0) { |
| 351 | goto end; |
| 352 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 353 | hs->state = SSL3_ST_CW_CERT_VRFY_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 354 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 356 | case SSL3_ST_CW_CERT_VRFY_A: |
David Benjamin | a660e7a | 2017-01-02 08:15:35 -0500 | [diff] [blame] | 357 | if (hs->cert_request && ssl_has_certificate(ssl)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 358 | ret = ssl3_send_cert_verify(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 359 | if (ret <= 0) { |
| 360 | goto end; |
| 361 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 362 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 363 | hs->state = SSL3_ST_CW_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 364 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 365 | |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 366 | case SSL3_ST_CW_CHANGE: |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 367 | if (!ssl->method->add_change_cipher_spec(ssl) || |
| 368 | !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 369 | ret = -1; |
| 370 | goto end; |
| 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 373 | hs->state = SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 374 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 376 | case SSL3_ST_CW_NEXT_PROTO_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 377 | if (hs->next_proto_neg_seen) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 378 | ret = ssl3_send_next_proto(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 379 | if (ret <= 0) { |
| 380 | goto end; |
| 381 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 382 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 383 | hs->state = SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 384 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 386 | case SSL3_ST_CW_CHANNEL_ID_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 387 | if (ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 388 | ret = ssl3_send_channel_id(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 389 | if (ret <= 0) { |
| 390 | goto end; |
| 391 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 392 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 393 | hs->state = SSL3_ST_CW_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 394 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 395 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 396 | case SSL3_ST_CW_FINISHED_A: |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 397 | ret = ssl3_send_finished(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 398 | if (ret <= 0) { |
| 399 | goto end; |
| 400 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 401 | hs->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 403 | if (ssl->session != NULL) { |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 404 | hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 405 | } else { |
| 406 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 407 | * record the handshake hashes at this point in the session so that |
| 408 | * any resumption of this session with ChannelID can sign those |
| 409 | * hashes. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 410 | ret = tls1_record_handshake_hashes_for_channel_id(hs); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 411 | if (ret <= 0) { |
| 412 | goto end; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 413 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 414 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 415 | ssl3_can_false_start(ssl) && |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 416 | /* No False Start on renegotiation (would complicate the state |
| 417 | * machine). */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 418 | !ssl->s3->initial_handshake_complete) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 419 | hs->next_state = SSL3_ST_FALSE_START; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 420 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 421 | hs->next_state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 424 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 426 | case SSL3_ST_FALSE_START: |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 427 | hs->state = SSL3_ST_CR_SESSION_TICKET_A; |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 428 | hs->in_false_start = 1; |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 429 | hs->can_early_write = 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 430 | ret = 1; |
| 431 | goto end; |
| 432 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 433 | case SSL3_ST_CR_SESSION_TICKET_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 434 | if (hs->ticket_expected) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 435 | ret = ssl3_get_new_session_ticket(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 436 | if (ret <= 0) { |
| 437 | goto end; |
| 438 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 439 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 440 | hs->state = SSL3_ST_CR_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 441 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 443 | case SSL3_ST_CR_CHANGE: |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 444 | ret = ssl->method->read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 445 | if (ret <= 0) { |
| 446 | goto end; |
| 447 | } |
| 448 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 449 | if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 450 | ret = -1; |
| 451 | goto end; |
| 452 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 453 | hs->state = SSL3_ST_CR_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 454 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 456 | case SSL3_ST_CR_FINISHED_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 457 | ret = ssl3_get_finished(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 458 | if (ret <= 0) { |
| 459 | goto end; |
| 460 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 462 | if (ssl->session != NULL) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 463 | hs->state = SSL3_ST_CW_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 464 | } else { |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 465 | hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 466 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 467 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 468 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 469 | case SSL3_ST_CW_FLUSH: |
David Benjamin | 8d5f9da | 2017-01-01 17:41:30 -0500 | [diff] [blame] | 470 | ret = ssl->method->flush_flight(ssl); |
| 471 | if (ret <= 0) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 472 | goto end; |
| 473 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 474 | hs->state = hs->next_state; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 475 | break; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 477 | case SSL_ST_TLS13: { |
| 478 | int early_return = 0; |
| 479 | ret = tls13_handshake(hs, &early_return); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 480 | if (ret <= 0) { |
| 481 | goto end; |
| 482 | } |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 483 | |
| 484 | if (early_return) { |
| 485 | ret = 1; |
| 486 | goto end; |
| 487 | } |
| 488 | |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 489 | hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 490 | break; |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 491 | } |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 492 | |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 493 | case SSL3_ST_FINISH_CLIENT_HANDSHAKE: |
David Benjamin | 9bbdf58 | 2017-08-02 19:46:29 -0400 | [diff] [blame] | 494 | ssl->method->on_handshake_complete(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 496 | SSL_SESSION_free(ssl->s3->established_session); |
| 497 | if (ssl->session != NULL) { |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 498 | SSL_SESSION_up_ref(ssl->session); |
| 499 | ssl->s3->established_session = ssl->session; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 500 | } else { |
| 501 | /* We make a copy of the session in order to maintain the immutability |
| 502 | * of the new established_session due to False Start. The caller may |
| 503 | * have taken a reference to the temporary session. */ |
| 504 | ssl->s3->established_session = |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 505 | SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL) |
| 506 | .release(); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 507 | if (ssl->s3->established_session == NULL) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 508 | ret = -1; |
| 509 | goto end; |
| 510 | } |
David Benjamin | 5ef40c6 | 2017-08-23 21:28:29 -0700 | [diff] [blame] | 511 | /* Renegotiations do not participate in session resumption. */ |
| 512 | if (!ssl->s3->initial_handshake_complete) { |
| 513 | ssl->s3->established_session->not_resumable = 0; |
| 514 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 515 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 516 | hs->new_session.reset(); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 517 | } |
| 518 | |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 519 | hs->state = SSL_ST_OK; |
| 520 | break; |
| 521 | |
| 522 | case SSL_ST_OK: { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 523 | ssl->s3->initial_handshake_complete = 1; |
David Benjamin | 5ef40c6 | 2017-08-23 21:28:29 -0700 | [diff] [blame] | 524 | ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 526 | ret = 1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 527 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 528 | goto end; |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 529 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 530 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 531 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 532 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 533 | ret = -1; |
| 534 | goto end; |
| 535 | } |
| 536 | |
David Benjamin | 16b1b1d | 2017-02-10 23:41:14 -0500 | [diff] [blame] | 537 | if (hs->state != state) { |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 538 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 539 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 542 | end: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 543 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 544 | return ret; |
| 545 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 546 | |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 547 | /* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of |
| 548 | * disabled algorithms. */ |
| 549 | static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a, |
| 550 | uint32_t *out_mask_k) { |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 551 | *out_mask_a = 0; |
| 552 | *out_mask_k = 0; |
| 553 | |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 554 | /* PSK requires a client callback. */ |
| 555 | if (ssl->psk_client_callback == NULL) { |
| 556 | *out_mask_a |= SSL_aPSK; |
| 557 | *out_mask_k |= SSL_kPSK; |
| 558 | } |
| 559 | } |
| 560 | |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 561 | static int ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) { |
| 562 | SSL *const ssl = hs->ssl; |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 563 | uint32_t mask_a, mask_k; |
| 564 | ssl_get_client_disabled(ssl, &mask_a, &mask_k); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 565 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 566 | CBB child; |
| 567 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 568 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 569 | } |
| 570 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 571 | /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */ |
| 572 | if (ssl->ctx->grease_enabled && |
| 573 | !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) { |
| 574 | return 0; |
| 575 | } |
| 576 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 577 | /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on |
| 578 | * hardware support. */ |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 579 | if (hs->max_version >= TLS1_3_VERSION) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 580 | if (!EVP_has_aes_hardware() && |
| 581 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
| 582 | return 0; |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 583 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 584 | if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) || |
| 585 | !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) { |
| 586 | return 0; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 587 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 588 | if (EVP_has_aes_hardware() && |
| 589 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | } |
| 593 | |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 594 | if (hs->min_version < TLS1_3_VERSION) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 595 | int any_enabled = 0; |
David Benjamin | ee910bf | 2017-07-25 22:36:00 -0400 | [diff] [blame] | 596 | for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 597 | /* Skip disabled ciphers */ |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 598 | if ((cipher->algorithm_mkey & mask_k) || |
| 599 | (cipher->algorithm_auth & mask_a)) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 600 | continue; |
| 601 | } |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 602 | if (SSL_CIPHER_get_min_version(cipher) > hs->max_version || |
| 603 | SSL_CIPHER_get_max_version(cipher) < hs->min_version) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 604 | continue; |
| 605 | } |
| 606 | any_enabled = 1; |
| 607 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 608 | return 0; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* If all ciphers were disabled, return the error to the caller. */ |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 613 | if (!any_enabled && hs->max_version < TLS1_3_VERSION) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 614 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 615 | return 0; |
| 616 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 620 | * added. */ |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 621 | if (hs->max_version == SSL3_VERSION && |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 622 | !ssl->s3->initial_handshake_complete) { |
| 623 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 624 | return 0; |
| 625 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 626 | } |
| 627 | |
David Benjamin | e0ff767 | 2016-09-19 18:40:03 -0400 | [diff] [blame] | 628 | if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
David Benjamin | 5e7e7cc | 2016-07-21 12:55:28 +0200 | [diff] [blame] | 629 | if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 630 | return 0; |
| 631 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | return CBB_flush(out); |
| 635 | } |
| 636 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 637 | int ssl_write_client_hello(SSL_HANDSHAKE *hs) { |
| 638 | SSL *const ssl = hs->ssl; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 639 | ScopedCBB cbb; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 640 | CBB body; |
| 641 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) { |
| 642 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 643 | } |
| 644 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 645 | /* Renegotiations do not participate in session resumption. */ |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 646 | int has_session_id = ssl->session != NULL && |
| 647 | !ssl->s3->initial_handshake_complete && |
| 648 | ssl->session->session_id_length > 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 649 | |
| 650 | CBB child; |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 651 | if (!CBB_add_u16(&body, hs->client_version) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 652 | !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 653 | !CBB_add_u8_length_prefixed(&body, &child)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 654 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 657 | if (has_session_id) { |
| 658 | if (!CBB_add_bytes(&child, ssl->session->session_id, |
| 659 | ssl->session->session_id_length)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 660 | return 0; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 661 | } |
| 662 | } else { |
| 663 | /* In TLS 1.3 experimental encodings, send a fake placeholder session ID |
| 664 | * when we do not otherwise have one to send. */ |
| 665 | if (hs->max_version >= TLS1_3_VERSION && |
Steven Valdez | dbe0158 | 2017-07-14 10:39:28 -0400 | [diff] [blame] | 666 | ssl->tls13_variant == tls13_experiment && |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 667 | !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 668 | return 0; |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 672 | if (SSL_is_dtls(ssl)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 673 | if (!CBB_add_u8_length_prefixed(&body, &child) || |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 674 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 675 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | size_t header_len = |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 680 | SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 681 | if (!ssl_write_client_cipher_list(hs, &body) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 682 | !CBB_add_u8(&body, 1 /* one compression method */) || |
| 683 | !CBB_add_u8(&body, 0 /* null compression */) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 684 | !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 685 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 688 | uint8_t *msg = NULL; |
| 689 | size_t len; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 690 | if (!ssl->method->finish_message(ssl, cbb.get(), &msg, &len)) { |
| 691 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* Now that the length prefixes have been computed, fill in the placeholder |
| 695 | * PSK binder. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 696 | if (hs->needs_psk_binder && |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 697 | !tls13_write_psk_binder(hs, msg, len)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 698 | OPENSSL_free(msg); |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 699 | return 0; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 700 | } |
| 701 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 702 | return ssl->method->add_message(ssl, msg, len); |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 703 | } |
| 704 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 705 | static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) { |
| 706 | SSL *const ssl = hs->ssl; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 707 | /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we |
| 708 | * may send multiple ClientHellos if we receive HelloVerifyRequest. */ |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 709 | if (!hs->transcript.Init()) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 710 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 711 | return -1; |
| 712 | } |
| 713 | |
David Benjamin | 68161cb | 2017-06-20 14:49:43 -0400 | [diff] [blame] | 714 | /* Freeze the version range. */ |
| 715 | if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 716 | return -1; |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 717 | } |
| 718 | |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 719 | /* Always advertise the ClientHello version from the original maximum version, |
| 720 | * even on renegotiation. The static RSA key exchange uses this field, and |
| 721 | * some servers fail when it changes across handshakes. */ |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 722 | if (SSL_is_dtls(hs->ssl)) { |
| 723 | hs->client_version = |
| 724 | hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION; |
| 725 | } else { |
| 726 | hs->client_version = |
| 727 | hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 728 | } |
| 729 | |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 730 | /* If the configured session has expired or was created at a disabled |
| 731 | * version, drop it. */ |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 732 | if (ssl->session != NULL) { |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 733 | if (ssl->session->is_server || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 734 | !ssl_supports_version(hs, ssl->session->ssl_version) || |
| 735 | (ssl->session->session_id_length == 0 && |
| 736 | ssl->session->tlsext_ticklen == 0) || |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 737 | ssl->session->not_resumable || |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 738 | !ssl_session_is_time_valid(ssl, ssl->session)) { |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 739 | ssl_set_session(ssl, NULL); |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 740 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 744 | * renegerate the client_random. The random must be reused. */ |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 745 | if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) && |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 746 | !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 747 | return -1; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 748 | } |
| 749 | |
Steven Valdez | 0e4a448 | 2017-07-17 11:12:34 -0400 | [diff] [blame] | 750 | /* Initialize a random session ID for the experimental TLS 1.3 variant |
| 751 | * requiring a session id. */ |
Steven Valdez | dbe0158 | 2017-07-14 10:39:28 -0400 | [diff] [blame] | 752 | if (ssl->tls13_variant == tls13_experiment) { |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 753 | hs->session_id_len = sizeof(hs->session_id); |
| 754 | if (!RAND_bytes(hs->session_id, hs->session_id_len)) { |
| 755 | return -1; |
| 756 | } |
| 757 | } |
| 758 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 759 | if (!ssl_write_client_hello(hs)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 760 | return -1; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 761 | } |
| 762 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 763 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 764 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 765 | |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 766 | static int dtls1_get_hello_verify_request(SSL_HANDSHAKE *hs) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 767 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 768 | SSLMessage msg; |
| 769 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 770 | if (ret <= 0) { |
| 771 | return ret; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 772 | } |
| 773 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 774 | if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 775 | ssl->d1->send_cookie = false; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 776 | return 1; |
| 777 | } |
| 778 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 779 | CBS hello_verify_request = msg.body, cookie; |
| 780 | uint16_t server_version; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 781 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 782 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 783 | CBS_len(&cookie) > sizeof(ssl->d1->cookie) || |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 784 | CBS_len(&hello_verify_request) != 0) { |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 785 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 786 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 787 | return -1; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 788 | } |
| 789 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 790 | OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 791 | ssl->d1->cookie_len = CBS_len(&cookie); |
| 792 | |
David Benjamin | b0c761e | 2017-06-25 22:42:55 -0400 | [diff] [blame] | 793 | ssl->d1->send_cookie = true; |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 794 | ssl->method->next_message(ssl); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 795 | return 1; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 796 | } |
| 797 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 798 | static int parse_server_version(SSL_HANDSHAKE *hs, uint16_t *out, |
| 799 | const SSLMessage &msg) { |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 800 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 801 | if (msg.type != SSL3_MT_SERVER_HELLO && |
| 802 | msg.type != SSL3_MT_HELLO_RETRY_REQUEST) { |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 803 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 804 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 805 | return 0; |
| 806 | } |
| 807 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 808 | CBS server_hello = msg.body; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 809 | if (!CBS_get_u16(&server_hello, out)) { |
| 810 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 811 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /* The server version may also be in the supported_versions extension if |
| 816 | * applicable. */ |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 817 | if (msg.type != SSL3_MT_SERVER_HELLO || *out != TLS1_2_VERSION) { |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 818 | return 1; |
| 819 | } |
| 820 | |
| 821 | uint8_t sid_length; |
| 822 | if (!CBS_skip(&server_hello, SSL3_RANDOM_SIZE) || |
| 823 | !CBS_get_u8(&server_hello, &sid_length) || |
| 824 | !CBS_skip(&server_hello, sid_length + 2 /* cipher_suite */ + |
| 825 | 1 /* compression_method */)) { |
| 826 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 827 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | /* The extensions block may not be present. */ |
| 832 | if (CBS_len(&server_hello) == 0) { |
| 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | CBS extensions; |
| 837 | if (!CBS_get_u16_length_prefixed(&server_hello, &extensions) || |
| 838 | CBS_len(&server_hello) != 0) { |
| 839 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 840 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | int have_supported_versions; |
| 845 | CBS supported_versions; |
| 846 | const SSL_EXTENSION_TYPE ext_types[] = { |
| 847 | {TLSEXT_TYPE_supported_versions, &have_supported_versions, |
| 848 | &supported_versions}, |
| 849 | }; |
| 850 | |
| 851 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 852 | if (!ssl_parse_extensions(&extensions, &alert, ext_types, |
| 853 | OPENSSL_ARRAY_SIZE(ext_types), |
| 854 | 1 /* ignore unknown */)) { |
| 855 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | if (have_supported_versions && |
| 860 | (!CBS_get_u16(&supported_versions, out) || |
| 861 | CBS_len(&supported_versions) != 0)) { |
| 862 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | return 1; |
| 867 | } |
| 868 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 869 | static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) { |
| 870 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 871 | SSLMessage msg; |
| 872 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 873 | if (ret <= 0) { |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 874 | uint32_t err = ERR_peek_error(); |
| 875 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 876 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 877 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 878 | * in response to ClientHello. This matches NSS's client behavior and |
| 879 | * gives a better error on a (probable) failure to negotiate initial |
| 880 | * parameters. Note: this error code comes after the original one. |
| 881 | * |
| 882 | * See https://crbug.com/446505. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 883 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 884 | } |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 885 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 886 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 887 | |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 888 | uint16_t server_version; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 889 | if (!parse_server_version(hs, &server_version, msg)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 890 | return -1; |
| 891 | } |
| 892 | |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 893 | if (!ssl_supports_version(hs, server_version)) { |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 894 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 895 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION); |
| 896 | return -1; |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 897 | } |
| 898 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 899 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 900 | if (!ssl->s3->have_version) { |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 901 | ssl->version = server_version; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 902 | /* At this point, the connection's version is known and ssl->version is |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 903 | * fixed. Begin enforcing the record-layer version. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 904 | ssl->s3->have_version = 1; |
Steven Valdez | 8f36c51 | 2017-06-20 10:55:02 -0400 | [diff] [blame] | 905 | } else if (server_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 906 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 907 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION); |
| 908 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 909 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 911 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 912 | hs->state = SSL_ST_TLS13; |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 913 | hs->do_tls13_handshake = tls13_client_handshake; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 914 | return 1; |
| 915 | } |
| 916 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 917 | if (hs->early_data_offered) { |
| 918 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 919 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION); |
| 920 | return -1; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 921 | } |
| 922 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 923 | ssl_clear_tls13_state(hs); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 924 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 925 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 926 | return -1; |
| 927 | } |
| 928 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 929 | CBS server_hello = msg.body, server_random, session_id; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 930 | uint16_t cipher_suite; |
| 931 | uint8_t compression_method; |
David Benjamin | b853f31 | 2017-07-14 18:40:34 -0400 | [diff] [blame] | 932 | if (!CBS_skip(&server_hello, 2 /* version */) || |
| 933 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 934 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 935 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 936 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 937 | !CBS_get_u8(&server_hello, &compression_method)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 938 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 939 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 940 | return -1; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 941 | } |
| 942 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 943 | /* Copy over the server random. */ |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 944 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), |
| 945 | SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 946 | |
David Benjamin | 5510863 | 2016-08-11 22:01:18 -0400 | [diff] [blame] | 947 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 948 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 949 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 950 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 951 | ssl->session->session_id_length != 0 && |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 952 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 953 | ssl->session->session_id_length)) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 954 | ssl->s3->session_reused = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 955 | } else { |
| 956 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 957 | * fill out. */ |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 958 | ssl_set_session(ssl, NULL); |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 959 | if (!ssl_get_new_session(hs, 0 /* client */)) { |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 960 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 961 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 962 | } |
| 963 | /* Note: session_id could be empty. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 964 | hs->new_session->session_id_length = CBS_len(&session_id); |
| 965 | OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id), |
| 966 | CBS_len(&session_id)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 967 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 968 | |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 969 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite); |
| 970 | if (cipher == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 971 | /* unknown cipher */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 972 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 973 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 974 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 975 | } |
David Benjamin | d2cb1c1 | 2016-11-02 17:49:09 -0400 | [diff] [blame] | 976 | |
| 977 | /* The cipher must be allowed in the selected version and enabled. */ |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 978 | uint32_t mask_a, mask_k; |
| 979 | ssl_get_client_disabled(ssl, &mask_a, &mask_k); |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 980 | if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) || |
| 981 | SSL_CIPHER_get_min_version(cipher) > ssl3_protocol_version(ssl) || |
| 982 | SSL_CIPHER_get_max_version(cipher) < ssl3_protocol_version(ssl) || |
| 983 | !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 984 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 985 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 986 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 987 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 988 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 989 | if (ssl->session != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 990 | if (ssl->session->ssl_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 991 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 992 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 993 | return -1; |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 994 | } |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 995 | if (ssl->session->cipher != cipher) { |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 996 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 997 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 998 | return -1; |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 999 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1000 | if (!ssl_session_is_context_valid(ssl, ssl->session)) { |
| 1001 | /* This is actually a client application bug. */ |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1002 | OPENSSL_PUT_ERROR(SSL, |
| 1003 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 1004 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1005 | return -1; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1006 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 1007 | } else { |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 1008 | hs->new_session->cipher = cipher; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1009 | } |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 1010 | hs->new_cipher = cipher; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 1011 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1012 | /* Now that the cipher is known, initialize the handshake hash and hash the |
| 1013 | * ServerHello. */ |
David Benjamin | ca9e8f5 | 2017-08-09 15:02:34 -0400 | [diff] [blame] | 1014 | if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1015 | !ssl_hash_message(hs, msg)) { |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 1016 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1017 | return -1; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1020 | /* If doing a full handshake, the server may request a client certificate |
| 1021 | * which requires hashing the handshake transcript. Otherwise, the handshake |
| 1022 | * buffer may be released. */ |
David Benjamin | 3d458dc | 2016-09-12 22:40:27 +0000 | [diff] [blame] | 1023 | if (ssl->session != NULL || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1024 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1025 | hs->transcript.FreeBuffer(); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1026 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 1027 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1028 | /* Only the NULL compression algorithm is supported. */ |
| 1029 | if (compression_method != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1030 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 1031 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1032 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1033 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1034 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1035 | /* TLS extensions */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1036 | if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1037 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 1038 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1039 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1041 | /* There should be nothing left over in the record. */ |
| 1042 | if (CBS_len(&server_hello) != 0) { |
| 1043 | /* wrong packet length */ |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1044 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 1045 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1046 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1047 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1048 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1049 | if (ssl->session != NULL && |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1050 | hs->extended_master_secret != ssl->session->extended_master_secret) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1051 | if (ssl->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1052 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 1053 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1054 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 1055 | } |
David Benjamin | d3bca04 | 2017-04-08 11:05:04 -0400 | [diff] [blame] | 1056 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1057 | return -1; |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1060 | ssl->method->next_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1061 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1062 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1063 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1064 | static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) { |
| 1065 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1066 | SSLMessage msg; |
| 1067 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1068 | if (ret <= 0) { |
| 1069 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1070 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1071 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1072 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) || |
| 1073 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1074 | return -1; |
| 1075 | } |
| 1076 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1077 | CBS body = msg.body; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1078 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 1079 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1080 | if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey, NULL, &body, |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 1081 | ssl->ctx->pool)) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1082 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1083 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1084 | } |
David Benjamin | e664a53 | 2017-07-20 20:19:36 -0400 | [diff] [blame] | 1085 | sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free); |
| 1086 | hs->new_session->certs = chain.release(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1087 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1088 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 || |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1089 | CBS_len(&body) != 0 || |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1090 | !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1091 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1092 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1093 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1094 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1095 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 1096 | if (!ssl_check_leaf_certificate( |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1097 | hs, hs->peer_pubkey.get(), |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1098 | sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1099 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1100 | return -1; |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1103 | ssl->method->next_message(ssl); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1104 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1107 | static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) { |
| 1108 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1109 | SSLMessage msg; |
| 1110 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1111 | if (ret <= 0) { |
| 1112 | return ret; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1113 | } |
| 1114 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1115 | if (msg.type != SSL3_MT_CERTIFICATE_STATUS) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1116 | /* A server may send status_request in ServerHello and then change |
| 1117 | * its mind about sending CertificateStatus. */ |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1118 | return 1; |
| 1119 | } |
| 1120 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1121 | if (!ssl_hash_message(hs, msg)) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1122 | return -1; |
| 1123 | } |
| 1124 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1125 | CBS certificate_status = msg.body, ocsp_response; |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1126 | uint8_t status_type; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1127 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1128 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1129 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1130 | CBS_len(&ocsp_response) == 0 || |
| 1131 | CBS_len(&certificate_status) != 0) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1132 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1133 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1134 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1137 | CRYPTO_BUFFER_free(hs->new_session->ocsp_response); |
| 1138 | hs->new_session->ocsp_response = |
| 1139 | CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool); |
| 1140 | if (hs->new_session->ocsp_response == nullptr) { |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1141 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1142 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1143 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1144 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1145 | ssl->method->next_message(ssl); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1146 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1147 | } |
| 1148 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1149 | static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) { |
| 1150 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1151 | SSLMessage msg; |
| 1152 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1153 | if (ret <= 0) { |
| 1154 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1157 | if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
David Benjamin | bac75b8 | 2016-09-16 19:34:02 -0400 | [diff] [blame] | 1158 | /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1159 | if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1160 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1161 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1162 | return -1; |
| 1163 | } |
| 1164 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1165 | return 1; |
| 1166 | } |
| 1167 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1168 | if (!ssl_hash_message(hs, msg)) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1169 | return -1; |
| 1170 | } |
| 1171 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1172 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1173 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1174 | CBS server_key_exchange = msg.body; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1175 | if (alg_a & SSL_aPSK) { |
| 1176 | CBS psk_identity_hint; |
| 1177 | |
| 1178 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1179 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1180 | &psk_identity_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1181 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1182 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1183 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /* Store PSK identity hint for later use, hint is used in |
| 1187 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1188 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1189 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1190 | * |
| 1191 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1192 | * a specific identity. */ |
| 1193 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1194 | CBS_contains_zero_byte(&psk_identity_hint)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1195 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1196 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1197 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 1200 | /* Save non-empty identity hints as a C string. Empty identity hints we |
| 1201 | * treat as missing. Plain PSK makes it possible to send either no hint |
| 1202 | * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell |
| 1203 | * empty hint. Having different capabilities is odd, so we interpret empty |
| 1204 | * and missing as identical. */ |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1205 | char *raw = nullptr; |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 1206 | if (CBS_len(&psk_identity_hint) != 0 && |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1207 | !CBS_strdup(&psk_identity_hint, &raw)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1208 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1209 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1210 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1211 | } |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1212 | hs->peer_psk_identity_hint.reset(raw); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1215 | if (alg_k & SSL_kECDHE) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1216 | /* Parse the server parameters. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1217 | uint8_t group_type; |
| 1218 | uint16_t group_id; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1219 | CBS point; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1220 | if (!CBS_get_u8(&server_key_exchange, &group_type) || |
| 1221 | group_type != NAMED_CURVE_TYPE || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1222 | !CBS_get_u16(&server_key_exchange, &group_id) || |
| 1223 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1224 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1225 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1226 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1227 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1228 | hs->new_session->group_id = group_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1229 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1230 | /* Ensure the group is consistent with preferences. */ |
| 1231 | if (!tls1_check_group_id(ssl, group_id)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1232 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1233 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1234 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1235 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1236 | |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1237 | /* Initialize ECDH and save the peer public key for later. */ |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1238 | hs->key_share = SSLKeyShare::Create(group_id); |
| 1239 | if (!hs->key_share || |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1240 | !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1241 | return -1; |
David Benjamin | c1cc858 | 2015-12-17 01:22:15 -0500 | [diff] [blame] | 1242 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1243 | } else if (!(alg_k & SSL_kPSK)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1244 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1245 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1246 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | /* At this point, |server_key_exchange| contains the signature, if any, while |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1250 | * |msg.body| contains the entire message. From that, derive a CBS containing |
| 1251 | * just the parameter. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1252 | CBS parameter; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1253 | CBS_init(¶meter, CBS_data(&msg.body), |
| 1254 | CBS_len(&msg.body) - CBS_len(&server_key_exchange)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1255 | |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1256 | /* ServerKeyExchange should be signed by the server's public key. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1257 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1258 | uint16_t signature_algorithm = 0; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1259 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1260 | if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1261 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1262 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1263 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1264 | } |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1265 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1266 | if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) { |
| 1267 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1268 | return -1; |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1269 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1270 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 1271 | } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1272 | hs->peer_pubkey.get())) { |
David Benjamin | 49ec9bb | 2016-07-14 00:11:26 -0400 | [diff] [blame] | 1273 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1274 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1275 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | /* The last field in |server_key_exchange| is the signature. */ |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1279 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1280 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1281 | CBS_len(&server_key_exchange) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1282 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1283 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1284 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1287 | ScopedCBB transcript; |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1288 | uint8_t *transcript_data; |
| 1289 | size_t transcript_len; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1290 | if (!CBB_init(transcript.get(), |
| 1291 | 2 * SSL3_RANDOM_SIZE + CBS_len(¶meter)) || |
| 1292 | !CBB_add_bytes(transcript.get(), ssl->s3->client_random, |
| 1293 | SSL3_RANDOM_SIZE) || |
| 1294 | !CBB_add_bytes(transcript.get(), ssl->s3->server_random, |
| 1295 | SSL3_RANDOM_SIZE) || |
| 1296 | !CBB_add_bytes(transcript.get(), CBS_data(¶meter), |
| 1297 | CBS_len(¶meter)) || |
| 1298 | !CBB_finish(transcript.get(), &transcript_data, &transcript_len)) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1299 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1300 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1301 | return -1; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1302 | } |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1303 | |
| 1304 | int sig_ok = ssl_public_key_verify( |
| 1305 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1306 | hs->peer_pubkey.get(), transcript_data, transcript_len); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1307 | OPENSSL_free(transcript_data); |
| 1308 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1309 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1310 | sig_ok = 1; |
| 1311 | ERR_clear_error(); |
| 1312 | #endif |
| 1313 | if (!sig_ok) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1314 | /* bad signature */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1315 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1316 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1317 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1318 | } |
| 1319 | } else { |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1320 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1321 | assert(alg_a == SSL_aPSK); |
| 1322 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1323 | if (CBS_len(&server_key_exchange) > 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1324 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
David Benjamin | 8d606e3 | 2017-06-15 22:43:04 -0400 | [diff] [blame] | 1325 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1326 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1327 | } |
| 1328 | } |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1329 | |
| 1330 | ssl->method->next_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1331 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1332 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1333 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1334 | static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) { |
| 1335 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1336 | SSLMessage msg; |
| 1337 | int ret = ssl_read_message(ssl, &msg); |
| 1338 | if (ret <= 0) { |
| 1339 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1340 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1341 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1342 | if (msg.type == SSL3_MT_SERVER_HELLO_DONE) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1343 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1344 | * client auth. */ |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1345 | hs->transcript.FreeBuffer(); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1346 | return 1; |
| 1347 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1348 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1349 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) || |
| 1350 | !ssl_hash_message(hs, msg)) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1351 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1352 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1353 | |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1354 | /* Get the certificate types. */ |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1355 | CBS body = msg.body, certificate_types; |
| 1356 | if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1357 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1358 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1359 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1360 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1361 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1362 | if (!CBS_stow(&certificate_types, &hs->certificate_types, |
| 1363 | &hs->num_certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1364 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1365 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1366 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1367 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1368 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1369 | CBS supported_signature_algorithms; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1370 | if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1371 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1372 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1373 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1374 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1375 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1376 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1377 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1378 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1379 | UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names = |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1380 | ssl_parse_client_CA_list(ssl, &alert, &body); |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1381 | if (!ca_names) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1382 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1383 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1384 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1385 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1386 | if (CBS_len(&body) != 0) { |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1387 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1388 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1389 | return -1; |
| 1390 | } |
| 1391 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1392 | hs->cert_request = 1; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1393 | hs->ca_names = std::move(ca_names); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 1394 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1395 | ssl->method->next_message(ssl); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1396 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1397 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1398 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1399 | static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) { |
| 1400 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1401 | SSLMessage msg; |
| 1402 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1403 | if (ret <= 0) { |
| 1404 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1405 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1406 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1407 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) || |
| 1408 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1409 | return -1; |
| 1410 | } |
| 1411 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1412 | /* ServerHelloDone is empty. */ |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1413 | if (CBS_len(&msg.body) != 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1414 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1415 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1416 | return -1; |
| 1417 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1418 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1419 | ssl->method->next_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1420 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1421 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1422 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1423 | static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) { |
| 1424 | SSL *const ssl = hs->ssl; |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1425 | /* Call cert_cb to update the certificate. */ |
| 1426 | if (ssl->cert->cert_cb) { |
| 1427 | int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1428 | if (ret < 0) { |
| 1429 | ssl->rwstate = SSL_X509_LOOKUP; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1430 | return -1; |
| 1431 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1432 | if (ret == 0) { |
| 1433 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 1434 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1435 | return -1; |
| 1436 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1439 | if (!ssl_has_certificate(ssl)) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1440 | /* Without a client certificate, the handshake buffer may be released. */ |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1441 | hs->transcript.FreeBuffer(); |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1442 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1443 | /* In SSL 3.0, the Certificate message is replaced with a warning alert. */ |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1444 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1445 | if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING, |
| 1446 | SSL_AD_NO_CERTIFICATE)) { |
| 1447 | return -1; |
| 1448 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1449 | return 1; |
| 1450 | } |
| 1451 | } |
| 1452 | |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 1453 | if (!ssl_on_certificate_selected(hs) || |
David Benjamin | 650aa1c | 2016-12-20 18:55:16 -0500 | [diff] [blame] | 1454 | !ssl3_output_cert_chain(ssl)) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1455 | return -1; |
| 1456 | } |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1457 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1458 | } |
| 1459 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 1460 | static_assert(sizeof(size_t) >= sizeof(unsigned), |
| 1461 | "size_t is smaller than unsigned"); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1462 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1463 | static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) { |
| 1464 | SSL *const ssl = hs->ssl; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1465 | ScopedCBB cbb; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1466 | CBB body; |
| 1467 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1468 | SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1469 | return -1; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1470 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1471 | |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1472 | uint8_t *pms = NULL; |
| 1473 | size_t pms_len = 0; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1474 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1475 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1476 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1477 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1478 | unsigned psk_len = 0; |
| 1479 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1480 | if (alg_a & SSL_aPSK) { |
| 1481 | if (ssl->psk_client_callback == NULL) { |
| 1482 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
| 1483 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1484 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1485 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1486 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1487 | OPENSSL_memset(identity, 0, sizeof(identity)); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1488 | psk_len = |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1489 | ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint.get(), |
| 1490 | identity, sizeof(identity), psk, sizeof(psk)); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1491 | if (psk_len == 0) { |
| 1492 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1493 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1494 | goto err; |
| 1495 | } |
| 1496 | assert(psk_len <= PSK_MAX_PSK_LEN); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1497 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1498 | OPENSSL_free(hs->new_session->psk_identity); |
| 1499 | hs->new_session->psk_identity = BUF_strdup(identity); |
| 1500 | if (hs->new_session->psk_identity == NULL) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1501 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1502 | goto err; |
| 1503 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1504 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1505 | /* Write out psk_identity. */ |
| 1506 | CBB child; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1507 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1508 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1509 | OPENSSL_strnlen(identity, sizeof(identity))) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1510 | !CBB_flush(&body)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1511 | goto err; |
| 1512 | } |
| 1513 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1514 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1515 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1516 | if (alg_k & SSL_kRSA) { |
| 1517 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1518 | pms = (uint8_t *)OPENSSL_malloc(pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1519 | if (pms == NULL) { |
| 1520 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1521 | goto err; |
| 1522 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1523 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1524 | RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get()); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1525 | if (rsa == NULL) { |
| 1526 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1527 | goto err; |
| 1528 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1529 | |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 1530 | pms[0] = hs->client_version >> 8; |
| 1531 | pms[1] = hs->client_version & 0xff; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1532 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1533 | goto err; |
| 1534 | } |
| 1535 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1536 | CBB child, *enc_pms = &body; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1537 | size_t enc_pms_len; |
| 1538 | /* In TLS, there is a length prefix. */ |
| 1539 | if (ssl->version > SSL3_VERSION) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1540 | if (!CBB_add_u16_length_prefixed(&body, &child)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1541 | goto err; |
| 1542 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1543 | enc_pms = &child; |
| 1544 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1545 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1546 | uint8_t *ptr; |
| 1547 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
| 1548 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len, |
| 1549 | RSA_PKCS1_PADDING) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1550 | !CBB_did_write(enc_pms, enc_pms_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1551 | !CBB_flush(&body)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1552 | goto err; |
| 1553 | } |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1554 | } else if (alg_k & SSL_kECDHE) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1555 | /* Generate a keypair and serialize the public half. */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1556 | CBB child; |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1557 | if (!CBB_add_u8_length_prefixed(&body, &child)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1558 | goto err; |
| 1559 | } |
| 1560 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1561 | /* Compute the premaster. */ |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1562 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1563 | if (!hs->key_share->Accept(&child, &pms, &pms_len, &alert, hs->peer_key, |
| 1564 | hs->peer_key_len)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1565 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1566 | goto err; |
| 1567 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1568 | if (!CBB_flush(&body)) { |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1569 | goto err; |
| 1570 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1571 | |
| 1572 | /* The key exchange state may now be discarded. */ |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1573 | hs->key_share.reset(); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1574 | OPENSSL_free(hs->peer_key); |
| 1575 | hs->peer_key = NULL; |
| 1576 | hs->peer_key_len = 0; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1577 | } else if (alg_k & SSL_kPSK) { |
| 1578 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1579 | * the pre-shared key. */ |
| 1580 | pms_len = psk_len; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1581 | pms = (uint8_t *)OPENSSL_malloc(pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1582 | if (pms == NULL) { |
| 1583 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1584 | goto err; |
| 1585 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1586 | OPENSSL_memset(pms, 0, pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1587 | } else { |
| 1588 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1589 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1590 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1593 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1594 | * key. */ |
| 1595 | if (alg_a & SSL_aPSK) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1596 | ScopedCBB pms_cbb; |
| 1597 | CBB child; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1598 | uint8_t *new_pms; |
| 1599 | size_t new_pms_len; |
| 1600 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1601 | if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms_len) || |
| 1602 | !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1603 | !CBB_add_bytes(&child, pms, pms_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1604 | !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1605 | !CBB_add_bytes(&child, psk, psk_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1606 | !CBB_finish(pms_cbb.get(), &new_pms, &new_pms_len)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1607 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1608 | goto err; |
| 1609 | } |
| 1610 | OPENSSL_cleanse(pms, pms_len); |
| 1611 | OPENSSL_free(pms); |
| 1612 | pms = new_pms; |
| 1613 | pms_len = new_pms_len; |
| 1614 | } |
| 1615 | |
| 1616 | /* The message must be added to the finished hash before calculating the |
| 1617 | * master secret. */ |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1618 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1619 | goto err; |
| 1620 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1621 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1622 | hs->new_session->master_key_length = tls1_generate_master_secret( |
| 1623 | hs, hs->new_session->master_key, pms, pms_len); |
| 1624 | if (hs->new_session->master_key_length == 0) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1625 | goto err; |
| 1626 | } |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1627 | hs->new_session->extended_master_secret = hs->extended_master_secret; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1628 | OPENSSL_cleanse(pms, pms_len); |
| 1629 | OPENSSL_free(pms); |
| 1630 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1631 | return 1; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1632 | |
| 1633 | err: |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1634 | if (pms != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1635 | OPENSSL_cleanse(pms, pms_len); |
| 1636 | OPENSSL_free(pms); |
| 1637 | } |
| 1638 | return -1; |
| 1639 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1640 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1641 | static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) { |
| 1642 | SSL *const ssl = hs->ssl; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1643 | assert(ssl_has_private_key(ssl)); |
| 1644 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1645 | ScopedCBB cbb; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1646 | CBB body, child; |
| 1647 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1648 | SSL3_MT_CERTIFICATE_VERIFY)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1649 | return -1; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1650 | } |
| 1651 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1652 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1653 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1654 | return -1; |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1655 | } |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1656 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1657 | /* Write out the digest type in TLS 1.2. */ |
| 1658 | if (!CBB_add_u16(&body, signature_algorithm)) { |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1659 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1660 | return -1; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1661 | } |
| 1662 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1663 | |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1664 | /* Set aside space for the signature. */ |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1665 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1666 | uint8_t *ptr; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1667 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1668 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1669 | return -1; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1670 | } |
| 1671 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1672 | size_t sig_len = max_sig_len; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1673 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1674 | * single final digest and signature, and must be special-cased. */ |
| 1675 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 1676 | if (ssl->cert->key_method != NULL) { |
| 1677 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY); |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1678 | return -1; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1679 | } |
| 1680 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1681 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1682 | size_t digest_len; |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1683 | if (!hs->transcript.GetSSL3CertVerifyHash( |
| 1684 | digest, &digest_len, hs->new_session.get(), signature_algorithm)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1685 | return -1; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1686 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1687 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1688 | UniquePtr<EVP_PKEY_CTX> pctx(EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL)); |
| 1689 | if (!pctx || |
| 1690 | !EVP_PKEY_sign_init(pctx.get()) || |
| 1691 | !EVP_PKEY_sign(pctx.get(), ptr, &sig_len, digest, digest_len)) { |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1692 | return -1; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1693 | } |
| 1694 | } else { |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1695 | switch (ssl_private_key_sign( |
| 1696 | hs, ptr, &sig_len, max_sig_len, signature_algorithm, |
| 1697 | hs->transcript.buffer_data(), hs->transcript.buffer_len())) { |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1698 | case ssl_private_key_success: |
| 1699 | break; |
| 1700 | case ssl_private_key_failure: |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1701 | return -1; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1702 | case ssl_private_key_retry: |
| 1703 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1704 | return -1; |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1705 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1706 | } |
| 1707 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1708 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | d781fc4 | 2017-07-12 16:25:57 -0400 | [diff] [blame] | 1709 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 1710 | return -1; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1711 | } |
| 1712 | |
David Benjamin | 4414874 | 2017-06-17 13:20:59 -0400 | [diff] [blame] | 1713 | /* The handshake buffer is no longer necessary. */ |
David Benjamin | 6dc8bf6 | 2017-07-19 16:38:21 -0400 | [diff] [blame] | 1714 | hs->transcript.FreeBuffer(); |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1715 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1718 | static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) { |
| 1719 | SSL *const ssl = hs->ssl; |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1720 | static const uint8_t kZero[32] = {0}; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1721 | size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32); |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1722 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1723 | ScopedCBB cbb; |
| 1724 | CBB body, child; |
| 1725 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1726 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1727 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1728 | ssl->s3->next_proto_negotiated_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1729 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1730 | !CBB_add_bytes(&child, kZero, padding_len) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1731 | !ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1732 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1733 | return -1; |
| 1734 | } |
| 1735 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1736 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1739 | static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) { |
| 1740 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1741 | if (!ssl_do_channel_id_callback(ssl)) { |
| 1742 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1745 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1746 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1747 | return -1; |
| 1748 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1749 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1750 | ScopedCBB cbb; |
| 1751 | CBB body; |
| 1752 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1753 | !tls1_write_channel_id(hs, &body) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1754 | !ssl_add_message_cbb(ssl, cbb.get())) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1755 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1756 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1757 | } |
| 1758 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1759 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1760 | } |
| 1761 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1762 | static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) { |
| 1763 | SSL *const ssl = hs->ssl; |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1764 | SSLMessage msg; |
| 1765 | int ret = ssl_read_message(ssl, &msg); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1766 | if (ret <= 0) { |
| 1767 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1768 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1769 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1770 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) || |
| 1771 | !ssl_hash_message(hs, msg)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1772 | return -1; |
| 1773 | } |
| 1774 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1775 | CBS new_session_ticket = msg.body, ticket; |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1776 | uint32_t tlsext_tick_lifetime_hint; |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1777 | if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) || |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1778 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1779 | CBS_len(&new_session_ticket) != 0) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1780 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1781 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1782 | return -1; |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1783 | } |
| 1784 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1785 | if (CBS_len(&ticket) == 0) { |
| 1786 | /* RFC 5077 allows a server to change its mind and send no ticket after |
David Benjamin | 78476f6 | 2016-11-12 11:20:55 +0900 | [diff] [blame] | 1787 | * negotiating the extension. The value of |ticket_expected| is checked in |
| 1788 | * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1789 | hs->ticket_expected = 0; |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1790 | ssl->method->next_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1791 | return 1; |
| 1792 | } |
| 1793 | |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1794 | SSL_SESSION *session = hs->new_session.get(); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1795 | UniquePtr<SSL_SESSION> renewed_session; |
| 1796 | if (ssl->session != NULL) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1797 | /* The server is sending a new ticket for an existing session. Sessions are |
| 1798 | * immutable once established, so duplicate all but the ticket of the |
| 1799 | * existing session. */ |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1800 | renewed_session = |
| 1801 | SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1802 | if (!renewed_session) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1803 | /* This should never happen. */ |
| 1804 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1805 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1806 | } |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1807 | session = renewed_session.get(); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1810 | /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */ |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 1811 | ssl_session_rebase_time(ssl, session); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1812 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1813 | if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1814 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1815 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1816 | } |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1817 | session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1818 | |
| 1819 | /* Generate a session ID for this session based on the session ticket. We use |
| 1820 | * the session ID mechanism for detecting ticket resumption. This also fits in |
| 1821 | * with assumptions elsewhere in OpenSSL.*/ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1822 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), |
| 1823 | session->session_id, &session->session_id_length, |
| 1824 | EVP_sha256(), NULL)) { |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1825 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1826 | } |
| 1827 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1828 | if (renewed_session) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1829 | session->not_resumable = 0; |
| 1830 | SSL_SESSION_free(ssl->session); |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 1831 | ssl->session = renewed_session.release(); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1832 | } |
| 1833 | |
David Benjamin | 8f94c31 | 2017-08-01 17:35:55 -0400 | [diff] [blame] | 1834 | ssl->method->next_message(ssl); |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1835 | return 1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1836 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 1837 | |
| 1838 | } // namespace bssl |