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