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