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