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