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 | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 155 | #include <openssl/aead.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 156 | #include <openssl/bn.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 158 | #include <openssl/bytestring.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 159 | #include <openssl/dh.h> |
| 160 | #include <openssl/ec_key.h> |
| 161 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 162 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | #include <openssl/evp.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | #include <openssl/md5.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 165 | #include <openssl/mem.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 166 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 168 | #include "../crypto/internal.h" |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 169 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 172 | static int ssl3_send_client_hello(SSL_HANDSHAKE *hs); |
| 173 | static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs); |
| 174 | static int ssl3_get_server_hello(SSL_HANDSHAKE *hs); |
| 175 | static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs); |
| 176 | static int ssl3_get_cert_status(SSL_HANDSHAKE *hs); |
| 177 | static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs); |
| 178 | static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs); |
| 179 | static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs); |
| 180 | static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs); |
| 181 | static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs); |
| 182 | static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs); |
| 183 | static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs); |
| 184 | static int ssl3_send_next_proto(SSL_HANDSHAKE *hs); |
| 185 | static int ssl3_send_channel_id(SSL_HANDSHAKE *hs); |
| 186 | static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 187 | |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 188 | int ssl3_connect(SSL_HANDSHAKE *hs) { |
| 189 | SSL *const ssl = hs->ssl; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 190 | int ret = -1; |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 191 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 192 | assert(ssl->handshake_func == ssl3_connect); |
| 193 | assert(!ssl->server); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 195 | for (;;) { |
David Benjamin | 16b1b1d | 2017-02-10 23:41:14 -0500 | [diff] [blame] | 196 | int state = hs->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 198 | switch (hs->state) { |
David Benjamin | fc0c9d9 | 2016-09-01 01:33:33 -0400 | [diff] [blame] | 199 | case SSL_ST_INIT: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 200 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 201 | hs->state = SSL3_ST_CW_CLNT_HELLO_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 202 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 204 | case SSL3_ST_CW_CLNT_HELLO_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 205 | ret = ssl3_send_client_hello(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 206 | if (ret <= 0) { |
| 207 | goto end; |
| 208 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 209 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 210 | if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) { |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame^] | 211 | if (hs->early_data_offered) { |
| 212 | if (!tls13_init_early_key_schedule(hs) || |
| 213 | !tls13_advance_key_schedule(hs, ssl->session->master_key, |
| 214 | ssl->session->master_key_length) || |
| 215 | !tls13_derive_early_secrets(hs) || |
| 216 | !tls13_set_traffic_key(ssl, evp_aead_seal, |
| 217 | hs->early_traffic_secret, |
| 218 | hs->hash_len)) { |
| 219 | ret = -1; |
| 220 | goto end; |
| 221 | } |
| 222 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 223 | hs->next_state = SSL3_ST_CR_SRVR_HELLO_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 224 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 225 | hs->next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 226 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 227 | hs->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 228 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 229 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 230 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 231 | assert(SSL_is_dtls(ssl)); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 232 | ret = dtls1_get_hello_verify(hs); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 233 | if (ret <= 0) { |
| 234 | goto end; |
| 235 | } |
| 236 | if (ssl->d1->send_cookie) { |
| 237 | ssl->method->received_flight(ssl); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 238 | hs->state = SSL3_ST_CW_CLNT_HELLO_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 239 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 240 | hs->state = SSL3_ST_CR_SRVR_HELLO_A; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 241 | } |
| 242 | break; |
| 243 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 244 | case SSL3_ST_CR_SRVR_HELLO_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 245 | ret = ssl3_get_server_hello(hs); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 246 | if (hs->state == SSL_ST_TLS13) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 247 | break; |
| 248 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 249 | if (ret <= 0) { |
| 250 | goto end; |
| 251 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 253 | if (ssl->session != NULL) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 254 | hs->state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 255 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 256 | hs->state = SSL3_ST_CR_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 257 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 258 | break; |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 259 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 260 | case SSL3_ST_CR_CERT_A: |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 261 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 262 | ret = ssl3_get_server_certificate(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 263 | if (ret <= 0) { |
| 264 | goto end; |
| 265 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 266 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 267 | hs->state = SSL3_ST_CR_CERT_STATUS_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 268 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 270 | case SSL3_ST_CR_CERT_STATUS_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 271 | if (hs->certificate_status_expected) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 272 | ret = ssl3_get_cert_status(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 273 | if (ret <= 0) { |
| 274 | goto end; |
| 275 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 276 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 277 | hs->state = SSL3_ST_VERIFY_SERVER_CERT; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 278 | break; |
| 279 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 280 | case SSL3_ST_VERIFY_SERVER_CERT: |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 281 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 282 | ret = ssl3_verify_server_cert(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 283 | if (ret <= 0) { |
| 284 | goto end; |
| 285 | } |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 286 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 287 | hs->state = SSL3_ST_CR_KEY_EXCH_A; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 288 | break; |
| 289 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 290 | case SSL3_ST_CR_KEY_EXCH_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 291 | ret = ssl3_get_server_key_exchange(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 292 | if (ret <= 0) { |
| 293 | goto end; |
| 294 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 295 | hs->state = SSL3_ST_CR_CERT_REQ_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 296 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 298 | case SSL3_ST_CR_CERT_REQ_A: |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 299 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 300 | ret = ssl3_get_certificate_request(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 301 | if (ret <= 0) { |
| 302 | goto end; |
| 303 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 304 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 305 | hs->state = SSL3_ST_CR_SRVR_DONE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 306 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 308 | case SSL3_ST_CR_SRVR_DONE_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 309 | ret = ssl3_get_server_hello_done(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 310 | if (ret <= 0) { |
| 311 | goto end; |
| 312 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 313 | ssl->method->received_flight(ssl); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 314 | hs->state = SSL3_ST_CW_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 315 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 317 | case SSL3_ST_CW_CERT_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 318 | if (hs->cert_request) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 319 | ret = ssl3_send_client_certificate(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 320 | if (ret <= 0) { |
| 321 | goto end; |
| 322 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 323 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 324 | hs->state = SSL3_ST_CW_KEY_EXCH_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 325 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 327 | case SSL3_ST_CW_KEY_EXCH_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 328 | ret = ssl3_send_client_key_exchange(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 329 | if (ret <= 0) { |
| 330 | goto end; |
| 331 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 332 | hs->state = SSL3_ST_CW_CERT_VRFY_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 333 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 335 | case SSL3_ST_CW_CERT_VRFY_A: |
| 336 | case SSL3_ST_CW_CERT_VRFY_B: |
David Benjamin | a660e7a | 2017-01-02 08:15:35 -0500 | [diff] [blame] | 337 | if (hs->cert_request && ssl_has_certificate(ssl)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 338 | ret = ssl3_send_cert_verify(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 339 | if (ret <= 0) { |
| 340 | goto end; |
| 341 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 342 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 343 | hs->state = SSL3_ST_CW_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 344 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 345 | |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 346 | case SSL3_ST_CW_CHANGE: |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 347 | if (!ssl->method->add_change_cipher_spec(ssl) || |
| 348 | !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 349 | ret = -1; |
| 350 | goto end; |
| 351 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 353 | hs->state = SSL3_ST_CW_NEXT_PROTO_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_NEXT_PROTO_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 357 | if (hs->next_proto_neg_seen) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 358 | ret = ssl3_send_next_proto(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_CHANNEL_ID_A; |
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 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 366 | case SSL3_ST_CW_CHANNEL_ID_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 367 | if (ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 368 | ret = ssl3_send_channel_id(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 369 | if (ret <= 0) { |
| 370 | goto end; |
| 371 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 372 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 373 | hs->state = SSL3_ST_CW_FINISHED_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_FINISHED_A: |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 377 | ret = ssl3_send_finished(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 378 | if (ret <= 0) { |
| 379 | goto end; |
| 380 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 381 | hs->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 382 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 383 | if (ssl->session != NULL) { |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 384 | hs->next_state = SSL3_ST_FINISH_CLIENT_HANDSHAKE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 385 | } else { |
| 386 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 387 | * record the handshake hashes at this point in the session so that |
| 388 | * any resumption of this session with ChannelID can sign those |
| 389 | * hashes. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 390 | ret = tls1_record_handshake_hashes_for_channel_id(hs); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 391 | if (ret <= 0) { |
| 392 | goto end; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 393 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 394 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 395 | ssl3_can_false_start(ssl) && |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 396 | /* No False Start on renegotiation (would complicate the state |
| 397 | * machine). */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 398 | !ssl->s3->initial_handshake_complete) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 399 | hs->next_state = SSL3_ST_FALSE_START; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 400 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 401 | hs->next_state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 402 | } |
| 403 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 404 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 406 | case SSL3_ST_FALSE_START: |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 407 | hs->state = SSL3_ST_CR_SESSION_TICKET_A; |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 408 | hs->in_false_start = 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 409 | ret = 1; |
| 410 | goto end; |
| 411 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 412 | case SSL3_ST_CR_SESSION_TICKET_A: |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 413 | if (hs->ticket_expected) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 414 | ret = ssl3_get_new_session_ticket(hs); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 415 | if (ret <= 0) { |
| 416 | goto end; |
| 417 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 418 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 419 | hs->state = SSL3_ST_CR_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 420 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 422 | case SSL3_ST_CR_CHANGE: |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 423 | ret = ssl->method->read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 424 | if (ret <= 0) { |
| 425 | goto end; |
| 426 | } |
| 427 | |
David Benjamin | 6773972 | 2016-11-17 17:03:59 +0900 | [diff] [blame] | 428 | if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 429 | ret = -1; |
| 430 | goto end; |
| 431 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 432 | hs->state = SSL3_ST_CR_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 433 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 435 | case SSL3_ST_CR_FINISHED_A: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 436 | ret = ssl3_get_finished(hs); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 437 | if (ret <= 0) { |
| 438 | goto end; |
| 439 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 440 | ssl->method->received_flight(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 442 | if (ssl->session != NULL) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 443 | hs->state = SSL3_ST_CW_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 444 | } else { |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 445 | hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 446 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 447 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 449 | case SSL3_ST_CW_FLUSH: |
David Benjamin | 8d5f9da | 2017-01-01 17:41:30 -0500 | [diff] [blame] | 450 | ret = ssl->method->flush_flight(ssl); |
| 451 | if (ret <= 0) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 452 | goto end; |
| 453 | } |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 454 | hs->state = hs->next_state; |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 455 | if (hs->state != SSL3_ST_FINISH_CLIENT_HANDSHAKE) { |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 456 | ssl->method->expect_flight(ssl); |
| 457 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 458 | break; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 460 | case SSL_ST_TLS13: |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 461 | ret = tls13_handshake(hs); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 462 | if (ret <= 0) { |
| 463 | goto end; |
| 464 | } |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 465 | hs->state = SSL3_ST_FINISH_CLIENT_HANDSHAKE; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 466 | break; |
| 467 | |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 468 | case SSL3_ST_FINISH_CLIENT_HANDSHAKE: |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 469 | ssl->method->release_current_message(ssl, 1 /* free_buffer */); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 470 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 471 | SSL_SESSION_free(ssl->s3->established_session); |
| 472 | if (ssl->session != NULL) { |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 473 | SSL_SESSION_up_ref(ssl->session); |
| 474 | ssl->s3->established_session = ssl->session; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 475 | } else { |
| 476 | /* We make a copy of the session in order to maintain the immutability |
| 477 | * of the new established_session due to False Start. The caller may |
| 478 | * have taken a reference to the temporary session. */ |
| 479 | ssl->s3->established_session = |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 480 | SSL_SESSION_dup(hs->new_session, SSL_SESSION_DUP_ALL); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 481 | if (ssl->s3->established_session == NULL) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 482 | ret = -1; |
| 483 | goto end; |
| 484 | } |
| 485 | ssl->s3->established_session->not_resumable = 0; |
| 486 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 487 | SSL_SESSION_free(hs->new_session); |
| 488 | hs->new_session = NULL; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 489 | } |
| 490 | |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 491 | hs->state = SSL_ST_OK; |
| 492 | break; |
| 493 | |
| 494 | case SSL_ST_OK: { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 495 | const int is_initial_handshake = !ssl->s3->initial_handshake_complete; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 496 | ssl->s3->initial_handshake_complete = 1; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 497 | if (is_initial_handshake) { |
| 498 | /* Renegotiations do not participate in session resumption. */ |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 499 | ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 500 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 502 | ret = 1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 503 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 504 | goto end; |
David Benjamin | 77458a4 | 2017-02-10 23:33:27 -0500 | [diff] [blame] | 505 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 506 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 507 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 508 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 509 | ret = -1; |
| 510 | goto end; |
| 511 | } |
| 512 | |
David Benjamin | 16b1b1d | 2017-02-10 23:41:14 -0500 | [diff] [blame] | 513 | if (hs->state != state) { |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 514 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 515 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 518 | end: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 519 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 520 | return ret; |
| 521 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 523 | uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) { |
| 524 | /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on |
| 525 | * a single byte repeatedly and ensures the values are deterministic. This |
| 526 | * allows the same ClientHello be sent twice for a HelloRetryRequest or the |
| 527 | * same group be advertised in both supported_groups and key_shares. */ |
| 528 | uint16_t ret = ssl->s3->client_random[index]; |
| 529 | /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */ |
| 530 | ret = (ret & 0xf0) | 0x0a; |
| 531 | ret |= ret << 8; |
| 532 | return ret; |
| 533 | } |
| 534 | |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 535 | /* ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of |
| 536 | * disabled algorithms. */ |
| 537 | static void ssl_get_client_disabled(SSL *ssl, uint32_t *out_mask_a, |
| 538 | uint32_t *out_mask_k) { |
| 539 | int have_rsa = 0, have_ecdsa = 0; |
| 540 | *out_mask_a = 0; |
| 541 | *out_mask_k = 0; |
| 542 | |
| 543 | /* Now go through all signature algorithms seeing if we support any for RSA or |
| 544 | * ECDSA. Do this for all versions not just TLS 1.2. */ |
| 545 | const uint16_t *sigalgs; |
| 546 | size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs); |
| 547 | for (size_t i = 0; i < num_sigalgs; i++) { |
| 548 | switch (sigalgs[i]) { |
| 549 | case SSL_SIGN_RSA_PSS_SHA512: |
| 550 | case SSL_SIGN_RSA_PSS_SHA384: |
| 551 | case SSL_SIGN_RSA_PSS_SHA256: |
| 552 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 553 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 554 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 555 | case SSL_SIGN_RSA_PKCS1_SHA1: |
| 556 | have_rsa = 1; |
| 557 | break; |
| 558 | |
| 559 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 560 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 561 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 562 | case SSL_SIGN_ECDSA_SHA1: |
| 563 | have_ecdsa = 1; |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* Disable auth if we don't include any appropriate signature algorithms. */ |
| 569 | if (!have_rsa) { |
| 570 | *out_mask_a |= SSL_aRSA; |
| 571 | } |
| 572 | if (!have_ecdsa) { |
| 573 | *out_mask_a |= SSL_aECDSA; |
| 574 | } |
| 575 | |
| 576 | /* PSK requires a client callback. */ |
| 577 | if (ssl->psk_client_callback == NULL) { |
| 578 | *out_mask_a |= SSL_aPSK; |
| 579 | *out_mask_k |= SSL_kPSK; |
| 580 | } |
| 581 | } |
| 582 | |
David Benjamin | 5e7e7cc | 2016-07-21 12:55:28 +0200 | [diff] [blame] | 583 | static int ssl_write_client_cipher_list(SSL *ssl, CBB *out, |
| 584 | uint16_t min_version, |
David Benjamin | e0ff767 | 2016-09-19 18:40:03 -0400 | [diff] [blame] | 585 | uint16_t max_version) { |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 586 | uint32_t mask_a, mask_k; |
| 587 | ssl_get_client_disabled(ssl, &mask_a, &mask_k); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 588 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 589 | CBB child; |
| 590 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 591 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 592 | } |
| 593 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 594 | /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */ |
| 595 | if (ssl->ctx->grease_enabled && |
| 596 | !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) { |
| 597 | return 0; |
| 598 | } |
| 599 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 600 | /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on |
| 601 | * hardware support. */ |
| 602 | if (max_version >= TLS1_3_VERSION) { |
| 603 | if (!EVP_has_aes_hardware() && |
| 604 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
| 605 | return 0; |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 606 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 607 | if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) || |
| 608 | !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) { |
| 609 | return 0; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 610 | } |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 611 | if (EVP_has_aes_hardware() && |
| 612 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 613 | return 0; |
| 614 | } |
| 615 | } |
| 616 | |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 617 | if (min_version < TLS1_3_VERSION) { |
| 618 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
| 619 | int any_enabled = 0; |
| 620 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 621 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 622 | /* Skip disabled ciphers */ |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 623 | if ((cipher->algorithm_mkey & mask_k) || |
| 624 | (cipher->algorithm_auth & mask_a)) { |
David Benjamin | abbbee1 | 2016-10-31 19:20:42 -0400 | [diff] [blame] | 625 | continue; |
| 626 | } |
| 627 | if (SSL_CIPHER_get_min_version(cipher) > max_version || |
| 628 | SSL_CIPHER_get_max_version(cipher) < min_version) { |
| 629 | continue; |
| 630 | } |
| 631 | any_enabled = 1; |
| 632 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 633 | return 0; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* If all ciphers were disabled, return the error to the caller. */ |
| 638 | if (!any_enabled && max_version < TLS1_3_VERSION) { |
| 639 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 640 | return 0; |
| 641 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 645 | * added. */ |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 646 | if (max_version == SSL3_VERSION && |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 647 | !ssl->s3->initial_handshake_complete) { |
| 648 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 649 | return 0; |
| 650 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 651 | } |
| 652 | |
David Benjamin | e0ff767 | 2016-09-19 18:40:03 -0400 | [diff] [blame] | 653 | if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
David Benjamin | 5e7e7cc | 2016-07-21 12:55:28 +0200 | [diff] [blame] | 654 | if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 655 | return 0; |
| 656 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return CBB_flush(out); |
| 660 | } |
| 661 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 662 | int ssl_write_client_hello(SSL_HANDSHAKE *hs) { |
| 663 | SSL *const ssl = hs->ssl; |
David Benjamin | e0ff767 | 2016-09-19 18:40:03 -0400 | [diff] [blame] | 664 | uint16_t min_version, max_version; |
| 665 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 669 | CBB cbb, body; |
| 670 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) { |
| 671 | goto err; |
| 672 | } |
| 673 | |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 674 | /* Renegotiations do not participate in session resumption. */ |
| 675 | int has_session = ssl->session != NULL && |
| 676 | !ssl->s3->initial_handshake_complete; |
| 677 | |
| 678 | CBB child; |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 679 | if (!CBB_add_u16(&body, hs->client_version) || |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 680 | !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 681 | !CBB_add_u8_length_prefixed(&body, &child) || |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 682 | (has_session && |
| 683 | !CBB_add_bytes(&child, ssl->session->session_id, |
| 684 | ssl->session->session_id_length))) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 685 | goto err; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 686 | } |
| 687 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 688 | if (SSL_is_dtls(ssl)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 689 | if (!CBB_add_u8_length_prefixed(&body, &child) || |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 690 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 691 | goto err; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | size_t header_len = |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 696 | SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 697 | if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) || |
| 698 | !CBB_add_u8(&body, 1 /* one compression method */) || |
| 699 | !CBB_add_u8(&body, 0 /* null compression */) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 700 | !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 701 | goto err; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 702 | } |
| 703 | |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 704 | uint8_t *msg = NULL; |
| 705 | size_t len; |
| 706 | if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) { |
| 707 | goto err; |
| 708 | } |
| 709 | |
| 710 | /* Now that the length prefixes have been computed, fill in the placeholder |
| 711 | * PSK binder. */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 712 | if (hs->needs_psk_binder && |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 713 | !tls13_write_psk_binder(hs, msg, len)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 714 | OPENSSL_free(msg); |
| 715 | goto err; |
| 716 | } |
| 717 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 718 | return ssl->method->add_message(ssl, msg, len); |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 719 | |
| 720 | err: |
| 721 | CBB_cleanup(&cbb); |
| 722 | return 0; |
Steven Valdez | 5440fe0 | 2016-07-18 12:40:30 -0400 | [diff] [blame] | 723 | } |
| 724 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 725 | static int ssl3_send_client_hello(SSL_HANDSHAKE *hs) { |
| 726 | SSL *const ssl = hs->ssl; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 727 | /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we |
| 728 | * may send multiple ClientHellos if we receive HelloVerifyRequest. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 729 | if (!SSL_TRANSCRIPT_init(&hs->transcript)) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 730 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 731 | return -1; |
| 732 | } |
| 733 | |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 734 | uint16_t min_version, max_version; |
| 735 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 736 | return -1; |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 737 | } |
| 738 | |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 739 | uint16_t max_wire_version = ssl->method->version_to_wire(max_version); |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 740 | assert(hs->state == SSL3_ST_CW_CLNT_HELLO_A); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 741 | if (!ssl->s3->have_version) { |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 742 | ssl->version = max_wire_version; |
| 743 | } |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 744 | |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 745 | /* Always advertise the ClientHello version from the original maximum version, |
| 746 | * even on renegotiation. The static RSA key exchange uses this field, and |
| 747 | * some servers fail when it changes across handshakes. */ |
| 748 | hs->client_version = max_wire_version; |
| 749 | if (max_version >= TLS1_3_VERSION) { |
| 750 | hs->client_version = ssl->method->version_to_wire(TLS1_2_VERSION); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 751 | } |
| 752 | |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 753 | /* If the configured session has expired or was created at a disabled |
| 754 | * version, drop it. */ |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 755 | if (ssl->session != NULL) { |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 756 | uint16_t session_version; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 757 | if (ssl->session->is_server || |
| 758 | !ssl->method->version_from_wire(&session_version, |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 759 | ssl->session->ssl_version) || |
| 760 | (session_version < TLS1_3_VERSION && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 761 | ssl->session->session_id_length == 0) || |
| 762 | ssl->session->not_resumable || |
| 763 | !ssl_session_is_time_valid(ssl, ssl->session) || |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 764 | session_version < min_version || session_version > max_version) { |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 765 | ssl_set_session(ssl, NULL); |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 766 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 770 | * renegerate the client_random. The random must be reused. */ |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 771 | if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) && |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 772 | !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 773 | return -1; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 774 | } |
| 775 | |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 776 | if (!ssl_write_client_hello(hs)) { |
Steven Valdez | a833c35 | 2016-11-01 13:39:36 -0400 | [diff] [blame] | 777 | return -1; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 778 | } |
| 779 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 780 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 781 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 782 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 783 | static int dtls1_get_hello_verify(SSL_HANDSHAKE *hs) { |
| 784 | SSL *const ssl = hs->ssl; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 785 | int al; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 786 | CBS hello_verify_request, cookie; |
| 787 | uint16_t server_version; |
| 788 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 789 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 790 | if (ret <= 0) { |
| 791 | return ret; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 795 | ssl->d1->send_cookie = 0; |
| 796 | ssl->s3->tmp.reuse_message = 1; |
| 797 | return 1; |
| 798 | } |
| 799 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 800 | CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 801 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 802 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 803 | CBS_len(&hello_verify_request) != 0) { |
| 804 | al = SSL_AD_DECODE_ERROR; |
| 805 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 806 | goto f_err; |
| 807 | } |
| 808 | |
| 809 | if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) { |
| 810 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 811 | goto f_err; |
| 812 | } |
| 813 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 814 | OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 815 | ssl->d1->cookie_len = CBS_len(&cookie); |
| 816 | |
| 817 | ssl->d1->send_cookie = 1; |
| 818 | return 1; |
| 819 | |
| 820 | f_err: |
| 821 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 822 | return -1; |
| 823 | } |
| 824 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 825 | static int ssl3_get_server_hello(SSL_HANDSHAKE *hs) { |
| 826 | SSL *const ssl = hs->ssl; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 827 | int al = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 828 | CBS server_hello, server_random, session_id; |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 829 | uint16_t server_wire_version, cipher_suite; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 830 | uint8_t compression_method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 831 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 832 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 833 | if (ret <= 0) { |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 834 | uint32_t err = ERR_peek_error(); |
| 835 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 836 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 837 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 838 | * in response to ClientHello. This matches NSS's client behavior and |
| 839 | * gives a better error on a (probable) failure to negotiate initial |
| 840 | * parameters. Note: this error code comes after the original one. |
| 841 | * |
| 842 | * See https://crbug.com/446505. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 843 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 844 | } |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 845 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 846 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 847 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 848 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO && |
| 849 | ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) { |
| 850 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 851 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 852 | return -1; |
| 853 | } |
| 854 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 855 | CBS_init(&server_hello, ssl->init_msg, ssl->init_num); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 856 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 857 | if (!CBS_get_u16(&server_hello, &server_wire_version)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 858 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 859 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 860 | goto f_err; |
| 861 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 862 | |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 863 | uint16_t min_version, max_version, server_version; |
David Benjamin | e0ff767 | 2016-09-19 18:40:03 -0400 | [diff] [blame] | 864 | if (!ssl_get_version_range(ssl, &min_version, &max_version) || |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 865 | !ssl->method->version_from_wire(&server_version, server_wire_version) || |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 866 | server_version < min_version || server_version > max_version) { |
| 867 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 868 | al = SSL_AD_PROTOCOL_VERSION; |
| 869 | goto f_err; |
| 870 | } |
| 871 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 872 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 873 | if (!ssl->s3->have_version) { |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 874 | ssl->version = server_wire_version; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 875 | /* 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] | 876 | * fixed. Begin enforcing the record-layer version. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 877 | ssl->s3->have_version = 1; |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 878 | } else if (server_wire_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 879 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 880 | al = SSL_AD_PROTOCOL_VERSION; |
| 881 | goto f_err; |
| 882 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 883 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 884 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 885 | hs->state = SSL_ST_TLS13; |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 886 | hs->do_tls13_handshake = tls13_client_handshake; |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 887 | return 1; |
| 888 | } |
| 889 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame^] | 890 | if (hs->early_data_offered) { |
| 891 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA); |
| 892 | al = SSL_AD_PROTOCOL_VERSION; |
| 893 | goto f_err; |
| 894 | } |
| 895 | |
David Benjamin | 6e4fc33 | 2016-11-17 16:43:08 +0900 | [diff] [blame] | 896 | ssl_clear_tls13_state(hs); |
David Benjamin | 4fe3c90 | 2016-08-16 02:17:03 -0400 | [diff] [blame] | 897 | |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 898 | if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO)) { |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 899 | return -1; |
| 900 | } |
| 901 | |
| 902 | if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
| 903 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 904 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 905 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 906 | !CBS_get_u8(&server_hello, &compression_method)) { |
| 907 | al = SSL_AD_DECODE_ERROR; |
| 908 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 909 | goto f_err; |
| 910 | } |
| 911 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 912 | /* Copy over the server random. */ |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 913 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 914 | |
David Benjamin | 5510863 | 2016-08-11 22:01:18 -0400 | [diff] [blame] | 915 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 916 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 917 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 918 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 919 | ssl->session->session_id_length != 0 && |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 920 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 921 | ssl->session->session_id_length)) { |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 922 | ssl->s3->session_reused = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 923 | } else { |
| 924 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 925 | * fill out. */ |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 926 | ssl_set_session(ssl, NULL); |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 927 | if (!ssl_get_new_session(hs, 0 /* client */)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 928 | goto f_err; |
| 929 | } |
| 930 | /* Note: session_id could be empty. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 931 | hs->new_session->session_id_length = CBS_len(&session_id); |
| 932 | OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id), |
| 933 | CBS_len(&session_id)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 934 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 935 | |
David Benjamin | d2cb1c1 | 2016-11-02 17:49:09 -0400 | [diff] [blame] | 936 | const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 937 | if (c == NULL) { |
| 938 | /* unknown cipher */ |
| 939 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 940 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 941 | goto f_err; |
| 942 | } |
David Benjamin | d2cb1c1 | 2016-11-02 17:49:09 -0400 | [diff] [blame] | 943 | |
| 944 | /* The cipher must be allowed in the selected version and enabled. */ |
David Benjamin | 2fe6e22 | 2017-01-25 02:18:43 -0500 | [diff] [blame] | 945 | uint32_t mask_a, mask_k; |
| 946 | ssl_get_client_disabled(ssl, &mask_a, &mask_k); |
| 947 | if ((c->algorithm_mkey & mask_k) || (c->algorithm_auth & mask_a) || |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 948 | SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) || |
David Benjamin | d2cb1c1 | 2016-11-02 17:49:09 -0400 | [diff] [blame] | 949 | SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) || |
| 950 | !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 951 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 952 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 953 | goto f_err; |
| 954 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 955 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 956 | if (ssl->session != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 957 | if (ssl->session->ssl_version != ssl->version) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 958 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 959 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 960 | goto f_err; |
| 961 | } |
Steven Valdez | 803c77a | 2016-09-06 14:13:43 -0400 | [diff] [blame] | 962 | if (ssl->session->cipher != c) { |
| 963 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 964 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 965 | goto f_err; |
| 966 | } |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 967 | if (!ssl_session_is_context_valid(ssl, ssl->session)) { |
| 968 | /* This is actually a client application bug. */ |
| 969 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 970 | OPENSSL_PUT_ERROR(SSL, |
| 971 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 972 | goto f_err; |
| 973 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 974 | } else { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 975 | hs->new_session->cipher = c; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 976 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 977 | hs->new_cipher = c; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 978 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 979 | /* Now that the cipher is known, initialize the handshake hash and hash the |
| 980 | * ServerHello. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 981 | if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl), |
| 982 | c->algorithm_prf) || |
| 983 | !ssl_hash_current_message(hs)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 984 | goto f_err; |
| 985 | } |
| 986 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 987 | /* If doing a full handshake, the server may request a client certificate |
| 988 | * which requires hashing the handshake transcript. Otherwise, the handshake |
| 989 | * buffer may be released. */ |
David Benjamin | 3d458dc | 2016-09-12 22:40:27 +0000 | [diff] [blame] | 990 | if (ssl->session != NULL || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 991 | !ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 992 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 993 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 994 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 995 | /* Only the NULL compression algorithm is supported. */ |
| 996 | if (compression_method != 0) { |
| 997 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 998 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 999 | goto f_err; |
| 1000 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1001 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1002 | /* TLS extensions */ |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1003 | if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1004 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1005 | goto err; |
| 1006 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1007 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1008 | /* There should be nothing left over in the record. */ |
| 1009 | if (CBS_len(&server_hello) != 0) { |
| 1010 | /* wrong packet length */ |
| 1011 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1012 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1013 | goto f_err; |
| 1014 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1015 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1016 | if (ssl->session != NULL && |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1017 | hs->extended_master_secret != ssl->session->extended_master_secret) { |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 1018 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1019 | if (ssl->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1020 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 1021 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1022 | 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] | 1023 | } |
| 1024 | goto f_err; |
| 1025 | } |
| 1026 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1027 | return 1; |
| 1028 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1029 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1030 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1031 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1032 | return -1; |
| 1033 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1034 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1035 | static int ssl3_get_server_certificate(SSL_HANDSHAKE *hs) { |
| 1036 | SSL *const ssl = hs->ssl; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1037 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1038 | if (ret <= 0) { |
| 1039 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1040 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1041 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1042 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1043 | !ssl_hash_current_message(hs)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1044 | return -1; |
| 1045 | } |
| 1046 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1047 | CBS cbs; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1048 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1049 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1050 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1051 | sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free); |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1052 | EVP_PKEY_free(hs->peer_pubkey); |
| 1053 | hs->peer_pubkey = NULL; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1054 | hs->new_session->certs = ssl_parse_cert_chain(&alert, &hs->peer_pubkey, NULL, |
| 1055 | &cbs, ssl->ctx->pool); |
| 1056 | if (hs->new_session->certs == NULL) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1057 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1058 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1059 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1060 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1061 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0 || |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1062 | CBS_len(&cbs) != 0 || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1063 | !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1064 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1065 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1066 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1067 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1068 | |
Adam Langley | 0567220 | 2016-12-13 12:05:49 -0800 | [diff] [blame] | 1069 | if (!ssl_check_leaf_certificate( |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1070 | hs, hs->peer_pubkey, |
| 1071 | sk_CRYPTO_BUFFER_value(hs->new_session->certs, 0))) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1072 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1073 | return -1; |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
David Benjamin | a58baaf | 2017-02-28 20:54:28 -0500 | [diff] [blame] | 1076 | /* Disallow the server certificate from changing during a renegotiation. See |
| 1077 | * https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation, |
| 1078 | * so this check is sufficient. */ |
| 1079 | if (ssl->s3->established_session != NULL) { |
| 1080 | if (sk_CRYPTO_BUFFER_num(ssl->s3->established_session->certs) != |
| 1081 | sk_CRYPTO_BUFFER_num(hs->new_session->certs)) { |
| 1082 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED); |
| 1083 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) { |
| 1088 | const CRYPTO_BUFFER *old_cert = |
| 1089 | sk_CRYPTO_BUFFER_value(ssl->s3->established_session->certs, i); |
| 1090 | const CRYPTO_BUFFER *new_cert = |
| 1091 | sk_CRYPTO_BUFFER_value(hs->new_session->certs, i); |
| 1092 | if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) || |
| 1093 | OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert), |
| 1094 | CRYPTO_BUFFER_data(new_cert), |
| 1095 | CRYPTO_BUFFER_len(old_cert)) != 0) { |
| 1096 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED); |
| 1097 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1098 | return -1; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1103 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1106 | static int ssl3_get_cert_status(SSL_HANDSHAKE *hs) { |
| 1107 | SSL *const ssl = hs->ssl; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1108 | int al; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1109 | CBS certificate_status, ocsp_response; |
| 1110 | uint8_t status_type; |
| 1111 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1112 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1113 | if (ret <= 0) { |
| 1114 | return ret; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { |
| 1118 | /* A server may send status_request in ServerHello and then change |
| 1119 | * its mind about sending CertificateStatus. */ |
| 1120 | ssl->s3->tmp.reuse_message = 1; |
| 1121 | return 1; |
| 1122 | } |
| 1123 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1124 | if (!ssl_hash_current_message(hs)) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1125 | return -1; |
| 1126 | } |
| 1127 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1128 | CBS_init(&certificate_status, ssl->init_msg, ssl->init_num); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1129 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1130 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1131 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1132 | CBS_len(&ocsp_response) == 0 || |
| 1133 | CBS_len(&certificate_status) != 0) { |
| 1134 | al = SSL_AD_DECODE_ERROR; |
| 1135 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1136 | goto f_err; |
| 1137 | } |
| 1138 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1139 | if (!CBS_stow(&ocsp_response, &hs->new_session->ocsp_response, |
| 1140 | &hs->new_session->ocsp_response_length)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1141 | al = SSL_AD_INTERNAL_ERROR; |
| 1142 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1143 | goto f_err; |
| 1144 | } |
| 1145 | return 1; |
| 1146 | |
| 1147 | f_err: |
| 1148 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 1149 | return -1; |
| 1150 | } |
| 1151 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1152 | static int ssl3_verify_server_cert(SSL_HANDSHAKE *hs) { |
| 1153 | SSL *const ssl = hs->ssl; |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 1154 | if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session, ssl)) { |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 1155 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
Adam Langley | 9498e74 | 2016-07-18 10:17:16 -0700 | [diff] [blame] | 1158 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1159 | } |
| 1160 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1161 | static int ssl3_get_server_key_exchange(SSL_HANDSHAKE *hs) { |
| 1162 | SSL *const ssl = hs->ssl; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1163 | int al; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1164 | DH *dh = NULL; |
| 1165 | EC_KEY *ecdh = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1166 | EC_POINT *srvr_ecpoint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1167 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1168 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1169 | if (ret <= 0) { |
| 1170 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1173 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
David Benjamin | bac75b8 | 2016-09-16 19:34:02 -0400 | [diff] [blame] | 1174 | /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1175 | if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1176 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1177 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1178 | return -1; |
| 1179 | } |
| 1180 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1181 | ssl->s3->tmp.reuse_message = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1182 | return 1; |
| 1183 | } |
| 1184 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1185 | if (!ssl_hash_current_message(hs)) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1186 | return -1; |
| 1187 | } |
| 1188 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1189 | /* Retain a copy of the original CBS to compute the signature over. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1190 | CBS server_key_exchange; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1191 | CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num); |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1192 | CBS server_key_exchange_orig = server_key_exchange; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1193 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1194 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1195 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1196 | |
| 1197 | if (alg_a & SSL_aPSK) { |
| 1198 | CBS psk_identity_hint; |
| 1199 | |
| 1200 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1201 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1202 | &psk_identity_hint)) { |
| 1203 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1204 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1205 | goto f_err; |
| 1206 | } |
| 1207 | |
| 1208 | /* Store PSK identity hint for later use, hint is used in |
| 1209 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1210 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1211 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1212 | * |
| 1213 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1214 | * a specific identity. */ |
| 1215 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1216 | CBS_contains_zero_byte(&psk_identity_hint)) { |
| 1217 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1218 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1219 | goto f_err; |
| 1220 | } |
| 1221 | |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 1222 | /* Save non-empty identity hints as a C string. Empty identity hints we |
| 1223 | * treat as missing. Plain PSK makes it possible to send either no hint |
| 1224 | * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell |
| 1225 | * empty hint. Having different capabilities is odd, so we interpret empty |
| 1226 | * and missing as identical. */ |
| 1227 | if (CBS_len(&psk_identity_hint) != 0 && |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1228 | !CBS_strdup(&psk_identity_hint, &hs->peer_psk_identity_hint)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1229 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1230 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1231 | goto f_err; |
| 1232 | } |
| 1233 | } |
| 1234 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1235 | if (alg_k & SSL_kDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1236 | CBS dh_p, dh_g, dh_Ys; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1237 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1238 | CBS_len(&dh_p) == 0 || |
| 1239 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1240 | CBS_len(&dh_g) == 0 || |
| 1241 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1242 | CBS_len(&dh_Ys) == 0) { |
| 1243 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1244 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1245 | goto f_err; |
| 1246 | } |
| 1247 | |
| 1248 | dh = DH_new(); |
| 1249 | if (dh == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1250 | goto err; |
| 1251 | } |
| 1252 | |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1253 | dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL); |
| 1254 | dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL); |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1255 | if (dh->p == NULL || dh->g == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1256 | goto err; |
| 1257 | } |
| 1258 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 1259 | unsigned bits = DH_num_bits(dh); |
| 1260 | if (bits < 1024) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1261 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1262 | goto err; |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 1263 | } else if (bits > 4096) { |
David Benjamin | cd24a39 | 2015-11-11 13:23:05 -0800 | [diff] [blame] | 1264 | /* Overly large DHE groups are prohibitively expensive, so enforce a limit |
| 1265 | * to prevent a server from causing us to perform too expensive of a |
| 1266 | * computation. */ |
| 1267 | OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG); |
| 1268 | goto err; |
| 1269 | } |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1270 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1271 | SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1272 | dh = NULL; |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1273 | |
| 1274 | /* Save the peer public key for later. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1275 | if (!CBS_stow(&dh_Ys, &hs->peer_key, &hs->peer_key_len)) { |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1276 | goto err; |
| 1277 | } |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1278 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1279 | /* Parse the server parameters. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1280 | uint8_t group_type; |
| 1281 | uint16_t group_id; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1282 | CBS point; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1283 | if (!CBS_get_u8(&server_key_exchange, &group_type) || |
| 1284 | group_type != NAMED_CURVE_TYPE || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1285 | !CBS_get_u16(&server_key_exchange, &group_id) || |
| 1286 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1287 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1288 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1289 | goto f_err; |
| 1290 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1291 | hs->new_session->group_id = group_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1292 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1293 | /* Ensure the group is consistent with preferences. */ |
| 1294 | if (!tls1_check_group_id(ssl, group_id)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1295 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1296 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1297 | goto f_err; |
| 1298 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1299 | |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1300 | /* Initialize ECDH and save the peer public key for later. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1301 | if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) || |
| 1302 | !CBS_stow(&point, &hs->peer_key, &hs->peer_key_len)) { |
David Benjamin | c1cc858 | 2015-12-17 01:22:15 -0500 | [diff] [blame] | 1303 | goto err; |
| 1304 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1305 | } else if (!(alg_k & SSL_kPSK)) { |
| 1306 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1307 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1308 | goto f_err; |
| 1309 | } |
| 1310 | |
| 1311 | /* At this point, |server_key_exchange| contains the signature, if any, while |
| 1312 | * |server_key_exchange_orig| contains the entire message. From that, derive |
| 1313 | * a CBS containing just the parameter. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1314 | CBS parameter; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1315 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1316 | CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange)); |
| 1317 | |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1318 | /* ServerKeyExchange should be signed by the server's public key. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1319 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1320 | uint16_t signature_algorithm = 0; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1321 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1322 | if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1323 | al = SSL_AD_DECODE_ERROR; |
| 1324 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1325 | goto f_err; |
| 1326 | } |
David Benjamin | 887c300 | 2016-07-08 16:15:32 -0700 | [diff] [blame] | 1327 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1328 | goto f_err; |
| 1329 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1330 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1331 | } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1332 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1333 | } else if (hs->peer_pubkey->type == EVP_PKEY_EC) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1334 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
David Benjamin | 49ec9bb | 2016-07-14 00:11:26 -0400 | [diff] [blame] | 1335 | } else { |
| 1336 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1337 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1338 | goto f_err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* The last field in |server_key_exchange| is the signature. */ |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1342 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1343 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1344 | CBS_len(&server_key_exchange) != 0) { |
| 1345 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1346 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1347 | goto f_err; |
| 1348 | } |
| 1349 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1350 | CBB transcript; |
| 1351 | uint8_t *transcript_data; |
| 1352 | size_t transcript_len; |
| 1353 | if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(¶meter)) || |
| 1354 | !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1355 | !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1356 | !CBB_add_bytes(&transcript, CBS_data(¶meter), CBS_len(¶meter)) || |
| 1357 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1358 | CBB_cleanup(&transcript); |
| 1359 | al = SSL_AD_INTERNAL_ERROR; |
| 1360 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1361 | goto f_err; |
| 1362 | } |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1363 | |
| 1364 | int sig_ok = ssl_public_key_verify( |
| 1365 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1366 | hs->peer_pubkey, transcript_data, transcript_len); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1367 | OPENSSL_free(transcript_data); |
| 1368 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1369 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1370 | sig_ok = 1; |
| 1371 | ERR_clear_error(); |
| 1372 | #endif |
| 1373 | if (!sig_ok) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1374 | /* bad signature */ |
| 1375 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1376 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1377 | goto f_err; |
| 1378 | } |
| 1379 | } else { |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1380 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1381 | assert(alg_a == SSL_aPSK); |
| 1382 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1383 | if (CBS_len(&server_key_exchange) > 0) { |
| 1384 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1385 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1386 | goto f_err; |
| 1387 | } |
| 1388 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1389 | return 1; |
| 1390 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1391 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1392 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1393 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1394 | DH_free(dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1395 | EC_POINT_free(srvr_ecpoint); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1396 | EC_KEY_free(ecdh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1397 | return -1; |
| 1398 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1399 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1400 | static int ssl3_get_certificate_request(SSL_HANDSHAKE *hs) { |
| 1401 | SSL *const ssl = hs->ssl; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1402 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1403 | if (msg_ret <= 0) { |
| 1404 | return msg_ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1405 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1406 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1407 | if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1408 | ssl->s3->tmp.reuse_message = 1; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1409 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1410 | * client auth. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1411 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1412 | return 1; |
| 1413 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1414 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1415 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_REQUEST) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1416 | !ssl_hash_current_message(hs)) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1417 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1418 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1419 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1420 | CBS cbs; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1421 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1422 | |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1423 | /* Get the certificate types. */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1424 | CBS certificate_types; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1425 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1426 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1427 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1428 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1429 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1430 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1431 | if (!CBS_stow(&certificate_types, &hs->certificate_types, |
| 1432 | &hs->num_certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1433 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1434 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1435 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1436 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1437 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1438 | CBS supported_signature_algorithms; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1439 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1440 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1441 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1442 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1443 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1444 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1445 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1446 | |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1447 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 1448 | STACK_OF(CRYPTO_BUFFER) *ca_names = |
| 1449 | ssl_parse_client_CA_list(ssl, &alert, &cbs); |
| 1450 | if (ca_names == NULL) { |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1451 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1452 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1453 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1454 | |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1455 | if (CBS_len(&cbs) != 0) { |
| 1456 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 1457 | sk_CRYPTO_BUFFER_pop_free(ca_names, CRYPTO_BUFFER_free); |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1458 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1459 | return -1; |
| 1460 | } |
| 1461 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1462 | hs->cert_request = 1; |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 1463 | sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free); |
| 1464 | hs->ca_names = ca_names; |
| 1465 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
David Benjamin | e0332e8 | 2016-07-13 22:40:36 -0400 | [diff] [blame] | 1466 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1467 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1468 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1469 | static int ssl3_get_server_hello_done(SSL_HANDSHAKE *hs) { |
| 1470 | SSL *const ssl = hs->ssl; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1471 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1472 | if (ret <= 0) { |
| 1473 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1474 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1475 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1476 | if (!ssl_check_message_type(ssl, SSL3_MT_SERVER_HELLO_DONE) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1477 | !ssl_hash_current_message(hs)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1478 | return -1; |
| 1479 | } |
| 1480 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1481 | /* ServerHelloDone is empty. */ |
| 1482 | if (ssl->init_num > 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1483 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1484 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1485 | return -1; |
| 1486 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1487 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1488 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1489 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1490 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1491 | static int ssl3_send_client_certificate(SSL_HANDSHAKE *hs) { |
| 1492 | SSL *const ssl = hs->ssl; |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1493 | /* Call cert_cb to update the certificate. */ |
| 1494 | if (ssl->cert->cert_cb) { |
| 1495 | int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1496 | if (ret < 0) { |
| 1497 | ssl->rwstate = SSL_X509_LOOKUP; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1498 | return -1; |
| 1499 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1500 | if (ret == 0) { |
| 1501 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 1502 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1503 | return -1; |
| 1504 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1505 | } |
| 1506 | |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1507 | if (!ssl_has_certificate(ssl)) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1508 | /* Without a client certificate, the handshake buffer may be released. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1509 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1510 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1511 | /* 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] | 1512 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1513 | if (!ssl->method->add_alert(ssl, SSL3_AL_WARNING, |
| 1514 | SSL_AD_NO_CERTIFICATE)) { |
| 1515 | return -1; |
| 1516 | } |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1517 | return 1; |
| 1518 | } |
| 1519 | } |
| 1520 | |
Adam Langley | 94a62e6 | 2017-02-10 15:24:56 -0800 | [diff] [blame] | 1521 | if (!ssl->ctx->x509_method->ssl_auto_chain_if_needed(ssl) || |
David Benjamin | 650aa1c | 2016-12-20 18:55:16 -0500 | [diff] [blame] | 1522 | !ssl3_output_cert_chain(ssl)) { |
David Benjamin | 5edfc8c | 2016-12-10 15:46:58 -0500 | [diff] [blame] | 1523 | return -1; |
| 1524 | } |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1525 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1526 | } |
| 1527 | |
Brian Smith | 274341d | 2015-10-08 17:10:15 -1000 | [diff] [blame] | 1528 | OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned), |
| 1529 | SIZE_T_IS_SMALLER_THAN_UNSIGNED); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1530 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1531 | static int ssl3_send_client_key_exchange(SSL_HANDSHAKE *hs) { |
| 1532 | SSL *const ssl = hs->ssl; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1533 | uint8_t *pms = NULL; |
| 1534 | size_t pms_len = 0; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1535 | CBB cbb, body; |
| 1536 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1537 | SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1538 | goto err; |
| 1539 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1540 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1541 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1542 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1543 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1544 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1545 | unsigned psk_len = 0; |
| 1546 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1547 | if (alg_a & SSL_aPSK) { |
| 1548 | if (ssl->psk_client_callback == NULL) { |
| 1549 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
| 1550 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1551 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1552 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1553 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1554 | OPENSSL_memset(identity, 0, sizeof(identity)); |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1555 | psk_len = |
| 1556 | ssl->psk_client_callback(ssl, hs->peer_psk_identity_hint, identity, |
| 1557 | sizeof(identity), psk, sizeof(psk)); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1558 | if (psk_len == 0) { |
| 1559 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1560 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1561 | goto err; |
| 1562 | } |
| 1563 | assert(psk_len <= PSK_MAX_PSK_LEN); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1564 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1565 | OPENSSL_free(hs->new_session->psk_identity); |
| 1566 | hs->new_session->psk_identity = BUF_strdup(identity); |
| 1567 | if (hs->new_session->psk_identity == NULL) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1568 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1569 | goto err; |
| 1570 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1571 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1572 | /* Write out psk_identity. */ |
| 1573 | CBB child; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1574 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1575 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1576 | OPENSSL_strnlen(identity, sizeof(identity))) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1577 | !CBB_flush(&body)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1578 | goto err; |
| 1579 | } |
| 1580 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1581 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1582 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1583 | if (alg_k & SSL_kRSA) { |
| 1584 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1585 | pms = OPENSSL_malloc(pms_len); |
| 1586 | if (pms == NULL) { |
| 1587 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1588 | goto err; |
| 1589 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1590 | |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1591 | RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1592 | if (rsa == NULL) { |
| 1593 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1594 | goto err; |
| 1595 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1596 | |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 1597 | pms[0] = hs->client_version >> 8; |
| 1598 | pms[1] = hs->client_version & 0xff; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1599 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1600 | goto err; |
| 1601 | } |
| 1602 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1603 | CBB child, *enc_pms = &body; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1604 | size_t enc_pms_len; |
| 1605 | /* In TLS, there is a length prefix. */ |
| 1606 | if (ssl->version > SSL3_VERSION) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1607 | if (!CBB_add_u16_length_prefixed(&body, &child)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1608 | goto err; |
| 1609 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1610 | enc_pms = &child; |
| 1611 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1612 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1613 | uint8_t *ptr; |
| 1614 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
| 1615 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len, |
| 1616 | RSA_PKCS1_PADDING) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1617 | !CBB_did_write(enc_pms, enc_pms_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1618 | !CBB_flush(&body)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1619 | goto err; |
| 1620 | } |
Matthew Braithwaite | 651aaef | 2016-12-08 16:14:36 -0800 | [diff] [blame] | 1621 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1622 | /* Generate a keypair and serialize the public half. */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1623 | CBB child; |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1624 | if (!SSL_ECDH_CTX_add_key(&hs->ecdh_ctx, &body, &child)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1625 | goto err; |
| 1626 | } |
| 1627 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1628 | /* Compute the premaster. */ |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1629 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1630 | if (!SSL_ECDH_CTX_accept(&hs->ecdh_ctx, &child, &pms, &pms_len, &alert, |
| 1631 | hs->peer_key, hs->peer_key_len)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1632 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1633 | goto err; |
| 1634 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1635 | if (!CBB_flush(&body)) { |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1636 | goto err; |
| 1637 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1638 | |
| 1639 | /* The key exchange state may now be discarded. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1640 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
| 1641 | OPENSSL_free(hs->peer_key); |
| 1642 | hs->peer_key = NULL; |
| 1643 | hs->peer_key_len = 0; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1644 | } else if (alg_k & SSL_kPSK) { |
| 1645 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1646 | * the pre-shared key. */ |
| 1647 | pms_len = psk_len; |
| 1648 | pms = OPENSSL_malloc(pms_len); |
| 1649 | if (pms == NULL) { |
| 1650 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1651 | goto err; |
| 1652 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1653 | OPENSSL_memset(pms, 0, pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1654 | } else { |
| 1655 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1656 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1657 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1658 | } |
| 1659 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1660 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1661 | * key. */ |
| 1662 | if (alg_a & SSL_aPSK) { |
| 1663 | CBB pms_cbb, child; |
| 1664 | uint8_t *new_pms; |
| 1665 | size_t new_pms_len; |
| 1666 | |
| 1667 | CBB_zero(&pms_cbb); |
| 1668 | if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) || |
| 1669 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1670 | !CBB_add_bytes(&child, pms, pms_len) || |
| 1671 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1672 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1673 | !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) { |
| 1674 | CBB_cleanup(&pms_cbb); |
| 1675 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1676 | goto err; |
| 1677 | } |
| 1678 | OPENSSL_cleanse(pms, pms_len); |
| 1679 | OPENSSL_free(pms); |
| 1680 | pms = new_pms; |
| 1681 | pms_len = new_pms_len; |
| 1682 | } |
| 1683 | |
| 1684 | /* The message must be added to the finished hash before calculating the |
| 1685 | * master secret. */ |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1686 | if (!ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1687 | goto err; |
| 1688 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1689 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1690 | hs->new_session->master_key_length = tls1_generate_master_secret( |
| 1691 | hs, hs->new_session->master_key, pms, pms_len); |
| 1692 | if (hs->new_session->master_key_length == 0) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1693 | goto err; |
| 1694 | } |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1695 | hs->new_session->extended_master_secret = hs->extended_master_secret; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1696 | OPENSSL_cleanse(pms, pms_len); |
| 1697 | OPENSSL_free(pms); |
| 1698 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1699 | return 1; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1700 | |
| 1701 | err: |
David Benjamin | 2b07fa4 | 2016-03-02 00:23:57 -0500 | [diff] [blame] | 1702 | CBB_cleanup(&cbb); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1703 | if (pms != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1704 | OPENSSL_cleanse(pms, pms_len); |
| 1705 | OPENSSL_free(pms); |
| 1706 | } |
| 1707 | return -1; |
| 1708 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1709 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1710 | static int ssl3_send_cert_verify(SSL_HANDSHAKE *hs) { |
| 1711 | SSL *const ssl = hs->ssl; |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1712 | assert(ssl_has_private_key(ssl)); |
| 1713 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1714 | CBB cbb, body, child; |
| 1715 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1716 | SSL3_MT_CERTIFICATE_VERIFY)) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1717 | goto err; |
| 1718 | } |
| 1719 | |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1720 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1721 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1722 | goto err; |
| 1723 | } |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1724 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1725 | /* Write out the digest type in TLS 1.2. */ |
| 1726 | if (!CBB_add_u16(&body, signature_algorithm)) { |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1727 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1728 | goto err; |
| 1729 | } |
| 1730 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1731 | |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1732 | /* Set aside space for the signature. */ |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1733 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1734 | uint8_t *ptr; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1735 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1736 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1737 | goto err; |
| 1738 | } |
| 1739 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1740 | size_t sig_len = max_sig_len; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1741 | enum ssl_private_key_result_t sign_result; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1742 | if (hs->state == SSL3_ST_CW_CERT_VRFY_A) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1743 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1744 | * single final digest and signature, and must be special-cased. */ |
| 1745 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 1746 | if (ssl->cert->key_method != NULL) { |
| 1747 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY); |
| 1748 | goto err; |
| 1749 | } |
| 1750 | |
| 1751 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1752 | size_t digest_len; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1753 | if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest, |
| 1754 | &digest_len, hs->new_session, |
| 1755 | signature_algorithm)) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1756 | goto err; |
| 1757 | } |
| 1758 | |
| 1759 | sign_result = ssl_private_key_success; |
| 1760 | |
| 1761 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL); |
| 1762 | if (pctx == NULL || |
| 1763 | !EVP_PKEY_sign_init(pctx) || |
| 1764 | !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) { |
| 1765 | EVP_PKEY_CTX_free(pctx); |
| 1766 | sign_result = ssl_private_key_failure; |
| 1767 | goto err; |
| 1768 | } |
| 1769 | EVP_PKEY_CTX_free(pctx); |
| 1770 | } else { |
| 1771 | sign_result = ssl_private_key_sign( |
| 1772 | ssl, ptr, &sig_len, max_sig_len, signature_algorithm, |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1773 | (const uint8_t *)hs->transcript.buffer->data, |
| 1774 | hs->transcript.buffer->length); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /* The handshake buffer is no longer necessary. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1778 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1779 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1780 | assert(hs->state == SSL3_ST_CW_CERT_VRFY_B); |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 1781 | sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | switch (sign_result) { |
| 1785 | case ssl_private_key_success: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1786 | break; |
| 1787 | case ssl_private_key_failure: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1788 | goto err; |
| 1789 | case ssl_private_key_retry: |
| 1790 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1791 | hs->state = SSL3_ST_CW_CERT_VRFY_B; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1792 | goto err; |
| 1793 | } |
| 1794 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1795 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1796 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1797 | goto err; |
| 1798 | } |
| 1799 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1800 | return 1; |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1801 | |
| 1802 | err: |
| 1803 | CBB_cleanup(&cbb); |
| 1804 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1807 | static int ssl3_send_next_proto(SSL_HANDSHAKE *hs) { |
| 1808 | SSL *const ssl = hs->ssl; |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1809 | static const uint8_t kZero[32] = {0}; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1810 | 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] | 1811 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1812 | CBB cbb, body, child; |
| 1813 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) || |
| 1814 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1815 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1816 | ssl->s3->next_proto_negotiated_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1817 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1818 | !CBB_add_bytes(&child, kZero, padding_len) || |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1819 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1820 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1821 | CBB_cleanup(&cbb); |
| 1822 | return -1; |
| 1823 | } |
| 1824 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1825 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1828 | static int ssl3_send_channel_id(SSL_HANDSHAKE *hs) { |
| 1829 | SSL *const ssl = hs->ssl; |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1830 | if (!ssl_do_channel_id_callback(ssl)) { |
| 1831 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1832 | } |
| 1833 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1834 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1835 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1836 | return -1; |
| 1837 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1838 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1839 | CBB cbb, body; |
David Benjamin | d94b83b | 2016-07-06 13:50:14 -0700 | [diff] [blame] | 1840 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1841 | !tls1_write_channel_id(hs, &body) || |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1842 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1843 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1844 | CBB_cleanup(&cbb); |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1845 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1848 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1849 | } |
| 1850 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1851 | static int ssl3_get_new_session_ticket(SSL_HANDSHAKE *hs) { |
| 1852 | SSL *const ssl = hs->ssl; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1853 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1854 | if (ret <= 0) { |
| 1855 | return ret; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1856 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1857 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1858 | if (!ssl_check_message_type(ssl, SSL3_MT_NEW_SESSION_TICKET) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1859 | !ssl_hash_current_message(hs)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1860 | return -1; |
| 1861 | } |
| 1862 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1863 | CBS new_session_ticket, ticket; |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1864 | uint32_t tlsext_tick_lifetime_hint; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1865 | CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num); |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1866 | if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) || |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1867 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1868 | CBS_len(&new_session_ticket) != 0) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1869 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1870 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1871 | return -1; |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1872 | } |
| 1873 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1874 | if (CBS_len(&ticket) == 0) { |
| 1875 | /* 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] | 1876 | * negotiating the extension. The value of |ticket_expected| is checked in |
| 1877 | * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1878 | hs->ticket_expected = 0; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1879 | return 1; |
| 1880 | } |
| 1881 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1882 | int session_renewed = ssl->session != NULL; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1883 | SSL_SESSION *session = hs->new_session; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1884 | if (session_renewed) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1885 | /* The server is sending a new ticket for an existing session. Sessions are |
| 1886 | * immutable once established, so duplicate all but the ticket of the |
| 1887 | * existing session. */ |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 1888 | session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1889 | if (session == NULL) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1890 | /* This should never happen. */ |
| 1891 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1892 | goto err; |
| 1893 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1896 | /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */ |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 1897 | ssl_session_rebase_time(ssl, session); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1898 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1899 | if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1900 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1901 | goto err; |
| 1902 | } |
Martin Kreichgauer | baafa4a | 2016-08-09 10:18:40 -0700 | [diff] [blame] | 1903 | session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1904 | |
| 1905 | /* Generate a session ID for this session based on the session ticket. We use |
| 1906 | * the session ID mechanism for detecting ticket resumption. This also fits in |
| 1907 | * with assumptions elsewhere in OpenSSL.*/ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1908 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), |
| 1909 | session->session_id, &session->session_id_length, |
| 1910 | EVP_sha256(), NULL)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1911 | goto err; |
| 1912 | } |
| 1913 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1914 | if (session_renewed) { |
| 1915 | session->not_resumable = 0; |
| 1916 | SSL_SESSION_free(ssl->session); |
| 1917 | ssl->session = session; |
| 1918 | } |
| 1919 | |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1920 | return 1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1921 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1922 | err: |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1923 | if (session_renewed) { |
| 1924 | SSL_SESSION_free(session); |
| 1925 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1926 | return -1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 1927 | } |