Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. |
| 148 | */ |
| 149 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 150 | #include <openssl/ssl.h> |
| 151 | |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame] | 152 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 153 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 155 | #include <openssl/bn.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 157 | #include <openssl/bytestring.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 158 | #include <openssl/dh.h> |
| 159 | #include <openssl/ec_key.h> |
| 160 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 161 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | #include <openssl/evp.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | #include <openssl/md5.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 164 | #include <openssl/mem.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 165 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | #include <openssl/x509.h> |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 167 | #include <openssl/x509v3.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 169 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | #include "../crypto/dh/internal.h" |
| 171 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 173 | static int ssl3_send_client_hello(SSL *ssl); |
| 174 | static int dtls1_get_hello_verify(SSL *ssl); |
| 175 | static int ssl3_get_server_hello(SSL *ssl); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 176 | static int ssl3_get_server_certificate(SSL *ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 177 | static int ssl3_get_cert_status(SSL *ssl); |
| 178 | static int ssl3_verify_server_cert(SSL *ssl); |
| 179 | static int ssl3_get_server_key_exchange(SSL *ssl); |
| 180 | static int ssl3_get_certificate_request(SSL *ssl); |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 181 | static int ssl3_get_server_hello_done(SSL *ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 182 | static int ssl3_send_client_certificate(SSL *ssl); |
| 183 | static int ssl3_send_client_key_exchange(SSL *ssl); |
| 184 | static int ssl3_send_cert_verify(SSL *ssl); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 185 | static int ssl3_send_next_proto(SSL *ssl); |
| 186 | static int ssl3_send_channel_id(SSL *ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 187 | static int ssl3_get_new_session_ticket(SSL *ssl); |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 188 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 189 | int ssl3_connect(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 190 | BUF_MEM *buf = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 191 | int ret = -1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 192 | int state, skip = 0; |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 193 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 194 | assert(ssl->handshake_func == ssl3_connect); |
| 195 | assert(!ssl->server); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 196 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -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 | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 201 | case SSL_ST_CONNECT: |
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 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 204 | if (ssl->init_buf == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 205 | buf = BUF_MEM_new(); |
| 206 | if (buf == NULL || |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 207 | !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 208 | ret = -1; |
| 209 | goto end; |
| 210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 212 | ssl->init_buf = buf; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 213 | buf = NULL; |
| 214 | } |
David Benjamin | 1d64afd | 2016-05-15 13:46:07 -0400 | [diff] [blame] | 215 | ssl->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 217 | if (!ssl_init_wbio_buffer(ssl)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 218 | ret = -1; |
| 219 | goto end; |
| 220 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 222 | ssl->state = SSL3_ST_CW_CLNT_HELLO_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 223 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 224 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 225 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 226 | case SSL3_ST_CW_CLNT_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 227 | ret = ssl3_send_client_hello(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 228 | if (ret <= 0) { |
| 229 | goto end; |
| 230 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 231 | |
| 232 | if (!SSL_IS_DTLS(ssl) || ssl->d1->send_cookie) { |
| 233 | ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A; |
| 234 | } else { |
| 235 | ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; |
| 236 | } |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 237 | ssl->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 238 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 240 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: |
| 241 | assert(SSL_IS_DTLS(ssl)); |
| 242 | ret = dtls1_get_hello_verify(ssl); |
| 243 | if (ret <= 0) { |
| 244 | goto end; |
| 245 | } |
| 246 | if (ssl->d1->send_cookie) { |
| 247 | ssl->method->received_flight(ssl); |
| 248 | ssl->state = SSL3_ST_CW_CLNT_HELLO_A; |
| 249 | } else { |
| 250 | ssl->state = SSL3_ST_CR_SRVR_HELLO_A; |
| 251 | } |
| 252 | break; |
| 253 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 254 | case SSL3_ST_CR_SRVR_HELLO_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 255 | ret = ssl3_get_server_hello(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 256 | if (ret <= 0) { |
| 257 | goto end; |
| 258 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 260 | if (ssl->hit) { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 261 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 262 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 263 | ssl->state = SSL3_ST_CR_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 264 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 265 | break; |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 266 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 267 | case SSL3_ST_CR_CERT_A: |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 268 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 269 | ret = ssl3_get_server_certificate(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 270 | if (ret <= 0) { |
| 271 | goto end; |
| 272 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 273 | } else { |
| 274 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 275 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 276 | ssl->state = SSL3_ST_CR_CERT_STATUS_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 277 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 279 | case SSL3_ST_CR_CERT_STATUS_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 280 | if (ssl->s3->tmp.certificate_status_expected) { |
| 281 | ret = ssl3_get_cert_status(ssl); |
| 282 | if (ret <= 0) { |
| 283 | goto end; |
| 284 | } |
| 285 | } else { |
| 286 | skip = 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 287 | } |
| 288 | ssl->state = SSL3_ST_VERIFY_SERVER_CERT; |
| 289 | break; |
| 290 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 291 | case SSL3_ST_VERIFY_SERVER_CERT: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 292 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
| 293 | ret = ssl3_verify_server_cert(ssl); |
| 294 | if (ret <= 0) { |
| 295 | goto end; |
| 296 | } |
| 297 | } else { |
| 298 | skip = 1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 299 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 300 | ssl->state = SSL3_ST_CR_KEY_EXCH_A; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 301 | break; |
| 302 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 303 | case SSL3_ST_CR_KEY_EXCH_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 304 | ret = ssl3_get_server_key_exchange(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 305 | if (ret <= 0) { |
| 306 | goto end; |
| 307 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 308 | ssl->state = SSL3_ST_CR_CERT_REQ_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 309 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 311 | case SSL3_ST_CR_CERT_REQ_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 312 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
| 313 | ret = ssl3_get_certificate_request(ssl); |
| 314 | if (ret <= 0) { |
| 315 | goto end; |
| 316 | } |
| 317 | } else { |
| 318 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 319 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 320 | ssl->state = SSL3_ST_CR_SRVR_DONE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 321 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 323 | case SSL3_ST_CR_SRVR_DONE_A: |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 324 | ret = ssl3_get_server_hello_done(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 325 | if (ret <= 0) { |
| 326 | goto end; |
| 327 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 328 | ssl->method->received_flight(ssl); |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 329 | ssl->state = SSL3_ST_CW_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 330 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 332 | case SSL3_ST_CW_CERT_A: |
| 333 | case SSL3_ST_CW_CERT_B: |
| 334 | case SSL3_ST_CW_CERT_C: |
| 335 | case SSL3_ST_CW_CERT_D: |
David Benjamin | 5744ca6 | 2016-06-13 14:05:19 -0400 | [diff] [blame] | 336 | if (ssl->s3->tmp.cert_request) { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 337 | ret = ssl3_send_client_certificate(ssl); |
| 338 | if (ret <= 0) { |
| 339 | goto end; |
| 340 | } |
| 341 | } else { |
| 342 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 343 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 344 | ssl->state = SSL3_ST_CW_KEY_EXCH_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 345 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 346 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 347 | case SSL3_ST_CW_KEY_EXCH_A: |
| 348 | case SSL3_ST_CW_KEY_EXCH_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 349 | ret = ssl3_send_client_key_exchange(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 350 | if (ret <= 0) { |
| 351 | goto end; |
| 352 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 353 | ssl->state = SSL3_ST_CW_CERT_VRFY_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 354 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 356 | case SSL3_ST_CW_CERT_VRFY_A: |
| 357 | case SSL3_ST_CW_CERT_VRFY_B: |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 358 | case SSL3_ST_CW_CERT_VRFY_C: |
David Benjamin | 5744ca6 | 2016-06-13 14:05:19 -0400 | [diff] [blame] | 359 | if (ssl->s3->tmp.cert_request) { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 360 | ret = ssl3_send_cert_verify(ssl); |
| 361 | if (ret <= 0) { |
| 362 | goto end; |
| 363 | } |
| 364 | } else { |
| 365 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 366 | } |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 367 | ssl->state = SSL3_ST_CW_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 368 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 370 | case SSL3_ST_CW_CHANGE: |
| 371 | ret = ssl->method->send_change_cipher_spec(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 372 | if (ret <= 0) { |
| 373 | goto end; |
| 374 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 376 | ssl->state = SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 378 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 379 | ret = -1; |
| 380 | goto end; |
| 381 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 382 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 383 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 384 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 385 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 386 | case SSL3_ST_CW_NEXT_PROTO_B: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 387 | if (ssl->s3->next_proto_neg_seen) { |
| 388 | ret = ssl3_send_next_proto(ssl); |
| 389 | if (ret <= 0) { |
| 390 | goto end; |
| 391 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 392 | } else { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 393 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 394 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 395 | ssl->state = SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 396 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 398 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 399 | case SSL3_ST_CW_CHANNEL_ID_B: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 400 | if (ssl->s3->tlsext_channel_id_valid) { |
| 401 | ret = ssl3_send_channel_id(ssl); |
| 402 | if (ret <= 0) { |
| 403 | goto end; |
| 404 | } |
| 405 | } else { |
| 406 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 407 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 408 | ssl->state = SSL3_ST_CW_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 409 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 411 | case SSL3_ST_CW_FINISHED_A: |
| 412 | case SSL3_ST_CW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 413 | ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 414 | SSL3_ST_CW_FINISHED_B); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 415 | if (ret <= 0) { |
| 416 | goto end; |
| 417 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 418 | ssl->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 419 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 420 | if (ssl->hit) { |
| 421 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 422 | } else { |
| 423 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 424 | * record the handshake hashes at this point in the session so that |
| 425 | * any resumption of this session with ChannelID can sign those |
| 426 | * hashes. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 427 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 428 | if (ret <= 0) { |
| 429 | goto end; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 430 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 431 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 432 | ssl3_can_false_start(ssl) && |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 433 | /* No False Start on renegotiation (would complicate the state |
| 434 | * machine). */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 435 | !ssl->s3->initial_handshake_complete) { |
| 436 | ssl->s3->tmp.next_state = SSL3_ST_FALSE_START; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 437 | } else { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 438 | ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 441 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 443 | case SSL3_ST_FALSE_START: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 444 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 445 | ssl->s3->tmp.in_false_start = 1; |
| 446 | |
| 447 | ssl_free_wbio_buffer(ssl); |
| 448 | ret = 1; |
| 449 | goto end; |
| 450 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 451 | case SSL3_ST_CR_SESSION_TICKET_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame] | 452 | if (ssl->tlsext_ticket_expected) { |
| 453 | ret = ssl3_get_new_session_ticket(ssl); |
| 454 | if (ret <= 0) { |
| 455 | goto end; |
| 456 | } |
| 457 | } else { |
| 458 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 459 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 460 | ssl->state = SSL3_ST_CR_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 461 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 463 | case SSL3_ST_CR_CHANGE: |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 464 | ret = ssl->method->read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 465 | if (ret <= 0) { |
| 466 | goto end; |
| 467 | } |
| 468 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 469 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 470 | ret = -1; |
| 471 | goto end; |
| 472 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 473 | ssl->state = SSL3_ST_CR_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 474 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 476 | case SSL3_ST_CR_FINISHED_A: |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 477 | ret = ssl3_get_finished(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 478 | if (ret <= 0) { |
| 479 | goto end; |
| 480 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 481 | ssl->method->received_flight(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 483 | if (ssl->hit) { |
David Benjamin | 352d0a9 | 2016-06-28 11:22:02 -0400 | [diff] [blame] | 484 | ssl->state = SSL3_ST_CW_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 485 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 486 | ssl->state = SSL_ST_OK; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 487 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 488 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 490 | case SSL3_ST_CW_FLUSH: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 491 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 492 | ssl->rwstate = SSL_WRITING; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 493 | ret = -1; |
| 494 | goto end; |
| 495 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 496 | ssl->state = ssl->s3->tmp.next_state; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 497 | if (ssl->state != SSL_ST_OK) { |
| 498 | ssl->method->expect_flight(ssl); |
| 499 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 500 | break; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 502 | case SSL_ST_OK: |
| 503 | /* clean a few things up */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 504 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
David Benjamin | f182ee1 | 2016-06-17 18:59:24 -0400 | [diff] [blame] | 506 | BUF_MEM_free(ssl->init_buf); |
| 507 | ssl->init_buf = NULL; |
| 508 | ssl->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 510 | /* Remove write buffering now. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 511 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 513 | const int is_initial_handshake = !ssl->s3->initial_handshake_complete; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 514 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 515 | ssl->s3->tmp.in_false_start = 0; |
| 516 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 518 | if (is_initial_handshake) { |
| 519 | /* Renegotiations do not participate in session resumption. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 520 | ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 521 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 523 | if (SSL_IS_DTLS(ssl)) { |
| 524 | ssl->d1->handshake_read_seq = 0; |
David Benjamin | 778f57e | 2016-06-17 19:16:23 -0400 | [diff] [blame] | 525 | ssl->d1->handshake_write_seq = 0; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 528 | ret = 1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 529 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 530 | goto end; |
| 531 | |
| 532 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 533 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 534 | ret = -1; |
| 535 | goto end; |
| 536 | } |
| 537 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 538 | if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) { |
| 539 | int new_state = ssl->state; |
| 540 | ssl->state = state; |
| 541 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1); |
| 542 | ssl->state = new_state; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 543 | } |
| 544 | skip = 0; |
| 545 | } |
| 546 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 547 | end: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 548 | BUF_MEM_free(buf); |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 549 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 550 | return ret; |
| 551 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | |
David Benjamin | 929d4ee | 2016-06-24 23:55:58 -0400 | [diff] [blame] | 553 | static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out, |
| 554 | uint16_t min_version, |
| 555 | uint16_t max_version) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 556 | /* Prepare disabled cipher masks. */ |
| 557 | ssl_set_client_disabled(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 559 | CBB child; |
| 560 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 561 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 562 | } |
| 563 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 564 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
| 565 | |
| 566 | int any_enabled = 0; |
| 567 | size_t i; |
| 568 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 569 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 570 | /* Skip disabled ciphers */ |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 571 | if ((cipher->algorithm_mkey & ssl->cert->mask_k) || |
| 572 | (cipher->algorithm_auth & ssl->cert->mask_a)) { |
| 573 | continue; |
| 574 | } |
David Benjamin | 929d4ee | 2016-06-24 23:55:58 -0400 | [diff] [blame] | 575 | if (SSL_CIPHER_get_min_version(cipher) > max_version || |
| 576 | SSL_CIPHER_get_max_version(cipher) < min_version) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 577 | continue; |
| 578 | } |
| 579 | any_enabled = 1; |
| 580 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 581 | return 0; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* If all ciphers were disabled, return the error to the caller. */ |
| 586 | if (!any_enabled) { |
| 587 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 592 | * added. */ |
| 593 | if (ssl->client_version == SSL3_VERSION && |
| 594 | !ssl->s3->initial_handshake_complete) { |
| 595 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 596 | return 0; |
| 597 | } |
| 598 | /* The renegotiation extension is required to be at index zero. */ |
| 599 | ssl->s3->tmp.extensions.sent |= (1u << 0); |
| 600 | } |
| 601 | |
| 602 | if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) && |
| 603 | !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | return CBB_flush(out); |
| 608 | } |
| 609 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 610 | static int ssl3_send_client_hello(SSL *ssl) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 611 | if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 612 | return ssl->method->write_message(ssl); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 613 | } |
| 614 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 615 | /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we |
| 616 | * may send multiple ClientHellos if we receive HelloVerifyRequest. */ |
| 617 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 618 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 619 | return -1; |
| 620 | } |
| 621 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 622 | CBB cbb; |
| 623 | CBB_zero(&cbb); |
| 624 | |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 625 | uint16_t min_version, max_version; |
| 626 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 627 | goto err; |
| 628 | } |
| 629 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 630 | assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A); |
| 631 | if (!ssl->s3->have_version) { |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 632 | ssl->version = ssl->method->version_to_wire(max_version); |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 633 | /* Only set |ssl->client_version| on the initial handshake. Renegotiations, |
| 634 | * although locked to a version, reuse the value. When using the plain RSA |
| 635 | * key exchange, the ClientHello version is checked in the premaster secret. |
| 636 | * Some servers fail when this value changes. */ |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 637 | ssl->client_version = ssl->version; |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 638 | } |
| 639 | |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 640 | /* If the configured session has expired or was created at a disabled |
| 641 | * version, drop it. */ |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 642 | if (ssl->session != NULL) { |
| 643 | uint16_t session_version = |
| 644 | ssl->method->version_from_wire(ssl->session->ssl_version); |
| 645 | if (ssl->session->session_id_length == 0 || ssl->session->not_resumable || |
| 646 | ssl->session->timeout < (long)(time(NULL) - ssl->session->time) || |
| 647 | session_version < min_version || session_version > max_version) { |
| 648 | SSL_set_session(ssl, NULL); |
| 649 | } |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 653 | * renegerate the client_random. The random must be reused. */ |
| 654 | if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) && |
| 655 | !ssl_fill_hello_random(ssl->s3->client_random, |
| 656 | sizeof(ssl->s3->client_random), 0 /* client */)) { |
| 657 | goto err; |
| 658 | } |
| 659 | |
| 660 | /* Renegotiations do not participate in session resumption. */ |
| 661 | int has_session = ssl->session != NULL && |
| 662 | !ssl->s3->initial_handshake_complete; |
| 663 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 664 | CBB body, child; |
| 665 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) || |
| 666 | !CBB_add_u16(&body, ssl->client_version) || |
| 667 | !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 668 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 669 | (has_session && |
| 670 | !CBB_add_bytes(&child, ssl->session->session_id, |
| 671 | ssl->session->session_id_length))) { |
| 672 | goto err; |
| 673 | } |
| 674 | |
| 675 | if (SSL_IS_DTLS(ssl)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 676 | if (!CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 677 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
| 678 | goto err; |
| 679 | } |
| 680 | } |
| 681 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 682 | size_t header_len = |
| 683 | SSL_IS_DTLS(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
David Benjamin | 929d4ee | 2016-06-24 23:55:58 -0400 | [diff] [blame] | 684 | if (!ssl3_write_client_cipher_list(ssl, &body, min_version, max_version) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 685 | !CBB_add_u8(&body, 1 /* one compression method */) || |
| 686 | !CBB_add_u8(&body, 0 /* null compression */) || |
| 687 | !ssl_add_clienthello_tlsext(ssl, &body, header_len + CBB_len(&body)) || |
| 688 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 689 | goto err; |
| 690 | } |
| 691 | |
| 692 | ssl->state = SSL3_ST_CW_CLNT_HELLO_B; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 693 | return ssl->method->write_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 694 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 695 | err: |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 696 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 697 | return -1; |
| 698 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 699 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 700 | static int dtls1_get_hello_verify(SSL *ssl) { |
| 701 | long n; |
| 702 | int al, ok = 0; |
| 703 | CBS hello_verify_request, cookie; |
| 704 | uint16_t server_version; |
| 705 | |
| 706 | n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
| 707 | |
| 708 | if (!ok) { |
| 709 | return n; |
| 710 | } |
| 711 | |
| 712 | if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 713 | ssl->d1->send_cookie = 0; |
| 714 | ssl->s3->tmp.reuse_message = 1; |
| 715 | return 1; |
| 716 | } |
| 717 | |
| 718 | CBS_init(&hello_verify_request, ssl->init_msg, n); |
| 719 | |
| 720 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 721 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 722 | CBS_len(&hello_verify_request) != 0) { |
| 723 | al = SSL_AD_DECODE_ERROR; |
| 724 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 725 | goto f_err; |
| 726 | } |
| 727 | |
| 728 | if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) { |
| 729 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 730 | goto f_err; |
| 731 | } |
| 732 | |
| 733 | memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
| 734 | ssl->d1->cookie_len = CBS_len(&cookie); |
| 735 | |
| 736 | ssl->d1->send_cookie = 1; |
| 737 | return 1; |
| 738 | |
| 739 | f_err: |
| 740 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | static int ssl3_get_server_hello(SSL *ssl) { |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 745 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 746 | const SSL_CIPHER *c; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 747 | CERT *ct = ssl->cert; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 748 | int al = SSL_AD_INTERNAL_ERROR, ok; |
| 749 | long n; |
| 750 | CBS server_hello, server_random, session_id; |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 751 | uint16_t server_wire_version, server_version, cipher_suite; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 752 | uint8_t compression_method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 753 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 754 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO, ssl_hash_message, |
| 755 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 756 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 757 | if (!ok) { |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 758 | uint32_t err = ERR_peek_error(); |
| 759 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 760 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 761 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 762 | * in response to ClientHello. This matches NSS's client behavior and |
| 763 | * gives a better error on a (probable) failure to negotiate initial |
| 764 | * parameters. Note: this error code comes after the original one. |
| 765 | * |
| 766 | * See https://crbug.com/446505. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 767 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 768 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 769 | return n; |
| 770 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 771 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 772 | CBS_init(&server_hello, ssl->init_msg, n); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 773 | |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 774 | if (!CBS_get_u16(&server_hello, &server_wire_version) || |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 775 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
| 776 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 777 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 778 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 779 | !CBS_get_u8(&server_hello, &compression_method)) { |
| 780 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 781 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 782 | goto f_err; |
| 783 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 784 | |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 785 | server_version = ssl->method->version_from_wire(server_wire_version); |
| 786 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 787 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 788 | if (!ssl->s3->have_version) { |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 789 | uint16_t min_version, max_version; |
| 790 | if (!ssl_get_version_range(ssl, &min_version, &max_version) || |
| 791 | server_version < min_version || server_version > max_version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 792 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 793 | al = SSL_AD_PROTOCOL_VERSION; |
| 794 | goto f_err; |
| 795 | } |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 796 | ssl->version = server_wire_version; |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 797 | ssl->s3->enc_method = ssl3_get_enc_method(server_version); |
| 798 | assert(ssl->s3->enc_method != NULL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 799 | /* At this point, the connection's version is known and ssl->version is |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 800 | * fixed. Begin enforcing the record-layer version. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 801 | ssl->s3->have_version = 1; |
David Benjamin | b6a0a51 | 2016-06-21 10:33:21 -0400 | [diff] [blame] | 802 | } else if (server_wire_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 803 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 804 | al = SSL_AD_PROTOCOL_VERSION; |
| 805 | goto f_err; |
| 806 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 807 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 808 | /* Copy over the server random. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 809 | memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 810 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 811 | assert(ssl->session == NULL || ssl->session->session_id_length > 0); |
| 812 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
| 813 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 814 | ssl->session->session_id_length)) { |
| 815 | if (ssl->sid_ctx_length != ssl->session->sid_ctx_length || |
| 816 | memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 817 | /* actually a client application bug */ |
| 818 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 819 | OPENSSL_PUT_ERROR(SSL, |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 820 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 821 | goto f_err; |
| 822 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 823 | ssl->hit = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 824 | } else { |
| 825 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 826 | * fill out. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 827 | ssl->hit = 0; |
| 828 | if (!ssl_get_new_session(ssl, 0 /* client */)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 829 | goto f_err; |
| 830 | } |
| 831 | /* Note: session_id could be empty. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 832 | ssl->session->session_id_length = CBS_len(&session_id); |
| 833 | memcpy(ssl->session->session_id, CBS_data(&session_id), |
| 834 | CBS_len(&session_id)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 835 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 836 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 837 | c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 838 | if (c == NULL) { |
| 839 | /* unknown cipher */ |
| 840 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 841 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 842 | goto f_err; |
| 843 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 844 | /* If the cipher is disabled then we didn't sent it in the ClientHello, so if |
| 845 | * the server selected it, it's an error. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 846 | if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) || |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 847 | SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) || |
| 848 | SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 849 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 850 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 851 | goto f_err; |
| 852 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 853 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 854 | sk = ssl_get_ciphers_by_id(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 855 | if (!sk_SSL_CIPHER_find(sk, NULL, c)) { |
| 856 | /* we did not say we would use this cipher */ |
| 857 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 858 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 859 | goto f_err; |
| 860 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 861 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 862 | if (ssl->hit) { |
| 863 | if (ssl->session->cipher != c) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 864 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 865 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 866 | goto f_err; |
| 867 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 868 | if (ssl->session->ssl_version != ssl->version) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 869 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 870 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 871 | goto f_err; |
| 872 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 873 | } else { |
| 874 | ssl->session->cipher = c; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 875 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 876 | ssl->s3->tmp.new_cipher = c; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 877 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 878 | /* Now that the cipher is known, initialize the handshake hash. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 879 | if (!ssl3_init_handshake_hash(ssl)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 880 | goto f_err; |
| 881 | } |
| 882 | |
David Benjamin | 5f04b65 | 2015-05-26 17:30:10 -0400 | [diff] [blame] | 883 | /* If doing a full handshake with TLS 1.2, the server may request a client |
| 884 | * certificate which requires hashing the handshake transcript under a |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 885 | * different hash. Otherwise, the handshake buffer may be released. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 886 | if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION || |
| 887 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 888 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 889 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 890 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 891 | /* Only the NULL compression algorithm is supported. */ |
| 892 | if (compression_method != 0) { |
| 893 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 894 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 895 | goto f_err; |
| 896 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 897 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 898 | /* TLS extensions */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 899 | if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 900 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 901 | goto err; |
| 902 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 903 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 904 | /* There should be nothing left over in the record. */ |
| 905 | if (CBS_len(&server_hello) != 0) { |
| 906 | /* wrong packet length */ |
| 907 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 908 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 909 | goto f_err; |
| 910 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 911 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 912 | if (ssl->hit && |
| 913 | ssl->s3->tmp.extended_master_secret != |
| 914 | ssl->session->extended_master_secret) { |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 915 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 916 | if (ssl->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 917 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 918 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 919 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 920 | } |
| 921 | goto f_err; |
| 922 | } |
| 923 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 924 | return 1; |
| 925 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 926 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 927 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 928 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 929 | return -1; |
| 930 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 931 | |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 932 | /* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server |
| 933 | * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the |
| 934 | * error queue. */ |
| 935 | static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 936 | int ret = 0; |
| 937 | EVP_PKEY *pkey = X509_get_pubkey(leaf); |
David Benjamin | d1c1c8e | 2015-06-08 18:36:21 -0400 | [diff] [blame] | 938 | if (pkey == NULL) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 939 | goto err; |
| 940 | } |
| 941 | |
| 942 | /* Check the certificate's type matches the cipher. */ |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 943 | const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 944 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 945 | assert(expected_type != EVP_PKEY_NONE); |
| 946 | if (pkey->type != expected_type) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 947 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 948 | goto err; |
| 949 | } |
| 950 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 951 | if (cipher->algorithm_auth & SSL_aECDSA) { |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 952 | /* TODO(davidben): This behavior is preserved from upstream. Should key |
| 953 | * usages be checked in other cases as well? */ |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 954 | /* This call populates the ex_flags field correctly */ |
| 955 | X509_check_purpose(leaf, -1, 0); |
| 956 | if ((leaf->ex_flags & EXFLAG_KUSAGE) && |
| 957 | !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 958 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 959 | goto err; |
| 960 | } |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 961 | |
| 962 | if (!tls1_check_ec_cert(ssl, leaf)) { |
| 963 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
| 964 | goto err; |
| 965 | } |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | ret = 1; |
| 969 | |
| 970 | err: |
| 971 | EVP_PKEY_free(pkey); |
| 972 | return ret; |
| 973 | } |
| 974 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 975 | static int ssl3_get_server_certificate(SSL *ssl) { |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 976 | int al, ok, ret = -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 977 | unsigned long n; |
| 978 | X509 *x = NULL; |
| 979 | STACK_OF(X509) *sk = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 980 | EVP_PKEY *pkey = NULL; |
| 981 | CBS cbs, certificate_list; |
| 982 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 983 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 984 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message, |
| 985 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 986 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 987 | if (!ok) { |
| 988 | return n; |
| 989 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 990 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 991 | CBS_init(&cbs, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 992 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 993 | sk = sk_X509_new_null(); |
| 994 | if (sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 995 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 996 | goto err; |
| 997 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 998 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 999 | if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) || |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 1000 | CBS_len(&certificate_list) == 0 || |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1001 | CBS_len(&cbs) != 0) { |
| 1002 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1003 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1004 | goto f_err; |
| 1005 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1006 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1007 | while (CBS_len(&certificate_list) > 0) { |
| 1008 | CBS certificate; |
| 1009 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 1010 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1011 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1012 | goto f_err; |
| 1013 | } |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1014 | /* A u24 length cannot overflow a long. */ |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1015 | data = CBS_data(&certificate); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1016 | x = d2i_X509(NULL, &data, (long)CBS_len(&certificate)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1017 | if (x == NULL) { |
| 1018 | al = SSL_AD_BAD_CERTIFICATE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1019 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1020 | goto f_err; |
| 1021 | } |
| 1022 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 1023 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1024 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1025 | goto f_err; |
| 1026 | } |
| 1027 | if (!sk_X509_push(sk, x)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1028 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1029 | goto err; |
| 1030 | } |
| 1031 | x = NULL; |
| 1032 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1033 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1034 | X509 *leaf = sk_X509_value(sk, 0); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1035 | if (!ssl3_check_leaf_certificate(ssl, leaf)) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1036 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1037 | goto f_err; |
| 1038 | } |
| 1039 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1040 | /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes |
| 1041 | * the leaf. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1042 | sk_X509_pop_free(ssl->session->cert_chain, X509_free); |
| 1043 | ssl->session->cert_chain = sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1044 | sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1045 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1046 | X509_free(ssl->session->peer); |
| 1047 | ssl->session->peer = X509_up_ref(leaf); |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1048 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1049 | ssl->session->verify_result = ssl->verify_result; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1050 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1051 | ret = 1; |
| 1052 | |
| 1053 | if (0) { |
| 1054 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1055 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | err: |
| 1059 | EVP_PKEY_free(pkey); |
| 1060 | X509_free(x); |
| 1061 | sk_X509_pop_free(sk, X509_free); |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1065 | static int ssl3_get_cert_status(SSL *ssl) { |
| 1066 | int ok, al; |
| 1067 | long n; |
| 1068 | CBS certificate_status, ocsp_response; |
| 1069 | uint8_t status_type; |
| 1070 | |
| 1071 | n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
| 1072 | |
| 1073 | if (!ok) { |
| 1074 | return n; |
| 1075 | } |
| 1076 | |
| 1077 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { |
| 1078 | /* A server may send status_request in ServerHello and then change |
| 1079 | * its mind about sending CertificateStatus. */ |
| 1080 | ssl->s3->tmp.reuse_message = 1; |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |
| 1084 | CBS_init(&certificate_status, ssl->init_msg, n); |
| 1085 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1086 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1087 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1088 | CBS_len(&ocsp_response) == 0 || |
| 1089 | CBS_len(&certificate_status) != 0) { |
| 1090 | al = SSL_AD_DECODE_ERROR; |
| 1091 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1092 | goto f_err; |
| 1093 | } |
| 1094 | |
| 1095 | if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response, |
| 1096 | &ssl->session->ocsp_response_length)) { |
| 1097 | al = SSL_AD_INTERNAL_ERROR; |
| 1098 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1099 | goto f_err; |
| 1100 | } |
| 1101 | return 1; |
| 1102 | |
| 1103 | f_err: |
| 1104 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 1105 | return -1; |
| 1106 | } |
| 1107 | |
| 1108 | static int ssl3_verify_server_cert(SSL *ssl) { |
| 1109 | int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain); |
| 1110 | if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) { |
| 1111 | int al = ssl_verify_alarm_type(ssl->verify_result); |
| 1112 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 1113 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1114 | } else { |
| 1115 | ret = 1; |
| 1116 | ERR_clear_error(); /* but we keep ssl->verify_result */ |
| 1117 | } |
| 1118 | |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1122 | static int ssl3_get_server_key_exchange(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1123 | EVP_MD_CTX md_ctx; |
| 1124 | int al, ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1125 | EVP_PKEY *pkey = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1126 | DH *dh = NULL; |
| 1127 | EC_KEY *ecdh = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1128 | EC_POINT *srvr_ecpoint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1129 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1130 | long n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1131 | if (!ok) { |
| 1132 | return n; |
| 1133 | } |
| 1134 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1135 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 1136 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1137 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1138 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1139 | return -1; |
| 1140 | } |
| 1141 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 1142 | /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no |
| 1143 | * identity hint. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1144 | if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1145 | /* TODO(davidben): This should be reset in one place with the rest of the |
| 1146 | * handshake state. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1147 | OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint); |
| 1148 | ssl->s3->tmp.peer_psk_identity_hint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1149 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1150 | ssl->s3->tmp.reuse_message = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1151 | return 1; |
| 1152 | } |
| 1153 | |
| 1154 | /* Retain a copy of the original CBS to compute the signature over. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1155 | CBS server_key_exchange; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1156 | CBS_init(&server_key_exchange, ssl->init_msg, n); |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1157 | CBS server_key_exchange_orig = server_key_exchange; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1158 | |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1159 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1160 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1161 | EVP_MD_CTX_init(&md_ctx); |
| 1162 | |
| 1163 | if (alg_a & SSL_aPSK) { |
| 1164 | CBS psk_identity_hint; |
| 1165 | |
| 1166 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1167 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1168 | &psk_identity_hint)) { |
| 1169 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1170 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1171 | goto f_err; |
| 1172 | } |
| 1173 | |
| 1174 | /* Store PSK identity hint for later use, hint is used in |
| 1175 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1176 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1177 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1178 | * |
| 1179 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1180 | * a specific identity. */ |
| 1181 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1182 | CBS_contains_zero_byte(&psk_identity_hint)) { |
| 1183 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1184 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1185 | goto f_err; |
| 1186 | } |
| 1187 | |
| 1188 | /* Save the identity hint as a C string. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1189 | if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1190 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1191 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1192 | goto f_err; |
| 1193 | } |
| 1194 | } |
| 1195 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1196 | if (alg_k & SSL_kDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1197 | CBS dh_p, dh_g, dh_Ys; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1198 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1199 | CBS_len(&dh_p) == 0 || |
| 1200 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1201 | CBS_len(&dh_g) == 0 || |
| 1202 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1203 | CBS_len(&dh_Ys) == 0) { |
| 1204 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1205 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1206 | goto f_err; |
| 1207 | } |
| 1208 | |
| 1209 | dh = DH_new(); |
| 1210 | if (dh == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1211 | goto err; |
| 1212 | } |
| 1213 | |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1214 | dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL); |
| 1215 | dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL); |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1216 | if (dh->p == NULL || dh->g == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1217 | goto err; |
| 1218 | } |
| 1219 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1220 | ssl->session->key_exchange_info = DH_num_bits(dh); |
| 1221 | if (ssl->session->key_exchange_info < 1024) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1222 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1223 | goto err; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1224 | } else if (ssl->session->key_exchange_info > 4096) { |
David Benjamin | cd24a39 | 2015-11-11 13:23:05 -0800 | [diff] [blame] | 1225 | /* Overly large DHE groups are prohibitively expensive, so enforce a limit |
| 1226 | * to prevent a server from causing us to perform too expensive of a |
| 1227 | * computation. */ |
| 1228 | OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG); |
| 1229 | goto err; |
| 1230 | } |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1231 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1232 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1233 | dh = NULL; |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1234 | |
| 1235 | /* Save the peer public key for later. */ |
| 1236 | size_t peer_key_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1237 | if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1238 | goto err; |
| 1239 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1240 | /* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len |
| 1241 | * fits in a uint16_t. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1242 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1243 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1244 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1245 | /* Parse the server parameters. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1246 | uint8_t group_type; |
| 1247 | uint16_t group_id; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1248 | CBS point; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1249 | if (!CBS_get_u8(&server_key_exchange, &group_type) || |
| 1250 | group_type != NAMED_CURVE_TYPE || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1251 | !CBS_get_u16(&server_key_exchange, &group_id) || |
| 1252 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1253 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1254 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1255 | goto f_err; |
| 1256 | } |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1257 | ssl->session->key_exchange_info = group_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1258 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1259 | /* Ensure the group is consistent with preferences. */ |
| 1260 | if (!tls1_check_group_id(ssl, group_id)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1261 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1262 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1263 | goto f_err; |
| 1264 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1265 | |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1266 | /* Initialize ECDH and save the peer public key for later. */ |
| 1267 | size_t peer_key_len; |
| 1268 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) || |
| 1269 | !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
David Benjamin | c1cc858 | 2015-12-17 01:22:15 -0500 | [diff] [blame] | 1270 | goto err; |
| 1271 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1272 | /* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len |
| 1273 | * fits in a uint16_t. */ |
| 1274 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1275 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
| 1276 | } else if (alg_k & SSL_kCECPQ1) { |
David Benjamin | 18a3518 | 2016-06-30 13:46:24 -0400 | [diff] [blame^] | 1277 | SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx); |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1278 | CBS key; |
| 1279 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1280 | al = SSL_AD_DECODE_ERROR; |
| 1281 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1282 | goto f_err; |
| 1283 | } |
| 1284 | |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1285 | size_t peer_key_len; |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1286 | if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1287 | goto err; |
| 1288 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1289 | /* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len |
| 1290 | * fits in a uint16_t. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1291 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1292 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1293 | } else if (!(alg_k & SSL_kPSK)) { |
| 1294 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1295 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1296 | goto f_err; |
| 1297 | } |
| 1298 | |
| 1299 | /* At this point, |server_key_exchange| contains the signature, if any, while |
| 1300 | * |server_key_exchange_orig| contains the entire message. From that, derive |
| 1301 | * a CBS containing just the parameter. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1302 | CBS parameter; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1303 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1304 | CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange)); |
| 1305 | |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1306 | /* ServerKeyExchange should be signed by the server's public key. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 1307 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1308 | pkey = X509_get_pubkey(ssl->session->peer); |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1309 | if (pkey == NULL) { |
| 1310 | goto err; |
| 1311 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1312 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1313 | uint16_t signature_algorithm = 0; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1314 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1315 | if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1316 | al = SSL_AD_DECODE_ERROR; |
| 1317 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1318 | goto f_err; |
| 1319 | } |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1320 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm, pkey)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1321 | goto f_err; |
| 1322 | } |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1323 | ssl->s3->tmp.peer_signature_algorithm = signature_algorithm; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1324 | } else if (pkey->type == EVP_PKEY_RSA) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1325 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 1326 | } else if (pkey->type == EVP_PKEY_EC) { |
| 1327 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | /* The last field in |server_key_exchange| is the signature. */ |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1331 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1332 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1333 | CBS_len(&server_key_exchange) != 0) { |
| 1334 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1335 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1336 | goto f_err; |
| 1337 | } |
| 1338 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1339 | const EVP_MD *md = tls12_get_hash(signature_algorithm); |
| 1340 | if (md == NULL) { |
| 1341 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1342 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 1343 | goto f_err; |
| 1344 | } |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1345 | int sig_ok = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) && |
| 1346 | EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random, |
| 1347 | SSL3_RANDOM_SIZE) && |
| 1348 | EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random, |
| 1349 | SSL3_RANDOM_SIZE) && |
| 1350 | EVP_DigestVerifyUpdate(&md_ctx, CBS_data(¶meter), |
| 1351 | CBS_len(¶meter)) && |
| 1352 | EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), |
| 1353 | CBS_len(&signature)); |
| 1354 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1355 | sig_ok = 1; |
| 1356 | ERR_clear_error(); |
| 1357 | #endif |
| 1358 | if (!sig_ok) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1359 | /* bad signature */ |
| 1360 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1361 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1362 | goto f_err; |
| 1363 | } |
| 1364 | } else { |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1365 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1366 | assert(alg_a == SSL_aPSK); |
| 1367 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1368 | if (CBS_len(&server_key_exchange) > 0) { |
| 1369 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1370 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1371 | goto f_err; |
| 1372 | } |
| 1373 | } |
| 1374 | EVP_PKEY_free(pkey); |
| 1375 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1376 | return 1; |
| 1377 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1378 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1379 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1380 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1381 | EVP_PKEY_free(pkey); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1382 | DH_free(dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1383 | EC_POINT_free(srvr_ecpoint); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1384 | EC_KEY_free(ecdh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1385 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1386 | return -1; |
| 1387 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1388 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1389 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 1390 | return X509_NAME_cmp(*a, *b); |
| 1391 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1392 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1393 | static int ssl3_get_certificate_request(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1394 | int ok, ret = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1395 | X509_NAME *xn = NULL; |
| 1396 | STACK_OF(X509_NAME) *ca_sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1397 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1398 | long n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1399 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1400 | if (!ok) { |
| 1401 | return n; |
| 1402 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1403 | |
David Benjamin | 5744ca6 | 2016-06-13 14:05:19 -0400 | [diff] [blame] | 1404 | ssl->s3->tmp.cert_request = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1405 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1406 | if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1407 | ssl->s3->tmp.reuse_message = 1; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1408 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1409 | * client auth. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1410 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1411 | return 1; |
| 1412 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1413 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1414 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 1415 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1416 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1417 | goto err; |
| 1418 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1419 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1420 | CBS cbs; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1421 | CBS_init(&cbs, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1422 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1423 | ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
| 1424 | if (ca_sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1425 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1426 | goto err; |
| 1427 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1428 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1429 | /* get the certificate types */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1430 | CBS certificate_types; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1431 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1432 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1433 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1434 | goto err; |
| 1435 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1436 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1437 | if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types, |
| 1438 | &ssl->s3->tmp.num_certificate_types)) { |
| 1439 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1440 | goto err; |
| 1441 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1442 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1443 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1444 | CBS supported_signature_algorithms; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1445 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1446 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
| 1447 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1448 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1449 | goto err; |
| 1450 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1451 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1452 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1453 | /* get the CA RDNs */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1454 | CBS certificate_authorities; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1455 | if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1456 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1457 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1458 | goto err; |
| 1459 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1460 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1461 | while (CBS_len(&certificate_authorities) > 0) { |
| 1462 | CBS distinguished_name; |
| 1463 | if (!CBS_get_u16_length_prefixed(&certificate_authorities, |
| 1464 | &distinguished_name)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1465 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1466 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1467 | goto err; |
| 1468 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1469 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1470 | const uint8_t *data = CBS_data(&distinguished_name); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1471 | /* A u16 length cannot overflow a long. */ |
| 1472 | xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name)); |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1473 | if (xn == NULL || |
| 1474 | data != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1475 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1476 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1477 | goto err; |
| 1478 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1479 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1480 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1481 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1482 | goto err; |
| 1483 | } |
David Benjamin | 6b6e0b2 | 2016-03-11 19:30:29 -0500 | [diff] [blame] | 1484 | xn = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1485 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1486 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1487 | /* we should setup a certificate to return.... */ |
David Benjamin | 5744ca6 | 2016-06-13 14:05:19 -0400 | [diff] [blame] | 1488 | ssl->s3->tmp.cert_request = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1489 | sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free); |
| 1490 | ssl->s3->tmp.ca_names = ca_sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1491 | ca_sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1492 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1493 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1494 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1495 | err: |
David Benjamin | 6b6e0b2 | 2016-03-11 19:30:29 -0500 | [diff] [blame] | 1496 | X509_NAME_free(xn); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1497 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1498 | return ret; |
| 1499 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1500 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1501 | static int ssl3_get_server_hello_done(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1502 | int ok; |
| 1503 | long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1504 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1505 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE, |
| 1506 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1507 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1508 | if (!ok) { |
| 1509 | return n; |
| 1510 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1511 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1512 | if (n > 0) { |
| 1513 | /* should contain no data */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1514 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1515 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1516 | return -1; |
| 1517 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1518 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1519 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1520 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1521 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1522 | /* ssl3_has_client_certificate returns true if a client certificate is |
| 1523 | * configured. */ |
| 1524 | static int ssl3_has_client_certificate(SSL *ssl) { |
| 1525 | return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl); |
| 1526 | } |
| 1527 | |
| 1528 | static int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, |
| 1529 | EVP_PKEY **out_pkey) { |
| 1530 | if (ssl->ctx->client_cert_cb == NULL) { |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | int ret = ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey); |
| 1535 | if (ret <= 0) { |
| 1536 | return ret; |
| 1537 | } |
| 1538 | |
| 1539 | assert(*out_x509 != NULL); |
| 1540 | assert(*out_pkey != NULL); |
| 1541 | return 1; |
| 1542 | } |
| 1543 | |
| 1544 | static int ssl3_send_client_certificate(SSL *ssl) { |
| 1545 | if (ssl->state == SSL3_ST_CW_CERT_A) { |
| 1546 | /* Call cert_cb to update the certificate. */ |
| 1547 | if (ssl->cert->cert_cb) { |
| 1548 | int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1549 | if (ret < 0) { |
| 1550 | ssl->rwstate = SSL_X509_LOOKUP; |
| 1551 | return -1; |
| 1552 | } |
| 1553 | if (ret == 0) { |
| 1554 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1555 | return -1; |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | if (ssl3_has_client_certificate(ssl)) { |
| 1560 | ssl->state = SSL3_ST_CW_CERT_C; |
| 1561 | } else { |
| 1562 | ssl->state = SSL3_ST_CW_CERT_B; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | if (ssl->state == SSL3_ST_CW_CERT_B) { |
| 1567 | /* Call client_cert_cb to update the certificate. */ |
| 1568 | X509 *x509 = NULL; |
| 1569 | EVP_PKEY *pkey = NULL; |
| 1570 | int ret = ssl_do_client_cert_cb(ssl, &x509, &pkey); |
| 1571 | if (ret < 0) { |
| 1572 | ssl->rwstate = SSL_X509_LOOKUP; |
| 1573 | return -1; |
| 1574 | } |
| 1575 | |
| 1576 | int setup_error = ret == 1 && (!SSL_use_certificate(ssl, x509) || |
| 1577 | !SSL_use_PrivateKey(ssl, pkey)); |
| 1578 | X509_free(x509); |
| 1579 | EVP_PKEY_free(pkey); |
| 1580 | if (setup_error) { |
| 1581 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1582 | return -1; |
| 1583 | } |
| 1584 | |
| 1585 | ssl->state = SSL3_ST_CW_CERT_C; |
| 1586 | } |
| 1587 | |
| 1588 | if (ssl->state == SSL3_ST_CW_CERT_C) { |
| 1589 | if (!ssl3_has_client_certificate(ssl)) { |
David Benjamin | 5744ca6 | 2016-06-13 14:05:19 -0400 | [diff] [blame] | 1590 | ssl->s3->tmp.cert_request = 0; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1591 | /* Without a client certificate, the handshake buffer may be released. */ |
| 1592 | ssl3_free_handshake_buffer(ssl); |
| 1593 | |
| 1594 | if (ssl->version == SSL3_VERSION) { |
| 1595 | /* In SSL 3.0, send no certificate by skipping both messages. */ |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1596 | ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1600 | CBB cbb, body; |
| 1601 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) || |
| 1602 | !CBB_add_u24(&body, 0 /* no certificates */) || |
| 1603 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1604 | return -1; |
| 1605 | } |
| 1606 | } else if (!ssl3_output_cert_chain(ssl)) { |
| 1607 | return -1; |
| 1608 | } |
| 1609 | ssl->state = SSL3_ST_CW_CERT_D; |
| 1610 | } |
| 1611 | |
| 1612 | assert(ssl->state == SSL3_ST_CW_CERT_D); |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1613 | return ssl->method->write_message(ssl); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1614 | } |
| 1615 | |
Brian Smith | 274341d | 2015-10-08 17:10:15 -1000 | [diff] [blame] | 1616 | OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned), |
| 1617 | SIZE_T_IS_SMALLER_THAN_UNSIGNED); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1618 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1619 | static int ssl3_send_client_key_exchange(SSL *ssl) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1620 | if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1621 | return ssl->method->write_message(ssl); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1622 | } |
| 1623 | assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A); |
| 1624 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1625 | uint8_t *pms = NULL; |
| 1626 | size_t pms_len = 0; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1627 | CBB cbb, body; |
| 1628 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1629 | SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1630 | goto err; |
| 1631 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1632 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1633 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1634 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1635 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1636 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1637 | unsigned psk_len = 0; |
| 1638 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1639 | if (alg_a & SSL_aPSK) { |
| 1640 | if (ssl->psk_client_callback == NULL) { |
| 1641 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
| 1642 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1643 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1644 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1645 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 1646 | memset(identity, 0, sizeof(identity)); |
| 1647 | psk_len = ssl->psk_client_callback( |
| 1648 | ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity), |
| 1649 | psk, sizeof(psk)); |
| 1650 | if (psk_len == 0) { |
| 1651 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1652 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1653 | goto err; |
| 1654 | } |
| 1655 | assert(psk_len <= PSK_MAX_PSK_LEN); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1656 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1657 | OPENSSL_free(ssl->session->psk_identity); |
| 1658 | ssl->session->psk_identity = BUF_strdup(identity); |
| 1659 | if (ssl->session->psk_identity == NULL) { |
| 1660 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1661 | goto err; |
| 1662 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1663 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1664 | /* Write out psk_identity. */ |
| 1665 | CBB child; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1666 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1667 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1668 | OPENSSL_strnlen(identity, sizeof(identity))) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1669 | !CBB_flush(&body)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1670 | goto err; |
| 1671 | } |
| 1672 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1673 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1674 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1675 | if (alg_k & SSL_kRSA) { |
| 1676 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1677 | pms = OPENSSL_malloc(pms_len); |
| 1678 | if (pms == NULL) { |
| 1679 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1680 | goto err; |
| 1681 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1682 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1683 | EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer); |
| 1684 | if (pkey == NULL) { |
| 1685 | goto err; |
| 1686 | } |
| 1687 | |
| 1688 | RSA *rsa = EVP_PKEY_get0_RSA(pkey); |
| 1689 | if (rsa == NULL) { |
| 1690 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1691 | EVP_PKEY_free(pkey); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1692 | goto err; |
| 1693 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1694 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1695 | EVP_PKEY_free(pkey); |
| 1696 | |
| 1697 | pms[0] = ssl->client_version >> 8; |
| 1698 | pms[1] = ssl->client_version & 0xff; |
| 1699 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1700 | goto err; |
| 1701 | } |
| 1702 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1703 | CBB child, *enc_pms = &body; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1704 | size_t enc_pms_len; |
| 1705 | /* In TLS, there is a length prefix. */ |
| 1706 | if (ssl->version > SSL3_VERSION) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1707 | if (!CBB_add_u16_length_prefixed(&body, &child)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1708 | goto err; |
| 1709 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1710 | enc_pms = &child; |
| 1711 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1712 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1713 | uint8_t *ptr; |
| 1714 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
| 1715 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len, |
| 1716 | RSA_PKCS1_PADDING) || |
| 1717 | /* Log the premaster secret, if logging is enabled. */ |
| 1718 | !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) || |
| 1719 | !CBB_did_write(enc_pms, enc_pms_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1720 | !CBB_flush(&body)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1721 | goto err; |
| 1722 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1723 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1724 | /* Generate a keypair and serialize the public half. */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1725 | CBB child; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1726 | if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &body, &child)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1727 | goto err; |
| 1728 | } |
| 1729 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1730 | /* Compute the premaster. */ |
| 1731 | uint8_t alert; |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1732 | if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len, |
| 1733 | &alert, ssl->s3->tmp.peer_key, |
| 1734 | ssl->s3->tmp.peer_key_len)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1735 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1736 | goto err; |
| 1737 | } |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1738 | if (!CBB_flush(&body)) { |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1739 | goto err; |
| 1740 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1741 | |
| 1742 | /* The key exchange state may now be discarded. */ |
| 1743 | SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx); |
| 1744 | OPENSSL_free(ssl->s3->tmp.peer_key); |
| 1745 | ssl->s3->tmp.peer_key = NULL; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1746 | } else if (alg_k & SSL_kPSK) { |
| 1747 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1748 | * the pre-shared key. */ |
| 1749 | pms_len = psk_len; |
| 1750 | pms = OPENSSL_malloc(pms_len); |
| 1751 | if (pms == NULL) { |
| 1752 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1753 | goto err; |
| 1754 | } |
| 1755 | memset(pms, 0, pms_len); |
| 1756 | } else { |
| 1757 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1758 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1759 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1760 | } |
| 1761 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1762 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1763 | * key. */ |
| 1764 | if (alg_a & SSL_aPSK) { |
| 1765 | CBB pms_cbb, child; |
| 1766 | uint8_t *new_pms; |
| 1767 | size_t new_pms_len; |
| 1768 | |
| 1769 | CBB_zero(&pms_cbb); |
| 1770 | if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) || |
| 1771 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1772 | !CBB_add_bytes(&child, pms, pms_len) || |
| 1773 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1774 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1775 | !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) { |
| 1776 | CBB_cleanup(&pms_cbb); |
| 1777 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1778 | goto err; |
| 1779 | } |
| 1780 | OPENSSL_cleanse(pms, pms_len); |
| 1781 | OPENSSL_free(pms); |
| 1782 | pms = new_pms; |
| 1783 | pms_len = new_pms_len; |
| 1784 | } |
| 1785 | |
| 1786 | /* The message must be added to the finished hash before calculating the |
| 1787 | * master secret. */ |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1788 | if (!ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1789 | goto err; |
| 1790 | } |
| 1791 | ssl->state = SSL3_ST_CW_KEY_EXCH_B; |
| 1792 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 1793 | ssl->session->master_key_length = |
| 1794 | tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1795 | if (ssl->session->master_key_length == 0) { |
| 1796 | goto err; |
| 1797 | } |
| 1798 | ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret; |
| 1799 | OPENSSL_cleanse(pms, pms_len); |
| 1800 | OPENSSL_free(pms); |
| 1801 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1802 | return ssl->method->write_message(ssl); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1803 | |
| 1804 | err: |
David Benjamin | 2b07fa4 | 2016-03-02 00:23:57 -0500 | [diff] [blame] | 1805 | CBB_cleanup(&cbb); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1806 | if (pms != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1807 | OPENSSL_cleanse(pms, pms_len); |
| 1808 | OPENSSL_free(pms); |
| 1809 | } |
| 1810 | return -1; |
| 1811 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1812 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1813 | static int ssl3_send_cert_verify(SSL *ssl) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1814 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1815 | return ssl->method->write_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1818 | assert(ssl_has_private_key(ssl)); |
| 1819 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1820 | CBB cbb, body, child; |
| 1821 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1822 | SSL3_MT_CERTIFICATE_VERIFY)) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1823 | goto err; |
| 1824 | } |
| 1825 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1826 | uint16_t signature_algorithm = tls1_choose_signature_algorithm(ssl); |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1827 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1828 | /* Write out the digest type in TLS 1.2. */ |
| 1829 | if (!CBB_add_u16(&body, signature_algorithm)) { |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1830 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1831 | goto err; |
| 1832 | } |
| 1833 | } |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1834 | |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1835 | /* Set aside space for the signature. */ |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1836 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1837 | uint8_t *ptr; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1838 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | 429fdc0 | 2016-06-17 17:33:30 -0400 | [diff] [blame] | 1839 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1840 | goto err; |
| 1841 | } |
| 1842 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1843 | size_t sig_len; |
| 1844 | enum ssl_private_key_result_t sign_result; |
| 1845 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1846 | /* Compute the digest. In TLS 1.1 and below, the digest type is also |
| 1847 | * selected here. */ |
| 1848 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1849 | size_t digest_len; |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1850 | if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, signature_algorithm)) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1851 | goto err; |
| 1852 | } |
| 1853 | |
| 1854 | /* The handshake buffer is no longer necessary. */ |
| 1855 | ssl3_free_handshake_buffer(ssl); |
| 1856 | |
| 1857 | /* Sign the digest. */ |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1858 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, |
| 1859 | signature_algorithm, digest, digest_len); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1860 | } else { |
| 1861 | assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1862 | sign_result = |
| 1863 | ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len); |
| 1864 | } |
| 1865 | |
| 1866 | switch (sign_result) { |
| 1867 | case ssl_private_key_success: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1868 | break; |
| 1869 | case ssl_private_key_failure: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1870 | goto err; |
| 1871 | case ssl_private_key_retry: |
| 1872 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1873 | ssl->state = SSL3_ST_CW_CERT_VRFY_B; |
| 1874 | goto err; |
| 1875 | } |
| 1876 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1877 | if (!CBB_did_write(&child, sig_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1878 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1879 | goto err; |
| 1880 | } |
| 1881 | |
| 1882 | ssl->state = SSL3_ST_CW_CERT_VRFY_C; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1883 | return ssl->method->write_message(ssl); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1884 | |
| 1885 | err: |
| 1886 | CBB_cleanup(&cbb); |
| 1887 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1888 | } |
| 1889 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1890 | static int ssl3_send_next_proto(SSL *ssl) { |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1891 | if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1892 | return ssl->method->write_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1895 | assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A); |
| 1896 | |
| 1897 | static const uint8_t kZero[32] = {0}; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1898 | size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32); |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1899 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1900 | CBB cbb, body, child; |
| 1901 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) || |
| 1902 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1903 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1904 | ssl->s3->next_proto_negotiated_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1905 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1906 | !CBB_add_bytes(&child, kZero, padding_len) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1907 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1908 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1909 | CBB_cleanup(&cbb); |
| 1910 | return -1; |
| 1911 | } |
| 1912 | |
| 1913 | ssl->state = SSL3_ST_CW_NEXT_PROTO_B; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1914 | return ssl->method->write_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1915 | } |
| 1916 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1917 | static int ssl3_send_channel_id(SSL *ssl) { |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1918 | if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1919 | return ssl->method->write_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1920 | } |
| 1921 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1922 | assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A); |
| 1923 | |
| 1924 | if (ssl->tlsext_channel_id_private == NULL && |
| 1925 | ssl->ctx->channel_id_cb != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1926 | EVP_PKEY *key = NULL; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1927 | ssl->ctx->channel_id_cb(ssl, &key); |
| 1928 | if (key != NULL && |
| 1929 | !SSL_set1_tls_channel_id(ssl, key)) { |
| 1930 | EVP_PKEY_free(key); |
| 1931 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1932 | } |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1933 | EVP_PKEY_free(key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1934 | } |
| 1935 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1936 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1937 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1938 | return -1; |
| 1939 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1940 | |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1941 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
| 1942 | if (ec_key == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1943 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1944 | return -1; |
| 1945 | } |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1946 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1947 | int ret = -1; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1948 | BIGNUM *x = BN_new(); |
| 1949 | BIGNUM *y = BN_new(); |
| 1950 | ECDSA_SIG *sig = NULL; |
| 1951 | if (x == NULL || y == NULL || |
| 1952 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 1953 | EC_KEY_get0_public_key(ec_key), |
| 1954 | x, y, NULL)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1955 | goto err; |
| 1956 | } |
| 1957 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1958 | uint8_t digest[EVP_MAX_MD_SIZE]; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 1959 | size_t digest_len; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1960 | if (!tls1_channel_id_hash(ssl, digest, &digest_len)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1961 | goto err; |
| 1962 | } |
| 1963 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1964 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1965 | if (sig == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1966 | goto err; |
| 1967 | } |
| 1968 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1969 | CBB cbb, body, child; |
| 1970 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1971 | SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS) || |
| 1972 | !CBB_add_u16(&body, TLSEXT_TYPE_channel_id) || |
| 1973 | !CBB_add_u16_length_prefixed(&body, &child) || |
| 1974 | !BN_bn2cbb_padded(&child, 32, x) || !BN_bn2cbb_padded(&child, 32, y) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1975 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 1976 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1977 | !ssl->method->finish_message(ssl, &cbb)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1978 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1979 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1980 | goto err; |
| 1981 | } |
| 1982 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1983 | ssl->state = SSL3_ST_CW_CHANNEL_ID_B; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1984 | ret = ssl->method->write_message(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1985 | |
| 1986 | err: |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1987 | BN_free(x); |
| 1988 | BN_free(y); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1989 | ECDSA_SIG_free(sig); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1990 | return ret; |
| 1991 | } |
| 1992 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1993 | static int ssl3_get_new_session_ticket(SSL *ssl) { |
| 1994 | int ok, al; |
| 1995 | long n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET, |
| 1996 | ssl_hash_message, &ok); |
| 1997 | |
| 1998 | if (!ok) { |
| 1999 | return n; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2000 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2001 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2002 | CBS new_session_ticket, ticket; |
| 2003 | uint32_t ticket_lifetime_hint; |
| 2004 | CBS_init(&new_session_ticket, ssl->init_msg, n); |
| 2005 | if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) || |
| 2006 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 2007 | CBS_len(&new_session_ticket) != 0) { |
| 2008 | al = SSL_AD_DECODE_ERROR; |
| 2009 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2010 | goto f_err; |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2011 | } |
| 2012 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2013 | if (CBS_len(&ticket) == 0) { |
| 2014 | /* RFC 5077 allows a server to change its mind and send no ticket after |
| 2015 | * negotiating the extension. The value of |tlsext_ticket_expected| is |
| 2016 | * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary |
| 2017 | * update. */ |
| 2018 | ssl->tlsext_ticket_expected = 0; |
| 2019 | return 1; |
| 2020 | } |
| 2021 | |
| 2022 | if (ssl->hit) { |
| 2023 | /* The server is sending a new ticket for an existing session. Sessions are |
| 2024 | * immutable once established, so duplicate all but the ticket of the |
| 2025 | * existing session. */ |
| 2026 | uint8_t *bytes; |
| 2027 | size_t bytes_len; |
| 2028 | if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) { |
| 2029 | goto err; |
| 2030 | } |
| 2031 | SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len); |
| 2032 | OPENSSL_free(bytes); |
| 2033 | if (new_session == NULL) { |
| 2034 | /* This should never happen. */ |
| 2035 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2036 | goto err; |
| 2037 | } |
| 2038 | |
| 2039 | SSL_SESSION_free(ssl->session); |
| 2040 | ssl->session = new_session; |
| 2041 | } |
| 2042 | |
| 2043 | if (!CBS_stow(&ticket, &ssl->session->tlsext_tick, |
| 2044 | &ssl->session->tlsext_ticklen)) { |
| 2045 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 2046 | goto err; |
| 2047 | } |
| 2048 | ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; |
| 2049 | |
| 2050 | /* Generate a session ID for this session based on the session ticket. We use |
| 2051 | * the session ID mechanism for detecting ticket resumption. This also fits in |
| 2052 | * with assumptions elsewhere in OpenSSL.*/ |
| 2053 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id, |
| 2054 | &ssl->session->session_id_length, EVP_sha256(), NULL)) { |
| 2055 | goto err; |
| 2056 | } |
| 2057 | |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2058 | return 1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2059 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2060 | f_err: |
| 2061 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 2062 | err: |
| 2063 | return -1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2064 | } |