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