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 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame^] | 127 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
Adam Langley | 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; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 201 | } else { |
| 202 | /* s->state == SSL_ST_RENEGOTIATE, * we will just send a |
| 203 | * HelloRequest */ |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 204 | s->state = SSL3_ST_SW_HELLO_REQ_A; |
| 205 | } |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame] | 206 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 207 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 209 | case SSL3_ST_SW_HELLO_REQ_A: |
| 210 | case SSL3_ST_SW_HELLO_REQ_B: |
| 211 | s->shutdown = 0; |
| 212 | dtls1_clear_record_buffer(s); |
| 213 | dtls1_start_timer(s); |
| 214 | ret = ssl3_send_hello_request(s); |
| 215 | if (ret <= 0) { |
| 216 | goto end; |
| 217 | } |
| 218 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; |
| 219 | s->state = SSL3_ST_SW_FLUSH; |
| 220 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 222 | if (!ssl3_init_finished_mac(s)) { |
| 223 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR); |
| 224 | ret = -1; |
| 225 | goto end; |
| 226 | } |
| 227 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 228 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 229 | case SSL3_ST_SW_HELLO_REQ_C: |
| 230 | s->state = SSL_ST_OK; |
| 231 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 233 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 234 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 235 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 236 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 237 | s->shutdown = 0; |
| 238 | ret = ssl3_get_client_hello(s); |
| 239 | if (ret <= 0) { |
| 240 | goto end; |
| 241 | } |
| 242 | dtls1_stop_timer(s); |
David Benjamin | a54e2e8 | 2015-02-16 16:31:43 -0500 | [diff] [blame] | 243 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 244 | s->init_num = 0; |
| 245 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 246 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 247 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 248 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 249 | s->renegotiate = 2; |
| 250 | dtls1_start_timer(s); |
| 251 | ret = ssl3_send_server_hello(s); |
| 252 | if (ret <= 0) { |
| 253 | goto end; |
| 254 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 255 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 256 | if (s->hit) { |
| 257 | if (s->tlsext_ticket_expected) { |
| 258 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 259 | } else { |
| 260 | s->state = SSL3_ST_SW_CHANGE_A; |
| 261 | } |
| 262 | } else { |
| 263 | s->state = SSL3_ST_SW_CERT_A; |
| 264 | } |
| 265 | s->init_num = 0; |
| 266 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 268 | case SSL3_ST_SW_CERT_A: |
| 269 | case SSL3_ST_SW_CERT_B: |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 270 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 271 | dtls1_start_timer(s); |
| 272 | ret = ssl3_send_server_certificate(s); |
| 273 | if (ret <= 0) { |
| 274 | goto end; |
| 275 | } |
| 276 | if (s->s3->tmp.certificate_status_expected) { |
| 277 | s->state = SSL3_ST_SW_CERT_STATUS_A; |
| 278 | } else { |
| 279 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 280 | } |
| 281 | } else { |
| 282 | skip = 1; |
| 283 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 284 | } |
| 285 | s->init_num = 0; |
| 286 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 287 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 288 | case SSL3_ST_SW_KEY_EXCH_A: |
| 289 | case SSL3_ST_SW_KEY_EXCH_B: |
| 290 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 292 | /* Send a ServerKeyExchange message if: |
| 293 | * - The key exchange is ephemeral or anonymous |
| 294 | * Diffie-Hellman. |
| 295 | * - There is a PSK identity hint. |
| 296 | * |
| 297 | * TODO(davidben): This logic is currently duplicated |
| 298 | * in s3_srvr.c. Fix this. In the meantime, keep them |
| 299 | * in sync. */ |
| 300 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
| 301 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) { |
| 302 | dtls1_start_timer(s); |
| 303 | ret = ssl3_send_server_key_exchange(s); |
| 304 | if (ret <= 0) { |
| 305 | goto end; |
| 306 | } |
| 307 | } else { |
| 308 | skip = 1; |
| 309 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 311 | s->state = SSL3_ST_SW_CERT_REQ_A; |
| 312 | s->init_num = 0; |
| 313 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 315 | case SSL3_ST_SW_CERT_REQ_A: |
| 316 | case SSL3_ST_SW_CERT_REQ_B: |
| 317 | if (/* don't request cert unless asked for it: */ |
| 318 | !(s->verify_mode & SSL_VERIFY_PEER) || |
| 319 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 320 | * don't request cert during re-negotiation: */ |
| 321 | ((s->session->peer != NULL) && |
| 322 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 323 | /* With normal PSK Certificates and |
| 324 | * Certificate Requests are omitted */ |
| 325 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 326 | /* no cert request */ |
| 327 | skip = 1; |
| 328 | s->s3->tmp.cert_request = 0; |
| 329 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
| 330 | } else { |
| 331 | s->s3->tmp.cert_request = 1; |
| 332 | dtls1_start_timer(s); |
| 333 | ret = ssl3_send_certificate_request(s); |
| 334 | if (ret <= 0) { |
| 335 | goto end; |
| 336 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 337 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 338 | s->init_num = 0; |
| 339 | } |
| 340 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 341 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 342 | case SSL3_ST_SW_SRVR_DONE_A: |
| 343 | case SSL3_ST_SW_SRVR_DONE_B: |
| 344 | dtls1_start_timer(s); |
| 345 | ret = ssl3_send_server_done(s); |
| 346 | if (ret <= 0) { |
| 347 | goto end; |
| 348 | } |
| 349 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 350 | s->state = SSL3_ST_SW_FLUSH; |
| 351 | s->init_num = 0; |
| 352 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 354 | case SSL3_ST_SW_FLUSH: |
| 355 | s->rwstate = SSL_WRITING; |
| 356 | if (BIO_flush(s->wbio) <= 0) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 357 | ret = -1; |
| 358 | goto end; |
| 359 | } |
| 360 | s->rwstate = SSL_NOTHING; |
| 361 | s->state = s->s3->tmp.next_state; |
| 362 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 364 | case SSL3_ST_SR_CERT_A: |
| 365 | case SSL3_ST_SR_CERT_B: |
| 366 | if (s->s3->tmp.cert_request) { |
| 367 | ret = ssl3_get_client_certificate(s); |
| 368 | if (ret <= 0) { |
| 369 | goto end; |
| 370 | } |
| 371 | } |
| 372 | s->init_num = 0; |
| 373 | s->state = SSL3_ST_SR_KEY_EXCH_A; |
| 374 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 376 | case SSL3_ST_SR_KEY_EXCH_A: |
| 377 | case SSL3_ST_SR_KEY_EXCH_B: |
| 378 | ret = ssl3_get_client_key_exchange(s); |
| 379 | if (ret <= 0) { |
| 380 | goto end; |
| 381 | } |
| 382 | s->state = SSL3_ST_SR_CERT_VRFY_A; |
| 383 | s->init_num = 0; |
| 384 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 386 | case SSL3_ST_SR_CERT_VRFY_A: |
| 387 | case SSL3_ST_SR_CERT_VRFY_B: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 388 | ret = ssl3_get_cert_verify(s); |
| 389 | if (ret <= 0) { |
| 390 | goto end; |
| 391 | } |
| 392 | s->state = SSL3_ST_SR_FINISHED_A; |
| 393 | s->init_num = 0; |
| 394 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 395 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 396 | case SSL3_ST_SR_FINISHED_A: |
| 397 | case SSL3_ST_SR_FINISHED_B: |
| 398 | s->d1->change_cipher_spec_ok = 1; |
| 399 | ret = |
| 400 | ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B); |
| 401 | if (ret <= 0) { |
| 402 | goto end; |
| 403 | } |
| 404 | dtls1_stop_timer(s); |
| 405 | if (s->hit) { |
| 406 | s->state = SSL_ST_OK; |
| 407 | } else if (s->tlsext_ticket_expected) { |
| 408 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 409 | } else { |
| 410 | s->state = SSL3_ST_SW_CHANGE_A; |
| 411 | } |
| 412 | s->init_num = 0; |
| 413 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 415 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 416 | case SSL3_ST_SW_SESSION_TICKET_B: |
| 417 | ret = ssl3_send_new_session_ticket(s); |
| 418 | if (ret <= 0) { |
| 419 | goto end; |
| 420 | } |
| 421 | s->state = SSL3_ST_SW_CHANGE_A; |
| 422 | s->init_num = 0; |
| 423 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 424 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 425 | case SSL3_ST_SW_CHANGE_A: |
| 426 | case SSL3_ST_SW_CHANGE_B: |
| 427 | s->session->cipher = s->s3->tmp.new_cipher; |
| 428 | if (!s->enc_method->setup_key_block(s)) { |
| 429 | ret = -1; |
| 430 | goto end; |
| 431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 433 | ret = dtls1_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A, |
| 434 | SSL3_ST_SW_CHANGE_B); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 436 | if (ret <= 0) { |
| 437 | goto end; |
| 438 | } |
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 | s->state = SSL3_ST_SW_FINISHED_A; |
| 441 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 442 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 443 | if (!s->enc_method->change_cipher_state( |
| 444 | s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
| 445 | ret = -1; |
| 446 | goto end; |
| 447 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 449 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); |
| 450 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 451 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 452 | case SSL3_ST_SW_FINISHED_A: |
| 453 | case SSL3_ST_SW_FINISHED_B: |
| 454 | ret = |
| 455 | ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, |
| 456 | s->enc_method->server_finished_label, |
| 457 | s->enc_method->server_finished_label_len); |
| 458 | if (ret <= 0) { |
| 459 | goto end; |
| 460 | } |
| 461 | s->state = SSL3_ST_SW_FLUSH; |
| 462 | if (s->hit) { |
| 463 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; |
| 464 | } else { |
| 465 | s->s3->tmp.next_state = SSL_ST_OK; |
| 466 | } |
| 467 | s->init_num = 0; |
| 468 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 469 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 470 | case SSL_ST_OK: |
| 471 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 473 | /* remove buffering on output */ |
| 474 | ssl_free_wbio_buffer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 476 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 478 | if (s->renegotiate == 2) { |
| 479 | /* skipped if we just sent a HelloRequest */ |
| 480 | s->renegotiate = 0; |
| 481 | s->new_session = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 482 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 483 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
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 | if (cb != NULL) { |
| 486 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
| 487 | } |
| 488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 490 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 492 | /* done handshaking, next message is client hello */ |
| 493 | s->d1->handshake_read_seq = 0; |
| 494 | /* next message is server hello */ |
| 495 | s->d1->handshake_write_seq = 0; |
| 496 | s->d1->next_handshake_write_seq = 0; |
| 497 | goto end; |
| 498 | |
| 499 | default: |
| 500 | OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE); |
| 501 | ret = -1; |
| 502 | goto end; |
| 503 | } |
| 504 | |
| 505 | if (!s->s3->tmp.reuse_message && !skip) { |
| 506 | if (cb != NULL && s->state != state) { |
| 507 | new_state = s->state; |
| 508 | s->state = state; |
| 509 | cb(s, SSL_CB_ACCEPT_LOOP, 1); |
| 510 | s->state = new_state; |
| 511 | } |
| 512 | } |
| 513 | skip = 0; |
| 514 | } |
| 515 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 516 | end: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 517 | s->in_handshake--; |
| 518 | if (buf != NULL) { |
| 519 | BUF_MEM_free(buf); |
| 520 | } |
| 521 | if (cb != NULL) { |
| 522 | cb(s, SSL_CB_ACCEPT_EXIT, ret); |
| 523 | } |
| 524 | return ret; |
| 525 | } |