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