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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 149 | #include <openssl/ssl.h> |
| 150 | |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 151 | #include <assert.h> |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 152 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
| 154 | #include <openssl/bn.h> |
| 155 | #include <openssl/buf.h> |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 156 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | #include <openssl/cipher.h> |
| 158 | #include <openssl/dh.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | #include <openssl/ec.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> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 166 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | #include <openssl/rand.h> |
| 168 | #include <openssl/x509.h> |
| 169 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 170 | #include "internal.h" |
Adam Langley | af6e45b | 2014-11-03 19:34:49 -0800 | [diff] [blame] | 171 | #include "../crypto/internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 173 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 174 | static int ssl3_get_client_hello(SSL *ssl); |
| 175 | static int ssl3_send_server_hello(SSL *ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 176 | static int ssl3_send_server_certificate(SSL *ssl); |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 177 | static int ssl3_send_certificate_status(SSL *ssl); |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 178 | static int ssl3_send_server_key_exchange(SSL *ssl); |
| 179 | static int ssl3_send_certificate_request(SSL *ssl); |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 180 | static int ssl3_send_server_hello_done(SSL *ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 181 | static int ssl3_get_client_certificate(SSL *ssl); |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 182 | static int ssl3_get_client_key_exchange(SSL *ssl); |
| 183 | static int ssl3_get_cert_verify(SSL *ssl); |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 184 | static int ssl3_get_next_proto(SSL *ssl); |
| 185 | static int ssl3_get_channel_id(SSL *ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 186 | static int ssl3_send_new_session_ticket(SSL *ssl); |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 187 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 188 | int ssl3_accept(SSL *ssl) { |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 189 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 190 | int ret = -1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 191 | int state, skip = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 193 | assert(ssl->handshake_func == ssl3_accept); |
| 194 | assert(ssl->server); |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 195 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 196 | for (;;) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 197 | state = ssl->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 198 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 199 | switch (ssl->state) { |
David Benjamin | fc0c9d9 | 2016-09-01 01:33:33 -0400 | [diff] [blame] | 200 | case SSL_ST_INIT: |
| 201 | ssl->state = SSL_ST_ACCEPT; |
| 202 | skip = 1; |
| 203 | break; |
| 204 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 205 | case SSL_ST_ACCEPT: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 206 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 208 | ssl->s3->hs = ssl_handshake_new(tls13_server_handshake); |
David Benjamin | 481b9d2 | 2016-07-27 14:01:59 -0400 | [diff] [blame] | 209 | if (ssl->s3->hs == NULL) { |
David Benjamin | 97718f1 | 2016-07-08 09:16:50 -0700 | [diff] [blame] | 210 | ret = -1; |
| 211 | goto end; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 212 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 213 | |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 214 | /* Enable a write buffer. This groups handshake messages within a flight |
| 215 | * into a single write. */ |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 216 | if (!ssl_init_wbio_buffer(ssl)) { |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 217 | ret = -1; |
| 218 | goto end; |
| 219 | } |
| 220 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 221 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 222 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 223 | ret = -1; |
| 224 | goto end; |
| 225 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 226 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 227 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 229 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 230 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 231 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 232 | case SSL3_ST_SR_CLNT_HELLO_C: |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 233 | case SSL3_ST_SR_CLNT_HELLO_D: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 234 | ret = ssl3_get_client_hello(ssl); |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 235 | if (ssl->state == SSL_ST_TLS13) { |
| 236 | break; |
| 237 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 238 | if (ret <= 0) { |
| 239 | goto end; |
| 240 | } |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 241 | ssl->method->received_flight(ssl); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 242 | ssl->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 243 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 244 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 245 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 246 | case SSL3_ST_SW_SRVR_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 247 | ret = ssl3_send_server_hello(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 248 | if (ret <= 0) { |
| 249 | goto end; |
| 250 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 251 | if (ssl->session != NULL) { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 252 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 253 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 254 | ssl->state = SSL3_ST_SW_CERT_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 255 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 256 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | case SSL3_ST_SW_CERT_A: |
| 259 | case SSL3_ST_SW_CERT_B: |
David Benjamin | 3d458dc | 2016-09-12 22:40:27 +0000 | [diff] [blame] | 260 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 261 | ret = ssl3_send_server_certificate(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 262 | if (ret <= 0) { |
| 263 | goto end; |
| 264 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 265 | } else { |
| 266 | skip = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 267 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 268 | ssl->state = SSL3_ST_SW_CERT_STATUS_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 271 | case SSL3_ST_SW_CERT_STATUS_A: |
| 272 | case SSL3_ST_SW_CERT_STATUS_B: |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 273 | if (ssl->s3->hs->certificate_status_expected) { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 274 | ret = ssl3_send_certificate_status(ssl); |
| 275 | if (ret <= 0) { |
| 276 | goto end; |
| 277 | } |
| 278 | } else { |
| 279 | skip = 1; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 280 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 281 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 282 | break; |
| 283 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | case SSL3_ST_SW_KEY_EXCH_A: |
| 285 | case SSL3_ST_SW_KEY_EXCH_B: |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 286 | case SSL3_ST_SW_KEY_EXCH_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 287 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 289 | /* PSK ciphers send ServerKeyExchange if there is an identity hint. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 290 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) || |
| 291 | ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) { |
| 292 | ret = ssl3_send_server_key_exchange(ssl); |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 293 | if (ret <= 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 294 | goto end; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 295 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 296 | } else { |
| 297 | skip = 1; |
| 298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 300 | ssl->state = SSL3_ST_SW_CERT_REQ_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 301 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 303 | case SSL3_ST_SW_CERT_REQ_A: |
| 304 | case SSL3_ST_SW_CERT_REQ_B: |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 305 | if (ssl->s3->hs->cert_request) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 306 | ret = ssl3_send_certificate_request(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | if (ret <= 0) { |
| 308 | goto end; |
| 309 | } |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 310 | } else { |
| 311 | skip = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 312 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 313 | ssl->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 314 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 316 | case SSL3_ST_SW_SRVR_DONE_A: |
| 317 | case SSL3_ST_SW_SRVR_DONE_B: |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 318 | ret = ssl3_send_server_hello_done(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 319 | if (ret <= 0) { |
| 320 | goto end; |
| 321 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 322 | ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 323 | ssl->state = SSL3_ST_SW_FLUSH; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 324 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 326 | case SSL3_ST_SR_CERT_A: |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 327 | if (ssl->s3->hs->cert_request) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 328 | ret = ssl3_get_client_certificate(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 329 | if (ret <= 0) { |
| 330 | goto end; |
| 331 | } |
| 332 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 333 | ssl->state = SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 334 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 336 | case SSL3_ST_SR_KEY_EXCH_A: |
| 337 | case SSL3_ST_SR_KEY_EXCH_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 338 | ret = ssl3_get_client_key_exchange(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 339 | if (ret <= 0) { |
| 340 | goto end; |
| 341 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 342 | ssl->state = SSL3_ST_SR_CERT_VRFY_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 343 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 345 | case SSL3_ST_SR_CERT_VRFY_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 346 | ret = ssl3_get_cert_verify(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 347 | if (ret <= 0) { |
| 348 | goto end; |
| 349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 351 | ssl->state = SSL3_ST_SR_CHANGE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 352 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 354 | case SSL3_ST_SR_CHANGE: |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 355 | ret = ssl->method->read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 356 | if (ret <= 0) { |
| 357 | goto end; |
| 358 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 359 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 360 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 361 | ret = -1; |
| 362 | goto end; |
| 363 | } |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 364 | |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 365 | ssl->state = SSL3_ST_SR_NEXT_PROTO_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 366 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 368 | case SSL3_ST_SR_NEXT_PROTO_A: |
David Benjamin | b74b081 | 2016-10-06 19:43:48 -0400 | [diff] [blame] | 369 | if (ssl->s3->hs->next_proto_neg_seen) { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 370 | ret = ssl3_get_next_proto(ssl); |
| 371 | if (ret <= 0) { |
| 372 | goto end; |
| 373 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 374 | } else { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 375 | skip = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 376 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 377 | ssl->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 378 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 380 | case SSL3_ST_SR_CHANNEL_ID_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 381 | if (ssl->s3->tlsext_channel_id_valid) { |
| 382 | ret = ssl3_get_channel_id(ssl); |
| 383 | if (ret <= 0) { |
| 384 | goto end; |
| 385 | } |
| 386 | } else { |
| 387 | skip = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 388 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 389 | ssl->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 390 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 392 | case SSL3_ST_SR_FINISHED_A: |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 393 | ret = ssl3_get_finished(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 394 | if (ret <= 0) { |
| 395 | goto end; |
| 396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 398 | ssl->method->received_flight(ssl); |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 399 | if (ssl->session != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 400 | ssl->state = SSL_ST_OK; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 401 | } else { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 402 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 403 | } |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 404 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 405 | /* If this is a full handshake with ChannelID then record the handshake |
| 406 | * hashes in |ssl->s3->new_session| in case we need them to verify a |
| 407 | * ChannelID signature on a resumption of this session in the future. */ |
| 408 | if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 409 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 410 | if (ret <= 0) { |
| 411 | goto end; |
| 412 | } |
| 413 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 414 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 416 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 417 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 418 | if (ssl->tlsext_ticket_expected) { |
| 419 | ret = ssl3_send_new_session_ticket(ssl); |
| 420 | if (ret <= 0) { |
| 421 | goto end; |
| 422 | } |
| 423 | } else { |
| 424 | skip = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 425 | } |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 426 | ssl->state = SSL3_ST_SW_CHANGE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 427 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 428 | |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 429 | case SSL3_ST_SW_CHANGE: |
| 430 | ret = ssl->method->send_change_cipher_spec(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 431 | if (ret <= 0) { |
| 432 | goto end; |
| 433 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 434 | ssl->state = SSL3_ST_SW_FINISHED_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 436 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 437 | ret = -1; |
| 438 | goto end; |
| 439 | } |
| 440 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 442 | case SSL3_ST_SW_FINISHED_A: |
| 443 | case SSL3_ST_SW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 444 | ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 445 | SSL3_ST_SW_FINISHED_B); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 446 | if (ret <= 0) { |
| 447 | goto end; |
| 448 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 449 | ssl->state = SSL3_ST_SW_FLUSH; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 450 | if (ssl->session != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 451 | ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 452 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 453 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 455 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 457 | case SSL3_ST_SW_FLUSH: |
| 458 | if (BIO_flush(ssl->wbio) <= 0) { |
| 459 | ssl->rwstate = SSL_WRITING; |
| 460 | ret = -1; |
| 461 | goto end; |
| 462 | } |
| 463 | |
| 464 | ssl->state = ssl->s3->tmp.next_state; |
| 465 | if (ssl->state != SSL_ST_OK) { |
| 466 | ssl->method->expect_flight(ssl); |
| 467 | } |
| 468 | break; |
| 469 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 470 | case SSL_ST_TLS13: |
| 471 | ret = tls13_handshake(ssl); |
| 472 | if (ret <= 0) { |
| 473 | goto end; |
| 474 | } |
| 475 | ssl->state = SSL_ST_OK; |
| 476 | break; |
| 477 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 478 | case SSL_ST_OK: |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 479 | /* Clean a few things up. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 480 | ssl3_cleanup_key_block(ssl); |
David Benjamin | 4497e58 | 2016-07-27 17:51:49 -0400 | [diff] [blame] | 481 | ssl->method->release_current_message(ssl, 1 /* free_buffer */); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 483 | /* If we aren't retaining peer certificates then we can discard it |
| 484 | * now. */ |
| 485 | if (ssl->s3->new_session != NULL && |
| 486 | ssl->ctx->retain_only_sha256_of_client_certs) { |
| 487 | X509_free(ssl->s3->new_session->peer); |
| 488 | ssl->s3->new_session->peer = NULL; |
| 489 | sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free); |
| 490 | ssl->s3->new_session->cert_chain = NULL; |
| 491 | } |
| 492 | |
| 493 | SSL_SESSION_free(ssl->s3->established_session); |
| 494 | if (ssl->session != NULL) { |
David Benjamin | b919540 | 2016-08-05 10:51:43 -0400 | [diff] [blame] | 495 | SSL_SESSION_up_ref(ssl->session); |
| 496 | ssl->s3->established_session = ssl->session; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 497 | } else { |
| 498 | ssl->s3->established_session = ssl->s3->new_session; |
| 499 | ssl->s3->established_session->not_resumable = 0; |
| 500 | ssl->s3->new_session = NULL; |
| 501 | } |
| 502 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 503 | /* remove buffering on output */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 504 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Steven Valdez | 143e8b3 | 2016-07-11 13:19:03 -0400 | [diff] [blame] | 506 | ssl_handshake_free(ssl->s3->hs); |
| 507 | ssl->s3->hs = NULL; |
| 508 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 509 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 511 | ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 513 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 515 | ret = 1; |
| 516 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 518 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 519 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 520 | ret = -1; |
| 521 | goto end; |
| 522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 524 | if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) { |
| 525 | int new_state = ssl->state; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 526 | ssl->state = state; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 527 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 528 | ssl->state = new_state; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 529 | } |
| 530 | skip = 0; |
| 531 | } |
| 532 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 533 | end: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 534 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 535 | return ret; |
| 536 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 538 | int ssl_client_cipher_list_contains_cipher( |
| 539 | const struct ssl_early_callback_ctx *client_hello, uint16_t id) { |
| 540 | CBS cipher_suites; |
| 541 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 542 | client_hello->cipher_suites_len); |
| 543 | |
| 544 | while (CBS_len(&cipher_suites) > 0) { |
| 545 | uint16_t got_id; |
| 546 | if (!CBS_get_u16(&cipher_suites, &got_id)) { |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | if (got_id == id) { |
| 551 | return 1; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 558 | static int negotiate_version( |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 559 | SSL *ssl, uint8_t *out_alert, |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 560 | const struct ssl_early_callback_ctx *client_hello) { |
| 561 | uint16_t min_version, max_version; |
| 562 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 563 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 564 | return 0; |
| 565 | } |
| 566 | |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 567 | uint16_t version = 0; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 568 | /* Check supported_versions extension if it is present. */ |
| 569 | CBS supported_versions; |
| 570 | if (ssl_early_callback_get_extension(client_hello, &supported_versions, |
| 571 | TLSEXT_TYPE_supported_versions)) { |
| 572 | CBS versions; |
| 573 | if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || |
| 574 | CBS_len(&supported_versions) != 0 || |
| 575 | CBS_len(&versions) == 0) { |
| 576 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 577 | *out_alert = SSL_AD_DECODE_ERROR; |
| 578 | return 0; |
| 579 | } |
| 580 | |
Brian Smith | f85d323 | 2016-10-28 10:34:06 -1000 | [diff] [blame] | 581 | /* Choose the newest commonly-supported version advertised by the client. |
| 582 | * The client orders the versions according to its preferences, but we're |
| 583 | * not required to honor the client's preferences. */ |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 584 | int found_version = 0; |
| 585 | while (CBS_len(&versions) != 0) { |
| 586 | uint16_t ext_version; |
| 587 | if (!CBS_get_u16(&versions, &ext_version)) { |
| 588 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 589 | *out_alert = SSL_AD_DECODE_ERROR; |
| 590 | return 0; |
| 591 | } |
| 592 | if (!ssl->method->version_from_wire(&ext_version, ext_version)) { |
| 593 | continue; |
| 594 | } |
| 595 | if (min_version <= ext_version && |
Brian Smith | f85d323 | 2016-10-28 10:34:06 -1000 | [diff] [blame] | 596 | ext_version <= max_version && |
| 597 | (!found_version || version < ext_version)) { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 598 | version = ext_version; |
| 599 | found_version = 1; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!found_version) { |
| 604 | goto unsupported_protocol; |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 605 | } |
| 606 | } else { |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 607 | /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2 |
| 608 | * do not use this mechanism. */ |
| 609 | if (SSL_is_dtls(ssl)) { |
| 610 | if (client_hello->version <= DTLS1_2_VERSION) { |
| 611 | version = TLS1_2_VERSION; |
| 612 | } else if (client_hello->version <= DTLS1_VERSION) { |
| 613 | version = TLS1_1_VERSION; |
| 614 | } else { |
| 615 | goto unsupported_protocol; |
| 616 | } |
| 617 | } else { |
| 618 | if (client_hello->version >= TLS1_2_VERSION) { |
| 619 | version = TLS1_2_VERSION; |
| 620 | } else if (client_hello->version >= TLS1_1_VERSION) { |
| 621 | version = TLS1_1_VERSION; |
| 622 | } else if (client_hello->version >= TLS1_VERSION) { |
| 623 | version = TLS1_VERSION; |
| 624 | } else if (client_hello->version >= SSL3_VERSION) { |
| 625 | version = SSL3_VERSION; |
| 626 | } else { |
| 627 | goto unsupported_protocol; |
| 628 | } |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 629 | } |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 630 | |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 631 | /* Apply our minimum and maximum version. */ |
| 632 | if (version > max_version) { |
| 633 | version = max_version; |
| 634 | } |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 635 | |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 636 | if (version < min_version) { |
| 637 | goto unsupported_protocol; |
| 638 | } |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* Handle FALLBACK_SCSV. */ |
| 642 | if (ssl_client_cipher_list_contains_cipher(client_hello, |
| 643 | SSL3_CK_FALLBACK_SCSV & 0xffff) && |
| 644 | version < max_version) { |
| 645 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
| 646 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 647 | return 0; |
| 648 | } |
| 649 | |
David Benjamin | 2dc0204 | 2016-09-19 19:57:37 -0400 | [diff] [blame] | 650 | ssl->client_version = client_hello->version; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 651 | ssl->version = ssl->method->version_to_wire(version); |
| 652 | ssl->s3->enc_method = ssl3_get_enc_method(version); |
| 653 | assert(ssl->s3->enc_method != NULL); |
| 654 | |
| 655 | /* At this point, the connection's version is known and |ssl->version| is |
| 656 | * fixed. Begin enforcing the record-layer version. */ |
| 657 | ssl->s3->have_version = 1; |
| 658 | |
| 659 | return 1; |
Steven Valdez | fdd1099 | 2016-09-15 16:27:05 -0400 | [diff] [blame] | 660 | |
| 661 | unsupported_protocol: |
| 662 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 663 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 664 | return 0; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 665 | } |
| 666 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 667 | static int ssl3_get_client_hello(SSL *ssl) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 668 | uint8_t al = SSL_AD_INTERNAL_ERROR; |
| 669 | int ret = -1; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 670 | SSL_SESSION *session = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 671 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 672 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) { |
| 673 | /* The first time around, read the ClientHello. */ |
| 674 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO, |
| 675 | ssl_hash_message); |
| 676 | if (msg_ret <= 0) { |
| 677 | return msg_ret; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 678 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 679 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 680 | ssl->state = SSL3_ST_SR_CLNT_HELLO_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 681 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 683 | struct ssl_early_callback_ctx client_hello; |
| 684 | if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg, |
| 685 | ssl->init_num)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 686 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 687 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 688 | goto f_err; |
| 689 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 690 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 691 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) { |
| 692 | /* Unlike other callbacks, the early callback is not run a second time if |
| 693 | * paused. */ |
| 694 | ssl->state = SSL3_ST_SR_CLNT_HELLO_C; |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 695 | |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 696 | /* Run the early callback. */ |
| 697 | if (ssl->ctx->select_certificate_cb != NULL) { |
| 698 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
| 699 | case 0: |
| 700 | ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 701 | goto err; |
| 702 | |
| 703 | case -1: |
| 704 | /* Connection rejected. */ |
David Benjamin | 2c66e07 | 2016-09-16 15:58:00 -0400 | [diff] [blame] | 705 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | e14ff06 | 2016-08-09 16:21:24 -0400 | [diff] [blame] | 706 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 707 | goto f_err; |
| 708 | |
| 709 | default: |
| 710 | /* fallthrough */; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 715 | /* Negotiate the protocol version if we have not done so yet. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 716 | if (!ssl->s3->have_version) { |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 717 | if (!negotiate_version(ssl, &al, &client_hello)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 718 | goto f_err; |
| 719 | } |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 720 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 721 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 722 | ssl->state = SSL_ST_TLS13; |
| 723 | return 1; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) { |
| 728 | /* Load the client random. */ |
| 729 | if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
| 730 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 731 | return -1; |
| 732 | } |
| 733 | memcpy(ssl->s3->client_random, client_hello.random, |
| 734 | client_hello.random_len); |
| 735 | |
| 736 | /* Determine whether we are doing session resumption. */ |
| 737 | int send_new_ticket = 0; |
| 738 | switch ( |
| 739 | ssl_get_prev_session(ssl, &session, &send_new_ticket, &client_hello)) { |
| 740 | case ssl_session_success: |
| 741 | break; |
| 742 | case ssl_session_error: |
| 743 | goto err; |
| 744 | case ssl_session_retry: |
| 745 | ssl->rwstate = SSL_PENDING_SESSION; |
| 746 | goto err; |
| 747 | } |
| 748 | ssl->tlsext_ticket_expected = send_new_ticket; |
| 749 | |
| 750 | /* The EMS state is needed when making the resumption decision, but |
| 751 | * extensions are not normally parsed until later. This detects the EMS |
| 752 | * extension for the resumption decision and it's checked against the result |
| 753 | * of the normal parse later in this function. */ |
| 754 | CBS ems; |
| 755 | int have_extended_master_secret = |
| 756 | ssl->version != SSL3_VERSION && |
| 757 | ssl_early_callback_get_extension(&client_hello, &ems, |
| 758 | TLSEXT_TYPE_extended_master_secret) && |
| 759 | CBS_len(&ems) == 0; |
| 760 | |
| 761 | int has_session = 0; |
| 762 | if (session != NULL) { |
| 763 | if (session->extended_master_secret && |
| 764 | !have_extended_master_secret) { |
| 765 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 766 | * is fatal to the connection. */ |
| 767 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 768 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
| 769 | goto f_err; |
| 770 | } |
| 771 | |
| 772 | has_session = |
| 773 | /* Only resume if the session's version matches the negotiated |
| 774 | * version: most clients do not accept a mismatch. */ |
| 775 | ssl->version == session->ssl_version && |
| 776 | /* If the client offers the EMS extension, but the previous session |
| 777 | * didn't use it, then negotiate a new session. */ |
Steven Valdez | b6b6ff3 | 2016-10-26 11:56:35 -0400 | [diff] [blame] | 778 | have_extended_master_secret == session->extended_master_secret && |
| 779 | /* Only resume if the session's cipher is still valid under the |
| 780 | * current configuration. */ |
| 781 | ssl_is_valid_cipher(ssl, session->cipher); |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | if (has_session) { |
| 785 | /* Use the old session. */ |
| 786 | ssl->session = session; |
| 787 | session = NULL; |
Steven Valdez | 4aa154e | 2016-07-29 14:32:55 -0400 | [diff] [blame] | 788 | ssl->s3->session_reused = 1; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 789 | } else { |
David Benjamin | 4d0be24 | 2016-09-01 01:10:07 -0400 | [diff] [blame] | 790 | ssl_set_session(ssl, NULL); |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 791 | if (!ssl_get_new_session(ssl, 1 /* server */)) { |
| 792 | goto err; |
| 793 | } |
| 794 | |
| 795 | /* Clear the session ID if we want the session to be single-use. */ |
| 796 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
| 797 | ssl->s3->new_session->session_id_length = 0; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | if (ssl->ctx->dos_protection_cb != NULL && |
| 802 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 803 | /* Connection rejected for DOS reasons. */ |
David Benjamin | 2c66e07 | 2016-09-16 15:58:00 -0400 | [diff] [blame] | 804 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 805 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
David Benjamin | 1deb41b | 2016-08-09 19:36:38 -0400 | [diff] [blame] | 806 | goto f_err; |
| 807 | } |
| 808 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 809 | /* Only null compression is supported. */ |
| 810 | if (memchr(client_hello.compression_methods, 0, |
| 811 | client_hello.compression_methods_len) == NULL) { |
| 812 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 813 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED); |
| 814 | goto f_err; |
| 815 | } |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 816 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 817 | /* TLS extensions. */ |
| 818 | if (!ssl_parse_clienthello_tlsext(ssl, &client_hello)) { |
| 819 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 820 | goto err; |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 821 | } |
David Benjamin | 4b30b28 | 2015-05-26 16:37:58 -0400 | [diff] [blame] | 822 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 823 | if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) { |
| 824 | al = SSL_AD_INTERNAL_ERROR; |
| 825 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 826 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 827 | } |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 828 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 829 | ssl->state = SSL3_ST_SR_CLNT_HELLO_D; |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 830 | } |
| 831 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 832 | /* Determine the remaining connection parameters. This is a separate state so |
| 833 | * |cert_cb| does not cause earlier logic to run multiple times. */ |
| 834 | assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_D); |
| 835 | |
| 836 | if (ssl->session != NULL) { |
| 837 | /* Check that the cipher is in the list. */ |
| 838 | if (!ssl_client_cipher_list_contains_cipher( |
| 839 | &client_hello, (uint16_t)ssl->session->cipher->id)) { |
| 840 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 841 | OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING); |
| 842 | goto f_err; |
| 843 | } |
| 844 | |
| 845 | ssl->s3->tmp.new_cipher = ssl->session->cipher; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 846 | } else { |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 847 | /* Call |cert_cb| to update server certificates if required. */ |
| 848 | if (ssl->cert->cert_cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 849 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 850 | if (rv == 0) { |
| 851 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 852 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 853 | goto f_err; |
| 854 | } |
| 855 | if (rv < 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 856 | ssl->rwstate = SSL_X509_LOOKUP; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 857 | goto err; |
| 858 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 859 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 860 | |
David Benjamin | 25fe85b | 2016-08-09 20:00:32 -0400 | [diff] [blame] | 861 | const SSL_CIPHER *c = |
| 862 | ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 863 | if (c == NULL) { |
| 864 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 865 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 866 | goto f_err; |
| 867 | } |
David Benjamin | fddbadc | 2016-08-09 19:53:00 -0400 | [diff] [blame] | 868 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 869 | ssl->s3->new_session->cipher = c; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 870 | ssl->s3->tmp.new_cipher = c; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 871 | |
| 872 | /* Determine whether to request a client certificate. */ |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 873 | ssl->s3->hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER); |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 874 | /* Only request a certificate if Channel ID isn't negotiated. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 875 | if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 876 | ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 877 | ssl->s3->hs->cert_request = 0; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 878 | } |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 879 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
David Benjamin | 3d458dc | 2016-09-12 22:40:27 +0000 | [diff] [blame] | 880 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 881 | ssl->s3->hs->cert_request = 0; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 882 | } |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 883 | |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 884 | if (!ssl->s3->hs->cert_request) { |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 885 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 886 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 887 | ssl->s3->new_session->verify_result = X509_V_OK; |
| 888 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 889 | } |
| 890 | |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 891 | /* Resolve ALPN after the cipher suite is selected. HTTP/2 negotiation depends |
| 892 | * on the cipher suite. */ |
| 893 | if (!ssl_negotiate_alpn(ssl, &al, &client_hello)) { |
| 894 | goto f_err; |
| 895 | } |
| 896 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 897 | /* Now that the cipher is known, initialize the handshake hash. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 898 | if (!ssl3_init_handshake_hash(ssl)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 899 | goto f_err; |
| 900 | } |
| 901 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 902 | /* Release the handshake buffer if client authentication isn't required. */ |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 903 | if (!ssl->s3->hs->cert_request) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 904 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 905 | } |
| 906 | |
David Benjamin | 0bd71eb | 2015-12-01 14:14:40 -0500 | [diff] [blame] | 907 | ret = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 908 | |
| 909 | if (0) { |
| 910 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 911 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 912 | } |
| 913 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 914 | err: |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 915 | SSL_SESSION_free(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 916 | return ret; |
| 917 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 918 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 919 | static int ssl3_send_server_hello(SSL *ssl) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 920 | if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 921 | return ssl->method->write_message(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 922 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 923 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 924 | assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A); |
| 925 | |
| 926 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 927 | * known attack while we fix ChannelID itself. */ |
| 928 | if (ssl->s3->tlsext_channel_id_valid && |
| 929 | (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 930 | ssl->s3->tlsext_channel_id_valid = 0; |
| 931 | } |
| 932 | |
| 933 | /* If this is a resumption and the original handshake didn't support |
| 934 | * ChannelID then we didn't record the original handshake hashes in the |
| 935 | * session and so cannot resume with ChannelIDs. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 936 | if (ssl->session != NULL && |
| 937 | ssl->session->original_handshake_hash_len == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 938 | ssl->s3->tlsext_channel_id_valid = 0; |
| 939 | } |
| 940 | |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 941 | struct timeval now; |
| 942 | ssl_get_current_time(ssl, &now); |
| 943 | ssl->s3->server_random[0] = now.tv_sec >> 24; |
| 944 | ssl->s3->server_random[1] = now.tv_sec >> 16; |
| 945 | ssl->s3->server_random[2] = now.tv_sec >> 8; |
| 946 | ssl->s3->server_random[3] = now.tv_sec; |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 947 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 948 | return -1; |
| 949 | } |
| 950 | |
David Benjamin | 5510863 | 2016-08-11 22:01:18 -0400 | [diff] [blame] | 951 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 952 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 953 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 954 | const SSL_SESSION *session = ssl->s3->new_session; |
| 955 | if (ssl->session != NULL) { |
| 956 | session = ssl->session; |
| 957 | } |
| 958 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 959 | CBB cbb, body, session_id; |
| 960 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) || |
| 961 | !CBB_add_u16(&body, ssl->version) || |
| 962 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 963 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 964 | !CBB_add_bytes(&session_id, session->session_id, |
| 965 | session->session_id_length) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 966 | !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) || |
| 967 | !CBB_add_u8(&body, 0 /* no compression */) || |
| 968 | !ssl_add_serverhello_tlsext(ssl, &body) || |
| 969 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 970 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 971 | CBB_cleanup(&cbb); |
| 972 | return -1; |
| 973 | } |
| 974 | |
| 975 | ssl->state = SSL3_ST_SW_SRVR_HELLO_B; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 976 | return ssl->method->write_message(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 977 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 978 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 979 | static int ssl3_send_server_certificate(SSL *ssl) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 980 | if (ssl->state == SSL3_ST_SW_CERT_B) { |
| 981 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 982 | } |
| 983 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 984 | if (!ssl_has_certificate(ssl)) { |
| 985 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
| 986 | return 0; |
| 987 | } |
| 988 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 989 | if (!ssl3_output_cert_chain(ssl)) { |
| 990 | return 0; |
| 991 | } |
| 992 | ssl->state = SSL3_ST_SW_CERT_B; |
| 993 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 994 | } |
| 995 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 996 | static int ssl3_send_certificate_status(SSL *ssl) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 997 | if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) { |
| 998 | return ssl->method->write_message(ssl); |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1001 | CBB cbb, body, ocsp_response; |
| 1002 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1003 | SSL3_MT_CERTIFICATE_STATUS) || |
| 1004 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 1005 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
| 1006 | !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 1007 | ssl->ctx->ocsp_response_length) || |
| 1008 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1009 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1010 | CBB_cleanup(&cbb); |
| 1011 | return -1; |
| 1012 | } |
| 1013 | |
| 1014 | ssl->state = SSL3_ST_SW_CERT_STATUS_B; |
| 1015 | return ssl->method->write_message(ssl); |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1016 | } |
| 1017 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 1018 | static int ssl3_send_server_key_exchange(SSL *ssl) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1019 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1020 | return ssl->method->write_message(ssl); |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1023 | CBB cbb, child; |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1024 | CBB_zero(&cbb); |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1025 | |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1026 | /* Put together the parameters. */ |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1027 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1028 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1029 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1031 | /* Pre-allocate enough room to comfortably fit an ECDHE public key. */ |
| 1032 | if (!CBB_init(&cbb, 128)) { |
| 1033 | goto err; |
| 1034 | } |
| 1035 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1036 | /* PSK ciphers begin with an identity hint. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1037 | if (alg_a & SSL_aPSK) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1038 | size_t len = |
| 1039 | (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint); |
| 1040 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1041 | !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint, |
| 1042 | len)) { |
| 1043 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1044 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1045 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1046 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1047 | if (alg_k & SSL_kDHE) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1048 | /* Determine the group to use. */ |
| 1049 | DH *params = ssl->cert->dh_tmp; |
| 1050 | if (params == NULL && ssl->cert->dh_tmp_cb != NULL) { |
| 1051 | params = ssl->cert->dh_tmp_cb(ssl, 0, 1024); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1052 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1053 | if (params == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1054 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1055 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1056 | goto err; |
| 1057 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1058 | ssl->s3->new_session->key_exchange_info = DH_num_bits(params); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1059 | |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1060 | /* Set up DH, generate a key, and emit the public half. */ |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1061 | DH *dh = DHparams_dup(params); |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1062 | if (dh == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1063 | goto err; |
| 1064 | } |
David Benjamin | 9f226a5 | 2015-04-26 22:12:32 -0400 | [diff] [blame] | 1065 | |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1066 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->hs->ecdh_ctx, dh); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1067 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1068 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) || |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1069 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1070 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1071 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1072 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &child)) { |
David Benjamin | 9f226a5 | 2015-04-26 22:12:32 -0400 | [diff] [blame] | 1073 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1074 | } |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1075 | } else if (alg_k & SSL_kECDHE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1076 | /* Determine the group to use. */ |
| 1077 | uint16_t group_id; |
| 1078 | if (!tls1_get_shared_group(ssl, &group_id)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1079 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1080 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1081 | goto err; |
| 1082 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1083 | ssl->s3->new_session->key_exchange_info = group_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1084 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1085 | /* Set up ECDH, generate a key, and emit the public half. */ |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1086 | if (!SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) || |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1087 | !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1088 | !CBB_add_u16(&cbb, group_id) || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1089 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1090 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &child)) { |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1091 | goto err; |
| 1092 | } |
| 1093 | } else if (alg_k & SSL_kCECPQ1) { |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1094 | SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->hs->ecdh_ctx); |
David Benjamin | 18a3518 | 2016-06-30 13:46:24 -0400 | [diff] [blame] | 1095 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1096 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &child)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1097 | goto err; |
| 1098 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1099 | } else { |
| 1100 | assert(alg_k & SSL_kPSK); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1101 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1102 | |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1103 | if (!CBB_finish(&cbb, &ssl->s3->hs->server_params, |
| 1104 | &ssl->s3->hs->server_params_len)) { |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1105 | goto err; |
| 1106 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* Assemble the message. */ |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1110 | CBB body; |
| 1111 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1112 | SSL3_MT_SERVER_KEY_EXCHANGE) || |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1113 | !CBB_add_bytes(&body, ssl->s3->hs->server_params, |
| 1114 | ssl->s3->hs->server_params_len)) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1115 | goto err; |
| 1116 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1117 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1118 | /* Add a signature. */ |
David Benjamin | 3d458dc | 2016-09-12 22:40:27 +0000 | [diff] [blame] | 1119 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1120 | if (!ssl_has_private_key(ssl)) { |
| 1121 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1122 | goto err; |
| 1123 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1124 | |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1125 | /* Determine the signature algorithm. */ |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1126 | uint16_t signature_algorithm; |
| 1127 | if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) { |
| 1128 | goto err; |
| 1129 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1130 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1131 | if (!CBB_add_u16(&body, signature_algorithm)) { |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1132 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1133 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1134 | goto err; |
| 1135 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* Add space for the signature. */ |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1139 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1140 | uint8_t *ptr; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1141 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1142 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1143 | goto err; |
| 1144 | } |
| 1145 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1146 | size_t sig_len; |
| 1147 | enum ssl_private_key_result_t sign_result; |
| 1148 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1149 | CBB transcript; |
| 1150 | uint8_t *transcript_data; |
| 1151 | size_t transcript_len; |
| 1152 | if (!CBB_init(&transcript, |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1153 | 2*SSL3_RANDOM_SIZE + ssl->s3->hs->server_params_len) || |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1154 | !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1155 | !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1156 | !CBB_add_bytes(&transcript, ssl->s3->hs->server_params, |
| 1157 | ssl->s3->hs->server_params_len) || |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1158 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1159 | CBB_cleanup(&transcript); |
| 1160 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1161 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1162 | goto err; |
| 1163 | } |
| 1164 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1165 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1166 | signature_algorithm, transcript_data, |
| 1167 | transcript_len); |
| 1168 | OPENSSL_free(transcript_data); |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1169 | } else { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1170 | assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B); |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 1171 | sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1172 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1173 | |
| 1174 | switch (sign_result) { |
| 1175 | case ssl_private_key_success: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1176 | if (!CBB_did_write(&child, sig_len)) { |
| 1177 | goto err; |
| 1178 | } |
| 1179 | break; |
| 1180 | case ssl_private_key_failure: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1181 | goto err; |
| 1182 | case ssl_private_key_retry: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1183 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1184 | ssl->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1185 | goto err; |
| 1186 | } |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1189 | if (!ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1190 | goto err; |
| 1191 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1192 | |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1193 | OPENSSL_free(ssl->s3->hs->server_params); |
| 1194 | ssl->s3->hs->server_params = NULL; |
| 1195 | ssl->s3->hs->server_params_len = 0; |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1196 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1197 | ssl->state = SSL3_ST_SW_KEY_EXCH_C; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1198 | return ssl->method->write_message(ssl); |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1199 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1200 | err: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1201 | CBB_cleanup(&cbb); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1202 | return -1; |
| 1203 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1204 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1205 | static int add_cert_types(SSL *ssl, CBB *cbb) { |
| 1206 | /* Get configured signature algorithms. */ |
| 1207 | int have_rsa_sign = 0; |
| 1208 | int have_ecdsa_sign = 0; |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1209 | const uint16_t *sig_algs; |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 1210 | size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs); |
| 1211 | for (size_t i = 0; i < num_sig_algs; i++) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1212 | switch (sig_algs[i]) { |
| 1213 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 1214 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 1215 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 1216 | case SSL_SIGN_RSA_PKCS1_SHA1: |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1217 | have_rsa_sign = 1; |
| 1218 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1219 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1220 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 1221 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 1222 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 1223 | case SSL_SIGN_ECDSA_SHA1: |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1224 | have_ecdsa_sign = 1; |
| 1225 | break; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1226 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1227 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1228 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1229 | if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) { |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | /* ECDSA certs can be used with RSA cipher suites as well so we don't need to |
| 1234 | * check for SSL_kECDH or SSL_kECDHE. */ |
| 1235 | if (ssl->version >= TLS1_VERSION && have_ecdsa_sign && |
| 1236 | !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) { |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | return 1; |
| 1241 | } |
| 1242 | |
| 1243 | static int ssl3_send_certificate_request(SSL *ssl) { |
| 1244 | if (ssl->state == SSL3_ST_SW_CERT_REQ_B) { |
| 1245 | return ssl->method->write_message(ssl); |
| 1246 | } |
| 1247 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1248 | CBB cbb, body, cert_types, sigalgs_cbb; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1249 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1250 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 1251 | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
| 1252 | !add_cert_types(ssl, &cert_types)) { |
| 1253 | goto err; |
| 1254 | } |
| 1255 | |
| 1256 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1257 | const uint16_t *sigalgs; |
David Benjamin | 3ef7697 | 2016-10-17 17:59:54 -0400 | [diff] [blame] | 1258 | size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs); |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1259 | if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1260 | goto err; |
| 1261 | } |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1262 | |
David Benjamin | 0fc37ef | 2016-08-17 15:29:46 -0400 | [diff] [blame] | 1263 | for (size_t i = 0; i < num_sigalgs; i++) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1264 | if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) { |
| 1265 | goto err; |
| 1266 | } |
| 1267 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1270 | if (!ssl_add_client_CA_list(ssl, &body) || |
| 1271 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1272 | goto err; |
| 1273 | } |
| 1274 | |
| 1275 | ssl->state = SSL3_ST_SW_CERT_REQ_B; |
| 1276 | return ssl->method->write_message(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1277 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1278 | err: |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1279 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1280 | CBB_cleanup(&cbb); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1281 | return -1; |
| 1282 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1283 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1284 | static int ssl3_send_server_hello_done(SSL *ssl) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1285 | if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) { |
| 1286 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1287 | } |
| 1288 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1289 | CBB cbb, body; |
| 1290 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) || |
| 1291 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1292 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1293 | CBB_cleanup(&cbb); |
| 1294 | return -1; |
| 1295 | } |
| 1296 | |
| 1297 | ssl->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1298 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | static int ssl3_get_client_certificate(SSL *ssl) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 1302 | assert(ssl->s3->hs->cert_request); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1303 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1304 | int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 1305 | if (msg_ret <= 0) { |
| 1306 | return msg_ret; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 1310 | if (ssl->version == SSL3_VERSION && |
| 1311 | ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1312 | /* In SSL 3.0, the Certificate message is omitted to signal no |
| 1313 | * certificate. */ |
David Benjamin | da2630c | 2016-08-01 19:57:43 -0400 | [diff] [blame] | 1314 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1315 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1316 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1317 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1318 | } |
| 1319 | |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1320 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 1321 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1322 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1323 | ssl->s3->tmp.reuse_message = 1; |
| 1324 | return 1; |
| 1325 | } |
| 1326 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1327 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1328 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1329 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1330 | } |
| 1331 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1332 | CBS certificate_msg; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1333 | CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1334 | uint8_t alert; |
| 1335 | STACK_OF(X509) *chain = ssl_parse_cert_chain( |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1336 | ssl, &alert, ssl->ctx->retain_only_sha256_of_client_certs |
| 1337 | ? ssl->s3->new_session->peer_sha256 |
| 1338 | : NULL, |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1339 | &certificate_msg); |
| 1340 | if (chain == NULL) { |
| 1341 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1342 | goto err; |
| 1343 | } |
| 1344 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1345 | if (CBS_len(&certificate_msg) != 0) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1346 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1347 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1348 | goto err; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1349 | } |
| 1350 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1351 | if (sk_X509_num(chain) == 0) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1352 | /* No client certificate so the handshake buffer may be discarded. */ |
| 1353 | ssl3_free_handshake_buffer(ssl); |
| 1354 | |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 1355 | /* In SSL 3.0, sending no certificate is signaled by omitting the |
| 1356 | * Certificate message. */ |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1357 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1358 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1359 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1360 | goto err; |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1364 | /* Fail for TLS only if we required a certificate */ |
| 1365 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1366 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1367 | goto err; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1368 | } |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1369 | |
| 1370 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 1371 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
David Benjamin | 7aa31d6 | 2016-08-08 21:38:32 -0400 | [diff] [blame] | 1372 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1373 | } else { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1374 | /* The hash would have been filled in. */ |
| 1375 | if (ssl->ctx->retain_only_sha256_of_client_certs) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1376 | ssl->s3->new_session->peer_sha256_valid = 1; |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1377 | } |
| 1378 | |
David Benjamin | 7aa31d6 | 2016-08-08 21:38:32 -0400 | [diff] [blame] | 1379 | if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result, |
| 1380 | chain)) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1381 | goto err; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1385 | X509_free(ssl->s3->new_session->peer); |
| 1386 | ssl->s3->new_session->peer = sk_X509_shift(chain); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1387 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1388 | sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free); |
| 1389 | ssl->s3->new_session->cert_chain = chain; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1390 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 1391 | * certificate, while we do include it in s3_clnt.c */ |
| 1392 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1393 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1394 | |
| 1395 | err: |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1396 | sk_X509_pop_free(chain, X509_free); |
| 1397 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1398 | } |
| 1399 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 1400 | static int ssl3_get_client_key_exchange(SSL *ssl) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1401 | int al; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1402 | CBS client_key_exchange; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 1403 | uint32_t alg_k; |
| 1404 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1405 | uint8_t *premaster_secret = NULL; |
| 1406 | size_t premaster_secret_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1407 | uint8_t *decrypt_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1408 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1409 | unsigned psk_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1410 | uint8_t psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1411 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1412 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1413 | int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1414 | ssl_hash_message); |
| 1415 | if (ret <= 0) { |
| 1416 | return ret; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1417 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1418 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1419 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1420 | CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num); |
| 1421 | alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1422 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1423 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1424 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1425 | if (alg_a & SSL_aPSK) { |
| 1426 | CBS psk_identity; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1427 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1428 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1429 | * then this is the only field in the message. */ |
| 1430 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1431 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1432 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1433 | al = SSL_AD_DECODE_ERROR; |
| 1434 | goto f_err; |
| 1435 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1436 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1437 | if (ssl->psk_server_callback == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1438 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1439 | al = SSL_AD_INTERNAL_ERROR; |
| 1440 | goto f_err; |
| 1441 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1442 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1443 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1444 | CBS_contains_zero_byte(&psk_identity)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1445 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1446 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1447 | goto f_err; |
| 1448 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1449 | |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1450 | if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1451 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1452 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1453 | goto f_err; |
| 1454 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1455 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1456 | /* Look up the key for the identity. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1457 | psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity, |
| 1458 | psk, sizeof(psk)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1459 | if (psk_len > PSK_MAX_PSK_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1460 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1461 | al = SSL_AD_INTERNAL_ERROR; |
| 1462 | goto f_err; |
| 1463 | } else if (psk_len == 0) { |
| 1464 | /* PSK related to the given identity not found */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1465 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1466 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1467 | goto f_err; |
| 1468 | } |
| 1469 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1470 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1471 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1472 | * |premaster_secret_len|. */ |
| 1473 | if (alg_k & SSL_kRSA) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1474 | /* Allocate a buffer large enough for an RSA decryption. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1475 | const size_t rsa_size = ssl_private_key_max_signature_len(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1476 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1477 | if (decrypt_buf == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1478 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1479 | goto err; |
| 1480 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1481 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1482 | enum ssl_private_key_result_t decrypt_result; |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1483 | size_t decrypt_len; |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1484 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1485 | if (!ssl_has_private_key(ssl) || |
David Benjamin | 0c0b7e1 | 2016-07-14 13:47:55 -0400 | [diff] [blame] | 1486 | ssl_private_key_type(ssl) != NID_rsaEncryption) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1487 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1488 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1489 | goto f_err; |
| 1490 | } |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1491 | CBS encrypted_premaster_secret; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1492 | if (ssl->version > SSL3_VERSION) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1493 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1494 | &encrypted_premaster_secret) || |
| 1495 | CBS_len(&client_key_exchange) != 0) { |
David Benjamin | ef5e515 | 2015-11-18 20:35:31 -0500 | [diff] [blame] | 1496 | al = SSL_AD_DECODE_ERROR; |
| 1497 | OPENSSL_PUT_ERROR(SSL, |
| 1498 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1499 | goto f_err; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1500 | } |
| 1501 | } else { |
| 1502 | encrypted_premaster_secret = client_key_exchange; |
| 1503 | } |
| 1504 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1505 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1506 | * timing-sensitive code below. */ |
| 1507 | decrypt_result = ssl_private_key_decrypt( |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1508 | ssl, decrypt_buf, &decrypt_len, rsa_size, |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1509 | CBS_data(&encrypted_premaster_secret), |
| 1510 | CBS_len(&encrypted_premaster_secret)); |
| 1511 | } else { |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1512 | assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1513 | /* Complete async decrypt. */ |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 1514 | decrypt_result = |
| 1515 | ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1516 | } |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1517 | |
| 1518 | switch (decrypt_result) { |
| 1519 | case ssl_private_key_success: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1520 | break; |
| 1521 | case ssl_private_key_failure: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1522 | goto err; |
| 1523 | case ssl_private_key_retry: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1524 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1525 | ssl->state = SSL3_ST_SR_KEY_EXCH_B; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1526 | goto err; |
| 1527 | } |
| 1528 | |
Daniel Bathgate | 4365c3f | 2016-04-14 17:18:02 -0400 | [diff] [blame] | 1529 | if (decrypt_len != rsa_size) { |
| 1530 | al = SSL_AD_DECRYPT_ERROR; |
| 1531 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1532 | goto f_err; |
| 1533 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1534 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1535 | /* Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1536 | * section 7.4.7.1. */ |
| 1537 | premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1538 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1539 | if (premaster_secret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1540 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1541 | goto err; |
| 1542 | } |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1543 | if (!RAND_bytes(premaster_secret, premaster_secret_len)) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1544 | goto err; |
| 1545 | } |
| 1546 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1547 | /* The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1548 | * publicly invalid. */ |
| 1549 | if (decrypt_len < 11 + premaster_secret_len) { |
| 1550 | al = SSL_AD_DECRYPT_ERROR; |
| 1551 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1552 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1553 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1554 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1555 | /* Check the padding. See RFC 3447, section 7.2.2. */ |
| 1556 | size_t padding_len = decrypt_len - premaster_secret_len; |
| 1557 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1558 | constant_time_eq_int_8(decrypt_buf[1], 2); |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1559 | for (size_t i = 2; i < padding_len - 1; i++) { |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1560 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1561 | } |
| 1562 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1563 | |
| 1564 | /* The premaster secret must begin with |client_version|. This too must be |
| 1565 | * checked in constant time (http://eprint.iacr.org/2003/052/). */ |
| 1566 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1567 | (unsigned)(ssl->client_version >> 8)); |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1568 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1569 | (unsigned)(ssl->client_version & 0xff)); |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1570 | |
| 1571 | /* Select, in constant time, either the decrypted premaster or the random |
| 1572 | * premaster based on |good|. */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1573 | for (size_t i = 0; i < premaster_secret_len; i++) { |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1574 | premaster_secret[i] = constant_time_select_8( |
| 1575 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1576 | } |
| 1577 | |
| 1578 | OPENSSL_free(decrypt_buf); |
| 1579 | decrypt_buf = NULL; |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1580 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1581 | /* Parse the ClientKeyExchange. */ |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1582 | CBS peer_key; |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1583 | if (!SSL_ECDH_CTX_get_key(&ssl->s3->hs->ecdh_ctx, &client_key_exchange, |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1584 | &peer_key) || |
| 1585 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1586 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1587 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1588 | goto f_err; |
| 1589 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1590 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1591 | /* Compute the premaster. */ |
| 1592 | uint8_t alert; |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1593 | if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, &premaster_secret, |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1594 | &premaster_secret_len, &alert, CBS_data(&peer_key), |
| 1595 | CBS_len(&peer_key))) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1596 | al = alert; |
| 1597 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1598 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1599 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1600 | /* The key exchange state may now be discarded. */ |
David Benjamin | a4c8ff0 | 2016-10-08 02:49:01 -0400 | [diff] [blame] | 1601 | SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1602 | } else if (alg_k & SSL_kPSK) { |
| 1603 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1604 | * pre-shared key. */ |
| 1605 | premaster_secret_len = psk_len; |
| 1606 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1607 | if (premaster_secret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1608 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1609 | goto err; |
| 1610 | } |
| 1611 | memset(premaster_secret, 0, premaster_secret_len); |
| 1612 | } else { |
| 1613 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1614 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1615 | goto f_err; |
| 1616 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1617 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1618 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1619 | * pre-shared key. */ |
| 1620 | if (alg_a & SSL_aPSK) { |
| 1621 | CBB new_premaster, child; |
| 1622 | uint8_t *new_data; |
| 1623 | size_t new_len; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1624 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 1625 | CBB_zero(&new_premaster); |
| 1626 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1627 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1628 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1629 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1630 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1631 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1632 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1633 | CBB_cleanup(&new_premaster); |
| 1634 | goto err; |
| 1635 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1636 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1637 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1638 | OPENSSL_free(premaster_secret); |
| 1639 | premaster_secret = new_data; |
| 1640 | premaster_secret_len = new_len; |
| 1641 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1642 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1643 | /* Compute the master secret */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1644 | ssl->s3->new_session->master_key_length = tls1_generate_master_secret( |
| 1645 | ssl, ssl->s3->new_session->master_key, premaster_secret, |
| 1646 | premaster_secret_len); |
| 1647 | if (ssl->s3->new_session->master_key_length == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1648 | goto err; |
| 1649 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1650 | ssl->s3->new_session->extended_master_secret = |
| 1651 | ssl->s3->tmp.extended_master_secret; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1652 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1653 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1654 | OPENSSL_free(premaster_secret); |
| 1655 | return 1; |
| 1656 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1657 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1658 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1659 | err: |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1660 | if (premaster_secret != NULL) { |
| 1661 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1662 | OPENSSL_free(premaster_secret); |
| 1663 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1664 | OPENSSL_free(decrypt_buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1665 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1666 | return -1; |
| 1667 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1668 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 1669 | static int ssl3_get_cert_verify(SSL *ssl) { |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1670 | int al, ret = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1671 | CBS certificate_verify, signature; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1672 | X509 *peer = ssl->s3->new_session->peer; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1673 | EVP_PKEY *pkey = NULL; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 1674 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1675 | /* Only RSA and ECDSA client certificates are supported, so a |
| 1676 | * CertificateVerify is required if and only if there's a client certificate. |
| 1677 | * */ |
| 1678 | if (peer == NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1679 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1680 | return 1; |
| 1681 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1682 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1683 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY, |
| 1684 | ssl_dont_hash_message); |
| 1685 | if (msg_ret <= 0) { |
| 1686 | return msg_ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1687 | } |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 1688 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1689 | /* Filter out unsupported certificate types. */ |
| 1690 | pkey = X509_get_pubkey(peer); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1691 | if (pkey == NULL) { |
| 1692 | goto err; |
| 1693 | } |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 1694 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1695 | CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num); |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 1696 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1697 | /* Determine the digest type if needbe. */ |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1698 | uint16_t signature_algorithm = 0; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1699 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1700 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1701 | al = SSL_AD_DECODE_ERROR; |
| 1702 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1703 | goto f_err; |
| 1704 | } |
David Benjamin | 887c300 | 2016-07-08 16:15:32 -0700 | [diff] [blame] | 1705 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1706 | goto f_err; |
| 1707 | } |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1708 | ssl->s3->tmp.peer_signature_algorithm = signature_algorithm; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1709 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1710 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 1711 | } else if (pkey->type == EVP_PKEY_EC) { |
| 1712 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
David Benjamin | 49ec9bb | 2016-07-14 00:11:26 -0400 | [diff] [blame] | 1713 | } else { |
| 1714 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1715 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1716 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1717 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1718 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1719 | /* Parse and verify the signature. */ |
| 1720 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1721 | CBS_len(&certificate_verify) != 0) { |
| 1722 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1723 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1724 | goto f_err; |
| 1725 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1726 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1727 | int sig_ok; |
| 1728 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1729 | * single final digest and signature, and must be special-cased. */ |
| 1730 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
David Benjamin | 0aa25bd | 2016-07-08 14:44:56 -0700 | [diff] [blame] | 1731 | const EVP_MD *md; |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1732 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1733 | size_t digest_len; |
David Benjamin | 0aa25bd | 2016-07-08 14:44:56 -0700 | [diff] [blame] | 1734 | if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len, |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1735 | signature_algorithm)) { |
| 1736 | goto err; |
| 1737 | } |
| 1738 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1739 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 1740 | sig_ok = pctx != NULL && |
| 1741 | EVP_PKEY_verify_init(pctx) && |
| 1742 | EVP_PKEY_CTX_set_signature_md(pctx, md) && |
| 1743 | EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 1744 | digest, digest_len); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1745 | EVP_PKEY_CTX_free(pctx); |
| 1746 | } else { |
| 1747 | sig_ok = ssl_public_key_verify( |
| 1748 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
| 1749 | pkey, (const uint8_t *)ssl->s3->handshake_buffer->data, |
| 1750 | ssl->s3->handshake_buffer->length); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1751 | } |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1752 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1753 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1754 | sig_ok = 1; |
| 1755 | ERR_clear_error(); |
| 1756 | #endif |
| 1757 | if (!sig_ok) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1758 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1759 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1760 | goto f_err; |
| 1761 | } |
| 1762 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1763 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 1764 | * message.*/ |
| 1765 | ssl3_free_handshake_buffer(ssl); |
David Benjamin | 528bd26 | 2016-07-08 09:34:05 -0700 | [diff] [blame] | 1766 | if (!ssl->method->hash_current_message(ssl)) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1767 | goto err; |
| 1768 | } |
| 1769 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1770 | ret = 1; |
| 1771 | |
| 1772 | if (0) { |
| 1773 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1774 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1775 | } |
| 1776 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1777 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1778 | EVP_PKEY_free(pkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1779 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1780 | return ret; |
| 1781 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1782 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1783 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 1784 | * sets the next_proto member in s if found */ |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 1785 | static int ssl3_get_next_proto(SSL *ssl) { |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1786 | int ret = |
| 1787 | ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message); |
| 1788 | if (ret <= 0) { |
| 1789 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1790 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1791 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1792 | CBS next_protocol, selected_protocol, padding; |
| 1793 | CBS_init(&next_protocol, ssl->init_msg, ssl->init_num); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1794 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1795 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1796 | CBS_len(&next_protocol) != 0) { |
| 1797 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1798 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated, |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1803 | &ssl->s3->next_proto_negotiated_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1804 | return 0; |
| 1805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1807 | return 1; |
| 1808 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1809 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1810 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame] | 1811 | static int ssl3_get_channel_id(SSL *ssl) { |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1812 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID, |
| 1813 | ssl_dont_hash_message); |
| 1814 | if (msg_ret <= 0) { |
| 1815 | return msg_ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1816 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1817 | |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1818 | if (!tls1_verify_channel_id(ssl) || |
| 1819 | !ssl->method->hash_current_message(ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1820 | return -1; |
| 1821 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1822 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1823 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1824 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1825 | static int ssl3_send_new_session_ticket(SSL *ssl) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1826 | if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) { |
| 1827 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1828 | } |
| 1829 | |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame^] | 1830 | const SSL_SESSION *session; |
| 1831 | SSL_SESSION *session_copy = NULL; |
| 1832 | if (ssl->session == NULL) { |
| 1833 | /* Fix the timeout to measure from the ticket issuance time. */ |
| 1834 | ssl_session_refresh_time(ssl, ssl->s3->new_session); |
| 1835 | session = ssl->s3->new_session; |
| 1836 | } else { |
| 1837 | /* We are renewing an existing session. Duplicate the session to adjust the |
| 1838 | * timeout. */ |
| 1839 | session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
| 1840 | if (session_copy == NULL) { |
| 1841 | return -1; |
| 1842 | } |
| 1843 | |
| 1844 | ssl_session_refresh_time(ssl, session_copy); |
| 1845 | session = session_copy; |
| 1846 | } |
| 1847 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1848 | CBB cbb, body, ticket; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame^] | 1849 | int ok = |
| 1850 | ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) && |
| 1851 | CBB_add_u32(&body, session->timeout) && |
| 1852 | CBB_add_u16_length_prefixed(&body, &ticket) && |
| 1853 | ssl_encrypt_ticket(ssl, &ticket, session) && |
| 1854 | ssl->method->finish_message(ssl, &cbb); |
| 1855 | |
| 1856 | SSL_SESSION_free(session_copy); |
| 1857 | CBB_cleanup(&cbb); |
| 1858 | |
| 1859 | if (!ok) { |
David Benjamin | e75cc27 | 2016-11-03 18:06:27 -0400 | [diff] [blame] | 1860 | return -1; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | ssl->state = SSL3_ST_SW_SESSION_TICKET_B; |
Steven Valdez | 1e6f11a | 2016-07-27 11:10:52 -0400 | [diff] [blame] | 1864 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1865 | } |