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 | 2b0aeeca | 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; |
| 358 | s->s3->change_cipher_spec = 0; |
| 359 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 360 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 361 | s->init_num = 0; |
| 362 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 364 | case SSL3_ST_CW_CERT_VRFY_A: |
| 365 | case SSL3_ST_CW_CERT_VRFY_B: |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 366 | case SSL3_ST_CW_CERT_VRFY_C: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 367 | ret = ssl3_send_cert_verify(s); |
| 368 | if (ret <= 0) { |
| 369 | goto end; |
| 370 | } |
| 371 | s->state = SSL3_ST_CW_CHANGE_A; |
| 372 | s->init_num = 0; |
| 373 | s->s3->change_cipher_spec = 0; |
| 374 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 376 | case SSL3_ST_CW_CHANGE_A: |
| 377 | case SSL3_ST_CW_CHANGE_B: |
| 378 | ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A, |
| 379 | SSL3_ST_CW_CHANGE_B); |
| 380 | if (ret <= 0) { |
| 381 | goto end; |
| 382 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 384 | s->state = SSL3_ST_CW_FINISHED_A; |
| 385 | if (s->s3->tlsext_channel_id_valid) { |
| 386 | s->state = SSL3_ST_CW_CHANNEL_ID_A; |
| 387 | } |
| 388 | if (s->s3->next_proto_neg_seen) { |
| 389 | s->state = SSL3_ST_CW_NEXT_PROTO_A; |
| 390 | } |
| 391 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 393 | s->session->cipher = s->s3->tmp.new_cipher; |
David Benjamin | 8b36841 | 2015-03-14 01:54:17 -0400 | [diff] [blame] | 394 | if (!s->enc_method->setup_key_block(s) || |
| 395 | !s->enc_method->change_cipher_state( |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 396 | s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 397 | ret = -1; |
| 398 | goto end; |
| 399 | } |
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 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 403 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 404 | case SSL3_ST_CW_NEXT_PROTO_B: |
| 405 | ret = ssl3_send_next_proto(s); |
| 406 | if (ret <= 0) { |
| 407 | goto end; |
| 408 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 410 | if (s->s3->tlsext_channel_id_valid) { |
| 411 | s->state = SSL3_ST_CW_CHANNEL_ID_A; |
| 412 | } else { |
| 413 | s->state = SSL3_ST_CW_FINISHED_A; |
| 414 | } |
| 415 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 417 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 418 | case SSL3_ST_CW_CHANNEL_ID_B: |
| 419 | ret = ssl3_send_channel_id(s); |
| 420 | if (ret <= 0) { |
| 421 | goto end; |
| 422 | } |
| 423 | s->state = SSL3_ST_CW_FINISHED_A; |
| 424 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 426 | case SSL3_ST_CW_FINISHED_A: |
| 427 | case SSL3_ST_CW_FINISHED_B: |
| 428 | ret = |
| 429 | ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B, |
| 430 | s->enc_method->client_finished_label, |
| 431 | s->enc_method->client_finished_label_len); |
| 432 | if (ret <= 0) { |
| 433 | goto end; |
| 434 | } |
| 435 | s->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 437 | if (s->hit) { |
| 438 | s->s3->tmp.next_state = SSL_ST_OK; |
| 439 | } else { |
| 440 | /* This is a non-resumption handshake. If it involves ChannelID, then |
| 441 | * record the handshake hashes at this point in the session so that |
| 442 | * any resumption of this session with ChannelID can sign those |
| 443 | * hashes. */ |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 444 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 445 | if (ret <= 0) { |
| 446 | goto end; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 447 | } |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 448 | if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) && |
| 449 | ssl3_can_false_start(s) && |
| 450 | /* No False Start on renegotiation (would complicate the state |
| 451 | * machine). */ |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 452 | !s->s3->initial_handshake_complete) { |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 453 | s->s3->tmp.next_state = SSL3_ST_FALSE_START; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 454 | } else { |
| 455 | /* Allow NewSessionTicket if ticket expected */ |
| 456 | if (s->tlsext_ticket_expected) { |
| 457 | s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
| 458 | } else { |
| 459 | s->s3->tmp.next_state = SSL3_ST_CR_CHANGE; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | s->init_num = 0; |
| 464 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 466 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 467 | case SSL3_ST_CR_SESSION_TICKET_B: |
| 468 | ret = ssl3_get_new_session_ticket(s); |
| 469 | if (ret <= 0) { |
| 470 | goto end; |
| 471 | } |
| 472 | s->state = SSL3_ST_CR_CHANGE; |
| 473 | s->init_num = 0; |
| 474 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 476 | case SSL3_ST_CR_CERT_STATUS_A: |
| 477 | case SSL3_ST_CR_CERT_STATUS_B: |
| 478 | ret = ssl3_get_cert_status(s); |
| 479 | if (ret <= 0) { |
| 480 | goto end; |
| 481 | } |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 482 | s->state = SSL3_ST_VERIFY_SERVER_CERT; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 483 | s->init_num = 0; |
| 484 | break; |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 485 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 486 | case SSL3_ST_CR_CHANGE: |
| 487 | /* At this point, the next message must be entirely behind a |
| 488 | * ChangeCipherSpec. */ |
| 489 | if (!ssl3_expect_change_cipher_spec(s)) { |
| 490 | ret = -1; |
| 491 | goto end; |
| 492 | } |
| 493 | s->state = SSL3_ST_CR_FINISHED_A; |
| 494 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 496 | case SSL3_ST_CR_FINISHED_A: |
| 497 | case SSL3_ST_CR_FINISHED_B: |
| 498 | ret = |
| 499 | ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B); |
| 500 | if (ret <= 0) { |
| 501 | goto end; |
| 502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 504 | if (s->hit) { |
| 505 | s->state = SSL3_ST_CW_CHANGE_A; |
| 506 | } else { |
| 507 | s->state = SSL_ST_OK; |
| 508 | } |
| 509 | s->init_num = 0; |
| 510 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 512 | case SSL3_ST_CW_FLUSH: |
| 513 | s->rwstate = SSL_WRITING; |
| 514 | if (BIO_flush(s->wbio) <= 0) { |
| 515 | ret = -1; |
| 516 | goto end; |
| 517 | } |
| 518 | s->rwstate = SSL_NOTHING; |
| 519 | s->state = s->s3->tmp.next_state; |
| 520 | break; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 522 | case SSL3_ST_FALSE_START: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 523 | /* Allow NewSessionTicket if ticket expected */ |
| 524 | if (s->tlsext_ticket_expected) { |
| 525 | s->state = SSL3_ST_CR_SESSION_TICKET_A; |
| 526 | } else { |
| 527 | s->state = SSL3_ST_CR_CHANGE; |
| 528 | } |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 529 | s->s3->tmp.in_false_start = 1; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 531 | ssl_free_wbio_buffer(s); |
| 532 | ret = 1; |
| 533 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 534 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 535 | case SSL_ST_OK: |
| 536 | /* clean a few things up */ |
| 537 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 539 | BUF_MEM_free(s->init_buf); |
| 540 | s->init_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 541 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 542 | /* Remove write buffering now. */ |
| 543 | ssl_free_wbio_buffer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 545 | const int is_initial_handshake = !s->s3->initial_handshake_complete; |
| 546 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 547 | s->init_num = 0; |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 548 | s->s3->tmp.in_false_start = 0; |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 549 | s->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 550 | |
David Benjamin | ba4594a | 2015-06-18 18:36:15 -0400 | [diff] [blame] | 551 | if (is_initial_handshake) { |
| 552 | /* Renegotiations do not participate in session resumption. */ |
| 553 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
| 554 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 556 | ret = 1; |
| 557 | /* s->server=0; */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 559 | if (cb != NULL) { |
| 560 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
| 561 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 563 | goto end; |
| 564 | |
| 565 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 566 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 567 | ret = -1; |
| 568 | goto end; |
| 569 | } |
| 570 | |
| 571 | if (!s->s3->tmp.reuse_message && !skip) { |
| 572 | if (cb != NULL && s->state != state) { |
| 573 | new_state = s->state; |
| 574 | s->state = state; |
| 575 | cb(s, SSL_CB_CONNECT_LOOP, 1); |
| 576 | s->state = new_state; |
| 577 | } |
| 578 | } |
| 579 | skip = 0; |
| 580 | } |
| 581 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 582 | end: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 583 | s->in_handshake--; |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 584 | BUF_MEM_free(buf); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 585 | if (cb != NULL) { |
| 586 | cb(s, SSL_CB_CONNECT_EXIT, ret); |
| 587 | } |
| 588 | return ret; |
| 589 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 591 | static int ssl3_write_client_cipher_list(SSL *ssl, CBB *out) { |
| 592 | /* Prepare disabled cipher masks. */ |
| 593 | ssl_set_client_disabled(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 595 | CBB child; |
| 596 | if (!CBB_add_u16_length_prefixed(out, &child)) { |
| 597 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 598 | } |
| 599 | |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 600 | STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl); |
| 601 | |
| 602 | int any_enabled = 0; |
| 603 | size_t i; |
| 604 | for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
| 605 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 606 | /* Skip disabled ciphers */ |
| 607 | if (cipher->algorithm_ssl & ssl->cert->mask_ssl || |
| 608 | cipher->algorithm_mkey & ssl->cert->mask_k || |
| 609 | cipher->algorithm_auth & ssl->cert->mask_a) { |
| 610 | continue; |
| 611 | } |
| 612 | any_enabled = 1; |
| 613 | if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) { |
| 614 | return 0; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* If all ciphers were disabled, return the error to the caller. */ |
| 619 | if (!any_enabled) { |
| 620 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 625 | * added. */ |
| 626 | if (ssl->client_version == SSL3_VERSION && |
| 627 | !ssl->s3->initial_handshake_complete) { |
| 628 | if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) { |
| 629 | return 0; |
| 630 | } |
| 631 | /* The renegotiation extension is required to be at index zero. */ |
| 632 | ssl->s3->tmp.extensions.sent |= (1u << 0); |
| 633 | } |
| 634 | |
| 635 | if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) && |
| 636 | !CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | return CBB_flush(out); |
| 641 | } |
| 642 | |
| 643 | int ssl3_send_client_hello(SSL *ssl) { |
| 644 | if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) { |
| 645 | return ssl_do_write(ssl); |
| 646 | } |
| 647 | |
| 648 | CBB cbb; |
| 649 | CBB_zero(&cbb); |
| 650 | |
| 651 | assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A); |
| 652 | if (!ssl->s3->have_version) { |
| 653 | uint16_t max_version = ssl3_get_max_client_version(ssl); |
| 654 | /* Disabling all versions is silly: return an error. */ |
| 655 | if (max_version == 0) { |
| 656 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
| 657 | goto err; |
| 658 | } |
| 659 | |
| 660 | ssl->version = max_version; |
| 661 | /* Only set |ssl->client_version| on the initial handshake. Renegotiations, |
| 662 | * although locked to a version, reuse the value. When using the plain RSA |
| 663 | * key exchange, the ClientHello version is checked in the premaster secret. |
| 664 | * Some servers fail when this value changes. */ |
| 665 | ssl->client_version = max_version; |
| 666 | } |
| 667 | |
David Benjamin | 1269ddd | 2015-10-18 15:18:55 -0400 | [diff] [blame^] | 668 | /* If the configured session has expired or was created at a version higher |
| 669 | * than our maximum version, drop it. */ |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 670 | if (ssl->session != NULL && |
| 671 | (ssl->session->session_id_length == 0 || ssl->session->not_resumable || |
David Benjamin | 1269ddd | 2015-10-18 15:18:55 -0400 | [diff] [blame^] | 672 | ssl->session->timeout < (long)(time(NULL) - ssl->session->time) || |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 673 | (!SSL_IS_DTLS(ssl) && ssl->session->ssl_version > ssl->version) || |
| 674 | (SSL_IS_DTLS(ssl) && ssl->session->ssl_version < ssl->version))) { |
| 675 | SSL_set_session(ssl, NULL); |
| 676 | } |
| 677 | |
| 678 | /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't |
| 679 | * renegerate the client_random. The random must be reused. */ |
| 680 | if ((!SSL_IS_DTLS(ssl) || !ssl->d1->send_cookie) && |
| 681 | !ssl_fill_hello_random(ssl->s3->client_random, |
| 682 | sizeof(ssl->s3->client_random), 0 /* client */)) { |
| 683 | goto err; |
| 684 | } |
| 685 | |
| 686 | /* Renegotiations do not participate in session resumption. */ |
| 687 | int has_session = ssl->session != NULL && |
| 688 | !ssl->s3->initial_handshake_complete; |
| 689 | |
| 690 | CBB child; |
| 691 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 692 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 693 | !CBB_add_u16(&cbb, ssl->client_version) || |
| 694 | !CBB_add_bytes(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 695 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 696 | (has_session && |
| 697 | !CBB_add_bytes(&child, ssl->session->session_id, |
| 698 | ssl->session->session_id_length))) { |
| 699 | goto err; |
| 700 | } |
| 701 | |
| 702 | if (SSL_IS_DTLS(ssl)) { |
| 703 | if (!CBB_add_u8_length_prefixed(&cbb, &child) || |
| 704 | !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) { |
| 705 | goto err; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | size_t length; |
| 710 | if (!ssl3_write_client_cipher_list(ssl, &cbb) || |
| 711 | !CBB_add_u8(&cbb, 1 /* one compression method */) || |
| 712 | !CBB_add_u8(&cbb, 0 /* null compression */) || |
| 713 | !ssl_add_clienthello_tlsext(ssl, &cbb, |
| 714 | CBB_len(&cbb) + SSL_HM_HEADER_LENGTH(ssl)) || |
| 715 | !CBB_finish(&cbb, NULL, &length) || |
| 716 | !ssl_set_handshake_header(ssl, SSL3_MT_CLIENT_HELLO, length)) { |
| 717 | goto err; |
| 718 | } |
| 719 | |
| 720 | ssl->state = SSL3_ST_CW_CLNT_HELLO_B; |
| 721 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 722 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 723 | err: |
David Benjamin | e8d5350 | 2015-10-10 14:13:23 -0400 | [diff] [blame] | 724 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 725 | return -1; |
| 726 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 727 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 728 | int ssl3_get_server_hello(SSL *s) { |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 729 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 730 | const SSL_CIPHER *c; |
| 731 | CERT *ct = s->cert; |
| 732 | int al = SSL_AD_INTERNAL_ERROR, ok; |
| 733 | long n; |
| 734 | CBS server_hello, server_random, session_id; |
| 735 | uint16_t server_version, cipher_suite; |
| 736 | uint8_t compression_method; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 737 | uint32_t mask_ssl; |
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 | n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A, |
| 740 | SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO, |
| 741 | 20000, /* ?? */ |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 742 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 743 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 744 | if (!ok) { |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 745 | uint32_t err = ERR_peek_error(); |
| 746 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 747 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
| 748 | /* Add a dedicated error code to the queue for a handshake_failure alert |
| 749 | * in response to ClientHello. This matches NSS's client behavior and |
| 750 | * gives a better error on a (probable) failure to negotiate initial |
| 751 | * parameters. Note: this error code comes after the original one. |
| 752 | * |
| 753 | * See https://crbug.com/446505. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 754 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
David Benjamin | 780d6dd | 2015-01-06 12:03:19 -0500 | [diff] [blame] | 755 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 756 | return n; |
| 757 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 758 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 759 | CBS_init(&server_hello, s->init_msg, n); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 760 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 761 | if (!CBS_get_u16(&server_hello, &server_version) || |
| 762 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
| 763 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
| 764 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 765 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 766 | !CBS_get_u8(&server_hello, &compression_method)) { |
| 767 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 768 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 769 | goto f_err; |
| 770 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 771 | |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 772 | assert(s->s3->have_version == s->s3->initial_handshake_complete); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 773 | if (!s->s3->have_version) { |
| 774 | if (!ssl3_is_version_enabled(s, server_version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 775 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 776 | s->version = server_version; |
| 777 | /* Mark the version as fixed so the record-layer version is not clamped |
| 778 | * to TLS 1.0. */ |
| 779 | s->s3->have_version = 1; |
| 780 | al = SSL_AD_PROTOCOL_VERSION; |
| 781 | goto f_err; |
| 782 | } |
| 783 | s->version = server_version; |
| 784 | s->enc_method = ssl3_get_enc_method(server_version); |
| 785 | assert(s->enc_method != NULL); |
| 786 | /* At this point, the connection's version is known and s->version is |
| 787 | * fixed. Begin enforcing the record-layer version. */ |
| 788 | s->s3->have_version = 1; |
| 789 | } else if (server_version != s->version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 790 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 791 | al = SSL_AD_PROTOCOL_VERSION; |
| 792 | goto f_err; |
| 793 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 795 | /* Copy over the server random. */ |
| 796 | memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 797 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 798 | assert(s->session == NULL || s->session->session_id_length > 0); |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 799 | if (!s->s3->initial_handshake_complete && s->session != NULL && |
| 800 | CBS_mem_equal(&session_id, s->session->session_id, |
| 801 | s->session->session_id_length)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 802 | if (s->sid_ctx_length != s->session->sid_ctx_length || |
| 803 | memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { |
| 804 | /* actually a client application bug */ |
| 805 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 806 | OPENSSL_PUT_ERROR(SSL, |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 807 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 808 | goto f_err; |
| 809 | } |
| 810 | s->hit = 1; |
| 811 | } else { |
| 812 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 813 | * fill out. */ |
| 814 | s->hit = 0; |
David Benjamin | dc2aea2 | 2015-10-18 12:50:32 -0400 | [diff] [blame] | 815 | if (!ssl_get_new_session(s, 0 /* client */)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 816 | goto f_err; |
| 817 | } |
| 818 | /* Note: session_id could be empty. */ |
| 819 | s->session->session_id_length = CBS_len(&session_id); |
| 820 | memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
| 821 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 822 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 823 | c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 824 | if (c == NULL) { |
| 825 | /* unknown cipher */ |
| 826 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 827 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 828 | goto f_err; |
| 829 | } |
| 830 | /* ct->mask_ssl was computed from client capabilities. Now |
| 831 | * that the final version is known, compute a new mask_ssl. */ |
| 832 | if (!SSL_USE_TLS1_2_CIPHERS(s)) { |
| 833 | mask_ssl = SSL_TLSV1_2; |
| 834 | } else { |
| 835 | mask_ssl = 0; |
| 836 | } |
| 837 | /* If the cipher is disabled then we didn't sent it in the ClientHello, so if |
| 838 | * the server selected it, it's an error. */ |
| 839 | if ((c->algorithm_ssl & mask_ssl) || |
| 840 | (c->algorithm_mkey & ct->mask_k) || |
| 841 | (c->algorithm_auth & ct->mask_a)) { |
| 842 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 843 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 844 | goto f_err; |
| 845 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 846 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 847 | sk = ssl_get_ciphers_by_id(s); |
| 848 | if (!sk_SSL_CIPHER_find(sk, NULL, c)) { |
| 849 | /* we did not say we would use this cipher */ |
| 850 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 851 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 852 | goto f_err; |
| 853 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 854 | |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 855 | if (s->hit) { |
| 856 | if (s->session->cipher != c) { |
| 857 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 858 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 859 | goto f_err; |
| 860 | } |
| 861 | if (s->session->ssl_version != s->version) { |
| 862 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 863 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
David Benjamin | ece3de9 | 2015-03-16 18:02:20 -0400 | [diff] [blame] | 864 | goto f_err; |
| 865 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 866 | } |
| 867 | s->s3->tmp.new_cipher = c; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 868 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 869 | /* Now that the cipher is known, initialize the handshake hash. */ |
| 870 | if (!ssl3_init_handshake_hash(s)) { |
| 871 | goto f_err; |
| 872 | } |
| 873 | |
David Benjamin | 5f04b65 | 2015-05-26 17:30:10 -0400 | [diff] [blame] | 874 | /* If doing a full handshake with TLS 1.2, the server may request a client |
| 875 | * certificate which requires hashing the handshake transcript under a |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 876 | * different hash. Otherwise, the handshake buffer may be released. */ |
| 877 | if (!SSL_USE_SIGALGS(s) || s->hit) { |
| 878 | ssl3_free_handshake_buffer(s); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 879 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 880 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 881 | /* Only the NULL compression algorithm is supported. */ |
| 882 | if (compression_method != 0) { |
| 883 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 884 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 885 | goto f_err; |
| 886 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 887 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 888 | /* TLS extensions */ |
| 889 | if (!ssl_parse_serverhello_tlsext(s, &server_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 890 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 891 | goto err; |
| 892 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 894 | /* There should be nothing left over in the record. */ |
| 895 | if (CBS_len(&server_hello) != 0) { |
| 896 | /* wrong packet length */ |
| 897 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 898 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 899 | goto f_err; |
| 900 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 901 | |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 902 | if (s->hit && |
| 903 | s->s3->tmp.extended_master_secret != s->session->extended_master_secret) { |
| 904 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 905 | if (s->session->extended_master_secret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 906 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 907 | } else { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 908 | 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] | 909 | } |
| 910 | goto f_err; |
| 911 | } |
| 912 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 913 | return 1; |
| 914 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 915 | f_err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 916 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 917 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 918 | return -1; |
| 919 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 920 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 921 | /* ssl3_check_certificate_for_cipher returns one if |leaf| is a suitable server |
| 922 | * certificate type for |cipher|. Otherwise, it returns zero and pushes an error |
| 923 | * on the error queue. */ |
| 924 | static int ssl3_check_certificate_for_cipher(X509 *leaf, |
| 925 | const SSL_CIPHER *cipher) { |
| 926 | int ret = 0; |
| 927 | EVP_PKEY *pkey = X509_get_pubkey(leaf); |
David Benjamin | d1c1c8e | 2015-06-08 18:36:21 -0400 | [diff] [blame] | 928 | if (pkey == NULL) { |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 929 | goto err; |
| 930 | } |
| 931 | |
| 932 | /* Check the certificate's type matches the cipher. */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 933 | int expected_type = ssl_cipher_get_key_type(cipher); |
| 934 | assert(expected_type != EVP_PKEY_NONE); |
| 935 | if (pkey->type != expected_type) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 936 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 937 | goto err; |
| 938 | } |
| 939 | |
| 940 | /* TODO(davidben): This behavior is preserved from upstream. Should key usages |
| 941 | * be checked in other cases as well? */ |
| 942 | if (cipher->algorithm_auth & SSL_aECDSA) { |
| 943 | /* This call populates the ex_flags field correctly */ |
| 944 | X509_check_purpose(leaf, -1, 0); |
| 945 | if ((leaf->ex_flags & EXFLAG_KUSAGE) && |
| 946 | !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 947 | OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 948 | goto err; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | ret = 1; |
| 953 | |
| 954 | err: |
| 955 | EVP_PKEY_free(pkey); |
| 956 | return ret; |
| 957 | } |
| 958 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 959 | int ssl3_get_server_certificate(SSL *s) { |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 960 | int al, ok, ret = -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 961 | unsigned long n; |
| 962 | X509 *x = NULL; |
| 963 | STACK_OF(X509) *sk = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 964 | EVP_PKEY *pkey = NULL; |
| 965 | CBS cbs, certificate_list; |
| 966 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 967 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 968 | 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] | 969 | SSL3_MT_CERTIFICATE, (long)s->max_cert_list, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 970 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 971 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 972 | if (!ok) { |
| 973 | return n; |
| 974 | } |
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 | CBS_init(&cbs, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 977 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 978 | sk = sk_X509_new_null(); |
| 979 | if (sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 980 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 981 | goto err; |
| 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 | if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) || |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 985 | CBS_len(&certificate_list) == 0 || |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 986 | CBS_len(&cbs) != 0) { |
| 987 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 988 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 989 | goto f_err; |
| 990 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 991 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 992 | while (CBS_len(&certificate_list) > 0) { |
| 993 | CBS certificate; |
| 994 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 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_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 997 | goto f_err; |
| 998 | } |
| 999 | data = CBS_data(&certificate); |
| 1000 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
| 1001 | if (x == NULL) { |
| 1002 | al = SSL_AD_BAD_CERTIFICATE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1003 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1004 | goto f_err; |
| 1005 | } |
| 1006 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 1007 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1008 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1009 | goto f_err; |
| 1010 | } |
| 1011 | if (!sk_X509_push(sk, x)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1012 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1013 | goto err; |
| 1014 | } |
| 1015 | x = NULL; |
| 1016 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1017 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1018 | X509 *leaf = sk_X509_value(sk, 0); |
| 1019 | if (!ssl3_check_certificate_for_cipher(leaf, s->s3->tmp.new_cipher)) { |
| 1020 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1021 | goto f_err; |
| 1022 | } |
| 1023 | |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1024 | /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes |
| 1025 | * the leaf. */ |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 1026 | sk_X509_pop_free(s->session->cert_chain, X509_free); |
| 1027 | s->session->cert_chain = sk; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1028 | sk = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1029 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1030 | X509_free(s->session->peer); |
David Benjamin | 436bf82 | 2015-06-07 19:50:37 -0400 | [diff] [blame] | 1031 | s->session->peer = X509_up_ref(leaf); |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1032 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1033 | s->session->verify_result = s->verify_result; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1034 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1035 | ret = 1; |
| 1036 | |
| 1037 | if (0) { |
| 1038 | f_err: |
| 1039 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1040 | } |
| 1041 | |
| 1042 | err: |
| 1043 | EVP_PKEY_free(pkey); |
| 1044 | X509_free(x); |
| 1045 | sk_X509_pop_free(sk, X509_free); |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | int ssl3_get_server_key_exchange(SSL *s) { |
| 1050 | EVP_MD_CTX md_ctx; |
| 1051 | int al, ok; |
| 1052 | long n, alg_k, alg_a; |
| 1053 | EVP_PKEY *pkey = NULL; |
| 1054 | const EVP_MD *md = NULL; |
| 1055 | RSA *rsa = NULL; |
| 1056 | DH *dh = NULL; |
| 1057 | EC_KEY *ecdh = NULL; |
| 1058 | BN_CTX *bn_ctx = NULL; |
| 1059 | EC_POINT *srvr_ecpoint = NULL; |
| 1060 | CBS server_key_exchange, server_key_exchange_orig, parameter; |
| 1061 | |
| 1062 | /* use same message size as in ssl3_get_certificate_request() as |
| 1063 | * ServerKeyExchange message may be skipped */ |
| 1064 | n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A, |
| 1065 | SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 1066 | ssl_hash_message, &ok); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1067 | if (!ok) { |
| 1068 | return n; |
| 1069 | } |
| 1070 | |
| 1071 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 1072 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1073 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1074 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1075 | return -1; |
| 1076 | } |
| 1077 | |
David Benjamin | 26416e9 | 2015-08-22 16:04:17 -0400 | [diff] [blame] | 1078 | /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no |
| 1079 | * identity hint. */ |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1080 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1081 | /* TODO(davidben): This should be reset in one place with the rest of the |
| 1082 | * handshake state. */ |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1083 | OPENSSL_free(s->s3->tmp.peer_psk_identity_hint); |
| 1084 | s->s3->tmp.peer_psk_identity_hint = NULL; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1085 | } |
| 1086 | s->s3->tmp.reuse_message = 1; |
| 1087 | return 1; |
| 1088 | } |
| 1089 | |
| 1090 | /* Retain a copy of the original CBS to compute the signature over. */ |
| 1091 | CBS_init(&server_key_exchange, s->init_msg, n); |
| 1092 | server_key_exchange_orig = server_key_exchange; |
| 1093 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1094 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1095 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1096 | EVP_MD_CTX_init(&md_ctx); |
| 1097 | |
| 1098 | if (alg_a & SSL_aPSK) { |
| 1099 | CBS psk_identity_hint; |
| 1100 | |
| 1101 | /* Each of the PSK key exchanges begins with a psk_identity_hint. */ |
| 1102 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, |
| 1103 | &psk_identity_hint)) { |
| 1104 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1105 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1106 | goto f_err; |
| 1107 | } |
| 1108 | |
| 1109 | /* Store PSK identity hint for later use, hint is used in |
| 1110 | * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK |
| 1111 | * identity hint can be as long as the maximum length of a PSK identity. |
| 1112 | * Also do not allow NULL characters; identities are saved as C strings. |
| 1113 | * |
| 1114 | * TODO(davidben): Should invalid hints be ignored? It's a hint rather than |
| 1115 | * a specific identity. */ |
| 1116 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1117 | CBS_contains_zero_byte(&psk_identity_hint)) { |
| 1118 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1119 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1120 | goto f_err; |
| 1121 | } |
| 1122 | |
| 1123 | /* Save the identity hint as a C string. */ |
| 1124 | if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) { |
| 1125 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1126 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1127 | goto f_err; |
| 1128 | } |
| 1129 | } |
| 1130 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1131 | if (alg_k & SSL_kDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1132 | CBS dh_p, dh_g, dh_Ys; |
| 1133 | |
| 1134 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1135 | CBS_len(&dh_p) == 0 || |
| 1136 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1137 | CBS_len(&dh_g) == 0 || |
| 1138 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1139 | CBS_len(&dh_Ys) == 0) { |
| 1140 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1141 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1142 | goto f_err; |
| 1143 | } |
| 1144 | |
| 1145 | dh = DH_new(); |
| 1146 | if (dh == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1147 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1148 | goto err; |
| 1149 | } |
| 1150 | |
| 1151 | if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL || |
| 1152 | (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL || |
| 1153 | (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) == |
| 1154 | NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1155 | OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1156 | goto err; |
| 1157 | } |
| 1158 | |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 1159 | s->session->key_exchange_info = DH_num_bits(dh); |
| 1160 | if (s->session->key_exchange_info < 1024) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1161 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1162 | goto err; |
| 1163 | } |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 1164 | DH_free(s->s3->tmp.peer_dh_tmp); |
| 1165 | s->s3->tmp.peer_dh_tmp = dh; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1166 | dh = NULL; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1167 | } else if (alg_k & SSL_kECDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1168 | uint16_t curve_id; |
| 1169 | int curve_nid = 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1170 | const EC_GROUP *group; |
| 1171 | CBS point; |
| 1172 | |
| 1173 | /* Extract elliptic curve parameters and the server's ephemeral ECDH public |
| 1174 | * key. Check curve is one of our preferences, if not server has sent an |
| 1175 | * invalid curve. */ |
| 1176 | if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) { |
| 1177 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1178 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1179 | goto f_err; |
| 1180 | } |
| 1181 | |
| 1182 | curve_nid = tls1_ec_curve_id2nid(curve_id); |
| 1183 | if (curve_nid == 0) { |
| 1184 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1185 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1186 | goto f_err; |
| 1187 | } |
| 1188 | |
David Benjamin | 4f7783e | 2015-03-05 03:05:11 -0500 | [diff] [blame] | 1189 | ecdh = EC_KEY_new_by_curve_name(curve_nid); |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 1190 | s->session->key_exchange_info = curve_id; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1191 | if (ecdh == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1192 | OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1193 | goto err; |
| 1194 | } |
| 1195 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1196 | group = EC_KEY_get0_group(ecdh); |
| 1197 | |
| 1198 | /* Next, get the encoded ECPoint */ |
| 1199 | if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) { |
| 1200 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1201 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1202 | goto f_err; |
| 1203 | } |
| 1204 | |
| 1205 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || |
| 1206 | ((bn_ctx = BN_CTX_new()) == NULL)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1207 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1208 | goto err; |
| 1209 | } |
| 1210 | |
| 1211 | if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point), |
| 1212 | CBS_len(&point), bn_ctx)) { |
| 1213 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1214 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1215 | goto f_err; |
| 1216 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1217 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 1218 | EC_KEY_free(s->s3->tmp.peer_ecdh_tmp); |
| 1219 | s->s3->tmp.peer_ecdh_tmp = ecdh; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1220 | ecdh = NULL; |
| 1221 | BN_CTX_free(bn_ctx); |
| 1222 | bn_ctx = NULL; |
| 1223 | EC_POINT_free(srvr_ecpoint); |
| 1224 | srvr_ecpoint = NULL; |
| 1225 | } else if (!(alg_k & SSL_kPSK)) { |
| 1226 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1227 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1228 | goto f_err; |
| 1229 | } |
| 1230 | |
| 1231 | /* At this point, |server_key_exchange| contains the signature, if any, while |
| 1232 | * |server_key_exchange_orig| contains the entire message. From that, derive |
| 1233 | * a CBS containing just the parameter. */ |
| 1234 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1235 | CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange)); |
| 1236 | |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1237 | /* ServerKeyExchange should be signed by the server's public key. */ |
| 1238 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
David Benjamin | b1bdc5b | 2015-08-22 12:56:34 -0400 | [diff] [blame] | 1239 | pkey = X509_get_pubkey(s->session->peer); |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1240 | if (pkey == NULL) { |
| 1241 | goto err; |
| 1242 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1243 | |
| 1244 | if (SSL_USE_SIGALGS(s)) { |
| 1245 | if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) { |
| 1246 | goto f_err; |
| 1247 | } |
| 1248 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1249 | md = EVP_md5_sha1(); |
| 1250 | } else { |
| 1251 | md = EVP_sha1(); |
| 1252 | } |
| 1253 | |
| 1254 | /* The last field in |server_key_exchange| is the signature. */ |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1255 | CBS signature; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1256 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1257 | CBS_len(&server_key_exchange) != 0) { |
| 1258 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1259 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1260 | goto f_err; |
| 1261 | } |
| 1262 | |
| 1263 | if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) || |
| 1264 | !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random, |
| 1265 | SSL3_RANDOM_SIZE) || |
| 1266 | !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random, |
| 1267 | SSL3_RANDOM_SIZE) || |
| 1268 | !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(¶meter), |
| 1269 | CBS_len(¶meter)) || |
| 1270 | !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), |
| 1271 | CBS_len(&signature))) { |
| 1272 | /* bad signature */ |
| 1273 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1274 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1275 | goto f_err; |
| 1276 | } |
| 1277 | } else { |
David Benjamin | f4958e7 | 2015-06-07 11:00:53 -0400 | [diff] [blame] | 1278 | /* PSK ciphers are the only supported certificate-less ciphers. */ |
| 1279 | assert(alg_a == SSL_aPSK); |
| 1280 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1281 | if (CBS_len(&server_key_exchange) > 0) { |
| 1282 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1283 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1284 | goto f_err; |
| 1285 | } |
| 1286 | } |
| 1287 | EVP_PKEY_free(pkey); |
| 1288 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1289 | return 1; |
| 1290 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1291 | f_err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1292 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1293 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1294 | EVP_PKEY_free(pkey); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1295 | RSA_free(rsa); |
| 1296 | DH_free(dh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1297 | BN_CTX_free(bn_ctx); |
| 1298 | EC_POINT_free(srvr_ecpoint); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1299 | EC_KEY_free(ecdh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1300 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1301 | return -1; |
| 1302 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1303 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1304 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) { |
| 1305 | return X509_NAME_cmp(*a, *b); |
| 1306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1307 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1308 | int ssl3_get_certificate_request(SSL *s) { |
| 1309 | int ok, ret = 0; |
| 1310 | unsigned long n; |
| 1311 | X509_NAME *xn = NULL; |
| 1312 | STACK_OF(X509_NAME) *ca_sk = NULL; |
| 1313 | CBS cbs; |
| 1314 | CBS certificate_types; |
| 1315 | CBS certificate_authorities; |
| 1316 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1317 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1318 | n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A, |
| 1319 | SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 1320 | ssl_hash_message, &ok); |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1321 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1322 | if (!ok) { |
| 1323 | return n; |
| 1324 | } |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1325 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1326 | s->s3->tmp.cert_req = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1327 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1328 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { |
| 1329 | s->s3->tmp.reuse_message = 1; |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1330 | /* If we get here we don't need the handshake buffer as we won't be doing |
| 1331 | * client auth. */ |
| 1332 | ssl3_free_handshake_buffer(s); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1333 | return 1; |
| 1334 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1335 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1336 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 1337 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1338 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1339 | goto err; |
| 1340 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1341 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1342 | CBS_init(&cbs, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1343 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1344 | ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
| 1345 | if (ca_sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1346 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1347 | goto err; |
| 1348 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1349 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1350 | /* get the certificate types */ |
| 1351 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) { |
| 1352 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1353 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1354 | goto err; |
| 1355 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1356 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1357 | if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types, |
| 1358 | &s->s3->tmp.num_certificate_types)) { |
| 1359 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1360 | goto err; |
| 1361 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1363 | if (SSL_USE_SIGALGS(s)) { |
| 1364 | CBS supported_signature_algorithms; |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 1365 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) || |
| 1366 | !tls1_parse_peer_sigalgs(s, &supported_signature_algorithms)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1367 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1368 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1369 | goto err; |
| 1370 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1371 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1372 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1373 | /* get the CA RDNs */ |
| 1374 | if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) { |
| 1375 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1376 | OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1377 | goto err; |
| 1378 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1379 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1380 | while (CBS_len(&certificate_authorities) > 0) { |
| 1381 | CBS distinguished_name; |
| 1382 | if (!CBS_get_u16_length_prefixed(&certificate_authorities, |
| 1383 | &distinguished_name)) { |
| 1384 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1385 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1386 | goto err; |
| 1387 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1388 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1389 | data = CBS_data(&distinguished_name); |
HÃ¥vard Molland | 16c623b | 2014-08-12 11:29:57 +0200 | [diff] [blame] | 1390 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1391 | xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name)); |
| 1392 | if (xn == NULL) { |
| 1393 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1394 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1395 | goto err; |
| 1396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1397 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1398 | if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) { |
| 1399 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1400 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1401 | goto err; |
| 1402 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1403 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1404 | if (CBS_len(&distinguished_name) != 0) { |
| 1405 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1406 | OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_LENGTH_MISMATCH); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1407 | goto err; |
| 1408 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1409 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1410 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1411 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1412 | goto err; |
| 1413 | } |
| 1414 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1415 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1416 | /* we should setup a certificate to return.... */ |
| 1417 | s->s3->tmp.cert_req = 1; |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1418 | 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] | 1419 | s->s3->tmp.ca_names = ca_sk; |
| 1420 | ca_sk = NULL; |
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 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1423 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1424 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1425 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1426 | return ret; |
| 1427 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1428 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1429 | int ssl3_get_new_session_ticket(SSL *s) { |
David Benjamin | 6807062 | 2015-02-08 23:44:59 -0500 | [diff] [blame] | 1430 | int ok, al; |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1431 | long n = s->method->ssl_get_message( |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1432 | 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] | 1433 | SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1434 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1435 | if (!ok) { |
| 1436 | return n; |
| 1437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1438 | |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 1439 | CBS new_session_ticket, ticket; |
| 1440 | uint32_t ticket_lifetime_hint; |
| 1441 | CBS_init(&new_session_ticket, s->init_msg, n); |
| 1442 | if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) || |
| 1443 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1444 | CBS_len(&new_session_ticket) != 0) { |
| 1445 | al = SSL_AD_DECODE_ERROR; |
| 1446 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1447 | goto f_err; |
| 1448 | } |
| 1449 | |
| 1450 | if (CBS_len(&ticket) == 0) { |
| 1451 | /* RFC 5077 allows a server to change its mind and send no ticket after |
| 1452 | * negotiating the extension. Behave as if no ticket was sent. */ |
| 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 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1569 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1570 | int ssl3_send_client_key_exchange(SSL *s) { |
| 1571 | uint8_t *p; |
| 1572 | int n = 0; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1573 | uint32_t alg_k; |
| 1574 | uint32_t alg_a; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1575 | uint8_t *q; |
| 1576 | EVP_PKEY *pkey = NULL; |
| 1577 | EC_KEY *clnt_ecdh = NULL; |
| 1578 | const EC_POINT *srvr_ecpoint = NULL; |
| 1579 | EVP_PKEY *srvr_pub_pkey = NULL; |
| 1580 | uint8_t *encodedPoint = NULL; |
| 1581 | int encoded_pt_len = 0; |
| 1582 | BN_CTX *bn_ctx = NULL; |
| 1583 | unsigned int psk_len = 0; |
| 1584 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1585 | uint8_t *pms = NULL; |
| 1586 | size_t pms_len = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1587 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1588 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) { |
| 1589 | p = ssl_handshake_start(s); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1590 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1591 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1592 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1593 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1594 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1595 | if (alg_a & SSL_aPSK) { |
| 1596 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 1597 | size_t identity_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1598 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1599 | if (s->psk_client_callback == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1600 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1601 | goto err; |
| 1602 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1603 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1604 | memset(identity, 0, sizeof(identity)); |
| 1605 | psk_len = |
| 1606 | s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity, |
| 1607 | sizeof(identity), psk, sizeof(psk)); |
| 1608 | if (psk_len > PSK_MAX_PSK_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1609 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1610 | goto err; |
| 1611 | } else if (psk_len == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1612 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1613 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1614 | goto err; |
| 1615 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1616 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1617 | identity_len = OPENSSL_strnlen(identity, sizeof(identity)); |
| 1618 | if (identity_len > PSK_MAX_IDENTITY_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1619 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1620 | goto err; |
| 1621 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1622 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1623 | OPENSSL_free(s->session->psk_identity); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1624 | s->session->psk_identity = BUF_strdup(identity); |
| 1625 | if (s->session->psk_identity == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1626 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1627 | goto err; |
| 1628 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1629 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1630 | /* Write out psk_identity. */ |
| 1631 | s2n(identity_len, p); |
| 1632 | memcpy(p, identity, identity_len); |
| 1633 | p += identity_len; |
| 1634 | n = 2 + identity_len; |
| 1635 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1636 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1637 | /* Depending on the key exchange method, compute |pms| and |pms_len|. */ |
| 1638 | if (alg_k & SSL_kRSA) { |
| 1639 | RSA *rsa; |
| 1640 | size_t enc_pms_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1641 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1642 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1643 | pms = OPENSSL_malloc(pms_len); |
| 1644 | if (pms == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1645 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1646 | goto err; |
| 1647 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1648 | |
David Benjamin | b1bdc5b | 2015-08-22 12:56:34 -0400 | [diff] [blame] | 1649 | pkey = X509_get_pubkey(s->session->peer); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1650 | if (pkey == NULL || |
| 1651 | pkey->type != EVP_PKEY_RSA || |
| 1652 | pkey->pkey.rsa == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1653 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1654 | EVP_PKEY_free(pkey); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1655 | goto err; |
| 1656 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1657 | |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 1658 | s->session->key_exchange_info = EVP_PKEY_bits(pkey); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1659 | rsa = pkey->pkey.rsa; |
| 1660 | EVP_PKEY_free(pkey); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1661 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1662 | pms[0] = s->client_version >> 8; |
| 1663 | pms[1] = s->client_version & 0xff; |
| 1664 | if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) { |
| 1665 | goto err; |
| 1666 | } |
| 1667 | |
| 1668 | s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH; |
| 1669 | |
| 1670 | q = p; |
| 1671 | /* In TLS and beyond, reserve space for the length prefix. */ |
| 1672 | if (s->version > SSL3_VERSION) { |
| 1673 | p += 2; |
| 1674 | n += 2; |
| 1675 | } |
| 1676 | if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len, |
| 1677 | RSA_PKCS1_PADDING)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1678 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_RSA_ENCRYPT); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1679 | goto err; |
| 1680 | } |
| 1681 | n += enc_pms_len; |
| 1682 | |
| 1683 | /* Log the premaster secret, if logging is enabled. */ |
| 1684 | if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms, |
| 1685 | pms_len)) { |
| 1686 | goto err; |
| 1687 | } |
| 1688 | |
| 1689 | /* Fill in the length prefix. */ |
| 1690 | if (s->version > SSL3_VERSION) { |
| 1691 | s2n(enc_pms_len, q); |
| 1692 | } |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1693 | } else if (alg_k & SSL_kDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1694 | DH *dh_srvr, *dh_clnt; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1695 | int dh_len; |
| 1696 | size_t pub_len; |
| 1697 | |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 1698 | if (s->s3->tmp.peer_dh_tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1699 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1700 | goto err; |
| 1701 | } |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 1702 | dh_srvr = s->s3->tmp.peer_dh_tmp; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1703 | |
| 1704 | /* generate a new random key */ |
| 1705 | dh_clnt = DHparams_dup(dh_srvr); |
| 1706 | if (dh_clnt == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1707 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1708 | goto err; |
| 1709 | } |
| 1710 | if (!DH_generate_key(dh_clnt)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1711 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1712 | DH_free(dh_clnt); |
| 1713 | goto err; |
| 1714 | } |
| 1715 | |
| 1716 | pms_len = DH_size(dh_clnt); |
| 1717 | pms = OPENSSL_malloc(pms_len); |
| 1718 | if (pms == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1719 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1720 | DH_free(dh_clnt); |
| 1721 | goto err; |
| 1722 | } |
| 1723 | |
| 1724 | dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt); |
| 1725 | if (dh_len <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1726 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1727 | DH_free(dh_clnt); |
| 1728 | goto err; |
| 1729 | } |
| 1730 | pms_len = dh_len; |
| 1731 | |
| 1732 | /* send off the data */ |
| 1733 | pub_len = BN_num_bytes(dh_clnt->pub_key); |
| 1734 | s2n(pub_len, p); |
| 1735 | BN_bn2bin(dh_clnt->pub_key, p); |
| 1736 | n += 2 + pub_len; |
| 1737 | |
| 1738 | DH_free(dh_clnt); |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1739 | } else if (alg_k & SSL_kECDHE) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1740 | const EC_GROUP *srvr_group = NULL; |
| 1741 | EC_KEY *tkey; |
| 1742 | int field_size = 0, ecdh_len; |
| 1743 | |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 1744 | if (s->s3->tmp.peer_ecdh_tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1745 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1746 | goto err; |
| 1747 | } |
| 1748 | |
David Benjamin | 6505567 | 2015-08-22 12:49:30 -0400 | [diff] [blame] | 1749 | tkey = s->s3->tmp.peer_ecdh_tmp; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1750 | |
| 1751 | srvr_group = EC_KEY_get0_group(tkey); |
| 1752 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); |
| 1753 | if (srvr_group == NULL || srvr_ecpoint == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1754 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1755 | goto err; |
| 1756 | } |
| 1757 | |
| 1758 | clnt_ecdh = EC_KEY_new(); |
| 1759 | if (clnt_ecdh == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1760 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1761 | goto err; |
| 1762 | } |
| 1763 | |
| 1764 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1765 | OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1766 | goto err; |
| 1767 | } |
| 1768 | |
| 1769 | /* Generate a new ECDH key pair */ |
| 1770 | if (!EC_KEY_generate_key(clnt_ecdh)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1771 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1772 | goto err; |
| 1773 | } |
| 1774 | |
| 1775 | field_size = EC_GROUP_get_degree(srvr_group); |
| 1776 | if (field_size <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1777 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1778 | goto err; |
| 1779 | } |
| 1780 | |
| 1781 | pms_len = (field_size + 7) / 8; |
| 1782 | pms = OPENSSL_malloc(pms_len); |
| 1783 | if (pms == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1784 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1785 | goto err; |
| 1786 | } |
| 1787 | |
| 1788 | ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL); |
| 1789 | if (ecdh_len <= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1790 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1791 | goto err; |
| 1792 | } |
| 1793 | pms_len = ecdh_len; |
| 1794 | |
| 1795 | /* First check the size of encoding and allocate memory accordingly. */ |
| 1796 | encoded_pt_len = |
| 1797 | EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh), |
| 1798 | POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); |
| 1799 | |
| 1800 | encodedPoint = |
| 1801 | (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t)); |
| 1802 | bn_ctx = BN_CTX_new(); |
| 1803 | if (encodedPoint == NULL || bn_ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1804 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1805 | goto err; |
| 1806 | } |
| 1807 | |
| 1808 | /* Encode the public key */ |
| 1809 | encoded_pt_len = EC_POINT_point2oct( |
| 1810 | srvr_group, EC_KEY_get0_public_key(clnt_ecdh), |
| 1811 | POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx); |
| 1812 | |
| 1813 | *p = encoded_pt_len; /* length of encoded point */ |
| 1814 | /* Encoded point will be copied here */ |
| 1815 | p += 1; |
| 1816 | n += 1; |
| 1817 | /* copy the point */ |
| 1818 | memcpy(p, encodedPoint, encoded_pt_len); |
| 1819 | /* increment n to account for length field */ |
| 1820 | n += encoded_pt_len; |
| 1821 | |
| 1822 | /* Free allocated memory */ |
| 1823 | BN_CTX_free(bn_ctx); |
| 1824 | bn_ctx = NULL; |
| 1825 | OPENSSL_free(encodedPoint); |
| 1826 | encodedPoint = NULL; |
| 1827 | EC_KEY_free(clnt_ecdh); |
| 1828 | clnt_ecdh = NULL; |
| 1829 | EVP_PKEY_free(srvr_pub_pkey); |
| 1830 | srvr_pub_pkey = NULL; |
| 1831 | } else if (alg_k & SSL_kPSK) { |
| 1832 | /* For plain PSK, other_secret is a block of 0s with the same length as |
| 1833 | * the pre-shared key. */ |
| 1834 | pms_len = psk_len; |
| 1835 | pms = OPENSSL_malloc(pms_len); |
| 1836 | if (pms == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1837 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1838 | goto err; |
| 1839 | } |
| 1840 | memset(pms, 0, pms_len); |
| 1841 | } else { |
| 1842 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1843 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1844 | goto err; |
| 1845 | } |
| 1846 | |
| 1847 | /* For a PSK cipher suite, other_secret is combined with the pre-shared |
| 1848 | * key. */ |
| 1849 | if (alg_a & SSL_aPSK) { |
| 1850 | CBB cbb, child; |
| 1851 | uint8_t *new_pms; |
| 1852 | size_t new_pms_len; |
| 1853 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 1854 | CBB_zero(&cbb); |
| 1855 | if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len) || |
| 1856 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1857 | !CBB_add_bytes(&child, pms, pms_len) || |
| 1858 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1859 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1860 | !CBB_finish(&cbb, &new_pms, &new_pms_len)) { |
| 1861 | CBB_cleanup(&cbb); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1862 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1863 | goto err; |
| 1864 | } |
| 1865 | OPENSSL_cleanse(pms, pms_len); |
| 1866 | OPENSSL_free(pms); |
| 1867 | pms = new_pms; |
| 1868 | pms_len = new_pms_len; |
| 1869 | } |
| 1870 | |
| 1871 | /* The message must be added to the finished hash before calculating the |
| 1872 | * master secret. */ |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1873 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) { |
| 1874 | goto err; |
| 1875 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1876 | s->state = SSL3_ST_CW_KEY_EXCH_B; |
| 1877 | |
| 1878 | s->session->master_key_length = s->enc_method->generate_master_secret( |
| 1879 | s, s->session->master_key, pms, pms_len); |
| 1880 | if (s->session->master_key_length == 0) { |
| 1881 | goto err; |
| 1882 | } |
| 1883 | s->session->extended_master_secret = s->s3->tmp.extended_master_secret; |
| 1884 | OPENSSL_cleanse(pms, pms_len); |
| 1885 | OPENSSL_free(pms); |
| 1886 | } |
| 1887 | |
| 1888 | /* SSL3_ST_CW_KEY_EXCH_B */ |
David Benjamin | 2fa83de | 2015-02-08 01:40:08 -0500 | [diff] [blame] | 1889 | return s->method->do_write(s); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1890 | |
| 1891 | err: |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1892 | BN_CTX_free(bn_ctx); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1893 | OPENSSL_free(encodedPoint); |
| 1894 | EC_KEY_free(clnt_ecdh); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1895 | EVP_PKEY_free(srvr_pub_pkey); |
| 1896 | if (pms) { |
| 1897 | OPENSSL_cleanse(pms, pms_len); |
| 1898 | OPENSSL_free(pms); |
| 1899 | } |
| 1900 | return -1; |
| 1901 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1902 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1903 | int ssl3_send_cert_verify(SSL *s) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1904 | if (s->state == SSL3_ST_CW_CERT_VRFY_A || |
| 1905 | s->state == SSL3_ST_CW_CERT_VRFY_B) { |
| 1906 | enum ssl_private_key_result_t sign_result; |
| 1907 | uint8_t *p = ssl_handshake_start(s); |
| 1908 | size_t signature_length = 0; |
| 1909 | unsigned long n = 0; |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1910 | assert(ssl_has_private_key(s)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1911 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1912 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) { |
| 1913 | uint8_t *buf = (uint8_t *)s->init_buf->data; |
| 1914 | const EVP_MD *md = NULL; |
| 1915 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1916 | size_t digest_length; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1917 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1918 | /* Write out the digest type if need be. */ |
| 1919 | if (SSL_USE_SIGALGS(s)) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1920 | md = tls1_choose_signing_digest(s); |
| 1921 | if (!tls12_get_sigandhash(s, p, md)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1922 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1923 | return -1; |
| 1924 | } |
| 1925 | p += 2; |
| 1926 | n += 2; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1927 | } |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1928 | |
| 1929 | /* Compute the digest. */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1930 | const int pkey_type = ssl_private_key_type(s); |
David Benjamin | 396a441 | 2015-07-05 01:38:09 -0400 | [diff] [blame] | 1931 | if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey_type)) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1932 | return -1; |
| 1933 | } |
| 1934 | |
| 1935 | /* The handshake buffer is no longer necessary. */ |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1936 | ssl3_free_handshake_buffer(s); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1937 | |
| 1938 | /* Sign the digest. */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1939 | signature_length = ssl_private_key_max_signature_len(s); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1940 | if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1941 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1942 | return -1; |
| 1943 | } |
| 1944 | |
| 1945 | s->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1946 | sign_result = ssl_private_key_sign(s, &p[2], &signature_length, |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1947 | signature_length, md, digest, |
| 1948 | digest_length); |
| 1949 | } else { |
| 1950 | if (SSL_USE_SIGALGS(s)) { |
| 1951 | /* The digest has already been selected and written. */ |
| 1952 | p += 2; |
| 1953 | n += 2; |
| 1954 | } |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1955 | signature_length = ssl_private_key_max_signature_len(s); |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1956 | s->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1957 | sign_result = ssl_private_key_sign_complete(s, &p[2], &signature_length, |
| 1958 | signature_length); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1961 | if (sign_result == ssl_private_key_retry) { |
| 1962 | s->state = SSL3_ST_CW_CERT_VRFY_B; |
| 1963 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1964 | } |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1965 | s->rwstate = SSL_NOTHING; |
| 1966 | if (sign_result != ssl_private_key_success) { |
| 1967 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | s2n(signature_length, p); |
| 1971 | n += signature_length + 2; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1972 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) { |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1973 | return -1; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1974 | } |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 1975 | s->state = SSL3_ST_CW_CERT_VRFY_C; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1976 | } |
| 1977 | |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1978 | return ssl_do_write(s); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | /* ssl3_has_client_certificate returns true if a client certificate is |
| 1982 | * configured. */ |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1983 | static int ssl3_has_client_certificate(SSL *ssl) { |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1984 | return ssl->cert && ssl->cert->x509 && ssl_has_private_key(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | int ssl3_send_client_certificate(SSL *s) { |
| 1988 | X509 *x509 = NULL; |
| 1989 | EVP_PKEY *pkey = NULL; |
| 1990 | int i; |
| 1991 | |
| 1992 | if (s->state == SSL3_ST_CW_CERT_A) { |
| 1993 | /* Let cert callback update client certificates if required */ |
| 1994 | if (s->cert->cert_cb) { |
| 1995 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1996 | if (i < 0) { |
| 1997 | s->rwstate = SSL_X509_LOOKUP; |
| 1998 | return -1; |
| 1999 | } |
| 2000 | if (i == 0) { |
| 2001 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 2002 | return 0; |
| 2003 | } |
| 2004 | s->rwstate = SSL_NOTHING; |
| 2005 | } |
| 2006 | |
| 2007 | if (ssl3_has_client_certificate(s)) { |
| 2008 | s->state = SSL3_ST_CW_CERT_C; |
| 2009 | } else { |
| 2010 | s->state = SSL3_ST_CW_CERT_B; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | /* We need to get a client cert */ |
| 2015 | if (s->state == SSL3_ST_CW_CERT_B) { |
| 2016 | /* If we get an error, we need to: |
| 2017 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); |
| 2018 | * We then get retried later */ |
| 2019 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 2020 | if (i < 0) { |
| 2021 | s->rwstate = SSL_X509_LOOKUP; |
| 2022 | return -1; |
| 2023 | } |
| 2024 | s->rwstate = SSL_NOTHING; |
| 2025 | if (i == 1 && pkey != NULL && x509 != NULL) { |
| 2026 | s->state = SSL3_ST_CW_CERT_B; |
| 2027 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) { |
| 2028 | i = 0; |
| 2029 | } |
| 2030 | } else if (i == 1) { |
| 2031 | i = 0; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2032 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2033 | } |
| 2034 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2035 | X509_free(x509); |
| 2036 | EVP_PKEY_free(pkey); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2037 | if (i && !ssl3_has_client_certificate(s)) { |
| 2038 | i = 0; |
| 2039 | } |
| 2040 | if (i == 0) { |
| 2041 | if (s->version == SSL3_VERSION) { |
| 2042 | s->s3->tmp.cert_req = 0; |
| 2043 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 2044 | return 1; |
| 2045 | } else { |
| 2046 | s->s3->tmp.cert_req = 2; |
David Benjamin | e76ccae | 2015-05-26 17:41:50 -0400 | [diff] [blame] | 2047 | /* There is no client certificate, so the handshake buffer may be |
| 2048 | * released. */ |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 2049 | ssl3_free_handshake_buffer(s); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | /* Ok, we have a cert */ |
| 2054 | s->state = SSL3_ST_CW_CERT_C; |
| 2055 | } |
| 2056 | |
| 2057 | if (s->state == SSL3_ST_CW_CERT_C) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2058 | if (s->s3->tmp.cert_req == 2) { |
| 2059 | /* Send an empty Certificate message. */ |
| 2060 | uint8_t *p = ssl_handshake_start(s); |
| 2061 | l2n3(0, p); |
| 2062 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, 3)) { |
| 2063 | return -1; |
| 2064 | } |
| 2065 | } else if (!ssl3_output_cert_chain(s)) { |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 2066 | return -1; |
| 2067 | } |
David Benjamin | 67dc838 | 2015-07-05 00:03:32 -0400 | [diff] [blame] | 2068 | s->state = SSL3_ST_CW_CERT_D; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /* SSL3_ST_CW_CERT_D */ |
| 2072 | return ssl_do_write(s); |
| 2073 | } |
| 2074 | |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2075 | int ssl3_send_next_proto(SSL *ssl) { |
| 2076 | if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) { |
| 2077 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2078 | } |
| 2079 | |
David Benjamin | e97b962 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 2080 | assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A); |
| 2081 | |
| 2082 | static const uint8_t kZero[32] = {0}; |
| 2083 | size_t padding_len = 32 - ((ssl->next_proto_negotiated_len + 2) % 32); |
| 2084 | |
| 2085 | CBB cbb, child; |
| 2086 | size_t length; |
| 2087 | CBB_zero(&cbb); |
| 2088 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 2089 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 2090 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 2091 | !CBB_add_bytes(&child, ssl->next_proto_negotiated, |
| 2092 | ssl->next_proto_negotiated_len) || |
| 2093 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 2094 | !CBB_add_bytes(&child, kZero, padding_len) || |
| 2095 | !CBB_finish(&cbb, NULL, &length) || |
| 2096 | !ssl_set_handshake_header(ssl, SSL3_MT_NEXT_PROTO, length)) { |
| 2097 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2098 | CBB_cleanup(&cbb); |
| 2099 | return -1; |
| 2100 | } |
| 2101 | |
| 2102 | ssl->state = SSL3_ST_CW_NEXT_PROTO_B; |
| 2103 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2106 | static int write_32_byte_big_endian(CBB *out, const BIGNUM *in) { |
| 2107 | uint8_t *ptr; |
| 2108 | return CBB_add_space(out, &ptr, 32) && |
| 2109 | BN_bn2bin_padded(ptr, 32, in); |
| 2110 | } |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2111 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2112 | int ssl3_send_channel_id(SSL *ssl) { |
| 2113 | if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) { |
| 2114 | return ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2117 | assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A); |
| 2118 | |
| 2119 | if (ssl->tlsext_channel_id_private == NULL && |
| 2120 | ssl->ctx->channel_id_cb != NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2121 | EVP_PKEY *key = NULL; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2122 | ssl->ctx->channel_id_cb(ssl, &key); |
| 2123 | if (key != NULL && |
| 2124 | !SSL_set1_tls_channel_id(ssl, key)) { |
| 2125 | EVP_PKEY_free(key); |
| 2126 | return -1; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2127 | } |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2128 | EVP_PKEY_free(key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2131 | if (ssl->tlsext_channel_id_private == NULL) { |
| 2132 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2133 | return -1; |
| 2134 | } |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2135 | ssl->rwstate = SSL_NOTHING; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2136 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2137 | if (EVP_PKEY_id(ssl->tlsext_channel_id_private) != EVP_PKEY_EC) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2138 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 2139 | return -1; |
| 2140 | } |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 2141 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2142 | int ret = -1; |
| 2143 | EC_KEY *ec_key = ssl->tlsext_channel_id_private->pkey.ec; |
| 2144 | BIGNUM *x = BN_new(); |
| 2145 | BIGNUM *y = BN_new(); |
| 2146 | ECDSA_SIG *sig = NULL; |
| 2147 | if (x == NULL || y == NULL || |
| 2148 | !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key), |
| 2149 | EC_KEY_get0_public_key(ec_key), |
| 2150 | x, y, NULL)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2151 | goto err; |
| 2152 | } |
| 2153 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 2154 | uint8_t digest[EVP_MAX_MD_SIZE]; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 2155 | size_t digest_len; |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2156 | if (!tls1_channel_id_hash(ssl, digest, &digest_len)) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2157 | goto err; |
| 2158 | } |
| 2159 | |
David Benjamin | 961ad6a | 2015-06-12 01:40:23 -0400 | [diff] [blame] | 2160 | sig = ECDSA_do_sign(digest, digest_len, ec_key); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2161 | if (sig == NULL) { |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2162 | goto err; |
| 2163 | } |
| 2164 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2165 | CBB cbb, child; |
| 2166 | size_t length; |
| 2167 | CBB_zero(&cbb); |
| 2168 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 2169 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 2170 | !CBB_add_u16(&cbb, TLSEXT_TYPE_channel_id) || |
| 2171 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 2172 | !write_32_byte_big_endian(&child, x) || |
| 2173 | !write_32_byte_big_endian(&child, y) || |
| 2174 | !write_32_byte_big_endian(&child, sig->r) || |
| 2175 | !write_32_byte_big_endian(&child, sig->s) || |
| 2176 | !CBB_finish(&cbb, NULL, &length) || |
| 2177 | !ssl_set_handshake_header(ssl, SSL3_MT_ENCRYPTED_EXTENSIONS, length)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2178 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2179 | CBB_cleanup(&cbb); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2180 | goto err; |
| 2181 | } |
| 2182 | |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2183 | ssl->state = SSL3_ST_CW_CHANNEL_ID_B; |
| 2184 | ret = ssl_do_write(ssl); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2185 | |
| 2186 | err: |
David Benjamin | 96e97b1 | 2015-10-10 16:30:22 -0400 | [diff] [blame] | 2187 | BN_free(x); |
| 2188 | BN_free(y); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2189 | ECDSA_SIG_free(sig); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2190 | return ret; |
| 2191 | } |
| 2192 | |
David Benjamin | fd8e69f | 2015-10-03 10:49:20 -0400 | [diff] [blame] | 2193 | int ssl_do_client_cert_cb(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) { |
| 2194 | if (ssl->ctx->client_cert_cb == NULL) { |
| 2195 | return 0; |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2196 | } |
David Benjamin | fd8e69f | 2015-10-03 10:49:20 -0400 | [diff] [blame] | 2197 | return ssl->ctx->client_cert_cb(ssl, out_x509, out_pkey); |
Adam Langley | 1bea173 | 2014-12-17 19:06:57 -0800 | [diff] [blame] | 2198 | } |
Paul Lietar | 8f1c268 | 2015-08-18 12:21:54 +0100 | [diff] [blame] | 2199 | |
| 2200 | int ssl3_verify_server_cert(SSL *s) { |
| 2201 | int ret = ssl_verify_cert_chain(s, s->session->cert_chain); |
| 2202 | if (s->verify_mode != SSL_VERIFY_NONE && ret <= 0) { |
| 2203 | int al = ssl_verify_alarm_type(s->verify_result); |
| 2204 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2205 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2206 | } else { |
| 2207 | ret = 1; |
| 2208 | ERR_clear_error(); /* but we keep s->verify_result */ |
| 2209 | } |
| 2210 | |
| 2211 | return ret; |
| 2212 | } |