1*2139Sjp161948 /* ssl/d1_srvr.c */ 2*2139Sjp161948 /* 3*2139Sjp161948 * DTLS implementation written by Nagendra Modadugu 4*2139Sjp161948 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5*2139Sjp161948 */ 6*2139Sjp161948 /* ==================================================================== 7*2139Sjp161948 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 8*2139Sjp161948 * 9*2139Sjp161948 * Redistribution and use in source and binary forms, with or without 10*2139Sjp161948 * modification, are permitted provided that the following conditions 11*2139Sjp161948 * are met: 12*2139Sjp161948 * 13*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright 14*2139Sjp161948 * notice, this list of conditions and the following disclaimer. 15*2139Sjp161948 * 16*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright 17*2139Sjp161948 * notice, this list of conditions and the following disclaimer in 18*2139Sjp161948 * the documentation and/or other materials provided with the 19*2139Sjp161948 * distribution. 20*2139Sjp161948 * 21*2139Sjp161948 * 3. All advertising materials mentioning features or use of this 22*2139Sjp161948 * software must display the following acknowledgment: 23*2139Sjp161948 * "This product includes software developed by the OpenSSL Project 24*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25*2139Sjp161948 * 26*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27*2139Sjp161948 * endorse or promote products derived from this software without 28*2139Sjp161948 * prior written permission. For written permission, please contact 29*2139Sjp161948 * openssl-core@OpenSSL.org. 30*2139Sjp161948 * 31*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL" 32*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written 33*2139Sjp161948 * permission of the OpenSSL Project. 34*2139Sjp161948 * 35*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following 36*2139Sjp161948 * acknowledgment: 37*2139Sjp161948 * "This product includes software developed by the OpenSSL Project 38*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39*2139Sjp161948 * 40*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE. 52*2139Sjp161948 * ==================================================================== 53*2139Sjp161948 * 54*2139Sjp161948 * This product includes cryptographic software written by Eric Young 55*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim 56*2139Sjp161948 * Hudson (tjh@cryptsoft.com). 57*2139Sjp161948 * 58*2139Sjp161948 */ 59*2139Sjp161948 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 60*2139Sjp161948 * All rights reserved. 61*2139Sjp161948 * 62*2139Sjp161948 * This package is an SSL implementation written 63*2139Sjp161948 * by Eric Young (eay@cryptsoft.com). 64*2139Sjp161948 * The implementation was written so as to conform with Netscapes SSL. 65*2139Sjp161948 * 66*2139Sjp161948 * This library is free for commercial and non-commercial use as long as 67*2139Sjp161948 * the following conditions are aheared to. The following conditions 68*2139Sjp161948 * apply to all code found in this distribution, be it the RC4, RSA, 69*2139Sjp161948 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 70*2139Sjp161948 * included with this distribution is covered by the same copyright terms 71*2139Sjp161948 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 72*2139Sjp161948 * 73*2139Sjp161948 * Copyright remains Eric Young's, and as such any Copyright notices in 74*2139Sjp161948 * the code are not to be removed. 75*2139Sjp161948 * If this package is used in a product, Eric Young should be given attribution 76*2139Sjp161948 * as the author of the parts of the library used. 77*2139Sjp161948 * This can be in the form of a textual message at program startup or 78*2139Sjp161948 * in documentation (online or textual) provided with the package. 79*2139Sjp161948 * 80*2139Sjp161948 * Redistribution and use in source and binary forms, with or without 81*2139Sjp161948 * modification, are permitted provided that the following conditions 82*2139Sjp161948 * are met: 83*2139Sjp161948 * 1. Redistributions of source code must retain the copyright 84*2139Sjp161948 * notice, this list of conditions and the following disclaimer. 85*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright 86*2139Sjp161948 * notice, this list of conditions and the following disclaimer in the 87*2139Sjp161948 * documentation and/or other materials provided with the distribution. 88*2139Sjp161948 * 3. All advertising materials mentioning features or use of this software 89*2139Sjp161948 * must display the following acknowledgement: 90*2139Sjp161948 * "This product includes cryptographic software written by 91*2139Sjp161948 * Eric Young (eay@cryptsoft.com)" 92*2139Sjp161948 * The word 'cryptographic' can be left out if the rouines from the library 93*2139Sjp161948 * being used are not cryptographic related :-). 94*2139Sjp161948 * 4. If you include any Windows specific code (or a derivative thereof) from 95*2139Sjp161948 * the apps directory (application code) you must include an acknowledgement: 96*2139Sjp161948 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 97*2139Sjp161948 * 98*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 99*2139Sjp161948 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 100*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 101*2139Sjp161948 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 102*2139Sjp161948 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 103*2139Sjp161948 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 104*2139Sjp161948 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 105*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 106*2139Sjp161948 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 107*2139Sjp161948 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 108*2139Sjp161948 * SUCH DAMAGE. 109*2139Sjp161948 * 110*2139Sjp161948 * The licence and distribution terms for any publically available version or 111*2139Sjp161948 * derivative of this code cannot be changed. i.e. this code cannot simply be 112*2139Sjp161948 * copied and put under another distribution licence 113*2139Sjp161948 * [including the GNU Public Licence.] 114*2139Sjp161948 */ 115*2139Sjp161948 116*2139Sjp161948 #include <stdio.h> 117*2139Sjp161948 #include "ssl_locl.h" 118*2139Sjp161948 #include <openssl/buffer.h> 119*2139Sjp161948 #include <openssl/rand.h> 120*2139Sjp161948 #include <openssl/objects.h> 121*2139Sjp161948 #include <openssl/evp.h> 122*2139Sjp161948 #include <openssl/x509.h> 123*2139Sjp161948 #include <openssl/md5.h> 124*2139Sjp161948 #ifndef OPENSSL_NO_DH 125*2139Sjp161948 #include <openssl/dh.h> 126*2139Sjp161948 #endif 127*2139Sjp161948 128*2139Sjp161948 static SSL_METHOD *dtls1_get_server_method(int ver); 129*2139Sjp161948 static int dtls1_send_hello_verify_request(SSL *s); 130*2139Sjp161948 131*2139Sjp161948 static SSL_METHOD *dtls1_get_server_method(int ver) 132*2139Sjp161948 { 133*2139Sjp161948 if (ver == DTLS1_VERSION) 134*2139Sjp161948 return(DTLSv1_server_method()); 135*2139Sjp161948 else 136*2139Sjp161948 return(NULL); 137*2139Sjp161948 } 138*2139Sjp161948 139*2139Sjp161948 IMPLEMENT_dtls1_meth_func(DTLSv1_server_method, 140*2139Sjp161948 dtls1_accept, 141*2139Sjp161948 ssl_undefined_function, 142*2139Sjp161948 dtls1_get_server_method) 143*2139Sjp161948 144*2139Sjp161948 int dtls1_accept(SSL *s) 145*2139Sjp161948 { 146*2139Sjp161948 BUF_MEM *buf; 147*2139Sjp161948 unsigned long l,Time=time(NULL); 148*2139Sjp161948 void (*cb)(const SSL *ssl,int type,int val)=NULL; 149*2139Sjp161948 long num1; 150*2139Sjp161948 int ret= -1; 151*2139Sjp161948 int new_state,state,skip=0; 152*2139Sjp161948 153*2139Sjp161948 RAND_add(&Time,sizeof(Time),0); 154*2139Sjp161948 ERR_clear_error(); 155*2139Sjp161948 clear_sys_error(); 156*2139Sjp161948 157*2139Sjp161948 if (s->info_callback != NULL) 158*2139Sjp161948 cb=s->info_callback; 159*2139Sjp161948 else if (s->ctx->info_callback != NULL) 160*2139Sjp161948 cb=s->ctx->info_callback; 161*2139Sjp161948 162*2139Sjp161948 /* init things to blank */ 163*2139Sjp161948 s->in_handshake++; 164*2139Sjp161948 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 165*2139Sjp161948 166*2139Sjp161948 if (s->cert == NULL) 167*2139Sjp161948 { 168*2139Sjp161948 SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET); 169*2139Sjp161948 return(-1); 170*2139Sjp161948 } 171*2139Sjp161948 172*2139Sjp161948 for (;;) 173*2139Sjp161948 { 174*2139Sjp161948 state=s->state; 175*2139Sjp161948 176*2139Sjp161948 switch (s->state) 177*2139Sjp161948 { 178*2139Sjp161948 case SSL_ST_RENEGOTIATE: 179*2139Sjp161948 s->new_session=1; 180*2139Sjp161948 /* s->state=SSL_ST_ACCEPT; */ 181*2139Sjp161948 182*2139Sjp161948 case SSL_ST_BEFORE: 183*2139Sjp161948 case SSL_ST_ACCEPT: 184*2139Sjp161948 case SSL_ST_BEFORE|SSL_ST_ACCEPT: 185*2139Sjp161948 case SSL_ST_OK|SSL_ST_ACCEPT: 186*2139Sjp161948 187*2139Sjp161948 s->server=1; 188*2139Sjp161948 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 189*2139Sjp161948 190*2139Sjp161948 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) 191*2139Sjp161948 { 192*2139Sjp161948 SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR); 193*2139Sjp161948 return -1; 194*2139Sjp161948 } 195*2139Sjp161948 s->type=SSL_ST_ACCEPT; 196*2139Sjp161948 197*2139Sjp161948 if (s->init_buf == NULL) 198*2139Sjp161948 { 199*2139Sjp161948 if ((buf=BUF_MEM_new()) == NULL) 200*2139Sjp161948 { 201*2139Sjp161948 ret= -1; 202*2139Sjp161948 goto end; 203*2139Sjp161948 } 204*2139Sjp161948 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 205*2139Sjp161948 { 206*2139Sjp161948 ret= -1; 207*2139Sjp161948 goto end; 208*2139Sjp161948 } 209*2139Sjp161948 s->init_buf=buf; 210*2139Sjp161948 } 211*2139Sjp161948 212*2139Sjp161948 if (!ssl3_setup_buffers(s)) 213*2139Sjp161948 { 214*2139Sjp161948 ret= -1; 215*2139Sjp161948 goto end; 216*2139Sjp161948 } 217*2139Sjp161948 218*2139Sjp161948 s->init_num=0; 219*2139Sjp161948 220*2139Sjp161948 if (s->state != SSL_ST_RENEGOTIATE) 221*2139Sjp161948 { 222*2139Sjp161948 /* Ok, we now need to push on a buffering BIO so that 223*2139Sjp161948 * the output is sent in a way that TCP likes :-) 224*2139Sjp161948 */ 225*2139Sjp161948 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } 226*2139Sjp161948 227*2139Sjp161948 ssl3_init_finished_mac(s); 228*2139Sjp161948 s->state=SSL3_ST_SR_CLNT_HELLO_A; 229*2139Sjp161948 s->ctx->stats.sess_accept++; 230*2139Sjp161948 } 231*2139Sjp161948 else 232*2139Sjp161948 { 233*2139Sjp161948 /* s->state == SSL_ST_RENEGOTIATE, 234*2139Sjp161948 * we will just send a HelloRequest */ 235*2139Sjp161948 s->ctx->stats.sess_accept_renegotiate++; 236*2139Sjp161948 s->state=SSL3_ST_SW_HELLO_REQ_A; 237*2139Sjp161948 } 238*2139Sjp161948 239*2139Sjp161948 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) 240*2139Sjp161948 s->d1->send_cookie = 1; 241*2139Sjp161948 else 242*2139Sjp161948 s->d1->send_cookie = 0; 243*2139Sjp161948 244*2139Sjp161948 break; 245*2139Sjp161948 246*2139Sjp161948 case SSL3_ST_SW_HELLO_REQ_A: 247*2139Sjp161948 case SSL3_ST_SW_HELLO_REQ_B: 248*2139Sjp161948 249*2139Sjp161948 s->shutdown=0; 250*2139Sjp161948 ret=dtls1_send_hello_request(s); 251*2139Sjp161948 if (ret <= 0) goto end; 252*2139Sjp161948 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; 253*2139Sjp161948 s->state=SSL3_ST_SW_FLUSH; 254*2139Sjp161948 s->init_num=0; 255*2139Sjp161948 256*2139Sjp161948 ssl3_init_finished_mac(s); 257*2139Sjp161948 break; 258*2139Sjp161948 259*2139Sjp161948 case SSL3_ST_SW_HELLO_REQ_C: 260*2139Sjp161948 s->state=SSL_ST_OK; 261*2139Sjp161948 break; 262*2139Sjp161948 263*2139Sjp161948 case SSL3_ST_SR_CLNT_HELLO_A: 264*2139Sjp161948 case SSL3_ST_SR_CLNT_HELLO_B: 265*2139Sjp161948 case SSL3_ST_SR_CLNT_HELLO_C: 266*2139Sjp161948 267*2139Sjp161948 s->shutdown=0; 268*2139Sjp161948 ret=ssl3_get_client_hello(s); 269*2139Sjp161948 if (ret <= 0) goto end; 270*2139Sjp161948 s->new_session = 2; 271*2139Sjp161948 272*2139Sjp161948 if ( s->d1->send_cookie) 273*2139Sjp161948 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; 274*2139Sjp161948 else 275*2139Sjp161948 s->state = SSL3_ST_SW_SRVR_HELLO_A; 276*2139Sjp161948 277*2139Sjp161948 s->init_num=0; 278*2139Sjp161948 break; 279*2139Sjp161948 280*2139Sjp161948 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: 281*2139Sjp161948 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: 282*2139Sjp161948 283*2139Sjp161948 ret = dtls1_send_hello_verify_request(s); 284*2139Sjp161948 if ( ret <= 0) goto end; 285*2139Sjp161948 s->d1->send_cookie = 0; 286*2139Sjp161948 s->state=SSL3_ST_SW_FLUSH; 287*2139Sjp161948 s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; 288*2139Sjp161948 break; 289*2139Sjp161948 290*2139Sjp161948 case SSL3_ST_SW_SRVR_HELLO_A: 291*2139Sjp161948 case SSL3_ST_SW_SRVR_HELLO_B: 292*2139Sjp161948 ret=dtls1_send_server_hello(s); 293*2139Sjp161948 if (ret <= 0) goto end; 294*2139Sjp161948 295*2139Sjp161948 if (s->hit) 296*2139Sjp161948 s->state=SSL3_ST_SW_CHANGE_A; 297*2139Sjp161948 else 298*2139Sjp161948 s->state=SSL3_ST_SW_CERT_A; 299*2139Sjp161948 s->init_num=0; 300*2139Sjp161948 break; 301*2139Sjp161948 302*2139Sjp161948 case SSL3_ST_SW_CERT_A: 303*2139Sjp161948 case SSL3_ST_SW_CERT_B: 304*2139Sjp161948 /* Check if it is anon DH */ 305*2139Sjp161948 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 306*2139Sjp161948 { 307*2139Sjp161948 ret=dtls1_send_server_certificate(s); 308*2139Sjp161948 if (ret <= 0) goto end; 309*2139Sjp161948 } 310*2139Sjp161948 else 311*2139Sjp161948 skip=1; 312*2139Sjp161948 s->state=SSL3_ST_SW_KEY_EXCH_A; 313*2139Sjp161948 s->init_num=0; 314*2139Sjp161948 break; 315*2139Sjp161948 316*2139Sjp161948 case SSL3_ST_SW_KEY_EXCH_A: 317*2139Sjp161948 case SSL3_ST_SW_KEY_EXCH_B: 318*2139Sjp161948 l=s->s3->tmp.new_cipher->algorithms; 319*2139Sjp161948 320*2139Sjp161948 /* clear this, it may get reset by 321*2139Sjp161948 * send_server_key_exchange */ 322*2139Sjp161948 if ((s->options & SSL_OP_EPHEMERAL_RSA) 323*2139Sjp161948 #ifndef OPENSSL_NO_KRB5 324*2139Sjp161948 && !(l & SSL_KRB5) 325*2139Sjp161948 #endif /* OPENSSL_NO_KRB5 */ 326*2139Sjp161948 ) 327*2139Sjp161948 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key 328*2139Sjp161948 * even when forbidden by protocol specs 329*2139Sjp161948 * (handshake may fail as clients are not required to 330*2139Sjp161948 * be able to handle this) */ 331*2139Sjp161948 s->s3->tmp.use_rsa_tmp=1; 332*2139Sjp161948 else 333*2139Sjp161948 s->s3->tmp.use_rsa_tmp=0; 334*2139Sjp161948 335*2139Sjp161948 /* only send if a DH key exchange, fortezza or 336*2139Sjp161948 * RSA but we have a sign only certificate */ 337*2139Sjp161948 if (s->s3->tmp.use_rsa_tmp 338*2139Sjp161948 || (l & (SSL_DH|SSL_kFZA)) 339*2139Sjp161948 || ((l & SSL_kRSA) 340*2139Sjp161948 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL 341*2139Sjp161948 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) 342*2139Sjp161948 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) 343*2139Sjp161948 ) 344*2139Sjp161948 ) 345*2139Sjp161948 ) 346*2139Sjp161948 ) 347*2139Sjp161948 { 348*2139Sjp161948 ret=dtls1_send_server_key_exchange(s); 349*2139Sjp161948 if (ret <= 0) goto end; 350*2139Sjp161948 } 351*2139Sjp161948 else 352*2139Sjp161948 skip=1; 353*2139Sjp161948 354*2139Sjp161948 s->state=SSL3_ST_SW_CERT_REQ_A; 355*2139Sjp161948 s->init_num=0; 356*2139Sjp161948 break; 357*2139Sjp161948 358*2139Sjp161948 case SSL3_ST_SW_CERT_REQ_A: 359*2139Sjp161948 case SSL3_ST_SW_CERT_REQ_B: 360*2139Sjp161948 if (/* don't request cert unless asked for it: */ 361*2139Sjp161948 !(s->verify_mode & SSL_VERIFY_PEER) || 362*2139Sjp161948 /* if SSL_VERIFY_CLIENT_ONCE is set, 363*2139Sjp161948 * don't request cert during re-negotiation: */ 364*2139Sjp161948 ((s->session->peer != NULL) && 365*2139Sjp161948 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 366*2139Sjp161948 /* never request cert in anonymous ciphersuites 367*2139Sjp161948 * (see section "Certificate request" in SSL 3 drafts 368*2139Sjp161948 * and in RFC 2246): */ 369*2139Sjp161948 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && 370*2139Sjp161948 /* ... except when the application insists on verification 371*2139Sjp161948 * (against the specs, but s3_clnt.c accepts this for SSL 3) */ 372*2139Sjp161948 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || 373*2139Sjp161948 /* never request cert in Kerberos ciphersuites */ 374*2139Sjp161948 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) 375*2139Sjp161948 { 376*2139Sjp161948 /* no cert request */ 377*2139Sjp161948 skip=1; 378*2139Sjp161948 s->s3->tmp.cert_request=0; 379*2139Sjp161948 s->state=SSL3_ST_SW_SRVR_DONE_A; 380*2139Sjp161948 } 381*2139Sjp161948 else 382*2139Sjp161948 { 383*2139Sjp161948 s->s3->tmp.cert_request=1; 384*2139Sjp161948 ret=dtls1_send_certificate_request(s); 385*2139Sjp161948 if (ret <= 0) goto end; 386*2139Sjp161948 #ifndef NETSCAPE_HANG_BUG 387*2139Sjp161948 s->state=SSL3_ST_SW_SRVR_DONE_A; 388*2139Sjp161948 #else 389*2139Sjp161948 s->state=SSL3_ST_SW_FLUSH; 390*2139Sjp161948 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 391*2139Sjp161948 #endif 392*2139Sjp161948 s->init_num=0; 393*2139Sjp161948 } 394*2139Sjp161948 break; 395*2139Sjp161948 396*2139Sjp161948 case SSL3_ST_SW_SRVR_DONE_A: 397*2139Sjp161948 case SSL3_ST_SW_SRVR_DONE_B: 398*2139Sjp161948 ret=dtls1_send_server_done(s); 399*2139Sjp161948 if (ret <= 0) goto end; 400*2139Sjp161948 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 401*2139Sjp161948 s->state=SSL3_ST_SW_FLUSH; 402*2139Sjp161948 s->init_num=0; 403*2139Sjp161948 break; 404*2139Sjp161948 405*2139Sjp161948 case SSL3_ST_SW_FLUSH: 406*2139Sjp161948 /* number of bytes to be flushed */ 407*2139Sjp161948 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); 408*2139Sjp161948 if (num1 > 0) 409*2139Sjp161948 { 410*2139Sjp161948 s->rwstate=SSL_WRITING; 411*2139Sjp161948 num1=BIO_flush(s->wbio); 412*2139Sjp161948 if (num1 <= 0) { ret= -1; goto end; } 413*2139Sjp161948 s->rwstate=SSL_NOTHING; 414*2139Sjp161948 } 415*2139Sjp161948 416*2139Sjp161948 s->state=s->s3->tmp.next_state; 417*2139Sjp161948 break; 418*2139Sjp161948 419*2139Sjp161948 case SSL3_ST_SR_CERT_A: 420*2139Sjp161948 case SSL3_ST_SR_CERT_B: 421*2139Sjp161948 /* Check for second client hello (MS SGC) */ 422*2139Sjp161948 ret = ssl3_check_client_hello(s); 423*2139Sjp161948 if (ret <= 0) 424*2139Sjp161948 goto end; 425*2139Sjp161948 if (ret == 2) 426*2139Sjp161948 s->state = SSL3_ST_SR_CLNT_HELLO_C; 427*2139Sjp161948 else { 428*2139Sjp161948 /* could be sent for a DH cert, even if we 429*2139Sjp161948 * have not asked for it :-) */ 430*2139Sjp161948 ret=ssl3_get_client_certificate(s); 431*2139Sjp161948 if (ret <= 0) goto end; 432*2139Sjp161948 s->init_num=0; 433*2139Sjp161948 s->state=SSL3_ST_SR_KEY_EXCH_A; 434*2139Sjp161948 } 435*2139Sjp161948 break; 436*2139Sjp161948 437*2139Sjp161948 case SSL3_ST_SR_KEY_EXCH_A: 438*2139Sjp161948 case SSL3_ST_SR_KEY_EXCH_B: 439*2139Sjp161948 ret=ssl3_get_client_key_exchange(s); 440*2139Sjp161948 if (ret <= 0) goto end; 441*2139Sjp161948 s->state=SSL3_ST_SR_CERT_VRFY_A; 442*2139Sjp161948 s->init_num=0; 443*2139Sjp161948 444*2139Sjp161948 /* We need to get hashes here so if there is 445*2139Sjp161948 * a client cert, it can be verified */ 446*2139Sjp161948 s->method->ssl3_enc->cert_verify_mac(s, 447*2139Sjp161948 &(s->s3->finish_dgst1), 448*2139Sjp161948 &(s->s3->tmp.cert_verify_md[0])); 449*2139Sjp161948 s->method->ssl3_enc->cert_verify_mac(s, 450*2139Sjp161948 &(s->s3->finish_dgst2), 451*2139Sjp161948 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); 452*2139Sjp161948 453*2139Sjp161948 break; 454*2139Sjp161948 455*2139Sjp161948 case SSL3_ST_SR_CERT_VRFY_A: 456*2139Sjp161948 case SSL3_ST_SR_CERT_VRFY_B: 457*2139Sjp161948 458*2139Sjp161948 /* we should decide if we expected this one */ 459*2139Sjp161948 ret=ssl3_get_cert_verify(s); 460*2139Sjp161948 if (ret <= 0) goto end; 461*2139Sjp161948 462*2139Sjp161948 s->state=SSL3_ST_SR_FINISHED_A; 463*2139Sjp161948 s->init_num=0; 464*2139Sjp161948 break; 465*2139Sjp161948 466*2139Sjp161948 case SSL3_ST_SR_FINISHED_A: 467*2139Sjp161948 case SSL3_ST_SR_FINISHED_B: 468*2139Sjp161948 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, 469*2139Sjp161948 SSL3_ST_SR_FINISHED_B); 470*2139Sjp161948 if (ret <= 0) goto end; 471*2139Sjp161948 if (s->hit) 472*2139Sjp161948 s->state=SSL_ST_OK; 473*2139Sjp161948 else 474*2139Sjp161948 s->state=SSL3_ST_SW_CHANGE_A; 475*2139Sjp161948 s->init_num=0; 476*2139Sjp161948 break; 477*2139Sjp161948 478*2139Sjp161948 case SSL3_ST_SW_CHANGE_A: 479*2139Sjp161948 case SSL3_ST_SW_CHANGE_B: 480*2139Sjp161948 481*2139Sjp161948 s->session->cipher=s->s3->tmp.new_cipher; 482*2139Sjp161948 if (!s->method->ssl3_enc->setup_key_block(s)) 483*2139Sjp161948 { ret= -1; goto end; } 484*2139Sjp161948 485*2139Sjp161948 ret=dtls1_send_change_cipher_spec(s, 486*2139Sjp161948 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); 487*2139Sjp161948 488*2139Sjp161948 if (ret <= 0) goto end; 489*2139Sjp161948 s->state=SSL3_ST_SW_FINISHED_A; 490*2139Sjp161948 s->init_num=0; 491*2139Sjp161948 492*2139Sjp161948 if (!s->method->ssl3_enc->change_cipher_state(s, 493*2139Sjp161948 SSL3_CHANGE_CIPHER_SERVER_WRITE)) 494*2139Sjp161948 { 495*2139Sjp161948 ret= -1; 496*2139Sjp161948 goto end; 497*2139Sjp161948 } 498*2139Sjp161948 499*2139Sjp161948 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); 500*2139Sjp161948 break; 501*2139Sjp161948 502*2139Sjp161948 case SSL3_ST_SW_FINISHED_A: 503*2139Sjp161948 case SSL3_ST_SW_FINISHED_B: 504*2139Sjp161948 ret=dtls1_send_finished(s, 505*2139Sjp161948 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, 506*2139Sjp161948 s->method->ssl3_enc->server_finished_label, 507*2139Sjp161948 s->method->ssl3_enc->server_finished_label_len); 508*2139Sjp161948 if (ret <= 0) goto end; 509*2139Sjp161948 s->state=SSL3_ST_SW_FLUSH; 510*2139Sjp161948 if (s->hit) 511*2139Sjp161948 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; 512*2139Sjp161948 else 513*2139Sjp161948 s->s3->tmp.next_state=SSL_ST_OK; 514*2139Sjp161948 s->init_num=0; 515*2139Sjp161948 break; 516*2139Sjp161948 517*2139Sjp161948 case SSL_ST_OK: 518*2139Sjp161948 /* clean a few things up */ 519*2139Sjp161948 ssl3_cleanup_key_block(s); 520*2139Sjp161948 521*2139Sjp161948 #if 0 522*2139Sjp161948 BUF_MEM_free(s->init_buf); 523*2139Sjp161948 s->init_buf=NULL; 524*2139Sjp161948 #endif 525*2139Sjp161948 526*2139Sjp161948 /* remove buffering on output */ 527*2139Sjp161948 ssl_free_wbio_buffer(s); 528*2139Sjp161948 529*2139Sjp161948 s->init_num=0; 530*2139Sjp161948 531*2139Sjp161948 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */ 532*2139Sjp161948 { 533*2139Sjp161948 /* actually not necessarily a 'new' session unless 534*2139Sjp161948 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */ 535*2139Sjp161948 536*2139Sjp161948 s->new_session=0; 537*2139Sjp161948 538*2139Sjp161948 ssl_update_cache(s,SSL_SESS_CACHE_SERVER); 539*2139Sjp161948 540*2139Sjp161948 s->ctx->stats.sess_accept_good++; 541*2139Sjp161948 /* s->server=1; */ 542*2139Sjp161948 s->handshake_func=dtls1_accept; 543*2139Sjp161948 544*2139Sjp161948 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 545*2139Sjp161948 } 546*2139Sjp161948 547*2139Sjp161948 ret = 1; 548*2139Sjp161948 549*2139Sjp161948 /* done handshaking, next message is client hello */ 550*2139Sjp161948 s->d1->handshake_read_seq = 0; 551*2139Sjp161948 /* next message is server hello */ 552*2139Sjp161948 s->d1->handshake_write_seq = 0; 553*2139Sjp161948 goto end; 554*2139Sjp161948 /* break; */ 555*2139Sjp161948 556*2139Sjp161948 default: 557*2139Sjp161948 SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE); 558*2139Sjp161948 ret= -1; 559*2139Sjp161948 goto end; 560*2139Sjp161948 /* break; */ 561*2139Sjp161948 } 562*2139Sjp161948 563*2139Sjp161948 if (!s->s3->tmp.reuse_message && !skip) 564*2139Sjp161948 { 565*2139Sjp161948 if (s->debug) 566*2139Sjp161948 { 567*2139Sjp161948 if ((ret=BIO_flush(s->wbio)) <= 0) 568*2139Sjp161948 goto end; 569*2139Sjp161948 } 570*2139Sjp161948 571*2139Sjp161948 572*2139Sjp161948 if ((cb != NULL) && (s->state != state)) 573*2139Sjp161948 { 574*2139Sjp161948 new_state=s->state; 575*2139Sjp161948 s->state=state; 576*2139Sjp161948 cb(s,SSL_CB_ACCEPT_LOOP,1); 577*2139Sjp161948 s->state=new_state; 578*2139Sjp161948 } 579*2139Sjp161948 } 580*2139Sjp161948 skip=0; 581*2139Sjp161948 } 582*2139Sjp161948 end: 583*2139Sjp161948 /* BIO_flush(s->wbio); */ 584*2139Sjp161948 585*2139Sjp161948 s->in_handshake--; 586*2139Sjp161948 if (cb != NULL) 587*2139Sjp161948 cb(s,SSL_CB_ACCEPT_EXIT,ret); 588*2139Sjp161948 return(ret); 589*2139Sjp161948 } 590*2139Sjp161948 591*2139Sjp161948 int dtls1_send_hello_request(SSL *s) 592*2139Sjp161948 { 593*2139Sjp161948 unsigned char *p; 594*2139Sjp161948 595*2139Sjp161948 if (s->state == SSL3_ST_SW_HELLO_REQ_A) 596*2139Sjp161948 { 597*2139Sjp161948 p=(unsigned char *)s->init_buf->data; 598*2139Sjp161948 p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0); 599*2139Sjp161948 600*2139Sjp161948 s->state=SSL3_ST_SW_HELLO_REQ_B; 601*2139Sjp161948 /* number of bytes to write */ 602*2139Sjp161948 s->init_num=DTLS1_HM_HEADER_LENGTH; 603*2139Sjp161948 s->init_off=0; 604*2139Sjp161948 605*2139Sjp161948 /* no need to buffer this message, since there are no retransmit 606*2139Sjp161948 * requests for it */ 607*2139Sjp161948 } 608*2139Sjp161948 609*2139Sjp161948 /* SSL3_ST_SW_HELLO_REQ_B */ 610*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 611*2139Sjp161948 } 612*2139Sjp161948 613*2139Sjp161948 int dtls1_send_hello_verify_request(SSL *s) 614*2139Sjp161948 { 615*2139Sjp161948 unsigned int msg_len; 616*2139Sjp161948 unsigned char *msg, *buf, *p; 617*2139Sjp161948 618*2139Sjp161948 if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) 619*2139Sjp161948 { 620*2139Sjp161948 buf = (unsigned char *)s->init_buf->data; 621*2139Sjp161948 622*2139Sjp161948 msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); 623*2139Sjp161948 *(p++) = s->version >> 8; 624*2139Sjp161948 *(p++) = s->version & 0xFF; 625*2139Sjp161948 626*2139Sjp161948 *(p++) = (unsigned char) s->d1->cookie_len; 627*2139Sjp161948 if ( s->ctx->app_gen_cookie_cb != NULL && 628*2139Sjp161948 s->ctx->app_gen_cookie_cb(s, s->d1->cookie, 629*2139Sjp161948 &(s->d1->cookie_len)) == 0) 630*2139Sjp161948 { 631*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR); 632*2139Sjp161948 return 0; 633*2139Sjp161948 } 634*2139Sjp161948 /* else the cookie is assumed to have 635*2139Sjp161948 * been initialized by the application */ 636*2139Sjp161948 637*2139Sjp161948 memcpy(p, s->d1->cookie, s->d1->cookie_len); 638*2139Sjp161948 p += s->d1->cookie_len; 639*2139Sjp161948 msg_len = p - msg; 640*2139Sjp161948 641*2139Sjp161948 dtls1_set_message_header(s, buf, 642*2139Sjp161948 DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len); 643*2139Sjp161948 644*2139Sjp161948 s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; 645*2139Sjp161948 /* number of bytes to write */ 646*2139Sjp161948 s->init_num=p-buf; 647*2139Sjp161948 s->init_off=0; 648*2139Sjp161948 649*2139Sjp161948 /* buffer the message to handle re-xmits */ 650*2139Sjp161948 dtls1_buffer_message(s, 0); 651*2139Sjp161948 } 652*2139Sjp161948 653*2139Sjp161948 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ 654*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 655*2139Sjp161948 } 656*2139Sjp161948 657*2139Sjp161948 int dtls1_send_server_hello(SSL *s) 658*2139Sjp161948 { 659*2139Sjp161948 unsigned char *buf; 660*2139Sjp161948 unsigned char *p,*d; 661*2139Sjp161948 int i; 662*2139Sjp161948 unsigned int sl; 663*2139Sjp161948 unsigned long l,Time; 664*2139Sjp161948 665*2139Sjp161948 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 666*2139Sjp161948 { 667*2139Sjp161948 buf=(unsigned char *)s->init_buf->data; 668*2139Sjp161948 p=s->s3->server_random; 669*2139Sjp161948 Time=time(NULL); /* Time */ 670*2139Sjp161948 l2n(Time,p); 671*2139Sjp161948 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); 672*2139Sjp161948 /* Do the message type and length last */ 673*2139Sjp161948 d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); 674*2139Sjp161948 675*2139Sjp161948 *(p++)=s->version>>8; 676*2139Sjp161948 *(p++)=s->version&0xff; 677*2139Sjp161948 678*2139Sjp161948 /* Random stuff */ 679*2139Sjp161948 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 680*2139Sjp161948 p+=SSL3_RANDOM_SIZE; 681*2139Sjp161948 682*2139Sjp161948 /* now in theory we have 3 options to sending back the 683*2139Sjp161948 * session id. If it is a re-use, we send back the 684*2139Sjp161948 * old session-id, if it is a new session, we send 685*2139Sjp161948 * back the new session-id or we send back a 0 length 686*2139Sjp161948 * session-id if we want it to be single use. 687*2139Sjp161948 * Currently I will not implement the '0' length session-id 688*2139Sjp161948 * 12-Jan-98 - I'll now support the '0' length stuff. 689*2139Sjp161948 */ 690*2139Sjp161948 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) 691*2139Sjp161948 s->session->session_id_length=0; 692*2139Sjp161948 693*2139Sjp161948 sl=s->session->session_id_length; 694*2139Sjp161948 if (sl > sizeof s->session->session_id) 695*2139Sjp161948 { 696*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 697*2139Sjp161948 return -1; 698*2139Sjp161948 } 699*2139Sjp161948 *(p++)=sl; 700*2139Sjp161948 memcpy(p,s->session->session_id,sl); 701*2139Sjp161948 p+=sl; 702*2139Sjp161948 703*2139Sjp161948 /* put the cipher */ 704*2139Sjp161948 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); 705*2139Sjp161948 p+=i; 706*2139Sjp161948 707*2139Sjp161948 /* put the compression method */ 708*2139Sjp161948 #ifdef OPENSSL_NO_COMP 709*2139Sjp161948 *(p++)=0; 710*2139Sjp161948 #else 711*2139Sjp161948 if (s->s3->tmp.new_compression == NULL) 712*2139Sjp161948 *(p++)=0; 713*2139Sjp161948 else 714*2139Sjp161948 *(p++)=s->s3->tmp.new_compression->id; 715*2139Sjp161948 #endif 716*2139Sjp161948 717*2139Sjp161948 /* do the header */ 718*2139Sjp161948 l=(p-d); 719*2139Sjp161948 d=buf; 720*2139Sjp161948 721*2139Sjp161948 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l); 722*2139Sjp161948 723*2139Sjp161948 s->state=SSL3_ST_CW_CLNT_HELLO_B; 724*2139Sjp161948 /* number of bytes to write */ 725*2139Sjp161948 s->init_num=p-buf; 726*2139Sjp161948 s->init_off=0; 727*2139Sjp161948 728*2139Sjp161948 /* buffer the message to handle re-xmits */ 729*2139Sjp161948 dtls1_buffer_message(s, 0); 730*2139Sjp161948 } 731*2139Sjp161948 732*2139Sjp161948 /* SSL3_ST_CW_CLNT_HELLO_B */ 733*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 734*2139Sjp161948 } 735*2139Sjp161948 736*2139Sjp161948 int dtls1_send_server_done(SSL *s) 737*2139Sjp161948 { 738*2139Sjp161948 unsigned char *p; 739*2139Sjp161948 740*2139Sjp161948 if (s->state == SSL3_ST_SW_SRVR_DONE_A) 741*2139Sjp161948 { 742*2139Sjp161948 p=(unsigned char *)s->init_buf->data; 743*2139Sjp161948 744*2139Sjp161948 /* do the header */ 745*2139Sjp161948 p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0); 746*2139Sjp161948 747*2139Sjp161948 s->state=SSL3_ST_SW_SRVR_DONE_B; 748*2139Sjp161948 /* number of bytes to write */ 749*2139Sjp161948 s->init_num=DTLS1_HM_HEADER_LENGTH; 750*2139Sjp161948 s->init_off=0; 751*2139Sjp161948 752*2139Sjp161948 /* buffer the message to handle re-xmits */ 753*2139Sjp161948 dtls1_buffer_message(s, 0); 754*2139Sjp161948 } 755*2139Sjp161948 756*2139Sjp161948 /* SSL3_ST_CW_CLNT_HELLO_B */ 757*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 758*2139Sjp161948 } 759*2139Sjp161948 760*2139Sjp161948 int dtls1_send_server_key_exchange(SSL *s) 761*2139Sjp161948 { 762*2139Sjp161948 #ifndef OPENSSL_NO_RSA 763*2139Sjp161948 unsigned char *q; 764*2139Sjp161948 int j,num; 765*2139Sjp161948 RSA *rsa; 766*2139Sjp161948 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 767*2139Sjp161948 unsigned int u; 768*2139Sjp161948 #endif 769*2139Sjp161948 #ifndef OPENSSL_NO_DH 770*2139Sjp161948 DH *dh=NULL,*dhp; 771*2139Sjp161948 #endif 772*2139Sjp161948 EVP_PKEY *pkey; 773*2139Sjp161948 unsigned char *p,*d; 774*2139Sjp161948 int al,i; 775*2139Sjp161948 unsigned long type; 776*2139Sjp161948 int n; 777*2139Sjp161948 CERT *cert; 778*2139Sjp161948 BIGNUM *r[4]; 779*2139Sjp161948 int nr[4],kn; 780*2139Sjp161948 BUF_MEM *buf; 781*2139Sjp161948 EVP_MD_CTX md_ctx; 782*2139Sjp161948 783*2139Sjp161948 EVP_MD_CTX_init(&md_ctx); 784*2139Sjp161948 if (s->state == SSL3_ST_SW_KEY_EXCH_A) 785*2139Sjp161948 { 786*2139Sjp161948 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; 787*2139Sjp161948 cert=s->cert; 788*2139Sjp161948 789*2139Sjp161948 buf=s->init_buf; 790*2139Sjp161948 791*2139Sjp161948 r[0]=r[1]=r[2]=r[3]=NULL; 792*2139Sjp161948 n=0; 793*2139Sjp161948 #ifndef OPENSSL_NO_RSA 794*2139Sjp161948 if (type & SSL_kRSA) 795*2139Sjp161948 { 796*2139Sjp161948 rsa=cert->rsa_tmp; 797*2139Sjp161948 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) 798*2139Sjp161948 { 799*2139Sjp161948 rsa=s->cert->rsa_tmp_cb(s, 800*2139Sjp161948 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 801*2139Sjp161948 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 802*2139Sjp161948 if(rsa == NULL) 803*2139Sjp161948 { 804*2139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 805*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); 806*2139Sjp161948 goto f_err; 807*2139Sjp161948 } 808*2139Sjp161948 RSA_up_ref(rsa); 809*2139Sjp161948 cert->rsa_tmp=rsa; 810*2139Sjp161948 } 811*2139Sjp161948 if (rsa == NULL) 812*2139Sjp161948 { 813*2139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 814*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); 815*2139Sjp161948 goto f_err; 816*2139Sjp161948 } 817*2139Sjp161948 r[0]=rsa->n; 818*2139Sjp161948 r[1]=rsa->e; 819*2139Sjp161948 s->s3->tmp.use_rsa_tmp=1; 820*2139Sjp161948 } 821*2139Sjp161948 else 822*2139Sjp161948 #endif 823*2139Sjp161948 #ifndef OPENSSL_NO_DH 824*2139Sjp161948 if (type & SSL_kEDH) 825*2139Sjp161948 { 826*2139Sjp161948 dhp=cert->dh_tmp; 827*2139Sjp161948 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) 828*2139Sjp161948 dhp=s->cert->dh_tmp_cb(s, 829*2139Sjp161948 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 830*2139Sjp161948 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 831*2139Sjp161948 if (dhp == NULL) 832*2139Sjp161948 { 833*2139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 834*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 835*2139Sjp161948 goto f_err; 836*2139Sjp161948 } 837*2139Sjp161948 838*2139Sjp161948 if (s->s3->tmp.dh != NULL) 839*2139Sjp161948 { 840*2139Sjp161948 DH_free(dh); 841*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 842*2139Sjp161948 goto err; 843*2139Sjp161948 } 844*2139Sjp161948 845*2139Sjp161948 if ((dh=DHparams_dup(dhp)) == NULL) 846*2139Sjp161948 { 847*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 848*2139Sjp161948 goto err; 849*2139Sjp161948 } 850*2139Sjp161948 851*2139Sjp161948 s->s3->tmp.dh=dh; 852*2139Sjp161948 if ((dhp->pub_key == NULL || 853*2139Sjp161948 dhp->priv_key == NULL || 854*2139Sjp161948 (s->options & SSL_OP_SINGLE_DH_USE))) 855*2139Sjp161948 { 856*2139Sjp161948 if(!DH_generate_key(dh)) 857*2139Sjp161948 { 858*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, 859*2139Sjp161948 ERR_R_DH_LIB); 860*2139Sjp161948 goto err; 861*2139Sjp161948 } 862*2139Sjp161948 } 863*2139Sjp161948 else 864*2139Sjp161948 { 865*2139Sjp161948 dh->pub_key=BN_dup(dhp->pub_key); 866*2139Sjp161948 dh->priv_key=BN_dup(dhp->priv_key); 867*2139Sjp161948 if ((dh->pub_key == NULL) || 868*2139Sjp161948 (dh->priv_key == NULL)) 869*2139Sjp161948 { 870*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 871*2139Sjp161948 goto err; 872*2139Sjp161948 } 873*2139Sjp161948 } 874*2139Sjp161948 r[0]=dh->p; 875*2139Sjp161948 r[1]=dh->g; 876*2139Sjp161948 r[2]=dh->pub_key; 877*2139Sjp161948 } 878*2139Sjp161948 else 879*2139Sjp161948 #endif 880*2139Sjp161948 { 881*2139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 882*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 883*2139Sjp161948 goto f_err; 884*2139Sjp161948 } 885*2139Sjp161948 for (i=0; r[i] != NULL; i++) 886*2139Sjp161948 { 887*2139Sjp161948 nr[i]=BN_num_bytes(r[i]); 888*2139Sjp161948 n+=2+nr[i]; 889*2139Sjp161948 } 890*2139Sjp161948 891*2139Sjp161948 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 892*2139Sjp161948 { 893*2139Sjp161948 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) 894*2139Sjp161948 == NULL) 895*2139Sjp161948 { 896*2139Sjp161948 al=SSL_AD_DECODE_ERROR; 897*2139Sjp161948 goto f_err; 898*2139Sjp161948 } 899*2139Sjp161948 kn=EVP_PKEY_size(pkey); 900*2139Sjp161948 } 901*2139Sjp161948 else 902*2139Sjp161948 { 903*2139Sjp161948 pkey=NULL; 904*2139Sjp161948 kn=0; 905*2139Sjp161948 } 906*2139Sjp161948 907*2139Sjp161948 if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn)) 908*2139Sjp161948 { 909*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); 910*2139Sjp161948 goto err; 911*2139Sjp161948 } 912*2139Sjp161948 d=(unsigned char *)s->init_buf->data; 913*2139Sjp161948 p= &(d[DTLS1_HM_HEADER_LENGTH]); 914*2139Sjp161948 915*2139Sjp161948 for (i=0; r[i] != NULL; i++) 916*2139Sjp161948 { 917*2139Sjp161948 s2n(nr[i],p); 918*2139Sjp161948 BN_bn2bin(r[i],p); 919*2139Sjp161948 p+=nr[i]; 920*2139Sjp161948 } 921*2139Sjp161948 922*2139Sjp161948 /* not anonymous */ 923*2139Sjp161948 if (pkey != NULL) 924*2139Sjp161948 { 925*2139Sjp161948 /* n is the length of the params, they start at 926*2139Sjp161948 * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space 927*2139Sjp161948 * at the end. */ 928*2139Sjp161948 #ifndef OPENSSL_NO_RSA 929*2139Sjp161948 if (pkey->type == EVP_PKEY_RSA) 930*2139Sjp161948 { 931*2139Sjp161948 q=md_buf; 932*2139Sjp161948 j=0; 933*2139Sjp161948 for (num=2; num > 0; num--) 934*2139Sjp161948 { 935*2139Sjp161948 EVP_DigestInit_ex(&md_ctx,(num == 2) 936*2139Sjp161948 ?s->ctx->md5:s->ctx->sha1, NULL); 937*2139Sjp161948 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 938*2139Sjp161948 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 939*2139Sjp161948 EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n); 940*2139Sjp161948 EVP_DigestFinal_ex(&md_ctx,q, 941*2139Sjp161948 (unsigned int *)&i); 942*2139Sjp161948 q+=i; 943*2139Sjp161948 j+=i; 944*2139Sjp161948 } 945*2139Sjp161948 if (RSA_sign(NID_md5_sha1, md_buf, j, 946*2139Sjp161948 &(p[2]), &u, pkey->pkey.rsa) <= 0) 947*2139Sjp161948 { 948*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); 949*2139Sjp161948 goto err; 950*2139Sjp161948 } 951*2139Sjp161948 s2n(u,p); 952*2139Sjp161948 n+=u+2; 953*2139Sjp161948 } 954*2139Sjp161948 else 955*2139Sjp161948 #endif 956*2139Sjp161948 #if !defined(OPENSSL_NO_DSA) 957*2139Sjp161948 if (pkey->type == EVP_PKEY_DSA) 958*2139Sjp161948 { 959*2139Sjp161948 /* lets do DSS */ 960*2139Sjp161948 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL); 961*2139Sjp161948 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 962*2139Sjp161948 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 963*2139Sjp161948 EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n); 964*2139Sjp161948 if (!EVP_SignFinal(&md_ctx,&(p[2]), 965*2139Sjp161948 (unsigned int *)&i,pkey)) 966*2139Sjp161948 { 967*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); 968*2139Sjp161948 goto err; 969*2139Sjp161948 } 970*2139Sjp161948 s2n(i,p); 971*2139Sjp161948 n+=i+2; 972*2139Sjp161948 } 973*2139Sjp161948 else 974*2139Sjp161948 #endif 975*2139Sjp161948 { 976*2139Sjp161948 /* Is this error check actually needed? */ 977*2139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 978*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); 979*2139Sjp161948 goto f_err; 980*2139Sjp161948 } 981*2139Sjp161948 } 982*2139Sjp161948 983*2139Sjp161948 d = dtls1_set_message_header(s, d, 984*2139Sjp161948 SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n); 985*2139Sjp161948 986*2139Sjp161948 /* we should now have things packed up, so lets send 987*2139Sjp161948 * it off */ 988*2139Sjp161948 s->init_num=n+DTLS1_HM_HEADER_LENGTH; 989*2139Sjp161948 s->init_off=0; 990*2139Sjp161948 991*2139Sjp161948 /* buffer the message to handle re-xmits */ 992*2139Sjp161948 dtls1_buffer_message(s, 0); 993*2139Sjp161948 } 994*2139Sjp161948 995*2139Sjp161948 s->state = SSL3_ST_SW_KEY_EXCH_B; 996*2139Sjp161948 EVP_MD_CTX_cleanup(&md_ctx); 997*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 998*2139Sjp161948 f_err: 999*2139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1000*2139Sjp161948 err: 1001*2139Sjp161948 EVP_MD_CTX_cleanup(&md_ctx); 1002*2139Sjp161948 return(-1); 1003*2139Sjp161948 } 1004*2139Sjp161948 1005*2139Sjp161948 int dtls1_send_certificate_request(SSL *s) 1006*2139Sjp161948 { 1007*2139Sjp161948 unsigned char *p,*d; 1008*2139Sjp161948 int i,j,nl,off,n; 1009*2139Sjp161948 STACK_OF(X509_NAME) *sk=NULL; 1010*2139Sjp161948 X509_NAME *name; 1011*2139Sjp161948 BUF_MEM *buf; 1012*2139Sjp161948 1013*2139Sjp161948 if (s->state == SSL3_ST_SW_CERT_REQ_A) 1014*2139Sjp161948 { 1015*2139Sjp161948 buf=s->init_buf; 1016*2139Sjp161948 1017*2139Sjp161948 d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]); 1018*2139Sjp161948 1019*2139Sjp161948 /* get the list of acceptable cert types */ 1020*2139Sjp161948 p++; 1021*2139Sjp161948 n=ssl3_get_req_cert_type(s,p); 1022*2139Sjp161948 d[0]=n; 1023*2139Sjp161948 p+=n; 1024*2139Sjp161948 n++; 1025*2139Sjp161948 1026*2139Sjp161948 off=n; 1027*2139Sjp161948 p+=2; 1028*2139Sjp161948 n+=2; 1029*2139Sjp161948 1030*2139Sjp161948 sk=SSL_get_client_CA_list(s); 1031*2139Sjp161948 nl=0; 1032*2139Sjp161948 if (sk != NULL) 1033*2139Sjp161948 { 1034*2139Sjp161948 for (i=0; i<sk_X509_NAME_num(sk); i++) 1035*2139Sjp161948 { 1036*2139Sjp161948 name=sk_X509_NAME_value(sk,i); 1037*2139Sjp161948 j=i2d_X509_NAME(name,NULL); 1038*2139Sjp161948 if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2)) 1039*2139Sjp161948 { 1040*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); 1041*2139Sjp161948 goto err; 1042*2139Sjp161948 } 1043*2139Sjp161948 p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]); 1044*2139Sjp161948 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 1045*2139Sjp161948 { 1046*2139Sjp161948 s2n(j,p); 1047*2139Sjp161948 i2d_X509_NAME(name,&p); 1048*2139Sjp161948 n+=2+j; 1049*2139Sjp161948 nl+=2+j; 1050*2139Sjp161948 } 1051*2139Sjp161948 else 1052*2139Sjp161948 { 1053*2139Sjp161948 d=p; 1054*2139Sjp161948 i2d_X509_NAME(name,&p); 1055*2139Sjp161948 j-=2; s2n(j,d); j+=2; 1056*2139Sjp161948 n+=j; 1057*2139Sjp161948 nl+=j; 1058*2139Sjp161948 } 1059*2139Sjp161948 } 1060*2139Sjp161948 } 1061*2139Sjp161948 /* else no CA names */ 1062*2139Sjp161948 p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]); 1063*2139Sjp161948 s2n(nl,p); 1064*2139Sjp161948 1065*2139Sjp161948 d=(unsigned char *)buf->data; 1066*2139Sjp161948 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; 1067*2139Sjp161948 l2n3(n,d); 1068*2139Sjp161948 s2n(s->d1->handshake_write_seq,d); 1069*2139Sjp161948 s->d1->handshake_write_seq++; 1070*2139Sjp161948 1071*2139Sjp161948 /* we should now have things packed up, so lets send 1072*2139Sjp161948 * it off */ 1073*2139Sjp161948 1074*2139Sjp161948 s->init_num=n+DTLS1_HM_HEADER_LENGTH; 1075*2139Sjp161948 s->init_off=0; 1076*2139Sjp161948 #ifdef NETSCAPE_HANG_BUG 1077*2139Sjp161948 /* XXX: what to do about this? */ 1078*2139Sjp161948 p=(unsigned char *)s->init_buf->data + s->init_num; 1079*2139Sjp161948 1080*2139Sjp161948 /* do the header */ 1081*2139Sjp161948 *(p++)=SSL3_MT_SERVER_DONE; 1082*2139Sjp161948 *(p++)=0; 1083*2139Sjp161948 *(p++)=0; 1084*2139Sjp161948 *(p++)=0; 1085*2139Sjp161948 s->init_num += 4; 1086*2139Sjp161948 #endif 1087*2139Sjp161948 1088*2139Sjp161948 /* XDTLS: set message header ? */ 1089*2139Sjp161948 /* buffer the message to handle re-xmits */ 1090*2139Sjp161948 dtls1_buffer_message(s, 0); 1091*2139Sjp161948 1092*2139Sjp161948 s->state = SSL3_ST_SW_CERT_REQ_B; 1093*2139Sjp161948 } 1094*2139Sjp161948 1095*2139Sjp161948 /* SSL3_ST_SW_CERT_REQ_B */ 1096*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1097*2139Sjp161948 err: 1098*2139Sjp161948 return(-1); 1099*2139Sjp161948 } 1100*2139Sjp161948 1101*2139Sjp161948 int dtls1_send_server_certificate(SSL *s) 1102*2139Sjp161948 { 1103*2139Sjp161948 unsigned long l; 1104*2139Sjp161948 X509 *x; 1105*2139Sjp161948 1106*2139Sjp161948 if (s->state == SSL3_ST_SW_CERT_A) 1107*2139Sjp161948 { 1108*2139Sjp161948 x=ssl_get_server_send_cert(s); 1109*2139Sjp161948 if (x == NULL && 1110*2139Sjp161948 /* VRS: allow null cert if auth == KRB5 */ 1111*2139Sjp161948 (s->s3->tmp.new_cipher->algorithms 1112*2139Sjp161948 & (SSL_MKEY_MASK|SSL_AUTH_MASK)) 1113*2139Sjp161948 != (SSL_aKRB5|SSL_kKRB5)) 1114*2139Sjp161948 { 1115*2139Sjp161948 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR); 1116*2139Sjp161948 return(0); 1117*2139Sjp161948 } 1118*2139Sjp161948 1119*2139Sjp161948 l=dtls1_output_cert_chain(s,x); 1120*2139Sjp161948 s->state=SSL3_ST_SW_CERT_B; 1121*2139Sjp161948 s->init_num=(int)l; 1122*2139Sjp161948 s->init_off=0; 1123*2139Sjp161948 1124*2139Sjp161948 /* buffer the message to handle re-xmits */ 1125*2139Sjp161948 dtls1_buffer_message(s, 0); 1126*2139Sjp161948 } 1127*2139Sjp161948 1128*2139Sjp161948 /* SSL3_ST_SW_CERT_B */ 1129*2139Sjp161948 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1130*2139Sjp161948 } 1131