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