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