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