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