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