Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
| 149 | #define NETSCAPE_HANG_BUG |
| 150 | |
| 151 | #include <assert.h> |
| 152 | #include <stdio.h> |
| 153 | #include <string.h> |
| 154 | |
| 155 | #include <openssl/bn.h> |
| 156 | #include <openssl/buf.h> |
| 157 | #include <openssl/bytestring.h> |
| 158 | #include <openssl/cipher.h> |
| 159 | #include <openssl/dh.h> |
| 160 | #include <openssl/ec.h> |
| 161 | #include <openssl/ecdsa.h> |
| 162 | #include <openssl/evp.h> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
| 166 | #include <openssl/obj.h> |
| 167 | #include <openssl/rand.h> |
| 168 | #include <openssl/sha.h> |
| 169 | #include <openssl/x509.h> |
| 170 | |
| 171 | #include "ssl_locl.h" |
| 172 | #include "../crypto/internal.h" |
| 173 | #include "../crypto/dh/internal.h" |
| 174 | |
| 175 | |
| 176 | /* INITIAL_SNIFF_BUFFER_SIZE is the number of bytes read in the initial sniff |
| 177 | * buffer. */ |
| 178 | #define INITIAL_SNIFF_BUFFER_SIZE 8 |
| 179 | |
| 180 | int ssl3_accept(SSL *s) { |
| 181 | BUF_MEM *buf = NULL; |
| 182 | unsigned long alg_a; |
| 183 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 184 | int ret = -1; |
| 185 | int new_state, state, skip = 0; |
| 186 | |
| 187 | assert(s->handshake_func == ssl3_accept); |
| 188 | assert(s->server); |
| 189 | assert(!SSL_IS_DTLS(s)); |
| 190 | |
| 191 | ERR_clear_error(); |
| 192 | ERR_clear_system_error(); |
| 193 | |
| 194 | if (s->info_callback != NULL) { |
| 195 | cb = s->info_callback; |
| 196 | } else if (s->ctx->info_callback != NULL) { |
| 197 | cb = s->ctx->info_callback; |
| 198 | } |
| 199 | |
| 200 | s->in_handshake++; |
| 201 | |
| 202 | if (s->cert == NULL) { |
| 203 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | for (;;) { |
| 208 | state = s->state; |
| 209 | |
| 210 | switch (s->state) { |
| 211 | case SSL_ST_RENEGOTIATE: |
| 212 | /* This state is the renegotiate entry point. It sends a HelloRequest |
| 213 | * and nothing else. */ |
| 214 | s->renegotiate = 1; |
| 215 | |
| 216 | if (cb != NULL) { |
| 217 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 218 | } |
| 219 | |
| 220 | if (s->init_buf == NULL) { |
| 221 | buf = BUF_MEM_new(); |
| 222 | if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 223 | ret = -1; |
| 224 | goto end; |
| 225 | } |
| 226 | s->init_buf = buf; |
| 227 | buf = NULL; |
| 228 | } |
| 229 | s->init_num = 0; |
| 230 | |
| 231 | if (!ssl3_setup_buffers(s)) { |
| 232 | ret = -1; |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | if (!s->s3->send_connection_binding && |
| 237 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { |
| 238 | /* Server attempting to renegotiate with client that doesn't support |
| 239 | * secure renegotiation. */ |
| 240 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, |
| 241 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 242 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 243 | ret = -1; |
| 244 | goto end; |
| 245 | } |
| 246 | |
| 247 | s->ctx->stats.sess_accept_renegotiate++; |
| 248 | s->state = SSL3_ST_SW_HELLO_REQ_A; |
| 249 | break; |
| 250 | |
| 251 | case SSL3_ST_SW_HELLO_REQ_A: |
| 252 | case SSL3_ST_SW_HELLO_REQ_B: |
| 253 | s->shutdown = 0; |
| 254 | ret = ssl3_send_hello_request(s); |
| 255 | if (ret <= 0) { |
| 256 | goto end; |
| 257 | } |
| 258 | s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C; |
| 259 | s->state = SSL3_ST_SW_FLUSH; |
| 260 | s->init_num = 0; |
| 261 | |
| 262 | if (!ssl3_init_finished_mac(s)) { |
| 263 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 264 | ret = -1; |
| 265 | goto end; |
| 266 | } |
| 267 | break; |
| 268 | |
| 269 | case SSL3_ST_SW_HELLO_REQ_C: |
| 270 | s->state = SSL_ST_OK; |
| 271 | break; |
| 272 | |
| 273 | case SSL_ST_ACCEPT: |
| 274 | case SSL_ST_BEFORE | SSL_ST_ACCEPT: |
| 275 | /* This state is the entry point for the handshake itself (initial and |
| 276 | * renegotiation). */ |
| 277 | if (cb != NULL) { |
| 278 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 279 | } |
| 280 | |
| 281 | if (s->init_buf == NULL) { |
| 282 | buf = BUF_MEM_new(); |
| 283 | if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 284 | ret = -1; |
| 285 | goto end; |
| 286 | } |
| 287 | s->init_buf = buf; |
| 288 | buf = NULL; |
| 289 | } |
| 290 | s->init_num = 0; |
| 291 | |
| 292 | if (!ssl3_init_finished_mac(s)) { |
| 293 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 294 | ret = -1; |
| 295 | goto end; |
| 296 | } |
| 297 | |
| 298 | if (!s->s3->have_version) { |
| 299 | /* This is the initial handshake. The record layer has not been |
| 300 | * initialized yet. Sniff for a V2ClientHello before reading a |
| 301 | * ClientHello normally. */ |
| 302 | assert(s->s3->rbuf.buf == NULL); |
| 303 | assert(s->s3->wbuf.buf == NULL); |
| 304 | s->state = SSL3_ST_SR_INITIAL_BYTES; |
| 305 | } else { |
| 306 | /* Enable a write buffer. This groups handshake messages within a |
| 307 | * flight into a single write. */ |
| 308 | if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) { |
| 309 | ret = -1; |
| 310 | goto end; |
| 311 | } |
| 312 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 313 | } |
| 314 | s->ctx->stats.sess_accept++; |
| 315 | break; |
| 316 | |
| 317 | case SSL3_ST_SR_INITIAL_BYTES: |
| 318 | ret = ssl3_get_initial_bytes(s); |
| 319 | if (ret <= 0) { |
| 320 | goto end; |
| 321 | } |
| 322 | /* ssl3_get_initial_bytes sets s->state to one of |
| 323 | * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */ |
| 324 | break; |
| 325 | |
| 326 | case SSL3_ST_SR_V2_CLIENT_HELLO: |
| 327 | ret = ssl3_get_v2_client_hello(s); |
| 328 | if (ret <= 0) { |
| 329 | goto end; |
| 330 | } |
| 331 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 332 | break; |
| 333 | |
| 334 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 335 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 336 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 337 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 338 | s->shutdown = 0; |
| 339 | ret = ssl3_get_client_hello(s); |
| 340 | if (ret == PENDING_SESSION) { |
| 341 | s->rwstate = SSL_PENDING_SESSION; |
| 342 | goto end; |
| 343 | } |
| 344 | if (ret == CERTIFICATE_SELECTION_PENDING) { |
| 345 | s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 346 | goto end; |
| 347 | } |
| 348 | if (ret <= 0) { |
| 349 | goto end; |
| 350 | } |
| 351 | s->renegotiate = 2; |
| 352 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 353 | s->init_num = 0; |
| 354 | break; |
| 355 | |
| 356 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 357 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 358 | ret = ssl3_send_server_hello(s); |
| 359 | if (ret <= 0) { |
| 360 | goto end; |
| 361 | } |
| 362 | if (s->hit) { |
| 363 | if (s->tlsext_ticket_expected) { |
| 364 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 365 | } else { |
| 366 | s->state = SSL3_ST_SW_CHANGE_A; |
| 367 | } |
| 368 | } else { |
| 369 | s->state = SSL3_ST_SW_CERT_A; |
| 370 | } |
| 371 | s->init_num = 0; |
| 372 | break; |
| 373 | |
| 374 | case SSL3_ST_SW_CERT_A: |
| 375 | case SSL3_ST_SW_CERT_B: |
| 376 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 377 | ret = ssl3_send_server_certificate(s); |
| 378 | if (ret <= 0) { |
| 379 | goto end; |
| 380 | } |
| 381 | if (s->s3->tmp.certificate_status_expected) { |
| 382 | s->state = SSL3_ST_SW_CERT_STATUS_A; |
| 383 | } else { |
| 384 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 385 | } |
| 386 | } else { |
| 387 | skip = 1; |
| 388 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 389 | } |
| 390 | s->init_num = 0; |
| 391 | break; |
| 392 | |
| 393 | case SSL3_ST_SW_KEY_EXCH_A: |
| 394 | case SSL3_ST_SW_KEY_EXCH_B: |
| 395 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 396 | |
| 397 | /* Send a ServerKeyExchange message if: |
| 398 | * - The key exchange is ephemeral or anonymous |
| 399 | * Diffie-Hellman. |
| 400 | * - There is a PSK identity hint. |
| 401 | * |
| 402 | * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix |
| 403 | * this. In the meantime, keep them in sync. */ |
| 404 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
| 405 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) { |
| 406 | ret = ssl3_send_server_key_exchange(s); |
| 407 | if (ret <= 0) |
| 408 | goto end; |
| 409 | } else { |
| 410 | skip = 1; |
| 411 | } |
| 412 | |
| 413 | s->state = SSL3_ST_SW_CERT_REQ_A; |
| 414 | s->init_num = 0; |
| 415 | break; |
| 416 | |
| 417 | case SSL3_ST_SW_CERT_REQ_A: |
| 418 | case SSL3_ST_SW_CERT_REQ_B: |
| 419 | if (/* don't request cert unless asked for it: */ |
| 420 | !(s->verify_mode & SSL_VERIFY_PEER) || |
| 421 | /* Don't request a certificate if an obc was presented */ |
| 422 | ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 423 | s->s3->tlsext_channel_id_valid) || |
| 424 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 425 | * don't request cert during re-negotiation: */ |
| 426 | ((s->session->peer != NULL) && |
| 427 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 428 | /* never request cert in anonymous ciphersuites |
| 429 | * (see section "Certificate request" in SSL 3 drafts |
| 430 | * and in RFC 2246): */ |
| 431 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 432 | /* ... except when the application insists on verification |
| 433 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 434 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
| 435 | /* With normal PSK Certificates and |
| 436 | * Certificate Requests are omitted */ |
| 437 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 438 | /* no cert request */ |
| 439 | skip = 1; |
| 440 | s->s3->tmp.cert_request = 0; |
| 441 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
| 442 | if (s->s3->handshake_buffer && |
| 443 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 444 | return -1; |
| 445 | } |
| 446 | } else { |
| 447 | s->s3->tmp.cert_request = 1; |
| 448 | ret = ssl3_send_certificate_request(s); |
| 449 | if (ret <= 0) { |
| 450 | goto end; |
| 451 | } |
| 452 | #ifndef NETSCAPE_HANG_BUG |
| 453 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
| 454 | #else |
| 455 | /* ServerHelloDone was already sent in the |
| 456 | * previous record. */ |
| 457 | s->state = SSL3_ST_SW_FLUSH; |
| 458 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 459 | #endif |
| 460 | s->init_num = 0; |
| 461 | } |
| 462 | break; |
| 463 | |
| 464 | case SSL3_ST_SW_SRVR_DONE_A: |
| 465 | case SSL3_ST_SW_SRVR_DONE_B: |
| 466 | ret = ssl3_send_server_done(s); |
| 467 | if (ret <= 0) { |
| 468 | goto end; |
| 469 | } |
| 470 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 471 | s->state = SSL3_ST_SW_FLUSH; |
| 472 | s->init_num = 0; |
| 473 | break; |
| 474 | |
| 475 | case SSL3_ST_SW_FLUSH: |
| 476 | /* This code originally checked to see if any data was pending using |
| 477 | * BIO_CTRL_INFO and then flushed. This caused problems as documented |
| 478 | * in PR#1939. The proposed fix doesn't completely resolve this issue |
| 479 | * as buggy implementations of BIO_CTRL_PENDING still exist. So instead |
| 480 | * we just flush unconditionally. */ |
| 481 | s->rwstate = SSL_WRITING; |
| 482 | if (BIO_flush(s->wbio) <= 0) { |
| 483 | ret = -1; |
| 484 | goto end; |
| 485 | } |
| 486 | s->rwstate = SSL_NOTHING; |
| 487 | |
| 488 | s->state = s->s3->tmp.next_state; |
| 489 | break; |
| 490 | |
| 491 | case SSL3_ST_SR_CERT_A: |
| 492 | case SSL3_ST_SR_CERT_B: |
| 493 | if (s->s3->tmp.cert_request) { |
| 494 | ret = ssl3_get_client_certificate(s); |
| 495 | if (ret <= 0) { |
| 496 | goto end; |
| 497 | } |
| 498 | } |
| 499 | s->init_num = 0; |
| 500 | s->state = SSL3_ST_SR_KEY_EXCH_A; |
| 501 | break; |
| 502 | |
| 503 | case SSL3_ST_SR_KEY_EXCH_A: |
| 504 | case SSL3_ST_SR_KEY_EXCH_B: |
| 505 | ret = ssl3_get_client_key_exchange(s); |
| 506 | if (ret <= 0) { |
| 507 | goto end; |
| 508 | } |
| 509 | s->state = SSL3_ST_SR_CERT_VRFY_A; |
| 510 | s->init_num = 0; |
| 511 | break; |
| 512 | |
| 513 | case SSL3_ST_SR_CERT_VRFY_A: |
| 514 | case SSL3_ST_SR_CERT_VRFY_B: |
| 515 | ret = ssl3_get_cert_verify(s); |
| 516 | if (ret <= 0) { |
| 517 | goto end; |
| 518 | } |
| 519 | |
| 520 | s->state = SSL3_ST_SR_CHANGE; |
| 521 | s->init_num = 0; |
| 522 | break; |
| 523 | |
| 524 | case SSL3_ST_SR_CHANGE: { |
| 525 | char next_proto_neg = 0; |
| 526 | char channel_id = 0; |
| 527 | next_proto_neg = s->s3->next_proto_neg_seen; |
| 528 | channel_id = s->s3->tlsext_channel_id_valid; |
| 529 | |
| 530 | /* At this point, the next message must be entirely behind a |
| 531 | * ChangeCipherSpec. */ |
| 532 | if (!ssl3_expect_change_cipher_spec(s)) { |
| 533 | ret = -1; |
| 534 | goto end; |
| 535 | } |
| 536 | if (next_proto_neg) { |
| 537 | s->state = SSL3_ST_SR_NEXT_PROTO_A; |
| 538 | } else if (channel_id) { |
| 539 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
| 540 | } else { |
| 541 | s->state = SSL3_ST_SR_FINISHED_A; |
| 542 | } |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | case SSL3_ST_SR_NEXT_PROTO_A: |
| 547 | case SSL3_ST_SR_NEXT_PROTO_B: |
| 548 | ret = ssl3_get_next_proto(s); |
| 549 | if (ret <= 0) { |
| 550 | goto end; |
| 551 | } |
| 552 | s->init_num = 0; |
| 553 | if (s->s3->tlsext_channel_id_valid) { |
| 554 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
| 555 | } else { |
| 556 | s->state = SSL3_ST_SR_FINISHED_A; |
| 557 | } |
| 558 | break; |
| 559 | |
| 560 | case SSL3_ST_SR_CHANNEL_ID_A: |
| 561 | case SSL3_ST_SR_CHANNEL_ID_B: |
| 562 | ret = ssl3_get_channel_id(s); |
| 563 | if (ret <= 0) { |
| 564 | goto end; |
| 565 | } |
| 566 | s->init_num = 0; |
| 567 | s->state = SSL3_ST_SR_FINISHED_A; |
| 568 | break; |
| 569 | |
| 570 | case SSL3_ST_SR_FINISHED_A: |
| 571 | case SSL3_ST_SR_FINISHED_B: |
| 572 | ret = |
| 573 | ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B); |
| 574 | if (ret <= 0) { |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | if (s->hit) { |
| 579 | s->state = SSL_ST_OK; |
| 580 | } else if (s->tlsext_ticket_expected) { |
| 581 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 582 | } else { |
| 583 | s->state = SSL3_ST_SW_CHANGE_A; |
| 584 | } |
| 585 | /* If this is a full handshake with ChannelID then record the hashshake |
| 586 | * hashes in |s->session| in case we need them to verify a ChannelID |
| 587 | * signature on a resumption of this session in the future. */ |
| 588 | if (!s->hit && s->s3->tlsext_channel_id_new) { |
| 589 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 590 | if (ret <= 0) { |
| 591 | goto end; |
| 592 | } |
| 593 | } |
| 594 | s->init_num = 0; |
| 595 | break; |
| 596 | |
| 597 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 598 | case SSL3_ST_SW_SESSION_TICKET_B: |
| 599 | ret = ssl3_send_new_session_ticket(s); |
| 600 | if (ret <= 0) { |
| 601 | goto end; |
| 602 | } |
| 603 | s->state = SSL3_ST_SW_CHANGE_A; |
| 604 | s->init_num = 0; |
| 605 | break; |
| 606 | |
| 607 | case SSL3_ST_SW_CHANGE_A: |
| 608 | case SSL3_ST_SW_CHANGE_B: |
| 609 | s->session->cipher = s->s3->tmp.new_cipher; |
| 610 | if (!s->enc_method->setup_key_block(s)) { |
| 611 | ret = -1; |
| 612 | goto end; |
| 613 | } |
| 614 | |
| 615 | ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A, |
| 616 | SSL3_ST_SW_CHANGE_B); |
| 617 | if (ret <= 0) { |
| 618 | goto end; |
| 619 | } |
| 620 | s->state = SSL3_ST_SW_FINISHED_A; |
| 621 | s->init_num = 0; |
| 622 | |
| 623 | if (!s->enc_method->change_cipher_state( |
| 624 | s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
| 625 | ret = -1; |
| 626 | goto end; |
| 627 | } |
| 628 | break; |
| 629 | |
| 630 | case SSL3_ST_SW_FINISHED_A: |
| 631 | case SSL3_ST_SW_FINISHED_B: |
| 632 | ret = |
| 633 | ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, |
| 634 | s->enc_method->server_finished_label, |
| 635 | s->enc_method->server_finished_label_len); |
| 636 | if (ret <= 0) { |
| 637 | goto end; |
| 638 | } |
| 639 | s->state = SSL3_ST_SW_FLUSH; |
| 640 | if (s->hit) { |
| 641 | s->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
| 642 | } else { |
| 643 | s->s3->tmp.next_state = SSL_ST_OK; |
| 644 | } |
| 645 | s->init_num = 0; |
| 646 | break; |
| 647 | |
| 648 | case SSL_ST_OK: |
| 649 | /* clean a few things up */ |
| 650 | ssl3_cleanup_key_block(s); |
| 651 | |
| 652 | BUF_MEM_free(s->init_buf); |
| 653 | s->init_buf = NULL; |
| 654 | |
| 655 | /* remove buffering on output */ |
| 656 | ssl_free_wbio_buffer(s); |
| 657 | |
| 658 | s->init_num = 0; |
| 659 | |
| 660 | /* If we aren't retaining peer certificates then we can discard it |
| 661 | * now. */ |
| 662 | if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs) { |
| 663 | X509_free(s->session->peer); |
| 664 | s->session->peer = NULL; |
| 665 | } |
| 666 | |
| 667 | if (s->renegotiate == 2) { |
| 668 | /* skipped if we just sent a HelloRequest */ |
| 669 | s->renegotiate = 0; |
| 670 | s->new_session = 0; |
| 671 | |
| 672 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
| 673 | |
| 674 | s->ctx->stats.sess_accept_good++; |
| 675 | |
| 676 | if (cb != NULL) { |
| 677 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | ret = 1; |
| 682 | goto end; |
| 683 | |
| 684 | default: |
| 685 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE); |
| 686 | ret = -1; |
| 687 | goto end; |
| 688 | } |
| 689 | |
| 690 | if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) { |
| 691 | new_state = s->state; |
| 692 | s->state = state; |
| 693 | cb(s, SSL_CB_ACCEPT_LOOP, 1); |
| 694 | s->state = new_state; |
| 695 | } |
| 696 | skip = 0; |
| 697 | } |
| 698 | |
| 699 | end: |
| 700 | s->in_handshake--; |
| 701 | if (buf != NULL) { |
| 702 | BUF_MEM_free(buf); |
| 703 | } |
| 704 | if (cb != NULL) { |
| 705 | cb(s, SSL_CB_ACCEPT_EXIT, ret); |
| 706 | } |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | static int ssl3_read_sniff_buffer(SSL *s, size_t n) { |
| 711 | if (s->s3->sniff_buffer == NULL) { |
| 712 | s->s3->sniff_buffer = BUF_MEM_new(); |
| 713 | } |
| 714 | if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) { |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | while (s->s3->sniff_buffer_len < n) { |
| 719 | int ret; |
| 720 | |
| 721 | s->rwstate = SSL_READING; |
| 722 | ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len, |
| 723 | n - s->s3->sniff_buffer_len); |
| 724 | if (ret <= 0) { |
| 725 | return ret; |
| 726 | } |
| 727 | s->rwstate = SSL_NOTHING; |
| 728 | s->s3->sniff_buffer_len += ret; |
| 729 | } |
| 730 | |
| 731 | return 1; |
| 732 | } |
| 733 | |
| 734 | int ssl3_get_initial_bytes(SSL *s) { |
| 735 | int ret; |
| 736 | const uint8_t *p; |
| 737 | |
| 738 | /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only |
| 739 | * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */ |
| 740 | ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE); |
| 741 | if (ret <= 0) { |
| 742 | return ret; |
| 743 | } |
| 744 | assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE); |
| 745 | p = (const uint8_t *)s->s3->sniff_buffer->data; |
| 746 | |
| 747 | /* Some dedicated error codes for protocol mixups should the application wish |
| 748 | * to interpret them differently. (These do not overlap with ClientHello or |
| 749 | * V2ClientHello.) */ |
| 750 | if (strncmp("GET ", (const char *)p, 4) == 0 || |
| 751 | strncmp("POST ", (const char *)p, 5) == 0 || |
| 752 | strncmp("HEAD ", (const char *)p, 5) == 0 || |
| 753 | strncmp("PUT ", (const char *)p, 4) == 0) { |
| 754 | OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST); |
| 755 | return -1; |
| 756 | } |
| 757 | if (strncmp("CONNECT ", (const char *)p, 8) == 0) { |
| 758 | OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST); |
| 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | /* Determine if this is a ClientHello or V2ClientHello. */ |
| 763 | if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO && |
| 764 | p[3] >= SSL3_VERSION_MAJOR) { |
| 765 | /* This is a V2ClientHello. */ |
| 766 | s->state = SSL3_ST_SR_V2_CLIENT_HELLO; |
| 767 | return 1; |
| 768 | } |
| 769 | if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR && |
| 770 | p[5] == SSL3_MT_CLIENT_HELLO) { |
| 771 | /* This is a ClientHello. Initialize the record layer with the already |
| 772 | * consumed data and continue the handshake. */ |
| 773 | if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) { |
| 774 | return -1; |
| 775 | } |
| 776 | assert(s->rstate == SSL_ST_READ_HEADER); |
| 777 | memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len); |
| 778 | s->s3->rbuf.offset = 0; |
| 779 | s->s3->rbuf.left = s->s3->sniff_buffer_len; |
| 780 | s->packet_length = 0; |
| 781 | |
| 782 | BUF_MEM_free(s->s3->sniff_buffer); |
| 783 | s->s3->sniff_buffer = NULL; |
| 784 | s->s3->sniff_buffer_len = 0; |
| 785 | |
| 786 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 787 | return 1; |
| 788 | } |
| 789 | |
| 790 | OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL); |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | int ssl3_get_v2_client_hello(SSL *s) { |
| 795 | const uint8_t *p; |
| 796 | int ret; |
| 797 | CBS v2_client_hello, cipher_specs, session_id, challenge; |
| 798 | size_t msg_length, rand_len, len; |
| 799 | uint8_t msg_type; |
| 800 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 801 | CBB client_hello, hello_body, cipher_suites; |
| 802 | uint8_t random[SSL3_RANDOM_SIZE]; |
| 803 | |
| 804 | /* Read the remainder of the V2ClientHello. We have previously read 8 bytes |
| 805 | * in ssl3_get_initial_bytes. */ |
| 806 | assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE); |
| 807 | p = (const uint8_t *)s->s3->sniff_buffer->data; |
| 808 | msg_length = ((p[0] & 0x7f) << 8) | p[1]; |
| 809 | if (msg_length > (1024 * 4)) { |
| 810 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE); |
| 811 | return -1; |
| 812 | } |
| 813 | if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) { |
| 814 | /* Reject lengths that are too short early. We have already read 8 bytes, |
| 815 | * so we should not attempt to process an (invalid) V2ClientHello which |
| 816 | * would be shorter than that. */ |
| 817 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, |
| 818 | SSL_R_RECORD_LENGTH_MISMATCH); |
| 819 | return -1; |
| 820 | } |
| 821 | |
| 822 | ret = ssl3_read_sniff_buffer(s, msg_length + 2); |
| 823 | if (ret <= 0) { |
| 824 | return ret; |
| 825 | } |
| 826 | assert(s->s3->sniff_buffer_len == msg_length + 2); |
| 827 | CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2, |
| 828 | msg_length); |
| 829 | |
| 830 | /* The V2ClientHello without the length is incorporated into the Finished |
| 831 | * hash. */ |
| 832 | ssl3_finish_mac(s, CBS_data(&v2_client_hello), CBS_len(&v2_client_hello)); |
| 833 | if (s->msg_callback) { |
| 834 | s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello), |
| 835 | CBS_len(&v2_client_hello), s, s->msg_callback_arg); |
| 836 | } |
| 837 | |
| 838 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 839 | !CBS_get_u16(&v2_client_hello, &version) || |
| 840 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 841 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 842 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 843 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 844 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 845 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 846 | CBS_len(&v2_client_hello) != 0) { |
| 847 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR); |
| 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | /* msg_type has already been checked. */ |
| 852 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
| 853 | |
| 854 | /* The client_random is the V2ClientHello challenge. Truncate or |
| 855 | * left-pad with zeros as needed. */ |
| 856 | memset(random, 0, SSL3_RANDOM_SIZE); |
| 857 | rand_len = CBS_len(&challenge); |
| 858 | if (rand_len > SSL3_RANDOM_SIZE) { |
| 859 | rand_len = SSL3_RANDOM_SIZE; |
| 860 | } |
| 861 | memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 862 | rand_len); |
| 863 | |
| 864 | /* Write out an equivalent SSLv3 ClientHello. */ |
| 865 | if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, |
| 866 | s->init_buf->max)) { |
| 867 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE); |
| 868 | return -1; |
| 869 | } |
| 870 | if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || |
| 871 | !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || |
| 872 | !CBB_add_u16(&hello_body, version) || |
| 873 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 874 | /* No session id. */ |
| 875 | !CBB_add_u8(&hello_body, 0) || |
| 876 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
| 877 | CBB_cleanup(&client_hello); |
| 878 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 879 | return -1; |
| 880 | } |
| 881 | |
| 882 | /* Copy the cipher suites. */ |
| 883 | while (CBS_len(&cipher_specs) > 0) { |
| 884 | uint32_t cipher_spec; |
| 885 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
| 886 | CBB_cleanup(&client_hello); |
| 887 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR); |
| 888 | return -1; |
| 889 | } |
| 890 | |
| 891 | /* Skip SSLv2 ciphers. */ |
| 892 | if ((cipher_spec & 0xff0000) != 0) { |
| 893 | continue; |
| 894 | } |
| 895 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
| 896 | CBB_cleanup(&client_hello); |
| 897 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 898 | return -1; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | /* Add the null compression scheme and finish. */ |
| 903 | if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) || |
| 904 | !CBB_finish(&client_hello, NULL, &len)) { |
| 905 | CBB_cleanup(&client_hello); |
| 906 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 907 | return -1; |
| 908 | } |
| 909 | |
| 910 | /* Mark the message for "re"-use by the version-specific method. */ |
| 911 | s->s3->tmp.reuse_message = 1; |
| 912 | s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO; |
| 913 | /* The handshake message header is 4 bytes. */ |
| 914 | s->s3->tmp.message_size = len - 4; |
| 915 | |
| 916 | /* Initialize the record layer. */ |
| 917 | if (!ssl3_setup_buffers(s) || !ssl_init_wbio_buffer(s, 1)) { |
| 918 | return -1; |
| 919 | } |
| 920 | |
| 921 | /* Drop the sniff buffer. */ |
| 922 | BUF_MEM_free(s->s3->sniff_buffer); |
| 923 | s->s3->sniff_buffer = NULL; |
| 924 | s->s3->sniff_buffer_len = 0; |
| 925 | |
| 926 | return 1; |
| 927 | } |
| 928 | |
| 929 | int ssl3_send_hello_request(SSL *s) { |
| 930 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) { |
| 931 | ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0); |
| 932 | s->state = SSL3_ST_SW_HELLO_REQ_B; |
| 933 | } |
| 934 | |
| 935 | /* SSL3_ST_SW_HELLO_REQ_B */ |
| 936 | return ssl_do_write(s); |
| 937 | } |
| 938 | |
| 939 | int ssl3_get_client_hello(SSL *s) { |
| 940 | int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1; |
| 941 | long n; |
| 942 | const SSL_CIPHER *c; |
| 943 | STACK_OF(SSL_CIPHER) *ciphers = NULL; |
| 944 | struct ssl_early_callback_ctx early_ctx; |
| 945 | CBS client_hello; |
| 946 | uint16_t client_version; |
| 947 | CBS client_random, session_id, cipher_suites, compression_methods; |
| 948 | |
| 949 | /* We do this so that we will respond with our native type. If we are TLSv1 |
| 950 | * and we get SSLv3, we will respond with TLSv1, This down switching should |
| 951 | * be handled by a different method. If we are SSLv3, we will respond with |
| 952 | * SSLv3, even if prompted with TLSv1. */ |
| 953 | switch (s->state) { |
| 954 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 955 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 956 | n = s->method->ssl_get_message( |
| 957 | s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B, |
| 958 | SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH, |
| 959 | SSL_GET_MESSAGE_HASH_MESSAGE, &ok); |
| 960 | |
| 961 | if (!ok) { |
| 962 | return n; |
| 963 | } |
| 964 | |
| 965 | /* If we require cookies and this ClientHello doesn't contain one, just |
| 966 | * return since we do not want to allocate any memory yet. So check |
| 967 | * cookie length... */ |
| 968 | if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) { |
| 969 | uint8_t cookie_length; |
| 970 | |
| 971 | CBS_init(&client_hello, s->init_msg, n); |
| 972 | if (!CBS_skip(&client_hello, 2 + SSL3_RANDOM_SIZE) || |
| 973 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 974 | !CBS_get_u8(&client_hello, &cookie_length)) { |
| 975 | al = SSL_AD_DECODE_ERROR; |
| 976 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 977 | goto f_err; |
| 978 | } |
| 979 | |
| 980 | if (cookie_length == 0) { |
| 981 | return 1; |
| 982 | } |
| 983 | } |
| 984 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 985 | /* fallthrough */ |
| 986 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 987 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 988 | /* We have previously parsed the ClientHello message, and can't call |
| 989 | * ssl_get_message again without hashing the message into the Finished |
| 990 | * digest again. */ |
| 991 | n = s->init_num; |
| 992 | |
| 993 | memset(&early_ctx, 0, sizeof(early_ctx)); |
| 994 | early_ctx.ssl = s; |
| 995 | early_ctx.client_hello = s->init_msg; |
| 996 | early_ctx.client_hello_len = n; |
| 997 | if (!ssl_early_callback_init(&early_ctx)) { |
| 998 | al = SSL_AD_DECODE_ERROR; |
| 999 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 1000 | SSL_R_CLIENTHELLO_PARSE_FAILED); |
| 1001 | goto f_err; |
| 1002 | } |
| 1003 | |
| 1004 | if (s->state == SSL3_ST_SR_CLNT_HELLO_C && |
| 1005 | s->ctx->select_certificate_cb != NULL) { |
| 1006 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 1007 | switch (s->ctx->select_certificate_cb(&early_ctx)) { |
| 1008 | case 0: |
| 1009 | return CERTIFICATE_SELECTION_PENDING; |
| 1010 | |
| 1011 | case -1: |
| 1012 | /* Connection rejected. */ |
| 1013 | al = SSL_AD_ACCESS_DENIED; |
| 1014 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 1015 | SSL_R_CONNECTION_REJECTED); |
| 1016 | goto f_err; |
| 1017 | |
| 1018 | default: |
| 1019 | /* fallthrough */; |
| 1020 | } |
| 1021 | } |
| 1022 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 1023 | break; |
| 1024 | |
| 1025 | default: |
| 1026 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE); |
| 1027 | return -1; |
| 1028 | } |
| 1029 | |
| 1030 | CBS_init(&client_hello, s->init_msg, n); |
| 1031 | if (!CBS_get_u16(&client_hello, &client_version) || |
| 1032 | !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) || |
| 1033 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 1034 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 1035 | al = SSL_AD_DECODE_ERROR; |
| 1036 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 1037 | goto f_err; |
| 1038 | } |
| 1039 | |
| 1040 | /* use version from inside client hello, not from record header (may differ: |
| 1041 | * see RFC 2246, Appendix E, second paragraph) */ |
| 1042 | s->client_version = client_version; |
| 1043 | |
| 1044 | /* Load the client random. */ |
| 1045 | memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE); |
| 1046 | |
| 1047 | if (SSL_IS_DTLS(s)) { |
| 1048 | CBS cookie; |
| 1049 | |
| 1050 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 1051 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
| 1052 | al = SSL_AD_DECODE_ERROR; |
| 1053 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 1054 | goto f_err; |
| 1055 | } |
| 1056 | |
| 1057 | /* Verify the cookie if appropriate option is set. */ |
| 1058 | if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && CBS_len(&cookie) > 0) { |
| 1059 | if (s->ctx->app_verify_cookie_cb != NULL) { |
| 1060 | if (s->ctx->app_verify_cookie_cb(s, CBS_data(&cookie), |
| 1061 | CBS_len(&cookie)) == 0) { |
| 1062 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1063 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 1064 | goto f_err; |
| 1065 | } |
| 1066 | /* else cookie verification succeeded */ |
| 1067 | } else if (!CBS_mem_equal(&cookie, s->d1->cookie, s->d1->cookie_len)) { |
| 1068 | /* default verification */ |
| 1069 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1070 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH); |
| 1071 | goto f_err; |
| 1072 | } |
| 1073 | /* Set to -2 so if successful we return 2 and don't send |
| 1074 | * HelloVerifyRequest. */ |
| 1075 | ret = -2; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | if (!s->s3->have_version) { |
| 1080 | /* Select version to use */ |
| 1081 | uint16_t version = ssl3_get_mutual_version(s, client_version); |
| 1082 | if (version == 0) { |
| 1083 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL); |
| 1084 | s->version = s->client_version; |
| 1085 | al = SSL_AD_PROTOCOL_VERSION; |
| 1086 | goto f_err; |
| 1087 | } |
| 1088 | s->version = version; |
| 1089 | s->enc_method = ssl3_get_enc_method(version); |
| 1090 | assert(s->enc_method != NULL); |
| 1091 | /* At this point, the connection's version is known and |s->version| is |
| 1092 | * fixed. Begin enforcing the record-layer version. */ |
| 1093 | s->s3->have_version = 1; |
| 1094 | } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version) |
| 1095 | : (s->client_version < s->version)) { |
| 1096 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 1097 | al = SSL_AD_PROTOCOL_VERSION; |
| 1098 | goto f_err; |
| 1099 | } |
| 1100 | |
| 1101 | s->hit = 0; |
| 1102 | /* Versions before 0.9.7 always allow clients to resume sessions in |
| 1103 | * renegotiation. 0.9.7 and later allow this by default, but optionally |
| 1104 | * ignore resumption requests with flag |
| 1105 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather than |
| 1106 | * a change to default behavior so that applications relying on this for |
| 1107 | * security won't even compile against older library versions). |
| 1108 | * |
| 1109 | * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to |
| 1110 | * request renegotiation but not a new session (s->new_session remains |
| 1111 | * unset): for servers, this essentially just means that the |
| 1112 | * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored. */ |
| 1113 | if (s->new_session && |
| 1114 | (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)) { |
| 1115 | if (!ssl_get_new_session(s, 1)) { |
| 1116 | goto err; |
| 1117 | } |
| 1118 | } else { |
| 1119 | i = ssl_get_prev_session(s, &early_ctx); |
| 1120 | if (i == PENDING_SESSION) { |
| 1121 | ret = PENDING_SESSION; |
| 1122 | goto err; |
| 1123 | } else if (i == -1) { |
| 1124 | goto err; |
| 1125 | } |
| 1126 | |
| 1127 | /* Only resume if the session's version matches the negotiated version: |
| 1128 | * most clients do not accept a mismatch. */ |
| 1129 | if (i == 1 && s->version == s->session->ssl_version) { |
| 1130 | s->hit = 1; |
| 1131 | } else { |
| 1132 | /* No session was found or it was unacceptable. */ |
| 1133 | if (!ssl_get_new_session(s, 1)) { |
| 1134 | goto err; |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 1140 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 1141 | CBS_len(&compression_methods) == 0) { |
| 1142 | al = SSL_AD_DECODE_ERROR; |
| 1143 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 1144 | goto f_err; |
| 1145 | } |
| 1146 | |
| 1147 | /* TODO(davidben): Per spec, cipher_suites can never be empty (specified at |
| 1148 | * the ClientHello structure level). This logic allows it to be empty if |
| 1149 | * resuming a session. Can we always require non-empty? If a client sends |
| 1150 | * empty cipher_suites because it's resuming a session, it could always fail |
| 1151 | * to resume a session, so it's unlikely to actually work. */ |
| 1152 | if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) { |
| 1153 | /* We need a cipher if we are not resuming a session. */ |
| 1154 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1155 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED); |
| 1156 | goto f_err; |
| 1157 | } |
| 1158 | |
| 1159 | ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites); |
| 1160 | if (ciphers == NULL) { |
| 1161 | goto err; |
| 1162 | } |
| 1163 | |
| 1164 | /* If it is a hit, check that the cipher is in the list. */ |
| 1165 | if (s->hit && CBS_len(&cipher_suites) > 0) { |
| 1166 | size_t j; |
| 1167 | int found_cipher = 0; |
| 1168 | unsigned long id = s->session->cipher->id; |
| 1169 | |
| 1170 | for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) { |
| 1171 | c = sk_SSL_CIPHER_value(ciphers, j); |
| 1172 | if (c->id == id) { |
| 1173 | found_cipher = 1; |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | if (!found_cipher) { |
| 1179 | /* we need to have the cipher in the cipher list if we are asked to reuse |
| 1180 | * it */ |
| 1181 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1182 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 1183 | SSL_R_REQUIRED_CIPHER_MISSING); |
| 1184 | goto f_err; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* Only null compression is supported. */ |
| 1189 | if (memchr(CBS_data(&compression_methods), 0, |
| 1190 | CBS_len(&compression_methods)) == NULL) { |
| 1191 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1192 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 1193 | SSL_R_NO_COMPRESSION_SPECIFIED); |
| 1194 | goto f_err; |
| 1195 | } |
| 1196 | |
| 1197 | /* TLS extensions. */ |
| 1198 | if (s->version >= SSL3_VERSION && |
| 1199 | !ssl_parse_clienthello_tlsext(s, &client_hello)) { |
| 1200 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT); |
| 1201 | goto err; |
| 1202 | } |
| 1203 | |
| 1204 | /* There should be nothing left over in the record. */ |
| 1205 | if (CBS_len(&client_hello) != 0) { |
| 1206 | /* wrong packet length */ |
| 1207 | al = SSL_AD_DECODE_ERROR; |
| 1208 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH); |
| 1209 | goto f_err; |
| 1210 | } |
| 1211 | |
| 1212 | /* Given ciphers and SSL_get_ciphers, we must pick a cipher */ |
| 1213 | if (!s->hit) { |
| 1214 | if (ciphers == NULL) { |
| 1215 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1216 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED); |
| 1217 | goto f_err; |
| 1218 | } |
| 1219 | |
| 1220 | /* Let cert callback update server certificates if required */ |
| 1221 | if (s->cert->cert_cb) { |
| 1222 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1223 | if (rv == 0) { |
| 1224 | al = SSL_AD_INTERNAL_ERROR; |
| 1225 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR); |
| 1226 | goto f_err; |
| 1227 | } |
| 1228 | if (rv < 0) { |
| 1229 | s->rwstate = SSL_X509_LOOKUP; |
| 1230 | goto err; |
| 1231 | } |
| 1232 | s->rwstate = SSL_NOTHING; |
| 1233 | } |
| 1234 | c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s)); |
| 1235 | |
| 1236 | if (c == NULL) { |
| 1237 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1238 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1239 | goto f_err; |
| 1240 | } |
| 1241 | s->s3->tmp.new_cipher = c; |
| 1242 | } else { |
| 1243 | /* Session-id reuse */ |
| 1244 | s->s3->tmp.new_cipher = s->session->cipher; |
| 1245 | } |
| 1246 | |
| 1247 | if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) && |
| 1248 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 1249 | goto f_err; |
| 1250 | } |
| 1251 | |
| 1252 | /* we now have the following setup; |
| 1253 | * client_random |
| 1254 | * cipher_list - our prefered list of ciphers |
| 1255 | * ciphers - the clients prefered list of ciphers |
| 1256 | * compression - basically ignored right now |
| 1257 | * ssl version is set - sslv3 |
| 1258 | * s->session - The ssl session has been setup. |
| 1259 | * s->hit - session reuse flag |
| 1260 | * s->tmp.new_cipher - the new cipher to use. */ |
| 1261 | |
| 1262 | if (ret < 0) { |
| 1263 | ret = -ret; |
| 1264 | } |
| 1265 | |
| 1266 | if (0) { |
| 1267 | f_err: |
| 1268 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1269 | } |
| 1270 | |
| 1271 | err: |
| 1272 | if (ciphers != NULL) { |
| 1273 | sk_SSL_CIPHER_free(ciphers); |
| 1274 | } |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | int ssl3_send_server_hello(SSL *s) { |
| 1279 | uint8_t *buf; |
| 1280 | uint8_t *p, *d; |
| 1281 | int sl; |
| 1282 | unsigned long l; |
| 1283 | |
| 1284 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { |
| 1285 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 1286 | * known attack while we fix ChannelID itself. */ |
| 1287 | if (s->s3->tlsext_channel_id_valid && |
| 1288 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0) { |
| 1289 | s->s3->tlsext_channel_id_valid = 0; |
| 1290 | } |
| 1291 | |
| 1292 | /* If this is a resumption and the original handshake didn't support |
| 1293 | * ChannelID then we didn't record the original handshake hashes in the |
| 1294 | * session and so cannot resume with ChannelIDs. */ |
| 1295 | if (s->hit && s->s3->tlsext_channel_id_new && |
| 1296 | s->session->original_handshake_hash_len == 0) { |
| 1297 | s->s3->tlsext_channel_id_valid = 0; |
| 1298 | } |
| 1299 | |
| 1300 | buf = (uint8_t *)s->init_buf->data; |
| 1301 | /* Do the message type and length last */ |
| 1302 | d = p = ssl_handshake_start(s); |
| 1303 | |
| 1304 | *(p++) = s->version >> 8; |
| 1305 | *(p++) = s->version & 0xff; |
| 1306 | |
| 1307 | /* Random stuff */ |
| 1308 | if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
| 1309 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1310 | return -1; |
| 1311 | } |
| 1312 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); |
| 1313 | p += SSL3_RANDOM_SIZE; |
| 1314 | |
| 1315 | /* There are several cases for the session ID to send |
| 1316 | * back in the server hello: |
| 1317 | * - For session reuse from the session cache, we send back the old session |
| 1318 | * ID. |
| 1319 | * - If stateless session reuse (using a session ticket) is successful, we |
| 1320 | * send back the client's "session ID" (which doesn't actually identify |
| 1321 | * the session). |
| 1322 | * - If it is a new session, we send back the new session ID. |
| 1323 | * - However, if we want the new session to be single-use, we send back a |
| 1324 | * 0-length session ID. |
| 1325 | * s->hit is non-zero in either case of session reuse, so the following |
| 1326 | * won't overwrite an ID that we're supposed to send back. */ |
| 1327 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) { |
| 1328 | s->session->session_id_length = 0; |
| 1329 | } |
| 1330 | |
| 1331 | sl = s->session->session_id_length; |
| 1332 | if (sl > (int)sizeof(s->session->session_id)) { |
| 1333 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1334 | return -1; |
| 1335 | } |
| 1336 | *(p++) = sl; |
| 1337 | memcpy(p, s->session->session_id, sl); |
| 1338 | p += sl; |
| 1339 | |
| 1340 | /* put the cipher */ |
| 1341 | s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p); |
| 1342 | |
| 1343 | /* put the compression method */ |
| 1344 | *(p++) = 0; |
| 1345 | if (ssl_prepare_serverhello_tlsext(s) <= 0) { |
| 1346 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT); |
| 1347 | return -1; |
| 1348 | } |
| 1349 | p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH); |
| 1350 | if (p == NULL) { |
| 1351 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1352 | return -1; |
| 1353 | } |
| 1354 | |
| 1355 | /* do the header */ |
| 1356 | l = (p - d); |
| 1357 | ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l); |
| 1358 | s->state = SSL3_ST_SW_SRVR_HELLO_B; |
| 1359 | } |
| 1360 | |
| 1361 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1362 | return ssl_do_write(s); |
| 1363 | } |
| 1364 | |
| 1365 | int ssl3_send_server_done(SSL *s) { |
| 1366 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { |
| 1367 | ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0); |
| 1368 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1369 | } |
| 1370 | |
| 1371 | /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1372 | return ssl_do_write(s); |
| 1373 | } |
| 1374 | |
| 1375 | int ssl3_send_server_key_exchange(SSL *s) { |
| 1376 | DH *dh = NULL, *dhp; |
| 1377 | EC_KEY *ecdh = NULL, *ecdhp; |
| 1378 | uint8_t *encodedPoint = NULL; |
| 1379 | int encodedlen = 0; |
| 1380 | uint16_t curve_id = 0; |
| 1381 | BN_CTX *bn_ctx = NULL; |
| 1382 | const char *psk_identity_hint = NULL; |
| 1383 | size_t psk_identity_hint_len = 0; |
| 1384 | EVP_PKEY *pkey; |
| 1385 | uint8_t *p, *d; |
| 1386 | int al, i; |
| 1387 | unsigned long alg_k; |
| 1388 | unsigned long alg_a; |
| 1389 | int n; |
| 1390 | CERT *cert; |
| 1391 | BIGNUM *r[4]; |
| 1392 | int nr[4], kn; |
| 1393 | BUF_MEM *buf; |
| 1394 | EVP_MD_CTX md_ctx; |
| 1395 | |
| 1396 | EVP_MD_CTX_init(&md_ctx); |
| 1397 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { |
| 1398 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1399 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1400 | cert = s->cert; |
| 1401 | |
| 1402 | buf = s->init_buf; |
| 1403 | |
| 1404 | r[0] = r[1] = r[2] = r[3] = NULL; |
| 1405 | n = 0; |
| 1406 | if (alg_a & SSL_aPSK) { |
| 1407 | /* size for PSK identity hint */ |
| 1408 | psk_identity_hint = s->psk_identity_hint; |
| 1409 | if (psk_identity_hint) { |
| 1410 | psk_identity_hint_len = strlen(psk_identity_hint); |
| 1411 | } else { |
| 1412 | psk_identity_hint_len = 0; |
| 1413 | } |
| 1414 | n += 2 + psk_identity_hint_len; |
| 1415 | } |
| 1416 | |
| 1417 | if (alg_k & SSL_kEDH) { |
| 1418 | dhp = cert->dh_tmp; |
| 1419 | if (dhp == NULL && s->cert->dh_tmp_cb != NULL) { |
| 1420 | dhp = s->cert->dh_tmp_cb(s, 0, 1024); |
| 1421 | } |
| 1422 | if (dhp == NULL) { |
| 1423 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1424 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1425 | SSL_R_MISSING_TMP_DH_KEY); |
| 1426 | goto f_err; |
| 1427 | } |
| 1428 | |
| 1429 | if (s->s3->tmp.dh != NULL) { |
| 1430 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1431 | ERR_R_INTERNAL_ERROR); |
| 1432 | goto err; |
| 1433 | } |
| 1434 | |
| 1435 | dh = DHparams_dup(dhp); |
| 1436 | if (dh == NULL) { |
| 1437 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1438 | goto err; |
| 1439 | } |
| 1440 | |
| 1441 | s->s3->tmp.dh = dh; |
| 1442 | if (dhp->pub_key == NULL || dhp->priv_key == NULL || |
| 1443 | (s->options & SSL_OP_SINGLE_DH_USE)) { |
| 1444 | if (!DH_generate_key(dh)) { |
| 1445 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1446 | goto err; |
| 1447 | } |
| 1448 | } else { |
| 1449 | dh->pub_key = BN_dup(dhp->pub_key); |
| 1450 | dh->priv_key = BN_dup(dhp->priv_key); |
| 1451 | if (dh->pub_key == NULL || dh->priv_key == NULL) { |
| 1452 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1453 | goto err; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | r[0] = dh->p; |
| 1458 | r[1] = dh->g; |
| 1459 | r[2] = dh->pub_key; |
| 1460 | } else if (alg_k & SSL_kEECDH) { |
| 1461 | const EC_GROUP *group; |
| 1462 | |
| 1463 | ecdhp = cert->ecdh_tmp; |
| 1464 | if (s->cert->ecdh_tmp_auto) { |
| 1465 | /* Get NID of appropriate shared curve */ |
| 1466 | int nid = tls1_get_shared_curve(s); |
| 1467 | if (nid != NID_undef) { |
| 1468 | ecdhp = EC_KEY_new_by_curve_name(nid); |
| 1469 | } |
| 1470 | } else if (ecdhp == NULL && s->cert->ecdh_tmp_cb) { |
| 1471 | ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024); |
| 1472 | } |
| 1473 | if (ecdhp == NULL) { |
| 1474 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1475 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1476 | SSL_R_MISSING_TMP_ECDH_KEY); |
| 1477 | goto f_err; |
| 1478 | } |
| 1479 | |
| 1480 | if (s->s3->tmp.ecdh != NULL) { |
| 1481 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1482 | ERR_R_INTERNAL_ERROR); |
| 1483 | goto err; |
| 1484 | } |
| 1485 | |
| 1486 | /* Duplicate the ECDH structure. */ |
| 1487 | if (ecdhp == NULL) { |
| 1488 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1489 | goto err; |
| 1490 | } |
| 1491 | |
| 1492 | if (s->cert->ecdh_tmp_auto) { |
| 1493 | ecdh = ecdhp; |
| 1494 | } else { |
| 1495 | ecdh = EC_KEY_dup(ecdhp); |
| 1496 | if (ecdh == NULL) { |
| 1497 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1498 | goto err; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | s->s3->tmp.ecdh = ecdh; |
| 1503 | if (EC_KEY_get0_public_key(ecdh) == NULL || |
| 1504 | EC_KEY_get0_private_key(ecdh) == NULL || |
| 1505 | (s->options & SSL_OP_SINGLE_ECDH_USE)) { |
| 1506 | if (!EC_KEY_generate_key(ecdh)) { |
| 1507 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1508 | goto err; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | group = EC_KEY_get0_group(ecdh); |
| 1513 | if (group == NULL || |
| 1514 | EC_KEY_get0_public_key(ecdh) == NULL || |
| 1515 | EC_KEY_get0_private_key(ecdh) == NULL) { |
| 1516 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1517 | goto err; |
| 1518 | } |
| 1519 | |
| 1520 | /* We only support ephemeral ECDH keys over named (not generic) curves. */ |
| 1521 | if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) { |
| 1522 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1523 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1524 | goto err; |
| 1525 | } |
| 1526 | |
| 1527 | /* Encode the public key. First check the size of encoding and allocate |
| 1528 | * memory accordingly. */ |
| 1529 | encodedlen = |
| 1530 | EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), |
| 1531 | POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); |
| 1532 | |
| 1533 | encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t)); |
| 1534 | bn_ctx = BN_CTX_new(); |
| 1535 | if (encodedPoint == NULL || bn_ctx == NULL) { |
| 1536 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1537 | ERR_R_MALLOC_FAILURE); |
| 1538 | goto err; |
| 1539 | } |
| 1540 | |
| 1541 | encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), |
| 1542 | POINT_CONVERSION_UNCOMPRESSED, |
| 1543 | encodedPoint, encodedlen, bn_ctx); |
| 1544 | |
| 1545 | if (encodedlen == 0) { |
| 1546 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1547 | goto err; |
| 1548 | } |
| 1549 | |
| 1550 | BN_CTX_free(bn_ctx); |
| 1551 | bn_ctx = NULL; |
| 1552 | |
| 1553 | /* We only support named (not generic) curves in ECDH ephemeral key |
| 1554 | * exchanges. In this situation, we need four additional bytes to encode |
| 1555 | * the entire ServerECDHParams structure. */ |
| 1556 | n += 4 + encodedlen; |
| 1557 | |
| 1558 | /* We'll generate the serverKeyExchange message explicitly so we can set |
| 1559 | * these to NULLs */ |
| 1560 | r[0] = NULL; |
| 1561 | r[1] = NULL; |
| 1562 | r[2] = NULL; |
| 1563 | r[3] = NULL; |
| 1564 | } else if (!(alg_k & SSL_kPSK)) { |
| 1565 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1566 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1567 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 1568 | goto f_err; |
| 1569 | } |
| 1570 | |
| 1571 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
| 1572 | nr[i] = BN_num_bytes(r[i]); |
| 1573 | n += 2 + nr[i]; |
| 1574 | } |
| 1575 | |
| 1576 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 1577 | pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher); |
| 1578 | if (pkey == NULL) { |
| 1579 | al = SSL_AD_DECODE_ERROR; |
| 1580 | goto f_err; |
| 1581 | } |
| 1582 | kn = EVP_PKEY_size(pkey); |
| 1583 | } else { |
| 1584 | pkey = NULL; |
| 1585 | kn = 0; |
| 1586 | } |
| 1587 | |
| 1588 | if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) { |
| 1589 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF); |
| 1590 | goto err; |
| 1591 | } |
| 1592 | d = p = ssl_handshake_start(s); |
| 1593 | |
| 1594 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
| 1595 | s2n(nr[i], p); |
| 1596 | BN_bn2bin(r[i], p); |
| 1597 | p += nr[i]; |
| 1598 | } |
| 1599 | |
| 1600 | /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK. When one of |
| 1601 | * them is used, the server key exchange record needs to have both the |
| 1602 | * psk_identity_hint and the ServerECDHParams. */ |
| 1603 | if (alg_a & SSL_aPSK) { |
| 1604 | /* copy PSK identity hint (if provided) */ |
| 1605 | s2n(psk_identity_hint_len, p); |
| 1606 | if (psk_identity_hint_len > 0) { |
| 1607 | memcpy(p, psk_identity_hint, psk_identity_hint_len); |
| 1608 | p += psk_identity_hint_len; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (alg_k & SSL_kEECDH) { |
| 1613 | /* We only support named (not generic) curves. In this situation, the |
| 1614 | * serverKeyExchange message has: |
| 1615 | * [1 byte CurveType], [2 byte CurveName] |
| 1616 | * [1 byte length of encoded point], followed by |
| 1617 | * the actual encoded point itself. */ |
| 1618 | *(p++) = NAMED_CURVE_TYPE; |
| 1619 | *(p++) = (uint8_t)(curve_id >> 8); |
| 1620 | *(p++) = (uint8_t)(curve_id & 0xff); |
| 1621 | *(p++) = encodedlen; |
| 1622 | memcpy(p, encodedPoint, encodedlen); |
| 1623 | p += encodedlen; |
| 1624 | OPENSSL_free(encodedPoint); |
| 1625 | encodedPoint = NULL; |
| 1626 | } |
| 1627 | |
| 1628 | /* not anonymous */ |
| 1629 | if (pkey != NULL) { |
| 1630 | /* n is the length of the params, they start at &(d[4]) and p points to |
| 1631 | * the space at the end. */ |
| 1632 | const EVP_MD *md; |
| 1633 | size_t sig_len = EVP_PKEY_size(pkey); |
| 1634 | |
| 1635 | /* Determine signature algorithm. */ |
| 1636 | if (SSL_USE_SIGALGS(s)) { |
| 1637 | md = tls1_choose_signing_digest(s, pkey); |
| 1638 | if (!tls12_get_sigandhash(p, pkey, md)) { |
| 1639 | /* Should never happen */ |
| 1640 | al = SSL_AD_INTERNAL_ERROR; |
| 1641 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1642 | ERR_R_INTERNAL_ERROR); |
| 1643 | goto f_err; |
| 1644 | } |
| 1645 | p += 2; |
| 1646 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1647 | md = EVP_md5_sha1(); |
| 1648 | } else { |
| 1649 | md = EVP_sha1(); |
| 1650 | } |
| 1651 | |
| 1652 | if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) || |
| 1653 | !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random, |
| 1654 | SSL3_RANDOM_SIZE) || |
| 1655 | !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random, |
| 1656 | SSL3_RANDOM_SIZE) || |
| 1657 | !EVP_DigestSignUpdate(&md_ctx, d, n) || |
| 1658 | !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) { |
| 1659 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP); |
| 1660 | goto err; |
| 1661 | } |
| 1662 | |
| 1663 | s2n(sig_len, p); |
| 1664 | n += sig_len + 2; |
| 1665 | if (SSL_USE_SIGALGS(s)) { |
| 1666 | n += 2; |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n); |
| 1671 | } |
| 1672 | |
| 1673 | s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1674 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1675 | return ssl_do_write(s); |
| 1676 | |
| 1677 | f_err: |
| 1678 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1679 | err: |
| 1680 | if (encodedPoint != NULL) { |
| 1681 | OPENSSL_free(encodedPoint); |
| 1682 | } |
| 1683 | BN_CTX_free(bn_ctx); |
| 1684 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1685 | return -1; |
| 1686 | } |
| 1687 | |
| 1688 | int ssl3_send_certificate_request(SSL *s) { |
| 1689 | uint8_t *p, *d; |
| 1690 | size_t i; |
| 1691 | int j, nl, off, n; |
| 1692 | STACK_OF(X509_NAME) *sk = NULL; |
| 1693 | X509_NAME *name; |
| 1694 | BUF_MEM *buf; |
| 1695 | |
| 1696 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { |
| 1697 | buf = s->init_buf; |
| 1698 | |
| 1699 | d = p = ssl_handshake_start(s); |
| 1700 | |
| 1701 | /* get the list of acceptable cert types */ |
| 1702 | p++; |
| 1703 | n = ssl3_get_req_cert_type(s, p); |
| 1704 | d[0] = n; |
| 1705 | p += n; |
| 1706 | n++; |
| 1707 | |
| 1708 | if (SSL_USE_SIGALGS(s)) { |
| 1709 | const uint8_t *psigs; |
| 1710 | nl = tls12_get_psigalgs(s, &psigs); |
| 1711 | s2n(nl, p); |
| 1712 | memcpy(p, psigs, nl); |
| 1713 | p += nl; |
| 1714 | n += nl + 2; |
| 1715 | } |
| 1716 | |
| 1717 | off = n; |
| 1718 | p += 2; |
| 1719 | n += 2; |
| 1720 | |
| 1721 | sk = SSL_get_client_CA_list(s); |
| 1722 | nl = 0; |
| 1723 | if (sk != NULL) { |
| 1724 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 1725 | name = sk_X509_NAME_value(sk, i); |
| 1726 | j = i2d_X509_NAME(name, NULL); |
| 1727 | if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) { |
| 1728 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1729 | goto err; |
| 1730 | } |
| 1731 | p = ssl_handshake_start(s) + n; |
| 1732 | s2n(j, p); |
| 1733 | i2d_X509_NAME(name, &p); |
| 1734 | n += 2 + j; |
| 1735 | nl += 2 + j; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | /* else no CA names */ |
| 1740 | p = ssl_handshake_start(s) + off; |
| 1741 | s2n(nl, p); |
| 1742 | |
| 1743 | ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n); |
| 1744 | |
| 1745 | #ifdef NETSCAPE_HANG_BUG |
| 1746 | if (!SSL_IS_DTLS(s)) { |
| 1747 | /* Prepare a ServerHelloDone in the same record. This is to workaround a |
| 1748 | * hang in Netscape. */ |
| 1749 | if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) { |
| 1750 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1751 | goto err; |
| 1752 | } |
| 1753 | p = (uint8_t *)s->init_buf->data + s->init_num; |
| 1754 | /* do the header */ |
| 1755 | *(p++) = SSL3_MT_SERVER_DONE; |
| 1756 | *(p++) = 0; |
| 1757 | *(p++) = 0; |
| 1758 | *(p++) = 0; |
| 1759 | s->init_num += 4; |
| 1760 | ssl3_finish_mac(s, p - 4, 4); |
| 1761 | } |
| 1762 | #endif |
| 1763 | |
| 1764 | s->state = SSL3_ST_SW_CERT_REQ_B; |
| 1765 | } |
| 1766 | |
| 1767 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 1768 | return ssl_do_write(s); |
| 1769 | |
| 1770 | err: |
| 1771 | return -1; |
| 1772 | } |
| 1773 | |
| 1774 | int ssl3_get_client_key_exchange(SSL *s) { |
| 1775 | int al, ok; |
| 1776 | long n; |
| 1777 | CBS client_key_exchange; |
| 1778 | unsigned long alg_k; |
| 1779 | unsigned long alg_a; |
| 1780 | uint8_t *premaster_secret = NULL; |
| 1781 | size_t premaster_secret_len = 0; |
| 1782 | RSA *rsa = NULL; |
| 1783 | uint8_t *decrypt_buf = NULL; |
| 1784 | EVP_PKEY *pkey = NULL; |
| 1785 | BIGNUM *pub = NULL; |
| 1786 | DH *dh_srvr; |
| 1787 | |
| 1788 | EC_KEY *srvr_ecdh = NULL; |
| 1789 | EVP_PKEY *clnt_pub_pkey = NULL; |
| 1790 | EC_POINT *clnt_ecpoint = NULL; |
| 1791 | BN_CTX *bn_ctx = NULL; |
| 1792 | unsigned int psk_len = 0; |
| 1793 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1794 | |
| 1795 | n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A, |
| 1796 | SSL3_ST_SR_KEY_EXCH_B, |
| 1797 | SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */ |
| 1798 | SSL_GET_MESSAGE_HASH_MESSAGE, &ok); |
| 1799 | |
| 1800 | if (!ok) { |
| 1801 | return n; |
| 1802 | } |
| 1803 | |
| 1804 | CBS_init(&client_key_exchange, s->init_msg, n); |
| 1805 | |
| 1806 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1807 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1808 | |
| 1809 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1810 | if (alg_a & SSL_aPSK) { |
| 1811 | CBS psk_identity; |
| 1812 | |
| 1813 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1814 | * then this is the only field in the message. */ |
| 1815 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1816 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
| 1817 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 1818 | al = SSL_AD_DECODE_ERROR; |
| 1819 | goto f_err; |
| 1820 | } |
| 1821 | |
| 1822 | if (s->psk_server_callback == NULL) { |
| 1823 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1824 | SSL_R_PSK_NO_SERVER_CB); |
| 1825 | al = SSL_AD_INTERNAL_ERROR; |
| 1826 | goto f_err; |
| 1827 | } |
| 1828 | |
| 1829 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1830 | CBS_contains_zero_byte(&psk_identity)) { |
| 1831 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1832 | SSL_R_DATA_LENGTH_TOO_LONG); |
| 1833 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1834 | goto f_err; |
| 1835 | } |
| 1836 | |
| 1837 | if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) { |
| 1838 | al = SSL_AD_INTERNAL_ERROR; |
| 1839 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1840 | ERR_R_MALLOC_FAILURE); |
| 1841 | goto f_err; |
| 1842 | } |
| 1843 | |
| 1844 | /* Look up the key for the identity. */ |
| 1845 | psk_len = |
| 1846 | s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk)); |
| 1847 | if (psk_len > PSK_MAX_PSK_LEN) { |
| 1848 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1849 | ERR_R_INTERNAL_ERROR); |
| 1850 | al = SSL_AD_INTERNAL_ERROR; |
| 1851 | goto f_err; |
| 1852 | } else if (psk_len == 0) { |
| 1853 | /* PSK related to the given identity not found */ |
| 1854 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1855 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1856 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1857 | goto f_err; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1862 | * |premaster_secret_len|. */ |
| 1863 | if (alg_k & SSL_kRSA) { |
| 1864 | CBS encrypted_premaster_secret; |
| 1865 | uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
| 1866 | uint8_t good; |
| 1867 | size_t rsa_size, decrypt_len, premaster_index, j; |
| 1868 | |
| 1869 | pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 1870 | if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) { |
| 1871 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1872 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1873 | SSL_R_MISSING_RSA_CERTIFICATE); |
| 1874 | goto f_err; |
| 1875 | } |
| 1876 | rsa = pkey->pkey.rsa; |
| 1877 | |
| 1878 | /* TLS and [incidentally] DTLS{0xFEFF} */ |
| 1879 | if (s->version > SSL3_VERSION) { |
| 1880 | CBS copy = client_key_exchange; |
| 1881 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1882 | &encrypted_premaster_secret) || |
| 1883 | CBS_len(&client_key_exchange) != 0) { |
| 1884 | if (!(s->options & SSL_OP_TLS_D5_BUG)) { |
| 1885 | al = SSL_AD_DECODE_ERROR; |
| 1886 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1887 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1888 | goto f_err; |
| 1889 | } else { |
| 1890 | encrypted_premaster_secret = copy; |
| 1891 | } |
| 1892 | } |
| 1893 | } else { |
| 1894 | encrypted_premaster_secret = client_key_exchange; |
| 1895 | } |
| 1896 | |
| 1897 | /* Reject overly short RSA keys because we want to be sure that the buffer |
| 1898 | * size makes it safe to iterate over the entire size of a premaster secret |
| 1899 | * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to |
| 1900 | * RSA padding, but the bound is sufficient to be safe. */ |
| 1901 | rsa_size = RSA_size(rsa); |
| 1902 | if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) { |
| 1903 | al = SSL_AD_DECRYPT_ERROR; |
| 1904 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1905 | SSL_R_DECRYPTION_FAILED); |
| 1906 | goto f_err; |
| 1907 | } |
| 1908 | |
| 1909 | /* We must not leak whether a decryption failure occurs because of |
| 1910 | * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, |
| 1911 | * section 7.4.7.1). The code follows that advice of the TLS RFC and |
| 1912 | * generates a random premaster secret for the case that the decrypt fails. |
| 1913 | * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */ |
| 1914 | if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) { |
| 1915 | goto err; |
| 1916 | } |
| 1917 | |
| 1918 | /* Allocate a buffer large enough for an RSA decryption. */ |
| 1919 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1920 | if (decrypt_buf == NULL) { |
| 1921 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1922 | ERR_R_MALLOC_FAILURE); |
| 1923 | goto err; |
| 1924 | } |
| 1925 | |
| 1926 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1927 | * timing-sensitive code below. */ |
| 1928 | if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size, |
| 1929 | CBS_data(&encrypted_premaster_secret), |
| 1930 | CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) { |
| 1931 | goto err; |
| 1932 | } |
| 1933 | if (decrypt_len != rsa_size) { |
| 1934 | /* This should never happen, but do a check so we do not read |
| 1935 | * uninitialized memory. */ |
| 1936 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1937 | ERR_R_INTERNAL_ERROR); |
| 1938 | goto err; |
| 1939 | } |
| 1940 | |
| 1941 | /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate. |
| 1942 | * |good| will be 0xff if the premaster is acceptable and zero otherwise. |
| 1943 | * */ |
| 1944 | good = |
| 1945 | constant_time_eq_int_8(RSA_message_index_PKCS1_type_2( |
| 1946 | decrypt_buf, decrypt_len, &premaster_index), |
| 1947 | 1); |
| 1948 | decrypt_len = decrypt_len - premaster_index; |
| 1949 | |
| 1950 | /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */ |
| 1951 | good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH); |
| 1952 | |
| 1953 | /* Copy over the unpadded premaster. Whatever the value of |
| 1954 | * |decrypt_good_mask|, copy as if the premaster were the right length. It |
| 1955 | * is important the memory access pattern be constant. */ |
| 1956 | premaster_secret = |
| 1957 | BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH), |
| 1958 | SSL_MAX_MASTER_KEY_LENGTH); |
| 1959 | if (premaster_secret == NULL) { |
| 1960 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1961 | ERR_R_MALLOC_FAILURE); |
| 1962 | goto err; |
| 1963 | } |
| 1964 | OPENSSL_free(decrypt_buf); |
| 1965 | decrypt_buf = NULL; |
| 1966 | |
| 1967 | /* If the version in the decrypted pre-master secret is correct then |
| 1968 | * version_good will be 0xff, otherwise it'll be zero. The |
| 1969 | * Klima-Pokorny-Rosa extension of Bleichenbacher's attack |
| 1970 | * (http://eprint.iacr.org/2003/052/) exploits the version number check as |
| 1971 | * a "bad version oracle". Thus version checks are done in constant time |
| 1972 | * and are treated like any other decryption error. */ |
| 1973 | good &= constant_time_eq_8(premaster_secret[0], |
| 1974 | (unsigned)(s->client_version >> 8)); |
| 1975 | good &= constant_time_eq_8(premaster_secret[1], |
| 1976 | (unsigned)(s->client_version & 0xff)); |
| 1977 | |
| 1978 | /* Now copy rand_premaster_secret over premaster_secret using |
| 1979 | * decrypt_good_mask. */ |
| 1980 | for (j = 0; j < sizeof(rand_premaster_secret); j++) { |
| 1981 | premaster_secret[j] = constant_time_select_8(good, premaster_secret[j], |
| 1982 | rand_premaster_secret[j]); |
| 1983 | } |
| 1984 | |
| 1985 | premaster_secret_len = sizeof(rand_premaster_secret); |
| 1986 | } else if (alg_k & SSL_kEDH) { |
| 1987 | CBS dh_Yc; |
| 1988 | int dh_len; |
| 1989 | |
| 1990 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) || |
| 1991 | CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) { |
| 1992 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1993 | SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 1994 | al = SSL_R_DECODE_ERROR; |
| 1995 | goto f_err; |
| 1996 | } |
| 1997 | |
| 1998 | if (s->s3->tmp.dh == NULL) { |
| 1999 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2000 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2001 | SSL_R_MISSING_TMP_DH_KEY); |
| 2002 | goto f_err; |
| 2003 | } |
| 2004 | dh_srvr = s->s3->tmp.dh; |
| 2005 | |
| 2006 | pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL); |
| 2007 | if (pub == NULL) { |
| 2008 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB); |
| 2009 | goto err; |
| 2010 | } |
| 2011 | |
| 2012 | /* Allocate a buffer for the premaster secret. */ |
| 2013 | premaster_secret = OPENSSL_malloc(DH_size(dh_srvr)); |
| 2014 | if (premaster_secret == NULL) { |
| 2015 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2016 | ERR_R_MALLOC_FAILURE); |
| 2017 | goto err; |
| 2018 | } |
| 2019 | |
| 2020 | dh_len = DH_compute_key(premaster_secret, pub, dh_srvr); |
| 2021 | if (dh_len <= 0) { |
| 2022 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB); |
| 2023 | BN_clear_free(pub); |
| 2024 | goto err; |
| 2025 | } |
| 2026 | |
| 2027 | DH_free(s->s3->tmp.dh); |
| 2028 | s->s3->tmp.dh = NULL; |
| 2029 | BN_clear_free(pub); |
| 2030 | pub = NULL; |
| 2031 | |
| 2032 | premaster_secret_len = dh_len; |
| 2033 | } else if (alg_k & SSL_kEECDH) { |
| 2034 | int field_size = 0, ecdh_len; |
| 2035 | const EC_KEY *tkey; |
| 2036 | const EC_GROUP *group; |
| 2037 | const BIGNUM *priv_key; |
| 2038 | CBS ecdh_Yc; |
| 2039 | |
| 2040 | /* initialize structures for server's ECDH key pair */ |
| 2041 | srvr_ecdh = EC_KEY_new(); |
| 2042 | if (srvr_ecdh == NULL) { |
| 2043 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2044 | ERR_R_MALLOC_FAILURE); |
| 2045 | goto err; |
| 2046 | } |
| 2047 | |
| 2048 | /* Use the ephermeral values we saved when generating the ServerKeyExchange |
| 2049 | * msg. */ |
| 2050 | tkey = s->s3->tmp.ecdh; |
| 2051 | |
| 2052 | group = EC_KEY_get0_group(tkey); |
| 2053 | priv_key = EC_KEY_get0_private_key(tkey); |
| 2054 | |
| 2055 | if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 2056 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) { |
| 2057 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2058 | goto err; |
| 2059 | } |
| 2060 | |
| 2061 | /* Let's get client's public key */ |
| 2062 | clnt_ecpoint = EC_POINT_new(group); |
| 2063 | if (clnt_ecpoint == NULL) { |
| 2064 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2065 | ERR_R_MALLOC_FAILURE); |
| 2066 | goto err; |
| 2067 | } |
| 2068 | |
| 2069 | /* Get client's public key from encoded point in the ClientKeyExchange |
| 2070 | * message. */ |
| 2071 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) || |
| 2072 | CBS_len(&client_key_exchange) != 0) { |
| 2073 | al = SSL_AD_DECODE_ERROR; |
| 2074 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 2075 | goto f_err; |
| 2076 | } |
| 2077 | |
| 2078 | bn_ctx = BN_CTX_new(); |
| 2079 | if (bn_ctx == NULL) { |
| 2080 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2081 | ERR_R_MALLOC_FAILURE); |
| 2082 | goto err; |
| 2083 | } |
| 2084 | |
| 2085 | if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc), |
| 2086 | CBS_len(&ecdh_Yc), bn_ctx)) { |
| 2087 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 2088 | goto err; |
| 2089 | } |
| 2090 | |
| 2091 | /* Allocate a buffer for both the secret and the PSK. */ |
| 2092 | field_size = EC_GROUP_get_degree(group); |
| 2093 | if (field_size <= 0) { |
| 2094 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2095 | goto err; |
| 2096 | } |
| 2097 | |
| 2098 | ecdh_len = (field_size + 7) / 8; |
| 2099 | premaster_secret = OPENSSL_malloc(ecdh_len); |
| 2100 | if (premaster_secret == NULL) { |
| 2101 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2102 | ERR_R_MALLOC_FAILURE); |
| 2103 | goto err; |
| 2104 | } |
| 2105 | |
| 2106 | /* Compute the shared pre-master secret */ |
| 2107 | ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint, |
| 2108 | srvr_ecdh, NULL); |
| 2109 | if (ecdh_len <= 0) { |
| 2110 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 2111 | goto err; |
| 2112 | } |
| 2113 | |
| 2114 | EVP_PKEY_free(clnt_pub_pkey); |
| 2115 | clnt_pub_pkey = NULL; |
| 2116 | EC_POINT_free(clnt_ecpoint); |
| 2117 | clnt_ecpoint = NULL; |
| 2118 | EC_KEY_free(srvr_ecdh); |
| 2119 | srvr_ecdh = NULL; |
| 2120 | BN_CTX_free(bn_ctx); |
| 2121 | bn_ctx = NULL; |
| 2122 | EC_KEY_free(s->s3->tmp.ecdh); |
| 2123 | s->s3->tmp.ecdh = NULL; |
| 2124 | |
| 2125 | premaster_secret_len = ecdh_len; |
| 2126 | } else if (alg_k & SSL_kPSK) { |
| 2127 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 2128 | * pre-shared key. */ |
| 2129 | premaster_secret_len = psk_len; |
| 2130 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 2131 | if (premaster_secret == NULL) { |
| 2132 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2133 | ERR_R_MALLOC_FAILURE); |
| 2134 | goto err; |
| 2135 | } |
| 2136 | memset(premaster_secret, 0, premaster_secret_len); |
| 2137 | } else { |
| 2138 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2139 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2140 | SSL_R_UNKNOWN_CIPHER_TYPE); |
| 2141 | goto f_err; |
| 2142 | } |
| 2143 | |
| 2144 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 2145 | * pre-shared key. */ |
| 2146 | if (alg_a & SSL_aPSK) { |
| 2147 | CBB new_premaster, child; |
| 2148 | uint8_t *new_data; |
| 2149 | size_t new_len; |
| 2150 | |
| 2151 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) { |
| 2152 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2153 | ERR_R_MALLOC_FAILURE); |
| 2154 | goto err; |
| 2155 | } |
| 2156 | if (!CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2157 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 2158 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2159 | !CBB_add_bytes(&child, psk, psk_len) || |
| 2160 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
| 2161 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2162 | ERR_R_INTERNAL_ERROR); |
| 2163 | CBB_cleanup(&new_premaster); |
| 2164 | goto err; |
| 2165 | } |
| 2166 | |
| 2167 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2168 | OPENSSL_free(premaster_secret); |
| 2169 | premaster_secret = new_data; |
| 2170 | premaster_secret_len = new_len; |
| 2171 | } |
| 2172 | |
| 2173 | /* Compute the master secret */ |
| 2174 | s->session->master_key_length = s->enc_method->generate_master_secret( |
| 2175 | s, s->session->master_key, premaster_secret, premaster_secret_len); |
| 2176 | if (s->session->master_key_length == 0) { |
| 2177 | goto err; |
| 2178 | } |
| 2179 | s->session->extended_master_secret = s->s3->tmp.extended_master_secret; |
| 2180 | |
| 2181 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2182 | OPENSSL_free(premaster_secret); |
| 2183 | return 1; |
| 2184 | |
| 2185 | f_err: |
| 2186 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2187 | err: |
| 2188 | if (premaster_secret) { |
| 2189 | if (premaster_secret_len) { |
| 2190 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2191 | } |
| 2192 | OPENSSL_free(premaster_secret); |
| 2193 | } |
| 2194 | if (decrypt_buf) { |
| 2195 | OPENSSL_free(decrypt_buf); |
| 2196 | } |
| 2197 | EVP_PKEY_free(clnt_pub_pkey); |
| 2198 | EC_POINT_free(clnt_ecpoint); |
| 2199 | if (srvr_ecdh != NULL) { |
| 2200 | EC_KEY_free(srvr_ecdh); |
| 2201 | } |
| 2202 | BN_CTX_free(bn_ctx); |
| 2203 | |
| 2204 | return -1; |
| 2205 | } |
| 2206 | |
| 2207 | int ssl3_get_cert_verify(SSL *s) { |
| 2208 | int al, ok, ret = 0; |
| 2209 | long n; |
| 2210 | CBS certificate_verify, signature; |
| 2211 | X509 *peer = s->session->peer; |
| 2212 | EVP_PKEY *pkey = NULL; |
| 2213 | const EVP_MD *md = NULL; |
| 2214 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 2215 | size_t digest_length; |
| 2216 | EVP_PKEY_CTX *pctx = NULL; |
| 2217 | |
| 2218 | /* Only RSA and ECDSA client certificates are supported, so a |
| 2219 | * CertificateVerify is required if and only if there's a client certificate. |
| 2220 | * */ |
| 2221 | if (peer == NULL) { |
| 2222 | if (s->s3->handshake_buffer && |
| 2223 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2224 | return -1; |
| 2225 | } |
| 2226 | return 1; |
| 2227 | } |
| 2228 | |
| 2229 | n = s->method->ssl_get_message( |
| 2230 | s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B, |
| 2231 | SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH, |
| 2232 | SSL_GET_MESSAGE_DONT_HASH_MESSAGE, &ok); |
| 2233 | |
| 2234 | if (!ok) { |
| 2235 | return n; |
| 2236 | } |
| 2237 | |
| 2238 | /* Filter out unsupported certificate types. */ |
| 2239 | pkey = X509_get_pubkey(peer); |
| 2240 | if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) || |
| 2241 | (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) { |
| 2242 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2243 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, |
| 2244 | SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 2245 | goto f_err; |
| 2246 | } |
| 2247 | |
| 2248 | CBS_init(&certificate_verify, s->init_msg, n); |
| 2249 | |
| 2250 | /* Determine the digest type if needbe. */ |
| 2251 | if (SSL_USE_SIGALGS(s) && |
| 2252 | !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) { |
| 2253 | goto f_err; |
| 2254 | } |
| 2255 | |
| 2256 | /* Compute the digest. */ |
| 2257 | if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) { |
| 2258 | goto err; |
| 2259 | } |
| 2260 | |
| 2261 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 2262 | * message.*/ |
| 2263 | if (s->s3->handshake_buffer && |
| 2264 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2265 | goto err; |
| 2266 | } |
| 2267 | ssl3_hash_current_message(s); |
| 2268 | |
| 2269 | /* Parse and verify the signature. */ |
| 2270 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 2271 | CBS_len(&certificate_verify) != 0) { |
| 2272 | al = SSL_AD_DECODE_ERROR; |
| 2273 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR); |
| 2274 | goto f_err; |
| 2275 | } |
| 2276 | |
| 2277 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 2278 | if (pctx == NULL) { |
| 2279 | goto err; |
| 2280 | } |
| 2281 | if (!EVP_PKEY_verify_init(pctx) || |
| 2282 | !EVP_PKEY_CTX_set_signature_md(pctx, md) || |
| 2283 | !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest, |
| 2284 | digest_length)) { |
| 2285 | al = SSL_AD_DECRYPT_ERROR; |
| 2286 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE); |
| 2287 | goto f_err; |
| 2288 | } |
| 2289 | |
| 2290 | ret = 1; |
| 2291 | |
| 2292 | if (0) { |
| 2293 | f_err: |
| 2294 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2295 | } |
| 2296 | |
| 2297 | err: |
| 2298 | EVP_PKEY_CTX_free(pctx); |
| 2299 | EVP_PKEY_free(pkey); |
| 2300 | |
| 2301 | return ret; |
| 2302 | } |
| 2303 | |
| 2304 | int ssl3_get_client_certificate(SSL *s) { |
| 2305 | int i, ok, al, ret = -1; |
| 2306 | X509 *x = NULL; |
| 2307 | unsigned long n; |
| 2308 | STACK_OF(X509) *sk = NULL; |
| 2309 | SHA256_CTX sha256; |
| 2310 | CBS certificate_msg, certificate_list; |
| 2311 | int is_first_certificate = 1; |
| 2312 | |
| 2313 | n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1, |
| 2314 | s->max_cert_list, SSL_GET_MESSAGE_HASH_MESSAGE, |
| 2315 | &ok); |
| 2316 | |
| 2317 | if (!ok) { |
| 2318 | return n; |
| 2319 | } |
| 2320 | |
| 2321 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
| 2322 | if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2323 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
| 2324 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2325 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2326 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2327 | goto f_err; |
| 2328 | } |
| 2329 | |
| 2330 | /* If tls asked for a client cert, the client must return a 0 list */ |
| 2331 | if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) { |
| 2332 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2333 | SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2334 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 2335 | goto f_err; |
| 2336 | } |
| 2337 | s->s3->tmp.reuse_message = 1; |
| 2338 | |
| 2339 | return 1; |
| 2340 | } |
| 2341 | |
| 2342 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 2343 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 2344 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2345 | SSL_R_WRONG_MESSAGE_TYPE); |
| 2346 | goto f_err; |
| 2347 | } |
| 2348 | |
| 2349 | CBS_init(&certificate_msg, s->init_msg, n); |
| 2350 | |
| 2351 | sk = sk_X509_new_null(); |
| 2352 | if (sk == NULL) { |
| 2353 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2354 | goto err; |
| 2355 | } |
| 2356 | |
| 2357 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 2358 | CBS_len(&certificate_msg) != 0) { |
| 2359 | al = SSL_AD_DECODE_ERROR; |
| 2360 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
| 2361 | goto f_err; |
| 2362 | } |
| 2363 | |
| 2364 | while (CBS_len(&certificate_list) > 0) { |
| 2365 | CBS certificate; |
| 2366 | const uint8_t *data; |
| 2367 | |
| 2368 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 2369 | al = SSL_AD_DECODE_ERROR; |
| 2370 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
| 2371 | goto f_err; |
| 2372 | } |
| 2373 | |
| 2374 | if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) { |
| 2375 | /* If this is the first certificate, and we don't want to keep peer |
| 2376 | * certificates in memory, then we hash it right away. */ |
| 2377 | SHA256_Init(&sha256); |
| 2378 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
| 2379 | SHA256_Final(s->session->peer_sha256, &sha256); |
| 2380 | s->session->peer_sha256_valid = 1; |
| 2381 | } |
| 2382 | is_first_certificate = 0; |
| 2383 | |
| 2384 | data = CBS_data(&certificate); |
| 2385 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
| 2386 | if (x == NULL) { |
| 2387 | al = SSL_AD_BAD_CERTIFICATE; |
| 2388 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB); |
| 2389 | goto f_err; |
| 2390 | } |
| 2391 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 2392 | al = SSL_AD_DECODE_ERROR; |
| 2393 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2394 | SSL_R_CERT_LENGTH_MISMATCH); |
| 2395 | goto f_err; |
| 2396 | } |
| 2397 | if (!sk_X509_push(sk, x)) { |
| 2398 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2399 | goto err; |
| 2400 | } |
| 2401 | x = NULL; |
| 2402 | } |
| 2403 | |
| 2404 | if (sk_X509_num(sk) <= 0) { |
| 2405 | /* TLS does not mind 0 certs returned */ |
| 2406 | if (s->version == SSL3_VERSION) { |
| 2407 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2408 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2409 | SSL_R_NO_CERTIFICATES_RETURNED); |
| 2410 | goto f_err; |
| 2411 | } |
| 2412 | /* Fail for TLS only if we required a certificate */ |
| 2413 | else if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2414 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
| 2415 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2416 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2417 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2418 | goto f_err; |
| 2419 | } |
| 2420 | /* No client certificate so digest cached records */ |
| 2421 | if (s->s3->handshake_buffer && |
| 2422 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2423 | al = SSL_AD_INTERNAL_ERROR; |
| 2424 | goto f_err; |
| 2425 | } |
| 2426 | } else { |
| 2427 | i = ssl_verify_cert_chain(s, sk); |
| 2428 | if (i <= 0) { |
| 2429 | al = ssl_verify_alarm_type(s->verify_result); |
| 2430 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2431 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2432 | goto f_err; |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | if (s->session->peer != NULL) { |
| 2437 | /* This should not be needed */ |
| 2438 | X509_free(s->session->peer); |
| 2439 | } |
| 2440 | |
| 2441 | s->session->peer = sk_X509_shift(sk); |
| 2442 | s->session->verify_result = s->verify_result; |
| 2443 | |
| 2444 | /* With the current implementation, sess_cert will always be NULL when we |
| 2445 | * arrive here. */ |
| 2446 | if (s->session->sess_cert == NULL) { |
| 2447 | s->session->sess_cert = ssl_sess_cert_new(); |
| 2448 | if (s->session->sess_cert == NULL) { |
| 2449 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2450 | goto err; |
| 2451 | } |
| 2452 | } |
| 2453 | if (s->session->sess_cert->cert_chain != NULL) { |
| 2454 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); |
| 2455 | } |
| 2456 | s->session->sess_cert->cert_chain = sk; |
| 2457 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 2458 | * certificate, while we do include it in s3_clnt.c */ |
| 2459 | |
| 2460 | sk = NULL; |
| 2461 | |
| 2462 | ret = 1; |
| 2463 | |
| 2464 | if (0) { |
| 2465 | f_err: |
| 2466 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2467 | } |
| 2468 | |
| 2469 | err: |
| 2470 | if (x != NULL) { |
| 2471 | X509_free(x); |
| 2472 | } |
| 2473 | if (sk != NULL) { |
| 2474 | sk_X509_pop_free(sk, X509_free); |
| 2475 | } |
| 2476 | return ret; |
| 2477 | } |
| 2478 | |
| 2479 | int ssl3_send_server_certificate(SSL *s) { |
| 2480 | CERT_PKEY *cpk; |
| 2481 | |
| 2482 | if (s->state == SSL3_ST_SW_CERT_A) { |
| 2483 | cpk = ssl_get_server_send_pkey(s); |
| 2484 | if (cpk == NULL) { |
| 2485 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, |
| 2486 | ERR_R_INTERNAL_ERROR); |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | ssl3_output_cert_chain(s, cpk); |
| 2491 | s->state = SSL3_ST_SW_CERT_B; |
| 2492 | } |
| 2493 | |
| 2494 | /* SSL3_ST_SW_CERT_B */ |
| 2495 | return ssl_do_write(s); |
| 2496 | } |
| 2497 | |
| 2498 | /* send a new session ticket (not necessarily for a new session) */ |
| 2499 | int ssl3_send_new_session_ticket(SSL *s) { |
| 2500 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { |
| 2501 | uint8_t *session; |
| 2502 | size_t session_len; |
| 2503 | uint8_t *p, *macstart; |
| 2504 | int len; |
| 2505 | unsigned int hlen; |
| 2506 | EVP_CIPHER_CTX ctx; |
| 2507 | HMAC_CTX hctx; |
| 2508 | SSL_CTX *tctx = s->initial_ctx; |
| 2509 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 2510 | uint8_t key_name[16]; |
| 2511 | /* The maximum overhead of encrypting the session is 16 (key name) + IV + |
| 2512 | * one block of encryption overhead + HMAC. */ |
| 2513 | const size_t max_ticket_overhead = |
| 2514 | 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
| 2515 | |
| 2516 | /* Serialize the SSL_SESSION to be encoded into the ticket. */ |
| 2517 | if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) { |
| 2518 | return -1; |
| 2519 | } |
| 2520 | |
| 2521 | /* If the session is too long, emit a dummy value rather than abort the |
| 2522 | * connection. */ |
| 2523 | if (session_len > 0xFFFF - max_ticket_overhead) { |
| 2524 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
| 2525 | const size_t placeholder_len = strlen(kTicketPlaceholder); |
| 2526 | |
| 2527 | OPENSSL_free(session); |
| 2528 | |
| 2529 | p = ssl_handshake_start(s); |
| 2530 | /* Emit ticket_lifetime_hint. */ |
| 2531 | l2n(0, p); |
| 2532 | /* Emit ticket. */ |
| 2533 | s2n(placeholder_len, p); |
| 2534 | memcpy(p, kTicketPlaceholder, placeholder_len); |
| 2535 | p += placeholder_len; |
| 2536 | |
| 2537 | len = p - ssl_handshake_start(s); |
| 2538 | ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len); |
| 2539 | s->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 2540 | return ssl_do_write(s); |
| 2541 | } |
| 2542 | |
| 2543 | /* Grow buffer if need be: the length calculation is as follows: |
| 2544 | * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) + |
| 2545 | * max_ticket_overhead + * session_length */ |
| 2546 | if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 + |
| 2547 | max_ticket_overhead + session_len)) { |
| 2548 | OPENSSL_free(session); |
| 2549 | return -1; |
| 2550 | } |
| 2551 | p = ssl_handshake_start(s); |
| 2552 | EVP_CIPHER_CTX_init(&ctx); |
| 2553 | HMAC_CTX_init(&hctx); |
| 2554 | /* Initialize HMAC and cipher contexts. If callback present it does all the |
| 2555 | * work otherwise use generated values from parent ctx. */ |
| 2556 | if (tctx->tlsext_ticket_key_cb) { |
| 2557 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, 1) < 0) { |
| 2558 | OPENSSL_free(session); |
| 2559 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2560 | HMAC_CTX_cleanup(&hctx); |
| 2561 | return -1; |
| 2562 | } |
| 2563 | } else { |
| 2564 | if (!RAND_bytes(iv, 16) || |
| 2565 | !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2566 | tctx->tlsext_tick_aes_key, iv) || |
| 2567 | !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), |
| 2568 | NULL)) { |
| 2569 | OPENSSL_free(session); |
| 2570 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2571 | HMAC_CTX_cleanup(&hctx); |
| 2572 | return -1; |
| 2573 | } |
| 2574 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 2575 | } |
| 2576 | |
| 2577 | /* Ticket lifetime hint (advisory only): We leave this unspecified for |
| 2578 | * resumed session (for simplicity), and guess that tickets for new |
| 2579 | * sessions will live as long as their sessions. */ |
| 2580 | l2n(s->hit ? 0 : s->session->timeout, p); |
| 2581 | |
| 2582 | /* Skip ticket length for now */ |
| 2583 | p += 2; |
| 2584 | /* Output key name */ |
| 2585 | macstart = p; |
| 2586 | memcpy(p, key_name, 16); |
| 2587 | p += 16; |
| 2588 | /* output IV */ |
| 2589 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 2590 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 2591 | /* Encrypt session data */ |
| 2592 | EVP_EncryptUpdate(&ctx, p, &len, session, session_len); |
| 2593 | p += len; |
| 2594 | EVP_EncryptFinal_ex(&ctx, p, &len); |
| 2595 | p += len; |
| 2596 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2597 | |
| 2598 | HMAC_Update(&hctx, macstart, p - macstart); |
| 2599 | HMAC_Final(&hctx, p, &hlen); |
| 2600 | HMAC_CTX_cleanup(&hctx); |
| 2601 | |
| 2602 | p += hlen; |
| 2603 | /* Now write out lengths: p points to end of data written */ |
| 2604 | /* Total length */ |
| 2605 | len = p - ssl_handshake_start(s); |
| 2606 | /* Skip ticket lifetime hint */ |
| 2607 | p = ssl_handshake_start(s) + 4; |
| 2608 | s2n(len - 6, p); |
| 2609 | ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len); |
| 2610 | s->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 2611 | OPENSSL_free(session); |
| 2612 | } |
| 2613 | |
| 2614 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
| 2615 | return ssl_do_write(s); |
| 2616 | } |
| 2617 | |
| 2618 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 2619 | * sets the next_proto member in s if found */ |
| 2620 | int ssl3_get_next_proto(SSL *s) { |
| 2621 | int ok; |
| 2622 | long n; |
| 2623 | CBS next_protocol, selected_protocol, padding; |
| 2624 | |
| 2625 | /* Clients cannot send a NextProtocol message if we didn't see the extension |
| 2626 | * in their ClientHello */ |
| 2627 | if (!s->s3->next_proto_neg_seen) { |
| 2628 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, |
| 2629 | SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
| 2630 | return -1; |
| 2631 | } |
| 2632 | |
| 2633 | n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A, |
| 2634 | SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO, |
| 2635 | 514, /* See the payload format below */ |
| 2636 | SSL_GET_MESSAGE_HASH_MESSAGE, &ok); |
| 2637 | |
| 2638 | if (!ok) { |
| 2639 | return n; |
| 2640 | } |
| 2641 | |
| 2642 | /* s->state doesn't reflect whether ChangeCipherSpec has been received in |
| 2643 | * this handshake, but s->s3->change_cipher_spec does (will be reset by |
| 2644 | * ssl3_get_finished). |
| 2645 | * |
| 2646 | * TODO(davidben): Is this check now redundant with |
| 2647 | * SSL3_FLAGS_EXPECT_CCS? */ |
| 2648 | if (!s->s3->change_cipher_spec) { |
| 2649 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, |
| 2650 | SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); |
| 2651 | return -1; |
| 2652 | } |
| 2653 | |
| 2654 | CBS_init(&next_protocol, s->init_msg, n); |
| 2655 | |
| 2656 | /* The payload looks like: |
| 2657 | * uint8 proto_len; |
| 2658 | * uint8 proto[proto_len]; |
| 2659 | * uint8 padding_len; |
| 2660 | * uint8 padding[padding_len]; */ |
| 2661 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 2662 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 2663 | CBS_len(&next_protocol) != 0 || |
| 2664 | !CBS_stow(&selected_protocol, &s->next_proto_negotiated, |
| 2665 | &s->next_proto_negotiated_len)) { |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
| 2669 | return 1; |
| 2670 | } |
| 2671 | |
| 2672 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 2673 | int ssl3_get_channel_id(SSL *s) { |
| 2674 | int ret = -1, ok; |
| 2675 | long n; |
| 2676 | EVP_MD_CTX md_ctx; |
| 2677 | uint8_t channel_id_hash[SHA256_DIGEST_LENGTH]; |
| 2678 | unsigned int channel_id_hash_len; |
| 2679 | const uint8_t *p; |
| 2680 | uint16_t extension_type, expected_extension_type; |
| 2681 | EC_GROUP *p256 = NULL; |
| 2682 | EC_KEY *key = NULL; |
| 2683 | EC_POINT *point = NULL; |
| 2684 | ECDSA_SIG sig; |
| 2685 | BIGNUM x, y; |
| 2686 | CBS encrypted_extensions, extension; |
| 2687 | |
| 2688 | n = s->method->ssl_get_message( |
| 2689 | s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B, |
| 2690 | SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, |
| 2691 | SSL_GET_MESSAGE_DONT_HASH_MESSAGE, &ok); |
| 2692 | |
| 2693 | if (!ok) { |
| 2694 | return n; |
| 2695 | } |
| 2696 | |
| 2697 | /* Before incorporating the EncryptedExtensions message to the handshake |
| 2698 | * hash, compute the hash that should have been signed. */ |
| 2699 | channel_id_hash_len = sizeof(channel_id_hash); |
| 2700 | EVP_MD_CTX_init(&md_ctx); |
| 2701 | if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) || |
| 2702 | !tls1_channel_id_hash(&md_ctx, s) || |
| 2703 | !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) { |
| 2704 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2705 | return -1; |
| 2706 | } |
| 2707 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2708 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
| 2709 | |
| 2710 | ssl3_hash_current_message(s); |
| 2711 | |
| 2712 | /* s->state doesn't reflect whether ChangeCipherSpec has been received in |
| 2713 | * this handshake, but s->s3->change_cipher_spec does (will be reset by |
| 2714 | * ssl3_get_finished). |
| 2715 | * |
| 2716 | * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */ |
| 2717 | if (!s->s3->change_cipher_spec) { |
| 2718 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, |
| 2719 | SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS); |
| 2720 | return -1; |
| 2721 | } |
| 2722 | |
| 2723 | CBS_init(&encrypted_extensions, s->init_msg, n); |
| 2724 | |
| 2725 | /* EncryptedExtensions could include multiple extensions, but the only |
| 2726 | * extension that could be negotiated is ChannelID, so there can only be one |
| 2727 | * entry. |
| 2728 | * |
| 2729 | * The payload looks like: |
| 2730 | * uint16 extension_type |
| 2731 | * uint16 extension_len; |
| 2732 | * uint8 x[32]; |
| 2733 | * uint8 y[32]; |
| 2734 | * uint8 r[32]; |
| 2735 | * uint8 s[32]; */ |
| 2736 | expected_extension_type = TLSEXT_TYPE_channel_id; |
| 2737 | if (s->s3->tlsext_channel_id_new) { |
| 2738 | expected_extension_type = TLSEXT_TYPE_channel_id_new; |
| 2739 | } |
| 2740 | |
| 2741 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 2742 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 2743 | CBS_len(&encrypted_extensions) != 0 || |
| 2744 | extension_type != expected_extension_type || |
| 2745 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 2746 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE); |
| 2747 | return -1; |
| 2748 | } |
| 2749 | |
| 2750 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2751 | if (!p256) { |
| 2752 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT); |
| 2753 | return -1; |
| 2754 | } |
| 2755 | |
| 2756 | BN_init(&x); |
| 2757 | BN_init(&y); |
| 2758 | sig.r = BN_new(); |
| 2759 | sig.s = BN_new(); |
| 2760 | |
| 2761 | p = CBS_data(&extension); |
| 2762 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 2763 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 2764 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 2765 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 2766 | goto err; |
| 2767 | } |
| 2768 | |
| 2769 | point = EC_POINT_new(p256); |
| 2770 | if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
| 2771 | goto err; |
| 2772 | } |
| 2773 | |
| 2774 | key = EC_KEY_new(); |
| 2775 | if (!key || !EC_KEY_set_group(key, p256) || |
| 2776 | !EC_KEY_set_public_key(key, point)) { |
| 2777 | goto err; |
| 2778 | } |
| 2779 | |
| 2780 | /* We stored the handshake hash in |tlsext_channel_id| the first time that we |
| 2781 | * were called. */ |
| 2782 | if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) { |
| 2783 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, |
| 2784 | SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 2785 | s->s3->tlsext_channel_id_valid = 0; |
| 2786 | goto err; |
| 2787 | } |
| 2788 | |
| 2789 | memcpy(s->s3->tlsext_channel_id, p, 64); |
| 2790 | ret = 1; |
| 2791 | |
| 2792 | err: |
| 2793 | BN_free(&x); |
| 2794 | BN_free(&y); |
| 2795 | BN_free(sig.r); |
| 2796 | BN_free(sig.s); |
| 2797 | if (key) { |
| 2798 | EC_KEY_free(key); |
| 2799 | } |
| 2800 | if (point) { |
| 2801 | EC_POINT_free(point); |
| 2802 | } |
| 2803 | if (p256) { |
| 2804 | EC_GROUP_free(p256); |
| 2805 | } |
| 2806 | return ret; |
| 2807 | } |