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