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