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/obj.h> |
| 124 | #include <openssl/rand.h> |
| 125 | #include <openssl/x509.h> |
| 126 | |
| 127 | #include "ssl_locl.h" |
| 128 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 129 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 130 | int dtls1_accept(SSL *s) { |
| 131 | BUF_MEM *buf = NULL; |
| 132 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 133 | unsigned long alg_a; |
| 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 | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 137 | assert(s->handshake_func == dtls1_accept); |
| 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 | 2481975 | 2014-12-15 18:42:07 -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 | 2481975 | 2014-12-15 18:42:07 -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 | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 150 | s->in_handshake++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 152 | if (s->cert == NULL) { |
| 153 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_NO_CERTIFICATE_SET); |
| 154 | return -1; |
| 155 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 157 | for (;;) { |
| 158 | state = s->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 160 | switch (s->state) { |
| 161 | case SSL_ST_RENEGOTIATE: |
| 162 | s->renegotiate = 1; |
| 163 | /* s->state=SSL_ST_ACCEPT; */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 165 | case SSL_ST_ACCEPT: |
| 166 | case SSL_ST_BEFORE | SSL_ST_ACCEPT: |
| 167 | if (cb != NULL) { |
| 168 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 169 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 171 | if (s->init_buf == NULL) { |
| 172 | buf = BUF_MEM_new(); |
| 173 | if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 174 | ret = -1; |
| 175 | goto end; |
| 176 | } |
| 177 | s->init_buf = buf; |
| 178 | buf = NULL; |
| 179 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 181 | if (!ssl3_setup_buffers(s)) { |
| 182 | ret = -1; |
| 183 | goto end; |
| 184 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 186 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 188 | if (s->state != SSL_ST_RENEGOTIATE) { |
| 189 | if (!ssl_init_wbio_buffer(s, 1)) { |
| 190 | ret = -1; |
| 191 | goto end; |
| 192 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 194 | if (!ssl3_init_finished_mac(s)) { |
| 195 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 196 | ret = -1; |
| 197 | goto end; |
| 198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 199 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 200 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 201 | s->ctx->stats.sess_accept++; |
| 202 | } else { |
| 203 | /* s->state == SSL_ST_RENEGOTIATE, * we will just send a |
| 204 | * HelloRequest */ |
| 205 | s->ctx->stats.sess_accept_renegotiate++; |
| 206 | s->state = SSL3_ST_SW_HELLO_REQ_A; |
| 207 | } |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 208 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 209 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 211 | case SSL3_ST_SW_HELLO_REQ_A: |
| 212 | case SSL3_ST_SW_HELLO_REQ_B: |
| 213 | s->shutdown = 0; |
| 214 | dtls1_clear_record_buffer(s); |
| 215 | dtls1_start_timer(s); |
| 216 | ret = ssl3_send_hello_request(s); |
| 217 | if (ret <= 0) { |
| 218 | goto end; |
| 219 | } |
| 220 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; |
| 221 | s->state = SSL3_ST_SW_FLUSH; |
| 222 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 224 | if (!ssl3_init_finished_mac(s)) { |
| 225 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 226 | ret = -1; |
| 227 | goto end; |
| 228 | } |
| 229 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 231 | case SSL3_ST_SW_HELLO_REQ_C: |
| 232 | s->state = SSL_ST_OK; |
| 233 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 235 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 236 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 237 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 238 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 239 | s->shutdown = 0; |
| 240 | ret = ssl3_get_client_hello(s); |
| 241 | if (ret <= 0) { |
| 242 | goto end; |
| 243 | } |
| 244 | dtls1_stop_timer(s); |
David Benjamin | a54e2e8 | 2015-02-16 16:31:43 -0500 | [diff] [blame^] | 245 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 246 | s->init_num = 0; |
| 247 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 249 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 250 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 251 | s->renegotiate = 2; |
| 252 | dtls1_start_timer(s); |
| 253 | ret = ssl3_send_server_hello(s); |
| 254 | if (ret <= 0) { |
| 255 | goto end; |
| 256 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 258 | if (s->hit) { |
| 259 | if (s->tlsext_ticket_expected) { |
| 260 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 261 | } else { |
| 262 | s->state = SSL3_ST_SW_CHANGE_A; |
| 263 | } |
| 264 | } else { |
| 265 | s->state = SSL3_ST_SW_CERT_A; |
| 266 | } |
| 267 | s->init_num = 0; |
| 268 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 270 | case SSL3_ST_SW_CERT_A: |
| 271 | case SSL3_ST_SW_CERT_B: |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 272 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 273 | dtls1_start_timer(s); |
| 274 | ret = ssl3_send_server_certificate(s); |
| 275 | if (ret <= 0) { |
| 276 | goto end; |
| 277 | } |
| 278 | if (s->s3->tmp.certificate_status_expected) { |
| 279 | s->state = SSL3_ST_SW_CERT_STATUS_A; |
| 280 | } else { |
| 281 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 282 | } |
| 283 | } else { |
| 284 | skip = 1; |
| 285 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 286 | } |
| 287 | s->init_num = 0; |
| 288 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 290 | case SSL3_ST_SW_KEY_EXCH_A: |
| 291 | case SSL3_ST_SW_KEY_EXCH_B: |
| 292 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 293 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 294 | /* Send a ServerKeyExchange message if: |
| 295 | * - The key exchange is ephemeral or anonymous |
| 296 | * Diffie-Hellman. |
| 297 | * - There is a PSK identity hint. |
| 298 | * |
| 299 | * TODO(davidben): This logic is currently duplicated |
| 300 | * in s3_srvr.c. Fix this. In the meantime, keep them |
| 301 | * in sync. */ |
| 302 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
| 303 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) { |
| 304 | dtls1_start_timer(s); |
| 305 | ret = ssl3_send_server_key_exchange(s); |
| 306 | if (ret <= 0) { |
| 307 | goto end; |
| 308 | } |
| 309 | } else { |
| 310 | skip = 1; |
| 311 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 313 | s->state = SSL3_ST_SW_CERT_REQ_A; |
| 314 | s->init_num = 0; |
| 315 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 317 | case SSL3_ST_SW_CERT_REQ_A: |
| 318 | case SSL3_ST_SW_CERT_REQ_B: |
| 319 | if (/* don't request cert unless asked for it: */ |
| 320 | !(s->verify_mode & SSL_VERIFY_PEER) || |
| 321 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 322 | * don't request cert during re-negotiation: */ |
| 323 | ((s->session->peer != NULL) && |
| 324 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
| 325 | /* never request cert in anonymous ciphersuites |
| 326 | * (see section "Certificate request" in SSL 3 drafts |
| 327 | * and in RFC 2246): */ |
| 328 | ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && |
| 329 | /* ... except when the application insists on verification |
| 330 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ |
| 331 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || |
| 332 | /* With normal PSK Certificates and |
| 333 | * Certificate Requests are omitted */ |
| 334 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 335 | /* no cert request */ |
| 336 | skip = 1; |
| 337 | s->s3->tmp.cert_request = 0; |
| 338 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
| 339 | } else { |
| 340 | s->s3->tmp.cert_request = 1; |
| 341 | dtls1_start_timer(s); |
| 342 | ret = ssl3_send_certificate_request(s); |
| 343 | if (ret <= 0) { |
| 344 | goto end; |
| 345 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 346 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 347 | s->init_num = 0; |
| 348 | } |
| 349 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 351 | case SSL3_ST_SW_SRVR_DONE_A: |
| 352 | case SSL3_ST_SW_SRVR_DONE_B: |
| 353 | dtls1_start_timer(s); |
| 354 | ret = ssl3_send_server_done(s); |
| 355 | if (ret <= 0) { |
| 356 | goto end; |
| 357 | } |
| 358 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 359 | s->state = SSL3_ST_SW_FLUSH; |
| 360 | s->init_num = 0; |
| 361 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 363 | case SSL3_ST_SW_FLUSH: |
| 364 | s->rwstate = SSL_WRITING; |
| 365 | if (BIO_flush(s->wbio) <= 0) { |
| 366 | /* If the write error was fatal, stop trying */ |
| 367 | if (!BIO_should_retry(s->wbio)) { |
| 368 | s->rwstate = SSL_NOTHING; |
| 369 | s->state = s->s3->tmp.next_state; |
| 370 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 372 | ret = -1; |
| 373 | goto end; |
| 374 | } |
| 375 | s->rwstate = SSL_NOTHING; |
| 376 | s->state = s->s3->tmp.next_state; |
| 377 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 378 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 379 | case SSL3_ST_SR_CERT_A: |
| 380 | case SSL3_ST_SR_CERT_B: |
| 381 | if (s->s3->tmp.cert_request) { |
| 382 | ret = ssl3_get_client_certificate(s); |
| 383 | if (ret <= 0) { |
| 384 | goto end; |
| 385 | } |
| 386 | } |
| 387 | s->init_num = 0; |
| 388 | s->state = SSL3_ST_SR_KEY_EXCH_A; |
| 389 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 391 | case SSL3_ST_SR_KEY_EXCH_A: |
| 392 | case SSL3_ST_SR_KEY_EXCH_B: |
| 393 | ret = ssl3_get_client_key_exchange(s); |
| 394 | if (ret <= 0) { |
| 395 | goto end; |
| 396 | } |
| 397 | s->state = SSL3_ST_SR_CERT_VRFY_A; |
| 398 | s->init_num = 0; |
| 399 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 401 | case SSL3_ST_SR_CERT_VRFY_A: |
| 402 | case SSL3_ST_SR_CERT_VRFY_B: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 403 | ret = ssl3_get_cert_verify(s); |
| 404 | if (ret <= 0) { |
| 405 | goto end; |
| 406 | } |
| 407 | s->state = SSL3_ST_SR_FINISHED_A; |
| 408 | s->init_num = 0; |
| 409 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 410 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 411 | case SSL3_ST_SR_FINISHED_A: |
| 412 | case SSL3_ST_SR_FINISHED_B: |
| 413 | s->d1->change_cipher_spec_ok = 1; |
| 414 | ret = |
| 415 | ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B); |
| 416 | if (ret <= 0) { |
| 417 | goto end; |
| 418 | } |
| 419 | dtls1_stop_timer(s); |
| 420 | if (s->hit) { |
| 421 | s->state = SSL_ST_OK; |
| 422 | } else if (s->tlsext_ticket_expected) { |
| 423 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 424 | } else { |
| 425 | s->state = SSL3_ST_SW_CHANGE_A; |
| 426 | } |
| 427 | s->init_num = 0; |
| 428 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 429 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 430 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 431 | case SSL3_ST_SW_SESSION_TICKET_B: |
| 432 | ret = ssl3_send_new_session_ticket(s); |
| 433 | if (ret <= 0) { |
| 434 | goto end; |
| 435 | } |
| 436 | s->state = SSL3_ST_SW_CHANGE_A; |
| 437 | s->init_num = 0; |
| 438 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 440 | case SSL3_ST_SW_CHANGE_A: |
| 441 | case SSL3_ST_SW_CHANGE_B: |
| 442 | s->session->cipher = s->s3->tmp.new_cipher; |
| 443 | if (!s->enc_method->setup_key_block(s)) { |
| 444 | ret = -1; |
| 445 | goto end; |
| 446 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 448 | ret = dtls1_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A, |
| 449 | SSL3_ST_SW_CHANGE_B); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 451 | if (ret <= 0) { |
| 452 | goto end; |
| 453 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 454 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 455 | s->state = SSL3_ST_SW_FINISHED_A; |
| 456 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 458 | if (!s->enc_method->change_cipher_state( |
| 459 | s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
| 460 | ret = -1; |
| 461 | goto end; |
| 462 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 464 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 465 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 467 | case SSL3_ST_SW_FINISHED_A: |
| 468 | case SSL3_ST_SW_FINISHED_B: |
| 469 | ret = |
| 470 | ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, |
| 471 | s->enc_method->server_finished_label, |
| 472 | s->enc_method->server_finished_label_len); |
| 473 | if (ret <= 0) { |
| 474 | goto end; |
| 475 | } |
| 476 | s->state = SSL3_ST_SW_FLUSH; |
| 477 | if (s->hit) { |
| 478 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; |
| 479 | } else { |
| 480 | s->s3->tmp.next_state = SSL_ST_OK; |
| 481 | } |
| 482 | s->init_num = 0; |
| 483 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 485 | case SSL_ST_OK: |
| 486 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 487 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 488 | /* remove buffering on output */ |
| 489 | ssl_free_wbio_buffer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 490 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 491 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 492 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 493 | if (s->renegotiate == 2) { |
| 494 | /* skipped if we just sent a HelloRequest */ |
| 495 | s->renegotiate = 0; |
| 496 | s->new_session = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 497 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 498 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 499 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 500 | s->ctx->stats.sess_accept_good++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 501 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 502 | if (cb != NULL) { |
| 503 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
| 504 | } |
| 505 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 507 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 508 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 509 | /* done handshaking, next message is client hello */ |
| 510 | s->d1->handshake_read_seq = 0; |
| 511 | /* next message is server hello */ |
| 512 | s->d1->handshake_write_seq = 0; |
| 513 | s->d1->next_handshake_write_seq = 0; |
| 514 | goto end; |
| 515 | |
| 516 | default: |
| 517 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE); |
| 518 | ret = -1; |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | if (!s->s3->tmp.reuse_message && !skip) { |
| 523 | if (cb != NULL && s->state != state) { |
| 524 | new_state = s->state; |
| 525 | s->state = state; |
| 526 | cb(s, SSL_CB_ACCEPT_LOOP, 1); |
| 527 | s->state = new_state; |
| 528 | } |
| 529 | } |
| 530 | skip = 0; |
| 531 | } |
| 532 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 533 | end: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 534 | s->in_handshake--; |
| 535 | if (buf != NULL) { |
| 536 | BUF_MEM_free(buf); |
| 537 | } |
| 538 | if (cb != NULL) { |
| 539 | cb(s, SSL_CB_ACCEPT_EXIT, ret); |
| 540 | } |
| 541 | return ret; |
| 542 | } |