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 | |
| 115 | #include <stdio.h> |
| 116 | |
| 117 | #include <openssl/bn.h> |
| 118 | #include <openssl/buf.h> |
| 119 | #include <openssl/dh.h> |
| 120 | #include <openssl/evp.h> |
| 121 | #include <openssl/md5.h> |
| 122 | #include <openssl/obj.h> |
| 123 | #include <openssl/rand.h> |
| 124 | #include <openssl/x509.h> |
| 125 | |
| 126 | #include "ssl_locl.h" |
| 127 | |
| 128 | static const SSL_METHOD *dtls1_get_server_method(int ver); |
| 129 | static int dtls1_send_hello_verify_request(SSL *s); |
| 130 | |
| 131 | static const SSL_METHOD *dtls1_get_server_method(int ver) |
| 132 | { |
| 133 | if (ver == DTLS1_VERSION) |
| 134 | return(DTLSv1_server_method()); |
| 135 | else if (ver == DTLS1_2_VERSION) |
| 136 | return(DTLSv1_2_server_method()); |
| 137 | else |
| 138 | return(NULL); |
| 139 | } |
| 140 | |
| 141 | IMPLEMENT_dtls1_meth_func(DTLS1_VERSION, |
| 142 | DTLSv1_server_method, |
| 143 | dtls1_accept, |
| 144 | ssl_undefined_function, |
| 145 | dtls1_get_server_method, |
| 146 | DTLSv1_enc_data) |
| 147 | |
| 148 | IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION, |
| 149 | DTLSv1_2_server_method, |
| 150 | dtls1_accept, |
| 151 | ssl_undefined_function, |
| 152 | dtls1_get_server_method, |
| 153 | DTLSv1_2_enc_data) |
| 154 | |
| 155 | IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION, |
| 156 | DTLS_server_method, |
| 157 | dtls1_accept, |
| 158 | ssl_undefined_function, |
| 159 | dtls1_get_server_method, |
| 160 | DTLSv1_2_enc_data) |
| 161 | |
| 162 | int dtls1_accept(SSL *s) |
| 163 | { |
| 164 | BUF_MEM *buf; |
| 165 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 166 | unsigned long alg_a; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | int ret= -1; |
| 168 | int new_state,state,skip=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
| 170 | ERR_clear_error(); |
| 171 | ERR_clear_system_error(); |
| 172 | |
| 173 | if (s->info_callback != NULL) |
| 174 | cb=s->info_callback; |
| 175 | else if (s->ctx->info_callback != NULL) |
| 176 | cb=s->ctx->info_callback; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
| 178 | /* init things to blank */ |
| 179 | s->in_handshake++; |
| 180 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 181 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | if (s->cert == NULL) |
| 183 | { |
| 184 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_NO_CERTIFICATE_SET); |
| 185 | return(-1); |
| 186 | } |
| 187 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | for (;;) |
| 189 | { |
| 190 | state=s->state; |
| 191 | |
| 192 | switch (s->state) |
| 193 | { |
| 194 | case SSL_ST_RENEGOTIATE: |
| 195 | s->renegotiate=1; |
| 196 | /* s->state=SSL_ST_ACCEPT; */ |
| 197 | |
| 198 | case SSL_ST_BEFORE: |
| 199 | case SSL_ST_ACCEPT: |
| 200 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: |
| 201 | case SSL_ST_OK|SSL_ST_ACCEPT: |
| 202 | |
| 203 | s->server=1; |
| 204 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 205 | |
| 206 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) |
| 207 | { |
| 208 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 209 | return -1; |
| 210 | } |
| 211 | s->type=SSL_ST_ACCEPT; |
| 212 | |
| 213 | if (s->init_buf == NULL) |
| 214 | { |
| 215 | if ((buf=BUF_MEM_new()) == NULL) |
| 216 | { |
| 217 | ret= -1; |
| 218 | goto end; |
| 219 | } |
| 220 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 221 | { |
| 222 | ret= -1; |
| 223 | goto end; |
| 224 | } |
| 225 | s->init_buf=buf; |
| 226 | } |
| 227 | |
| 228 | if (!ssl3_setup_buffers(s)) |
| 229 | { |
| 230 | ret= -1; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | s->init_num=0; |
| 235 | |
| 236 | if (s->state != SSL_ST_RENEGOTIATE) |
| 237 | { |
| 238 | /* Ok, we now need to push on a buffering BIO so that |
| 239 | * the output is sent in a way that TCP likes :-) |
| 240 | * ...but not with SCTP :-) |
| 241 | */ |
| 242 | if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } |
| 243 | |
| 244 | ssl3_init_finished_mac(s); |
| 245 | s->state=SSL3_ST_SR_CLNT_HELLO_A; |
| 246 | s->ctx->stats.sess_accept++; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | /* s->state == SSL_ST_RENEGOTIATE, |
| 251 | * we will just send a HelloRequest */ |
| 252 | s->ctx->stats.sess_accept_renegotiate++; |
| 253 | s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 254 | } |
| 255 | |
| 256 | break; |
| 257 | |
| 258 | case SSL3_ST_SW_HELLO_REQ_A: |
| 259 | case SSL3_ST_SW_HELLO_REQ_B: |
| 260 | |
| 261 | s->shutdown=0; |
| 262 | dtls1_clear_record_buffer(s); |
| 263 | dtls1_start_timer(s); |
| 264 | ret=ssl3_send_hello_request(s); |
| 265 | if (ret <= 0) goto end; |
| 266 | s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; |
| 267 | s->state=SSL3_ST_SW_FLUSH; |
| 268 | s->init_num=0; |
| 269 | |
| 270 | ssl3_init_finished_mac(s); |
| 271 | break; |
| 272 | |
| 273 | case SSL3_ST_SW_HELLO_REQ_C: |
| 274 | s->state=SSL_ST_OK; |
| 275 | break; |
| 276 | |
| 277 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 278 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 279 | case SSL3_ST_SR_CLNT_HELLO_C: |
David Benjamin | 95fcaa4 | 2014-08-05 13:10:14 -0400 | [diff] [blame] | 280 | case SSL3_ST_SR_CLNT_HELLO_D: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
| 282 | s->shutdown=0; |
| 283 | ret=ssl3_get_client_hello(s); |
| 284 | if (ret <= 0) goto end; |
| 285 | dtls1_stop_timer(s); |
| 286 | |
| 287 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) |
| 288 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; |
| 289 | else |
| 290 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 291 | |
| 292 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: |
| 296 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: |
| 297 | |
| 298 | ret = dtls1_send_hello_verify_request(s); |
| 299 | if ( ret <= 0) goto end; |
| 300 | s->state=SSL3_ST_SW_FLUSH; |
| 301 | s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; |
| 302 | |
| 303 | /* HelloVerifyRequest resets Finished MAC */ |
David Benjamin | cc23df5 | 2014-08-03 13:37:47 -0400 | [diff] [blame] | 304 | ssl3_init_finished_mac(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | break; |
| 306 | |
| 307 | |
| 308 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 309 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 310 | s->renegotiate = 2; |
| 311 | dtls1_start_timer(s); |
| 312 | ret=ssl3_send_server_hello(s); |
| 313 | if (ret <= 0) goto end; |
| 314 | |
| 315 | if (s->hit) |
| 316 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | if (s->tlsext_ticket_expected) |
| 318 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
| 319 | else |
| 320 | s->state=SSL3_ST_SW_CHANGE_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | } |
| 322 | else |
| 323 | s->state=SSL3_ST_SW_CERT_A; |
| 324 | s->init_num=0; |
| 325 | break; |
| 326 | |
| 327 | case SSL3_ST_SW_CERT_A: |
| 328 | case SSL3_ST_SW_CERT_B: |
| 329 | /* Check if it is anon DH or normal PSK */ |
| 330 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 331 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
| 332 | { |
| 333 | dtls1_start_timer(s); |
| 334 | ret=ssl3_send_server_certificate(s); |
| 335 | if (ret <= 0) goto end; |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 336 | if (s->s3->tmp.certificate_status_expected) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | s->state=SSL3_ST_SW_CERT_STATUS_A; |
| 338 | else |
| 339 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | skip = 1; |
| 344 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 345 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 346 | s->init_num=0; |
| 347 | break; |
| 348 | |
| 349 | case SSL3_ST_SW_KEY_EXCH_A: |
| 350 | case SSL3_ST_SW_KEY_EXCH_B: |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 351 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 353 | /* Send a ServerKeyExchange message if: |
| 354 | * - The key exchange is ephemeral or anonymous |
| 355 | * Diffie-Hellman. |
| 356 | * - There is a PSK identity hint. |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 357 | * |
| 358 | * TODO(davidben): This logic is currently duplicated |
| 359 | * in s3_srvr.c. Fix this. In the meantime, keep them |
| 360 | * in sync. |
| 361 | */ |
| 362 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 363 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 364 | { |
| 365 | dtls1_start_timer(s); |
| 366 | ret=ssl3_send_server_key_exchange(s); |
| 367 | if (ret <= 0) goto end; |
| 368 | } |
| 369 | else |
| 370 | skip=1; |
| 371 | |
| 372 | s->state=SSL3_ST_SW_CERT_REQ_A; |
| 373 | s->init_num=0; |
| 374 | break; |
| 375 | |
| 376 | case SSL3_ST_SW_CERT_REQ_A: |
| 377 | case SSL3_ST_SW_CERT_REQ_B: |
| 378 | if (/* don't request cert unless asked for it: */ |
| 379 | !(s->verify_mode & SSL_VERIFY_PEER) || |
| 380 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 381 | * don't request cert during re-negotiation: */ |
| 382 | ((s->session->peer != NULL) && |
| 383 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 384 | /* never request cert in anonymous ciphersuites |
| 385 | * (see section "Certificate request" in SSL 3 drafts |
| 386 | * and in RFC 2246): */ |
| 387 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 388 | /* ... except when the application insists on verification |
| 389 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 390 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 391 | /* With normal PSK Certificates and |
| 392 | * Certificate Requests are omitted */ |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 393 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | { |
| 395 | /* no cert request */ |
| 396 | skip=1; |
| 397 | s->s3->tmp.cert_request=0; |
| 398 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | s->s3->tmp.cert_request=1; |
| 403 | dtls1_start_timer(s); |
| 404 | ret=ssl3_send_certificate_request(s); |
| 405 | if (ret <= 0) goto end; |
| 406 | #ifndef NETSCAPE_HANG_BUG |
| 407 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 408 | #else |
| 409 | s->state=SSL3_ST_SW_FLUSH; |
| 410 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 411 | #endif |
| 412 | s->init_num=0; |
| 413 | } |
| 414 | break; |
| 415 | |
| 416 | case SSL3_ST_SW_SRVR_DONE_A: |
| 417 | case SSL3_ST_SW_SRVR_DONE_B: |
| 418 | dtls1_start_timer(s); |
| 419 | ret=ssl3_send_server_done(s); |
| 420 | if (ret <= 0) goto end; |
| 421 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 422 | s->state=SSL3_ST_SW_FLUSH; |
| 423 | s->init_num=0; |
| 424 | break; |
| 425 | |
| 426 | case SSL3_ST_SW_FLUSH: |
| 427 | s->rwstate=SSL_WRITING; |
| 428 | if (BIO_flush(s->wbio) <= 0) |
| 429 | { |
| 430 | /* If the write error was fatal, stop trying */ |
| 431 | if (!BIO_should_retry(s->wbio)) |
| 432 | { |
| 433 | s->rwstate=SSL_NOTHING; |
| 434 | s->state=s->s3->tmp.next_state; |
| 435 | } |
| 436 | |
| 437 | ret= -1; |
| 438 | goto end; |
| 439 | } |
| 440 | s->rwstate=SSL_NOTHING; |
| 441 | s->state=s->s3->tmp.next_state; |
| 442 | break; |
| 443 | |
| 444 | case SSL3_ST_SR_CERT_A: |
| 445 | case SSL3_ST_SR_CERT_B: |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 446 | if (s->s3->tmp.cert_request) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | { |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 448 | ret=ssl3_get_client_certificate(s); |
| 449 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | } |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 451 | s->init_num=0; |
| 452 | s->state=SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case SSL3_ST_SR_KEY_EXCH_A: |
| 456 | case SSL3_ST_SR_KEY_EXCH_B: |
| 457 | ret=ssl3_get_client_key_exchange(s); |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 458 | if (ret <= 0) |
| 459 | goto end; |
David Benjamin | b52e3dd | 2014-08-20 11:12:48 -0400 | [diff] [blame] | 460 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 461 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | break; |
| 463 | |
| 464 | case SSL3_ST_SR_CERT_VRFY_A: |
| 465 | case SSL3_ST_SR_CERT_VRFY_B: |
| 466 | |
| 467 | s->d1->change_cipher_spec_ok = 1; |
| 468 | /* we should decide if we expected this one */ |
| 469 | ret=ssl3_get_cert_verify(s); |
| 470 | if (ret <= 0) goto end; |
| 471 | s->state=SSL3_ST_SR_FINISHED_A; |
| 472 | s->init_num=0; |
| 473 | break; |
| 474 | |
| 475 | case SSL3_ST_SR_FINISHED_A: |
| 476 | case SSL3_ST_SR_FINISHED_B: |
| 477 | s->d1->change_cipher_spec_ok = 1; |
| 478 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 479 | SSL3_ST_SR_FINISHED_B); |
| 480 | if (ret <= 0) goto end; |
| 481 | dtls1_stop_timer(s); |
| 482 | if (s->hit) |
| 483 | s->state=SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | else if (s->tlsext_ticket_expected) |
| 485 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 486 | else |
| 487 | s->state=SSL3_ST_SW_CHANGE_A; |
| 488 | s->init_num=0; |
| 489 | break; |
| 490 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 492 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 493 | ret=ssl3_send_new_session_ticket(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 494 | if (ret <= 0) goto end; |
| 495 | s->state=SSL3_ST_SW_CHANGE_A; |
| 496 | s->init_num=0; |
| 497 | break; |
| 498 | |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 499 | #if 0 |
| 500 | // TODO(davidben): Implement OCSP stapling on the server. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | case SSL3_ST_SW_CERT_STATUS_A: |
| 502 | case SSL3_ST_SW_CERT_STATUS_B: |
| 503 | ret=ssl3_send_cert_status(s); |
| 504 | if (ret <= 0) goto end; |
| 505 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 506 | s->init_num=0; |
| 507 | break; |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 508 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 509 | |
| 510 | case SSL3_ST_SW_CHANGE_A: |
| 511 | case SSL3_ST_SW_CHANGE_B: |
| 512 | |
| 513 | s->session->cipher=s->s3->tmp.new_cipher; |
| 514 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 515 | { ret= -1; goto end; } |
| 516 | |
| 517 | ret=dtls1_send_change_cipher_spec(s, |
| 518 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 519 | |
| 520 | if (ret <= 0) goto end; |
| 521 | |
| 522 | s->state=SSL3_ST_SW_FINISHED_A; |
| 523 | s->init_num=0; |
| 524 | |
| 525 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 526 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 527 | { |
| 528 | ret= -1; |
| 529 | goto end; |
| 530 | } |
| 531 | |
| 532 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 533 | break; |
| 534 | |
| 535 | case SSL3_ST_SW_FINISHED_A: |
| 536 | case SSL3_ST_SW_FINISHED_B: |
| 537 | ret=ssl3_send_finished(s, |
| 538 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 539 | s->method->ssl3_enc->server_finished_label, |
| 540 | s->method->ssl3_enc->server_finished_label_len); |
| 541 | if (ret <= 0) goto end; |
| 542 | s->state=SSL3_ST_SW_FLUSH; |
| 543 | if (s->hit) |
| 544 | { |
| 545 | s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; |
| 546 | } |
| 547 | else |
| 548 | { |
| 549 | s->s3->tmp.next_state=SSL_ST_OK; |
| 550 | } |
| 551 | s->init_num=0; |
| 552 | break; |
| 553 | |
| 554 | case SSL_ST_OK: |
| 555 | /* clean a few things up */ |
| 556 | ssl3_cleanup_key_block(s); |
| 557 | |
| 558 | #if 0 |
| 559 | BUF_MEM_free(s->init_buf); |
| 560 | s->init_buf=NULL; |
| 561 | #endif |
| 562 | |
| 563 | /* remove buffering on output */ |
| 564 | ssl_free_wbio_buffer(s); |
| 565 | |
| 566 | s->init_num=0; |
| 567 | |
| 568 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ |
| 569 | { |
| 570 | s->renegotiate=0; |
| 571 | s->new_session=0; |
| 572 | |
| 573 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 574 | |
| 575 | s->ctx->stats.sess_accept_good++; |
| 576 | /* s->server=1; */ |
| 577 | s->handshake_func=dtls1_accept; |
| 578 | |
| 579 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 580 | } |
| 581 | |
| 582 | ret = 1; |
| 583 | |
| 584 | /* done handshaking, next message is client hello */ |
| 585 | s->d1->handshake_read_seq = 0; |
| 586 | /* next message is server hello */ |
| 587 | s->d1->handshake_write_seq = 0; |
| 588 | s->d1->next_handshake_write_seq = 0; |
| 589 | goto end; |
| 590 | /* break; */ |
| 591 | |
| 592 | default: |
| 593 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE); |
| 594 | ret= -1; |
| 595 | goto end; |
| 596 | /* break; */ |
| 597 | } |
| 598 | |
| 599 | if (!s->s3->tmp.reuse_message && !skip) |
| 600 | { |
| 601 | if (s->debug) |
| 602 | { |
| 603 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 604 | goto end; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | if ((cb != NULL) && (s->state != state)) |
| 609 | { |
| 610 | new_state=s->state; |
| 611 | s->state=state; |
| 612 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 613 | s->state=new_state; |
| 614 | } |
| 615 | } |
| 616 | skip=0; |
| 617 | } |
| 618 | end: |
| 619 | /* BIO_flush(s->wbio); */ |
| 620 | |
| 621 | s->in_handshake--; |
| 622 | |
| 623 | if (cb != NULL) |
| 624 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 625 | return(ret); |
| 626 | } |
| 627 | |
| 628 | int dtls1_send_hello_verify_request(SSL *s) |
| 629 | { |
| 630 | unsigned int msg_len; |
| 631 | unsigned char *msg, *buf, *p; |
| 632 | |
| 633 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) |
| 634 | { |
| 635 | buf = (unsigned char *)s->init_buf->data; |
| 636 | |
| 637 | msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); |
| 638 | /* Always use DTLS 1.0 version: see RFC 6347 */ |
| 639 | *(p++) = DTLS1_VERSION >> 8; |
| 640 | *(p++) = DTLS1_VERSION & 0xFF; |
| 641 | |
| 642 | if (s->ctx->app_gen_cookie_cb == NULL || |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 643 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, &(s->d1->cookie_len)) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 644 | { |
| 645 | OPENSSL_PUT_ERROR(SSL, dtls1_send_hello_verify_request, ERR_R_INTERNAL_ERROR); |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | *(p++) = (unsigned char) s->d1->cookie_len; |
| 650 | memcpy(p, s->d1->cookie, s->d1->cookie_len); |
| 651 | p += s->d1->cookie_len; |
| 652 | msg_len = p - msg; |
| 653 | |
| 654 | dtls1_set_message_header(s, buf, |
| 655 | DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len); |
| 656 | |
| 657 | s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; |
| 658 | /* number of bytes to write */ |
| 659 | s->init_num=p-buf; |
| 660 | s->init_off=0; |
| 661 | } |
| 662 | |
| 663 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 664 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE, add_to_finished_hash)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 665 | } |