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