diff options
Diffstat (limited to 'src/lib/libssl/d1_srvr.c')
| -rw-r--r-- | src/lib/libssl/d1_srvr.c | 751 |
1 files changed, 0 insertions, 751 deletions
diff --git a/src/lib/libssl/d1_srvr.c b/src/lib/libssl/d1_srvr.c deleted file mode 100644 index f6664237ae..0000000000 --- a/src/lib/libssl/d1_srvr.c +++ /dev/null | |||
| @@ -1,751 +0,0 @@ | |||
| 1 | /* $OpenBSD: d1_srvr.c,v 1.67 2015/09/13 09:20:19 jsing Exp $ */ | ||
| 2 | /* | ||
| 3 | * DTLS implementation written by Nagendra Modadugu | ||
| 4 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. | ||
| 5 | */ | ||
| 6 | /* ==================================================================== | ||
| 7 | * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. | ||
| 8 | * | ||
| 9 | * Redistribution and use in source and binary forms, with or without | ||
| 10 | * modification, are permitted provided that the following conditions | ||
| 11 | * are met: | ||
| 12 | * | ||
| 13 | * 1. Redistributions of source code must retain the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer. | ||
| 15 | * | ||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 17 | * notice, this list of conditions and the following disclaimer in | ||
| 18 | * the documentation and/or other materials provided with the | ||
| 19 | * distribution. | ||
| 20 | * | ||
| 21 | * 3. All advertising materials mentioning features or use of this | ||
| 22 | * software must display the following acknowledgment: | ||
| 23 | * "This product includes software developed by the OpenSSL Project | ||
| 24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 25 | * | ||
| 26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 27 | * endorse or promote products derived from this software without | ||
| 28 | * prior written permission. For written permission, please contact | ||
| 29 | * openssl-core@OpenSSL.org. | ||
| 30 | * | ||
| 31 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 32 | * nor may "OpenSSL" appear in their names without prior written | ||
| 33 | * permission of the OpenSSL Project. | ||
| 34 | * | ||
| 35 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 36 | * acknowledgment: | ||
| 37 | * "This product includes software developed by the OpenSSL Project | ||
| 38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 39 | * | ||
| 40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 52 | * ==================================================================== | ||
| 53 | * | ||
| 54 | * This product includes cryptographic software written by Eric Young | ||
| 55 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 56 | * Hudson (tjh@cryptsoft.com). | ||
| 57 | * | ||
| 58 | */ | ||
| 59 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 60 | * All rights reserved. | ||
| 61 | * | ||
| 62 | * This package is an SSL implementation written | ||
| 63 | * by Eric Young (eay@cryptsoft.com). | ||
| 64 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 65 | * | ||
| 66 | * This library is free for commercial and non-commercial use as long as | ||
| 67 | * the following conditions are aheared to. The following conditions | ||
| 68 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 69 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 70 | * included with this distribution is covered by the same copyright terms | ||
| 71 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 72 | * | ||
| 73 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 74 | * the code are not to be removed. | ||
| 75 | * If this package is used in a product, Eric Young should be given attribution | ||
| 76 | * as the author of the parts of the library used. | ||
| 77 | * This can be in the form of a textual message at program startup or | ||
| 78 | * in documentation (online or textual) provided with the package. | ||
| 79 | * | ||
| 80 | * Redistribution and use in source and binary forms, with or without | ||
| 81 | * modification, are permitted provided that the following conditions | ||
| 82 | * are met: | ||
| 83 | * 1. Redistributions of source code must retain the copyright | ||
| 84 | * notice, this list of conditions and the following disclaimer. | ||
| 85 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 86 | * notice, this list of conditions and the following disclaimer in the | ||
| 87 | * documentation and/or other materials provided with the distribution. | ||
| 88 | * 3. All advertising materials mentioning features or use of this software | ||
| 89 | * must display the following acknowledgement: | ||
| 90 | * "This product includes cryptographic software written by | ||
| 91 | * Eric Young (eay@cryptsoft.com)" | ||
| 92 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 93 | * being used are not cryptographic related :-). | ||
| 94 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 95 | * the apps directory (application code) you must include an acknowledgement: | ||
| 96 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 97 | * | ||
| 98 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 99 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 100 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 101 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 102 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 103 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 104 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 105 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 106 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 107 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 108 | * SUCH DAMAGE. | ||
| 109 | * | ||
| 110 | * The licence and distribution terms for any publically available version or | ||
| 111 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 112 | * copied and put under another distribution licence | ||
| 113 | * [including the GNU Public Licence.] | ||
| 114 | */ | ||
| 115 | |||
| 116 | #include <stdio.h> | ||
| 117 | |||
| 118 | #include "ssl_locl.h" | ||
| 119 | |||
| 120 | #include <openssl/bn.h> | ||
| 121 | #include <openssl/buffer.h> | ||
| 122 | #include <openssl/dh.h> | ||
| 123 | #include <openssl/evp.h> | ||
| 124 | #include <openssl/md5.h> | ||
| 125 | #include <openssl/objects.h> | ||
| 126 | #include <openssl/x509.h> | ||
| 127 | |||
| 128 | static const SSL_METHOD *dtls1_get_server_method(int ver); | ||
| 129 | static int dtls1_send_hello_verify_request(SSL *s); | ||
| 130 | |||
| 131 | const SSL_METHOD DTLSv1_server_method_data = { | ||
| 132 | .version = DTLS1_VERSION, | ||
| 133 | .ssl_new = dtls1_new, | ||
| 134 | .ssl_clear = dtls1_clear, | ||
| 135 | .ssl_free = dtls1_free, | ||
| 136 | .ssl_accept = dtls1_accept, | ||
| 137 | .ssl_connect = ssl_undefined_function, | ||
| 138 | .ssl_read = ssl3_read, | ||
| 139 | .ssl_peek = ssl3_peek, | ||
| 140 | .ssl_write = ssl3_write, | ||
| 141 | .ssl_shutdown = dtls1_shutdown, | ||
| 142 | .ssl_renegotiate = ssl3_renegotiate, | ||
| 143 | .ssl_renegotiate_check = ssl3_renegotiate_check, | ||
| 144 | .ssl_get_message = dtls1_get_message, | ||
| 145 | .ssl_read_bytes = dtls1_read_bytes, | ||
| 146 | .ssl_write_bytes = dtls1_write_app_data_bytes, | ||
| 147 | .ssl_dispatch_alert = dtls1_dispatch_alert, | ||
| 148 | .ssl_ctrl = dtls1_ctrl, | ||
| 149 | .ssl_ctx_ctrl = ssl3_ctx_ctrl, | ||
| 150 | .get_cipher_by_char = ssl3_get_cipher_by_char, | ||
| 151 | .put_cipher_by_char = ssl3_put_cipher_by_char, | ||
| 152 | .ssl_pending = ssl3_pending, | ||
| 153 | .num_ciphers = ssl3_num_ciphers, | ||
| 154 | .get_cipher = dtls1_get_cipher, | ||
| 155 | .get_ssl_method = dtls1_get_server_method, | ||
| 156 | .get_timeout = dtls1_default_timeout, | ||
| 157 | .ssl3_enc = &DTLSv1_enc_data, | ||
| 158 | .ssl_version = ssl_undefined_void_function, | ||
| 159 | .ssl_callback_ctrl = ssl3_callback_ctrl, | ||
| 160 | .ssl_ctx_callback_ctrl = ssl3_ctx_callback_ctrl, | ||
| 161 | }; | ||
| 162 | |||
| 163 | const SSL_METHOD * | ||
| 164 | DTLSv1_server_method(void) | ||
| 165 | { | ||
| 166 | return &DTLSv1_server_method_data; | ||
| 167 | } | ||
| 168 | |||
| 169 | static const SSL_METHOD * | ||
| 170 | dtls1_get_server_method(int ver) | ||
| 171 | { | ||
| 172 | if (ver == DTLS1_VERSION) | ||
| 173 | return (DTLSv1_server_method()); | ||
| 174 | return (NULL); | ||
| 175 | } | ||
| 176 | |||
| 177 | int | ||
| 178 | dtls1_accept(SSL *s) | ||
| 179 | { | ||
| 180 | void (*cb)(const SSL *ssl, int type, int val) = NULL; | ||
| 181 | unsigned long alg_k; | ||
| 182 | int ret = -1; | ||
| 183 | int new_state, state, skip = 0; | ||
| 184 | int listen; | ||
| 185 | |||
| 186 | ERR_clear_error(); | ||
| 187 | errno = 0; | ||
| 188 | |||
| 189 | if (s->info_callback != NULL) | ||
| 190 | cb = s->info_callback; | ||
| 191 | else if (s->ctx->info_callback != NULL) | ||
| 192 | cb = s->ctx->info_callback; | ||
| 193 | |||
| 194 | listen = s->d1->listen; | ||
| 195 | |||
| 196 | /* init things to blank */ | ||
| 197 | s->in_handshake++; | ||
| 198 | if (!SSL_in_init(s) || SSL_in_before(s)) | ||
| 199 | SSL_clear(s); | ||
| 200 | |||
| 201 | s->d1->listen = listen; | ||
| 202 | |||
| 203 | if (s->cert == NULL) { | ||
| 204 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_NO_CERTIFICATE_SET); | ||
| 205 | ret = -1; | ||
| 206 | goto end; | ||
| 207 | } | ||
| 208 | |||
| 209 | for (;;) { | ||
| 210 | state = s->state; | ||
| 211 | |||
| 212 | switch (s->state) { | ||
| 213 | case SSL_ST_RENEGOTIATE: | ||
| 214 | s->renegotiate = 1; | ||
| 215 | /* s->state=SSL_ST_ACCEPT; */ | ||
| 216 | |||
| 217 | case SSL_ST_BEFORE: | ||
| 218 | case SSL_ST_ACCEPT: | ||
| 219 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: | ||
| 220 | case SSL_ST_OK|SSL_ST_ACCEPT: | ||
| 221 | |||
| 222 | s->server = 1; | ||
| 223 | if (cb != NULL) | ||
| 224 | cb(s, SSL_CB_HANDSHAKE_START, 1); | ||
| 225 | |||
| 226 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { | ||
| 227 | SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR); | ||
| 228 | ret = -1; | ||
| 229 | goto end; | ||
| 230 | } | ||
| 231 | s->type = SSL_ST_ACCEPT; | ||
| 232 | |||
| 233 | if (!ssl3_setup_init_buffer(s)) { | ||
| 234 | ret = -1; | ||
| 235 | goto end; | ||
| 236 | } | ||
| 237 | if (!ssl3_setup_buffers(s)) { | ||
| 238 | ret = -1; | ||
| 239 | goto end; | ||
| 240 | } | ||
| 241 | |||
| 242 | s->init_num = 0; | ||
| 243 | |||
| 244 | if (s->state != SSL_ST_RENEGOTIATE) { | ||
| 245 | /* Ok, we now need to push on a buffering BIO so that | ||
| 246 | * the output is sent in a way that TCP likes :-) | ||
| 247 | * ...but not with SCTP :-) | ||
| 248 | */ | ||
| 249 | if (!ssl_init_wbio_buffer(s, 1)) { | ||
| 250 | ret = -1; | ||
| 251 | goto end; | ||
| 252 | } | ||
| 253 | |||
| 254 | if (!tls1_init_finished_mac(s)) { | ||
| 255 | ret = -1; | ||
| 256 | goto end; | ||
| 257 | } | ||
| 258 | |||
| 259 | s->state = SSL3_ST_SR_CLNT_HELLO_A; | ||
| 260 | s->ctx->stats.sess_accept++; | ||
| 261 | } else { | ||
| 262 | /* s->state == SSL_ST_RENEGOTIATE, | ||
| 263 | * we will just send a HelloRequest */ | ||
| 264 | s->ctx->stats.sess_accept_renegotiate++; | ||
| 265 | s->state = SSL3_ST_SW_HELLO_REQ_A; | ||
| 266 | } | ||
| 267 | |||
| 268 | break; | ||
| 269 | |||
| 270 | case SSL3_ST_SW_HELLO_REQ_A: | ||
| 271 | case SSL3_ST_SW_HELLO_REQ_B: | ||
| 272 | |||
| 273 | s->shutdown = 0; | ||
| 274 | dtls1_clear_record_buffer(s); | ||
| 275 | dtls1_start_timer(s); | ||
| 276 | ret = ssl3_send_hello_request(s); | ||
| 277 | if (ret <= 0) | ||
| 278 | goto end; | ||
| 279 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | ||
| 280 | s->state = SSL3_ST_SW_FLUSH; | ||
| 281 | s->init_num = 0; | ||
| 282 | |||
| 283 | if (!tls1_init_finished_mac(s)) { | ||
| 284 | ret = -1; | ||
| 285 | goto end; | ||
| 286 | } | ||
| 287 | break; | ||
| 288 | |||
| 289 | case SSL3_ST_SW_HELLO_REQ_C: | ||
| 290 | s->state = SSL_ST_OK; | ||
| 291 | break; | ||
| 292 | |||
| 293 | case SSL3_ST_SR_CLNT_HELLO_A: | ||
| 294 | case SSL3_ST_SR_CLNT_HELLO_B: | ||
| 295 | case SSL3_ST_SR_CLNT_HELLO_C: | ||
| 296 | |||
| 297 | s->shutdown = 0; | ||
| 298 | ret = ssl3_get_client_hello(s); | ||
| 299 | if (ret <= 0) | ||
| 300 | goto end; | ||
| 301 | dtls1_stop_timer(s); | ||
| 302 | |||
| 303 | if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) | ||
| 304 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; | ||
| 305 | else | ||
| 306 | s->state = SSL3_ST_SW_SRVR_HELLO_A; | ||
| 307 | |||
| 308 | s->init_num = 0; | ||
| 309 | |||
| 310 | /* Reflect ClientHello sequence to remain stateless while listening */ | ||
| 311 | if (listen) { | ||
| 312 | memcpy(s->s3->write_sequence, s->s3->read_sequence, sizeof(s->s3->write_sequence)); | ||
| 313 | } | ||
| 314 | |||
| 315 | /* If we're just listening, stop here */ | ||
| 316 | if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) { | ||
| 317 | ret = 2; | ||
| 318 | s->d1->listen = 0; | ||
| 319 | /* Set expected sequence numbers | ||
| 320 | * to continue the handshake. | ||
| 321 | */ | ||
| 322 | s->d1->handshake_read_seq = 2; | ||
| 323 | s->d1->handshake_write_seq = 1; | ||
| 324 | s->d1->next_handshake_write_seq = 1; | ||
| 325 | goto end; | ||
| 326 | } | ||
| 327 | |||
| 328 | break; | ||
| 329 | |||
| 330 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: | ||
| 331 | case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: | ||
| 332 | |||
| 333 | ret = dtls1_send_hello_verify_request(s); | ||
| 334 | if (ret <= 0) | ||
| 335 | goto end; | ||
| 336 | s->state = SSL3_ST_SW_FLUSH; | ||
| 337 | s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A; | ||
| 338 | |||
| 339 | /* HelloVerifyRequest resets Finished MAC */ | ||
| 340 | if (!tls1_init_finished_mac(s)) { | ||
| 341 | ret = -1; | ||
| 342 | goto end; | ||
| 343 | } | ||
| 344 | break; | ||
| 345 | |||
| 346 | |||
| 347 | case SSL3_ST_SW_SRVR_HELLO_A: | ||
| 348 | case SSL3_ST_SW_SRVR_HELLO_B: | ||
| 349 | s->renegotiate = 2; | ||
| 350 | dtls1_start_timer(s); | ||
| 351 | ret = ssl3_send_server_hello(s); | ||
| 352 | if (ret <= 0) | ||
| 353 | goto end; | ||
| 354 | |||
| 355 | if (s->hit) { | ||
| 356 | if (s->tlsext_ticket_expected) | ||
| 357 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
| 358 | else | ||
| 359 | s->state = SSL3_ST_SW_CHANGE_A; | ||
| 360 | } else | ||
| 361 | s->state = SSL3_ST_SW_CERT_A; | ||
| 362 | s->init_num = 0; | ||
| 363 | break; | ||
| 364 | |||
| 365 | case SSL3_ST_SW_CERT_A: | ||
| 366 | case SSL3_ST_SW_CERT_B: | ||
| 367 | /* Check if it is anon DH. */ | ||
| 368 | if (!(s->s3->tmp.new_cipher->algorithm_auth & | ||
| 369 | SSL_aNULL)) { | ||
| 370 | dtls1_start_timer(s); | ||
| 371 | ret = dtls1_send_server_certificate(s); | ||
| 372 | if (ret <= 0) | ||
| 373 | goto end; | ||
| 374 | if (s->tlsext_status_expected) | ||
| 375 | s->state = SSL3_ST_SW_CERT_STATUS_A; | ||
| 376 | else | ||
| 377 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
| 378 | } else { | ||
| 379 | skip = 1; | ||
| 380 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
| 381 | } | ||
| 382 | s->init_num = 0; | ||
| 383 | break; | ||
| 384 | |||
| 385 | case SSL3_ST_SW_KEY_EXCH_A: | ||
| 386 | case SSL3_ST_SW_KEY_EXCH_B: | ||
| 387 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
| 388 | |||
| 389 | /* Only send if using a DH key exchange. */ | ||
| 390 | if (alg_k & (SSL_kDHE|SSL_kECDHE)) { | ||
| 391 | dtls1_start_timer(s); | ||
| 392 | ret = ssl3_send_server_key_exchange(s); | ||
| 393 | if (ret <= 0) | ||
| 394 | goto end; | ||
| 395 | } else | ||
| 396 | skip = 1; | ||
| 397 | |||
| 398 | s->state = SSL3_ST_SW_CERT_REQ_A; | ||
| 399 | s->init_num = 0; | ||
| 400 | break; | ||
| 401 | |||
| 402 | case SSL3_ST_SW_CERT_REQ_A: | ||
| 403 | case SSL3_ST_SW_CERT_REQ_B: | ||
| 404 | /* | ||
| 405 | * Determine whether or not we need to request a | ||
| 406 | * certificate. | ||
| 407 | * | ||
| 408 | * Do not request a certificate if: | ||
| 409 | * | ||
| 410 | * - We did not ask for it (SSL_VERIFY_PEER is unset). | ||
| 411 | * | ||
| 412 | * - SSL_VERIFY_CLIENT_ONCE is set and we are | ||
| 413 | * renegotiating. | ||
| 414 | * | ||
| 415 | * - We are using an anonymous ciphersuites | ||
| 416 | * (see section "Certificate request" in SSL 3 drafts | ||
| 417 | * and in RFC 2246) ... except when the application | ||
| 418 | * insists on verification (against the specs, but | ||
| 419 | * s3_clnt.c accepts this for SSL 3). | ||
| 420 | */ | ||
| 421 | if (!(s->verify_mode & SSL_VERIFY_PEER) || | ||
| 422 | ((s->session->peer != NULL) && | ||
| 423 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | ||
| 424 | ((s->s3->tmp.new_cipher->algorithm_auth & | ||
| 425 | SSL_aNULL) && !(s->verify_mode & | ||
| 426 | SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { | ||
| 427 | /* no cert request */ | ||
| 428 | skip = 1; | ||
| 429 | s->s3->tmp.cert_request = 0; | ||
| 430 | s->state = SSL3_ST_SW_SRVR_DONE_A; | ||
| 431 | } else { | ||
| 432 | s->s3->tmp.cert_request = 1; | ||
| 433 | dtls1_start_timer(s); | ||
| 434 | ret = ssl3_send_certificate_request(s); | ||
| 435 | if (ret <= 0) | ||
| 436 | goto end; | ||
| 437 | s->state = SSL3_ST_SW_SRVR_DONE_A; | ||
| 438 | s->init_num = 0; | ||
| 439 | } | ||
| 440 | break; | ||
| 441 | |||
| 442 | case SSL3_ST_SW_SRVR_DONE_A: | ||
| 443 | case SSL3_ST_SW_SRVR_DONE_B: | ||
| 444 | dtls1_start_timer(s); | ||
| 445 | ret = ssl3_send_server_done(s); | ||
| 446 | if (ret <= 0) | ||
| 447 | goto end; | ||
| 448 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; | ||
| 449 | s->state = SSL3_ST_SW_FLUSH; | ||
| 450 | s->init_num = 0; | ||
| 451 | break; | ||
| 452 | |||
| 453 | case SSL3_ST_SW_FLUSH: | ||
| 454 | s->rwstate = SSL_WRITING; | ||
| 455 | if (BIO_flush(s->wbio) <= 0) { | ||
| 456 | /* If the write error was fatal, stop trying */ | ||
| 457 | if (!BIO_should_retry(s->wbio)) { | ||
| 458 | s->rwstate = SSL_NOTHING; | ||
| 459 | s->state = s->s3->tmp.next_state; | ||
| 460 | } | ||
| 461 | |||
| 462 | ret = -1; | ||
| 463 | goto end; | ||
| 464 | } | ||
| 465 | s->rwstate = SSL_NOTHING; | ||
| 466 | s->state = s->s3->tmp.next_state; | ||
| 467 | break; | ||
| 468 | |||
| 469 | case SSL3_ST_SR_CERT_A: | ||
| 470 | case SSL3_ST_SR_CERT_B: | ||
| 471 | if (s->s3->tmp.cert_request) { | ||
| 472 | ret = ssl3_get_client_certificate(s); | ||
| 473 | if (ret <= 0) | ||
| 474 | goto end; | ||
| 475 | } | ||
| 476 | s->init_num = 0; | ||
| 477 | s->state = SSL3_ST_SR_KEY_EXCH_A; | ||
| 478 | break; | ||
| 479 | |||
| 480 | case SSL3_ST_SR_KEY_EXCH_A: | ||
| 481 | case SSL3_ST_SR_KEY_EXCH_B: | ||
| 482 | ret = ssl3_get_client_key_exchange(s); | ||
| 483 | if (ret <= 0) | ||
| 484 | goto end; | ||
| 485 | |||
| 486 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
| 487 | s->init_num = 0; | ||
| 488 | |||
| 489 | if (ret == 2) { | ||
| 490 | /* For the ECDH ciphersuites when | ||
| 491 | * the client sends its ECDH pub key in | ||
| 492 | * a certificate, the CertificateVerify | ||
| 493 | * message is not sent. | ||
| 494 | */ | ||
| 495 | s->state = SSL3_ST_SR_FINISHED_A; | ||
| 496 | s->init_num = 0; | ||
| 497 | } else if (SSL_USE_SIGALGS(s)) { | ||
| 498 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
| 499 | s->init_num = 0; | ||
| 500 | if (!s->session->peer) | ||
| 501 | break; | ||
| 502 | |||
| 503 | /* | ||
| 504 | * For sigalgs freeze the handshake buffer | ||
| 505 | * at this point and digest cached records. | ||
| 506 | */ | ||
| 507 | if (!s->s3->handshake_buffer) { | ||
| 508 | SSLerr(SSL_F_SSL3_ACCEPT, | ||
| 509 | ERR_R_INTERNAL_ERROR); | ||
| 510 | ret = -1; | ||
| 511 | goto end; | ||
| 512 | } | ||
| 513 | s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; | ||
| 514 | if (!tls1_digest_cached_records(s)) { | ||
| 515 | ret = -1; | ||
| 516 | goto end; | ||
| 517 | } | ||
| 518 | } else { | ||
| 519 | s->state = SSL3_ST_SR_CERT_VRFY_A; | ||
| 520 | s->init_num = 0; | ||
| 521 | |||
| 522 | /* We need to get hashes here so if there is | ||
| 523 | * a client cert, it can be verified */ | ||
| 524 | s->method->ssl3_enc->cert_verify_mac(s, | ||
| 525 | NID_md5, &(s->s3->tmp.cert_verify_md[0])); | ||
| 526 | s->method->ssl3_enc->cert_verify_mac(s, | ||
| 527 | NID_sha1, | ||
| 528 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); | ||
| 529 | } | ||
| 530 | break; | ||
| 531 | |||
| 532 | case SSL3_ST_SR_CERT_VRFY_A: | ||
| 533 | case SSL3_ST_SR_CERT_VRFY_B: | ||
| 534 | |||
| 535 | s->d1->change_cipher_spec_ok = 1; | ||
| 536 | /* we should decide if we expected this one */ | ||
| 537 | ret = ssl3_get_cert_verify(s); | ||
| 538 | if (ret <= 0) | ||
| 539 | goto end; | ||
| 540 | s->state = SSL3_ST_SR_FINISHED_A; | ||
| 541 | s->init_num = 0; | ||
| 542 | break; | ||
| 543 | |||
| 544 | case SSL3_ST_SR_FINISHED_A: | ||
| 545 | case SSL3_ST_SR_FINISHED_B: | ||
| 546 | s->d1->change_cipher_spec_ok = 1; | ||
| 547 | ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, | ||
| 548 | SSL3_ST_SR_FINISHED_B); | ||
| 549 | if (ret <= 0) | ||
| 550 | goto end; | ||
| 551 | dtls1_stop_timer(s); | ||
| 552 | if (s->hit) | ||
| 553 | s->state = SSL_ST_OK; | ||
| 554 | else if (s->tlsext_ticket_expected) | ||
| 555 | s->state = SSL3_ST_SW_SESSION_TICKET_A; | ||
| 556 | else | ||
| 557 | s->state = SSL3_ST_SW_CHANGE_A; | ||
| 558 | s->init_num = 0; | ||
| 559 | break; | ||
| 560 | |||
| 561 | case SSL3_ST_SW_SESSION_TICKET_A: | ||
| 562 | case SSL3_ST_SW_SESSION_TICKET_B: | ||
| 563 | ret = ssl3_send_newsession_ticket(s); | ||
| 564 | if (ret <= 0) | ||
| 565 | goto end; | ||
| 566 | s->state = SSL3_ST_SW_CHANGE_A; | ||
| 567 | s->init_num = 0; | ||
| 568 | break; | ||
| 569 | |||
| 570 | case SSL3_ST_SW_CERT_STATUS_A: | ||
| 571 | case SSL3_ST_SW_CERT_STATUS_B: | ||
| 572 | ret = ssl3_send_cert_status(s); | ||
| 573 | if (ret <= 0) | ||
| 574 | goto end; | ||
| 575 | s->state = SSL3_ST_SW_KEY_EXCH_A; | ||
| 576 | s->init_num = 0; | ||
| 577 | break; | ||
| 578 | |||
| 579 | |||
| 580 | case SSL3_ST_SW_CHANGE_A: | ||
| 581 | case SSL3_ST_SW_CHANGE_B: | ||
| 582 | |||
| 583 | s->session->cipher = s->s3->tmp.new_cipher; | ||
| 584 | if (!s->method->ssl3_enc->setup_key_block(s)) { | ||
| 585 | ret = -1; | ||
| 586 | goto end; | ||
| 587 | } | ||
| 588 | |||
| 589 | ret = dtls1_send_change_cipher_spec(s, | ||
| 590 | SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B); | ||
| 591 | |||
| 592 | if (ret <= 0) | ||
| 593 | goto end; | ||
| 594 | |||
| 595 | |||
| 596 | s->state = SSL3_ST_SW_FINISHED_A; | ||
| 597 | s->init_num = 0; | ||
| 598 | |||
| 599 | if (!s->method->ssl3_enc->change_cipher_state(s, | ||
| 600 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { | ||
| 601 | ret = -1; | ||
| 602 | goto end; | ||
| 603 | } | ||
| 604 | |||
| 605 | dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); | ||
| 606 | break; | ||
| 607 | |||
| 608 | case SSL3_ST_SW_FINISHED_A: | ||
| 609 | case SSL3_ST_SW_FINISHED_B: | ||
| 610 | ret = ssl3_send_finished(s, | ||
| 611 | SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, | ||
| 612 | s->method->ssl3_enc->server_finished_label, | ||
| 613 | s->method->ssl3_enc->server_finished_label_len); | ||
| 614 | if (ret <= 0) | ||
| 615 | goto end; | ||
| 616 | s->state = SSL3_ST_SW_FLUSH; | ||
| 617 | if (s->hit) { | ||
| 618 | s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A; | ||
| 619 | |||
| 620 | } else { | ||
| 621 | s->s3->tmp.next_state = SSL_ST_OK; | ||
| 622 | } | ||
| 623 | s->init_num = 0; | ||
| 624 | break; | ||
| 625 | |||
| 626 | case SSL_ST_OK: | ||
| 627 | /* clean a few things up */ | ||
| 628 | tls1_cleanup_key_block(s); | ||
| 629 | |||
| 630 | /* remove buffering on output */ | ||
| 631 | ssl_free_wbio_buffer(s); | ||
| 632 | |||
| 633 | s->init_num = 0; | ||
| 634 | |||
| 635 | if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ | ||
| 636 | { | ||
| 637 | s->renegotiate = 0; | ||
| 638 | s->new_session = 0; | ||
| 639 | |||
| 640 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); | ||
| 641 | |||
| 642 | s->ctx->stats.sess_accept_good++; | ||
| 643 | /* s->server=1; */ | ||
| 644 | s->handshake_func = dtls1_accept; | ||
| 645 | |||
| 646 | if (cb != NULL) | ||
| 647 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); | ||
| 648 | } | ||
| 649 | |||
| 650 | ret = 1; | ||
| 651 | |||
| 652 | /* done handshaking, next message is client hello */ | ||
| 653 | s->d1->handshake_read_seq = 0; | ||
| 654 | /* next message is server hello */ | ||
| 655 | s->d1->handshake_write_seq = 0; | ||
| 656 | s->d1->next_handshake_write_seq = 0; | ||
| 657 | goto end; | ||
| 658 | /* break; */ | ||
| 659 | |||
| 660 | default: | ||
| 661 | SSLerr(SSL_F_DTLS1_ACCEPT, SSL_R_UNKNOWN_STATE); | ||
| 662 | ret = -1; | ||
| 663 | goto end; | ||
| 664 | /* break; */ | ||
| 665 | } | ||
| 666 | |||
| 667 | if (!s->s3->tmp.reuse_message && !skip) { | ||
| 668 | if (s->debug) { | ||
| 669 | if ((ret = BIO_flush(s->wbio)) <= 0) | ||
| 670 | goto end; | ||
| 671 | } | ||
| 672 | |||
| 673 | if ((cb != NULL) && (s->state != state)) { | ||
| 674 | new_state = s->state; | ||
| 675 | s->state = state; | ||
| 676 | cb(s, SSL_CB_ACCEPT_LOOP, 1); | ||
| 677 | s->state = new_state; | ||
| 678 | } | ||
| 679 | } | ||
| 680 | skip = 0; | ||
| 681 | } | ||
| 682 | end: | ||
| 683 | /* BIO_flush(s->wbio); */ | ||
| 684 | |||
| 685 | s->in_handshake--; | ||
| 686 | |||
| 687 | if (cb != NULL) | ||
| 688 | cb(s, SSL_CB_ACCEPT_EXIT, ret); | ||
| 689 | |||
| 690 | return (ret); | ||
| 691 | } | ||
| 692 | |||
| 693 | int | ||
| 694 | dtls1_send_hello_verify_request(SSL *s) | ||
| 695 | { | ||
| 696 | unsigned char *d, *p; | ||
| 697 | |||
| 698 | if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { | ||
| 699 | d = p = ssl3_handshake_msg_start(s, | ||
| 700 | DTLS1_MT_HELLO_VERIFY_REQUEST); | ||
| 701 | |||
| 702 | *(p++) = s->version >> 8; | ||
| 703 | *(p++) = s->version & 0xFF; | ||
| 704 | |||
| 705 | if (s->ctx->app_gen_cookie_cb == NULL || | ||
| 706 | s->ctx->app_gen_cookie_cb(s, s->d1->cookie, | ||
| 707 | &(s->d1->cookie_len)) == 0) { | ||
| 708 | SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST, | ||
| 709 | ERR_R_INTERNAL_ERROR); | ||
| 710 | return 0; | ||
| 711 | } | ||
| 712 | |||
| 713 | *(p++) = (unsigned char) s->d1->cookie_len; | ||
| 714 | memcpy(p, s->d1->cookie, s->d1->cookie_len); | ||
| 715 | p += s->d1->cookie_len; | ||
| 716 | |||
| 717 | ssl3_handshake_msg_finish(s, p - d); | ||
| 718 | |||
| 719 | s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; | ||
| 720 | } | ||
| 721 | |||
| 722 | /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ | ||
| 723 | return (ssl3_handshake_write(s)); | ||
| 724 | } | ||
| 725 | |||
| 726 | int | ||
| 727 | dtls1_send_server_certificate(SSL *s) | ||
| 728 | { | ||
| 729 | unsigned long l; | ||
| 730 | X509 *x; | ||
| 731 | |||
| 732 | if (s->state == SSL3_ST_SW_CERT_A) { | ||
| 733 | x = ssl_get_server_send_cert(s); | ||
| 734 | if (x == NULL) { | ||
| 735 | SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE, | ||
| 736 | ERR_R_INTERNAL_ERROR); | ||
| 737 | return (0); | ||
| 738 | } | ||
| 739 | |||
| 740 | l = dtls1_output_cert_chain(s, x); | ||
| 741 | s->state = SSL3_ST_SW_CERT_B; | ||
| 742 | s->init_num = (int)l; | ||
| 743 | s->init_off = 0; | ||
| 744 | |||
| 745 | /* buffer the message to handle re-xmits */ | ||
| 746 | dtls1_buffer_message(s, 0); | ||
| 747 | } | ||
| 748 | |||
| 749 | /* SSL3_ST_SW_CERT_B */ | ||
| 750 | return (dtls1_do_write(s, SSL3_RT_HANDSHAKE)); | ||
| 751 | } | ||
