1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libssl/src/d1_srvr.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1162 @@
1.4 +/* ssl/d1_srvr.c */
1.5 +/*
1.6 + * DTLS implementation written by Nagendra Modadugu
1.7 + * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
1.8 + */
1.9 +/* ====================================================================
1.10 + * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
1.11 + *
1.12 + * Redistribution and use in source and binary forms, with or without
1.13 + * modification, are permitted provided that the following conditions
1.14 + * are met:
1.15 + *
1.16 + * 1. Redistributions of source code must retain the above copyright
1.17 + * notice, this list of conditions and the following disclaimer.
1.18 + *
1.19 + * 2. Redistributions in binary form must reproduce the above copyright
1.20 + * notice, this list of conditions and the following disclaimer in
1.21 + * the documentation and/or other materials provided with the
1.22 + * distribution.
1.23 + *
1.24 + * 3. All advertising materials mentioning features or use of this
1.25 + * software must display the following acknowledgment:
1.26 + * "This product includes software developed by the OpenSSL Project
1.27 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.28 + *
1.29 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.30 + * endorse or promote products derived from this software without
1.31 + * prior written permission. For written permission, please contact
1.32 + * openssl-core@OpenSSL.org.
1.33 + *
1.34 + * 5. Products derived from this software may not be called "OpenSSL"
1.35 + * nor may "OpenSSL" appear in their names without prior written
1.36 + * permission of the OpenSSL Project.
1.37 + *
1.38 + * 6. Redistributions of any form whatsoever must retain the following
1.39 + * acknowledgment:
1.40 + * "This product includes software developed by the OpenSSL Project
1.41 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.42 + *
1.43 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.44 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.45 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.46 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.47 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.48 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.49 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.50 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.52 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.53 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.54 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.55 + * ====================================================================
1.56 + *
1.57 + * This product includes cryptographic software written by Eric Young
1.58 + * (eay@cryptsoft.com). This product includes software written by Tim
1.59 + * Hudson (tjh@cryptsoft.com).
1.60 + *
1.61 + */
1.62 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.63 + * All rights reserved.
1.64 + *
1.65 + * This package is an SSL implementation written
1.66 + * by Eric Young (eay@cryptsoft.com).
1.67 + * The implementation was written so as to conform with Netscapes SSL.
1.68 + *
1.69 + * This library is free for commercial and non-commercial use as long as
1.70 + * the following conditions are aheared to. The following conditions
1.71 + * apply to all code found in this distribution, be it the RC4, RSA,
1.72 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.73 + * included with this distribution is covered by the same copyright terms
1.74 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.75 + *
1.76 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.77 + * the code are not to be removed.
1.78 + * If this package is used in a product, Eric Young should be given attribution
1.79 + * as the author of the parts of the library used.
1.80 + * This can be in the form of a textual message at program startup or
1.81 + * in documentation (online or textual) provided with the package.
1.82 + *
1.83 + * Redistribution and use in source and binary forms, with or without
1.84 + * modification, are permitted provided that the following conditions
1.85 + * are met:
1.86 + * 1. Redistributions of source code must retain the copyright
1.87 + * notice, this list of conditions and the following disclaimer.
1.88 + * 2. Redistributions in binary form must reproduce the above copyright
1.89 + * notice, this list of conditions and the following disclaimer in the
1.90 + * documentation and/or other materials provided with the distribution.
1.91 + * 3. All advertising materials mentioning features or use of this software
1.92 + * must display the following acknowledgement:
1.93 + * "This product includes cryptographic software written by
1.94 + * Eric Young (eay@cryptsoft.com)"
1.95 + * The word 'cryptographic' can be left out if the rouines from the library
1.96 + * being used are not cryptographic related :-).
1.97 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.98 + * the apps directory (application code) you must include an acknowledgement:
1.99 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.100 + *
1.101 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.102 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.103 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.104 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.105 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.106 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.107 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.108 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.109 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.110 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.111 + * SUCH DAMAGE.
1.112 + *
1.113 + * The licence and distribution terms for any publically available version or
1.114 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.115 + * copied and put under another distribution licence
1.116 + * [including the GNU Public Licence.]
1.117 + */
1.118 +/*
1.119 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.120 + */
1.121 +
1.122 +#include <stdio.h>
1.123 +#include "ssl_locl.h"
1.124 +#include <openssl/buffer.h>
1.125 +#include <openssl/rand.h>
1.126 +#include <openssl/objects.h>
1.127 +#include <openssl/evp.h>
1.128 +#include <openssl/x509.h>
1.129 +#include <openssl/md5.h>
1.130 +#ifndef OPENSSL_NO_DH
1.131 +#include <openssl/dh.h>
1.132 +#endif
1.133 +
1.134 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.135 +#include "libssl_wsd.h"
1.136 +#endif
1.137 +
1.138 +#ifdef EMULATOR
1.139 +
1.140 + GET_STATIC_VAR_FROM_TLS(DTLSv1_server_method_data,d1_srvr,SSL_METHOD)
1.141 +
1.142 + #define DTLSv1_server_method_data (*GET_WSD_VAR_NAME(DTLSv1_server_method_data,d1_srvr,s)())
1.143 +
1.144 +#endif
1.145 +
1.146 +static SSL_METHOD *dtls1_get_server_method(int ver);
1.147 +static int dtls1_send_hello_verify_request(SSL *s);
1.148 +
1.149 +static SSL_METHOD *dtls1_get_server_method(int ver)
1.150 + {
1.151 + if (ver == DTLS1_VERSION)
1.152 + return(DTLSv1_server_method());
1.153 + else
1.154 + return(NULL);
1.155 + }
1.156 +
1.157 +EXPORT_C IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
1.158 + dtls1_accept,
1.159 + ssl_undefined_function,
1.160 + dtls1_get_server_method)
1.161 +
1.162 +int dtls1_accept(SSL *s)
1.163 + {
1.164 + BUF_MEM *buf;
1.165 + unsigned long l,Time=(unsigned long)time(NULL);
1.166 + void (*cb)(const SSL *ssl,int type,int val)=NULL;
1.167 + long num1;
1.168 + int ret= -1;
1.169 + int new_state,state,skip=0;
1.170 +
1.171 + RAND_add(&Time,sizeof(Time),0);
1.172 + ERR_clear_error();
1.173 + clear_sys_error();
1.174 +
1.175 + if (s->info_callback != NULL)
1.176 + cb=s->info_callback;
1.177 + else if (s->ctx->info_callback != NULL)
1.178 + cb=s->ctx->info_callback;
1.179 +
1.180 + /* init things to blank */
1.181 + s->in_handshake++;
1.182 + if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
1.183 +
1.184 + if (s->cert == NULL)
1.185 + {
1.186 + SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
1.187 + return(-1);
1.188 + }
1.189 +
1.190 + for (;;)
1.191 + {
1.192 + state=s->state;
1.193 +
1.194 + switch (s->state)
1.195 + {
1.196 + case SSL_ST_RENEGOTIATE:
1.197 + s->new_session=1;
1.198 + /* s->state=SSL_ST_ACCEPT; */
1.199 +
1.200 + case SSL_ST_BEFORE:
1.201 + case SSL_ST_ACCEPT:
1.202 + case SSL_ST_BEFORE|SSL_ST_ACCEPT:
1.203 + case SSL_ST_OK|SSL_ST_ACCEPT:
1.204 +
1.205 + s->server=1;
1.206 + if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
1.207 +
1.208 + if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))
1.209 + {
1.210 + SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
1.211 + return -1;
1.212 + }
1.213 + s->type=SSL_ST_ACCEPT;
1.214 +
1.215 + if (s->init_buf == NULL)
1.216 + {
1.217 + if ((buf=BUF_MEM_new()) == NULL)
1.218 + {
1.219 + ret= -1;
1.220 + goto end;
1.221 + }
1.222 + if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
1.223 + {
1.224 + ret= -1;
1.225 + goto end;
1.226 + }
1.227 + s->init_buf=buf;
1.228 + }
1.229 +
1.230 + if (!ssl3_setup_buffers(s))
1.231 + {
1.232 + ret= -1;
1.233 + goto end;
1.234 + }
1.235 +
1.236 + s->init_num=0;
1.237 +
1.238 + if (s->state != SSL_ST_RENEGOTIATE)
1.239 + {
1.240 + /* Ok, we now need to push on a buffering BIO so that
1.241 + * the output is sent in a way that TCP likes :-)
1.242 + */
1.243 + if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
1.244 +
1.245 + ssl3_init_finished_mac(s);
1.246 + s->state=SSL3_ST_SR_CLNT_HELLO_A;
1.247 + s->ctx->stats.sess_accept++;
1.248 + }
1.249 + else
1.250 + {
1.251 + /* s->state == SSL_ST_RENEGOTIATE,
1.252 + * we will just send a HelloRequest */
1.253 + s->ctx->stats.sess_accept_renegotiate++;
1.254 + s->state=SSL3_ST_SW_HELLO_REQ_A;
1.255 + }
1.256 +
1.257 + if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
1.258 + s->d1->send_cookie = 1;
1.259 + else
1.260 + s->d1->send_cookie = 0;
1.261 +
1.262 + break;
1.263 +
1.264 + case SSL3_ST_SW_HELLO_REQ_A:
1.265 + case SSL3_ST_SW_HELLO_REQ_B:
1.266 +
1.267 + s->shutdown=0;
1.268 + ret=dtls1_send_hello_request(s);
1.269 + if (ret <= 0) goto end;
1.270 + s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
1.271 + s->state=SSL3_ST_SW_FLUSH;
1.272 + s->init_num=0;
1.273 +
1.274 + ssl3_init_finished_mac(s);
1.275 + break;
1.276 +
1.277 + case SSL3_ST_SW_HELLO_REQ_C:
1.278 + s->state=SSL_ST_OK;
1.279 + break;
1.280 +
1.281 + case SSL3_ST_SR_CLNT_HELLO_A:
1.282 + case SSL3_ST_SR_CLNT_HELLO_B:
1.283 + case SSL3_ST_SR_CLNT_HELLO_C:
1.284 +
1.285 + s->shutdown=0;
1.286 + ret=ssl3_get_client_hello(s);
1.287 + if (ret <= 0) goto end;
1.288 + s->new_session = 2;
1.289 +
1.290 + if ( s->d1->send_cookie)
1.291 + s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
1.292 + else
1.293 + s->state = SSL3_ST_SW_SRVR_HELLO_A;
1.294 +
1.295 + s->init_num=0;
1.296 + break;
1.297 +
1.298 + case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
1.299 + case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
1.300 +
1.301 + ret = dtls1_send_hello_verify_request(s);
1.302 + if ( ret <= 0) goto end;
1.303 + s->d1->send_cookie = 0;
1.304 + s->state=SSL3_ST_SW_FLUSH;
1.305 + s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
1.306 +
1.307 + /* HelloVerifyRequests resets Finished MAC */
1.308 + if (s->client_version != DTLS1_BAD_VER)
1.309 + ssl3_init_finished_mac(s);
1.310 + break;
1.311 +
1.312 + case SSL3_ST_SW_SRVR_HELLO_A:
1.313 + case SSL3_ST_SW_SRVR_HELLO_B:
1.314 + ret=dtls1_send_server_hello(s);
1.315 + if (ret <= 0) goto end;
1.316 +
1.317 + if (s->hit)
1.318 + s->state=SSL3_ST_SW_CHANGE_A;
1.319 + else
1.320 + s->state=SSL3_ST_SW_CERT_A;
1.321 + s->init_num=0;
1.322 + break;
1.323 +
1.324 + case SSL3_ST_SW_CERT_A:
1.325 + case SSL3_ST_SW_CERT_B:
1.326 + /* Check if it is anon DH */
1.327 + if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1.328 + {
1.329 + ret=dtls1_send_server_certificate(s);
1.330 + if (ret <= 0) goto end;
1.331 + }
1.332 + else
1.333 + skip=1;
1.334 + s->state=SSL3_ST_SW_KEY_EXCH_A;
1.335 + s->init_num=0;
1.336 + break;
1.337 +
1.338 + case SSL3_ST_SW_KEY_EXCH_A:
1.339 + case SSL3_ST_SW_KEY_EXCH_B:
1.340 + l=s->s3->tmp.new_cipher->algorithms;
1.341 +
1.342 + /* clear this, it may get reset by
1.343 + * send_server_key_exchange */
1.344 + if ((s->options & SSL_OP_EPHEMERAL_RSA)
1.345 +#ifndef OPENSSL_NO_KRB5
1.346 + && !(l & SSL_KRB5)
1.347 +#endif /* OPENSSL_NO_KRB5 */
1.348 + )
1.349 + /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
1.350 + * even when forbidden by protocol specs
1.351 + * (handshake may fail as clients are not required to
1.352 + * be able to handle this) */
1.353 + s->s3->tmp.use_rsa_tmp=1;
1.354 + else
1.355 + s->s3->tmp.use_rsa_tmp=0;
1.356 +
1.357 + /* only send if a DH key exchange, fortezza or
1.358 + * RSA but we have a sign only certificate */
1.359 + if (s->s3->tmp.use_rsa_tmp
1.360 + || (l & (SSL_DH|SSL_kFZA))
1.361 + || ((l & SSL_kRSA)
1.362 + && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
1.363 + || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
1.364 + && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
1.365 + )
1.366 + )
1.367 + )
1.368 + )
1.369 + {
1.370 + ret=dtls1_send_server_key_exchange(s);
1.371 + if (ret <= 0) goto end;
1.372 + }
1.373 + else
1.374 + skip=1;
1.375 +
1.376 + s->state=SSL3_ST_SW_CERT_REQ_A;
1.377 + s->init_num=0;
1.378 + break;
1.379 +
1.380 + case SSL3_ST_SW_CERT_REQ_A:
1.381 + case SSL3_ST_SW_CERT_REQ_B:
1.382 + if (/* don't request cert unless asked for it: */
1.383 + !(s->verify_mode & SSL_VERIFY_PEER) ||
1.384 + /* if SSL_VERIFY_CLIENT_ONCE is set,
1.385 + * don't request cert during re-negotiation: */
1.386 + ((s->session->peer != NULL) &&
1.387 + (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
1.388 + /* never request cert in anonymous ciphersuites
1.389 + * (see section "Certificate request" in SSL 3 drafts
1.390 + * and in RFC 2246): */
1.391 + ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
1.392 + /* ... except when the application insists on verification
1.393 + * (against the specs, but s3_clnt.c accepts this for SSL 3) */
1.394 + !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
1.395 + /* never request cert in Kerberos ciphersuites */
1.396 + (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
1.397 + {
1.398 + /* no cert request */
1.399 + skip=1;
1.400 + s->s3->tmp.cert_request=0;
1.401 + s->state=SSL3_ST_SW_SRVR_DONE_A;
1.402 + }
1.403 + else
1.404 + {
1.405 + s->s3->tmp.cert_request=1;
1.406 + ret=dtls1_send_certificate_request(s);
1.407 + if (ret <= 0) goto end;
1.408 +#ifndef NETSCAPE_HANG_BUG
1.409 + s->state=SSL3_ST_SW_SRVR_DONE_A;
1.410 +#else
1.411 + s->state=SSL3_ST_SW_FLUSH;
1.412 + s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
1.413 +#endif
1.414 + s->init_num=0;
1.415 + }
1.416 + break;
1.417 +
1.418 + case SSL3_ST_SW_SRVR_DONE_A:
1.419 + case SSL3_ST_SW_SRVR_DONE_B:
1.420 + ret=dtls1_send_server_done(s);
1.421 + if (ret <= 0) goto end;
1.422 + s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
1.423 + s->state=SSL3_ST_SW_FLUSH;
1.424 + s->init_num=0;
1.425 + break;
1.426 +
1.427 + case SSL3_ST_SW_FLUSH:
1.428 + /* number of bytes to be flushed */
1.429 + num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
1.430 + if (num1 > 0)
1.431 + {
1.432 + s->rwstate=SSL_WRITING;
1.433 + num1=BIO_flush(s->wbio);
1.434 + if (num1 <= 0) { ret= -1; goto end; }
1.435 + s->rwstate=SSL_NOTHING;
1.436 + }
1.437 +
1.438 + s->state=s->s3->tmp.next_state;
1.439 + break;
1.440 +
1.441 + case SSL3_ST_SR_CERT_A:
1.442 + case SSL3_ST_SR_CERT_B:
1.443 + /* Check for second client hello (MS SGC) */
1.444 + ret = ssl3_check_client_hello(s);
1.445 + if (ret <= 0)
1.446 + goto end;
1.447 + if (ret == 2)
1.448 + s->state = SSL3_ST_SR_CLNT_HELLO_C;
1.449 + else {
1.450 + /* could be sent for a DH cert, even if we
1.451 + * have not asked for it :-) */
1.452 + ret=ssl3_get_client_certificate(s);
1.453 + if (ret <= 0) goto end;
1.454 + s->init_num=0;
1.455 + s->state=SSL3_ST_SR_KEY_EXCH_A;
1.456 + }
1.457 + break;
1.458 +
1.459 + case SSL3_ST_SR_KEY_EXCH_A:
1.460 + case SSL3_ST_SR_KEY_EXCH_B:
1.461 + ret=ssl3_get_client_key_exchange(s);
1.462 + if (ret <= 0) goto end;
1.463 + s->state=SSL3_ST_SR_CERT_VRFY_A;
1.464 + s->init_num=0;
1.465 +
1.466 + /* We need to get hashes here so if there is
1.467 + * a client cert, it can be verified */
1.468 + s->method->ssl3_enc->cert_verify_mac(s,
1.469 + &(s->s3->finish_dgst1),
1.470 + &(s->s3->tmp.cert_verify_md[0]));
1.471 + s->method->ssl3_enc->cert_verify_mac(s,
1.472 + &(s->s3->finish_dgst2),
1.473 + &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
1.474 +
1.475 + break;
1.476 +
1.477 + case SSL3_ST_SR_CERT_VRFY_A:
1.478 + case SSL3_ST_SR_CERT_VRFY_B:
1.479 +
1.480 + /* we should decide if we expected this one */
1.481 + ret=ssl3_get_cert_verify(s);
1.482 + if (ret <= 0) goto end;
1.483 +
1.484 + s->state=SSL3_ST_SR_FINISHED_A;
1.485 + s->init_num=0;
1.486 + break;
1.487 +
1.488 + case SSL3_ST_SR_FINISHED_A:
1.489 + case SSL3_ST_SR_FINISHED_B:
1.490 + ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
1.491 + SSL3_ST_SR_FINISHED_B);
1.492 + if (ret <= 0) goto end;
1.493 + if (s->hit)
1.494 + s->state=SSL_ST_OK;
1.495 + else
1.496 + s->state=SSL3_ST_SW_CHANGE_A;
1.497 + s->init_num=0;
1.498 + break;
1.499 +
1.500 + case SSL3_ST_SW_CHANGE_A:
1.501 + case SSL3_ST_SW_CHANGE_B:
1.502 +
1.503 + s->session->cipher=s->s3->tmp.new_cipher;
1.504 + if (!s->method->ssl3_enc->setup_key_block(s))
1.505 + { ret= -1; goto end; }
1.506 +
1.507 + ret=dtls1_send_change_cipher_spec(s,
1.508 + SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
1.509 +
1.510 + if (ret <= 0) goto end;
1.511 + s->state=SSL3_ST_SW_FINISHED_A;
1.512 + s->init_num=0;
1.513 +
1.514 + if (!s->method->ssl3_enc->change_cipher_state(s,
1.515 + SSL3_CHANGE_CIPHER_SERVER_WRITE))
1.516 + {
1.517 + ret= -1;
1.518 + goto end;
1.519 + }
1.520 +
1.521 + dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
1.522 + break;
1.523 +
1.524 + case SSL3_ST_SW_FINISHED_A:
1.525 + case SSL3_ST_SW_FINISHED_B:
1.526 + ret=dtls1_send_finished(s,
1.527 + SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
1.528 + s->method->ssl3_enc->server_finished_label,
1.529 + s->method->ssl3_enc->server_finished_label_len);
1.530 + if (ret <= 0) goto end;
1.531 + s->state=SSL3_ST_SW_FLUSH;
1.532 + if (s->hit)
1.533 + s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
1.534 + else
1.535 + s->s3->tmp.next_state=SSL_ST_OK;
1.536 + s->init_num=0;
1.537 + break;
1.538 +
1.539 + case SSL_ST_OK:
1.540 + /* clean a few things up */
1.541 + ssl3_cleanup_key_block(s);
1.542 +
1.543 +#if 0
1.544 + BUF_MEM_free(s->init_buf);
1.545 + s->init_buf=NULL;
1.546 +#endif
1.547 +
1.548 + /* remove buffering on output */
1.549 + ssl_free_wbio_buffer(s);
1.550 +
1.551 + s->init_num=0;
1.552 +
1.553 + if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
1.554 + {
1.555 + /* actually not necessarily a 'new' session unless
1.556 + * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
1.557 +
1.558 + s->new_session=0;
1.559 +
1.560 + ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
1.561 +
1.562 + s->ctx->stats.sess_accept_good++;
1.563 + /* s->server=1; */
1.564 + s->handshake_func=dtls1_accept;
1.565 +
1.566 + if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
1.567 + }
1.568 +
1.569 + ret = 1;
1.570 +
1.571 + /* done handshaking, next message is client hello */
1.572 + s->d1->handshake_read_seq = 0;
1.573 + /* next message is server hello */
1.574 + s->d1->handshake_write_seq = 0;
1.575 + goto end;
1.576 + /* break; */
1.577 +
1.578 + default:
1.579 + SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);
1.580 + ret= -1;
1.581 + goto end;
1.582 + /* break; */
1.583 + }
1.584 +
1.585 + if (!s->s3->tmp.reuse_message && !skip)
1.586 + {
1.587 + if (s->debug)
1.588 + {
1.589 + if ((ret=BIO_flush(s->wbio)) <= 0)
1.590 + goto end;
1.591 + }
1.592 +
1.593 +
1.594 + if ((cb != NULL) && (s->state != state))
1.595 + {
1.596 + new_state=s->state;
1.597 + s->state=state;
1.598 + cb(s,SSL_CB_ACCEPT_LOOP,1);
1.599 + s->state=new_state;
1.600 + }
1.601 + }
1.602 + skip=0;
1.603 + }
1.604 +end:
1.605 + /* BIO_flush(s->wbio); */
1.606 +
1.607 + s->in_handshake--;
1.608 + if (cb != NULL)
1.609 + cb(s,SSL_CB_ACCEPT_EXIT,ret);
1.610 + return(ret);
1.611 + }
1.612 +
1.613 +int dtls1_send_hello_request(SSL *s)
1.614 + {
1.615 + unsigned char *p;
1.616 +
1.617 + if (s->state == SSL3_ST_SW_HELLO_REQ_A)
1.618 + {
1.619 + p=(unsigned char *)s->init_buf->data;
1.620 + p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
1.621 +
1.622 + s->state=SSL3_ST_SW_HELLO_REQ_B;
1.623 + /* number of bytes to write */
1.624 + s->init_num=DTLS1_HM_HEADER_LENGTH;
1.625 + s->init_off=0;
1.626 +
1.627 + /* no need to buffer this message, since there are no retransmit
1.628 + * requests for it */
1.629 + }
1.630 +
1.631 + /* SSL3_ST_SW_HELLO_REQ_B */
1.632 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.633 + }
1.634 +
1.635 +int dtls1_send_hello_verify_request(SSL *s)
1.636 + {
1.637 + unsigned int msg_len;
1.638 + unsigned char *msg, *buf, *p;
1.639 +
1.640 + if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
1.641 + {
1.642 + buf = (unsigned char *)s->init_buf->data;
1.643 +
1.644 + msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
1.645 + if (s->client_version == DTLS1_BAD_VER)
1.646 + *(p++) = DTLS1_BAD_VER>>8,
1.647 + *(p++) = DTLS1_BAD_VER&0xff;
1.648 + else
1.649 + *(p++) = s->version >> 8,
1.650 + *(p++) = s->version & 0xFF;
1.651 +
1.652 + if (s->ctx->app_gen_cookie_cb != NULL &&
1.653 + s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1.654 + &(s->d1->cookie_len)) == 0)
1.655 + {
1.656 + SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR);
1.657 + return 0;
1.658 + }
1.659 + /* else the cookie is assumed to have
1.660 + * been initialized by the application */
1.661 +
1.662 + *(p++) = (unsigned char) s->d1->cookie_len;
1.663 + memcpy(p, s->d1->cookie, s->d1->cookie_len);
1.664 + p += s->d1->cookie_len;
1.665 + msg_len = p - msg;
1.666 +
1.667 + dtls1_set_message_header(s, buf,
1.668 + DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
1.669 +
1.670 + s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
1.671 + /* number of bytes to write */
1.672 + s->init_num=p-buf;
1.673 + s->init_off=0;
1.674 +
1.675 + /* buffer the message to handle re-xmits */
1.676 + dtls1_buffer_message(s, 0);
1.677 + }
1.678 +
1.679 + /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
1.680 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.681 + }
1.682 +
1.683 +int dtls1_send_server_hello(SSL *s)
1.684 + {
1.685 + unsigned char *buf;
1.686 + unsigned char *p,*d;
1.687 + int i;
1.688 + unsigned int sl;
1.689 + unsigned long l,Time;
1.690 +
1.691 + if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
1.692 + {
1.693 + buf=(unsigned char *)s->init_buf->data;
1.694 + p=s->s3->server_random;
1.695 + Time=(unsigned long)time(NULL); /* Time */
1.696 + l2n(Time,p);
1.697 + RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
1.698 + /* Do the message type and length last */
1.699 + d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
1.700 +
1.701 + if (s->client_version == DTLS1_BAD_VER)
1.702 + *(p++)=DTLS1_BAD_VER>>8,
1.703 + *(p++)=DTLS1_BAD_VER&0xff;
1.704 + else
1.705 + *(p++)=s->version>>8,
1.706 + *(p++)=s->version&0xff;
1.707 +
1.708 + /* Random stuff */
1.709 + memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1.710 + p+=SSL3_RANDOM_SIZE;
1.711 +
1.712 + /* now in theory we have 3 options to sending back the
1.713 + * session id. If it is a re-use, we send back the
1.714 + * old session-id, if it is a new session, we send
1.715 + * back the new session-id or we send back a 0 length
1.716 + * session-id if we want it to be single use.
1.717 + * Currently I will not implement the '0' length session-id
1.718 + * 12-Jan-98 - I'll now support the '0' length stuff.
1.719 + */
1.720 + if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
1.721 + s->session->session_id_length=0;
1.722 +
1.723 + sl=s->session->session_id_length;
1.724 + if (sl > sizeof s->session->session_id)
1.725 + {
1.726 + SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1.727 + return -1;
1.728 + }
1.729 + *(p++)=sl;
1.730 + memcpy(p,s->session->session_id,sl);
1.731 + p+=sl;
1.732 +
1.733 + /* put the cipher */
1.734 + i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
1.735 + p+=i;
1.736 +
1.737 + /* put the compression method */
1.738 +#ifdef OPENSSL_NO_COMP
1.739 + *(p++)=0;
1.740 +#else
1.741 + if (s->s3->tmp.new_compression == NULL)
1.742 + *(p++)=0;
1.743 + else
1.744 + *(p++)=s->s3->tmp.new_compression->id;
1.745 +#endif
1.746 +
1.747 + /* do the header */
1.748 + l=(p-d);
1.749 + d=buf;
1.750 +
1.751 + d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
1.752 +
1.753 + s->state=SSL3_ST_CW_CLNT_HELLO_B;
1.754 + /* number of bytes to write */
1.755 + s->init_num=p-buf;
1.756 + s->init_off=0;
1.757 +
1.758 + /* buffer the message to handle re-xmits */
1.759 + dtls1_buffer_message(s, 0);
1.760 + }
1.761 +
1.762 + /* SSL3_ST_CW_CLNT_HELLO_B */
1.763 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.764 + }
1.765 +
1.766 +int dtls1_send_server_done(SSL *s)
1.767 + {
1.768 + unsigned char *p;
1.769 +
1.770 + if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1.771 + {
1.772 + p=(unsigned char *)s->init_buf->data;
1.773 +
1.774 + /* do the header */
1.775 + p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
1.776 +
1.777 + s->state=SSL3_ST_SW_SRVR_DONE_B;
1.778 + /* number of bytes to write */
1.779 + s->init_num=DTLS1_HM_HEADER_LENGTH;
1.780 + s->init_off=0;
1.781 +
1.782 + /* buffer the message to handle re-xmits */
1.783 + dtls1_buffer_message(s, 0);
1.784 + }
1.785 +
1.786 + /* SSL3_ST_CW_CLNT_HELLO_B */
1.787 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.788 + }
1.789 +
1.790 +int dtls1_send_server_key_exchange(SSL *s)
1.791 + {
1.792 +#ifndef OPENSSL_NO_RSA
1.793 + unsigned char *q;
1.794 + int j,num;
1.795 + RSA *rsa;
1.796 + unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1.797 + unsigned int u;
1.798 +#endif
1.799 +#ifndef OPENSSL_NO_DH
1.800 + DH *dh=NULL,*dhp;
1.801 +#endif
1.802 + EVP_PKEY *pkey;
1.803 + unsigned char *p,*d;
1.804 + int al,i;
1.805 + unsigned long type;
1.806 + int n;
1.807 + CERT *cert;
1.808 + BIGNUM *r[4];
1.809 + int nr[4],kn;
1.810 + BUF_MEM *buf;
1.811 + EVP_MD_CTX md_ctx;
1.812 +
1.813 + EVP_MD_CTX_init(&md_ctx);
1.814 + if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1.815 + {
1.816 + type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1.817 + cert=s->cert;
1.818 +
1.819 + buf=s->init_buf;
1.820 +
1.821 + r[0]=r[1]=r[2]=r[3]=NULL;
1.822 + n=0;
1.823 +#ifndef OPENSSL_NO_RSA
1.824 + if (type & SSL_kRSA)
1.825 + {
1.826 + rsa=cert->rsa_tmp;
1.827 + if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1.828 + {
1.829 + rsa=s->cert->rsa_tmp_cb(s,
1.830 + SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1.831 + SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1.832 + if(rsa == NULL)
1.833 + {
1.834 + al=SSL_AD_HANDSHAKE_FAILURE;
1.835 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1.836 + goto f_err;
1.837 + }
1.838 + RSA_up_ref(rsa);
1.839 + cert->rsa_tmp=rsa;
1.840 + }
1.841 + if (rsa == NULL)
1.842 + {
1.843 + al=SSL_AD_HANDSHAKE_FAILURE;
1.844 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1.845 + goto f_err;
1.846 + }
1.847 + r[0]=rsa->n;
1.848 + r[1]=rsa->e;
1.849 + s->s3->tmp.use_rsa_tmp=1;
1.850 + }
1.851 + else
1.852 +#endif
1.853 +#ifndef OPENSSL_NO_DH
1.854 + if (type & SSL_kEDH)
1.855 + {
1.856 + dhp=cert->dh_tmp;
1.857 + if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1.858 + dhp=s->cert->dh_tmp_cb(s,
1.859 + SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1.860 + SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1.861 + if (dhp == NULL)
1.862 + {
1.863 + al=SSL_AD_HANDSHAKE_FAILURE;
1.864 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1.865 + goto f_err;
1.866 + }
1.867 +
1.868 + if (s->s3->tmp.dh != NULL)
1.869 + {
1.870 + DH_free(dh);
1.871 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1.872 + goto err;
1.873 + }
1.874 +
1.875 + if ((dh=DHparams_dup(dhp)) == NULL)
1.876 + {
1.877 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1.878 + goto err;
1.879 + }
1.880 +
1.881 + s->s3->tmp.dh=dh;
1.882 + if ((dhp->pub_key == NULL ||
1.883 + dhp->priv_key == NULL ||
1.884 + (s->options & SSL_OP_SINGLE_DH_USE)))
1.885 + {
1.886 + if(!DH_generate_key(dh))
1.887 + {
1.888 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
1.889 + ERR_R_DH_LIB);
1.890 + goto err;
1.891 + }
1.892 + }
1.893 + else
1.894 + {
1.895 + dh->pub_key=BN_dup(dhp->pub_key);
1.896 + dh->priv_key=BN_dup(dhp->priv_key);
1.897 + if ((dh->pub_key == NULL) ||
1.898 + (dh->priv_key == NULL))
1.899 + {
1.900 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1.901 + goto err;
1.902 + }
1.903 + }
1.904 + r[0]=dh->p;
1.905 + r[1]=dh->g;
1.906 + r[2]=dh->pub_key;
1.907 + }
1.908 + else
1.909 +#endif
1.910 + {
1.911 + al=SSL_AD_HANDSHAKE_FAILURE;
1.912 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1.913 + goto f_err;
1.914 + }
1.915 + for (i=0; r[i] != NULL; i++)
1.916 + {
1.917 + nr[i]=BN_num_bytes(r[i]);
1.918 + n+=2+nr[i];
1.919 + }
1.920 +
1.921 + if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1.922 + {
1.923 + if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1.924 + == NULL)
1.925 + {
1.926 + al=SSL_AD_DECODE_ERROR;
1.927 + goto f_err;
1.928 + }
1.929 + kn=EVP_PKEY_size(pkey);
1.930 + }
1.931 + else
1.932 + {
1.933 + pkey=NULL;
1.934 + kn=0;
1.935 + }
1.936 +
1.937 + if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))
1.938 + {
1.939 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1.940 + goto err;
1.941 + }
1.942 + d=(unsigned char *)s->init_buf->data;
1.943 + p= &(d[DTLS1_HM_HEADER_LENGTH]);
1.944 +
1.945 + for (i=0; r[i] != NULL; i++)
1.946 + {
1.947 + s2n(nr[i],p);
1.948 + BN_bn2bin(r[i],p);
1.949 + p+=nr[i];
1.950 + }
1.951 +
1.952 + /* not anonymous */
1.953 + if (pkey != NULL)
1.954 + {
1.955 + /* n is the length of the params, they start at
1.956 + * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space
1.957 + * at the end. */
1.958 +#ifndef OPENSSL_NO_RSA
1.959 + if (pkey->type == EVP_PKEY_RSA)
1.960 + {
1.961 + q=md_buf;
1.962 + j=0;
1.963 + for (num=2; num > 0; num--)
1.964 + {
1.965 + EVP_DigestInit_ex(&md_ctx,(num == 2)
1.966 + ?s->ctx->md5:s->ctx->sha1, NULL);
1.967 + EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1.968 + EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1.969 + EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
1.970 + EVP_DigestFinal_ex(&md_ctx,q,
1.971 + (unsigned int *)&i);
1.972 + q+=i;
1.973 + j+=i;
1.974 + }
1.975 + if (RSA_sign(NID_md5_sha1, md_buf, j,
1.976 + &(p[2]), &u, pkey->pkey.rsa) <= 0)
1.977 + {
1.978 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1.979 + goto err;
1.980 + }
1.981 + s2n(u,p);
1.982 + n+=u+2;
1.983 + }
1.984 + else
1.985 +#endif
1.986 +#if !defined(OPENSSL_NO_DSA)
1.987 + if (pkey->type == EVP_PKEY_DSA)
1.988 + {
1.989 + /* lets do DSS */
1.990 + EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1.991 + EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1.992 + EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1.993 + EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
1.994 + if (!EVP_SignFinal(&md_ctx,&(p[2]),
1.995 + (unsigned int *)&i,pkey))
1.996 + {
1.997 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1.998 + goto err;
1.999 + }
1.1000 + s2n(i,p);
1.1001 + n+=i+2;
1.1002 + }
1.1003 + else
1.1004 +#endif
1.1005 + {
1.1006 + /* Is this error check actually needed? */
1.1007 + al=SSL_AD_HANDSHAKE_FAILURE;
1.1008 + SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1.1009 + goto f_err;
1.1010 + }
1.1011 + }
1.1012 +
1.1013 + d = dtls1_set_message_header(s, d,
1.1014 + SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
1.1015 +
1.1016 + /* we should now have things packed up, so lets send
1.1017 + * it off */
1.1018 + s->init_num=n+DTLS1_HM_HEADER_LENGTH;
1.1019 + s->init_off=0;
1.1020 +
1.1021 + /* buffer the message to handle re-xmits */
1.1022 + dtls1_buffer_message(s, 0);
1.1023 + }
1.1024 +
1.1025 + s->state = SSL3_ST_SW_KEY_EXCH_B;
1.1026 + EVP_MD_CTX_cleanup(&md_ctx);
1.1027 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.1028 +f_err:
1.1029 + ssl3_send_alert(s,SSL3_AL_FATAL,al);
1.1030 +err:
1.1031 + EVP_MD_CTX_cleanup(&md_ctx);
1.1032 + return(-1);
1.1033 + }
1.1034 +
1.1035 +int dtls1_send_certificate_request(SSL *s)
1.1036 + {
1.1037 + unsigned char *p,*d;
1.1038 + int i,j,nl,off,n;
1.1039 + STACK_OF(X509_NAME) *sk=NULL;
1.1040 + X509_NAME *name;
1.1041 + BUF_MEM *buf;
1.1042 + unsigned int msg_len;
1.1043 +
1.1044 + if (s->state == SSL3_ST_SW_CERT_REQ_A)
1.1045 + {
1.1046 + buf=s->init_buf;
1.1047 +
1.1048 + d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1.1049 +
1.1050 + /* get the list of acceptable cert types */
1.1051 + p++;
1.1052 + n=ssl3_get_req_cert_type(s,p);
1.1053 + d[0]=n;
1.1054 + p+=n;
1.1055 + n++;
1.1056 +
1.1057 + off=n;
1.1058 + p+=2;
1.1059 + n+=2;
1.1060 +
1.1061 + sk=SSL_get_client_CA_list(s);
1.1062 + nl=0;
1.1063 + if (sk != NULL)
1.1064 + {
1.1065 + for (i=0; i<sk_X509_NAME_num(sk); i++)
1.1066 + {
1.1067 + name=sk_X509_NAME_value(sk,i);
1.1068 + j=i2d_X509_NAME(name,NULL);
1.1069 + if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2))
1.1070 + {
1.1071 + SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1.1072 + goto err;
1.1073 + }
1.1074 + p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]);
1.1075 + if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1.1076 + {
1.1077 + s2n(j,p);
1.1078 + i2d_X509_NAME(name,&p);
1.1079 + n+=2+j;
1.1080 + nl+=2+j;
1.1081 + }
1.1082 + else
1.1083 + {
1.1084 + d=p;
1.1085 + i2d_X509_NAME(name,&p);
1.1086 + j-=2; s2n(j,d); j+=2;
1.1087 + n+=j;
1.1088 + nl+=j;
1.1089 + }
1.1090 + }
1.1091 + }
1.1092 + /* else no CA names */
1.1093 + p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]);
1.1094 + s2n(nl,p);
1.1095 +
1.1096 + d=(unsigned char *)buf->data;
1.1097 + *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1.1098 + l2n3(n,d);
1.1099 + s2n(s->d1->handshake_write_seq,d);
1.1100 + s->d1->handshake_write_seq++;
1.1101 +
1.1102 + /* we should now have things packed up, so lets send
1.1103 + * it off */
1.1104 +
1.1105 + s->init_num=n+DTLS1_HM_HEADER_LENGTH;
1.1106 + s->init_off=0;
1.1107 +#ifdef NETSCAPE_HANG_BUG
1.1108 +/* XXX: what to do about this? */
1.1109 + p=(unsigned char *)s->init_buf->data + s->init_num;
1.1110 +
1.1111 + /* do the header */
1.1112 + *(p++)=SSL3_MT_SERVER_DONE;
1.1113 + *(p++)=0;
1.1114 + *(p++)=0;
1.1115 + *(p++)=0;
1.1116 + s->init_num += 4;
1.1117 +#endif
1.1118 +
1.1119 + /* XDTLS: set message header ? */
1.1120 + msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
1.1121 + dtls1_set_message_header(s, (void *)s->init_buf->data,
1.1122 + SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len);
1.1123 +
1.1124 + /* buffer the message to handle re-xmits */
1.1125 + dtls1_buffer_message(s, 0);
1.1126 +
1.1127 + s->state = SSL3_ST_SW_CERT_REQ_B;
1.1128 + }
1.1129 +
1.1130 + /* SSL3_ST_SW_CERT_REQ_B */
1.1131 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.1132 +err:
1.1133 + return(-1);
1.1134 + }
1.1135 +
1.1136 +int dtls1_send_server_certificate(SSL *s)
1.1137 + {
1.1138 + unsigned long l;
1.1139 + X509 *x;
1.1140 +
1.1141 + if (s->state == SSL3_ST_SW_CERT_A)
1.1142 + {
1.1143 + x=ssl_get_server_send_cert(s);
1.1144 + if (x == NULL &&
1.1145 + /* VRS: allow null cert if auth == KRB5 */
1.1146 + (s->s3->tmp.new_cipher->algorithms
1.1147 + & (SSL_MKEY_MASK|SSL_AUTH_MASK))
1.1148 + != (SSL_aKRB5|SSL_kKRB5))
1.1149 + {
1.1150 + SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
1.1151 + return(0);
1.1152 + }
1.1153 +
1.1154 + l=dtls1_output_cert_chain(s,x);
1.1155 + s->state=SSL3_ST_SW_CERT_B;
1.1156 + s->init_num=(int)l;
1.1157 + s->init_off=0;
1.1158 +
1.1159 + /* buffer the message to handle re-xmits */
1.1160 + dtls1_buffer_message(s, 0);
1.1161 + }
1.1162 +
1.1163 + /* SSL3_ST_SW_CERT_B */
1.1164 + return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
1.1165 + }