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