Home | History | Annotate | Download | only in ssl
      1 /* ssl/d1_srvr.c */
      2 /*
      3  * DTLS implementation written by Nagendra Modadugu
      4  * (nagendra (at) cs.stanford.edu) for the OpenSSL project 2005.
      5  */
      6 /* ====================================================================
      7  * Copyright (c) 1999-2005 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 (at) 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 (at) cryptsoft.com).  This product includes software written by Tim
     56  * Hudson (tjh (at) cryptsoft.com).
     57  *
     58  */
     59 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
     60  * All rights reserved.
     61  *
     62  * This package is an SSL implementation written
     63  * by Eric Young (eay (at) 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 (at) 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 (at) 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 (at) 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 #include "ssl_locl.h"
    118 #include <openssl/buffer.h>
    119 #include <openssl/rand.h>
    120 #include <openssl/objects.h>
    121 #include <openssl/evp.h>
    122 #include <openssl/x509.h>
    123 #include <openssl/md5.h>
    124 #ifndef OPENSSL_NO_DH
    125 #include <openssl/dh.h>
    126 #endif
    127 
    128 static SSL_METHOD *dtls1_get_server_method(int ver);
    129 static int dtls1_send_hello_verify_request(SSL *s);
    130 
    131 static SSL_METHOD *dtls1_get_server_method(int ver)
    132 	{
    133 	if (ver == DTLS1_VERSION)
    134 		return(DTLSv1_server_method());
    135 	else
    136 		return(NULL);
    137 	}
    138 
    139 IMPLEMENT_dtls1_meth_func(DTLSv1_server_method,
    140 			dtls1_accept,
    141 			ssl_undefined_function,
    142 			dtls1_get_server_method)
    143 
    144 int dtls1_accept(SSL *s)
    145 	{
    146 	BUF_MEM *buf;
    147 	unsigned long l,Time=(unsigned long)time(NULL);
    148 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
    149 	int ret= -1;
    150 	int new_state,state,skip=0;
    151 
    152 	RAND_add(&Time,sizeof(Time),0);
    153 	ERR_clear_error();
    154 	clear_sys_error();
    155 
    156 	if (s->info_callback != NULL)
    157 		cb=s->info_callback;
    158 	else if (s->ctx->info_callback != NULL)
    159 		cb=s->ctx->info_callback;
    160 
    161 	/* init things to blank */
    162 	s->in_handshake++;
    163 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
    164 
    165 	if (s->cert == NULL)
    166 		{
    167 		SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
    168 		return(-1);
    169 		}
    170 
    171 	for (;;)
    172 		{
    173 		state=s->state;
    174 
    175 		switch (s->state)
    176 			{
    177 		case SSL_ST_RENEGOTIATE:
    178 			s->new_session=1;
    179 			/* s->state=SSL_ST_ACCEPT; */
    180 
    181 		case SSL_ST_BEFORE:
    182 		case SSL_ST_ACCEPT:
    183 		case SSL_ST_BEFORE|SSL_ST_ACCEPT:
    184 		case SSL_ST_OK|SSL_ST_ACCEPT:
    185 
    186 			s->server=1;
    187 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
    188 
    189 			if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00))
    190 				{
    191 				SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR);
    192 				return -1;
    193 				}
    194 			s->type=SSL_ST_ACCEPT;
    195 
    196 			if (s->init_buf == NULL)
    197 				{
    198 				if ((buf=BUF_MEM_new()) == NULL)
    199 					{
    200 					ret= -1;
    201 					goto end;
    202 					}
    203 				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
    204 					{
    205 					ret= -1;
    206 					goto end;
    207 					}
    208 				s->init_buf=buf;
    209 				}
    210 
    211 			if (!ssl3_setup_buffers(s))
    212 				{
    213 				ret= -1;
    214 				goto end;
    215 				}
    216 
    217 			s->init_num=0;
    218 
    219 			if (s->state != SSL_ST_RENEGOTIATE)
    220 				{
    221 				/* Ok, we now need to push on a buffering BIO so that
    222 				 * the output is sent in a way that TCP likes :-)
    223 				 */
    224 				if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
    225 
    226 				ssl3_init_finished_mac(s);
    227 				s->state=SSL3_ST_SR_CLNT_HELLO_A;
    228 				s->ctx->stats.sess_accept++;
    229 				}
    230 			else
    231 				{
    232 				/* s->state == SSL_ST_RENEGOTIATE,
    233 				 * we will just send a HelloRequest */
    234 				s->ctx->stats.sess_accept_renegotiate++;
    235 				s->state=SSL3_ST_SW_HELLO_REQ_A;
    236 				}
    237 
    238 			break;
    239 
    240 		case SSL3_ST_SW_HELLO_REQ_A:
    241 		case SSL3_ST_SW_HELLO_REQ_B:
    242 
    243 			s->shutdown=0;
    244 			dtls1_start_timer(s);
    245 			ret=dtls1_send_hello_request(s);
    246 			if (ret <= 0) goto end;
    247 			s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
    248 			s->state=SSL3_ST_SW_FLUSH;
    249 			s->init_num=0;
    250 
    251 			ssl3_init_finished_mac(s);
    252 			break;
    253 
    254 		case SSL3_ST_SW_HELLO_REQ_C:
    255 			s->state=SSL_ST_OK;
    256 			break;
    257 
    258 		case SSL3_ST_SR_CLNT_HELLO_A:
    259 		case SSL3_ST_SR_CLNT_HELLO_B:
    260 		case SSL3_ST_SR_CLNT_HELLO_C:
    261 
    262 			s->shutdown=0;
    263 			ret=ssl3_get_client_hello(s);
    264 			if (ret <= 0) goto end;
    265 			dtls1_stop_timer(s);
    266 
    267 			if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
    268 				s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A;
    269 			else
    270 				s->state = SSL3_ST_SW_SRVR_HELLO_A;
    271 
    272 			s->init_num=0;
    273 
    274 			/* If we're just listening, stop here */
    275 			if (s->d1->listen && s->state == SSL3_ST_SW_SRVR_HELLO_A)
    276 				{
    277 				ret = 2;
    278 				s->d1->listen = 0;
    279 				goto end;
    280 				}
    281 
    282 			break;
    283 
    284 		case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A:
    285 		case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B:
    286 
    287 			dtls1_start_timer(s);
    288 			ret = dtls1_send_hello_verify_request(s);
    289 			if ( ret <= 0) goto end;
    290 			s->state=SSL3_ST_SW_FLUSH;
    291 			s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A;
    292 
    293 			/* HelloVerifyRequests resets Finished MAC */
    294 			if (s->client_version != DTLS1_BAD_VER)
    295 				ssl3_init_finished_mac(s);
    296 			break;
    297 
    298 		case SSL3_ST_SW_SRVR_HELLO_A:
    299 		case SSL3_ST_SW_SRVR_HELLO_B:
    300 			s->new_session = 2;
    301 			dtls1_start_timer(s);
    302 			ret=dtls1_send_server_hello(s);
    303 			if (ret <= 0) goto end;
    304 
    305 #ifndef OPENSSL_NO_TLSEXT
    306 			if (s->hit)
    307 				{
    308 				if (s->tlsext_ticket_expected)
    309 					s->state=SSL3_ST_SW_SESSION_TICKET_A;
    310 				else
    311 					s->state=SSL3_ST_SW_CHANGE_A;
    312 				}
    313 #else
    314 			if (s->hit)
    315 					s->state=SSL3_ST_SW_CHANGE_A;
    316 #endif
    317 			else
    318 				s->state=SSL3_ST_SW_CERT_A;
    319 			s->init_num=0;
    320 			break;
    321 
    322 		case SSL3_ST_SW_CERT_A:
    323 		case SSL3_ST_SW_CERT_B:
    324 			/* Check if it is anon DH */
    325 			if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
    326 				{
    327 				dtls1_start_timer(s);
    328 				ret=dtls1_send_server_certificate(s);
    329 				if (ret <= 0) goto end;
    330 #ifndef OPENSSL_NO_TLSEXT
    331 				if (s->tlsext_status_expected)
    332 					s->state=SSL3_ST_SW_CERT_STATUS_A;
    333 				else
    334 					s->state=SSL3_ST_SW_KEY_EXCH_A;
    335 				}
    336 			else
    337 				{
    338 				skip = 1;
    339 				s->state=SSL3_ST_SW_KEY_EXCH_A;
    340 				}
    341 #else
    342 				}
    343 			else
    344 				skip=1;
    345 
    346 			s->state=SSL3_ST_SW_KEY_EXCH_A;
    347 #endif
    348 			s->init_num=0;
    349 			break;
    350 
    351 		case SSL3_ST_SW_KEY_EXCH_A:
    352 		case SSL3_ST_SW_KEY_EXCH_B:
    353 			l=s->s3->tmp.new_cipher->algorithms;
    354 
    355 			/* clear this, it may get reset by
    356 			 * send_server_key_exchange */
    357 			if ((s->options & SSL_OP_EPHEMERAL_RSA)
    358 #ifndef OPENSSL_NO_KRB5
    359 				&& !(l & SSL_KRB5)
    360 #endif /* OPENSSL_NO_KRB5 */
    361 				)
    362 				/* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
    363 				 * even when forbidden by protocol specs
    364 				 * (handshake may fail as clients are not required to
    365 				 * be able to handle this) */
    366 				s->s3->tmp.use_rsa_tmp=1;
    367 			else
    368 				s->s3->tmp.use_rsa_tmp=0;
    369 
    370 			/* only send if a DH key exchange, fortezza or
    371 			 * RSA but we have a sign only certificate */
    372 			if (s->s3->tmp.use_rsa_tmp
    373 			    || (l & (SSL_DH|SSL_kFZA))
    374 			    || ((l & SSL_kRSA)
    375 				&& (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
    376 				    || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
    377 					&& EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
    378 					)
    379 				    )
    380 				)
    381 			    )
    382 				{
    383 				dtls1_start_timer(s);
    384 				ret=dtls1_send_server_key_exchange(s);
    385 				if (ret <= 0) goto end;
    386 				}
    387 			else
    388 				skip=1;
    389 
    390 			s->state=SSL3_ST_SW_CERT_REQ_A;
    391 			s->init_num=0;
    392 			break;
    393 
    394 		case SSL3_ST_SW_CERT_REQ_A:
    395 		case SSL3_ST_SW_CERT_REQ_B:
    396 			if (/* don't request cert unless asked for it: */
    397 				!(s->verify_mode & SSL_VERIFY_PEER) ||
    398 				/* if SSL_VERIFY_CLIENT_ONCE is set,
    399 				 * don't request cert during re-negotiation: */
    400 				((s->session->peer != NULL) &&
    401 				 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
    402 				/* never request cert in anonymous ciphersuites
    403 				 * (see section "Certificate request" in SSL 3 drafts
    404 				 * and in RFC 2246): */
    405 				((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
    406 				 /* ... except when the application insists on verification
    407 				  * (against the specs, but s3_clnt.c accepts this for SSL 3) */
    408 				 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
    409                                  /* never request cert in Kerberos ciphersuites */
    410                                 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
    411 				{
    412 				/* no cert request */
    413 				skip=1;
    414 				s->s3->tmp.cert_request=0;
    415 				s->state=SSL3_ST_SW_SRVR_DONE_A;
    416 				}
    417 			else
    418 				{
    419 				s->s3->tmp.cert_request=1;
    420 				dtls1_start_timer(s);
    421 				ret=dtls1_send_certificate_request(s);
    422 				if (ret <= 0) goto end;
    423 #ifndef NETSCAPE_HANG_BUG
    424 				s->state=SSL3_ST_SW_SRVR_DONE_A;
    425 #else
    426 				s->state=SSL3_ST_SW_FLUSH;
    427 				s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
    428 #endif
    429 				s->init_num=0;
    430 				}
    431 			break;
    432 
    433 		case SSL3_ST_SW_SRVR_DONE_A:
    434 		case SSL3_ST_SW_SRVR_DONE_B:
    435 			dtls1_start_timer(s);
    436 			ret=dtls1_send_server_done(s);
    437 			if (ret <= 0) goto end;
    438 			s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
    439 			s->state=SSL3_ST_SW_FLUSH;
    440 			s->init_num=0;
    441 			break;
    442 
    443 		case SSL3_ST_SW_FLUSH:
    444 			s->rwstate=SSL_WRITING;
    445 			if (BIO_flush(s->wbio) <= 0)
    446 				{
    447 				ret= -1;
    448 				goto end;
    449 				}
    450 			s->rwstate=SSL_NOTHING;
    451 			s->state=s->s3->tmp.next_state;
    452 			break;
    453 
    454 		case SSL3_ST_SR_CERT_A:
    455 		case SSL3_ST_SR_CERT_B:
    456 			/* Check for second client hello (MS SGC) */
    457 			ret = ssl3_check_client_hello(s);
    458 			if (ret <= 0)
    459 				goto end;
    460 			dtls1_stop_timer(s);
    461 			if (ret == 2)
    462 				s->state = SSL3_ST_SR_CLNT_HELLO_C;
    463 			else {
    464 				/* could be sent for a DH cert, even if we
    465 				 * have not asked for it :-) */
    466 				ret=ssl3_get_client_certificate(s);
    467 				if (ret <= 0) goto end;
    468 				dtls1_stop_timer(s);
    469 				s->init_num=0;
    470 				s->state=SSL3_ST_SR_KEY_EXCH_A;
    471 			}
    472 			break;
    473 
    474 		case SSL3_ST_SR_KEY_EXCH_A:
    475 		case SSL3_ST_SR_KEY_EXCH_B:
    476 			ret=ssl3_get_client_key_exchange(s);
    477 			if (ret <= 0) goto end;
    478 			dtls1_stop_timer(s);
    479 			s->state=SSL3_ST_SR_CERT_VRFY_A;
    480 			s->init_num=0;
    481 
    482 			/* We need to get hashes here so if there is
    483 			 * a client cert, it can be verified */
    484 			s->method->ssl3_enc->cert_verify_mac(s,
    485 				&(s->s3->finish_dgst1),
    486 				&(s->s3->tmp.cert_verify_md[0]));
    487 			s->method->ssl3_enc->cert_verify_mac(s,
    488 				&(s->s3->finish_dgst2),
    489 				&(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
    490 
    491 			break;
    492 
    493 		case SSL3_ST_SR_CERT_VRFY_A:
    494 		case SSL3_ST_SR_CERT_VRFY_B:
    495 
    496 			s->d1->change_cipher_spec_ok = 1;
    497 			/* we should decide if we expected this one */
    498 			ret=ssl3_get_cert_verify(s);
    499 			if (ret <= 0) goto end;
    500 			dtls1_stop_timer(s);
    501 
    502 			s->state=SSL3_ST_SR_FINISHED_A;
    503 			s->init_num=0;
    504 			break;
    505 
    506 		case SSL3_ST_SR_FINISHED_A:
    507 		case SSL3_ST_SR_FINISHED_B:
    508 			s->d1->change_cipher_spec_ok = 1;
    509 			ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
    510 				SSL3_ST_SR_FINISHED_B);
    511 			if (ret <= 0) goto end;
    512 			dtls1_stop_timer(s);
    513 			if (s->hit)
    514 				s->state=SSL_ST_OK;
    515 #ifndef OPENSSL_NO_TLSEXT
    516 			else if (s->tlsext_ticket_expected)
    517 				s->state=SSL3_ST_SW_SESSION_TICKET_A;
    518 #endif
    519 			else
    520 				s->state=SSL3_ST_SW_CHANGE_A;
    521 			s->init_num=0;
    522 			break;
    523 
    524 #ifndef OPENSSL_NO_TLSEXT
    525 		case SSL3_ST_SW_SESSION_TICKET_A:
    526 		case SSL3_ST_SW_SESSION_TICKET_B:
    527 			ret=dtls1_send_newsession_ticket(s);
    528 			if (ret <= 0) goto end;
    529 			s->state=SSL3_ST_SW_CHANGE_A;
    530 			s->init_num=0;
    531 			break;
    532 
    533 		case SSL3_ST_SW_CERT_STATUS_A:
    534 		case SSL3_ST_SW_CERT_STATUS_B:
    535 			ret=ssl3_send_cert_status(s);
    536 			if (ret <= 0) goto end;
    537 			s->state=SSL3_ST_SW_KEY_EXCH_A;
    538 			s->init_num=0;
    539 			break;
    540 
    541 #endif
    542 
    543 		case SSL3_ST_SW_CHANGE_A:
    544 		case SSL3_ST_SW_CHANGE_B:
    545 
    546 			s->session->cipher=s->s3->tmp.new_cipher;
    547 			if (!s->method->ssl3_enc->setup_key_block(s))
    548 				{ ret= -1; goto end; }
    549 
    550 			ret=dtls1_send_change_cipher_spec(s,
    551 				SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
    552 
    553 			if (ret <= 0) goto end;
    554 			s->state=SSL3_ST_SW_FINISHED_A;
    555 			s->init_num=0;
    556 
    557 			if (!s->method->ssl3_enc->change_cipher_state(s,
    558 				SSL3_CHANGE_CIPHER_SERVER_WRITE))
    559 				{
    560 				ret= -1;
    561 				goto end;
    562 				}
    563 
    564 			dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
    565 			break;
    566 
    567 		case SSL3_ST_SW_FINISHED_A:
    568 		case SSL3_ST_SW_FINISHED_B:
    569 			ret=dtls1_send_finished(s,
    570 				SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
    571 				s->method->ssl3_enc->server_finished_label,
    572 				s->method->ssl3_enc->server_finished_label_len);
    573 			if (ret <= 0) goto end;
    574 			s->state=SSL3_ST_SW_FLUSH;
    575 			if (s->hit)
    576 				s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
    577 			else
    578 				s->s3->tmp.next_state=SSL_ST_OK;
    579 			s->init_num=0;
    580 			break;
    581 
    582 		case SSL_ST_OK:
    583 			/* clean a few things up */
    584 			ssl3_cleanup_key_block(s);
    585 
    586 #if 0
    587 			BUF_MEM_free(s->init_buf);
    588 			s->init_buf=NULL;
    589 #endif
    590 
    591 			/* remove buffering on output */
    592 			ssl_free_wbio_buffer(s);
    593 
    594 			s->init_num=0;
    595 
    596 			if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
    597 				{
    598 				/* actually not necessarily a 'new' session unless
    599 				 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
    600 
    601 				s->new_session=0;
    602 
    603 				ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
    604 
    605 				s->ctx->stats.sess_accept_good++;
    606 				/* s->server=1; */
    607 				s->handshake_func=dtls1_accept;
    608 
    609 				if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
    610 				}
    611 
    612 			ret = 1;
    613 
    614 			/* done handshaking, next message is client hello */
    615 			s->d1->handshake_read_seq = 0;
    616 			/* next message is server hello */
    617 			s->d1->handshake_write_seq = 0;
    618 			s->d1->next_handshake_write_seq = 0;
    619 			goto end;
    620 			/* break; */
    621 
    622 		default:
    623 			SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE);
    624 			ret= -1;
    625 			goto end;
    626 			/* break; */
    627 			}
    628 
    629 		if (!s->s3->tmp.reuse_message && !skip)
    630 			{
    631 			if (s->debug)
    632 				{
    633 				if ((ret=BIO_flush(s->wbio)) <= 0)
    634 					goto end;
    635 				}
    636 
    637 
    638 			if ((cb != NULL) && (s->state != state))
    639 				{
    640 				new_state=s->state;
    641 				s->state=state;
    642 				cb(s,SSL_CB_ACCEPT_LOOP,1);
    643 				s->state=new_state;
    644 				}
    645 			}
    646 		skip=0;
    647 		}
    648 end:
    649 	/* BIO_flush(s->wbio); */
    650 
    651 	s->in_handshake--;
    652 	if (cb != NULL)
    653 		cb(s,SSL_CB_ACCEPT_EXIT,ret);
    654 	return(ret);
    655 	}
    656 
    657 int dtls1_send_hello_request(SSL *s)
    658 	{
    659 	unsigned char *p;
    660 
    661 	if (s->state == SSL3_ST_SW_HELLO_REQ_A)
    662 		{
    663 		p=(unsigned char *)s->init_buf->data;
    664 		p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0);
    665 
    666 		s->state=SSL3_ST_SW_HELLO_REQ_B;
    667 		/* number of bytes to write */
    668 		s->init_num=DTLS1_HM_HEADER_LENGTH;
    669 		s->init_off=0;
    670 
    671 		/* no need to buffer this message, since there are no retransmit
    672 		 * requests for it */
    673 		}
    674 
    675 	/* SSL3_ST_SW_HELLO_REQ_B */
    676 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
    677 	}
    678 
    679 int dtls1_send_hello_verify_request(SSL *s)
    680 	{
    681 	unsigned int msg_len;
    682 	unsigned char *msg, *buf, *p;
    683 
    684 	if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A)
    685 		{
    686 		buf = (unsigned char *)s->init_buf->data;
    687 
    688 		msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
    689 		if (s->client_version == DTLS1_BAD_VER)
    690 			*(p++) = DTLS1_BAD_VER>>8,
    691 			*(p++) = DTLS1_BAD_VER&0xff;
    692 		else
    693 			*(p++) = s->version >> 8,
    694 			*(p++) = s->version & 0xFF;
    695 
    696 		if (s->ctx->app_gen_cookie_cb == NULL ||
    697 		     s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
    698 			 &(s->d1->cookie_len)) == 0)
    699 			{
    700 			SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR);
    701 			return 0;
    702 			}
    703 
    704 		*(p++) = (unsigned char) s->d1->cookie_len;
    705 		memcpy(p, s->d1->cookie, s->d1->cookie_len);
    706 		p += s->d1->cookie_len;
    707 		msg_len = p - msg;
    708 
    709 		dtls1_set_message_header(s, buf,
    710 			DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len);
    711 
    712 		s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B;
    713 		/* number of bytes to write */
    714 		s->init_num=p-buf;
    715 		s->init_off=0;
    716 
    717 		/* buffer the message to handle re-xmits */
    718 		dtls1_buffer_message(s, 0);
    719 		}
    720 
    721 	/* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
    722 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
    723 	}
    724 
    725 int dtls1_send_server_hello(SSL *s)
    726 	{
    727 	unsigned char *buf;
    728 	unsigned char *p,*d;
    729 	int i;
    730 	unsigned int sl;
    731 	unsigned long l,Time;
    732 
    733 	if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
    734 		{
    735 		buf=(unsigned char *)s->init_buf->data;
    736 		p=s->s3->server_random;
    737 		Time=(unsigned long)time(NULL);			/* Time */
    738 		l2n(Time,p);
    739 		RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
    740 		/* Do the message type and length last */
    741 		d=p= &(buf[DTLS1_HM_HEADER_LENGTH]);
    742 
    743 		if (s->client_version == DTLS1_BAD_VER)
    744 			*(p++)=DTLS1_BAD_VER>>8,
    745 			*(p++)=DTLS1_BAD_VER&0xff;
    746 		else
    747 			*(p++)=s->version>>8,
    748 			*(p++)=s->version&0xff;
    749 
    750 		/* Random stuff */
    751 		memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
    752 		p+=SSL3_RANDOM_SIZE;
    753 
    754 		/* now in theory we have 3 options to sending back the
    755 		 * session id.  If it is a re-use, we send back the
    756 		 * old session-id, if it is a new session, we send
    757 		 * back the new session-id or we send back a 0 length
    758 		 * session-id if we want it to be single use.
    759 		 * Currently I will not implement the '0' length session-id
    760 		 * 12-Jan-98 - I'll now support the '0' length stuff.
    761 		 */
    762 		if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
    763 			s->session->session_id_length=0;
    764 
    765 		sl=s->session->session_id_length;
    766 		if (sl > sizeof s->session->session_id)
    767 			{
    768 			SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
    769 			return -1;
    770 			}
    771 		*(p++)=sl;
    772 		memcpy(p,s->session->session_id,sl);
    773 		p+=sl;
    774 
    775 		/* put the cipher */
    776 		if (s->s3->tmp.new_cipher == NULL)
    777 			return -1;
    778 		i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
    779 		p+=i;
    780 
    781 		/* put the compression method */
    782 #ifdef OPENSSL_NO_COMP
    783 		*(p++)=0;
    784 #else
    785 		if (s->s3->tmp.new_compression == NULL)
    786 			*(p++)=0;
    787 		else
    788 			*(p++)=s->s3->tmp.new_compression->id;
    789 #endif
    790 
    791 #ifndef OPENSSL_NO_TLSEXT
    792 		if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
    793 			{
    794 			SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR);
    795 			return -1;
    796 			}
    797 #endif
    798 
    799 		/* do the header */
    800 		l=(p-d);
    801 		d=buf;
    802 
    803 		d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l);
    804 
    805 		s->state=SSL3_ST_SW_SRVR_HELLO_B;
    806 		/* number of bytes to write */
    807 		s->init_num=p-buf;
    808 		s->init_off=0;
    809 
    810 		/* buffer the message to handle re-xmits */
    811 		dtls1_buffer_message(s, 0);
    812 		}
    813 
    814 	/* SSL3_ST_SW_SRVR_HELLO_B */
    815 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
    816 	}
    817 
    818 int dtls1_send_server_done(SSL *s)
    819 	{
    820 	unsigned char *p;
    821 
    822 	if (s->state == SSL3_ST_SW_SRVR_DONE_A)
    823 		{
    824 		p=(unsigned char *)s->init_buf->data;
    825 
    826 		/* do the header */
    827 		p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0);
    828 
    829 		s->state=SSL3_ST_SW_SRVR_DONE_B;
    830 		/* number of bytes to write */
    831 		s->init_num=DTLS1_HM_HEADER_LENGTH;
    832 		s->init_off=0;
    833 
    834 		/* buffer the message to handle re-xmits */
    835 		dtls1_buffer_message(s, 0);
    836 		}
    837 
    838 	/* SSL3_ST_SW_SRVR_DONE_B */
    839 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
    840 	}
    841 
    842 int dtls1_send_server_key_exchange(SSL *s)
    843 	{
    844 #ifndef OPENSSL_NO_RSA
    845 	unsigned char *q;
    846 	int j,num;
    847 	RSA *rsa;
    848 	unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
    849 	unsigned int u;
    850 #endif
    851 #ifndef OPENSSL_NO_DH
    852 	DH *dh=NULL,*dhp;
    853 #endif
    854 	EVP_PKEY *pkey;
    855 	unsigned char *p,*d;
    856 	int al,i;
    857 	unsigned long type;
    858 	int n;
    859 	CERT *cert;
    860 	BIGNUM *r[4];
    861 	int nr[4],kn;
    862 	BUF_MEM *buf;
    863 	EVP_MD_CTX md_ctx;
    864 
    865 	EVP_MD_CTX_init(&md_ctx);
    866 	if (s->state == SSL3_ST_SW_KEY_EXCH_A)
    867 		{
    868 		type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
    869 		cert=s->cert;
    870 
    871 		buf=s->init_buf;
    872 
    873 		r[0]=r[1]=r[2]=r[3]=NULL;
    874 		n=0;
    875 #ifndef OPENSSL_NO_RSA
    876 		if (type & SSL_kRSA)
    877 			{
    878 			rsa=cert->rsa_tmp;
    879 			if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
    880 				{
    881 				rsa=s->cert->rsa_tmp_cb(s,
    882 				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
    883 				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
    884 				if(rsa == NULL)
    885 				{
    886 					al=SSL_AD_HANDSHAKE_FAILURE;
    887 					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
    888 					goto f_err;
    889 				}
    890 				RSA_up_ref(rsa);
    891 				cert->rsa_tmp=rsa;
    892 				}
    893 			if (rsa == NULL)
    894 				{
    895 				al=SSL_AD_HANDSHAKE_FAILURE;
    896 				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
    897 				goto f_err;
    898 				}
    899 			r[0]=rsa->n;
    900 			r[1]=rsa->e;
    901 			s->s3->tmp.use_rsa_tmp=1;
    902 			}
    903 		else
    904 #endif
    905 #ifndef OPENSSL_NO_DH
    906 			if (type & SSL_kEDH)
    907 			{
    908 			dhp=cert->dh_tmp;
    909 			if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
    910 				dhp=s->cert->dh_tmp_cb(s,
    911 				      SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
    912 				      SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
    913 			if (dhp == NULL)
    914 				{
    915 				al=SSL_AD_HANDSHAKE_FAILURE;
    916 				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
    917 				goto f_err;
    918 				}
    919 
    920 			if (s->s3->tmp.dh != NULL)
    921 				{
    922 				DH_free(dh);
    923 				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
    924 				goto err;
    925 				}
    926 
    927 			if ((dh=DHparams_dup(dhp)) == NULL)
    928 				{
    929 				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
    930 				goto err;
    931 				}
    932 
    933 			s->s3->tmp.dh=dh;
    934 			if ((dhp->pub_key == NULL ||
    935 			     dhp->priv_key == NULL ||
    936 			     (s->options & SSL_OP_SINGLE_DH_USE)))
    937 				{
    938 				if(!DH_generate_key(dh))
    939 				    {
    940 				    SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,
    941 					   ERR_R_DH_LIB);
    942 				    goto err;
    943 				    }
    944 				}
    945 			else
    946 				{
    947 				dh->pub_key=BN_dup(dhp->pub_key);
    948 				dh->priv_key=BN_dup(dhp->priv_key);
    949 				if ((dh->pub_key == NULL) ||
    950 					(dh->priv_key == NULL))
    951 					{
    952 					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
    953 					goto err;
    954 					}
    955 				}
    956 			r[0]=dh->p;
    957 			r[1]=dh->g;
    958 			r[2]=dh->pub_key;
    959 			}
    960 		else
    961 #endif
    962 			{
    963 			al=SSL_AD_HANDSHAKE_FAILURE;
    964 			SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
    965 			goto f_err;
    966 			}
    967 		for (i=0; r[i] != NULL; i++)
    968 			{
    969 			nr[i]=BN_num_bytes(r[i]);
    970 			n+=2+nr[i];
    971 			}
    972 
    973 		if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
    974 			{
    975 			if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
    976 				== NULL)
    977 				{
    978 				al=SSL_AD_DECODE_ERROR;
    979 				goto f_err;
    980 				}
    981 			kn=EVP_PKEY_size(pkey);
    982 			}
    983 		else
    984 			{
    985 			pkey=NULL;
    986 			kn=0;
    987 			}
    988 
    989 		if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn))
    990 			{
    991 			SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
    992 			goto err;
    993 			}
    994 		d=(unsigned char *)s->init_buf->data;
    995 		p= &(d[DTLS1_HM_HEADER_LENGTH]);
    996 
    997 		for (i=0; r[i] != NULL; i++)
    998 			{
    999 			s2n(nr[i],p);
   1000 			BN_bn2bin(r[i],p);
   1001 			p+=nr[i];
   1002 			}
   1003 
   1004 		/* not anonymous */
   1005 		if (pkey != NULL)
   1006 			{
   1007 			/* n is the length of the params, they start at
   1008 			 * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space
   1009 			 * at the end. */
   1010 #ifndef OPENSSL_NO_RSA
   1011 			if (pkey->type == EVP_PKEY_RSA)
   1012 				{
   1013 				q=md_buf;
   1014 				j=0;
   1015 				for (num=2; num > 0; num--)
   1016 					{
   1017 					EVP_DigestInit_ex(&md_ctx,(num == 2)
   1018 						?s->ctx->md5:s->ctx->sha1, NULL);
   1019 					EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
   1020 					EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
   1021 					EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
   1022 					EVP_DigestFinal_ex(&md_ctx,q,
   1023 						(unsigned int *)&i);
   1024 					q+=i;
   1025 					j+=i;
   1026 					}
   1027 				if (RSA_sign(NID_md5_sha1, md_buf, j,
   1028 					&(p[2]), &u, pkey->pkey.rsa) <= 0)
   1029 					{
   1030 					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
   1031 					goto err;
   1032 					}
   1033 				s2n(u,p);
   1034 				n+=u+2;
   1035 				}
   1036 			else
   1037 #endif
   1038 #if !defined(OPENSSL_NO_DSA)
   1039 				if (pkey->type == EVP_PKEY_DSA)
   1040 				{
   1041 				/* lets do DSS */
   1042 				EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
   1043 				EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
   1044 				EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
   1045 				EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n);
   1046 				if (!EVP_SignFinal(&md_ctx,&(p[2]),
   1047 					(unsigned int *)&i,pkey))
   1048 					{
   1049 					SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
   1050 					goto err;
   1051 					}
   1052 				s2n(i,p);
   1053 				n+=i+2;
   1054 				}
   1055 			else
   1056 #endif
   1057 				{
   1058 				/* Is this error check actually needed? */
   1059 				al=SSL_AD_HANDSHAKE_FAILURE;
   1060 				SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
   1061 				goto f_err;
   1062 				}
   1063 			}
   1064 
   1065 		d = dtls1_set_message_header(s, d,
   1066 			SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n);
   1067 
   1068 		/* we should now have things packed up, so lets send
   1069 		 * it off */
   1070 		s->init_num=n+DTLS1_HM_HEADER_LENGTH;
   1071 		s->init_off=0;
   1072 
   1073 		/* buffer the message to handle re-xmits */
   1074 		dtls1_buffer_message(s, 0);
   1075 		}
   1076 
   1077 	s->state = SSL3_ST_SW_KEY_EXCH_B;
   1078 	EVP_MD_CTX_cleanup(&md_ctx);
   1079 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
   1080 f_err:
   1081 	ssl3_send_alert(s,SSL3_AL_FATAL,al);
   1082 err:
   1083 	EVP_MD_CTX_cleanup(&md_ctx);
   1084 	return(-1);
   1085 	}
   1086 
   1087 int dtls1_send_certificate_request(SSL *s)
   1088 	{
   1089 	unsigned char *p,*d;
   1090 	int i,j,nl,off,n;
   1091 	STACK_OF(X509_NAME) *sk=NULL;
   1092 	X509_NAME *name;
   1093 	BUF_MEM *buf;
   1094 	unsigned int msg_len;
   1095 
   1096 	if (s->state == SSL3_ST_SW_CERT_REQ_A)
   1097 		{
   1098 		buf=s->init_buf;
   1099 
   1100 		d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
   1101 
   1102 		/* get the list of acceptable cert types */
   1103 		p++;
   1104 		n=ssl3_get_req_cert_type(s,p);
   1105 		d[0]=n;
   1106 		p+=n;
   1107 		n++;
   1108 
   1109 		off=n;
   1110 		p+=2;
   1111 		n+=2;
   1112 
   1113 		sk=SSL_get_client_CA_list(s);
   1114 		nl=0;
   1115 		if (sk != NULL)
   1116 			{
   1117 			for (i=0; i<sk_X509_NAME_num(sk); i++)
   1118 				{
   1119 				name=sk_X509_NAME_value(sk,i);
   1120 				j=i2d_X509_NAME(name,NULL);
   1121 				if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2))
   1122 					{
   1123 					SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
   1124 					goto err;
   1125 					}
   1126 				p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]);
   1127 				if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
   1128 					{
   1129 					s2n(j,p);
   1130 					i2d_X509_NAME(name,&p);
   1131 					n+=2+j;
   1132 					nl+=2+j;
   1133 					}
   1134 				else
   1135 					{
   1136 					d=p;
   1137 					i2d_X509_NAME(name,&p);
   1138 					j-=2; s2n(j,d); j+=2;
   1139 					n+=j;
   1140 					nl+=j;
   1141 					}
   1142 				}
   1143 			}
   1144 		/* else no CA names */
   1145 		p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]);
   1146 		s2n(nl,p);
   1147 
   1148 		d=(unsigned char *)buf->data;
   1149 		*(d++)=SSL3_MT_CERTIFICATE_REQUEST;
   1150 		l2n3(n,d);
   1151 		s2n(s->d1->handshake_write_seq,d);
   1152 		s->d1->handshake_write_seq++;
   1153 
   1154 		/* we should now have things packed up, so lets send
   1155 		 * it off */
   1156 
   1157 		s->init_num=n+DTLS1_HM_HEADER_LENGTH;
   1158 		s->init_off=0;
   1159 #ifdef NETSCAPE_HANG_BUG
   1160 /* XXX: what to do about this? */
   1161 		p=(unsigned char *)s->init_buf->data + s->init_num;
   1162 
   1163 		/* do the header */
   1164 		*(p++)=SSL3_MT_SERVER_DONE;
   1165 		*(p++)=0;
   1166 		*(p++)=0;
   1167 		*(p++)=0;
   1168 		s->init_num += 4;
   1169 #endif
   1170 
   1171 		/* XDTLS:  set message header ? */
   1172 		msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
   1173 		dtls1_set_message_header(s, (void *)s->init_buf->data,
   1174 			SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len);
   1175 
   1176 		/* buffer the message to handle re-xmits */
   1177 		dtls1_buffer_message(s, 0);
   1178 
   1179 		s->state = SSL3_ST_SW_CERT_REQ_B;
   1180 		}
   1181 
   1182 	/* SSL3_ST_SW_CERT_REQ_B */
   1183 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
   1184 err:
   1185 	return(-1);
   1186 	}
   1187 
   1188 int dtls1_send_server_certificate(SSL *s)
   1189 	{
   1190 	unsigned long l;
   1191 	X509 *x;
   1192 
   1193 	if (s->state == SSL3_ST_SW_CERT_A)
   1194 		{
   1195 		x=ssl_get_server_send_cert(s);
   1196 		if (x == NULL &&
   1197                         /* VRS: allow null cert if auth == KRB5 */
   1198                         (s->s3->tmp.new_cipher->algorithms
   1199                                 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
   1200                         != (SSL_aKRB5|SSL_kKRB5))
   1201 			{
   1202 			SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
   1203 			return(0);
   1204 			}
   1205 
   1206 		l=dtls1_output_cert_chain(s,x);
   1207 		s->state=SSL3_ST_SW_CERT_B;
   1208 		s->init_num=(int)l;
   1209 		s->init_off=0;
   1210 
   1211 		/* buffer the message to handle re-xmits */
   1212 		dtls1_buffer_message(s, 0);
   1213 		}
   1214 
   1215 	/* SSL3_ST_SW_CERT_B */
   1216 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
   1217 	}
   1218 
   1219 #ifndef OPENSSL_NO_TLSEXT
   1220 int dtls1_send_newsession_ticket(SSL *s)
   1221 	{
   1222 	if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
   1223 		{
   1224 		unsigned char *p, *senc, *macstart;
   1225 		int len, slen;
   1226 		unsigned int hlen, msg_len;
   1227 		EVP_CIPHER_CTX ctx;
   1228 		HMAC_CTX hctx;
   1229 		SSL_CTX *tctx = s->initial_ctx;
   1230 		unsigned char iv[EVP_MAX_IV_LENGTH];
   1231 		unsigned char key_name[16];
   1232 
   1233 		/* get session encoding length */
   1234 		slen = i2d_SSL_SESSION(s->session, NULL);
   1235 		/* Some length values are 16 bits, so forget it if session is
   1236  		 * too long
   1237  		 */
   1238 		if (slen > 0xFF00)
   1239 			return -1;
   1240 		/* Grow buffer if need be: the length calculation is as
   1241  		 * follows 12 (DTLS handshake message header) +
   1242  		 * 4 (ticket lifetime hint) + 2 (ticket length) +
   1243  		 * 16 (key name) + max_iv_len (iv length) +
   1244  		 * session_length + max_enc_block_size (max encrypted session
   1245  		 * length) + max_md_size (HMAC).
   1246  		 */
   1247 		if (!BUF_MEM_grow(s->init_buf,
   1248 			DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH +
   1249 			EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
   1250 			return -1;
   1251 		senc = OPENSSL_malloc(slen);
   1252 		if (!senc)
   1253 			return -1;
   1254 		p = senc;
   1255 		i2d_SSL_SESSION(s->session, &p);
   1256 
   1257 		p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]);
   1258 		EVP_CIPHER_CTX_init(&ctx);
   1259 		HMAC_CTX_init(&hctx);
   1260 		/* Initialize HMAC and cipher contexts. If callback present
   1261 		 * it does all the work otherwise use generated values
   1262 		 * from parent ctx.
   1263 		 */
   1264 		if (tctx->tlsext_ticket_key_cb)
   1265 			{
   1266 			if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
   1267 							 &hctx, 1) < 0)
   1268 				{
   1269 				OPENSSL_free(senc);
   1270 				return -1;
   1271 				}
   1272 			}
   1273 		else
   1274 			{
   1275 			RAND_pseudo_bytes(iv, 16);
   1276 			EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
   1277 					tctx->tlsext_tick_aes_key, iv);
   1278 			HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
   1279 					tlsext_tick_md(), NULL);
   1280 			memcpy(key_name, tctx->tlsext_tick_key_name, 16);
   1281 			}
   1282 		l2n(s->session->tlsext_tick_lifetime_hint, p);
   1283 		/* Skip ticket length for now */
   1284 		p += 2;
   1285 		/* Output key name */
   1286 		macstart = p;
   1287 		memcpy(p, key_name, 16);
   1288 		p += 16;
   1289 		/* output IV */
   1290 		memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
   1291 		p += EVP_CIPHER_CTX_iv_length(&ctx);
   1292 		/* Encrypt session data */
   1293 		EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
   1294 		p += len;
   1295 		EVP_EncryptFinal(&ctx, p, &len);
   1296 		p += len;
   1297 		EVP_CIPHER_CTX_cleanup(&ctx);
   1298 
   1299 		HMAC_Update(&hctx, macstart, p - macstart);
   1300 		HMAC_Final(&hctx, p, &hlen);
   1301 		HMAC_CTX_cleanup(&hctx);
   1302 
   1303 		p += hlen;
   1304 		/* Now write out lengths: p points to end of data written */
   1305 		/* Total length */
   1306 		len = p - (unsigned char *)(s->init_buf->data);
   1307 		/* Ticket length */
   1308 		p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4;
   1309 		s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p);
   1310 
   1311 		/* number of bytes to write */
   1312 		s->init_num= len;
   1313 		s->state=SSL3_ST_SW_SESSION_TICKET_B;
   1314 		s->init_off=0;
   1315 		OPENSSL_free(senc);
   1316 
   1317 		/* XDTLS:  set message header ? */
   1318 		msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH;
   1319 		dtls1_set_message_header(s, (void *)s->init_buf->data,
   1320 			SSL3_MT_NEWSESSION_TICKET, msg_len, 0, msg_len);
   1321 
   1322 		/* buffer the message to handle re-xmits */
   1323 		dtls1_buffer_message(s, 0);
   1324 		}
   1325 
   1326 	/* SSL3_ST_SW_SESSION_TICKET_B */
   1327 	return(dtls1_do_write(s,SSL3_RT_HANDSHAKE));
   1328 	}
   1329 #endif
   1330