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