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 | #include <stdio.h> |
| 119 | |
| 120 | #include <openssl/bn.h> |
| 121 | #include <openssl/buf.h> |
| 122 | #include <openssl/dh.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 123 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | #include <openssl/evp.h> |
| 125 | #include <openssl/md5.h> |
| 126 | #include <openssl/obj.h> |
| 127 | #include <openssl/rand.h> |
| 128 | #include <openssl/x509.h> |
| 129 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 130 | #include "internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 132 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 133 | int dtls1_accept(SSL *ssl) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 134 | BUF_MEM *buf = NULL; |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 135 | void (*cb)(const SSL *ssl, int type, int value) = NULL; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 136 | uint32_t alg_a; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 137 | int ret = -1; |
| 138 | int new_state, state, skip = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 140 | assert(ssl->handshake_func == dtls1_accept); |
| 141 | assert(ssl->server); |
| 142 | assert(SSL_IS_DTLS(ssl)); |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 143 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 144 | ERR_clear_error(); |
| 145 | ERR_clear_system_error(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 147 | if (ssl->info_callback != NULL) { |
| 148 | cb = ssl->info_callback; |
| 149 | } else if (ssl->ctx->info_callback != NULL) { |
| 150 | cb = ssl->ctx->info_callback; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 151 | } |
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 | ssl->in_handshake++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 155 | for (;;) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 156 | state = ssl->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 158 | switch (ssl->state) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 159 | case SSL_ST_ACCEPT: |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 160 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 161 | cb(ssl, SSL_CB_HANDSHAKE_START, 1); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 162 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 164 | if (ssl->init_buf == NULL) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 165 | buf = BUF_MEM_new(); |
| 166 | if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 167 | ret = -1; |
| 168 | goto end; |
| 169 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 170 | ssl->init_buf = buf; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 171 | buf = NULL; |
| 172 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 174 | ssl->init_num = 0; |
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 (!ssl_init_wbio_buffer(ssl, 1)) { |
David Benjamin | d23d5a5 | 2015-05-15 21:48:48 -0400 | [diff] [blame] | 177 | ret = -1; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 178 | goto end; |
| 179 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 180 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 181 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 182 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 183 | ret = -1; |
| 184 | goto end; |
| 185 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 187 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 188 | break; |
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 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 191 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 192 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 193 | case SSL3_ST_SR_CLNT_HELLO_D: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 194 | ssl->shutdown = 0; |
| 195 | ret = ssl3_get_client_hello(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 196 | if (ret <= 0) { |
| 197 | goto end; |
| 198 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 199 | dtls1_stop_timer(ssl); |
| 200 | ssl->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 201 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 202 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 204 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 205 | case SSL3_ST_SW_SRVR_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 206 | dtls1_start_timer(ssl); |
| 207 | ret = ssl3_send_server_hello(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 208 | if (ret <= 0) { |
| 209 | goto end; |
| 210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 212 | if (ssl->hit) { |
| 213 | if (ssl->tlsext_ticket_expected) { |
| 214 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 215 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 216 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 217 | } |
| 218 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 219 | ssl->state = SSL3_ST_SW_CERT_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 220 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 221 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 222 | break; |
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 | case SSL3_ST_SW_CERT_A: |
| 225 | case SSL3_ST_SW_CERT_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 226 | if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) { |
| 227 | dtls1_start_timer(ssl); |
| 228 | ret = ssl3_send_server_certificate(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 229 | if (ret <= 0) { |
| 230 | goto end; |
| 231 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 232 | if (ssl->s3->tmp.certificate_status_expected) { |
| 233 | ssl->state = SSL3_ST_SW_CERT_STATUS_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 234 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 235 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 236 | } |
| 237 | } else { |
| 238 | skip = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 239 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 240 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 241 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 242 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 244 | case SSL3_ST_SW_CERT_STATUS_A: |
| 245 | case SSL3_ST_SW_CERT_STATUS_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 246 | ret = ssl3_send_certificate_status(ssl); |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 247 | if (ret <= 0) { |
| 248 | goto end; |
| 249 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 250 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
| 251 | ssl->init_num = 0; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 252 | break; |
| 253 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 254 | case SSL3_ST_SW_KEY_EXCH_A: |
| 255 | case SSL3_ST_SW_KEY_EXCH_B: |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 256 | case SSL3_ST_SW_KEY_EXCH_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 257 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 258 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 259 | /* Send a ServerKeyExchange message if: |
| 260 | * - The key exchange is ephemeral or anonymous |
| 261 | * Diffie-Hellman. |
| 262 | * - There is a PSK identity hint. |
| 263 | * |
| 264 | * TODO(davidben): This logic is currently duplicated |
| 265 | * in s3_srvr.c. Fix this. In the meantime, keep them |
| 266 | * in sync. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 267 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) || |
| 268 | ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) { |
| 269 | dtls1_start_timer(ssl); |
| 270 | ret = ssl3_send_server_key_exchange(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 271 | if (ret <= 0) { |
| 272 | goto end; |
| 273 | } |
| 274 | } else { |
| 275 | skip = 1; |
| 276 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 278 | ssl->state = SSL3_ST_SW_CERT_REQ_A; |
| 279 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 280 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 282 | case SSL3_ST_SW_CERT_REQ_A: |
| 283 | case SSL3_ST_SW_CERT_REQ_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 284 | if (ssl->s3->tmp.cert_request) { |
| 285 | dtls1_start_timer(ssl); |
| 286 | ret = ssl3_send_certificate_request(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 287 | if (ret <= 0) { |
| 288 | goto end; |
| 289 | } |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 290 | } else { |
| 291 | skip = 1; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 292 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 293 | ssl->state = SSL3_ST_SW_SRVR_DONE_A; |
| 294 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 295 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 297 | case SSL3_ST_SW_SRVR_DONE_A: |
| 298 | case SSL3_ST_SW_SRVR_DONE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 299 | dtls1_start_timer(ssl); |
| 300 | ret = ssl3_send_server_done(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 301 | if (ret <= 0) { |
| 302 | goto end; |
| 303 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 304 | ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 305 | ssl->state = SSL3_ST_SW_FLUSH; |
| 306 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 307 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 309 | case SSL3_ST_SW_FLUSH: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 310 | ssl->rwstate = SSL_WRITING; |
| 311 | if (BIO_flush(ssl->wbio) <= 0) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 312 | ret = -1; |
| 313 | goto end; |
| 314 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 315 | ssl->rwstate = SSL_NOTHING; |
| 316 | ssl->state = ssl->s3->tmp.next_state; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 317 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 319 | case SSL3_ST_SR_CERT_A: |
| 320 | case SSL3_ST_SR_CERT_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 321 | if (ssl->s3->tmp.cert_request) { |
| 322 | ret = ssl3_get_client_certificate(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 323 | if (ret <= 0) { |
| 324 | goto end; |
| 325 | } |
| 326 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 327 | ssl->init_num = 0; |
| 328 | ssl->state = SSL3_ST_SR_KEY_EXCH_A; |
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 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 331 | case SSL3_ST_SR_KEY_EXCH_A: |
| 332 | case SSL3_ST_SR_KEY_EXCH_B: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 333 | case SSL3_ST_SR_KEY_EXCH_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 334 | ret = ssl3_get_client_key_exchange(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 335 | if (ret <= 0) { |
| 336 | goto end; |
| 337 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 338 | ssl->state = SSL3_ST_SR_CERT_VRFY_A; |
| 339 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 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_SR_CERT_VRFY_A: |
| 343 | case SSL3_ST_SR_CERT_VRFY_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 344 | ret = ssl3_get_cert_verify(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 345 | if (ret <= 0) { |
| 346 | goto end; |
| 347 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 348 | ssl->state = SSL3_ST_SR_CHANGE; |
| 349 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 350 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 352 | case SSL3_ST_SR_CHANGE: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 353 | ret = ssl->method->ssl_read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 354 | if (ret <= 0) { |
| 355 | goto end; |
| 356 | } |
| 357 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 358 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) { |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 359 | ret = -1; |
| 360 | goto end; |
| 361 | } |
| 362 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 363 | ssl->state = SSL3_ST_SR_FINISHED_A; |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 364 | break; |
| 365 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 366 | case SSL3_ST_SR_FINISHED_A: |
| 367 | case SSL3_ST_SR_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 368 | ret = ssl3_get_finished(ssl, SSL3_ST_SR_FINISHED_A, |
| 369 | SSL3_ST_SR_FINISHED_B); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 370 | if (ret <= 0) { |
| 371 | goto end; |
| 372 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 373 | dtls1_stop_timer(ssl); |
| 374 | if (ssl->hit) { |
| 375 | ssl->state = SSL_ST_OK; |
| 376 | } else if (ssl->tlsext_ticket_expected) { |
| 377 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 378 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 379 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 380 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 381 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 382 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 384 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 385 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 386 | ret = ssl3_send_new_session_ticket(ssl); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 387 | if (ret <= 0) { |
| 388 | goto end; |
| 389 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 390 | ssl->state = SSL3_ST_SW_CHANGE_A; |
| 391 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 392 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 394 | case SSL3_ST_SW_CHANGE_A: |
| 395 | case SSL3_ST_SW_CHANGE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 396 | ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A, |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 397 | SSL3_ST_SW_CHANGE_B); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 398 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 399 | if (ret <= 0) { |
| 400 | goto end; |
| 401 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 403 | ssl->state = SSL3_ST_SW_FINISHED_A; |
| 404 | ssl->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 406 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 407 | ret = -1; |
| 408 | goto end; |
| 409 | } |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 410 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 411 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 412 | case SSL3_ST_SW_FINISHED_A: |
| 413 | case SSL3_ST_SW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 414 | ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 415 | SSL3_ST_SW_FINISHED_B); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 416 | if (ret <= 0) { |
| 417 | goto end; |
| 418 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 419 | ssl->state = SSL3_ST_SW_FLUSH; |
| 420 | if (ssl->hit) { |
| 421 | ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 422 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 423 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 424 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 425 | ssl->init_num = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 426 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 427 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 428 | case SSL_ST_OK: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 429 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 431 | /* remove buffering on output */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 432 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 433 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 434 | ssl->init_num = 0; |
| 435 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 437 | ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 438 | |
David Benjamin | d23d5a5 | 2015-05-15 21:48:48 -0400 | [diff] [blame] | 439 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 440 | cb(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 441 | } |
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 | ret = 1; |
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 | /* done handshaking, next message is client hello */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 446 | ssl->d1->handshake_read_seq = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 447 | /* next message is server hello */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 448 | ssl->d1->handshake_write_seq = 0; |
| 449 | ssl->d1->next_handshake_write_seq = 0; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 450 | goto end; |
| 451 | |
| 452 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 453 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 454 | ret = -1; |
| 455 | goto end; |
| 456 | } |
| 457 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 458 | if (!ssl->s3->tmp.reuse_message && !skip) { |
| 459 | if (cb != NULL && ssl->state != state) { |
| 460 | new_state = ssl->state; |
| 461 | ssl->state = state; |
| 462 | cb(ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 463 | ssl->state = new_state; |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | skip = 0; |
| 467 | } |
| 468 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 469 | end: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 470 | ssl->in_handshake--; |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 471 | BUF_MEM_free(buf); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 472 | if (cb != NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 473 | cb(ssl, SSL_CB_ACCEPT_EXIT, ret); |
Adam Langley | 2481975 | 2014-12-15 18:42:07 -0800 | [diff] [blame] | 474 | } |
| 475 | return ret; |
| 476 | } |