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> |
| 117 | |
| 118 | #include <openssl/bn.h> |
| 119 | #include <openssl/buf.h> |
| 120 | #include <openssl/dh.h> |
| 121 | #include <openssl/evp.h> |
| 122 | #include <openssl/md5.h> |
| 123 | #include <openssl/mem.h> |
| 124 | #include <openssl/obj.h> |
| 125 | #include <openssl/rand.h> |
| 126 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame^] | 127 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | static int dtls1_get_hello_verify(SSL *s); |
| 130 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 131 | int dtls1_connect(SSL *s) { |
| 132 | BUF_MEM *buf = NULL; |
| 133 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 134 | int ret = -1; |
| 135 | int new_state, state, skip = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 137 | assert(s->handshake_func == dtls1_connect); |
| 138 | assert(!s->server); |
| 139 | assert(SSL_IS_DTLS(s)); |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 140 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 141 | ERR_clear_error(); |
| 142 | ERR_clear_system_error(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 144 | if (s->info_callback != NULL) { |
| 145 | cb = s->info_callback; |
| 146 | } else if (s->ctx->info_callback != NULL) { |
| 147 | cb = s->ctx->info_callback; |
| 148 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 150 | s->in_handshake++; |
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 | for (;;) { |
| 153 | state = s->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 155 | switch (s->state) { |
| 156 | case SSL_ST_RENEGOTIATE: |
| 157 | s->renegotiate = 1; |
| 158 | s->state = SSL_ST_CONNECT; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 159 | /* break */ |
| 160 | case SSL_ST_CONNECT: |
| 161 | case SSL_ST_BEFORE | SSL_ST_CONNECT: |
| 162 | if (cb != NULL) { |
| 163 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 164 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 166 | if (s->init_buf == NULL) { |
| 167 | buf = BUF_MEM_new(); |
| 168 | if (buf == NULL || |
| 169 | !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 170 | ret = -1; |
| 171 | goto end; |
| 172 | } |
| 173 | s->init_buf = buf; |
| 174 | buf = NULL; |
| 175 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 176 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 177 | if (!ssl3_setup_buffers(s) || |
| 178 | !ssl_init_wbio_buffer(s, 0)) { |
| 179 | ret = -1; |
| 180 | goto end; |
| 181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 183 | /* don't push the buffering BIO quite yet */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 185 | s->state = SSL3_ST_CW_CLNT_HELLO_A; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 186 | s->init_num = 0; |
| 187 | s->d1->send_cookie = 0; |
| 188 | s->hit = 0; |
| 189 | break; |
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 | case SSL3_ST_CW_CLNT_HELLO_A: |
| 192 | case SSL3_ST_CW_CLNT_HELLO_B: |
| 193 | s->shutdown = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 195 | /* every DTLS ClientHello resets Finished MAC */ |
| 196 | if (!ssl3_init_finished_mac(s)) { |
| 197 | OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR); |
| 198 | ret = -1; |
| 199 | goto end; |
| 200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 202 | dtls1_start_timer(s); |
| 203 | ret = ssl3_send_client_hello(s); |
| 204 | if (ret <= 0) { |
| 205 | goto end; |
| 206 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 208 | if (s->d1->send_cookie) { |
| 209 | s->state = SSL3_ST_CW_FLUSH; |
| 210 | s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A; |
| 211 | } else { |
| 212 | s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; |
| 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 215 | s->init_num = 0; |
| 216 | /* turn on buffering for the next lot of output */ |
| 217 | if (s->bbio != s->wbio) { |
| 218 | s->wbio = BIO_push(s->bbio, s->wbio); |
| 219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 221 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 223 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: |
| 224 | case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: |
| 225 | ret = dtls1_get_hello_verify(s); |
| 226 | if (ret <= 0) { |
| 227 | goto end; |
| 228 | } |
| 229 | if (s->d1->send_cookie) { |
| 230 | /* start again, with a cookie */ |
| 231 | dtls1_stop_timer(s); |
| 232 | s->state = SSL3_ST_CW_CLNT_HELLO_A; |
| 233 | } else { |
| 234 | s->state = SSL3_ST_CR_SRVR_HELLO_A; |
| 235 | } |
| 236 | s->init_num = 0; |
| 237 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 239 | case SSL3_ST_CR_SRVR_HELLO_A: |
| 240 | case SSL3_ST_CR_SRVR_HELLO_B: |
| 241 | ret = ssl3_get_server_hello(s); |
| 242 | if (ret <= 0) { |
| 243 | goto end; |
| 244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 245 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 246 | if (s->hit) { |
| 247 | s->state = SSL3_ST_CR_FINISHED_A; |
| 248 | if (s->tlsext_ticket_expected) { |
| 249 | /* receive renewed session ticket */ |
| 250 | s->state = SSL3_ST_CR_SESSION_TICKET_A; |
| 251 | } |
| 252 | } else { |
| 253 | s->state = SSL3_ST_CR_CERT_A; |
| 254 | } |
| 255 | s->init_num = 0; |
| 256 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 258 | case SSL3_ST_CR_CERT_A: |
| 259 | case SSL3_ST_CR_CERT_B: |
| 260 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 261 | ret = ssl3_get_server_certificate(s); |
| 262 | if (ret <= 0) { |
| 263 | goto end; |
| 264 | } |
| 265 | if (s->s3->tmp.certificate_status_expected) { |
| 266 | s->state = SSL3_ST_CR_CERT_STATUS_A; |
| 267 | } else { |
| 268 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 269 | } |
| 270 | } else { |
| 271 | skip = 1; |
| 272 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 273 | } |
| 274 | s->init_num = 0; |
| 275 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 276 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 277 | case SSL3_ST_CR_KEY_EXCH_A: |
| 278 | case SSL3_ST_CR_KEY_EXCH_B: |
| 279 | ret = ssl3_get_server_key_exchange(s); |
| 280 | if (ret <= 0) { |
| 281 | goto end; |
| 282 | } |
| 283 | s->state = SSL3_ST_CR_CERT_REQ_A; |
| 284 | s->init_num = 0; |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 285 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 286 | /* at this point we check that we have the |
| 287 | * required stuff from the server */ |
| 288 | if (!ssl3_check_cert_and_algorithm(s)) { |
| 289 | ret = -1; |
| 290 | goto end; |
| 291 | } |
| 292 | break; |
David Benjamin | f2fedef | 2014-08-16 01:37:34 -0400 | [diff] [blame] | 293 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 294 | case SSL3_ST_CR_CERT_REQ_A: |
| 295 | case SSL3_ST_CR_CERT_REQ_B: |
| 296 | ret = ssl3_get_certificate_request(s); |
| 297 | if (ret <= 0) { |
| 298 | goto end; |
| 299 | } |
| 300 | s->state = SSL3_ST_CR_SRVR_DONE_A; |
| 301 | s->init_num = 0; |
| 302 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 304 | case SSL3_ST_CR_SRVR_DONE_A: |
| 305 | case SSL3_ST_CR_SRVR_DONE_B: |
| 306 | ret = ssl3_get_server_done(s); |
| 307 | if (ret <= 0) { |
| 308 | goto end; |
| 309 | } |
| 310 | dtls1_stop_timer(s); |
| 311 | if (s->s3->tmp.cert_req) { |
| 312 | s->s3->tmp.next_state = SSL3_ST_CW_CERT_A; |
| 313 | } else { |
| 314 | s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A; |
| 315 | } |
| 316 | s->init_num = 0; |
| 317 | s->state = s->s3->tmp.next_state; |
| 318 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 320 | case SSL3_ST_CW_CERT_A: |
| 321 | case SSL3_ST_CW_CERT_B: |
| 322 | case SSL3_ST_CW_CERT_C: |
| 323 | case SSL3_ST_CW_CERT_D: |
| 324 | dtls1_start_timer(s); |
| 325 | ret = ssl3_send_client_certificate(s); |
| 326 | if (ret <= 0) { |
| 327 | goto end; |
| 328 | } |
| 329 | s->state = SSL3_ST_CW_KEY_EXCH_A; |
| 330 | s->init_num = 0; |
| 331 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 332 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 333 | case SSL3_ST_CW_KEY_EXCH_A: |
| 334 | case SSL3_ST_CW_KEY_EXCH_B: |
| 335 | dtls1_start_timer(s); |
| 336 | ret = ssl3_send_client_key_exchange(s); |
| 337 | if (ret <= 0) { |
| 338 | goto end; |
| 339 | } |
| 340 | /* For TLS, cert_req is set to 2, so a cert chain |
| 341 | * of nothing is sent, but no verify packet is sent */ |
| 342 | if (s->s3->tmp.cert_req == 1) { |
| 343 | s->state = SSL3_ST_CW_CERT_VRFY_A; |
| 344 | } else { |
| 345 | s->state = SSL3_ST_CW_CHANGE_A; |
| 346 | s->s3->change_cipher_spec = 0; |
| 347 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 349 | s->init_num = 0; |
| 350 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 352 | case SSL3_ST_CW_CERT_VRFY_A: |
| 353 | case SSL3_ST_CW_CERT_VRFY_B: |
| 354 | dtls1_start_timer(s); |
| 355 | ret = ssl3_send_cert_verify(s); |
| 356 | if (ret <= 0) { |
| 357 | goto end; |
| 358 | } |
| 359 | s->state = SSL3_ST_CW_CHANGE_A; |
| 360 | s->init_num = 0; |
| 361 | s->s3->change_cipher_spec = 0; |
| 362 | break; |
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 | case SSL3_ST_CW_CHANGE_A: |
| 365 | case SSL3_ST_CW_CHANGE_B: |
| 366 | if (!s->hit) { |
| 367 | dtls1_start_timer(s); |
| 368 | } |
| 369 | ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A, |
| 370 | SSL3_ST_CW_CHANGE_B); |
| 371 | if (ret <= 0) { |
| 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 | s->state = SSL3_ST_CW_FINISHED_A; |
| 376 | s->init_num = 0; |
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 | s->session->cipher = s->s3->tmp.new_cipher; |
| 379 | if (!s->enc_method->setup_key_block(s) || |
| 380 | !s->enc_method->change_cipher_state( |
| 381 | s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 382 | ret = -1; |
| 383 | goto end; |
| 384 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 386 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 387 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 389 | case SSL3_ST_CW_FINISHED_A: |
| 390 | case SSL3_ST_CW_FINISHED_B: |
| 391 | if (!s->hit) { |
| 392 | dtls1_start_timer(s); |
| 393 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 395 | ret = |
| 396 | ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B, |
| 397 | s->enc_method->client_finished_label, |
| 398 | s->enc_method->client_finished_label_len); |
| 399 | if (ret <= 0) { |
| 400 | goto end; |
| 401 | } |
| 402 | s->state = SSL3_ST_CW_FLUSH; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 404 | if (s->hit) { |
| 405 | s->s3->tmp.next_state = SSL_ST_OK; |
| 406 | } else { |
| 407 | /* Allow NewSessionTicket if ticket expected */ |
| 408 | if (s->tlsext_ticket_expected) { |
| 409 | s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; |
| 410 | } else { |
| 411 | s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; |
| 412 | } |
| 413 | } |
| 414 | s->init_num = 0; |
| 415 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 417 | case SSL3_ST_CR_SESSION_TICKET_A: |
| 418 | case SSL3_ST_CR_SESSION_TICKET_B: |
| 419 | ret = ssl3_get_new_session_ticket(s); |
| 420 | if (ret <= 0) { |
| 421 | goto end; |
| 422 | } |
| 423 | s->state = SSL3_ST_CR_FINISHED_A; |
| 424 | s->init_num = 0; |
| 425 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 427 | case SSL3_ST_CR_CERT_STATUS_A: |
| 428 | case SSL3_ST_CR_CERT_STATUS_B: |
| 429 | ret = ssl3_get_cert_status(s); |
| 430 | if (ret <= 0) { |
| 431 | goto end; |
| 432 | } |
| 433 | s->state = SSL3_ST_CR_KEY_EXCH_A; |
| 434 | s->init_num = 0; |
| 435 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 437 | case SSL3_ST_CR_FINISHED_A: |
| 438 | case SSL3_ST_CR_FINISHED_B: |
| 439 | s->d1->change_cipher_spec_ok = 1; |
| 440 | ret = |
| 441 | ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B); |
| 442 | if (ret <= 0) { |
| 443 | goto end; |
| 444 | } |
| 445 | dtls1_stop_timer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 446 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 447 | if (s->hit) { |
| 448 | s->state = SSL3_ST_CW_CHANGE_A; |
| 449 | } else { |
| 450 | s->state = SSL_ST_OK; |
| 451 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 452 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 453 | s->init_num = 0; |
| 454 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 456 | case SSL3_ST_CW_FLUSH: |
| 457 | s->rwstate = SSL_WRITING; |
| 458 | if (BIO_flush(s->wbio) <= 0) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 459 | ret = -1; |
| 460 | goto end; |
| 461 | } |
| 462 | s->rwstate = SSL_NOTHING; |
| 463 | s->state = s->s3->tmp.next_state; |
| 464 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 465 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 466 | case SSL_ST_OK: |
| 467 | /* clean a few things up */ |
| 468 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 469 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 470 | /* Remove write buffering now. */ |
| 471 | ssl_free_wbio_buffer(s); |
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 | s->init_num = 0; |
| 474 | s->renegotiate = 0; |
| 475 | s->new_session = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 476 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 477 | ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 478 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 479 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 480 | |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 481 | if (cb != NULL) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 482 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 483 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 485 | /* done with handshaking */ |
| 486 | s->d1->handshake_read_seq = 0; |
| 487 | s->d1->next_handshake_write_seq = 0; |
| 488 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 490 | default: |
| 491 | OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE); |
| 492 | ret = -1; |
| 493 | goto end; |
| 494 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 496 | /* did we do anything? */ |
| 497 | if (!s->s3->tmp.reuse_message && !skip) { |
| 498 | if ((cb != NULL) && (s->state != state)) { |
| 499 | new_state = s->state; |
| 500 | s->state = state; |
| 501 | cb(s, SSL_CB_CONNECT_LOOP, 1); |
| 502 | s->state = new_state; |
| 503 | } |
| 504 | } |
| 505 | skip = 0; |
| 506 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 508 | end: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 509 | s->in_handshake--; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 511 | if (buf != NULL) { |
| 512 | BUF_MEM_free(buf); |
| 513 | } |
| 514 | if (cb != NULL) { |
| 515 | cb(s, SSL_CB_CONNECT_EXIT, ret); |
| 516 | } |
| 517 | return ret; |
| 518 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 519 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 520 | static int dtls1_get_hello_verify(SSL *s) { |
| 521 | long n; |
| 522 | int al, ok = 0; |
| 523 | CBS hello_verify_request, cookie; |
| 524 | uint16_t server_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 526 | n = s->method->ssl_get_message( |
| 527 | s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, |
| 528 | -1, |
| 529 | /* Use the same maximum size as ssl3_get_server_hello. */ |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 530 | 20000, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 531 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 532 | if (!ok) { |
| 533 | return n; |
| 534 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 536 | if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 537 | s->d1->send_cookie = 0; |
| 538 | s->s3->tmp.reuse_message = 1; |
| 539 | return 1; |
| 540 | } |
David Benjamin | 51e3283 | 2014-08-13 15:13:57 -0400 | [diff] [blame] | 541 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 542 | CBS_init(&hello_verify_request, s->init_msg, n); |
David Benjamin | 51e3283 | 2014-08-13 15:13:57 -0400 | [diff] [blame] | 543 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 544 | if (!CBS_get_u16(&hello_verify_request, &server_version) || |
| 545 | !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) || |
| 546 | CBS_len(&hello_verify_request) != 0) { |
| 547 | al = SSL_AD_DECODE_ERROR; |
Adam Langley | 5edc4e2 | 2015-03-09 13:58:39 -0700 | [diff] [blame] | 548 | OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 549 | goto f_err; |
| 550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 552 | if (CBS_len(&cookie) > sizeof(s->d1->cookie)) { |
| 553 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 554 | goto f_err; |
| 555 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 557 | memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie)); |
| 558 | s->d1->cookie_len = CBS_len(&cookie); |
| 559 | |
| 560 | s->d1->send_cookie = 1; |
| 561 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
| 563 | f_err: |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 564 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 565 | return -1; |
| 566 | } |