Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | /* Undefined in Google code. We've never enabled this workaround |
| 150 | * #define REUSE_CIPHER_BUG */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | #define NETSCAPE_HANG_BUG |
| 152 | |
| 153 | #include <stdio.h> |
| 154 | |
| 155 | #include <openssl/bn.h> |
| 156 | #include <openssl/buf.h> |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 157 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | #include <openssl/cipher.h> |
| 159 | #include <openssl/dh.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | #include <openssl/ec.h> |
| 161 | #include <openssl/ecdsa.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | #include <openssl/evp.h> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
| 166 | #include <openssl/obj.h> |
| 167 | #include <openssl/rand.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | #include <openssl/x509.h> |
| 170 | |
| 171 | #include "ssl_locl.h" |
| 172 | #include "../crypto/dh/internal.h" |
| 173 | |
| 174 | static const SSL_METHOD *ssl3_get_server_method(int ver); |
| 175 | |
| 176 | static const SSL_METHOD *ssl3_get_server_method(int ver) |
| 177 | { |
| 178 | if (ver == SSL3_VERSION) |
| 179 | return(SSLv3_server_method()); |
| 180 | else |
| 181 | return(NULL); |
| 182 | } |
| 183 | |
| 184 | IMPLEMENT_ssl3_meth_func(SSLv3_server_method, |
| 185 | ssl3_accept, |
| 186 | ssl_undefined_function, |
| 187 | ssl3_get_server_method) |
| 188 | |
| 189 | int ssl3_accept(SSL *s) |
| 190 | { |
| 191 | BUF_MEM *buf; |
| 192 | unsigned long alg_k; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 195 | int ret= -1; |
| 196 | int new_state,state,skip=0; |
| 197 | |
| 198 | ERR_clear_error(); |
| 199 | ERR_clear_system_error(); |
| 200 | |
| 201 | if (s->info_callback != NULL) |
| 202 | cb=s->info_callback; |
| 203 | else if (s->ctx->info_callback != NULL) |
| 204 | cb=s->ctx->info_callback; |
| 205 | |
| 206 | /* init things to blank */ |
| 207 | s->in_handshake++; |
| 208 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 209 | |
| 210 | if (s->cert == NULL) |
| 211 | { |
| 212 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET); |
| 213 | return(-1); |
| 214 | } |
| 215 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | for (;;) |
| 217 | { |
| 218 | state=s->state; |
| 219 | |
| 220 | switch (s->state) |
| 221 | { |
| 222 | case SSL_ST_RENEGOTIATE: |
| 223 | s->renegotiate=1; |
| 224 | /* s->state=SSL_ST_ACCEPT; */ |
| 225 | |
| 226 | case SSL_ST_BEFORE: |
| 227 | case SSL_ST_ACCEPT: |
| 228 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: |
| 229 | case SSL_ST_OK|SSL_ST_ACCEPT: |
| 230 | |
| 231 | s->server=1; |
| 232 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 233 | |
| 234 | if ((s->version>>8) != 3) |
| 235 | { |
| 236 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 237 | return -1; |
| 238 | } |
| 239 | s->type=SSL_ST_ACCEPT; |
| 240 | |
| 241 | if (s->init_buf == NULL) |
| 242 | { |
| 243 | if ((buf=BUF_MEM_new()) == NULL) |
| 244 | { |
| 245 | ret= -1; |
| 246 | goto end; |
| 247 | } |
| 248 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 249 | { |
| 250 | ret= -1; |
| 251 | goto end; |
| 252 | } |
| 253 | s->init_buf=buf; |
| 254 | } |
| 255 | |
| 256 | if (!ssl3_setup_buffers(s)) |
| 257 | { |
| 258 | ret= -1; |
| 259 | goto end; |
| 260 | } |
| 261 | |
| 262 | s->init_num=0; |
| 263 | s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE; |
| 264 | s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY; |
| 265 | |
| 266 | if (s->state != SSL_ST_RENEGOTIATE) |
| 267 | { |
| 268 | /* Ok, we now need to push on a buffering BIO so that |
| 269 | * the output is sent in a way that TCP likes :-) |
| 270 | */ |
| 271 | if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } |
| 272 | |
| 273 | ssl3_init_finished_mac(s); |
| 274 | s->state=SSL3_ST_SR_CLNT_HELLO_A; |
| 275 | s->ctx->stats.sess_accept++; |
| 276 | } |
| 277 | else if (!s->s3->send_connection_binding && |
| 278 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 279 | { |
| 280 | /* Server attempting to renegotiate with |
| 281 | * client that doesn't support secure |
| 282 | * renegotiation. |
| 283 | */ |
| 284 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 285 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
| 286 | ret = -1; |
| 287 | goto end; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | /* s->state == SSL_ST_RENEGOTIATE, |
| 292 | * we will just send a HelloRequest */ |
| 293 | s->ctx->stats.sess_accept_renegotiate++; |
| 294 | s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 295 | } |
| 296 | break; |
| 297 | |
| 298 | case SSL3_ST_SW_HELLO_REQ_A: |
| 299 | case SSL3_ST_SW_HELLO_REQ_B: |
| 300 | |
| 301 | s->shutdown=0; |
| 302 | ret=ssl3_send_hello_request(s); |
| 303 | if (ret <= 0) goto end; |
| 304 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; |
| 305 | s->state=SSL3_ST_SW_FLUSH; |
| 306 | s->init_num=0; |
| 307 | |
| 308 | ssl3_init_finished_mac(s); |
| 309 | break; |
| 310 | |
| 311 | case SSL3_ST_SW_HELLO_REQ_C: |
| 312 | s->state=SSL_ST_OK; |
| 313 | break; |
| 314 | |
| 315 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 316 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 317 | case SSL3_ST_SR_CLNT_HELLO_C: |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | case SSL3_ST_SR_CLNT_HELLO_D: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | s->shutdown=0; |
| 320 | ret=ssl3_get_client_hello(s); |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | if (ret == PENDING_SESSION) { |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | s->rwstate = SSL_PENDING_SESSION; |
| 323 | goto end; |
| 324 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | if (ret == CERTIFICATE_SELECTION_PENDING) |
| 326 | { |
| 327 | s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 328 | goto end; |
| 329 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 330 | if (ret <= 0) goto end; |
| 331 | s->renegotiate = 2; |
| 332 | s->state=SSL3_ST_SW_SRVR_HELLO_A; |
| 333 | s->init_num=0; |
| 334 | break; |
| 335 | |
| 336 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 337 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 338 | ret=ssl3_send_server_hello(s); |
| 339 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 340 | if (s->hit) |
| 341 | { |
| 342 | if (s->tlsext_ticket_expected) |
| 343 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
| 344 | else |
| 345 | s->state=SSL3_ST_SW_CHANGE_A; |
| 346 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | else |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 348 | s->state = SSL3_ST_SW_CERT_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | s->init_num = 0; |
| 350 | break; |
| 351 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | case SSL3_ST_SW_CERT_A: |
| 353 | case SSL3_ST_SW_CERT_B: |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 354 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | { |
| 356 | ret=ssl3_send_server_certificate(s); |
| 357 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 358 | if (s->tlsext_status_expected) |
| 359 | s->state=SSL3_ST_SW_CERT_STATUS_A; |
| 360 | else |
| 361 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | skip = 1; |
| 366 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 367 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 368 | s->init_num=0; |
| 369 | break; |
| 370 | |
| 371 | case SSL3_ST_SW_KEY_EXCH_A: |
| 372 | case SSL3_ST_SW_KEY_EXCH_B: |
| 373 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 374 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
| 376 | /* clear this, it may get reset by |
| 377 | * send_server_key_exchange */ |
| 378 | if ((s->options & SSL_OP_EPHEMERAL_RSA) |
| 379 | ) |
| 380 | /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key |
| 381 | * even when forbidden by protocol specs |
| 382 | * (handshake may fail as clients are not required to |
| 383 | * be able to handle this) */ |
| 384 | s->s3->tmp.use_rsa_tmp=1; |
| 385 | else |
| 386 | s->s3->tmp.use_rsa_tmp=0; |
| 387 | |
| 388 | |
| 389 | /* only send if a DH key exchange, fortezza or |
| 390 | * RSA but we have a sign only certificate |
| 391 | * |
| 392 | * PSK: may send PSK identity hints |
| 393 | * |
| 394 | * For ECC ciphersuites, we send a serverKeyExchange |
| 395 | * message only if the cipher suite is either |
| 396 | * ECDH-anon or ECDHE. In other cases, the |
| 397 | * server certificate contains the server's |
| 398 | * public key for key exchange. |
| 399 | */ |
| 400 | if (s->s3->tmp.use_rsa_tmp |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | /* PSK: send ServerKeyExchange if either: |
| 402 | * - PSK identity hint is provided, or |
| 403 | * - the key exchange is kEECDH. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 404 | #ifndef OPENSSL_NO_PSK |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | || ((alg_a & SSL_aPSK) && ((alg_k & SSL_kEECDH) || s->session->psk_identity_hint)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | #endif |
| 407 | || (alg_k & SSL_kEDH) |
| 408 | || (alg_k & SSL_kEECDH) |
| 409 | || ((alg_k & SSL_kRSA) |
| 410 | && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL |
| 411 | || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) |
| 412 | && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) |
| 413 | ) |
| 414 | ) |
| 415 | ) |
| 416 | ) |
| 417 | { |
| 418 | ret=ssl3_send_server_key_exchange(s); |
| 419 | if (ret <= 0) goto end; |
| 420 | } |
| 421 | else |
| 422 | skip=1; |
| 423 | |
| 424 | s->state=SSL3_ST_SW_CERT_REQ_A; |
| 425 | s->init_num=0; |
| 426 | break; |
| 427 | |
| 428 | case SSL3_ST_SW_CERT_REQ_A: |
| 429 | case SSL3_ST_SW_CERT_REQ_B: |
| 430 | if (/* don't request cert unless asked for it: */ |
| 431 | !(s->verify_mode & SSL_VERIFY_PEER) || |
Adam Langley | 509e5ed | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | /* Don't request a certificate if an obc was presented */ |
| 433 | ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 434 | s->s3->tlsext_channel_id_valid) || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 436 | * don't request cert during re-negotiation: */ |
| 437 | ((s->session->peer != NULL) && |
| 438 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 439 | /* never request cert in anonymous ciphersuites |
| 440 | * (see section "Certificate request" in SSL 3 drafts |
| 441 | * and in RFC 2246): */ |
| 442 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 443 | /* ... except when the application insists on verification |
| 444 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 445 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 446 | /* With normal PSK Certificates and |
| 447 | * Certificate Requests are omitted */ |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 448 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | { |
| 450 | /* no cert request */ |
| 451 | skip=1; |
| 452 | s->s3->tmp.cert_request=0; |
| 453 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 454 | if (s->s3->handshake_buffer) |
| 455 | if (!ssl3_digest_cached_records(s)) |
| 456 | return -1; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | s->s3->tmp.cert_request=1; |
| 461 | ret=ssl3_send_certificate_request(s); |
| 462 | if (ret <= 0) goto end; |
| 463 | #ifndef NETSCAPE_HANG_BUG |
| 464 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 465 | #else |
| 466 | s->state=SSL3_ST_SW_FLUSH; |
| 467 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 468 | #endif |
| 469 | s->init_num=0; |
| 470 | } |
| 471 | break; |
| 472 | |
| 473 | case SSL3_ST_SW_SRVR_DONE_A: |
| 474 | case SSL3_ST_SW_SRVR_DONE_B: |
| 475 | ret=ssl3_send_server_done(s); |
| 476 | if (ret <= 0) goto end; |
| 477 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 478 | s->state=SSL3_ST_SW_FLUSH; |
| 479 | s->init_num=0; |
| 480 | break; |
| 481 | |
| 482 | case SSL3_ST_SW_FLUSH: |
| 483 | |
| 484 | /* This code originally checked to see if |
| 485 | * any data was pending using BIO_CTRL_INFO |
| 486 | * and then flushed. This caused problems |
| 487 | * as documented in PR#1939. The proposed |
| 488 | * fix doesn't completely resolve this issue |
| 489 | * as buggy implementations of BIO_CTRL_PENDING |
| 490 | * still exist. So instead we just flush |
| 491 | * unconditionally. |
| 492 | */ |
| 493 | |
| 494 | s->rwstate=SSL_WRITING; |
| 495 | if (BIO_flush(s->wbio) <= 0) |
| 496 | { |
| 497 | ret= -1; |
| 498 | goto end; |
| 499 | } |
| 500 | s->rwstate=SSL_NOTHING; |
| 501 | |
| 502 | s->state=s->s3->tmp.next_state; |
| 503 | break; |
| 504 | |
| 505 | case SSL3_ST_SR_CERT_A: |
| 506 | case SSL3_ST_SR_CERT_B: |
| 507 | /* Check for second client hello (MS SGC) */ |
| 508 | ret = ssl3_check_client_hello(s); |
| 509 | if (ret <= 0) |
| 510 | goto end; |
| 511 | if (ret == 2) |
| 512 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 513 | else { |
| 514 | if (s->s3->tmp.cert_request) |
| 515 | { |
| 516 | ret=ssl3_get_client_certificate(s); |
| 517 | if (ret <= 0) goto end; |
| 518 | } |
| 519 | s->init_num=0; |
| 520 | s->state=SSL3_ST_SR_KEY_EXCH_A; |
| 521 | } |
| 522 | break; |
| 523 | |
| 524 | case SSL3_ST_SR_KEY_EXCH_A: |
| 525 | case SSL3_ST_SR_KEY_EXCH_B: |
| 526 | ret=ssl3_get_client_key_exchange(s); |
| 527 | if (ret <= 0) |
| 528 | goto end; |
| 529 | if (ret == 2) |
| 530 | { |
| 531 | /* For the ECDH ciphersuites when |
| 532 | * the client sends its ECDH pub key in |
| 533 | * a certificate, the CertificateVerify |
| 534 | * message is not sent. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | s->init_num = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | s->state=SSL3_ST_SR_POST_CLIENT_CERT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | } |
| 539 | else if (SSL_USE_SIGALGS(s)) |
| 540 | { |
| 541 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 542 | s->init_num=0; |
| 543 | if (!s->session->peer) |
| 544 | break; |
| 545 | /* For sigalgs freeze the handshake buffer |
| 546 | * at this point and digest cached records. |
| 547 | */ |
| 548 | if (!s->s3->handshake_buffer) |
| 549 | { |
| 550 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 551 | return -1; |
| 552 | } |
| 553 | s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; |
| 554 | if (!ssl3_digest_cached_records(s)) |
| 555 | return -1; |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | int offset=0; |
| 560 | int dgst_num; |
| 561 | |
| 562 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 563 | s->init_num=0; |
| 564 | |
| 565 | /* We need to get hashes here so if there is |
| 566 | * a client cert, it can be verified |
| 567 | * FIXME - digest processing for CertificateVerify |
| 568 | * should be generalized. But it is next step |
| 569 | */ |
| 570 | if (s->s3->handshake_buffer) |
| 571 | if (!ssl3_digest_cached_records(s)) |
| 572 | return -1; |
| 573 | for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++) |
| 574 | if (s->s3->handshake_dgst[dgst_num]) |
| 575 | { |
| 576 | int dgst_size; |
| 577 | |
| 578 | s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset])); |
| 579 | dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]); |
| 580 | if (dgst_size < 0) |
| 581 | { |
| 582 | ret = -1; |
| 583 | goto end; |
| 584 | } |
| 585 | offset+=dgst_size; |
| 586 | } |
| 587 | } |
| 588 | break; |
| 589 | |
| 590 | case SSL3_ST_SR_CERT_VRFY_A: |
| 591 | case SSL3_ST_SR_CERT_VRFY_B: |
| 592 | |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 593 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | /* we should decide if we expected this one */ |
| 595 | ret=ssl3_get_cert_verify(s); |
| 596 | if (ret <= 0) goto end; |
| 597 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | s->state=SSL3_ST_SR_POST_CLIENT_CERT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 599 | s->init_num=0; |
| 600 | break; |
| 601 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 602 | case SSL3_ST_SR_POST_CLIENT_CERT: { |
| 603 | char next_proto_neg = 0; |
| 604 | char channel_id = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 605 | # if !defined(OPENSSL_NO_NEXTPROTONEG) |
| 606 | next_proto_neg = s->s3->next_proto_neg_seen; |
| 607 | # endif |
| 608 | channel_id = s->s3->tlsext_channel_id_valid; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 609 | |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 610 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 611 | if (next_proto_neg) |
| 612 | s->state=SSL3_ST_SR_NEXT_PROTO_A; |
| 613 | else if (channel_id) |
| 614 | s->state=SSL3_ST_SR_CHANNEL_ID_A; |
| 615 | else |
| 616 | s->state=SSL3_ST_SR_FINISHED_A; |
| 617 | break; |
| 618 | } |
| 619 | |
David Benjamin | 6dbd73d | 2014-07-03 15:59:49 -0400 | [diff] [blame] | 620 | #if !defined(OPENSSL_NO_NEXTPROTONEG) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 621 | case SSL3_ST_SR_NEXT_PROTO_A: |
| 622 | case SSL3_ST_SR_NEXT_PROTO_B: |
| 623 | ret=ssl3_get_next_proto(s); |
| 624 | if (ret <= 0) goto end; |
| 625 | s->init_num = 0; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 626 | if (s->s3->tlsext_channel_id_valid) |
| 627 | s->state=SSL3_ST_SR_CHANNEL_ID_A; |
| 628 | else |
| 629 | s->state=SSL3_ST_SR_FINISHED_A; |
| 630 | break; |
| 631 | #endif |
| 632 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 633 | case SSL3_ST_SR_CHANNEL_ID_A: |
| 634 | case SSL3_ST_SR_CHANNEL_ID_B: |
| 635 | ret=ssl3_get_channel_id(s); |
| 636 | if (ret <= 0) goto end; |
| 637 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | s->state=SSL3_ST_SR_FINISHED_A; |
| 639 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 640 | |
| 641 | case SSL3_ST_SR_FINISHED_A: |
| 642 | case SSL3_ST_SR_FINISHED_B: |
Adam Langley | ce7f9ca | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 643 | s->s3->flags |= SSL3_FLAGS_CCS_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 644 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 645 | SSL3_ST_SR_FINISHED_B); |
| 646 | if (ret <= 0) goto end; |
| 647 | if (s->hit) |
| 648 | s->state=SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | else if (s->tlsext_ticket_expected) |
| 650 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | else |
| 652 | s->state=SSL3_ST_SW_CHANGE_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 653 | /* If this is a full handshake with ChannelID then |
| 654 | * record the hashshake hashes in |s->session| in case |
| 655 | * we need them to verify a ChannelID signature on a |
| 656 | * resumption of this session in the future. */ |
| 657 | if (!s->hit && s->s3->tlsext_channel_id_new) |
| 658 | { |
| 659 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 660 | if (ret <= 0) goto end; |
| 661 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 662 | s->init_num=0; |
| 663 | break; |
| 664 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 665 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 666 | case SSL3_ST_SW_SESSION_TICKET_B: |
| 667 | ret=ssl3_send_newsession_ticket(s); |
| 668 | if (ret <= 0) goto end; |
| 669 | s->state=SSL3_ST_SW_CHANGE_A; |
| 670 | s->init_num=0; |
| 671 | break; |
| 672 | |
| 673 | case SSL3_ST_SW_CERT_STATUS_A: |
| 674 | case SSL3_ST_SW_CERT_STATUS_B: |
| 675 | ret=ssl3_send_cert_status(s); |
| 676 | if (ret <= 0) goto end; |
| 677 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 678 | s->init_num=0; |
| 679 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 680 | |
| 681 | case SSL3_ST_SW_CHANGE_A: |
| 682 | case SSL3_ST_SW_CHANGE_B: |
| 683 | |
| 684 | s->session->cipher=s->s3->tmp.new_cipher; |
| 685 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 686 | { ret= -1; goto end; } |
| 687 | |
| 688 | ret=ssl3_send_change_cipher_spec(s, |
| 689 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 690 | |
| 691 | if (ret <= 0) goto end; |
| 692 | s->state=SSL3_ST_SW_FINISHED_A; |
| 693 | s->init_num=0; |
| 694 | |
| 695 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 696 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 697 | { |
| 698 | ret= -1; |
| 699 | goto end; |
| 700 | } |
| 701 | |
| 702 | break; |
| 703 | |
| 704 | case SSL3_ST_SW_FINISHED_A: |
| 705 | case SSL3_ST_SW_FINISHED_B: |
| 706 | ret=ssl3_send_finished(s, |
| 707 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 708 | s->method->ssl3_enc->server_finished_label, |
| 709 | s->method->ssl3_enc->server_finished_label_len); |
| 710 | if (ret <= 0) goto end; |
| 711 | s->state=SSL3_ST_SW_FLUSH; |
| 712 | if (s->hit) |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 713 | s->s3->tmp.next_state=SSL3_ST_SR_POST_CLIENT_CERT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 714 | else |
| 715 | s->s3->tmp.next_state=SSL_ST_OK; |
| 716 | s->init_num=0; |
| 717 | break; |
| 718 | |
| 719 | case SSL_ST_OK: |
| 720 | /* clean a few things up */ |
| 721 | ssl3_cleanup_key_block(s); |
| 722 | |
| 723 | BUF_MEM_free(s->init_buf); |
| 724 | s->init_buf=NULL; |
| 725 | |
| 726 | /* remove buffering on output */ |
| 727 | ssl_free_wbio_buffer(s); |
| 728 | |
| 729 | s->init_num=0; |
| 730 | |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | /* If we aren't retaining peer certificates then we can |
| 732 | * discard it now. */ |
| 733 | if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs) |
| 734 | { |
| 735 | X509_free(s->session->peer); |
| 736 | s->session->peer = NULL; |
| 737 | } |
| 738 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ |
| 740 | { |
| 741 | s->renegotiate=0; |
| 742 | s->new_session=0; |
| 743 | |
| 744 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 745 | |
| 746 | s->ctx->stats.sess_accept_good++; |
| 747 | /* s->server=1; */ |
| 748 | s->handshake_func=ssl3_accept; |
| 749 | |
| 750 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 751 | } |
| 752 | |
| 753 | ret = 1; |
| 754 | goto end; |
| 755 | /* break; */ |
| 756 | |
| 757 | default: |
| 758 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE); |
| 759 | ret= -1; |
| 760 | goto end; |
| 761 | /* break; */ |
| 762 | } |
| 763 | |
| 764 | if (!s->s3->tmp.reuse_message && !skip) |
| 765 | { |
| 766 | if (s->debug) |
| 767 | { |
| 768 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 769 | goto end; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | if ((cb != NULL) && (s->state != state)) |
| 774 | { |
| 775 | new_state=s->state; |
| 776 | s->state=state; |
| 777 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 778 | s->state=new_state; |
| 779 | } |
| 780 | } |
| 781 | skip=0; |
| 782 | } |
| 783 | end: |
| 784 | /* BIO_flush(s->wbio); */ |
| 785 | |
| 786 | s->in_handshake--; |
| 787 | if (cb != NULL) |
| 788 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 789 | return(ret); |
| 790 | } |
| 791 | |
| 792 | int ssl3_send_hello_request(SSL *s) |
| 793 | { |
| 794 | |
| 795 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) |
| 796 | { |
| 797 | ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0); |
| 798 | s->state=SSL3_ST_SW_HELLO_REQ_B; |
| 799 | } |
| 800 | |
| 801 | /* SSL3_ST_SW_HELLO_REQ_B */ |
| 802 | return ssl_do_write(s); |
| 803 | } |
| 804 | |
| 805 | int ssl3_check_client_hello(SSL *s) |
| 806 | { |
| 807 | int ok; |
| 808 | long n; |
| 809 | |
| 810 | /* this function is called when we really expect a Certificate message, |
| 811 | * so permit appropriate message length */ |
| 812 | n=s->method->ssl_get_message(s, |
| 813 | SSL3_ST_SR_CERT_A, |
| 814 | SSL3_ST_SR_CERT_B, |
| 815 | -1, |
| 816 | s->max_cert_list, |
| 817 | &ok); |
| 818 | if (!ok) return((int)n); |
| 819 | s->s3->tmp.reuse_message = 1; |
| 820 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) |
| 821 | { |
| 822 | /* We only allow the client to restart the handshake once per |
| 823 | * negotiation. */ |
| 824 | if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) |
| 825 | { |
| 826 | OPENSSL_PUT_ERROR(SSL, ssl3_check_client_hello, SSL_R_MULTIPLE_SGC_RESTARTS); |
| 827 | return -1; |
| 828 | } |
| 829 | /* Throw away what we have done so far in the current handshake, |
| 830 | * which will now be aborted. (A full SSL_clear would be too much.) */ |
| 831 | #ifndef OPENSSL_NO_DH |
| 832 | if (s->s3->tmp.dh != NULL) |
| 833 | { |
| 834 | DH_free(s->s3->tmp.dh); |
| 835 | s->s3->tmp.dh = NULL; |
| 836 | } |
| 837 | #endif |
| 838 | #ifndef OPENSSL_NO_ECDH |
| 839 | if (s->s3->tmp.ecdh != NULL) |
| 840 | { |
| 841 | EC_KEY_free(s->s3->tmp.ecdh); |
| 842 | s->s3->tmp.ecdh = NULL; |
| 843 | } |
| 844 | #endif |
| 845 | s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE; |
| 846 | return 2; |
| 847 | } |
| 848 | return 1; |
| 849 | } |
| 850 | |
| 851 | int ssl3_get_client_hello(SSL *s) |
| 852 | { |
| 853 | int i,j,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1; |
| 854 | unsigned int cookie_len; |
| 855 | long n; |
| 856 | unsigned long id; |
| 857 | unsigned char *p,*d; |
| 858 | SSL_CIPHER *c; |
| 859 | STACK_OF(SSL_CIPHER) *ciphers=NULL; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 860 | struct ssl_early_callback_ctx early_ctx; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 861 | CBS cbs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 862 | |
| 863 | /* We do this so that we will respond with our native type. |
| 864 | * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, |
| 865 | * This down switching should be handled by a different method. |
| 866 | * If we are SSLv3, we will respond with SSLv3, even if prompted with |
| 867 | * TLSv1. |
| 868 | */ |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 869 | switch (s->state) { |
| 870 | case SSL3_ST_SR_CLNT_HELLO_A: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 871 | s->state=SSL3_ST_SR_CLNT_HELLO_B; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 872 | /* fallthrough */ |
| 873 | case SSL3_ST_SR_CLNT_HELLO_B: |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 874 | s->first_packet=1; |
| 875 | n=s->method->ssl_get_message(s, |
| 876 | SSL3_ST_SR_CLNT_HELLO_B, |
| 877 | SSL3_ST_SR_CLNT_HELLO_C, |
| 878 | SSL3_MT_CLIENT_HELLO, |
| 879 | SSL3_RT_MAX_PLAIN_LENGTH, |
| 880 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 881 | |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 882 | if (!ok) return((int)n); |
| 883 | s->first_packet=0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | |
| 885 | /* If we require cookies and this ClientHello doesn't |
| 886 | * contain one, just return since we do not want to |
| 887 | * allocate any memory yet. So check cookie length... |
| 888 | */ |
| 889 | if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) |
| 890 | { |
| 891 | unsigned int session_length, cookie_length; |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 892 | p = s->init_msg; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | |
| 894 | if (n < 2 + SSL3_RANDOM_SIZE) |
| 895 | return 1; |
| 896 | session_length = *(p + 2 + SSL3_RANDOM_SIZE); |
| 897 | if (n < 2 + SSL3_RANDOM_SIZE + 1 + session_length) |
| 898 | return 1; |
| 899 | cookie_length = |
| 900 | *(p + 2 + SSL3_RANDOM_SIZE + 1 + session_length); |
| 901 | if (cookie_length == 0) |
| 902 | return 1; |
| 903 | } |
| 904 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 905 | /* fallthrough */ |
| 906 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 907 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 908 | /* We have previously parsed the ClientHello message, |
| 909 | * and can't call ssl_get_message again without hashing |
| 910 | * the message into the Finished digest again. */ |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 911 | n = s->init_num; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 912 | |
| 913 | memset(&early_ctx, 0, sizeof(early_ctx)); |
| 914 | early_ctx.ssl = s; |
| 915 | early_ctx.client_hello = s->init_msg; |
| 916 | early_ctx.client_hello_len = n; |
| 917 | if (!ssl_early_callback_init(&early_ctx)) |
| 918 | { |
| 919 | al = SSL_AD_DECODE_ERROR; |
| 920 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_PARSE_FAILED); |
| 921 | goto f_err; |
| 922 | } |
| 923 | |
| 924 | if (s->state == SSL3_ST_SR_CLNT_HELLO_C && |
| 925 | s->ctx->select_certificate_cb != NULL) |
| 926 | { |
| 927 | int ret; |
| 928 | |
| 929 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 930 | ret = s->ctx->select_certificate_cb(&early_ctx); |
| 931 | if (ret == 0) |
| 932 | return CERTIFICATE_SELECTION_PENDING; |
| 933 | else if (ret == -1) |
| 934 | { |
| 935 | /* Connection rejected. */ |
| 936 | al = SSL_AD_ACCESS_DENIED; |
| 937 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED); |
| 938 | goto f_err; |
| 939 | } |
| 940 | } |
| 941 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
David Benjamin | 4c852c5 | 2014-07-02 15:24:48 -0400 | [diff] [blame] | 942 | break; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 943 | default: |
David Benjamin | 4c852c5 | 2014-07-02 15:24:48 -0400 | [diff] [blame] | 944 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 945 | return -1; |
| 946 | } |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 947 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 948 | d = p = s->init_msg; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 949 | |
| 950 | /* use version from inside client hello, not from record header |
| 951 | * (may differ: see RFC 2246, Appendix E, second paragraph) */ |
| 952 | s->client_version=(((int)p[0])<<8)|(int)p[1]; |
| 953 | p+=2; |
| 954 | |
| 955 | if (SSL_IS_DTLS(s) ? (s->client_version > s->version && |
| 956 | s->method->version != DTLS_ANY_VERSION) |
| 957 | : (s->client_version < s->version)) |
| 958 | { |
| 959 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 960 | if ((s->client_version>>8) == SSL3_VERSION_MAJOR && |
| 961 | !s->enc_write_ctx && !s->write_hash) |
| 962 | { |
| 963 | /* similar to ssl3_get_record, send alert using remote version number */ |
| 964 | s->version = s->client_version; |
| 965 | } |
| 966 | al = SSL_AD_PROTOCOL_VERSION; |
| 967 | goto f_err; |
| 968 | } |
| 969 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 970 | /* load the client random */ |
| 971 | memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); |
| 972 | p+=SSL3_RANDOM_SIZE; |
| 973 | |
| 974 | /* get the session-id */ |
| 975 | j= *(p++); |
| 976 | |
| 977 | s->hit=0; |
| 978 | /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation. |
| 979 | * 0.9.7 and later allow this by default, but optionally ignore resumption requests |
| 980 | * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather |
| 981 | * than a change to default behavior so that applications relying on this for security |
| 982 | * won't even compile against older library versions). |
| 983 | * |
| 984 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request |
| 985 | * renegotiation but not a new session (s->new_session remains unset): for servers, |
| 986 | * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 987 | * setting will be ignored. |
| 988 | */ |
| 989 | if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) |
| 990 | { |
| 991 | if (!ssl_get_new_session(s,1)) |
| 992 | goto err; |
| 993 | } |
| 994 | else |
| 995 | { |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 996 | i=ssl_get_prev_session(s, &early_ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 997 | if (i == 1) |
| 998 | { /* previous session */ |
| 999 | s->hit=1; |
| 1000 | } |
| 1001 | else if (i == -1) |
| 1002 | goto err; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1003 | else if (i == PENDING_SESSION) |
| 1004 | { |
| 1005 | ret = PENDING_SESSION; |
| 1006 | goto err; |
| 1007 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1008 | else /* i == 0 */ |
| 1009 | { |
| 1010 | if (!ssl_get_new_session(s,1)) |
| 1011 | goto err; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | p+=j; |
| 1016 | |
| 1017 | if (SSL_IS_DTLS(s)) |
| 1018 | { |
| 1019 | /* cookie stuff */ |
| 1020 | cookie_len = *(p++); |
| 1021 | |
| 1022 | /* |
| 1023 | * The ClientHello may contain a cookie even if the |
| 1024 | * HelloVerify message has not been sent--make sure that it |
| 1025 | * does not cause an overflow. |
| 1026 | */ |
| 1027 | if ( cookie_len > sizeof(s->d1->rcvd_cookie)) |
| 1028 | { |
| 1029 | /* too much data */ |
| 1030 | al = SSL_AD_DECODE_ERROR; |
| 1031 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 1032 | goto f_err; |
| 1033 | } |
| 1034 | |
| 1035 | /* verify the cookie if appropriate option is set. */ |
| 1036 | if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && |
| 1037 | cookie_len > 0) |
| 1038 | { |
| 1039 | memcpy(s->d1->rcvd_cookie, p, cookie_len); |
| 1040 | |
| 1041 | if ( s->ctx->app_verify_cookie_cb != NULL) |
| 1042 | { |
| 1043 | if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie, |
| 1044 | cookie_len) == 0) |
| 1045 | { |
| 1046 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1047 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 1048 | goto f_err; |
| 1049 | } |
| 1050 | /* else cookie verification succeeded */ |
| 1051 | } |
| 1052 | else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie, |
| 1053 | s->d1->cookie_len) != 0) /* default verification */ |
| 1054 | { |
| 1055 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1056 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 1057 | goto f_err; |
| 1058 | } |
| 1059 | /* Set to -2 so if successful we return 2 */ |
| 1060 | ret = -2; |
| 1061 | } |
| 1062 | |
| 1063 | p += cookie_len; |
| 1064 | if (s->method->version == DTLS_ANY_VERSION) |
| 1065 | { |
| 1066 | /* Select version to use */ |
| 1067 | if (s->client_version <= DTLS1_2_VERSION && |
| 1068 | !(s->options & SSL_OP_NO_DTLSv1_2)) |
| 1069 | { |
| 1070 | s->version = DTLS1_2_VERSION; |
| 1071 | s->method = DTLSv1_2_server_method(); |
| 1072 | } |
| 1073 | else if (tls1_suiteb(s)) |
| 1074 | { |
| 1075 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); |
| 1076 | s->version = s->client_version; |
| 1077 | al = SSL_AD_PROTOCOL_VERSION; |
| 1078 | goto f_err; |
| 1079 | } |
| 1080 | else if (s->client_version <= DTLS1_VERSION && |
| 1081 | !(s->options & SSL_OP_NO_DTLSv1)) |
| 1082 | { |
| 1083 | s->version = DTLS1_VERSION; |
| 1084 | s->method = DTLSv1_server_method(); |
| 1085 | } |
| 1086 | else |
| 1087 | { |
| 1088 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 1089 | s->version = s->client_version; |
| 1090 | al = SSL_AD_PROTOCOL_VERSION; |
| 1091 | goto f_err; |
| 1092 | } |
| 1093 | s->session->ssl_version = s->version; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | n2s(p,i); |
| 1098 | if ((i == 0) && (j != 0)) |
| 1099 | { |
| 1100 | /* we need a cipher if we are not resuming a session */ |
| 1101 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1102 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED); |
| 1103 | goto f_err; |
| 1104 | } |
| 1105 | if ((p+i) >= (d+n)) |
| 1106 | { |
| 1107 | /* not enough data */ |
| 1108 | al=SSL_AD_DECODE_ERROR; |
| 1109 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_LENGTH_MISMATCH); |
| 1110 | goto f_err; |
| 1111 | } |
| 1112 | if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) |
| 1113 | == NULL)) |
| 1114 | { |
| 1115 | goto err; |
| 1116 | } |
| 1117 | p+=i; |
| 1118 | |
| 1119 | /* If it is a hit, check that the cipher is in the list */ |
| 1120 | if ((s->hit) && (i > 0)) |
| 1121 | { |
| 1122 | j=0; |
| 1123 | id=s->session->cipher->id; |
| 1124 | |
| 1125 | #ifdef CIPHER_DEBUG |
| 1126 | printf("client sent %d ciphers\n",sk_num(ciphers)); |
| 1127 | #endif |
| 1128 | for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) |
| 1129 | { |
| 1130 | c=sk_SSL_CIPHER_value(ciphers,i); |
| 1131 | #ifdef CIPHER_DEBUG |
| 1132 | printf("client [%2d of %2d]:%s\n", |
| 1133 | i,sk_num(ciphers),SSL_CIPHER_get_name(c)); |
| 1134 | #endif |
| 1135 | if (c->id == id) |
| 1136 | { |
| 1137 | j=1; |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | /* Disabled because it can be used in a ciphersuite downgrade |
| 1142 | * attack: CVE-2010-4180. |
| 1143 | */ |
| 1144 | #if 0 |
| 1145 | if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) |
| 1146 | { |
| 1147 | /* Special case as client bug workaround: the previously used cipher may |
| 1148 | * not be in the current list, the client instead might be trying to |
| 1149 | * continue using a cipher that before wasn't chosen due to server |
| 1150 | * preferences. We'll have to reject the connection if the cipher is not |
| 1151 | * enabled, though. */ |
| 1152 | c = sk_SSL_CIPHER_value(ciphers, 0); |
| 1153 | if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) |
| 1154 | { |
| 1155 | s->session->cipher = c; |
| 1156 | j = 1; |
| 1157 | } |
| 1158 | } |
| 1159 | #endif |
| 1160 | if (j == 0) |
| 1161 | { |
| 1162 | /* we need to have the cipher in the cipher |
| 1163 | * list if we are asked to reuse it */ |
| 1164 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1165 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_REQUIRED_CIPHER_MISSING); |
| 1166 | goto f_err; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | /* compression */ |
| 1171 | i= *(p++); |
| 1172 | if ((p+i) > (d+n)) |
| 1173 | { |
| 1174 | /* not enough data */ |
| 1175 | al=SSL_AD_DECODE_ERROR; |
| 1176 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_LENGTH_MISMATCH); |
| 1177 | goto f_err; |
| 1178 | } |
| 1179 | for (j=0; j<i; j++) |
| 1180 | { |
| 1181 | if (p[j] == 0) break; |
| 1182 | } |
| 1183 | |
| 1184 | p+=i; |
| 1185 | if (j >= i) |
| 1186 | { |
| 1187 | /* no compress */ |
| 1188 | al=SSL_AD_DECODE_ERROR; |
| 1189 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_COMPRESSION_SPECIFIED); |
| 1190 | goto f_err; |
| 1191 | } |
| 1192 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1193 | CBS_init(&cbs, p, d + n - p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1194 | /* TLS extensions*/ |
| 1195 | if (s->version >= SSL3_VERSION) |
| 1196 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1197 | if (!ssl_parse_clienthello_tlsext(s, &cbs)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1198 | { |
| 1199 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT); |
| 1200 | goto err; |
| 1201 | } |
| 1202 | } |
| 1203 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1204 | /* There should be nothing left over in the record. */ |
| 1205 | if (CBS_len(&cbs) != 0) |
| 1206 | { |
| 1207 | /* wrong packet length */ |
| 1208 | al=SSL_AD_DECODE_ERROR; |
| 1209 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH); |
| 1210 | goto f_err; |
| 1211 | } |
| 1212 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1213 | /* Check if we want to use external pre-shared secret for this |
| 1214 | * handshake for not reused session only. We need to generate |
| 1215 | * server_random before calling tls_session_secret_cb in order to allow |
| 1216 | * SessionTicket processing to use it in key derivation. */ |
| 1217 | { |
| 1218 | unsigned char *pos; |
| 1219 | pos=s->s3->server_random; |
| 1220 | if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) |
| 1221 | { |
| 1222 | goto f_err; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) |
| 1227 | { |
| 1228 | SSL_CIPHER *pref_cipher=NULL; |
| 1229 | |
| 1230 | s->session->master_key_length=sizeof(s->session->master_key); |
| 1231 | if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, |
| 1232 | ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) |
| 1233 | { |
| 1234 | s->hit=1; |
| 1235 | s->session->ciphers=ciphers; |
| 1236 | s->session->verify_result=X509_V_OK; |
| 1237 | |
| 1238 | ciphers=NULL; |
| 1239 | |
| 1240 | /* check if some cipher was preferred by call back */ |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1241 | pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, ssl_get_cipher_preferences(s)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1242 | if (pref_cipher == NULL) |
| 1243 | { |
| 1244 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1245 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1246 | goto f_err; |
| 1247 | } |
| 1248 | |
| 1249 | s->session->cipher=pref_cipher; |
| 1250 | |
| 1251 | if (s->cipher_list) |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1252 | ssl_cipher_preference_list_free(s->cipher_list); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1253 | |
| 1254 | if (s->cipher_list_by_id) |
| 1255 | sk_SSL_CIPHER_free(s->cipher_list_by_id); |
| 1256 | |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1257 | s->cipher_list = ssl_cipher_preference_list_from_ciphers(s->session->ciphers); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1258 | s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); |
| 1259 | } |
| 1260 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1261 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1262 | /* Given s->session->ciphers and SSL_get_ciphers, we must |
| 1263 | * pick a cipher */ |
| 1264 | |
| 1265 | if (!s->hit) |
| 1266 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1267 | if (s->session->ciphers != NULL) |
| 1268 | sk_SSL_CIPHER_free(s->session->ciphers); |
| 1269 | s->session->ciphers=ciphers; |
| 1270 | if (ciphers == NULL) |
| 1271 | { |
| 1272 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1273 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED); |
| 1274 | goto f_err; |
| 1275 | } |
| 1276 | ciphers=NULL; |
| 1277 | /* Let cert callback update server certificates if required */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1278 | if (s->cert->cert_cb) |
| 1279 | { |
| 1280 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1281 | if (rv == 0) |
| 1282 | { |
| 1283 | al=SSL_AD_INTERNAL_ERROR; |
| 1284 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR); |
| 1285 | goto f_err; |
| 1286 | } |
| 1287 | if (rv < 0) |
| 1288 | { |
| 1289 | s->rwstate=SSL_X509_LOOKUP; |
| 1290 | return -1; |
| 1291 | } |
| 1292 | s->rwstate = SSL_NOTHING; |
| 1293 | } |
| 1294 | c=ssl3_choose_cipher(s,s->session->ciphers, |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1295 | ssl_get_cipher_preferences(s)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1296 | |
| 1297 | if (c == NULL) |
| 1298 | { |
| 1299 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1300 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1301 | goto f_err; |
| 1302 | } |
| 1303 | s->s3->tmp.new_cipher=c; |
| 1304 | } |
| 1305 | else |
| 1306 | { |
| 1307 | /* Session-id reuse */ |
| 1308 | #ifdef REUSE_CIPHER_BUG |
| 1309 | STACK_OF(SSL_CIPHER) *sk; |
| 1310 | SSL_CIPHER *nc=NULL; |
| 1311 | SSL_CIPHER *ec=NULL; |
| 1312 | |
| 1313 | if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) |
| 1314 | { |
| 1315 | sk=s->session->ciphers; |
| 1316 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) |
| 1317 | { |
| 1318 | c=sk_SSL_CIPHER_value(sk,i); |
| 1319 | if (c->algorithm_enc & SSL_eNULL) |
| 1320 | nc=c; |
| 1321 | if (SSL_C_IS_EXPORT(c)) |
| 1322 | ec=c; |
| 1323 | } |
| 1324 | if (nc != NULL) |
| 1325 | s->s3->tmp.new_cipher=nc; |
| 1326 | else if (ec != NULL) |
| 1327 | s->s3->tmp.new_cipher=ec; |
| 1328 | else |
| 1329 | s->s3->tmp.new_cipher=s->session->cipher; |
| 1330 | } |
| 1331 | else |
| 1332 | #endif |
| 1333 | s->s3->tmp.new_cipher=s->session->cipher; |
| 1334 | } |
| 1335 | |
| 1336 | if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) |
| 1337 | { |
| 1338 | if (!ssl3_digest_cached_records(s)) |
| 1339 | goto f_err; |
| 1340 | } |
| 1341 | |
| 1342 | /* we now have the following setup. |
| 1343 | * client_random |
| 1344 | * cipher_list - our prefered list of ciphers |
| 1345 | * ciphers - the clients prefered list of ciphers |
| 1346 | * compression - basically ignored right now |
| 1347 | * ssl version is set - sslv3 |
| 1348 | * s->session - The ssl session has been setup. |
| 1349 | * s->hit - session reuse flag |
| 1350 | * s->tmp.new_cipher - the new cipher to use. |
| 1351 | */ |
| 1352 | |
| 1353 | /* Handles TLS extensions that we couldn't check earlier */ |
| 1354 | if (s->version >= SSL3_VERSION) |
| 1355 | { |
| 1356 | if (ssl_check_clienthello_tlsext_late(s) <= 0) |
| 1357 | { |
| 1358 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_TLSEXT); |
| 1359 | goto err; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | if (ret < 0) ret=-ret; |
| 1364 | if (0) |
| 1365 | { |
| 1366 | f_err: |
| 1367 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1368 | } |
| 1369 | err: |
| 1370 | if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1371 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | int ssl3_send_server_hello(SSL *s) |
| 1375 | { |
| 1376 | unsigned char *buf; |
| 1377 | unsigned char *p,*d; |
| 1378 | int i,sl; |
| 1379 | unsigned long l; |
| 1380 | |
| 1381 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) |
| 1382 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1383 | /* We only accept ChannelIDs on connections with ECDHE in order |
| 1384 | * to avoid a known attack while we fix ChannelID itself. */ |
| 1385 | if (s->s3 && |
| 1386 | s->s3->tlsext_channel_id_valid && |
| 1387 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0) |
| 1388 | s->s3->tlsext_channel_id_valid = 0; |
| 1389 | |
| 1390 | /* If this is a resumption and the original handshake didn't |
| 1391 | * support ChannelID then we didn't record the original |
| 1392 | * handshake hashes in the session and so cannot resume with |
| 1393 | * ChannelIDs. */ |
| 1394 | if (s->hit && |
| 1395 | s->s3->tlsext_channel_id_new && |
| 1396 | s->session->original_handshake_hash_len == 0) |
| 1397 | s->s3->tlsext_channel_id_valid = 0; |
| 1398 | |
Adam Langley | 1b9d477 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1399 | if (s->mode & SSL_MODE_RELEASE_BUFFERS) |
| 1400 | { |
| 1401 | /* Free s->session->ciphers in order to release memory. This |
| 1402 | * breaks SSL_get_shared_ciphers(), but many servers will |
| 1403 | * prefer the memory savings. |
| 1404 | * |
| 1405 | * It also breaks REUSE_CIPHER_BUG, which is disabled |
| 1406 | * in our build. */ |
| 1407 | sk_SSL_CIPHER_free(s->session->ciphers); |
| 1408 | s->session->ciphers = NULL; |
| 1409 | } |
| 1410 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1411 | buf=(unsigned char *)s->init_buf->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1412 | /* Do the message type and length last */ |
| 1413 | d=p= ssl_handshake_start(s); |
| 1414 | |
| 1415 | *(p++)=s->version>>8; |
| 1416 | *(p++)=s->version&0xff; |
| 1417 | |
| 1418 | /* Random stuff */ |
| 1419 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); |
| 1420 | p+=SSL3_RANDOM_SIZE; |
| 1421 | |
| 1422 | /* There are several cases for the session ID to send |
| 1423 | * back in the server hello: |
| 1424 | * - For session reuse from the session cache, |
| 1425 | * we send back the old session ID. |
| 1426 | * - If stateless session reuse (using a session ticket) |
| 1427 | * is successful, we send back the client's "session ID" |
| 1428 | * (which doesn't actually identify the session). |
| 1429 | * - If it is a new session, we send back the new |
| 1430 | * session ID. |
| 1431 | * - However, if we want the new session to be single-use, |
| 1432 | * we send back a 0-length session ID. |
| 1433 | * s->hit is non-zero in either case of session reuse, |
| 1434 | * so the following won't overwrite an ID that we're supposed |
| 1435 | * to send back. |
| 1436 | */ |
| 1437 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) |
| 1438 | && !s->hit) |
| 1439 | s->session->session_id_length=0; |
| 1440 | |
| 1441 | sl=s->session->session_id_length; |
| 1442 | if (sl > (int)sizeof(s->session->session_id)) |
| 1443 | { |
| 1444 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1445 | return -1; |
| 1446 | } |
| 1447 | *(p++)=sl; |
| 1448 | memcpy(p,s->session->session_id,sl); |
| 1449 | p+=sl; |
| 1450 | |
| 1451 | /* put the cipher */ |
| 1452 | i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); |
| 1453 | p+=i; |
| 1454 | |
| 1455 | /* put the compression method */ |
| 1456 | *(p++)=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1457 | if (ssl_prepare_serverhello_tlsext(s) <= 0) |
| 1458 | { |
| 1459 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) |
| 1463 | { |
| 1464 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1465 | return -1; |
| 1466 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1467 | /* do the header */ |
| 1468 | l=(p-d); |
| 1469 | ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l); |
| 1470 | s->state=SSL3_ST_SW_SRVR_HELLO_B; |
| 1471 | } |
| 1472 | |
| 1473 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1474 | return ssl_do_write(s); |
| 1475 | } |
| 1476 | |
| 1477 | int ssl3_send_server_done(SSL *s) |
| 1478 | { |
| 1479 | |
| 1480 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) |
| 1481 | { |
| 1482 | ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0); |
| 1483 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1484 | } |
| 1485 | |
| 1486 | /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1487 | return ssl_do_write(s); |
| 1488 | } |
| 1489 | |
| 1490 | int ssl3_send_server_key_exchange(SSL *s) |
| 1491 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1492 | unsigned char *q; |
| 1493 | int j,num; |
| 1494 | RSA *rsa; |
| 1495 | unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 1496 | unsigned int u; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1497 | #ifndef OPENSSL_NO_DH |
| 1498 | DH *dh=NULL,*dhp; |
| 1499 | #endif |
| 1500 | #ifndef OPENSSL_NO_ECDH |
| 1501 | EC_KEY *ecdh=NULL, *ecdhp; |
| 1502 | unsigned char *encodedPoint = NULL; |
| 1503 | int encodedlen = 0; |
| 1504 | int curve_id = 0; |
| 1505 | BN_CTX *bn_ctx = NULL; |
| 1506 | #endif |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1507 | #ifndef OPENSSL_NO_PSK |
| 1508 | const char* psk_identity_hint; |
| 1509 | size_t psk_identity_hint_len; |
| 1510 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1511 | EVP_PKEY *pkey; |
| 1512 | const EVP_MD *md = NULL; |
| 1513 | unsigned char *p,*d; |
| 1514 | int al,i; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1515 | unsigned long alg_k; |
| 1516 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1517 | int n; |
| 1518 | CERT *cert; |
| 1519 | BIGNUM *r[4]; |
| 1520 | int nr[4],kn; |
| 1521 | BUF_MEM *buf; |
| 1522 | EVP_MD_CTX md_ctx; |
| 1523 | |
| 1524 | EVP_MD_CTX_init(&md_ctx); |
| 1525 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) |
| 1526 | { |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1527 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1528 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1529 | cert=s->cert; |
| 1530 | |
| 1531 | buf=s->init_buf; |
| 1532 | |
| 1533 | r[0]=r[1]=r[2]=r[3]=NULL; |
| 1534 | n=0; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1535 | #ifndef OPENSSL_NO_PSK |
| 1536 | if (alg_a & SSL_aPSK) |
| 1537 | { |
| 1538 | /* size for PSK identity hint */ |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1539 | psk_identity_hint = s->session->psk_identity_hint; |
| 1540 | if (psk_identity_hint) |
| 1541 | psk_identity_hint_len = strlen(psk_identity_hint); |
| 1542 | else |
| 1543 | psk_identity_hint_len = 0; |
| 1544 | n+=2+psk_identity_hint_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1545 | } |
| 1546 | #endif /* !OPENSSL_NO_PSK */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1547 | if (alg_k & SSL_kRSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1548 | { |
| 1549 | rsa=cert->rsa_tmp; |
| 1550 | if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) |
| 1551 | { |
| 1552 | rsa=s->cert->rsa_tmp_cb(s, |
| 1553 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), |
| 1554 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); |
| 1555 | if(rsa == NULL) |
| 1556 | { |
| 1557 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1558 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_ERROR_GENERATING_TMP_RSA_KEY); |
| 1559 | goto f_err; |
| 1560 | } |
| 1561 | RSA_up_ref(rsa); |
| 1562 | cert->rsa_tmp=rsa; |
| 1563 | } |
| 1564 | if (rsa == NULL) |
| 1565 | { |
| 1566 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1567 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_RSA_KEY); |
| 1568 | goto f_err; |
| 1569 | } |
| 1570 | r[0]=rsa->n; |
| 1571 | r[1]=rsa->e; |
| 1572 | s->s3->tmp.use_rsa_tmp=1; |
| 1573 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1574 | #ifndef OPENSSL_NO_DH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1575 | else if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1576 | { |
| 1577 | dhp=cert->dh_tmp; |
| 1578 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) |
| 1579 | dhp=s->cert->dh_tmp_cb(s, |
| 1580 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), |
| 1581 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); |
| 1582 | if (dhp == NULL) |
| 1583 | { |
| 1584 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1585 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 1586 | goto f_err; |
| 1587 | } |
| 1588 | |
| 1589 | if (s->s3->tmp.dh != NULL) |
| 1590 | { |
| 1591 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1592 | goto err; |
| 1593 | } |
| 1594 | |
| 1595 | if ((dh=DHparams_dup(dhp)) == NULL) |
| 1596 | { |
| 1597 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1598 | goto err; |
| 1599 | } |
| 1600 | |
| 1601 | s->s3->tmp.dh=dh; |
| 1602 | if ((dhp->pub_key == NULL || |
| 1603 | dhp->priv_key == NULL || |
| 1604 | (s->options & SSL_OP_SINGLE_DH_USE))) |
| 1605 | { |
| 1606 | if(!DH_generate_key(dh)) |
| 1607 | { |
| 1608 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1609 | goto err; |
| 1610 | } |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | dh->pub_key=BN_dup(dhp->pub_key); |
| 1615 | dh->priv_key=BN_dup(dhp->priv_key); |
| 1616 | if ((dh->pub_key == NULL) || |
| 1617 | (dh->priv_key == NULL)) |
| 1618 | { |
| 1619 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1620 | goto err; |
| 1621 | } |
| 1622 | } |
| 1623 | r[0]=dh->p; |
| 1624 | r[1]=dh->g; |
| 1625 | r[2]=dh->pub_key; |
| 1626 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1627 | #endif |
| 1628 | #ifndef OPENSSL_NO_ECDH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1629 | else if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1630 | { |
| 1631 | const EC_GROUP *group; |
| 1632 | |
| 1633 | ecdhp=cert->ecdh_tmp; |
| 1634 | if (s->cert->ecdh_tmp_auto) |
| 1635 | { |
| 1636 | /* Get NID of appropriate shared curve */ |
| 1637 | int nid = tls1_shared_curve(s, -2); |
| 1638 | if (nid != NID_undef) |
| 1639 | ecdhp = EC_KEY_new_by_curve_name(nid); |
| 1640 | } |
| 1641 | else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) |
| 1642 | { |
| 1643 | ecdhp=s->cert->ecdh_tmp_cb(s, |
| 1644 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), |
| 1645 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); |
| 1646 | } |
| 1647 | if (ecdhp == NULL) |
| 1648 | { |
| 1649 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1650 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY); |
| 1651 | goto f_err; |
| 1652 | } |
| 1653 | |
| 1654 | if (s->s3->tmp.ecdh != NULL) |
| 1655 | { |
| 1656 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1657 | goto err; |
| 1658 | } |
| 1659 | |
| 1660 | /* Duplicate the ECDH structure. */ |
| 1661 | if (ecdhp == NULL) |
| 1662 | { |
| 1663 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1664 | goto err; |
| 1665 | } |
| 1666 | if (s->cert->ecdh_tmp_auto) |
| 1667 | ecdh = ecdhp; |
| 1668 | else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) |
| 1669 | { |
| 1670 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1671 | goto err; |
| 1672 | } |
| 1673 | |
| 1674 | s->s3->tmp.ecdh=ecdh; |
| 1675 | if ((EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1676 | (EC_KEY_get0_private_key(ecdh) == NULL) || |
| 1677 | (s->options & SSL_OP_SINGLE_ECDH_USE)) |
| 1678 | { |
| 1679 | if(!EC_KEY_generate_key(ecdh)) |
| 1680 | { |
| 1681 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1682 | goto err; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (((group = EC_KEY_get0_group(ecdh)) == NULL) || |
| 1687 | (EC_KEY_get0_public_key(ecdh) == NULL) || |
| 1688 | (EC_KEY_get0_private_key(ecdh) == NULL)) |
| 1689 | { |
| 1690 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1691 | goto err; |
| 1692 | } |
| 1693 | |
| 1694 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && |
| 1695 | (EC_GROUP_get_degree(group) > 163)) |
| 1696 | { |
| 1697 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); |
| 1698 | goto err; |
| 1699 | } |
| 1700 | |
| 1701 | /* XXX: For now, we only support ephemeral ECDH |
| 1702 | * keys over named (not generic) curves. For |
| 1703 | * supported named curves, curve_id is non-zero. |
| 1704 | */ |
| 1705 | if ((curve_id = |
| 1706 | tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) |
| 1707 | == 0) |
| 1708 | { |
| 1709 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1710 | goto err; |
| 1711 | } |
| 1712 | |
| 1713 | /* Encode the public key. |
| 1714 | * First check the size of encoding and |
| 1715 | * allocate memory accordingly. |
| 1716 | */ |
| 1717 | encodedlen = EC_POINT_point2oct(group, |
| 1718 | EC_KEY_get0_public_key(ecdh), |
| 1719 | POINT_CONVERSION_UNCOMPRESSED, |
| 1720 | NULL, 0, NULL); |
| 1721 | |
| 1722 | encodedPoint = (unsigned char *) |
| 1723 | OPENSSL_malloc(encodedlen*sizeof(unsigned char)); |
| 1724 | bn_ctx = BN_CTX_new(); |
| 1725 | if ((encodedPoint == NULL) || (bn_ctx == NULL)) |
| 1726 | { |
| 1727 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1728 | goto err; |
| 1729 | } |
| 1730 | |
| 1731 | |
| 1732 | encodedlen = EC_POINT_point2oct(group, |
| 1733 | EC_KEY_get0_public_key(ecdh), |
| 1734 | POINT_CONVERSION_UNCOMPRESSED, |
| 1735 | encodedPoint, encodedlen, bn_ctx); |
| 1736 | |
| 1737 | if (encodedlen == 0) |
| 1738 | { |
| 1739 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1740 | goto err; |
| 1741 | } |
| 1742 | |
| 1743 | BN_CTX_free(bn_ctx); bn_ctx=NULL; |
| 1744 | |
| 1745 | /* XXX: For now, we only support named (not |
| 1746 | * generic) curves in ECDH ephemeral key exchanges. |
| 1747 | * In this situation, we need four additional bytes |
| 1748 | * to encode the entire ServerECDHParams |
| 1749 | * structure. |
| 1750 | */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1751 | n += 4 + encodedlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1752 | |
| 1753 | /* We'll generate the serverKeyExchange message |
| 1754 | * explicitly so we can set these to NULLs |
| 1755 | */ |
| 1756 | r[0]=NULL; |
| 1757 | r[1]=NULL; |
| 1758 | r[2]=NULL; |
| 1759 | r[3]=NULL; |
| 1760 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1761 | #endif /* !OPENSSL_NO_ECDH */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1762 | else if (!(alg_k & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1763 | { |
| 1764 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1765 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 1766 | goto f_err; |
| 1767 | } |
| 1768 | for (i=0; i < 4 && r[i] != NULL; i++) |
| 1769 | { |
| 1770 | nr[i]=BN_num_bytes(r[i]); |
| 1771 | n+=2+nr[i]; |
| 1772 | } |
| 1773 | |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 1774 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1775 | { |
| 1776 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md)) |
| 1777 | == NULL) |
| 1778 | { |
| 1779 | al=SSL_AD_DECODE_ERROR; |
| 1780 | goto f_err; |
| 1781 | } |
| 1782 | kn=EVP_PKEY_size(pkey); |
| 1783 | } |
| 1784 | else |
| 1785 | { |
| 1786 | pkey=NULL; |
| 1787 | kn=0; |
| 1788 | } |
| 1789 | |
| 1790 | if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn)) |
| 1791 | { |
| 1792 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF); |
| 1793 | goto err; |
| 1794 | } |
| 1795 | d = p = ssl_handshake_start(s); |
| 1796 | |
| 1797 | for (i=0; i < 4 && r[i] != NULL; i++) |
| 1798 | { |
| 1799 | s2n(nr[i],p); |
| 1800 | BN_bn2bin(r[i],p); |
| 1801 | p+=nr[i]; |
| 1802 | } |
| 1803 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1804 | /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK. |
| 1805 | * When one of them is used, the server key exchange record needs to have both |
| 1806 | * the psk_identity_hint and the ServerECDHParams. */ |
| 1807 | #ifndef OPENSSL_NO_PSK |
| 1808 | if (alg_a & SSL_aPSK) |
| 1809 | { |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1810 | /* copy PSK identity hint (if provided) */ |
| 1811 | s2n(psk_identity_hint_len, p); |
| 1812 | if (psk_identity_hint_len > 0) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1813 | { |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1814 | memcpy(p, psk_identity_hint, psk_identity_hint_len); |
| 1815 | p+=psk_identity_hint_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1816 | } |
| 1817 | } |
| 1818 | #endif /* OPENSSL_NO_PSK */ |
| 1819 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1820 | #ifndef OPENSSL_NO_ECDH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1821 | if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | { |
| 1823 | /* XXX: For now, we only support named (not generic) curves. |
| 1824 | * In this situation, the serverKeyExchange message has: |
| 1825 | * [1 byte CurveType], [2 byte CurveName] |
| 1826 | * [1 byte length of encoded point], followed by |
| 1827 | * the actual encoded point itself |
| 1828 | */ |
| 1829 | *p = NAMED_CURVE_TYPE; |
| 1830 | p += 1; |
| 1831 | *p = 0; |
| 1832 | p += 1; |
| 1833 | *p = curve_id; |
| 1834 | p += 1; |
| 1835 | *p = encodedlen; |
| 1836 | p += 1; |
| 1837 | memcpy((unsigned char*)p, |
| 1838 | (unsigned char *)encodedPoint, |
| 1839 | encodedlen); |
| 1840 | OPENSSL_free(encodedPoint); |
| 1841 | encodedPoint = NULL; |
| 1842 | p += encodedlen; |
| 1843 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1844 | #endif /* OPENSSL_NO_ECDH */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1845 | |
| 1846 | /* not anonymous */ |
| 1847 | if (pkey != NULL) |
| 1848 | { |
| 1849 | /* n is the length of the params, they start at &(d[4]) |
| 1850 | * and p points to the space at the end. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1851 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) |
| 1852 | { |
| 1853 | q=md_buf; |
| 1854 | j=0; |
| 1855 | for (num=2; num > 0; num--) |
| 1856 | { |
| 1857 | EVP_DigestInit_ex(&md_ctx,(num == 2) |
| 1858 | ?s->ctx->md5:s->ctx->sha1, NULL); |
| 1859 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1860 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1861 | EVP_DigestUpdate(&md_ctx,d,n); |
| 1862 | EVP_DigestFinal_ex(&md_ctx,q, |
| 1863 | (unsigned int *)&i); |
| 1864 | q+=i; |
| 1865 | j+=i; |
| 1866 | } |
| 1867 | if (RSA_sign(NID_md5_sha1, md_buf, j, |
| 1868 | &(p[2]), &u, pkey->pkey.rsa) <= 0) |
| 1869 | { |
| 1870 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_RSA); |
| 1871 | goto err; |
| 1872 | } |
| 1873 | s2n(u,p); |
| 1874 | n+=u+2; |
| 1875 | } |
| 1876 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1877 | if (md) |
| 1878 | { |
| 1879 | /* send signature algorithm */ |
| 1880 | if (SSL_USE_SIGALGS(s)) |
| 1881 | { |
| 1882 | if (!tls12_get_sigandhash(p, pkey, md)) |
| 1883 | { |
| 1884 | /* Should never happen */ |
| 1885 | al=SSL_AD_INTERNAL_ERROR; |
| 1886 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1887 | goto f_err; |
| 1888 | } |
| 1889 | p+=2; |
| 1890 | } |
| 1891 | #ifdef SSL_DEBUG |
| 1892 | fprintf(stderr, "Using hash %s\n", |
| 1893 | EVP_MD_name(md)); |
| 1894 | #endif |
| 1895 | EVP_SignInit_ex(&md_ctx, md, NULL); |
| 1896 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1897 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1898 | EVP_SignUpdate(&md_ctx,d,n); |
| 1899 | if (!EVP_SignFinal(&md_ctx,&(p[2]), |
| 1900 | (unsigned int *)&i,pkey)) |
| 1901 | { |
| 1902 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP); |
| 1903 | goto err; |
| 1904 | } |
| 1905 | s2n(i,p); |
| 1906 | n+=i+2; |
| 1907 | if (SSL_USE_SIGALGS(s)) |
| 1908 | n+= 2; |
| 1909 | } |
| 1910 | else |
| 1911 | { |
| 1912 | /* Is this error check actually needed? */ |
| 1913 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 1914 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_PKEY_TYPE); |
| 1915 | goto f_err; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n); |
| 1920 | } |
| 1921 | |
| 1922 | s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1923 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1924 | return ssl_do_write(s); |
| 1925 | f_err: |
| 1926 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1927 | err: |
| 1928 | #ifndef OPENSSL_NO_ECDH |
| 1929 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 1930 | BN_CTX_free(bn_ctx); |
| 1931 | #endif |
| 1932 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1933 | return(-1); |
| 1934 | } |
| 1935 | |
| 1936 | int ssl3_send_certificate_request(SSL *s) |
| 1937 | { |
| 1938 | unsigned char *p,*d; |
| 1939 | int i,j,nl,off,n; |
| 1940 | STACK_OF(X509_NAME) *sk=NULL; |
| 1941 | X509_NAME *name; |
| 1942 | BUF_MEM *buf; |
| 1943 | |
| 1944 | if (s->state == SSL3_ST_SW_CERT_REQ_A) |
| 1945 | { |
| 1946 | buf=s->init_buf; |
| 1947 | |
| 1948 | d=p=ssl_handshake_start(s); |
| 1949 | |
| 1950 | /* get the list of acceptable cert types */ |
| 1951 | p++; |
| 1952 | n=ssl3_get_req_cert_type(s,p); |
| 1953 | d[0]=n; |
| 1954 | p+=n; |
| 1955 | n++; |
| 1956 | |
| 1957 | if (SSL_USE_SIGALGS(s)) |
| 1958 | { |
| 1959 | const unsigned char *psigs; |
| 1960 | nl = tls12_get_psigalgs(s, &psigs); |
| 1961 | s2n(nl, p); |
| 1962 | memcpy(p, psigs, nl); |
| 1963 | p += nl; |
| 1964 | n += nl + 2; |
| 1965 | } |
| 1966 | |
| 1967 | off=n; |
| 1968 | p+=2; |
| 1969 | n+=2; |
| 1970 | |
| 1971 | sk=SSL_get_client_CA_list(s); |
| 1972 | nl=0; |
| 1973 | if (sk != NULL) |
| 1974 | { |
| 1975 | for (i=0; i<sk_X509_NAME_num(sk); i++) |
| 1976 | { |
| 1977 | name=sk_X509_NAME_value(sk,i); |
| 1978 | j=i2d_X509_NAME(name,NULL); |
| 1979 | if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2)) |
| 1980 | { |
| 1981 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1982 | goto err; |
| 1983 | } |
| 1984 | p = ssl_handshake_start(s) + n; |
Alex Chernyakhovsky | 9c890d4 | 2014-07-05 00:53:11 -0400 | [diff] [blame] | 1985 | s2n(j,p); |
| 1986 | i2d_X509_NAME(name,&p); |
| 1987 | n+=2+j; |
| 1988 | nl+=2+j; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | /* else no CA names */ |
| 1992 | p = ssl_handshake_start(s) + off; |
| 1993 | s2n(nl,p); |
| 1994 | |
| 1995 | ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n); |
| 1996 | |
| 1997 | #ifdef NETSCAPE_HANG_BUG |
| 1998 | if (!SSL_IS_DTLS(s)) |
| 1999 | { |
Adam Langley | ce1cfe1 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2000 | if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) |
| 2001 | { |
| 2002 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 2003 | goto err; |
| 2004 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2005 | p=(unsigned char *)s->init_buf->data + s->init_num; |
| 2006 | /* do the header */ |
| 2007 | *(p++)=SSL3_MT_SERVER_DONE; |
| 2008 | *(p++)=0; |
| 2009 | *(p++)=0; |
| 2010 | *(p++)=0; |
| 2011 | s->init_num += 4; |
| 2012 | } |
| 2013 | #endif |
| 2014 | |
| 2015 | s->state = SSL3_ST_SW_CERT_REQ_B; |
| 2016 | } |
| 2017 | |
| 2018 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 2019 | return ssl_do_write(s); |
| 2020 | err: |
| 2021 | return(-1); |
| 2022 | } |
| 2023 | |
| 2024 | int ssl3_get_client_key_exchange(SSL *s) |
| 2025 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2026 | int al,ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2027 | long n; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2028 | CBS client_key_exchange; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2029 | unsigned long alg_k; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2030 | unsigned long alg_a; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2031 | uint8_t *premaster_secret = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2032 | RSA *rsa=NULL; |
| 2033 | EVP_PKEY *pkey=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2034 | #ifndef OPENSSL_NO_DH |
| 2035 | BIGNUM *pub=NULL; |
| 2036 | DH *dh_srvr, *dh_clnt = NULL; |
| 2037 | #endif |
| 2038 | |
| 2039 | #ifndef OPENSSL_NO_ECDH |
| 2040 | EC_KEY *srvr_ecdh = NULL; |
| 2041 | EVP_PKEY *clnt_pub_pkey = NULL; |
| 2042 | EC_POINT *clnt_ecpoint = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2043 | BN_CTX *bn_ctx = NULL; |
| 2044 | #ifndef OPENSSL_NO_PSK |
| 2045 | unsigned int psk_len = 0; |
| 2046 | unsigned char psk[PSK_MAX_PSK_LEN]; |
| 2047 | #endif /* OPENSSL_NO_PSK */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2048 | #endif |
| 2049 | |
| 2050 | n=s->method->ssl_get_message(s, |
| 2051 | SSL3_ST_SR_KEY_EXCH_A, |
| 2052 | SSL3_ST_SR_KEY_EXCH_B, |
| 2053 | SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 2054 | 2048, /* ??? */ |
| 2055 | &ok); |
| 2056 | |
| 2057 | if (!ok) return((int)n); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2058 | CBS_init(&client_key_exchange, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2059 | |
| 2060 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2061 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2062 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2063 | #ifndef OPENSSL_NO_PSK |
| 2064 | if (alg_a & SSL_aPSK) |
| 2065 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2066 | CBS psk_identity; |
| 2067 | int psk_err = -1; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2068 | unsigned char *t = NULL; |
| 2069 | unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4]; |
| 2070 | unsigned int pre_ms_len = 0; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2071 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2072 | /* If using PSK, the ClientKeyExchange contains a |
| 2073 | * psk_identity. If PSK, then this is the only field |
| 2074 | * in the message. */ |
| 2075 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 2076 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2077 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2078 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 2079 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2080 | goto psk_err; |
| 2081 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2082 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2083 | if (s->psk_server_callback == NULL) |
| 2084 | { |
| 2085 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_NO_SERVER_CB); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2086 | al = SSL_AD_INTERNAL_ERROR; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2087 | goto psk_err; |
| 2088 | } |
| 2089 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2090 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 2091 | CBS_contains_zero_byte(&psk_identity)) |
| 2092 | { |
| 2093 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG); |
| 2094 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 2095 | goto psk_err; |
| 2096 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2097 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2098 | if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) |
| 2099 | { |
| 2100 | al = SSL_AD_INTERNAL_ERROR; |
| 2101 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2102 | goto psk_err; |
| 2103 | } |
| 2104 | |
| 2105 | /* Look up the key for the identity. */ |
| 2106 | psk_len = s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk)); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2107 | if (psk_len > PSK_MAX_PSK_LEN) |
| 2108 | { |
| 2109 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2110 | al = SSL_AD_INTERNAL_ERROR; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2111 | goto psk_err; |
| 2112 | } |
| 2113 | else if (psk_len == 0) |
| 2114 | { |
| 2115 | /* PSK related to the given identity not found */ |
| 2116 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND); |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2117 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2118 | goto psk_err; |
| 2119 | } |
| 2120 | if (!(alg_k & SSL_kEECDH)) |
| 2121 | { |
| 2122 | /* Create the shared secret now if we're not using ECDHE-PSK.*/ |
| 2123 | pre_ms_len=2+psk_len+2+psk_len; |
| 2124 | t = pre_ms; |
| 2125 | s2n(psk_len, t); |
| 2126 | memset(t, 0, psk_len); |
| 2127 | t+=psk_len; |
| 2128 | s2n(psk_len, t); |
| 2129 | memcpy(t, psk, psk_len); |
| 2130 | |
| 2131 | s->session->master_key_length= |
| 2132 | s->method->ssl3_enc->generate_master_secret(s, |
| 2133 | s->session->master_key, pre_ms, pre_ms_len); |
| 2134 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2135 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2136 | psk_err = 0; |
| 2137 | psk_err: |
| 2138 | OPENSSL_cleanse(pre_ms, sizeof(pre_ms)); |
| 2139 | if (psk_err != 0) |
| 2140 | goto f_err; |
| 2141 | } |
| 2142 | #endif /* OPENSSL_NO_PSK */ |
| 2143 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2144 | if (alg_k & SSL_kRSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2145 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2146 | CBS encrypted_premaster_secret; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2147 | unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
| 2148 | int decrypt_len, decrypt_good_mask; |
| 2149 | unsigned char version_good; |
| 2150 | size_t j; |
| 2151 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2152 | /* FIX THIS UP EAY EAY EAY EAY */ |
| 2153 | if (s->s3->tmp.use_rsa_tmp) |
| 2154 | { |
| 2155 | if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) |
| 2156 | rsa=s->cert->rsa_tmp; |
| 2157 | /* Don't do a callback because rsa_tmp should |
| 2158 | * be sent already */ |
| 2159 | if (rsa == NULL) |
| 2160 | { |
| 2161 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2162 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_RSA_PKEY); |
| 2163 | goto f_err; |
| 2164 | |
| 2165 | } |
| 2166 | } |
| 2167 | else |
| 2168 | { |
| 2169 | pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 2170 | if ( (pkey == NULL) || |
| 2171 | (pkey->type != EVP_PKEY_RSA) || |
| 2172 | (pkey->pkey.rsa == NULL)) |
| 2173 | { |
| 2174 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2175 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE); |
| 2176 | goto f_err; |
| 2177 | } |
| 2178 | rsa=pkey->pkey.rsa; |
| 2179 | } |
| 2180 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2181 | /* TLS and [incidentally] DTLS{0xFEFF} |
| 2182 | * |
| 2183 | * TODO(davidben): Should this (and |
| 2184 | * ssl3_send_client_key_exchange) include DTLS1_BAD_VER? |
| 2185 | * Alternatively, get rid of DTLS1_BAD_VER? |
| 2186 | */ |
| 2187 | if (s->version > SSL3_VERSION) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2188 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2189 | CBS copy = client_key_exchange; |
| 2190 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 2191 | &encrypted_premaster_secret) || |
| 2192 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2193 | { |
| 2194 | if (!(s->options & SSL_OP_TLS_D5_BUG)) |
| 2195 | { |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2196 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2197 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2198 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2199 | } |
| 2200 | else |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2201 | encrypted_premaster_secret = copy; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2203 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2204 | else |
| 2205 | encrypted_premaster_secret = client_key_exchange; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2206 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2207 | /* Reject overly short RSA ciphertext because we want to be |
| 2208 | * sure that the buffer size makes it safe to iterate over the |
| 2209 | * entire size of a premaster secret |
| 2210 | * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is |
| 2211 | * larger due to RSA padding, but the bound is sufficient to be |
| 2212 | * safe. */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2213 | if (CBS_len(&encrypted_premaster_secret) < SSL_MAX_MASTER_KEY_LENGTH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2214 | { |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2215 | al = SSL_AD_DECRYPT_ERROR; |
| 2216 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED); |
| 2217 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2220 | /* We must not leak whether a decryption failure occurs because |
| 2221 | * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see |
| 2222 | * RFC 2246, section 7.4.7.1). The code follows that advice of |
| 2223 | * the TLS RFC and generates a random premaster secret for the |
| 2224 | * case that the decrypt fails. See |
| 2225 | * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */ |
| 2226 | if (RAND_pseudo_bytes(rand_premaster_secret, |
| 2227 | sizeof(rand_premaster_secret)) <= 0) |
| 2228 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2229 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2230 | /* Allocate a buffer large enough for an RSA decryption. */ |
| 2231 | premaster_secret = OPENSSL_malloc(RSA_size(rsa)); |
| 2232 | if (premaster_secret == NULL) |
| 2233 | { |
| 2234 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2235 | goto err; |
| 2236 | } |
| 2237 | |
| 2238 | decrypt_len = RSA_private_decrypt( |
| 2239 | CBS_len(&encrypted_premaster_secret), |
| 2240 | CBS_data(&encrypted_premaster_secret), |
| 2241 | premaster_secret, |
| 2242 | rsa, |
| 2243 | RSA_PKCS1_PADDING); |
| 2244 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2245 | ERR_clear_error(); |
| 2246 | |
| 2247 | /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. |
| 2248 | * decrypt_good_mask will be zero if so and non-zero otherwise. */ |
| 2249 | decrypt_good_mask = decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH; |
| 2250 | |
| 2251 | /* If the version in the decrypted pre-master secret is correct |
| 2252 | * then version_good will be zero. The Klima-Pokorny-Rosa |
| 2253 | * extension of Bleichenbacher's attack |
| 2254 | * (http://eprint.iacr.org/2003/052/) exploits the version |
| 2255 | * number check as a "bad version oracle". Thus version checks |
| 2256 | * are done in constant time and are treated like any other |
| 2257 | * decryption error. */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2258 | version_good = premaster_secret[0] ^ (s->client_version>>8); |
| 2259 | version_good |= premaster_secret[1] ^ (s->client_version&0xff); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2260 | |
| 2261 | /* The premaster secret must contain the same version number as |
| 2262 | * the ClientHello to detect version rollback attacks |
| 2263 | * (strangely, the protocol does not offer such protection for |
| 2264 | * DH ciphersuites). However, buggy clients exist that send the |
| 2265 | * negotiated protocol version instead if the server does not |
| 2266 | * support the requested protocol version. If |
| 2267 | * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */ |
| 2268 | if (s->options & SSL_OP_TLS_ROLLBACK_BUG) |
| 2269 | { |
| 2270 | unsigned char workaround_mask = version_good; |
| 2271 | unsigned char workaround; |
| 2272 | |
| 2273 | /* workaround_mask will be 0xff if version_good is |
| 2274 | * non-zero (i.e. the version match failed). Otherwise |
| 2275 | * it'll be 0x00. */ |
| 2276 | workaround_mask |= workaround_mask >> 4; |
| 2277 | workaround_mask |= workaround_mask >> 2; |
| 2278 | workaround_mask |= workaround_mask >> 1; |
| 2279 | workaround_mask = ~((workaround_mask & 1) - 1); |
| 2280 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2281 | workaround = premaster_secret[0] ^ (s->version>>8); |
| 2282 | workaround |= premaster_secret[1] ^ (s->version&0xff); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2283 | |
| 2284 | /* If workaround_mask is 0xff (i.e. there was a version |
| 2285 | * mismatch) then we copy the value of workaround over |
| 2286 | * version_good. */ |
| 2287 | version_good = (workaround & workaround_mask) | |
| 2288 | (version_good & ~workaround_mask); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2291 | /* If any bits in version_good are set then they'll poision |
| 2292 | * decrypt_good_mask and cause rand_premaster_secret to be |
| 2293 | * used. */ |
| 2294 | decrypt_good_mask |= version_good; |
| 2295 | |
| 2296 | /* decrypt_good_mask will be zero iff decrypt_len == |
| 2297 | * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We |
| 2298 | * fold the bottom 32 bits of it with an OR so that the LSB |
| 2299 | * will be zero iff everything is good. This assumes that we'll |
| 2300 | * never decrypt a value > 2**31 bytes, which seems safe. */ |
| 2301 | decrypt_good_mask |= decrypt_good_mask >> 16; |
| 2302 | decrypt_good_mask |= decrypt_good_mask >> 8; |
| 2303 | decrypt_good_mask |= decrypt_good_mask >> 4; |
| 2304 | decrypt_good_mask |= decrypt_good_mask >> 2; |
| 2305 | decrypt_good_mask |= decrypt_good_mask >> 1; |
| 2306 | /* Now select only the LSB and subtract one. If decrypt_len == |
| 2307 | * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then |
| 2308 | * decrypt_good_mask will be all ones. Otherwise it'll be all |
| 2309 | * zeros. */ |
| 2310 | decrypt_good_mask &= 1; |
| 2311 | decrypt_good_mask--; |
| 2312 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2313 | /* Now copy rand_premaster_secret over premaster_secret using |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2314 | * decrypt_good_mask. */ |
| 2315 | for (j = 0; j < sizeof(rand_premaster_secret); j++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2316 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2317 | premaster_secret[j] = (premaster_secret[j] & decrypt_good_mask) | |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2318 | (rand_premaster_secret[j] & ~decrypt_good_mask); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2319 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2320 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2321 | s->session->master_key_length= |
| 2322 | s->method->ssl3_enc->generate_master_secret(s, |
| 2323 | s->session->master_key, |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2324 | premaster_secret, sizeof(rand_premaster_secret)); |
| 2325 | |
| 2326 | OPENSSL_cleanse(premaster_secret, sizeof(rand_premaster_secret)); |
| 2327 | OPENSSL_free(premaster_secret); |
| 2328 | premaster_secret = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2329 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2330 | #ifndef OPENSSL_NO_DH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2331 | else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2332 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2333 | CBS dh_Yc; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2334 | int idx = -1; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2335 | int dh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2336 | EVP_PKEY *skey = NULL; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2337 | |
| 2338 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) || |
| 2339 | CBS_len(&client_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2340 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2341 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 2342 | al = SSL_R_DECODE_ERROR; |
| 2343 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2344 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2345 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2346 | if (alg_k & SSL_kDHr) |
| 2347 | idx = SSL_PKEY_DH_RSA; |
| 2348 | else if (alg_k & SSL_kDHd) |
| 2349 | idx = SSL_PKEY_DH_DSA; |
| 2350 | if (idx >= 0) |
| 2351 | { |
| 2352 | skey = s->cert->pkeys[idx].privatekey; |
| 2353 | if ((skey == NULL) || |
| 2354 | (skey->type != EVP_PKEY_DH) || |
| 2355 | (skey->pkey.dh == NULL)) |
| 2356 | { |
| 2357 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2358 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE); |
| 2359 | goto f_err; |
| 2360 | } |
| 2361 | dh_srvr = skey->pkey.dh; |
| 2362 | } |
| 2363 | else if (s->s3->tmp.dh == NULL) |
| 2364 | { |
| 2365 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2366 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 2367 | goto f_err; |
| 2368 | } |
| 2369 | else |
| 2370 | dh_srvr=s->s3->tmp.dh; |
| 2371 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2372 | if (CBS_len(&dh_Yc) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2373 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2374 | /* Get pubkey from the client certificate. This is the |
| 2375 | * 'implicit' case of ClientDiffieHellman. |
| 2376 | * |
| 2377 | * TODO(davidben): Either lose this code or fix a bug |
| 2378 | * (or get the spec changed): if there is a fixed_dh |
| 2379 | * client certificate, per spec, the 'implicit' mode |
| 2380 | * MUST be used. This logic will still accept 'explicit' |
| 2381 | * mode. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2382 | EVP_PKEY *clkey=X509_get_pubkey(s->session->peer); |
| 2383 | if (clkey) |
| 2384 | { |
| 2385 | if (EVP_PKEY_cmp_parameters(clkey, skey) == 1) |
| 2386 | dh_clnt = EVP_PKEY_get1_DH(clkey); |
| 2387 | } |
| 2388 | if (dh_clnt == NULL) |
| 2389 | { |
| 2390 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2391 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY); |
| 2392 | goto f_err; |
| 2393 | } |
| 2394 | EVP_PKEY_free(clkey); |
| 2395 | pub = dh_clnt->pub_key; |
| 2396 | } |
| 2397 | else |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2398 | pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2399 | if (pub == NULL) |
| 2400 | { |
| 2401 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB); |
| 2402 | goto err; |
| 2403 | } |
| 2404 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2405 | /* Allocate a buffer for the premaster secret. */ |
| 2406 | premaster_secret = OPENSSL_malloc(DH_size(dh_srvr)); |
| 2407 | if (premaster_secret == NULL) |
| 2408 | { |
| 2409 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2410 | goto err; |
| 2411 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2412 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2413 | dh_len = DH_compute_key(premaster_secret, pub, dh_srvr); |
| 2414 | if (dh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2415 | { |
| 2416 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB); |
| 2417 | BN_clear_free(pub); |
| 2418 | goto err; |
| 2419 | } |
| 2420 | |
| 2421 | DH_free(s->s3->tmp.dh); |
| 2422 | s->s3->tmp.dh=NULL; |
| 2423 | if (dh_clnt) |
| 2424 | DH_free(dh_clnt); |
| 2425 | else |
| 2426 | BN_clear_free(pub); |
| 2427 | pub=NULL; |
| 2428 | s->session->master_key_length= |
| 2429 | s->method->ssl3_enc->generate_master_secret(s, |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2430 | s->session->master_key, premaster_secret, dh_len); |
| 2431 | OPENSSL_cleanse(premaster_secret, dh_len); |
| 2432 | OPENSSL_free(premaster_secret); |
| 2433 | premaster_secret = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2434 | if (dh_clnt) |
| 2435 | return 2; |
| 2436 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2437 | #endif |
| 2438 | |
| 2439 | #ifndef OPENSSL_NO_ECDH |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2440 | else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2441 | { |
| 2442 | int ret = 1; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2443 | int field_size = 0, ecdh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2444 | const EC_KEY *tkey; |
| 2445 | const EC_GROUP *group; |
| 2446 | const BIGNUM *priv_key; |
| 2447 | |
| 2448 | /* initialize structures for server's ECDH key pair */ |
| 2449 | if ((srvr_ecdh = EC_KEY_new()) == NULL) |
| 2450 | { |
| 2451 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2452 | goto err; |
| 2453 | } |
| 2454 | |
| 2455 | /* Let's get server private key and group information */ |
| 2456 | if (alg_k & (SSL_kECDHr|SSL_kECDHe)) |
| 2457 | { |
| 2458 | /* use the certificate */ |
| 2459 | tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec; |
| 2460 | } |
| 2461 | else |
| 2462 | { |
| 2463 | /* use the ephermeral values we saved when |
| 2464 | * generating the ServerKeyExchange msg. |
| 2465 | */ |
| 2466 | tkey = s->s3->tmp.ecdh; |
| 2467 | } |
| 2468 | |
| 2469 | group = EC_KEY_get0_group(tkey); |
| 2470 | priv_key = EC_KEY_get0_private_key(tkey); |
| 2471 | |
| 2472 | if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 2473 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) |
| 2474 | { |
| 2475 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2476 | goto err; |
| 2477 | } |
| 2478 | |
| 2479 | /* Let's get client's public key */ |
| 2480 | if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) |
| 2481 | { |
| 2482 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2483 | goto err; |
| 2484 | } |
| 2485 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2486 | if (CBS_len(&client_key_exchange) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2487 | { |
| 2488 | /* Client Publickey was in Client Certificate */ |
| 2489 | |
| 2490 | if (alg_k & SSL_kEECDH) |
| 2491 | { |
| 2492 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2493 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY); |
| 2494 | goto f_err; |
| 2495 | } |
| 2496 | if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer)) |
| 2497 | == NULL) || |
| 2498 | (clnt_pub_pkey->type != EVP_PKEY_EC)) |
| 2499 | { |
| 2500 | /* XXX: For now, we do not support client |
| 2501 | * authentication using ECDH certificates |
| 2502 | * so this branch (n == 0L) of the code is |
| 2503 | * never executed. When that support is |
| 2504 | * added, we ought to ensure the key |
| 2505 | * received in the certificate is |
| 2506 | * authorized for key agreement. |
| 2507 | * ECDH_compute_key implicitly checks that |
| 2508 | * the two ECDH shares are for the same |
| 2509 | * group. |
| 2510 | */ |
| 2511 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2512 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNABLE_TO_DECODE_ECDH_CERTS); |
| 2513 | goto f_err; |
| 2514 | } |
| 2515 | |
| 2516 | if (EC_POINT_copy(clnt_ecpoint, |
| 2517 | EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0) |
| 2518 | { |
| 2519 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2520 | goto err; |
| 2521 | } |
| 2522 | ret = 2; /* Skip certificate verify processing */ |
| 2523 | } |
| 2524 | else |
| 2525 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2526 | CBS ecdh_Yc; |
| 2527 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2528 | /* Get client's public key from encoded point |
| 2529 | * in the ClientKeyExchange message. |
| 2530 | */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2531 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) || |
| 2532 | CBS_len(&client_key_exchange) != 0) |
| 2533 | { |
| 2534 | al = SSL_AD_DECODE_ERROR; |
| 2535 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 2536 | goto f_err; |
| 2537 | } |
| 2538 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2539 | if ((bn_ctx = BN_CTX_new()) == NULL) |
| 2540 | { |
| 2541 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2542 | goto err; |
| 2543 | } |
| 2544 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2545 | if (!EC_POINT_oct2point(group, clnt_ecpoint, |
| 2546 | CBS_data(&ecdh_Yc), CBS_len(&ecdh_Yc), bn_ctx)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2547 | { |
| 2548 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2549 | goto err; |
| 2550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2553 | /* Allocate a buffer for both the secret and the PSK. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2554 | field_size = EC_GROUP_get_degree(group); |
| 2555 | if (field_size <= 0) |
| 2556 | { |
| 2557 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2558 | goto err; |
| 2559 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2560 | |
| 2561 | ecdh_len = (field_size + 7) / 8; |
| 2562 | premaster_secret = OPENSSL_malloc(ecdh_len); |
| 2563 | if (premaster_secret == NULL) |
| 2564 | { |
| 2565 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2566 | goto err; |
| 2567 | } |
| 2568 | |
| 2569 | /* Compute the shared pre-master secret */ |
| 2570 | ecdh_len = ECDH_compute_key(premaster_secret, |
| 2571 | ecdh_len, clnt_ecpoint, srvr_ecdh, NULL); |
| 2572 | if (ecdh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2573 | { |
| 2574 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2575 | goto err; |
| 2576 | } |
| 2577 | |
| 2578 | EVP_PKEY_free(clnt_pub_pkey); |
| 2579 | EC_POINT_free(clnt_ecpoint); |
| 2580 | EC_KEY_free(srvr_ecdh); |
| 2581 | BN_CTX_free(bn_ctx); |
| 2582 | EC_KEY_free(s->s3->tmp.ecdh); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2583 | s->s3->tmp.ecdh = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2584 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2585 | #ifndef OPENSSL_NO_PSK |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2586 | /* ECDHE PSK ciphersuites from RFC 5489 */ |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2587 | if ((alg_a & SSL_aPSK) && psk_len != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2588 | { |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2589 | unsigned char *pre_ms; |
| 2590 | unsigned int pre_ms_len; |
| 2591 | unsigned char *t; |
| 2592 | |
| 2593 | pre_ms_len = 2+psk_len+2+ecdh_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2594 | pre_ms = OPENSSL_malloc(pre_ms_len); |
| 2595 | if (pre_ms == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2596 | { |
| 2597 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2598 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2601 | memset(pre_ms, 0, pre_ms_len); |
| 2602 | t = pre_ms; |
| 2603 | s2n(psk_len, t); |
| 2604 | memcpy(t, psk, psk_len); |
| 2605 | t += psk_len; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2606 | s2n(ecdh_len, t); |
| 2607 | memcpy(t, premaster_secret, ecdh_len); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2608 | s->session->master_key_length = s->method->ssl3_enc \ |
| 2609 | -> generate_master_secret(s, |
| 2610 | s->session->master_key, pre_ms, pre_ms_len); |
| 2611 | OPENSSL_cleanse(pre_ms, pre_ms_len); |
| 2612 | OPENSSL_free(pre_ms); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2613 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2614 | #endif /* OPENSSL_NO_PSK */ |
| 2615 | if (!(alg_a & SSL_aPSK)) |
| 2616 | { |
| 2617 | /* Compute the master secret */ |
| 2618 | s->session->master_key_length = s->method->ssl3_enc \ |
| 2619 | -> generate_master_secret(s, |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2620 | s->session->master_key, premaster_secret, ecdh_len); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2623 | OPENSSL_cleanse(premaster_secret, ecdh_len); |
| 2624 | OPENSSL_free(premaster_secret); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2625 | return ret; |
| 2626 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2627 | #endif |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2628 | else if (alg_k & SSL_kGOST) |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2629 | { |
| 2630 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_GOST_NOT_SUPPORTED); |
| 2631 | goto err; |
| 2632 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2633 | else if (!(alg_k & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2634 | { |
| 2635 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2636 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNKNOWN_CIPHER_TYPE); |
| 2637 | goto f_err; |
| 2638 | } |
| 2639 | |
| 2640 | return(1); |
| 2641 | f_err: |
| 2642 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2643 | err: |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame^] | 2644 | if (premaster_secret) |
| 2645 | OPENSSL_free(premaster_secret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2646 | #ifndef OPENSSL_NO_ECDH |
| 2647 | EVP_PKEY_free(clnt_pub_pkey); |
| 2648 | EC_POINT_free(clnt_ecpoint); |
| 2649 | if (srvr_ecdh != NULL) |
| 2650 | EC_KEY_free(srvr_ecdh); |
| 2651 | BN_CTX_free(bn_ctx); |
| 2652 | #endif |
| 2653 | return(-1); |
| 2654 | } |
| 2655 | |
| 2656 | int ssl3_get_cert_verify(SSL *s) |
| 2657 | { |
| 2658 | EVP_PKEY *pkey=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2659 | int al,ok,ret=0; |
| 2660 | long n; |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2661 | CBS certificate_verify, signature; |
| 2662 | int type = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2663 | X509 *peer; |
| 2664 | const EVP_MD *md = NULL; |
| 2665 | EVP_MD_CTX mctx; |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2666 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2667 | EVP_MD_CTX_init(&mctx); |
| 2668 | |
| 2669 | n=s->method->ssl_get_message(s, |
| 2670 | SSL3_ST_SR_CERT_VRFY_A, |
| 2671 | SSL3_ST_SR_CERT_VRFY_B, |
| 2672 | -1, |
| 2673 | 516, /* Enough for 4096 bit RSA key with TLS v1.2 */ |
| 2674 | &ok); |
| 2675 | |
| 2676 | if (!ok) return((int)n); |
| 2677 | |
| 2678 | if (s->session->peer != NULL) |
| 2679 | { |
| 2680 | peer=s->session->peer; |
| 2681 | pkey=X509_get_pubkey(peer); |
| 2682 | type=X509_certificate_type(peer,pkey); |
| 2683 | } |
| 2684 | else |
| 2685 | { |
| 2686 | peer=NULL; |
| 2687 | pkey=NULL; |
| 2688 | } |
| 2689 | |
| 2690 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) |
| 2691 | { |
| 2692 | s->s3->tmp.reuse_message=1; |
| 2693 | if ((peer != NULL) && (type & EVP_PKT_SIGN)) |
| 2694 | { |
| 2695 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2696 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_MISSING_VERIFY_MESSAGE); |
| 2697 | goto f_err; |
| 2698 | } |
| 2699 | ret=1; |
| 2700 | goto end; |
| 2701 | } |
| 2702 | |
| 2703 | if (peer == NULL) |
| 2704 | { |
| 2705 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_NO_CLIENT_CERT_RECEIVED); |
| 2706 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2707 | goto f_err; |
| 2708 | } |
| 2709 | |
| 2710 | if (!(type & EVP_PKT_SIGN)) |
| 2711 | { |
| 2712 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); |
| 2713 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 2714 | goto f_err; |
| 2715 | } |
| 2716 | |
| 2717 | if (s->s3->change_cipher_spec) |
| 2718 | { |
| 2719 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_CCS_RECEIVED_EARLY); |
| 2720 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2721 | goto f_err; |
| 2722 | } |
| 2723 | |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2724 | CBS_init(&certificate_verify, s->init_msg, n); |
| 2725 | |
| 2726 | /* We now have a signature that we need to verify. */ |
| 2727 | /* TODO(davidben): This should share code with |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 2728 | * ssl3_get_server_key_exchange. */ |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 2729 | |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2730 | if (SSL_USE_SIGALGS(s)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2731 | { |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 2732 | if (!tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2733 | goto f_err; |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2734 | } |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2735 | |
| 2736 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 2737 | CBS_len(&certificate_verify) != 0) |
Adam Langley | 3213bed | 2014-06-23 12:26:07 -0700 | [diff] [blame] | 2738 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2739 | al = SSL_AD_DECODE_ERROR; |
| 2740 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2741 | goto f_err; |
| 2742 | } |
| 2743 | |
| 2744 | if (SSL_USE_SIGALGS(s)) |
| 2745 | { |
| 2746 | long hdatalen = 0; |
| 2747 | char *hdata; |
| 2748 | hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); |
| 2749 | if (hdatalen <= 0) |
| 2750 | { |
| 2751 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR); |
| 2752 | al=SSL_AD_INTERNAL_ERROR; |
| 2753 | goto f_err; |
| 2754 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2755 | if (!EVP_VerifyInit_ex(&mctx, md, NULL) |
| 2756 | || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) |
| 2757 | { |
| 2758 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_EVP_LIB); |
| 2759 | al=SSL_AD_INTERNAL_ERROR; |
| 2760 | goto f_err; |
| 2761 | } |
| 2762 | |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2763 | if (EVP_VerifyFinal(&mctx, |
| 2764 | CBS_data(&signature), CBS_len(&signature), |
| 2765 | pkey) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2766 | { |
| 2767 | al=SSL_AD_DECRYPT_ERROR; |
| 2768 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE); |
| 2769 | goto f_err; |
| 2770 | } |
| 2771 | } |
| 2772 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2773 | if (pkey->type == EVP_PKEY_RSA) |
| 2774 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2775 | if (!RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, |
| 2776 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, |
| 2777 | CBS_data(&signature), CBS_len(&signature), |
| 2778 | pkey->pkey.rsa)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2779 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2780 | al = SSL_AD_DECRYPT_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2781 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_RSA_SIGNATURE); |
| 2782 | goto f_err; |
| 2783 | } |
| 2784 | } |
| 2785 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2786 | #ifndef OPENSSL_NO_DSA |
| 2787 | if (pkey->type == EVP_PKEY_DSA) |
| 2788 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2789 | if (DSA_verify(pkey->save_type, |
| 2790 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), |
| 2791 | SHA_DIGEST_LENGTH, |
| 2792 | CBS_data(&signature), CBS_len(&signature), |
| 2793 | pkey->pkey.dsa) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2794 | { |
| 2795 | /* bad signature */ |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2796 | al = SSL_AD_DECRYPT_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2797 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_DSA_SIGNATURE); |
| 2798 | goto f_err; |
| 2799 | } |
| 2800 | } |
| 2801 | else |
| 2802 | #endif |
| 2803 | #ifndef OPENSSL_NO_ECDSA |
| 2804 | if (pkey->type == EVP_PKEY_EC) |
| 2805 | { |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2806 | if (!ECDSA_verify(pkey->save_type, |
| 2807 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), |
| 2808 | SHA_DIGEST_LENGTH, |
| 2809 | CBS_data(&signature), CBS_len(&signature), |
| 2810 | pkey->pkey.ec)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2811 | { |
| 2812 | /* bad signature */ |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2813 | al = SSL_AD_DECRYPT_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2814 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_ECDSA_SIGNATURE); |
| 2815 | goto f_err; |
| 2816 | } |
| 2817 | } |
| 2818 | else |
| 2819 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2820 | { |
| 2821 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, ERR_R_INTERNAL_ERROR); |
| 2822 | al=SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2823 | goto f_err; |
| 2824 | } |
| 2825 | |
| 2826 | |
| 2827 | ret=1; |
| 2828 | if (0) |
| 2829 | { |
| 2830 | f_err: |
| 2831 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2832 | } |
| 2833 | end: |
| 2834 | if (s->s3->handshake_buffer) |
| 2835 | { |
| 2836 | BIO_free(s->s3->handshake_buffer); |
| 2837 | s->s3->handshake_buffer = NULL; |
| 2838 | s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; |
| 2839 | } |
| 2840 | EVP_MD_CTX_cleanup(&mctx); |
| 2841 | EVP_PKEY_free(pkey); |
| 2842 | return(ret); |
| 2843 | } |
| 2844 | |
| 2845 | int ssl3_get_client_certificate(SSL *s) |
| 2846 | { |
| 2847 | int i,ok,al,ret= -1; |
| 2848 | X509 *x=NULL; |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2849 | unsigned long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2850 | STACK_OF(X509) *sk=NULL; |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2851 | SHA256_CTX sha256; |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2852 | CBS certificate_msg, certificate_list; |
| 2853 | int is_first_certificate = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2854 | |
| 2855 | n=s->method->ssl_get_message(s, |
| 2856 | SSL3_ST_SR_CERT_A, |
| 2857 | SSL3_ST_SR_CERT_B, |
| 2858 | -1, |
| 2859 | s->max_cert_list, |
| 2860 | &ok); |
| 2861 | |
| 2862 | if (!ok) return((int)n); |
| 2863 | |
| 2864 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) |
| 2865 | { |
| 2866 | if ( (s->verify_mode & SSL_VERIFY_PEER) && |
| 2867 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2868 | { |
| 2869 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2870 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2871 | goto f_err; |
| 2872 | } |
| 2873 | /* If tls asked for a client cert, the client must return a 0 list */ |
| 2874 | if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) |
| 2875 | { |
| 2876 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2877 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2878 | goto f_err; |
| 2879 | } |
| 2880 | s->s3->tmp.reuse_message=1; |
| 2881 | return(1); |
| 2882 | } |
| 2883 | |
| 2884 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 2885 | { |
| 2886 | al=SSL_AD_UNEXPECTED_MESSAGE; |
| 2887 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_WRONG_MESSAGE_TYPE); |
| 2888 | goto f_err; |
| 2889 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2890 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 2891 | CBS_init(&certificate_msg, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2892 | |
| 2893 | if ((sk=sk_X509_new_null()) == NULL) |
| 2894 | { |
| 2895 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2896 | goto err; |
| 2897 | } |
| 2898 | |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2899 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 2900 | CBS_len(&certificate_msg) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2901 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2902 | al = SSL_AD_DECODE_ERROR; |
| 2903 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2904 | goto f_err; |
| 2905 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2906 | |
| 2907 | while (CBS_len(&certificate_list) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2908 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2909 | CBS certificate; |
| 2910 | const uint8_t *data; |
| 2911 | |
| 2912 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2913 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2914 | al = SSL_AD_DECODE_ERROR; |
| 2915 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2916 | goto f_err; |
| 2917 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2918 | if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2919 | { |
| 2920 | /* If this is the first certificate, and we don't want |
| 2921 | * to keep peer certificates in memory, then we hash it |
| 2922 | * right away. */ |
| 2923 | SHA256_Init(&sha256); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2924 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2925 | SHA256_Final(s->session->peer_sha256, &sha256); |
| 2926 | s->session->peer_sha256_valid = 1; |
| 2927 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2928 | is_first_certificate = 0; |
| 2929 | data = CBS_data(&certificate); |
| 2930 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2931 | if (x == NULL) |
| 2932 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2933 | al = SSL_AD_BAD_CERTIFICATE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2934 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2935 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2936 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2937 | if (!CBS_skip(&certificate, data - CBS_data(&certificate))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2938 | { |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2939 | al = SSL_AD_INTERNAL_ERROR; |
| 2940 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_INTERNAL_ERROR); |
| 2941 | goto f_err; |
| 2942 | } |
| 2943 | if (CBS_len(&certificate) != 0) |
| 2944 | { |
| 2945 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2946 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 2947 | goto f_err; |
| 2948 | } |
| 2949 | if (!sk_X509_push(sk,x)) |
| 2950 | { |
| 2951 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2952 | goto err; |
| 2953 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2954 | x = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | if (sk_X509_num(sk) <= 0) |
| 2958 | { |
| 2959 | /* TLS does not mind 0 certs returned */ |
| 2960 | if (s->version == SSL3_VERSION) |
| 2961 | { |
| 2962 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2963 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_NO_CERTIFICATES_RETURNED); |
| 2964 | goto f_err; |
| 2965 | } |
| 2966 | /* Fail for TLS only if we required a certificate */ |
| 2967 | else if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2968 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 2969 | { |
| 2970 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2971 | al=SSL_AD_HANDSHAKE_FAILURE; |
| 2972 | goto f_err; |
| 2973 | } |
| 2974 | /* No client certificate so digest cached records */ |
| 2975 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) |
| 2976 | { |
| 2977 | al=SSL_AD_INTERNAL_ERROR; |
| 2978 | goto f_err; |
| 2979 | } |
| 2980 | } |
| 2981 | else |
| 2982 | { |
| 2983 | i=ssl_verify_cert_chain(s,sk); |
| 2984 | if (i <= 0) |
| 2985 | { |
| 2986 | al=ssl_verify_alarm_type(s->verify_result); |
| 2987 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2988 | goto f_err; |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | if (s->session->peer != NULL) /* This should not be needed */ |
| 2993 | X509_free(s->session->peer); |
| 2994 | s->session->peer=sk_X509_shift(sk); |
| 2995 | s->session->verify_result = s->verify_result; |
| 2996 | |
| 2997 | /* With the current implementation, sess_cert will always be NULL |
| 2998 | * when we arrive here. */ |
| 2999 | if (s->session->sess_cert == NULL) |
| 3000 | { |
| 3001 | s->session->sess_cert = ssl_sess_cert_new(); |
| 3002 | if (s->session->sess_cert == NULL) |
| 3003 | { |
| 3004 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 3005 | goto err; |
| 3006 | } |
| 3007 | } |
| 3008 | if (s->session->sess_cert->cert_chain != NULL) |
| 3009 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); |
| 3010 | s->session->sess_cert->cert_chain=sk; |
| 3011 | /* Inconsistency alert: cert_chain does *not* include the |
| 3012 | * peer's own certificate, while we do include it in s3_clnt.c */ |
| 3013 | |
| 3014 | sk=NULL; |
| 3015 | |
| 3016 | ret=1; |
| 3017 | if (0) |
| 3018 | { |
| 3019 | f_err: |
| 3020 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 3021 | } |
| 3022 | err: |
| 3023 | if (x != NULL) X509_free(x); |
| 3024 | if (sk != NULL) sk_X509_pop_free(sk,X509_free); |
| 3025 | return(ret); |
| 3026 | } |
| 3027 | |
| 3028 | int ssl3_send_server_certificate(SSL *s) |
| 3029 | { |
| 3030 | CERT_PKEY *cpk; |
| 3031 | |
| 3032 | if (s->state == SSL3_ST_SW_CERT_A) |
| 3033 | { |
| 3034 | cpk=ssl_get_server_send_pkey(s); |
| 3035 | if (cpk == NULL) |
| 3036 | { |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 3037 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, ERR_R_INTERNAL_ERROR); |
| 3038 | return(0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | ssl3_output_cert_chain(s,cpk); |
| 3042 | s->state=SSL3_ST_SW_CERT_B; |
| 3043 | } |
| 3044 | |
| 3045 | /* SSL3_ST_SW_CERT_B */ |
| 3046 | return ssl_do_write(s); |
| 3047 | } |
| 3048 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3049 | /* send a new session ticket (not necessarily for a new session) */ |
| 3050 | int ssl3_send_newsession_ticket(SSL *s) |
| 3051 | { |
| 3052 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) |
| 3053 | { |
| 3054 | unsigned char *p, *senc, *macstart; |
| 3055 | const unsigned char *const_p; |
| 3056 | int len, slen_full, slen; |
| 3057 | SSL_SESSION *sess; |
| 3058 | unsigned int hlen; |
| 3059 | EVP_CIPHER_CTX ctx; |
| 3060 | HMAC_CTX hctx; |
| 3061 | SSL_CTX *tctx = s->initial_ctx; |
| 3062 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 3063 | unsigned char key_name[16]; |
| 3064 | |
| 3065 | /* get session encoding length */ |
| 3066 | slen_full = i2d_SSL_SESSION(s->session, NULL); |
| 3067 | /* Some length values are 16 bits, so forget it if session is |
| 3068 | * too long |
| 3069 | */ |
| 3070 | if (slen_full > 0xFF00) |
| 3071 | return -1; |
| 3072 | senc = OPENSSL_malloc(slen_full); |
| 3073 | if (!senc) |
| 3074 | return -1; |
| 3075 | p = senc; |
| 3076 | i2d_SSL_SESSION(s->session, &p); |
| 3077 | |
| 3078 | /* create a fresh copy (not shared with other threads) to clean up */ |
| 3079 | const_p = senc; |
| 3080 | sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); |
| 3081 | if (sess == NULL) |
| 3082 | { |
| 3083 | OPENSSL_free(senc); |
| 3084 | return -1; |
| 3085 | } |
| 3086 | sess->session_id_length = 0; /* ID is irrelevant for the ticket */ |
| 3087 | |
| 3088 | slen = i2d_SSL_SESSION(sess, NULL); |
| 3089 | if (slen > slen_full) /* shouldn't ever happen */ |
| 3090 | { |
| 3091 | OPENSSL_free(senc); |
| 3092 | return -1; |
| 3093 | } |
| 3094 | p = senc; |
| 3095 | i2d_SSL_SESSION(sess, &p); |
| 3096 | SSL_SESSION_free(sess); |
| 3097 | |
| 3098 | /* Grow buffer if need be: the length calculation is as |
| 3099 | * follows handshake_header_length + |
| 3100 | * 4 (ticket lifetime hint) + 2 (ticket length) + |
| 3101 | * 16 (key name) + max_iv_len (iv length) + |
| 3102 | * session_length + max_enc_block_size (max encrypted session |
| 3103 | * length) + max_md_size (HMAC). |
| 3104 | */ |
| 3105 | if (!BUF_MEM_grow(s->init_buf, |
| 3106 | SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH + |
| 3107 | EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) |
| 3108 | return -1; |
| 3109 | p = ssl_handshake_start(s); |
| 3110 | EVP_CIPHER_CTX_init(&ctx); |
| 3111 | HMAC_CTX_init(&hctx); |
| 3112 | /* Initialize HMAC and cipher contexts. If callback present |
| 3113 | * it does all the work otherwise use generated values |
| 3114 | * from parent ctx. |
| 3115 | */ |
| 3116 | if (tctx->tlsext_ticket_key_cb) |
| 3117 | { |
| 3118 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, |
| 3119 | &hctx, 1) < 0) |
| 3120 | { |
| 3121 | OPENSSL_free(senc); |
| 3122 | return -1; |
| 3123 | } |
| 3124 | } |
| 3125 | else |
| 3126 | { |
| 3127 | RAND_pseudo_bytes(iv, 16); |
| 3128 | EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 3129 | tctx->tlsext_tick_aes_key, iv); |
| 3130 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, |
| 3131 | tlsext_tick_md(), NULL); |
| 3132 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 3133 | } |
| 3134 | |
| 3135 | /* Ticket lifetime hint (advisory only): |
| 3136 | * We leave this unspecified for resumed session (for simplicity), |
| 3137 | * and guess that tickets for new sessions will live as long |
| 3138 | * as their sessions. */ |
| 3139 | l2n(s->hit ? 0 : s->session->timeout, p); |
| 3140 | |
| 3141 | /* Skip ticket length for now */ |
| 3142 | p += 2; |
| 3143 | /* Output key name */ |
| 3144 | macstart = p; |
| 3145 | memcpy(p, key_name, 16); |
| 3146 | p += 16; |
| 3147 | /* output IV */ |
| 3148 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 3149 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 3150 | /* Encrypt session data */ |
| 3151 | EVP_EncryptUpdate(&ctx, p, &len, senc, slen); |
| 3152 | p += len; |
| 3153 | EVP_EncryptFinal_ex(&ctx, p, &len); |
| 3154 | p += len; |
| 3155 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 3156 | |
| 3157 | HMAC_Update(&hctx, macstart, p - macstart); |
| 3158 | HMAC_Final(&hctx, p, &hlen); |
| 3159 | HMAC_CTX_cleanup(&hctx); |
| 3160 | |
| 3161 | p += hlen; |
| 3162 | /* Now write out lengths: p points to end of data written */ |
| 3163 | /* Total length */ |
| 3164 | len = p - ssl_handshake_start(s); |
| 3165 | ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len); |
| 3166 | /* Skip ticket lifetime hint */ |
| 3167 | p = ssl_handshake_start(s) + 4; |
| 3168 | s2n(len - 6, p); |
| 3169 | s->state=SSL3_ST_SW_SESSION_TICKET_B; |
| 3170 | OPENSSL_free(senc); |
| 3171 | } |
| 3172 | |
| 3173 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
| 3174 | return ssl_do_write(s); |
| 3175 | } |
| 3176 | |
| 3177 | int ssl3_send_cert_status(SSL *s) |
| 3178 | { |
| 3179 | if (s->state == SSL3_ST_SW_CERT_STATUS_A) |
| 3180 | { |
| 3181 | unsigned char *p; |
| 3182 | /* Grow buffer if need be: the length calculation is as |
| 3183 | * follows 1 (message type) + 3 (message length) + |
| 3184 | * 1 (ocsp response type) + 3 (ocsp response length) |
| 3185 | * + (ocsp response) |
| 3186 | */ |
| 3187 | if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) |
| 3188 | return -1; |
| 3189 | |
| 3190 | p=(unsigned char *)s->init_buf->data; |
| 3191 | |
| 3192 | /* do the header */ |
| 3193 | *(p++)=SSL3_MT_CERTIFICATE_STATUS; |
| 3194 | /* message length */ |
| 3195 | l2n3(s->tlsext_ocsp_resplen + 4, p); |
| 3196 | /* status type */ |
| 3197 | *(p++)= s->tlsext_status_type; |
| 3198 | /* length of OCSP response */ |
| 3199 | l2n3(s->tlsext_ocsp_resplen, p); |
| 3200 | /* actual response */ |
| 3201 | memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); |
| 3202 | /* number of bytes to write */ |
| 3203 | s->init_num = 8 + s->tlsext_ocsp_resplen; |
| 3204 | s->state=SSL3_ST_SW_CERT_STATUS_B; |
| 3205 | s->init_off = 0; |
| 3206 | } |
| 3207 | |
| 3208 | /* SSL3_ST_SW_CERT_STATUS_B */ |
| 3209 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 3210 | } |
| 3211 | |
| 3212 | # ifndef OPENSSL_NO_NEXTPROTONEG |
| 3213 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 3214 | * sets the next_proto member in s if found */ |
| 3215 | int ssl3_get_next_proto(SSL *s) |
| 3216 | { |
| 3217 | int ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3218 | long n; |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 3219 | CBS next_protocol, selected_protocol, padding; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3220 | |
| 3221 | /* Clients cannot send a NextProtocol message if we didn't see the |
| 3222 | * extension in their ClientHello */ |
| 3223 | if (!s->s3->next_proto_neg_seen) |
| 3224 | { |
| 3225 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
| 3226 | return -1; |
| 3227 | } |
| 3228 | |
| 3229 | n=s->method->ssl_get_message(s, |
| 3230 | SSL3_ST_SR_NEXT_PROTO_A, |
| 3231 | SSL3_ST_SR_NEXT_PROTO_B, |
| 3232 | SSL3_MT_NEXT_PROTO, |
| 3233 | 514, /* See the payload format below */ |
| 3234 | &ok); |
| 3235 | |
| 3236 | if (!ok) |
| 3237 | return((int)n); |
| 3238 | |
| 3239 | /* s->state doesn't reflect whether ChangeCipherSpec has been received |
| 3240 | * in this handshake, but s->s3->change_cipher_spec does (will be reset |
| 3241 | * by ssl3_get_finished). */ |
| 3242 | if (!s->s3->change_cipher_spec) |
| 3243 | { |
| 3244 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); |
| 3245 | return -1; |
| 3246 | } |
| 3247 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 3248 | CBS_init(&next_protocol, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3249 | |
| 3250 | /* The payload looks like: |
| 3251 | * uint8 proto_len; |
| 3252 | * uint8 proto[proto_len]; |
| 3253 | * uint8 padding_len; |
| 3254 | * uint8 padding[padding_len]; |
| 3255 | */ |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 3256 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 3257 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 3258 | CBS_len(&next_protocol) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3259 | return 0; |
| 3260 | |
David Benjamin | ced551f | 2014-07-11 23:16:16 -0400 | [diff] [blame] | 3261 | if (!CBS_stow(&selected_protocol, |
| 3262 | &s->next_proto_negotiated, |
| 3263 | &s->next_proto_negotiated_len)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3264 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3265 | |
| 3266 | return 1; |
| 3267 | } |
| 3268 | # endif |
| 3269 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3270 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 3271 | int ssl3_get_channel_id(SSL *s) |
| 3272 | { |
| 3273 | int ret = -1, ok; |
| 3274 | long n; |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 3275 | const uint8_t *p; |
| 3276 | uint16_t extension_type, expected_extension_type; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3277 | EC_GROUP* p256 = NULL; |
| 3278 | EC_KEY* key = NULL; |
| 3279 | EC_POINT* point = NULL; |
| 3280 | ECDSA_SIG sig; |
| 3281 | BIGNUM x, y; |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 3282 | CBS encrypted_extensions, extension; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3283 | |
| 3284 | if (s->state == SSL3_ST_SR_CHANNEL_ID_A && s->init_num == 0) |
| 3285 | { |
| 3286 | /* The first time that we're called we take the current |
| 3287 | * handshake hash and store it. */ |
| 3288 | EVP_MD_CTX md_ctx; |
| 3289 | unsigned int len; |
| 3290 | |
| 3291 | EVP_MD_CTX_init(&md_ctx); |
| 3292 | EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL); |
| 3293 | if (!tls1_channel_id_hash(&md_ctx, s)) |
| 3294 | return -1; |
| 3295 | len = sizeof(s->s3->tlsext_channel_id); |
| 3296 | EVP_DigestFinal(&md_ctx, s->s3->tlsext_channel_id, &len); |
| 3297 | EVP_MD_CTX_cleanup(&md_ctx); |
| 3298 | } |
| 3299 | |
| 3300 | n = s->method->ssl_get_message(s, |
| 3301 | SSL3_ST_SR_CHANNEL_ID_A, |
| 3302 | SSL3_ST_SR_CHANNEL_ID_B, |
| 3303 | SSL3_MT_ENCRYPTED_EXTENSIONS, |
| 3304 | 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, |
| 3305 | &ok); |
| 3306 | |
| 3307 | if (!ok) |
| 3308 | return((int)n); |
| 3309 | |
| 3310 | ssl3_finish_mac(s, (unsigned char*)s->init_buf->data, s->init_num + 4); |
| 3311 | |
| 3312 | /* s->state doesn't reflect whether ChangeCipherSpec has been received |
| 3313 | * in this handshake, but s->s3->change_cipher_spec does (will be reset |
| 3314 | * by ssl3_get_finished). */ |
| 3315 | if (!s->s3->change_cipher_spec) |
| 3316 | { |
| 3317 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS); |
| 3318 | return -1; |
| 3319 | } |
| 3320 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 3321 | CBS_init(&encrypted_extensions, s->init_msg, n); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3322 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 3323 | /* EncryptedExtensions could include multiple extensions, but |
| 3324 | * the only extension that could be negotiated is ChannelID, |
| 3325 | * so there can only be one entry. |
| 3326 | * |
| 3327 | * The payload looks like: |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3328 | * uint16 extension_type |
| 3329 | * uint16 extension_len; |
| 3330 | * uint8 x[32]; |
| 3331 | * uint8 y[32]; |
| 3332 | * uint8 r[32]; |
| 3333 | * uint8 s[32]; |
| 3334 | */ |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3335 | expected_extension_type = TLSEXT_TYPE_channel_id; |
| 3336 | if (s->s3->tlsext_channel_id_new) |
| 3337 | expected_extension_type = TLSEXT_TYPE_channel_id_new; |
| 3338 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 3339 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 3340 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 3341 | CBS_len(&encrypted_extensions) != 0 || |
| 3342 | extension_type != expected_extension_type || |
| 3343 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3344 | { |
| 3345 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE); |
| 3346 | return -1; |
| 3347 | } |
| 3348 | |
| 3349 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 3350 | if (!p256) |
| 3351 | { |
| 3352 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT); |
| 3353 | return -1; |
| 3354 | } |
| 3355 | |
| 3356 | BN_init(&x); |
| 3357 | BN_init(&y); |
| 3358 | sig.r = BN_new(); |
| 3359 | sig.s = BN_new(); |
| 3360 | |
David Benjamin | 9a6232f | 2014-07-11 23:07:47 -0400 | [diff] [blame] | 3361 | p = CBS_data(&extension); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3362 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 3363 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 3364 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 3365 | BN_bin2bn(p + 96, 32, sig.s) == NULL) |
| 3366 | goto err; |
| 3367 | |
| 3368 | point = EC_POINT_new(p256); |
| 3369 | if (!point || |
| 3370 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) |
| 3371 | goto err; |
| 3372 | |
| 3373 | key = EC_KEY_new(); |
| 3374 | if (!key || |
| 3375 | !EC_KEY_set_group(key, p256) || |
| 3376 | !EC_KEY_set_public_key(key, point)) |
| 3377 | goto err; |
| 3378 | |
| 3379 | /* We stored the handshake hash in |tlsext_channel_id| the first time |
| 3380 | * that we were called. */ |
| 3381 | switch (ECDSA_do_verify(s->s3->tlsext_channel_id, SHA256_DIGEST_LENGTH, &sig, key)) { |
| 3382 | case 1: |
| 3383 | break; |
| 3384 | case 0: |
| 3385 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 3386 | s->s3->tlsext_channel_id_valid = 0; |
| 3387 | goto err; |
| 3388 | default: |
| 3389 | s->s3->tlsext_channel_id_valid = 0; |
| 3390 | goto err; |
| 3391 | } |
| 3392 | |
| 3393 | memcpy(s->s3->tlsext_channel_id, p, 64); |
| 3394 | ret = 1; |
| 3395 | |
| 3396 | err: |
| 3397 | BN_free(&x); |
| 3398 | BN_free(&y); |
| 3399 | BN_free(sig.r); |
| 3400 | BN_free(sig.s); |
| 3401 | if (key) |
| 3402 | EC_KEY_free(key); |
| 3403 | if (point) |
| 3404 | EC_POINT_free(point); |
| 3405 | if (p256) |
| 3406 | EC_GROUP_free(p256); |
| 3407 | return ret; |
| 3408 | } |
| 3409 | |