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