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