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