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