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