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 | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 336 | if (ssl->s3->tmp.cert_req) { |
| 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 | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 359 | /* If cert_req is 2, client certificates are sent, but not |
| 360 | * CertificateVerify. */ |
| 361 | if (ssl->s3->tmp.cert_req == 1) { |
| 362 | ret = ssl3_send_cert_verify(ssl); |
| 363 | if (ret <= 0) { |
| 364 | goto end; |
| 365 | } |
| 366 | } else { |
| 367 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 368 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 369 | ssl->state = SSL3_ST_CW_CHANGE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 370 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 372 | case SSL3_ST_CW_CHANGE_A: |
| 373 | case SSL3_ST_CW_CHANGE_B: |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 374 | ret = ssl->method->send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A, |
| 375 | SSL3_ST_CW_CHANGE_B); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 376 | if (ret <= 0) { |
| 377 | goto end; |
| 378 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 380 | ssl->state = SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 382 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 383 | ret = -1; |
| 384 | goto end; |
| 385 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 387 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 389 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 390 | case SSL3_ST_CW_NEXT_PROTO_B: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 391 | if (ssl->s3->next_proto_neg_seen) { |
| 392 | ret = ssl3_send_next_proto(ssl); |
| 393 | if (ret <= 0) { |
| 394 | goto end; |
| 395 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 396 | } else { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 397 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 398 | } |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 399 | ssl->state = SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 400 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 402 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 403 | case SSL3_ST_CW_CHANNEL_ID_B: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 404 | if (ssl->s3->tlsext_channel_id_valid) { |
| 405 | ret = ssl3_send_channel_id(ssl); |
| 406 | if (ret <= 0) { |
| 407 | goto end; |
| 408 | } |
| 409 | } else { |
| 410 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 411 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 412 | ssl->state = SSL3_ST_CW_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 413 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 415 | case SSL3_ST_CW_FINISHED_A: |
| 416 | case SSL3_ST_CW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 417 | ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 418 | SSL3_ST_CW_FINISHED_B); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 419 | if (ret <= 0) { |
| 420 | goto end; |
| 421 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 422 | ssl->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 424 | if (ssl->hit) { |
| 425 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 426 | } else { |
| 427 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 428 | * record the handshake hashes at this point in the session so that |
| 429 | * any resumption of this session with ChannelID can sign those |
| 430 | * hashes. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 431 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 432 | if (ret <= 0) { |
| 433 | goto end; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 434 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 435 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 436 | ssl3_can_false_start(ssl) && |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 437 | /* No False Start on renegotiation (would complicate the state |
| 438 | * machine). */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 439 | !ssl->s3->initial_handshake_complete) { |
| 440 | ssl->s3->tmp.next_state = SSL3_ST_FALSE_START; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 441 | } else { |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 442 | ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 445 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 446 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 447 | case SSL3_ST_FALSE_START: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 448 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 449 | ssl->s3->tmp.in_false_start = 1; |
| 450 | |
| 451 | ssl_free_wbio_buffer(ssl); |
| 452 | ret = 1; |
| 453 | goto end; |
| 454 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 455 | case SSL3_ST_CR_SESSION_TICKET_A: |
David Benjamin | 47749a6 | 2016-06-13 13:56:33 -0400 | [diff] [blame^] | 456 | if (ssl->tlsext_ticket_expected) { |
| 457 | ret = ssl3_get_new_session_ticket(ssl); |
| 458 | if (ret <= 0) { |
| 459 | goto end; |
| 460 | } |
| 461 | } else { |
| 462 | skip = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 463 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 464 | ssl->state = SSL3_ST_CR_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 465 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 467 | case SSL3_ST_CR_CHANGE: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 468 | ret = ssl->method->ssl_read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 469 | if (ret <= 0) { |
| 470 | goto end; |
| 471 | } |
| 472 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 473 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 474 | ret = -1; |
| 475 | goto end; |
| 476 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 477 | ssl->state = SSL3_ST_CR_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 478 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 480 | case SSL3_ST_CR_FINISHED_A: |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 481 | ret = ssl3_get_finished(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 482 | if (ret <= 0) { |
| 483 | goto end; |
| 484 | } |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 485 | ssl->method->received_flight(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 486 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 487 | if (ssl->hit) { |
| 488 | ssl->state = SSL3_ST_CW_CHANGE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 489 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 490 | ssl->state = SSL_ST_OK; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 491 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 492 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 494 | case SSL3_ST_CW_FLUSH: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 495 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 496 | ssl->rwstate = SSL_WRITING; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 497 | ret = -1; |
| 498 | goto end; |
| 499 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 500 | ssl->state = ssl->s3->tmp.next_state; |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 501 | if (ssl->state != SSL_ST_OK) { |
| 502 | ssl->method->expect_flight(ssl); |
| 503 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 504 | break; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 506 | case SSL_ST_OK: |
| 507 | /* clean a few things up */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 508 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 510 | /* |init_buf| cannot be released in DTLS because post-handshake |
| 511 | * retransmit relies on that buffer being available as scratch space. |
| 512 | * |
| 513 | * TODO(davidben): Fix this. */ |
| 514 | if (!SSL_IS_DTLS(ssl)) { |
| 515 | BUF_MEM_free(ssl->init_buf); |
| 516 | ssl->init_buf = NULL; |
| 517 | ssl->init_num = 0; |
| 518 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 520 | /* Remove write buffering now. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 521 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 522 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 523 | const int is_initial_handshake = !ssl->s3->initial_handshake_complete; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 524 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 525 | ssl->s3->tmp.in_false_start = 0; |
| 526 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 527 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 528 | if (is_initial_handshake) { |
| 529 | /* Renegotiations do not participate in session resumption. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 530 | ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 531 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 532 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 533 | if (SSL_IS_DTLS(ssl)) { |
| 534 | ssl->d1->handshake_read_seq = 0; |
| 535 | ssl->d1->next_handshake_write_seq = 0; |
| 536 | } |
| 537 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 538 | ret = 1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 539 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 540 | goto end; |
| 541 | |
| 542 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 543 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 544 | ret = -1; |
| 545 | goto end; |
| 546 | } |
| 547 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 548 | if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) { |
| 549 | int new_state = ssl->state; |
| 550 | ssl->state = state; |
| 551 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1); |
| 552 | ssl->state = new_state; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 553 | } |
| 554 | skip = 0; |
| 555 | } |
| 556 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | end: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 558 | BUF_MEM_free(buf); |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 559 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 560 | return ret; |
| 561 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 563 | static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) { |
| 564 | /* Prepare disabled cipher masks. */ |
| 565 | ssl_set_client_disabled(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 566 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 567 | CBB child; |
| 568 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 569 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 570 | } |
| 571 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 572 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
| 573 | |
| 574 | int any_enabled = 0; |
| 575 | size_t i; |
| 576 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 577 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 578 | /* Skip disabled ciphers */ |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 579 | if ((cipher->algorithm_mkey & ssl->cert->mask_k) || |
| 580 | (cipher->algorithm_auth & ssl->cert->mask_a)) { |
| 581 | continue; |
| 582 | } |
Nick Harper | 1fd39d8 | 2016-06-14 18:14:35 -0700 | [diff] [blame] | 583 | /* TODO(davidben): Also check |SSL_CIPHER_get_max_version| against the |
| 584 | * minimum enabled version. See https://crbug.com/boringssl/66. */ |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 585 | if (SSL_CIPHER_get_min_version(cipher) > |
| 586 | ssl3_version_from_wire(ssl, ssl->client_version)) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 587 | continue; |
| 588 | } |
| 589 | any_enabled = 1; |
| 590 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 591 | return 0; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* If all ciphers were disabled, return the error to the caller. */ |
| 596 | if (!any_enabled) { |
| 597 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 602 | * added. */ |
| 603 | if (ssl->client_version == SSL3_VERSION && |
| 604 | !ssl->s3->initial_handshake_complete) { |
| 605 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 606 | return 0; |
| 607 | } |
| 608 | /* The renegotiation extension is required to be at index zero. */ |
| 609 | ssl->s3->tmp.extensions.sent |= (1u << 0); |
| 610 | } |
| 611 | |
| 612 | if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) && |
| 613 | !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | return CBB_flush(out); |
| 618 | } |
| 619 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 620 | static int ssl3_send_client_hello(SSL *ssl) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 621 | if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) { |
| 622 | return ssl_do_write(ssl); |
| 623 | } |
| 624 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 625 | /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we |
| 626 | * may send multiple ClientHellos if we receive HelloVerifyRequest. */ |
| 627 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 628 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 629 | return -1; |
| 630 | } |
| 631 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 632 | CBB cbb; |
| 633 | CBB_zero(&cbb); |
| 634 | |
| 635 | assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A); |
| 636 | if (!ssl->s3->have_version) { |
| 637 | uint16_t max_version = ssl3_get_max_client_version(ssl); |
| 638 | /* Disabling all versions is silly: return an error. */ |
| 639 | if (max_version == 0) { |
| 640 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 641 | goto err; |
| 642 | } |
| 643 | |
| 644 | ssl->version = max_version; |
| 645 | /* Only set |ssl->client_version| on the initial handshake. Renegotiations, |
| 646 | * although locked to a version, reuse the value. When using the plain RSA |
| 647 | * key exchange, the ClientHello version is checked in the premaster secret. |
| 648 | * Some servers fail when this value changes. */ |
| 649 | ssl->client_version = max_version; |
| 650 | } |
| 651 | |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 652 | /* If the configured session has expired or was created at a disabled |
| 653 | * version, drop it. */ |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 654 | if (ssl->session != NULL && |
| 655 | (ssl->session->session_id_length == 0 || ssl->session->not_resumable || |
David Benjamin | 1269ddd | 2015-10-18 15:18:55 -0400 | [diff] [blame] | 656 | ssl->session->timeout < (long)(time(NULL) - ssl->session->time) || |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 657 | !ssl3_is_version_enabled(ssl, ssl->session->ssl_version))) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 658 | SSL_set_session(ssl, NULL); |
| 659 | } |
| 660 | |
| 661 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 662 | * renegerate the client_random. The random must be reused. */ |
| 663 | if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) && |
| 664 | !ssl_fill_hello_random(ssl->s3->client_random, |
| 665 | sizeof(ssl->s3->client_random), 0 /* client */)) { |
| 666 | goto err; |
| 667 | } |
| 668 | |
| 669 | /* Renegotiations do not participate in session resumption. */ |
| 670 | int has_session = ssl->session != NULL && |
| 671 | !ssl->s3->initial_handshake_complete; |
| 672 | |
| 673 | CBB child; |
| 674 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 675 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 676 | !CBB_add_u16(&cbb, ssl->client_version) || |
| 677 | !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 678 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 679 | (has_session && |
| 680 | !CBB_add_bytes(&child, ssl->session->session_id, |
| 681 | ssl->session->session_id_length))) { |
| 682 | goto err; |
| 683 | } |
| 684 | |
| 685 | if (SSL_IS_DTLS(ssl)) { |
| 686 | if (!CBB_add_u8_length_prefixed(&cbb, &child) || |
| 687 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
| 688 | goto err; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | size_t length; |
| 693 | if (!ssl3_write_client_cipher_list(ssl, &cbb) || |
| 694 | !CBB_add_u8(&cbb, 1 /* one compression method */) || |
| 695 | !CBB_add_u8(&cbb, 0 /* null compression */) || |
| 696 | !ssl_add_clienthello_tlsext(ssl, &cbb, |
| 697 | CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) || |
| 698 | !CBB_finish(&cbb, NULL, &length) || |
| 699 | !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) { |
| 700 | goto err; |
| 701 | } |
| 702 | |
| 703 | ssl->state = SSL3_ST_CW_CLNT_HELLO_B; |
| 704 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 705 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 706 | err: |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 707 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 708 | return -1; |
| 709 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 711 | static int dtls1_get_hello_verify(SSL *ssl) { |
| 712 | long n; |
| 713 | int al, ok = 0; |
| 714 | CBS hello_verify_request, cookie; |
| 715 | uint16_t server_version; |
| 716 | |
| 717 | n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
| 718 | |
| 719 | if (!ok) { |
| 720 | return n; |
| 721 | } |
| 722 | |
| 723 | if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 724 | ssl->d1->send_cookie = 0; |
| 725 | ssl->s3->tmp.reuse_message = 1; |
| 726 | return 1; |
| 727 | } |
| 728 | |
| 729 | CBS_init(&hello_verify_request, ssl->init_msg, n); |
| 730 | |
| 731 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 732 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 733 | CBS_len(&hello_verify_request) != 0) { |
| 734 | al = SSL_AD_DECODE_ERROR; |
| 735 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 736 | goto f_err; |
| 737 | } |
| 738 | |
| 739 | if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) { |
| 740 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 741 | goto f_err; |
| 742 | } |
| 743 | |
| 744 | memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
| 745 | ssl->d1->cookie_len = CBS_len(&cookie); |
| 746 | |
| 747 | ssl->d1->send_cookie = 1; |
| 748 | return 1; |
| 749 | |
| 750 | f_err: |
| 751 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 752 | return -1; |
| 753 | } |
| 754 | |
| 755 | static int ssl3_get_server_hello(SSL *ssl) { |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 756 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 757 | const SSL_CIPHER *c; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 758 | CERT *ct = ssl->cert; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 759 | int al = SSL_AD_INTERNAL_ERROR, ok; |
| 760 | long n; |
| 761 | CBS server_hello, server_random, session_id; |
| 762 | uint16_t server_version, cipher_suite; |
| 763 | uint8_t compression_method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 764 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 765 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO, ssl_hash_message, |
| 766 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 767 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 768 | if (!ok) { |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 769 | uint32_t err = ERR_peek_error(); |
| 770 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 771 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 772 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 773 | * in response to ClientHello. This matches NSS's client behavior and |
| 774 | * gives a better error on a (probable) failure to negotiate initial |
| 775 | * parameters. Note: this error code comes after the original one. |
| 776 | * |
| 777 | * See https://crbug.com/446505. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 778 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 779 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 780 | return n; |
| 781 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 782 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 783 | CBS_init(&server_hello, ssl->init_msg, n); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 784 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 785 | if (!CBS_get_u16(&server_hello, &server_version) || |
| 786 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
| 787 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 788 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 789 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 790 | !CBS_get_u8(&server_hello, &compression_method)) { |
| 791 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 792 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 793 | goto f_err; |
| 794 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 795 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 796 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 797 | if (!ssl->s3->have_version) { |
| 798 | if (!ssl3_is_version_enabled(ssl, server_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 799 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 800 | al = SSL_AD_PROTOCOL_VERSION; |
| 801 | goto f_err; |
| 802 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 803 | ssl->version = server_version; |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 804 | ssl->s3->enc_method = ssl3_get_enc_method(server_version); |
| 805 | assert(ssl->s3->enc_method != NULL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 806 | /* 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] | 807 | * fixed. Begin enforcing the record-layer version. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 808 | ssl->s3->have_version = 1; |
| 809 | } else if (server_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 810 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 811 | al = SSL_AD_PROTOCOL_VERSION; |
| 812 | goto f_err; |
| 813 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 814 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 815 | /* Copy over the server random. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 816 | memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 817 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 818 | assert(ssl->session == NULL || ssl->session->session_id_length > 0); |
| 819 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
| 820 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 821 | ssl->session->session_id_length)) { |
| 822 | if (ssl->sid_ctx_length != ssl->session->sid_ctx_length || |
| 823 | memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 824 | /* actually a client application bug */ |
| 825 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 826 | OPENSSL_PUT_ERROR(SSL, |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 827 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 828 | goto f_err; |
| 829 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 830 | ssl->hit = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 831 | } else { |
| 832 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 833 | * fill out. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 834 | ssl->hit = 0; |
| 835 | if (!ssl_get_new_session(ssl, 0 /* client */)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 836 | goto f_err; |
| 837 | } |
| 838 | /* Note: session_id could be empty. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 839 | ssl->session->session_id_length = CBS_len(&session_id); |
| 840 | memcpy(ssl->session->session_id, CBS_data(&session_id), |
| 841 | CBS_len(&session_id)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 842 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 843 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 844 | c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 845 | if (c == NULL) { |
| 846 | /* unknown cipher */ |
| 847 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 848 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 849 | goto f_err; |
| 850 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 851 | /* If the cipher is disabled then we didn't sent it in the ClientHello, so if |
| 852 | * the server selected it, it's an error. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 853 | 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] | 854 | SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) || |
| 855 | SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 856 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 857 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 858 | goto f_err; |
| 859 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 860 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 861 | sk = ssl_get_ciphers_by_id(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 862 | if (!sk_SSL_CIPHER_find(sk, NULL, c)) { |
| 863 | /* we did not say we would use this cipher */ |
| 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_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 866 | goto f_err; |
| 867 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 868 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 869 | if (ssl->hit) { |
| 870 | if (ssl->session->cipher != c) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 871 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 872 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 873 | goto f_err; |
| 874 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 875 | if (ssl->session->ssl_version != ssl->version) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 876 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 877 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 878 | goto f_err; |
| 879 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 880 | } else { |
| 881 | ssl->session->cipher = c; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 882 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 883 | ssl->s3->tmp.new_cipher = c; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 884 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 885 | /* Now that the cipher is known, initialize the handshake hash. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 886 | if (!ssl3_init_handshake_hash(ssl)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 887 | goto f_err; |
| 888 | } |
| 889 | |
David Benjamin | 5f04b65 | 2015-05-26 17:30:10 -0400 | [diff] [blame] | 890 | /* If doing a full handshake with TLS 1.2, the server may request a client |
| 891 | * certificate which requires hashing the handshake transcript under a |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 892 | * different hash. Otherwise, the handshake buffer may be released. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 893 | if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION || |
| 894 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 895 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 896 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 897 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 898 | /* Only the NULL compression algorithm is supported. */ |
| 899 | if (compression_method != 0) { |
| 900 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 901 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 902 | goto f_err; |
| 903 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 904 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 905 | /* TLS extensions */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 906 | if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 907 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 908 | goto err; |
| 909 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 911 | /* There should be nothing left over in the record. */ |
| 912 | if (CBS_len(&server_hello) != 0) { |
| 913 | /* wrong packet length */ |
| 914 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 915 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 916 | goto f_err; |
| 917 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 918 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 919 | if (ssl->hit && |
| 920 | ssl->s3->tmp.extended_master_secret != |
| 921 | ssl->session->extended_master_secret) { |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 922 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 923 | if (ssl->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 924 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 925 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 926 | 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] | 927 | } |
| 928 | goto f_err; |
| 929 | } |
| 930 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 931 | return 1; |
| 932 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 933 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 934 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 935 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 936 | return -1; |
| 937 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 938 | |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 939 | /* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server |
| 940 | * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the |
| 941 | * error queue. */ |
| 942 | static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 943 | int ret = 0; |
| 944 | EVP_PKEY *pkey = X509_get_pubkey(leaf); |
David Benjamin | d1c1c8e | 2015-06-08 18:36:21 -0400 | [diff] [blame] | 945 | if (pkey == NULL) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 946 | goto err; |
| 947 | } |
| 948 | |
| 949 | /* Check the certificate's type matches the cipher. */ |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 950 | const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 951 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 952 | assert(expected_type != EVP_PKEY_NONE); |
| 953 | if (pkey->type != expected_type) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 954 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 955 | goto err; |
| 956 | } |
| 957 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 958 | if (cipher->algorithm_auth & SSL_aECDSA) { |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 959 | /* TODO(davidben): This behavior is preserved from upstream. Should key |
| 960 | * usages be checked in other cases as well? */ |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 961 | /* This call populates the ex_flags field correctly */ |
| 962 | X509_check_purpose(leaf, -1, 0); |
| 963 | if ((leaf->ex_flags & EXFLAG_KUSAGE) && |
| 964 | !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 965 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 966 | goto err; |
| 967 | } |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 968 | |
| 969 | if (!tls1_check_ec_cert(ssl, leaf)) { |
| 970 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
| 971 | goto err; |
| 972 | } |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | ret = 1; |
| 976 | |
| 977 | err: |
| 978 | EVP_PKEY_free(pkey); |
| 979 | return ret; |
| 980 | } |
| 981 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 982 | static int ssl3_get_server_certificate(SSL *ssl) { |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 983 | int al, ok, ret = -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 984 | unsigned long n; |
| 985 | X509 *x = NULL; |
| 986 | STACK_OF(X509) *sk = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 987 | EVP_PKEY *pkey = NULL; |
| 988 | CBS cbs, certificate_list; |
| 989 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 990 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 991 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message, |
| 992 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 993 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 994 | if (!ok) { |
| 995 | return n; |
| 996 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 997 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 998 | CBS_init(&cbs, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 999 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1000 | sk = sk_X509_new_null(); |
| 1001 | if (sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1002 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1003 | goto err; |
| 1004 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1005 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1006 | if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) || |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 1007 | CBS_len(&certificate_list) == 0 || |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1008 | CBS_len(&cbs) != 0) { |
| 1009 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1010 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1011 | goto f_err; |
| 1012 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1013 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1014 | while (CBS_len(&certificate_list) > 0) { |
| 1015 | CBS certificate; |
| 1016 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 1017 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1018 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1019 | goto f_err; |
| 1020 | } |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1021 | /* A u24 length cannot overflow a long. */ |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1022 | data = CBS_data(&certificate); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1023 | x = d2i_X509(NULL, &data, (long)CBS_len(&certificate)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1024 | if (x == NULL) { |
| 1025 | al = SSL_AD_BAD_CERTIFICATE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1026 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1027 | goto f_err; |
| 1028 | } |
| 1029 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 1030 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1031 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1032 | goto f_err; |
| 1033 | } |
| 1034 | if (!sk_X509_push(sk, x)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1035 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1036 | goto err; |
| 1037 | } |
| 1038 | x = NULL; |
| 1039 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1041 | X509 *leaf = sk_X509_value(sk, 0); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1042 | if (!ssl3_check_leaf_certificate(ssl, leaf)) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1043 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1044 | goto f_err; |
| 1045 | } |
| 1046 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1047 | /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes |
| 1048 | * the leaf. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1049 | sk_X509_pop_free(ssl->session->cert_chain, X509_free); |
| 1050 | ssl->session->cert_chain = sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1051 | sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1052 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1053 | X509_free(ssl->session->peer); |
| 1054 | ssl->session->peer = X509_up_ref(leaf); |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1055 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1056 | ssl->session->verify_result = ssl->verify_result; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1057 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1058 | ret = 1; |
| 1059 | |
| 1060 | if (0) { |
| 1061 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1062 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | err: |
| 1066 | EVP_PKEY_free(pkey); |
| 1067 | X509_free(x); |
| 1068 | sk_X509_pop_free(sk, X509_free); |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1072 | static int ssl3_get_cert_status(SSL *ssl) { |
| 1073 | int ok, al; |
| 1074 | long n; |
| 1075 | CBS certificate_status, ocsp_response; |
| 1076 | uint8_t status_type; |
| 1077 | |
| 1078 | n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
| 1079 | |
| 1080 | if (!ok) { |
| 1081 | return n; |
| 1082 | } |
| 1083 | |
| 1084 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { |
| 1085 | /* A server may send status_request in ServerHello and then change |
| 1086 | * its mind about sending CertificateStatus. */ |
| 1087 | ssl->s3->tmp.reuse_message = 1; |
| 1088 | return 1; |
| 1089 | } |
| 1090 | |
| 1091 | CBS_init(&certificate_status, ssl->init_msg, n); |
| 1092 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1093 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1094 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1095 | CBS_len(&ocsp_response) == 0 || |
| 1096 | CBS_len(&certificate_status) != 0) { |
| 1097 | al = SSL_AD_DECODE_ERROR; |
| 1098 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1099 | goto f_err; |
| 1100 | } |
| 1101 | |
| 1102 | if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response, |
| 1103 | &ssl->session->ocsp_response_length)) { |
| 1104 | al = SSL_AD_INTERNAL_ERROR; |
| 1105 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1106 | goto f_err; |
| 1107 | } |
| 1108 | return 1; |
| 1109 | |
| 1110 | f_err: |
| 1111 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 1112 | return -1; |
| 1113 | } |
| 1114 | |
| 1115 | static int ssl3_verify_server_cert(SSL *ssl) { |
| 1116 | int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain); |
| 1117 | if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) { |
| 1118 | int al = ssl_verify_alarm_type(ssl->verify_result); |
| 1119 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 1120 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1121 | } else { |
| 1122 | ret = 1; |
| 1123 | ERR_clear_error(); /* but we keep ssl->verify_result */ |
| 1124 | } |
| 1125 | |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1129 | static int ssl3_get_server_key_exchange(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1130 | EVP_MD_CTX md_ctx; |
| 1131 | int al, ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1132 | EVP_PKEY *pkey = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1133 | DH *dh = NULL; |
| 1134 | EC_KEY *ecdh = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1135 | EC_POINT *srvr_ecpoint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1136 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1137 | 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] | 1138 | if (!ok) { |
| 1139 | return n; |
| 1140 | } |
| 1141 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1142 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 1143 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1144 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1145 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1146 | return -1; |
| 1147 | } |
| 1148 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 1149 | /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no |
| 1150 | * identity hint. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1151 | if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1152 | /* TODO(davidben): This should be reset in one place with the rest of the |
| 1153 | * handshake state. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1154 | OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint); |
| 1155 | ssl->s3->tmp.peer_psk_identity_hint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1156 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1157 | ssl->s3->tmp.reuse_message = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1158 | return 1; |
| 1159 | } |
| 1160 | |
| 1161 | /* Retain a copy of the original CBS to compute the signature over. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1162 | CBS server_key_exchange; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1163 | CBS_init(&server_key_exchange, ssl->init_msg, n); |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1164 | CBS server_key_exchange_orig = server_key_exchange; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1165 | |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1166 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1167 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1168 | EVP_MD_CTX_init(&md_ctx); |
| 1169 | |
| 1170 | if (alg_a & SSL_aPSK) { |
| 1171 | CBS psk_identity_hint; |
| 1172 | |
| 1173 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1174 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1175 | &psk_identity_hint)) { |
| 1176 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1177 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1178 | goto f_err; |
| 1179 | } |
| 1180 | |
| 1181 | /* Store PSK identity hint for later use, hint is used in |
| 1182 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1183 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1184 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1185 | * |
| 1186 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1187 | * a specific identity. */ |
| 1188 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1189 | CBS_contains_zero_byte(&psk_identity_hint)) { |
| 1190 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1191 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1192 | goto f_err; |
| 1193 | } |
| 1194 | |
| 1195 | /* Save the identity hint as a C string. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1196 | 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] | 1197 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1198 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1199 | goto f_err; |
| 1200 | } |
| 1201 | } |
| 1202 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1203 | if (alg_k & SSL_kDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1204 | CBS dh_p, dh_g, dh_Ys; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1205 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1206 | CBS_len(&dh_p) == 0 || |
| 1207 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1208 | CBS_len(&dh_g) == 0 || |
| 1209 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1210 | CBS_len(&dh_Ys) == 0) { |
| 1211 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1212 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1213 | goto f_err; |
| 1214 | } |
| 1215 | |
| 1216 | dh = DH_new(); |
| 1217 | if (dh == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1218 | goto err; |
| 1219 | } |
| 1220 | |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1221 | dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL); |
| 1222 | 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] | 1223 | if (dh->p == NULL || dh->g == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1224 | goto err; |
| 1225 | } |
| 1226 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1227 | ssl->session->key_exchange_info = DH_num_bits(dh); |
| 1228 | if (ssl->session->key_exchange_info < 1024) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1229 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1230 | goto err; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1231 | } else if (ssl->session->key_exchange_info > 4096) { |
David Benjamin | cd24a39 | 2015-11-11 13:23:05 -0800 | [diff] [blame] | 1232 | /* Overly large DHE groups are prohibitively expensive, so enforce a limit |
| 1233 | * to prevent a server from causing us to perform too expensive of a |
| 1234 | * computation. */ |
| 1235 | OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG); |
| 1236 | goto err; |
| 1237 | } |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1238 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1239 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1240 | dh = NULL; |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1241 | |
| 1242 | /* Save the peer public key for later. */ |
| 1243 | size_t peer_key_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1244 | 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] | 1245 | goto err; |
| 1246 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1247 | /* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len |
| 1248 | * fits in a uint16_t. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1249 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1250 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1251 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1252 | /* Parse the server parameters. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1253 | uint8_t group_type; |
| 1254 | uint16_t group_id; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1255 | CBS point; |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1256 | if (!CBS_get_u8(&server_key_exchange, &group_type) || |
| 1257 | group_type != NAMED_CURVE_TYPE || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1258 | !CBS_get_u16(&server_key_exchange, &group_id) || |
| 1259 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1260 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1261 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1262 | goto f_err; |
| 1263 | } |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1264 | ssl->session->key_exchange_info = group_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1265 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1266 | /* Ensure the group is consistent with preferences. */ |
| 1267 | if (!tls1_check_group_id(ssl, group_id)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1268 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1269 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1270 | goto f_err; |
| 1271 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1272 | |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1273 | /* Initialize ECDH and save the peer public key for later. */ |
| 1274 | size_t peer_key_len; |
| 1275 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) || |
| 1276 | !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
David Benjamin | c1cc858 | 2015-12-17 01:22:15 -0500 | [diff] [blame] | 1277 | goto err; |
| 1278 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1279 | /* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len |
| 1280 | * fits in a uint16_t. */ |
| 1281 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1282 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
| 1283 | } else if (alg_k & SSL_kCECPQ1) { |
| 1284 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1)) { |
| 1285 | goto err; |
| 1286 | } |
| 1287 | CBS key; |
| 1288 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1289 | al = SSL_AD_DECODE_ERROR; |
| 1290 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1291 | goto f_err; |
| 1292 | } |
| 1293 | |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1294 | size_t peer_key_len; |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1295 | if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1296 | goto err; |
| 1297 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1298 | /* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len |
| 1299 | * fits in a uint16_t. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1300 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1301 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1302 | } else if (!(alg_k & SSL_kPSK)) { |
| 1303 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1304 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1305 | goto f_err; |
| 1306 | } |
| 1307 | |
| 1308 | /* At this point, |server_key_exchange| contains the signature, if any, while |
| 1309 | * |server_key_exchange_orig| contains the entire message. From that, derive |
| 1310 | * a CBS containing just the parameter. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1311 | CBS parameter; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1312 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1313 | CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange)); |
| 1314 | |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1315 | /* ServerKeyExchange should be signed by the server's public key. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 1316 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1317 | pkey = X509_get_pubkey(ssl->session->peer); |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1318 | if (pkey == NULL) { |
| 1319 | goto err; |
| 1320 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1321 | |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1322 | const EVP_MD *md = NULL; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1323 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1324 | uint8_t hash, signature; |
| 1325 | if (!CBS_get_u8(&server_key_exchange, &hash) || |
| 1326 | !CBS_get_u8(&server_key_exchange, &signature)) { |
| 1327 | al = SSL_AD_DECODE_ERROR; |
| 1328 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1329 | goto f_err; |
| 1330 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1331 | if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1332 | goto f_err; |
| 1333 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1334 | ssl->s3->tmp.server_key_exchange_hash = hash; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1335 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1336 | md = EVP_md5_sha1(); |
| 1337 | } else { |
| 1338 | md = EVP_sha1(); |
| 1339 | } |
| 1340 | |
| 1341 | /* The last field in |server_key_exchange| is the signature. */ |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1342 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1343 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1344 | CBS_len(&server_key_exchange) != 0) { |
| 1345 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1346 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1347 | goto f_err; |
| 1348 | } |
| 1349 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1350 | int sig_ok = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) && |
| 1351 | EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random, |
| 1352 | SSL3_RANDOM_SIZE) && |
| 1353 | EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random, |
| 1354 | SSL3_RANDOM_SIZE) && |
| 1355 | EVP_DigestVerifyUpdate(&md_ctx, CBS_data(¶meter), |
| 1356 | CBS_len(¶meter)) && |
| 1357 | EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), |
| 1358 | CBS_len(&signature)); |
| 1359 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1360 | sig_ok = 1; |
| 1361 | ERR_clear_error(); |
| 1362 | #endif |
| 1363 | if (!sig_ok) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1364 | /* bad signature */ |
| 1365 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1366 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1367 | goto f_err; |
| 1368 | } |
| 1369 | } else { |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1370 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1371 | assert(alg_a == SSL_aPSK); |
| 1372 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1373 | if (CBS_len(&server_key_exchange) > 0) { |
| 1374 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1375 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1376 | goto f_err; |
| 1377 | } |
| 1378 | } |
| 1379 | EVP_PKEY_free(pkey); |
| 1380 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1381 | return 1; |
| 1382 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1383 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1384 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1385 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1386 | EVP_PKEY_free(pkey); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1387 | DH_free(dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1388 | EC_POINT_free(srvr_ecpoint); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1389 | EC_KEY_free(ecdh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1390 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1391 | return -1; |
| 1392 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1393 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1394 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 1395 | return X509_NAME_cmp(*a, *b); |
| 1396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1397 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1398 | static int ssl3_get_certificate_request(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1399 | int ok, ret = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1400 | X509_NAME *xn = NULL; |
| 1401 | STACK_OF(X509_NAME) *ca_sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1402 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1403 | 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] | 1404 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1405 | if (!ok) { |
| 1406 | return n; |
| 1407 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1408 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1409 | ssl->s3->tmp.cert_req = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1410 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1411 | if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1412 | ssl->s3->tmp.reuse_message = 1; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1413 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1414 | * client auth. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1415 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1416 | return 1; |
| 1417 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1418 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1419 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 1420 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1421 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1422 | goto err; |
| 1423 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1424 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1425 | CBS cbs; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1426 | CBS_init(&cbs, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1427 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1428 | ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
| 1429 | if (ca_sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1430 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1431 | goto err; |
| 1432 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1433 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1434 | /* get the certificate types */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1435 | CBS certificate_types; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1436 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1437 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1438 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1439 | goto err; |
| 1440 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1441 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1442 | if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types, |
| 1443 | &ssl->s3->tmp.num_certificate_types)) { |
| 1444 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1445 | goto err; |
| 1446 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1447 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1448 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1449 | CBS supported_signature_algorithms; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1450 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1451 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
| 1452 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1453 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1454 | goto err; |
| 1455 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1456 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1457 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1458 | /* get the CA RDNs */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1459 | CBS certificate_authorities; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1460 | if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1461 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1462 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1463 | goto err; |
| 1464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1465 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1466 | while (CBS_len(&certificate_authorities) > 0) { |
| 1467 | CBS distinguished_name; |
| 1468 | if (!CBS_get_u16_length_prefixed(&certificate_authorities, |
| 1469 | &distinguished_name)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1470 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1471 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1472 | goto err; |
| 1473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1474 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1475 | const uint8_t *data = CBS_data(&distinguished_name); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1476 | /* A u16 length cannot overflow a long. */ |
| 1477 | xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name)); |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1478 | if (xn == NULL || |
| 1479 | data != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1480 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1481 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1482 | goto err; |
| 1483 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1484 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1485 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1486 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1487 | goto err; |
| 1488 | } |
David Benjamin | 6b6e0b2 | 2016-03-11 19:30:29 -0500 | [diff] [blame] | 1489 | xn = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1490 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1491 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1492 | /* we should setup a certificate to return.... */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1493 | ssl->s3->tmp.cert_req = 1; |
| 1494 | sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free); |
| 1495 | ssl->s3->tmp.ca_names = ca_sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1496 | ca_sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1497 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1498 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1499 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1500 | err: |
David Benjamin | 6b6e0b2 | 2016-03-11 19:30:29 -0500 | [diff] [blame] | 1501 | X509_NAME_free(xn); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1502 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1503 | return ret; |
| 1504 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1505 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1506 | static int ssl3_get_server_hello_done(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1507 | int ok; |
| 1508 | long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1509 | |
David Benjamin | 3dcec45 | 2016-06-07 17:13:28 -0400 | [diff] [blame] | 1510 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE, |
| 1511 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1512 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1513 | if (!ok) { |
| 1514 | return n; |
| 1515 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1516 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1517 | if (n > 0) { |
| 1518 | /* should contain no data */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1519 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1520 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1521 | return -1; |
| 1522 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1523 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1524 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1525 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1526 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1527 | /* ssl3_has_client_certificate returns true if a client certificate is |
| 1528 | * configured. */ |
| 1529 | static int ssl3_has_client_certificate(SSL *ssl) { |
| 1530 | return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl); |
| 1531 | } |
| 1532 | |
| 1533 | static int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, |
| 1534 | EVP_PKEY **out_pkey) { |
| 1535 | if (ssl->ctx->client_cert_cb == NULL) { |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | int ret = ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey); |
| 1540 | if (ret <= 0) { |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
| 1544 | assert(*out_x509 != NULL); |
| 1545 | assert(*out_pkey != NULL); |
| 1546 | return 1; |
| 1547 | } |
| 1548 | |
| 1549 | static int ssl3_send_client_certificate(SSL *ssl) { |
| 1550 | if (ssl->state == SSL3_ST_CW_CERT_A) { |
| 1551 | /* Call cert_cb to update the certificate. */ |
| 1552 | if (ssl->cert->cert_cb) { |
| 1553 | int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1554 | if (ret < 0) { |
| 1555 | ssl->rwstate = SSL_X509_LOOKUP; |
| 1556 | return -1; |
| 1557 | } |
| 1558 | if (ret == 0) { |
| 1559 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1560 | return -1; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | if (ssl3_has_client_certificate(ssl)) { |
| 1565 | ssl->state = SSL3_ST_CW_CERT_C; |
| 1566 | } else { |
| 1567 | ssl->state = SSL3_ST_CW_CERT_B; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | if (ssl->state == SSL3_ST_CW_CERT_B) { |
| 1572 | /* Call client_cert_cb to update the certificate. */ |
| 1573 | X509 *x509 = NULL; |
| 1574 | EVP_PKEY *pkey = NULL; |
| 1575 | int ret = ssl_do_client_cert_cb(ssl, &x509, &pkey); |
| 1576 | if (ret < 0) { |
| 1577 | ssl->rwstate = SSL_X509_LOOKUP; |
| 1578 | return -1; |
| 1579 | } |
| 1580 | |
| 1581 | int setup_error = ret == 1 && (!SSL_use_certificate(ssl, x509) || |
| 1582 | !SSL_use_PrivateKey(ssl, pkey)); |
| 1583 | X509_free(x509); |
| 1584 | EVP_PKEY_free(pkey); |
| 1585 | if (setup_error) { |
| 1586 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1587 | return -1; |
| 1588 | } |
| 1589 | |
| 1590 | ssl->state = SSL3_ST_CW_CERT_C; |
| 1591 | } |
| 1592 | |
| 1593 | if (ssl->state == SSL3_ST_CW_CERT_C) { |
| 1594 | if (!ssl3_has_client_certificate(ssl)) { |
| 1595 | /* Without a client certificate, the handshake buffer may be released. */ |
| 1596 | ssl3_free_handshake_buffer(ssl); |
| 1597 | |
| 1598 | if (ssl->version == SSL3_VERSION) { |
| 1599 | /* In SSL 3.0, send no certificate by skipping both messages. */ |
| 1600 | ssl->s3->tmp.cert_req = 0; |
| 1601 | ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | /* In TLS, send an empty Certificate message. */ |
| 1606 | ssl->s3->tmp.cert_req = 2; |
| 1607 | uint8_t *p = ssl_handshake_start(ssl); |
| 1608 | l2n3(0, p); |
| 1609 | if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) { |
| 1610 | return -1; |
| 1611 | } |
| 1612 | } else if (!ssl3_output_cert_chain(ssl)) { |
| 1613 | return -1; |
| 1614 | } |
| 1615 | ssl->state = SSL3_ST_CW_CERT_D; |
| 1616 | } |
| 1617 | |
| 1618 | assert(ssl->state == SSL3_ST_CW_CERT_D); |
| 1619 | return ssl_do_write(ssl); |
| 1620 | } |
| 1621 | |
Brian Smith | 274341d | 2015-10-08 17:10:15 -1000 | [diff] [blame] | 1622 | OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned), |
| 1623 | SIZE_T_IS_SMALLER_THAN_UNSIGNED); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1624 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1625 | static int ssl3_send_client_key_exchange(SSL *ssl) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1626 | if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) { |
| 1627 | return ssl_do_write(ssl); |
| 1628 | } |
| 1629 | assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A); |
| 1630 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1631 | uint8_t *pms = NULL; |
| 1632 | size_t pms_len = 0; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1633 | CBB cbb; |
| 1634 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1635 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) { |
| 1636 | goto err; |
| 1637 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1638 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1639 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1640 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1641 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1642 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1643 | unsigned psk_len = 0; |
| 1644 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1645 | if (alg_a & SSL_aPSK) { |
| 1646 | if (ssl->psk_client_callback == NULL) { |
| 1647 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
| 1648 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1649 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1650 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1651 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 1652 | memset(identity, 0, sizeof(identity)); |
| 1653 | psk_len = ssl->psk_client_callback( |
| 1654 | ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity), |
| 1655 | psk, sizeof(psk)); |
| 1656 | if (psk_len == 0) { |
| 1657 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1658 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1659 | goto err; |
| 1660 | } |
| 1661 | assert(psk_len <= PSK_MAX_PSK_LEN); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1662 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1663 | OPENSSL_free(ssl->session->psk_identity); |
| 1664 | ssl->session->psk_identity = BUF_strdup(identity); |
| 1665 | if (ssl->session->psk_identity == NULL) { |
| 1666 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1667 | goto err; |
| 1668 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1669 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1670 | /* Write out psk_identity. */ |
| 1671 | CBB child; |
| 1672 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1673 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1674 | OPENSSL_strnlen(identity, sizeof(identity))) || |
| 1675 | !CBB_flush(&cbb)) { |
| 1676 | goto err; |
| 1677 | } |
| 1678 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1679 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1680 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1681 | if (alg_k & SSL_kRSA) { |
| 1682 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1683 | pms = OPENSSL_malloc(pms_len); |
| 1684 | if (pms == NULL) { |
| 1685 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1686 | goto err; |
| 1687 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1688 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1689 | EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer); |
| 1690 | if (pkey == NULL) { |
| 1691 | goto err; |
| 1692 | } |
| 1693 | |
| 1694 | RSA *rsa = EVP_PKEY_get0_RSA(pkey); |
| 1695 | if (rsa == NULL) { |
| 1696 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1697 | EVP_PKEY_free(pkey); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1698 | goto err; |
| 1699 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1700 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1701 | ssl->session->key_exchange_info = EVP_PKEY_bits(pkey); |
| 1702 | EVP_PKEY_free(pkey); |
| 1703 | |
| 1704 | pms[0] = ssl->client_version >> 8; |
| 1705 | pms[1] = ssl->client_version & 0xff; |
| 1706 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1707 | goto err; |
| 1708 | } |
| 1709 | |
| 1710 | CBB child, *enc_pms = &cbb; |
| 1711 | size_t enc_pms_len; |
| 1712 | /* In TLS, there is a length prefix. */ |
| 1713 | if (ssl->version > SSL3_VERSION) { |
| 1714 | if (!CBB_add_u16_length_prefixed(&cbb, &child)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1715 | goto err; |
| 1716 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1717 | enc_pms = &child; |
| 1718 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1719 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1720 | uint8_t *ptr; |
| 1721 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
| 1722 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len, |
| 1723 | RSA_PKCS1_PADDING) || |
| 1724 | /* Log the premaster secret, if logging is enabled. */ |
| 1725 | !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) || |
| 1726 | !CBB_did_write(enc_pms, enc_pms_len) || |
| 1727 | !CBB_flush(&cbb)) { |
| 1728 | goto err; |
| 1729 | } |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1730 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1731 | /* Generate a keypair and serialize the public half. */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1732 | CBB child; |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1733 | if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &cbb, &child)) { |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1734 | goto err; |
| 1735 | } |
| 1736 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1737 | /* Compute the premaster. */ |
| 1738 | uint8_t alert; |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1739 | if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len, |
| 1740 | &alert, ssl->s3->tmp.peer_key, |
| 1741 | ssl->s3->tmp.peer_key_len)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1742 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1743 | goto err; |
| 1744 | } |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1745 | if (!CBB_flush(&cbb)) { |
| 1746 | goto err; |
| 1747 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1748 | |
| 1749 | /* The key exchange state may now be discarded. */ |
| 1750 | SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx); |
| 1751 | OPENSSL_free(ssl->s3->tmp.peer_key); |
| 1752 | ssl->s3->tmp.peer_key = NULL; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1753 | } else if (alg_k & SSL_kPSK) { |
| 1754 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1755 | * the pre-shared key. */ |
| 1756 | pms_len = psk_len; |
| 1757 | pms = OPENSSL_malloc(pms_len); |
| 1758 | if (pms == NULL) { |
| 1759 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1760 | goto err; |
| 1761 | } |
| 1762 | memset(pms, 0, pms_len); |
| 1763 | } else { |
| 1764 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1765 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1766 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1767 | } |
| 1768 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1769 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1770 | * key. */ |
| 1771 | if (alg_a & SSL_aPSK) { |
| 1772 | CBB pms_cbb, child; |
| 1773 | uint8_t *new_pms; |
| 1774 | size_t new_pms_len; |
| 1775 | |
| 1776 | CBB_zero(&pms_cbb); |
| 1777 | if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) || |
| 1778 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1779 | !CBB_add_bytes(&child, pms, pms_len) || |
| 1780 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1781 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1782 | !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) { |
| 1783 | CBB_cleanup(&pms_cbb); |
| 1784 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1785 | goto err; |
| 1786 | } |
| 1787 | OPENSSL_cleanse(pms, pms_len); |
| 1788 | OPENSSL_free(pms); |
| 1789 | pms = new_pms; |
| 1790 | pms_len = new_pms_len; |
| 1791 | } |
| 1792 | |
| 1793 | /* The message must be added to the finished hash before calculating the |
| 1794 | * master secret. */ |
| 1795 | size_t length; |
| 1796 | if (!CBB_finish(&cbb, NULL, &length) || |
| 1797 | !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) { |
| 1798 | goto err; |
| 1799 | } |
| 1800 | ssl->state = SSL3_ST_CW_KEY_EXCH_B; |
| 1801 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 1802 | ssl->session->master_key_length = |
| 1803 | tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1804 | if (ssl->session->master_key_length == 0) { |
| 1805 | goto err; |
| 1806 | } |
| 1807 | ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret; |
| 1808 | OPENSSL_cleanse(pms, pms_len); |
| 1809 | OPENSSL_free(pms); |
| 1810 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1811 | /* SSL3_ST_CW_KEY_EXCH_B */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1812 | return ssl_do_write(ssl); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1813 | |
| 1814 | err: |
David Benjamin | 2b07fa4 | 2016-03-02 00:23:57 -0500 | [diff] [blame] | 1815 | CBB_cleanup(&cbb); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1816 | if (pms != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1817 | OPENSSL_cleanse(pms, pms_len); |
| 1818 | OPENSSL_free(pms); |
| 1819 | } |
| 1820 | return -1; |
| 1821 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1823 | static int ssl3_send_cert_verify(SSL *ssl) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1824 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) { |
| 1825 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1828 | CBB cbb, child; |
| 1829 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1830 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) { |
| 1831 | goto err; |
| 1832 | } |
| 1833 | |
| 1834 | assert(ssl_has_private_key(ssl)); |
| 1835 | |
| 1836 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
| 1837 | size_t sig_len; |
| 1838 | enum ssl_private_key_result_t sign_result; |
| 1839 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) { |
| 1840 | /* Select and write out the digest type in TLS 1.2. */ |
| 1841 | const EVP_MD *md = NULL; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1842 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1843 | md = tls1_choose_signing_digest(ssl); |
| 1844 | if (!tls12_add_sigandhash(ssl, &cbb, md)) { |
| 1845 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1846 | goto err; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | /* Compute the digest. In TLS 1.1 and below, the digest type is also |
| 1851 | * selected here. */ |
| 1852 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1853 | size_t digest_len; |
| 1854 | if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md, |
| 1855 | ssl_private_key_type(ssl))) { |
| 1856 | goto err; |
| 1857 | } |
| 1858 | |
| 1859 | /* The handshake buffer is no longer necessary. */ |
| 1860 | ssl3_free_handshake_buffer(ssl); |
| 1861 | |
| 1862 | /* Sign the digest. */ |
| 1863 | uint8_t *ptr; |
| 1864 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1865 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1866 | goto err; |
| 1867 | } |
| 1868 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md, |
| 1869 | digest, digest_len); |
| 1870 | } else { |
| 1871 | assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B); |
| 1872 | |
| 1873 | /* Skip over the already written signature algorithm and retry the |
| 1874 | * signature. */ |
| 1875 | uint8_t *ptr; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1876 | if ((ssl3_protocol_version(ssl) >= TLS1_2_VERSION && |
| 1877 | !CBB_did_write(&cbb, 2)) || |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1878 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1879 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1880 | goto err; |
| 1881 | } |
| 1882 | sign_result = |
| 1883 | ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len); |
| 1884 | } |
| 1885 | |
| 1886 | switch (sign_result) { |
| 1887 | case ssl_private_key_success: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1888 | break; |
| 1889 | case ssl_private_key_failure: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1890 | goto err; |
| 1891 | case ssl_private_key_retry: |
| 1892 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1893 | ssl->state = SSL3_ST_CW_CERT_VRFY_B; |
| 1894 | goto err; |
| 1895 | } |
| 1896 | |
| 1897 | size_t length; |
| 1898 | if (!CBB_did_write(&child, sig_len) || |
| 1899 | !CBB_finish(&cbb, NULL, &length) || |
| 1900 | !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) { |
| 1901 | goto err; |
| 1902 | } |
| 1903 | |
| 1904 | ssl->state = SSL3_ST_CW_CERT_VRFY_C; |
| 1905 | return ssl_do_write(ssl); |
| 1906 | |
| 1907 | err: |
| 1908 | CBB_cleanup(&cbb); |
| 1909 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1910 | } |
| 1911 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1912 | static int ssl3_send_next_proto(SSL *ssl) { |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1913 | if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) { |
| 1914 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1915 | } |
| 1916 | |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1917 | assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A); |
| 1918 | |
| 1919 | static const uint8_t kZero[32] = {0}; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1920 | 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] | 1921 | |
| 1922 | CBB cbb, child; |
| 1923 | size_t length; |
| 1924 | CBB_zero(&cbb); |
| 1925 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1926 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 1927 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1928 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1929 | ssl->s3->next_proto_negotiated_len) || |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1930 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 1931 | !CBB_add_bytes(&child, kZero, padding_len) || |
| 1932 | !CBB_finish(&cbb, NULL, &length) || |
| 1933 | !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) { |
| 1934 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1935 | CBB_cleanup(&cbb); |
| 1936 | return -1; |
| 1937 | } |
| 1938 | |
| 1939 | ssl->state = SSL3_ST_CW_NEXT_PROTO_B; |
| 1940 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1941 | } |
| 1942 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 1943 | static int ssl3_send_channel_id(SSL *ssl) { |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1944 | if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) { |
| 1945 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1946 | } |
| 1947 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1948 | assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A); |
| 1949 | |
| 1950 | if (ssl->tlsext_channel_id_private == NULL && |
| 1951 | ssl->ctx->channel_id_cb != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1952 | EVP_PKEY *key = NULL; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1953 | ssl->ctx->channel_id_cb(ssl, &key); |
| 1954 | if (key != NULL && |
| 1955 | !SSL_set1_tls_channel_id(ssl, key)) { |
| 1956 | EVP_PKEY_free(key); |
| 1957 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1958 | } |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1959 | EVP_PKEY_free(key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1962 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1963 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1964 | return -1; |
| 1965 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1966 | |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1967 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
| 1968 | if (ec_key == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1969 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1970 | return -1; |
| 1971 | } |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1972 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1973 | int ret = -1; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1974 | BIGNUM *x = BN_new(); |
| 1975 | BIGNUM *y = BN_new(); |
| 1976 | ECDSA_SIG *sig = NULL; |
| 1977 | if (x == NULL || y == NULL || |
| 1978 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 1979 | EC_KEY_get0_public_key(ec_key), |
| 1980 | x, y, NULL)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1981 | goto err; |
| 1982 | } |
| 1983 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1984 | uint8_t digest[EVP_MAX_MD_SIZE]; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 1985 | size_t digest_len; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1986 | if (!tls1_channel_id_hash(ssl, digest, &digest_len)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1987 | goto err; |
| 1988 | } |
| 1989 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1990 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1991 | if (sig == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1992 | goto err; |
| 1993 | } |
| 1994 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1995 | CBB cbb, child; |
| 1996 | size_t length; |
| 1997 | CBB_zero(&cbb); |
| 1998 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1999 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 2000 | !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) || |
| 2001 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 2002 | !BN_bn2cbb_padded(&child, 32, x) || |
| 2003 | !BN_bn2cbb_padded(&child, 32, y) || |
| 2004 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 2005 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2006 | !CBB_finish(&cbb, NULL, &length) || |
Steven Valdez | 1eca1d3 | 2016-05-23 16:29:25 -0400 | [diff] [blame] | 2007 | !ssl_set_handshake_header(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS, |
| 2008 | length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2009 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2010 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2011 | goto err; |
| 2012 | } |
| 2013 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2014 | ssl->state = SSL3_ST_CW_CHANNEL_ID_B; |
| 2015 | ret = ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2016 | |
| 2017 | err: |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2018 | BN_free(x); |
| 2019 | BN_free(y); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2020 | ECDSA_SIG_free(sig); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2021 | return ret; |
| 2022 | } |
| 2023 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2024 | static int ssl3_get_new_session_ticket(SSL *ssl) { |
| 2025 | int ok, al; |
| 2026 | long n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET, |
| 2027 | ssl_hash_message, &ok); |
| 2028 | |
| 2029 | if (!ok) { |
| 2030 | return n; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2031 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2032 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2033 | CBS new_session_ticket, ticket; |
| 2034 | uint32_t ticket_lifetime_hint; |
| 2035 | CBS_init(&new_session_ticket, ssl->init_msg, n); |
| 2036 | if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) || |
| 2037 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 2038 | CBS_len(&new_session_ticket) != 0) { |
| 2039 | al = SSL_AD_DECODE_ERROR; |
| 2040 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 2041 | goto f_err; |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2042 | } |
| 2043 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2044 | if (CBS_len(&ticket) == 0) { |
| 2045 | /* RFC 5077 allows a server to change its mind and send no ticket after |
| 2046 | * negotiating the extension. The value of |tlsext_ticket_expected| is |
| 2047 | * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary |
| 2048 | * update. */ |
| 2049 | ssl->tlsext_ticket_expected = 0; |
| 2050 | return 1; |
| 2051 | } |
| 2052 | |
| 2053 | if (ssl->hit) { |
| 2054 | /* The server is sending a new ticket for an existing session. Sessions are |
| 2055 | * immutable once established, so duplicate all but the ticket of the |
| 2056 | * existing session. */ |
| 2057 | uint8_t *bytes; |
| 2058 | size_t bytes_len; |
| 2059 | if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) { |
| 2060 | goto err; |
| 2061 | } |
| 2062 | SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len); |
| 2063 | OPENSSL_free(bytes); |
| 2064 | if (new_session == NULL) { |
| 2065 | /* This should never happen. */ |
| 2066 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2067 | goto err; |
| 2068 | } |
| 2069 | |
| 2070 | SSL_SESSION_free(ssl->session); |
| 2071 | ssl->session = new_session; |
| 2072 | } |
| 2073 | |
| 2074 | if (!CBS_stow(&ticket, &ssl->session->tlsext_tick, |
| 2075 | &ssl->session->tlsext_ticklen)) { |
| 2076 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 2077 | goto err; |
| 2078 | } |
| 2079 | ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; |
| 2080 | |
| 2081 | /* Generate a session ID for this session based on the session ticket. We use |
| 2082 | * the session ID mechanism for detecting ticket resumption. This also fits in |
| 2083 | * with assumptions elsewhere in OpenSSL.*/ |
| 2084 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id, |
| 2085 | &ssl->session->session_id_length, EVP_sha256(), NULL)) { |
| 2086 | goto err; |
| 2087 | } |
| 2088 | |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2089 | return 1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2090 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 2091 | f_err: |
| 2092 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 2093 | err: |
| 2094 | return -1; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2095 | } |