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