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 | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 273 | if (ssl->s3->tmp.certificate_status_expected) { |
| 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: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 305 | if (ssl->s3->tmp.cert_request) { |
| 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: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 327 | if (ssl->s3->tmp.cert_request) { |
| 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 | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 369 | if (ssl->s3->next_proto_neg_seen) { |
| 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 | /* For TLS versions which use ClientHello.version, convert it to a version we |
| 568 | * are aware of. */ |
| 569 | uint16_t version = 0; |
| 570 | if (SSL_is_dtls(ssl)) { |
| 571 | if (client_hello->version <= DTLS1_2_VERSION) { |
| 572 | version = TLS1_2_VERSION; |
| 573 | } else if (client_hello->version <= DTLS1_VERSION) { |
| 574 | version = TLS1_1_VERSION; |
| 575 | } |
| 576 | } else { |
| 577 | if (client_hello->version >= TLS1_3_VERSION) { |
| 578 | version = TLS1_3_VERSION; |
| 579 | } else if (client_hello->version >= TLS1_2_VERSION) { |
| 580 | version = TLS1_2_VERSION; |
| 581 | } else if (client_hello->version >= TLS1_1_VERSION) { |
| 582 | version = TLS1_1_VERSION; |
| 583 | } else if (client_hello->version >= TLS1_VERSION) { |
| 584 | version = TLS1_VERSION; |
| 585 | } else if (client_hello->version >= SSL3_VERSION) { |
| 586 | version = SSL3_VERSION; |
| 587 | } |
| 588 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 589 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 590 | /* Apply our minimum and maximum version. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 591 | if (version > max_version) { |
| 592 | version = max_version; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 593 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 594 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 595 | if (version < min_version) { |
| 596 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 597 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 598 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 599 | } |
| 600 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 601 | /* Handle FALLBACK_SCSV. */ |
| 602 | if (ssl_client_cipher_list_contains_cipher(client_hello, |
| 603 | SSL3_CK_FALLBACK_SCSV & 0xffff) && |
| 604 | version < max_version) { |
| 605 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
| 606 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 607 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 608 | } |
| 609 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 610 | ssl->client_version = client_hello->version; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 611 | ssl->version = ssl->method->version_to_wire(version); |
| 612 | ssl->s3->enc_method = ssl3_get_enc_method(version); |
| 613 | assert(ssl->s3->enc_method != NULL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 614 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 615 | /* At this point, the connection's version is known and |ssl->version| is |
| 616 | * fixed. Begin enforcing the record-layer version. */ |
| 617 | ssl->s3->have_version = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 618 | |
| 619 | return 1; |
| 620 | } |
| 621 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 622 | static int ssl3_get_client_hello(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 623 | int al = SSL_AD_INTERNAL_ERROR, ret = -1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 624 | SSL_SESSION *session = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 625 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 626 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_A) { |
| 627 | /* The first time around, read the ClientHello. */ |
| 628 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO, |
| 629 | ssl_hash_message); |
| 630 | if (msg_ret <= 0) { |
| 631 | return msg_ret; |
| 632 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 633 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 634 | ssl->state = SSL3_ST_SR_CLNT_HELLO_B; |
| 635 | } |
| 636 | |
| 637 | struct ssl_early_callback_ctx client_hello; |
| 638 | if (!ssl_early_callback_init(ssl, &client_hello, ssl->init_msg, |
| 639 | ssl->init_num)) { |
| 640 | al = SSL_AD_DECODE_ERROR; |
| 641 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 642 | goto f_err; |
| 643 | } |
| 644 | |
| 645 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B) { |
| 646 | /* Unlike other callbacks, the early callback is not run a second time if |
| 647 | * paused. */ |
| 648 | ssl->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 649 | |
| 650 | /* Run the early callback. */ |
| 651 | if (ssl->ctx->select_certificate_cb != NULL) { |
| 652 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
| 653 | case 0: |
| 654 | ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 655 | goto err; |
| 656 | |
| 657 | case -1: |
| 658 | /* Connection rejected. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 659 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 660 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 661 | goto f_err; |
| 662 | |
| 663 | default: |
| 664 | /* fallthrough */; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 665 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 666 | } |
| 667 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 668 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 669 | /* Negotiate the protocol version if we have not done so yet. */ |
| 670 | if (!ssl->s3->have_version) { |
| 671 | if (!negotiate_version(ssl, &al, &client_hello)) { |
| 672 | goto f_err; |
| 673 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 674 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 675 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 676 | ssl->state = SSL_ST_TLS13; |
| 677 | return 1; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_C) { |
| 682 | /* Load the client random. */ |
| 683 | if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
| 684 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 685 | return -1; |
| 686 | } |
| 687 | memcpy(ssl->s3->client_random, client_hello.random, |
| 688 | client_hello.random_len); |
| 689 | |
| 690 | /* Determine whether we are doing session resumption. */ |
| 691 | int send_new_ticket = 0; |
| 692 | switch ( |
| 693 | ssl_get_prev_session(ssl, &session, &send_new_ticket, &client_hello)) { |
| 694 | case ssl_session_success: |
| 695 | break; |
| 696 | case ssl_session_error: |
| 697 | goto err; |
| 698 | case ssl_session_retry: |
| 699 | ssl->rwstate = SSL_PENDING_SESSION; |
| 700 | goto err; |
| 701 | } |
| 702 | ssl->tlsext_ticket_expected = send_new_ticket; |
| 703 | |
| 704 | /* The EMS state is needed when making the resumption decision, but |
| 705 | * extensions are not normally parsed until later. This detects the EMS |
| 706 | * extension for the resumption decision and it's checked against the result |
| 707 | * of the normal parse later in this function. */ |
| 708 | CBS ems; |
| 709 | int have_extended_master_secret = |
| 710 | ssl->version != SSL3_VERSION && |
| 711 | ssl_early_callback_get_extension(&client_hello, &ems, |
| 712 | TLSEXT_TYPE_extended_master_secret) && |
| 713 | CBS_len(&ems) == 0; |
| 714 | |
| 715 | int has_session = 0; |
| 716 | if (session != NULL) { |
| 717 | if (session->extended_master_secret && |
| 718 | !have_extended_master_secret) { |
| 719 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 720 | * is fatal to the connection. */ |
| 721 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 722 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 723 | goto f_err; |
| 724 | } |
| 725 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 726 | has_session = |
| 727 | /* Only resume if the session's version matches the negotiated |
| 728 | * version: most clients do not accept a mismatch. */ |
| 729 | ssl->version == session->ssl_version && |
| 730 | /* If the client offers the EMS extension, but the previous session |
| 731 | * didn't use it, then negotiate a new session. */ |
| 732 | have_extended_master_secret == session->extended_master_secret; |
| 733 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 734 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 735 | if (has_session) { |
| 736 | /* Use the old session. */ |
| 737 | ssl->session = session; |
| 738 | session = NULL; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 739 | ssl->s3->session_reused = 1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 740 | } else { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 741 | ssl_set_session(ssl, NULL); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 742 | if (!ssl_get_new_session(ssl, 1 /* server */)) { |
| 743 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 744 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 745 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 746 | /* Clear the session ID if we want the session to be single-use. */ |
| 747 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
| 748 | ssl->s3->new_session->session_id_length = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 752 | if (ssl->ctx->dos_protection_cb != NULL && |
| 753 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 754 | /* Connection rejected for DOS reasons. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 755 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 756 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 757 | goto f_err; |
| 758 | } |
| 759 | |
| 760 | /* Only null compression is supported. */ |
| 761 | if (memchr(client_hello.compression_methods, 0, |
| 762 | client_hello.compression_methods_len) == NULL) { |
| 763 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 764 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED); |
| 765 | goto f_err; |
| 766 | } |
| 767 | |
| 768 | /* TLS extensions. */ |
| 769 | if (!ssl_parse_clienthello_tlsext(ssl, &client_hello)) { |
| 770 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 771 | goto err; |
| 772 | } |
| 773 | |
| 774 | if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) { |
| 775 | al = SSL_AD_INTERNAL_ERROR; |
| 776 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT); |
| 777 | goto f_err; |
| 778 | } |
| 779 | |
| 780 | ssl->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 781 | } |
| 782 | |
| 783 | /* Determine the remaining connection parameters. This is a separate state so |
| 784 | * |cert_cb| does not cause earlier logic to run multiple times. */ |
| 785 | assert(ssl->state == SSL3_ST_SR_CLNT_HELLO_D); |
| 786 | |
| 787 | if (ssl->session != NULL) { |
| 788 | /* Check that the cipher is in the list. */ |
| 789 | if (!ssl_client_cipher_list_contains_cipher( |
| 790 | &client_hello, (uint16_t)ssl->session->cipher->id)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 791 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 792 | OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 793 | goto f_err; |
| 794 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 795 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 796 | ssl->s3->tmp.new_cipher = ssl->session->cipher; |
| 797 | ssl->s3->tmp.cert_request = 0; |
| 798 | } else { |
| 799 | /* Call |cert_cb| to update server certificates if required. */ |
| 800 | if (ssl->cert->cert_cb != NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 801 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 802 | if (rv == 0) { |
| 803 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 804 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 805 | goto f_err; |
| 806 | } |
| 807 | if (rv < 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 808 | ssl->rwstate = SSL_X509_LOOKUP; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 809 | goto err; |
| 810 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 811 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 812 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 813 | const SSL_CIPHER *c = |
| 814 | ssl3_choose_cipher(ssl, &client_hello, ssl_get_cipher_preferences(ssl)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 815 | if (c == NULL) { |
| 816 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 817 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 818 | goto f_err; |
| 819 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 820 | |
| 821 | ssl->s3->new_session->cipher = c; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 822 | ssl->s3->tmp.new_cipher = c; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 823 | |
| 824 | /* Determine whether to request a client certificate. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 825 | ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 826 | /* Only request a certificate if Channel ID isn't negotiated. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 827 | if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 828 | ssl->s3->tlsext_channel_id_valid) { |
| 829 | ssl->s3->tmp.cert_request = 0; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 830 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 831 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
| 832 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 833 | ssl->s3->tmp.cert_request = 0; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 834 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 835 | |
| 836 | if (!ssl->s3->tmp.cert_request) { |
| 837 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 838 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 839 | ssl->s3->new_session->verify_result = X509_V_OK; |
| 840 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 843 | /* Now that the cipher is known, initialize the handshake hash. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 844 | if (!ssl3_init_handshake_hash(ssl)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 845 | goto f_err; |
| 846 | } |
| 847 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 848 | /* Release the handshake buffer if client authentication isn't required. */ |
| 849 | if (!ssl->s3->tmp.cert_request) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 850 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 853 | ret = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 854 | |
| 855 | if (0) { |
| 856 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 857 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | err: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 861 | SSL_SESSION_free(session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 862 | return ret; |
| 863 | } |
| 864 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 865 | static int ssl3_send_server_hello(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 866 | if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 867 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 870 | assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A); |
| 871 | |
| 872 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 873 | * known attack while we fix ChannelID itself. */ |
| 874 | if (ssl->s3->tlsext_channel_id_valid && |
| 875 | (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 876 | ssl->s3->tlsext_channel_id_valid = 0; |
| 877 | } |
| 878 | |
| 879 | /* If this is a resumption and the original handshake didn't support |
| 880 | * ChannelID then we didn't record the original handshake hashes in the |
| 881 | * session and so cannot resume with ChannelIDs. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 882 | if (ssl->session != NULL && |
| 883 | ssl->session->original_handshake_hash_len == 0) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 884 | ssl->s3->tlsext_channel_id_valid = 0; |
| 885 | } |
| 886 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 887 | struct timeval now; |
| 888 | ssl_get_current_time(ssl, &now); |
| 889 | ssl->s3->server_random[0] = now.tv_sec >> 24; |
| 890 | ssl->s3->server_random[1] = now.tv_sec >> 16; |
| 891 | ssl->s3->server_random[2] = now.tv_sec >> 8; |
| 892 | ssl->s3->server_random[3] = now.tv_sec; |
| 893 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 894 | return -1; |
| 895 | } |
| 896 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 897 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 898 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 899 | |
| 900 | const SSL_SESSION *session = ssl->s3->new_session; |
| 901 | if (ssl->session != NULL) { |
| 902 | session = ssl->session; |
| 903 | } |
| 904 | |
| 905 | CBB cbb, body, session_id; |
| 906 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) || |
| 907 | !CBB_add_u16(&body, ssl->version) || |
| 908 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 909 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 910 | !CBB_add_bytes(&session_id, session->session_id, |
| 911 | session->session_id_length) || |
| 912 | !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) || |
| 913 | !CBB_add_u8(&body, 0 /* no compression */) || |
| 914 | !ssl_add_serverhello_tlsext(ssl, &body) || |
| 915 | !ssl->method->finish_message(ssl, &cbb)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 916 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 917 | CBB_cleanup(&cbb); |
| 918 | return -1; |
| 919 | } |
| 920 | |
| 921 | ssl->state = SSL3_ST_SW_SRVR_HELLO_B; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 922 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 923 | } |
| 924 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 925 | static int ssl3_send_server_certificate(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 926 | if (ssl->state == SSL3_ST_SW_CERT_B) { |
| 927 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 928 | } |
| 929 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 930 | if (!ssl_has_certificate(ssl)) { |
| 931 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | if (!ssl3_output_cert_chain(ssl)) { |
| 936 | return 0; |
| 937 | } |
| 938 | ssl->state = SSL3_ST_SW_CERT_B; |
| 939 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static int ssl3_send_certificate_status(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 943 | if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) { |
| 944 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 945 | } |
| 946 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 947 | CBB cbb, body, ocsp_response; |
| 948 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 949 | SSL3_MT_CERTIFICATE_STATUS) || |
| 950 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 951 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
| 952 | !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 953 | ssl->ctx->ocsp_response_length) || |
| 954 | !ssl->method->finish_message(ssl, &cbb)) { |
| 955 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 956 | CBB_cleanup(&cbb); |
| 957 | return -1; |
| 958 | } |
| 959 | |
| 960 | ssl->state = SSL3_ST_SW_CERT_STATUS_B; |
| 961 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 962 | } |
| 963 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 964 | static int ssl3_send_server_key_exchange(SSL *ssl) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 965 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 966 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 969 | CBB cbb, child; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 970 | CBB_zero(&cbb); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 971 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 972 | /* Put together the parameters. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 973 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 974 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 975 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 976 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 977 | /* Pre-allocate enough room to comfortably fit an ECDHE public key. */ |
| 978 | if (!CBB_init(&cbb, 128)) { |
| 979 | goto err; |
| 980 | } |
| 981 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 982 | /* PSK ciphers begin with an identity hint. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 983 | if (alg_a & SSL_aPSK) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 984 | size_t len = |
| 985 | (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint); |
| 986 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 987 | !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint, |
| 988 | len)) { |
| 989 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 990 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 993 | if (alg_k & SSL_kDHE) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 994 | /* Determine the group to use. */ |
| 995 | DH *params = ssl->cert->dh_tmp; |
| 996 | if (params == NULL && ssl->cert->dh_tmp_cb != NULL) { |
| 997 | params = ssl->cert->dh_tmp_cb(ssl, 0, 1024); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 998 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 999 | if (params == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1000 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1001 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1002 | goto err; |
| 1003 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1004 | ssl->s3->new_session->key_exchange_info = DH_num_bits(params); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1005 | |
| 1006 | /* Set up DH, generate a key, and emit the public half. */ |
| 1007 | DH *dh = DHparams_dup(params); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1008 | if (dh == NULL) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1009 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1012 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh); |
| 1013 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1014 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) || |
| 1015 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1016 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) || |
| 1017 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1018 | !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1019 | goto err; |
| 1020 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1021 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1022 | /* Determine the group to use. */ |
| 1023 | uint16_t group_id; |
| 1024 | if (!tls1_get_shared_group(ssl, &group_id)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1025 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1026 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1027 | goto err; |
| 1028 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1029 | ssl->s3->new_session->key_exchange_info = group_id; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1030 | |
| 1031 | /* Set up ECDH, generate a key, and emit the public half. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1032 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1033 | !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1034 | !CBB_add_u16(&cbb, group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1035 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1036 | !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) { |
| 1037 | goto err; |
| 1038 | } |
| 1039 | } else if (alg_k & SSL_kCECPQ1) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1040 | SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx); |
| 1041 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1042 | !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1043 | goto err; |
| 1044 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1045 | } else { |
| 1046 | assert(alg_k & SSL_kPSK); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1049 | size_t len; |
| 1050 | if (!CBB_finish(&cbb, &ssl->s3->tmp.server_params, &len) || |
| 1051 | len > 0xffffffffu) { |
| 1052 | OPENSSL_free(ssl->s3->tmp.server_params); |
| 1053 | ssl->s3->tmp.server_params = NULL; |
| 1054 | goto err; |
| 1055 | } |
| 1056 | ssl->s3->tmp.server_params_len = (uint32_t)len; |
| 1057 | } |
| 1058 | |
| 1059 | /* Assemble the message. */ |
| 1060 | CBB body; |
| 1061 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1062 | SSL3_MT_SERVER_KEY_EXCHANGE) || |
| 1063 | !CBB_add_bytes(&body, ssl->s3->tmp.server_params, |
| 1064 | ssl->s3->tmp.server_params_len)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1065 | goto err; |
| 1066 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1067 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1068 | /* Add a signature. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1069 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1070 | if (!ssl_has_private_key(ssl)) { |
| 1071 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1072 | goto err; |
| 1073 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1074 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1075 | /* Determine the signature algorithm. */ |
| 1076 | uint16_t signature_algorithm; |
| 1077 | if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) { |
| 1078 | goto err; |
| 1079 | } |
| 1080 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1081 | if (!CBB_add_u16(&body, signature_algorithm)) { |
| 1082 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1083 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1084 | goto err; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /* Add space for the signature. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1089 | 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] | 1090 | uint8_t *ptr; |
| 1091 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 1092 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1093 | goto err; |
| 1094 | } |
| 1095 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1096 | size_t sig_len; |
| 1097 | enum ssl_private_key_result_t sign_result; |
| 1098 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1099 | CBB transcript; |
| 1100 | uint8_t *transcript_data; |
| 1101 | size_t transcript_len; |
| 1102 | if (!CBB_init(&transcript, |
| 1103 | 2*SSL3_RANDOM_SIZE + ssl->s3->tmp.server_params_len) || |
| 1104 | !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1105 | !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1106 | !CBB_add_bytes(&transcript, ssl->s3->tmp.server_params, |
| 1107 | ssl->s3->tmp.server_params_len) || |
| 1108 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1109 | CBB_cleanup(&transcript); |
| 1110 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1111 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1112 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1113 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1114 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1115 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, |
| 1116 | signature_algorithm, transcript_data, |
| 1117 | transcript_len); |
| 1118 | OPENSSL_free(transcript_data); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1119 | } else { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1120 | assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1121 | 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] | 1122 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1123 | |
| 1124 | switch (sign_result) { |
| 1125 | case ssl_private_key_success: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1126 | if (!CBB_did_write(&child, sig_len)) { |
| 1127 | goto err; |
| 1128 | } |
| 1129 | break; |
| 1130 | case ssl_private_key_failure: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1131 | goto err; |
| 1132 | case ssl_private_key_retry: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1133 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1134 | ssl->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1135 | goto err; |
| 1136 | } |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1139 | if (!ssl->method->finish_message(ssl, &cbb)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1140 | goto err; |
| 1141 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1142 | |
| 1143 | OPENSSL_free(ssl->s3->tmp.server_params); |
| 1144 | ssl->s3->tmp.server_params = NULL; |
| 1145 | ssl->s3->tmp.server_params_len = 0; |
| 1146 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1147 | ssl->state = SSL3_ST_SW_KEY_EXCH_C; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1148 | return ssl->method->write_message(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1149 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1150 | err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1151 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1152 | return -1; |
| 1153 | } |
| 1154 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1155 | static int add_cert_types(SSL *ssl, CBB *cbb) { |
| 1156 | /* Get configured signature algorithms. */ |
| 1157 | int have_rsa_sign = 0; |
| 1158 | int have_ecdsa_sign = 0; |
| 1159 | const uint16_t *sig_algs; |
| 1160 | size_t sig_algs_len = tls12_get_psigalgs(ssl, &sig_algs); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 1161 | for (size_t i = 0; i < sig_algs_len; i++) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1162 | switch (sig_algs[i]) { |
| 1163 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 1164 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 1165 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 1166 | case SSL_SIGN_RSA_PKCS1_SHA1: |
| 1167 | have_rsa_sign = 1; |
| 1168 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1169 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1170 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 1171 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 1172 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 1173 | case SSL_SIGN_ECDSA_SHA1: |
| 1174 | have_ecdsa_sign = 1; |
| 1175 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1176 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1179 | if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) { |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | /* ECDSA certs can be used with RSA cipher suites as well so we don't need to |
| 1184 | * check for SSL_kECDH or SSL_kECDHE. */ |
| 1185 | if (ssl->version >= TLS1_VERSION && have_ecdsa_sign && |
| 1186 | !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) { |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | return 1; |
| 1191 | } |
| 1192 | |
| 1193 | static int ssl3_send_certificate_request(SSL *ssl) { |
| 1194 | if (ssl->state == SSL3_ST_SW_CERT_REQ_B) { |
| 1195 | return ssl->method->write_message(ssl); |
| 1196 | } |
| 1197 | |
| 1198 | CBB cbb, body, cert_types, sigalgs_cbb; |
| 1199 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1200 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 1201 | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
| 1202 | !add_cert_types(ssl, &cert_types)) { |
| 1203 | goto err; |
| 1204 | } |
| 1205 | |
| 1206 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1207 | const uint16_t *sigalgs; |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1208 | size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1209 | if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) { |
| 1210 | goto err; |
| 1211 | } |
| 1212 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1213 | for (size_t i = 0; i < num_sigalgs; i++) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1214 | if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) { |
| 1215 | goto err; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (!ssl_add_client_CA_list(ssl, &body) || |
| 1221 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1222 | goto err; |
| 1223 | } |
| 1224 | |
| 1225 | ssl->state = SSL3_ST_SW_CERT_REQ_B; |
| 1226 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1227 | |
| 1228 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1229 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1230 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1231 | return -1; |
| 1232 | } |
| 1233 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1234 | static int ssl3_send_server_hello_done(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1235 | if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) { |
| 1236 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1237 | } |
| 1238 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1239 | CBB cbb, body; |
| 1240 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) || |
| 1241 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1242 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1243 | CBB_cleanup(&cbb); |
| 1244 | return -1; |
| 1245 | } |
| 1246 | |
| 1247 | ssl->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1248 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | static int ssl3_get_client_certificate(SSL *ssl) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1252 | assert(ssl->s3->tmp.cert_request); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1253 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1254 | int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 1255 | if (msg_ret <= 0) { |
| 1256 | return msg_ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 1260 | if (ssl->version == SSL3_VERSION && |
| 1261 | ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1262 | /* In SSL 3.0, the Certificate message is omitted to signal no |
| 1263 | * certificate. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1264 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1265 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1266 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1267 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1270 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1271 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1272 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1273 | ssl->s3->tmp.reuse_message = 1; |
| 1274 | return 1; |
| 1275 | } |
| 1276 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1277 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1278 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1279 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1280 | } |
| 1281 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1282 | CBS certificate_msg; |
| 1283 | CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num); |
| 1284 | uint8_t alert; |
| 1285 | STACK_OF(X509) *chain = ssl_parse_cert_chain( |
| 1286 | ssl, &alert, ssl->ctx->retain_only_sha256_of_client_certs |
| 1287 | ? ssl->s3->new_session->peer_sha256 |
| 1288 | : NULL, |
| 1289 | &certificate_msg); |
| 1290 | if (chain == NULL) { |
| 1291 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1292 | goto err; |
| 1293 | } |
| 1294 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1295 | if (CBS_len(&certificate_msg) != 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1296 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1297 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1298 | goto err; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1299 | } |
| 1300 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1301 | if (sk_X509_num(chain) == 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1302 | /* No client certificate so the handshake buffer may be discarded. */ |
| 1303 | ssl3_free_handshake_buffer(ssl); |
| 1304 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1305 | /* In SSL 3.0, sending no certificate is signaled by omitting the |
| 1306 | * Certificate message. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1307 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1308 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1309 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1310 | goto err; |
| 1311 | } |
| 1312 | |
| 1313 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1314 | /* Fail for TLS only if we required a certificate */ |
| 1315 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1316 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1317 | goto err; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1318 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1319 | |
| 1320 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1321 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1322 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1323 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1324 | /* The hash would have been filled in. */ |
| 1325 | if (ssl->ctx->retain_only_sha256_of_client_certs) { |
| 1326 | ssl->s3->new_session->peer_sha256_valid = 1; |
| 1327 | } |
| 1328 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1329 | if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result, |
| 1330 | chain)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1331 | goto err; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1335 | X509_free(ssl->s3->new_session->peer); |
| 1336 | ssl->s3->new_session->peer = sk_X509_shift(chain); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1337 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1338 | sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free); |
| 1339 | ssl->s3->new_session->cert_chain = chain; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1340 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 1341 | * certificate, while we do include it in s3_clnt.c */ |
| 1342 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1343 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1344 | |
| 1345 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1346 | sk_X509_pop_free(chain, X509_free); |
| 1347 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | static int ssl3_get_client_key_exchange(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1351 | int al; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1352 | CBS client_key_exchange; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1353 | uint32_t alg_k; |
| 1354 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1355 | uint8_t *premaster_secret = NULL; |
| 1356 | size_t premaster_secret_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1357 | uint8_t *decrypt_buf = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1358 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1359 | unsigned psk_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1360 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1361 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1362 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1363 | int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1364 | ssl_hash_message); |
| 1365 | if (ret <= 0) { |
| 1366 | return ret; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1367 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1368 | } |
| 1369 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1370 | CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num); |
| 1371 | alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1372 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1373 | |
| 1374 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1375 | if (alg_a & SSL_aPSK) { |
| 1376 | CBS psk_identity; |
| 1377 | |
| 1378 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1379 | * then this is the only field in the message. */ |
| 1380 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1381 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1382 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1383 | al = SSL_AD_DECODE_ERROR; |
| 1384 | goto f_err; |
| 1385 | } |
| 1386 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1387 | if (ssl->psk_server_callback == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1388 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1389 | al = SSL_AD_INTERNAL_ERROR; |
| 1390 | goto f_err; |
| 1391 | } |
| 1392 | |
| 1393 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1394 | CBS_contains_zero_byte(&psk_identity)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1395 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1396 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1397 | goto f_err; |
| 1398 | } |
| 1399 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1400 | if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1401 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1402 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1403 | goto f_err; |
| 1404 | } |
| 1405 | |
| 1406 | /* Look up the key for the identity. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1407 | psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity, |
| 1408 | psk, sizeof(psk)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1409 | if (psk_len > PSK_MAX_PSK_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1410 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1411 | al = SSL_AD_INTERNAL_ERROR; |
| 1412 | goto f_err; |
| 1413 | } else if (psk_len == 0) { |
| 1414 | /* PSK related to the given identity not found */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1415 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1416 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1417 | goto f_err; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1422 | * |premaster_secret_len|. */ |
| 1423 | if (alg_k & SSL_kRSA) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1424 | /* Allocate a buffer large enough for an RSA decryption. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1425 | const size_t rsa_size = ssl_private_key_max_signature_len(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1426 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1427 | if (decrypt_buf == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1428 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1429 | goto err; |
| 1430 | } |
| 1431 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1432 | enum ssl_private_key_result_t decrypt_result; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1433 | size_t decrypt_len; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1434 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1435 | if (!ssl_has_private_key(ssl) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1436 | ssl_private_key_type(ssl) != NID_rsaEncryption) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1437 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1438 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1439 | goto f_err; |
| 1440 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1441 | CBS encrypted_premaster_secret; |
| 1442 | if (ssl->version > SSL3_VERSION) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1443 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1444 | &encrypted_premaster_secret) || |
| 1445 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1446 | al = SSL_AD_DECODE_ERROR; |
| 1447 | OPENSSL_PUT_ERROR(SSL, |
| 1448 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1449 | goto f_err; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1450 | } |
| 1451 | } else { |
| 1452 | encrypted_premaster_secret = client_key_exchange; |
| 1453 | } |
| 1454 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1455 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1456 | * timing-sensitive code below. */ |
| 1457 | decrypt_result = ssl_private_key_decrypt( |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1458 | ssl, decrypt_buf, &decrypt_len, rsa_size, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1459 | CBS_data(&encrypted_premaster_secret), |
| 1460 | CBS_len(&encrypted_premaster_secret)); |
| 1461 | } else { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1462 | assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1463 | /* Complete async decrypt. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1464 | decrypt_result = |
| 1465 | ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1466 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1467 | |
| 1468 | switch (decrypt_result) { |
| 1469 | case ssl_private_key_success: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1470 | break; |
| 1471 | case ssl_private_key_failure: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1472 | goto err; |
| 1473 | case ssl_private_key_retry: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1474 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1475 | ssl->state = SSL3_ST_SR_KEY_EXCH_B; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1476 | goto err; |
| 1477 | } |
| 1478 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1479 | if (decrypt_len != rsa_size) { |
| 1480 | al = SSL_AD_DECRYPT_ERROR; |
| 1481 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1482 | goto f_err; |
| 1483 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1484 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1485 | /* Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1486 | * section 7.4.7.1. */ |
| 1487 | premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1488 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1489 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1490 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1491 | goto err; |
| 1492 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1493 | if (!RAND_bytes(premaster_secret, premaster_secret_len)) { |
| 1494 | goto err; |
| 1495 | } |
| 1496 | |
| 1497 | /* The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1498 | * publicly invalid. */ |
| 1499 | if (decrypt_len < 11 + premaster_secret_len) { |
| 1500 | al = SSL_AD_DECRYPT_ERROR; |
| 1501 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1502 | goto f_err; |
| 1503 | } |
| 1504 | |
| 1505 | /* Check the padding. See RFC 3447, section 7.2.2. */ |
| 1506 | size_t padding_len = decrypt_len - premaster_secret_len; |
| 1507 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1508 | constant_time_eq_int_8(decrypt_buf[1], 2); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 1509 | for (size_t i = 2; i < padding_len - 1; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1510 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1511 | } |
| 1512 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1513 | |
| 1514 | /* The premaster secret must begin with |client_version|. This too must be |
| 1515 | * checked in constant time (http://eprint.iacr.org/2003/052/). */ |
| 1516 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
| 1517 | (unsigned)(ssl->client_version >> 8)); |
| 1518 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
| 1519 | (unsigned)(ssl->client_version & 0xff)); |
| 1520 | |
| 1521 | /* Select, in constant time, either the decrypted premaster or the random |
| 1522 | * premaster based on |good|. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 1523 | for (size_t i = 0; i < premaster_secret_len; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1524 | premaster_secret[i] = constant_time_select_8( |
| 1525 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1526 | } |
| 1527 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1528 | OPENSSL_free(decrypt_buf); |
| 1529 | decrypt_buf = NULL; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1530 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
| 1531 | /* Parse the ClientKeyExchange. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1532 | CBS peer_key; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1533 | if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange, |
| 1534 | &peer_key) || |
| 1535 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1536 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1537 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1538 | goto f_err; |
| 1539 | } |
| 1540 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1541 | /* Compute the premaster. */ |
| 1542 | uint8_t alert; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1543 | if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret, |
| 1544 | &premaster_secret_len, &alert, CBS_data(&peer_key), |
| 1545 | CBS_len(&peer_key))) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1546 | al = alert; |
| 1547 | goto f_err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1550 | /* The key exchange state may now be discarded. */ |
| 1551 | SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1552 | } else if (alg_k & SSL_kPSK) { |
| 1553 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1554 | * pre-shared key. */ |
| 1555 | premaster_secret_len = psk_len; |
| 1556 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1557 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1558 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1559 | goto err; |
| 1560 | } |
| 1561 | memset(premaster_secret, 0, premaster_secret_len); |
| 1562 | } else { |
| 1563 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1564 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1565 | goto f_err; |
| 1566 | } |
| 1567 | |
| 1568 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1569 | * pre-shared key. */ |
| 1570 | if (alg_a & SSL_aPSK) { |
| 1571 | CBB new_premaster, child; |
| 1572 | uint8_t *new_data; |
| 1573 | size_t new_len; |
| 1574 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1575 | CBB_zero(&new_premaster); |
| 1576 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1577 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1578 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1579 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1580 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1581 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1582 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1583 | CBB_cleanup(&new_premaster); |
| 1584 | goto err; |
| 1585 | } |
| 1586 | |
| 1587 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1588 | OPENSSL_free(premaster_secret); |
| 1589 | premaster_secret = new_data; |
| 1590 | premaster_secret_len = new_len; |
| 1591 | } |
| 1592 | |
| 1593 | /* Compute the master secret */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1594 | ssl->s3->new_session->master_key_length = tls1_generate_master_secret( |
| 1595 | ssl, ssl->s3->new_session->master_key, premaster_secret, |
| 1596 | premaster_secret_len); |
| 1597 | if (ssl->s3->new_session->master_key_length == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1598 | goto err; |
| 1599 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1600 | ssl->s3->new_session->extended_master_secret = |
| 1601 | ssl->s3->tmp.extended_master_secret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1602 | |
| 1603 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1604 | OPENSSL_free(premaster_secret); |
| 1605 | return 1; |
| 1606 | |
| 1607 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1608 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1609 | err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1610 | if (premaster_secret != NULL) { |
| 1611 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1612 | OPENSSL_free(premaster_secret); |
| 1613 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1614 | OPENSSL_free(decrypt_buf); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1615 | |
| 1616 | return -1; |
| 1617 | } |
| 1618 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1619 | static int ssl3_get_cert_verify(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1620 | int al, ret = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1621 | CBS certificate_verify, signature; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1622 | X509 *peer = ssl->s3->new_session->peer; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1623 | EVP_PKEY *pkey = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1624 | |
| 1625 | /* Only RSA and ECDSA client certificates are supported, so a |
| 1626 | * CertificateVerify is required if and only if there's a client certificate. |
| 1627 | * */ |
| 1628 | if (peer == NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1629 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1630 | return 1; |
| 1631 | } |
| 1632 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1633 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY, |
| 1634 | ssl_dont_hash_message); |
| 1635 | if (msg_ret <= 0) { |
| 1636 | return msg_ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | /* Filter out unsupported certificate types. */ |
| 1640 | pkey = X509_get_pubkey(peer); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1641 | if (pkey == NULL) { |
| 1642 | goto err; |
| 1643 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1644 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1645 | CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1646 | |
| 1647 | /* Determine the digest type if needbe. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1648 | uint16_t signature_algorithm = 0; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1649 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1650 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1651 | al = SSL_AD_DECODE_ERROR; |
| 1652 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1653 | goto f_err; |
| 1654 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1655 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1656 | goto f_err; |
| 1657 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1658 | ssl->s3->tmp.peer_signature_algorithm = signature_algorithm; |
| 1659 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1660 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 1661 | } else if (pkey->type == EVP_PKEY_EC) { |
| 1662 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
| 1663 | } else { |
| 1664 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1665 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1666 | goto f_err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1667 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1668 | |
| 1669 | /* Parse and verify the signature. */ |
| 1670 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1671 | CBS_len(&certificate_verify) != 0) { |
| 1672 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1673 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1674 | goto f_err; |
| 1675 | } |
| 1676 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1677 | int sig_ok; |
| 1678 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1679 | * single final digest and signature, and must be special-cased. */ |
| 1680 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 1681 | const EVP_MD *md; |
| 1682 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1683 | size_t digest_len; |
| 1684 | if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len, |
| 1685 | signature_algorithm)) { |
| 1686 | goto err; |
| 1687 | } |
| 1688 | |
| 1689 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 1690 | sig_ok = pctx != NULL && |
| 1691 | EVP_PKEY_verify_init(pctx) && |
| 1692 | EVP_PKEY_CTX_set_signature_md(pctx, md) && |
| 1693 | EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 1694 | digest, digest_len); |
| 1695 | EVP_PKEY_CTX_free(pctx); |
| 1696 | } else { |
| 1697 | sig_ok = ssl_public_key_verify( |
| 1698 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
| 1699 | pkey, (const uint8_t *)ssl->s3->handshake_buffer->data, |
| 1700 | ssl->s3->handshake_buffer->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1701 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1702 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1703 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1704 | sig_ok = 1; |
| 1705 | ERR_clear_error(); |
| 1706 | #endif |
| 1707 | if (!sig_ok) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1708 | al = SSL_AD_DECRYPT_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1709 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1710 | goto f_err; |
| 1711 | } |
| 1712 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1713 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 1714 | * message.*/ |
| 1715 | ssl3_free_handshake_buffer(ssl); |
| 1716 | if (!ssl->method->hash_current_message(ssl)) { |
| 1717 | goto err; |
| 1718 | } |
| 1719 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1720 | ret = 1; |
| 1721 | |
| 1722 | if (0) { |
| 1723 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1724 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | err: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1728 | EVP_PKEY_free(pkey); |
| 1729 | |
| 1730 | return ret; |
| 1731 | } |
| 1732 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1733 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 1734 | * sets the next_proto member in s if found */ |
| 1735 | static int ssl3_get_next_proto(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1736 | int ret = |
| 1737 | ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message); |
| 1738 | if (ret <= 0) { |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1742 | CBS next_protocol, selected_protocol, padding; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1743 | CBS_init(&next_protocol, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1744 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1745 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 1746 | CBS_len(&next_protocol) != 0) { |
| 1747 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1748 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated, |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1753 | &ssl->s3->next_proto_negotiated_len)) { |
| 1754 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1757 | return 1; |
| 1758 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1759 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1760 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 1761 | static int ssl3_get_channel_id(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1762 | int ret = -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1763 | uint8_t channel_id_hash[EVP_MAX_MD_SIZE]; |
| 1764 | size_t channel_id_hash_len; |
| 1765 | const uint8_t *p; |
| 1766 | uint16_t extension_type; |
| 1767 | EC_GROUP *p256 = NULL; |
| 1768 | EC_KEY *key = NULL; |
| 1769 | EC_POINT *point = NULL; |
| 1770 | ECDSA_SIG sig; |
| 1771 | BIGNUM x, y; |
| 1772 | CBS encrypted_extensions, extension; |
| 1773 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1774 | int msg_ret = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID, |
| 1775 | ssl_dont_hash_message); |
| 1776 | if (msg_ret <= 0) { |
| 1777 | return msg_ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | /* Before incorporating the EncryptedExtensions message to the handshake |
| 1781 | * hash, compute the hash that should have been signed. */ |
| 1782 | if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) { |
| 1783 | return -1; |
| 1784 | } |
| 1785 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
| 1786 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1787 | if (!ssl->method->hash_current_message(ssl)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1788 | return -1; |
| 1789 | } |
| 1790 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1791 | CBS_init(&encrypted_extensions, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1792 | |
| 1793 | /* EncryptedExtensions could include multiple extensions, but the only |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1794 | * extension that could be negotiated is Channel ID, so there can only be one |
| 1795 | * entry. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1796 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 1797 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 1798 | CBS_len(&encrypted_extensions) != 0 || |
| 1799 | extension_type != TLSEXT_TYPE_channel_id || |
| 1800 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame^] | 1801 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1802 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1803 | return -1; |
| 1804 | } |
| 1805 | |
| 1806 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 1807 | if (!p256) { |
| 1808 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
| 1809 | return -1; |
| 1810 | } |
| 1811 | |
| 1812 | BN_init(&x); |
| 1813 | BN_init(&y); |
| 1814 | sig.r = BN_new(); |
| 1815 | sig.s = BN_new(); |
| 1816 | if (sig.r == NULL || sig.s == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1817 | goto err; |
| 1818 | } |
| 1819 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1820 | p = CBS_data(&extension); |
| 1821 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 1822 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 1823 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 1824 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 1825 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1828 | point = EC_POINT_new(p256); |
| 1829 | if (!point || |
| 1830 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
| 1831 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1832 | } |
| 1833 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1834 | key = EC_KEY_new(); |
| 1835 | if (!key || !EC_KEY_set_group(key, p256) || |
| 1836 | !EC_KEY_set_public_key(key, point)) { |
| 1837 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1838 | } |
| 1839 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1840 | /* We stored the handshake hash in |tlsext_channel_id| the first time that we |
| 1841 | * were called. */ |
| 1842 | if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) { |
| 1843 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 1844 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1845 | goto err; |
| 1846 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1847 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1848 | memcpy(ssl->s3->tlsext_channel_id, p, 64); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1849 | ret = 1; |
| 1850 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1851 | err: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1852 | BN_free(&x); |
| 1853 | BN_free(&y); |
| 1854 | BN_free(sig.r); |
| 1855 | BN_free(sig.s); |
| 1856 | EC_KEY_free(key); |
| 1857 | EC_POINT_free(point); |
| 1858 | EC_GROUP_free(p256); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1859 | return ret; |
| 1860 | } |
| 1861 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1862 | static int ssl3_send_new_session_ticket(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1863 | if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) { |
| 1864 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1865 | } |
| 1866 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1867 | CBB cbb, body, ticket; |
| 1868 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1869 | SSL3_MT_NEW_SESSION_TICKET) || |
| 1870 | /* Ticket lifetime hint (advisory only): We leave this unspecified for |
| 1871 | * resumed session (for simplicity), and guess that tickets for new |
| 1872 | * sessions will live as long as their sessions. */ |
| 1873 | !CBB_add_u32(&body, |
| 1874 | ssl->session != NULL ? 0 : ssl->s3->new_session->timeout) || |
| 1875 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
| 1876 | !ssl_encrypt_ticket(ssl, &ticket, ssl->session != NULL |
| 1877 | ? ssl->session |
| 1878 | : ssl->s3->new_session) || |
| 1879 | !ssl->method->finish_message(ssl, &cbb)) { |
| 1880 | return 0; |
| 1881 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1882 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1883 | ssl->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 1884 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1885 | } |