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