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