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 | */ |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame^] | 242 | if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame^] | 244 | if (!ssl3_init_finished_mac(s)) |
| 245 | { |
| 246 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 247 | ret = -1; |
| 248 | goto end; |
| 249 | } |
| 250 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | s->state=SSL3_ST_SR_CLNT_HELLO_A; |
| 252 | s->ctx->stats.sess_accept++; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | /* s->state == SSL_ST_RENEGOTIATE, |
| 257 | * we will just send a HelloRequest */ |
| 258 | s->ctx->stats.sess_accept_renegotiate++; |
| 259 | s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 260 | } |
| 261 | |
| 262 | break; |
| 263 | |
| 264 | case SSL3_ST_SW_HELLO_REQ_A: |
| 265 | case SSL3_ST_SW_HELLO_REQ_B: |
| 266 | |
| 267 | s->shutdown=0; |
| 268 | dtls1_clear_record_buffer(s); |
| 269 | dtls1_start_timer(s); |
| 270 | ret=ssl3_send_hello_request(s); |
| 271 | if (ret <= 0) goto end; |
| 272 | s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; |
| 273 | s->state=SSL3_ST_SW_FLUSH; |
| 274 | s->init_num=0; |
| 275 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame^] | 276 | if (!ssl3_init_finished_mac(s)) |
| 277 | { |
| 278 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 279 | ret = -1; |
| 280 | goto end; |
| 281 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | break; |
| 283 | |
| 284 | case SSL3_ST_SW_HELLO_REQ_C: |
| 285 | s->state=SSL_ST_OK; |
| 286 | break; |
| 287 | |
| 288 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 289 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 290 | case SSL3_ST_SR_CLNT_HELLO_C: |
David Benjamin | 95fcaa4 | 2014-08-05 13:10:14 -0400 | [diff] [blame] | 291 | case SSL3_ST_SR_CLNT_HELLO_D: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 292 | |
| 293 | s->shutdown=0; |
| 294 | ret=ssl3_get_client_hello(s); |
| 295 | if (ret <= 0) goto end; |
| 296 | dtls1_stop_timer(s); |
| 297 | |
| 298 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) |
| 299 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; |
| 300 | else |
| 301 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 302 | |
| 303 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 304 | break; |
| 305 | |
| 306 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: |
| 307 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: |
| 308 | |
| 309 | ret = dtls1_send_hello_verify_request(s); |
| 310 | if ( ret <= 0) goto end; |
| 311 | s->state=SSL3_ST_SW_FLUSH; |
| 312 | s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; |
| 313 | |
| 314 | /* HelloVerifyRequest resets Finished MAC */ |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame^] | 315 | if (!ssl3_init_finished_mac(s)) |
| 316 | { |
| 317 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 318 | ret = -1; |
| 319 | goto end; |
| 320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | |
| 324 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 325 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 326 | s->renegotiate = 2; |
| 327 | dtls1_start_timer(s); |
| 328 | ret=ssl3_send_server_hello(s); |
| 329 | if (ret <= 0) goto end; |
| 330 | |
| 331 | if (s->hit) |
| 332 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | if (s->tlsext_ticket_expected) |
| 334 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
| 335 | else |
| 336 | s->state=SSL3_ST_SW_CHANGE_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | } |
| 338 | else |
| 339 | s->state=SSL3_ST_SW_CERT_A; |
| 340 | s->init_num=0; |
| 341 | break; |
| 342 | |
| 343 | case SSL3_ST_SW_CERT_A: |
| 344 | case SSL3_ST_SW_CERT_B: |
| 345 | /* Check if it is anon DH or normal PSK */ |
| 346 | if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) |
| 347 | && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
| 348 | { |
| 349 | dtls1_start_timer(s); |
| 350 | ret=ssl3_send_server_certificate(s); |
| 351 | if (ret <= 0) goto end; |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 352 | if (s->s3->tmp.certificate_status_expected) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | s->state=SSL3_ST_SW_CERT_STATUS_A; |
| 354 | else |
| 355 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 356 | } |
| 357 | else |
| 358 | { |
| 359 | skip = 1; |
| 360 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 361 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | s->init_num=0; |
| 363 | break; |
| 364 | |
| 365 | case SSL3_ST_SW_KEY_EXCH_A: |
| 366 | case SSL3_ST_SW_KEY_EXCH_B: |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 367 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 368 | |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 369 | /* Send a ServerKeyExchange message if: |
| 370 | * - The key exchange is ephemeral or anonymous |
| 371 | * Diffie-Hellman. |
| 372 | * - There is a PSK identity hint. |
David Benjamin | b9cc33a | 2014-07-15 00:09:48 -0400 | [diff] [blame] | 373 | * |
| 374 | * TODO(davidben): This logic is currently duplicated |
| 375 | * in s3_srvr.c. Fix this. In the meantime, keep them |
| 376 | * in sync. |
| 377 | */ |
| 378 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
David Benjamin | 688d8df | 2014-11-02 23:06:42 -0500 | [diff] [blame] | 379 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | { |
| 381 | dtls1_start_timer(s); |
| 382 | ret=ssl3_send_server_key_exchange(s); |
| 383 | if (ret <= 0) goto end; |
| 384 | } |
| 385 | else |
| 386 | skip=1; |
| 387 | |
| 388 | s->state=SSL3_ST_SW_CERT_REQ_A; |
| 389 | s->init_num=0; |
| 390 | break; |
| 391 | |
| 392 | case SSL3_ST_SW_CERT_REQ_A: |
| 393 | case SSL3_ST_SW_CERT_REQ_B: |
| 394 | if (/* don't request cert unless asked for it: */ |
| 395 | !(s->verify_mode & SSL_VERIFY_PEER) || |
| 396 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 397 | * don't request cert during re-negotiation: */ |
| 398 | ((s->session->peer != NULL) && |
| 399 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 400 | /* never request cert in anonymous ciphersuites |
| 401 | * (see section "Certificate request" in SSL 3 drafts |
| 402 | * and in RFC 2246): */ |
| 403 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 404 | /* ... except when the application insists on verification |
| 405 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 406 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 407 | /* With normal PSK Certificates and |
| 408 | * Certificate Requests are omitted */ |
David Benjamin | d26aea6 | 2014-07-12 00:13:56 -0400 | [diff] [blame] | 409 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | { |
| 411 | /* no cert request */ |
| 412 | skip=1; |
| 413 | s->s3->tmp.cert_request=0; |
| 414 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | s->s3->tmp.cert_request=1; |
| 419 | dtls1_start_timer(s); |
| 420 | ret=ssl3_send_certificate_request(s); |
| 421 | if (ret <= 0) goto end; |
| 422 | #ifndef NETSCAPE_HANG_BUG |
| 423 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 424 | #else |
| 425 | s->state=SSL3_ST_SW_FLUSH; |
| 426 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 427 | #endif |
| 428 | s->init_num=0; |
| 429 | } |
| 430 | break; |
| 431 | |
| 432 | case SSL3_ST_SW_SRVR_DONE_A: |
| 433 | case SSL3_ST_SW_SRVR_DONE_B: |
| 434 | dtls1_start_timer(s); |
| 435 | ret=ssl3_send_server_done(s); |
| 436 | if (ret <= 0) goto end; |
| 437 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 438 | s->state=SSL3_ST_SW_FLUSH; |
| 439 | s->init_num=0; |
| 440 | break; |
| 441 | |
| 442 | case SSL3_ST_SW_FLUSH: |
| 443 | s->rwstate=SSL_WRITING; |
| 444 | if (BIO_flush(s->wbio) <= 0) |
| 445 | { |
| 446 | /* If the write error was fatal, stop trying */ |
| 447 | if (!BIO_should_retry(s->wbio)) |
| 448 | { |
| 449 | s->rwstate=SSL_NOTHING; |
| 450 | s->state=s->s3->tmp.next_state; |
| 451 | } |
| 452 | |
| 453 | ret= -1; |
| 454 | goto end; |
| 455 | } |
| 456 | s->rwstate=SSL_NOTHING; |
| 457 | s->state=s->s3->tmp.next_state; |
| 458 | break; |
| 459 | |
| 460 | case SSL3_ST_SR_CERT_A: |
| 461 | case SSL3_ST_SR_CERT_B: |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 462 | if (s->s3->tmp.cert_request) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | { |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 464 | ret=ssl3_get_client_certificate(s); |
| 465 | if (ret <= 0) goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | } |
David Benjamin | 92909a6 | 2014-08-20 11:40:03 -0400 | [diff] [blame] | 467 | s->init_num=0; |
| 468 | s->state=SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 469 | break; |
| 470 | |
| 471 | case SSL3_ST_SR_KEY_EXCH_A: |
| 472 | case SSL3_ST_SR_KEY_EXCH_B: |
| 473 | ret=ssl3_get_client_key_exchange(s); |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 474 | if (ret <= 0) |
| 475 | goto end; |
David Benjamin | b52e3dd | 2014-08-20 11:12:48 -0400 | [diff] [blame] | 476 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 477 | s->init_num=0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 478 | break; |
| 479 | |
| 480 | case SSL3_ST_SR_CERT_VRFY_A: |
| 481 | case SSL3_ST_SR_CERT_VRFY_B: |
| 482 | |
| 483 | s->d1->change_cipher_spec_ok = 1; |
| 484 | /* we should decide if we expected this one */ |
| 485 | ret=ssl3_get_cert_verify(s); |
| 486 | if (ret <= 0) goto end; |
| 487 | s->state=SSL3_ST_SR_FINISHED_A; |
| 488 | s->init_num=0; |
| 489 | break; |
| 490 | |
| 491 | case SSL3_ST_SR_FINISHED_A: |
| 492 | case SSL3_ST_SR_FINISHED_B: |
| 493 | s->d1->change_cipher_spec_ok = 1; |
| 494 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 495 | SSL3_ST_SR_FINISHED_B); |
| 496 | if (ret <= 0) goto end; |
| 497 | dtls1_stop_timer(s); |
| 498 | if (s->hit) |
| 499 | s->state=SSL_ST_OK; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | else if (s->tlsext_ticket_expected) |
| 501 | s->state=SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 502 | else |
| 503 | s->state=SSL3_ST_SW_CHANGE_A; |
| 504 | s->init_num=0; |
| 505 | break; |
| 506 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 507 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 508 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 8da9906 | 2014-08-24 12:03:09 -0400 | [diff] [blame] | 509 | ret=ssl3_send_new_session_ticket(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 510 | if (ret <= 0) goto end; |
| 511 | s->state=SSL3_ST_SW_CHANGE_A; |
| 512 | s->init_num=0; |
| 513 | break; |
| 514 | |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 515 | #if 0 |
| 516 | // TODO(davidben): Implement OCSP stapling on the server. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | case SSL3_ST_SW_CERT_STATUS_A: |
| 518 | case SSL3_ST_SW_CERT_STATUS_B: |
| 519 | ret=ssl3_send_cert_status(s); |
| 520 | if (ret <= 0) goto end; |
| 521 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 522 | s->init_num=0; |
| 523 | break; |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 524 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 525 | |
| 526 | case SSL3_ST_SW_CHANGE_A: |
| 527 | case SSL3_ST_SW_CHANGE_B: |
| 528 | |
| 529 | s->session->cipher=s->s3->tmp.new_cipher; |
| 530 | if (!s->method->ssl3_enc->setup_key_block(s)) |
| 531 | { ret= -1; goto end; } |
| 532 | |
| 533 | ret=dtls1_send_change_cipher_spec(s, |
| 534 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 535 | |
| 536 | if (ret <= 0) goto end; |
| 537 | |
| 538 | s->state=SSL3_ST_SW_FINISHED_A; |
| 539 | s->init_num=0; |
| 540 | |
| 541 | if (!s->method->ssl3_enc->change_cipher_state(s, |
| 542 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 543 | { |
| 544 | ret= -1; |
| 545 | goto end; |
| 546 | } |
| 547 | |
| 548 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 549 | break; |
| 550 | |
| 551 | case SSL3_ST_SW_FINISHED_A: |
| 552 | case SSL3_ST_SW_FINISHED_B: |
| 553 | ret=ssl3_send_finished(s, |
| 554 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 555 | s->method->ssl3_enc->server_finished_label, |
| 556 | s->method->ssl3_enc->server_finished_label_len); |
| 557 | if (ret <= 0) goto end; |
| 558 | s->state=SSL3_ST_SW_FLUSH; |
| 559 | if (s->hit) |
| 560 | { |
| 561 | s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | s->s3->tmp.next_state=SSL_ST_OK; |
| 566 | } |
| 567 | s->init_num=0; |
| 568 | break; |
| 569 | |
| 570 | case SSL_ST_OK: |
| 571 | /* clean a few things up */ |
| 572 | ssl3_cleanup_key_block(s); |
| 573 | |
| 574 | #if 0 |
| 575 | BUF_MEM_free(s->init_buf); |
| 576 | s->init_buf=NULL; |
| 577 | #endif |
| 578 | |
| 579 | /* remove buffering on output */ |
| 580 | ssl_free_wbio_buffer(s); |
| 581 | |
| 582 | s->init_num=0; |
| 583 | |
| 584 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ |
| 585 | { |
| 586 | s->renegotiate=0; |
| 587 | s->new_session=0; |
| 588 | |
| 589 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 590 | |
| 591 | s->ctx->stats.sess_accept_good++; |
| 592 | /* s->server=1; */ |
| 593 | s->handshake_func=dtls1_accept; |
| 594 | |
| 595 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 596 | } |
| 597 | |
| 598 | ret = 1; |
| 599 | |
| 600 | /* done handshaking, next message is client hello */ |
| 601 | s->d1->handshake_read_seq = 0; |
| 602 | /* next message is server hello */ |
| 603 | s->d1->handshake_write_seq = 0; |
| 604 | s->d1->next_handshake_write_seq = 0; |
| 605 | goto end; |
| 606 | /* break; */ |
| 607 | |
| 608 | default: |
| 609 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE); |
| 610 | ret= -1; |
| 611 | goto end; |
| 612 | /* break; */ |
| 613 | } |
| 614 | |
| 615 | if (!s->s3->tmp.reuse_message && !skip) |
| 616 | { |
| 617 | if (s->debug) |
| 618 | { |
| 619 | if ((ret=BIO_flush(s->wbio)) <= 0) |
| 620 | goto end; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | if ((cb != NULL) && (s->state != state)) |
| 625 | { |
| 626 | new_state=s->state; |
| 627 | s->state=state; |
| 628 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 629 | s->state=new_state; |
| 630 | } |
| 631 | } |
| 632 | skip=0; |
| 633 | } |
| 634 | end: |
| 635 | /* BIO_flush(s->wbio); */ |
| 636 | |
| 637 | s->in_handshake--; |
| 638 | |
| 639 | if (cb != NULL) |
| 640 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 641 | return(ret); |
| 642 | } |
| 643 | |
| 644 | int dtls1_send_hello_verify_request(SSL *s) |
| 645 | { |
| 646 | unsigned int msg_len; |
| 647 | unsigned char *msg, *buf, *p; |
| 648 | |
| 649 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) |
| 650 | { |
| 651 | buf = (unsigned char *)s->init_buf->data; |
| 652 | |
| 653 | msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); |
| 654 | /* Always use DTLS 1.0 version: see RFC 6347 */ |
| 655 | *(p++) = DTLS1_VERSION >> 8; |
| 656 | *(p++) = DTLS1_VERSION & 0xFF; |
| 657 | |
| 658 | if (s->ctx->app_gen_cookie_cb == NULL || |
David Benjamin | fb4ea28 | 2014-08-15 13:38:15 -0400 | [diff] [blame] | 659 | 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] | 660 | { |
| 661 | OPENSSL_PUT_ERROR(SSL, dtls1_send_hello_verify_request, ERR_R_INTERNAL_ERROR); |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | *(p++) = (unsigned char) s->d1->cookie_len; |
| 666 | memcpy(p, s->d1->cookie, s->d1->cookie_len); |
| 667 | p += s->d1->cookie_len; |
| 668 | msg_len = p - msg; |
| 669 | |
| 670 | dtls1_set_message_header(s, buf, |
| 671 | DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len); |
| 672 | |
| 673 | s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; |
| 674 | /* number of bytes to write */ |
| 675 | s->init_num=p-buf; |
| 676 | s->init_off=0; |
| 677 | } |
| 678 | |
| 679 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 680 | return(dtls1_do_write(s,SSL3_RT_HANDSHAKE, add_to_finished_hash)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 681 | } |