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