Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 149 | #include <openssl/ssl.h> |
| 150 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | #include <assert.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 152 | #include <string.h> |
| 153 | |
| 154 | #include <openssl/bn.h> |
| 155 | #include <openssl/buf.h> |
| 156 | #include <openssl/bytestring.h> |
| 157 | #include <openssl/cipher.h> |
| 158 | #include <openssl/dh.h> |
| 159 | #include <openssl/ec.h> |
| 160 | #include <openssl/ecdsa.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 161 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | #include <openssl/evp.h> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 166 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | #include <openssl/rand.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 168 | #include <openssl/x509.h> |
| 169 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 170 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | #include "../crypto/internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 172 | |
| 173 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 174 | static int ssl3_get_client_hello(SSL *ssl); |
| 175 | static int ssl3_send_server_hello(SSL *ssl); |
| 176 | static int ssl3_send_server_certificate(SSL *ssl); |
| 177 | static int ssl3_send_certificate_status(SSL *ssl); |
| 178 | static int ssl3_send_server_key_exchange(SSL *ssl); |
| 179 | static int ssl3_send_certificate_request(SSL *ssl); |
| 180 | static int ssl3_send_server_hello_done(SSL *ssl); |
| 181 | static int ssl3_get_client_certificate(SSL *ssl); |
| 182 | static int ssl3_get_client_key_exchange(SSL *ssl); |
| 183 | static int ssl3_get_cert_verify(SSL *ssl); |
| 184 | static int ssl3_get_next_proto(SSL *ssl); |
| 185 | static int ssl3_get_channel_id(SSL *ssl); |
| 186 | static int ssl3_send_new_session_ticket(SSL *ssl); |
| 187 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 188 | int ssl3_accept(SSL *ssl) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 189 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 190 | int ret = -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 191 | int state, skip = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 192 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 193 | assert(ssl->handshake_func == ssl3_accept); |
| 194 | assert(ssl->server); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 195 | |
| 196 | for (;;) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 197 | state = ssl->state; |
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 | switch (ssl->state) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 200 | case SSL_ST_INIT: |
| 201 | ssl->state = SSL_ST_ACCEPT; |
| 202 | skip = 1; |
| 203 | break; |
| 204 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 205 | case SSL_ST_ACCEPT: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 206 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 207 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 208 | ssl->s3->hs = ssl_handshake_new(tls13_server_handshake); |
| 209 | if (ssl->s3->hs == NULL) { |
| 210 | ret = -1; |
| 211 | goto end; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 212 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 213 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 214 | /* Enable a write buffer. This groups handshake messages within a flight |
| 215 | * into a single write. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 216 | if (!ssl_init_wbio_buffer(ssl)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 217 | ret = -1; |
| 218 | goto end; |
| 219 | } |
| 220 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 221 | if (!ssl3_init_handshake_buffer(ssl)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 222 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 223 | ret = -1; |
| 224 | goto end; |
| 225 | } |
| 226 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 227 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | break; |
| 229 | |
| 230 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 231 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 232 | case SSL3_ST_SR_CLNT_HELLO_C: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 233 | case SSL3_ST_SR_CLNT_HELLO_D: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 234 | ret = ssl3_get_client_hello(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 235 | if (ssl->state == SSL_ST_TLS13) { |
| 236 | break; |
| 237 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 238 | if (ret <= 0) { |
| 239 | goto end; |
| 240 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 241 | ssl->method->received_flight(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 242 | ssl->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | break; |
| 244 | |
| 245 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 246 | case SSL3_ST_SW_SRVR_HELLO_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 247 | ret = ssl3_send_server_hello(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 248 | if (ret <= 0) { |
| 249 | goto end; |
| 250 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 251 | if (ssl->session != NULL) { |
| 252 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 253 | } else { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 254 | ssl->state = SSL3_ST_SW_CERT_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | case SSL3_ST_SW_CERT_A: |
| 259 | case SSL3_ST_SW_CERT_B: |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 260 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 261 | ret = ssl3_send_server_certificate(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | if (ret <= 0) { |
| 263 | goto end; |
| 264 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | } else { |
| 266 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 268 | ssl->state = SSL3_ST_SW_CERT_STATUS_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | break; |
| 270 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 271 | case SSL3_ST_SW_CERT_STATUS_A: |
| 272 | case SSL3_ST_SW_CERT_STATUS_B: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 273 | if (ssl->s3->hs->certificate_status_expected) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 274 | ret = ssl3_send_certificate_status(ssl); |
| 275 | if (ret <= 0) { |
| 276 | goto end; |
| 277 | } |
| 278 | } else { |
| 279 | skip = 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 280 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 281 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 282 | break; |
| 283 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 284 | case SSL3_ST_SW_KEY_EXCH_A: |
| 285 | case SSL3_ST_SW_KEY_EXCH_B: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 286 | case SSL3_ST_SW_KEY_EXCH_C: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 287 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 288 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 289 | /* PSK ciphers send ServerKeyExchange if there is an identity hint. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 290 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) || |
| 291 | ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) { |
| 292 | ret = ssl3_send_server_key_exchange(ssl); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 293 | if (ret <= 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 294 | goto end; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 295 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | } else { |
| 297 | skip = 1; |
| 298 | } |
| 299 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 300 | ssl->state = SSL3_ST_SW_CERT_REQ_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 301 | break; |
| 302 | |
| 303 | case SSL3_ST_SW_CERT_REQ_A: |
| 304 | case SSL3_ST_SW_CERT_REQ_B: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 305 | if (ssl->s3->hs->cert_request) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 306 | ret = ssl3_send_certificate_request(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 307 | if (ret <= 0) { |
| 308 | goto end; |
| 309 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 310 | } else { |
| 311 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 312 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 313 | ssl->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 314 | break; |
| 315 | |
| 316 | case SSL3_ST_SW_SRVR_DONE_A: |
| 317 | case SSL3_ST_SW_SRVR_DONE_B: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 318 | ret = ssl3_send_server_hello_done(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 319 | if (ret <= 0) { |
| 320 | goto end; |
| 321 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 322 | ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 323 | ssl->state = SSL3_ST_SW_FLUSH; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 324 | break; |
| 325 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 326 | case SSL3_ST_SR_CERT_A: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 327 | if (ssl->s3->hs->cert_request) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 328 | ret = ssl3_get_client_certificate(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 329 | if (ret <= 0) { |
| 330 | goto end; |
| 331 | } |
| 332 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 333 | ssl->state = SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | break; |
| 335 | |
| 336 | case SSL3_ST_SR_KEY_EXCH_A: |
| 337 | case SSL3_ST_SR_KEY_EXCH_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 338 | ret = ssl3_get_client_key_exchange(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | if (ret <= 0) { |
| 340 | goto end; |
| 341 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 342 | ssl->state = SSL3_ST_SR_CERT_VRFY_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 343 | break; |
| 344 | |
| 345 | case SSL3_ST_SR_CERT_VRFY_A: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 346 | ret = ssl3_get_cert_verify(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 347 | if (ret <= 0) { |
| 348 | goto end; |
| 349 | } |
| 350 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 351 | ssl->state = SSL3_ST_SR_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 352 | break; |
| 353 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 354 | case SSL3_ST_SR_CHANGE: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 355 | ret = ssl->method->read_change_cipher_spec(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 356 | if (ret <= 0) { |
| 357 | goto end; |
| 358 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 359 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 360 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 361 | ret = -1; |
| 362 | goto end; |
| 363 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 364 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 365 | ssl->state = SSL3_ST_SR_NEXT_PROTO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 366 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 367 | |
| 368 | case SSL3_ST_SR_NEXT_PROTO_A: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 369 | if (ssl->s3->hs->next_proto_neg_seen) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 370 | ret = ssl3_get_next_proto(ssl); |
| 371 | if (ret <= 0) { |
| 372 | goto end; |
| 373 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 374 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 375 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 376 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 377 | ssl->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case SSL3_ST_SR_CHANNEL_ID_A: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 381 | if (ssl->s3->tlsext_channel_id_valid) { |
| 382 | ret = ssl3_get_channel_id(ssl); |
| 383 | if (ret <= 0) { |
| 384 | goto end; |
| 385 | } |
| 386 | } else { |
| 387 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 388 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 389 | ssl->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 390 | break; |
| 391 | |
| 392 | case SSL3_ST_SR_FINISHED_A: |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 393 | ret = ssl3_get_finished(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 394 | if (ret <= 0) { |
| 395 | goto end; |
| 396 | } |
| 397 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 398 | ssl->method->received_flight(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 399 | if (ssl->session != NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 400 | ssl->state = SSL_ST_OK; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 401 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 402 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 403 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 404 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 405 | /* If this is a full handshake with ChannelID then record the handshake |
| 406 | * hashes in |ssl->s3->new_session| in case we need them to verify a |
| 407 | * ChannelID signature on a resumption of this session in the future. */ |
| 408 | if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 409 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 410 | if (ret <= 0) { |
| 411 | goto end; |
| 412 | } |
| 413 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 414 | break; |
| 415 | |
| 416 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 417 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 418 | if (ssl->tlsext_ticket_expected) { |
| 419 | ret = ssl3_send_new_session_ticket(ssl); |
| 420 | if (ret <= 0) { |
| 421 | goto end; |
| 422 | } |
| 423 | } else { |
| 424 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 425 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 426 | ssl->state = SSL3_ST_SW_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 427 | break; |
| 428 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 429 | case SSL3_ST_SW_CHANGE: |
| 430 | ret = ssl->method->send_change_cipher_spec(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 431 | if (ret <= 0) { |
| 432 | goto end; |
| 433 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 434 | ssl->state = SSL3_ST_SW_FINISHED_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 435 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 436 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 437 | ret = -1; |
| 438 | goto end; |
| 439 | } |
| 440 | break; |
| 441 | |
| 442 | case SSL3_ST_SW_FINISHED_A: |
| 443 | case SSL3_ST_SW_FINISHED_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 444 | ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 445 | SSL3_ST_SW_FINISHED_B); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 446 | if (ret <= 0) { |
| 447 | goto end; |
| 448 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 449 | ssl->state = SSL3_ST_SW_FLUSH; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 450 | if (ssl->session != NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 451 | ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 452 | } else { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 453 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 454 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 455 | break; |
| 456 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 457 | case SSL3_ST_SW_FLUSH: |
| 458 | if (BIO_flush(ssl->wbio) <= 0) { |
| 459 | ssl->rwstate = SSL_WRITING; |
| 460 | ret = -1; |
| 461 | goto end; |
| 462 | } |
| 463 | |
| 464 | ssl->state = ssl->s3->tmp.next_state; |
| 465 | if (ssl->state != SSL_ST_OK) { |
| 466 | ssl->method->expect_flight(ssl); |
| 467 | } |
| 468 | break; |
| 469 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 470 | case SSL_ST_TLS13: |
| 471 | ret = tls13_handshake(ssl); |
| 472 | if (ret <= 0) { |
| 473 | goto end; |
| 474 | } |
| 475 | ssl->state = SSL_ST_OK; |
| 476 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 477 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 478 | case SSL_ST_OK: |
| 479 | /* Clean a few things up. */ |
| 480 | ssl3_cleanup_key_block(ssl); |
| 481 | ssl->method->release_current_message(ssl, 1 /* free_buffer */); |
| 482 | |
| 483 | /* If we aren't retaining peer certificates then we can discard it |
| 484 | * now. */ |
| 485 | if (ssl->s3->new_session != NULL && |
| 486 | ssl->ctx->retain_only_sha256_of_client_certs) { |
| 487 | X509_free(ssl->s3->new_session->peer); |
| 488 | ssl->s3->new_session->peer = NULL; |
| 489 | sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free); |
| 490 | ssl->s3->new_session->cert_chain = NULL; |
| 491 | } |
| 492 | |
| 493 | SSL_SESSION_free(ssl->s3->established_session); |
| 494 | if (ssl->session != NULL) { |
| 495 | SSL_SESSION_up_ref(ssl->session); |
| 496 | ssl->s3->established_session = ssl->session; |
| 497 | } else { |
| 498 | ssl->s3->established_session = ssl->s3->new_session; |
| 499 | ssl->s3->established_session->not_resumable = 0; |
| 500 | ssl->s3->new_session = NULL; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 501 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 502 | |
| 503 | /* remove buffering on output */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 504 | ssl_free_wbio_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 505 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 506 | ssl_handshake_free(ssl->s3->hs); |
| 507 | ssl->s3->hs = NULL; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 508 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 509 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 510 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 511 | ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 512 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 513 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 514 | |
| 515 | ret = 1; |
| 516 | goto end; |
| 517 | |
| 518 | default: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 519 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 520 | ret = -1; |
| 521 | goto end; |
| 522 | } |
| 523 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 524 | if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) { |
| 525 | int new_state = ssl->state; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 526 | ssl->state = state; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 527 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 528 | ssl->state = new_state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 529 | } |
| 530 | skip = 0; |
| 531 | } |
| 532 | |
| 533 | end: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 534 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 535 | return ret; |
| 536 | } |
| 537 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 538 | int ssl_client_cipher_list_contains_cipher( |
| 539 | const struct ssl_early_callback_ctx *client_hello, uint16_t id) { |
| 540 | CBS cipher_suites; |
| 541 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 542 | client_hello->cipher_suites_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 543 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 544 | while (CBS_len(&cipher_suites) > 0) { |
| 545 | uint16_t got_id; |
| 546 | if (!CBS_get_u16(&cipher_suites, &got_id)) { |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | if (got_id == id) { |
| 551 | return 1; |
| 552 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 553 | } |
| 554 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 555 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 556 | } |
| 557 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 558 | static int negotiate_version( |
| 559 | SSL *ssl, int *out_alert, |
| 560 | const struct ssl_early_callback_ctx *client_hello) { |
| 561 | uint16_t min_version, max_version; |
| 562 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 563 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 564 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 565 | } |
| 566 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 567 | uint16_t version = 0; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 568 | /* Check supported_versions extension if it is present. */ |
| 569 | CBS supported_versions; |
| 570 | if (ssl_early_callback_get_extension(client_hello, &supported_versions, |
| 571 | TLSEXT_TYPE_supported_versions)) { |
| 572 | CBS versions; |
| 573 | if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || |
| 574 | CBS_len(&supported_versions) != 0 || |
| 575 | CBS_len(&versions) == 0) { |
| 576 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 577 | *out_alert = SSL_AD_DECODE_ERROR; |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | int found_version = 0; |
| 582 | while (CBS_len(&versions) != 0) { |
| 583 | uint16_t ext_version; |
| 584 | if (!CBS_get_u16(&versions, &ext_version)) { |
| 585 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 586 | *out_alert = SSL_AD_DECODE_ERROR; |
| 587 | return 0; |
| 588 | } |
| 589 | if (!ssl->method->version_from_wire(&ext_version, ext_version)) { |
| 590 | continue; |
| 591 | } |
| 592 | if (min_version <= ext_version && |
| 593 | ext_version <= max_version) { |
| 594 | version = ext_version; |
| 595 | found_version = 1; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | if (!found_version) { |
| 601 | goto unsupported_protocol; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 602 | } |
| 603 | } else { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 604 | /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2 |
| 605 | * do not use this mechanism. */ |
| 606 | if (SSL_is_dtls(ssl)) { |
| 607 | if (client_hello->version <= DTLS1_2_VERSION) { |
| 608 | version = TLS1_2_VERSION; |
| 609 | } else if (client_hello->version <= DTLS1_VERSION) { |
| 610 | version = TLS1_1_VERSION; |
| 611 | } else { |
| 612 | goto unsupported_protocol; |
| 613 | } |
| 614 | } else { |
| 615 | if (client_hello->version >= TLS1_2_VERSION) { |
| 616 | version = TLS1_2_VERSION; |
| 617 | } else if (client_hello->version >= TLS1_1_VERSION) { |
| 618 | version = TLS1_1_VERSION; |
| 619 | } else if (client_hello->version >= TLS1_VERSION) { |
| 620 | version = TLS1_VERSION; |
| 621 | } else if (client_hello->version >= SSL3_VERSION) { |
| 622 | version = SSL3_VERSION; |
| 623 | } else { |
| 624 | goto unsupported_protocol; |
| 625 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 626 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 627 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 628 | /* Apply our minimum and maximum version. */ |
| 629 | if (version > max_version) { |
| 630 | version = max_version; |
| 631 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 632 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 633 | if (version < min_version) { |
| 634 | goto unsupported_protocol; |
| 635 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 636 | } |
| 637 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 638 | /* Handle FALLBACK_SCSV. */ |
| 639 | if (ssl_client_cipher_list_contains_cipher(client_hello, |
| 640 | SSL3_CK_FALLBACK_SCSV & 0xffff) && |
| 641 | version < max_version) { |
| 642 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
| 643 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 644 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 645 | } |
| 646 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 647 | ssl->client_version = client_hello->version; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 648 | ssl->version = ssl->method->version_to_wire(version); |
| 649 | ssl->s3->enc_method = ssl3_get_enc_method(version); |
| 650 | assert(ssl->s3->enc_method != NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 651 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 652 | /* At this point, the connection's version is known and |ssl->version| is |
| 653 | * fixed. Begin enforcing the record-layer version. */ |
| 654 | ssl->s3->have_version = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 655 | |
| 656 | return 1; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 657 | |
| 658 | unsupported_protocol: |
| 659 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 660 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 661 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 662 | } |
| 663 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 664 | static int ssl3_get_client_hello(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 665 | int al = SSL_AD_INTERNAL_ERROR, ret = -1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 666 | SSL_SESSION *session = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 667 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 668 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) { |
| 669 | /* The first time around, read the ClientHello. */ |
| 670 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO, |
| 671 | ssl_hash_message); |
| 672 | if (msg_ret <= 0) { |
| 673 | return msg_ret; |
| 674 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 675 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 676 | ssl->state = SSL3_ST_SR_CLNT_HELLO_B; |
| 677 | } |
| 678 | |
| 679 | struct ssl_early_callback_ctx client_hello; |
| 680 | if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg, |
| 681 | ssl->init_num)) { |
| 682 | al = SSL_AD_DECODE_ERROR; |
| 683 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 684 | goto f_err; |
| 685 | } |
| 686 | |
| 687 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) { |
| 688 | /* Unlike other callbacks, the early callback is not run a second time if |
| 689 | * paused. */ |
| 690 | ssl->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 691 | |
| 692 | /* Run the early callback. */ |
| 693 | if (ssl->ctx->select_certificate_cb != NULL) { |
| 694 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
| 695 | case 0: |
| 696 | ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 697 | goto err; |
| 698 | |
| 699 | case -1: |
| 700 | /* Connection rejected. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 701 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 702 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 703 | goto f_err; |
| 704 | |
| 705 | default: |
| 706 | /* fallthrough */; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 707 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 708 | } |
| 709 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 710 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 711 | /* Negotiate the protocol version if we have not done so yet. */ |
| 712 | if (!ssl->s3->have_version) { |
| 713 | if (!negotiate_version(ssl, &al, &client_hello)) { |
| 714 | goto f_err; |
| 715 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 716 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 717 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 718 | ssl->state = SSL_ST_TLS13; |
| 719 | return 1; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) { |
| 724 | /* Load the client random. */ |
| 725 | if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
| 726 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 727 | return -1; |
| 728 | } |
| 729 | memcpy(ssl->s3->client_random, client_hello.random, |
| 730 | client_hello.random_len); |
| 731 | |
| 732 | /* Determine whether we are doing session resumption. */ |
| 733 | int send_new_ticket = 0; |
| 734 | switch ( |
| 735 | ssl_get_prev_session(ssl, &session, &send_new_ticket, &client_hello)) { |
| 736 | case ssl_session_success: |
| 737 | break; |
| 738 | case ssl_session_error: |
| 739 | goto err; |
| 740 | case ssl_session_retry: |
| 741 | ssl->rwstate = SSL_PENDING_SESSION; |
| 742 | goto err; |
| 743 | } |
| 744 | ssl->tlsext_ticket_expected = send_new_ticket; |
| 745 | |
| 746 | /* The EMS state is needed when making the resumption decision, but |
| 747 | * extensions are not normally parsed until later. This detects the EMS |
| 748 | * extension for the resumption decision and it's checked against the result |
| 749 | * of the normal parse later in this function. */ |
| 750 | CBS ems; |
| 751 | int have_extended_master_secret = |
| 752 | ssl->version != SSL3_VERSION && |
| 753 | ssl_early_callback_get_extension(&client_hello, &ems, |
| 754 | TLSEXT_TYPE_extended_master_secret) && |
| 755 | CBS_len(&ems) == 0; |
| 756 | |
| 757 | int has_session = 0; |
| 758 | if (session != NULL) { |
| 759 | if (session->extended_master_secret && |
| 760 | !have_extended_master_secret) { |
| 761 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 762 | * is fatal to the connection. */ |
| 763 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 764 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 765 | goto f_err; |
| 766 | } |
| 767 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 768 | has_session = |
| 769 | /* Only resume if the session's version matches the negotiated |
| 770 | * version: most clients do not accept a mismatch. */ |
| 771 | ssl->version == session->ssl_version && |
| 772 | /* If the client offers the EMS extension, but the previous session |
| 773 | * didn't use it, then negotiate a new session. */ |
| 774 | have_extended_master_secret == session->extended_master_secret; |
| 775 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 776 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 777 | if (has_session) { |
| 778 | /* Use the old session. */ |
| 779 | ssl->session = session; |
| 780 | session = NULL; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 781 | ssl->s3->session_reused = 1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 782 | } else { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 783 | ssl_set_session(ssl, NULL); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 784 | if (!ssl_get_new_session(ssl, 1 /* server */)) { |
| 785 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 786 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 787 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 788 | /* Clear the session ID if we want the session to be single-use. */ |
| 789 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
| 790 | ssl->s3->new_session->session_id_length = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 794 | if (ssl->ctx->dos_protection_cb != NULL && |
| 795 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 796 | /* Connection rejected for DOS reasons. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 797 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 798 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 799 | goto f_err; |
| 800 | } |
| 801 | |
| 802 | /* Only null compression is supported. */ |
| 803 | if (memchr(client_hello.compression_methods, 0, |
| 804 | client_hello.compression_methods_len) == NULL) { |
| 805 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 806 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED); |
| 807 | goto f_err; |
| 808 | } |
| 809 | |
| 810 | /* TLS extensions. */ |
| 811 | if (!ssl_parse_clienthello_tlsext(ssl, &client_hello)) { |
| 812 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 813 | goto err; |
| 814 | } |
| 815 | |
| 816 | if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) { |
| 817 | al = SSL_AD_INTERNAL_ERROR; |
| 818 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT); |
| 819 | goto f_err; |
| 820 | } |
| 821 | |
| 822 | ssl->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 823 | } |
| 824 | |
| 825 | /* Determine the remaining connection parameters. This is a separate state so |
| 826 | * |cert_cb| does not cause earlier logic to run multiple times. */ |
| 827 | assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_D); |
| 828 | |
| 829 | if (ssl->session != NULL) { |
| 830 | /* Check that the cipher is in the list. */ |
| 831 | if (!ssl_client_cipher_list_contains_cipher( |
| 832 | &client_hello, (uint16_t)ssl->session->cipher->id)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 833 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 834 | OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 835 | goto f_err; |
| 836 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 837 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 838 | ssl->s3->tmp.new_cipher = ssl->session->cipher; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 839 | } else { |
| 840 | /* Call |cert_cb| to update server certificates if required. */ |
| 841 | if (ssl->cert->cert_cb != NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 842 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 843 | if (rv == 0) { |
| 844 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 845 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 846 | goto f_err; |
| 847 | } |
| 848 | if (rv < 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 849 | ssl->rwstate = SSL_X509_LOOKUP; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 850 | goto err; |
| 851 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 852 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 853 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 854 | const SSL_CIPHER *c = |
| 855 | ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 856 | if (c == NULL) { |
| 857 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 858 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 859 | goto f_err; |
| 860 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 861 | |
| 862 | ssl->s3->new_session->cipher = c; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 863 | ssl->s3->tmp.new_cipher = c; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 864 | |
| 865 | /* Determine whether to request a client certificate. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 866 | ssl->s3->hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 867 | /* Only request a certificate if Channel ID isn't negotiated. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 868 | if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 869 | ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 870 | ssl->s3->hs->cert_request = 0; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 871 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 872 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
| 873 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 874 | ssl->s3->hs->cert_request = 0; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 875 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 876 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 877 | if (!ssl->s3->hs->cert_request) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 878 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 879 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 880 | ssl->s3->new_session->verify_result = X509_V_OK; |
| 881 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 884 | /* Now that the cipher is known, initialize the handshake hash. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 885 | if (!ssl3_init_handshake_hash(ssl)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 886 | goto f_err; |
| 887 | } |
| 888 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 889 | /* Release the handshake buffer if client authentication isn't required. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 890 | if (!ssl->s3->hs->cert_request) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 891 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 894 | ret = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 895 | |
| 896 | if (0) { |
| 897 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 898 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | err: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 902 | SSL_SESSION_free(session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 903 | return ret; |
| 904 | } |
| 905 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 906 | static int ssl3_send_server_hello(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 907 | if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 908 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 911 | assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A); |
| 912 | |
| 913 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 914 | * known attack while we fix ChannelID itself. */ |
| 915 | if (ssl->s3->tlsext_channel_id_valid && |
| 916 | (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 917 | ssl->s3->tlsext_channel_id_valid = 0; |
| 918 | } |
| 919 | |
| 920 | /* If this is a resumption and the original handshake didn't support |
| 921 | * ChannelID then we didn't record the original handshake hashes in the |
| 922 | * session and so cannot resume with ChannelIDs. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 923 | if (ssl->session != NULL && |
| 924 | ssl->session->original_handshake_hash_len == 0) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 925 | ssl->s3->tlsext_channel_id_valid = 0; |
| 926 | } |
| 927 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 928 | struct timeval now; |
| 929 | ssl_get_current_time(ssl, &now); |
| 930 | ssl->s3->server_random[0] = now.tv_sec >> 24; |
| 931 | ssl->s3->server_random[1] = now.tv_sec >> 16; |
| 932 | ssl->s3->server_random[2] = now.tv_sec >> 8; |
| 933 | ssl->s3->server_random[3] = now.tv_sec; |
| 934 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 935 | return -1; |
| 936 | } |
| 937 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 938 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 939 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 940 | |
| 941 | const SSL_SESSION *session = ssl->s3->new_session; |
| 942 | if (ssl->session != NULL) { |
| 943 | session = ssl->session; |
| 944 | } |
| 945 | |
| 946 | CBB cbb, body, session_id; |
| 947 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) || |
| 948 | !CBB_add_u16(&body, ssl->version) || |
| 949 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 950 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 951 | !CBB_add_bytes(&session_id, session->session_id, |
| 952 | session->session_id_length) || |
| 953 | !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) || |
| 954 | !CBB_add_u8(&body, 0 /* no compression */) || |
| 955 | !ssl_add_serverhello_tlsext(ssl, &body) || |
| 956 | !ssl->method->finish_message(ssl, &cbb)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 957 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 958 | CBB_cleanup(&cbb); |
| 959 | return -1; |
| 960 | } |
| 961 | |
| 962 | ssl->state = SSL3_ST_SW_SRVR_HELLO_B; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 963 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 964 | } |
| 965 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 966 | static int ssl3_send_server_certificate(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 967 | if (ssl->state == SSL3_ST_SW_CERT_B) { |
| 968 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 969 | } |
| 970 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 971 | if (!ssl_has_certificate(ssl)) { |
| 972 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | if (!ssl3_output_cert_chain(ssl)) { |
| 977 | return 0; |
| 978 | } |
| 979 | ssl->state = SSL3_ST_SW_CERT_B; |
| 980 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static int ssl3_send_certificate_status(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 984 | if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) { |
| 985 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 986 | } |
| 987 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 988 | CBB cbb, body, ocsp_response; |
| 989 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 990 | SSL3_MT_CERTIFICATE_STATUS) || |
| 991 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 992 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
| 993 | !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 994 | ssl->ctx->ocsp_response_length) || |
| 995 | !ssl->method->finish_message(ssl, &cbb)) { |
| 996 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 997 | CBB_cleanup(&cbb); |
| 998 | return -1; |
| 999 | } |
| 1000 | |
| 1001 | ssl->state = SSL3_ST_SW_CERT_STATUS_B; |
| 1002 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1005 | static int ssl3_send_server_key_exchange(SSL *ssl) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1006 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1007 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1010 | CBB cbb, child; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1011 | CBB_zero(&cbb); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1012 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1013 | /* Put together the parameters. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1014 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1015 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1016 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1017 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1018 | /* Pre-allocate enough room to comfortably fit an ECDHE public key. */ |
| 1019 | if (!CBB_init(&cbb, 128)) { |
| 1020 | goto err; |
| 1021 | } |
| 1022 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1023 | /* PSK ciphers begin with an identity hint. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1024 | if (alg_a & SSL_aPSK) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1025 | size_t len = |
| 1026 | (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint); |
| 1027 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1028 | !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint, |
| 1029 | len)) { |
| 1030 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1031 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1034 | if (alg_k & SSL_kDHE) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1035 | /* Determine the group to use. */ |
| 1036 | DH *params = ssl->cert->dh_tmp; |
| 1037 | if (params == NULL && ssl->cert->dh_tmp_cb != NULL) { |
| 1038 | params = ssl->cert->dh_tmp_cb(ssl, 0, 1024); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1039 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1040 | if (params == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1041 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1042 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1043 | goto err; |
| 1044 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1045 | ssl->s3->new_session->key_exchange_info = DH_num_bits(params); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1046 | |
| 1047 | /* Set up DH, generate a key, and emit the public half. */ |
| 1048 | DH *dh = DHparams_dup(params); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1049 | if (dh == NULL) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1050 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1053 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->hs->ecdh_ctx, dh); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1054 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1055 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) || |
| 1056 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1057 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) || |
| 1058 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1059 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &child)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1060 | goto err; |
| 1061 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1062 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1063 | /* Determine the group to use. */ |
| 1064 | uint16_t group_id; |
| 1065 | if (!tls1_get_shared_group(ssl, &group_id)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1066 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1067 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1068 | goto err; |
| 1069 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1070 | ssl->s3->new_session->key_exchange_info = group_id; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1071 | |
| 1072 | /* Set up ECDH, generate a key, and emit the public half. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1073 | if (!SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1074 | !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1075 | !CBB_add_u16(&cbb, group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1076 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1077 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &child)) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1078 | goto err; |
| 1079 | } |
| 1080 | } else if (alg_k & SSL_kCECPQ1) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1081 | SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->hs->ecdh_ctx); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1082 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1083 | !SSL_ECDH_CTX_offer(&ssl->s3->hs->ecdh_ctx, &child)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1084 | goto err; |
| 1085 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1086 | } else { |
| 1087 | assert(alg_k & SSL_kPSK); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1090 | if (!CBB_finish(&cbb, &ssl->s3->hs->server_params, |
| 1091 | &ssl->s3->hs->server_params_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1092 | goto err; |
| 1093 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* Assemble the message. */ |
| 1097 | CBB body; |
| 1098 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1099 | SSL3_MT_SERVER_KEY_EXCHANGE) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1100 | !CBB_add_bytes(&body, ssl->s3->hs->server_params, |
| 1101 | ssl->s3->hs->server_params_len)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1102 | goto err; |
| 1103 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1104 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1105 | /* Add a signature. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1106 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1107 | if (!ssl_has_private_key(ssl)) { |
| 1108 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1109 | goto err; |
| 1110 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1111 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1112 | /* Determine the signature algorithm. */ |
| 1113 | uint16_t signature_algorithm; |
| 1114 | if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) { |
| 1115 | goto err; |
| 1116 | } |
| 1117 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1118 | if (!CBB_add_u16(&body, signature_algorithm)) { |
| 1119 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1120 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1121 | goto err; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* Add space for the signature. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1126 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1127 | uint8_t *ptr; |
| 1128 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 1129 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1130 | goto err; |
| 1131 | } |
| 1132 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1133 | size_t sig_len; |
| 1134 | enum ssl_private_key_result_t sign_result; |
| 1135 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1136 | CBB transcript; |
| 1137 | uint8_t *transcript_data; |
| 1138 | size_t transcript_len; |
| 1139 | if (!CBB_init(&transcript, |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1140 | 2*SSL3_RANDOM_SIZE + ssl->s3->hs->server_params_len) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1141 | !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1142 | !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1143 | !CBB_add_bytes(&transcript, ssl->s3->hs->server_params, |
| 1144 | ssl->s3->hs->server_params_len) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1145 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1146 | CBB_cleanup(&transcript); |
| 1147 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1148 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1149 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1150 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1151 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1152 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, |
| 1153 | signature_algorithm, transcript_data, |
| 1154 | transcript_len); |
| 1155 | OPENSSL_free(transcript_data); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1156 | } else { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1157 | assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1158 | sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1159 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1160 | |
| 1161 | switch (sign_result) { |
| 1162 | case ssl_private_key_success: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1163 | if (!CBB_did_write(&child, sig_len)) { |
| 1164 | goto err; |
| 1165 | } |
| 1166 | break; |
| 1167 | case ssl_private_key_failure: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1168 | goto err; |
| 1169 | case ssl_private_key_retry: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1170 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1171 | ssl->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1172 | goto err; |
| 1173 | } |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1176 | if (!ssl->method->finish_message(ssl, &cbb)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1177 | goto err; |
| 1178 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1179 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1180 | OPENSSL_free(ssl->s3->hs->server_params); |
| 1181 | ssl->s3->hs->server_params = NULL; |
| 1182 | ssl->s3->hs->server_params_len = 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1183 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1184 | ssl->state = SSL3_ST_SW_KEY_EXCH_C; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1185 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1186 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1187 | err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1188 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1189 | return -1; |
| 1190 | } |
| 1191 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1192 | static int add_cert_types(SSL *ssl, CBB *cbb) { |
| 1193 | /* Get configured signature algorithms. */ |
| 1194 | int have_rsa_sign = 0; |
| 1195 | int have_ecdsa_sign = 0; |
| 1196 | const uint16_t *sig_algs; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1197 | size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs); |
| 1198 | for (size_t i = 0; i < num_sig_algs; i++) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1199 | switch (sig_algs[i]) { |
| 1200 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 1201 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 1202 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 1203 | case SSL_SIGN_RSA_PKCS1_SHA1: |
| 1204 | have_rsa_sign = 1; |
| 1205 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1206 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1207 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 1208 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 1209 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 1210 | case SSL_SIGN_ECDSA_SHA1: |
| 1211 | have_ecdsa_sign = 1; |
| 1212 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1213 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1214 | } |
| 1215 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1216 | if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) { |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | /* ECDSA certs can be used with RSA cipher suites as well so we don't need to |
| 1221 | * check for SSL_kECDH or SSL_kECDHE. */ |
| 1222 | if (ssl->version >= TLS1_VERSION && have_ecdsa_sign && |
| 1223 | !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) { |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | return 1; |
| 1228 | } |
| 1229 | |
| 1230 | static int ssl3_send_certificate_request(SSL *ssl) { |
| 1231 | if (ssl->state == SSL3_ST_SW_CERT_REQ_B) { |
| 1232 | return ssl->method->write_message(ssl); |
| 1233 | } |
| 1234 | |
| 1235 | CBB cbb, body, cert_types, sigalgs_cbb; |
| 1236 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1237 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 1238 | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
| 1239 | !add_cert_types(ssl, &cert_types)) { |
| 1240 | goto err; |
| 1241 | } |
| 1242 | |
| 1243 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1244 | const uint16_t *sigalgs; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1245 | size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1246 | if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) { |
| 1247 | goto err; |
| 1248 | } |
| 1249 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1250 | for (size_t i = 0; i < num_sigalgs; i++) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1251 | if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) { |
| 1252 | goto err; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | if (!ssl_add_client_CA_list(ssl, &body) || |
| 1258 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1259 | goto err; |
| 1260 | } |
| 1261 | |
| 1262 | ssl->state = SSL3_ST_SW_CERT_REQ_B; |
| 1263 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1264 | |
| 1265 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1266 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1267 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1268 | return -1; |
| 1269 | } |
| 1270 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1271 | static int ssl3_send_server_hello_done(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1272 | if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) { |
| 1273 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1274 | } |
| 1275 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1276 | CBB cbb, body; |
| 1277 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) || |
| 1278 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1279 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1280 | CBB_cleanup(&cbb); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | ssl->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1285 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static int ssl3_get_client_certificate(SSL *ssl) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1289 | assert(ssl->s3->hs->cert_request); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1290 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1291 | int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 1292 | if (msg_ret <= 0) { |
| 1293 | return msg_ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 1297 | if (ssl->version == SSL3_VERSION && |
| 1298 | ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1299 | /* In SSL 3.0, the Certificate message is omitted to signal no |
| 1300 | * certificate. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1301 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1302 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1303 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1304 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1307 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1308 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1309 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1310 | ssl->s3->tmp.reuse_message = 1; |
| 1311 | return 1; |
| 1312 | } |
| 1313 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1314 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1315 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1316 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1317 | } |
| 1318 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1319 | CBS certificate_msg; |
| 1320 | CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num); |
| 1321 | uint8_t alert; |
| 1322 | STACK_OF(X509) *chain = ssl_parse_cert_chain( |
| 1323 | ssl, &alert, ssl->ctx->retain_only_sha256_of_client_certs |
| 1324 | ? ssl->s3->new_session->peer_sha256 |
| 1325 | : NULL, |
| 1326 | &certificate_msg); |
| 1327 | if (chain == NULL) { |
| 1328 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1329 | goto err; |
| 1330 | } |
| 1331 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1332 | if (CBS_len(&certificate_msg) != 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1333 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1334 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1335 | goto err; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1336 | } |
| 1337 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1338 | if (sk_X509_num(chain) == 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1339 | /* No client certificate so the handshake buffer may be discarded. */ |
| 1340 | ssl3_free_handshake_buffer(ssl); |
| 1341 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1342 | /* In SSL 3.0, sending no certificate is signaled by omitting the |
| 1343 | * Certificate message. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1344 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1345 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1346 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1347 | goto err; |
| 1348 | } |
| 1349 | |
| 1350 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1351 | /* Fail for TLS only if we required a certificate */ |
| 1352 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1353 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1354 | goto err; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1355 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1356 | |
| 1357 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1358 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1359 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1360 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1361 | /* The hash would have been filled in. */ |
| 1362 | if (ssl->ctx->retain_only_sha256_of_client_certs) { |
| 1363 | ssl->s3->new_session->peer_sha256_valid = 1; |
| 1364 | } |
| 1365 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1366 | if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result, |
| 1367 | chain)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1368 | goto err; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1372 | X509_free(ssl->s3->new_session->peer); |
| 1373 | ssl->s3->new_session->peer = sk_X509_shift(chain); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1374 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1375 | sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free); |
| 1376 | ssl->s3->new_session->cert_chain = chain; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1377 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 1378 | * certificate, while we do include it in s3_clnt.c */ |
| 1379 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1380 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1381 | |
| 1382 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1383 | sk_X509_pop_free(chain, X509_free); |
| 1384 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | static int ssl3_get_client_key_exchange(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1388 | int al; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1389 | CBS client_key_exchange; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1390 | uint32_t alg_k; |
| 1391 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1392 | uint8_t *premaster_secret = NULL; |
| 1393 | size_t premaster_secret_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1394 | uint8_t *decrypt_buf = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1395 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1396 | unsigned psk_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1397 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1398 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1399 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1400 | int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1401 | ssl_hash_message); |
| 1402 | if (ret <= 0) { |
| 1403 | return ret; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1404 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1407 | CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num); |
| 1408 | alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1409 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1410 | |
| 1411 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1412 | if (alg_a & SSL_aPSK) { |
| 1413 | CBS psk_identity; |
| 1414 | |
| 1415 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1416 | * then this is the only field in the message. */ |
| 1417 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1418 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1419 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1420 | al = SSL_AD_DECODE_ERROR; |
| 1421 | goto f_err; |
| 1422 | } |
| 1423 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1424 | if (ssl->psk_server_callback == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1425 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1426 | al = SSL_AD_INTERNAL_ERROR; |
| 1427 | goto f_err; |
| 1428 | } |
| 1429 | |
| 1430 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1431 | CBS_contains_zero_byte(&psk_identity)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1432 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1433 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1434 | goto f_err; |
| 1435 | } |
| 1436 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1437 | if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1438 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1439 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1440 | goto f_err; |
| 1441 | } |
| 1442 | |
| 1443 | /* Look up the key for the identity. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1444 | psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity, |
| 1445 | psk, sizeof(psk)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1446 | if (psk_len > PSK_MAX_PSK_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1447 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1448 | al = SSL_AD_INTERNAL_ERROR; |
| 1449 | goto f_err; |
| 1450 | } else if (psk_len == 0) { |
| 1451 | /* PSK related to the given identity not found */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1452 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1453 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1454 | goto f_err; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1459 | * |premaster_secret_len|. */ |
| 1460 | if (alg_k & SSL_kRSA) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1461 | /* Allocate a buffer large enough for an RSA decryption. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1462 | const size_t rsa_size = ssl_private_key_max_signature_len(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1463 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1464 | if (decrypt_buf == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1465 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1466 | goto err; |
| 1467 | } |
| 1468 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1469 | enum ssl_private_key_result_t decrypt_result; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1470 | size_t decrypt_len; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1471 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1472 | if (!ssl_has_private_key(ssl) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1473 | ssl_private_key_type(ssl) != NID_rsaEncryption) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1474 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1475 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1476 | goto f_err; |
| 1477 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1478 | CBS encrypted_premaster_secret; |
| 1479 | if (ssl->version > SSL3_VERSION) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1480 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1481 | &encrypted_premaster_secret) || |
| 1482 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1483 | al = SSL_AD_DECODE_ERROR; |
| 1484 | OPENSSL_PUT_ERROR(SSL, |
| 1485 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1486 | goto f_err; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1487 | } |
| 1488 | } else { |
| 1489 | encrypted_premaster_secret = client_key_exchange; |
| 1490 | } |
| 1491 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1492 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1493 | * timing-sensitive code below. */ |
| 1494 | decrypt_result = ssl_private_key_decrypt( |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1495 | ssl, decrypt_buf, &decrypt_len, rsa_size, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1496 | CBS_data(&encrypted_premaster_secret), |
| 1497 | CBS_len(&encrypted_premaster_secret)); |
| 1498 | } else { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1499 | assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1500 | /* Complete async decrypt. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1501 | decrypt_result = |
| 1502 | ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1503 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1504 | |
| 1505 | switch (decrypt_result) { |
| 1506 | case ssl_private_key_success: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1507 | break; |
| 1508 | case ssl_private_key_failure: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1509 | goto err; |
| 1510 | case ssl_private_key_retry: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1511 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1512 | ssl->state = SSL3_ST_SR_KEY_EXCH_B; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1513 | goto err; |
| 1514 | } |
| 1515 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1516 | if (decrypt_len != rsa_size) { |
| 1517 | al = SSL_AD_DECRYPT_ERROR; |
| 1518 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1519 | goto f_err; |
| 1520 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1521 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1522 | /* Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1523 | * section 7.4.7.1. */ |
| 1524 | premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1525 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1526 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1527 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1528 | goto err; |
| 1529 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1530 | if (!RAND_bytes(premaster_secret, premaster_secret_len)) { |
| 1531 | goto err; |
| 1532 | } |
| 1533 | |
| 1534 | /* The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1535 | * publicly invalid. */ |
| 1536 | if (decrypt_len < 11 + premaster_secret_len) { |
| 1537 | al = SSL_AD_DECRYPT_ERROR; |
| 1538 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1539 | goto f_err; |
| 1540 | } |
| 1541 | |
| 1542 | /* Check the padding. See RFC 3447, section 7.2.2. */ |
| 1543 | size_t padding_len = decrypt_len - premaster_secret_len; |
| 1544 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1545 | constant_time_eq_int_8(decrypt_buf[1], 2); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1546 | for (size_t i = 2; i < padding_len - 1; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1547 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1548 | } |
| 1549 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1550 | |
| 1551 | /* The premaster secret must begin with |client_version|. This too must be |
| 1552 | * checked in constant time (http://eprint.iacr.org/2003/052/). */ |
| 1553 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
| 1554 | (unsigned)(ssl->client_version >> 8)); |
| 1555 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
| 1556 | (unsigned)(ssl->client_version & 0xff)); |
| 1557 | |
| 1558 | /* Select, in constant time, either the decrypted premaster or the random |
| 1559 | * premaster based on |good|. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1560 | for (size_t i = 0; i < premaster_secret_len; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1561 | premaster_secret[i] = constant_time_select_8( |
| 1562 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1563 | } |
| 1564 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1565 | OPENSSL_free(decrypt_buf); |
| 1566 | decrypt_buf = NULL; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1567 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
| 1568 | /* Parse the ClientKeyExchange. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1569 | CBS peer_key; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1570 | if (!SSL_ECDH_CTX_get_key(&ssl->s3->hs->ecdh_ctx, &client_key_exchange, |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1571 | &peer_key) || |
| 1572 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1573 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1574 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1575 | goto f_err; |
| 1576 | } |
| 1577 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1578 | /* Compute the premaster. */ |
| 1579 | uint8_t alert; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1580 | if (!SSL_ECDH_CTX_finish(&ssl->s3->hs->ecdh_ctx, &premaster_secret, |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1581 | &premaster_secret_len, &alert, CBS_data(&peer_key), |
| 1582 | CBS_len(&peer_key))) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1583 | al = alert; |
| 1584 | goto f_err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1587 | /* The key exchange state may now be discarded. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame^] | 1588 | SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1589 | } else if (alg_k & SSL_kPSK) { |
| 1590 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1591 | * pre-shared key. */ |
| 1592 | premaster_secret_len = psk_len; |
| 1593 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1594 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1595 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1596 | goto err; |
| 1597 | } |
| 1598 | memset(premaster_secret, 0, premaster_secret_len); |
| 1599 | } else { |
| 1600 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1601 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1602 | goto f_err; |
| 1603 | } |
| 1604 | |
| 1605 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1606 | * pre-shared key. */ |
| 1607 | if (alg_a & SSL_aPSK) { |
| 1608 | CBB new_premaster, child; |
| 1609 | uint8_t *new_data; |
| 1610 | size_t new_len; |
| 1611 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1612 | CBB_zero(&new_premaster); |
| 1613 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1614 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1615 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1616 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1617 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1618 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1619 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1620 | CBB_cleanup(&new_premaster); |
| 1621 | goto err; |
| 1622 | } |
| 1623 | |
| 1624 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1625 | OPENSSL_free(premaster_secret); |
| 1626 | premaster_secret = new_data; |
| 1627 | premaster_secret_len = new_len; |
| 1628 | } |
| 1629 | |
| 1630 | /* Compute the master secret */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1631 | ssl->s3->new_session->master_key_length = tls1_generate_master_secret( |
| 1632 | ssl, ssl->s3->new_session->master_key, premaster_secret, |
| 1633 | premaster_secret_len); |
| 1634 | if (ssl->s3->new_session->master_key_length == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1635 | goto err; |
| 1636 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1637 | ssl->s3->new_session->extended_master_secret = |
| 1638 | ssl->s3->tmp.extended_master_secret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1639 | |
| 1640 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1641 | OPENSSL_free(premaster_secret); |
| 1642 | return 1; |
| 1643 | |
| 1644 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1645 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1646 | err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1647 | if (premaster_secret != NULL) { |
| 1648 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1649 | OPENSSL_free(premaster_secret); |
| 1650 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1651 | OPENSSL_free(decrypt_buf); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1652 | |
| 1653 | return -1; |
| 1654 | } |
| 1655 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1656 | static int ssl3_get_cert_verify(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1657 | int al, ret = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1658 | CBS certificate_verify, signature; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1659 | X509 *peer = ssl->s3->new_session->peer; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1660 | EVP_PKEY *pkey = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1661 | |
| 1662 | /* Only RSA and ECDSA client certificates are supported, so a |
| 1663 | * CertificateVerify is required if and only if there's a client certificate. |
| 1664 | * */ |
| 1665 | if (peer == NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1666 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1667 | return 1; |
| 1668 | } |
| 1669 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1670 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY, |
| 1671 | ssl_dont_hash_message); |
| 1672 | if (msg_ret <= 0) { |
| 1673 | return msg_ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* Filter out unsupported certificate types. */ |
| 1677 | pkey = X509_get_pubkey(peer); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1678 | if (pkey == NULL) { |
| 1679 | goto err; |
| 1680 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1681 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1682 | CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1683 | |
| 1684 | /* Determine the digest type if needbe. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1685 | uint16_t signature_algorithm = 0; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1686 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1687 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1688 | al = SSL_AD_DECODE_ERROR; |
| 1689 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1690 | goto f_err; |
| 1691 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1692 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1693 | goto f_err; |
| 1694 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1695 | ssl->s3->tmp.peer_signature_algorithm = signature_algorithm; |
| 1696 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1697 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 1698 | } else if (pkey->type == EVP_PKEY_EC) { |
| 1699 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
| 1700 | } else { |
| 1701 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1702 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1703 | goto f_err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1704 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1705 | |
| 1706 | /* Parse and verify the signature. */ |
| 1707 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1708 | CBS_len(&certificate_verify) != 0) { |
| 1709 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1710 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1711 | goto f_err; |
| 1712 | } |
| 1713 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1714 | int sig_ok; |
| 1715 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1716 | * single final digest and signature, and must be special-cased. */ |
| 1717 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 1718 | const EVP_MD *md; |
| 1719 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1720 | size_t digest_len; |
| 1721 | if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len, |
| 1722 | signature_algorithm)) { |
| 1723 | goto err; |
| 1724 | } |
| 1725 | |
| 1726 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 1727 | sig_ok = pctx != NULL && |
| 1728 | EVP_PKEY_verify_init(pctx) && |
| 1729 | EVP_PKEY_CTX_set_signature_md(pctx, md) && |
| 1730 | EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 1731 | digest, digest_len); |
| 1732 | EVP_PKEY_CTX_free(pctx); |
| 1733 | } else { |
| 1734 | sig_ok = ssl_public_key_verify( |
| 1735 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
| 1736 | pkey, (const uint8_t *)ssl->s3->handshake_buffer->data, |
| 1737 | ssl->s3->handshake_buffer->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1738 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1739 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1740 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1741 | sig_ok = 1; |
| 1742 | ERR_clear_error(); |
| 1743 | #endif |
| 1744 | if (!sig_ok) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1745 | al = SSL_AD_DECRYPT_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1746 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1747 | goto f_err; |
| 1748 | } |
| 1749 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1750 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 1751 | * message.*/ |
| 1752 | ssl3_free_handshake_buffer(ssl); |
| 1753 | if (!ssl->method->hash_current_message(ssl)) { |
| 1754 | goto err; |
| 1755 | } |
| 1756 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1757 | ret = 1; |
| 1758 | |
| 1759 | if (0) { |
| 1760 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1761 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | err: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1765 | EVP_PKEY_free(pkey); |
| 1766 | |
| 1767 | return ret; |
| 1768 | } |
| 1769 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1770 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 1771 | * sets the next_proto member in s if found */ |
| 1772 | static int ssl3_get_next_proto(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1773 | int ret = |
| 1774 | ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message); |
| 1775 | if (ret <= 0) { |
| 1776 | return ret; |
| 1777 | } |
| 1778 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1779 | CBS next_protocol, selected_protocol, padding; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1780 | CBS_init(&next_protocol, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1781 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1782 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1783 | CBS_len(&next_protocol) != 0) { |
| 1784 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1785 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated, |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1790 | &ssl->s3->next_proto_negotiated_len)) { |
| 1791 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1794 | return 1; |
| 1795 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1796 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1797 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 1798 | static int ssl3_get_channel_id(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1799 | int ret = -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1800 | uint8_t channel_id_hash[EVP_MAX_MD_SIZE]; |
| 1801 | size_t channel_id_hash_len; |
| 1802 | const uint8_t *p; |
| 1803 | uint16_t extension_type; |
| 1804 | EC_GROUP *p256 = NULL; |
| 1805 | EC_KEY *key = NULL; |
| 1806 | EC_POINT *point = NULL; |
| 1807 | ECDSA_SIG sig; |
| 1808 | BIGNUM x, y; |
| 1809 | CBS encrypted_extensions, extension; |
| 1810 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1811 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID, |
| 1812 | ssl_dont_hash_message); |
| 1813 | if (msg_ret <= 0) { |
| 1814 | return msg_ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | /* Before incorporating the EncryptedExtensions message to the handshake |
| 1818 | * hash, compute the hash that should have been signed. */ |
| 1819 | if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) { |
| 1820 | return -1; |
| 1821 | } |
| 1822 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
| 1823 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1824 | if (!ssl->method->hash_current_message(ssl)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1825 | return -1; |
| 1826 | } |
| 1827 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1828 | CBS_init(&encrypted_extensions, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1829 | |
| 1830 | /* EncryptedExtensions could include multiple extensions, but the only |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1831 | * extension that could be negotiated is Channel ID, so there can only be one |
| 1832 | * entry. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1833 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 1834 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 1835 | CBS_len(&encrypted_extensions) != 0 || |
| 1836 | extension_type != TLSEXT_TYPE_channel_id || |
| 1837 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1838 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1839 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1840 | return -1; |
| 1841 | } |
| 1842 | |
| 1843 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 1844 | if (!p256) { |
| 1845 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
| 1846 | return -1; |
| 1847 | } |
| 1848 | |
| 1849 | BN_init(&x); |
| 1850 | BN_init(&y); |
| 1851 | sig.r = BN_new(); |
| 1852 | sig.s = BN_new(); |
| 1853 | if (sig.r == NULL || sig.s == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1854 | goto err; |
| 1855 | } |
| 1856 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1857 | p = CBS_data(&extension); |
| 1858 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 1859 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 1860 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 1861 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 1862 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1865 | point = EC_POINT_new(p256); |
| 1866 | if (!point || |
| 1867 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
| 1868 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1871 | key = EC_KEY_new(); |
| 1872 | if (!key || !EC_KEY_set_group(key, p256) || |
| 1873 | !EC_KEY_set_public_key(key, point)) { |
| 1874 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1877 | /* We stored the handshake hash in |tlsext_channel_id| the first time that we |
| 1878 | * were called. */ |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 1879 | int sig_ok = ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key); |
| 1880 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1881 | sig_ok = 1; |
| 1882 | #endif |
| 1883 | if (!sig_ok) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1884 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 1885 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1886 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1887 | goto err; |
| 1888 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1889 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1890 | memcpy(ssl->s3->tlsext_channel_id, p, 64); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1891 | ret = 1; |
| 1892 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1893 | err: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1894 | BN_free(&x); |
| 1895 | BN_free(&y); |
| 1896 | BN_free(sig.r); |
| 1897 | BN_free(sig.s); |
| 1898 | EC_KEY_free(key); |
| 1899 | EC_POINT_free(point); |
| 1900 | EC_GROUP_free(p256); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1901 | return ret; |
| 1902 | } |
| 1903 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1904 | static int ssl3_send_new_session_ticket(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1905 | if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) { |
| 1906 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1909 | CBB cbb, body, ticket; |
| 1910 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1911 | SSL3_MT_NEW_SESSION_TICKET) || |
| 1912 | /* Ticket lifetime hint (advisory only): We leave this unspecified for |
| 1913 | * resumed session (for simplicity), and guess that tickets for new |
| 1914 | * sessions will live as long as their sessions. */ |
| 1915 | !CBB_add_u32(&body, |
| 1916 | ssl->session != NULL ? 0 : ssl->s3->new_session->timeout) || |
| 1917 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
| 1918 | !ssl_encrypt_ticket(ssl, &ticket, ssl->session != NULL |
| 1919 | ? ssl->session |
| 1920 | : ssl->s3->new_session) || |
| 1921 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1922 | return 0; |
| 1923 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1924 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1925 | ssl->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 1926 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1927 | } |