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 | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 115 | #include <openssl/ssl.h> |
| 116 | |
David Benjamin | 0b145c2 | 2014-11-26 20:10:09 -0500 | [diff] [blame] | 117 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 118 | |
| 119 | #include <openssl/bn.h> |
| 120 | #include <openssl/buf.h> |
| 121 | #include <openssl/dh.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 122 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 123 | #include <openssl/evp.h> |
| 124 | #include <openssl/md5.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 131 | int dtls1_accept(SSL *ssl) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 132 | BUF_MEM *buf = NULL; |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 133 | void (*cb)(const SSL *ssl, int type, int value) = 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 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 138 | assert(ssl->handshake_func == dtls1_accept); |
| 139 | assert(ssl->server); |
| 140 | assert(SSL_IS_DTLS(ssl)); |
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_system_error(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 144 | if (ssl->info_callback != NULL) { |
| 145 | cb = ssl->info_callback; |
| 146 | } else if (ssl->ctx->info_callback != NULL) { |
| 147 | cb = ssl->ctx->info_callback; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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 | for (;;) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 151 | state = ssl->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 153 | switch (ssl->state) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 154 | case SSL_ST_ACCEPT: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 155 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 156 | cb(ssl, SSL_CB_HANDSHAKE_START, 1); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 157 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 159 | if (ssl->init_buf == NULL) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 160 | buf = BUF_MEM_new(); |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 161 | if (buf == NULL || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 162 | ret = -1; |
| 163 | goto end; |
| 164 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 165 | ssl->init_buf = buf; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 166 | buf = NULL; |
| 167 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 169 | ssl->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 170 | |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 171 | if (!ssl_init_wbio_buffer(ssl)) { |
David Benjamin | d23d5a5 | 2015-05-15 21:48:48 -0400 | [diff] [blame] | 172 | ret = -1; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 173 | goto end; |
| 174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 176 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 177 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 178 | ret = -1; |
| 179 | goto end; |
| 180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 182 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 183 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 185 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 186 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 187 | case SSL3_ST_SR_CLNT_HELLO_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 188 | ret = ssl3_get_client_hello(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 189 | if (ret <= 0) { |
| 190 | goto end; |
| 191 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 192 | dtls1_stop_timer(ssl); |
| 193 | ssl->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 194 | break; |
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 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 197 | case SSL3_ST_SW_SRVR_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 198 | dtls1_start_timer(ssl); |
| 199 | ret = ssl3_send_server_hello(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 200 | if (ret <= 0) { |
| 201 | goto end; |
| 202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 204 | if (ssl->hit) { |
| 205 | if (ssl->tlsext_ticket_expected) { |
| 206 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 207 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 208 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 209 | } |
| 210 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 211 | ssl->state = SSL3_ST_SW_CERT_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 212 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 213 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 215 | case SSL3_ST_SW_CERT_A: |
| 216 | case SSL3_ST_SW_CERT_B: |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 217 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 218 | dtls1_start_timer(ssl); |
| 219 | ret = ssl3_send_server_certificate(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 220 | if (ret <= 0) { |
| 221 | goto end; |
| 222 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 223 | if (ssl->s3->tmp.certificate_status_expected) { |
| 224 | ssl->state = SSL3_ST_SW_CERT_STATUS_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 225 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 226 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 227 | } |
| 228 | } else { |
| 229 | skip = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 230 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 231 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 232 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 234 | case SSL3_ST_SW_CERT_STATUS_A: |
| 235 | case SSL3_ST_SW_CERT_STATUS_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 236 | ret = ssl3_send_certificate_status(ssl); |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 237 | if (ret <= 0) { |
| 238 | goto end; |
| 239 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 240 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 241 | break; |
| 242 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 243 | case SSL3_ST_SW_KEY_EXCH_A: |
| 244 | case SSL3_ST_SW_KEY_EXCH_B: |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 245 | case SSL3_ST_SW_KEY_EXCH_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 246 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 248 | /* Send a ServerKeyExchange message if: |
| 249 | * - The key exchange is ephemeral or anonymous |
| 250 | * Diffie-Hellman. |
| 251 | * - There is a PSK identity hint. |
| 252 | * |
| 253 | * TODO(davidben): This logic is currently duplicated |
| 254 | * in s3_srvr.c. Fix this. In the meantime, keep them |
| 255 | * in sync. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 256 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) || |
| 257 | ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) { |
| 258 | dtls1_start_timer(ssl); |
| 259 | ret = ssl3_send_server_key_exchange(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 260 | if (ret <= 0) { |
| 261 | goto end; |
| 262 | } |
| 263 | } else { |
| 264 | skip = 1; |
| 265 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 266 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 267 | ssl->state = SSL3_ST_SW_CERT_REQ_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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_REQ_A: |
| 271 | case SSL3_ST_SW_CERT_REQ_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 272 | if (ssl->s3->tmp.cert_request) { |
| 273 | dtls1_start_timer(ssl); |
| 274 | ret = ssl3_send_certificate_request(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 275 | if (ret <= 0) { |
| 276 | goto end; |
| 277 | } |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 278 | } else { |
| 279 | skip = 1; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 280 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 281 | ssl->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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_SRVR_DONE_A: |
| 285 | case SSL3_ST_SW_SRVR_DONE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 286 | dtls1_start_timer(ssl); |
| 287 | ret = ssl3_send_server_done(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 288 | if (ret <= 0) { |
| 289 | goto end; |
| 290 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 291 | ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 292 | ssl->state = SSL3_ST_SW_FLUSH; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 293 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 295 | case SSL3_ST_SW_FLUSH: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 296 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 297 | ssl->rwstate = SSL_WRITING; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 298 | ret = -1; |
| 299 | goto end; |
| 300 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 301 | ssl->state = ssl->s3->tmp.next_state; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 302 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 304 | case SSL3_ST_SR_CERT_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 305 | if (ssl->s3->tmp.cert_request) { |
| 306 | ret = ssl3_get_client_certificate(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 307 | if (ret <= 0) { |
| 308 | goto end; |
| 309 | } |
| 310 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 311 | ssl->state = SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 312 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 313 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 314 | case SSL3_ST_SR_KEY_EXCH_A: |
| 315 | case SSL3_ST_SR_KEY_EXCH_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 316 | ret = ssl3_get_client_key_exchange(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 317 | if (ret <= 0) { |
| 318 | goto end; |
| 319 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 320 | ssl->state = SSL3_ST_SR_CERT_VRFY_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 321 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 323 | case SSL3_ST_SR_CERT_VRFY_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 324 | ret = ssl3_get_cert_verify(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 325 | if (ret <= 0) { |
| 326 | goto end; |
| 327 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 328 | ssl->state = SSL3_ST_SR_CHANGE; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 329 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 330 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 331 | case SSL3_ST_SR_CHANGE: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 332 | ret = ssl->method->ssl_read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 333 | if (ret <= 0) { |
| 334 | goto end; |
| 335 | } |
| 336 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 337 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) { |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 338 | ret = -1; |
| 339 | goto end; |
| 340 | } |
| 341 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 342 | ssl->state = SSL3_ST_SR_FINISHED_A; |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 343 | break; |
| 344 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 345 | case SSL3_ST_SR_FINISHED_A: |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 346 | ret = ssl3_get_finished(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 347 | if (ret <= 0) { |
| 348 | goto end; |
| 349 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 350 | dtls1_stop_timer(ssl); |
| 351 | if (ssl->hit) { |
| 352 | ssl->state = SSL_ST_OK; |
| 353 | } else if (ssl->tlsext_ticket_expected) { |
| 354 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 355 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 356 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 357 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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_SW_SESSION_TICKET_A: |
| 361 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 362 | ret = ssl3_send_new_session_ticket(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 363 | if (ret <= 0) { |
| 364 | goto end; |
| 365 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 366 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 367 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 368 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 369 | case SSL3_ST_SW_CHANGE_A: |
| 370 | case SSL3_ST_SW_CHANGE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 371 | ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A, |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 372 | SSL3_ST_SW_CHANGE_B); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 374 | if (ret <= 0) { |
| 375 | goto end; |
| 376 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 378 | ssl->state = SSL3_ST_SW_FINISHED_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 380 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 381 | ret = -1; |
| 382 | goto end; |
| 383 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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_SW_FINISHED_A: |
| 387 | case SSL3_ST_SW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 388 | ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 389 | SSL3_ST_SW_FINISHED_B); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 390 | if (ret <= 0) { |
| 391 | goto end; |
| 392 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 393 | ssl->state = SSL3_ST_SW_FLUSH; |
| 394 | if (ssl->hit) { |
| 395 | ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 396 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 397 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 398 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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 SSL_ST_OK: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 402 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 404 | /* remove buffering on output */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 405 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | |
David Benjamin | fce37b0 | 2016-05-15 13:51:35 -0400 | [diff] [blame] | 407 | /* |init_buf| cannot be released because post-handshake retransmit |
| 408 | * relies on that buffer being available as scratch space. |
| 409 | * |
| 410 | * TODO(davidben): Fix this. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 411 | ssl->init_num = 0; |
| 412 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 413 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 414 | ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | |
David Benjamin | d23d5a5 | 2015-05-15 21:48:48 -0400 | [diff] [blame] | 416 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 417 | cb(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 418 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 419 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 420 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 422 | /* done handshaking, next message is client hello */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 423 | ssl->d1->handshake_read_seq = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 424 | /* next message is server hello */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 425 | ssl->d1->handshake_write_seq = 0; |
| 426 | ssl->d1->next_handshake_write_seq = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 427 | goto end; |
| 428 | |
| 429 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 430 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 431 | ret = -1; |
| 432 | goto end; |
| 433 | } |
| 434 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 435 | if (!ssl->s3->tmp.reuse_message && !skip) { |
| 436 | if (cb != NULL && ssl->state != state) { |
| 437 | new_state = ssl->state; |
| 438 | ssl->state = state; |
| 439 | cb(ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 440 | ssl->state = new_state; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | skip = 0; |
| 444 | } |
| 445 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 446 | end: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 447 | BUF_MEM_free(buf); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 448 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 449 | cb(ssl, SSL_CB_ACCEPT_EXIT, ret); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 450 | } |
| 451 | return ret; |
| 452 | } |