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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 149 | #include <openssl/ssl.h> |
| 150 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 162 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 163 | #include <openssl/evp.h> |
| 164 | #include <openssl/hmac.h> |
| 165 | #include <openssl/md5.h> |
| 166 | #include <openssl/mem.h> |
| 167 | #include <openssl/obj.h> |
| 168 | #include <openssl/rand.h> |
| 169 | #include <openssl/sha.h> |
| 170 | #include <openssl/x509.h> |
| 171 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 172 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 173 | #include "../crypto/internal.h" |
| 174 | #include "../crypto/dh/internal.h" |
| 175 | |
| 176 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 177 | int ssl3_accept(SSL *s) { |
| 178 | BUF_MEM *buf = NULL; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 179 | uint32_t alg_a; |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 180 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 181 | int ret = -1; |
| 182 | int new_state, state, skip = 0; |
| 183 | |
| 184 | assert(s->handshake_func == ssl3_accept); |
| 185 | assert(s->server); |
| 186 | assert(!SSL_IS_DTLS(s)); |
| 187 | |
| 188 | ERR_clear_error(); |
| 189 | ERR_clear_system_error(); |
| 190 | |
| 191 | if (s->info_callback != NULL) { |
| 192 | cb = s->info_callback; |
| 193 | } else if (s->ctx->info_callback != NULL) { |
| 194 | cb = s->ctx->info_callback; |
| 195 | } |
| 196 | |
| 197 | s->in_handshake++; |
| 198 | |
| 199 | if (s->cert == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 200 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | for (;;) { |
| 205 | state = s->state; |
| 206 | |
| 207 | switch (s->state) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 208 | case SSL_ST_ACCEPT: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 209 | if (cb != NULL) { |
| 210 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 211 | } |
| 212 | |
| 213 | if (s->init_buf == NULL) { |
| 214 | buf = BUF_MEM_new(); |
| 215 | if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 216 | ret = -1; |
| 217 | goto end; |
| 218 | } |
| 219 | s->init_buf = buf; |
| 220 | buf = NULL; |
| 221 | } |
| 222 | s->init_num = 0; |
| 223 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 224 | /* Enable a write buffer. This groups handshake messages within a flight |
| 225 | * into a single write. */ |
| 226 | if (!ssl_init_wbio_buffer(s, 1)) { |
| 227 | ret = -1; |
| 228 | goto end; |
| 229 | } |
| 230 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 231 | if (!ssl3_init_handshake_buffer(s)) { |
| 232 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 233 | ret = -1; |
| 234 | goto end; |
| 235 | } |
| 236 | |
| 237 | if (!s->s3->have_version) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 238 | s->state = SSL3_ST_SR_INITIAL_BYTES; |
| 239 | } else { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 241 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 242 | break; |
| 243 | |
| 244 | case SSL3_ST_SR_INITIAL_BYTES: |
| 245 | ret = ssl3_get_initial_bytes(s); |
| 246 | if (ret <= 0) { |
| 247 | goto end; |
| 248 | } |
| 249 | /* ssl3_get_initial_bytes sets s->state to one of |
| 250 | * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */ |
| 251 | break; |
| 252 | |
| 253 | case SSL3_ST_SR_V2_CLIENT_HELLO: |
| 254 | ret = ssl3_get_v2_client_hello(s); |
| 255 | if (ret <= 0) { |
| 256 | goto end; |
| 257 | } |
| 258 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 259 | break; |
| 260 | |
| 261 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 262 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 263 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 264 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 265 | s->shutdown = 0; |
| 266 | ret = ssl3_get_client_hello(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | if (ret <= 0) { |
| 268 | goto end; |
| 269 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 270 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 271 | s->init_num = 0; |
| 272 | break; |
| 273 | |
| 274 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 275 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 276 | ret = ssl3_send_server_hello(s); |
| 277 | if (ret <= 0) { |
| 278 | goto end; |
| 279 | } |
| 280 | if (s->hit) { |
| 281 | if (s->tlsext_ticket_expected) { |
| 282 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 283 | } else { |
| 284 | s->state = SSL3_ST_SW_CHANGE_A; |
| 285 | } |
| 286 | } else { |
| 287 | s->state = SSL3_ST_SW_CERT_A; |
| 288 | } |
| 289 | s->init_num = 0; |
| 290 | break; |
| 291 | |
| 292 | case SSL3_ST_SW_CERT_A: |
| 293 | case SSL3_ST_SW_CERT_B: |
| 294 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 295 | ret = ssl3_send_server_certificate(s); |
| 296 | if (ret <= 0) { |
| 297 | goto end; |
| 298 | } |
| 299 | if (s->s3->tmp.certificate_status_expected) { |
| 300 | s->state = SSL3_ST_SW_CERT_STATUS_A; |
| 301 | } else { |
| 302 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 303 | } |
| 304 | } else { |
| 305 | skip = 1; |
| 306 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 307 | } |
| 308 | s->init_num = 0; |
| 309 | break; |
| 310 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 311 | case SSL3_ST_SW_CERT_STATUS_A: |
| 312 | case SSL3_ST_SW_CERT_STATUS_B: |
| 313 | ret = ssl3_send_certificate_status(s); |
| 314 | if (ret <= 0) { |
| 315 | goto end; |
| 316 | } |
| 317 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 318 | s->init_num = 0; |
| 319 | break; |
| 320 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 321 | case SSL3_ST_SW_KEY_EXCH_A: |
| 322 | case SSL3_ST_SW_KEY_EXCH_B: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 323 | case SSL3_ST_SW_KEY_EXCH_C: |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 324 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 325 | |
| 326 | /* Send a ServerKeyExchange message if: |
| 327 | * - The key exchange is ephemeral or anonymous |
| 328 | * Diffie-Hellman. |
| 329 | * - There is a PSK identity hint. |
| 330 | * |
| 331 | * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix |
| 332 | * this. In the meantime, keep them in sync. */ |
| 333 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
| 334 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) { |
| 335 | ret = ssl3_send_server_key_exchange(s); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 336 | if (ret <= 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 337 | goto end; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 338 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | } else { |
| 340 | skip = 1; |
| 341 | } |
| 342 | |
| 343 | s->state = SSL3_ST_SW_CERT_REQ_A; |
| 344 | s->init_num = 0; |
| 345 | break; |
| 346 | |
| 347 | case SSL3_ST_SW_CERT_REQ_A: |
| 348 | case SSL3_ST_SW_CERT_REQ_B: |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 349 | if (s->s3->tmp.cert_request) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 350 | ret = ssl3_send_certificate_request(s); |
| 351 | if (ret <= 0) { |
| 352 | goto end; |
| 353 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 354 | } else { |
| 355 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 356 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 357 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
| 358 | s->init_num = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 359 | break; |
| 360 | |
| 361 | case SSL3_ST_SW_SRVR_DONE_A: |
| 362 | case SSL3_ST_SW_SRVR_DONE_B: |
| 363 | ret = ssl3_send_server_done(s); |
| 364 | if (ret <= 0) { |
| 365 | goto end; |
| 366 | } |
| 367 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 368 | s->state = SSL3_ST_SW_FLUSH; |
| 369 | s->init_num = 0; |
| 370 | break; |
| 371 | |
| 372 | case SSL3_ST_SW_FLUSH: |
| 373 | /* This code originally checked to see if any data was pending using |
| 374 | * BIO_CTRL_INFO and then flushed. This caused problems as documented |
| 375 | * in PR#1939. The proposed fix doesn't completely resolve this issue |
| 376 | * as buggy implementations of BIO_CTRL_PENDING still exist. So instead |
| 377 | * we just flush unconditionally. */ |
| 378 | s->rwstate = SSL_WRITING; |
| 379 | if (BIO_flush(s->wbio) <= 0) { |
| 380 | ret = -1; |
| 381 | goto end; |
| 382 | } |
| 383 | s->rwstate = SSL_NOTHING; |
| 384 | |
| 385 | s->state = s->s3->tmp.next_state; |
| 386 | break; |
| 387 | |
| 388 | case SSL3_ST_SR_CERT_A: |
| 389 | case SSL3_ST_SR_CERT_B: |
| 390 | if (s->s3->tmp.cert_request) { |
| 391 | ret = ssl3_get_client_certificate(s); |
| 392 | if (ret <= 0) { |
| 393 | goto end; |
| 394 | } |
| 395 | } |
| 396 | s->init_num = 0; |
| 397 | s->state = SSL3_ST_SR_KEY_EXCH_A; |
| 398 | break; |
| 399 | |
| 400 | case SSL3_ST_SR_KEY_EXCH_A: |
| 401 | case SSL3_ST_SR_KEY_EXCH_B: |
| 402 | ret = ssl3_get_client_key_exchange(s); |
| 403 | if (ret <= 0) { |
| 404 | goto end; |
| 405 | } |
| 406 | s->state = SSL3_ST_SR_CERT_VRFY_A; |
| 407 | s->init_num = 0; |
| 408 | break; |
| 409 | |
| 410 | case SSL3_ST_SR_CERT_VRFY_A: |
| 411 | case SSL3_ST_SR_CERT_VRFY_B: |
| 412 | ret = ssl3_get_cert_verify(s); |
| 413 | if (ret <= 0) { |
| 414 | goto end; |
| 415 | } |
| 416 | |
| 417 | s->state = SSL3_ST_SR_CHANGE; |
| 418 | s->init_num = 0; |
| 419 | break; |
| 420 | |
| 421 | case SSL3_ST_SR_CHANGE: { |
| 422 | char next_proto_neg = 0; |
| 423 | char channel_id = 0; |
| 424 | next_proto_neg = s->s3->next_proto_neg_seen; |
| 425 | channel_id = s->s3->tlsext_channel_id_valid; |
| 426 | |
| 427 | /* At this point, the next message must be entirely behind a |
| 428 | * ChangeCipherSpec. */ |
| 429 | if (!ssl3_expect_change_cipher_spec(s)) { |
| 430 | ret = -1; |
| 431 | goto end; |
| 432 | } |
| 433 | if (next_proto_neg) { |
| 434 | s->state = SSL3_ST_SR_NEXT_PROTO_A; |
| 435 | } else if (channel_id) { |
| 436 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
| 437 | } else { |
| 438 | s->state = SSL3_ST_SR_FINISHED_A; |
| 439 | } |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | case SSL3_ST_SR_NEXT_PROTO_A: |
| 444 | case SSL3_ST_SR_NEXT_PROTO_B: |
| 445 | ret = ssl3_get_next_proto(s); |
| 446 | if (ret <= 0) { |
| 447 | goto end; |
| 448 | } |
| 449 | s->init_num = 0; |
| 450 | if (s->s3->tlsext_channel_id_valid) { |
| 451 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
| 452 | } else { |
| 453 | s->state = SSL3_ST_SR_FINISHED_A; |
| 454 | } |
| 455 | break; |
| 456 | |
| 457 | case SSL3_ST_SR_CHANNEL_ID_A: |
| 458 | case SSL3_ST_SR_CHANNEL_ID_B: |
| 459 | ret = ssl3_get_channel_id(s); |
| 460 | if (ret <= 0) { |
| 461 | goto end; |
| 462 | } |
| 463 | s->init_num = 0; |
| 464 | s->state = SSL3_ST_SR_FINISHED_A; |
| 465 | break; |
| 466 | |
| 467 | case SSL3_ST_SR_FINISHED_A: |
| 468 | case SSL3_ST_SR_FINISHED_B: |
| 469 | ret = |
| 470 | ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B); |
| 471 | if (ret <= 0) { |
| 472 | goto end; |
| 473 | } |
| 474 | |
| 475 | if (s->hit) { |
| 476 | s->state = SSL_ST_OK; |
| 477 | } else if (s->tlsext_ticket_expected) { |
| 478 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 479 | } else { |
| 480 | s->state = SSL3_ST_SW_CHANGE_A; |
| 481 | } |
| 482 | /* If this is a full handshake with ChannelID then record the hashshake |
| 483 | * hashes in |s->session| in case we need them to verify a ChannelID |
| 484 | * signature on a resumption of this session in the future. */ |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 485 | if (!s->hit) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 486 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 487 | if (ret <= 0) { |
| 488 | goto end; |
| 489 | } |
| 490 | } |
| 491 | s->init_num = 0; |
| 492 | break; |
| 493 | |
| 494 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 495 | case SSL3_ST_SW_SESSION_TICKET_B: |
| 496 | ret = ssl3_send_new_session_ticket(s); |
| 497 | if (ret <= 0) { |
| 498 | goto end; |
| 499 | } |
| 500 | s->state = SSL3_ST_SW_CHANGE_A; |
| 501 | s->init_num = 0; |
| 502 | break; |
| 503 | |
| 504 | case SSL3_ST_SW_CHANGE_A: |
| 505 | case SSL3_ST_SW_CHANGE_B: |
| 506 | s->session->cipher = s->s3->tmp.new_cipher; |
| 507 | if (!s->enc_method->setup_key_block(s)) { |
| 508 | ret = -1; |
| 509 | goto end; |
| 510 | } |
| 511 | |
| 512 | ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A, |
| 513 | SSL3_ST_SW_CHANGE_B); |
| 514 | if (ret <= 0) { |
| 515 | goto end; |
| 516 | } |
| 517 | s->state = SSL3_ST_SW_FINISHED_A; |
| 518 | s->init_num = 0; |
| 519 | |
| 520 | if (!s->enc_method->change_cipher_state( |
| 521 | s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
| 522 | ret = -1; |
| 523 | goto end; |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case SSL3_ST_SW_FINISHED_A: |
| 528 | case SSL3_ST_SW_FINISHED_B: |
| 529 | ret = |
| 530 | ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, |
| 531 | s->enc_method->server_finished_label, |
| 532 | s->enc_method->server_finished_label_len); |
| 533 | if (ret <= 0) { |
| 534 | goto end; |
| 535 | } |
| 536 | s->state = SSL3_ST_SW_FLUSH; |
| 537 | if (s->hit) { |
| 538 | s->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
| 539 | } else { |
| 540 | s->s3->tmp.next_state = SSL_ST_OK; |
| 541 | } |
| 542 | s->init_num = 0; |
| 543 | break; |
| 544 | |
| 545 | case SSL_ST_OK: |
| 546 | /* clean a few things up */ |
| 547 | ssl3_cleanup_key_block(s); |
| 548 | |
| 549 | BUF_MEM_free(s->init_buf); |
| 550 | s->init_buf = NULL; |
| 551 | |
| 552 | /* remove buffering on output */ |
| 553 | ssl_free_wbio_buffer(s); |
| 554 | |
| 555 | s->init_num = 0; |
| 556 | |
| 557 | /* If we aren't retaining peer certificates then we can discard it |
| 558 | * now. */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 559 | if (s->ctx->retain_only_sha256_of_client_certs) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 560 | X509_free(s->session->peer); |
| 561 | s->session->peer = NULL; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 562 | sk_X509_pop_free(s->session->cert_chain, X509_free); |
| 563 | s->session->cert_chain = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 566 | s->s3->initial_handshake_complete = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 567 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 568 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 569 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 570 | if (cb != NULL) { |
| 571 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | ret = 1; |
| 575 | goto end; |
| 576 | |
| 577 | default: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 578 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 579 | ret = -1; |
| 580 | goto end; |
| 581 | } |
| 582 | |
| 583 | if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) { |
| 584 | new_state = s->state; |
| 585 | s->state = state; |
| 586 | cb(s, SSL_CB_ACCEPT_LOOP, 1); |
| 587 | s->state = new_state; |
| 588 | } |
| 589 | skip = 0; |
| 590 | } |
| 591 | |
| 592 | end: |
| 593 | s->in_handshake--; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 594 | BUF_MEM_free(buf); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 595 | if (cb != NULL) { |
| 596 | cb(s, SSL_CB_ACCEPT_EXIT, ret); |
| 597 | } |
| 598 | return ret; |
| 599 | } |
| 600 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 601 | int ssl3_get_initial_bytes(SSL *s) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 602 | /* Read the first 5 bytes, the size of the TLS record header. This is |
| 603 | * sufficient to detect a V2ClientHello and ensures that we never read beyond |
| 604 | * the first record. */ |
| 605 | int ret = ssl_read_buffer_extend_to(s, SSL3_RT_HEADER_LENGTH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 606 | if (ret <= 0) { |
| 607 | return ret; |
| 608 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 609 | assert(ssl_read_buffer_len(s) == SSL3_RT_HEADER_LENGTH); |
| 610 | const uint8_t *p = ssl_read_buffer(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 611 | |
| 612 | /* Some dedicated error codes for protocol mixups should the application wish |
| 613 | * to interpret them differently. (These do not overlap with ClientHello or |
| 614 | * V2ClientHello.) */ |
| 615 | if (strncmp("GET ", (const char *)p, 4) == 0 || |
| 616 | strncmp("POST ", (const char *)p, 5) == 0 || |
| 617 | strncmp("HEAD ", (const char *)p, 5) == 0 || |
| 618 | strncmp("PUT ", (const char *)p, 4) == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 619 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 620 | return -1; |
| 621 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 622 | if (strncmp("CONNE", (const char *)p, 5) == 0) { |
| 623 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 624 | return -1; |
| 625 | } |
| 626 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 627 | /* Determine if this is a V2ClientHello. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 628 | if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO && |
| 629 | p[3] >= SSL3_VERSION_MAJOR) { |
| 630 | /* This is a V2ClientHello. */ |
| 631 | s->state = SSL3_ST_SR_V2_CLIENT_HELLO; |
| 632 | return 1; |
| 633 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 634 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 635 | /* Fall through to the standard logic. */ |
| 636 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 637 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | int ssl3_get_v2_client_hello(SSL *s) { |
| 641 | const uint8_t *p; |
| 642 | int ret; |
| 643 | CBS v2_client_hello, cipher_specs, session_id, challenge; |
| 644 | size_t msg_length, rand_len, len; |
| 645 | uint8_t msg_type; |
| 646 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 647 | CBB client_hello, hello_body, cipher_suites; |
| 648 | uint8_t random[SSL3_RANDOM_SIZE]; |
| 649 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 650 | /* Determine the length of the V2ClientHello. */ |
| 651 | assert(ssl_read_buffer_len(s) >= SSL3_RT_HEADER_LENGTH); |
| 652 | p = ssl_read_buffer(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 653 | msg_length = ((p[0] & 0x7f) << 8) | p[1]; |
| 654 | if (msg_length > (1024 * 4)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 655 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 656 | return -1; |
| 657 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 658 | if (msg_length < SSL3_RT_HEADER_LENGTH - 2) { |
| 659 | /* Reject lengths that are too short early. We have already read |
| 660 | * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an |
| 661 | * (invalid) V2ClientHello which would be shorter than that. */ |
| 662 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 663 | return -1; |
| 664 | } |
| 665 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 666 | /* Read the remainder of the V2ClientHello. */ |
| 667 | ret = ssl_read_buffer_extend_to(s, 2 + msg_length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 668 | if (ret <= 0) { |
| 669 | return ret; |
| 670 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 671 | assert(ssl_read_buffer_len(s) == msg_length + 2); |
| 672 | CBS_init(&v2_client_hello, ssl_read_buffer(s) + 2, msg_length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 673 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 674 | /* The V2ClientHello without the length is incorporated into the handshake |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 675 | * hash. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 676 | if (!ssl3_update_handshake_hash(s, CBS_data(&v2_client_hello), |
| 677 | CBS_len(&v2_client_hello))) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 678 | return -1; |
| 679 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 680 | if (s->msg_callback) { |
| 681 | s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello), |
| 682 | CBS_len(&v2_client_hello), s, s->msg_callback_arg); |
| 683 | } |
| 684 | |
| 685 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 686 | !CBS_get_u16(&v2_client_hello, &version) || |
| 687 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 688 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 689 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 690 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 691 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 692 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 693 | CBS_len(&v2_client_hello) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 694 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | /* msg_type has already been checked. */ |
| 699 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
| 700 | |
| 701 | /* The client_random is the V2ClientHello challenge. Truncate or |
| 702 | * left-pad with zeros as needed. */ |
| 703 | memset(random, 0, SSL3_RANDOM_SIZE); |
| 704 | rand_len = CBS_len(&challenge); |
| 705 | if (rand_len > SSL3_RANDOM_SIZE) { |
| 706 | rand_len = SSL3_RANDOM_SIZE; |
| 707 | } |
| 708 | memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 709 | rand_len); |
| 710 | |
| 711 | /* Write out an equivalent SSLv3 ClientHello. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 712 | CBB_zero(&client_hello); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 713 | if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 714 | s->init_buf->max) || |
| 715 | !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 716 | !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || |
| 717 | !CBB_add_u16(&hello_body, version) || |
| 718 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 719 | /* No session id. */ |
| 720 | !CBB_add_u8(&hello_body, 0) || |
| 721 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
| 722 | CBB_cleanup(&client_hello); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 723 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | /* Copy the cipher suites. */ |
| 728 | while (CBS_len(&cipher_specs) > 0) { |
| 729 | uint32_t cipher_spec; |
| 730 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
| 731 | CBB_cleanup(&client_hello); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 732 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 733 | return -1; |
| 734 | } |
| 735 | |
| 736 | /* Skip SSLv2 ciphers. */ |
| 737 | if ((cipher_spec & 0xff0000) != 0) { |
| 738 | continue; |
| 739 | } |
| 740 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
| 741 | CBB_cleanup(&client_hello); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 742 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 743 | return -1; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /* Add the null compression scheme and finish. */ |
| 748 | if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) || |
| 749 | !CBB_finish(&client_hello, NULL, &len)) { |
| 750 | CBB_cleanup(&client_hello); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 751 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 752 | return -1; |
| 753 | } |
| 754 | |
| 755 | /* Mark the message for "re"-use by the version-specific method. */ |
| 756 | s->s3->tmp.reuse_message = 1; |
| 757 | s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO; |
| 758 | /* The handshake message header is 4 bytes. */ |
| 759 | s->s3->tmp.message_size = len - 4; |
| 760 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 761 | /* Consume and discard the V2ClientHello. */ |
| 762 | ssl_read_buffer_consume(s, 2 + msg_length); |
| 763 | ssl_read_buffer_discard(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 764 | |
| 765 | return 1; |
| 766 | } |
| 767 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 768 | int ssl3_get_client_hello(SSL *s) { |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 769 | int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 770 | long n; |
| 771 | const SSL_CIPHER *c; |
| 772 | STACK_OF(SSL_CIPHER) *ciphers = NULL; |
| 773 | struct ssl_early_callback_ctx early_ctx; |
| 774 | CBS client_hello; |
| 775 | uint16_t client_version; |
| 776 | CBS client_random, session_id, cipher_suites, compression_methods; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 777 | SSL_SESSION *session = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 778 | |
| 779 | /* We do this so that we will respond with our native type. If we are TLSv1 |
| 780 | * and we get SSLv3, we will respond with TLSv1, This down switching should |
| 781 | * be handled by a different method. If we are SSLv3, we will respond with |
| 782 | * SSLv3, even if prompted with TLSv1. */ |
| 783 | switch (s->state) { |
| 784 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 785 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 786 | n = s->method->ssl_get_message( |
| 787 | s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B, |
| 788 | SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH, |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 789 | ssl_hash_message, &ok); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 790 | |
| 791 | if (!ok) { |
| 792 | return n; |
| 793 | } |
| 794 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 795 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 796 | /* fallthrough */ |
| 797 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 798 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 799 | /* We have previously parsed the ClientHello message, and can't call |
| 800 | * ssl_get_message again without hashing the message into the Finished |
| 801 | * digest again. */ |
| 802 | n = s->init_num; |
| 803 | |
| 804 | memset(&early_ctx, 0, sizeof(early_ctx)); |
| 805 | early_ctx.ssl = s; |
| 806 | early_ctx.client_hello = s->init_msg; |
| 807 | early_ctx.client_hello_len = n; |
| 808 | if (!ssl_early_callback_init(&early_ctx)) { |
| 809 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 810 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 811 | goto f_err; |
| 812 | } |
| 813 | |
| 814 | if (s->state == SSL3_ST_SR_CLNT_HELLO_C && |
| 815 | s->ctx->select_certificate_cb != NULL) { |
| 816 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 817 | switch (s->ctx->select_certificate_cb(&early_ctx)) { |
| 818 | case 0: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 819 | s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 820 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 821 | |
| 822 | case -1: |
| 823 | /* Connection rejected. */ |
| 824 | al = SSL_AD_ACCESS_DENIED; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 825 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 826 | goto f_err; |
| 827 | |
| 828 | default: |
| 829 | /* fallthrough */; |
| 830 | } |
| 831 | } |
| 832 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 833 | break; |
| 834 | |
| 835 | default: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 836 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 837 | return -1; |
| 838 | } |
| 839 | |
| 840 | CBS_init(&client_hello, s->init_msg, n); |
| 841 | if (!CBS_get_u16(&client_hello, &client_version) || |
| 842 | !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) || |
| 843 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 844 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 845 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 846 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 847 | goto f_err; |
| 848 | } |
| 849 | |
| 850 | /* use version from inside client hello, not from record header (may differ: |
| 851 | * see RFC 2246, Appendix E, second paragraph) */ |
| 852 | s->client_version = client_version; |
| 853 | |
| 854 | /* Load the client random. */ |
| 855 | memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE); |
| 856 | |
| 857 | if (SSL_IS_DTLS(s)) { |
| 858 | CBS cookie; |
| 859 | |
| 860 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 861 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
| 862 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 863 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 864 | goto f_err; |
| 865 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 868 | /* Note: This codepath may run twice if |ssl_get_prev_session| completes |
| 869 | * asynchronously. |
| 870 | * |
| 871 | * TODO(davidben): Clean up the order of events around ClientHello |
| 872 | * processing. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 873 | if (!s->s3->have_version) { |
| 874 | /* Select version to use */ |
| 875 | uint16_t version = ssl3_get_mutual_version(s, client_version); |
| 876 | if (version == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 877 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 878 | s->version = s->client_version; |
| 879 | al = SSL_AD_PROTOCOL_VERSION; |
| 880 | goto f_err; |
| 881 | } |
| 882 | s->version = version; |
| 883 | s->enc_method = ssl3_get_enc_method(version); |
| 884 | assert(s->enc_method != NULL); |
| 885 | /* At this point, the connection's version is known and |s->version| is |
| 886 | * fixed. Begin enforcing the record-layer version. */ |
| 887 | s->s3->have_version = 1; |
| 888 | } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version) |
| 889 | : (s->client_version < s->version)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 890 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 891 | al = SSL_AD_PROTOCOL_VERSION; |
| 892 | goto f_err; |
| 893 | } |
| 894 | |
| 895 | s->hit = 0; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 896 | int send_new_ticket = 0; |
| 897 | switch (ssl_get_prev_session(s, &session, &send_new_ticket, &early_ctx)) { |
| 898 | case ssl_session_success: |
| 899 | break; |
| 900 | case ssl_session_error: |
| 901 | goto err; |
| 902 | case ssl_session_retry: |
| 903 | s->rwstate = SSL_PENDING_SESSION; |
| 904 | goto err; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 905 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 906 | s->tlsext_ticket_expected = send_new_ticket; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 907 | |
| 908 | /* The EMS state is needed when making the resumption decision, but |
| 909 | * extensions are not normally parsed until later. This detects the EMS |
| 910 | * extension for the resumption decision and it's checked against the result |
| 911 | * of the normal parse later in this function. */ |
| 912 | const uint8_t *ems_data; |
| 913 | size_t ems_len; |
| 914 | int have_extended_master_secret = |
| 915 | s->version != SSL3_VERSION && |
| 916 | SSL_early_callback_ctx_extension_get(&early_ctx, |
| 917 | TLSEXT_TYPE_extended_master_secret, |
| 918 | &ems_data, &ems_len) && |
| 919 | ems_len == 0; |
| 920 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 921 | if (session != NULL) { |
| 922 | if (session->extended_master_secret && |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 923 | !have_extended_master_secret) { |
| 924 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 925 | * is fatal to the connection. */ |
| 926 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 927 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 928 | goto f_err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 929 | } |
| 930 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 931 | s->hit = |
| 932 | /* Only resume if the session's version matches the negotiated version: |
| 933 | * most clients do not accept a mismatch. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 934 | s->version == session->ssl_version && |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 935 | /* If the client offers the EMS extension, but the previous session |
| 936 | * didn't use it, then negotiate a new session. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 937 | have_extended_master_secret == session->extended_master_secret; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 940 | if (s->hit) { |
| 941 | /* Use the new session. */ |
| 942 | SSL_SESSION_free(s->session); |
| 943 | s->session = session; |
| 944 | session = NULL; |
| 945 | |
| 946 | s->verify_result = s->session->verify_result; |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 947 | } else if (!ssl_get_new_session(s, 1)) { |
| 948 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 951 | if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) { |
| 952 | /* Connection rejected for DOS reasons. */ |
| 953 | al = SSL_AD_ACCESS_DENIED; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 954 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 955 | goto f_err; |
| 956 | } |
| 957 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 958 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 959 | CBS_len(&cipher_suites) == 0 || |
| 960 | CBS_len(&cipher_suites) % 2 != 0 || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 961 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 962 | CBS_len(&compression_methods) == 0) { |
| 963 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 964 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 965 | goto f_err; |
| 966 | } |
| 967 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 968 | ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites); |
| 969 | if (ciphers == NULL) { |
| 970 | goto err; |
| 971 | } |
| 972 | |
| 973 | /* If it is a hit, check that the cipher is in the list. */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 974 | if (s->hit) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 975 | size_t j; |
| 976 | int found_cipher = 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 977 | uint32_t id = s->session->cipher->id; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 978 | |
| 979 | for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) { |
| 980 | c = sk_SSL_CIPHER_value(ciphers, j); |
| 981 | if (c->id == id) { |
| 982 | found_cipher = 1; |
| 983 | break; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | if (!found_cipher) { |
| 988 | /* we need to have the cipher in the cipher list if we are asked to reuse |
| 989 | * it */ |
| 990 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 991 | OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 992 | goto f_err; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /* Only null compression is supported. */ |
| 997 | if (memchr(CBS_data(&compression_methods), 0, |
| 998 | CBS_len(&compression_methods)) == NULL) { |
| 999 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1000 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1001 | goto f_err; |
| 1002 | } |
| 1003 | |
| 1004 | /* TLS extensions. */ |
| 1005 | if (s->version >= SSL3_VERSION && |
| 1006 | !ssl_parse_clienthello_tlsext(s, &client_hello)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1007 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1008 | goto err; |
| 1009 | } |
| 1010 | |
| 1011 | /* There should be nothing left over in the record. */ |
| 1012 | if (CBS_len(&client_hello) != 0) { |
| 1013 | /* wrong packet length */ |
| 1014 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1015 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1016 | goto f_err; |
| 1017 | } |
| 1018 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1019 | if (have_extended_master_secret != s->s3->tmp.extended_master_secret) { |
| 1020 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1021 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1022 | goto f_err; |
| 1023 | } |
| 1024 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1025 | /* Given ciphers and SSL_get_ciphers, we must pick a cipher */ |
| 1026 | if (!s->hit) { |
| 1027 | if (ciphers == NULL) { |
| 1028 | al = SSL_AD_ILLEGAL_PARAMETER; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1029 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1030 | goto f_err; |
| 1031 | } |
| 1032 | |
| 1033 | /* Let cert callback update server certificates if required */ |
| 1034 | if (s->cert->cert_cb) { |
| 1035 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1036 | if (rv == 0) { |
| 1037 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1038 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1039 | goto f_err; |
| 1040 | } |
| 1041 | if (rv < 0) { |
| 1042 | s->rwstate = SSL_X509_LOOKUP; |
| 1043 | goto err; |
| 1044 | } |
| 1045 | s->rwstate = SSL_NOTHING; |
| 1046 | } |
| 1047 | c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s)); |
| 1048 | |
| 1049 | if (c == NULL) { |
| 1050 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1051 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1052 | goto f_err; |
| 1053 | } |
| 1054 | s->s3->tmp.new_cipher = c; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1055 | |
| 1056 | /* Determine whether to request a client certificate. */ |
| 1057 | s->s3->tmp.cert_request = !!(s->verify_mode & SSL_VERIFY_PEER); |
| 1058 | /* Only request a certificate if Channel ID isn't negotiated. */ |
| 1059 | if ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 1060 | s->s3->tlsext_channel_id_valid) { |
| 1061 | s->s3->tmp.cert_request = 0; |
| 1062 | } |
| 1063 | /* Plain PSK forbids Certificate and CertificateRequest. */ |
| 1064 | if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) { |
| 1065 | s->s3->tmp.cert_request = 0; |
| 1066 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1067 | } else { |
| 1068 | /* Session-id reuse */ |
| 1069 | s->s3->tmp.new_cipher = s->session->cipher; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1070 | s->s3->tmp.cert_request = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1073 | /* Now that the cipher is known, initialize the handshake hash. */ |
| 1074 | if (!ssl3_init_handshake_hash(s)) { |
| 1075 | goto f_err; |
| 1076 | } |
| 1077 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1078 | /* In TLS 1.2, client authentication requires hashing the handshake transcript |
| 1079 | * under a different hash. Otherwise, release the handshake buffer. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1080 | if (!SSL_USE_SIGALGS(s) || !s->s3->tmp.cert_request) { |
| 1081 | ssl3_free_handshake_buffer(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* we now have the following setup; |
| 1085 | * client_random |
| 1086 | * cipher_list - our prefered list of ciphers |
| 1087 | * ciphers - the clients prefered list of ciphers |
| 1088 | * compression - basically ignored right now |
| 1089 | * ssl version is set - sslv3 |
| 1090 | * s->session - The ssl session has been setup. |
| 1091 | * s->hit - session reuse flag |
| 1092 | * s->tmp.new_cipher - the new cipher to use. */ |
| 1093 | |
| 1094 | if (ret < 0) { |
| 1095 | ret = -ret; |
| 1096 | } |
| 1097 | |
| 1098 | if (0) { |
| 1099 | f_err: |
| 1100 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1101 | } |
| 1102 | |
| 1103 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1104 | sk_SSL_CIPHER_free(ciphers); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1105 | SSL_SESSION_free(session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1106 | return ret; |
| 1107 | } |
| 1108 | |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1109 | int ssl3_send_server_hello(SSL *s) { |
| 1110 | uint8_t *buf; |
| 1111 | uint8_t *p, *d; |
| 1112 | int sl; |
| 1113 | unsigned long l; |
| 1114 | |
| 1115 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { |
| 1116 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 1117 | * known attack while we fix ChannelID itself. */ |
| 1118 | if (s->s3->tlsext_channel_id_valid && |
| 1119 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 1120 | s->s3->tlsext_channel_id_valid = 0; |
| 1121 | } |
| 1122 | |
| 1123 | /* If this is a resumption and the original handshake didn't support |
| 1124 | * ChannelID then we didn't record the original handshake hashes in the |
| 1125 | * session and so cannot resume with ChannelIDs. */ |
| 1126 | if (s->hit && s->session->original_handshake_hash_len == 0) { |
| 1127 | s->s3->tlsext_channel_id_valid = 0; |
| 1128 | } |
| 1129 | |
| 1130 | buf = (uint8_t *)s->init_buf->data; |
| 1131 | /* Do the message type and length last */ |
| 1132 | d = p = ssl_handshake_start(s); |
| 1133 | |
| 1134 | *(p++) = s->version >> 8; |
| 1135 | *(p++) = s->version & 0xff; |
| 1136 | |
| 1137 | /* Random stuff */ |
| 1138 | if (!ssl_fill_hello_random(s->s3->server_random, SSL3_RANDOM_SIZE, |
| 1139 | 1 /* server */)) { |
| 1140 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); |
| 1144 | p += SSL3_RANDOM_SIZE; |
| 1145 | |
| 1146 | /* There are several cases for the session ID to send |
| 1147 | * back in the server hello: |
| 1148 | * - For session reuse from the session cache, we send back the old session |
| 1149 | * ID. |
| 1150 | * - If stateless session reuse (using a session ticket) is successful, we |
| 1151 | * send back the client's "session ID" (which doesn't actually identify |
| 1152 | * the session). |
| 1153 | * - If it is a new session, we send back the new session ID. |
| 1154 | * - However, if we want the new session to be single-use, we send back a |
| 1155 | * 0-length session ID. |
| 1156 | * s->hit is non-zero in either case of session reuse, so the following |
| 1157 | * won't overwrite an ID that we're supposed to send back. */ |
| 1158 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) { |
| 1159 | s->session->session_id_length = 0; |
| 1160 | } |
| 1161 | |
| 1162 | sl = s->session->session_id_length; |
| 1163 | if (sl > (int)sizeof(s->session->session_id)) { |
| 1164 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | *(p++) = sl; |
| 1168 | memcpy(p, s->session->session_id, sl); |
| 1169 | p += sl; |
| 1170 | |
| 1171 | /* put the cipher */ |
| 1172 | s2n(ssl_cipher_get_value(s->s3->tmp.new_cipher), p); |
| 1173 | |
| 1174 | /* put the compression method */ |
| 1175 | *(p++) = 0; |
| 1176 | |
| 1177 | p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH); |
| 1178 | if (p == NULL) { |
| 1179 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1180 | return -1; |
| 1181 | } |
| 1182 | |
| 1183 | /* do the header */ |
| 1184 | l = (p - d); |
| 1185 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) { |
| 1186 | return -1; |
| 1187 | } |
| 1188 | s->state = SSL3_ST_SW_SRVR_HELLO_B; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1191 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1192 | return ssl_do_write(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1195 | int ssl3_send_certificate_status(SSL *ssl) { |
| 1196 | if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) { |
| 1197 | CBB out, ocsp_response; |
| 1198 | size_t length; |
| 1199 | |
| 1200 | CBB_zero(&out); |
| 1201 | if (!CBB_init_fixed(&out, ssl_handshake_start(ssl), |
| 1202 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 1203 | !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) || |
| 1204 | !CBB_add_u24_length_prefixed(&out, &ocsp_response) || |
| 1205 | !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 1206 | ssl->ctx->ocsp_response_length) || |
| 1207 | !CBB_finish(&out, NULL, &length) || |
| 1208 | !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) { |
| 1209 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1210 | CBB_cleanup(&out); |
| 1211 | return -1; |
| 1212 | } |
| 1213 | |
| 1214 | ssl->state = SSL3_ST_SW_CERT_STATUS_B; |
| 1215 | } |
| 1216 | |
| 1217 | /* SSL3_ST_SW_CERT_STATUS_B */ |
| 1218 | return ssl_do_write(ssl); |
| 1219 | } |
| 1220 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1221 | int ssl3_send_server_done(SSL *s) { |
| 1222 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1223 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) { |
| 1224 | return -1; |
| 1225 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1226 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1227 | } |
| 1228 | |
| 1229 | /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1230 | return ssl_do_write(s); |
| 1231 | } |
| 1232 | |
| 1233 | int ssl3_send_server_key_exchange(SSL *s) { |
| 1234 | DH *dh = NULL, *dhp; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1235 | EC_KEY *ecdh = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1236 | uint8_t *encodedPoint = NULL; |
| 1237 | int encodedlen = 0; |
| 1238 | uint16_t curve_id = 0; |
| 1239 | BN_CTX *bn_ctx = NULL; |
| 1240 | const char *psk_identity_hint = NULL; |
| 1241 | size_t psk_identity_hint_len = 0; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1242 | size_t sig_len; |
| 1243 | size_t max_sig_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1244 | uint8_t *p, *d; |
| 1245 | int al, i; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1246 | uint32_t alg_k; |
| 1247 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1248 | int n; |
| 1249 | CERT *cert; |
| 1250 | BIGNUM *r[4]; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1251 | int nr[4]; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1252 | BUF_MEM *buf; |
| 1253 | EVP_MD_CTX md_ctx; |
| 1254 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1255 | if (s->state == SSL3_ST_SW_KEY_EXCH_C) { |
| 1256 | return ssl_do_write(s); |
| 1257 | } |
| 1258 | |
| 1259 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 1260 | if (!ssl_has_private_key(s)) { |
| 1261 | al = SSL_AD_INTERNAL_ERROR; |
| 1262 | goto f_err; |
| 1263 | } |
| 1264 | max_sig_len = ssl_private_key_max_signature_len(s); |
| 1265 | } else { |
| 1266 | max_sig_len = 0; |
| 1267 | } |
| 1268 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1269 | EVP_MD_CTX_init(&md_ctx); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1270 | enum ssl_private_key_result_t sign_result; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1271 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { |
| 1272 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1273 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1274 | cert = s->cert; |
| 1275 | |
| 1276 | buf = s->init_buf; |
| 1277 | |
| 1278 | r[0] = r[1] = r[2] = r[3] = NULL; |
| 1279 | n = 0; |
| 1280 | if (alg_a & SSL_aPSK) { |
| 1281 | /* size for PSK identity hint */ |
| 1282 | psk_identity_hint = s->psk_identity_hint; |
| 1283 | if (psk_identity_hint) { |
| 1284 | psk_identity_hint_len = strlen(psk_identity_hint); |
| 1285 | } else { |
| 1286 | psk_identity_hint_len = 0; |
| 1287 | } |
| 1288 | n += 2 + psk_identity_hint_len; |
| 1289 | } |
| 1290 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1291 | if (alg_k & SSL_kDHE) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1292 | dhp = cert->dh_tmp; |
| 1293 | if (dhp == NULL && s->cert->dh_tmp_cb != NULL) { |
| 1294 | dhp = s->cert->dh_tmp_cb(s, 0, 1024); |
| 1295 | } |
| 1296 | if (dhp == NULL) { |
| 1297 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1298 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1299 | goto f_err; |
| 1300 | } |
| 1301 | |
| 1302 | if (s->s3->tmp.dh != NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1303 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1304 | goto err; |
| 1305 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1306 | dh = DHparams_dup(dhp); |
| 1307 | if (dh == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1308 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1309 | goto err; |
| 1310 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1311 | s->s3->tmp.dh = dh; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1312 | |
| 1313 | if (!DH_generate_key(dh)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1314 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1315 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | r[0] = dh->p; |
| 1319 | r[1] = dh->g; |
| 1320 | r[2] = dh->pub_key; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1321 | } else if (alg_k & SSL_kECDHE) { |
| 1322 | /* Determine the curve to use. */ |
| 1323 | int nid = NID_undef; |
| 1324 | if (cert->ecdh_nid != NID_undef) { |
| 1325 | nid = cert->ecdh_nid; |
| 1326 | } else if (cert->ecdh_tmp_cb != NULL) { |
| 1327 | /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result |
| 1328 | * to the caller. */ |
| 1329 | EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024); |
| 1330 | if (template != NULL && EC_KEY_get0_group(template) != NULL) { |
| 1331 | nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1332 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1333 | } else { |
| 1334 | nid = tls1_get_shared_curve(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1335 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1336 | if (nid == NID_undef) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1337 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1338 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1339 | goto f_err; |
| 1340 | } |
| 1341 | |
| 1342 | if (s->s3->tmp.ecdh != NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1343 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1344 | goto err; |
| 1345 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1346 | ecdh = EC_KEY_new_by_curve_name(nid); |
| 1347 | if (ecdh == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1348 | goto err; |
| 1349 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1350 | s->s3->tmp.ecdh = ecdh; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1351 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1352 | if (!EC_KEY_generate_key(ecdh)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1353 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1354 | goto err; |
| 1355 | } |
| 1356 | |
| 1357 | /* We only support ephemeral ECDH keys over named (not generic) curves. */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1358 | const EC_GROUP *group = EC_KEY_get0_group(ecdh); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1359 | if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1360 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1361 | goto err; |
| 1362 | } |
| 1363 | |
| 1364 | /* Encode the public key. First check the size of encoding and allocate |
| 1365 | * memory accordingly. */ |
| 1366 | encodedlen = |
| 1367 | EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), |
| 1368 | POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); |
| 1369 | |
| 1370 | encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t)); |
| 1371 | bn_ctx = BN_CTX_new(); |
| 1372 | if (encodedPoint == NULL || bn_ctx == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1373 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1374 | goto err; |
| 1375 | } |
| 1376 | |
| 1377 | encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), |
| 1378 | POINT_CONVERSION_UNCOMPRESSED, |
| 1379 | encodedPoint, encodedlen, bn_ctx); |
| 1380 | |
| 1381 | if (encodedlen == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1382 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1383 | goto err; |
| 1384 | } |
| 1385 | |
| 1386 | BN_CTX_free(bn_ctx); |
| 1387 | bn_ctx = NULL; |
| 1388 | |
| 1389 | /* We only support named (not generic) curves in ECDH ephemeral key |
| 1390 | * exchanges. In this situation, we need four additional bytes to encode |
| 1391 | * the entire ServerECDHParams structure. */ |
| 1392 | n += 4 + encodedlen; |
| 1393 | |
| 1394 | /* We'll generate the serverKeyExchange message explicitly so we can set |
| 1395 | * these to NULLs */ |
| 1396 | r[0] = NULL; |
| 1397 | r[1] = NULL; |
| 1398 | r[2] = NULL; |
| 1399 | r[3] = NULL; |
| 1400 | } else if (!(alg_k & SSL_kPSK)) { |
| 1401 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1402 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1403 | goto f_err; |
| 1404 | } |
| 1405 | |
| 1406 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
| 1407 | nr[i] = BN_num_bytes(r[i]); |
| 1408 | n += 2 + nr[i]; |
| 1409 | } |
| 1410 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1411 | if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + max_sig_len)) { |
| 1412 | OPENSSL_PUT_ERROR(SSL, ERR_LIB_BUF); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1413 | goto err; |
| 1414 | } |
| 1415 | d = p = ssl_handshake_start(s); |
| 1416 | |
| 1417 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
| 1418 | s2n(nr[i], p); |
| 1419 | BN_bn2bin(r[i], p); |
| 1420 | p += nr[i]; |
| 1421 | } |
| 1422 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1423 | /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1424 | * them is used, the server key exchange record needs to have both the |
| 1425 | * psk_identity_hint and the ServerECDHParams. */ |
| 1426 | if (alg_a & SSL_aPSK) { |
| 1427 | /* copy PSK identity hint (if provided) */ |
| 1428 | s2n(psk_identity_hint_len, p); |
| 1429 | if (psk_identity_hint_len > 0) { |
| 1430 | memcpy(p, psk_identity_hint, psk_identity_hint_len); |
| 1431 | p += psk_identity_hint_len; |
| 1432 | } |
| 1433 | } |
| 1434 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1435 | if (alg_k & SSL_kECDHE) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1436 | /* We only support named (not generic) curves. In this situation, the |
| 1437 | * serverKeyExchange message has: |
| 1438 | * [1 byte CurveType], [2 byte CurveName] |
| 1439 | * [1 byte length of encoded point], followed by |
| 1440 | * the actual encoded point itself. */ |
| 1441 | *(p++) = NAMED_CURVE_TYPE; |
| 1442 | *(p++) = (uint8_t)(curve_id >> 8); |
| 1443 | *(p++) = (uint8_t)(curve_id & 0xff); |
| 1444 | *(p++) = encodedlen; |
| 1445 | memcpy(p, encodedPoint, encodedlen); |
| 1446 | p += encodedlen; |
| 1447 | OPENSSL_free(encodedPoint); |
| 1448 | encodedPoint = NULL; |
| 1449 | } |
| 1450 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1451 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 1452 | /* n is the length of the params, they start at d and p points to |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1453 | * the space at the end. */ |
| 1454 | const EVP_MD *md; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1455 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1456 | unsigned int digest_length; |
| 1457 | |
| 1458 | const int pkey_type = ssl_private_key_type(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1459 | |
| 1460 | /* Determine signature algorithm. */ |
| 1461 | if (SSL_USE_SIGALGS(s)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1462 | md = tls1_choose_signing_digest(s); |
| 1463 | if (!tls12_get_sigandhash(s, p, md)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1464 | /* Should never happen */ |
| 1465 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1466 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1467 | goto f_err; |
| 1468 | } |
| 1469 | p += 2; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1470 | } else if (pkey_type == EVP_PKEY_RSA) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1471 | md = EVP_md5_sha1(); |
| 1472 | } else { |
| 1473 | md = EVP_sha1(); |
| 1474 | } |
| 1475 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1476 | if (!EVP_DigestInit_ex(&md_ctx, md, NULL) || |
| 1477 | !EVP_DigestUpdate(&md_ctx, s->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1478 | !EVP_DigestUpdate(&md_ctx, s->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1479 | !EVP_DigestUpdate(&md_ctx, d, n) || |
| 1480 | !EVP_DigestFinal_ex(&md_ctx, digest, &digest_length)) { |
| 1481 | OPENSSL_PUT_ERROR(SSL, ERR_LIB_EVP); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1482 | goto err; |
| 1483 | } |
| 1484 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1485 | sign_result = ssl_private_key_sign(s, &p[2], &sig_len, max_sig_len, |
| 1486 | EVP_MD_CTX_md(&md_ctx), digest, |
| 1487 | digest_length); |
| 1488 | } else { |
| 1489 | /* This key exchange doesn't involve a signature. */ |
| 1490 | sign_result = ssl_private_key_success; |
| 1491 | sig_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1492 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1493 | } else { |
| 1494 | assert(s->state == SSL3_ST_SW_KEY_EXCH_B); |
| 1495 | /* Restore |p|. */ |
| 1496 | p = ssl_handshake_start(s) + s->init_num - SSL_HM_HEADER_LENGTH(s); |
| 1497 | sign_result = ssl_private_key_sign_complete(s, &p[2], &sig_len, |
| 1498 | max_sig_len); |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1501 | switch (sign_result) { |
| 1502 | case ssl_private_key_success: |
| 1503 | s->rwstate = SSL_NOTHING; |
| 1504 | break; |
| 1505 | case ssl_private_key_failure: |
| 1506 | s->rwstate = SSL_NOTHING; |
| 1507 | goto err; |
| 1508 | case ssl_private_key_retry: |
| 1509 | s->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1510 | /* Stash away |p|. */ |
| 1511 | s->init_num = p - ssl_handshake_start(s) + SSL_HM_HEADER_LENGTH(s); |
| 1512 | s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1513 | goto err; |
| 1514 | } |
| 1515 | |
| 1516 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 1517 | s2n(sig_len, p); |
| 1518 | p += sig_len; |
| 1519 | } |
| 1520 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, |
| 1521 | p - ssl_handshake_start(s))) { |
| 1522 | goto err; |
| 1523 | } |
| 1524 | s->state = SSL3_ST_SW_KEY_EXCH_C; |
| 1525 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1526 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1527 | return ssl_do_write(s); |
| 1528 | |
| 1529 | f_err: |
| 1530 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1531 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1532 | OPENSSL_free(encodedPoint); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1533 | BN_CTX_free(bn_ctx); |
| 1534 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1535 | return -1; |
| 1536 | } |
| 1537 | |
| 1538 | int ssl3_send_certificate_request(SSL *s) { |
| 1539 | uint8_t *p, *d; |
| 1540 | size_t i; |
| 1541 | int j, nl, off, n; |
| 1542 | STACK_OF(X509_NAME) *sk = NULL; |
| 1543 | X509_NAME *name; |
| 1544 | BUF_MEM *buf; |
| 1545 | |
| 1546 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { |
| 1547 | buf = s->init_buf; |
| 1548 | |
| 1549 | d = p = ssl_handshake_start(s); |
| 1550 | |
| 1551 | /* get the list of acceptable cert types */ |
| 1552 | p++; |
| 1553 | n = ssl3_get_req_cert_type(s, p); |
| 1554 | d[0] = n; |
| 1555 | p += n; |
| 1556 | n++; |
| 1557 | |
| 1558 | if (SSL_USE_SIGALGS(s)) { |
| 1559 | const uint8_t *psigs; |
| 1560 | nl = tls12_get_psigalgs(s, &psigs); |
| 1561 | s2n(nl, p); |
| 1562 | memcpy(p, psigs, nl); |
| 1563 | p += nl; |
| 1564 | n += nl + 2; |
| 1565 | } |
| 1566 | |
| 1567 | off = n; |
| 1568 | p += 2; |
| 1569 | n += 2; |
| 1570 | |
| 1571 | sk = SSL_get_client_CA_list(s); |
| 1572 | nl = 0; |
| 1573 | if (sk != NULL) { |
| 1574 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 1575 | name = sk_X509_NAME_value(sk, i); |
| 1576 | j = i2d_X509_NAME(name, NULL); |
| 1577 | if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1578 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1579 | goto err; |
| 1580 | } |
| 1581 | p = ssl_handshake_start(s) + n; |
| 1582 | s2n(j, p); |
| 1583 | i2d_X509_NAME(name, &p); |
| 1584 | n += 2 + j; |
| 1585 | nl += 2 + j; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | /* else no CA names */ |
| 1590 | p = ssl_handshake_start(s) + off; |
| 1591 | s2n(nl, p); |
| 1592 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1593 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) { |
| 1594 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1595 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1596 | s->state = SSL3_ST_SW_CERT_REQ_B; |
| 1597 | } |
| 1598 | |
| 1599 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 1600 | return ssl_do_write(s); |
| 1601 | |
| 1602 | err: |
| 1603 | return -1; |
| 1604 | } |
| 1605 | |
| 1606 | int ssl3_get_client_key_exchange(SSL *s) { |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1607 | int al, ok; |
| 1608 | long n; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1609 | CBS client_key_exchange; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1610 | uint32_t alg_k; |
| 1611 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1612 | uint8_t *premaster_secret = NULL; |
| 1613 | size_t premaster_secret_len = 0; |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1614 | RSA *rsa = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1615 | uint8_t *decrypt_buf = NULL; |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1616 | EVP_PKEY *pkey = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1617 | BIGNUM *pub = NULL; |
| 1618 | DH *dh_srvr; |
| 1619 | |
| 1620 | EC_KEY *srvr_ecdh = NULL; |
| 1621 | EVP_PKEY *clnt_pub_pkey = NULL; |
| 1622 | EC_POINT *clnt_ecpoint = NULL; |
| 1623 | BN_CTX *bn_ctx = NULL; |
| 1624 | unsigned int psk_len = 0; |
| 1625 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1626 | |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1627 | n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A, |
| 1628 | SSL3_ST_SR_KEY_EXCH_B, |
| 1629 | SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */ |
| 1630 | ssl_hash_message, &ok); |
| 1631 | |
| 1632 | if (!ok) { |
| 1633 | return n; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1636 | CBS_init(&client_key_exchange, s->init_msg, n); |
| 1637 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1638 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1639 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1640 | |
| 1641 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1642 | if (alg_a & SSL_aPSK) { |
| 1643 | CBS psk_identity; |
| 1644 | |
| 1645 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1646 | * then this is the only field in the message. */ |
| 1647 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1648 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1649 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1650 | al = SSL_AD_DECODE_ERROR; |
| 1651 | goto f_err; |
| 1652 | } |
| 1653 | |
| 1654 | if (s->psk_server_callback == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1655 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1656 | al = SSL_AD_INTERNAL_ERROR; |
| 1657 | goto f_err; |
| 1658 | } |
| 1659 | |
| 1660 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1661 | CBS_contains_zero_byte(&psk_identity)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1662 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1663 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1664 | goto f_err; |
| 1665 | } |
| 1666 | |
| 1667 | if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) { |
| 1668 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1669 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1670 | goto f_err; |
| 1671 | } |
| 1672 | |
| 1673 | /* Look up the key for the identity. */ |
| 1674 | psk_len = |
| 1675 | s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk)); |
| 1676 | if (psk_len > PSK_MAX_PSK_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1677 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1678 | al = SSL_AD_INTERNAL_ERROR; |
| 1679 | goto f_err; |
| 1680 | } else if (psk_len == 0) { |
| 1681 | /* PSK related to the given identity not found */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1682 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1683 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1684 | goto f_err; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1689 | * |premaster_secret_len|. */ |
| 1690 | if (alg_k & SSL_kRSA) { |
| 1691 | CBS encrypted_premaster_secret; |
| 1692 | uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
| 1693 | uint8_t good; |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1694 | size_t rsa_size, decrypt_len, premaster_index, j; |
| 1695 | |
| 1696 | pkey = s->cert->privatekey; |
| 1697 | if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) { |
| 1698 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1699 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1700 | goto f_err; |
| 1701 | } |
| 1702 | rsa = pkey->pkey.rsa; |
| 1703 | |
| 1704 | /* TLS and [incidentally] DTLS{0xFEFF} */ |
| 1705 | if (s->version > SSL3_VERSION) { |
| 1706 | CBS copy = client_key_exchange; |
| 1707 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1708 | &encrypted_premaster_secret) || |
| 1709 | CBS_len(&client_key_exchange) != 0) { |
| 1710 | if (!(s->options & SSL_OP_TLS_D5_BUG)) { |
| 1711 | al = SSL_AD_DECODE_ERROR; |
| 1712 | OPENSSL_PUT_ERROR(SSL, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1713 | goto f_err; |
| 1714 | } else { |
| 1715 | encrypted_premaster_secret = copy; |
| 1716 | } |
| 1717 | } |
| 1718 | } else { |
| 1719 | encrypted_premaster_secret = client_key_exchange; |
| 1720 | } |
| 1721 | |
| 1722 | /* Reject overly short RSA keys because we want to be sure that the buffer |
| 1723 | * size makes it safe to iterate over the entire size of a premaster secret |
| 1724 | * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to |
| 1725 | * RSA padding, but the bound is sufficient to be safe. */ |
| 1726 | rsa_size = RSA_size(rsa); |
| 1727 | if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) { |
| 1728 | al = SSL_AD_DECRYPT_ERROR; |
| 1729 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1730 | goto f_err; |
| 1731 | } |
| 1732 | |
| 1733 | /* We must not leak whether a decryption failure occurs because of |
| 1734 | * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, |
| 1735 | * section 7.4.7.1). The code follows that advice of the TLS RFC and |
| 1736 | * generates a random premaster secret for the case that the decrypt fails. |
| 1737 | * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */ |
| 1738 | if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) { |
| 1739 | goto err; |
| 1740 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1741 | |
| 1742 | /* Allocate a buffer large enough for an RSA decryption. */ |
| 1743 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1744 | if (decrypt_buf == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1745 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1746 | goto err; |
| 1747 | } |
| 1748 | |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1749 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1750 | * timing-sensitive code below. */ |
| 1751 | if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size, |
| 1752 | CBS_data(&encrypted_premaster_secret), |
| 1753 | CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) { |
| 1754 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1755 | } |
| 1756 | if (decrypt_len != rsa_size) { |
| 1757 | /* This should never happen, but do a check so we do not read |
| 1758 | * uninitialized memory. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1759 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1760 | goto err; |
| 1761 | } |
| 1762 | |
| 1763 | /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate. |
| 1764 | * |good| will be 0xff if the premaster is acceptable and zero otherwise. |
| 1765 | * */ |
| 1766 | good = |
| 1767 | constant_time_eq_int_8(RSA_message_index_PKCS1_type_2( |
| 1768 | decrypt_buf, decrypt_len, &premaster_index), |
| 1769 | 1); |
| 1770 | decrypt_len = decrypt_len - premaster_index; |
| 1771 | |
| 1772 | /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */ |
| 1773 | good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH); |
| 1774 | |
| 1775 | /* Copy over the unpadded premaster. Whatever the value of |
| 1776 | * |decrypt_good_mask|, copy as if the premaster were the right length. It |
| 1777 | * is important the memory access pattern be constant. */ |
| 1778 | premaster_secret = |
| 1779 | BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH), |
| 1780 | SSL_MAX_MASTER_KEY_LENGTH); |
| 1781 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1782 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1783 | goto err; |
| 1784 | } |
| 1785 | OPENSSL_free(decrypt_buf); |
| 1786 | decrypt_buf = NULL; |
| 1787 | |
| 1788 | /* If the version in the decrypted pre-master secret is correct then |
| 1789 | * version_good will be 0xff, otherwise it'll be zero. The |
| 1790 | * Klima-Pokorny-Rosa extension of Bleichenbacher's attack |
| 1791 | * (http://eprint.iacr.org/2003/052/) exploits the version number check as |
| 1792 | * a "bad version oracle". Thus version checks are done in constant time |
| 1793 | * and are treated like any other decryption error. */ |
| 1794 | good &= constant_time_eq_8(premaster_secret[0], |
| 1795 | (unsigned)(s->client_version >> 8)); |
| 1796 | good &= constant_time_eq_8(premaster_secret[1], |
| 1797 | (unsigned)(s->client_version & 0xff)); |
| 1798 | |
| 1799 | /* Now copy rand_premaster_secret over premaster_secret using |
| 1800 | * decrypt_good_mask. */ |
| 1801 | for (j = 0; j < sizeof(rand_premaster_secret); j++) { |
| 1802 | premaster_secret[j] = constant_time_select_8(good, premaster_secret[j], |
| 1803 | rand_premaster_secret[j]); |
| 1804 | } |
| 1805 | |
| 1806 | premaster_secret_len = sizeof(rand_premaster_secret); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1807 | } else if (alg_k & SSL_kDHE) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1808 | CBS dh_Yc; |
| 1809 | int dh_len; |
| 1810 | |
| 1811 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) || |
| 1812 | CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1813 | OPENSSL_PUT_ERROR(SSL, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1814 | al = SSL_R_DECODE_ERROR; |
| 1815 | goto f_err; |
| 1816 | } |
| 1817 | |
| 1818 | if (s->s3->tmp.dh == NULL) { |
| 1819 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1820 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1821 | goto f_err; |
| 1822 | } |
| 1823 | dh_srvr = s->s3->tmp.dh; |
| 1824 | |
| 1825 | pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL); |
| 1826 | if (pub == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1827 | OPENSSL_PUT_ERROR(SSL, SSL_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1828 | goto err; |
| 1829 | } |
| 1830 | |
| 1831 | /* Allocate a buffer for the premaster secret. */ |
| 1832 | premaster_secret = OPENSSL_malloc(DH_size(dh_srvr)); |
| 1833 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1834 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1835 | BN_clear_free(pub); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1836 | goto err; |
| 1837 | } |
| 1838 | |
| 1839 | dh_len = DH_compute_key(premaster_secret, pub, dh_srvr); |
| 1840 | if (dh_len <= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1841 | OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1842 | BN_clear_free(pub); |
| 1843 | goto err; |
| 1844 | } |
| 1845 | |
| 1846 | DH_free(s->s3->tmp.dh); |
| 1847 | s->s3->tmp.dh = NULL; |
| 1848 | BN_clear_free(pub); |
| 1849 | pub = NULL; |
| 1850 | |
| 1851 | premaster_secret_len = dh_len; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1852 | } else if (alg_k & SSL_kECDHE) { |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1853 | int field_size = 0, ecdh_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1854 | const EC_KEY *tkey; |
| 1855 | const EC_GROUP *group; |
| 1856 | const BIGNUM *priv_key; |
| 1857 | CBS ecdh_Yc; |
| 1858 | |
| 1859 | /* initialize structures for server's ECDH key pair */ |
| 1860 | srvr_ecdh = EC_KEY_new(); |
| 1861 | if (srvr_ecdh == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1862 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1863 | goto err; |
| 1864 | } |
| 1865 | |
| 1866 | /* Use the ephermeral values we saved when generating the ServerKeyExchange |
| 1867 | * msg. */ |
| 1868 | tkey = s->s3->tmp.ecdh; |
| 1869 | |
| 1870 | group = EC_KEY_get0_group(tkey); |
| 1871 | priv_key = EC_KEY_get0_private_key(tkey); |
| 1872 | |
| 1873 | if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 1874 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1875 | OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1876 | goto err; |
| 1877 | } |
| 1878 | |
| 1879 | /* Let's get client's public key */ |
| 1880 | clnt_ecpoint = EC_POINT_new(group); |
| 1881 | if (clnt_ecpoint == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1882 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1883 | goto err; |
| 1884 | } |
| 1885 | |
| 1886 | /* Get client's public key from encoded point in the ClientKeyExchange |
| 1887 | * message. */ |
| 1888 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) || |
| 1889 | CBS_len(&client_key_exchange) != 0) { |
| 1890 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1891 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1892 | goto f_err; |
| 1893 | } |
| 1894 | |
| 1895 | bn_ctx = BN_CTX_new(); |
| 1896 | if (bn_ctx == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1897 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1898 | goto err; |
| 1899 | } |
| 1900 | |
| 1901 | if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc), |
| 1902 | CBS_len(&ecdh_Yc), bn_ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1903 | OPENSSL_PUT_ERROR(SSL, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1904 | goto err; |
| 1905 | } |
| 1906 | |
| 1907 | /* Allocate a buffer for both the secret and the PSK. */ |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 1908 | field_size = EC_GROUP_get_degree(group); |
| 1909 | if (field_size <= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1910 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1911 | goto err; |
| 1912 | } |
| 1913 | |
| 1914 | ecdh_len = (field_size + 7) / 8; |
| 1915 | premaster_secret = OPENSSL_malloc(ecdh_len); |
| 1916 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1917 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1918 | goto err; |
| 1919 | } |
| 1920 | |
| 1921 | /* Compute the shared pre-master secret */ |
| 1922 | ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint, |
| 1923 | srvr_ecdh, NULL); |
| 1924 | if (ecdh_len <= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1925 | OPENSSL_PUT_ERROR(SSL, ERR_R_ECDH_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1926 | goto err; |
| 1927 | } |
| 1928 | |
| 1929 | EVP_PKEY_free(clnt_pub_pkey); |
| 1930 | clnt_pub_pkey = NULL; |
| 1931 | EC_POINT_free(clnt_ecpoint); |
| 1932 | clnt_ecpoint = NULL; |
| 1933 | EC_KEY_free(srvr_ecdh); |
| 1934 | srvr_ecdh = NULL; |
| 1935 | BN_CTX_free(bn_ctx); |
| 1936 | bn_ctx = NULL; |
| 1937 | EC_KEY_free(s->s3->tmp.ecdh); |
| 1938 | s->s3->tmp.ecdh = NULL; |
| 1939 | |
| 1940 | premaster_secret_len = ecdh_len; |
| 1941 | } else if (alg_k & SSL_kPSK) { |
| 1942 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1943 | * pre-shared key. */ |
| 1944 | premaster_secret_len = psk_len; |
| 1945 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1946 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1947 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1948 | goto err; |
| 1949 | } |
| 1950 | memset(premaster_secret, 0, premaster_secret_len); |
| 1951 | } else { |
| 1952 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1953 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1954 | goto f_err; |
| 1955 | } |
| 1956 | |
| 1957 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1958 | * pre-shared key. */ |
| 1959 | if (alg_a & SSL_aPSK) { |
| 1960 | CBB new_premaster, child; |
| 1961 | uint8_t *new_data; |
| 1962 | size_t new_len; |
| 1963 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1964 | CBB_zero(&new_premaster); |
| 1965 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1966 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1967 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1968 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1969 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1970 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1971 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1972 | CBB_cleanup(&new_premaster); |
| 1973 | goto err; |
| 1974 | } |
| 1975 | |
| 1976 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1977 | OPENSSL_free(premaster_secret); |
| 1978 | premaster_secret = new_data; |
| 1979 | premaster_secret_len = new_len; |
| 1980 | } |
| 1981 | |
| 1982 | /* Compute the master secret */ |
| 1983 | s->session->master_key_length = s->enc_method->generate_master_secret( |
| 1984 | s, s->session->master_key, premaster_secret, premaster_secret_len); |
| 1985 | if (s->session->master_key_length == 0) { |
| 1986 | goto err; |
| 1987 | } |
| 1988 | s->session->extended_master_secret = s->s3->tmp.extended_master_secret; |
| 1989 | |
| 1990 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1991 | OPENSSL_free(premaster_secret); |
| 1992 | return 1; |
| 1993 | |
| 1994 | f_err: |
| 1995 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1996 | err: |
| 1997 | if (premaster_secret) { |
| 1998 | if (premaster_secret_len) { |
| 1999 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2000 | } |
| 2001 | OPENSSL_free(premaster_secret); |
| 2002 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2003 | OPENSSL_free(decrypt_buf); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2004 | EVP_PKEY_free(clnt_pub_pkey); |
| 2005 | EC_POINT_free(clnt_ecpoint); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2006 | EC_KEY_free(srvr_ecdh); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2007 | BN_CTX_free(bn_ctx); |
| 2008 | |
| 2009 | return -1; |
| 2010 | } |
| 2011 | |
| 2012 | int ssl3_get_cert_verify(SSL *s) { |
| 2013 | int al, ok, ret = 0; |
| 2014 | long n; |
| 2015 | CBS certificate_verify, signature; |
| 2016 | X509 *peer = s->session->peer; |
| 2017 | EVP_PKEY *pkey = NULL; |
| 2018 | const EVP_MD *md = NULL; |
| 2019 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 2020 | size_t digest_length; |
| 2021 | EVP_PKEY_CTX *pctx = NULL; |
| 2022 | |
| 2023 | /* Only RSA and ECDSA client certificates are supported, so a |
| 2024 | * CertificateVerify is required if and only if there's a client certificate. |
| 2025 | * */ |
| 2026 | if (peer == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2027 | ssl3_free_handshake_buffer(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2028 | return 1; |
| 2029 | } |
| 2030 | |
| 2031 | n = s->method->ssl_get_message( |
| 2032 | s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B, |
| 2033 | SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH, |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2034 | ssl_dont_hash_message, &ok); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2035 | |
| 2036 | if (!ok) { |
| 2037 | return n; |
| 2038 | } |
| 2039 | |
| 2040 | /* Filter out unsupported certificate types. */ |
| 2041 | pkey = X509_get_pubkey(peer); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2042 | if (pkey == NULL) { |
| 2043 | goto err; |
| 2044 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2045 | if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) || |
| 2046 | (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) { |
| 2047 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2048 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2049 | goto f_err; |
| 2050 | } |
| 2051 | |
| 2052 | CBS_init(&certificate_verify, s->init_msg, n); |
| 2053 | |
| 2054 | /* Determine the digest type if needbe. */ |
| 2055 | if (SSL_USE_SIGALGS(s) && |
| 2056 | !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) { |
| 2057 | goto f_err; |
| 2058 | } |
| 2059 | |
| 2060 | /* Compute the digest. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2061 | if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey->type)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2062 | goto err; |
| 2063 | } |
| 2064 | |
| 2065 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 2066 | * message.*/ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2067 | ssl3_free_handshake_buffer(s); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2068 | if (!ssl3_hash_current_message(s)) { |
| 2069 | goto err; |
| 2070 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2071 | |
| 2072 | /* Parse and verify the signature. */ |
| 2073 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 2074 | CBS_len(&certificate_verify) != 0) { |
| 2075 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2076 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2077 | goto f_err; |
| 2078 | } |
| 2079 | |
| 2080 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 2081 | if (pctx == NULL) { |
| 2082 | goto err; |
| 2083 | } |
| 2084 | if (!EVP_PKEY_verify_init(pctx) || |
| 2085 | !EVP_PKEY_CTX_set_signature_md(pctx, md) || |
| 2086 | !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest, |
| 2087 | digest_length)) { |
| 2088 | al = SSL_AD_DECRYPT_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2089 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2090 | goto f_err; |
| 2091 | } |
| 2092 | |
| 2093 | ret = 1; |
| 2094 | |
| 2095 | if (0) { |
| 2096 | f_err: |
| 2097 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2098 | } |
| 2099 | |
| 2100 | err: |
| 2101 | EVP_PKEY_CTX_free(pctx); |
| 2102 | EVP_PKEY_free(pkey); |
| 2103 | |
| 2104 | return ret; |
| 2105 | } |
| 2106 | |
| 2107 | int ssl3_get_client_certificate(SSL *s) { |
| 2108 | int i, ok, al, ret = -1; |
| 2109 | X509 *x = NULL; |
| 2110 | unsigned long n; |
| 2111 | STACK_OF(X509) *sk = NULL; |
| 2112 | SHA256_CTX sha256; |
| 2113 | CBS certificate_msg, certificate_list; |
| 2114 | int is_first_certificate = 1; |
| 2115 | |
| 2116 | n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1, |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2117 | (long)s->max_cert_list, ssl_hash_message, &ok); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2118 | |
| 2119 | if (!ok) { |
| 2120 | return n; |
| 2121 | } |
| 2122 | |
| 2123 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
| 2124 | if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2125 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2126 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2127 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2128 | goto f_err; |
| 2129 | } |
| 2130 | |
| 2131 | /* If tls asked for a client cert, the client must return a 0 list */ |
| 2132 | if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2133 | OPENSSL_PUT_ERROR(SSL, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2134 | SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2135 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 2136 | goto f_err; |
| 2137 | } |
| 2138 | s->s3->tmp.reuse_message = 1; |
| 2139 | |
| 2140 | return 1; |
| 2141 | } |
| 2142 | |
| 2143 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 2144 | al = SSL_AD_UNEXPECTED_MESSAGE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2145 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_MESSAGE_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2146 | goto f_err; |
| 2147 | } |
| 2148 | |
| 2149 | CBS_init(&certificate_msg, s->init_msg, n); |
| 2150 | |
| 2151 | sk = sk_X509_new_null(); |
| 2152 | if (sk == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2153 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2154 | goto err; |
| 2155 | } |
| 2156 | |
| 2157 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 2158 | CBS_len(&certificate_msg) != 0) { |
| 2159 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2160 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2161 | goto f_err; |
| 2162 | } |
| 2163 | |
| 2164 | while (CBS_len(&certificate_list) > 0) { |
| 2165 | CBS certificate; |
| 2166 | const uint8_t *data; |
| 2167 | |
| 2168 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 2169 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2170 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2171 | goto f_err; |
| 2172 | } |
| 2173 | |
| 2174 | if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) { |
| 2175 | /* If this is the first certificate, and we don't want to keep peer |
| 2176 | * certificates in memory, then we hash it right away. */ |
| 2177 | SHA256_Init(&sha256); |
| 2178 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
| 2179 | SHA256_Final(s->session->peer_sha256, &sha256); |
| 2180 | s->session->peer_sha256_valid = 1; |
| 2181 | } |
| 2182 | is_first_certificate = 0; |
| 2183 | |
| 2184 | data = CBS_data(&certificate); |
| 2185 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
| 2186 | if (x == NULL) { |
| 2187 | al = SSL_AD_BAD_CERTIFICATE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2188 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2189 | goto f_err; |
| 2190 | } |
| 2191 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 2192 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2193 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2194 | goto f_err; |
| 2195 | } |
| 2196 | if (!sk_X509_push(sk, x)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2197 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2198 | goto err; |
| 2199 | } |
| 2200 | x = NULL; |
| 2201 | } |
| 2202 | |
| 2203 | if (sk_X509_num(sk) <= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2204 | /* No client certificate so the handshake buffer may be discarded. */ |
| 2205 | ssl3_free_handshake_buffer(s); |
| 2206 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2207 | /* TLS does not mind 0 certs returned */ |
| 2208 | if (s->version == SSL3_VERSION) { |
| 2209 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2210 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2211 | goto f_err; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2212 | } else if ((s->verify_mode & SSL_VERIFY_PEER) && |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2213 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2214 | /* Fail for TLS only if we required a certificate */ |
| 2215 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2216 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2217 | goto f_err; |
| 2218 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2219 | } else { |
| 2220 | i = ssl_verify_cert_chain(s, sk); |
| 2221 | if (i <= 0) { |
| 2222 | al = ssl_verify_alarm_type(s->verify_result); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2223 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2224 | goto f_err; |
| 2225 | } |
| 2226 | } |
| 2227 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2228 | X509_free(s->session->peer); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2229 | s->session->peer = sk_X509_shift(sk); |
| 2230 | s->session->verify_result = s->verify_result; |
| 2231 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2232 | sk_X509_pop_free(s->session->cert_chain, X509_free); |
| 2233 | s->session->cert_chain = sk; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2234 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 2235 | * certificate, while we do include it in s3_clnt.c */ |
| 2236 | |
| 2237 | sk = NULL; |
| 2238 | |
| 2239 | ret = 1; |
| 2240 | |
| 2241 | if (0) { |
| 2242 | f_err: |
| 2243 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2244 | } |
| 2245 | |
| 2246 | err: |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2247 | X509_free(x); |
| 2248 | sk_X509_pop_free(sk, X509_free); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2249 | return ret; |
| 2250 | } |
| 2251 | |
| 2252 | int ssl3_send_server_certificate(SSL *s) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2253 | if (s->state == SSL3_ST_SW_CERT_A) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2254 | if (!ssl3_output_cert_chain(s)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2255 | return 0; |
| 2256 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2257 | s->state = SSL3_ST_SW_CERT_B; |
| 2258 | } |
| 2259 | |
| 2260 | /* SSL3_ST_SW_CERT_B */ |
| 2261 | return ssl_do_write(s); |
| 2262 | } |
| 2263 | |
| 2264 | /* send a new session ticket (not necessarily for a new session) */ |
| 2265 | int ssl3_send_new_session_ticket(SSL *s) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2266 | int ret = -1; |
| 2267 | uint8_t *session = NULL; |
| 2268 | size_t session_len; |
| 2269 | EVP_CIPHER_CTX ctx; |
| 2270 | HMAC_CTX hctx; |
| 2271 | |
| 2272 | EVP_CIPHER_CTX_init(&ctx); |
| 2273 | HMAC_CTX_init(&hctx); |
| 2274 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2275 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2276 | uint8_t *p, *macstart; |
| 2277 | int len; |
| 2278 | unsigned int hlen; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2279 | SSL_CTX *tctx = s->initial_ctx; |
| 2280 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 2281 | uint8_t key_name[16]; |
| 2282 | /* The maximum overhead of encrypting the session is 16 (key name) + IV + |
| 2283 | * one block of encryption overhead + HMAC. */ |
| 2284 | const size_t max_ticket_overhead = |
| 2285 | 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
| 2286 | |
| 2287 | /* Serialize the SSL_SESSION to be encoded into the ticket. */ |
| 2288 | if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2289 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | /* If the session is too long, emit a dummy value rather than abort the |
| 2293 | * connection. */ |
| 2294 | if (session_len > 0xFFFF - max_ticket_overhead) { |
| 2295 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
| 2296 | const size_t placeholder_len = strlen(kTicketPlaceholder); |
| 2297 | |
| 2298 | OPENSSL_free(session); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2299 | session = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2300 | |
| 2301 | p = ssl_handshake_start(s); |
| 2302 | /* Emit ticket_lifetime_hint. */ |
| 2303 | l2n(0, p); |
| 2304 | /* Emit ticket. */ |
| 2305 | s2n(placeholder_len, p); |
| 2306 | memcpy(p, kTicketPlaceholder, placeholder_len); |
| 2307 | p += placeholder_len; |
| 2308 | |
| 2309 | len = p - ssl_handshake_start(s); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2310 | if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) { |
| 2311 | goto err; |
| 2312 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2313 | s->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 2314 | return ssl_do_write(s); |
| 2315 | } |
| 2316 | |
| 2317 | /* Grow buffer if need be: the length calculation is as follows: |
| 2318 | * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) + |
| 2319 | * max_ticket_overhead + * session_length */ |
| 2320 | if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 + |
| 2321 | max_ticket_overhead + session_len)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2322 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2323 | } |
| 2324 | p = ssl_handshake_start(s); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2325 | /* Initialize HMAC and cipher contexts. If callback present it does all the |
| 2326 | * work otherwise use generated values from parent ctx. */ |
| 2327 | if (tctx->tlsext_ticket_key_cb) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2328 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, |
| 2329 | 1 /* encrypt */) < 0) { |
| 2330 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2331 | } |
| 2332 | } else { |
| 2333 | if (!RAND_bytes(iv, 16) || |
| 2334 | !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2335 | tctx->tlsext_tick_aes_key, iv) || |
| 2336 | !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), |
| 2337 | NULL)) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2338 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2339 | } |
| 2340 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 2341 | } |
| 2342 | |
| 2343 | /* Ticket lifetime hint (advisory only): We leave this unspecified for |
| 2344 | * resumed session (for simplicity), and guess that tickets for new |
| 2345 | * sessions will live as long as their sessions. */ |
| 2346 | l2n(s->hit ? 0 : s->session->timeout, p); |
| 2347 | |
| 2348 | /* Skip ticket length for now */ |
| 2349 | p += 2; |
| 2350 | /* Output key name */ |
| 2351 | macstart = p; |
| 2352 | memcpy(p, key_name, 16); |
| 2353 | p += 16; |
| 2354 | /* output IV */ |
| 2355 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 2356 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 2357 | /* Encrypt session data */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2358 | if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) { |
| 2359 | goto err; |
| 2360 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2361 | p += len; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2362 | if (!EVP_EncryptFinal_ex(&ctx, p, &len)) { |
| 2363 | goto err; |
| 2364 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2365 | p += len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2366 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2367 | if (!HMAC_Update(&hctx, macstart, p - macstart) || |
| 2368 | !HMAC_Final(&hctx, p, &hlen)) { |
| 2369 | goto err; |
| 2370 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2371 | |
| 2372 | p += hlen; |
| 2373 | /* Now write out lengths: p points to end of data written */ |
| 2374 | /* Total length */ |
| 2375 | len = p - ssl_handshake_start(s); |
| 2376 | /* Skip ticket lifetime hint */ |
| 2377 | p = ssl_handshake_start(s) + 4; |
| 2378 | s2n(len - 6, p); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2379 | if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) { |
| 2380 | goto err; |
| 2381 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2382 | s->state = SSL3_ST_SW_SESSION_TICKET_B; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2386 | ret = ssl_do_write(s); |
| 2387 | |
| 2388 | err: |
| 2389 | OPENSSL_free(session); |
| 2390 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2391 | HMAC_CTX_cleanup(&hctx); |
| 2392 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 2396 | * sets the next_proto member in s if found */ |
| 2397 | int ssl3_get_next_proto(SSL *s) { |
| 2398 | int ok; |
| 2399 | long n; |
| 2400 | CBS next_protocol, selected_protocol, padding; |
| 2401 | |
| 2402 | /* Clients cannot send a NextProtocol message if we didn't see the extension |
| 2403 | * in their ClientHello */ |
| 2404 | if (!s->s3->next_proto_neg_seen) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2405 | OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2406 | return -1; |
| 2407 | } |
| 2408 | |
| 2409 | n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A, |
| 2410 | SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO, |
| 2411 | 514, /* See the payload format below */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2412 | ssl_hash_message, &ok); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2413 | |
| 2414 | if (!ok) { |
| 2415 | return n; |
| 2416 | } |
| 2417 | |
| 2418 | /* s->state doesn't reflect whether ChangeCipherSpec has been received in |
| 2419 | * this handshake, but s->s3->change_cipher_spec does (will be reset by |
| 2420 | * ssl3_get_finished). |
| 2421 | * |
| 2422 | * TODO(davidben): Is this check now redundant with |
| 2423 | * SSL3_FLAGS_EXPECT_CCS? */ |
| 2424 | if (!s->s3->change_cipher_spec) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2425 | OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2426 | return -1; |
| 2427 | } |
| 2428 | |
| 2429 | CBS_init(&next_protocol, s->init_msg, n); |
| 2430 | |
| 2431 | /* The payload looks like: |
| 2432 | * uint8 proto_len; |
| 2433 | * uint8 proto[proto_len]; |
| 2434 | * uint8 padding_len; |
| 2435 | * uint8 padding[padding_len]; */ |
| 2436 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 2437 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 2438 | CBS_len(&next_protocol) != 0 || |
| 2439 | !CBS_stow(&selected_protocol, &s->next_proto_negotiated, |
| 2440 | &s->next_proto_negotiated_len)) { |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | return 1; |
| 2445 | } |
| 2446 | |
| 2447 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
| 2448 | int ssl3_get_channel_id(SSL *s) { |
| 2449 | int ret = -1, ok; |
| 2450 | long n; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2451 | uint8_t channel_id_hash[EVP_MAX_MD_SIZE]; |
| 2452 | size_t channel_id_hash_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2453 | const uint8_t *p; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2454 | uint16_t extension_type; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2455 | EC_GROUP *p256 = NULL; |
| 2456 | EC_KEY *key = NULL; |
| 2457 | EC_POINT *point = NULL; |
| 2458 | ECDSA_SIG sig; |
| 2459 | BIGNUM x, y; |
| 2460 | CBS encrypted_extensions, extension; |
| 2461 | |
| 2462 | n = s->method->ssl_get_message( |
| 2463 | s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B, |
| 2464 | SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2465 | ssl_dont_hash_message, &ok); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2466 | |
| 2467 | if (!ok) { |
| 2468 | return n; |
| 2469 | } |
| 2470 | |
| 2471 | /* Before incorporating the EncryptedExtensions message to the handshake |
| 2472 | * hash, compute the hash that should have been signed. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2473 | if (!tls1_channel_id_hash(s, channel_id_hash, &channel_id_hash_len)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2474 | return -1; |
| 2475 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2476 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
| 2477 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2478 | if (!ssl3_hash_current_message(s)) { |
| 2479 | return -1; |
| 2480 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2481 | |
| 2482 | /* s->state doesn't reflect whether ChangeCipherSpec has been received in |
| 2483 | * this handshake, but s->s3->change_cipher_spec does (will be reset by |
| 2484 | * ssl3_get_finished). |
| 2485 | * |
| 2486 | * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */ |
| 2487 | if (!s->s3->change_cipher_spec) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2488 | OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2489 | return -1; |
| 2490 | } |
| 2491 | |
| 2492 | CBS_init(&encrypted_extensions, s->init_msg, n); |
| 2493 | |
| 2494 | /* EncryptedExtensions could include multiple extensions, but the only |
| 2495 | * extension that could be negotiated is ChannelID, so there can only be one |
| 2496 | * entry. |
| 2497 | * |
| 2498 | * The payload looks like: |
| 2499 | * uint16 extension_type |
| 2500 | * uint16 extension_len; |
| 2501 | * uint8 x[32]; |
| 2502 | * uint8 y[32]; |
| 2503 | * uint8 r[32]; |
| 2504 | * uint8 s[32]; */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2505 | |
| 2506 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 2507 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 2508 | CBS_len(&encrypted_extensions) != 0 || |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2509 | extension_type != TLSEXT_TYPE_channel_id || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2510 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2511 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2512 | return -1; |
| 2513 | } |
| 2514 | |
| 2515 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2516 | if (!p256) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2517 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2518 | return -1; |
| 2519 | } |
| 2520 | |
| 2521 | BN_init(&x); |
| 2522 | BN_init(&y); |
| 2523 | sig.r = BN_new(); |
| 2524 | sig.s = BN_new(); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2525 | if (sig.r == NULL || sig.s == NULL) { |
| 2526 | goto err; |
| 2527 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2528 | |
| 2529 | p = CBS_data(&extension); |
| 2530 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 2531 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 2532 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 2533 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 2534 | goto err; |
| 2535 | } |
| 2536 | |
| 2537 | point = EC_POINT_new(p256); |
| 2538 | if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
| 2539 | goto err; |
| 2540 | } |
| 2541 | |
| 2542 | key = EC_KEY_new(); |
| 2543 | if (!key || !EC_KEY_set_group(key, p256) || |
| 2544 | !EC_KEY_set_public_key(key, point)) { |
| 2545 | goto err; |
| 2546 | } |
| 2547 | |
| 2548 | /* We stored the handshake hash in |tlsext_channel_id| the first time that we |
| 2549 | * were called. */ |
| 2550 | if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 2551 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2552 | s->s3->tlsext_channel_id_valid = 0; |
| 2553 | goto err; |
| 2554 | } |
| 2555 | |
| 2556 | memcpy(s->s3->tlsext_channel_id, p, 64); |
| 2557 | ret = 1; |
| 2558 | |
| 2559 | err: |
| 2560 | BN_free(&x); |
| 2561 | BN_free(&y); |
| 2562 | BN_free(sig.r); |
| 2563 | BN_free(sig.s); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 2564 | EC_KEY_free(key); |
| 2565 | EC_POINT_free(point); |
| 2566 | EC_GROUP_free(p256); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 2567 | return ret; |
| 2568 | } |