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