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 | */ |
| 149 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 150 | #include <openssl/ssl.h> |
| 151 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 152 | #include <assert.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 153 | #include <string.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 154 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 155 | #include <openssl/aead.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 156 | #include <openssl/bn.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | #include <openssl/buf.h> |
| 158 | #include <openssl/bytestring.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 159 | #include <openssl/dh.h> |
| 160 | #include <openssl/ec_key.h> |
| 161 | #include <openssl/ecdsa.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 162 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 163 | #include <openssl/evp.h> |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 164 | #include <openssl/md5.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 165 | #include <openssl/mem.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 166 | #include <openssl/rand.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | #include <openssl/x509.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 168 | #include <openssl/x509v3.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
| 172 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 173 | static int ssl3_send_client_hello(SSL *ssl); |
| 174 | static int dtls1_get_hello_verify(SSL *ssl); |
| 175 | static int ssl3_get_server_hello(SSL *ssl); |
| 176 | static int ssl3_get_server_certificate(SSL *ssl); |
| 177 | static int ssl3_get_cert_status(SSL *ssl); |
| 178 | static int ssl3_verify_server_cert(SSL *ssl); |
| 179 | static int ssl3_get_server_key_exchange(SSL *ssl); |
| 180 | static int ssl3_get_certificate_request(SSL *ssl); |
| 181 | static int ssl3_get_server_hello_done(SSL *ssl); |
| 182 | static int ssl3_send_client_certificate(SSL *ssl); |
| 183 | static int ssl3_send_client_key_exchange(SSL *ssl); |
| 184 | static int ssl3_send_cert_verify(SSL *ssl); |
| 185 | static int ssl3_send_next_proto(SSL *ssl); |
| 186 | static int ssl3_send_channel_id(SSL *ssl); |
| 187 | static int ssl3_get_new_session_ticket(SSL *ssl); |
| 188 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 189 | int ssl3_connect(SSL *ssl) { |
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_connect); |
| 194 | assert(!ssl->server); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 195 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 196 | for (;;) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 197 | state = ssl->state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 198 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 199 | switch (ssl->state) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 200 | case SSL_ST_INIT: |
| 201 | ssl->state = SSL_ST_CONNECT; |
| 202 | skip = 1; |
| 203 | break; |
| 204 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 205 | case SSL_ST_CONNECT: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 206 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 207 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 208 | ssl->s3->hs = ssl_handshake_new(tls13_client_handshake); |
| 209 | if (ssl->s3->hs == NULL) { |
| 210 | ret = -1; |
| 211 | goto end; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 212 | } |
| 213 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 214 | if (!ssl_init_wbio_buffer(ssl)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 215 | ret = -1; |
| 216 | goto end; |
| 217 | } |
| 218 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 219 | ssl->state = SSL3_ST_CW_CLNT_HELLO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | break; |
| 221 | |
| 222 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 223 | case SSL3_ST_CW_CLNT_HELLO_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 224 | ret = ssl3_send_client_hello(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 225 | if (ret <= 0) { |
| 226 | goto end; |
| 227 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 228 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 229 | if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 230 | ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A; |
| 231 | } else { |
| 232 | ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; |
| 233 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 234 | ssl->state = SSL3_ST_CW_FLUSH; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 235 | break; |
| 236 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 237 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 238 | assert(SSL_is_dtls(ssl)); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 239 | ret = dtls1_get_hello_verify(ssl); |
| 240 | if (ret <= 0) { |
| 241 | goto end; |
| 242 | } |
| 243 | if (ssl->d1->send_cookie) { |
| 244 | ssl->method->received_flight(ssl); |
| 245 | ssl->state = SSL3_ST_CW_CLNT_HELLO_A; |
| 246 | } else { |
| 247 | ssl->state = SSL3_ST_CR_SRVR_HELLO_A; |
| 248 | } |
| 249 | break; |
| 250 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 251 | case SSL3_ST_CR_SRVR_HELLO_A: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 252 | ret = ssl3_get_server_hello(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 253 | if (ssl->state == SSL_ST_TLS13) { |
| 254 | break; |
| 255 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | if (ret <= 0) { |
| 257 | goto end; |
| 258 | } |
| 259 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 260 | if (ssl->session != NULL) { |
| 261 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | } else { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 263 | ssl->state = SSL3_ST_CR_CERT_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 264 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | break; |
| 266 | |
| 267 | case SSL3_ST_CR_CERT_A: |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 268 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 269 | ret = ssl3_get_server_certificate(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 270 | if (ret <= 0) { |
| 271 | goto end; |
| 272 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 273 | } else { |
| 274 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 275 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 276 | ssl->state = SSL3_ST_CR_CERT_STATUS_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 277 | break; |
| 278 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 279 | case SSL3_ST_CR_CERT_STATUS_A: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 280 | if (ssl->s3->hs->certificate_status_expected) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 281 | ret = ssl3_get_cert_status(ssl); |
| 282 | if (ret <= 0) { |
| 283 | goto end; |
| 284 | } |
| 285 | } else { |
| 286 | skip = 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 287 | } |
| 288 | ssl->state = SSL3_ST_VERIFY_SERVER_CERT; |
| 289 | break; |
| 290 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 291 | case SSL3_ST_VERIFY_SERVER_CERT: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 292 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
| 293 | ret = ssl3_verify_server_cert(ssl); |
| 294 | if (ret <= 0) { |
| 295 | goto end; |
| 296 | } |
| 297 | } else { |
| 298 | skip = 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 299 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 300 | ssl->state = SSL3_ST_CR_KEY_EXCH_A; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 301 | break; |
| 302 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 303 | case SSL3_ST_CR_KEY_EXCH_A: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 304 | ret = ssl3_get_server_key_exchange(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 305 | if (ret <= 0) { |
| 306 | goto end; |
| 307 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 308 | ssl->state = SSL3_ST_CR_CERT_REQ_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | case SSL3_ST_CR_CERT_REQ_A: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 312 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
| 313 | ret = ssl3_get_certificate_request(ssl); |
| 314 | if (ret <= 0) { |
| 315 | goto end; |
| 316 | } |
| 317 | } else { |
| 318 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 319 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 320 | ssl->state = SSL3_ST_CR_SRVR_DONE_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | case SSL3_ST_CR_SRVR_DONE_A: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 324 | ret = ssl3_get_server_hello_done(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 325 | if (ret <= 0) { |
| 326 | goto end; |
| 327 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 328 | ssl->method->received_flight(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 329 | ssl->state = SSL3_ST_CW_CERT_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case SSL3_ST_CW_CERT_A: |
| 333 | case SSL3_ST_CW_CERT_B: |
| 334 | case SSL3_ST_CW_CERT_C: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 335 | if (ssl->s3->hs->cert_request) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 336 | ret = ssl3_send_client_certificate(ssl); |
| 337 | if (ret <= 0) { |
| 338 | goto end; |
| 339 | } |
| 340 | } else { |
| 341 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 342 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 343 | ssl->state = SSL3_ST_CW_KEY_EXCH_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 344 | break; |
| 345 | |
| 346 | case SSL3_ST_CW_KEY_EXCH_A: |
| 347 | case SSL3_ST_CW_KEY_EXCH_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 348 | ret = ssl3_send_client_key_exchange(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 349 | if (ret <= 0) { |
| 350 | goto end; |
| 351 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 352 | ssl->state = SSL3_ST_CW_CERT_VRFY_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 353 | break; |
| 354 | |
| 355 | case SSL3_ST_CW_CERT_VRFY_A: |
| 356 | case SSL3_ST_CW_CERT_VRFY_B: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 357 | case SSL3_ST_CW_CERT_VRFY_C: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 358 | if (ssl->s3->hs->cert_request) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 359 | ret = ssl3_send_cert_verify(ssl); |
| 360 | if (ret <= 0) { |
| 361 | goto end; |
| 362 | } |
| 363 | } else { |
| 364 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 365 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 366 | ssl->state = SSL3_ST_CW_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 367 | break; |
| 368 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 369 | case SSL3_ST_CW_CHANGE: |
| 370 | ret = ssl->method->send_change_cipher_spec(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 371 | if (ret <= 0) { |
| 372 | goto end; |
| 373 | } |
| 374 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 375 | ssl->state = SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 376 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 377 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 378 | ret = -1; |
| 379 | goto end; |
| 380 | } |
| 381 | |
| 382 | break; |
| 383 | |
| 384 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 385 | case SSL3_ST_CW_NEXT_PROTO_B: |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 386 | if (ssl->s3->hs->next_proto_neg_seen) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 387 | ret = ssl3_send_next_proto(ssl); |
| 388 | if (ret <= 0) { |
| 389 | goto end; |
| 390 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 391 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 392 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 393 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 394 | ssl->state = SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 395 | break; |
| 396 | |
| 397 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 398 | case SSL3_ST_CW_CHANNEL_ID_B: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 399 | if (ssl->s3->tlsext_channel_id_valid) { |
| 400 | ret = ssl3_send_channel_id(ssl); |
| 401 | if (ret <= 0) { |
| 402 | goto end; |
| 403 | } |
| 404 | } else { |
| 405 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 406 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 407 | ssl->state = SSL3_ST_CW_FINISHED_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case SSL3_ST_CW_FINISHED_A: |
| 411 | case SSL3_ST_CW_FINISHED_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 412 | ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 413 | SSL3_ST_CW_FINISHED_B); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 414 | if (ret <= 0) { |
| 415 | goto end; |
| 416 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 417 | ssl->state = SSL3_ST_CW_FLUSH; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 418 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 419 | if (ssl->session != NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 420 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 421 | } else { |
| 422 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 423 | * record the handshake hashes at this point in the session so that |
| 424 | * any resumption of this session with ChannelID can sign those |
| 425 | * hashes. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 426 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 427 | if (ret <= 0) { |
| 428 | goto end; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 429 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 430 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 431 | ssl3_can_false_start(ssl) && |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 432 | /* No False Start on renegotiation (would complicate the state |
| 433 | * machine). */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 434 | !ssl->s3->initial_handshake_complete) { |
| 435 | ssl->s3->tmp.next_state = SSL3_ST_FALSE_START; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 436 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 437 | ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 440 | break; |
| 441 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 442 | case SSL3_ST_FALSE_START: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 443 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 444 | ssl->s3->hs->in_false_start = 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 445 | |
| 446 | ssl_free_wbio_buffer(ssl); |
| 447 | ret = 1; |
| 448 | goto end; |
| 449 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 450 | case SSL3_ST_CR_SESSION_TICKET_A: |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 451 | if (ssl->s3->hs->ticket_expected) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 452 | ret = ssl3_get_new_session_ticket(ssl); |
| 453 | if (ret <= 0) { |
| 454 | goto end; |
| 455 | } |
| 456 | } else { |
| 457 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 458 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 459 | ssl->state = SSL3_ST_CR_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 460 | break; |
| 461 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 462 | case SSL3_ST_CR_CHANGE: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 463 | ret = ssl->method->read_change_cipher_spec(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 464 | if (ret <= 0) { |
| 465 | goto end; |
| 466 | } |
| 467 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 468 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 469 | ret = -1; |
| 470 | goto end; |
| 471 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 472 | ssl->state = SSL3_ST_CR_FINISHED_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 473 | break; |
| 474 | |
| 475 | case SSL3_ST_CR_FINISHED_A: |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 476 | ret = ssl3_get_finished(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 477 | if (ret <= 0) { |
| 478 | goto end; |
| 479 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 480 | ssl->method->received_flight(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 481 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 482 | if (ssl->session != NULL) { |
| 483 | ssl->state = SSL3_ST_CW_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 484 | } else { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 485 | ssl->state = SSL_ST_OK; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 486 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 487 | break; |
| 488 | |
| 489 | case SSL3_ST_CW_FLUSH: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 490 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 491 | ssl->rwstate = SSL_WRITING; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 492 | ret = -1; |
| 493 | goto end; |
| 494 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 495 | ssl->state = ssl->s3->tmp.next_state; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 496 | if (ssl->state != SSL_ST_OK) { |
| 497 | ssl->method->expect_flight(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 498 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 499 | break; |
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 | case SSL_ST_TLS13: |
| 502 | ret = tls13_handshake(ssl); |
| 503 | if (ret <= 0) { |
| 504 | goto end; |
| 505 | } |
| 506 | ssl->state = SSL_ST_OK; |
| 507 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 508 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 509 | case SSL_ST_OK: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 510 | ssl->method->release_current_message(ssl, 1 /* free_buffer */); |
| 511 | |
| 512 | SSL_SESSION_free(ssl->s3->established_session); |
| 513 | if (ssl->session != NULL) { |
| 514 | SSL_SESSION_up_ref(ssl->session); |
| 515 | ssl->s3->established_session = ssl->session; |
| 516 | } else { |
| 517 | /* We make a copy of the session in order to maintain the immutability |
| 518 | * of the new established_session due to False Start. The caller may |
| 519 | * have taken a reference to the temporary session. */ |
| 520 | ssl->s3->established_session = |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 521 | SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 522 | if (ssl->s3->established_session == NULL) { |
| 523 | /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init| |
| 524 | * callers. */ |
| 525 | ssl->state = SSL_ST_ERROR; |
| 526 | skip = 1; |
| 527 | ret = -1; |
| 528 | goto end; |
| 529 | } |
| 530 | ssl->s3->established_session->not_resumable = 0; |
| 531 | |
| 532 | SSL_SESSION_free(ssl->s3->new_session); |
| 533 | ssl->s3->new_session = NULL; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 534 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 535 | |
| 536 | /* Remove write buffering now. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 537 | ssl_free_wbio_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 538 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 539 | const int is_initial_handshake = !ssl->s3->initial_handshake_complete; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 540 | ssl->s3->initial_handshake_complete = 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 541 | if (is_initial_handshake) { |
| 542 | /* Renegotiations do not participate in session resumption. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 543 | ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 544 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 545 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 546 | ssl_handshake_free(ssl->s3->hs); |
| 547 | ssl->s3->hs = NULL; |
| 548 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 549 | ret = 1; |
| 550 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 551 | goto end; |
| 552 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 553 | case SSL_ST_ERROR: |
| 554 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 555 | ret = -1; |
| 556 | goto end; |
| 557 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 558 | default: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 559 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 560 | ret = -1; |
| 561 | goto end; |
| 562 | } |
| 563 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 564 | if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) { |
| 565 | int new_state = ssl->state; |
| 566 | ssl->state = state; |
| 567 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1); |
| 568 | ssl->state = new_state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 569 | } |
| 570 | skip = 0; |
| 571 | } |
| 572 | |
| 573 | end: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 574 | ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 578 | uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) { |
| 579 | /* Use the client_random for entropy. This both avoids calling |RAND_bytes| on |
| 580 | * a single byte repeatedly and ensures the values are deterministic. This |
| 581 | * allows the same ClientHello be sent twice for a HelloRetryRequest or the |
| 582 | * same group be advertised in both supported_groups and key_shares. */ |
| 583 | uint16_t ret = ssl->s3->client_random[index]; |
| 584 | /* This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. */ |
| 585 | ret = (ret & 0xf0) | 0x0a; |
| 586 | ret |= ret << 8; |
| 587 | return ret; |
| 588 | } |
| 589 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 590 | static int ssl_write_client_cipher_list(SSL *ssl, CBB *out, |
| 591 | uint16_t min_version, |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 592 | uint16_t max_version) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 593 | /* Prepare disabled cipher masks. */ |
| 594 | ssl_set_client_disabled(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 595 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 596 | CBB child; |
| 597 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 598 | return 0; |
Adam Langley | fdeb488 | 2015-10-30 13:15:30 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 601 | /* Add a fake cipher suite. See draft-davidben-tls-grease-01. */ |
| 602 | if (ssl->ctx->grease_enabled && |
| 603 | !CBB_add_u16(&child, ssl_get_grease_value(ssl, ssl_grease_cipher))) { |
| 604 | return 0; |
| 605 | } |
| 606 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 607 | /* Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on |
| 608 | * hardware support. */ |
| 609 | if (max_version >= TLS1_3_VERSION) { |
| 610 | if (!EVP_has_aes_hardware() && |
| 611 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
| 612 | return 0; |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 613 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 614 | if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) || |
| 615 | !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) { |
| 616 | return 0; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 617 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 618 | if (EVP_has_aes_hardware() && |
| 619 | !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | } |
| 623 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 624 | if (min_version < TLS1_3_VERSION) { |
| 625 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
| 626 | int any_enabled = 0; |
| 627 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 628 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 629 | /* Skip disabled ciphers */ |
| 630 | if ((cipher->algorithm_mkey & ssl->cert->mask_k) || |
| 631 | (cipher->algorithm_auth & ssl->cert->mask_a)) { |
| 632 | continue; |
| 633 | } |
| 634 | if (SSL_CIPHER_get_min_version(cipher) > max_version || |
| 635 | SSL_CIPHER_get_max_version(cipher) < min_version) { |
| 636 | continue; |
| 637 | } |
| 638 | any_enabled = 1; |
| 639 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 640 | return 0; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /* If all ciphers were disabled, return the error to the caller. */ |
| 645 | if (!any_enabled && max_version < TLS1_3_VERSION) { |
| 646 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 647 | return 0; |
| 648 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 652 | * added. */ |
| 653 | if (ssl->client_version == SSL3_VERSION && |
| 654 | !ssl->s3->initial_handshake_complete) { |
| 655 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 656 | return 0; |
| 657 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 658 | } |
| 659 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 660 | if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 661 | if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 662 | return 0; |
| 663 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | return CBB_flush(out); |
| 667 | } |
| 668 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 669 | int ssl_write_client_hello(SSL *ssl) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 670 | uint16_t min_version, max_version; |
| 671 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 672 | return 0; |
| 673 | } |
| 674 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 675 | CBB cbb, body; |
| 676 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO)) { |
| 677 | goto err; |
| 678 | } |
| 679 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 680 | /* Renegotiations do not participate in session resumption. */ |
| 681 | int has_session = ssl->session != NULL && |
| 682 | !ssl->s3->initial_handshake_complete; |
| 683 | |
| 684 | CBB child; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 685 | if (!CBB_add_u16(&body, ssl->client_version) || |
| 686 | !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 687 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 688 | (has_session && |
| 689 | !CBB_add_bytes(&child, ssl->session->session_id, |
| 690 | ssl->session->session_id_length))) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 691 | goto err; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | if (SSL_is_dtls(ssl)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 695 | if (!CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 696 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 697 | goto err; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
| 701 | size_t header_len = |
| 702 | SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 703 | if (!ssl_write_client_cipher_list(ssl, &body, min_version, max_version) || |
| 704 | !CBB_add_u8(&body, 1 /* one compression method */) || |
| 705 | !CBB_add_u8(&body, 0 /* null compression */) || |
| 706 | !ssl_add_clienthello_tlsext(ssl, &body, header_len + CBB_len(&body))) { |
| 707 | goto err; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 710 | uint8_t *msg = NULL; |
| 711 | size_t len; |
| 712 | if (!ssl->method->finish_message(ssl, &cbb, &msg, &len)) { |
| 713 | goto err; |
| 714 | } |
| 715 | |
| 716 | /* Now that the length prefixes have been computed, fill in the placeholder |
| 717 | * PSK binder. */ |
| 718 | if (ssl->s3->hs->needs_psk_binder && |
| 719 | !tls13_write_psk_binder(ssl, msg, len)) { |
| 720 | OPENSSL_free(msg); |
| 721 | goto err; |
| 722 | } |
| 723 | |
| 724 | return ssl->method->queue_message(ssl, msg, len); |
| 725 | |
| 726 | err: |
| 727 | CBB_cleanup(&cbb); |
| 728 | return 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 729 | } |
| 730 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 731 | static int ssl3_send_client_hello(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 732 | if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 733 | return ssl->method->write_message(ssl); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 734 | } |
| 735 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 736 | /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we |
| 737 | * may send multiple ClientHellos if we receive HelloVerifyRequest. */ |
| 738 | if (!ssl3_init_handshake_buffer(ssl)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 739 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 740 | return -1; |
| 741 | } |
| 742 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 743 | uint16_t min_version, max_version; |
| 744 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 745 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 746 | } |
| 747 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 748 | assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A); |
| 749 | if (!ssl->s3->have_version) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 750 | ssl->version = ssl->method->version_to_wire(max_version); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 751 | /* Only set |ssl->client_version| on the initial handshake. Renegotiations, |
| 752 | * although locked to a version, reuse the value. When using the plain RSA |
| 753 | * key exchange, the ClientHello version is checked in the premaster secret. |
| 754 | * Some servers fail when this value changes. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 755 | ssl->client_version = ssl->version; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 756 | |
| 757 | if (max_version >= TLS1_3_VERSION) { |
| 758 | ssl->client_version = ssl->method->version_to_wire(TLS1_2_VERSION); |
| 759 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 760 | } |
| 761 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 762 | /* If the configured session has expired or was created at a disabled |
| 763 | * version, drop it. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 764 | if (ssl->session != NULL) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 765 | uint16_t session_version; |
| 766 | if (!ssl->method->version_from_wire(&session_version, |
| 767 | ssl->session->ssl_version) || |
| 768 | (session_version < TLS1_3_VERSION && |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 769 | ssl->session->session_id_length == 0) || |
| 770 | ssl->session->not_resumable || |
| 771 | !ssl_session_is_time_valid(ssl, ssl->session) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 772 | session_version < min_version || session_version > max_version) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 773 | ssl_set_session(ssl, NULL); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 774 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 778 | * renegerate the client_random. The random must be reused. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 779 | if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) && |
| 780 | !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 781 | return -1; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 784 | if (!ssl_write_client_hello(ssl)) { |
| 785 | return -1; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | ssl->state = SSL3_ST_CW_CLNT_HELLO_B; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 789 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 790 | } |
| 791 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 792 | static int dtls1_get_hello_verify(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 793 | int al; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 794 | CBS hello_verify_request, cookie; |
| 795 | uint16_t server_version; |
| 796 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 797 | int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 798 | if (ret <= 0) { |
| 799 | return ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 803 | ssl->d1->send_cookie = 0; |
| 804 | ssl->s3->tmp.reuse_message = 1; |
| 805 | return 1; |
| 806 | } |
| 807 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 808 | CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 809 | |
| 810 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 811 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 812 | CBS_len(&hello_verify_request) != 0) { |
| 813 | al = SSL_AD_DECODE_ERROR; |
| 814 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 815 | goto f_err; |
| 816 | } |
| 817 | |
| 818 | if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) { |
| 819 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 820 | goto f_err; |
| 821 | } |
| 822 | |
| 823 | memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
| 824 | ssl->d1->cookie_len = CBS_len(&cookie); |
| 825 | |
| 826 | ssl->d1->send_cookie = 1; |
| 827 | return 1; |
| 828 | |
| 829 | f_err: |
| 830 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 831 | return -1; |
| 832 | } |
| 833 | |
| 834 | static int ssl3_get_server_hello(SSL *ssl) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 835 | CERT *ct = ssl->cert; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 836 | int al = SSL_AD_INTERNAL_ERROR; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 837 | CBS server_hello, server_random, session_id; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 838 | uint16_t server_wire_version, cipher_suite; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 839 | uint8_t compression_method; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 840 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 841 | int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 842 | if (ret <= 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 843 | uint32_t err = ERR_peek_error(); |
| 844 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 845 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 846 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 847 | * in response to ClientHello. This matches NSS's client behavior and |
| 848 | * gives a better error on a (probable) failure to negotiate initial |
| 849 | * parameters. Note: this error code comes after the original one. |
| 850 | * |
| 851 | * See https://crbug.com/446505. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 852 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 853 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 854 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 855 | } |
| 856 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 857 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO && |
| 858 | ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) { |
| 859 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 860 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 861 | return -1; |
| 862 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 863 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 864 | CBS_init(&server_hello, ssl->init_msg, ssl->init_num); |
| 865 | |
| 866 | if (!CBS_get_u16(&server_hello, &server_wire_version)) { |
| 867 | al = SSL_AD_DECODE_ERROR; |
| 868 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 869 | goto f_err; |
| 870 | } |
| 871 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 872 | uint16_t min_version, max_version, server_version; |
| 873 | if (!ssl_get_version_range(ssl, &min_version, &max_version) || |
| 874 | !ssl->method->version_from_wire(&server_version, server_wire_version) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 875 | server_version < min_version || server_version > max_version) { |
| 876 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 877 | al = SSL_AD_PROTOCOL_VERSION; |
| 878 | goto f_err; |
| 879 | } |
| 880 | |
| 881 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 882 | if (!ssl->s3->have_version) { |
| 883 | ssl->version = server_wire_version; |
| 884 | ssl->s3->enc_method = ssl3_get_enc_method(server_version); |
| 885 | assert(ssl->s3->enc_method != NULL); |
| 886 | /* At this point, the connection's version is known and ssl->version is |
| 887 | * fixed. Begin enforcing the record-layer version. */ |
| 888 | ssl->s3->have_version = 1; |
| 889 | } else if (server_wire_version != ssl->version) { |
| 890 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 891 | al = SSL_AD_PROTOCOL_VERSION; |
| 892 | goto f_err; |
| 893 | } |
| 894 | |
| 895 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 896 | ssl->state = SSL_ST_TLS13; |
| 897 | return 1; |
| 898 | } |
| 899 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 900 | ssl_clear_tls13_state(ssl); |
| 901 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 902 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { |
| 903 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 904 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 905 | return -1; |
| 906 | } |
| 907 | |
| 908 | if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 909 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 910 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 911 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 912 | !CBS_get_u8(&server_hello, &compression_method)) { |
| 913 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 914 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 915 | goto f_err; |
| 916 | } |
| 917 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 918 | /* Copy over the server random. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 919 | memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 920 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 921 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 922 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 923 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 924 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 925 | ssl->session->session_id_length != 0 && |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 926 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 927 | ssl->session->session_id_length)) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 928 | ssl->s3->session_reused = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 929 | } else { |
| 930 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 931 | * fill out. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 932 | ssl_set_session(ssl, NULL); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 933 | if (!ssl_get_new_session(ssl, 0 /* client */)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 934 | goto f_err; |
| 935 | } |
| 936 | /* Note: session_id could be empty. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 937 | ssl->s3->new_session->session_id_length = CBS_len(&session_id); |
| 938 | memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id), |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 939 | CBS_len(&session_id)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 940 | } |
| 941 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 942 | const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 943 | if (c == NULL) { |
| 944 | /* unknown cipher */ |
| 945 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 946 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 947 | goto f_err; |
| 948 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 949 | |
| 950 | /* The cipher must be allowed in the selected version and enabled. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 951 | if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 952 | SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 953 | SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl) || |
| 954 | !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, c)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 955 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 956 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 957 | goto f_err; |
| 958 | } |
| 959 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 960 | if (ssl->session != NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 961 | if (ssl->session->ssl_version != ssl->version) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 962 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 963 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 964 | goto f_err; |
| 965 | } |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 966 | if (ssl->session->cipher != c) { |
| 967 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 968 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 969 | goto f_err; |
| 970 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 971 | if (!ssl_session_is_context_valid(ssl, ssl->session)) { |
| 972 | /* This is actually a client application bug. */ |
| 973 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 974 | OPENSSL_PUT_ERROR(SSL, |
| 975 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 976 | goto f_err; |
| 977 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 978 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 979 | ssl->s3->new_session->cipher = c; |
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 | ssl->s3->tmp.new_cipher = c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 982 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 983 | /* Now that the cipher is known, initialize the handshake hash. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 984 | if (!ssl3_init_handshake_hash(ssl)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 985 | goto f_err; |
| 986 | } |
| 987 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 988 | /* If doing a full handshake, the server may request a client certificate |
| 989 | * which requires hashing the handshake transcript. Otherwise, the handshake |
| 990 | * buffer may be released. */ |
| 991 | if (ssl->session != NULL || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 992 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 993 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* Only the NULL compression algorithm is supported. */ |
| 997 | if (compression_method != 0) { |
| 998 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 999 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1000 | goto f_err; |
| 1001 | } |
| 1002 | |
| 1003 | /* TLS extensions */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1004 | if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1005 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1006 | goto err; |
| 1007 | } |
| 1008 | |
| 1009 | /* There should be nothing left over in the record. */ |
| 1010 | if (CBS_len(&server_hello) != 0) { |
| 1011 | /* wrong packet length */ |
| 1012 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1013 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1014 | goto f_err; |
| 1015 | } |
| 1016 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1017 | if (ssl->session != NULL && |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1018 | ssl->s3->tmp.extended_master_secret != |
| 1019 | ssl->session->extended_master_secret) { |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1020 | al = SSL_AD_HANDSHAKE_FAILURE; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1021 | if (ssl->session->extended_master_secret) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1022 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1023 | } else { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1024 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1025 | } |
| 1026 | goto f_err; |
| 1027 | } |
| 1028 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1029 | return 1; |
| 1030 | |
| 1031 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1032 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1033 | err: |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1037 | static int ssl3_get_server_certificate(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1038 | int ret = |
| 1039 | ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message); |
| 1040 | if (ret <= 0) { |
| 1041 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1044 | CBS cbs; |
| 1045 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
| 1046 | uint8_t alert; |
| 1047 | STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs); |
| 1048 | if (chain == NULL) { |
| 1049 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1050 | goto err; |
| 1051 | } |
| 1052 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1053 | if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1054 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1055 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1056 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1059 | X509 *leaf = sk_X509_value(chain, 0); |
| 1060 | if (!ssl_check_leaf_certificate(ssl, leaf)) { |
| 1061 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1062 | goto err; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 1063 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1064 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1065 | /* NOTE: Unlike the server half, the client's copy of |x509_chain| includes |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1066 | * the leaf. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1067 | sk_X509_pop_free(ssl->s3->new_session->x509_chain, X509_free); |
| 1068 | ssl->s3->new_session->x509_chain = chain; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1069 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1070 | X509_free(ssl->s3->new_session->x509_peer); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1071 | X509_up_ref(leaf); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1072 | ssl->s3->new_session->x509_peer = leaf; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1073 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1074 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1075 | |
| 1076 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1077 | sk_X509_pop_free(chain, X509_free); |
| 1078 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1081 | static int ssl3_get_cert_status(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1082 | int al; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1083 | CBS certificate_status, ocsp_response; |
| 1084 | uint8_t status_type; |
| 1085 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1086 | int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 1087 | if (ret <= 0) { |
| 1088 | return ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { |
| 1092 | /* A server may send status_request in ServerHello and then change |
| 1093 | * its mind about sending CertificateStatus. */ |
| 1094 | ssl->s3->tmp.reuse_message = 1; |
| 1095 | return 1; |
| 1096 | } |
| 1097 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1098 | CBS_init(&certificate_status, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1099 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1100 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1101 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1102 | CBS_len(&ocsp_response) == 0 || |
| 1103 | CBS_len(&certificate_status) != 0) { |
| 1104 | al = SSL_AD_DECODE_ERROR; |
| 1105 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1106 | goto f_err; |
| 1107 | } |
| 1108 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1109 | if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response, |
| 1110 | &ssl->s3->new_session->ocsp_response_length)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1111 | al = SSL_AD_INTERNAL_ERROR; |
| 1112 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1113 | goto f_err; |
| 1114 | } |
| 1115 | return 1; |
| 1116 | |
| 1117 | f_err: |
| 1118 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 1119 | return -1; |
| 1120 | } |
| 1121 | |
| 1122 | static int ssl3_verify_server_cert(SSL *ssl) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1123 | if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result, |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1124 | ssl->s3->new_session->x509_chain)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1125 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1126 | } |
| 1127 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1128 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static int ssl3_get_server_key_exchange(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1132 | int al; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1133 | EVP_PKEY *pkey = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1134 | DH *dh = NULL; |
| 1135 | EC_KEY *ecdh = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1136 | EC_POINT *srvr_ecpoint = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1137 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1138 | int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 1139 | if (ret <= 0) { |
| 1140 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1143 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1144 | /* Some ciphers (pure PSK) have an optional ServerKeyExchange message. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1145 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1146 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1147 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1148 | return -1; |
| 1149 | } |
| 1150 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1151 | ssl->s3->tmp.reuse_message = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1152 | return 1; |
| 1153 | } |
| 1154 | |
| 1155 | /* Retain a copy of the original CBS to compute the signature over. */ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1156 | CBS server_key_exchange; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1157 | CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1158 | CBS server_key_exchange_orig = server_key_exchange; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1159 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1160 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1161 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1162 | |
| 1163 | if (alg_a & SSL_aPSK) { |
| 1164 | CBS psk_identity_hint; |
| 1165 | |
| 1166 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1167 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1168 | &psk_identity_hint)) { |
| 1169 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1170 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1171 | goto f_err; |
| 1172 | } |
| 1173 | |
| 1174 | /* Store PSK identity hint for later use, hint is used in |
| 1175 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1176 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1177 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1178 | * |
| 1179 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1180 | * a specific identity. */ |
| 1181 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1182 | CBS_contains_zero_byte(&psk_identity_hint)) { |
| 1183 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1184 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1185 | goto f_err; |
| 1186 | } |
| 1187 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1188 | /* Save non-empty identity hints as a C string. Empty identity hints we |
| 1189 | * treat as missing. Plain PSK makes it possible to send either no hint |
| 1190 | * (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell |
| 1191 | * empty hint. Having different capabilities is odd, so we interpret empty |
| 1192 | * and missing as identical. */ |
| 1193 | if (CBS_len(&psk_identity_hint) != 0 && |
| 1194 | !CBS_strdup(&psk_identity_hint, &ssl->s3->hs->peer_psk_identity_hint)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1195 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1196 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1197 | goto f_err; |
| 1198 | } |
| 1199 | } |
| 1200 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1201 | if (alg_k & SSL_kDHE) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1202 | CBS dh_p, dh_g, dh_Ys; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1203 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1204 | CBS_len(&dh_p) == 0 || |
| 1205 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1206 | CBS_len(&dh_g) == 0 || |
| 1207 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1208 | CBS_len(&dh_Ys) == 0) { |
| 1209 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1210 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1211 | goto f_err; |
| 1212 | } |
| 1213 | |
| 1214 | dh = DH_new(); |
| 1215 | if (dh == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1216 | goto err; |
| 1217 | } |
| 1218 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1219 | dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL); |
| 1220 | dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL); |
| 1221 | if (dh->p == NULL || dh->g == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1222 | goto err; |
| 1223 | } |
| 1224 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1225 | ssl->s3->new_session->key_exchange_info = DH_num_bits(dh); |
| 1226 | if (ssl->s3->new_session->key_exchange_info < 1024) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1227 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1228 | goto err; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1229 | } else if (ssl->s3->new_session->key_exchange_info > 4096) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1230 | /* Overly large DHE groups are prohibitively expensive, so enforce a limit |
| 1231 | * to prevent a server from causing us to perform too expensive of a |
| 1232 | * computation. */ |
| 1233 | OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1234 | goto err; |
| 1235 | } |
| 1236 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1237 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->hs->ecdh_ctx, dh); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1238 | dh = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1239 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1240 | /* Save the peer public key for later. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1241 | if (!CBS_stow(&dh_Ys, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1242 | goto err; |
| 1243 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1244 | } else if (alg_k & SSL_kECDHE) { |
| 1245 | /* Parse the server parameters. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1246 | uint8_t group_type; |
| 1247 | uint16_t group_id; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1248 | CBS point; |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1249 | if (!CBS_get_u8(&server_key_exchange, &group_type) || |
| 1250 | group_type != NAMED_CURVE_TYPE || |
| 1251 | !CBS_get_u16(&server_key_exchange, &group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1252 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1253 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1254 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1255 | goto f_err; |
| 1256 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1257 | ssl->s3->new_session->key_exchange_info = group_id; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1258 | |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1259 | /* Ensure the group is consistent with preferences. */ |
| 1260 | if (!tls1_check_group_id(ssl, group_id)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1261 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1262 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1263 | goto f_err; |
| 1264 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1265 | |
| 1266 | /* Initialize ECDH and save the peer public key for later. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1267 | if (!SSL_ECDH_CTX_init(&ssl->s3->hs->ecdh_ctx, group_id) || |
| 1268 | !CBS_stow(&point, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1269 | goto err; |
| 1270 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1271 | } else if (alg_k & SSL_kCECPQ1) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1272 | SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->hs->ecdh_ctx); |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1273 | CBS key; |
| 1274 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) { |
| 1275 | al = SSL_AD_DECODE_ERROR; |
| 1276 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1277 | goto f_err; |
| 1278 | } |
| 1279 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1280 | if (!CBS_stow(&key, &ssl->s3->hs->peer_key, &ssl->s3->hs->peer_key_len)) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1281 | goto err; |
| 1282 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1283 | } else if (!(alg_k & SSL_kPSK)) { |
| 1284 | al = SSL_AD_UNEXPECTED_MESSAGE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1285 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1286 | goto f_err; |
| 1287 | } |
| 1288 | |
| 1289 | /* At this point, |server_key_exchange| contains the signature, if any, while |
| 1290 | * |server_key_exchange_orig| contains the entire message. From that, derive |
| 1291 | * a CBS containing just the parameter. */ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1292 | CBS parameter; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1293 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1294 | CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange)); |
| 1295 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1296 | /* ServerKeyExchange should be signed by the server's public key. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1297 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1298 | pkey = X509_get_pubkey(ssl->s3->new_session->x509_peer); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1299 | if (pkey == NULL) { |
| 1300 | goto err; |
| 1301 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1302 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1303 | uint16_t signature_algorithm = 0; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1304 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1305 | if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1306 | al = SSL_AD_DECODE_ERROR; |
| 1307 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1308 | goto f_err; |
| 1309 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1310 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1311 | goto f_err; |
| 1312 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1313 | ssl->s3->tmp.peer_signature_algorithm = signature_algorithm; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1314 | } else if (pkey->type == EVP_PKEY_RSA) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1315 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
| 1316 | } else if (pkey->type == EVP_PKEY_EC) { |
| 1317 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1318 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1319 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1320 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1321 | goto f_err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /* The last field in |server_key_exchange| is the signature. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1325 | CBS signature; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1326 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1327 | CBS_len(&server_key_exchange) != 0) { |
| 1328 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1329 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1330 | goto f_err; |
| 1331 | } |
| 1332 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1333 | CBB transcript; |
| 1334 | uint8_t *transcript_data; |
| 1335 | size_t transcript_len; |
| 1336 | if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(¶meter)) || |
| 1337 | !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1338 | !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1339 | !CBB_add_bytes(&transcript, CBS_data(¶meter), CBS_len(¶meter)) || |
| 1340 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1341 | CBB_cleanup(&transcript); |
| 1342 | al = SSL_AD_INTERNAL_ERROR; |
| 1343 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1344 | goto f_err; |
| 1345 | } |
| 1346 | |
| 1347 | int sig_ok = ssl_public_key_verify( |
| 1348 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
| 1349 | pkey, transcript_data, transcript_len); |
| 1350 | OPENSSL_free(transcript_data); |
| 1351 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1352 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1353 | sig_ok = 1; |
| 1354 | ERR_clear_error(); |
| 1355 | #endif |
| 1356 | if (!sig_ok) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1357 | /* bad signature */ |
| 1358 | al = SSL_AD_DECRYPT_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1359 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1360 | goto f_err; |
| 1361 | } |
| 1362 | } else { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1363 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1364 | assert(alg_a == SSL_aPSK); |
| 1365 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1366 | if (CBS_len(&server_key_exchange) > 0) { |
| 1367 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1368 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1369 | goto f_err; |
| 1370 | } |
| 1371 | } |
| 1372 | EVP_PKEY_free(pkey); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1373 | return 1; |
| 1374 | |
| 1375 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1376 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1377 | err: |
| 1378 | EVP_PKEY_free(pkey); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1379 | DH_free(dh); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1380 | EC_POINT_free(srvr_ecpoint); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1381 | EC_KEY_free(ecdh); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1382 | return -1; |
| 1383 | } |
| 1384 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1385 | static int ssl3_get_certificate_request(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1386 | int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message); |
| 1387 | if (msg_ret <= 0) { |
| 1388 | return msg_ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1389 | } |
| 1390 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1391 | if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1392 | ssl->s3->tmp.reuse_message = 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1393 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1394 | * client auth. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1395 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1396 | return 1; |
| 1397 | } |
| 1398 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1399 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 1400 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1401 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1402 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1405 | CBS cbs; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1406 | CBS_init(&cbs, ssl->init_msg, ssl->init_num); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1407 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1408 | /* Get the certificate types. */ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1409 | CBS certificate_types; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1410 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1411 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1412 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1413 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1414 | } |
| 1415 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1416 | if (!CBS_stow(&certificate_types, &ssl->s3->hs->certificate_types, |
| 1417 | &ssl->s3->hs->num_certificate_types)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1418 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1419 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1422 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1423 | CBS supported_signature_algorithms; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1424 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1425 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
| 1426 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1427 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1428 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1432 | uint8_t alert; |
| 1433 | STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs); |
| 1434 | if (ca_sk == NULL) { |
| 1435 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1436 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1439 | if (CBS_len(&cbs) != 0) { |
| 1440 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1441 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
| 1442 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1443 | return -1; |
| 1444 | } |
| 1445 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1446 | ssl->s3->hs->cert_request = 1; |
| 1447 | sk_X509_NAME_pop_free(ssl->s3->hs->ca_names, X509_NAME_free); |
| 1448 | ssl->s3->hs->ca_names = ca_sk; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1449 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1452 | static int ssl3_get_server_hello_done(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1453 | int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE, |
| 1454 | ssl_hash_message); |
| 1455 | if (ret <= 0) { |
| 1456 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1459 | /* ServerHelloDone is empty. */ |
| 1460 | if (ssl->init_num > 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1461 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1462 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1463 | return -1; |
| 1464 | } |
| 1465 | |
| 1466 | return 1; |
| 1467 | } |
| 1468 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1469 | static int ssl3_send_client_certificate(SSL *ssl) { |
| 1470 | if (ssl->state == SSL3_ST_CW_CERT_A) { |
| 1471 | /* Call cert_cb to update the certificate. */ |
| 1472 | if (ssl->cert->cert_cb) { |
| 1473 | int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1474 | if (ret < 0) { |
| 1475 | ssl->rwstate = SSL_X509_LOOKUP; |
| 1476 | return -1; |
| 1477 | } |
| 1478 | if (ret == 0) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1479 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1480 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1481 | return -1; |
| 1482 | } |
| 1483 | } |
| 1484 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1485 | ssl->state = SSL3_ST_CW_CERT_B; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | if (ssl->state == SSL3_ST_CW_CERT_B) { |
| 1489 | /* Call client_cert_cb to update the certificate. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1490 | int should_retry; |
| 1491 | if (!ssl_do_client_cert_cb(ssl, &should_retry)) { |
| 1492 | if (should_retry) { |
| 1493 | ssl->rwstate = SSL_X509_LOOKUP; |
| 1494 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1495 | return -1; |
| 1496 | } |
| 1497 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1498 | if (!ssl_has_certificate(ssl)) { |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1499 | ssl->s3->hs->cert_request = 0; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1500 | /* Without a client certificate, the handshake buffer may be released. */ |
| 1501 | ssl3_free_handshake_buffer(ssl); |
| 1502 | |
| 1503 | if (ssl->version == SSL3_VERSION) { |
| 1504 | /* In SSL 3.0, send no certificate by skipping both messages. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1505 | ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 1506 | return 1; |
| 1507 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1508 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1509 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1510 | if (!ssl3_output_cert_chain(ssl)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1511 | return -1; |
| 1512 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1513 | ssl->state = SSL3_ST_CW_CERT_C; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1516 | assert(ssl->state == SSL3_ST_CW_CERT_C); |
| 1517 | return ssl->method->write_message(ssl); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1518 | } |
| 1519 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1520 | OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned), |
| 1521 | SIZE_T_IS_SMALLER_THAN_UNSIGNED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1522 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1523 | static int ssl3_send_client_key_exchange(SSL *ssl) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1524 | if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1525 | return ssl->method->write_message(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1526 | } |
| 1527 | assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A); |
| 1528 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1529 | uint8_t *pms = NULL; |
| 1530 | size_t pms_len = 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1531 | CBB cbb, body; |
| 1532 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1533 | SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1534 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1537 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1538 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
| 1539 | |
| 1540 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1541 | unsigned psk_len = 0; |
| 1542 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1543 | if (alg_a & SSL_aPSK) { |
| 1544 | if (ssl->psk_client_callback == NULL) { |
| 1545 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
| 1546 | goto err; |
| 1547 | } |
| 1548 | |
| 1549 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 1550 | memset(identity, 0, sizeof(identity)); |
| 1551 | psk_len = ssl->psk_client_callback( |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1552 | ssl, ssl->s3->hs->peer_psk_identity_hint, identity, sizeof(identity), |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1553 | psk, sizeof(psk)); |
| 1554 | if (psk_len == 0) { |
| 1555 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1556 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1557 | goto err; |
| 1558 | } |
| 1559 | assert(psk_len <= PSK_MAX_PSK_LEN); |
| 1560 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1561 | OPENSSL_free(ssl->s3->new_session->psk_identity); |
| 1562 | ssl->s3->new_session->psk_identity = BUF_strdup(identity); |
| 1563 | if (ssl->s3->new_session->psk_identity == NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1564 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1565 | goto err; |
| 1566 | } |
| 1567 | |
| 1568 | /* Write out psk_identity. */ |
| 1569 | CBB child; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1570 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1571 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1572 | OPENSSL_strnlen(identity, sizeof(identity))) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1573 | !CBB_flush(&body)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1574 | goto err; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1579 | if (alg_k & SSL_kRSA) { |
| 1580 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1581 | pms = OPENSSL_malloc(pms_len); |
| 1582 | if (pms == NULL) { |
| 1583 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1584 | goto err; |
| 1585 | } |
| 1586 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1587 | EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->x509_peer); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1588 | if (pkey == NULL) { |
| 1589 | goto err; |
| 1590 | } |
| 1591 | |
| 1592 | RSA *rsa = EVP_PKEY_get0_RSA(pkey); |
| 1593 | if (rsa == NULL) { |
| 1594 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1595 | EVP_PKEY_free(pkey); |
| 1596 | goto err; |
| 1597 | } |
| 1598 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1599 | EVP_PKEY_free(pkey); |
| 1600 | |
| 1601 | pms[0] = ssl->client_version >> 8; |
| 1602 | pms[1] = ssl->client_version & 0xff; |
| 1603 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1604 | goto err; |
| 1605 | } |
| 1606 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1607 | CBB child, *enc_pms = &body; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1608 | size_t enc_pms_len; |
| 1609 | /* In TLS, there is a length prefix. */ |
| 1610 | if (ssl->version > SSL3_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1611 | if (!CBB_add_u16_length_prefixed(&body, &child)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1612 | goto err; |
| 1613 | } |
| 1614 | enc_pms = &child; |
| 1615 | } |
| 1616 | |
| 1617 | uint8_t *ptr; |
| 1618 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
| 1619 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len, |
| 1620 | RSA_PKCS1_PADDING) || |
| 1621 | /* Log the premaster secret, if logging is enabled. */ |
| 1622 | !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) || |
| 1623 | !CBB_did_write(enc_pms, enc_pms_len) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1624 | !CBB_flush(&body)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1625 | goto err; |
| 1626 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1627 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
| 1628 | /* Generate a keypair and serialize the public half. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1629 | CBB child; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1630 | if (!SSL_ECDH_CTX_add_key(&ssl->s3->hs->ecdh_ctx, &body, &child)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1631 | goto err; |
| 1632 | } |
| 1633 | |
| 1634 | /* Compute the premaster. */ |
| 1635 | uint8_t alert; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1636 | if (!SSL_ECDH_CTX_accept(&ssl->s3->hs->ecdh_ctx, &child, &pms, &pms_len, |
| 1637 | &alert, ssl->s3->hs->peer_key, |
| 1638 | ssl->s3->hs->peer_key_len)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1639 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1640 | goto err; |
| 1641 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1642 | if (!CBB_flush(&body)) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1643 | goto err; |
| 1644 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1645 | |
| 1646 | /* The key exchange state may now be discarded. */ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1647 | SSL_ECDH_CTX_cleanup(&ssl->s3->hs->ecdh_ctx); |
| 1648 | OPENSSL_free(ssl->s3->hs->peer_key); |
| 1649 | ssl->s3->hs->peer_key = NULL; |
| 1650 | ssl->s3->hs->peer_key_len = 0; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1651 | } else if (alg_k & SSL_kPSK) { |
| 1652 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1653 | * the pre-shared key. */ |
| 1654 | pms_len = psk_len; |
| 1655 | pms = OPENSSL_malloc(pms_len); |
| 1656 | if (pms == NULL) { |
| 1657 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1658 | goto err; |
| 1659 | } |
| 1660 | memset(pms, 0, pms_len); |
| 1661 | } else { |
| 1662 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1663 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1664 | goto err; |
| 1665 | } |
| 1666 | |
| 1667 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1668 | * key. */ |
| 1669 | if (alg_a & SSL_aPSK) { |
| 1670 | CBB pms_cbb, child; |
| 1671 | uint8_t *new_pms; |
| 1672 | size_t new_pms_len; |
| 1673 | |
| 1674 | CBB_zero(&pms_cbb); |
| 1675 | if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) || |
| 1676 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1677 | !CBB_add_bytes(&child, pms, pms_len) || |
| 1678 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1679 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1680 | !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) { |
| 1681 | CBB_cleanup(&pms_cbb); |
| 1682 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1683 | goto err; |
| 1684 | } |
| 1685 | OPENSSL_cleanse(pms, pms_len); |
| 1686 | OPENSSL_free(pms); |
| 1687 | pms = new_pms; |
| 1688 | pms_len = new_pms_len; |
| 1689 | } |
| 1690 | |
| 1691 | /* The message must be added to the finished hash before calculating the |
| 1692 | * master secret. */ |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1693 | if (!ssl_complete_message(ssl, &cbb)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1694 | goto err; |
| 1695 | } |
| 1696 | ssl->state = SSL3_ST_CW_KEY_EXCH_B; |
| 1697 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1698 | ssl->s3->new_session->master_key_length = |
| 1699 | tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms, |
| 1700 | pms_len); |
| 1701 | if (ssl->s3->new_session->master_key_length == 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1702 | goto err; |
| 1703 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1704 | ssl->s3->new_session->extended_master_secret = |
| 1705 | ssl->s3->tmp.extended_master_secret; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1706 | OPENSSL_cleanse(pms, pms_len); |
| 1707 | OPENSSL_free(pms); |
| 1708 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1709 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1710 | |
| 1711 | err: |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1712 | CBB_cleanup(&cbb); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1713 | if (pms != NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1714 | OPENSSL_cleanse(pms, pms_len); |
| 1715 | OPENSSL_free(pms); |
| 1716 | } |
| 1717 | return -1; |
| 1718 | } |
| 1719 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1720 | static int ssl3_send_cert_verify(SSL *ssl) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1721 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1722 | return ssl->method->write_message(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | assert(ssl_has_private_key(ssl)); |
| 1726 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1727 | CBB cbb, body, child; |
| 1728 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1729 | SSL3_MT_CERTIFICATE_VERIFY)) { |
| 1730 | goto err; |
| 1731 | } |
| 1732 | |
| 1733 | uint16_t signature_algorithm; |
| 1734 | if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) { |
| 1735 | goto err; |
| 1736 | } |
| 1737 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1738 | /* Write out the digest type in TLS 1.2. */ |
| 1739 | if (!CBB_add_u16(&body, signature_algorithm)) { |
| 1740 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1741 | goto err; |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | /* Set aside space for the signature. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1746 | 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] | 1747 | uint8_t *ptr; |
| 1748 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 1749 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1750 | goto err; |
| 1751 | } |
| 1752 | |
| 1753 | size_t sig_len = max_sig_len; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1754 | enum ssl_private_key_result_t sign_result; |
| 1755 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1756 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1757 | * single final digest and signature, and must be special-cased. */ |
| 1758 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
| 1759 | if (ssl->cert->key_method != NULL) { |
| 1760 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1761 | goto err; |
| 1762 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1763 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1764 | const EVP_MD *md; |
| 1765 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1766 | size_t digest_len; |
| 1767 | if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len, |
| 1768 | signature_algorithm)) { |
| 1769 | goto err; |
| 1770 | } |
| 1771 | |
| 1772 | sign_result = ssl_private_key_success; |
| 1773 | |
| 1774 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL); |
| 1775 | if (pctx == NULL || |
| 1776 | !EVP_PKEY_sign_init(pctx) || |
| 1777 | !EVP_PKEY_CTX_set_signature_md(pctx, md) || |
| 1778 | !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) { |
| 1779 | EVP_PKEY_CTX_free(pctx); |
| 1780 | sign_result = ssl_private_key_failure; |
| 1781 | goto err; |
| 1782 | } |
| 1783 | EVP_PKEY_CTX_free(pctx); |
| 1784 | } else { |
| 1785 | sign_result = ssl_private_key_sign( |
| 1786 | ssl, ptr, &sig_len, max_sig_len, signature_algorithm, |
| 1787 | (const uint8_t *)ssl->s3->handshake_buffer->data, |
| 1788 | ssl->s3->handshake_buffer->length); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | /* The handshake buffer is no longer necessary. */ |
| 1792 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1793 | } else { |
| 1794 | assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1795 | sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | switch (sign_result) { |
| 1799 | case ssl_private_key_success: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1800 | break; |
| 1801 | case ssl_private_key_failure: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1802 | goto err; |
| 1803 | case ssl_private_key_retry: |
| 1804 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1805 | ssl->state = SSL3_ST_CW_CERT_VRFY_B; |
| 1806 | goto err; |
| 1807 | } |
| 1808 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1809 | if (!CBB_did_write(&child, sig_len) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1810 | !ssl_complete_message(ssl, &cbb)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1811 | goto err; |
| 1812 | } |
| 1813 | |
| 1814 | ssl->state = SSL3_ST_CW_CERT_VRFY_C; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1815 | return ssl->method->write_message(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1816 | |
| 1817 | err: |
| 1818 | CBB_cleanup(&cbb); |
| 1819 | return -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1822 | static int ssl3_send_next_proto(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1823 | if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1824 | return ssl->method->write_message(ssl); |
Adam Langley | fdeb488 | 2015-10-30 13:15:30 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1827 | assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A); |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1828 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1829 | static const uint8_t kZero[32] = {0}; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1830 | size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32); |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1831 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1832 | CBB cbb, body, child; |
| 1833 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) || |
| 1834 | !CBB_add_u8_length_prefixed(&body, &child) || |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1835 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1836 | ssl->s3->next_proto_negotiated_len) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1837 | !CBB_add_u8_length_prefixed(&body, &child) || |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1838 | !CBB_add_bytes(&child, kZero, padding_len) || |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1839 | !ssl_complete_message(ssl, &cbb)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1840 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1841 | CBB_cleanup(&cbb); |
Adam Langley | fdeb488 | 2015-10-30 13:15:30 -0700 | [diff] [blame] | 1842 | return -1; |
| 1843 | } |
Adam Langley | fdeb488 | 2015-10-30 13:15:30 -0700 | [diff] [blame] | 1844 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1845 | ssl->state = SSL3_ST_CW_NEXT_PROTO_B; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1846 | return ssl->method->write_message(ssl); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1847 | } |
| 1848 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1849 | static int ssl3_send_channel_id(SSL *ssl) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1850 | if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1851 | return ssl->method->write_message(ssl); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A); |
| 1855 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1856 | if (!ssl_do_channel_id_callback(ssl)) { |
| 1857 | return -1; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1861 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
| 1862 | return -1; |
| 1863 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1864 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1865 | CBB cbb, body; |
| 1866 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) || |
| 1867 | !tls1_write_channel_id(ssl, &body) || |
| 1868 | !ssl_complete_message(ssl, &cbb)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1869 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1870 | CBB_cleanup(&cbb); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1871 | return -1; |
Kenny Root | a04d78d | 2015-09-25 00:26:37 +0000 | [diff] [blame] | 1872 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1873 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1874 | ssl->state = SSL3_ST_CW_CHANNEL_ID_B; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1875 | return ssl->method->write_message(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1876 | } |
| 1877 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1878 | static int ssl3_get_new_session_ticket(SSL *ssl) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1879 | int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET, |
| 1880 | ssl_hash_message); |
| 1881 | if (ret <= 0) { |
| 1882 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1883 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1884 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1885 | CBS new_session_ticket, ticket; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1886 | uint32_t tlsext_tick_lifetime_hint; |
| 1887 | CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num); |
| 1888 | if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) || |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1889 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1890 | CBS_len(&new_session_ticket) != 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1891 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1892 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1893 | return -1; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1896 | if (CBS_len(&ticket) == 0) { |
| 1897 | /* RFC 5077 allows a server to change its mind and send no ticket after |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1898 | * negotiating the extension. The value of |ticket_expected| is checked in |
| 1899 | * |ssl_update_cache| so is cleared here to avoid an unnecessary update. */ |
| 1900 | ssl->s3->hs->ticket_expected = 0; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1901 | return 1; |
| 1902 | } |
| 1903 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1904 | int session_renewed = ssl->session != NULL; |
| 1905 | SSL_SESSION *session = ssl->s3->new_session; |
| 1906 | if (session_renewed) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1907 | /* The server is sending a new ticket for an existing session. Sessions are |
| 1908 | * immutable once established, so duplicate all but the ticket of the |
| 1909 | * existing session. */ |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1910 | session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1911 | if (session == NULL) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1912 | /* This should never happen. */ |
| 1913 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1914 | goto err; |
| 1915 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1916 | } |
| 1917 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame^] | 1918 | /* |tlsext_tick_lifetime_hint| is measured from when the ticket was issued. */ |
| 1919 | ssl_session_refresh_time(ssl, session); |
| 1920 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1921 | if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1922 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1923 | goto err; |
| 1924 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1925 | session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1926 | |
| 1927 | /* Generate a session ID for this session based on the session ticket. We use |
| 1928 | * the session ID mechanism for detecting ticket resumption. This also fits in |
| 1929 | * with assumptions elsewhere in OpenSSL.*/ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1930 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), |
| 1931 | session->session_id, &session->session_id_length, |
| 1932 | EVP_sha256(), NULL)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1933 | goto err; |
| 1934 | } |
| 1935 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1936 | if (session_renewed) { |
| 1937 | session->not_resumable = 0; |
| 1938 | SSL_SESSION_free(ssl->session); |
| 1939 | ssl->session = session; |
| 1940 | } |
| 1941 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1942 | return 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1943 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1944 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1945 | if (session_renewed) { |
| 1946 | SSL_SESSION_free(session); |
| 1947 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1948 | return -1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1949 | } |