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