Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1 | /* ssl/s3_clnt.c */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 8 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 15 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 22 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * "This product includes cryptographic software written by |
| 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 40 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 52 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] |
| 57 | */ |
Bodo Möller | 8c74b5e | 2002-01-14 23:40:26 +0000 | [diff] [blame] | 58 | /* ==================================================================== |
Bodo Möller | 52b8dad | 2007-02-17 06:45:38 +0000 | [diff] [blame] | 59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
Bodo Möller | 8c74b5e | 2002-01-14 23:40:26 +0000 | [diff] [blame] | 60 | * |
| 61 | * Redistribution and use in source and binary forms, with or without |
| 62 | * modification, are permitted provided that the following conditions |
| 63 | * are met: |
| 64 | * |
| 65 | * 1. Redistributions of source code must retain the above copyright |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 66 | * notice, this list of conditions and the following disclaimer. |
Bodo Möller | 8c74b5e | 2002-01-14 23:40:26 +0000 | [diff] [blame] | 67 | * |
| 68 | * 2. Redistributions in binary form must reproduce the above copyright |
| 69 | * notice, this list of conditions and the following disclaimer in |
| 70 | * the documentation and/or other materials provided with the |
| 71 | * distribution. |
| 72 | * |
| 73 | * 3. All advertising materials mentioning features or use of this |
| 74 | * software must display the following acknowledgment: |
| 75 | * "This product includes software developed by the OpenSSL Project |
| 76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 77 | * |
| 78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 79 | * endorse or promote products derived from this software without |
| 80 | * prior written permission. For written permission, please contact |
| 81 | * openssl-core@openssl.org. |
| 82 | * |
| 83 | * 5. Products derived from this software may not be called "OpenSSL" |
| 84 | * nor may "OpenSSL" appear in their names without prior written |
| 85 | * permission of the OpenSSL Project. |
| 86 | * |
| 87 | * 6. Redistributions of any form whatsoever must retain the following |
| 88 | * acknowledgment: |
| 89 | * "This product includes software developed by the OpenSSL Project |
| 90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 91 | * |
| 92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 103 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 104 | * ==================================================================== |
| 105 | * |
| 106 | * This product includes cryptographic software written by Eric Young |
| 107 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 108 | * Hudson (tjh@cryptsoft.com). |
| 109 | * |
| 110 | */ |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 111 | /* ==================================================================== |
| 112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 113 | * |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 114 | * Portions of the attached software ("Contribution") are developed by |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 115 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 116 | * |
| 117 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 118 | * license provided above. |
| 119 | * |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 120 | * ECC cipher suite support in OpenSSL originally written by |
| 121 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 122 | * |
| 123 | */ |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 124 | /* ==================================================================== |
| 125 | * Copyright 2005 Nokia. All rights reserved. |
| 126 | * |
| 127 | * The portions of the attached software ("Contribution") is developed by |
| 128 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 129 | * license. |
| 130 | * |
| 131 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 132 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 133 | * support (see RFC 4279) to OpenSSL. |
| 134 | * |
| 135 | * No patent licenses or other rights except those expressly stated in |
| 136 | * the OpenSSL open source license shall be deemed granted or received |
| 137 | * expressly, by implication, estoppel, or otherwise. |
| 138 | * |
| 139 | * No assurances are provided by Nokia that the Contribution does not |
| 140 | * infringe the patent or other intellectual property rights of any third |
| 141 | * party or that the license provides you with all the necessary rights |
| 142 | * to make use of the Contribution. |
| 143 | * |
| 144 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 145 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 146 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 147 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 148 | * OTHERWISE. |
| 149 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 150 | |
| 151 | #include <stdio.h> |
Lutz Jänicke | 7b63c0f | 2002-07-10 07:01:54 +0000 | [diff] [blame] | 152 | #include "ssl_locl.h" |
| 153 | #include "kssl_lcl.h" |
Bodo Möller | ec57782 | 1999-04-23 22:13:45 +0000 | [diff] [blame] | 154 | #include <openssl/buffer.h> |
| 155 | #include <openssl/rand.h> |
| 156 | #include <openssl/objects.h> |
| 157 | #include <openssl/evp.h> |
Ben Laurie | dbad169 | 2001-07-30 23:57:25 +0000 | [diff] [blame] | 158 | #include <openssl/md5.h> |
Nils Larsch | 3eeaab4 | 2005-07-16 12:37:36 +0000 | [diff] [blame] | 159 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 160 | # include <openssl/dh.h> |
Nils Larsch | 3eeaab4 | 2005-07-16 12:37:36 +0000 | [diff] [blame] | 161 | #endif |
Geoff Thorpe | d095b68 | 2004-05-17 18:53:47 +0000 | [diff] [blame] | 162 | #include <openssl/bn.h> |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 163 | #ifndef OPENSSL_NO_ENGINE |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 164 | # include <openssl/engine.h> |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 165 | #endif |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 166 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 167 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 168 | #ifndef OPENSSL_NO_TLSEXT |
| 169 | static int ssl3_check_finished(SSL *s); |
| 170 | #endif |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 171 | |
Dr. Stephen Henson | 3881d81 | 2014-10-29 12:51:31 +0000 | [diff] [blame] | 172 | #ifndef OPENSSL_NO_SSL3_METHOD |
Nils Larsch | 4ebb342 | 2005-08-14 21:48:33 +0000 | [diff] [blame] | 173 | static const SSL_METHOD *ssl3_get_client_method(int ver) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 174 | { |
| 175 | if (ver == SSL3_VERSION) |
| 176 | return (SSLv3_client_method()); |
| 177 | else |
| 178 | return (NULL); |
| 179 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 180 | |
Dr. Stephen Henson | f3b656b | 2005-08-05 23:56:11 +0000 | [diff] [blame] | 181 | IMPLEMENT_ssl3_meth_func(SSLv3_client_method, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 182 | ssl_undefined_function, |
| 183 | ssl3_connect, ssl3_get_client_method) |
Dr. Stephen Henson | 3881d81 | 2014-10-29 12:51:31 +0000 | [diff] [blame] | 184 | #endif |
Ulf Möller | 6b691a5 | 1999-04-19 21:31:43 +0000 | [diff] [blame] | 185 | int ssl3_connect(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 186 | { |
| 187 | BUF_MEM *buf = NULL; |
| 188 | unsigned long Time = (unsigned long)time(NULL); |
| 189 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
| 190 | int ret = -1; |
| 191 | int new_state, state, skip = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 192 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 193 | RAND_add(&Time, sizeof(Time), 0); |
| 194 | ERR_clear_error(); |
| 195 | clear_sys_error(); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 196 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 197 | if (s->info_callback != NULL) |
| 198 | cb = s->info_callback; |
| 199 | else if (s->ctx->info_callback != NULL) |
| 200 | cb = s->ctx->info_callback; |
| 201 | |
| 202 | s->in_handshake++; |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 203 | if (!SSL_in_init(s) || SSL_in_before(s)) { |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 204 | if (!SSL_clear(s)) |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 205 | return -1; |
| 206 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 207 | |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 208 | #ifndef OPENSSL_NO_HEARTBEATS |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 209 | /* |
| 210 | * If we're awaiting a HeartbeatResponse, pretend we already got and |
| 211 | * don't await it anymore, because Heartbeats don't make sense during |
| 212 | * handshakes anyway. |
| 213 | */ |
| 214 | if (s->tlsext_hb_pending) { |
| 215 | s->tlsext_hb_pending = 0; |
| 216 | s->tlsext_hb_seq++; |
| 217 | } |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 218 | #endif |
| 219 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 220 | for (;;) { |
| 221 | state = s->state; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 222 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 223 | switch (s->state) { |
| 224 | case SSL_ST_RENEGOTIATE: |
| 225 | s->renegotiate = 1; |
| 226 | s->state = SSL_ST_CONNECT; |
| 227 | s->ctx->stats.sess_connect_renegotiate++; |
| 228 | /* break */ |
| 229 | case SSL_ST_BEFORE: |
| 230 | case SSL_ST_CONNECT: |
| 231 | case SSL_ST_BEFORE | SSL_ST_CONNECT: |
| 232 | case SSL_ST_OK | SSL_ST_CONNECT: |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 233 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 234 | s->server = 0; |
| 235 | if (cb != NULL) |
| 236 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 237 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 238 | if ((s->version & 0xff00) != 0x0300) { |
| 239 | SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 240 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 241 | ret = -1; |
| 242 | goto end; |
| 243 | } |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 244 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 245 | if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) { |
| 246 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW); |
| 247 | return -1; |
| 248 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 249 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 250 | /* s->version=SSL3_VERSION; */ |
| 251 | s->type = SSL_ST_CONNECT; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 252 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 253 | if (s->init_buf == NULL) { |
| 254 | if ((buf = BUF_MEM_new()) == NULL) { |
| 255 | ret = -1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 256 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 257 | goto end; |
| 258 | } |
| 259 | if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 260 | ret = -1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 261 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 262 | goto end; |
| 263 | } |
| 264 | s->init_buf = buf; |
| 265 | buf = NULL; |
| 266 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 267 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 268 | if (!ssl3_setup_buffers(s)) { |
| 269 | ret = -1; |
| 270 | goto end; |
| 271 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 272 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 273 | /* setup buffing BIO */ |
| 274 | if (!ssl_init_wbio_buffer(s, 0)) { |
| 275 | ret = -1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 276 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 277 | goto end; |
| 278 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 279 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 280 | /* don't push the buffering BIO quite yet */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 281 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 282 | ssl3_init_finished_mac(s); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 283 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 284 | s->state = SSL3_ST_CW_CLNT_HELLO_A; |
| 285 | s->ctx->stats.sess_connect++; |
| 286 | s->init_num = 0; |
| 287 | s->s3->flags &= ~SSL3_FLAGS_CCS_OK; |
| 288 | /* |
| 289 | * Should have been reset by ssl3_get_finished, too. |
| 290 | */ |
| 291 | s->s3->change_cipher_spec = 0; |
| 292 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 293 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 294 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 295 | case SSL3_ST_CW_CLNT_HELLO_B: |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 296 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 297 | s->shutdown = 0; |
| 298 | ret = ssl3_client_hello(s); |
| 299 | if (ret <= 0) |
| 300 | goto end; |
| 301 | s->state = SSL3_ST_CR_SRVR_HELLO_A; |
| 302 | s->init_num = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 303 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 304 | /* turn on buffering for the next lot of output */ |
| 305 | if (s->bbio != s->wbio) |
| 306 | s->wbio = BIO_push(s->bbio, s->wbio); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 307 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 308 | break; |
Bodo Möller | 241520e | 2006-01-11 06:10:40 +0000 | [diff] [blame] | 309 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 310 | case SSL3_ST_CR_SRVR_HELLO_A: |
| 311 | case SSL3_ST_CR_SRVR_HELLO_B: |
| 312 | ret = ssl3_get_server_hello(s); |
| 313 | if (ret <= 0) |
| 314 | goto end; |
| 315 | |
| 316 | if (s->hit) { |
| 317 | s->state = SSL3_ST_CR_FINISHED_A; |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 318 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 319 | if (s->tlsext_ticket_expected) { |
| 320 | /* receive renewed session ticket */ |
| 321 | s->state = SSL3_ST_CR_SESSION_TICKET_A; |
| 322 | } |
Bodo Möller | c519e89 | 2011-09-05 13:36:23 +0000 | [diff] [blame] | 323 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 324 | } else { |
| 325 | s->state = SSL3_ST_CR_CERT_A; |
| 326 | } |
| 327 | s->init_num = 0; |
| 328 | break; |
| 329 | case SSL3_ST_CR_CERT_A: |
| 330 | case SSL3_ST_CR_CERT_B: |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 331 | #ifndef OPENSSL_NO_TLSEXT |
| 332 | /* Noop (ret = 0) for everything but EAP-FAST. */ |
| 333 | ret = ssl3_check_finished(s); |
| 334 | if (ret < 0) |
| 335 | goto end; |
| 336 | if (ret == 1) { |
| 337 | s->hit = 1; |
| 338 | s->state = SSL3_ST_CR_FINISHED_A; |
| 339 | s->init_num = 0; |
| 340 | break; |
| 341 | } |
| 342 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 343 | /* Check if it is anon DH/ECDH, SRP auth */ |
| 344 | /* or PSK */ |
| 345 | if (! |
| 346 | (s->s3->tmp. |
| 347 | new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) |
| 348 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 349 | ret = ssl3_get_server_certificate(s); |
| 350 | if (ret <= 0) |
| 351 | goto end; |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 352 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 353 | if (s->tlsext_status_expected) |
| 354 | s->state = SSL3_ST_CR_CERT_STATUS_A; |
| 355 | else |
| 356 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 357 | } else { |
| 358 | skip = 1; |
| 359 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 360 | } |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 361 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 362 | } else |
| 363 | skip = 1; |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 364 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 365 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 366 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 367 | s->init_num = 0; |
| 368 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 369 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 370 | case SSL3_ST_CR_KEY_EXCH_A: |
| 371 | case SSL3_ST_CR_KEY_EXCH_B: |
| 372 | ret = ssl3_get_key_exchange(s); |
| 373 | if (ret <= 0) |
| 374 | goto end; |
| 375 | s->state = SSL3_ST_CR_CERT_REQ_A; |
| 376 | s->init_num = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 377 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 378 | /* |
| 379 | * at this point we check that we have the required stuff from |
| 380 | * the server |
| 381 | */ |
| 382 | if (!ssl3_check_cert_and_algorithm(s)) { |
| 383 | ret = -1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 384 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 385 | goto end; |
| 386 | } |
| 387 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 388 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 389 | case SSL3_ST_CR_CERT_REQ_A: |
| 390 | case SSL3_ST_CR_CERT_REQ_B: |
| 391 | ret = ssl3_get_certificate_request(s); |
| 392 | if (ret <= 0) |
| 393 | goto end; |
| 394 | s->state = SSL3_ST_CR_SRVR_DONE_A; |
| 395 | s->init_num = 0; |
| 396 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 397 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 398 | case SSL3_ST_CR_SRVR_DONE_A: |
| 399 | case SSL3_ST_CR_SRVR_DONE_B: |
| 400 | ret = ssl3_get_server_done(s); |
| 401 | if (ret <= 0) |
| 402 | goto end; |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 403 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 404 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
| 405 | if ((ret = SRP_Calc_A_param(s)) <= 0) { |
| 406 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC); |
| 407 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 408 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 409 | goto end; |
| 410 | } |
| 411 | } |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 412 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 413 | if (s->s3->tmp.cert_req) |
| 414 | s->state = SSL3_ST_CW_CERT_A; |
| 415 | else |
| 416 | s->state = SSL3_ST_CW_KEY_EXCH_A; |
| 417 | s->init_num = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 418 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 419 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 420 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 421 | case SSL3_ST_CW_CERT_A: |
| 422 | case SSL3_ST_CW_CERT_B: |
| 423 | case SSL3_ST_CW_CERT_C: |
| 424 | case SSL3_ST_CW_CERT_D: |
| 425 | ret = ssl3_send_client_certificate(s); |
| 426 | if (ret <= 0) |
| 427 | goto end; |
| 428 | s->state = SSL3_ST_CW_KEY_EXCH_A; |
| 429 | s->init_num = 0; |
| 430 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 431 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 432 | case SSL3_ST_CW_KEY_EXCH_A: |
| 433 | case SSL3_ST_CW_KEY_EXCH_B: |
| 434 | ret = ssl3_send_client_key_exchange(s); |
| 435 | if (ret <= 0) |
| 436 | goto end; |
| 437 | /* |
| 438 | * EAY EAY EAY need to check for DH fix cert sent back |
| 439 | */ |
| 440 | /* |
| 441 | * For TLS, cert_req is set to 2, so a cert chain of nothing is |
| 442 | * sent, but no verify packet is sent |
| 443 | */ |
| 444 | /* |
| 445 | * XXX: For now, we do not support client authentication in ECDH |
| 446 | * cipher suites with ECDH (rather than ECDSA) certificates. We |
| 447 | * need to skip the certificate verify message when client's |
| 448 | * ECDH public key is sent inside the client certificate. |
| 449 | */ |
| 450 | if (s->s3->tmp.cert_req == 1) { |
| 451 | s->state = SSL3_ST_CW_CERT_VRFY_A; |
| 452 | } else { |
| 453 | s->state = SSL3_ST_CW_CHANGE_A; |
| 454 | } |
| 455 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { |
| 456 | s->state = SSL3_ST_CW_CHANGE_A; |
| 457 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 458 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 459 | s->init_num = 0; |
| 460 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 461 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 462 | case SSL3_ST_CW_CERT_VRFY_A: |
| 463 | case SSL3_ST_CW_CERT_VRFY_B: |
| 464 | ret = ssl3_send_client_verify(s); |
| 465 | if (ret <= 0) |
| 466 | goto end; |
| 467 | s->state = SSL3_ST_CW_CHANGE_A; |
| 468 | s->init_num = 0; |
| 469 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 470 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 471 | case SSL3_ST_CW_CHANGE_A: |
| 472 | case SSL3_ST_CW_CHANGE_B: |
| 473 | ret = ssl3_send_change_cipher_spec(s, |
| 474 | SSL3_ST_CW_CHANGE_A, |
| 475 | SSL3_ST_CW_CHANGE_B); |
| 476 | if (ret <= 0) |
| 477 | goto end; |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 478 | |
Ben Laurie | bf48836 | 2010-09-05 17:14:01 +0000 | [diff] [blame] | 479 | #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 480 | s->state = SSL3_ST_CW_FINISHED_A; |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 481 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 482 | if (s->s3->next_proto_neg_seen) |
| 483 | s->state = SSL3_ST_CW_NEXT_PROTO_A; |
| 484 | else |
| 485 | s->state = SSL3_ST_CW_FINISHED_A; |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 486 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 487 | s->init_num = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 488 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 489 | s->session->cipher = s->s3->tmp.new_cipher; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 490 | #ifdef OPENSSL_NO_COMP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 491 | s->session->compress_meth = 0; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 492 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 493 | if (s->s3->tmp.new_compression == NULL) |
| 494 | s->session->compress_meth = 0; |
| 495 | else |
| 496 | s->session->compress_meth = s->s3->tmp.new_compression->id; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 497 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 498 | if (!s->method->ssl3_enc->setup_key_block(s)) { |
| 499 | ret = -1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 500 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 501 | goto end; |
| 502 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 503 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 504 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 505 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 506 | { |
| 507 | ret = -1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 508 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 509 | goto end; |
| 510 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 511 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 512 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 513 | |
Ben Laurie | bf48836 | 2010-09-05 17:14:01 +0000 | [diff] [blame] | 514 | #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 515 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 516 | case SSL3_ST_CW_NEXT_PROTO_B: |
| 517 | ret = ssl3_send_next_proto(s); |
| 518 | if (ret <= 0) |
| 519 | goto end; |
| 520 | s->state = SSL3_ST_CW_FINISHED_A; |
| 521 | break; |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 522 | #endif |
| 523 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 524 | case SSL3_ST_CW_FINISHED_A: |
| 525 | case SSL3_ST_CW_FINISHED_B: |
| 526 | ret = ssl3_send_finished(s, |
| 527 | SSL3_ST_CW_FINISHED_A, |
| 528 | SSL3_ST_CW_FINISHED_B, |
| 529 | s->method-> |
| 530 | ssl3_enc->client_finished_label, |
| 531 | s->method-> |
| 532 | ssl3_enc->client_finished_label_len); |
| 533 | if (ret <= 0) |
| 534 | goto end; |
| 535 | s->state = SSL3_ST_CW_FLUSH; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 536 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 537 | /* clear flags */ |
| 538 | s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; |
| 539 | if (s->hit) { |
| 540 | s->s3->tmp.next_state = SSL_ST_OK; |
| 541 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) { |
| 542 | s->state = SSL_ST_OK; |
| 543 | s->s3->flags |= SSL3_FLAGS_POP_BUFFER; |
| 544 | s->s3->delay_buf_pop_ret = 0; |
| 545 | } |
| 546 | } else { |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 547 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 548 | /* |
| 549 | * Allow NewSessionTicket if ticket expected |
| 550 | */ |
| 551 | if (s->tlsext_ticket_expected) |
| 552 | s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
| 553 | else |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 554 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 555 | |
| 556 | s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; |
| 557 | } |
| 558 | s->init_num = 0; |
| 559 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 560 | |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 561 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 562 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 563 | case SSL3_ST_CR_SESSION_TICKET_B: |
| 564 | ret = ssl3_get_new_session_ticket(s); |
| 565 | if (ret <= 0) |
| 566 | goto end; |
| 567 | s->state = SSL3_ST_CR_FINISHED_A; |
| 568 | s->init_num = 0; |
| 569 | break; |
Dr. Stephen Henson | 67c8e7f | 2007-09-26 21:56:59 +0000 | [diff] [blame] | 570 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 571 | case SSL3_ST_CR_CERT_STATUS_A: |
| 572 | case SSL3_ST_CR_CERT_STATUS_B: |
| 573 | ret = ssl3_get_cert_status(s); |
| 574 | if (ret <= 0) |
| 575 | goto end; |
| 576 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 577 | s->init_num = 0; |
| 578 | break; |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 579 | #endif |
| 580 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 581 | case SSL3_ST_CR_FINISHED_A: |
| 582 | case SSL3_ST_CR_FINISHED_B: |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 583 | if (!s->s3->change_cipher_spec) |
| 584 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 585 | ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, |
| 586 | SSL3_ST_CR_FINISHED_B); |
| 587 | if (ret <= 0) |
| 588 | goto end; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 589 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 590 | if (s->hit) |
| 591 | s->state = SSL3_ST_CW_CHANGE_A; |
| 592 | else |
| 593 | s->state = SSL_ST_OK; |
| 594 | s->init_num = 0; |
| 595 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 596 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 597 | case SSL3_ST_CW_FLUSH: |
| 598 | s->rwstate = SSL_WRITING; |
| 599 | if (BIO_flush(s->wbio) <= 0) { |
| 600 | ret = -1; |
| 601 | goto end; |
| 602 | } |
| 603 | s->rwstate = SSL_NOTHING; |
| 604 | s->state = s->s3->tmp.next_state; |
| 605 | break; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 606 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 607 | case SSL_ST_OK: |
| 608 | /* clean a few things up */ |
| 609 | ssl3_cleanup_key_block(s); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 610 | BUF_MEM_free(s->init_buf); |
| 611 | s->init_buf = NULL; |
Mark J. Cox | 413c4f4 | 1999-02-16 09:22:21 +0000 | [diff] [blame] | 612 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 613 | /* |
| 614 | * If we are not 'joining' the last two packets, remove the |
| 615 | * buffering now |
| 616 | */ |
| 617 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) |
| 618 | ssl_free_wbio_buffer(s); |
| 619 | /* else do it later in ssl3_write */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 620 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 621 | s->init_num = 0; |
| 622 | s->renegotiate = 0; |
| 623 | s->new_session = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 624 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 625 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
| 626 | if (s->hit) |
| 627 | s->ctx->stats.sess_hit++; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 628 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 629 | ret = 1; |
| 630 | /* s->server=0; */ |
| 631 | s->handshake_func = ssl3_connect; |
| 632 | s->ctx->stats.sess_connect_good++; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 633 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 634 | if (cb != NULL) |
| 635 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 636 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 637 | goto end; |
| 638 | /* break; */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 639 | |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 640 | case SSL_ST_ERR: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 641 | default: |
| 642 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE); |
| 643 | ret = -1; |
| 644 | goto end; |
| 645 | /* break; */ |
| 646 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 647 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 648 | /* did we do anything */ |
| 649 | if (!s->s3->tmp.reuse_message && !skip) { |
| 650 | if (s->debug) { |
| 651 | if ((ret = BIO_flush(s->wbio)) <= 0) |
| 652 | goto end; |
| 653 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 654 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 655 | if ((cb != NULL) && (s->state != state)) { |
| 656 | new_state = s->state; |
| 657 | s->state = state; |
| 658 | cb(s, SSL_CB_CONNECT_LOOP, 1); |
| 659 | s->state = new_state; |
| 660 | } |
| 661 | } |
| 662 | skip = 0; |
| 663 | } |
| 664 | end: |
| 665 | s->in_handshake--; |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 666 | BUF_MEM_free(buf); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 667 | if (cb != NULL) |
| 668 | cb(s, SSL_CB_CONNECT_EXIT, ret); |
| 669 | return (ret); |
| 670 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 671 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 672 | int ssl3_client_hello(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 673 | { |
| 674 | unsigned char *buf; |
| 675 | unsigned char *p, *d; |
| 676 | int i; |
| 677 | unsigned long l; |
| 678 | int al = 0; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 679 | #ifndef OPENSSL_NO_COMP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 680 | int j; |
| 681 | SSL_COMP *comp; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 682 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 683 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 684 | buf = (unsigned char *)s->init_buf->data; |
| 685 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) { |
| 686 | SSL_SESSION *sess = s->session; |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 687 | if ((sess == NULL) || (sess->ssl_version != s->version) || |
| 688 | #ifdef OPENSSL_NO_TLSEXT |
| 689 | !sess->session_id_length || |
| 690 | #else |
| 691 | /* |
| 692 | * In the case of EAP-FAST, we can have a pre-shared |
| 693 | * "ticket" without a session ID. |
| 694 | */ |
| 695 | (!sess->session_id_length && !sess->tlsext_tick) || |
| 696 | #endif |
| 697 | (sess->not_resumable)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 698 | if (!ssl_get_new_session(s, 0)) |
| 699 | goto err; |
| 700 | } |
| 701 | if (s->method->version == DTLS_ANY_VERSION) { |
| 702 | /* Determine which DTLS version to use */ |
| 703 | int options = s->options; |
| 704 | /* If DTLS 1.2 disabled correct the version number */ |
| 705 | if (options & SSL_OP_NO_DTLSv1_2) { |
| 706 | if (tls1_suiteb(s)) { |
| 707 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, |
| 708 | SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); |
| 709 | goto err; |
| 710 | } |
| 711 | /* |
| 712 | * Disabling all versions is silly: return an error. |
| 713 | */ |
| 714 | if (options & SSL_OP_NO_DTLSv1) { |
| 715 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION); |
| 716 | goto err; |
| 717 | } |
| 718 | /* |
| 719 | * Update method so we don't use any DTLS 1.2 features. |
| 720 | */ |
| 721 | s->method = DTLSv1_client_method(); |
| 722 | s->version = DTLS1_VERSION; |
| 723 | } else { |
| 724 | /* |
| 725 | * We only support one version: update method |
| 726 | */ |
| 727 | if (options & SSL_OP_NO_DTLSv1) |
| 728 | s->method = DTLSv1_2_client_method(); |
| 729 | s->version = DTLS1_2_VERSION; |
| 730 | } |
| 731 | s->client_version = s->version; |
| 732 | } |
| 733 | /* else use the pre-loaded session */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 734 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 735 | p = s->s3->client_random; |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 736 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 737 | /* |
| 738 | * for DTLS if client_random is initialized, reuse it, we are |
| 739 | * required to use same upon reply to HelloVerify |
| 740 | */ |
| 741 | if (SSL_IS_DTLS(s)) { |
| 742 | size_t idx; |
| 743 | i = 1; |
| 744 | for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { |
| 745 | if (p[idx]) { |
| 746 | i = 0; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | } else |
| 751 | i = 1; |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 752 | |
Matt Caswell | e1b568d | 2015-02-26 11:56:00 +0000 | [diff] [blame] | 753 | if (i && ssl_fill_hello_random(s, 0, p, |
| 754 | sizeof(s->s3->client_random)) <= 0) |
| 755 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 756 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 757 | /* Do the message type and length last */ |
| 758 | d = p = ssl_handshake_start(s); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 759 | |
Matt Caswell | 35a1cc9 | 2015-01-17 00:06:54 +0000 | [diff] [blame] | 760 | /*- |
| 761 | * version indicates the negotiated version: for example from |
| 762 | * an SSLv2/v3 compatible client hello). The client_version |
| 763 | * field is the maximum version we permit and it is also |
| 764 | * used in RSA encrypted premaster secrets. Some servers can |
| 765 | * choke if we initially report a higher version then |
| 766 | * renegotiate to a lower one in the premaster secret. This |
| 767 | * didn't happen with TLS 1.0 as most servers supported it |
| 768 | * but it can with TLS 1.1 or later if the server only supports |
| 769 | * 1.0. |
| 770 | * |
| 771 | * Possible scenario with previous logic: |
| 772 | * 1. Client hello indicates TLS 1.2 |
| 773 | * 2. Server hello says TLS 1.0 |
| 774 | * 3. RSA encrypted premaster secret uses 1.2. |
| 775 | * 4. Handhaked proceeds using TLS 1.0. |
| 776 | * 5. Server sends hello request to renegotiate. |
| 777 | * 6. Client hello indicates TLS v1.0 as we now |
| 778 | * know that is maximum server supports. |
| 779 | * 7. Server chokes on RSA encrypted premaster secret |
| 780 | * containing version 1.0. |
| 781 | * |
| 782 | * For interoperability it should be OK to always use the |
| 783 | * maximum version we support in client hello and then rely |
| 784 | * on the checking of version to ensure the servers isn't |
| 785 | * being inconsistent: for example initially negotiating with |
| 786 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using |
| 787 | * client_version in client hello and not resetting it to |
| 788 | * the negotiated version. |
| 789 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 790 | *(p++) = s->client_version >> 8; |
| 791 | *(p++) = s->client_version & 0xff; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 792 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 793 | /* Random stuff */ |
| 794 | memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); |
| 795 | p += SSL3_RANDOM_SIZE; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 796 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 797 | /* Session ID */ |
| 798 | if (s->new_session) |
| 799 | i = 0; |
| 800 | else |
| 801 | i = s->session->session_id_length; |
| 802 | *(p++) = i; |
| 803 | if (i != 0) { |
| 804 | if (i > (int)sizeof(s->session->session_id)) { |
| 805 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
| 806 | goto err; |
| 807 | } |
| 808 | memcpy(p, s->session->session_id, i); |
| 809 | p += i; |
| 810 | } |
| 811 | |
| 812 | /* cookie stuff for DTLS */ |
| 813 | if (SSL_IS_DTLS(s)) { |
| 814 | if (s->d1->cookie_len > sizeof(s->d1->cookie)) { |
| 815 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
| 816 | goto err; |
| 817 | } |
| 818 | *(p++) = s->d1->cookie_len; |
| 819 | memcpy(p, s->d1->cookie, s->d1->cookie_len); |
| 820 | p += s->d1->cookie_len; |
| 821 | } |
| 822 | |
| 823 | /* Ciphers supported */ |
| 824 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0); |
| 825 | if (i == 0) { |
| 826 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); |
| 827 | goto err; |
| 828 | } |
Dr. Stephen Henson | 800e1cd | 2012-04-17 15:12:09 +0000 | [diff] [blame] | 829 | #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 830 | /* |
| 831 | * Some servers hang if client hello > 256 bytes as hack workaround |
| 832 | * chop number of supported ciphers to keep it well below this if we |
| 833 | * use TLS v1.2 |
| 834 | */ |
| 835 | if (TLS1_get_version(s) >= TLS1_2_VERSION |
| 836 | && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) |
| 837 | i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; |
Dr. Stephen Henson | 800e1cd | 2012-04-17 15:12:09 +0000 | [diff] [blame] | 838 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 839 | s2n(i, p); |
| 840 | p += i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 841 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 842 | /* COMPRESSION */ |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 843 | #ifdef OPENSSL_NO_COMP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 844 | *(p++) = 1; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 845 | #else |
Dr. Stephen Henson | 566dda0 | 2005-10-08 00:18:53 +0000 | [diff] [blame] | 846 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 847 | if (!ssl_allow_compression(s) || !s->ctx->comp_methods) |
| 848 | j = 0; |
| 849 | else |
| 850 | j = sk_SSL_COMP_num(s->ctx->comp_methods); |
| 851 | *(p++) = 1 + j; |
| 852 | for (i = 0; i < j; i++) { |
| 853 | comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); |
| 854 | *(p++) = comp->id; |
| 855 | } |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 856 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 857 | *(p++) = 0; /* Add the NULL method */ |
Bodo Möller | 761772d | 2007-09-21 06:54:24 +0000 | [diff] [blame] | 858 | |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 859 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 860 | /* TLS extensions */ |
| 861 | if (ssl_prepare_clienthello_tlsext(s) <= 0) { |
| 862 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); |
| 863 | goto err; |
| 864 | } |
| 865 | if ((p = |
| 866 | ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, |
| 867 | &al)) == NULL) { |
| 868 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 869 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
| 870 | goto err; |
| 871 | } |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 872 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 873 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 874 | l = p - d; |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 875 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) { |
Matt Caswell | 77d514c | 2015-03-09 15:33:46 +0000 | [diff] [blame] | 876 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 877 | SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); |
| 878 | goto err; |
| 879 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 880 | s->state = SSL3_ST_CW_CLNT_HELLO_B; |
| 881 | } |
| 882 | |
| 883 | /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 884 | return ssl_do_write(s); |
| 885 | err: |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 886 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 887 | return (-1); |
| 888 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 889 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 890 | int ssl3_get_server_hello(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 891 | { |
| 892 | STACK_OF(SSL_CIPHER) *sk; |
| 893 | const SSL_CIPHER *c; |
| 894 | CERT *ct = s->cert; |
| 895 | unsigned char *p, *d; |
| 896 | int i, al = SSL_AD_INTERNAL_ERROR, ok; |
| 897 | unsigned int j; |
| 898 | long n; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 899 | #ifndef OPENSSL_NO_COMP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 900 | SSL_COMP *comp; |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 901 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 902 | /* |
| 903 | * Hello verify request and/or server hello version may not match so set |
| 904 | * first packet if we're negotiating version. |
| 905 | */ |
| 906 | if (SSL_IS_DTLS(s)) |
| 907 | s->first_packet = 1; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 908 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 909 | n = s->method->ssl_get_message(s, |
| 910 | SSL3_ST_CR_SRVR_HELLO_A, |
| 911 | SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 912 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 913 | if (!ok) |
| 914 | return ((int)n); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 915 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 916 | if (SSL_IS_DTLS(s)) { |
| 917 | s->first_packet = 0; |
| 918 | if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 919 | if (s->d1->send_cookie == 0) { |
| 920 | s->s3->tmp.reuse_message = 1; |
| 921 | return 1; |
| 922 | } else { /* already sent a cookie */ |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 923 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 924 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 925 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); |
| 926 | goto f_err; |
| 927 | } |
| 928 | } |
| 929 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 930 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 931 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { |
| 932 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 933 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); |
| 934 | goto f_err; |
| 935 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 936 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 937 | d = p = (unsigned char *)s->init_msg; |
| 938 | if (s->method->version == DTLS_ANY_VERSION) { |
| 939 | /* Work out correct protocol version to use */ |
| 940 | int hversion = (p[0] << 8) | p[1]; |
| 941 | int options = s->options; |
| 942 | if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2)) |
| 943 | s->method = DTLSv1_2_client_method(); |
| 944 | else if (tls1_suiteb(s)) { |
| 945 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, |
| 946 | SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); |
| 947 | s->version = hversion; |
| 948 | al = SSL_AD_PROTOCOL_VERSION; |
| 949 | goto f_err; |
| 950 | } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1)) |
| 951 | s->method = DTLSv1_client_method(); |
| 952 | else { |
| 953 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); |
| 954 | s->version = hversion; |
| 955 | al = SSL_AD_PROTOCOL_VERSION; |
| 956 | goto f_err; |
| 957 | } |
| 958 | s->version = s->method->version; |
| 959 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 960 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 961 | if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) { |
| 962 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); |
| 963 | s->version = (s->version & 0xff00) | p[1]; |
| 964 | al = SSL_AD_PROTOCOL_VERSION; |
| 965 | goto f_err; |
| 966 | } |
| 967 | p += 2; |
Emilia Kasper | 7b3ba508 | 2014-11-19 15:56:27 +0100 | [diff] [blame] | 968 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 969 | /* load the server hello data */ |
| 970 | /* load the server random */ |
| 971 | memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE); |
| 972 | p += SSL3_RANDOM_SIZE; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 973 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 974 | s->hit = 0; |
Ben Laurie | 54a656e | 2002-11-13 15:43:43 +0000 | [diff] [blame] | 975 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 976 | /* get the session-id */ |
| 977 | j = *(p++); |
| 978 | |
| 979 | if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) { |
| 980 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 981 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); |
| 982 | goto f_err; |
| 983 | } |
Dr. Stephen Henson | 12bf56c | 2008-11-15 17:18:12 +0000 | [diff] [blame] | 984 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 985 | /* |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 986 | * Check if we can resume the session based on external pre-shared secret. |
| 987 | * EAP-FAST (RFC 4851) supports two types of session resumption. |
| 988 | * Resumption based on server-side state works with session IDs. |
| 989 | * Resumption based on pre-shared Protected Access Credentials (PACs) |
| 990 | * works by overriding the SessionTicket extension at the application |
| 991 | * layer, and does not send a session ID. (We do not know whether EAP-FAST |
| 992 | * servers would honour the session ID.) Therefore, the session ID alone |
| 993 | * is not a reliable indicator of session resumption, so we first check if |
| 994 | * we can resume, and later peek at the next handshake message to see if the |
| 995 | * server wants to resume. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 996 | */ |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 997 | if (s->version >= TLS1_VERSION && s->tls_session_secret_cb && |
| 998 | s->session->tlsext_tick) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 999 | SSL_CIPHER *pref_cipher = NULL; |
| 1000 | s->session->master_key_length = sizeof(s->session->master_key); |
| 1001 | if (s->tls_session_secret_cb(s, s->session->master_key, |
| 1002 | &s->session->master_key_length, |
| 1003 | NULL, &pref_cipher, |
| 1004 | s->tls_session_secret_cb_arg)) { |
| 1005 | s->session->cipher = pref_cipher ? |
| 1006 | pref_cipher : ssl_get_cipher_by_char(s, p + j); |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1007 | } else { |
| 1008 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); |
| 1009 | al = SSL_AD_INTERNAL_ERROR; |
| 1010 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | #endif /* OPENSSL_NO_TLSEXT */ |
Dr. Stephen Henson | 12bf56c | 2008-11-15 17:18:12 +0000 | [diff] [blame] | 1014 | |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1015 | if (j != 0 && j == s->session->session_id_length |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1016 | && memcmp(p, s->session->session_id, j) == 0) { |
| 1017 | if (s->sid_ctx_length != s->session->sid_ctx_length |
| 1018 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { |
| 1019 | /* actually a client application bug */ |
| 1020 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1021 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, |
| 1022 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 1023 | goto f_err; |
| 1024 | } |
| 1025 | s->hit = 1; |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1026 | } else { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1027 | /* |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 1028 | * If we were trying for session-id reuse but the server |
| 1029 | * didn't echo the ID, make a new SSL_SESSION. |
| 1030 | * In the case of EAP-FAST and PAC, we do not send a session ID, |
| 1031 | * so the PAC-based session secret is always preserved. It'll be |
| 1032 | * overwritten if the server refuses resumption. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1033 | */ |
| 1034 | if (s->session->session_id_length > 0) { |
| 1035 | if (!ssl_get_new_session(s, 0)) { |
| 1036 | goto f_err; |
| 1037 | } |
| 1038 | } |
| 1039 | s->session->session_id_length = j; |
| 1040 | memcpy(s->session->session_id, p, j); /* j could be 0 */ |
| 1041 | } |
| 1042 | p += j; |
| 1043 | c = ssl_get_cipher_by_char(s, p); |
| 1044 | if (c == NULL) { |
| 1045 | /* unknown cipher */ |
| 1046 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1047 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 1048 | goto f_err; |
| 1049 | } |
| 1050 | /* Set version disabled mask now we know version */ |
| 1051 | if (!SSL_USE_TLS1_2_CIPHERS(s)) |
| 1052 | ct->mask_ssl = SSL_TLSV1_2; |
| 1053 | else |
| 1054 | ct->mask_ssl = 0; |
| 1055 | /* |
| 1056 | * If it is a disabled cipher we didn't send it in client hello, so |
| 1057 | * return an error. |
| 1058 | */ |
| 1059 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) { |
| 1060 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1061 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
| 1062 | goto f_err; |
| 1063 | } |
| 1064 | p += ssl_put_cipher_by_char(s, NULL, NULL); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1065 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1066 | sk = ssl_get_ciphers_by_id(s); |
| 1067 | i = sk_SSL_CIPHER_find(sk, c); |
| 1068 | if (i < 0) { |
| 1069 | /* we did not say we would use this cipher */ |
| 1070 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1071 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); |
| 1072 | goto f_err; |
| 1073 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1074 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1075 | /* |
| 1076 | * Depending on the session caching (internal/external), the cipher |
| 1077 | * and/or cipher_id values may not be set. Make sure that cipher_id is |
| 1078 | * set and use it for comparison. |
| 1079 | */ |
| 1080 | if (s->session->cipher) |
| 1081 | s->session->cipher_id = s->session->cipher->id; |
| 1082 | if (s->hit && (s->session->cipher_id != c->id)) { |
Rich Salz | 9e9858d | 2015-02-06 10:52:12 -0500 | [diff] [blame] | 1083 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1084 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, |
| 1085 | SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 1086 | goto f_err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1087 | } |
| 1088 | s->s3->tmp.new_cipher = c; |
| 1089 | /* |
| 1090 | * Don't digest cached records if no sigalgs: we may need them for client |
| 1091 | * authentication. |
| 1092 | */ |
| 1093 | if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s)) |
| 1094 | goto f_err; |
| 1095 | /* lets get the compression algorithm */ |
| 1096 | /* COMPRESSION */ |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 1097 | #ifdef OPENSSL_NO_COMP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1098 | if (*(p++) != 0) { |
| 1099 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1100 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, |
| 1101 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1102 | goto f_err; |
| 1103 | } |
| 1104 | /* |
| 1105 | * If compression is disabled we'd better not try to resume a session |
| 1106 | * using compression. |
| 1107 | */ |
| 1108 | if (s->session->compress_meth != 0) { |
| 1109 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); |
| 1110 | goto f_err; |
| 1111 | } |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 1112 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1113 | j = *(p++); |
| 1114 | if (s->hit && j != s->session->compress_meth) { |
| 1115 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1116 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, |
| 1117 | SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
| 1118 | goto f_err; |
| 1119 | } |
| 1120 | if (j == 0) |
| 1121 | comp = NULL; |
| 1122 | else if (!ssl_allow_compression(s)) { |
| 1123 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1124 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); |
| 1125 | goto f_err; |
| 1126 | } else |
| 1127 | comp = ssl3_comp_find(s->ctx->comp_methods, j); |
| 1128 | |
| 1129 | if ((j != 0) && (comp == NULL)) { |
| 1130 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1131 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, |
| 1132 | SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1133 | goto f_err; |
| 1134 | } else { |
| 1135 | s->s3->tmp.new_compression = comp; |
| 1136 | } |
Dr. Stephen Henson | 09b6c2e | 2005-09-30 23:35:33 +0000 | [diff] [blame] | 1137 | #endif |
Bodo Möller | 761772d | 2007-09-21 06:54:24 +0000 | [diff] [blame] | 1138 | |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1139 | #ifndef OPENSSL_NO_TLSEXT |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1140 | /* TLS extensions */ |
| 1141 | if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) { |
| 1142 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT); |
| 1143 | goto err; |
| 1144 | } |
Bodo Möller | ed3883d | 2006-01-02 23:14:37 +0000 | [diff] [blame] | 1145 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1146 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1147 | if (p != (d + n)) { |
| 1148 | /* wrong packet length */ |
| 1149 | al = SSL_AD_DECODE_ERROR; |
| 1150 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); |
| 1151 | goto f_err; |
| 1152 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1153 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1154 | return (1); |
| 1155 | f_err: |
| 1156 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1157 | err: |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1158 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1159 | return (-1); |
| 1160 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1161 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 1162 | int ssl3_get_server_certificate(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1163 | { |
| 1164 | int al, i, ok, ret = -1; |
| 1165 | unsigned long n, nc, llen, l; |
| 1166 | X509 *x = NULL; |
| 1167 | const unsigned char *q, *p; |
| 1168 | unsigned char *d; |
| 1169 | STACK_OF(X509) *sk = NULL; |
| 1170 | SESS_CERT *sc; |
| 1171 | EVP_PKEY *pkey = NULL; |
| 1172 | int need_cert = 1; /* VRS: 0=> will allow null cert if auth == |
| 1173 | * KRB5 */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1174 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1175 | n = s->method->ssl_get_message(s, |
| 1176 | SSL3_ST_CR_CERT_A, |
| 1177 | SSL3_ST_CR_CERT_B, |
| 1178 | -1, s->max_cert_list, &ok); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1179 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1180 | if (!ok) |
| 1181 | return ((int)n); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1182 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1183 | if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || |
| 1184 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && |
| 1185 | (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) { |
| 1186 | s->s3->tmp.reuse_message = 1; |
| 1187 | return (1); |
| 1188 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1189 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1190 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 1191 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1192 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE); |
| 1193 | goto f_err; |
| 1194 | } |
| 1195 | p = d = (unsigned char *)s->init_msg; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1196 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1197 | if ((sk = sk_X509_new_null()) == NULL) { |
| 1198 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1199 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1200 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1201 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1202 | n2l3(p, llen); |
| 1203 | if (llen + 3 != n) { |
| 1204 | al = SSL_AD_DECODE_ERROR; |
| 1205 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); |
| 1206 | goto f_err; |
| 1207 | } |
| 1208 | for (nc = 0; nc < llen;) { |
| 1209 | n2l3(p, l); |
| 1210 | if ((l + nc + 3) > llen) { |
| 1211 | al = SSL_AD_DECODE_ERROR; |
| 1212 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1213 | SSL_R_CERT_LENGTH_MISMATCH); |
| 1214 | goto f_err; |
| 1215 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1216 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1217 | q = p; |
| 1218 | x = d2i_X509(NULL, &q, l); |
| 1219 | if (x == NULL) { |
| 1220 | al = SSL_AD_BAD_CERTIFICATE; |
| 1221 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); |
| 1222 | goto f_err; |
| 1223 | } |
| 1224 | if (q != (p + l)) { |
| 1225 | al = SSL_AD_DECODE_ERROR; |
| 1226 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1227 | SSL_R_CERT_LENGTH_MISMATCH); |
| 1228 | goto f_err; |
| 1229 | } |
| 1230 | if (!sk_X509_push(sk, x)) { |
| 1231 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1232 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1233 | } |
| 1234 | x = NULL; |
| 1235 | nc += l + 3; |
| 1236 | p = q; |
| 1237 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1238 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1239 | i = ssl_verify_cert_chain(s, sk); |
| 1240 | if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) |
Richard Levitte | 82d5d46 | 2001-07-12 08:51:47 +0000 | [diff] [blame] | 1241 | #ifndef OPENSSL_NO_KRB5 |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1242 | && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && |
| 1243 | (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) |
| 1244 | #endif /* OPENSSL_NO_KRB5 */ |
| 1245 | ) { |
| 1246 | al = ssl_verify_alarm_type(s->verify_result); |
| 1247 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1248 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1249 | goto f_err; |
| 1250 | } |
| 1251 | ERR_clear_error(); /* but we keep s->verify_result */ |
| 1252 | if (i > 1) { |
| 1253 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i); |
| 1254 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1255 | goto f_err; |
| 1256 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1257 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1258 | sc = ssl_sess_cert_new(); |
| 1259 | if (sc == NULL) |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1260 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1261 | |
Rich Salz | 62adbce | 2015-04-11 10:22:36 -0400 | [diff] [blame] | 1262 | ssl_sess_cert_free(s->session->sess_cert); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1263 | s->session->sess_cert = sc; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1264 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1265 | sc->cert_chain = sk; |
| 1266 | /* |
| 1267 | * Inconsistency alert: cert_chain does include the peer's certificate, |
| 1268 | * which we don't include in s3_srvr.c |
| 1269 | */ |
| 1270 | x = sk_X509_value(sk, 0); |
| 1271 | sk = NULL; |
| 1272 | /* |
| 1273 | * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end |
| 1274 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1275 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1276 | pkey = X509_get_pubkey(x); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1277 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1278 | /* VRS: allow null cert if auth == KRB5 */ |
| 1279 | need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && |
| 1280 | (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) |
| 1281 | ? 0 : 1; |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 1282 | |
| 1283 | #ifdef KSSL_DEBUG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1284 | fprintf(stderr, "pkey,x = %p, %p\n", pkey, x); |
| 1285 | fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey)); |
| 1286 | fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n", |
| 1287 | s->s3->tmp.new_cipher->name, |
| 1288 | s->s3->tmp.new_cipher->algorithm_mkey, |
| 1289 | s->s3->tmp.new_cipher->algorithm_auth, need_cert); |
| 1290 | #endif /* KSSL_DEBUG */ |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 1291 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1292 | if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) { |
| 1293 | x = NULL; |
| 1294 | al = SSL3_AL_FATAL; |
| 1295 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1296 | SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
| 1297 | goto f_err; |
| 1298 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1299 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1300 | i = ssl_cert_type(x, pkey); |
| 1301 | if (need_cert && i < 0) { |
| 1302 | x = NULL; |
| 1303 | al = SSL3_AL_FATAL; |
| 1304 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1305 | SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 1306 | goto f_err; |
| 1307 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1308 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1309 | if (need_cert) { |
| 1310 | int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 1311 | if (exp_idx >= 0 && i != exp_idx) { |
| 1312 | x = NULL; |
| 1313 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1314 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, |
| 1315 | SSL_R_WRONG_CERTIFICATE_TYPE); |
| 1316 | goto f_err; |
| 1317 | } |
| 1318 | sc->peer_cert_type = i; |
| 1319 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 1320 | /* |
| 1321 | * Why would the following ever happen? We just created sc a couple |
| 1322 | * of lines ago. |
| 1323 | */ |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1324 | X509_free(sc->peer_pkeys[i].x509); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1325 | sc->peer_pkeys[i].x509 = x; |
| 1326 | sc->peer_key = &(sc->peer_pkeys[i]); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1327 | |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1328 | X509_free(s->session->peer); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1329 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); |
| 1330 | s->session->peer = x; |
| 1331 | } else { |
| 1332 | sc->peer_cert_type = i; |
| 1333 | sc->peer_key = NULL; |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 1334 | |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 1335 | X509_free(s->session->peer); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1336 | s->session->peer = NULL; |
| 1337 | } |
| 1338 | s->session->verify_result = s->verify_result; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1339 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1340 | x = NULL; |
| 1341 | ret = 1; |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 1342 | goto done; |
| 1343 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1344 | f_err: |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 1345 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 1346 | err: |
| 1347 | s->state = SSL_ST_ERR; |
Rich Salz | 6669647 | 2015-05-01 14:29:48 -0400 | [diff] [blame] | 1348 | done: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1349 | EVP_PKEY_free(pkey); |
| 1350 | X509_free(x); |
| 1351 | sk_X509_pop_free(sk, X509_free); |
| 1352 | return (ret); |
| 1353 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1354 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 1355 | int ssl3_get_key_exchange(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1356 | { |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1357 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1358 | unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2]; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1359 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1360 | EVP_MD_CTX md_ctx; |
| 1361 | unsigned char *param, *p; |
| 1362 | int al, j, ok; |
| 1363 | long i, param_len, n, alg_k, alg_a; |
| 1364 | EVP_PKEY *pkey = NULL; |
| 1365 | const EVP_MD *md = NULL; |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1366 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1367 | RSA *rsa = NULL; |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 1368 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1369 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1370 | DH *dh = NULL; |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 1371 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 1372 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1373 | EC_KEY *ecdh = NULL; |
| 1374 | BN_CTX *bn_ctx = NULL; |
| 1375 | EC_POINT *srvr_ecpoint = NULL; |
| 1376 | int curve_nid = 0; |
| 1377 | int encoded_pt_len = 0; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1378 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1379 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1380 | EVP_MD_CTX_init(&md_ctx); |
Dr. Stephen Henson | b15f876 | 2014-10-24 12:30:33 +0100 | [diff] [blame] | 1381 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1382 | /* |
| 1383 | * use same message size as in ssl3_get_certificate_request() as |
| 1384 | * ServerKeyExchange message may be skipped |
| 1385 | */ |
| 1386 | n = s->method->ssl_get_message(s, |
| 1387 | SSL3_ST_CR_KEY_EXCH_A, |
| 1388 | SSL3_ST_CR_KEY_EXCH_B, |
| 1389 | -1, s->max_cert_list, &ok); |
| 1390 | if (!ok) |
| 1391 | return ((int)n); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1392 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1393 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
Dr. Stephen Henson | b15f876 | 2014-10-24 12:30:33 +0100 | [diff] [blame] | 1394 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1395 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 1396 | /* |
| 1397 | * Can't skip server key exchange if this is an ephemeral |
| 1398 | * ciphersuite. |
| 1399 | */ |
| 1400 | if (alg_k & (SSL_kDHE | SSL_kECDHE)) { |
| 1401 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
| 1402 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1403 | goto f_err; |
| 1404 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 1405 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1406 | /* |
| 1407 | * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no |
| 1408 | * identity hint is sent. Set session->sess_cert anyway to avoid |
| 1409 | * problems later. |
| 1410 | */ |
| 1411 | if (alg_k & SSL_kPSK) { |
| 1412 | s->session->sess_cert = ssl_sess_cert_new(); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1413 | OPENSSL_free(s->ctx->psk_identity_hint); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1414 | s->ctx->psk_identity_hint = NULL; |
| 1415 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 1416 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1417 | s->s3->tmp.reuse_message = 1; |
| 1418 | return (1); |
| 1419 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1420 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1421 | param = p = (unsigned char *)s->init_msg; |
| 1422 | if (s->session->sess_cert != NULL) { |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1423 | #ifndef OPENSSL_NO_RSA |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 1424 | RSA_free(s->session->sess_cert->peer_rsa_tmp); |
| 1425 | s->session->sess_cert->peer_rsa_tmp = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1426 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1427 | #ifndef OPENSSL_NO_DH |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 1428 | DH_free(s->session->sess_cert->peer_dh_tmp); |
| 1429 | s->session->sess_cert->peer_dh_tmp = NULL; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1430 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 1431 | #ifndef OPENSSL_NO_EC |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 1432 | EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); |
| 1433 | s->session->sess_cert->peer_ecdh_tmp = NULL; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1434 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1435 | } else { |
| 1436 | s->session->sess_cert = ssl_sess_cert_new(); |
| 1437 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1438 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1439 | /* Total length of the parameters including the length prefix */ |
| 1440 | param_len = 0; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1441 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1442 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1443 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1444 | al = SSL_AD_DECODE_ERROR; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1445 | |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 1446 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1447 | if (alg_k & SSL_kPSK) { |
| 1448 | char tmp_id_hint[PSK_MAX_IDENTITY_LEN + 1]; |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 1449 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1450 | param_len = 2; |
| 1451 | if (param_len > n) { |
| 1452 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1453 | goto f_err; |
| 1454 | } |
| 1455 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1456 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1457 | /* |
| 1458 | * Store PSK identity hint for later use, hint is used in |
| 1459 | * ssl3_send_client_key_exchange. Assume that the maximum length of |
| 1460 | * a PSK identity hint can be as long as the maximum length of a PSK |
| 1461 | * identity. |
| 1462 | */ |
| 1463 | if (i > PSK_MAX_IDENTITY_LEN) { |
| 1464 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1465 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); |
| 1466 | goto f_err; |
| 1467 | } |
| 1468 | if (i > n - param_len) { |
| 1469 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, |
| 1470 | SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); |
| 1471 | goto f_err; |
| 1472 | } |
| 1473 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1474 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1475 | /* |
| 1476 | * If received PSK identity hint contains NULL characters, the hint |
| 1477 | * is truncated from the first NULL. p may not be ending with NULL, |
| 1478 | * so create a NULL-terminated string. |
| 1479 | */ |
| 1480 | memcpy(tmp_id_hint, p, i); |
| 1481 | memset(tmp_id_hint + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 1482 | OPENSSL_free(s->ctx->psk_identity_hint); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1483 | s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint); |
| 1484 | if (s->ctx->psk_identity_hint == NULL) { |
| 1485 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1486 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 1487 | goto f_err; |
| 1488 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 1489 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1490 | p += i; |
| 1491 | n -= param_len; |
| 1492 | } else |
| 1493 | #endif /* !OPENSSL_NO_PSK */ |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 1494 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1495 | if (alg_k & SSL_kSRP) { |
| 1496 | param_len = 2; |
| 1497 | if (param_len > n) { |
| 1498 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1499 | goto f_err; |
| 1500 | } |
| 1501 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1502 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1503 | if (i > n - param_len) { |
| 1504 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH); |
| 1505 | goto f_err; |
| 1506 | } |
| 1507 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1508 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1509 | if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) { |
| 1510 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1511 | goto err; |
| 1512 | } |
| 1513 | p += i; |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 1514 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1515 | if (2 > n - param_len) { |
| 1516 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1517 | goto f_err; |
| 1518 | } |
| 1519 | param_len += 2; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1520 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1521 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1522 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1523 | if (i > n - param_len) { |
| 1524 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH); |
| 1525 | goto f_err; |
| 1526 | } |
| 1527 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1528 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1529 | if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) { |
| 1530 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1531 | goto err; |
| 1532 | } |
| 1533 | p += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1534 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1535 | if (1 > n - param_len) { |
| 1536 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1537 | goto f_err; |
| 1538 | } |
| 1539 | param_len += 1; |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 1540 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1541 | i = (unsigned int)(p[0]); |
| 1542 | p++; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1543 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1544 | if (i > n - param_len) { |
| 1545 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH); |
| 1546 | goto f_err; |
| 1547 | } |
| 1548 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1549 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1550 | if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) { |
| 1551 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1552 | goto err; |
| 1553 | } |
| 1554 | p += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1555 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1556 | if (2 > n - param_len) { |
| 1557 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1558 | goto f_err; |
| 1559 | } |
| 1560 | param_len += 2; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1561 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1562 | n2s(p, i); |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 1563 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1564 | if (i > n - param_len) { |
| 1565 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH); |
| 1566 | goto f_err; |
| 1567 | } |
| 1568 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1569 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1570 | if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) { |
| 1571 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1572 | goto err; |
| 1573 | } |
| 1574 | p += i; |
| 1575 | n -= param_len; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1576 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1577 | if (!srp_verify_server_param(s, &al)) { |
| 1578 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); |
| 1579 | goto f_err; |
| 1580 | } |
Dr. Stephen Henson | 0989790 | 2014-08-03 21:25:22 +0100 | [diff] [blame] | 1581 | |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 1582 | /* We must check if there is a certificate */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1583 | # ifndef OPENSSL_NO_RSA |
| 1584 | if (alg_a & SSL_aRSA) |
| 1585 | pkey = |
| 1586 | X509_get_pubkey(s->session-> |
| 1587 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1588 | # else |
| 1589 | if (0) ; |
| 1590 | # endif |
| 1591 | # ifndef OPENSSL_NO_DSA |
| 1592 | else if (alg_a & SSL_aDSS) |
| 1593 | pkey = |
| 1594 | X509_get_pubkey(s->session-> |
| 1595 | sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. |
| 1596 | x509); |
| 1597 | # endif |
| 1598 | } else |
| 1599 | #endif /* !OPENSSL_NO_SRP */ |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 1600 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1601 | if (alg_k & SSL_kRSA) { |
| 1602 | /* Temporary RSA keys only allowed in export ciphersuites */ |
| 1603 | if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { |
| 1604 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1605 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
| 1606 | goto f_err; |
| 1607 | } |
| 1608 | if ((rsa = RSA_new()) == NULL) { |
| 1609 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 1610 | goto err; |
| 1611 | } |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1612 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1613 | param_len = 2; |
| 1614 | if (param_len > n) { |
| 1615 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1616 | goto f_err; |
| 1617 | } |
| 1618 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1619 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1620 | if (i > n - param_len) { |
| 1621 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH); |
| 1622 | goto f_err; |
| 1623 | } |
| 1624 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1625 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1626 | if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) { |
| 1627 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1628 | goto err; |
| 1629 | } |
| 1630 | p += i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1631 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1632 | if (2 > n - param_len) { |
| 1633 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1634 | goto f_err; |
| 1635 | } |
| 1636 | param_len += 2; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1637 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1638 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1639 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1640 | if (i > n - param_len) { |
| 1641 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH); |
| 1642 | goto f_err; |
| 1643 | } |
| 1644 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1645 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1646 | if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) { |
| 1647 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1648 | goto err; |
| 1649 | } |
| 1650 | p += i; |
| 1651 | n -= param_len; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1652 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1653 | /* this should be because we are using an export cipher */ |
| 1654 | if (alg_a & SSL_aRSA) |
| 1655 | pkey = |
| 1656 | X509_get_pubkey(s->session-> |
| 1657 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1658 | else { |
| 1659 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
| 1660 | goto err; |
| 1661 | } |
| 1662 | s->session->sess_cert->peer_rsa_tmp = rsa; |
| 1663 | rsa = NULL; |
| 1664 | } |
| 1665 | #else /* OPENSSL_NO_RSA */ |
| 1666 | if (0) ; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1667 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1668 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1669 | else if (alg_k & SSL_kDHE) { |
| 1670 | if ((dh = DH_new()) == NULL) { |
| 1671 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB); |
| 1672 | goto err; |
| 1673 | } |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1674 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1675 | param_len = 2; |
| 1676 | if (param_len > n) { |
| 1677 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1678 | goto f_err; |
| 1679 | } |
| 1680 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1681 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1682 | if (i > n - param_len) { |
| 1683 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH); |
| 1684 | goto f_err; |
| 1685 | } |
| 1686 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1687 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1688 | if (!(dh->p = BN_bin2bn(p, i, NULL))) { |
| 1689 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1690 | goto err; |
| 1691 | } |
| 1692 | p += i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1693 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1694 | if (2 > n - param_len) { |
| 1695 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1696 | goto f_err; |
| 1697 | } |
| 1698 | param_len += 2; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1699 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1700 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1701 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1702 | if (i > n - param_len) { |
| 1703 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH); |
| 1704 | goto f_err; |
| 1705 | } |
| 1706 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1707 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1708 | if (!(dh->g = BN_bin2bn(p, i, NULL))) { |
| 1709 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1710 | goto err; |
| 1711 | } |
| 1712 | p += i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1713 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1714 | if (2 > n - param_len) { |
| 1715 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1716 | goto f_err; |
| 1717 | } |
| 1718 | param_len += 2; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1719 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1720 | n2s(p, i); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1721 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1722 | if (i > n - param_len) { |
| 1723 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH); |
| 1724 | goto f_err; |
| 1725 | } |
| 1726 | param_len += i; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1727 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1728 | if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) { |
| 1729 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); |
| 1730 | goto err; |
| 1731 | } |
| 1732 | p += i; |
| 1733 | n -= param_len; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1734 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1735 | if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) { |
| 1736 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1737 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL); |
| 1738 | goto f_err; |
| 1739 | } |
| 1740 | # ifndef OPENSSL_NO_RSA |
| 1741 | if (alg_a & SSL_aRSA) |
| 1742 | pkey = |
| 1743 | X509_get_pubkey(s->session-> |
| 1744 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1745 | # else |
| 1746 | if (0) ; |
| 1747 | # endif |
| 1748 | # ifndef OPENSSL_NO_DSA |
| 1749 | else if (alg_a & SSL_aDSS) |
| 1750 | pkey = |
| 1751 | X509_get_pubkey(s->session-> |
| 1752 | sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. |
| 1753 | x509); |
| 1754 | # endif |
| 1755 | /* else anonymous DH, so no certificate or pkey. */ |
Dr. Stephen Henson | b362cca | 2013-12-15 13:32:24 +0000 | [diff] [blame] | 1756 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1757 | s->session->sess_cert->peer_dh_tmp = dh; |
| 1758 | dh = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1759 | } |
| 1760 | #endif /* !OPENSSL_NO_DH */ |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1761 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 1762 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1763 | else if (alg_k & SSL_kECDHE) { |
| 1764 | EC_GROUP *ngroup; |
| 1765 | const EC_GROUP *group; |
Nils Larsch | 9dd8405 | 2005-05-16 10:11:04 +0000 | [diff] [blame] | 1766 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1767 | if ((ecdh = EC_KEY_new()) == NULL) { |
| 1768 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 1769 | goto err; |
| 1770 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1771 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1772 | /* |
| 1773 | * Extract elliptic curve parameters and the server's ephemeral ECDH |
| 1774 | * public key. Keep accumulating lengths of various components in |
| 1775 | * param_len and make sure it never exceeds n. |
| 1776 | */ |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1777 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1778 | /* |
| 1779 | * XXX: For now we only support named (not generic) curves and the |
| 1780 | * ECParameters in this case is just three bytes. We also need one |
| 1781 | * byte for the length of the encoded point |
| 1782 | */ |
| 1783 | param_len = 4; |
| 1784 | if (param_len > n) { |
| 1785 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1786 | goto f_err; |
| 1787 | } |
| 1788 | /* |
| 1789 | * Check curve is one of our preferences, if not server has sent an |
| 1790 | * invalid curve. ECParameters is 3 bytes. |
| 1791 | */ |
| 1792 | if (!tls1_check_curve(s, p, 3)) { |
| 1793 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE); |
| 1794 | goto f_err; |
| 1795 | } |
Dr. Stephen Henson | d18b716 | 2012-07-24 13:47:40 +0000 | [diff] [blame] | 1796 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1797 | if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) { |
| 1798 | al = SSL_AD_INTERNAL_ERROR; |
| 1799 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, |
| 1800 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
| 1801 | goto f_err; |
| 1802 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1803 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1804 | ngroup = EC_GROUP_new_by_curve_name(curve_nid); |
| 1805 | if (ngroup == NULL) { |
| 1806 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); |
| 1807 | goto err; |
| 1808 | } |
| 1809 | if (EC_KEY_set_group(ecdh, ngroup) == 0) { |
| 1810 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); |
| 1811 | goto err; |
| 1812 | } |
| 1813 | EC_GROUP_free(ngroup); |
Nils Larsch | 9dd8405 | 2005-05-16 10:11:04 +0000 | [diff] [blame] | 1814 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1815 | group = EC_KEY_get0_group(ecdh); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1816 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1817 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
| 1818 | (EC_GROUP_get_degree(group) > 163)) { |
| 1819 | al = SSL_AD_EXPORT_RESTRICTION; |
| 1820 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, |
| 1821 | SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); |
| 1822 | goto f_err; |
| 1823 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1824 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1825 | p += 3; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1826 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1827 | /* Next, get the encoded ECPoint */ |
| 1828 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || |
| 1829 | ((bn_ctx = BN_CTX_new()) == NULL)) { |
| 1830 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 1831 | goto err; |
| 1832 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1833 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1834 | encoded_pt_len = *p; /* length of encoded point */ |
| 1835 | p += 1; |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1836 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1837 | if ((encoded_pt_len > n - param_len) || |
| 1838 | (EC_POINT_oct2point(group, srvr_ecpoint, |
| 1839 | p, encoded_pt_len, bn_ctx) == 0)) { |
| 1840 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); |
| 1841 | goto f_err; |
| 1842 | } |
| 1843 | param_len += encoded_pt_len; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1844 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1845 | n -= param_len; |
| 1846 | p += encoded_pt_len; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 1847 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1848 | /* |
| 1849 | * The ECC/TLS specification does not mention the use of DSA to sign |
| 1850 | * ECParameters in the server key exchange message. We do support RSA |
| 1851 | * and ECDSA. |
| 1852 | */ |
| 1853 | if (0) ; |
| 1854 | # ifndef OPENSSL_NO_RSA |
| 1855 | else if (alg_a & SSL_aRSA) |
| 1856 | pkey = |
| 1857 | X509_get_pubkey(s->session-> |
| 1858 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1859 | # endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 1860 | # ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1861 | else if (alg_a & SSL_aECDSA) |
| 1862 | pkey = |
| 1863 | X509_get_pubkey(s->session-> |
| 1864 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); |
| 1865 | # endif |
| 1866 | /* else anonymous ECDH, so no certificate or pkey. */ |
| 1867 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); |
| 1868 | s->session->sess_cert->peer_ecdh_tmp = ecdh; |
| 1869 | ecdh = NULL; |
| 1870 | BN_CTX_free(bn_ctx); |
| 1871 | bn_ctx = NULL; |
| 1872 | EC_POINT_free(srvr_ecpoint); |
| 1873 | srvr_ecpoint = NULL; |
| 1874 | } else if (alg_k) { |
| 1875 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 1876 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); |
| 1877 | goto f_err; |
| 1878 | } |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 1879 | #endif /* !OPENSSL_NO_EC */ |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 1880 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1881 | /* p points to the next byte, there are 'n' bytes left */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1882 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1883 | /* if it was signed, check the signature */ |
| 1884 | if (pkey != NULL) { |
| 1885 | if (SSL_USE_SIGALGS(s)) { |
| 1886 | int rv; |
| 1887 | if (2 > n) { |
| 1888 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1889 | goto f_err; |
| 1890 | } |
| 1891 | rv = tls12_check_peer_sigalg(&md, s, p, pkey); |
| 1892 | if (rv == -1) |
| 1893 | goto err; |
| 1894 | else if (rv == 0) { |
| 1895 | goto f_err; |
| 1896 | } |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 1897 | #ifdef SSL_DEBUG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1898 | fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); |
Dr. Stephen Henson | a2f9200 | 2011-05-09 15:44:01 +0000 | [diff] [blame] | 1899 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1900 | p += 2; |
| 1901 | n -= 2; |
| 1902 | } else |
| 1903 | md = EVP_sha1(); |
Matt Caswell | f2be92b | 2014-07-26 23:47:40 +0100 | [diff] [blame] | 1904 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1905 | if (2 > n) { |
| 1906 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); |
| 1907 | goto f_err; |
| 1908 | } |
| 1909 | n2s(p, i); |
| 1910 | n -= 2; |
| 1911 | j = EVP_PKEY_size(pkey); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1912 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1913 | /* |
| 1914 | * Check signature length. If n is 0 then signature is empty |
| 1915 | */ |
| 1916 | if ((i != n) || (n > j) || (n <= 0)) { |
| 1917 | /* wrong packet length */ |
| 1918 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); |
| 1919 | goto f_err; |
| 1920 | } |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1921 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1922 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { |
| 1923 | int num; |
| 1924 | unsigned int size; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1925 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1926 | j = 0; |
| 1927 | q = md_buf; |
| 1928 | for (num = 2; num > 0; num--) { |
| 1929 | EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); |
| 1930 | EVP_DigestInit_ex(&md_ctx, (num == 2) |
| 1931 | ? s->ctx->md5 : s->ctx->sha1, NULL); |
| 1932 | EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), |
| 1933 | SSL3_RANDOM_SIZE); |
| 1934 | EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), |
| 1935 | SSL3_RANDOM_SIZE); |
| 1936 | EVP_DigestUpdate(&md_ctx, param, param_len); |
| 1937 | EVP_DigestFinal_ex(&md_ctx, q, &size); |
| 1938 | q += size; |
| 1939 | j += size; |
| 1940 | } |
| 1941 | i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa); |
| 1942 | if (i < 0) { |
| 1943 | al = SSL_AD_DECRYPT_ERROR; |
| 1944 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT); |
| 1945 | goto f_err; |
| 1946 | } |
| 1947 | if (i == 0) { |
| 1948 | /* bad signature */ |
| 1949 | al = SSL_AD_DECRYPT_ERROR; |
| 1950 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); |
| 1951 | goto f_err; |
| 1952 | } |
| 1953 | } else |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1954 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1955 | { |
| 1956 | EVP_VerifyInit_ex(&md_ctx, md, NULL); |
| 1957 | EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]), |
| 1958 | SSL3_RANDOM_SIZE); |
| 1959 | EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]), |
| 1960 | SSL3_RANDOM_SIZE); |
| 1961 | EVP_VerifyUpdate(&md_ctx, param, param_len); |
| 1962 | if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) { |
| 1963 | /* bad signature */ |
| 1964 | al = SSL_AD_DECRYPT_ERROR; |
| 1965 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); |
| 1966 | goto f_err; |
| 1967 | } |
| 1968 | } |
| 1969 | } else { |
| 1970 | /* aNULL, aSRP or kPSK do not need public keys */ |
| 1971 | if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) { |
| 1972 | /* Might be wrong key type, check it */ |
| 1973 | if (ssl3_check_cert_and_algorithm(s)) |
| 1974 | /* Otherwise this shouldn't happen */ |
| 1975 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
| 1976 | goto err; |
| 1977 | } |
| 1978 | /* still data left over */ |
| 1979 | if (n != 0) { |
| 1980 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); |
| 1981 | goto f_err; |
| 1982 | } |
| 1983 | } |
| 1984 | EVP_PKEY_free(pkey); |
| 1985 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1986 | return (1); |
| 1987 | f_err: |
| 1988 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1989 | err: |
| 1990 | EVP_PKEY_free(pkey); |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1991 | #ifndef OPENSSL_NO_RSA |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 1992 | RSA_free(rsa); |
Bodo Möller | 6b521df | 1999-07-12 15:20:08 +0000 | [diff] [blame] | 1993 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 1994 | #ifndef OPENSSL_NO_DH |
Rich Salz | d640708 | 2015-03-24 10:17:37 -0400 | [diff] [blame] | 1995 | DH_free(dh); |
Bodo Möller | 6b521df | 1999-07-12 15:20:08 +0000 | [diff] [blame] | 1996 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 1997 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1998 | BN_CTX_free(bn_ctx); |
| 1999 | EC_POINT_free(srvr_ecpoint); |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 2000 | EC_KEY_free(ecdh); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2001 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2002 | EVP_MD_CTX_cleanup(&md_ctx); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2003 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2004 | return (-1); |
| 2005 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2006 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 2007 | int ssl3_get_certificate_request(SSL *s) |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 2008 | { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2009 | int ok, ret = 0; |
| 2010 | unsigned long n, nc, l; |
| 2011 | unsigned int llen, ctype_num, i; |
| 2012 | X509_NAME *xn = NULL; |
| 2013 | const unsigned char *p, *q; |
| 2014 | unsigned char *d; |
| 2015 | STACK_OF(X509_NAME) *ca_sk = NULL; |
| 2016 | |
| 2017 | n = s->method->ssl_get_message(s, |
| 2018 | SSL3_ST_CR_CERT_REQ_A, |
| 2019 | SSL3_ST_CR_CERT_REQ_B, |
| 2020 | -1, s->max_cert_list, &ok); |
| 2021 | |
| 2022 | if (!ok) |
| 2023 | return ((int)n); |
| 2024 | |
| 2025 | s->s3->tmp.cert_req = 0; |
| 2026 | |
| 2027 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { |
| 2028 | s->s3->tmp.reuse_message = 1; |
| 2029 | /* |
| 2030 | * If we get here we don't need any cached handshake records as we |
| 2031 | * wont be doing client auth. |
| 2032 | */ |
| 2033 | if (s->s3->handshake_buffer) { |
| 2034 | if (!ssl3_digest_cached_records(s)) |
| 2035 | goto err; |
| 2036 | } |
| 2037 | return (1); |
| 2038 | } |
| 2039 | |
| 2040 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 2041 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 2042 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE); |
| 2043 | goto err; |
| 2044 | } |
| 2045 | |
| 2046 | /* TLS does not like anon-DH with client cert */ |
| 2047 | if (s->version > SSL3_VERSION) { |
| 2048 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) { |
| 2049 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 2050 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, |
| 2051 | SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); |
| 2052 | goto err; |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | p = d = (unsigned char *)s->init_msg; |
| 2057 | |
| 2058 | if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { |
| 2059 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
| 2060 | goto err; |
| 2061 | } |
| 2062 | |
| 2063 | /* get the certificate types */ |
| 2064 | ctype_num = *(p++); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2065 | OPENSSL_free(s->cert->ctypes); |
| 2066 | s->cert->ctypes = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2067 | if (ctype_num > SSL3_CT_NUMBER) { |
| 2068 | /* If we exceed static buffer copy all to cert structure */ |
| 2069 | s->cert->ctypes = OPENSSL_malloc(ctype_num); |
| 2070 | if (s->cert->ctypes == NULL) { |
| 2071 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
| 2072 | goto err; |
| 2073 | } |
| 2074 | memcpy(s->cert->ctypes, p, ctype_num); |
| 2075 | s->cert->ctype_num = (size_t)ctype_num; |
| 2076 | ctype_num = SSL3_CT_NUMBER; |
| 2077 | } |
| 2078 | for (i = 0; i < ctype_num; i++) |
| 2079 | s->s3->tmp.ctype[i] = p[i]; |
| 2080 | p += p[-1]; |
| 2081 | if (SSL_USE_SIGALGS(s)) { |
| 2082 | n2s(p, llen); |
| 2083 | /* |
| 2084 | * Check we have enough room for signature algorithms and following |
| 2085 | * length value. |
| 2086 | */ |
| 2087 | if ((unsigned long)(p - d + llen + 2) > n) { |
| 2088 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2089 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, |
| 2090 | SSL_R_DATA_LENGTH_TOO_LONG); |
| 2091 | goto err; |
| 2092 | } |
| 2093 | /* Clear certificate digests and validity flags */ |
| 2094 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
| 2095 | s->cert->pkeys[i].digest = NULL; |
| 2096 | s->cert->pkeys[i].valid_flags = 0; |
| 2097 | } |
| 2098 | if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) { |
| 2099 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2100 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, |
| 2101 | SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
| 2102 | goto err; |
| 2103 | } |
| 2104 | if (!tls1_process_sigalgs(s)) { |
| 2105 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 2106 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
| 2107 | goto err; |
| 2108 | } |
| 2109 | p += llen; |
| 2110 | } |
| 2111 | |
| 2112 | /* get the CA RDNs */ |
| 2113 | n2s(p, llen); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2114 | |
| 2115 | if ((unsigned long)(p - d + llen) != n) { |
| 2116 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2117 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); |
| 2118 | goto err; |
| 2119 | } |
| 2120 | |
| 2121 | for (nc = 0; nc < llen;) { |
| 2122 | n2s(p, l); |
| 2123 | if ((l + nc + 2) > llen) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2124 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2125 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG); |
| 2126 | goto err; |
| 2127 | } |
| 2128 | |
| 2129 | q = p; |
| 2130 | |
| 2131 | if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) { |
Matt Caswell | 3c33c6f | 2015-02-05 15:57:54 +0000 | [diff] [blame] | 2132 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2133 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); |
| 2134 | goto err; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | if (q != (p + l)) { |
| 2138 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2139 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, |
| 2140 | SSL_R_CA_DN_LENGTH_MISMATCH); |
| 2141 | goto err; |
| 2142 | } |
| 2143 | if (!sk_X509_NAME_push(ca_sk, xn)) { |
| 2144 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); |
| 2145 | goto err; |
| 2146 | } |
| 2147 | |
| 2148 | p += l; |
| 2149 | nc += l + 2; |
| 2150 | } |
| 2151 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2152 | /* we should setup a certificate to return.... */ |
| 2153 | s->s3->tmp.cert_req = 1; |
| 2154 | s->s3->tmp.ctype_num = ctype_num; |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 2155 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2156 | s->s3->tmp.ca_names = ca_sk; |
| 2157 | ca_sk = NULL; |
| 2158 | |
| 2159 | ret = 1; |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2160 | goto done; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2161 | err: |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2162 | s->state = SSL_ST_ERR; |
| 2163 | done: |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 2164 | sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2165 | return (ret); |
| 2166 | } |
| 2167 | |
| 2168 | static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) |
| 2169 | { |
| 2170 | return (X509_NAME_cmp(*a, *b)); |
| 2171 | } |
| 2172 | |
| 2173 | #ifndef OPENSSL_NO_TLSEXT |
| 2174 | int ssl3_get_new_session_ticket(SSL *s) |
| 2175 | { |
| 2176 | int ok, al, ret = 0, ticklen; |
| 2177 | long n; |
| 2178 | const unsigned char *p; |
| 2179 | unsigned char *d; |
| 2180 | |
| 2181 | n = s->method->ssl_get_message(s, |
| 2182 | SSL3_ST_CR_SESSION_TICKET_A, |
| 2183 | SSL3_ST_CR_SESSION_TICKET_B, |
| 2184 | SSL3_MT_NEWSESSION_TICKET, 16384, &ok); |
| 2185 | |
| 2186 | if (!ok) |
| 2187 | return ((int)n); |
| 2188 | |
| 2189 | if (n < 6) { |
| 2190 | /* need at least ticket_lifetime_hint + ticket length */ |
| 2191 | al = SSL_AD_DECODE_ERROR; |
| 2192 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
| 2193 | goto f_err; |
| 2194 | } |
| 2195 | |
| 2196 | p = d = (unsigned char *)s->init_msg; |
| 2197 | n2l(p, s->session->tlsext_tick_lifetime_hint); |
| 2198 | n2s(p, ticklen); |
| 2199 | /* ticket_lifetime_hint + ticket_length + ticket */ |
| 2200 | if (ticklen + 6 != n) { |
| 2201 | al = SSL_AD_DECODE_ERROR; |
| 2202 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); |
| 2203 | goto f_err; |
| 2204 | } |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2205 | OPENSSL_free(s->session->tlsext_tick); |
| 2206 | s->session->tlsext_ticklen = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2207 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
| 2208 | if (!s->session->tlsext_tick) { |
| 2209 | SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); |
| 2210 | goto err; |
| 2211 | } |
| 2212 | memcpy(s->session->tlsext_tick, p, ticklen); |
| 2213 | s->session->tlsext_ticklen = ticklen; |
| 2214 | /* |
| 2215 | * There are two ways to detect a resumed ticket session. One is to set |
| 2216 | * an appropriate session ID and then the server must return a match in |
| 2217 | * ServerHello. This allows the normal client session ID matching to work |
| 2218 | * and we know much earlier that the ticket has been accepted. The |
| 2219 | * other way is to set zero length session ID when the ticket is |
| 2220 | * presented and rely on the handshake to determine session resumption. |
| 2221 | * We choose the former approach because this fits in with assumptions |
| 2222 | * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is |
| 2223 | * SHA256 is disabled) hash of the ticket. |
| 2224 | */ |
| 2225 | EVP_Digest(p, ticklen, |
| 2226 | s->session->session_id, &s->session->session_id_length, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2227 | EVP_sha256(), NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2228 | ret = 1; |
| 2229 | return (ret); |
| 2230 | f_err: |
| 2231 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2232 | err: |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2233 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2234 | return (-1); |
| 2235 | } |
| 2236 | |
| 2237 | int ssl3_get_cert_status(SSL *s) |
| 2238 | { |
| 2239 | int ok, al; |
| 2240 | unsigned long resplen, n; |
| 2241 | const unsigned char *p; |
| 2242 | |
| 2243 | n = s->method->ssl_get_message(s, |
| 2244 | SSL3_ST_CR_CERT_STATUS_A, |
| 2245 | SSL3_ST_CR_CERT_STATUS_B, |
| 2246 | SSL3_MT_CERTIFICATE_STATUS, 16384, &ok); |
| 2247 | |
| 2248 | if (!ok) |
| 2249 | return ((int)n); |
| 2250 | if (n < 4) { |
| 2251 | /* need at least status type + length */ |
| 2252 | al = SSL_AD_DECODE_ERROR; |
| 2253 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); |
| 2254 | goto f_err; |
| 2255 | } |
| 2256 | p = (unsigned char *)s->init_msg; |
| 2257 | if (*p++ != TLSEXT_STATUSTYPE_ocsp) { |
| 2258 | al = SSL_AD_DECODE_ERROR; |
| 2259 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); |
| 2260 | goto f_err; |
| 2261 | } |
| 2262 | n2l3(p, resplen); |
| 2263 | if (resplen + 4 != n) { |
| 2264 | al = SSL_AD_DECODE_ERROR; |
| 2265 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); |
| 2266 | goto f_err; |
| 2267 | } |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2268 | OPENSSL_free(s->tlsext_ocsp_resp); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2269 | s->tlsext_ocsp_resp = BUF_memdup(p, resplen); |
| 2270 | if (!s->tlsext_ocsp_resp) { |
| 2271 | al = SSL_AD_INTERNAL_ERROR; |
| 2272 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); |
| 2273 | goto f_err; |
| 2274 | } |
| 2275 | s->tlsext_ocsp_resplen = resplen; |
| 2276 | if (s->ctx->tlsext_status_cb) { |
| 2277 | int ret; |
| 2278 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2279 | if (ret == 0) { |
| 2280 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2281 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE); |
| 2282 | goto f_err; |
| 2283 | } |
| 2284 | if (ret < 0) { |
| 2285 | al = SSL_AD_INTERNAL_ERROR; |
| 2286 | SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); |
| 2287 | goto f_err; |
| 2288 | } |
| 2289 | } |
| 2290 | return 1; |
| 2291 | f_err: |
| 2292 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2293 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2294 | return (-1); |
Ralf S. Engelschall | dfeab06 | 1998-12-21 11:00:56 +0000 | [diff] [blame] | 2295 | } |
| 2296 | #endif |
| 2297 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 2298 | int ssl3_get_server_done(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2299 | { |
| 2300 | int ok, ret = 0; |
| 2301 | long n; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2302 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2303 | /* Second to last param should be very small, like 0 :-) */ |
| 2304 | n = s->method->ssl_get_message(s, |
| 2305 | SSL3_ST_CR_SRVR_DONE_A, |
| 2306 | SSL3_ST_CR_SRVR_DONE_B, |
| 2307 | SSL3_MT_SERVER_DONE, 30, &ok); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2308 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2309 | if (!ok) |
| 2310 | return ((int)n); |
| 2311 | if (n > 0) { |
| 2312 | /* should contain no data */ |
| 2313 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 2314 | SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 2315 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2316 | return -1; |
| 2317 | } |
| 2318 | ret = 1; |
| 2319 | return (ret); |
| 2320 | } |
Bodo Möller | 176f31d | 2003-02-28 15:37:10 +0000 | [diff] [blame] | 2321 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 2322 | int ssl3_send_client_key_exchange(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2323 | { |
| 2324 | unsigned char *p; |
| 2325 | int n; |
| 2326 | unsigned long alg_k; |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 2327 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2328 | unsigned char *q; |
| 2329 | EVP_PKEY *pkey = NULL; |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 2330 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 2331 | #ifndef OPENSSL_NO_KRB5 |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2332 | KSSL_ERR kssl_err; |
| 2333 | #endif /* OPENSSL_NO_KRB5 */ |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 2334 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2335 | EC_KEY *clnt_ecdh = NULL; |
| 2336 | const EC_POINT *srvr_ecpoint = NULL; |
| 2337 | EVP_PKEY *srvr_pub_pkey = NULL; |
| 2338 | unsigned char *encodedPoint = NULL; |
| 2339 | int encoded_pt_len = 0; |
| 2340 | BN_CTX *bn_ctx = NULL; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2341 | #endif |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2342 | unsigned char *pms = NULL; |
| 2343 | size_t pmslen = 0; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2344 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2345 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) { |
| 2346 | p = ssl_handshake_start(s); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2347 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2348 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2349 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2350 | /* Fool emacs indentation */ |
| 2351 | if (0) { |
| 2352 | } |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 2353 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2354 | else if (alg_k & SSL_kRSA) { |
| 2355 | RSA *rsa; |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2356 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
| 2357 | pms = OPENSSL_malloc(pmslen); |
| 2358 | if (!pms) |
| 2359 | goto memerr; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2360 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2361 | if (s->session->sess_cert == NULL) { |
| 2362 | /* |
| 2363 | * We should always have a server certificate with SSL_kRSA. |
| 2364 | */ |
| 2365 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2366 | ERR_R_INTERNAL_ERROR); |
| 2367 | goto err; |
| 2368 | } |
Emilia Käsper | 1716003 | 2014-07-24 22:15:29 +0200 | [diff] [blame] | 2369 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2370 | if (s->session->sess_cert->peer_rsa_tmp != NULL) |
| 2371 | rsa = s->session->sess_cert->peer_rsa_tmp; |
| 2372 | else { |
| 2373 | pkey = |
| 2374 | X509_get_pubkey(s->session-> |
| 2375 | sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC]. |
| 2376 | x509); |
| 2377 | if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) |
| 2378 | || (pkey->pkey.rsa == NULL)) { |
| 2379 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2380 | ERR_R_INTERNAL_ERROR); |
| 2381 | goto err; |
| 2382 | } |
| 2383 | rsa = pkey->pkey.rsa; |
| 2384 | EVP_PKEY_free(pkey); |
| 2385 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2386 | |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2387 | pms[0] = s->client_version >> 8; |
| 2388 | pms[1] = s->client_version & 0xff; |
| 2389 | if (RAND_bytes(pms + 2, pmslen - 2) <= 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2390 | goto err; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2391 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2392 | q = p; |
| 2393 | /* Fix buf for TLS and beyond */ |
| 2394 | if (s->version > SSL3_VERSION) |
| 2395 | p += 2; |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2396 | n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2397 | # ifdef PKCS1_CHECK |
| 2398 | if (s->options & SSL_OP_PKCS1_CHECK_1) |
| 2399 | p[1]++; |
| 2400 | if (s->options & SSL_OP_PKCS1_CHECK_2) |
| 2401 | tmp_buf[0] = 0x70; |
| 2402 | # endif |
| 2403 | if (n <= 0) { |
| 2404 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2405 | SSL_R_BAD_RSA_ENCRYPT); |
| 2406 | goto err; |
| 2407 | } |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 2408 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2409 | /* Fix buf for TLS and beyond */ |
| 2410 | if (s->version > SSL3_VERSION) { |
| 2411 | s2n(n, q); |
| 2412 | n += 2; |
| 2413 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2414 | } |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 2415 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 2416 | #ifndef OPENSSL_NO_KRB5 |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2417 | else if (alg_k & SSL_kKRB5) { |
| 2418 | krb5_error_code krb5rc; |
| 2419 | KSSL_CTX *kssl_ctx = s->kssl_ctx; |
| 2420 | /* krb5_data krb5_ap_req; */ |
| 2421 | krb5_data *enc_ticket; |
| 2422 | krb5_data authenticator, *authp = NULL; |
| 2423 | EVP_CIPHER_CTX ciph_ctx; |
| 2424 | const EVP_CIPHER *enc = NULL; |
| 2425 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 2426 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
| 2427 | unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH]; |
| 2428 | int padl, outl = sizeof(epms); |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 2429 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2430 | EVP_CIPHER_CTX_init(&ciph_ctx); |
Dr. Stephen Henson | de941e2 | 2002-03-14 18:22:23 +0000 | [diff] [blame] | 2431 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2432 | # ifdef KSSL_DEBUG |
| 2433 | fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n", |
| 2434 | alg_k, SSL_kKRB5); |
| 2435 | # endif /* KSSL_DEBUG */ |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 2436 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2437 | authp = NULL; |
| 2438 | # ifdef KRB5SENDAUTH |
| 2439 | if (KRB5SENDAUTH) |
| 2440 | authp = &authenticator; |
| 2441 | # endif /* KRB5SENDAUTH */ |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2442 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2443 | krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err); |
| 2444 | enc = kssl_map_enc(kssl_ctx->enctype); |
| 2445 | if (enc == NULL) |
| 2446 | goto err; |
| 2447 | # ifdef KSSL_DEBUG |
| 2448 | { |
| 2449 | fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc); |
| 2450 | if (krb5rc && kssl_err.text) |
| 2451 | fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n", |
| 2452 | kssl_err.text); |
| 2453 | } |
| 2454 | # endif /* KSSL_DEBUG */ |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 2455 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2456 | if (krb5rc) { |
| 2457 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 2458 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason); |
| 2459 | goto err; |
| 2460 | } |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 2461 | |
Matt Caswell | 50e735f | 2015-01-05 11:30:03 +0000 | [diff] [blame] | 2462 | /*- |
| 2463 | * 20010406 VRS - Earlier versions used KRB5 AP_REQ |
| 2464 | * in place of RFC 2712 KerberosWrapper, as in: |
| 2465 | * |
| 2466 | * Send ticket (copy to *p, set n = length) |
| 2467 | * n = krb5_ap_req.length; |
| 2468 | * memcpy(p, krb5_ap_req.data, krb5_ap_req.length); |
| 2469 | * if (krb5_ap_req.data) |
| 2470 | * kssl_krb5_free_data_contents(NULL,&krb5_ap_req); |
| 2471 | * |
| 2472 | * Now using real RFC 2712 KerberosWrapper |
| 2473 | * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) |
| 2474 | * Note: 2712 "opaque" types are here replaced |
| 2475 | * with a 2-byte length followed by the value. |
| 2476 | * Example: |
| 2477 | * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms |
| 2478 | * Where "xx xx" = length bytes. Shown here with |
| 2479 | * optional authenticator omitted. |
| 2480 | */ |
Richard Levitte | f9b3bff | 2000-11-30 22:53:34 +0000 | [diff] [blame] | 2481 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2482 | /* KerberosWrapper.Ticket */ |
| 2483 | s2n(enc_ticket->length, p); |
| 2484 | memcpy(p, enc_ticket->data, enc_ticket->length); |
| 2485 | p += enc_ticket->length; |
| 2486 | n = enc_ticket->length + 2; |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2487 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2488 | /* KerberosWrapper.Authenticator */ |
| 2489 | if (authp && authp->length) { |
| 2490 | s2n(authp->length, p); |
| 2491 | memcpy(p, authp->data, authp->length); |
| 2492 | p += authp->length; |
| 2493 | n += authp->length + 2; |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2494 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2495 | free(authp->data); |
| 2496 | authp->data = NULL; |
| 2497 | authp->length = 0; |
| 2498 | } else { |
| 2499 | s2n(0, p); /* null authenticator length */ |
| 2500 | n += 2; |
| 2501 | } |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2502 | |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2503 | pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
| 2504 | pms = OPENSSL_malloc(pmslen); |
| 2505 | if (!pms) |
| 2506 | goto memerr; |
| 2507 | |
| 2508 | pms[0] = s->client_version >> 8; |
| 2509 | pms[1] = s->client_version & 0xff; |
| 2510 | if (RAND_bytes(pms + 2, pmslen - 2) <= 0) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2511 | goto err; |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2512 | |
Matt Caswell | 35a1cc9 | 2015-01-17 00:06:54 +0000 | [diff] [blame] | 2513 | /*- |
| 2514 | * 20010420 VRS. Tried it this way; failed. |
| 2515 | * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); |
| 2516 | * EVP_CIPHER_CTX_set_key_length(&ciph_ctx, |
| 2517 | * kssl_ctx->length); |
| 2518 | * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); |
| 2519 | */ |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2520 | |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 2521 | memset(iv, 0, sizeof(iv)); /* per RFC 1510 */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2522 | EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv); |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2523 | EVP_EncryptUpdate(&ciph_ctx, epms, &outl, pms, pmslen); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2524 | EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl); |
| 2525 | outl += padl; |
| 2526 | if (outl > (int)sizeof epms) { |
| 2527 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2528 | ERR_R_INTERNAL_ERROR); |
| 2529 | goto err; |
| 2530 | } |
| 2531 | EVP_CIPHER_CTX_cleanup(&ciph_ctx); |
Richard Levitte | 2a1ef75 | 2001-07-09 21:46:58 +0000 | [diff] [blame] | 2532 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2533 | /* KerberosWrapper.EncryptedPreMasterSecret */ |
| 2534 | s2n(outl, p); |
| 2535 | memcpy(p, epms, outl); |
| 2536 | p += outl; |
| 2537 | n += outl + 2; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2538 | OPENSSL_cleanse(epms, outl); |
| 2539 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2540 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 2541 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2542 | else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) { |
| 2543 | DH *dh_srvr, *dh_clnt; |
| 2544 | SESS_CERT *scert = s->session->sess_cert; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2545 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2546 | if (scert == NULL) { |
| 2547 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 2548 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2549 | SSL_R_UNEXPECTED_MESSAGE); |
| 2550 | goto err; |
| 2551 | } |
Bodo Möller | e194fe8 | 2008-05-28 22:17:34 +0000 | [diff] [blame] | 2552 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2553 | if (scert->peer_dh_tmp != NULL) |
| 2554 | dh_srvr = scert->peer_dh_tmp; |
| 2555 | else { |
| 2556 | /* we get them from the cert */ |
| 2557 | int idx = scert->peer_cert_type; |
| 2558 | EVP_PKEY *spkey = NULL; |
| 2559 | dh_srvr = NULL; |
| 2560 | if (idx >= 0) |
| 2561 | spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509); |
| 2562 | if (spkey) { |
| 2563 | dh_srvr = EVP_PKEY_get1_DH(spkey); |
| 2564 | EVP_PKEY_free(spkey); |
| 2565 | } |
| 2566 | if (dh_srvr == NULL) { |
| 2567 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2568 | ERR_R_INTERNAL_ERROR); |
| 2569 | goto err; |
| 2570 | } |
| 2571 | } |
| 2572 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { |
| 2573 | /* Use client certificate key */ |
| 2574 | EVP_PKEY *clkey = s->cert->key->privatekey; |
| 2575 | dh_clnt = NULL; |
| 2576 | if (clkey) |
| 2577 | dh_clnt = EVP_PKEY_get1_DH(clkey); |
| 2578 | if (dh_clnt == NULL) { |
| 2579 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2580 | ERR_R_INTERNAL_ERROR); |
| 2581 | goto err; |
| 2582 | } |
| 2583 | } else { |
| 2584 | /* generate a new random key */ |
| 2585 | if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { |
| 2586 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); |
| 2587 | goto err; |
| 2588 | } |
| 2589 | if (!DH_generate_key(dh_clnt)) { |
| 2590 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); |
| 2591 | DH_free(dh_clnt); |
| 2592 | goto err; |
| 2593 | } |
| 2594 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2595 | |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2596 | pmslen = DH_size(dh_clnt); |
| 2597 | pms = OPENSSL_malloc(pmslen); |
| 2598 | if (!pms) |
| 2599 | goto memerr; |
| 2600 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2601 | /* |
| 2602 | * use the 'p' output buffer for the DH key, but make sure to |
| 2603 | * clear it out afterwards |
| 2604 | */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 2605 | |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2606 | n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2607 | if (scert->peer_dh_tmp == NULL) |
| 2608 | DH_free(dh_srvr); |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 2609 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2610 | if (n <= 0) { |
| 2611 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); |
| 2612 | DH_free(dh_clnt); |
| 2613 | goto err; |
| 2614 | } |
Andy Polyakov | 6b937f8 | 2015-03-12 08:54:28 +0100 | [diff] [blame] | 2615 | pmslen = n; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2616 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2617 | if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) |
| 2618 | n = 0; |
| 2619 | else { |
| 2620 | /* send off the data */ |
| 2621 | n = BN_num_bytes(dh_clnt->pub_key); |
| 2622 | s2n(n, p); |
| 2623 | BN_bn2bin(dh_clnt->pub_key, p); |
| 2624 | n += 2; |
| 2625 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2626 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2627 | DH_free(dh_clnt); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2628 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2629 | /* perhaps clean things up a bit EAY EAY EAY EAY */ |
| 2630 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 2631 | #endif |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2632 | |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 2633 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2634 | else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) { |
| 2635 | const EC_GROUP *srvr_group = NULL; |
| 2636 | EC_KEY *tkey; |
| 2637 | int ecdh_clnt_cert = 0; |
| 2638 | int field_size = 0; |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2639 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2640 | if (s->session->sess_cert == NULL) { |
| 2641 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 2642 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2643 | SSL_R_UNEXPECTED_MESSAGE); |
| 2644 | goto err; |
| 2645 | } |
Dr. Stephen Henson | a5362db | 2014-05-29 15:00:05 +0100 | [diff] [blame] | 2646 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2647 | /* |
| 2648 | * Did we send out the client's ECDH share for use in premaster |
| 2649 | * computation as part of client certificate? If so, set |
| 2650 | * ecdh_clnt_cert to 1. |
| 2651 | */ |
| 2652 | if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) { |
Matt Caswell | 50e735f | 2015-01-05 11:30:03 +0000 | [diff] [blame] | 2653 | /*- |
| 2654 | * XXX: For now, we do not support client |
| 2655 | * authentication using ECDH certificates. |
| 2656 | * To add such support, one needs to add |
| 2657 | * code that checks for appropriate |
| 2658 | * conditions and sets ecdh_clnt_cert to 1. |
| 2659 | * For example, the cert have an ECC |
| 2660 | * key on the same curve as the server's |
| 2661 | * and the key should be authorized for |
| 2662 | * key agreement. |
| 2663 | * |
| 2664 | * One also needs to add code in ssl3_connect |
| 2665 | * to skip sending the certificate verify |
| 2666 | * message. |
| 2667 | * |
| 2668 | * if ((s->cert->key->privatekey != NULL) && |
| 2669 | * (s->cert->key->privatekey->type == |
| 2670 | * EVP_PKEY_EC) && ...) |
| 2671 | * ecdh_clnt_cert = 1; |
| 2672 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2673 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2674 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2675 | if (s->session->sess_cert->peer_ecdh_tmp != NULL) { |
| 2676 | tkey = s->session->sess_cert->peer_ecdh_tmp; |
| 2677 | } else { |
| 2678 | /* Get the Server Public Key from Cert */ |
| 2679 | srvr_pub_pkey = |
| 2680 | X509_get_pubkey(s->session-> |
| 2681 | sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); |
| 2682 | if ((srvr_pub_pkey == NULL) |
| 2683 | || (srvr_pub_pkey->type != EVP_PKEY_EC) |
| 2684 | || (srvr_pub_pkey->pkey.ec == NULL)) { |
| 2685 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2686 | ERR_R_INTERNAL_ERROR); |
| 2687 | goto err; |
| 2688 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2689 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2690 | tkey = srvr_pub_pkey->pkey.ec; |
| 2691 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2692 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2693 | srvr_group = EC_KEY_get0_group(tkey); |
| 2694 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); |
Nils Larsch | 9dd8405 | 2005-05-16 10:11:04 +0000 | [diff] [blame] | 2695 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2696 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { |
| 2697 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2698 | ERR_R_INTERNAL_ERROR); |
| 2699 | goto err; |
| 2700 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2701 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2702 | if ((clnt_ecdh = EC_KEY_new()) == NULL) { |
| 2703 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2704 | ERR_R_MALLOC_FAILURE); |
| 2705 | goto err; |
| 2706 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2707 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2708 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { |
| 2709 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); |
| 2710 | goto err; |
| 2711 | } |
| 2712 | if (ecdh_clnt_cert) { |
| 2713 | /* |
| 2714 | * Reuse key info from our certificate We only need our |
| 2715 | * private key to perform the ECDH computation. |
| 2716 | */ |
| 2717 | const BIGNUM *priv_key; |
| 2718 | tkey = s->cert->key->privatekey->pkey.ec; |
| 2719 | priv_key = EC_KEY_get0_private_key(tkey); |
| 2720 | if (priv_key == NULL) { |
| 2721 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2722 | ERR_R_MALLOC_FAILURE); |
| 2723 | goto err; |
| 2724 | } |
| 2725 | if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) { |
| 2726 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); |
| 2727 | goto err; |
| 2728 | } |
| 2729 | } else { |
| 2730 | /* Generate a new ECDH key pair */ |
| 2731 | if (!(EC_KEY_generate_key(clnt_ecdh))) { |
| 2732 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2733 | ERR_R_ECDH_LIB); |
| 2734 | goto err; |
| 2735 | } |
| 2736 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2737 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2738 | /* |
| 2739 | * use the 'p' output buffer for the ECDH key, but make sure to |
| 2740 | * clear it out afterwards |
| 2741 | */ |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2742 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2743 | field_size = EC_GROUP_get_degree(srvr_group); |
| 2744 | if (field_size <= 0) { |
| 2745 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); |
| 2746 | goto err; |
| 2747 | } |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2748 | pmslen = (field_size + 7) / 8; |
| 2749 | pms = OPENSSL_malloc(pmslen); |
| 2750 | if (!pms) |
| 2751 | goto memerr; |
| 2752 | n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL); |
| 2753 | if (n <= 0 || pmslen != (size_t)n) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2754 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); |
| 2755 | goto err; |
| 2756 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2757 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2758 | if (ecdh_clnt_cert) { |
| 2759 | /* Send empty client key exch message */ |
| 2760 | n = 0; |
| 2761 | } else { |
| 2762 | /* |
| 2763 | * First check the size of encoding and allocate memory |
| 2764 | * accordingly. |
| 2765 | */ |
| 2766 | encoded_pt_len = |
| 2767 | EC_POINT_point2oct(srvr_group, |
| 2768 | EC_KEY_get0_public_key(clnt_ecdh), |
| 2769 | POINT_CONVERSION_UNCOMPRESSED, |
| 2770 | NULL, 0, NULL); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2771 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2772 | encodedPoint = (unsigned char *) |
| 2773 | OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char)); |
| 2774 | bn_ctx = BN_CTX_new(); |
| 2775 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) { |
| 2776 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2777 | ERR_R_MALLOC_FAILURE); |
| 2778 | goto err; |
| 2779 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2780 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2781 | /* Encode the public key */ |
| 2782 | n = EC_POINT_point2oct(srvr_group, |
| 2783 | EC_KEY_get0_public_key(clnt_ecdh), |
| 2784 | POINT_CONVERSION_UNCOMPRESSED, |
| 2785 | encodedPoint, encoded_pt_len, bn_ctx); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2786 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2787 | *p = n; /* length of encoded point */ |
| 2788 | /* Encoded point will be copied here */ |
| 2789 | p += 1; |
| 2790 | /* copy the point */ |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 2791 | memcpy(p, encodedPoint, n); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2792 | /* increment n to account for length field */ |
| 2793 | n += 1; |
| 2794 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 2795 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2796 | /* Free allocated memory */ |
| 2797 | BN_CTX_free(bn_ctx); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2798 | OPENSSL_free(encodedPoint); |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 2799 | EC_KEY_free(clnt_ecdh); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2800 | EVP_PKEY_free(srvr_pub_pkey); |
| 2801 | } |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 2802 | #endif /* !OPENSSL_NO_EC */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2803 | else if (alg_k & SSL_kGOST) { |
| 2804 | /* GOST key exchange message creation */ |
| 2805 | EVP_PKEY_CTX *pkey_ctx; |
| 2806 | X509 *peer_cert; |
| 2807 | size_t msglen; |
| 2808 | unsigned int md_len; |
| 2809 | int keytype; |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2810 | unsigned char shared_ukm[32], tmp[256]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2811 | EVP_MD_CTX *ukm_hash; |
| 2812 | EVP_PKEY *pub_key; |
Dr. Stephen Henson | 0e1dba9 | 2007-10-26 12:06:36 +0000 | [diff] [blame] | 2813 | |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2814 | pmslen = 32; |
| 2815 | pms = OPENSSL_malloc(pmslen); |
| 2816 | if (!pms) |
| 2817 | goto memerr; |
| 2818 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2819 | /* |
| 2820 | * Get server sertificate PKEY and create ctx from it |
| 2821 | */ |
| 2822 | peer_cert = |
| 2823 | s->session-> |
| 2824 | sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509; |
| 2825 | if (!peer_cert) |
| 2826 | peer_cert = |
| 2827 | s->session-> |
| 2828 | sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509; |
| 2829 | if (!peer_cert) { |
| 2830 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2831 | SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
| 2832 | goto err; |
| 2833 | } |
Dr. Stephen Henson | 0e1dba9 | 2007-10-26 12:06:36 +0000 | [diff] [blame] | 2834 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2835 | pkey_ctx = EVP_PKEY_CTX_new(pub_key = |
| 2836 | X509_get_pubkey(peer_cert), NULL); |
| 2837 | /* |
| 2838 | * If we have send a certificate, and certificate key |
| 2839 | * |
| 2840 | * * parameters match those of server certificate, use |
| 2841 | * certificate key for key exchange |
| 2842 | */ |
Dr. Stephen Henson | 0e1dba9 | 2007-10-26 12:06:36 +0000 | [diff] [blame] | 2843 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2844 | /* Otherwise, generate ephemeral key pair */ |
Dr. Stephen Henson | 0e1dba9 | 2007-10-26 12:06:36 +0000 | [diff] [blame] | 2845 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2846 | EVP_PKEY_encrypt_init(pkey_ctx); |
| 2847 | /* Generate session key */ |
Matt Caswell | 266483d | 2015-02-26 11:57:37 +0000 | [diff] [blame] | 2848 | if (RAND_bytes(pms, pmslen) <= 0) { |
| 2849 | EVP_PKEY_CTX_free(pkey_ctx); |
| 2850 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2851 | ERR_R_INTERNAL_ERROR); |
| 2852 | goto err; |
| 2853 | }; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2854 | /* |
| 2855 | * If we have client certificate, use its secret as peer key |
| 2856 | */ |
| 2857 | if (s->s3->tmp.cert_req && s->cert->key->privatekey) { |
| 2858 | if (EVP_PKEY_derive_set_peer |
| 2859 | (pkey_ctx, s->cert->key->privatekey) <= 0) { |
| 2860 | /* |
| 2861 | * If there was an error - just ignore it. Ephemeral key |
| 2862 | * * would be used |
| 2863 | */ |
| 2864 | ERR_clear_error(); |
| 2865 | } |
| 2866 | } |
| 2867 | /* |
| 2868 | * Compute shared IV and store it in algorithm-specific context |
| 2869 | * data |
| 2870 | */ |
| 2871 | ukm_hash = EVP_MD_CTX_create(); |
| 2872 | EVP_DigestInit(ukm_hash, |
| 2873 | EVP_get_digestbynid(NID_id_GostR3411_94)); |
| 2874 | EVP_DigestUpdate(ukm_hash, s->s3->client_random, |
| 2875 | SSL3_RANDOM_SIZE); |
| 2876 | EVP_DigestUpdate(ukm_hash, s->s3->server_random, |
| 2877 | SSL3_RANDOM_SIZE); |
| 2878 | EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); |
| 2879 | EVP_MD_CTX_destroy(ukm_hash); |
| 2880 | if (EVP_PKEY_CTX_ctrl |
| 2881 | (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, |
| 2882 | shared_ukm) < 0) { |
| 2883 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2884 | SSL_R_LIBRARY_BUG); |
| 2885 | goto err; |
| 2886 | } |
| 2887 | /* Make GOST keytransport blob message */ |
| 2888 | /* |
| 2889 | * Encapsulate it into sequence |
| 2890 | */ |
| 2891 | *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; |
| 2892 | msglen = 255; |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2893 | if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2894 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2895 | SSL_R_LIBRARY_BUG); |
| 2896 | goto err; |
| 2897 | } |
| 2898 | if (msglen >= 0x80) { |
| 2899 | *(p++) = 0x81; |
| 2900 | *(p++) = msglen & 0xff; |
| 2901 | n = msglen + 3; |
| 2902 | } else { |
| 2903 | *(p++) = msglen & 0xff; |
| 2904 | n = msglen + 2; |
| 2905 | } |
| 2906 | memcpy(p, tmp, msglen); |
| 2907 | /* Check if pubkey from client certificate was used */ |
| 2908 | if (EVP_PKEY_CTX_ctrl |
| 2909 | (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { |
| 2910 | /* Set flag "skip certificate verify" */ |
| 2911 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; |
| 2912 | } |
| 2913 | EVP_PKEY_CTX_free(pkey_ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2914 | EVP_PKEY_free(pub_key); |
| 2915 | |
| 2916 | } |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 2917 | #ifndef OPENSSL_NO_SRP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2918 | else if (alg_k & SSL_kSRP) { |
| 2919 | if (s->srp_ctx.A != NULL) { |
| 2920 | /* send off the data */ |
| 2921 | n = BN_num_bytes(s->srp_ctx.A); |
| 2922 | s2n(n, p); |
| 2923 | BN_bn2bin(s->srp_ctx.A, p); |
| 2924 | n += 2; |
| 2925 | } else { |
| 2926 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2927 | ERR_R_INTERNAL_ERROR); |
| 2928 | goto err; |
| 2929 | } |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2930 | OPENSSL_free(s->session->srp_username); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2931 | s->session->srp_username = BUF_strdup(s->srp_ctx.login); |
| 2932 | if (s->session->srp_username == NULL) { |
| 2933 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2934 | ERR_R_MALLOC_FAILURE); |
| 2935 | goto err; |
| 2936 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2937 | } |
Ben Laurie | edc032b | 2011-03-12 17:01:19 +0000 | [diff] [blame] | 2938 | #endif |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 2939 | #ifndef OPENSSL_NO_PSK |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2940 | else if (alg_k & SSL_kPSK) { |
| 2941 | /* |
| 2942 | * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a |
| 2943 | * \0-terminated identity. The last byte is for us for simulating |
| 2944 | * strnlen. |
| 2945 | */ |
| 2946 | char identity[PSK_MAX_IDENTITY_LEN + 2]; |
| 2947 | size_t identity_len; |
| 2948 | unsigned char *t = NULL; |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2949 | unsigned int psk_len = 0; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2950 | int psk_err = 1; |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 2951 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2952 | n = 0; |
| 2953 | if (s->psk_client_callback == NULL) { |
| 2954 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2955 | SSL_R_PSK_NO_CLIENT_CB); |
| 2956 | goto err; |
| 2957 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 2958 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2959 | memset(identity, 0, sizeof(identity)); |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2960 | /* Allocate maximum size buffer */ |
| 2961 | pmslen = PSK_MAX_PSK_LEN * 2 + 4; |
| 2962 | pms = OPENSSL_malloc(pmslen); |
| 2963 | if (!pms) |
| 2964 | goto memerr; |
| 2965 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2966 | psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint, |
| 2967 | identity, sizeof(identity) - 1, |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2968 | pms, pmslen); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2969 | if (psk_len > PSK_MAX_PSK_LEN) { |
| 2970 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2971 | ERR_R_INTERNAL_ERROR); |
| 2972 | goto psk_err; |
| 2973 | } else if (psk_len == 0) { |
| 2974 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2975 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 2976 | goto psk_err; |
| 2977 | } |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2978 | /* Change pmslen to real length */ |
| 2979 | pmslen = 2 + psk_len + 2 + psk_len; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2980 | identity[PSK_MAX_IDENTITY_LEN + 1] = '\0'; |
| 2981 | identity_len = strlen(identity); |
| 2982 | if (identity_len > PSK_MAX_IDENTITY_LEN) { |
| 2983 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 2984 | ERR_R_INTERNAL_ERROR); |
| 2985 | goto psk_err; |
| 2986 | } |
| 2987 | /* create PSK pre_master_secret */ |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 2988 | t = pms; |
| 2989 | memmove(pms + psk_len + 4, pms, psk_len); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2990 | s2n(psk_len, t); |
| 2991 | memset(t, 0, psk_len); |
| 2992 | t += psk_len; |
| 2993 | s2n(psk_len, t); |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 2994 | |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 2995 | OPENSSL_free(s->session->psk_identity_hint); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 2996 | s->session->psk_identity_hint = |
| 2997 | BUF_strdup(s->ctx->psk_identity_hint); |
| 2998 | if (s->ctx->psk_identity_hint != NULL |
| 2999 | && s->session->psk_identity_hint == NULL) { |
| 3000 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 3001 | ERR_R_MALLOC_FAILURE); |
| 3002 | goto psk_err; |
| 3003 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 3004 | |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 3005 | OPENSSL_free(s->session->psk_identity); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3006 | s->session->psk_identity = BUF_strdup(identity); |
| 3007 | if (s->session->psk_identity == NULL) { |
| 3008 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 3009 | ERR_R_MALLOC_FAILURE); |
| 3010 | goto psk_err; |
| 3011 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 3012 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3013 | s2n(identity_len, p); |
| 3014 | memcpy(p, identity, identity_len); |
| 3015 | n = 2 + identity_len; |
| 3016 | psk_err = 0; |
| 3017 | psk_err: |
| 3018 | OPENSSL_cleanse(identity, sizeof(identity)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3019 | if (psk_err != 0) { |
| 3020 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 3021 | goto err; |
| 3022 | } |
| 3023 | } |
Nils Larsch | ddac197 | 2006-03-10 23:06:27 +0000 | [diff] [blame] | 3024 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3025 | else { |
| 3026 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 3027 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
| 3028 | goto err; |
| 3029 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3030 | |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 3031 | if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) { |
Matt Caswell | 77d514c | 2015-03-09 15:33:46 +0000 | [diff] [blame] | 3032 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 3033 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
| 3034 | goto err; |
| 3035 | } |
| 3036 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3037 | s->state = SSL3_ST_CW_KEY_EXCH_B; |
| 3038 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3039 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3040 | /* SSL3_ST_CW_KEY_EXCH_B */ |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 3041 | n = ssl_do_write(s); |
| 3042 | #ifndef OPENSSL_NO_SRP |
| 3043 | /* Check for SRP */ |
| 3044 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
| 3045 | /* |
| 3046 | * If everything written generate master key: no need to save PMS as |
| 3047 | * SRP_generate_client_master_secret generates it internally. |
| 3048 | */ |
| 3049 | if (n > 0) { |
| 3050 | if ((s->session->master_key_length = |
| 3051 | SRP_generate_client_master_secret(s, |
| 3052 | s->session->master_key)) < |
| 3053 | 0) { |
| 3054 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, |
| 3055 | ERR_R_INTERNAL_ERROR); |
| 3056 | goto err; |
| 3057 | } |
| 3058 | } |
| 3059 | } else |
| 3060 | #endif |
| 3061 | /* If we haven't written everything save PMS */ |
| 3062 | if (n <= 0) { |
| 3063 | s->cert->pms = pms; |
| 3064 | s->cert->pmslen = pmslen; |
| 3065 | } else { |
| 3066 | /* If we don't have a PMS restore */ |
| 3067 | if (pms == NULL) { |
| 3068 | pms = s->cert->pms; |
| 3069 | pmslen = s->cert->pmslen; |
| 3070 | } |
| 3071 | if (pms == NULL) { |
| 3072 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3073 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
| 3074 | goto err; |
| 3075 | } |
| 3076 | s->session->master_key_length = |
| 3077 | s->method->ssl3_enc->generate_master_secret(s, |
| 3078 | s-> |
| 3079 | session->master_key, |
| 3080 | pms, pmslen); |
Rich Salz | 4b45c6e | 2015-04-30 17:57:32 -0400 | [diff] [blame] | 3081 | OPENSSL_clear_free(pms, pmslen); |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 3082 | s->cert->pms = NULL; |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 3083 | if (s->session->master_key_length < 0) { |
Matt Caswell | 69f6823 | 2015-03-06 14:37:17 +0000 | [diff] [blame] | 3084 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3085 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); |
| 3086 | goto err; |
| 3087 | } |
Dr. Stephen Henson | c660ec6 | 2015-01-23 02:41:09 +0000 | [diff] [blame] | 3088 | } |
| 3089 | return n; |
| 3090 | memerr: |
| 3091 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3092 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3093 | err: |
Rich Salz | 4b45c6e | 2015-04-30 17:57:32 -0400 | [diff] [blame] | 3094 | OPENSSL_clear_free(pms, pmslen); |
| 3095 | s->cert->pms = NULL; |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 3096 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3097 | BN_CTX_free(bn_ctx); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 3098 | OPENSSL_free(encodedPoint); |
Rich Salz | 8fdc373 | 2015-03-25 18:35:24 -0400 | [diff] [blame] | 3099 | EC_KEY_free(clnt_ecdh); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3100 | EVP_PKEY_free(srvr_pub_pkey); |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 3101 | #endif |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 3102 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3103 | return (-1); |
| 3104 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3105 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 3106 | int ssl3_send_client_verify(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3107 | { |
| 3108 | unsigned char *p; |
| 3109 | unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; |
| 3110 | EVP_PKEY *pkey; |
| 3111 | EVP_PKEY_CTX *pctx = NULL; |
| 3112 | EVP_MD_CTX mctx; |
| 3113 | unsigned u = 0; |
| 3114 | unsigned long n; |
| 3115 | int j; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3116 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3117 | EVP_MD_CTX_init(&mctx); |
Dr. Stephen Henson | 855a54a | 2011-05-12 17:35:03 +0000 | [diff] [blame] | 3118 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3119 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) { |
| 3120 | p = ssl_handshake_start(s); |
| 3121 | pkey = s->cert->key->privatekey; |
Dr. Stephen Henson | 0e1dba9 | 2007-10-26 12:06:36 +0000 | [diff] [blame] | 3122 | /* Create context from key and test if sha1 is allowed as digest */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3123 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 3124 | EVP_PKEY_sign_init(pctx); |
| 3125 | if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { |
| 3126 | if (!SSL_USE_SIGALGS(s)) |
| 3127 | s->method->ssl3_enc->cert_verify_mac(s, |
| 3128 | NID_sha1, |
| 3129 | &(data |
| 3130 | [MD5_DIGEST_LENGTH])); |
| 3131 | } else { |
| 3132 | ERR_clear_error(); |
| 3133 | } |
| 3134 | /* |
| 3135 | * For TLS v1.2 send signature algorithm and signature using agreed |
| 3136 | * digest and cached handshake records. |
| 3137 | */ |
| 3138 | if (SSL_USE_SIGALGS(s)) { |
| 3139 | long hdatalen = 0; |
| 3140 | void *hdata; |
| 3141 | const EVP_MD *md = s->cert->key->digest; |
| 3142 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); |
| 3143 | if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) { |
| 3144 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
| 3145 | goto err; |
| 3146 | } |
| 3147 | p += 2; |
Dr. Stephen Henson | 855a54a | 2011-05-12 17:35:03 +0000 | [diff] [blame] | 3148 | #ifdef SSL_DEBUG |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3149 | fprintf(stderr, "Using TLS 1.2 with client alg %s\n", |
| 3150 | EVP_MD_name(md)); |
Dr. Stephen Henson | 855a54a | 2011-05-12 17:35:03 +0000 | [diff] [blame] | 3151 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3152 | if (!EVP_SignInit_ex(&mctx, md, NULL) |
| 3153 | || !EVP_SignUpdate(&mctx, hdata, hdatalen) |
| 3154 | || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { |
| 3155 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB); |
| 3156 | goto err; |
| 3157 | } |
| 3158 | s2n(u, p); |
| 3159 | n = u + 4; |
Dr. Stephen Henson | 0cfb0e7 | 2015-01-23 02:49:16 +0000 | [diff] [blame] | 3160 | /* |
| 3161 | * For extended master secret we've already digested cached |
| 3162 | * records. |
| 3163 | */ |
| 3164 | if (s->session->flags & SSL_SESS_FLAG_EXTMS) { |
| 3165 | BIO_free(s->s3->handshake_buffer); |
| 3166 | s->s3->handshake_buffer = NULL; |
| 3167 | s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; |
| 3168 | } else if (!ssl3_digest_cached_records(s)) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3169 | goto err; |
| 3170 | } else |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3171 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3172 | if (pkey->type == EVP_PKEY_RSA) { |
| 3173 | s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0])); |
| 3174 | if (RSA_sign(NID_md5_sha1, data, |
| 3175 | MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, |
| 3176 | &(p[2]), &u, pkey->pkey.rsa) <= 0) { |
| 3177 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB); |
| 3178 | goto err; |
| 3179 | } |
| 3180 | s2n(u, p); |
| 3181 | n = u + 2; |
| 3182 | } else |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3183 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3184 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3185 | if (pkey->type == EVP_PKEY_DSA) { |
| 3186 | if (!DSA_sign(pkey->save_type, |
| 3187 | &(data[MD5_DIGEST_LENGTH]), |
| 3188 | SHA_DIGEST_LENGTH, &(p[2]), |
| 3189 | (unsigned int *)&j, pkey->pkey.dsa)) { |
| 3190 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB); |
| 3191 | goto err; |
| 3192 | } |
| 3193 | s2n(j, p); |
| 3194 | n = j + 2; |
| 3195 | } else |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3196 | #endif |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 3197 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3198 | if (pkey->type == EVP_PKEY_EC) { |
| 3199 | if (!ECDSA_sign(pkey->save_type, |
| 3200 | &(data[MD5_DIGEST_LENGTH]), |
| 3201 | SHA_DIGEST_LENGTH, &(p[2]), |
| 3202 | (unsigned int *)&j, pkey->pkey.ec)) { |
| 3203 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB); |
| 3204 | goto err; |
| 3205 | } |
| 3206 | s2n(j, p); |
| 3207 | n = j + 2; |
| 3208 | } else |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 3209 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3210 | if (pkey->type == NID_id_GostR3410_94 |
| 3211 | || pkey->type == NID_id_GostR3410_2001) { |
| 3212 | unsigned char signbuf[64]; |
| 3213 | int i; |
| 3214 | size_t sigsize = 64; |
| 3215 | s->method->ssl3_enc->cert_verify_mac(s, |
| 3216 | NID_id_GostR3411_94, data); |
| 3217 | if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { |
| 3218 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
| 3219 | goto err; |
| 3220 | } |
| 3221 | for (i = 63, j = 0; i >= 0; j++, i--) { |
| 3222 | p[2 + j] = signbuf[i]; |
| 3223 | } |
| 3224 | s2n(j, p); |
| 3225 | n = j + 2; |
| 3226 | } else { |
| 3227 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
| 3228 | goto err; |
| 3229 | } |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 3230 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) { |
Matt Caswell | 77d514c | 2015-03-09 15:33:46 +0000 | [diff] [blame] | 3231 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); |
| 3232 | goto err; |
| 3233 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3234 | s->state = SSL3_ST_CW_CERT_VRFY_B; |
| 3235 | } |
| 3236 | EVP_MD_CTX_cleanup(&mctx); |
| 3237 | EVP_PKEY_CTX_free(pctx); |
| 3238 | return ssl_do_write(s); |
| 3239 | err: |
| 3240 | EVP_MD_CTX_cleanup(&mctx); |
| 3241 | EVP_PKEY_CTX_free(pctx); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 3242 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3243 | return (-1); |
| 3244 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3245 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3246 | /* |
| 3247 | * Check a certificate can be used for client authentication. Currently check |
| 3248 | * cert exists, if we have a suitable digest for TLS 1.2 if static DH client |
| 3249 | * certificates can be used and optionally checks suitability for Suite B. |
Dr. Stephen Henson | 0d60939 | 2012-01-25 14:51:49 +0000 | [diff] [blame] | 3250 | */ |
| 3251 | static int ssl3_check_client_certificate(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3252 | { |
| 3253 | unsigned long alg_k; |
| 3254 | if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) |
| 3255 | return 0; |
| 3256 | /* If no suitable signature algorithm can't use certificate */ |
| 3257 | if (SSL_USE_SIGALGS(s) && !s->cert->key->digest) |
| 3258 | return 0; |
| 3259 | /* |
| 3260 | * If strict mode check suitability of chain before using it. This also |
| 3261 | * adjusts suite B digest if necessary. |
| 3262 | */ |
| 3263 | if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && |
| 3264 | !tls1_check_chain(s, NULL, NULL, NULL, -2)) |
| 3265 | return 0; |
| 3266 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 3267 | /* See if we can use client certificate for fixed DH */ |
| 3268 | if (alg_k & (SSL_kDHr | SSL_kDHd)) { |
| 3269 | SESS_CERT *scert = s->session->sess_cert; |
| 3270 | int i = scert->peer_cert_type; |
| 3271 | EVP_PKEY *clkey = NULL, *spkey = NULL; |
| 3272 | clkey = s->cert->key->privatekey; |
| 3273 | /* If client key not DH assume it can be used */ |
| 3274 | if (EVP_PKEY_id(clkey) != EVP_PKEY_DH) |
| 3275 | return 1; |
| 3276 | if (i >= 0) |
| 3277 | spkey = X509_get_pubkey(scert->peer_pkeys[i].x509); |
| 3278 | if (spkey) { |
| 3279 | /* Compare server and client parameters */ |
| 3280 | i = EVP_PKEY_cmp_parameters(clkey, spkey); |
| 3281 | EVP_PKEY_free(spkey); |
| 3282 | if (i != 1) |
| 3283 | return 0; |
| 3284 | } |
| 3285 | s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; |
| 3286 | } |
| 3287 | return 1; |
| 3288 | } |
Dr. Stephen Henson | 0d60939 | 2012-01-25 14:51:49 +0000 | [diff] [blame] | 3289 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 3290 | int ssl3_send_client_certificate(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3291 | { |
| 3292 | X509 *x509 = NULL; |
| 3293 | EVP_PKEY *pkey = NULL; |
| 3294 | int i; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3295 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3296 | if (s->state == SSL3_ST_CW_CERT_A) { |
| 3297 | /* Let cert callback update client certificates if required */ |
| 3298 | if (s->cert->cert_cb) { |
| 3299 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 3300 | if (i < 0) { |
| 3301 | s->rwstate = SSL_X509_LOOKUP; |
| 3302 | return -1; |
| 3303 | } |
| 3304 | if (i == 0) { |
| 3305 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 3306 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3307 | return 0; |
| 3308 | } |
| 3309 | s->rwstate = SSL_NOTHING; |
| 3310 | } |
| 3311 | if (ssl3_check_client_certificate(s)) |
| 3312 | s->state = SSL3_ST_CW_CERT_C; |
| 3313 | else |
| 3314 | s->state = SSL3_ST_CW_CERT_B; |
| 3315 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3316 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3317 | /* We need to get a client cert */ |
| 3318 | if (s->state == SSL3_ST_CW_CERT_B) { |
| 3319 | /* |
| 3320 | * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; |
| 3321 | * return(-1); We then get retied later |
| 3322 | */ |
| 3323 | i = 0; |
| 3324 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 3325 | if (i < 0) { |
| 3326 | s->rwstate = SSL_X509_LOOKUP; |
| 3327 | return (-1); |
| 3328 | } |
| 3329 | s->rwstate = SSL_NOTHING; |
| 3330 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { |
| 3331 | s->state = SSL3_ST_CW_CERT_B; |
| 3332 | if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) |
| 3333 | i = 0; |
| 3334 | } else if (i == 1) { |
| 3335 | i = 0; |
| 3336 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, |
| 3337 | SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
| 3338 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3339 | |
Rich Salz | 222561f | 2015-04-30 17:33:59 -0400 | [diff] [blame] | 3340 | X509_free(x509); |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 3341 | EVP_PKEY_free(pkey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3342 | if (i && !ssl3_check_client_certificate(s)) |
| 3343 | i = 0; |
| 3344 | if (i == 0) { |
| 3345 | if (s->version == SSL3_VERSION) { |
| 3346 | s->s3->tmp.cert_req = 0; |
| 3347 | ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 3348 | return (1); |
| 3349 | } else { |
| 3350 | s->s3->tmp.cert_req = 2; |
Dr. Stephen Henson | dab18ab | 2015-05-07 00:04:48 +0100 | [diff] [blame^] | 3351 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) { |
| 3352 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 3353 | s->state = SSL_ST_ERR; |
| 3354 | return 0; |
| 3355 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3358 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3359 | /* Ok, we have a cert */ |
| 3360 | s->state = SSL3_ST_CW_CERT_C; |
| 3361 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3362 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3363 | if (s->state == SSL3_ST_CW_CERT_C) { |
| 3364 | s->state = SSL3_ST_CW_CERT_D; |
| 3365 | if (!ssl3_output_cert_chain(s, |
| 3366 | (s->s3->tmp.cert_req == |
| 3367 | 2) ? NULL : s->cert->key)) { |
| 3368 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); |
| 3369 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Matt Caswell | cc273a9 | 2015-04-30 11:32:35 +0100 | [diff] [blame] | 3370 | s->state = SSL_ST_ERR; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3371 | return 0; |
| 3372 | } |
| 3373 | } |
| 3374 | /* SSL3_ST_CW_CERT_D */ |
| 3375 | return ssl_do_write(s); |
| 3376 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3377 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3378 | #define has_bits(i,m) (((i)&(m)) == (m)) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3379 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 3380 | int ssl3_check_cert_and_algorithm(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3381 | { |
| 3382 | int i, idx; |
| 3383 | long alg_k, alg_a; |
| 3384 | EVP_PKEY *pkey = NULL; |
| 3385 | SESS_CERT *sc; |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3386 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3387 | RSA *rsa; |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 3388 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3389 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3390 | DH *dh; |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 3391 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3392 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3393 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 3394 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3395 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3396 | /* we don't have a certificate */ |
| 3397 | if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK)) |
| 3398 | return (1); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3399 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3400 | sc = s->session->sess_cert; |
| 3401 | if (sc == NULL) { |
| 3402 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); |
| 3403 | goto err; |
| 3404 | } |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3405 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3406 | rsa = s->session->sess_cert->peer_rsa_tmp; |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 3407 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3408 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3409 | dh = s->session->sess_cert->peer_dh_tmp; |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 3410 | #endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3411 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3412 | /* This is the passed certificate */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3413 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3414 | idx = sc->peer_cert_type; |
Rich Salz | 10bf4fc | 2015-03-10 19:09:27 -0400 | [diff] [blame] | 3415 | #ifndef OPENSSL_NO_EC |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3416 | if (idx == SSL_PKEY_ECC) { |
| 3417 | if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) { |
| 3418 | /* check failed */ |
| 3419 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); |
| 3420 | goto f_err; |
| 3421 | } else { |
| 3422 | return 1; |
| 3423 | } |
| 3424 | } else if (alg_a & SSL_aECDSA) { |
| 3425 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3426 | SSL_R_MISSING_ECDSA_SIGNING_CERT); |
| 3427 | goto f_err; |
| 3428 | } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) { |
| 3429 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT); |
| 3430 | goto f_err; |
| 3431 | } |
Bodo Möller | ea26226 | 2002-08-09 08:56:08 +0000 | [diff] [blame] | 3432 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3433 | pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509); |
| 3434 | i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey); |
| 3435 | EVP_PKEY_free(pkey); |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3436 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3437 | /* Check that we have a certificate if we require one */ |
| 3438 | if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { |
| 3439 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3440 | SSL_R_MISSING_RSA_SIGNING_CERT); |
| 3441 | goto f_err; |
| 3442 | } |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3443 | #ifndef OPENSSL_NO_DSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3444 | else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { |
| 3445 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3446 | SSL_R_MISSING_DSA_SIGNING_CERT); |
| 3447 | goto f_err; |
| 3448 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3449 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3450 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3451 | if ((alg_k & SSL_kRSA) && |
| 3452 | !(has_bits(i, EVP_PK_RSA | EVP_PKT_ENC) || (rsa != NULL))) { |
| 3453 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3454 | SSL_R_MISSING_RSA_ENCRYPTING_CERT); |
| 3455 | goto f_err; |
| 3456 | } |
Ulf Möller | 79df9d6 | 1999-04-27 03:19:12 +0000 | [diff] [blame] | 3457 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3458 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3459 | if ((alg_k & SSL_kDHE) && |
| 3460 | !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || (dh != NULL))) { |
| 3461 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_DH_KEY); |
| 3462 | goto f_err; |
| 3463 | } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) && |
| 3464 | !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) { |
| 3465 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3466 | SSL_R_MISSING_DH_RSA_CERT); |
| 3467 | goto f_err; |
| 3468 | } |
| 3469 | # ifndef OPENSSL_NO_DSA |
| 3470 | else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) && |
| 3471 | !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) { |
| 3472 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3473 | SSL_R_MISSING_DH_DSA_CERT); |
| 3474 | goto f_err; |
| 3475 | } |
| 3476 | # endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3477 | #endif |
| 3478 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3479 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i, EVP_PKT_EXP)) { |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3480 | #ifndef OPENSSL_NO_RSA |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3481 | if (alg_k & SSL_kRSA) { |
| 3482 | if (rsa == NULL |
| 3483 | || RSA_size(rsa) * 8 > |
| 3484 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { |
| 3485 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3486 | SSL_R_MISSING_EXPORT_TMP_RSA_KEY); |
| 3487 | goto f_err; |
| 3488 | } |
| 3489 | } else |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3490 | #endif |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame] | 3491 | #ifndef OPENSSL_NO_DH |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3492 | if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) { |
| 3493 | if (dh == NULL |
| 3494 | || DH_size(dh) * 8 > |
| 3495 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { |
| 3496 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3497 | SSL_R_MISSING_EXPORT_TMP_DH_KEY); |
| 3498 | goto f_err; |
| 3499 | } |
| 3500 | } else |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 3501 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3502 | { |
| 3503 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, |
| 3504 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 3505 | goto f_err; |
| 3506 | } |
| 3507 | } |
| 3508 | return (1); |
| 3509 | f_err: |
| 3510 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 3511 | err: |
| 3512 | return (0); |
| 3513 | } |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 3514 | |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 3515 | #ifndef OPENSSL_NO_TLSEXT |
Emilia Kasper | 6e3d015 | 2015-04-21 18:12:58 +0200 | [diff] [blame] | 3516 | /* |
| 3517 | * Normally, we can tell if the server is resuming the session from |
| 3518 | * the session ID. EAP-FAST (RFC 4851), however, relies on the next server |
| 3519 | * message after the ServerHello to determine if the server is resuming. |
| 3520 | * Therefore, we allow EAP-FAST to peek ahead. |
| 3521 | * ssl3_check_finished returns 1 if we are resuming from an external |
| 3522 | * pre-shared secret, we have a "ticket" and the next server handshake message |
| 3523 | * is Finished; and 0 otherwise. It returns -1 upon an error. |
| 3524 | */ |
| 3525 | static int ssl3_check_finished(SSL *s) |
| 3526 | { |
| 3527 | int ok = 0; |
| 3528 | |
| 3529 | if (s->version < TLS1_VERSION || !s->tls_session_secret_cb || |
| 3530 | !s->session->tlsext_tick) |
| 3531 | return 0; |
| 3532 | |
| 3533 | /* Need to permit this temporarily, in case the next message is Finished. */ |
| 3534 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
| 3535 | /* |
| 3536 | * This function is called when we might get a Certificate message instead, |
| 3537 | * so permit appropriate message length. |
| 3538 | * We ignore the return value as we're only interested in the message type |
| 3539 | * and not its length. |
| 3540 | */ |
| 3541 | s->method->ssl_get_message(s, |
| 3542 | SSL3_ST_CR_CERT_A, |
| 3543 | SSL3_ST_CR_CERT_B, |
| 3544 | -1, s->max_cert_list, &ok); |
| 3545 | s->s3->flags &= ~SSL3_FLAGS_CCS_OK; |
| 3546 | |
| 3547 | if (!ok) |
| 3548 | return -1; |
| 3549 | |
| 3550 | s->s3->tmp.reuse_message = 1; |
| 3551 | |
| 3552 | if (s->s3->tmp.message_type == SSL3_MT_FINISHED) |
| 3553 | return 1; |
| 3554 | |
| 3555 | /* If we're not done, then the CCS arrived early and we should bail. */ |
| 3556 | if (s->s3->change_cipher_spec) { |
| 3557 | SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY); |
| 3558 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 3559 | return -1; |
| 3560 | } |
| 3561 | |
| 3562 | return 0; |
| 3563 | } |
| 3564 | |
Ben Laurie | bf48836 | 2010-09-05 17:14:01 +0000 | [diff] [blame] | 3565 | # ifndef OPENSSL_NO_NEXTPROTONEG |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 3566 | int ssl3_send_next_proto(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3567 | { |
| 3568 | unsigned int len, padding_len; |
| 3569 | unsigned char *d; |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 3570 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3571 | if (s->state == SSL3_ST_CW_NEXT_PROTO_A) { |
| 3572 | len = s->next_proto_negotiated_len; |
| 3573 | padding_len = 32 - ((len + 2) % 32); |
| 3574 | d = (unsigned char *)s->init_buf->data; |
| 3575 | d[4] = len; |
| 3576 | memcpy(d + 5, s->next_proto_negotiated, len); |
| 3577 | d[5 + len] = padding_len; |
| 3578 | memset(d + 6 + len, 0, padding_len); |
| 3579 | *(d++) = SSL3_MT_NEXT_PROTO; |
| 3580 | l2n3(2 + len + padding_len, d); |
| 3581 | s->state = SSL3_ST_CW_NEXT_PROTO_B; |
| 3582 | s->init_num = 4 + 2 + len + padding_len; |
| 3583 | s->init_off = 0; |
| 3584 | } |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 3585 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3586 | return ssl3_do_write(s, SSL3_RT_HANDSHAKE); |
| 3587 | } |
Ben Laurie | ee2ffc2 | 2010-07-28 10:06:55 +0000 | [diff] [blame] | 3588 | # endif |
Dr. Stephen Henson | 6434abb | 2007-08-11 23:18:29 +0000 | [diff] [blame] | 3589 | #endif |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3590 | |
| 3591 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3592 | { |
| 3593 | int i = 0; |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3594 | #ifndef OPENSSL_NO_ENGINE |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3595 | if (s->ctx->client_cert_engine) { |
| 3596 | i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, |
| 3597 | SSL_get_client_CA_list(s), |
| 3598 | px509, ppkey, NULL, NULL, NULL); |
| 3599 | if (i != 0) |
| 3600 | return i; |
| 3601 | } |
Dr. Stephen Henson | 368888b | 2008-06-01 22:33:24 +0000 | [diff] [blame] | 3602 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 3603 | if (s->ctx->client_cert_cb) |
| 3604 | i = s->ctx->client_cert_cb(s, px509, ppkey); |
| 3605 | return i; |
| 3606 | } |