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