Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame^] | 1 | /* ssl/s3_srvr.c */ |
| 2 | /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. |
| 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * "This product includes cryptographic software written by |
| 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. |
| 52 | * |
| 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] |
| 57 | */ |
| 58 | |
| 59 | #define REUSE_CIPHER_BUG |
| 60 | |
| 61 | #include <stdio.h> |
| 62 | #include "buffer.h" |
| 63 | #include "rand.h" |
| 64 | #include "objects.h" |
| 65 | #include "evp.h" |
| 66 | #include "x509.h" |
| 67 | #include "ssl_locl.h" |
| 68 | |
| 69 | #define BREAK break |
| 70 | /* SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_MALLOC_FAILURE); |
| 71 | * SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE); |
| 72 | * SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); |
| 73 | * SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_MALLOC_FAILURE); |
| 74 | * SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); |
| 75 | */ |
| 76 | |
| 77 | #ifndef NOPROTO |
| 78 | static int ssl3_get_client_hello(SSL *s); |
| 79 | static int ssl3_send_server_hello(SSL *s); |
| 80 | static int ssl3_send_server_key_exchange(SSL *s); |
| 81 | static int ssl3_send_certificate_request(SSL *s); |
| 82 | static int ssl3_send_server_done(SSL *s); |
| 83 | static int ssl3_get_cert_verify(SSL *s); |
| 84 | static int ssl3_get_client_key_exchange(SSL *s); |
| 85 | static int ssl3_get_client_certificate(SSL *s); |
| 86 | static int ssl3_send_hello_request(SSL *s); |
| 87 | |
| 88 | #else |
| 89 | |
| 90 | static int ssl3_get_client_hello(); |
| 91 | static int ssl3_send_server_hello(); |
| 92 | static int ssl3_send_server_key_exchange(); |
| 93 | static int ssl3_send_certificate_request(); |
| 94 | static int ssl3_send_server_done(); |
| 95 | static int ssl3_get_cert_verify(); |
| 96 | static int ssl3_get_client_key_exchange(); |
| 97 | static int ssl3_get_client_certificate(); |
| 98 | static int ssl3_send_hello_request(); |
| 99 | static SSL_METHOD *ssl3_get_server_method(); |
| 100 | |
| 101 | #endif |
| 102 | |
| 103 | static SSL_METHOD *ssl3_get_server_method(ver) |
| 104 | int ver; |
| 105 | { |
| 106 | if (ver == 3) |
| 107 | return(SSLv3_server_method()); |
| 108 | else |
| 109 | return(NULL); |
| 110 | } |
| 111 | |
| 112 | SSL_METHOD *SSLv3_server_method() |
| 113 | { |
| 114 | static int init=1; |
| 115 | static SSL_METHOD SSLv3_server_data; |
| 116 | |
| 117 | if (init) |
| 118 | { |
| 119 | init=0; |
| 120 | memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(), |
| 121 | sizeof(SSL_METHOD)); |
| 122 | SSLv3_server_data.ssl_accept=ssl3_accept; |
| 123 | SSLv3_server_data.get_ssl_method=ssl3_get_server_method; |
| 124 | } |
| 125 | return(&SSLv3_server_data); |
| 126 | } |
| 127 | |
| 128 | int ssl3_accept(s) |
| 129 | SSL *s; |
| 130 | { |
| 131 | BUF_MEM *buf; |
| 132 | unsigned long l,Time=time(NULL); |
| 133 | void (*cb)()=NULL; |
| 134 | long num1; |
| 135 | int ret= -1; |
| 136 | CERT *ct; |
| 137 | BIO *bbio,*under; |
| 138 | int new_state,state,skip=0; |
| 139 | |
| 140 | RAND_seed((unsigned char *)&Time,sizeof(Time)); |
| 141 | ERR_clear_error(); |
| 142 | errno=0; |
| 143 | |
| 144 | if (s->info_callback != NULL) |
| 145 | cb=s->info_callback; |
| 146 | else if (s->ctx->info_callback != NULL) |
| 147 | cb=s->ctx->info_callback; |
| 148 | |
| 149 | /* init things to blank */ |
| 150 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 151 | s->in_handshake++; |
| 152 | |
| 153 | #ifdef undef |
| 154 | /* FIX THIS EAY EAY EAY */ |
| 155 | /* we don't actually need a cert, we just need a cert or a DH_tmp */ |
| 156 | if (((s->session == NULL) || (s->session->cert == NULL)) && |
| 157 | (s->cert == NULL)) |
| 158 | { |
| 159 | SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); |
| 160 | ret= -1; |
| 161 | goto end; |
| 162 | } |
| 163 | #endif |
| 164 | |
| 165 | for (;;) |
| 166 | { |
| 167 | state=s->state; |
| 168 | |
| 169 | switch (s->state) |
| 170 | { |
| 171 | case SSL_ST_RENEGOTIATE: |
| 172 | s->new_session=1; |
| 173 | /* s->state=SSL_ST_ACCEPT; */ |
| 174 | |
| 175 | case SSL_ST_BEFORE: |
| 176 | case SSL_ST_ACCEPT: |
| 177 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: |
| 178 | case SSL_ST_OK|SSL_ST_ACCEPT: |
| 179 | |
| 180 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 181 | |
| 182 | s->version=3; |
| 183 | s->type=SSL_ST_ACCEPT; |
| 184 | |
| 185 | if (s->init_buf == NULL) |
| 186 | { |
| 187 | if ((buf=BUF_MEM_new()) == NULL) |
| 188 | { |
| 189 | ret= -1; |
| 190 | goto end; |
| 191 | } |
| 192 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 193 | { |
| 194 | ret= -1; |
| 195 | goto end; |
| 196 | } |
| 197 | s->init_buf=buf; |
| 198 | } |
| 199 | |
| 200 | if (!ssl3_setup_buffers(s)) |
| 201 | { |
| 202 | ret= -1; |
| 203 | goto end; |
| 204 | } |
| 205 | |
| 206 | /* Ok, we now need to push on a buffering BIO so that |
| 207 | * the output is sent in a way that TCP likes :-) |
| 208 | */ |
| 209 | if (s->bbio == NULL) |
| 210 | { |
| 211 | bbio=BIO_new(BIO_f_buffer()); |
| 212 | if (bbio == NULL) |
| 213 | { |
| 214 | SSLerr(SSL_F_SSL3_ACCEPT,ERR_LIB_BUF); |
| 215 | ret= -1; |
| 216 | goto end; |
| 217 | } |
| 218 | s->bbio=bbio; |
| 219 | } |
| 220 | else |
| 221 | bbio=s->bbio; |
| 222 | BIO_reset(bbio); |
| 223 | if (!BIO_set_write_buffer_size(bbio,16*1024)) |
| 224 | { |
| 225 | SSLerr(SSL_F_SSL3_ACCEPT,ERR_LIB_BUF); |
| 226 | ret= -1; |
| 227 | goto end; |
| 228 | } |
| 229 | |
| 230 | s->wbio=BIO_push(bbio,s->wbio); |
| 231 | |
| 232 | s->ctx->sess_accept++; |
| 233 | s->init_num=0; |
| 234 | |
| 235 | if (s->state != SSL_ST_RENEGOTIATE) |
| 236 | { |
| 237 | s->state=SSL3_ST_SR_CLNT_HELLO_A; |
| 238 | ssl3_init_finished_mac(s); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | s->state=SSL3_ST_SW_HELLO_REQ_A; |
| 243 | } |
| 244 | break; |
| 245 | |
| 246 | case SSL3_ST_SW_HELLO_REQ_A: |
| 247 | case SSL3_ST_SW_HELLO_REQ_B: |
| 248 | |
| 249 | s->shutdown=0; |
| 250 | ret=ssl3_send_hello_request(s); |
| 251 | if (ret <= 0) goto end; |
| 252 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; |
| 253 | s->state=SSL3_ST_SW_FLUSH; |
| 254 | s->init_num=0; |
| 255 | |
| 256 | ssl3_init_finished_mac(s); |
| 257 | break; |
| 258 | |
| 259 | case SSL3_ST_SW_HELLO_REQ_C: |
| 260 | /* remove buffering on output */ |
| 261 | under=BIO_pop(s->wbio); |
| 262 | if (under != NULL) |
| 263 | s->wbio=under; |
| 264 | else |
| 265 | abort(); /* ok */ |
| 266 | BIO_free(s->bbio); |
| 267 | s->bbio=NULL; |
| 268 | |
| 269 | s->state=SSL_ST_OK; |
| 270 | ret=1; |
| 271 | goto end; |
| 272 | break; |
| 273 | |
| 274 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 275 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 276 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 277 | |
| 278 | s->shutdown=0; |
| 279 | ret=ssl3_get_client_hello(s); |
| 280 | if (ret <= 0) goto end; |
| 281 | s->state=SSL3_ST_SW_SRVR_HELLO_A; |
| 282 | s->init_num=0; |
| 283 | break; |
| 284 | |
| 285 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 286 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 287 | ret=ssl3_send_server_hello(s); |
| 288 | if (ret <= 0) goto end; |
| 289 | |
| 290 | if (s->hit) |
| 291 | s->state=SSL3_ST_SW_CHANGE_A; |
| 292 | else |
| 293 | s->state=SSL3_ST_SW_CERT_A; |
| 294 | s->init_num=0; |
| 295 | break; |
| 296 | |
| 297 | case SSL3_ST_SW_CERT_A: |
| 298 | case SSL3_ST_SW_CERT_B: |
| 299 | /* Check if it is anon DH */ |
| 300 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) |
| 301 | { |
| 302 | ret=ssl3_send_server_certificate(s); |
| 303 | if (ret <= 0) goto end; |
| 304 | } |
| 305 | else |
| 306 | skip=1; |
| 307 | s->state=SSL3_ST_SW_KEY_EXCH_A; |
| 308 | s->init_num=0; |
| 309 | break; |
| 310 | |
| 311 | case SSL3_ST_SW_KEY_EXCH_A: |
| 312 | case SSL3_ST_SW_KEY_EXCH_B: |
| 313 | l=s->s3->tmp.new_cipher->algorithms; |
| 314 | if (s->session->cert == NULL) |
| 315 | { |
| 316 | if (s->cert != NULL) |
| 317 | { |
| 318 | CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT); |
| 319 | s->session->cert=s->cert; |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | CRYPTO_add(&s->ctx->default_cert->references,1,CRYPTO_LOCK_SSL_CERT); |
| 324 | s->session->cert=s->ctx->default_cert; |
| 325 | } |
| 326 | } |
| 327 | ct=s->session->cert; |
| 328 | |
| 329 | /* clear this, it may get reset by |
| 330 | * send_server_key_exchange */ |
| 331 | if (s->ctx->options & SSL_OP_EPHEMERAL_RSA) |
| 332 | s->s3->tmp.use_rsa_tmp=1; |
| 333 | else |
| 334 | s->s3->tmp.use_rsa_tmp=0; |
| 335 | |
| 336 | /* only send if a DH key exchange, fortezza or |
| 337 | * RSA but we have a sign only certificate */ |
| 338 | if ( s->s3->tmp.use_rsa_tmp || |
| 339 | (l & (SSL_DH|SSL_kFZA)) || |
| 340 | ((l & SSL_kRSA) && |
| 341 | ((ct->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)|| |
| 342 | ((l & SSL_EXPORT) && |
| 343 | (EVP_PKEY_size(ct->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > 512) |
| 344 | ) |
| 345 | ) |
| 346 | ) |
| 347 | ) |
| 348 | { |
| 349 | ret=ssl3_send_server_key_exchange(s); |
| 350 | if (ret <= 0) goto end; |
| 351 | } |
| 352 | else |
| 353 | skip=1; |
| 354 | |
| 355 | s->state=SSL3_ST_SW_CERT_REQ_A; |
| 356 | s->init_num=0; |
| 357 | break; |
| 358 | |
| 359 | case SSL3_ST_SW_CERT_REQ_A: |
| 360 | case SSL3_ST_SW_CERT_REQ_B: |
| 361 | if (!(s->verify_mode & SSL_VERIFY_PEER) || |
| 362 | ((s->session->peer != NULL) && |
| 363 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE))) |
| 364 | { |
| 365 | /* no cert request */ |
| 366 | skip=1; |
| 367 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | ret=ssl3_send_certificate_request(s); |
| 372 | if (ret <= 0) goto end; |
| 373 | s->state=SSL3_ST_SW_SRVR_DONE_A; |
| 374 | s->init_num=0; |
| 375 | } |
| 376 | break; |
| 377 | |
| 378 | case SSL3_ST_SW_SRVR_DONE_A: |
| 379 | case SSL3_ST_SW_SRVR_DONE_B: |
| 380 | ret=ssl3_send_server_done(s); |
| 381 | if (ret <= 0) goto end; |
| 382 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; |
| 383 | s->state=SSL3_ST_SW_FLUSH; |
| 384 | s->init_num=0; |
| 385 | break; |
| 386 | |
| 387 | case SSL3_ST_SW_FLUSH: |
| 388 | /* number of bytes to be flushed */ |
| 389 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); |
| 390 | if (num1 > 0) |
| 391 | { |
| 392 | s->rwstate=SSL_WRITING; |
| 393 | num1=BIO_flush(s->wbio); |
| 394 | if (num1 <= 0) { ret= -1; goto end; } |
| 395 | s->rwstate=SSL_NOTHING; |
| 396 | } |
| 397 | |
| 398 | s->state=s->s3->tmp.next_state; |
| 399 | break; |
| 400 | |
| 401 | case SSL3_ST_SR_CERT_A: |
| 402 | case SSL3_ST_SR_CERT_B: |
| 403 | /* could be sent for a DH cert, even if we |
| 404 | * have not asked for it :-) */ |
| 405 | ret=ssl3_get_client_certificate(s); |
| 406 | if (ret <= 0) goto end; |
| 407 | s->init_num=0; |
| 408 | s->state=SSL3_ST_SR_KEY_EXCH_A; |
| 409 | break; |
| 410 | |
| 411 | case SSL3_ST_SR_KEY_EXCH_A: |
| 412 | case SSL3_ST_SR_KEY_EXCH_B: |
| 413 | ret=ssl3_get_client_key_exchange(s); |
| 414 | if (ret <= 0) goto end; |
| 415 | s->state=SSL3_ST_SR_CERT_VRFY_A; |
| 416 | s->init_num=0; |
| 417 | |
| 418 | /* We need to get hashes here so if there is |
| 419 | * a client cert, it can be verified */ |
| 420 | ssl3_final_finish_mac(s,&(s->s3->finish_dgst1), |
| 421 | NULL,&(s->s3->tmp.finish_md1[0])); |
| 422 | ssl3_final_finish_mac(s,&(s->s3->finish_dgst2), |
| 423 | NULL,&(s->s3->tmp.finish_md2[0])); |
| 424 | |
| 425 | break; |
| 426 | |
| 427 | case SSL3_ST_SR_CERT_VRFY_A: |
| 428 | case SSL3_ST_SR_CERT_VRFY_B: |
| 429 | |
| 430 | /* we should decide if we expected this one */ |
| 431 | ret=ssl3_get_cert_verify(s); |
| 432 | if (ret <= 0) goto end; |
| 433 | |
| 434 | s->state=SSL3_ST_SR_FINISHED_A; |
| 435 | s->init_num=0; |
| 436 | break; |
| 437 | |
| 438 | case SSL3_ST_SR_FINISHED_A: |
| 439 | case SSL3_ST_SR_FINISHED_B: |
| 440 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, |
| 441 | SSL3_ST_SR_FINISHED_B, |
| 442 | &(ssl3_client_finished_const[0])); |
| 443 | if (ret <= 0) goto end; |
| 444 | if (s->hit) |
| 445 | s->state=SSL_ST_OK; |
| 446 | else |
| 447 | s->state=SSL3_ST_SW_CHANGE_A; |
| 448 | s->init_num=0; |
| 449 | break; |
| 450 | |
| 451 | case SSL3_ST_SW_CHANGE_A: |
| 452 | case SSL3_ST_SW_CHANGE_B: |
| 453 | |
| 454 | s->session->cipher=s->s3->tmp.new_cipher; |
| 455 | if (!ssl3_setup_key_block(s)) { ret= -1; goto end; } |
| 456 | |
| 457 | ret=ssl3_send_change_cipher_spec(s, |
| 458 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); |
| 459 | |
| 460 | if (ret <= 0) goto end; |
| 461 | s->state=SSL3_ST_SW_FINISHED_A; |
| 462 | s->init_num=0; |
| 463 | |
| 464 | if (!ssl3_change_cipher_state(s, |
| 465 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) |
| 466 | { |
| 467 | ret= -1; |
| 468 | goto end; |
| 469 | } |
| 470 | |
| 471 | break; |
| 472 | |
| 473 | case SSL3_ST_SW_FINISHED_A: |
| 474 | case SSL3_ST_SW_FINISHED_B: |
| 475 | ret=ssl3_send_finished(s, |
| 476 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, |
| 477 | &(ssl3_server_finished_const[0])); |
| 478 | if (ret <= 0) goto end; |
| 479 | s->state=SSL3_ST_SW_FLUSH; |
| 480 | if (s->hit) |
| 481 | s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; |
| 482 | else |
| 483 | s->s3->tmp.next_state=SSL_ST_OK; |
| 484 | s->init_num=0; |
| 485 | break; |
| 486 | |
| 487 | case SSL_ST_OK: |
| 488 | /* clean a few things up */ |
| 489 | ssl3_cleanup_key_block(s); |
| 490 | |
| 491 | BUF_MEM_free(s->init_buf); |
| 492 | s->init_buf=NULL; |
| 493 | |
| 494 | /* remove buffering on output */ |
| 495 | under=BIO_pop(s->wbio); |
| 496 | if (under != NULL) |
| 497 | s->wbio=under; |
| 498 | else |
| 499 | abort(); /* ok */ |
| 500 | BIO_free(s->bbio); |
| 501 | s->bbio=NULL; |
| 502 | |
| 503 | s->new_session=0; |
| 504 | s->init_num=0; |
| 505 | |
| 506 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); |
| 507 | |
| 508 | s->ctx->sess_accept_good++; |
| 509 | /* s->server=1; */ |
| 510 | s->handshake_func=ssl3_accept; |
| 511 | ret=1; |
| 512 | |
| 513 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); |
| 514 | |
| 515 | goto end; |
| 516 | break; |
| 517 | |
| 518 | default: |
| 519 | SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); |
| 520 | ret= -1; |
| 521 | goto end; |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | if (!s->s3->tmp.reuse_message && !skip) |
| 526 | { |
| 527 | if (s->debug) BIO_flush(s->wbio); |
| 528 | |
| 529 | if ((cb != NULL) && (s->state != state)) |
| 530 | { |
| 531 | new_state=s->state; |
| 532 | s->state=state; |
| 533 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 534 | s->state=new_state; |
| 535 | } |
| 536 | } |
| 537 | skip=0; |
| 538 | } |
| 539 | end: |
| 540 | /* BIO_flush(s->wbio); */ |
| 541 | |
| 542 | if (cb != NULL) |
| 543 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 544 | s->in_handshake--; |
| 545 | return(ret); |
| 546 | } |
| 547 | |
| 548 | static int ssl3_send_hello_request(s) |
| 549 | SSL *s; |
| 550 | { |
| 551 | unsigned char *p; |
| 552 | |
| 553 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) |
| 554 | { |
| 555 | p=(unsigned char *)s->init_buf->data; |
| 556 | *(p++)=SSL3_MT_CLIENT_REQUEST; |
| 557 | *(p++)=0; |
| 558 | *(p++)=0; |
| 559 | *(p++)=0; |
| 560 | |
| 561 | s->state=SSL3_ST_SW_HELLO_REQ_B; |
| 562 | /* number of bytes to write */ |
| 563 | s->init_num=4; |
| 564 | s->init_off=0; |
| 565 | } |
| 566 | |
| 567 | /* SSL3_ST_SW_HELLO_REQ_B */ |
| 568 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 569 | } |
| 570 | |
| 571 | static int ssl3_get_client_hello(s) |
| 572 | SSL *s; |
| 573 | { |
| 574 | int i,j,ok,al,ret= -1; |
| 575 | long n; |
| 576 | unsigned long id; |
| 577 | unsigned char *p,*d; |
| 578 | SSL_CIPHER *c; |
| 579 | STACK *ciphers=NULL; |
| 580 | |
| 581 | if (s->state == SSL3_ST_SR_CLNT_HELLO_A) |
| 582 | { |
| 583 | s->first_packet=1; |
| 584 | s->state=SSL3_ST_SR_CLNT_HELLO_B; |
| 585 | } |
| 586 | n=ssl3_get_message(s, |
| 587 | SSL3_ST_SR_CLNT_HELLO_B, |
| 588 | SSL3_ST_SR_CLNT_HELLO_C, |
| 589 | SSL3_MT_CLIENT_HELLO, |
| 590 | SSL3_RT_MAX_PLAIN_LENGTH, |
| 591 | &ok); |
| 592 | |
| 593 | if (!ok) return((int)n); |
| 594 | d=p=(unsigned char *)s->init_buf->data; |
| 595 | |
| 596 | if (p[0] != SSL3_VERSION_MAJOR) |
| 597 | { |
| 598 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_WRONG_SSL_VERSION); |
| 599 | goto err; |
| 600 | } |
| 601 | p+=2; |
| 602 | |
| 603 | /* load the client random */ |
| 604 | memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); |
| 605 | p+=SSL3_RANDOM_SIZE; |
| 606 | |
| 607 | /* get the session-id */ |
| 608 | j= *(p++); |
| 609 | |
| 610 | s->hit=0; |
| 611 | if (j == 0) |
| 612 | { |
| 613 | if (!ssl_get_new_session(s,1)) |
| 614 | goto err; |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | i=ssl_get_prev_session(s,j,p); |
| 619 | if (i == 1) |
| 620 | { /* previous session */ |
| 621 | s->hit=1; |
| 622 | } |
| 623 | else |
| 624 | { |
| 625 | if (!ssl_get_new_session(s,1)) |
| 626 | goto err; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | p+=j; |
| 631 | n2s(p,i); |
| 632 | if ((i == 0) && (j != 0)) |
| 633 | { |
| 634 | /* we need a cipher if we are not resuming a session */ |
| 635 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 636 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); |
| 637 | goto f_err; |
| 638 | } |
| 639 | if ((i+p) > (d+n)) |
| 640 | { |
| 641 | /* not enough data */ |
| 642 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 643 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); |
| 644 | goto f_err; |
| 645 | } |
| 646 | if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) |
| 647 | == NULL)) |
| 648 | { |
| 649 | goto err; |
| 650 | } |
| 651 | p+=i; |
| 652 | |
| 653 | /* If it is a hit, check that the cipher is in the list */ |
| 654 | if ((s->hit) && (i > 0)) |
| 655 | { |
| 656 | j=0; |
| 657 | id=s->session->cipher->id; |
| 658 | |
| 659 | for (i=0; i<sk_num(ciphers); i++) |
| 660 | { |
| 661 | c=(SSL_CIPHER *)sk_value(ciphers,i); |
| 662 | if (c->id == id) |
| 663 | { |
| 664 | j=1; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | if (j == 0) |
| 669 | { |
| 670 | if ((s->ctx->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_num(ciphers) == 1)) |
| 671 | { |
| 672 | /* Very bad for multi-threading.... */ |
| 673 | s->session->cipher= |
| 674 | (SSL_CIPHER *)sk_value(ciphers,0); |
| 675 | } |
| 676 | else |
| 677 | { |
| 678 | /* we need to have the cipher in the cipher |
| 679 | * list if we are asked to reuse it */ |
| 680 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 681 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING); |
| 682 | goto f_err; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* compression */ |
| 688 | i= *(p++); |
| 689 | for (j=0; j<i; j++) |
| 690 | if (p[j] == 0) break; |
| 691 | |
| 692 | p+=i; |
| 693 | if (j >= i) |
| 694 | { |
| 695 | /* no compress */ |
| 696 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 697 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); |
| 698 | goto f_err; |
| 699 | } |
| 700 | |
| 701 | if (p > (d+n)) |
| 702 | { |
| 703 | /* wrong number of bytes, |
| 704 | * there could be more to follow */ |
| 705 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 706 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); |
| 707 | goto f_err; |
| 708 | } |
| 709 | |
| 710 | /* do nothing with compression */ |
| 711 | |
| 712 | /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must |
| 713 | * pick a cipher */ |
| 714 | |
| 715 | if (!s->hit) |
| 716 | { |
| 717 | if (s->session->ciphers != NULL) |
| 718 | sk_free(s->session->ciphers); |
| 719 | s->session->ciphers=ciphers; |
| 720 | if (ciphers == NULL) |
| 721 | { |
| 722 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 723 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); |
| 724 | goto f_err; |
| 725 | } |
| 726 | ciphers=NULL; |
| 727 | c=ssl3_choose_cipher(s,s->session->ciphers, |
| 728 | ssl_get_ciphers_by_id(s)); |
| 729 | |
| 730 | if (c == NULL) |
| 731 | { |
| 732 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 733 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); |
| 734 | goto f_err; |
| 735 | } |
| 736 | s->s3->tmp.new_cipher=c; |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | /* Session-id reuse */ |
| 741 | #ifdef REUSE_CIPHER_BUG |
| 742 | STACK *sk; |
| 743 | SSL_CIPHER *nc=NULL; |
| 744 | SSL_CIPHER *ec=NULL; |
| 745 | |
| 746 | if (s->ctx->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) |
| 747 | { |
| 748 | sk=s->session->ciphers; |
| 749 | for (i=0; i<sk_num(sk); i++) |
| 750 | { |
| 751 | c=(SSL_CIPHER *)sk_value(sk,i); |
| 752 | if (c->algorithms & SSL_eNULL) |
| 753 | nc=c; |
| 754 | if (c->algorithms & SSL_EXP) |
| 755 | ec=c; |
| 756 | } |
| 757 | if (nc != NULL) |
| 758 | s->s3->tmp.new_cipher=nc; |
| 759 | else if (ec != NULL) |
| 760 | s->s3->tmp.new_cipher=ec; |
| 761 | else |
| 762 | s->s3->tmp.new_cipher=s->session->cipher; |
| 763 | } |
| 764 | else |
| 765 | #endif |
| 766 | s->s3->tmp.new_cipher=s->session->cipher; |
| 767 | } |
| 768 | |
| 769 | /* we now have the following setup. |
| 770 | * client_random |
| 771 | * cipher_list - our prefered list of ciphers |
| 772 | * ciphers - the clients prefered list of ciphers |
| 773 | * compression - basically ignored right now |
| 774 | * ssl version is set - sslv3 |
| 775 | * s->session - The ssl session has been setup. |
| 776 | * s->hit - sesson reuse flag |
| 777 | * s->tmp.new_cipher - the new cipher to use. |
| 778 | */ |
| 779 | |
| 780 | ret=1; |
| 781 | if (0) |
| 782 | { |
| 783 | f_err: |
| 784 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 785 | } |
| 786 | err: |
| 787 | if (ciphers != NULL) sk_free(ciphers); |
| 788 | return(ret); |
| 789 | } |
| 790 | |
| 791 | static int ssl3_send_server_hello(s) |
| 792 | SSL *s; |
| 793 | { |
| 794 | unsigned char *buf; |
| 795 | unsigned char *p,*d; |
| 796 | int i,sl; |
| 797 | unsigned long l,Time; |
| 798 | |
| 799 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) |
| 800 | { |
| 801 | buf=(unsigned char *)s->init_buf->data; |
| 802 | p=s->s3->server_random; |
| 803 | Time=time(NULL); /* Time */ |
| 804 | l2n(Time,p); |
| 805 | RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); |
| 806 | /* Do the message type and length last */ |
| 807 | d=p= &(buf[4]); |
| 808 | |
| 809 | *(p++)=SSL3_VERSION_MAJOR; |
| 810 | *(p++)=SSL3_VERSION_MINOR; |
| 811 | |
| 812 | /* Random stuff */ |
| 813 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); |
| 814 | p+=SSL3_RANDOM_SIZE; |
| 815 | |
| 816 | /* now in theory we have 3 options to sending back the |
| 817 | * session id. If it is a re-use, we send back the |
| 818 | * old session-id, if it is a new session, we send |
| 819 | * back the new session-id or we send back a 0 length |
| 820 | * session-id if we want it to be single use. |
| 821 | * Currently I will not implement the '0' length session-id |
| 822 | */ |
| 823 | |
| 824 | sl=s->session->session_id_length; |
| 825 | *(p++)=sl; |
| 826 | memcpy(p,s->session->session_id,sl); |
| 827 | p+=sl; |
| 828 | |
| 829 | /* put the cipher */ |
| 830 | i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); |
| 831 | p+=i; |
| 832 | |
| 833 | /* put the compression method */ |
| 834 | *(p++)=0; |
| 835 | |
| 836 | /* do the header */ |
| 837 | l=(p-d); |
| 838 | d=buf; |
| 839 | *(d++)=SSL3_MT_SERVER_HELLO; |
| 840 | l2n3(l,d); |
| 841 | |
| 842 | s->state=SSL3_ST_CW_CLNT_HELLO_B; |
| 843 | /* number of bytes to write */ |
| 844 | s->init_num=p-buf; |
| 845 | s->init_off=0; |
| 846 | } |
| 847 | |
| 848 | /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 849 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 850 | } |
| 851 | |
| 852 | static int ssl3_send_server_done(s) |
| 853 | SSL *s; |
| 854 | { |
| 855 | unsigned char *p; |
| 856 | |
| 857 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) |
| 858 | { |
| 859 | p=(unsigned char *)s->init_buf->data; |
| 860 | |
| 861 | /* do the header */ |
| 862 | *(p++)=SSL3_MT_SERVER_DONE; |
| 863 | *(p++)=0; |
| 864 | *(p++)=0; |
| 865 | *(p++)=0; |
| 866 | |
| 867 | s->state=SSL3_ST_SW_SRVR_DONE_B; |
| 868 | /* number of bytes to write */ |
| 869 | s->init_num=4; |
| 870 | s->init_off=0; |
| 871 | } |
| 872 | |
| 873 | /* SSL3_ST_CW_CLNT_HELLO_B */ |
| 874 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 875 | } |
| 876 | |
| 877 | static int ssl3_send_server_key_exchange(s) |
| 878 | SSL *s; |
| 879 | { |
| 880 | #ifndef NO_RSA |
| 881 | unsigned char *q; |
| 882 | int j,num; |
| 883 | RSA *rsa; |
| 884 | unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; |
| 885 | #endif |
| 886 | #ifndef NO_DH |
| 887 | DH *dh,*dhp; |
| 888 | #endif |
| 889 | EVP_PKEY *pkey; |
| 890 | unsigned char *p,*d; |
| 891 | int al,i; |
| 892 | unsigned long type; |
| 893 | int n; |
| 894 | CERT *cert; |
| 895 | BIGNUM *r[4]; |
| 896 | int nr[4],kn; |
| 897 | BUF_MEM *buf; |
| 898 | EVP_MD_CTX md_ctx; |
| 899 | |
| 900 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) |
| 901 | { |
| 902 | type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; |
| 903 | cert=s->session->cert; |
| 904 | |
| 905 | buf=s->init_buf; |
| 906 | |
| 907 | r[0]=r[1]=r[2]=r[3]=NULL; |
| 908 | n=0; |
| 909 | #ifndef NO_RSA |
| 910 | if (type & SSL_kRSA) |
| 911 | { |
| 912 | rsa=cert->rsa_tmp; |
| 913 | if ((rsa == NULL) && (s->ctx->default_cert->rsa_tmp_cb != NULL)) |
| 914 | { |
| 915 | rsa=s->ctx->default_cert->rsa_tmp_cb(s, |
| 916 | (s->s3->tmp.new_cipher->algorithms| |
| 917 | SSL_NOT_EXP)?0:1); |
| 918 | CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA); |
| 919 | cert->rsa_tmp=rsa; |
| 920 | } |
| 921 | if (rsa == NULL) |
| 922 | { |
| 923 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 924 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); |
| 925 | goto f_err; |
| 926 | } |
| 927 | r[0]=rsa->n; |
| 928 | r[1]=rsa->e; |
| 929 | s->s3->tmp.use_rsa_tmp=1; |
| 930 | } |
| 931 | else |
| 932 | #endif |
| 933 | #ifndef NO_DH |
| 934 | if (type & SSL_kEDH) |
| 935 | { |
| 936 | dhp=cert->dh_tmp; |
| 937 | if ((dhp == NULL) && (cert->dh_tmp_cb != NULL)) |
| 938 | dhp=cert->dh_tmp_cb(s, |
| 939 | (s->s3->tmp.new_cipher->algorithms| |
| 940 | SSL_NOT_EXP)?0:1); |
| 941 | if (dhp == NULL) |
| 942 | { |
| 943 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 944 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); |
| 945 | goto f_err; |
| 946 | } |
| 947 | if ((dh=DHparams_dup(dhp)) == NULL) |
| 948 | { |
| 949 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); |
| 950 | goto err; |
| 951 | } |
| 952 | |
| 953 | s->s3->tmp.dh=dh; |
| 954 | if (((dhp->pub_key == NULL) || |
| 955 | (dhp->priv_key == NULL) || |
| 956 | (s->ctx->options & SSL_OP_SINGLE_DH_USE)) && |
| 957 | (!DH_generate_key(dh))) |
| 958 | { |
| 959 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); |
| 960 | goto err; |
| 961 | } |
| 962 | else |
| 963 | { |
| 964 | dh->pub_key=BN_dup(dhp->pub_key); |
| 965 | dh->priv_key=BN_dup(dhp->priv_key); |
| 966 | if ((dh->pub_key == NULL) || |
| 967 | (dh->priv_key == NULL)) |
| 968 | { |
| 969 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); |
| 970 | goto err; |
| 971 | } |
| 972 | } |
| 973 | r[0]=dh->p; |
| 974 | r[1]=dh->g; |
| 975 | r[2]=dh->pub_key; |
| 976 | } |
| 977 | else |
| 978 | #endif |
| 979 | { |
| 980 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 981 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 982 | goto f_err; |
| 983 | } |
| 984 | for (i=0; r[i] != NULL; i++) |
| 985 | { |
| 986 | nr[i]=BN_num_bytes(r[i]); |
| 987 | n+=2+nr[i]; |
| 988 | } |
| 989 | |
| 990 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) |
| 991 | { |
| 992 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) |
| 993 | == NULL) |
| 994 | { |
| 995 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 996 | goto f_err; |
| 997 | } |
| 998 | kn=EVP_PKEY_size(pkey); |
| 999 | } |
| 1000 | else |
| 1001 | { |
| 1002 | pkey=NULL; |
| 1003 | kn=0; |
| 1004 | } |
| 1005 | |
| 1006 | if (!BUF_MEM_grow(buf,n+4+kn)) |
| 1007 | { |
| 1008 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); |
| 1009 | goto err; |
| 1010 | } |
| 1011 | d=(unsigned char *)s->init_buf->data; |
| 1012 | p= &(d[4]); |
| 1013 | |
| 1014 | for (i=0; r[i] != NULL; i++) |
| 1015 | { |
| 1016 | s2n(nr[i],p); |
| 1017 | BN_bn2bin(r[i],p); |
| 1018 | p+=nr[i]; |
| 1019 | } |
| 1020 | |
| 1021 | /* not anonymous */ |
| 1022 | if (pkey != NULL) |
| 1023 | { |
| 1024 | /* n is the length of the params, they start at &(d[4]) |
| 1025 | * and p points to the space at the end. */ |
| 1026 | #ifndef NO_RSA |
| 1027 | if (pkey->type == EVP_PKEY_RSA) |
| 1028 | { |
| 1029 | q=md_buf; |
| 1030 | j=0; |
| 1031 | for (num=2; num > 0; num--) |
| 1032 | { |
| 1033 | EVP_DigestInit(&md_ctx,(num == 2)?EVP_md5():EVP_sha1()); |
| 1034 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1035 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1036 | EVP_DigestUpdate(&md_ctx,&(d[4]),n); |
| 1037 | EVP_DigestFinal(&md_ctx,q, |
| 1038 | (unsigned int *)&i); |
| 1039 | q+=i; |
| 1040 | j+=i; |
| 1041 | } |
| 1042 | i=RSA_private_encrypt(j,md_buf,&(p[2]), |
| 1043 | pkey->pkey.rsa,RSA_PKCS1_PADDING); |
| 1044 | if (i <= 0) |
| 1045 | { |
| 1046 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); |
| 1047 | goto err; |
| 1048 | } |
| 1049 | s2n(i,p); |
| 1050 | n+=i+2; |
| 1051 | } |
| 1052 | else |
| 1053 | #endif |
| 1054 | #if !defined(NO_DSA) |
| 1055 | if (pkey->type == EVP_PKEY_DSA) |
| 1056 | { |
| 1057 | /* lets do DSS */ |
| 1058 | EVP_SignInit(&md_ctx,EVP_dss1()); |
| 1059 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
| 1060 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); |
| 1061 | EVP_SignUpdate(&md_ctx,&(d[4]),n); |
| 1062 | if (!EVP_SignFinal(&md_ctx,&(p[2]), |
| 1063 | (unsigned int *)&i,pkey)) |
| 1064 | { |
| 1065 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); |
| 1066 | goto err; |
| 1067 | } |
| 1068 | s2n(i,p); |
| 1069 | n+=i+2; |
| 1070 | } |
| 1071 | else |
| 1072 | #endif |
| 1073 | { |
| 1074 | /* Is this error check actually needed? */ |
| 1075 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 1076 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); |
| 1077 | goto f_err; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; |
| 1082 | l2n3(n,d); |
| 1083 | |
| 1084 | /* we should now have things packed up, so lets send |
| 1085 | * it off */ |
| 1086 | s->init_num=n+4; |
| 1087 | s->init_off=0; |
| 1088 | } |
| 1089 | |
| 1090 | /* SSL3_ST_SW_KEY_EXCH_B */ |
| 1091 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1092 | f_err: |
| 1093 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1094 | err: |
| 1095 | return(-1); |
| 1096 | } |
| 1097 | |
| 1098 | static int ssl3_send_certificate_request(s) |
| 1099 | SSL *s; |
| 1100 | { |
| 1101 | unsigned char *p,*d; |
| 1102 | int i,j,nl,off,n; |
| 1103 | STACK *sk=NULL; |
| 1104 | X509_NAME *name; |
| 1105 | BUF_MEM *buf; |
| 1106 | |
| 1107 | if (s->state == SSL3_ST_SW_CERT_REQ_A) |
| 1108 | { |
| 1109 | buf=s->init_buf; |
| 1110 | |
| 1111 | d=p=(unsigned char *)&(buf->data[4]); |
| 1112 | |
| 1113 | /* get the list of acceptable cert types */ |
| 1114 | p++; |
| 1115 | n=ssl3_get_req_cert_type(s,p); |
| 1116 | d[0]=n; |
| 1117 | p+=n; |
| 1118 | n++; |
| 1119 | |
| 1120 | off=n; |
| 1121 | p+=2; |
| 1122 | n+=2; |
| 1123 | |
| 1124 | sk=SSL_get_client_CA_list(s); |
| 1125 | nl=0; |
| 1126 | if (sk != NULL) |
| 1127 | { |
| 1128 | for (i=0; i<sk_num(sk); i++) |
| 1129 | { |
| 1130 | name=(X509_NAME *)sk_value(sk,i); |
| 1131 | j=i2d_X509_NAME(name,NULL); |
| 1132 | if (!BUF_MEM_grow(buf,4+n+j+2)) |
| 1133 | { |
| 1134 | SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); |
| 1135 | goto err; |
| 1136 | } |
| 1137 | p=(unsigned char *)&(buf->data[4+n]); |
| 1138 | if (!(s->ctx->options & SSL_OP_NETSCAPE_CA_DN_BUG)) |
| 1139 | { |
| 1140 | s2n(j,p); |
| 1141 | i2d_X509_NAME(name,&p); |
| 1142 | n+=2+j; |
| 1143 | nl+=2+j; |
| 1144 | } |
| 1145 | else |
| 1146 | { |
| 1147 | d=p; |
| 1148 | i2d_X509_NAME(name,&p); |
| 1149 | j-=2; s2n(j,d); j+=2; |
| 1150 | n+=j; |
| 1151 | nl+=j; |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | /* else no CA names */ |
| 1156 | p=(unsigned char *)&(buf->data[4+off]); |
| 1157 | s2n(nl,p); |
| 1158 | |
| 1159 | d=(unsigned char *)buf->data; |
| 1160 | *(d++)=SSL3_MT_CERTIFICATE_REQUEST; |
| 1161 | l2n3(n,d); |
| 1162 | |
| 1163 | /* we should now have things packed up, so lets send |
| 1164 | * it off */ |
| 1165 | |
| 1166 | s->init_num=n+4; |
| 1167 | s->init_off=0; |
| 1168 | } |
| 1169 | |
| 1170 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 1171 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1172 | err: |
| 1173 | return(-1); |
| 1174 | } |
| 1175 | |
| 1176 | static int ssl3_get_client_key_exchange(s) |
| 1177 | SSL *s; |
| 1178 | { |
| 1179 | int i,al,ok; |
| 1180 | long n; |
| 1181 | unsigned long l; |
| 1182 | unsigned char *p; |
| 1183 | RSA *rsa=NULL; |
| 1184 | BIGNUM *pub=NULL; |
| 1185 | EVP_PKEY *pkey=NULL; |
| 1186 | DH *dh_srvr; |
| 1187 | |
| 1188 | n=ssl3_get_message(s, |
| 1189 | SSL3_ST_SR_KEY_EXCH_A, |
| 1190 | SSL3_ST_SR_KEY_EXCH_B, |
| 1191 | SSL3_MT_CLIENT_KEY_EXCHANGE, |
| 1192 | 400, /* ???? */ |
| 1193 | &ok); |
| 1194 | |
| 1195 | if (!ok) return((int)n); |
| 1196 | p=(unsigned char *)s->init_buf->data; |
| 1197 | |
| 1198 | l=s->s3->tmp.new_cipher->algorithms; |
| 1199 | |
| 1200 | #ifndef NO_RSA |
| 1201 | if (l & SSL_kRSA) |
| 1202 | { |
| 1203 | /* FIX THIS UP EAY EAY EAY EAY */ |
| 1204 | if (s->s3->tmp.use_rsa_tmp) |
| 1205 | { |
| 1206 | if ((s->session->cert != NULL) && |
| 1207 | (s->session->cert->rsa_tmp != NULL)) |
| 1208 | rsa=s->session->cert->rsa_tmp; |
| 1209 | else if ((s->ctx->default_cert != NULL) && |
| 1210 | (s->ctx->default_cert->rsa_tmp != NULL)) |
| 1211 | rsa=s->ctx->default_cert->rsa_tmp; |
| 1212 | /* Don't do a callback because rsa_tmp should |
| 1213 | * be sent already */ |
| 1214 | if (rsa == NULL) |
| 1215 | { |
| 1216 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 1217 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); |
| 1218 | goto f_err; |
| 1219 | |
| 1220 | } |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 1225 | if ( (pkey == NULL) || |
| 1226 | (pkey->type != EVP_PKEY_RSA) || |
| 1227 | (pkey->pkey.rsa == NULL)) |
| 1228 | { |
| 1229 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 1230 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); |
| 1231 | goto f_err; |
| 1232 | } |
| 1233 | rsa=pkey->pkey.rsa; |
| 1234 | } |
| 1235 | |
| 1236 | i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); |
| 1237 | #if 1 |
| 1238 | /* If a bad decrypt, use a dud master key */ |
| 1239 | if ((i != SSL_MAX_MASTER_KEY_LENGTH) || |
| 1240 | (p[0] != 3) || |
| 1241 | (p[1] != 0)) |
| 1242 | { |
| 1243 | p[0]=3; |
| 1244 | p[1]=0; |
| 1245 | RAND_bytes(&(p[2]),SSL_MAX_MASTER_KEY_LENGTH-2); |
| 1246 | i=SSL_MAX_MASTER_KEY_LENGTH; |
| 1247 | } |
| 1248 | #else |
| 1249 | if (i != SSL_MAX_MASTER_KEY_LENGTH) |
| 1250 | { |
| 1251 | al=SSL_AD_DECODE_ERROR; |
| 1252 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); |
| 1253 | goto f_err; |
| 1254 | } |
| 1255 | |
| 1256 | if ((p[0] != (s->version>>8)) || (p[1] != (s->version & 0xff))) |
| 1257 | { |
| 1258 | al=SSL_AD_DECODE_ERROR; |
| 1259 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
| 1260 | goto f_err; |
| 1261 | } |
| 1262 | #endif |
| 1263 | |
| 1264 | s->session->master_key_length= |
| 1265 | ssl3_generate_master_secret(s, |
| 1266 | s->session->master_key, |
| 1267 | p,i); |
| 1268 | memset(p,0,i); |
| 1269 | } |
| 1270 | else |
| 1271 | #endif |
| 1272 | #ifndef NO_DH |
| 1273 | if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
| 1274 | { |
| 1275 | |
| 1276 | n2s(p,i); |
| 1277 | if (n != i+2) |
| 1278 | { |
| 1279 | if (!(s->ctx->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) |
| 1280 | { |
| 1281 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 1282 | goto err; |
| 1283 | } |
| 1284 | else |
| 1285 | { |
| 1286 | p-=2; |
| 1287 | i=(int)n; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | if (n == 0L) /* the parameters are in the cert */ |
| 1292 | { |
| 1293 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 1294 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); |
| 1295 | goto f_err; |
| 1296 | } |
| 1297 | else |
| 1298 | { |
| 1299 | if (s->s3->tmp.dh == NULL) |
| 1300 | { |
| 1301 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 1302 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); |
| 1303 | goto f_err; |
| 1304 | } |
| 1305 | else |
| 1306 | dh_srvr=s->s3->tmp.dh; |
| 1307 | } |
| 1308 | |
| 1309 | pub=BN_bin2bn(p,i,NULL); |
| 1310 | if (pub == NULL) |
| 1311 | { |
| 1312 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); |
| 1313 | goto err; |
| 1314 | } |
| 1315 | i=DH_compute_key(p,pub,dh_srvr); |
| 1316 | |
| 1317 | if (i <= 0) |
| 1318 | { |
| 1319 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); |
| 1320 | goto err; |
| 1321 | } |
| 1322 | |
| 1323 | DH_free(s->s3->tmp.dh); |
| 1324 | s->s3->tmp.dh=NULL; |
| 1325 | |
| 1326 | BN_clear_free(pub); |
| 1327 | pub=NULL; |
| 1328 | s->session->master_key_length= |
| 1329 | ssl3_generate_master_secret(s, |
| 1330 | s->session->master_key,p,i); |
| 1331 | } |
| 1332 | else |
| 1333 | #endif |
| 1334 | { |
| 1335 | al=SSL3_AD_HANDSHAKE_FAILURE; |
| 1336 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE); |
| 1337 | goto f_err; |
| 1338 | } |
| 1339 | |
| 1340 | return(1); |
| 1341 | f_err: |
| 1342 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1343 | err: |
| 1344 | return(-1); |
| 1345 | } |
| 1346 | |
| 1347 | static int ssl3_get_cert_verify(s) |
| 1348 | SSL *s; |
| 1349 | { |
| 1350 | EVP_PKEY *pkey=NULL; |
| 1351 | unsigned char *p; |
| 1352 | int al,ok,ret=0; |
| 1353 | long n; |
| 1354 | int type=0,i,j; |
| 1355 | X509 *peer; |
| 1356 | |
| 1357 | n=ssl3_get_message(s, |
| 1358 | SSL3_ST_SR_CERT_VRFY_A, |
| 1359 | SSL3_ST_SR_CERT_VRFY_B, |
| 1360 | -1, |
| 1361 | 512, /* 512? */ |
| 1362 | &ok); |
| 1363 | |
| 1364 | if (!ok) return((int)n); |
| 1365 | |
| 1366 | if (s->session->peer != NULL) |
| 1367 | { |
| 1368 | peer=s->session->peer; |
| 1369 | pkey=X509_get_pubkey(peer); |
| 1370 | type=X509_certificate_type(peer,pkey); |
| 1371 | } |
| 1372 | else |
| 1373 | { |
| 1374 | peer=NULL; |
| 1375 | pkey=NULL; |
| 1376 | } |
| 1377 | |
| 1378 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) |
| 1379 | { |
| 1380 | s->s3->tmp.reuse_message=1; |
| 1381 | if ((peer != NULL) && (type | EVP_PKT_SIGN)) |
| 1382 | { |
| 1383 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); |
| 1384 | al=SSL3_AD_UNEXPECTED_MESSAGE; |
| 1385 | goto f_err; |
| 1386 | } |
| 1387 | ret=1; |
| 1388 | goto end; |
| 1389 | } |
| 1390 | |
| 1391 | if (peer == NULL) |
| 1392 | { |
| 1393 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); |
| 1394 | al=SSL3_AD_UNEXPECTED_MESSAGE; |
| 1395 | goto f_err; |
| 1396 | } |
| 1397 | |
| 1398 | if (!(type & EVP_PKT_SIGN)) |
| 1399 | { |
| 1400 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); |
| 1401 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1402 | goto f_err; |
| 1403 | } |
| 1404 | |
| 1405 | if (s->s3->change_cipher_spec) |
| 1406 | { |
| 1407 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); |
| 1408 | al=SSL3_AD_UNEXPECTED_MESSAGE; |
| 1409 | goto f_err; |
| 1410 | } |
| 1411 | |
| 1412 | /* we now have a signature that we need to verify */ |
| 1413 | p=(unsigned char *)s->init_buf->data; |
| 1414 | n2s(p,i); |
| 1415 | n-=2; |
| 1416 | if (i > n) |
| 1417 | { |
| 1418 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); |
| 1419 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1420 | goto f_err; |
| 1421 | } |
| 1422 | |
| 1423 | j=EVP_PKEY_size(pkey); |
| 1424 | if ((i > j) || (n > j) || (n <= 0)) |
| 1425 | { |
| 1426 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); |
| 1427 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1428 | goto f_err; |
| 1429 | } |
| 1430 | |
| 1431 | #ifndef NO_RSA |
| 1432 | if (pkey->type == EVP_PKEY_RSA) |
| 1433 | { |
| 1434 | i=RSA_public_decrypt(i,p,p,pkey->pkey.rsa,RSA_PKCS1_PADDING); |
| 1435 | if (i < 0) |
| 1436 | { |
| 1437 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1438 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); |
| 1439 | goto f_err; |
| 1440 | } |
| 1441 | if ((i != (MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH)) || |
| 1442 | memcmp(&(s->s3->tmp.finish_md1[0]), |
| 1443 | p,MD5_DIGEST_LENGTH) || |
| 1444 | memcmp(&(s->s3->tmp.finish_md2[0]), |
| 1445 | &(p[MD5_DIGEST_LENGTH]),SHA_DIGEST_LENGTH)) |
| 1446 | { |
| 1447 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1448 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); |
| 1449 | goto f_err; |
| 1450 | } |
| 1451 | } |
| 1452 | else |
| 1453 | #endif |
| 1454 | #ifndef NO_DSA |
| 1455 | if (pkey->type == EVP_PKEY_DSA) |
| 1456 | { |
| 1457 | j=DSA_verify(pkey->save_type,s->s3->tmp.finish_md2, |
| 1458 | SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); |
| 1459 | if (j <= 0) |
| 1460 | { |
| 1461 | /* bad signature */ |
| 1462 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1463 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); |
| 1464 | goto f_err; |
| 1465 | } |
| 1466 | } |
| 1467 | else |
| 1468 | #endif |
| 1469 | { |
| 1470 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR); |
| 1471 | al=SSL3_AD_UNSUPPORTED_CERTIFICATE; |
| 1472 | goto f_err; |
| 1473 | } |
| 1474 | |
| 1475 | |
| 1476 | ret=1; |
| 1477 | if (0) |
| 1478 | { |
| 1479 | f_err: |
| 1480 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1481 | } |
| 1482 | end: |
| 1483 | return(ret); |
| 1484 | } |
| 1485 | |
| 1486 | static int ssl3_get_client_certificate(s) |
| 1487 | SSL *s; |
| 1488 | { |
| 1489 | int i,ok,al,ret= -1; |
| 1490 | X509 *x=NULL; |
| 1491 | unsigned long l,nc,llen,n; |
| 1492 | unsigned char *p,*d,*q; |
| 1493 | STACK *sk=NULL; |
| 1494 | |
| 1495 | n=ssl3_get_message(s, |
| 1496 | SSL3_ST_SR_CERT_A, |
| 1497 | SSL3_ST_SR_CERT_B, |
| 1498 | -1, |
| 1499 | #if defined(MSDOS) && !defined(WIN32) |
| 1500 | 1024*30, /* 30k max cert list :-) */ |
| 1501 | #else |
| 1502 | 1024*100, /* 100k max cert list :-) */ |
| 1503 | #endif |
| 1504 | &ok); |
| 1505 | |
| 1506 | if (!ok) return((int)n); |
| 1507 | |
| 1508 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) |
| 1509 | { |
| 1510 | if ( (s->verify_mode & SSL_VERIFY_PEER) && |
| 1511 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
| 1512 | { |
| 1513 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 1514 | al=SSL3_AD_NO_CERTIFICATE; |
| 1515 | goto f_err; |
| 1516 | } |
| 1517 | s->s3->tmp.reuse_message=1; |
| 1518 | return(1); |
| 1519 | } |
| 1520 | |
| 1521 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) |
| 1522 | { |
| 1523 | al=SSL3_AD_UNEXPECTED_MESSAGE; |
| 1524 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); |
| 1525 | goto f_err; |
| 1526 | } |
| 1527 | d=p=(unsigned char *)s->init_buf->data; |
| 1528 | |
| 1529 | if ((sk=sk_new_null()) == NULL) |
| 1530 | { |
| 1531 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); |
| 1532 | goto err; |
| 1533 | } |
| 1534 | |
| 1535 | n2l3(p,llen); |
| 1536 | if (llen+3 != n) |
| 1537 | { |
| 1538 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1539 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); |
| 1540 | goto f_err; |
| 1541 | } |
| 1542 | for (nc=0; nc<llen; ) |
| 1543 | { |
| 1544 | n2l3(p,l); |
| 1545 | if ((l+nc+3) > llen) |
| 1546 | { |
| 1547 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1548 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); |
| 1549 | goto f_err; |
| 1550 | } |
| 1551 | |
| 1552 | q=p; |
| 1553 | x=d2i_X509(NULL,&p,l); |
| 1554 | if (x == NULL) |
| 1555 | { |
| 1556 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); |
| 1557 | goto err; |
| 1558 | } |
| 1559 | if (p != (q+l)) |
| 1560 | { |
| 1561 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1562 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); |
| 1563 | goto f_err; |
| 1564 | } |
| 1565 | if (!sk_push(sk,(char *)x)) |
| 1566 | { |
| 1567 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); |
| 1568 | goto err; |
| 1569 | } |
| 1570 | x=NULL; |
| 1571 | nc+=l+3; |
| 1572 | } |
| 1573 | |
| 1574 | if (sk_num(sk) <= 0) |
| 1575 | { |
| 1576 | al=SSL3_AD_ILLEGAL_PARAMETER; |
| 1577 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_PASSED); |
| 1578 | goto f_err; |
| 1579 | } |
| 1580 | i=ssl_verify_cert_chain(s,sk); |
| 1581 | if (!i) |
| 1582 | { |
| 1583 | al=ssl_verify_alarm_type(s->verify_result); |
| 1584 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); |
| 1585 | goto f_err; |
| 1586 | } |
| 1587 | |
| 1588 | /* This should not be needed */ |
| 1589 | if (s->session->peer != NULL) |
| 1590 | X509_free(s->session->peer); |
| 1591 | |
| 1592 | s->session->peer=(X509 *)sk_shift(sk); |
| 1593 | |
| 1594 | ret=1; |
| 1595 | if (0) |
| 1596 | { |
| 1597 | f_err: |
| 1598 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 1599 | } |
| 1600 | err: |
| 1601 | if (x != NULL) X509_free(x); |
| 1602 | if (sk != NULL) sk_pop_free(sk,X509_free); |
| 1603 | return(ret); |
| 1604 | } |
| 1605 | |
| 1606 | int ssl3_send_server_certificate(s) |
| 1607 | SSL *s; |
| 1608 | { |
| 1609 | unsigned long l; |
| 1610 | X509 *x; |
| 1611 | |
| 1612 | if (s->state == SSL3_ST_SW_CERT_A) |
| 1613 | { |
| 1614 | x=ssl_get_server_send_cert(s); |
| 1615 | if (x == NULL) |
| 1616 | { |
| 1617 | SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR); |
| 1618 | return(0); |
| 1619 | } |
| 1620 | |
| 1621 | l=ssl3_output_cert_chain(s,x); |
| 1622 | s->state=SSL3_ST_SW_CERT_B; |
| 1623 | s->init_num=(int)l; |
| 1624 | s->init_off=0; |
| 1625 | } |
| 1626 | |
| 1627 | /* SSL3_ST_SW_CERT_B */ |
| 1628 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); |
| 1629 | } |