Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* ssl/s3_clnt.c */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. |
| 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * "This product includes cryptographic software written by |
| 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. |
| 52 | * |
| 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] |
| 57 | */ |
| 58 | /* ==================================================================== |
| 59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 60 | * |
| 61 | * Redistribution and use in source and binary forms, with or without |
| 62 | * modification, are permitted provided that the following conditions |
| 63 | * are met: |
| 64 | * |
| 65 | * 1. Redistributions of source code must retain the above copyright |
| 66 | * notice, this list of conditions and the following disclaimer. |
| 67 | * |
| 68 | * 2. Redistributions in binary form must reproduce the above copyright |
| 69 | * notice, this list of conditions and the following disclaimer in |
| 70 | * the documentation and/or other materials provided with the |
| 71 | * distribution. |
| 72 | * |
| 73 | * 3. All advertising materials mentioning features or use of this |
| 74 | * software must display the following acknowledgment: |
| 75 | * "This product includes software developed by the OpenSSL Project |
| 76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 77 | * |
| 78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 79 | * endorse or promote products derived from this software without |
| 80 | * prior written permission. For written permission, please contact |
| 81 | * openssl-core@openssl.org. |
| 82 | * |
| 83 | * 5. Products derived from this software may not be called "OpenSSL" |
| 84 | * nor may "OpenSSL" appear in their names without prior written |
| 85 | * permission of the OpenSSL Project. |
| 86 | * |
| 87 | * 6. Redistributions of any form whatsoever must retain the following |
| 88 | * acknowledgment: |
| 89 | * "This product includes software developed by the OpenSSL Project |
| 90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 91 | * |
| 92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 103 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 104 | * ==================================================================== |
| 105 | * |
| 106 | * This product includes cryptographic software written by Eric Young |
| 107 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 108 | * Hudson (tjh@cryptsoft.com). |
| 109 | * |
| 110 | */ |
| 111 | /* ==================================================================== |
| 112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 113 | * |
| 114 | * Portions of the attached software ("Contribution") are developed by |
| 115 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 116 | * |
| 117 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 118 | * license provided above. |
| 119 | * |
| 120 | * ECC cipher suite support in OpenSSL originally written by |
| 121 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 122 | * |
| 123 | */ |
| 124 | /* ==================================================================== |
| 125 | * Copyright 2005 Nokia. All rights reserved. |
| 126 | * |
| 127 | * The portions of the attached software ("Contribution") is developed by |
| 128 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 129 | * license. |
| 130 | * |
| 131 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 132 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 133 | * support (see RFC 4279) to OpenSSL. |
| 134 | * |
| 135 | * No patent licenses or other rights except those expressly stated in |
| 136 | * the OpenSSL open source license shall be deemed granted or received |
| 137 | * expressly, by implication, estoppel, or otherwise. |
| 138 | * |
| 139 | * No assurances are provided by Nokia that the Contribution does not |
| 140 | * infringe the patent or other intellectual property rights of any third |
| 141 | * party or that the license provides you with all the necessary rights |
| 142 | * to make use of the Contribution. |
| 143 | * |
| 144 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 145 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 146 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 147 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 148 | * OTHERWISE. |
| 149 | */ |
| 150 | |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 151 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | #include <stdio.h> |
| 153 | |
| 154 | #include <openssl/buf.h> |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 155 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | #include <openssl/rand.h> |
| 157 | #include <openssl/obj.h> |
| 158 | #include <openssl/evp.h> |
| 159 | #include <openssl/mem.h> |
| 160 | #include <openssl/md5.h> |
| 161 | #include <openssl/dh.h> |
| 162 | #include <openssl/bn.h> |
| 163 | #include <openssl/engine.h> |
| 164 | #include <openssl/x509.h> |
| 165 | |
| 166 | #include "ssl_locl.h" |
| 167 | #include "../crypto/dh/internal.h" |
| 168 | |
David Benjamin | 5b33a5e | 2014-09-24 16:27:30 -0400 | [diff] [blame] | 169 | IMPLEMENT_tls_meth_func(TLS1_2_VERSION, TLSv1_2_client_method, |
| 170 | ssl_undefined_function, |
| 171 | ssl3_connect, |
David Benjamin | 5b33a5e | 2014-09-24 16:27:30 -0400 | [diff] [blame] | 172 | TLSv1_2_enc_data) |
| 173 | |
| 174 | IMPLEMENT_tls_meth_func(TLS1_1_VERSION, TLSv1_1_client_method, |
| 175 | ssl_undefined_function, |
| 176 | ssl3_connect, |
David Benjamin | 5b33a5e | 2014-09-24 16:27:30 -0400 | [diff] [blame] | 177 | TLSv1_1_enc_data) |
| 178 | |
| 179 | IMPLEMENT_tls_meth_func(TLS1_VERSION, TLSv1_client_method, |
| 180 | ssl_undefined_function, |
| 181 | ssl3_connect, |
David Benjamin | 5b33a5e | 2014-09-24 16:27:30 -0400 | [diff] [blame] | 182 | TLSv1_enc_data) |
| 183 | |
David Benjamin | 00075b8 | 2014-09-24 15:48:14 -0400 | [diff] [blame] | 184 | IMPLEMENT_tls_meth_func(SSL3_VERSION, SSLv3_client_method, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | ssl_undefined_function, |
| 186 | ssl3_connect, |
David Benjamin | 00075b8 | 2014-09-24 15:48:14 -0400 | [diff] [blame] | 187 | SSLv3_enc_data) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | |
| 189 | int ssl3_connect(SSL *s) |
| 190 | { |
| 191 | BUF_MEM *buf=NULL; |
| 192 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 193 | int ret= -1; |
| 194 | int new_state,state,skip=0; |
| 195 | |
| 196 | ERR_clear_error(); |
| 197 | ERR_clear_system_error(); |
| 198 | |
| 199 | if (s->info_callback != NULL) |
| 200 | cb=s->info_callback; |
| 201 | else if (s->ctx->info_callback != NULL) |
| 202 | cb=s->ctx->info_callback; |
| 203 | |
| 204 | s->in_handshake++; |
| 205 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 206 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | for (;;) |
| 208 | { |
| 209 | state=s->state; |
| 210 | |
| 211 | switch(s->state) |
| 212 | { |
| 213 | case SSL_ST_RENEGOTIATE: |
| 214 | s->renegotiate=1; |
| 215 | s->state=SSL_ST_CONNECT; |
| 216 | s->ctx->stats.sess_connect_renegotiate++; |
| 217 | /* break */ |
| 218 | case SSL_ST_BEFORE: |
| 219 | case SSL_ST_CONNECT: |
| 220 | case SSL_ST_BEFORE|SSL_ST_CONNECT: |
| 221 | case SSL_ST_OK|SSL_ST_CONNECT: |
| 222 | |
| 223 | s->server=0; |
| 224 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 225 | |
| 226 | if ((s->version & 0xff00 ) != 0x0300) |
| 227 | { |
| 228 | OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR); |
| 229 | ret = -1; |
| 230 | goto end; |
| 231 | } |
| 232 | |
| 233 | /* s->version=SSL3_VERSION; */ |
| 234 | s->type=SSL_ST_CONNECT; |
| 235 | |
| 236 | if (s->init_buf == NULL) |
| 237 | { |
| 238 | if ((buf=BUF_MEM_new()) == NULL) |
| 239 | { |
| 240 | ret= -1; |
| 241 | goto end; |
| 242 | } |
| 243 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 244 | { |
| 245 | ret= -1; |
| 246 | goto end; |
| 247 | } |
| 248 | s->init_buf=buf; |
| 249 | buf=NULL; |
| 250 | } |
| 251 | |
| 252 | if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } |
| 253 | |
| 254 | /* setup buffing BIO */ |
| 255 | if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } |
| 256 | |
| 257 | /* don't push the buffering BIO quite yet */ |
| 258 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 259 | if (!ssl3_init_finished_mac(s)) |
| 260 | { |
| 261 | OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR); |
| 262 | ret = -1; |
| 263 | goto end; |
| 264 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
| 266 | s->state=SSL3_ST_CW_CLNT_HELLO_A; |
| 267 | s->ctx->stats.sess_connect++; |
| 268 | s->init_num=0; |
| 269 | break; |
| 270 | |
| 271 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 272 | case SSL3_ST_CW_CLNT_HELLO_B: |
| 273 | |
| 274 | s->shutdown=0; |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 275 | ret=ssl3_send_client_hello(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | if (ret <= 0) goto end; |
| 277 | s->state=SSL3_ST_CR_SRVR_HELLO_A; |
| 278 | s->init_num=0; |
| 279 | |
| 280 | /* turn on buffering for the next lot of output */ |
| 281 | if (s->bbio != s->wbio) |
| 282 | s->wbio=BIO_push(s->bbio,s->wbio); |
| 283 | |
| 284 | break; |
| 285 | |
| 286 | case SSL3_ST_CR_SRVR_HELLO_A: |
| 287 | case SSL3_ST_CR_SRVR_HELLO_B: |
| 288 | ret=ssl3_get_server_hello(s); |
| 289 | if (ret <= 0) goto end; |
| 290 | |
| 291 | if (s->hit) |
| 292 | { |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 293 | s->state=SSL3_ST_CR_CHANGE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | if (s->tlsext_ticket_expected) |
| 295 | { |
| 296 | /* receive renewed session ticket */ |
| 297 | s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | } |
| 300 | else |
| 301 | { |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 302 | s->state=SSL3_ST_CR_CERT_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | } |
| 304 | s->init_num=0; |
| 305 | break; |
David Benjamin | 2b0aeec | 2014-07-01 00:39:02 -0400 | [diff] [blame] | 306 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | case SSL3_ST_CR_CERT_A: |
| 308 | case SSL3_ST_CR_CERT_B: |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 309 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | { |
| 311 | ret=ssl3_get_server_certificate(s); |
| 312 | if (ret <= 0) goto end; |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 313 | if (s->s3->tmp.certificate_status_expected) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | s->state=SSL3_ST_CR_CERT_STATUS_A; |
| 315 | else |
| 316 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | skip = 1; |
| 321 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 322 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 323 | s->init_num=0; |
| 324 | break; |
| 325 | |
| 326 | case SSL3_ST_CR_KEY_EXCH_A: |
| 327 | case SSL3_ST_CR_KEY_EXCH_B: |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 328 | ret=ssl3_get_server_key_exchange(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | if (ret <= 0) goto end; |
| 330 | s->state=SSL3_ST_CR_CERT_REQ_A; |
| 331 | s->init_num=0; |
| 332 | |
| 333 | /* at this point we check that we have the |
| 334 | * required stuff from the server */ |
| 335 | if (!ssl3_check_cert_and_algorithm(s)) |
| 336 | { |
| 337 | ret= -1; |
| 338 | goto end; |
| 339 | } |
| 340 | break; |
| 341 | |
| 342 | case SSL3_ST_CR_CERT_REQ_A: |
| 343 | case SSL3_ST_CR_CERT_REQ_B: |
| 344 | ret=ssl3_get_certificate_request(s); |
| 345 | if (ret <= 0) goto end; |
| 346 | s->state=SSL3_ST_CR_SRVR_DONE_A; |
| 347 | s->init_num=0; |
| 348 | break; |
| 349 | |
| 350 | case SSL3_ST_CR_SRVR_DONE_A: |
| 351 | case SSL3_ST_CR_SRVR_DONE_B: |
| 352 | ret=ssl3_get_server_done(s); |
| 353 | if (ret <= 0) goto end; |
| 354 | if (s->s3->tmp.cert_req) |
| 355 | s->state=SSL3_ST_CW_CERT_A; |
| 356 | else |
| 357 | s->state=SSL3_ST_CW_KEY_EXCH_A; |
| 358 | s->init_num=0; |
| 359 | |
| 360 | break; |
| 361 | |
| 362 | case SSL3_ST_CW_CERT_A: |
| 363 | case SSL3_ST_CW_CERT_B: |
| 364 | case SSL3_ST_CW_CERT_C: |
| 365 | case SSL3_ST_CW_CERT_D: |
| 366 | ret=ssl3_send_client_certificate(s); |
| 367 | if (ret <= 0) goto end; |
| 368 | s->state=SSL3_ST_CW_KEY_EXCH_A; |
| 369 | s->init_num=0; |
| 370 | break; |
| 371 | |
| 372 | case SSL3_ST_CW_KEY_EXCH_A: |
| 373 | case SSL3_ST_CW_KEY_EXCH_B: |
| 374 | ret=ssl3_send_client_key_exchange(s); |
| 375 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | /* For TLS, cert_req is set to 2, so a cert chain |
| 377 | * of nothing is sent, but no verify packet is sent */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | if (s->s3->tmp.cert_req == 1) |
| 379 | { |
| 380 | s->state=SSL3_ST_CW_CERT_VRFY_A; |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | s->state=SSL3_ST_CW_CHANGE_A; |
| 385 | s->s3->change_cipher_spec=0; |
| 386 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | |
| 388 | s->init_num=0; |
| 389 | break; |
| 390 | |
| 391 | case SSL3_ST_CW_CERT_VRFY_A: |
| 392 | case SSL3_ST_CW_CERT_VRFY_B: |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 393 | ret=ssl3_send_cert_verify(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | if (ret <= 0) goto end; |
| 395 | s->state=SSL3_ST_CW_CHANGE_A; |
| 396 | s->init_num=0; |
| 397 | s->s3->change_cipher_spec=0; |
| 398 | break; |
| 399 | |
| 400 | case SSL3_ST_CW_CHANGE_A: |
| 401 | case SSL3_ST_CW_CHANGE_B: |
| 402 | ret=ssl3_send_change_cipher_spec(s, |
| 403 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); |
| 404 | if (ret <= 0) goto end; |
| 405 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | s->state=SSL3_ST_CW_FINISHED_A; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 407 | if (s->s3->tlsext_channel_id_valid) |
| 408 | s->state=SSL3_ST_CW_CHANNEL_ID_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | if (s->s3->next_proto_neg_seen) |
| 410 | s->state=SSL3_ST_CW_NEXT_PROTO_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 411 | s->init_num=0; |
| 412 | |
| 413 | s->session->cipher=s->s3->tmp.new_cipher; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 415 | { |
| 416 | ret= -1; |
| 417 | goto end; |
| 418 | } |
| 419 | |
| 420 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 421 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 422 | { |
| 423 | ret= -1; |
| 424 | goto end; |
| 425 | } |
| 426 | |
| 427 | break; |
| 428 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 429 | case SSL3_ST_CW_NEXT_PROTO_A: |
| 430 | case SSL3_ST_CW_NEXT_PROTO_B: |
| 431 | ret=ssl3_send_next_proto(s); |
| 432 | if (ret <= 0) goto end; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | if (s->s3->tlsext_channel_id_valid) |
| 434 | s->state=SSL3_ST_CW_CHANNEL_ID_A; |
| 435 | else |
| 436 | s->state=SSL3_ST_CW_FINISHED_A; |
| 437 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | case SSL3_ST_CW_CHANNEL_ID_A: |
| 440 | case SSL3_ST_CW_CHANNEL_ID_B: |
| 441 | ret=ssl3_send_channel_id(s); |
| 442 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | s->state=SSL3_ST_CW_FINISHED_A; |
| 444 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | |
| 446 | case SSL3_ST_CW_FINISHED_A: |
| 447 | case SSL3_ST_CW_FINISHED_B: |
| 448 | ret=ssl3_send_finished(s, |
| 449 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, |
| 450 | s->method->ssl3_enc->client_finished_label, |
| 451 | s->method->ssl3_enc->client_finished_label_len); |
| 452 | if (ret <= 0) goto end; |
| 453 | s->state=SSL3_ST_CW_FLUSH; |
| 454 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | if (s->hit) |
| 456 | { |
| 457 | s->s3->tmp.next_state=SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 458 | } |
| 459 | else |
| 460 | { |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | /* This is a non-resumption handshake. If it |
| 462 | * involves ChannelID, then record the |
| 463 | * handshake hashes at this point in the |
| 464 | * session so that any resumption of this |
| 465 | * session with ChannelID can sign those |
| 466 | * hashes. */ |
| 467 | if (s->s3->tlsext_channel_id_new) |
| 468 | { |
| 469 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 470 | if (ret <= 0) |
| 471 | goto end; |
| 472 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) |
| 474 | && ssl3_can_cutthrough(s) |
| 475 | && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */ |
| 476 | ) |
| 477 | { |
Adam Langley | 045cc55 | 2014-07-16 10:45:57 -0700 | [diff] [blame] | 478 | s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 480 | else |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | { |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | /* Allow NewSessionTicket if ticket expected */ |
| 483 | if (s->tlsext_ticket_expected) |
| 484 | s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; |
| 485 | else |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 486 | s->s3->tmp.next_state=SSL3_ST_CR_CHANGE; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 487 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 488 | } |
| 489 | s->init_num=0; |
| 490 | break; |
| 491 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 492 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 493 | case SSL3_ST_CR_SESSION_TICKET_B: |
| 494 | ret=ssl3_get_new_session_ticket(s); |
| 495 | if (ret <= 0) goto end; |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 496 | s->state=SSL3_ST_CR_CHANGE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | s->init_num=0; |
| 498 | break; |
| 499 | |
| 500 | case SSL3_ST_CR_CERT_STATUS_A: |
| 501 | case SSL3_ST_CR_CERT_STATUS_B: |
| 502 | ret=ssl3_get_cert_status(s); |
| 503 | if (ret <= 0) goto end; |
| 504 | s->state=SSL3_ST_CR_KEY_EXCH_A; |
| 505 | s->init_num=0; |
| 506 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 508 | case SSL3_ST_CR_CHANGE: |
| 509 | /* At this point, the next message must be entirely |
| 510 | * behind a ChangeCipherSpec. */ |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 511 | if (!ssl3_expect_change_cipher_spec(s)) |
| 512 | { |
| 513 | ret = -1; |
| 514 | goto end; |
| 515 | } |
David Benjamin | cb5abad | 2014-07-25 12:14:28 -0400 | [diff] [blame] | 516 | s->state = SSL3_ST_CR_FINISHED_A; |
| 517 | break; |
| 518 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | case SSL3_ST_CR_FINISHED_A: |
| 520 | case SSL3_ST_CR_FINISHED_B: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, |
| 522 | SSL3_ST_CR_FINISHED_B); |
| 523 | if (ret <= 0) goto end; |
| 524 | |
| 525 | if (s->hit) |
| 526 | s->state=SSL3_ST_CW_CHANGE_A; |
| 527 | else |
| 528 | s->state=SSL_ST_OK; |
| 529 | s->init_num=0; |
| 530 | break; |
| 531 | |
| 532 | case SSL3_ST_CW_FLUSH: |
| 533 | s->rwstate=SSL_WRITING; |
| 534 | if (BIO_flush(s->wbio) <= 0) |
| 535 | { |
| 536 | ret= -1; |
| 537 | goto end; |
| 538 | } |
| 539 | s->rwstate=SSL_NOTHING; |
| 540 | s->state=s->s3->tmp.next_state; |
| 541 | break; |
| 542 | |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 543 | case SSL3_ST_CUTTHROUGH_COMPLETE: |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | /* Allow NewSessionTicket if ticket expected */ |
| 545 | if (s->tlsext_ticket_expected) |
| 546 | s->state=SSL3_ST_CR_SESSION_TICKET_A; |
| 547 | else |
David Benjamin | 7e3305e | 2014-07-28 14:52:32 -0400 | [diff] [blame] | 548 | s->state=SSL3_ST_CR_CHANGE; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | |
Adam Langley | 045cc55 | 2014-07-16 10:45:57 -0700 | [diff] [blame] | 550 | ssl_free_wbio_buffer(s); |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | ret = 1; |
| 552 | goto end; |
| 553 | /* break; */ |
| 554 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | case SSL_ST_OK: |
| 556 | /* clean a few things up */ |
| 557 | ssl3_cleanup_key_block(s); |
| 558 | |
| 559 | if (s->init_buf != NULL) |
| 560 | { |
| 561 | BUF_MEM_free(s->init_buf); |
| 562 | s->init_buf=NULL; |
| 563 | } |
| 564 | |
David Benjamin | e1b20a0 | 2014-11-05 01:28:53 -0500 | [diff] [blame] | 565 | /* Remove write buffering now. */ |
| 566 | ssl_free_wbio_buffer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 567 | |
| 568 | s->init_num=0; |
| 569 | s->renegotiate=0; |
| 570 | s->new_session=0; |
| 571 | |
| 572 | ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); |
| 573 | if (s->hit) s->ctx->stats.sess_hit++; |
| 574 | |
| 575 | ret=1; |
| 576 | /* s->server=0; */ |
| 577 | s->handshake_func=ssl3_connect; |
| 578 | s->ctx->stats.sess_connect_good++; |
| 579 | |
| 580 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 581 | |
| 582 | goto end; |
| 583 | /* break; */ |
| 584 | |
| 585 | default: |
| 586 | OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE); |
| 587 | ret= -1; |
| 588 | goto end; |
| 589 | /* break; */ |
| 590 | } |
| 591 | |
| 592 | /* did we do anything */ |
| 593 | if (!s->s3->tmp.reuse_message && !skip) |
| 594 | { |
| 595 | if (s->debug) |
| 596 | { |
| 597 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 598 | goto end; |
| 599 | } |
| 600 | |
| 601 | if ((cb != NULL) && (s->state != state)) |
| 602 | { |
| 603 | new_state=s->state; |
| 604 | s->state=state; |
| 605 | cb(s,SSL_CB_CONNECT_LOOP,1); |
| 606 | s->state=new_state; |
| 607 | } |
| 608 | } |
| 609 | skip=0; |
| 610 | } |
| 611 | end: |
| 612 | s->in_handshake--; |
| 613 | if (buf != NULL) |
| 614 | BUF_MEM_free(buf); |
| 615 | if (cb != NULL) |
| 616 | cb(s,SSL_CB_CONNECT_EXIT,ret); |
| 617 | return(ret); |
| 618 | } |
| 619 | |
| 620 | |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 621 | int ssl3_send_client_hello(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 622 | { |
| 623 | unsigned char *buf; |
| 624 | unsigned char *p,*d; |
| 625 | int i; |
| 626 | unsigned long l; |
| 627 | |
| 628 | buf=(unsigned char *)s->init_buf->data; |
| 629 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) |
| 630 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 631 | if (s->method->version == DTLS_ANY_VERSION) |
| 632 | { |
| 633 | /* Determine which DTLS version to use */ |
| 634 | int options = s->options; |
| 635 | /* If DTLS 1.2 disabled correct the version number */ |
| 636 | if (options & SSL_OP_NO_DTLSv1_2) |
| 637 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 638 | /* Disabling all versions is silly: return an |
| 639 | * error. |
| 640 | */ |
| 641 | if (options & SSL_OP_NO_DTLSv1) |
| 642 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 643 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 644 | goto err; |
| 645 | } |
| 646 | /* Update method so we don't use any DTLS 1.2 |
| 647 | * features. |
| 648 | */ |
| 649 | s->method = DTLSv1_client_method(); |
| 650 | s->version = DTLS1_VERSION; |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | /* We only support one version: update method */ |
| 655 | if (options & SSL_OP_NO_DTLSv1) |
| 656 | s->method = DTLSv1_2_client_method(); |
| 657 | s->version = DTLS1_2_VERSION; |
| 658 | } |
| 659 | s->client_version = s->version; |
| 660 | } |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 661 | |
| 662 | /* If the configured session was created at a version |
| 663 | * higher than our maximum version, drop it. */ |
| 664 | if (s->session && |
| 665 | (s->session->session_id_length == 0 || |
| 666 | s->session->not_resumable || |
| 667 | (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) || |
| 668 | (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) |
| 669 | { |
| 670 | SSL_set_session(s, NULL); |
| 671 | } |
| 672 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 673 | /* else use the pre-loaded session */ |
| 674 | |
| 675 | p=s->s3->client_random; |
| 676 | |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 677 | /* If resending the ClientHello in DTLS after a |
| 678 | * HelloVerifyRequest, don't renegerate the client_random. The |
| 679 | * random must be reused. */ |
| 680 | if (!SSL_IS_DTLS(s) || !s->d1->send_cookie) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 681 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 682 | ssl_fill_hello_random(s, 0, p, |
| 683 | sizeof(s->s3->client_random)); |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 684 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 685 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 686 | /* Do the message type and length last. |
| 687 | * Note: the final argument to ssl_add_clienthello_tlsext below |
| 688 | * depends on the size of this prefix. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 689 | d=p= ssl_handshake_start(s); |
| 690 | |
| 691 | /* version indicates the negotiated version: for example from |
| 692 | * an SSLv2/v3 compatible client hello). The client_version |
| 693 | * field is the maximum version we permit and it is also |
| 694 | * used in RSA encrypted premaster secrets. Some servers can |
| 695 | * choke if we initially report a higher version then |
| 696 | * renegotiate to a lower one in the premaster secret. This |
| 697 | * didn't happen with TLS 1.0 as most servers supported it |
| 698 | * but it can with TLS 1.1 or later if the server only supports |
| 699 | * 1.0. |
| 700 | * |
| 701 | * Possible scenario with previous logic: |
| 702 | * 1. Client hello indicates TLS 1.2 |
| 703 | * 2. Server hello says TLS 1.0 |
| 704 | * 3. RSA encrypted premaster secret uses 1.2. |
| 705 | * 4. Handhaked proceeds using TLS 1.0. |
| 706 | * 5. Server sends hello request to renegotiate. |
| 707 | * 6. Client hello indicates TLS v1.0 as we now |
| 708 | * know that is maximum server supports. |
| 709 | * 7. Server chokes on RSA encrypted premaster secret |
| 710 | * containing version 1.0. |
| 711 | * |
| 712 | * For interoperability it should be OK to always use the |
| 713 | * maximum version we support in client hello and then rely |
| 714 | * on the checking of version to ensure the servers isn't |
| 715 | * being inconsistent: for example initially negotiating with |
| 716 | * TLS 1.0 and renegotiating with TLS 1.2. We do this by using |
| 717 | * client_version in client hello and not resetting it to |
| 718 | * the negotiated version. |
| 719 | */ |
| 720 | #if 0 |
| 721 | *(p++)=s->version>>8; |
| 722 | *(p++)=s->version&0xff; |
| 723 | s->client_version=s->version; |
| 724 | #else |
| 725 | *(p++)=s->client_version>>8; |
| 726 | *(p++)=s->client_version&0xff; |
| 727 | #endif |
| 728 | |
| 729 | /* Random stuff */ |
| 730 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); |
| 731 | p+=SSL3_RANDOM_SIZE; |
| 732 | |
| 733 | /* Session ID */ |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 734 | if (s->new_session || s->session == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 735 | i=0; |
| 736 | else |
| 737 | i=s->session->session_id_length; |
| 738 | *(p++)=i; |
| 739 | if (i != 0) |
| 740 | { |
| 741 | if (i > (int)sizeof(s->session->session_id)) |
| 742 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 743 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 744 | goto err; |
| 745 | } |
| 746 | memcpy(p,s->session->session_id,i); |
| 747 | p+=i; |
| 748 | } |
| 749 | |
| 750 | /* cookie stuff for DTLS */ |
| 751 | if (SSL_IS_DTLS(s)) |
| 752 | { |
| 753 | if ( s->d1->cookie_len > sizeof(s->d1->cookie)) |
| 754 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 755 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 756 | goto err; |
| 757 | } |
| 758 | *(p++) = s->d1->cookie_len; |
| 759 | memcpy(p, s->d1->cookie, s->d1->cookie_len); |
| 760 | p += s->d1->cookie_len; |
| 761 | } |
| 762 | |
| 763 | /* Ciphers supported */ |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 764 | i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 765 | if (i == 0) |
| 766 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 767 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_NO_CIPHERS_AVAILABLE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 768 | goto err; |
| 769 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | s2n(i,p); |
| 771 | p+=i; |
| 772 | |
| 773 | /* COMPRESSION */ |
| 774 | *(p++)=1; |
| 775 | *(p++)=0; /* Add the NULL method */ |
| 776 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 777 | /* TLS extensions*/ |
| 778 | if (ssl_prepare_clienthello_tlsext(s) <= 0) |
| 779 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 780 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 781 | goto err; |
| 782 | } |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 783 | if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 784 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 785 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 786 | goto err; |
| 787 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 788 | |
| 789 | l= p-d; |
| 790 | ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l); |
| 791 | s->state=SSL3_ST_CW_CLNT_HELLO_B; |
| 792 | } |
| 793 | |
| 794 | /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 795 | return ssl_do_write(s); |
| 796 | err: |
| 797 | return(-1); |
| 798 | } |
| 799 | |
| 800 | int ssl3_get_server_hello(SSL *s) |
| 801 | { |
| 802 | STACK_OF(SSL_CIPHER) *sk; |
| 803 | const SSL_CIPHER *c; |
| 804 | CERT *ct = s->cert; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 805 | int al=SSL_AD_INTERNAL_ERROR,ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | long n; |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 807 | CBS server_hello, server_random, session_id; |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 808 | uint16_t server_version, cipher_suite; |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 809 | uint8_t compression_method; |
David Benjamin | 39ebf53 | 2014-08-31 02:23:49 -0400 | [diff] [blame] | 810 | unsigned long mask_ssl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 811 | |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 812 | /* DTLS_ANY_VERSION does not sniff the version ahead of time, |
| 813 | * so disable the version check. */ |
| 814 | if (SSL_IS_DTLS(s)) |
| 815 | s->first_packet = 1; |
| 816 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 817 | n=s->method->ssl_get_message(s, |
| 818 | SSL3_ST_CR_SRVR_HELLO_A, |
| 819 | SSL3_ST_CR_SRVR_HELLO_B, |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 820 | SSL3_MT_SERVER_HELLO, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 821 | 20000, /* ?? */ |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 822 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 823 | &ok); |
| 824 | |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 825 | if (SSL_IS_DTLS(s)) |
| 826 | s->first_packet = 0; |
| 827 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 828 | if (!ok) return((int)n); |
| 829 | |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 830 | CBS_init(&server_hello, s->init_msg, n); |
| 831 | |
| 832 | if (!CBS_get_u16(&server_hello, &server_version) || |
| 833 | !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) || |
| 834 | !CBS_get_u8_length_prefixed(&server_hello, &session_id) || |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 835 | CBS_len(&session_id) > SSL3_SESSION_ID_SIZE || |
| 836 | !CBS_get_u16(&server_hello, &cipher_suite) || |
| 837 | !CBS_get_u8(&server_hello, &compression_method)) |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 838 | { |
| 839 | al = SSL_AD_DECODE_ERROR; |
| 840 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR); |
| 841 | goto f_err; |
| 842 | } |
| 843 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 844 | if (s->method->version == DTLS_ANY_VERSION) |
| 845 | { |
| 846 | /* Work out correct protocol version to use */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 847 | int options = s->options; |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 848 | if (server_version == DTLS1_2_VERSION |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 849 | && !(options & SSL_OP_NO_DTLSv1_2)) |
| 850 | s->method = DTLSv1_2_client_method(); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 851 | else if (server_version == DTLS1_VERSION |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 852 | && !(options & SSL_OP_NO_DTLSv1)) |
| 853 | s->method = DTLSv1_client_method(); |
| 854 | else |
| 855 | { |
| 856 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 857 | s->version = server_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 858 | al = SSL_AD_PROTOCOL_VERSION; |
| 859 | goto f_err; |
| 860 | } |
| 861 | s->version = s->client_version = s->method->version; |
| 862 | } |
| 863 | |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 864 | if (server_version != s->version) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 865 | { |
| 866 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION); |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 867 | s->version = (s->version & 0xff00) | (server_version & 0xff); |
| 868 | al = SSL_AD_PROTOCOL_VERSION; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 869 | goto f_err; |
| 870 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 871 | |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 872 | /* Copy over the server random. */ |
| 873 | memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 874 | |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 875 | assert(s->session == NULL || s->session->session_id_length > 0); |
| 876 | if (s->session != NULL && |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 877 | CBS_mem_equal(&session_id, |
| 878 | s->session->session_id, s->session->session_id_length)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 879 | { |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 880 | if(s->sid_ctx_length != s->session->sid_ctx_length |
| 881 | || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) |
| 882 | { |
| 883 | /* actually a client application bug */ |
| 884 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 885 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 886 | goto f_err; |
| 887 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 888 | s->hit = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 889 | } |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 890 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 891 | { |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 892 | /* The session wasn't resumed. Create a fresh SSL_SESSION to |
| 893 | * fill out. */ |
David Benjamin | d1681e6 | 2014-11-20 14:54:14 -0500 | [diff] [blame] | 894 | s->hit = 0; |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 895 | if (!ssl_get_new_session(s, 0)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 896 | { |
David Benjamin | 81ea0bf | 2014-11-23 04:20:17 -0500 | [diff] [blame^] | 897 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 898 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 899 | /* Note: session_id could be empty. */ |
| 900 | s->session->session_id_length = CBS_len(&session_id); |
| 901 | memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 902 | } |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 903 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 904 | c = ssl3_get_cipher_by_value(cipher_suite); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 905 | if (c == NULL) |
| 906 | { |
| 907 | /* unknown cipher */ |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 908 | al = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 909 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 910 | goto f_err; |
| 911 | } |
David Benjamin | 39ebf53 | 2014-08-31 02:23:49 -0400 | [diff] [blame] | 912 | /* ct->mask_ssl was computed from client capabilities. Now |
| 913 | * that the final version is known, compute a new mask_ssl. */ |
| 914 | if (!SSL_USE_TLS1_2_CIPHERS(s)) |
| 915 | mask_ssl = SSL_TLSV1_2; |
| 916 | else |
| 917 | mask_ssl = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 918 | /* If it is a disabled cipher we didn't send it in client hello, |
| 919 | * so return an error. |
| 920 | */ |
David Benjamin | 39ebf53 | 2014-08-31 02:23:49 -0400 | [diff] [blame] | 921 | if (c->algorithm_ssl & mask_ssl || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 922 | c->algorithm_mkey & ct->mask_k || |
| 923 | c->algorithm_auth & ct->mask_a) |
| 924 | { |
| 925 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 926 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED); |
| 927 | goto f_err; |
| 928 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 929 | |
| 930 | sk=ssl_get_ciphers_by_id(s); |
| 931 | if (!sk_SSL_CIPHER_find(sk, NULL, c)) |
| 932 | { |
| 933 | /* we did not say we would use this cipher */ |
| 934 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 935 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED); |
| 936 | goto f_err; |
| 937 | } |
| 938 | |
| 939 | /* Depending on the session caching (internal/external), the cipher |
| 940 | and/or cipher_id values may not be set. Make sure that |
| 941 | cipher_id is set and use it for comparison. */ |
| 942 | if (s->session->cipher) |
| 943 | s->session->cipher_id = s->session->cipher->id; |
| 944 | if (s->hit && (s->session->cipher_id != c->id)) |
| 945 | { |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 946 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 947 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 948 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 949 | } |
| 950 | s->s3->tmp.new_cipher=c; |
David Benjamin | bdf5e72 | 2014-11-11 00:52:15 -0500 | [diff] [blame] | 951 | |
| 952 | /* Most clients also require that the negotiated version match the |
| 953 | * session's version if resuming. However OpenSSL has historically not |
| 954 | * had the corresponding logic on the server, so this may not be |
| 955 | * compatible, depending on other factors. (Whether the ClientHello |
| 956 | * version is clamped to the session's version and whether the session |
| 957 | * cache is keyed on IP address.) |
| 958 | * |
| 959 | * TODO(davidben): See if we can still enforce this? Perhaps for the |
| 960 | * future TLS 1.3 and forward if this is fixed upstream. */ |
| 961 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 962 | /* Don't digest cached records if no sigalgs: we may need them for |
| 963 | * client authentication. |
| 964 | */ |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 965 | if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, free_handshake_buffer)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 966 | goto f_err; |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 967 | |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 968 | /* Only the NULL compression algorithm is supported. */ |
| 969 | if (compression_method != 0) |
| 970 | { |
| 971 | al = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 972 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 973 | goto f_err; |
| 974 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 975 | |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 976 | /* TLS extensions */ |
| 977 | if (!ssl_parse_serverhello_tlsext(s, &server_hello)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 978 | { |
| 979 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT); |
| 980 | goto err; |
| 981 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 982 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 983 | /* There should be nothing left over in the record. */ |
David Benjamin | a03d95d | 2014-07-12 19:49:07 -0400 | [diff] [blame] | 984 | if (CBS_len(&server_hello) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 985 | { |
| 986 | /* wrong packet length */ |
| 987 | al=SSL_AD_DECODE_ERROR; |
| 988 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH); |
| 989 | goto f_err; |
| 990 | } |
| 991 | |
| 992 | return(1); |
| 993 | f_err: |
| 994 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 995 | err: |
| 996 | return(-1); |
| 997 | } |
| 998 | |
| 999 | int ssl3_get_server_certificate(SSL *s) |
| 1000 | { |
| 1001 | int al,i,ok,ret= -1; |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1002 | unsigned long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1003 | X509 *x=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1004 | STACK_OF(X509) *sk=NULL; |
| 1005 | SESS_CERT *sc; |
| 1006 | EVP_PKEY *pkey=NULL; |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1007 | CBS cbs, certificate_list; |
| 1008 | const uint8_t* data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1009 | |
| 1010 | n=s->method->ssl_get_message(s, |
| 1011 | SSL3_ST_CR_CERT_A, |
| 1012 | SSL3_ST_CR_CERT_B, |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 1013 | SSL3_MT_CERTIFICATE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | s->max_cert_list, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1015 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1016 | &ok); |
| 1017 | |
| 1018 | if (!ok) return((int)n); |
| 1019 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 1020 | CBS_init(&cbs, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1021 | |
| 1022 | if ((sk=sk_X509_new_null()) == NULL) |
| 1023 | { |
| 1024 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE); |
| 1025 | goto err; |
| 1026 | } |
| 1027 | |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1028 | if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) || |
| 1029 | CBS_len(&cbs) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1030 | { |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1031 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1032 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH); |
| 1033 | goto f_err; |
| 1034 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1035 | |
| 1036 | while (CBS_len(&certificate_list) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1037 | { |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1038 | CBS certificate; |
| 1039 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | { |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1041 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1042 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 1043 | goto f_err; |
| 1044 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1045 | data = CBS_data(&certificate); |
| 1046 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1047 | if (x == NULL) |
| 1048 | { |
| 1049 | al=SSL_AD_BAD_CERTIFICATE; |
| 1050 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB); |
| 1051 | goto f_err; |
| 1052 | } |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1053 | if (!CBS_skip(&certificate, data - CBS_data(&certificate))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1054 | { |
Alex Chernyakhovsky | c6318e3 | 2014-07-04 22:52:07 -0400 | [diff] [blame] | 1055 | al = SSL_AD_INTERNAL_ERROR; |
| 1056 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR); |
| 1057 | goto f_err; |
| 1058 | } |
| 1059 | if (CBS_len(&certificate) != 0) |
| 1060 | { |
| 1061 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1062 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH); |
| 1063 | goto f_err; |
| 1064 | } |
| 1065 | if (!sk_X509_push(sk,x)) |
| 1066 | { |
| 1067 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE); |
| 1068 | goto err; |
| 1069 | } |
| 1070 | x=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | i=ssl_verify_cert_chain(s,sk); |
| 1074 | if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) |
| 1075 | ) |
| 1076 | { |
| 1077 | al=ssl_verify_alarm_type(s->verify_result); |
| 1078 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1079 | goto f_err; |
| 1080 | } |
| 1081 | ERR_clear_error(); /* but we keep s->verify_result */ |
| 1082 | |
| 1083 | sc=ssl_sess_cert_new(); |
| 1084 | if (sc == NULL) goto err; |
| 1085 | |
| 1086 | if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); |
| 1087 | s->session->sess_cert=sc; |
| 1088 | |
| 1089 | sc->cert_chain=sk; |
| 1090 | /* Inconsistency alert: cert_chain does include the peer's |
| 1091 | * certificate, which we don't include in s3_srvr.c */ |
| 1092 | x=sk_X509_value(sk,0); |
| 1093 | sk=NULL; |
| 1094 | /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ |
| 1095 | |
| 1096 | pkey=X509_get_pubkey(x); |
| 1097 | |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1098 | if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1099 | { |
| 1100 | x=NULL; |
| 1101 | al=SSL3_AL_FATAL; |
| 1102 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
| 1103 | goto f_err; |
| 1104 | } |
| 1105 | |
| 1106 | i=ssl_cert_type(x,pkey); |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1107 | if (i < 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1108 | { |
| 1109 | x=NULL; |
| 1110 | al=SSL3_AL_FATAL; |
| 1111 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 1112 | goto f_err; |
| 1113 | } |
| 1114 | |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1115 | int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 1116 | if (exp_idx >= 0 && i != exp_idx) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1117 | { |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1118 | x=NULL; |
| 1119 | al=SSL_AD_ILLEGAL_PARAMETER; |
| 1120 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE); |
| 1121 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1122 | } |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1123 | sc->peer_cert_type=i; |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1124 | /* Why would the following ever happen? |
| 1125 | * We just created sc a couple of lines ago. */ |
| 1126 | if (sc->peer_pkeys[i].x509 != NULL) |
| 1127 | X509_free(sc->peer_pkeys[i].x509); |
David Benjamin | 150c617 | 2014-08-05 22:22:49 -0400 | [diff] [blame] | 1128 | sc->peer_pkeys[i].x509 = X509_up_ref(x); |
| 1129 | sc->peer_key = &(sc->peer_pkeys[i]); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1130 | |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1131 | if (s->session->peer != NULL) |
| 1132 | X509_free(s->session->peer); |
David Benjamin | 150c617 | 2014-08-05 22:22:49 -0400 | [diff] [blame] | 1133 | s->session->peer = X509_up_ref(x); |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 1134 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1135 | s->session->verify_result = s->verify_result; |
| 1136 | |
| 1137 | x=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1138 | ret=1; |
| 1139 | if (0) |
| 1140 | { |
| 1141 | f_err: |
| 1142 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1143 | } |
| 1144 | err: |
| 1145 | EVP_PKEY_free(pkey); |
| 1146 | X509_free(x); |
| 1147 | sk_X509_pop_free(sk,X509_free); |
| 1148 | return(ret); |
| 1149 | } |
| 1150 | |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1151 | int ssl3_get_server_key_exchange(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1152 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1153 | EVP_MD_CTX md_ctx; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1154 | int al,ok; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1155 | long n,alg_k,alg_a; |
| 1156 | EVP_PKEY *pkey=NULL; |
| 1157 | const EVP_MD *md = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1158 | RSA *rsa=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1159 | DH *dh=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1160 | EC_KEY *ecdh = NULL; |
| 1161 | BN_CTX *bn_ctx = NULL; |
| 1162 | EC_POINT *srvr_ecpoint = NULL; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1163 | CBS server_key_exchange, server_key_exchange_orig, parameter; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1164 | |
| 1165 | /* use same message size as in ssl3_get_certificate_request() |
| 1166 | * as ServerKeyExchange message may be skipped */ |
| 1167 | n=s->method->ssl_get_message(s, |
| 1168 | SSL3_ST_CR_KEY_EXCH_A, |
| 1169 | SSL3_ST_CR_KEY_EXCH_B, |
| 1170 | -1, |
| 1171 | s->max_cert_list, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1172 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1173 | &ok); |
| 1174 | if (!ok) return((int)n); |
| 1175 | |
| 1176 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) |
| 1177 | { |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1178 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) |
| 1179 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1180 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 1181 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1185 | /* In plain PSK ciphersuite, ServerKeyExchange can be |
| 1186 | omitted if no identity hint is sent. Set |
| 1187 | session->sess_cert anyway to avoid problems |
| 1188 | later.*/ |
David Benjamin | 5235f74 | 2014-07-12 13:11:24 -0400 | [diff] [blame] | 1189 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1190 | { |
David Benjamin | 0c49ec9 | 2014-07-12 13:16:51 -0400 | [diff] [blame] | 1191 | /* PSK ciphersuites that also send a |
| 1192 | * Certificate would have already initialized |
| 1193 | * |sess_cert|. */ |
| 1194 | if (s->session->sess_cert == NULL) |
| 1195 | s->session->sess_cert = ssl_sess_cert_new(); |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1196 | |
| 1197 | /* TODO(davidben): This should be reset in one place |
| 1198 | * with the rest of the handshake state. */ |
| 1199 | if (s->s3->tmp.peer_psk_identity_hint) |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1200 | { |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1201 | OPENSSL_free(s->s3->tmp.peer_psk_identity_hint); |
| 1202 | s->s3->tmp.peer_psk_identity_hint = NULL; |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1203 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1204 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1205 | s->s3->tmp.reuse_message=1; |
| 1206 | return(1); |
| 1207 | } |
| 1208 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1209 | /* Retain a copy of the original CBS to compute the signature |
| 1210 | * over. */ |
| 1211 | CBS_init(&server_key_exchange, s->init_msg, n); |
| 1212 | server_key_exchange_orig = server_key_exchange; |
| 1213 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1214 | if (s->session->sess_cert != NULL) |
| 1215 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1216 | if (s->session->sess_cert->peer_dh_tmp) |
| 1217 | { |
| 1218 | DH_free(s->session->sess_cert->peer_dh_tmp); |
| 1219 | s->session->sess_cert->peer_dh_tmp=NULL; |
| 1220 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1221 | if (s->session->sess_cert->peer_ecdh_tmp) |
| 1222 | { |
| 1223 | EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); |
| 1224 | s->session->sess_cert->peer_ecdh_tmp=NULL; |
| 1225 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1226 | } |
| 1227 | else |
| 1228 | { |
| 1229 | s->session->sess_cert=ssl_sess_cert_new(); |
| 1230 | } |
| 1231 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1232 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 1233 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 1234 | EVP_MD_CTX_init(&md_ctx); |
| 1235 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1236 | if (alg_a & SSL_aPSK) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1237 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1238 | CBS psk_identity_hint; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1239 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1240 | /* Each of the PSK key exchanges begins with a |
| 1241 | * psk_identity_hint. */ |
| 1242 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint)) |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1243 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1244 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1245 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1246 | goto f_err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1247 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1248 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1249 | /* Store PSK identity hint for later use, hint is used in |
| 1250 | * ssl3_send_client_key_exchange. Assume that the maximum |
| 1251 | * length of a PSK identity hint can be as long as the maximum |
| 1252 | * length of a PSK identity. Also do not allow NULL |
| 1253 | * characters; identities are saved as C strings. |
| 1254 | * |
| 1255 | * TODO(davidben): Should invalid hints be ignored? It's a hint |
| 1256 | * rather than a specific identity. */ |
| 1257 | if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN || |
| 1258 | CBS_contains_zero_byte(&psk_identity_hint)) |
| 1259 | { |
| 1260 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1261 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1262 | goto f_err; |
| 1263 | } |
| 1264 | |
| 1265 | /* Save the identity hint as a C string. */ |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1266 | if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1267 | { |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1268 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1269 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1270 | goto f_err; |
| 1271 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1272 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1273 | |
David Benjamin | 525a0fe | 2014-11-08 11:41:12 -0500 | [diff] [blame] | 1274 | if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1275 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1276 | CBS dh_p, dh_g, dh_Ys; |
| 1277 | |
| 1278 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) || |
| 1279 | CBS_len(&dh_p) == 0 || |
| 1280 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) || |
| 1281 | CBS_len(&dh_g) == 0 || |
| 1282 | !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) || |
| 1283 | CBS_len(&dh_Ys) == 0) |
| 1284 | { |
| 1285 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1286 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1287 | goto f_err; |
| 1288 | } |
| 1289 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1290 | if ((dh=DH_new()) == NULL) |
| 1291 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1292 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1293 | goto err; |
| 1294 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1295 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1296 | if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1297 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1298 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1299 | goto err; |
| 1300 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1301 | if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1302 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1303 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1304 | goto err; |
| 1305 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1306 | if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL))) |
| 1307 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1308 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1309 | goto err; |
| 1310 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1311 | |
HÃ¥vard Molland | 16c623b | 2014-08-12 11:29:57 +0200 | [diff] [blame] | 1312 | if (DH_size(dh) < 512/8) |
| 1313 | { |
| 1314 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_DH_P_LENGTH); |
| 1315 | goto err; |
| 1316 | } |
| 1317 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1318 | if (alg_a & SSL_aRSA) |
| 1319 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1320 | /* else anonymous DH, so no certificate or pkey. */ |
| 1321 | |
| 1322 | s->session->sess_cert->peer_dh_tmp=dh; |
| 1323 | dh=NULL; |
| 1324 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1325 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1326 | else if (alg_k & SSL_kEECDH) |
| 1327 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1328 | uint16_t curve_id; |
| 1329 | int curve_nid = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1330 | EC_GROUP *ngroup; |
| 1331 | const EC_GROUP *group; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1332 | CBS point; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1333 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1334 | /* Extract elliptic curve parameters and the server's |
| 1335 | * ephemeral ECDH public key. Check curve is one of |
| 1336 | * our preferences, if not server has sent an invalid |
| 1337 | * curve. |
| 1338 | */ |
| 1339 | if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1340 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1341 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1342 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1343 | goto f_err; |
| 1344 | } |
| 1345 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1346 | if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1347 | { |
| 1348 | al=SSL_AD_INTERNAL_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1349 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1350 | goto f_err; |
| 1351 | } |
| 1352 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1353 | if ((ecdh=EC_KEY_new()) == NULL) |
| 1354 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1355 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1356 | goto err; |
| 1357 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1358 | ngroup = EC_GROUP_new_by_curve_name(curve_nid); |
| 1359 | if (ngroup == NULL) |
| 1360 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1361 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | goto err; |
| 1363 | } |
| 1364 | if (EC_KEY_set_group(ecdh, ngroup) == 0) |
| 1365 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1366 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1367 | goto err; |
| 1368 | } |
| 1369 | EC_GROUP_free(ngroup); |
| 1370 | |
| 1371 | group = EC_KEY_get0_group(ecdh); |
| 1372 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1373 | /* Next, get the encoded ECPoint */ |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1374 | if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) |
| 1375 | { |
| 1376 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1377 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1378 | goto f_err; |
| 1379 | } |
| 1380 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1381 | if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || |
| 1382 | ((bn_ctx = BN_CTX_new()) == NULL)) |
| 1383 | { |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1384 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1385 | goto err; |
| 1386 | } |
| 1387 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1388 | if (!EC_POINT_oct2point(group, srvr_ecpoint, |
| 1389 | CBS_data(&point), CBS_len(&point), bn_ctx)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1390 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1391 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1392 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1393 | goto f_err; |
| 1394 | } |
| 1395 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1396 | /* The ECC/TLS specification does not mention |
| 1397 | * the use of DSA to sign ECParameters in the server |
| 1398 | * key exchange message. We do support RSA and ECDSA. |
| 1399 | */ |
| 1400 | if (0) ; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1401 | else if (alg_a & SSL_aRSA) |
| 1402 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1403 | else if (alg_a & SSL_aECDSA) |
| 1404 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1405 | /* else anonymous ECDH, so no certificate or pkey. */ |
| 1406 | EC_KEY_set_public_key(ecdh, srvr_ecpoint); |
| 1407 | s->session->sess_cert->peer_ecdh_tmp=ecdh; |
| 1408 | ecdh=NULL; |
| 1409 | BN_CTX_free(bn_ctx); |
| 1410 | bn_ctx = NULL; |
| 1411 | EC_POINT_free(srvr_ecpoint); |
| 1412 | srvr_ecpoint = NULL; |
| 1413 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1414 | |
| 1415 | else if (!(alg_k & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1416 | { |
| 1417 | al=SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1418 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1419 | goto f_err; |
| 1420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1421 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1422 | /* At this point, |server_key_exchange| contains the |
| 1423 | * signature, if any, while |server_key_exchange_orig| |
| 1424 | * contains the entire message. From that, derive a CBS |
| 1425 | * containing just the parameter. */ |
| 1426 | CBS_init(¶meter, CBS_data(&server_key_exchange_orig), |
| 1427 | CBS_len(&server_key_exchange_orig) - |
| 1428 | CBS_len(&server_key_exchange)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1429 | |
| 1430 | /* if it was signed, check the signature */ |
| 1431 | if (pkey != NULL) |
| 1432 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1433 | CBS signature; |
| 1434 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1435 | if (SSL_USE_SIGALGS(s)) |
| 1436 | { |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 1437 | if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1438 | goto f_err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1439 | } |
| 1440 | else |
| 1441 | md = EVP_sha1(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1442 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1443 | /* The last field in |server_key_exchange| is the |
| 1444 | * signature. */ |
| 1445 | if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) || |
| 1446 | CBS_len(&server_key_exchange) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1447 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1448 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1449 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1450 | goto f_err; |
| 1451 | } |
| 1452 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1453 | if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) |
| 1454 | { |
| 1455 | int num; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1456 | unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2]; |
| 1457 | size_t md_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1458 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1459 | q=md_buf; |
| 1460 | for (num=2; num > 0; num--) |
| 1461 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1462 | unsigned int digest_len; |
David Benjamin | e712778 | 2014-08-18 23:15:29 -0400 | [diff] [blame] | 1463 | EVP_DigestInit_ex(&md_ctx, |
| 1464 | (num == 2) ? EVP_md5() : EVP_sha1(), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1465 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1466 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1467 | EVP_DigestUpdate(&md_ctx, CBS_data(¶meter), CBS_len(¶meter)); |
| 1468 | EVP_DigestFinal_ex(&md_ctx, q, &digest_len); |
| 1469 | q += digest_len; |
| 1470 | md_len += digest_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1471 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1472 | if (!RSA_verify(NID_md5_sha1, md_buf, md_len, |
| 1473 | CBS_data(&signature), CBS_len(&signature), |
| 1474 | pkey->pkey.rsa)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1475 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1476 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1477 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1478 | goto f_err; |
| 1479 | } |
| 1480 | } |
| 1481 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1482 | { |
David Benjamin | fd617a5 | 2014-10-31 00:21:46 -0400 | [diff] [blame] | 1483 | if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) || |
| 1484 | !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1485 | !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1486 | !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(¶meter), CBS_len(¶meter)) || |
| 1487 | !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1488 | { |
| 1489 | /* bad signature */ |
| 1490 | al=SSL_AD_DECRYPT_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1491 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1492 | goto f_err; |
| 1493 | } |
| 1494 | } |
| 1495 | } |
| 1496 | else |
| 1497 | { |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 1498 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1499 | { |
| 1500 | /* Might be wrong key type, check it */ |
| 1501 | if (ssl3_check_cert_and_algorithm(s)) |
| 1502 | /* Otherwise this shouldn't happen */ |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1503 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1504 | goto err; |
| 1505 | } |
| 1506 | /* still data left over */ |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1507 | if (CBS_len(&server_key_exchange) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1508 | { |
| 1509 | al=SSL_AD_DECODE_ERROR; |
David Benjamin | 8f8040d | 2014-07-14 19:14:46 -0400 | [diff] [blame] | 1510 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1511 | goto f_err; |
| 1512 | } |
| 1513 | } |
| 1514 | EVP_PKEY_free(pkey); |
| 1515 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1516 | return(1); |
| 1517 | f_err: |
| 1518 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1519 | err: |
| 1520 | EVP_PKEY_free(pkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1521 | if (rsa != NULL) |
| 1522 | RSA_free(rsa); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1523 | if (dh != NULL) |
| 1524 | DH_free(dh); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1525 | BN_CTX_free(bn_ctx); |
| 1526 | EC_POINT_free(srvr_ecpoint); |
| 1527 | if (ecdh != NULL) |
| 1528 | EC_KEY_free(ecdh); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1529 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1530 | return(-1); |
| 1531 | } |
| 1532 | |
| 1533 | static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) |
| 1534 | { |
| 1535 | return(X509_NAME_cmp(*a,*b)); |
| 1536 | } |
| 1537 | |
| 1538 | int ssl3_get_certificate_request(SSL *s) |
| 1539 | { |
| 1540 | int ok,ret=0; |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1541 | unsigned long n; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1542 | X509_NAME *xn=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1543 | STACK_OF(X509_NAME) *ca_sk=NULL; |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1544 | CBS cbs; |
| 1545 | CBS certificate_types; |
| 1546 | CBS certificate_authorities; |
| 1547 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1548 | |
| 1549 | n=s->method->ssl_get_message(s, |
| 1550 | SSL3_ST_CR_CERT_REQ_A, |
| 1551 | SSL3_ST_CR_CERT_REQ_B, |
| 1552 | -1, |
| 1553 | s->max_cert_list, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1554 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1555 | &ok); |
| 1556 | |
| 1557 | if (!ok) return((int)n); |
| 1558 | |
| 1559 | s->s3->tmp.cert_req=0; |
| 1560 | |
| 1561 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) |
| 1562 | { |
| 1563 | s->s3->tmp.reuse_message=1; |
| 1564 | /* If we get here we don't need any cached handshake records |
| 1565 | * as we wont be doing client auth. |
| 1566 | */ |
| 1567 | if (s->s3->handshake_buffer) |
| 1568 | { |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1569 | if (!ssl3_digest_cached_records(s, free_handshake_buffer)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1570 | goto err; |
| 1571 | } |
| 1572 | return(1); |
| 1573 | } |
| 1574 | |
| 1575 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) |
| 1576 | { |
| 1577 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 1578 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE); |
| 1579 | goto err; |
| 1580 | } |
| 1581 | |
| 1582 | /* TLS does not like anon-DH with client cert */ |
| 1583 | if (s->version > SSL3_VERSION) |
| 1584 | { |
| 1585 | if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 1586 | { |
| 1587 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 1588 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); |
| 1589 | goto err; |
| 1590 | } |
| 1591 | } |
| 1592 | |
David Benjamin | 51b1f74 | 2014-07-12 16:31:12 -0400 | [diff] [blame] | 1593 | CBS_init(&cbs, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1594 | |
| 1595 | ca_sk = sk_X509_NAME_new(ca_dn_cmp); |
| 1596 | if (ca_sk == NULL) |
| 1597 | { |
| 1598 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE); |
| 1599 | goto err; |
| 1600 | } |
| 1601 | |
| 1602 | /* get the certificate types */ |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1603 | if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1604 | { |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1605 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1606 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR); |
| 1607 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1608 | } |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 1609 | if (!CBS_stow(&certificate_types, |
| 1610 | &s->s3->tmp.certificate_types, |
| 1611 | &s->s3->tmp.num_certificate_types)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1612 | { |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1613 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1614 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1615 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1616 | if (SSL_USE_SIGALGS(s)) |
| 1617 | { |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1618 | CBS supported_signature_algorithms; |
| 1619 | if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1620 | { |
| 1621 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1622 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1623 | goto err; |
| 1624 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 1625 | if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1626 | { |
| 1627 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1628 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
| 1629 | goto err; |
| 1630 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* get the CA RDNs */ |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1634 | if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1635 | { |
| 1636 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1637 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH); |
| 1638 | goto err; |
| 1639 | } |
| 1640 | |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1641 | while (CBS_len(&certificate_authorities) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1642 | { |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1643 | CBS distinguished_name; |
| 1644 | if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1645 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1646 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1647 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG); |
| 1648 | goto err; |
| 1649 | } |
| 1650 | |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1651 | data = CBS_data(&distinguished_name); |
| 1652 | if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1653 | { |
Alex Chernyakhovsky | 9c890d4 | 2014-07-05 00:53:11 -0400 | [diff] [blame] | 1654 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1655 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB); |
| 1656 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
David Benjamin | 407a10c | 2014-07-16 12:58:59 -0400 | [diff] [blame] | 1659 | if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 1660 | { |
| 1661 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1662 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR); |
| 1663 | goto err; |
| 1664 | } |
| 1665 | if (CBS_len(&distinguished_name) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1666 | { |
| 1667 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1668 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH); |
| 1669 | goto err; |
| 1670 | } |
| 1671 | if (!sk_X509_NAME_push(ca_sk,xn)) |
| 1672 | { |
| 1673 | OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE); |
| 1674 | goto err; |
| 1675 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1678 | /* we should setup a certificate to return.... */ |
| 1679 | s->s3->tmp.cert_req=1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1680 | if (s->s3->tmp.ca_names != NULL) |
| 1681 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); |
| 1682 | s->s3->tmp.ca_names=ca_sk; |
| 1683 | ca_sk=NULL; |
| 1684 | |
| 1685 | ret=1; |
| 1686 | err: |
| 1687 | if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); |
| 1688 | return(ret); |
| 1689 | } |
| 1690 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1691 | int ssl3_get_new_session_ticket(SSL *s) |
| 1692 | { |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1693 | int ok,al,ret=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1694 | long n; |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1695 | CBS new_session_ticket, ticket; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1696 | |
| 1697 | n=s->method->ssl_get_message(s, |
| 1698 | SSL3_ST_CR_SESSION_TICKET_A, |
| 1699 | SSL3_ST_CR_SESSION_TICKET_B, |
David Benjamin | 6444287 | 2014-07-21 17:43:45 -0400 | [diff] [blame] | 1700 | SSL3_MT_NEWSESSION_TICKET, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1701 | 16384, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1702 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1703 | &ok); |
| 1704 | |
| 1705 | if (!ok) |
| 1706 | return((int)n); |
| 1707 | |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1708 | CBS_init(&new_session_ticket, s->init_msg, n); |
| 1709 | |
| 1710 | if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) || |
| 1711 | !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) || |
| 1712 | CBS_len(&new_session_ticket) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1713 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1714 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1715 | OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1716 | goto f_err; |
| 1717 | } |
| 1718 | |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1719 | if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1720 | { |
| 1721 | OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE); |
| 1722 | goto err; |
| 1723 | } |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1724 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1725 | /* There are two ways to detect a resumed ticket sesion. |
| 1726 | * One is to set an appropriate session ID and then the server |
| 1727 | * must return a match in ServerHello. This allows the normal |
| 1728 | * client session ID matching to work and we know much |
| 1729 | * earlier that the ticket has been accepted. |
| 1730 | * |
| 1731 | * The other way is to set zero length session ID when the |
| 1732 | * ticket is presented and rely on the handshake to determine |
| 1733 | * session resumption. |
| 1734 | * |
| 1735 | * We choose the former approach because this fits in with |
| 1736 | * assumptions elsewhere in OpenSSL. The session ID is set |
| 1737 | * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the |
| 1738 | * ticket. |
| 1739 | */ |
David Benjamin | e044c3d | 2014-07-14 19:14:12 -0400 | [diff] [blame] | 1740 | EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1741 | s->session->session_id, &s->session->session_id_length, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1742 | EVP_sha256(), NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1743 | ret=1; |
| 1744 | return(ret); |
| 1745 | f_err: |
| 1746 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1747 | err: |
| 1748 | return(-1); |
| 1749 | } |
| 1750 | |
| 1751 | int ssl3_get_cert_status(SSL *s) |
| 1752 | { |
| 1753 | int ok, al; |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1754 | long n; |
| 1755 | CBS certificate_status, ocsp_response; |
| 1756 | uint8_t status_type; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1757 | |
| 1758 | n=s->method->ssl_get_message(s, |
| 1759 | SSL3_ST_CR_CERT_STATUS_A, |
| 1760 | SSL3_ST_CR_CERT_STATUS_B, |
| 1761 | SSL3_MT_CERTIFICATE_STATUS, |
| 1762 | 16384, |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1763 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1764 | &ok); |
| 1765 | |
| 1766 | if (!ok) return((int)n); |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1767 | |
| 1768 | CBS_init(&certificate_status, s->init_msg, n); |
| 1769 | if (!CBS_get_u8(&certificate_status, &status_type) || |
| 1770 | status_type != TLSEXT_STATUSTYPE_ocsp || |
| 1771 | !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) || |
| 1772 | CBS_len(&ocsp_response) == 0 || |
| 1773 | CBS_len(&certificate_status) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1774 | { |
| 1775 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1776 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1777 | goto f_err; |
| 1778 | } |
David Benjamin | 4606268 | 2014-07-14 19:14:32 -0400 | [diff] [blame] | 1779 | |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1780 | if (!CBS_stow(&ocsp_response, |
| 1781 | &s->session->ocsp_response, &s->session->ocsp_response_length)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1782 | { |
| 1783 | al = SSL_AD_INTERNAL_ERROR; |
| 1784 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE); |
| 1785 | goto f_err; |
| 1786 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1787 | return 1; |
| 1788 | f_err: |
| 1789 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1790 | return(-1); |
| 1791 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1792 | |
| 1793 | int ssl3_get_server_done(SSL *s) |
| 1794 | { |
| 1795 | int ok,ret=0; |
| 1796 | long n; |
| 1797 | |
| 1798 | n=s->method->ssl_get_message(s, |
| 1799 | SSL3_ST_CR_SRVR_DONE_A, |
| 1800 | SSL3_ST_CR_SRVR_DONE_B, |
| 1801 | SSL3_MT_SERVER_DONE, |
| 1802 | 30, /* should be very small, like 0 :-) */ |
David Benjamin | 590cbe9 | 2014-08-25 21:34:56 -0400 | [diff] [blame] | 1803 | SSL_GET_MESSAGE_HASH_MESSAGE, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1804 | &ok); |
| 1805 | |
| 1806 | if (!ok) return((int)n); |
| 1807 | if (n > 0) |
| 1808 | { |
| 1809 | /* should contain no data */ |
| 1810 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
| 1811 | OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH); |
| 1812 | return -1; |
| 1813 | } |
| 1814 | ret=1; |
| 1815 | return(ret); |
| 1816 | } |
| 1817 | |
| 1818 | |
| 1819 | int ssl3_send_client_key_exchange(SSL *s) |
| 1820 | { |
| 1821 | unsigned char *p; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | int n = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1823 | unsigned long alg_k; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1824 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1825 | unsigned char *q; |
| 1826 | EVP_PKEY *pkey=NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1827 | EC_KEY *clnt_ecdh = NULL; |
| 1828 | const EC_POINT *srvr_ecpoint = NULL; |
| 1829 | EVP_PKEY *srvr_pub_pkey = NULL; |
| 1830 | unsigned char *encodedPoint = NULL; |
| 1831 | int encoded_pt_len = 0; |
| 1832 | BN_CTX * bn_ctx = NULL; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1833 | unsigned int psk_len = 0; |
| 1834 | unsigned char psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1835 | uint8_t *pms = NULL; |
| 1836 | size_t pms_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1837 | |
| 1838 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) |
| 1839 | { |
| 1840 | p = ssl_handshake_start(s); |
| 1841 | |
| 1842 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1843 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 1844 | |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1845 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1846 | if (alg_a & SSL_aPSK) |
| 1847 | { |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1848 | char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 1849 | size_t identity_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1850 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1851 | if (s->psk_client_callback == NULL) |
| 1852 | { |
| 1853 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB); |
| 1854 | goto err; |
| 1855 | } |
| 1856 | |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1857 | memset(identity, 0, sizeof(identity)); |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 1858 | psk_len = s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1859 | identity, sizeof(identity), psk, sizeof(psk)); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1860 | if (psk_len > PSK_MAX_PSK_LEN) |
| 1861 | { |
| 1862 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1863 | goto err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1864 | } |
| 1865 | else if (psk_len == 0) |
| 1866 | { |
| 1867 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1868 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1869 | goto err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1870 | } |
Adam Langley | 01797e3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1871 | identity_len = OPENSSL_strnlen(identity, sizeof(identity)); |
| 1872 | if (identity_len > PSK_MAX_IDENTITY_LEN) |
| 1873 | { |
| 1874 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1875 | goto err; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1878 | if (s->session->psk_identity != NULL) |
| 1879 | OPENSSL_free(s->session->psk_identity); |
| 1880 | s->session->psk_identity = BUF_strdup(identity); |
| 1881 | if (s->session->psk_identity == NULL) |
| 1882 | { |
| 1883 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1884 | goto err; |
| 1885 | } |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1886 | |
| 1887 | /* Write out psk_identity. */ |
| 1888 | s2n(identity_len, p); |
| 1889 | memcpy(p, identity, identity_len); |
| 1890 | p += identity_len; |
| 1891 | n = 2 + identity_len; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1892 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1893 | |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1894 | /* Depending on the key exchange method, compute |pms| |
| 1895 | * and |pms_len|. */ |
David Benjamin | 457112e | 2014-08-27 14:35:09 -0400 | [diff] [blame] | 1896 | if (alg_k & SSL_kRSA) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1897 | { |
| 1898 | RSA *rsa; |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1899 | size_t enc_pms_len; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1900 | |
| 1901 | pms_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1902 | pms = OPENSSL_malloc(pms_len); |
| 1903 | if (pms == NULL) |
| 1904 | { |
| 1905 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 1906 | goto err; |
| 1907 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1908 | |
Emilia Käsper | c3d7960 | 2014-07-24 22:15:29 +0200 | [diff] [blame] | 1909 | if (s->session->sess_cert == NULL) |
| 1910 | { |
| 1911 | /* We should always have a server certificate with SSL_kRSA. */ |
| 1912 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1913 | goto err; |
| 1914 | } |
| 1915 | |
David Benjamin | 525a0fe | 2014-11-08 11:41:12 -0500 | [diff] [blame] | 1916 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
| 1917 | if ((pkey == NULL) || |
| 1918 | (pkey->type != EVP_PKEY_RSA) || |
| 1919 | (pkey->pkey.rsa == NULL)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1920 | { |
David Benjamin | 525a0fe | 2014-11-08 11:41:12 -0500 | [diff] [blame] | 1921 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
David Benjamin | 1df1124 | 2014-11-08 11:41:13 -0500 | [diff] [blame] | 1922 | if (pkey != NULL) |
| 1923 | EVP_PKEY_free(pkey); |
David Benjamin | 525a0fe | 2014-11-08 11:41:12 -0500 | [diff] [blame] | 1924 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1925 | } |
David Benjamin | 525a0fe | 2014-11-08 11:41:12 -0500 | [diff] [blame] | 1926 | rsa=pkey->pkey.rsa; |
| 1927 | EVP_PKEY_free(pkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1928 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1929 | pms[0]=s->client_version>>8; |
| 1930 | pms[1]=s->client_version&0xff; |
| 1931 | if (RAND_bytes(&pms[2],SSL_MAX_MASTER_KEY_LENGTH-2) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1932 | goto err; |
| 1933 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1934 | s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1935 | |
| 1936 | q=p; |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1937 | /* In TLS and beyond, reserve space for the length prefix. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1938 | if (s->version > SSL3_VERSION) |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1939 | { |
| 1940 | p += 2; |
| 1941 | n += 2; |
| 1942 | } |
| 1943 | if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), |
| 1944 | pms, pms_len, RSA_PKCS1_PADDING)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1945 | { |
| 1946 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT); |
| 1947 | goto err; |
| 1948 | } |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1949 | n += enc_pms_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1950 | |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 1951 | /* Log the premaster secret, if logging is enabled. */ |
| 1952 | if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1953 | p, enc_pms_len, pms, pms_len)) |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 1954 | { |
| 1955 | goto err; |
| 1956 | } |
| 1957 | |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1958 | /* Fill in the length prefix. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1959 | if (s->version > SSL3_VERSION) |
| 1960 | { |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1961 | s2n(enc_pms_len, q); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1962 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1963 | } |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 1964 | else if (alg_k & SSL_kEDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1965 | { |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1966 | DH *dh_srvr, *dh_clnt; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1967 | SESS_CERT *scert = s->session->sess_cert; |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 1968 | int dh_len; |
| 1969 | size_t pub_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1970 | |
| 1971 | if (scert == NULL) |
| 1972 | { |
| 1973 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 1974 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
| 1975 | goto err; |
| 1976 | } |
| 1977 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 1978 | if (scert->peer_dh_tmp == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1979 | { |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 1980 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 1981 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1982 | } |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 1983 | dh_srvr=scert->peer_dh_tmp; |
| 1984 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1985 | /* generate a new random key */ |
| 1986 | if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1987 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1988 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB); |
| 1989 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1990 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1991 | if (!DH_generate_key(dh_clnt)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1992 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1993 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB); |
| 1994 | DH_free(dh_clnt); |
| 1995 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1998 | pms_len = DH_size(dh_clnt); |
| 1999 | pms = OPENSSL_malloc(pms_len); |
| 2000 | if (pms == NULL) |
| 2001 | { |
| 2002 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2003 | DH_free(dh_clnt); |
| 2004 | goto err; |
| 2005 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2006 | |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2007 | dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt); |
| 2008 | if (dh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2009 | { |
| 2010 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB); |
| 2011 | DH_free(dh_clnt); |
| 2012 | goto err; |
| 2013 | } |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2014 | pms_len = dh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2015 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2016 | /* send off the data */ |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2017 | pub_len = BN_num_bytes(dh_clnt->pub_key); |
| 2018 | s2n(pub_len, p); |
| 2019 | BN_bn2bin(dh_clnt->pub_key, p); |
| 2020 | n += 2 + pub_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2021 | |
| 2022 | DH_free(dh_clnt); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2023 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2024 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2025 | else if (alg_k & SSL_kEECDH) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2026 | { |
| 2027 | const EC_GROUP *srvr_group = NULL; |
| 2028 | EC_KEY *tkey; |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2029 | int field_size = 0, ecdh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2030 | |
Adam Langley | d06eddd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2031 | if (s->session->sess_cert == NULL) |
| 2032 | { |
| 2033 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
| 2034 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE); |
| 2035 | goto err; |
| 2036 | } |
| 2037 | |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2038 | if (s->session->sess_cert->peer_ecdh_tmp == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2039 | { |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2040 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2041 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2042 | } |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2043 | tkey = s->session->sess_cert->peer_ecdh_tmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2044 | |
| 2045 | srvr_group = EC_KEY_get0_group(tkey); |
| 2046 | srvr_ecpoint = EC_KEY_get0_public_key(tkey); |
| 2047 | |
| 2048 | if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) |
| 2049 | { |
| 2050 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2051 | goto err; |
| 2052 | } |
| 2053 | |
| 2054 | if ((clnt_ecdh=EC_KEY_new()) == NULL) |
| 2055 | { |
| 2056 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2057 | goto err; |
| 2058 | } |
| 2059 | |
| 2060 | if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) |
| 2061 | { |
| 2062 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB); |
| 2063 | goto err; |
| 2064 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2065 | /* Generate a new ECDH key pair */ |
| 2066 | if (!(EC_KEY_generate_key(clnt_ecdh))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2067 | { |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2068 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB); |
| 2069 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2072 | field_size = EC_GROUP_get_degree(srvr_group); |
| 2073 | if (field_size <= 0) |
| 2074 | { |
| 2075 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB); |
| 2076 | goto err; |
| 2077 | } |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2078 | |
| 2079 | pms_len = (field_size + 7) / 8; |
| 2080 | pms = OPENSSL_malloc(pms_len); |
| 2081 | if (pms == NULL) |
| 2082 | { |
| 2083 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2084 | goto err; |
| 2085 | } |
| 2086 | |
| 2087 | ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL); |
| 2088 | if (ecdh_len <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2089 | { |
| 2090 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB); |
| 2091 | goto err; |
| 2092 | } |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2093 | pms_len = ecdh_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2094 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2095 | /* First check the size of encoding and |
| 2096 | * allocate memory accordingly. |
| 2097 | */ |
| 2098 | encoded_pt_len = |
| 2099 | EC_POINT_point2oct(srvr_group, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2100 | EC_KEY_get0_public_key(clnt_ecdh), |
| 2101 | POINT_CONVERSION_UNCOMPRESSED, |
| 2102 | NULL, 0, NULL); |
| 2103 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2104 | encodedPoint = (unsigned char *) |
| 2105 | OPENSSL_malloc(encoded_pt_len * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2106 | sizeof(unsigned char)); |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2107 | bn_ctx = BN_CTX_new(); |
| 2108 | if ((encodedPoint == NULL) || |
| 2109 | (bn_ctx == NULL)) |
| 2110 | { |
| 2111 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2112 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2115 | /* Encode the public key */ |
| 2116 | encoded_pt_len = EC_POINT_point2oct(srvr_group, |
| 2117 | EC_KEY_get0_public_key(clnt_ecdh), |
| 2118 | POINT_CONVERSION_UNCOMPRESSED, |
| 2119 | encodedPoint, encoded_pt_len, bn_ctx); |
| 2120 | |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2121 | *p = encoded_pt_len; /* length of encoded point */ |
| 2122 | /* Encoded point will be copied here */ |
| 2123 | p += 1; |
| 2124 | n += 1; |
| 2125 | /* copy the point */ |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2126 | memcpy(p, encodedPoint, encoded_pt_len); |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 2127 | /* increment n to account for length field */ |
| 2128 | n += encoded_pt_len; |
| 2129 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2130 | /* Free allocated memory */ |
| 2131 | BN_CTX_free(bn_ctx); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2132 | bn_ctx = NULL; |
Adam Langley | b2cb0ec | 2014-09-02 14:28:49 -0700 | [diff] [blame] | 2133 | OPENSSL_free(encodedPoint); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2134 | encodedPoint = NULL; |
Adam Langley | b2cb0ec | 2014-09-02 14:28:49 -0700 | [diff] [blame] | 2135 | EC_KEY_free(clnt_ecdh); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2136 | clnt_ecdh = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2137 | EVP_PKEY_free(srvr_pub_pkey); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2138 | srvr_pub_pkey = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2139 | } |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2140 | else if (alg_k & SSL_kPSK) |
| 2141 | { |
| 2142 | /* For plain PSK, other_secret is a block of 0s with the same |
| 2143 | * length as the pre-shared key. */ |
| 2144 | pms_len = psk_len; |
| 2145 | pms = OPENSSL_malloc(pms_len); |
| 2146 | if (pms == NULL) |
| 2147 | { |
| 2148 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2149 | goto err; |
| 2150 | } |
| 2151 | memset(pms, 0, pms_len); |
| 2152 | } |
| 2153 | else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2154 | { |
| 2155 | ssl3_send_alert(s, SSL3_AL_FATAL, |
| 2156 | SSL_AD_HANDSHAKE_FAILURE); |
| 2157 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2158 | goto err; |
| 2159 | } |
| 2160 | |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2161 | /* For a PSK cipher suite, other_secret is combined |
| 2162 | * with the pre-shared key. */ |
David Benjamin | 1f10d9c | 2014-11-08 11:41:14 -0500 | [diff] [blame] | 2163 | if (alg_a & SSL_aPSK) |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2164 | { |
| 2165 | CBB cbb, child; |
| 2166 | uint8_t *new_pms; |
| 2167 | size_t new_pms_len; |
| 2168 | |
| 2169 | if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) |
| 2170 | { |
| 2171 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE); |
| 2172 | goto err; |
| 2173 | } |
| 2174 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 2175 | !CBB_add_bytes(&child, pms, pms_len) || |
| 2176 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 2177 | !CBB_add_bytes(&child, psk, psk_len) || |
| 2178 | !CBB_finish(&cbb, &new_pms, &new_pms_len)) |
| 2179 | { |
| 2180 | CBB_cleanup(&cbb); |
| 2181 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR); |
| 2182 | goto err; |
| 2183 | } |
| 2184 | OPENSSL_cleanse(pms, pms_len); |
| 2185 | OPENSSL_free(pms); |
| 2186 | pms = new_pms; |
| 2187 | pms_len = new_pms_len; |
| 2188 | } |
| 2189 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2190 | ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n); |
| 2191 | s->state=SSL3_ST_CW_KEY_EXCH_B; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2192 | |
| 2193 | /* The message must be added to the finished hash before |
| 2194 | * calculating the master secret. */ |
| 2195 | s->method->ssl3_enc->add_to_finished_hash(s); |
| 2196 | |
| 2197 | s->session->master_key_length = |
| 2198 | s->method->ssl3_enc->generate_master_secret(s, |
| 2199 | s->session->master_key, |
| 2200 | pms, pms_len); |
| 2201 | if (s->session->master_key_length == 0) |
| 2202 | { |
| 2203 | goto err; |
| 2204 | } |
| 2205 | s->session->extended_master_secret = s->s3->tmp.extended_master_secret; |
| 2206 | OPENSSL_cleanse(pms, pms_len); |
| 2207 | OPENSSL_free(pms); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | /* SSL3_ST_CW_KEY_EXCH_B */ |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2211 | /* The message has already been added to the finished hash. */ |
| 2212 | return s->method->ssl3_enc->do_write(s, dont_add_to_finished_hash); |
| 2213 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2214 | err: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2215 | BN_CTX_free(bn_ctx); |
| 2216 | if (encodedPoint != NULL) OPENSSL_free(encodedPoint); |
| 2217 | if (clnt_ecdh != NULL) |
| 2218 | EC_KEY_free(clnt_ecdh); |
| 2219 | EVP_PKEY_free(srvr_pub_pkey); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2220 | if (pms) |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2221 | { |
| 2222 | OPENSSL_cleanse(pms, pms_len); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2223 | OPENSSL_free(pms); |
David Benjamin | 93d67d3 | 2014-10-28 00:53:38 -0400 | [diff] [blame] | 2224 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2225 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2228 | int ssl3_send_cert_verify(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2229 | { |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2230 | unsigned char *buf, *p; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2231 | const EVP_MD *md = NULL; |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2232 | uint8_t digest[EVP_MAX_MD_SIZE]; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2233 | size_t digest_length; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2234 | EVP_PKEY *pkey; |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2235 | EVP_PKEY_CTX *pctx = NULL; |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2236 | size_t signature_length = 0; |
| 2237 | unsigned long n = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2238 | |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2239 | buf=(unsigned char *)s->init_buf->data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2240 | |
| 2241 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) |
| 2242 | { |
| 2243 | p= ssl_handshake_start(s); |
Adam Langley | aed2306 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2244 | pkey = s->cert->key->privatekey; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2245 | |
| 2246 | /* Write out the digest type if needbe. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2247 | if (SSL_USE_SIGALGS(s)) |
| 2248 | { |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2249 | md = tls1_choose_signing_digest(s, pkey); |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2250 | if (!tls12_get_sigandhash(p, pkey, md)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2251 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2252 | OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2253 | goto err; |
| 2254 | } |
| 2255 | p += 2; |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2256 | n += 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2257 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2258 | |
| 2259 | /* Compute the digest. */ |
| 2260 | if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2261 | goto err; |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2262 | |
| 2263 | /* The handshake buffer is no longer necessary. */ |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2264 | if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, free_handshake_buffer)) |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2265 | goto err; |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2266 | |
| 2267 | /* Sign the digest. */ |
| 2268 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 2269 | if (pctx == NULL) |
| 2270 | goto err; |
| 2271 | |
| 2272 | /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */ |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2273 | if (!EVP_PKEY_sign_init(pctx) || |
| 2274 | !EVP_PKEY_CTX_set_signature_md(pctx, md) || |
| 2275 | !EVP_PKEY_sign(pctx, NULL, &signature_length, |
| 2276 | digest, digest_length)) |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2277 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2278 | OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB); |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2279 | goto err; |
| 2280 | } |
| 2281 | |
| 2282 | if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) |
| 2283 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2284 | OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG); |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2285 | goto err; |
| 2286 | } |
| 2287 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2288 | if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, |
| 2289 | digest, digest_length)) |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2290 | { |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 2291 | OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB); |
David Benjamin | 0e2908a | 2014-07-09 15:22:43 -0400 | [diff] [blame] | 2292 | goto err; |
| 2293 | } |
| 2294 | |
| 2295 | s2n(signature_length, p); |
| 2296 | n += signature_length + 2; |
| 2297 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2298 | ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n); |
| 2299 | s->state=SSL3_ST_CW_CERT_VRFY_B; |
| 2300 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2301 | EVP_PKEY_CTX_free(pctx); |
| 2302 | return ssl_do_write(s); |
| 2303 | err: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2304 | EVP_PKEY_CTX_free(pctx); |
| 2305 | return(-1); |
| 2306 | } |
| 2307 | |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2308 | /* ssl3_has_client_certificate returns true if a client certificate is |
| 2309 | * configured. */ |
| 2310 | static int ssl3_has_client_certificate(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2311 | { |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2312 | return s->cert && s->cert->key->x509 && s->cert->key->privatekey; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | int ssl3_send_client_certificate(SSL *s) |
| 2316 | { |
| 2317 | X509 *x509=NULL; |
| 2318 | EVP_PKEY *pkey=NULL; |
| 2319 | int i; |
| 2320 | |
| 2321 | if (s->state == SSL3_ST_CW_CERT_A) |
| 2322 | { |
| 2323 | /* Let cert callback update client certificates if required */ |
| 2324 | if (s->cert->cert_cb) |
| 2325 | { |
| 2326 | i = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 2327 | if (i < 0) |
| 2328 | { |
| 2329 | s->rwstate=SSL_X509_LOOKUP; |
| 2330 | return -1; |
| 2331 | } |
| 2332 | if (i == 0) |
| 2333 | { |
| 2334 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR); |
| 2335 | return 0; |
| 2336 | } |
| 2337 | s->rwstate=SSL_NOTHING; |
| 2338 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2339 | if (ssl3_has_client_certificate(s)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2340 | s->state=SSL3_ST_CW_CERT_C; |
| 2341 | else |
| 2342 | s->state=SSL3_ST_CW_CERT_B; |
| 2343 | } |
| 2344 | |
| 2345 | /* We need to get a client cert */ |
| 2346 | if (s->state == SSL3_ST_CW_CERT_B) |
| 2347 | { |
| 2348 | /* If we get an error, we need to |
| 2349 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); |
David Benjamin | 7bbeead | 2014-07-25 03:56:20 -0400 | [diff] [blame] | 2350 | * We then get retried later */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2351 | i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 2352 | if (i < 0) |
| 2353 | { |
| 2354 | s->rwstate=SSL_X509_LOOKUP; |
| 2355 | return(-1); |
| 2356 | } |
| 2357 | s->rwstate=SSL_NOTHING; |
| 2358 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) |
| 2359 | { |
| 2360 | s->state=SSL3_ST_CW_CERT_B; |
| 2361 | if ( !SSL_use_certificate(s,x509) || |
| 2362 | !SSL_use_PrivateKey(s,pkey)) |
| 2363 | i=0; |
| 2364 | } |
| 2365 | else if (i == 1) |
| 2366 | { |
| 2367 | i=0; |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2368 | OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | if (x509 != NULL) X509_free(x509); |
| 2372 | if (pkey != NULL) EVP_PKEY_free(pkey); |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2373 | if (i && !ssl3_has_client_certificate(s)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2374 | i = 0; |
| 2375 | if (i == 0) |
| 2376 | { |
| 2377 | if (s->version == SSL3_VERSION) |
| 2378 | { |
| 2379 | s->s3->tmp.cert_req=0; |
| 2380 | ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); |
| 2381 | return(1); |
| 2382 | } |
| 2383 | else |
| 2384 | { |
| 2385 | s->s3->tmp.cert_req=2; |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /* Ok, we have a cert */ |
| 2390 | s->state=SSL3_ST_CW_CERT_C; |
| 2391 | } |
| 2392 | |
| 2393 | if (s->state == SSL3_ST_CW_CERT_C) |
| 2394 | { |
| 2395 | s->state=SSL3_ST_CW_CERT_D; |
| 2396 | ssl3_output_cert_chain(s, |
| 2397 | (s->s3->tmp.cert_req == 2)?NULL:s->cert->key); |
| 2398 | } |
| 2399 | /* SSL3_ST_CW_CERT_D */ |
| 2400 | return ssl_do_write(s); |
| 2401 | } |
| 2402 | |
| 2403 | #define has_bits(i,m) (((i)&(m)) == (m)) |
| 2404 | |
| 2405 | int ssl3_check_cert_and_algorithm(SSL *s) |
| 2406 | { |
| 2407 | int i,idx; |
| 2408 | long alg_k,alg_a; |
| 2409 | EVP_PKEY *pkey=NULL; |
| 2410 | SESS_CERT *sc; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2411 | DH *dh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2412 | |
David Benjamin | e8f3d66 | 2014-07-12 01:10:19 -0400 | [diff] [blame] | 2413 | /* we don't have a certificate */ |
| 2414 | if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) |
| 2415 | return 1; |
| 2416 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2417 | alg_k=s->s3->tmp.new_cipher->algorithm_mkey; |
| 2418 | alg_a=s->s3->tmp.new_cipher->algorithm_auth; |
| 2419 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2420 | sc=s->session->sess_cert; |
| 2421 | if (sc == NULL) |
| 2422 | { |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2423 | OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2424 | goto err; |
| 2425 | } |
| 2426 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2427 | dh=s->session->sess_cert->peer_dh_tmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2428 | |
| 2429 | /* This is the passed certificate */ |
| 2430 | |
| 2431 | idx=sc->peer_cert_type; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2432 | if (idx == SSL_PKEY_ECC) |
| 2433 | { |
| 2434 | if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, |
| 2435 | s) == 0) |
| 2436 | { /* check failed */ |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2437 | OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2438 | goto f_err; |
| 2439 | } |
| 2440 | else |
| 2441 | { |
| 2442 | return 1; |
| 2443 | } |
| 2444 | } |
| 2445 | else if (alg_a & SSL_aECDSA) |
| 2446 | { |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2447 | OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_ECDSA_SIGNING_CERT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2448 | goto f_err; |
| 2449 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2450 | pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); |
| 2451 | i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); |
| 2452 | EVP_PKEY_free(pkey); |
| 2453 | |
| 2454 | |
| 2455 | /* Check that we have a certificate if we require one */ |
| 2456 | if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) |
| 2457 | { |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2458 | OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_SIGNING_CERT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2459 | goto f_err; |
| 2460 | } |
David Benjamin | 525a0fe | 2014-11-08 11:41:12 -0500 | [diff] [blame] | 2461 | if ((alg_k & SSL_kRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_ENC)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2462 | { |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2463 | OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_RSA_ENCRYPTING_CERT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2464 | goto f_err; |
| 2465 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2466 | if ((alg_k & SSL_kEDH) && |
| 2467 | !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) |
| 2468 | { |
HÃ¥vard Molland | 90974e7 | 2014-08-11 15:50:54 +0200 | [diff] [blame] | 2469 | OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2470 | goto f_err; |
| 2471 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2472 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2473 | return(1); |
| 2474 | f_err: |
| 2475 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
| 2476 | err: |
| 2477 | return(0); |
| 2478 | } |
| 2479 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2480 | int ssl3_send_next_proto(SSL *s) |
| 2481 | { |
| 2482 | unsigned int len, padding_len; |
| 2483 | unsigned char *d; |
| 2484 | |
| 2485 | if (s->state == SSL3_ST_CW_NEXT_PROTO_A) |
| 2486 | { |
| 2487 | len = s->next_proto_negotiated_len; |
| 2488 | padding_len = 32 - ((len + 2) % 32); |
| 2489 | d = (unsigned char *)s->init_buf->data; |
| 2490 | d[4] = len; |
| 2491 | memcpy(d + 5, s->next_proto_negotiated, len); |
| 2492 | d[5 + len] = padding_len; |
| 2493 | memset(d + 6 + len, 0, padding_len); |
| 2494 | *(d++)=SSL3_MT_NEXT_PROTO; |
| 2495 | l2n3(2 + len + padding_len, d); |
| 2496 | s->state = SSL3_ST_CW_NEXT_PROTO_B; |
| 2497 | s->init_num = 4 + 2 + len + padding_len; |
| 2498 | s->init_off = 0; |
| 2499 | } |
| 2500 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2501 | return ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2502 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2503 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2504 | |
| 2505 | int ssl3_send_channel_id(SSL *s) |
| 2506 | { |
| 2507 | unsigned char *d; |
| 2508 | int ret = -1, public_key_len; |
| 2509 | EVP_MD_CTX md_ctx; |
| 2510 | size_t sig_len; |
| 2511 | ECDSA_SIG *sig = NULL; |
| 2512 | unsigned char *public_key = NULL, *derp, *der_sig = NULL; |
| 2513 | |
| 2514 | if (s->state != SSL3_ST_CW_CHANNEL_ID_A) |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2515 | return ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2516 | |
| 2517 | if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) |
| 2518 | { |
| 2519 | EVP_PKEY *key = NULL; |
| 2520 | s->ctx->channel_id_cb(s, &key); |
| 2521 | if (key != NULL) |
| 2522 | { |
| 2523 | s->tlsext_channel_id_private = key; |
| 2524 | } |
| 2525 | } |
| 2526 | if (!s->tlsext_channel_id_private) |
| 2527 | { |
| 2528 | s->rwstate=SSL_CHANNEL_ID_LOOKUP; |
| 2529 | return (-1); |
| 2530 | } |
| 2531 | s->rwstate=SSL_NOTHING; |
| 2532 | |
| 2533 | d = (unsigned char *)s->init_buf->data; |
| 2534 | *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS; |
| 2535 | l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d); |
| 2536 | if (s->s3->tlsext_channel_id_new) |
| 2537 | s2n(TLSEXT_TYPE_channel_id_new, d); |
| 2538 | else |
| 2539 | s2n(TLSEXT_TYPE_channel_id, d); |
| 2540 | s2n(TLSEXT_CHANNEL_ID_SIZE, d); |
| 2541 | |
| 2542 | EVP_MD_CTX_init(&md_ctx); |
| 2543 | |
| 2544 | public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL); |
| 2545 | if (public_key_len <= 0) |
| 2546 | { |
| 2547 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY); |
| 2548 | goto err; |
| 2549 | } |
| 2550 | /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a |
| 2551 | * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y |
| 2552 | * field elements as 32-byte, big-endian numbers. */ |
| 2553 | if (public_key_len != 65) |
| 2554 | { |
| 2555 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256); |
| 2556 | goto err; |
| 2557 | } |
| 2558 | public_key = OPENSSL_malloc(public_key_len); |
| 2559 | if (!public_key) |
| 2560 | { |
| 2561 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE); |
| 2562 | goto err; |
| 2563 | } |
| 2564 | |
| 2565 | derp = public_key; |
| 2566 | i2d_PublicKey(s->tlsext_channel_id_private, &derp); |
| 2567 | |
| 2568 | if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, |
| 2569 | s->tlsext_channel_id_private) != 1) |
| 2570 | { |
| 2571 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED); |
| 2572 | goto err; |
| 2573 | } |
| 2574 | |
| 2575 | if (!tls1_channel_id_hash(&md_ctx, s)) |
| 2576 | goto err; |
| 2577 | |
| 2578 | if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) |
| 2579 | { |
| 2580 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED); |
| 2581 | goto err; |
| 2582 | } |
| 2583 | |
| 2584 | der_sig = OPENSSL_malloc(sig_len); |
| 2585 | if (!der_sig) |
| 2586 | { |
| 2587 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE); |
| 2588 | goto err; |
| 2589 | } |
| 2590 | |
| 2591 | if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) |
| 2592 | { |
| 2593 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED); |
| 2594 | goto err; |
| 2595 | } |
| 2596 | |
| 2597 | derp = der_sig; |
| 2598 | sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len); |
| 2599 | if (sig == NULL) |
| 2600 | { |
| 2601 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG); |
| 2602 | goto err; |
| 2603 | } |
| 2604 | |
| 2605 | /* The first byte of public_key will be 0x4, denoting an uncompressed key. */ |
| 2606 | memcpy(d, public_key + 1, 64); |
| 2607 | d += 64; |
David Benjamin | f2b32a2 | 2014-11-02 17:32:40 -0500 | [diff] [blame] | 2608 | if (!BN_bn2bin_padded(d, 32, sig->r) || |
| 2609 | !BN_bn2bin_padded(d + 32, 32, sig->s)) |
| 2610 | { |
| 2611 | OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR); |
| 2612 | goto err; |
| 2613 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2614 | |
| 2615 | s->state = SSL3_ST_CW_CHANNEL_ID_B; |
| 2616 | s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE; |
| 2617 | s->init_off = 0; |
| 2618 | |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2619 | ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE, add_to_finished_hash); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2620 | |
| 2621 | err: |
| 2622 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2623 | if (public_key) |
| 2624 | OPENSSL_free(public_key); |
| 2625 | if (der_sig) |
| 2626 | OPENSSL_free(der_sig); |
| 2627 | if (sig) |
| 2628 | ECDSA_SIG_free(sig); |
| 2629 | |
| 2630 | return ret; |
| 2631 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2632 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2633 | int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) |
| 2634 | { |
| 2635 | int i = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2636 | if (s->ctx->client_cert_cb) |
| 2637 | i = s->ctx->client_cert_cb(s,px509,ppkey); |
| 2638 | return i; |
| 2639 | } |