Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. |
| 148 | */ |
| 149 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 150 | #include <openssl/ssl.h> |
| 151 | |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame] | 152 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 153 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 155 | #include <openssl/bn.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 157 | #include <openssl/bytestring.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 158 | #include <openssl/dh.h> |
| 159 | #include <openssl/ec_key.h> |
| 160 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 161 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | #include <openssl/evp.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | #include <openssl/md5.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 164 | #include <openssl/mem.h> |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 165 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | #include <openssl/x509.h> |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 167 | #include <openssl/x509v3.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 169 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | #include "../crypto/dh/internal.h" |
| 171 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 173 | int ssl3_connect(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 174 | BUF_MEM *buf = NULL; |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 175 | void (*cb)(const SSL *ssl, int type, int value) = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 176 | int ret = -1; |
| 177 | int new_state, state, skip = 0; |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 178 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 179 | assert(ssl->handshake_func == ssl3_connect); |
| 180 | assert(!ssl->server); |
| 181 | assert(!SSL_IS_DTLS(ssl)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 183 | ERR_clear_system_error(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 185 | if (ssl->info_callback != NULL) { |
| 186 | cb = ssl->info_callback; |
| 187 | } else if (ssl->ctx->info_callback != NULL) { |
| 188 | cb = ssl->ctx->info_callback; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 189 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 191 | for (;;) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 192 | state = ssl->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 194 | switch (ssl->state) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 195 | case SSL_ST_CONNECT: |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 196 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 197 | cb(ssl, SSL_CB_HANDSHAKE_START, 1); |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 200 | if (ssl->init_buf == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 201 | buf = BUF_MEM_new(); |
| 202 | if (buf == NULL || |
| 203 | !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 204 | ret = -1; |
| 205 | goto end; |
| 206 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 208 | ssl->init_buf = buf; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 209 | buf = NULL; |
| 210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 212 | if (!ssl_init_wbio_buffer(ssl)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 213 | ret = -1; |
| 214 | goto end; |
| 215 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 217 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 218 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 219 | ret = -1; |
| 220 | goto end; |
| 221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 223 | ssl->state = SSL3_ST_CW_CLNT_HELLO_A; |
| 224 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 225 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 227 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 228 | case SSL3_ST_CW_CLNT_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 229 | ret = ssl3_send_client_hello(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 230 | if (ret <= 0) { |
| 231 | goto end; |
| 232 | } |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 233 | ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A; |
| 234 | ssl->state = SSL3_ST_CW_FLUSH; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 235 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 236 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 237 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 238 | case SSL3_ST_CR_SRVR_HELLO_A: |
| 239 | case SSL3_ST_CR_SRVR_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 240 | ret = ssl3_get_server_hello(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 241 | if (ret <= 0) { |
| 242 | goto end; |
| 243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 245 | if (ssl->hit) { |
| 246 | ssl->state = SSL3_ST_CR_CHANGE; |
| 247 | if (ssl->tlsext_ticket_expected) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 248 | /* receive renewed session ticket */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 249 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 250 | } |
| 251 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 252 | ssl->state = SSL3_ST_CR_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 253 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 254 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 255 | break; |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 256 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 257 | case SSL3_ST_CR_CERT_A: |
| 258 | case SSL3_ST_CR_CERT_B: |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame^] | 259 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 260 | ret = ssl3_get_server_certificate(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 261 | if (ret <= 0) { |
| 262 | goto end; |
| 263 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 264 | if (ssl->s3->tmp.certificate_status_expected) { |
| 265 | ssl->state = SSL3_ST_CR_CERT_STATUS_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 266 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 267 | ssl->state = SSL3_ST_VERIFY_SERVER_CERT; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 268 | } |
| 269 | } else { |
| 270 | skip = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 271 | ssl->state = SSL3_ST_CR_KEY_EXCH_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 272 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 273 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 274 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 275 | |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 276 | case SSL3_ST_VERIFY_SERVER_CERT: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 277 | ret = ssl3_verify_server_cert(ssl); |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 278 | if (ret <= 0) { |
| 279 | goto end; |
| 280 | } |
| 281 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 282 | ssl->state = SSL3_ST_CR_KEY_EXCH_A; |
| 283 | ssl->init_num = 0; |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 284 | break; |
| 285 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 286 | case SSL3_ST_CR_KEY_EXCH_A: |
| 287 | case SSL3_ST_CR_KEY_EXCH_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 288 | ret = ssl3_get_server_key_exchange(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 289 | if (ret <= 0) { |
| 290 | goto end; |
| 291 | } |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame^] | 292 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
| 293 | ssl->state = SSL3_ST_CR_CERT_REQ_A; |
| 294 | } else { |
| 295 | ssl->state = SSL3_ST_CR_SRVR_DONE_A; |
| 296 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 297 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 298 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 300 | case SSL3_ST_CR_CERT_REQ_A: |
| 301 | case SSL3_ST_CR_CERT_REQ_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 302 | ret = ssl3_get_certificate_request(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 303 | if (ret <= 0) { |
| 304 | goto end; |
| 305 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 306 | ssl->state = SSL3_ST_CR_SRVR_DONE_A; |
| 307 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 308 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 310 | case SSL3_ST_CR_SRVR_DONE_A: |
| 311 | case SSL3_ST_CR_SRVR_DONE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 312 | ret = ssl3_get_server_done(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 313 | if (ret <= 0) { |
| 314 | goto end; |
| 315 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 316 | if (ssl->s3->tmp.cert_req) { |
| 317 | ssl->state = SSL3_ST_CW_CERT_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 318 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 319 | ssl->state = SSL3_ST_CW_KEY_EXCH_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 320 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 321 | ssl->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 323 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 325 | case SSL3_ST_CW_CERT_A: |
| 326 | case SSL3_ST_CW_CERT_B: |
| 327 | case SSL3_ST_CW_CERT_C: |
| 328 | case SSL3_ST_CW_CERT_D: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 329 | ret = ssl3_send_client_certificate(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 330 | if (ret <= 0) { |
| 331 | goto end; |
| 332 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 333 | ssl->state = SSL3_ST_CW_KEY_EXCH_A; |
| 334 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 335 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 337 | case SSL3_ST_CW_KEY_EXCH_A: |
| 338 | case SSL3_ST_CW_KEY_EXCH_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 339 | ret = ssl3_send_client_key_exchange(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 340 | if (ret <= 0) { |
| 341 | goto end; |
| 342 | } |
| 343 | /* For TLS, cert_req is set to 2, so a cert chain |
| 344 | * of nothing is sent, but no verify packet is sent */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 345 | if (ssl->s3->tmp.cert_req == 1) { |
| 346 | ssl->state = SSL3_ST_CW_CERT_VRFY_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 347 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 348 | ssl->state = SSL3_ST_CW_CHANGE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 351 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 352 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 354 | case SSL3_ST_CW_CERT_VRFY_A: |
| 355 | case SSL3_ST_CW_CERT_VRFY_B: |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 356 | case SSL3_ST_CW_CERT_VRFY_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 357 | ret = ssl3_send_cert_verify(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 358 | if (ret <= 0) { |
| 359 | goto end; |
| 360 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 361 | ssl->state = SSL3_ST_CW_CHANGE_A; |
| 362 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 363 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 365 | case SSL3_ST_CW_CHANGE_A: |
| 366 | case SSL3_ST_CW_CHANGE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 367 | ret = ssl3_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A, |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 368 | SSL3_ST_CW_CHANGE_B); |
| 369 | if (ret <= 0) { |
| 370 | goto end; |
| 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 373 | ssl->state = SSL3_ST_CW_FINISHED_A; |
| 374 | if (ssl->s3->tlsext_channel_id_valid) { |
| 375 | ssl->state = SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 376 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 377 | if (ssl->s3->next_proto_neg_seen) { |
| 378 | ssl->state = SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 379 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 380 | ssl->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 382 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 383 | ret = -1; |
| 384 | goto end; |
| 385 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 387 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 389 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 390 | case SSL3_ST_CW_NEXT_PROTO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 391 | ret = ssl3_send_next_proto(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 392 | if (ret <= 0) { |
| 393 | goto end; |
| 394 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 395 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 396 | if (ssl->s3->tlsext_channel_id_valid) { |
| 397 | ssl->state = SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 398 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 399 | ssl->state = SSL3_ST_CW_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 400 | } |
| 401 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 403 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 404 | case SSL3_ST_CW_CHANNEL_ID_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 405 | ret = ssl3_send_channel_id(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 406 | if (ret <= 0) { |
| 407 | goto end; |
| 408 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 409 | ssl->state = SSL3_ST_CW_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 410 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 411 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 412 | case SSL3_ST_CW_FINISHED_A: |
| 413 | case SSL3_ST_CW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 414 | ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 415 | SSL3_ST_CW_FINISHED_B); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 416 | if (ret <= 0) { |
| 417 | goto end; |
| 418 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 419 | ssl->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 420 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 421 | if (ssl->hit) { |
| 422 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 423 | } else { |
| 424 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 425 | * record the handshake hashes at this point in the session so that |
| 426 | * any resumption of this session with ChannelID can sign those |
| 427 | * hashes. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 428 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 429 | if (ret <= 0) { |
| 430 | goto end; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 431 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 432 | if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) && |
| 433 | ssl3_can_false_start(ssl) && |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 434 | /* No False Start on renegotiation (would complicate the state |
| 435 | * machine). */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 436 | !ssl->s3->initial_handshake_complete) { |
| 437 | ssl->s3->tmp.next_state = SSL3_ST_FALSE_START; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 438 | } else { |
| 439 | /* Allow NewSessionTicket if ticket expected */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 440 | if (ssl->tlsext_ticket_expected) { |
| 441 | ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 442 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 443 | ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 447 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 448 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 450 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 451 | case SSL3_ST_CR_SESSION_TICKET_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 452 | ret = ssl3_get_new_session_ticket(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 453 | if (ret <= 0) { |
| 454 | goto end; |
| 455 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 456 | ssl->state = SSL3_ST_CR_CHANGE; |
| 457 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 458 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 460 | case SSL3_ST_CR_CERT_STATUS_A: |
| 461 | case SSL3_ST_CR_CERT_STATUS_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 462 | ret = ssl3_get_cert_status(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 463 | if (ret <= 0) { |
| 464 | goto end; |
| 465 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 466 | ssl->state = SSL3_ST_VERIFY_SERVER_CERT; |
| 467 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 468 | break; |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 469 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 470 | case SSL3_ST_CR_CHANGE: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 471 | ret = ssl->method->ssl_read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 472 | if (ret <= 0) { |
| 473 | goto end; |
| 474 | } |
| 475 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 476 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 477 | ret = -1; |
| 478 | goto end; |
| 479 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 480 | ssl->state = SSL3_ST_CR_FINISHED_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 481 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 483 | case SSL3_ST_CR_FINISHED_A: |
| 484 | case SSL3_ST_CR_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 485 | ret = ssl3_get_finished(ssl, SSL3_ST_CR_FINISHED_A, |
| 486 | SSL3_ST_CR_FINISHED_B); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 487 | if (ret <= 0) { |
| 488 | goto end; |
| 489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 491 | if (ssl->hit) { |
| 492 | ssl->state = SSL3_ST_CW_CHANGE_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 493 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 494 | ssl->state = SSL_ST_OK; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 495 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 496 | ssl->init_num = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 497 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 499 | case SSL3_ST_CW_FLUSH: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 500 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 501 | ssl->rwstate = SSL_WRITING; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 502 | ret = -1; |
| 503 | goto end; |
| 504 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 505 | ssl->state = ssl->s3->tmp.next_state; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 506 | break; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 508 | case SSL3_ST_FALSE_START: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 509 | /* Allow NewSessionTicket if ticket expected */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 510 | if (ssl->tlsext_ticket_expected) { |
| 511 | ssl->state = SSL3_ST_CR_SESSION_TICKET_A; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 512 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 513 | ssl->state = SSL3_ST_CR_CHANGE; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 514 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 515 | ssl->s3->tmp.in_false_start = 1; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 517 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 518 | ret = 1; |
| 519 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 520 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 521 | case SSL_ST_OK: |
| 522 | /* clean a few things up */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 523 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 524 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 525 | BUF_MEM_free(ssl->init_buf); |
| 526 | ssl->init_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 527 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 528 | /* Remove write buffering now. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 529 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 531 | const int is_initial_handshake = !ssl->s3->initial_handshake_complete; |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 532 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 533 | ssl->init_num = 0; |
| 534 | ssl->s3->tmp.in_false_start = 0; |
| 535 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 537 | if (is_initial_handshake) { |
| 538 | /* Renegotiations do not participate in session resumption. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 539 | ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT); |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 540 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 541 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 542 | ret = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 543 | /* ssl->server=0; */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 545 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 546 | cb(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 547 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 548 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 549 | goto end; |
| 550 | |
| 551 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 552 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 553 | ret = -1; |
| 554 | goto end; |
| 555 | } |
| 556 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 557 | if (!ssl->s3->tmp.reuse_message && !skip) { |
| 558 | if (cb != NULL && ssl->state != state) { |
| 559 | new_state = ssl->state; |
| 560 | ssl->state = state; |
| 561 | cb(ssl, SSL_CB_CONNECT_LOOP, 1); |
| 562 | ssl->state = new_state; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | skip = 0; |
| 566 | } |
| 567 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 568 | end: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 569 | BUF_MEM_free(buf); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 570 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 571 | cb(ssl, SSL_CB_CONNECT_EXIT, ret); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 572 | } |
| 573 | return ret; |
| 574 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 575 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 576 | static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) { |
| 577 | /* Prepare disabled cipher masks. */ |
| 578 | ssl_set_client_disabled(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 579 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 580 | CBB child; |
| 581 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 582 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 583 | } |
| 584 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 585 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
| 586 | |
| 587 | int any_enabled = 0; |
| 588 | size_t i; |
| 589 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 590 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 591 | /* Skip disabled ciphers */ |
David Benjamin | c7817d8 | 2015-11-05 18:28:33 -0500 | [diff] [blame] | 592 | if ((cipher->algorithm_mkey & ssl->cert->mask_k) || |
| 593 | (cipher->algorithm_auth & ssl->cert->mask_a)) { |
| 594 | continue; |
| 595 | } |
| 596 | if (SSL_CIPHER_get_min_version(cipher) > |
| 597 | ssl3_version_from_wire(ssl, ssl->client_version)) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 598 | continue; |
| 599 | } |
| 600 | any_enabled = 1; |
| 601 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 602 | return 0; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /* If all ciphers were disabled, return the error to the caller. */ |
| 607 | if (!any_enabled) { |
| 608 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 613 | * added. */ |
| 614 | if (ssl->client_version == SSL3_VERSION && |
| 615 | !ssl->s3->initial_handshake_complete) { |
| 616 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 617 | return 0; |
| 618 | } |
| 619 | /* The renegotiation extension is required to be at index zero. */ |
| 620 | ssl->s3->tmp.extensions.sent |= (1u << 0); |
| 621 | } |
| 622 | |
| 623 | if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) && |
| 624 | !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | return CBB_flush(out); |
| 629 | } |
| 630 | |
| 631 | int ssl3_send_client_hello(SSL *ssl) { |
| 632 | if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) { |
| 633 | return ssl_do_write(ssl); |
| 634 | } |
| 635 | |
David Benjamin | 13e81fc | 2015-11-02 17:16:13 -0500 | [diff] [blame] | 636 | /* In DTLS, reset the handshake buffer each time a new ClientHello is |
| 637 | * assembled. We may send multiple if we receive HelloVerifyRequest. */ |
| 638 | if (SSL_IS_DTLS(ssl) && !ssl3_init_handshake_buffer(ssl)) { |
| 639 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 640 | return -1; |
| 641 | } |
| 642 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 643 | CBB cbb; |
| 644 | CBB_zero(&cbb); |
| 645 | |
| 646 | assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A); |
| 647 | if (!ssl->s3->have_version) { |
| 648 | uint16_t max_version = ssl3_get_max_client_version(ssl); |
| 649 | /* Disabling all versions is silly: return an error. */ |
| 650 | if (max_version == 0) { |
| 651 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 652 | goto err; |
| 653 | } |
| 654 | |
| 655 | ssl->version = max_version; |
| 656 | /* Only set |ssl->client_version| on the initial handshake. Renegotiations, |
| 657 | * although locked to a version, reuse the value. When using the plain RSA |
| 658 | * key exchange, the ClientHello version is checked in the premaster secret. |
| 659 | * Some servers fail when this value changes. */ |
| 660 | ssl->client_version = max_version; |
| 661 | } |
| 662 | |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 663 | /* If the configured session has expired or was created at a disabled |
| 664 | * version, drop it. */ |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 665 | if (ssl->session != NULL && |
| 666 | (ssl->session->session_id_length == 0 || ssl->session->not_resumable || |
David Benjamin | 1269ddd | 2015-10-18 15:18:55 -0400 | [diff] [blame] | 667 | ssl->session->timeout < (long)(time(NULL) - ssl->session->time) || |
David Benjamin | e29ea16 | 2016-03-23 16:10:44 -0400 | [diff] [blame] | 668 | !ssl3_is_version_enabled(ssl, ssl->session->ssl_version))) { |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 669 | SSL_set_session(ssl, NULL); |
| 670 | } |
| 671 | |
| 672 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 673 | * renegerate the client_random. The random must be reused. */ |
| 674 | if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) && |
| 675 | !ssl_fill_hello_random(ssl->s3->client_random, |
| 676 | sizeof(ssl->s3->client_random), 0 /* client */)) { |
| 677 | goto err; |
| 678 | } |
| 679 | |
| 680 | /* Renegotiations do not participate in session resumption. */ |
| 681 | int has_session = ssl->session != NULL && |
| 682 | !ssl->s3->initial_handshake_complete; |
| 683 | |
| 684 | CBB child; |
| 685 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 686 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 687 | !CBB_add_u16(&cbb, ssl->client_version) || |
| 688 | !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 689 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 690 | (has_session && |
| 691 | !CBB_add_bytes(&child, ssl->session->session_id, |
| 692 | ssl->session->session_id_length))) { |
| 693 | goto err; |
| 694 | } |
| 695 | |
| 696 | if (SSL_IS_DTLS(ssl)) { |
| 697 | if (!CBB_add_u8_length_prefixed(&cbb, &child) || |
| 698 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
| 699 | goto err; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | size_t length; |
| 704 | if (!ssl3_write_client_cipher_list(ssl, &cbb) || |
| 705 | !CBB_add_u8(&cbb, 1 /* one compression method */) || |
| 706 | !CBB_add_u8(&cbb, 0 /* null compression */) || |
| 707 | !ssl_add_clienthello_tlsext(ssl, &cbb, |
| 708 | CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) || |
| 709 | !CBB_finish(&cbb, NULL, &length) || |
| 710 | !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) { |
| 711 | goto err; |
| 712 | } |
| 713 | |
| 714 | ssl->state = SSL3_ST_CW_CLNT_HELLO_B; |
| 715 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 716 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 717 | err: |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 718 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 719 | return -1; |
| 720 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 721 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 722 | int ssl3_get_server_hello(SSL *ssl) { |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 723 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 724 | const SSL_CIPHER *c; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 725 | CERT *ct = ssl->cert; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 726 | int al = SSL_AD_INTERNAL_ERROR, ok; |
| 727 | long n; |
| 728 | CBS server_hello, server_random, session_id; |
| 729 | uint16_t server_version, cipher_suite; |
| 730 | uint8_t compression_method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 732 | n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_HELLO_A, |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 733 | SSL3_ST_CR_SRVR_HELLO_B, |
| 734 | SSL3_MT_SERVER_HELLO, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 735 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 736 | if (!ok) { |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 737 | uint32_t err = ERR_peek_error(); |
| 738 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 739 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 740 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 741 | * in response to ClientHello. This matches NSS's client behavior and |
| 742 | * gives a better error on a (probable) failure to negotiate initial |
| 743 | * parameters. Note: this error code comes after the original one. |
| 744 | * |
| 745 | * See https://crbug.com/446505. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 746 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 747 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 748 | return n; |
| 749 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 750 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 751 | CBS_init(&server_hello, ssl->init_msg, n); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 752 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 753 | if (!CBS_get_u16(&server_hello, &server_version) || |
| 754 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
| 755 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 756 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 757 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 758 | !CBS_get_u8(&server_hello, &compression_method)) { |
| 759 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 760 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 761 | goto f_err; |
| 762 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 763 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 764 | assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete); |
| 765 | if (!ssl->s3->have_version) { |
| 766 | if (!ssl3_is_version_enabled(ssl, server_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 767 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 768 | ssl->version = server_version; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 769 | /* Mark the version as fixed so the record-layer version is not clamped |
| 770 | * to TLS 1.0. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 771 | ssl->s3->have_version = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 772 | al = SSL_AD_PROTOCOL_VERSION; |
| 773 | goto f_err; |
| 774 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 775 | ssl->version = server_version; |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 776 | ssl->s3->enc_method = ssl3_get_enc_method(server_version); |
| 777 | assert(ssl->s3->enc_method != NULL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 778 | /* At this point, the connection's version is known and ssl->version is |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 779 | * fixed. Begin enforcing the record-layer version. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 780 | ssl->s3->have_version = 1; |
| 781 | } else if (server_version != ssl->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 782 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 783 | al = SSL_AD_PROTOCOL_VERSION; |
| 784 | goto f_err; |
| 785 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 786 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 787 | /* Copy over the server random. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 788 | memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 789 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 790 | assert(ssl->session == NULL || ssl->session->session_id_length > 0); |
| 791 | if (!ssl->s3->initial_handshake_complete && ssl->session != NULL && |
| 792 | CBS_mem_equal(&session_id, ssl->session->session_id, |
| 793 | ssl->session->session_id_length)) { |
| 794 | if (ssl->sid_ctx_length != ssl->session->sid_ctx_length || |
| 795 | memcmp(ssl->session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 796 | /* actually a client application bug */ |
| 797 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 798 | OPENSSL_PUT_ERROR(SSL, |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 799 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 800 | goto f_err; |
| 801 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 802 | ssl->hit = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 803 | } else { |
| 804 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 805 | * fill out. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 806 | ssl->hit = 0; |
| 807 | if (!ssl_get_new_session(ssl, 0 /* client */)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 808 | goto f_err; |
| 809 | } |
| 810 | /* Note: session_id could be empty. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 811 | ssl->session->session_id_length = CBS_len(&session_id); |
| 812 | memcpy(ssl->session->session_id, CBS_data(&session_id), |
| 813 | CBS_len(&session_id)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 814 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 815 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 816 | c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 817 | if (c == NULL) { |
| 818 | /* unknown cipher */ |
| 819 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 820 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 821 | goto f_err; |
| 822 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 823 | /* If the cipher is disabled then we didn't sent it in the ClientHello, so if |
| 824 | * the server selected it, it's an error. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 825 | if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) || |
David Benjamin | b9e4fa5 | 2015-12-29 23:58:17 -0500 | [diff] [blame] | 826 | SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 827 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 828 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 829 | goto f_err; |
| 830 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 831 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 832 | sk = ssl_get_ciphers_by_id(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 833 | if (!sk_SSL_CIPHER_find(sk, NULL, c)) { |
| 834 | /* we did not say we would use this cipher */ |
| 835 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 836 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 837 | goto f_err; |
| 838 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 839 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 840 | if (ssl->hit) { |
| 841 | if (ssl->session->cipher != c) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 842 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 843 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 844 | goto f_err; |
| 845 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 846 | if (ssl->session->ssl_version != ssl->version) { |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 847 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 848 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 849 | goto f_err; |
| 850 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 851 | } else { |
| 852 | ssl->session->cipher = c; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 853 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 854 | ssl->s3->tmp.new_cipher = c; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 855 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 856 | /* Now that the cipher is known, initialize the handshake hash. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 857 | if (!ssl3_init_handshake_hash(ssl)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 858 | goto f_err; |
| 859 | } |
| 860 | |
David Benjamin | 5f04b65 | 2015-05-26 17:30:10 -0400 | [diff] [blame] | 861 | /* If doing a full handshake with TLS 1.2, the server may request a client |
| 862 | * certificate which requires hashing the handshake transcript under a |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 863 | * different hash. Otherwise, the handshake buffer may be released. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame^] | 864 | if (ssl->hit || ssl3_protocol_version(ssl) < TLS1_2_VERSION || |
| 865 | !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 866 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 867 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 868 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 869 | /* Only the NULL compression algorithm is supported. */ |
| 870 | if (compression_method != 0) { |
| 871 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 872 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 873 | goto f_err; |
| 874 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 875 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 876 | /* TLS extensions */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 877 | if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 878 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 879 | goto err; |
| 880 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 881 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 882 | /* There should be nothing left over in the record. */ |
| 883 | if (CBS_len(&server_hello) != 0) { |
| 884 | /* wrong packet length */ |
| 885 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 886 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 887 | goto f_err; |
| 888 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 889 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 890 | if (ssl->hit && |
| 891 | ssl->s3->tmp.extended_master_secret != |
| 892 | ssl->session->extended_master_secret) { |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 893 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 894 | if (ssl->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 895 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 896 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 897 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 898 | } |
| 899 | goto f_err; |
| 900 | } |
| 901 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 902 | return 1; |
| 903 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 904 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 905 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 906 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 907 | return -1; |
| 908 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 909 | |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 910 | /* ssl3_check_leaf_certificate returns one if |leaf| is a suitable leaf server |
| 911 | * certificate for |ssl|. Otherwise, it returns zero and pushes an error on the |
| 912 | * error queue. */ |
| 913 | static int ssl3_check_leaf_certificate(SSL *ssl, X509 *leaf) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 914 | int ret = 0; |
| 915 | EVP_PKEY *pkey = X509_get_pubkey(leaf); |
David Benjamin | d1c1c8e | 2015-06-08 18:36:21 -0400 | [diff] [blame] | 916 | if (pkey == NULL) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 917 | goto err; |
| 918 | } |
| 919 | |
| 920 | /* Check the certificate's type matches the cipher. */ |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 921 | const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 922 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 923 | assert(expected_type != EVP_PKEY_NONE); |
| 924 | if (pkey->type != expected_type) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 925 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 926 | goto err; |
| 927 | } |
| 928 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 929 | if (cipher->algorithm_auth & SSL_aECDSA) { |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 930 | /* TODO(davidben): This behavior is preserved from upstream. Should key |
| 931 | * usages be checked in other cases as well? */ |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 932 | /* This call populates the ex_flags field correctly */ |
| 933 | X509_check_purpose(leaf, -1, 0); |
| 934 | if ((leaf->ex_flags & EXFLAG_KUSAGE) && |
| 935 | !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 936 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 937 | goto err; |
| 938 | } |
David Benjamin | 99fdfb9 | 2015-11-02 12:11:35 -0500 | [diff] [blame] | 939 | |
| 940 | if (!tls1_check_ec_cert(ssl, leaf)) { |
| 941 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT); |
| 942 | goto err; |
| 943 | } |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | ret = 1; |
| 947 | |
| 948 | err: |
| 949 | EVP_PKEY_free(pkey); |
| 950 | return ret; |
| 951 | } |
| 952 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 953 | int ssl3_get_server_certificate(SSL *ssl) { |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 954 | int al, ok, ret = -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 955 | unsigned long n; |
| 956 | X509 *x = NULL; |
| 957 | STACK_OF(X509) *sk = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 958 | EVP_PKEY *pkey = NULL; |
| 959 | CBS cbs, certificate_list; |
| 960 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 961 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 962 | n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B, |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 963 | SSL3_MT_CERTIFICATE, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 964 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 965 | if (!ok) { |
| 966 | return n; |
| 967 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 968 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 969 | CBS_init(&cbs, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 970 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 971 | sk = sk_X509_new_null(); |
| 972 | if (sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 973 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 974 | goto err; |
| 975 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 976 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 977 | if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) || |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 978 | CBS_len(&certificate_list) == 0 || |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 979 | CBS_len(&cbs) != 0) { |
| 980 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 981 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 982 | goto f_err; |
| 983 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 984 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 985 | while (CBS_len(&certificate_list) > 0) { |
| 986 | CBS certificate; |
| 987 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 988 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 989 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 990 | goto f_err; |
| 991 | } |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 992 | /* A u24 length cannot overflow a long. */ |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 993 | data = CBS_data(&certificate); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 994 | x = d2i_X509(NULL, &data, (long)CBS_len(&certificate)); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 995 | if (x == NULL) { |
| 996 | al = SSL_AD_BAD_CERTIFICATE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 997 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 998 | goto f_err; |
| 999 | } |
| 1000 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 1001 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1002 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1003 | goto f_err; |
| 1004 | } |
| 1005 | if (!sk_X509_push(sk, x)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1006 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1007 | goto err; |
| 1008 | } |
| 1009 | x = NULL; |
| 1010 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1011 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1012 | X509 *leaf = sk_X509_value(sk, 0); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1013 | if (!ssl3_check_leaf_certificate(ssl, leaf)) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1014 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1015 | goto f_err; |
| 1016 | } |
| 1017 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1018 | /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes |
| 1019 | * the leaf. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1020 | sk_X509_pop_free(ssl->session->cert_chain, X509_free); |
| 1021 | ssl->session->cert_chain = sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1022 | sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1023 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1024 | X509_free(ssl->session->peer); |
| 1025 | ssl->session->peer = X509_up_ref(leaf); |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1026 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1027 | ssl->session->verify_result = ssl->verify_result; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1028 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1029 | ret = 1; |
| 1030 | |
| 1031 | if (0) { |
| 1032 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1033 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | err: |
| 1037 | EVP_PKEY_free(pkey); |
| 1038 | X509_free(x); |
| 1039 | sk_X509_pop_free(sk, X509_free); |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1043 | int ssl3_get_server_key_exchange(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1044 | EVP_MD_CTX md_ctx; |
| 1045 | int al, ok; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1046 | EVP_PKEY *pkey = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1047 | DH *dh = NULL; |
| 1048 | EC_KEY *ecdh = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1049 | EC_POINT *srvr_ecpoint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1050 | |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1051 | long n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_KEY_EXCH_A, |
| 1052 | SSL3_ST_CR_KEY_EXCH_B, -1, |
| 1053 | ssl_hash_message, &ok); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1054 | if (!ok) { |
| 1055 | return n; |
| 1056 | } |
| 1057 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1058 | if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 1059 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1060 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1061 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1062 | return -1; |
| 1063 | } |
| 1064 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 1065 | /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no |
| 1066 | * identity hint. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1067 | if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1068 | /* TODO(davidben): This should be reset in one place with the rest of the |
| 1069 | * handshake state. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1070 | OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint); |
| 1071 | ssl->s3->tmp.peer_psk_identity_hint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1072 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1073 | ssl->s3->tmp.reuse_message = 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1074 | return 1; |
| 1075 | } |
| 1076 | |
| 1077 | /* Retain a copy of the original CBS to compute the signature over. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1078 | CBS server_key_exchange; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1079 | CBS_init(&server_key_exchange, ssl->init_msg, n); |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1080 | CBS server_key_exchange_orig = server_key_exchange; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1081 | |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1082 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1083 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1084 | EVP_MD_CTX_init(&md_ctx); |
| 1085 | |
| 1086 | if (alg_a & SSL_aPSK) { |
| 1087 | CBS psk_identity_hint; |
| 1088 | |
| 1089 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1090 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1091 | &psk_identity_hint)) { |
| 1092 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1093 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1094 | goto f_err; |
| 1095 | } |
| 1096 | |
| 1097 | /* Store PSK identity hint for later use, hint is used in |
| 1098 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1099 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1100 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1101 | * |
| 1102 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1103 | * a specific identity. */ |
| 1104 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1105 | CBS_contains_zero_byte(&psk_identity_hint)) { |
| 1106 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1107 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1108 | goto f_err; |
| 1109 | } |
| 1110 | |
| 1111 | /* Save the identity hint as a C string. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1112 | if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1113 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1114 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1115 | goto f_err; |
| 1116 | } |
| 1117 | } |
| 1118 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1119 | if (alg_k & SSL_kDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1120 | CBS dh_p, dh_g, dh_Ys; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1121 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1122 | CBS_len(&dh_p) == 0 || |
| 1123 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1124 | CBS_len(&dh_g) == 0 || |
| 1125 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1126 | CBS_len(&dh_Ys) == 0) { |
| 1127 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1128 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1129 | goto f_err; |
| 1130 | } |
| 1131 | |
| 1132 | dh = DH_new(); |
| 1133 | if (dh == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1134 | goto err; |
| 1135 | } |
| 1136 | |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1137 | dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL); |
| 1138 | dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL); |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1139 | if (dh->p == NULL || dh->g == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1140 | goto err; |
| 1141 | } |
| 1142 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1143 | ssl->session->key_exchange_info = DH_num_bits(dh); |
| 1144 | if (ssl->session->key_exchange_info < 1024) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1145 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1146 | goto err; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1147 | } else if (ssl->session->key_exchange_info > 4096) { |
David Benjamin | cd24a39 | 2015-11-11 13:23:05 -0800 | [diff] [blame] | 1148 | /* Overly large DHE groups are prohibitively expensive, so enforce a limit |
| 1149 | * to prevent a server from causing us to perform too expensive of a |
| 1150 | * computation. */ |
| 1151 | OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG); |
| 1152 | goto err; |
| 1153 | } |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1154 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1155 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1156 | dh = NULL; |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1157 | |
| 1158 | /* Save the peer public key for later. */ |
| 1159 | size_t peer_key_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1160 | if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1161 | goto err; |
| 1162 | } |
| 1163 | /* |dh_Ys| has a u16 length prefix, so this fits in a |uint16_t|. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1164 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1165 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1166 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1167 | /* Parse the server parameters. */ |
| 1168 | uint8_t curve_type; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1169 | uint16_t curve_id; |
David Benjamin | 48cce66 | 2015-12-17 01:33:08 -0500 | [diff] [blame] | 1170 | CBS point; |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1171 | if (!CBS_get_u8(&server_key_exchange, &curve_type) || |
| 1172 | curve_type != NAMED_CURVE_TYPE || |
| 1173 | !CBS_get_u16(&server_key_exchange, &curve_id) || |
| 1174 | !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1175 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1176 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1177 | goto f_err; |
| 1178 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1179 | ssl->session->key_exchange_info = curve_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1180 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1181 | /* Ensure the curve is consistent with preferences. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1182 | if (!tls1_check_curve_id(ssl, curve_id)) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1183 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1184 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1185 | goto f_err; |
| 1186 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1187 | |
| 1188 | /* Initialize ECDH and save the peer public key for later. */ |
| 1189 | size_t peer_key_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1190 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, curve_id) || |
| 1191 | !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) { |
David Benjamin | c1cc858 | 2015-12-17 01:22:15 -0500 | [diff] [blame] | 1192 | goto err; |
| 1193 | } |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1194 | /* |point| has a u8 length prefix, so this fits in a |uint16_t|. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1195 | assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff); |
| 1196 | ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1197 | } else if (!(alg_k & SSL_kPSK)) { |
| 1198 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1199 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1200 | goto f_err; |
| 1201 | } |
| 1202 | |
| 1203 | /* At this point, |server_key_exchange| contains the signature, if any, while |
| 1204 | * |server_key_exchange_orig| contains the entire message. From that, derive |
| 1205 | * a CBS containing just the parameter. */ |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1206 | CBS parameter; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1207 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1208 | CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange)); |
| 1209 | |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1210 | /* ServerKeyExchange should be signed by the server's public key. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame^] | 1211 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1212 | pkey = X509_get_pubkey(ssl->session->peer); |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1213 | if (pkey == NULL) { |
| 1214 | goto err; |
| 1215 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1216 | |
David Benjamin | b35d684 | 2015-12-29 17:08:08 -0500 | [diff] [blame] | 1217 | const EVP_MD *md = NULL; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1218 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1219 | uint8_t hash, signature; |
| 1220 | if (!CBS_get_u8(&server_key_exchange, &hash) || |
| 1221 | !CBS_get_u8(&server_key_exchange, &signature)) { |
| 1222 | al = SSL_AD_DECODE_ERROR; |
| 1223 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1224 | goto f_err; |
| 1225 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1226 | if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature, pkey)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1227 | goto f_err; |
| 1228 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1229 | ssl->s3->tmp.server_key_exchange_hash = hash; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1230 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1231 | md = EVP_md5_sha1(); |
| 1232 | } else { |
| 1233 | md = EVP_sha1(); |
| 1234 | } |
| 1235 | |
| 1236 | /* The last field in |server_key_exchange| is the signature. */ |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1237 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1238 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1239 | CBS_len(&server_key_exchange) != 0) { |
| 1240 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1241 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1242 | goto f_err; |
| 1243 | } |
| 1244 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1245 | int sig_ok = EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) && |
| 1246 | EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->client_random, |
| 1247 | SSL3_RANDOM_SIZE) && |
| 1248 | EVP_DigestVerifyUpdate(&md_ctx, ssl->s3->server_random, |
| 1249 | SSL3_RANDOM_SIZE) && |
| 1250 | EVP_DigestVerifyUpdate(&md_ctx, CBS_data(¶meter), |
| 1251 | CBS_len(¶meter)) && |
| 1252 | EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), |
| 1253 | CBS_len(&signature)); |
| 1254 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1255 | sig_ok = 1; |
| 1256 | ERR_clear_error(); |
| 1257 | #endif |
| 1258 | if (!sig_ok) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1259 | /* bad signature */ |
| 1260 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1261 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1262 | goto f_err; |
| 1263 | } |
| 1264 | } else { |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1265 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1266 | assert(alg_a == SSL_aPSK); |
| 1267 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1268 | if (CBS_len(&server_key_exchange) > 0) { |
| 1269 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1270 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1271 | goto f_err; |
| 1272 | } |
| 1273 | } |
| 1274 | EVP_PKEY_free(pkey); |
| 1275 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1276 | return 1; |
| 1277 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1278 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1279 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1280 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1281 | EVP_PKEY_free(pkey); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1282 | DH_free(dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1283 | EC_POINT_free(srvr_ecpoint); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1284 | EC_KEY_free(ecdh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1285 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1286 | return -1; |
| 1287 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1288 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1289 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 1290 | return X509_NAME_cmp(*a, *b); |
| 1291 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1292 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1293 | int ssl3_get_certificate_request(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1294 | int ok, ret = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1295 | X509_NAME *xn = NULL; |
| 1296 | STACK_OF(X509_NAME) *ca_sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1297 | |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1298 | long n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_REQ_A, |
| 1299 | SSL3_ST_CR_CERT_REQ_B, -1, |
| 1300 | ssl_hash_message, &ok); |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1301 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1302 | if (!ok) { |
| 1303 | return n; |
| 1304 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1305 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1306 | ssl->s3->tmp.cert_req = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1307 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1308 | if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { |
| 1309 | ssl->s3->tmp.reuse_message = 1; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1310 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1311 | * client auth. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1312 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1313 | return 1; |
| 1314 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1315 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1316 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 1317 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1318 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1319 | goto err; |
| 1320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1321 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1322 | CBS cbs; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1323 | CBS_init(&cbs, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1324 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1325 | ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
| 1326 | if (ca_sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1327 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1328 | goto err; |
| 1329 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1330 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1331 | /* get the certificate types */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1332 | CBS certificate_types; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1333 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1334 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1335 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1336 | goto err; |
| 1337 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1338 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1339 | if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types, |
| 1340 | &ssl->s3->tmp.num_certificate_types)) { |
| 1341 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1342 | goto err; |
| 1343 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1344 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1345 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1346 | CBS supported_signature_algorithms; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1347 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1348 | !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
| 1349 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1350 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1351 | goto err; |
| 1352 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1353 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1354 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1355 | /* get the CA RDNs */ |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1356 | CBS certificate_authorities; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1357 | if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1358 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1359 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1360 | goto err; |
| 1361 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1363 | while (CBS_len(&certificate_authorities) > 0) { |
| 1364 | CBS distinguished_name; |
| 1365 | if (!CBS_get_u16_length_prefixed(&certificate_authorities, |
| 1366 | &distinguished_name)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1367 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1368 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1369 | goto err; |
| 1370 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1371 | |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1372 | const uint8_t *data = CBS_data(&distinguished_name); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1373 | /* A u16 length cannot overflow a long. */ |
| 1374 | xn = d2i_X509_NAME(NULL, &data, (long)CBS_len(&distinguished_name)); |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1375 | if (xn == NULL || |
| 1376 | data != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1377 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | a5177cb | 2016-03-11 19:38:17 -0500 | [diff] [blame] | 1378 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1379 | goto err; |
| 1380 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1381 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1382 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1383 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1384 | goto err; |
| 1385 | } |
David Benjamin | 6b6e0b2 | 2016-03-11 19:30:29 -0500 | [diff] [blame] | 1386 | xn = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1387 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1388 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1389 | /* we should setup a certificate to return.... */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1390 | ssl->s3->tmp.cert_req = 1; |
| 1391 | sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free); |
| 1392 | ssl->s3->tmp.ca_names = ca_sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1393 | ca_sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1394 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1395 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1396 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1397 | err: |
David Benjamin | 6b6e0b2 | 2016-03-11 19:30:29 -0500 | [diff] [blame] | 1398 | X509_NAME_free(xn); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1399 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1400 | return ret; |
| 1401 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1402 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1403 | int ssl3_get_new_session_ticket(SSL *ssl) { |
David Benjamin | 6807062 | 2015-02-08 23:44:59 -0500 | [diff] [blame] | 1404 | int ok, al; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1405 | long n = ssl->method->ssl_get_message( |
| 1406 | ssl, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B, |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1407 | SSL3_MT_NEWSESSION_TICKET, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1408 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1409 | if (!ok) { |
| 1410 | return n; |
| 1411 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1412 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1413 | CBS new_session_ticket, ticket; |
| 1414 | uint32_t ticket_lifetime_hint; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1415 | CBS_init(&new_session_ticket, ssl->init_msg, n); |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1416 | if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) || |
| 1417 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1418 | CBS_len(&new_session_ticket) != 0) { |
| 1419 | al = SSL_AD_DECODE_ERROR; |
| 1420 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1421 | goto f_err; |
| 1422 | } |
| 1423 | |
| 1424 | if (CBS_len(&ticket) == 0) { |
| 1425 | /* RFC 5077 allows a server to change its mind and send no ticket after |
Adam Langley | 09d68c9 | 2015-10-28 18:08:10 -0700 | [diff] [blame] | 1426 | * negotiating the extension. The value of |tlsext_ticket_expected| is |
| 1427 | * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary |
| 1428 | * update. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1429 | ssl->tlsext_ticket_expected = 0; |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1430 | return 1; |
| 1431 | } |
| 1432 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1433 | if (ssl->hit) { |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 1434 | /* The server is sending a new ticket for an existing session. Sessions are |
| 1435 | * immutable once established, so duplicate all but the ticket of the |
| 1436 | * existing session. */ |
| 1437 | uint8_t *bytes; |
| 1438 | size_t bytes_len; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1439 | if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &bytes, &bytes_len)) { |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 1440 | goto err; |
| 1441 | } |
| 1442 | SSL_SESSION *new_session = SSL_SESSION_from_bytes(bytes, bytes_len); |
| 1443 | OPENSSL_free(bytes); |
| 1444 | if (new_session == NULL) { |
| 1445 | /* This should never happen. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1446 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 1447 | goto err; |
| 1448 | } |
| 1449 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1450 | SSL_SESSION_free(ssl->session); |
| 1451 | ssl->session = new_session; |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 1452 | } |
| 1453 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1454 | if (!CBS_stow(&ticket, &ssl->session->tlsext_tick, |
| 1455 | &ssl->session->tlsext_ticklen)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1456 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1457 | goto err; |
| 1458 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1459 | ssl->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1460 | |
David Benjamin | 6807062 | 2015-02-08 23:44:59 -0500 | [diff] [blame] | 1461 | /* Generate a session ID for this session based on the session ticket. We use |
| 1462 | * the session ID mechanism for detecting ticket resumption. This also fits in |
| 1463 | * with assumptions elsewhere in OpenSSL.*/ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1464 | if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), ssl->session->session_id, |
| 1465 | &ssl->session->session_id_length, EVP_sha256(), NULL)) { |
David Benjamin | 6807062 | 2015-02-08 23:44:59 -0500 | [diff] [blame] | 1466 | goto err; |
| 1467 | } |
| 1468 | |
| 1469 | return 1; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1470 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1471 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1472 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1473 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1474 | return -1; |
| 1475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1476 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1477 | int ssl3_get_cert_status(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1478 | int ok, al; |
| 1479 | long n; |
| 1480 | CBS certificate_status, ocsp_response; |
| 1481 | uint8_t status_type; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1482 | |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1483 | n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_CERT_STATUS_A, |
| 1484 | SSL3_ST_CR_CERT_STATUS_B, -1, |
| 1485 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1486 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1487 | if (!ok) { |
| 1488 | return n; |
| 1489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1490 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1491 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { |
David Benjamin | dcd979f | 2015-04-20 18:26:52 -0400 | [diff] [blame] | 1492 | /* A server may send status_request in ServerHello and then change |
| 1493 | * its mind about sending CertificateStatus. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1494 | ssl->s3->tmp.reuse_message = 1; |
David Benjamin | dcd979f | 2015-04-20 18:26:52 -0400 | [diff] [blame] | 1495 | return 1; |
| 1496 | } |
| 1497 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1498 | CBS_init(&certificate_status, ssl->init_msg, n); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1499 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1500 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1501 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1502 | CBS_len(&ocsp_response) == 0 || |
| 1503 | CBS_len(&certificate_status) != 0) { |
| 1504 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1505 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1506 | goto f_err; |
| 1507 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1508 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1509 | if (!CBS_stow(&ocsp_response, &ssl->session->ocsp_response, |
| 1510 | &ssl->session->ocsp_response_length)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1511 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1512 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1513 | goto f_err; |
| 1514 | } |
| 1515 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1516 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1517 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1518 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1519 | return -1; |
| 1520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1521 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1522 | int ssl3_get_server_done(SSL *ssl) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1523 | int ok; |
| 1524 | long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1525 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1526 | n = ssl->method->ssl_get_message(ssl, SSL3_ST_CR_SRVR_DONE_A, |
David Benjamin | 060cfb0 | 2016-05-12 00:43:05 -0400 | [diff] [blame] | 1527 | SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE, |
| 1528 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1529 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1530 | if (!ok) { |
| 1531 | return n; |
| 1532 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1533 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1534 | if (n > 0) { |
| 1535 | /* should contain no data */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1536 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1537 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1538 | return -1; |
| 1539 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1540 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1541 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1542 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1543 | |
Brian Smith | 274341d | 2015-10-08 17:10:15 -1000 | [diff] [blame] | 1544 | OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned), |
| 1545 | SIZE_T_IS_SMALLER_THAN_UNSIGNED); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1546 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1547 | int ssl3_send_client_key_exchange(SSL *ssl) { |
| 1548 | if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) { |
| 1549 | return ssl_do_write(ssl); |
| 1550 | } |
| 1551 | assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A); |
| 1552 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1553 | uint8_t *pms = NULL; |
| 1554 | size_t pms_len = 0; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1555 | CBB cbb; |
| 1556 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1557 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) { |
| 1558 | goto err; |
| 1559 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1560 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1561 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1562 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1563 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1564 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1565 | unsigned psk_len = 0; |
| 1566 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1567 | if (alg_a & SSL_aPSK) { |
| 1568 | if (ssl->psk_client_callback == NULL) { |
| 1569 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
| 1570 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1571 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1572 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1573 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 1574 | memset(identity, 0, sizeof(identity)); |
| 1575 | psk_len = ssl->psk_client_callback( |
| 1576 | ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity), |
| 1577 | psk, sizeof(psk)); |
| 1578 | if (psk_len == 0) { |
| 1579 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1580 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1581 | goto err; |
| 1582 | } |
| 1583 | assert(psk_len <= PSK_MAX_PSK_LEN); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1584 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1585 | OPENSSL_free(ssl->session->psk_identity); |
| 1586 | ssl->session->psk_identity = BUF_strdup(identity); |
| 1587 | if (ssl->session->psk_identity == NULL) { |
| 1588 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1589 | goto err; |
| 1590 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1591 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1592 | /* Write out psk_identity. */ |
| 1593 | CBB child; |
| 1594 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1595 | !CBB_add_bytes(&child, (const uint8_t *)identity, |
| 1596 | OPENSSL_strnlen(identity, sizeof(identity))) || |
| 1597 | !CBB_flush(&cbb)) { |
| 1598 | goto err; |
| 1599 | } |
| 1600 | } |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1601 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1602 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1603 | if (alg_k & SSL_kRSA) { |
| 1604 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1605 | pms = OPENSSL_malloc(pms_len); |
| 1606 | if (pms == NULL) { |
| 1607 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1608 | goto err; |
| 1609 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1610 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1611 | EVP_PKEY *pkey = X509_get_pubkey(ssl->session->peer); |
| 1612 | if (pkey == NULL) { |
| 1613 | goto err; |
| 1614 | } |
| 1615 | |
| 1616 | RSA *rsa = EVP_PKEY_get0_RSA(pkey); |
| 1617 | if (rsa == NULL) { |
| 1618 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1619 | EVP_PKEY_free(pkey); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1620 | goto err; |
| 1621 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1622 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1623 | ssl->session->key_exchange_info = EVP_PKEY_bits(pkey); |
| 1624 | EVP_PKEY_free(pkey); |
| 1625 | |
| 1626 | pms[0] = ssl->client_version >> 8; |
| 1627 | pms[1] = ssl->client_version & 0xff; |
| 1628 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1629 | goto err; |
| 1630 | } |
| 1631 | |
| 1632 | CBB child, *enc_pms = &cbb; |
| 1633 | size_t enc_pms_len; |
| 1634 | /* In TLS, there is a length prefix. */ |
| 1635 | if (ssl->version > SSL3_VERSION) { |
| 1636 | if (!CBB_add_u16_length_prefixed(&cbb, &child)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1637 | goto err; |
| 1638 | } |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1639 | enc_pms = &child; |
| 1640 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1641 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1642 | uint8_t *ptr; |
| 1643 | if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) || |
| 1644 | !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len, |
| 1645 | RSA_PKCS1_PADDING) || |
| 1646 | /* Log the premaster secret, if logging is enabled. */ |
| 1647 | !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) || |
| 1648 | !CBB_did_write(enc_pms, enc_pms_len) || |
| 1649 | !CBB_flush(&cbb)) { |
| 1650 | goto err; |
| 1651 | } |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1652 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) { |
| 1653 | /* Generate a keypair and serialize the public half. ECDHE uses a u8 length |
| 1654 | * prefix while DHE uses u16. */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1655 | CBB child; |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1656 | int child_ok; |
| 1657 | if (alg_k & SSL_kECDHE) { |
| 1658 | child_ok = CBB_add_u8_length_prefixed(&cbb, &child); |
| 1659 | } else { |
| 1660 | child_ok = CBB_add_u16_length_prefixed(&cbb, &child); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1661 | } |
| 1662 | |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1663 | if (!child_ok || |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1664 | !SSL_ECDH_CTX_generate_keypair(&ssl->s3->tmp.ecdh_ctx, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1665 | !CBB_flush(&cbb)) { |
| 1666 | goto err; |
| 1667 | } |
| 1668 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1669 | /* Compute the premaster. */ |
| 1670 | uint8_t alert; |
| 1671 | if (!SSL_ECDH_CTX_compute_secret(&ssl->s3->tmp.ecdh_ctx, &pms, &pms_len, |
| 1672 | &alert, ssl->s3->tmp.peer_key, |
| 1673 | ssl->s3->tmp.peer_key_len)) { |
| 1674 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1675 | goto err; |
| 1676 | } |
| 1677 | |
| 1678 | /* The key exchange state may now be discarded. */ |
| 1679 | SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx); |
| 1680 | OPENSSL_free(ssl->s3->tmp.peer_key); |
| 1681 | ssl->s3->tmp.peer_key = NULL; |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1682 | } else if (alg_k & SSL_kPSK) { |
| 1683 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1684 | * the pre-shared key. */ |
| 1685 | pms_len = psk_len; |
| 1686 | pms = OPENSSL_malloc(pms_len); |
| 1687 | if (pms == NULL) { |
| 1688 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1689 | goto err; |
| 1690 | } |
| 1691 | memset(pms, 0, pms_len); |
| 1692 | } else { |
| 1693 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1694 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1695 | goto err; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1698 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1699 | * key. */ |
| 1700 | if (alg_a & SSL_aPSK) { |
| 1701 | CBB pms_cbb, child; |
| 1702 | uint8_t *new_pms; |
| 1703 | size_t new_pms_len; |
| 1704 | |
| 1705 | CBB_zero(&pms_cbb); |
| 1706 | if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) || |
| 1707 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1708 | !CBB_add_bytes(&child, pms, pms_len) || |
| 1709 | !CBB_add_u16_length_prefixed(&pms_cbb, &child) || |
| 1710 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1711 | !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) { |
| 1712 | CBB_cleanup(&pms_cbb); |
| 1713 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1714 | goto err; |
| 1715 | } |
| 1716 | OPENSSL_cleanse(pms, pms_len); |
| 1717 | OPENSSL_free(pms); |
| 1718 | pms = new_pms; |
| 1719 | pms_len = new_pms_len; |
| 1720 | } |
| 1721 | |
| 1722 | /* The message must be added to the finished hash before calculating the |
| 1723 | * master secret. */ |
| 1724 | size_t length; |
| 1725 | if (!CBB_finish(&cbb, NULL, &length) || |
| 1726 | !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, length)) { |
| 1727 | goto err; |
| 1728 | } |
| 1729 | ssl->state = SSL3_ST_CW_KEY_EXCH_B; |
| 1730 | |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 1731 | ssl->session->master_key_length = |
| 1732 | tls1_generate_master_secret(ssl, ssl->session->master_key, pms, pms_len); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1733 | if (ssl->session->master_key_length == 0) { |
| 1734 | goto err; |
| 1735 | } |
| 1736 | ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret; |
| 1737 | OPENSSL_cleanse(pms, pms_len); |
| 1738 | OPENSSL_free(pms); |
| 1739 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1740 | /* SSL3_ST_CW_KEY_EXCH_B */ |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1741 | return ssl_do_write(ssl); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1742 | |
| 1743 | err: |
David Benjamin | 2b07fa4 | 2016-03-02 00:23:57 -0500 | [diff] [blame] | 1744 | CBB_cleanup(&cbb); |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 1745 | if (pms != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1746 | OPENSSL_cleanse(pms, pms_len); |
| 1747 | OPENSSL_free(pms); |
| 1748 | } |
| 1749 | return -1; |
| 1750 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1751 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1752 | int ssl3_send_cert_verify(SSL *ssl) { |
| 1753 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) { |
| 1754 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1757 | CBB cbb, child; |
| 1758 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1759 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) { |
| 1760 | goto err; |
| 1761 | } |
| 1762 | |
| 1763 | assert(ssl_has_private_key(ssl)); |
| 1764 | |
| 1765 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
| 1766 | size_t sig_len; |
| 1767 | enum ssl_private_key_result_t sign_result; |
| 1768 | if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) { |
| 1769 | /* Select and write out the digest type in TLS 1.2. */ |
| 1770 | const EVP_MD *md = NULL; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1771 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1772 | md = tls1_choose_signing_digest(ssl); |
| 1773 | if (!tls12_add_sigandhash(ssl, &cbb, md)) { |
| 1774 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1775 | goto err; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | /* Compute the digest. In TLS 1.1 and below, the digest type is also |
| 1780 | * selected here. */ |
| 1781 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1782 | size_t digest_len; |
| 1783 | if (!ssl3_cert_verify_hash(ssl, digest, &digest_len, &md, |
| 1784 | ssl_private_key_type(ssl))) { |
| 1785 | goto err; |
| 1786 | } |
| 1787 | |
| 1788 | /* The handshake buffer is no longer necessary. */ |
| 1789 | ssl3_free_handshake_buffer(ssl); |
| 1790 | |
| 1791 | /* Sign the digest. */ |
| 1792 | uint8_t *ptr; |
| 1793 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1794 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1795 | goto err; |
| 1796 | } |
| 1797 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md, |
| 1798 | digest, digest_len); |
| 1799 | } else { |
| 1800 | assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B); |
| 1801 | |
| 1802 | /* Skip over the already written signature algorithm and retry the |
| 1803 | * signature. */ |
| 1804 | uint8_t *ptr; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1805 | if ((ssl3_protocol_version(ssl) >= TLS1_2_VERSION && |
| 1806 | !CBB_did_write(&cbb, 2)) || |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1807 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1808 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1809 | goto err; |
| 1810 | } |
| 1811 | sign_result = |
| 1812 | ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len); |
| 1813 | } |
| 1814 | |
| 1815 | switch (sign_result) { |
| 1816 | case ssl_private_key_success: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1817 | break; |
| 1818 | case ssl_private_key_failure: |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1819 | goto err; |
| 1820 | case ssl_private_key_retry: |
| 1821 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1822 | ssl->state = SSL3_ST_CW_CERT_VRFY_B; |
| 1823 | goto err; |
| 1824 | } |
| 1825 | |
| 1826 | size_t length; |
| 1827 | if (!CBB_did_write(&child, sig_len) || |
| 1828 | !CBB_finish(&cbb, NULL, &length) || |
| 1829 | !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_VERIFY, length)) { |
| 1830 | goto err; |
| 1831 | } |
| 1832 | |
| 1833 | ssl->state = SSL3_ST_CW_CERT_VRFY_C; |
| 1834 | return ssl_do_write(ssl); |
| 1835 | |
| 1836 | err: |
| 1837 | CBB_cleanup(&cbb); |
| 1838 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /* ssl3_has_client_certificate returns true if a client certificate is |
| 1842 | * configured. */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1843 | static int ssl3_has_client_certificate(SSL *ssl) { |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1844 | return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1847 | int ssl3_send_client_certificate(SSL *ssl) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1848 | if (ssl->state == SSL3_ST_CW_CERT_A) { |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1849 | /* Call cert_cb to update the certificate. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1850 | if (ssl->cert->cert_cb) { |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1851 | int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 1852 | if (ret < 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1853 | ssl->rwstate = SSL_X509_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1854 | return -1; |
| 1855 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1856 | if (ret == 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1857 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1858 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1859 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1860 | } |
| 1861 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1862 | if (ssl3_has_client_certificate(ssl)) { |
| 1863 | ssl->state = SSL3_ST_CW_CERT_C; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1864 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1865 | ssl->state = SSL3_ST_CW_CERT_B; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1866 | } |
| 1867 | } |
| 1868 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1869 | if (ssl->state == SSL3_ST_CW_CERT_B) { |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1870 | /* Call client_cert_cb to update the certificate. */ |
| 1871 | X509 *x509 = NULL; |
| 1872 | EVP_PKEY *pkey = NULL; |
| 1873 | int ret = ssl_do_client_cert_cb(ssl, &x509, &pkey); |
| 1874 | if (ret < 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1875 | ssl->rwstate = SSL_X509_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1876 | return -1; |
| 1877 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1878 | |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1879 | int setup_error = ret == 1 && (!SSL_use_certificate(ssl, x509) || |
| 1880 | !SSL_use_PrivateKey(ssl, pkey)); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1881 | X509_free(x509); |
| 1882 | EVP_PKEY_free(pkey); |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1883 | if (setup_error) { |
| 1884 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1885 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1886 | } |
| 1887 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1888 | ssl->state = SSL3_ST_CW_CERT_C; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1889 | } |
| 1890 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1891 | if (ssl->state == SSL3_ST_CW_CERT_C) { |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1892 | if (!ssl3_has_client_certificate(ssl)) { |
| 1893 | /* Without a client certificate, the handshake buffer may be released. */ |
| 1894 | ssl3_free_handshake_buffer(ssl); |
| 1895 | |
| 1896 | if (ssl->version == SSL3_VERSION) { |
| 1897 | /* In SSL 3.0, send no certificate by skipping both messages. */ |
| 1898 | ssl->s3->tmp.cert_req = 0; |
| 1899 | ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 1900 | return 1; |
| 1901 | } |
| 1902 | |
| 1903 | /* In TLS, send an empty Certificate message. */ |
| 1904 | ssl->s3->tmp.cert_req = 2; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1905 | uint8_t *p = ssl_handshake_start(ssl); |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1906 | l2n3(0, p); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1907 | if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE, 3)) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1908 | return -1; |
| 1909 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1910 | } else if (!ssl3_output_cert_chain(ssl)) { |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1911 | return -1; |
| 1912 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1913 | ssl->state = SSL3_ST_CW_CERT_D; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 1916 | assert(ssl->state == SSL3_ST_CW_CERT_D); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1917 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1920 | int ssl3_send_next_proto(SSL *ssl) { |
| 1921 | if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) { |
| 1922 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1923 | } |
| 1924 | |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1925 | assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A); |
| 1926 | |
| 1927 | static const uint8_t kZero[32] = {0}; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1928 | size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32); |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1929 | |
| 1930 | CBB cbb, child; |
| 1931 | size_t length; |
| 1932 | CBB_zero(&cbb); |
| 1933 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1934 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 1935 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1936 | !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated, |
| 1937 | ssl->s3->next_proto_negotiated_len) || |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1938 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 1939 | !CBB_add_bytes(&child, kZero, padding_len) || |
| 1940 | !CBB_finish(&cbb, NULL, &length) || |
| 1941 | !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) { |
| 1942 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1943 | CBB_cleanup(&cbb); |
| 1944 | return -1; |
| 1945 | } |
| 1946 | |
| 1947 | ssl->state = SSL3_ST_CW_NEXT_PROTO_B; |
| 1948 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1949 | } |
| 1950 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1951 | int ssl3_send_channel_id(SSL *ssl) { |
| 1952 | if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) { |
| 1953 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1954 | } |
| 1955 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1956 | assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A); |
| 1957 | |
| 1958 | if (ssl->tlsext_channel_id_private == NULL && |
| 1959 | ssl->ctx->channel_id_cb != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1960 | EVP_PKEY *key = NULL; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1961 | ssl->ctx->channel_id_cb(ssl, &key); |
| 1962 | if (key != NULL && |
| 1963 | !SSL_set1_tls_channel_id(ssl, key)) { |
| 1964 | EVP_PKEY_free(key); |
| 1965 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1966 | } |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1967 | EVP_PKEY_free(key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1968 | } |
| 1969 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1970 | if (ssl->tlsext_channel_id_private == NULL) { |
| 1971 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1972 | return -1; |
| 1973 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1974 | |
David Benjamin | 758d127 | 2015-11-20 17:47:25 -0500 | [diff] [blame] | 1975 | EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private); |
| 1976 | if (ec_key == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1977 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1978 | return -1; |
| 1979 | } |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1980 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1981 | int ret = -1; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1982 | BIGNUM *x = BN_new(); |
| 1983 | BIGNUM *y = BN_new(); |
| 1984 | ECDSA_SIG *sig = NULL; |
| 1985 | if (x == NULL || y == NULL || |
| 1986 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 1987 | EC_KEY_get0_public_key(ec_key), |
| 1988 | x, y, NULL)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1989 | goto err; |
| 1990 | } |
| 1991 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1992 | uint8_t digest[EVP_MAX_MD_SIZE]; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 1993 | size_t digest_len; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 1994 | if (!tls1_channel_id_hash(ssl, digest, &digest_len)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1995 | goto err; |
| 1996 | } |
| 1997 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 1998 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1999 | if (sig == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2000 | goto err; |
| 2001 | } |
| 2002 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2003 | CBB cbb, child; |
| 2004 | size_t length; |
| 2005 | CBB_zero(&cbb); |
| 2006 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 2007 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 2008 | !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) || |
| 2009 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | ae0eaaa | 2015-12-17 02:01:25 -0500 | [diff] [blame] | 2010 | !BN_bn2cbb_padded(&child, 32, x) || |
| 2011 | !BN_bn2cbb_padded(&child, 32, y) || |
| 2012 | !BN_bn2cbb_padded(&child, 32, sig->r) || |
| 2013 | !BN_bn2cbb_padded(&child, 32, sig->s) || |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2014 | !CBB_finish(&cbb, NULL, &length) || |
| 2015 | !ssl_set_handshake_header(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS, length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2016 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2017 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2018 | goto err; |
| 2019 | } |
| 2020 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2021 | ssl->state = SSL3_ST_CW_CHANNEL_ID_B; |
| 2022 | ret = ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2023 | |
| 2024 | err: |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2025 | BN_free(x); |
| 2026 | BN_free(y); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2027 | ECDSA_SIG_free(sig); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2028 | return ret; |
| 2029 | } |
| 2030 | |
David Benjamin | fd8e69f | 2015-10-03 10:49:20 -0400 | [diff] [blame] | 2031 | int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) { |
| 2032 | if (ssl->ctx->client_cert_cb == NULL) { |
| 2033 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2034 | } |
David Benjamin | 454aa4c | 2016-03-10 08:43:27 -0500 | [diff] [blame] | 2035 | |
| 2036 | int ret = ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey); |
| 2037 | if (ret <= 0) { |
| 2038 | return ret; |
| 2039 | } |
| 2040 | |
| 2041 | assert(*out_x509 != NULL); |
| 2042 | assert(*out_pkey != NULL); |
| 2043 | return 1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2044 | } |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2045 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2046 | int ssl3_verify_server_cert(SSL *ssl) { |
| 2047 | int ret = ssl_verify_cert_chain(ssl, ssl->session->cert_chain); |
| 2048 | if (ssl->verify_mode != SSL_VERIFY_NONE && ret <= 0) { |
| 2049 | int al = ssl_verify_alarm_type(ssl->verify_result); |
| 2050 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2051 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2052 | } else { |
| 2053 | ret = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2054 | ERR_clear_error(); /* but we keep ssl->verify_result */ |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | return ret; |
| 2058 | } |