Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DTLS implementation written by Nagendra Modadugu |
| 3 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. |
| 4 | */ |
| 5 | /* ==================================================================== |
| 6 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * openssl-core@OpenSSL.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). |
| 56 | * |
| 57 | */ |
| 58 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 59 | * All rights reserved. |
| 60 | * |
| 61 | * This package is an SSL implementation written |
| 62 | * by Eric Young (eay@cryptsoft.com). |
| 63 | * The implementation was written so as to conform with Netscapes SSL. |
| 64 | * |
| 65 | * This library is free for commercial and non-commercial use as long as |
| 66 | * the following conditions are aheared to. The following conditions |
| 67 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 68 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 69 | * included with this distribution is covered by the same copyright terms |
| 70 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 71 | * |
| 72 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 73 | * the code are not to be removed. |
| 74 | * If this package is used in a product, Eric Young should be given attribution |
| 75 | * as the author of the parts of the library used. |
| 76 | * This can be in the form of a textual message at program startup or |
| 77 | * in documentation (online or textual) provided with the package. |
| 78 | * |
| 79 | * Redistribution and use in source and binary forms, with or without |
| 80 | * modification, are permitted provided that the following conditions |
| 81 | * are met: |
| 82 | * 1. Redistributions of source code must retain the copyright |
| 83 | * notice, this list of conditions and the following disclaimer. |
| 84 | * 2. Redistributions in binary form must reproduce the above copyright |
| 85 | * notice, this list of conditions and the following disclaimer in the |
| 86 | * documentation and/or other materials provided with the distribution. |
| 87 | * 3. All advertising materials mentioning features or use of this software |
| 88 | * must display the following acknowledgement: |
| 89 | * "This product includes cryptographic software written by |
| 90 | * Eric Young (eay@cryptsoft.com)" |
| 91 | * The word 'cryptographic' can be left out if the rouines from the library |
| 92 | * being used are not cryptographic related :-). |
| 93 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 94 | * the apps directory (application code) you must include an acknowledgement: |
| 95 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 96 | * |
| 97 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 98 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 99 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 100 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 101 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 102 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 103 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 104 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 105 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 106 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 107 | * SUCH DAMAGE. |
| 108 | * |
| 109 | * The licence and distribution terms for any publically available version or |
| 110 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 111 | * copied and put under another distribution licence |
| 112 | * [including the GNU Public Licence.] |
| 113 | */ |
| 114 | |
David Benjamin | 0b145c2 | 2014-11-26 20:10:09 -0500 | [diff] [blame] | 115 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | #include <stdio.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 117 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | |
| 119 | #include <openssl/bn.h> |
| 120 | #include <openssl/buf.h> |
| 121 | #include <openssl/dh.h> |
| 122 | #include <openssl/evp.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 123 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #include <openssl/md5.h> |
| 125 | #include <openssl/mem.h> |
| 126 | #include <openssl/obj.h> |
| 127 | #include <openssl/rand.h> |
| 128 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 129 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | static int dtls1_get_hello_verify(SSL *s); |
| 132 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 133 | int dtls1_connect(SSL *s) { |
| 134 | BUF_MEM *buf = NULL; |
| 135 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 136 | int ret = -1; |
| 137 | int new_state, state, skip = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 139 | assert(s->handshake_func == dtls1_connect); |
| 140 | assert(!s->server); |
| 141 | assert(SSL_IS_DTLS(s)); |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 142 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 143 | ERR_clear_error(); |
| 144 | ERR_clear_system_error(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 146 | if (s->info_callback != NULL) { |
| 147 | cb = s->info_callback; |
| 148 | } else if (s->ctx->info_callback != NULL) { |
| 149 | cb = s->ctx->info_callback; |
| 150 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 152 | s->in_handshake++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 154 | for (;;) { |
| 155 | state = s->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 157 | switch (s->state) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 158 | case SSL_ST_CONNECT: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 159 | if (cb != NULL) { |
| 160 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 161 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 163 | if (s->init_buf == NULL) { |
| 164 | buf = BUF_MEM_new(); |
| 165 | if (buf == NULL || |
| 166 | !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 167 | ret = -1; |
| 168 | goto end; |
| 169 | } |
| 170 | s->init_buf = buf; |
| 171 | buf = NULL; |
| 172 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 174 | if (!ssl_init_wbio_buffer(s, 0)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 175 | ret = -1; |
| 176 | goto end; |
| 177 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 178 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 179 | /* don't push the buffering BIO quite yet */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 181 | s->state = SSL3_ST_CW_CLNT_HELLO_A; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 182 | s->init_num = 0; |
| 183 | s->d1->send_cookie = 0; |
| 184 | s->hit = 0; |
| 185 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 187 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 188 | case SSL3_ST_CW_CLNT_HELLO_B: |
| 189 | s->shutdown = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 190 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 191 | /* every DTLS ClientHello resets Finished MAC */ |
| 192 | if (!ssl3_init_finished_mac(s)) { |
| 193 | OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR); |
| 194 | ret = -1; |
| 195 | goto end; |
| 196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 198 | dtls1_start_timer(s); |
| 199 | ret = ssl3_send_client_hello(s); |
| 200 | if (ret <= 0) { |
| 201 | goto end; |
| 202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 204 | if (s->d1->send_cookie) { |
| 205 | s->state = SSL3_ST_CW_FLUSH; |
| 206 | s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A; |
| 207 | } else { |
| 208 | s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; |
| 209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 211 | s->init_num = 0; |
| 212 | /* turn on buffering for the next lot of output */ |
| 213 | if (s->bbio != s->wbio) { |
| 214 | s->wbio = BIO_push(s->bbio, s->wbio); |
| 215 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 217 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 219 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: |
| 220 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: |
| 221 | ret = dtls1_get_hello_verify(s); |
| 222 | if (ret <= 0) { |
| 223 | goto end; |
| 224 | } |
| 225 | if (s->d1->send_cookie) { |
| 226 | /* start again, with a cookie */ |
| 227 | dtls1_stop_timer(s); |
| 228 | s->state = SSL3_ST_CW_CLNT_HELLO_A; |
| 229 | } else { |
| 230 | s->state = SSL3_ST_CR_SRVR_HELLO_A; |
| 231 | } |
| 232 | s->init_num = 0; |
| 233 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 235 | case SSL3_ST_CR_SRVR_HELLO_A: |
| 236 | case SSL3_ST_CR_SRVR_HELLO_B: |
| 237 | ret = ssl3_get_server_hello(s); |
| 238 | if (ret <= 0) { |
| 239 | goto end; |
| 240 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 242 | if (s->hit) { |
| 243 | s->state = SSL3_ST_CR_FINISHED_A; |
| 244 | if (s->tlsext_ticket_expected) { |
| 245 | /* receive renewed session ticket */ |
| 246 | s->state = SSL3_ST_CR_SESSION_TICKET_A; |
| 247 | } |
| 248 | } else { |
| 249 | s->state = SSL3_ST_CR_CERT_A; |
| 250 | } |
| 251 | s->init_num = 0; |
| 252 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 253 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 254 | case SSL3_ST_CR_CERT_A: |
| 255 | case SSL3_ST_CR_CERT_B: |
| 256 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 257 | ret = ssl3_get_server_certificate(s); |
| 258 | if (ret <= 0) { |
| 259 | goto end; |
| 260 | } |
| 261 | if (s->s3->tmp.certificate_status_expected) { |
| 262 | s->state = SSL3_ST_CR_CERT_STATUS_A; |
| 263 | } else { |
| 264 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 265 | } |
| 266 | } else { |
| 267 | skip = 1; |
| 268 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 269 | } |
| 270 | s->init_num = 0; |
| 271 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 273 | case SSL3_ST_CR_KEY_EXCH_A: |
| 274 | case SSL3_ST_CR_KEY_EXCH_B: |
| 275 | ret = ssl3_get_server_key_exchange(s); |
| 276 | if (ret <= 0) { |
| 277 | goto end; |
| 278 | } |
| 279 | s->state = SSL3_ST_CR_CERT_REQ_A; |
| 280 | s->init_num = 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 281 | break; |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 282 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 283 | case SSL3_ST_CR_CERT_REQ_A: |
| 284 | case SSL3_ST_CR_CERT_REQ_B: |
| 285 | ret = ssl3_get_certificate_request(s); |
| 286 | if (ret <= 0) { |
| 287 | goto end; |
| 288 | } |
| 289 | s->state = SSL3_ST_CR_SRVR_DONE_A; |
| 290 | s->init_num = 0; |
| 291 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 292 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 293 | case SSL3_ST_CR_SRVR_DONE_A: |
| 294 | case SSL3_ST_CR_SRVR_DONE_B: |
| 295 | ret = ssl3_get_server_done(s); |
| 296 | if (ret <= 0) { |
| 297 | goto end; |
| 298 | } |
| 299 | dtls1_stop_timer(s); |
| 300 | if (s->s3->tmp.cert_req) { |
| 301 | s->s3->tmp.next_state = SSL3_ST_CW_CERT_A; |
| 302 | } else { |
| 303 | s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A; |
| 304 | } |
| 305 | s->init_num = 0; |
| 306 | s->state = s->s3->tmp.next_state; |
| 307 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 309 | case SSL3_ST_CW_CERT_A: |
| 310 | case SSL3_ST_CW_CERT_B: |
| 311 | case SSL3_ST_CW_CERT_C: |
| 312 | case SSL3_ST_CW_CERT_D: |
| 313 | dtls1_start_timer(s); |
| 314 | ret = ssl3_send_client_certificate(s); |
| 315 | if (ret <= 0) { |
| 316 | goto end; |
| 317 | } |
| 318 | s->state = SSL3_ST_CW_KEY_EXCH_A; |
| 319 | s->init_num = 0; |
| 320 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 322 | case SSL3_ST_CW_KEY_EXCH_A: |
| 323 | case SSL3_ST_CW_KEY_EXCH_B: |
| 324 | dtls1_start_timer(s); |
| 325 | ret = ssl3_send_client_key_exchange(s); |
| 326 | if (ret <= 0) { |
| 327 | goto end; |
| 328 | } |
| 329 | /* For TLS, cert_req is set to 2, so a cert chain |
| 330 | * of nothing is sent, but no verify packet is sent */ |
| 331 | if (s->s3->tmp.cert_req == 1) { |
| 332 | s->state = SSL3_ST_CW_CERT_VRFY_A; |
| 333 | } else { |
| 334 | s->state = SSL3_ST_CW_CHANGE_A; |
| 335 | s->s3->change_cipher_spec = 0; |
| 336 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 338 | s->init_num = 0; |
| 339 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 340 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 341 | case SSL3_ST_CW_CERT_VRFY_A: |
| 342 | case SSL3_ST_CW_CERT_VRFY_B: |
| 343 | dtls1_start_timer(s); |
| 344 | ret = ssl3_send_cert_verify(s); |
| 345 | if (ret <= 0) { |
| 346 | goto end; |
| 347 | } |
| 348 | s->state = SSL3_ST_CW_CHANGE_A; |
| 349 | s->init_num = 0; |
| 350 | s->s3->change_cipher_spec = 0; |
| 351 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 353 | case SSL3_ST_CW_CHANGE_A: |
| 354 | case SSL3_ST_CW_CHANGE_B: |
| 355 | if (!s->hit) { |
| 356 | dtls1_start_timer(s); |
| 357 | } |
| 358 | ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A, |
| 359 | SSL3_ST_CW_CHANGE_B); |
| 360 | if (ret <= 0) { |
| 361 | goto end; |
| 362 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 364 | s->state = SSL3_ST_CW_FINISHED_A; |
| 365 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 366 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 367 | s->session->cipher = s->s3->tmp.new_cipher; |
| 368 | if (!s->enc_method->setup_key_block(s) || |
| 369 | !s->enc_method->change_cipher_state( |
| 370 | s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 371 | ret = -1; |
| 372 | goto end; |
| 373 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 374 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 375 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 376 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 378 | case SSL3_ST_CW_FINISHED_A: |
| 379 | case SSL3_ST_CW_FINISHED_B: |
| 380 | if (!s->hit) { |
| 381 | dtls1_start_timer(s); |
| 382 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 384 | ret = |
| 385 | ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B, |
| 386 | s->enc_method->client_finished_label, |
| 387 | s->enc_method->client_finished_label_len); |
| 388 | if (ret <= 0) { |
| 389 | goto end; |
| 390 | } |
| 391 | s->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 393 | if (s->hit) { |
| 394 | s->s3->tmp.next_state = SSL_ST_OK; |
| 395 | } else { |
| 396 | /* Allow NewSessionTicket if ticket expected */ |
| 397 | if (s->tlsext_ticket_expected) { |
| 398 | s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
| 399 | } else { |
| 400 | s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; |
| 401 | } |
| 402 | } |
| 403 | s->init_num = 0; |
| 404 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 406 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 407 | case SSL3_ST_CR_SESSION_TICKET_B: |
| 408 | ret = ssl3_get_new_session_ticket(s); |
| 409 | if (ret <= 0) { |
| 410 | goto end; |
| 411 | } |
| 412 | s->state = SSL3_ST_CR_FINISHED_A; |
| 413 | s->init_num = 0; |
| 414 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 416 | case SSL3_ST_CR_CERT_STATUS_A: |
| 417 | case SSL3_ST_CR_CERT_STATUS_B: |
| 418 | ret = ssl3_get_cert_status(s); |
| 419 | if (ret <= 0) { |
| 420 | goto end; |
| 421 | } |
| 422 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 423 | s->init_num = 0; |
| 424 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 426 | case SSL3_ST_CR_FINISHED_A: |
| 427 | case SSL3_ST_CR_FINISHED_B: |
| 428 | s->d1->change_cipher_spec_ok = 1; |
| 429 | ret = |
| 430 | ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B); |
| 431 | if (ret <= 0) { |
| 432 | goto end; |
| 433 | } |
| 434 | dtls1_stop_timer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 436 | if (s->hit) { |
| 437 | s->state = SSL3_ST_CW_CHANGE_A; |
| 438 | } else { |
| 439 | s->state = SSL_ST_OK; |
| 440 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 442 | s->init_num = 0; |
| 443 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 445 | case SSL3_ST_CW_FLUSH: |
| 446 | s->rwstate = SSL_WRITING; |
| 447 | if (BIO_flush(s->wbio) <= 0) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 448 | ret = -1; |
| 449 | goto end; |
| 450 | } |
| 451 | s->rwstate = SSL_NOTHING; |
| 452 | s->state = s->s3->tmp.next_state; |
| 453 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 454 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 455 | case SSL_ST_OK: |
| 456 | /* clean a few things up */ |
| 457 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 458 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 459 | /* Remove write buffering now. */ |
| 460 | ssl_free_wbio_buffer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 462 | s->init_num = 0; |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 463 | s->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 464 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 465 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 467 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 468 | |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 469 | if (cb != NULL) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 470 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 471 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 473 | /* done with handshaking */ |
| 474 | s->d1->handshake_read_seq = 0; |
| 475 | s->d1->next_handshake_write_seq = 0; |
| 476 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 478 | default: |
| 479 | OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE); |
| 480 | ret = -1; |
| 481 | goto end; |
| 482 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 484 | /* did we do anything? */ |
| 485 | if (!s->s3->tmp.reuse_message && !skip) { |
| 486 | if ((cb != NULL) && (s->state != state)) { |
| 487 | new_state = s->state; |
| 488 | s->state = state; |
| 489 | cb(s, SSL_CB_CONNECT_LOOP, 1); |
| 490 | s->state = new_state; |
| 491 | } |
| 492 | } |
| 493 | skip = 0; |
| 494 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 496 | end: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 497 | s->in_handshake--; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 499 | BUF_MEM_free(buf); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 500 | if (cb != NULL) { |
| 501 | cb(s, SSL_CB_CONNECT_EXIT, ret); |
| 502 | } |
| 503 | return ret; |
| 504 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 506 | static int dtls1_get_hello_verify(SSL *s) { |
| 507 | long n; |
| 508 | int al, ok = 0; |
| 509 | CBS hello_verify_request, cookie; |
| 510 | uint16_t server_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 512 | n = s->method->ssl_get_message( |
| 513 | s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, |
| 514 | -1, |
| 515 | /* Use the same maximum size as ssl3_get_server_hello. */ |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 516 | 20000, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 518 | if (!ok) { |
| 519 | return n; |
| 520 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 522 | if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 523 | s->d1->send_cookie = 0; |
| 524 | s->s3->tmp.reuse_message = 1; |
| 525 | return 1; |
| 526 | } |
David Benjamin | 51e3283 | 2014-08-13 15:13:57 -0400 | [diff] [blame] | 527 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 528 | CBS_init(&hello_verify_request, s->init_msg, n); |
David Benjamin | 51e3283 | 2014-08-13 15:13:57 -0400 | [diff] [blame] | 529 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 530 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 531 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 532 | CBS_len(&hello_verify_request) != 0) { |
| 533 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 5edc4e2 | 2015-03-09 13:58:39 -0700 | [diff] [blame] | 534 | OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 535 | goto f_err; |
| 536 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 538 | if (CBS_len(&cookie) > sizeof(s->d1->cookie)) { |
| 539 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 540 | goto f_err; |
| 541 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 542 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 543 | memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
| 544 | s->d1->cookie_len = CBS_len(&cookie); |
| 545 | |
| 546 | s->d1->send_cookie = 1; |
| 547 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 548 | |
| 549 | f_err: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 550 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 551 | return -1; |
| 552 | } |