diff options
| author | cvs2svn <admin@example.com> | 2002-03-12 00:05:45 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2002-03-12 00:05:45 +0000 |
| commit | 3779f2f4a8b544a7e4c362915322726b66cff114 (patch) | |
| tree | b6d77e66cbfdf6c0d8953fba2917f26f86fa50f6 /src/lib/libssl/s3_srvr.c | |
| parent | f39945c2b3b0f9e4950384bdb8effdac6eed9199 (diff) | |
| download | openbsd-3779f2f4a8b544a7e4c362915322726b66cff114.tar.gz openbsd-3779f2f4a8b544a7e4c362915322726b66cff114.tar.bz2 openbsd-3779f2f4a8b544a7e4c362915322726b66cff114.zip | |
This commit was manufactured by cvs2git to create tag 'OPENBSD_3_1_BASE'.OPENBSD_3_1_BASE
Diffstat (limited to 'src/lib/libssl/s3_srvr.c')
| -rw-r--r-- | src/lib/libssl/s3_srvr.c | 1777 |
1 files changed, 0 insertions, 1777 deletions
diff --git a/src/lib/libssl/s3_srvr.c b/src/lib/libssl/s3_srvr.c deleted file mode 100644 index 258af84867..0000000000 --- a/src/lib/libssl/s3_srvr.c +++ /dev/null | |||
| @@ -1,1777 +0,0 @@ | |||
| 1 | /* ssl/s3_srvr.c */ | ||
| 2 | /* Copyright (C) 1995-1998 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 | #define NETSCAPE_HANG_BUG | ||
| 61 | |||
| 62 | |||
| 63 | #include <stdio.h> | ||
| 64 | #include <openssl/buffer.h> | ||
| 65 | #include <openssl/rand.h> | ||
| 66 | #include <openssl/objects.h> | ||
| 67 | #include <openssl/md5.h> | ||
| 68 | #include <openssl/sha.h> | ||
| 69 | #include <openssl/evp.h> | ||
| 70 | #include <openssl/x509.h> | ||
| 71 | #include "ssl_locl.h" | ||
| 72 | |||
| 73 | static SSL_METHOD *ssl3_get_server_method(int ver); | ||
| 74 | static int ssl3_get_client_hello(SSL *s); | ||
| 75 | static int ssl3_check_client_hello(SSL *s); | ||
| 76 | static int ssl3_send_server_hello(SSL *s); | ||
| 77 | static int ssl3_send_server_key_exchange(SSL *s); | ||
| 78 | static int ssl3_send_certificate_request(SSL *s); | ||
| 79 | static int ssl3_send_server_done(SSL *s); | ||
| 80 | static int ssl3_get_client_key_exchange(SSL *s); | ||
| 81 | static int ssl3_get_client_certificate(SSL *s); | ||
| 82 | static int ssl3_get_cert_verify(SSL *s); | ||
| 83 | static int ssl3_send_hello_request(SSL *s); | ||
| 84 | |||
| 85 | static SSL_METHOD *ssl3_get_server_method(int ver) | ||
| 86 | { | ||
| 87 | if (ver == SSL3_VERSION) | ||
| 88 | return(SSLv3_server_method()); | ||
| 89 | else | ||
| 90 | return(NULL); | ||
| 91 | } | ||
| 92 | |||
| 93 | SSL_METHOD *SSLv3_server_method(void) | ||
| 94 | { | ||
| 95 | static int init=1; | ||
| 96 | static SSL_METHOD SSLv3_server_data; | ||
| 97 | |||
| 98 | if (init) | ||
| 99 | { | ||
| 100 | memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(), | ||
| 101 | sizeof(SSL_METHOD)); | ||
| 102 | SSLv3_server_data.ssl_accept=ssl3_accept; | ||
| 103 | SSLv3_server_data.get_ssl_method=ssl3_get_server_method; | ||
| 104 | init=0; | ||
| 105 | } | ||
| 106 | return(&SSLv3_server_data); | ||
| 107 | } | ||
| 108 | |||
| 109 | int ssl3_accept(SSL *s) | ||
| 110 | { | ||
| 111 | BUF_MEM *buf; | ||
| 112 | unsigned long l,Time=time(NULL); | ||
| 113 | void (*cb)()=NULL; | ||
| 114 | long num1; | ||
| 115 | int ret= -1; | ||
| 116 | int new_state,state,skip=0; | ||
| 117 | |||
| 118 | RAND_add(&Time,sizeof(Time),0); | ||
| 119 | ERR_clear_error(); | ||
| 120 | clear_sys_error(); | ||
| 121 | |||
| 122 | if (s->info_callback != NULL) | ||
| 123 | cb=s->info_callback; | ||
| 124 | else if (s->ctx->info_callback != NULL) | ||
| 125 | cb=s->ctx->info_callback; | ||
| 126 | |||
| 127 | /* init things to blank */ | ||
| 128 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | ||
| 129 | s->in_handshake++; | ||
| 130 | |||
| 131 | if (s->cert == NULL) | ||
| 132 | { | ||
| 133 | SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); | ||
| 134 | return(-1); | ||
| 135 | } | ||
| 136 | |||
| 137 | for (;;) | ||
| 138 | { | ||
| 139 | state=s->state; | ||
| 140 | |||
| 141 | switch (s->state) | ||
| 142 | { | ||
| 143 | case SSL_ST_RENEGOTIATE: | ||
| 144 | s->new_session=1; | ||
| 145 | /* s->state=SSL_ST_ACCEPT; */ | ||
| 146 | |||
| 147 | case SSL_ST_BEFORE: | ||
| 148 | case SSL_ST_ACCEPT: | ||
| 149 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: | ||
| 150 | case SSL_ST_OK|SSL_ST_ACCEPT: | ||
| 151 | |||
| 152 | s->server=1; | ||
| 153 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | ||
| 154 | |||
| 155 | if ((s->version>>8) != 3) | ||
| 156 | { | ||
| 157 | SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR); | ||
| 158 | return -1; | ||
| 159 | } | ||
| 160 | s->type=SSL_ST_ACCEPT; | ||
| 161 | |||
| 162 | if (s->init_buf == NULL) | ||
| 163 | { | ||
| 164 | if ((buf=BUF_MEM_new()) == NULL) | ||
| 165 | { | ||
| 166 | ret= -1; | ||
| 167 | goto end; | ||
| 168 | } | ||
| 169 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) | ||
| 170 | { | ||
| 171 | ret= -1; | ||
| 172 | goto end; | ||
| 173 | } | ||
| 174 | s->init_buf=buf; | ||
| 175 | } | ||
| 176 | |||
| 177 | if (!ssl3_setup_buffers(s)) | ||
| 178 | { | ||
| 179 | ret= -1; | ||
| 180 | goto end; | ||
| 181 | } | ||
| 182 | |||
| 183 | /* Ok, we now need to push on a buffering BIO so that | ||
| 184 | * the output is sent in a way that TCP likes :-) | ||
| 185 | */ | ||
| 186 | if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } | ||
| 187 | |||
| 188 | s->init_num=0; | ||
| 189 | |||
| 190 | if (s->state != SSL_ST_RENEGOTIATE) | ||
| 191 | { | ||
| 192 | ssl3_init_finished_mac(s); | ||
| 193 | s->state=SSL3_ST_SR_CLNT_HELLO_A; | ||
| 194 | s->ctx->stats.sess_accept++; | ||
| 195 | } | ||
| 196 | else | ||
| 197 | { | ||
| 198 | s->ctx->stats.sess_accept_renegotiate++; | ||
| 199 | s->state=SSL3_ST_SW_HELLO_REQ_A; | ||
| 200 | } | ||
| 201 | break; | ||
| 202 | |||
| 203 | case SSL3_ST_SW_HELLO_REQ_A: | ||
| 204 | case SSL3_ST_SW_HELLO_REQ_B: | ||
| 205 | |||
| 206 | s->shutdown=0; | ||
| 207 | ret=ssl3_send_hello_request(s); | ||
| 208 | if (ret <= 0) goto end; | ||
| 209 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; | ||
| 210 | s->state=SSL3_ST_SW_FLUSH; | ||
| 211 | s->init_num=0; | ||
| 212 | |||
| 213 | ssl3_init_finished_mac(s); | ||
| 214 | break; | ||
| 215 | |||
| 216 | case SSL3_ST_SW_HELLO_REQ_C: | ||
| 217 | s->state=SSL_ST_OK; | ||
| 218 | ret=1; | ||
| 219 | goto end; | ||
| 220 | /* break; */ | ||
| 221 | |||
| 222 | case SSL3_ST_SR_CLNT_HELLO_A: | ||
| 223 | case SSL3_ST_SR_CLNT_HELLO_B: | ||
| 224 | case SSL3_ST_SR_CLNT_HELLO_C: | ||
| 225 | |||
| 226 | s->shutdown=0; | ||
| 227 | ret=ssl3_get_client_hello(s); | ||
| 228 | if (ret <= 0) goto end; | ||
| 229 | s->state=SSL3_ST_SW_SRVR_HELLO_A; | ||
| 230 | s->init_num=0; | ||
| 231 | break; | ||
| 232 | |||
| 233 | case SSL3_ST_SW_SRVR_HELLO_A: | ||
| 234 | case SSL3_ST_SW_SRVR_HELLO_B: | ||
| 235 | ret=ssl3_send_server_hello(s); | ||
| 236 | if (ret <= 0) goto end; | ||
| 237 | |||
| 238 | if (s->hit) | ||
| 239 | s->state=SSL3_ST_SW_CHANGE_A; | ||
| 240 | else | ||
| 241 | s->state=SSL3_ST_SW_CERT_A; | ||
| 242 | s->init_num=0; | ||
| 243 | break; | ||
| 244 | |||
| 245 | case SSL3_ST_SW_CERT_A: | ||
| 246 | case SSL3_ST_SW_CERT_B: | ||
| 247 | /* Check if it is anon DH */ | ||
| 248 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | ||
| 249 | { | ||
| 250 | ret=ssl3_send_server_certificate(s); | ||
| 251 | if (ret <= 0) goto end; | ||
| 252 | } | ||
| 253 | else | ||
| 254 | skip=1; | ||
| 255 | s->state=SSL3_ST_SW_KEY_EXCH_A; | ||
| 256 | s->init_num=0; | ||
| 257 | break; | ||
| 258 | |||
| 259 | case SSL3_ST_SW_KEY_EXCH_A: | ||
| 260 | case SSL3_ST_SW_KEY_EXCH_B: | ||
| 261 | l=s->s3->tmp.new_cipher->algorithms; | ||
| 262 | |||
| 263 | /* clear this, it may get reset by | ||
| 264 | * send_server_key_exchange */ | ||
| 265 | if (s->options & SSL_OP_EPHEMERAL_RSA) | ||
| 266 | s->s3->tmp.use_rsa_tmp=1; | ||
| 267 | else | ||
| 268 | s->s3->tmp.use_rsa_tmp=0; | ||
| 269 | |||
| 270 | /* only send if a DH key exchange, fortezza or | ||
| 271 | * RSA but we have a sign only certificate */ | ||
| 272 | if (s->s3->tmp.use_rsa_tmp | ||
| 273 | || (l & (SSL_DH|SSL_kFZA)) | ||
| 274 | || ((l & SSL_kRSA) | ||
| 275 | && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL | ||
| 276 | || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) | ||
| 277 | && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) | ||
| 278 | ) | ||
| 279 | ) | ||
| 280 | ) | ||
| 281 | ) | ||
| 282 | { | ||
| 283 | ret=ssl3_send_server_key_exchange(s); | ||
| 284 | if (ret <= 0) goto end; | ||
| 285 | } | ||
| 286 | else | ||
| 287 | skip=1; | ||
| 288 | |||
| 289 | s->state=SSL3_ST_SW_CERT_REQ_A; | ||
| 290 | s->init_num=0; | ||
| 291 | break; | ||
| 292 | |||
| 293 | case SSL3_ST_SW_CERT_REQ_A: | ||
| 294 | case SSL3_ST_SW_CERT_REQ_B: | ||
| 295 | if (/* don't request cert unless asked for it: */ | ||
| 296 | !(s->verify_mode & SSL_VERIFY_PEER) || | ||
| 297 | /* if SSL_VERIFY_CLIENT_ONCE is set, | ||
| 298 | * don't request cert during re-negotiation: */ | ||
| 299 | ((s->session->peer != NULL) && | ||
| 300 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || | ||
| 301 | /* never request cert in anonymous ciphersuites | ||
| 302 | * (see section "Certificate request" in SSL 3 drafts | ||
| 303 | * and in RFC 2246): */ | ||
| 304 | ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && | ||
| 305 | /* ... except when the application insists on verification | ||
| 306 | * (against the specs, but s3_clnt.c accepts this for SSL 3) */ | ||
| 307 | !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) | ||
| 308 | { | ||
| 309 | /* no cert request */ | ||
| 310 | skip=1; | ||
| 311 | s->s3->tmp.cert_request=0; | ||
| 312 | s->state=SSL3_ST_SW_SRVR_DONE_A; | ||
| 313 | } | ||
| 314 | else | ||
| 315 | { | ||
| 316 | s->s3->tmp.cert_request=1; | ||
| 317 | ret=ssl3_send_certificate_request(s); | ||
| 318 | if (ret <= 0) goto end; | ||
| 319 | #ifndef NETSCAPE_HANG_BUG | ||
| 320 | s->state=SSL3_ST_SW_SRVR_DONE_A; | ||
| 321 | #else | ||
| 322 | s->state=SSL3_ST_SW_FLUSH; | ||
| 323 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; | ||
| 324 | #endif | ||
| 325 | s->init_num=0; | ||
| 326 | } | ||
| 327 | break; | ||
| 328 | |||
| 329 | case SSL3_ST_SW_SRVR_DONE_A: | ||
| 330 | case SSL3_ST_SW_SRVR_DONE_B: | ||
| 331 | ret=ssl3_send_server_done(s); | ||
| 332 | if (ret <= 0) goto end; | ||
| 333 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; | ||
| 334 | s->state=SSL3_ST_SW_FLUSH; | ||
| 335 | s->init_num=0; | ||
| 336 | break; | ||
| 337 | |||
| 338 | case SSL3_ST_SW_FLUSH: | ||
| 339 | /* number of bytes to be flushed */ | ||
| 340 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | ||
| 341 | if (num1 > 0) | ||
| 342 | { | ||
| 343 | s->rwstate=SSL_WRITING; | ||
| 344 | num1=BIO_flush(s->wbio); | ||
| 345 | if (num1 <= 0) { ret= -1; goto end; } | ||
| 346 | s->rwstate=SSL_NOTHING; | ||
| 347 | } | ||
| 348 | |||
| 349 | s->state=s->s3->tmp.next_state; | ||
| 350 | break; | ||
| 351 | |||
| 352 | case SSL3_ST_SR_CERT_A: | ||
| 353 | case SSL3_ST_SR_CERT_B: | ||
| 354 | /* Check for second client hello (MS SGC) */ | ||
| 355 | ret = ssl3_check_client_hello(s); | ||
| 356 | if (ret <= 0) | ||
| 357 | goto end; | ||
| 358 | if (ret == 2) | ||
| 359 | s->state = SSL3_ST_SR_CLNT_HELLO_C; | ||
| 360 | else { | ||
| 361 | /* could be sent for a DH cert, even if we | ||
| 362 | * have not asked for it :-) */ | ||
| 363 | ret=ssl3_get_client_certificate(s); | ||
| 364 | if (ret <= 0) goto end; | ||
| 365 | s->init_num=0; | ||
| 366 | s->state=SSL3_ST_SR_KEY_EXCH_A; | ||
| 367 | } | ||
| 368 | break; | ||
| 369 | |||
| 370 | case SSL3_ST_SR_KEY_EXCH_A: | ||
| 371 | case SSL3_ST_SR_KEY_EXCH_B: | ||
| 372 | ret=ssl3_get_client_key_exchange(s); | ||
| 373 | if (ret <= 0) goto end; | ||
| 374 | s->state=SSL3_ST_SR_CERT_VRFY_A; | ||
| 375 | s->init_num=0; | ||
| 376 | |||
| 377 | /* We need to get hashes here so if there is | ||
| 378 | * a client cert, it can be verified */ | ||
| 379 | s->method->ssl3_enc->cert_verify_mac(s, | ||
| 380 | &(s->s3->finish_dgst1), | ||
| 381 | &(s->s3->tmp.cert_verify_md[0])); | ||
| 382 | s->method->ssl3_enc->cert_verify_mac(s, | ||
| 383 | &(s->s3->finish_dgst2), | ||
| 384 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); | ||
| 385 | |||
| 386 | break; | ||
| 387 | |||
| 388 | case SSL3_ST_SR_CERT_VRFY_A: | ||
| 389 | case SSL3_ST_SR_CERT_VRFY_B: | ||
| 390 | |||
| 391 | /* we should decide if we expected this one */ | ||
| 392 | ret=ssl3_get_cert_verify(s); | ||
| 393 | if (ret <= 0) goto end; | ||
| 394 | |||
| 395 | s->state=SSL3_ST_SR_FINISHED_A; | ||
| 396 | s->init_num=0; | ||
| 397 | break; | ||
| 398 | |||
| 399 | case SSL3_ST_SR_FINISHED_A: | ||
| 400 | case SSL3_ST_SR_FINISHED_B: | ||
| 401 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, | ||
| 402 | SSL3_ST_SR_FINISHED_B); | ||
| 403 | if (ret <= 0) goto end; | ||
| 404 | if (s->hit) | ||
| 405 | s->state=SSL_ST_OK; | ||
| 406 | else | ||
| 407 | s->state=SSL3_ST_SW_CHANGE_A; | ||
| 408 | s->init_num=0; | ||
| 409 | break; | ||
| 410 | |||
| 411 | case SSL3_ST_SW_CHANGE_A: | ||
| 412 | case SSL3_ST_SW_CHANGE_B: | ||
| 413 | |||
| 414 | s->session->cipher=s->s3->tmp.new_cipher; | ||
| 415 | if (!s->method->ssl3_enc->setup_key_block(s)) | ||
| 416 | { ret= -1; goto end; } | ||
| 417 | |||
| 418 | ret=ssl3_send_change_cipher_spec(s, | ||
| 419 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); | ||
| 420 | |||
| 421 | if (ret <= 0) goto end; | ||
| 422 | s->state=SSL3_ST_SW_FINISHED_A; | ||
| 423 | s->init_num=0; | ||
| 424 | |||
| 425 | if (!s->method->ssl3_enc->change_cipher_state(s, | ||
| 426 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) | ||
| 427 | { | ||
| 428 | ret= -1; | ||
| 429 | goto end; | ||
| 430 | } | ||
| 431 | |||
| 432 | break; | ||
| 433 | |||
| 434 | case SSL3_ST_SW_FINISHED_A: | ||
| 435 | case SSL3_ST_SW_FINISHED_B: | ||
| 436 | ret=ssl3_send_finished(s, | ||
| 437 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, | ||
| 438 | s->method->ssl3_enc->server_finished_label, | ||
| 439 | s->method->ssl3_enc->server_finished_label_len); | ||
| 440 | if (ret <= 0) goto end; | ||
| 441 | s->state=SSL3_ST_SW_FLUSH; | ||
| 442 | if (s->hit) | ||
| 443 | s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; | ||
| 444 | else | ||
| 445 | s->s3->tmp.next_state=SSL_ST_OK; | ||
| 446 | s->init_num=0; | ||
| 447 | break; | ||
| 448 | |||
| 449 | case SSL_ST_OK: | ||
| 450 | /* clean a few things up */ | ||
| 451 | ssl3_cleanup_key_block(s); | ||
| 452 | |||
| 453 | BUF_MEM_free(s->init_buf); | ||
| 454 | s->init_buf=NULL; | ||
| 455 | |||
| 456 | /* remove buffering on output */ | ||
| 457 | ssl_free_wbio_buffer(s); | ||
| 458 | |||
| 459 | s->new_session=0; | ||
| 460 | s->init_num=0; | ||
| 461 | |||
| 462 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); | ||
| 463 | |||
| 464 | s->ctx->stats.sess_accept_good++; | ||
| 465 | /* s->server=1; */ | ||
| 466 | s->handshake_func=ssl3_accept; | ||
| 467 | ret=1; | ||
| 468 | |||
| 469 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | ||
| 470 | |||
| 471 | goto end; | ||
| 472 | /* break; */ | ||
| 473 | |||
| 474 | default: | ||
| 475 | SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); | ||
| 476 | ret= -1; | ||
| 477 | goto end; | ||
| 478 | /* break; */ | ||
| 479 | } | ||
| 480 | |||
| 481 | if (!s->s3->tmp.reuse_message && !skip) | ||
| 482 | { | ||
| 483 | if (s->debug) | ||
| 484 | { | ||
| 485 | if ((ret=BIO_flush(s->wbio)) <= 0) | ||
| 486 | goto end; | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | if ((cb != NULL) && (s->state != state)) | ||
| 491 | { | ||
| 492 | new_state=s->state; | ||
| 493 | s->state=state; | ||
| 494 | cb(s,SSL_CB_ACCEPT_LOOP,1); | ||
| 495 | s->state=new_state; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | skip=0; | ||
| 499 | } | ||
| 500 | end: | ||
| 501 | /* BIO_flush(s->wbio); */ | ||
| 502 | |||
| 503 | if (cb != NULL) | ||
| 504 | cb(s,SSL_CB_ACCEPT_EXIT,ret); | ||
| 505 | s->in_handshake--; | ||
| 506 | return(ret); | ||
| 507 | } | ||
| 508 | |||
| 509 | static int ssl3_send_hello_request(SSL *s) | ||
| 510 | { | ||
| 511 | unsigned char *p; | ||
| 512 | |||
| 513 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) | ||
| 514 | { | ||
| 515 | p=(unsigned char *)s->init_buf->data; | ||
| 516 | *(p++)=SSL3_MT_HELLO_REQUEST; | ||
| 517 | *(p++)=0; | ||
| 518 | *(p++)=0; | ||
| 519 | *(p++)=0; | ||
| 520 | |||
| 521 | s->state=SSL3_ST_SW_HELLO_REQ_B; | ||
| 522 | /* number of bytes to write */ | ||
| 523 | s->init_num=4; | ||
| 524 | s->init_off=0; | ||
| 525 | } | ||
| 526 | |||
| 527 | /* SSL3_ST_SW_HELLO_REQ_B */ | ||
| 528 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 529 | } | ||
| 530 | |||
| 531 | static int ssl3_check_client_hello(SSL *s) | ||
| 532 | { | ||
| 533 | int ok; | ||
| 534 | long n; | ||
| 535 | |||
| 536 | n=ssl3_get_message(s, | ||
| 537 | SSL3_ST_SR_CERT_A, | ||
| 538 | SSL3_ST_SR_CERT_B, | ||
| 539 | -1, | ||
| 540 | SSL3_RT_MAX_PLAIN_LENGTH, | ||
| 541 | &ok); | ||
| 542 | if (!ok) return((int)n); | ||
| 543 | s->s3->tmp.reuse_message = 1; | ||
| 544 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) | ||
| 545 | { | ||
| 546 | /* Throw away what we have done so far in the current handshake, | ||
| 547 | * which will now be aborted. (A full SSL_clear would be too much.) | ||
| 548 | * I hope that tmp.dh is the only thing that may need to be cleared | ||
| 549 | * when a handshake is not completed ... */ | ||
| 550 | #ifndef NO_DH | ||
| 551 | if (s->s3->tmp.dh != NULL) | ||
| 552 | { | ||
| 553 | DH_free(s->s3->tmp.dh); | ||
| 554 | s->s3->tmp.dh = NULL; | ||
| 555 | } | ||
| 556 | #endif | ||
| 557 | return 2; | ||
| 558 | } | ||
| 559 | return 1; | ||
| 560 | } | ||
| 561 | |||
| 562 | static int ssl3_get_client_hello(SSL *s) | ||
| 563 | { | ||
| 564 | int i,j,ok,al,ret= -1; | ||
| 565 | long n; | ||
| 566 | unsigned long id; | ||
| 567 | unsigned char *p,*d,*q; | ||
| 568 | SSL_CIPHER *c; | ||
| 569 | SSL_COMP *comp=NULL; | ||
| 570 | STACK_OF(SSL_CIPHER) *ciphers=NULL; | ||
| 571 | |||
| 572 | /* We do this so that we will respond with our native type. | ||
| 573 | * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, | ||
| 574 | * This down switching should be handled by a different method. | ||
| 575 | * If we are SSLv3, we will respond with SSLv3, even if prompted with | ||
| 576 | * TLSv1. | ||
| 577 | */ | ||
| 578 | if (s->state == SSL3_ST_SR_CLNT_HELLO_A) | ||
| 579 | { | ||
| 580 | s->first_packet=1; | ||
| 581 | s->state=SSL3_ST_SR_CLNT_HELLO_B; | ||
| 582 | } | ||
| 583 | n=ssl3_get_message(s, | ||
| 584 | SSL3_ST_SR_CLNT_HELLO_B, | ||
| 585 | SSL3_ST_SR_CLNT_HELLO_C, | ||
| 586 | SSL3_MT_CLIENT_HELLO, | ||
| 587 | SSL3_RT_MAX_PLAIN_LENGTH, | ||
| 588 | &ok); | ||
| 589 | |||
| 590 | if (!ok) return((int)n); | ||
| 591 | d=p=(unsigned char *)s->init_buf->data; | ||
| 592 | |||
| 593 | /* use version from inside client hello, not from record header | ||
| 594 | * (may differ: see RFC 2246, Appendix E, second paragraph) */ | ||
| 595 | s->client_version=(((int)p[0])<<8)|(int)p[1]; | ||
| 596 | p+=2; | ||
| 597 | |||
| 598 | /* load the client random */ | ||
| 599 | memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); | ||
| 600 | p+=SSL3_RANDOM_SIZE; | ||
| 601 | |||
| 602 | /* get the session-id */ | ||
| 603 | j= *(p++); | ||
| 604 | |||
| 605 | s->hit=0; | ||
| 606 | if (j == 0) | ||
| 607 | { | ||
| 608 | if (!ssl_get_new_session(s,1)) | ||
| 609 | goto err; | ||
| 610 | } | ||
| 611 | else | ||
| 612 | { | ||
| 613 | i=ssl_get_prev_session(s,p,j); | ||
| 614 | if (i == 1) | ||
| 615 | { /* previous session */ | ||
| 616 | s->hit=1; | ||
| 617 | } | ||
| 618 | else if (i == -1) | ||
| 619 | goto err; | ||
| 620 | else /* i == 0 */ | ||
| 621 | { | ||
| 622 | if (!ssl_get_new_session(s,1)) | ||
| 623 | goto err; | ||
| 624 | } | ||
| 625 | } | ||
| 626 | |||
| 627 | p+=j; | ||
| 628 | n2s(p,i); | ||
| 629 | if ((i == 0) && (j != 0)) | ||
| 630 | { | ||
| 631 | /* we need a cipher if we are not resuming a session */ | ||
| 632 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
| 633 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); | ||
| 634 | goto f_err; | ||
| 635 | } | ||
| 636 | if ((i+p) > (d+n)) | ||
| 637 | { | ||
| 638 | /* not enough data */ | ||
| 639 | al=SSL_AD_DECODE_ERROR; | ||
| 640 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); | ||
| 641 | goto f_err; | ||
| 642 | } | ||
| 643 | if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) | ||
| 644 | == NULL)) | ||
| 645 | { | ||
| 646 | goto err; | ||
| 647 | } | ||
| 648 | p+=i; | ||
| 649 | |||
| 650 | /* If it is a hit, check that the cipher is in the list */ | ||
| 651 | if ((s->hit) && (i > 0)) | ||
| 652 | { | ||
| 653 | j=0; | ||
| 654 | id=s->session->cipher->id; | ||
| 655 | |||
| 656 | #ifdef CIPHER_DEBUG | ||
| 657 | printf("client sent %d ciphers\n",sk_num(ciphers)); | ||
| 658 | #endif | ||
| 659 | for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) | ||
| 660 | { | ||
| 661 | c=sk_SSL_CIPHER_value(ciphers,i); | ||
| 662 | #ifdef CIPHER_DEBUG | ||
| 663 | printf("client [%2d of %2d]:%s\n", | ||
| 664 | i,sk_num(ciphers),SSL_CIPHER_get_name(c)); | ||
| 665 | #endif | ||
| 666 | if (c->id == id) | ||
| 667 | { | ||
| 668 | j=1; | ||
| 669 | break; | ||
| 670 | } | ||
| 671 | } | ||
| 672 | if (j == 0) | ||
| 673 | { | ||
| 674 | if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) | ||
| 675 | { | ||
| 676 | /* Very bad for multi-threading.... */ | ||
| 677 | s->session->cipher=sk_SSL_CIPHER_value(ciphers, | ||
| 678 | 0); | ||
| 679 | } | ||
| 680 | else | ||
| 681 | { | ||
| 682 | /* we need to have the cipher in the cipher | ||
| 683 | * list if we are asked to reuse it */ | ||
| 684 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
| 685 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING); | ||
| 686 | goto f_err; | ||
| 687 | } | ||
| 688 | } | ||
| 689 | } | ||
| 690 | |||
| 691 | /* compression */ | ||
| 692 | i= *(p++); | ||
| 693 | q=p; | ||
| 694 | for (j=0; j<i; j++) | ||
| 695 | { | ||
| 696 | if (p[j] == 0) break; | ||
| 697 | } | ||
| 698 | |||
| 699 | p+=i; | ||
| 700 | if (j >= i) | ||
| 701 | { | ||
| 702 | /* no compress */ | ||
| 703 | al=SSL_AD_DECODE_ERROR; | ||
| 704 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); | ||
| 705 | goto f_err; | ||
| 706 | } | ||
| 707 | |||
| 708 | /* Worst case, we will use the NULL compression, but if we have other | ||
| 709 | * options, we will now look for them. We have i-1 compression | ||
| 710 | * algorithms from the client, starting at q. */ | ||
| 711 | s->s3->tmp.new_compression=NULL; | ||
| 712 | if (s->ctx->comp_methods != NULL) | ||
| 713 | { /* See if we have a match */ | ||
| 714 | int m,nn,o,v,done=0; | ||
| 715 | |||
| 716 | nn=sk_SSL_COMP_num(s->ctx->comp_methods); | ||
| 717 | for (m=0; m<nn; m++) | ||
| 718 | { | ||
| 719 | comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); | ||
| 720 | v=comp->id; | ||
| 721 | for (o=0; o<i; o++) | ||
| 722 | { | ||
| 723 | if (v == q[o]) | ||
| 724 | { | ||
| 725 | done=1; | ||
| 726 | break; | ||
| 727 | } | ||
| 728 | } | ||
| 729 | if (done) break; | ||
| 730 | } | ||
| 731 | if (done) | ||
| 732 | s->s3->tmp.new_compression=comp; | ||
| 733 | else | ||
| 734 | comp=NULL; | ||
| 735 | } | ||
| 736 | |||
| 737 | /* TLS does not mind if there is extra stuff */ | ||
| 738 | if (s->version == SSL3_VERSION) | ||
| 739 | { | ||
| 740 | if (p > (d+n)) | ||
| 741 | { | ||
| 742 | /* wrong number of bytes, | ||
| 743 | * there could be more to follow */ | ||
| 744 | al=SSL_AD_DECODE_ERROR; | ||
| 745 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); | ||
| 746 | goto f_err; | ||
| 747 | } | ||
| 748 | } | ||
| 749 | |||
| 750 | /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must | ||
| 751 | * pick a cipher */ | ||
| 752 | |||
| 753 | if (!s->hit) | ||
| 754 | { | ||
| 755 | s->session->compress_meth=(comp == NULL)?0:comp->id; | ||
| 756 | if (s->session->ciphers != NULL) | ||
| 757 | sk_SSL_CIPHER_free(s->session->ciphers); | ||
| 758 | s->session->ciphers=ciphers; | ||
| 759 | if (ciphers == NULL) | ||
| 760 | { | ||
| 761 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
| 762 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); | ||
| 763 | goto f_err; | ||
| 764 | } | ||
| 765 | ciphers=NULL; | ||
| 766 | c=ssl3_choose_cipher(s,s->session->ciphers, | ||
| 767 | ssl_get_ciphers_by_id(s)); | ||
| 768 | |||
| 769 | if (c == NULL) | ||
| 770 | { | ||
| 771 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 772 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); | ||
| 773 | goto f_err; | ||
| 774 | } | ||
| 775 | s->s3->tmp.new_cipher=c; | ||
| 776 | } | ||
| 777 | else | ||
| 778 | { | ||
| 779 | /* Session-id reuse */ | ||
| 780 | #ifdef REUSE_CIPHER_BUG | ||
| 781 | STACK_OF(SSL_CIPHER) *sk; | ||
| 782 | SSL_CIPHER *nc=NULL; | ||
| 783 | SSL_CIPHER *ec=NULL; | ||
| 784 | |||
| 785 | if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) | ||
| 786 | { | ||
| 787 | sk=s->session->ciphers; | ||
| 788 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) | ||
| 789 | { | ||
| 790 | c=sk_SSL_CIPHER_value(sk,i); | ||
| 791 | if (c->algorithms & SSL_eNULL) | ||
| 792 | nc=c; | ||
| 793 | if (SSL_C_IS_EXPORT(c)) | ||
| 794 | ec=c; | ||
| 795 | } | ||
| 796 | if (nc != NULL) | ||
| 797 | s->s3->tmp.new_cipher=nc; | ||
| 798 | else if (ec != NULL) | ||
| 799 | s->s3->tmp.new_cipher=ec; | ||
| 800 | else | ||
| 801 | s->s3->tmp.new_cipher=s->session->cipher; | ||
| 802 | } | ||
| 803 | else | ||
| 804 | #endif | ||
| 805 | s->s3->tmp.new_cipher=s->session->cipher; | ||
| 806 | } | ||
| 807 | |||
| 808 | /* we now have the following setup. | ||
| 809 | * client_random | ||
| 810 | * cipher_list - our prefered list of ciphers | ||
| 811 | * ciphers - the clients prefered list of ciphers | ||
| 812 | * compression - basically ignored right now | ||
| 813 | * ssl version is set - sslv3 | ||
| 814 | * s->session - The ssl session has been setup. | ||
| 815 | * s->hit - session reuse flag | ||
| 816 | * s->tmp.new_cipher - the new cipher to use. | ||
| 817 | */ | ||
| 818 | |||
| 819 | ret=1; | ||
| 820 | if (0) | ||
| 821 | { | ||
| 822 | f_err: | ||
| 823 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
| 824 | } | ||
| 825 | err: | ||
| 826 | if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); | ||
| 827 | return(ret); | ||
| 828 | } | ||
| 829 | |||
| 830 | static int ssl3_send_server_hello(SSL *s) | ||
| 831 | { | ||
| 832 | unsigned char *buf; | ||
| 833 | unsigned char *p,*d; | ||
| 834 | int i,sl; | ||
| 835 | unsigned long l,Time; | ||
| 836 | |||
| 837 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) | ||
| 838 | { | ||
| 839 | buf=(unsigned char *)s->init_buf->data; | ||
| 840 | p=s->s3->server_random; | ||
| 841 | Time=time(NULL); /* Time */ | ||
| 842 | l2n(Time,p); | ||
| 843 | RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); | ||
| 844 | /* Do the message type and length last */ | ||
| 845 | d=p= &(buf[4]); | ||
| 846 | |||
| 847 | *(p++)=s->version>>8; | ||
| 848 | *(p++)=s->version&0xff; | ||
| 849 | |||
| 850 | /* Random stuff */ | ||
| 851 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | ||
| 852 | p+=SSL3_RANDOM_SIZE; | ||
| 853 | |||
| 854 | /* now in theory we have 3 options to sending back the | ||
| 855 | * session id. If it is a re-use, we send back the | ||
| 856 | * old session-id, if it is a new session, we send | ||
| 857 | * back the new session-id or we send back a 0 length | ||
| 858 | * session-id if we want it to be single use. | ||
| 859 | * Currently I will not implement the '0' length session-id | ||
| 860 | * 12-Jan-98 - I'll now support the '0' length stuff. | ||
| 861 | */ | ||
| 862 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) | ||
| 863 | s->session->session_id_length=0; | ||
| 864 | |||
| 865 | sl=s->session->session_id_length; | ||
| 866 | *(p++)=sl; | ||
| 867 | memcpy(p,s->session->session_id,sl); | ||
| 868 | p+=sl; | ||
| 869 | |||
| 870 | /* put the cipher */ | ||
| 871 | i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); | ||
| 872 | p+=i; | ||
| 873 | |||
| 874 | /* put the compression method */ | ||
| 875 | if (s->s3->tmp.new_compression == NULL) | ||
| 876 | *(p++)=0; | ||
| 877 | else | ||
| 878 | *(p++)=s->s3->tmp.new_compression->id; | ||
| 879 | |||
| 880 | /* do the header */ | ||
| 881 | l=(p-d); | ||
| 882 | d=buf; | ||
| 883 | *(d++)=SSL3_MT_SERVER_HELLO; | ||
| 884 | l2n3(l,d); | ||
| 885 | |||
| 886 | s->state=SSL3_ST_CW_CLNT_HELLO_B; | ||
| 887 | /* number of bytes to write */ | ||
| 888 | s->init_num=p-buf; | ||
| 889 | s->init_off=0; | ||
| 890 | } | ||
| 891 | |||
| 892 | /* SSL3_ST_CW_CLNT_HELLO_B */ | ||
| 893 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 894 | } | ||
| 895 | |||
| 896 | static int ssl3_send_server_done(SSL *s) | ||
| 897 | { | ||
| 898 | unsigned char *p; | ||
| 899 | |||
| 900 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) | ||
| 901 | { | ||
| 902 | p=(unsigned char *)s->init_buf->data; | ||
| 903 | |||
| 904 | /* do the header */ | ||
| 905 | *(p++)=SSL3_MT_SERVER_DONE; | ||
| 906 | *(p++)=0; | ||
| 907 | *(p++)=0; | ||
| 908 | *(p++)=0; | ||
| 909 | |||
| 910 | s->state=SSL3_ST_SW_SRVR_DONE_B; | ||
| 911 | /* number of bytes to write */ | ||
| 912 | s->init_num=4; | ||
| 913 | s->init_off=0; | ||
| 914 | } | ||
| 915 | |||
| 916 | /* SSL3_ST_CW_CLNT_HELLO_B */ | ||
| 917 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 918 | } | ||
| 919 | |||
| 920 | static int ssl3_send_server_key_exchange(SSL *s) | ||
| 921 | { | ||
| 922 | #ifndef NO_RSA | ||
| 923 | unsigned char *q; | ||
| 924 | int j,num; | ||
| 925 | RSA *rsa; | ||
| 926 | unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | ||
| 927 | unsigned int u; | ||
| 928 | #endif | ||
| 929 | #ifndef NO_DH | ||
| 930 | DH *dh=NULL,*dhp; | ||
| 931 | #endif | ||
| 932 | EVP_PKEY *pkey; | ||
| 933 | unsigned char *p,*d; | ||
| 934 | int al,i; | ||
| 935 | unsigned long type; | ||
| 936 | int n; | ||
| 937 | CERT *cert; | ||
| 938 | BIGNUM *r[4]; | ||
| 939 | int nr[4],kn; | ||
| 940 | BUF_MEM *buf; | ||
| 941 | EVP_MD_CTX md_ctx; | ||
| 942 | |||
| 943 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) | ||
| 944 | { | ||
| 945 | type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; | ||
| 946 | cert=s->cert; | ||
| 947 | |||
| 948 | buf=s->init_buf; | ||
| 949 | |||
| 950 | r[0]=r[1]=r[2]=r[3]=NULL; | ||
| 951 | n=0; | ||
| 952 | #ifndef NO_RSA | ||
| 953 | if (type & SSL_kRSA) | ||
| 954 | { | ||
| 955 | rsa=cert->rsa_tmp; | ||
| 956 | if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) | ||
| 957 | { | ||
| 958 | rsa=s->cert->rsa_tmp_cb(s, | ||
| 959 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | ||
| 960 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
| 961 | if(rsa == NULL) | ||
| 962 | { | ||
| 963 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 964 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); | ||
| 965 | goto f_err; | ||
| 966 | } | ||
| 967 | CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA); | ||
| 968 | cert->rsa_tmp=rsa; | ||
| 969 | } | ||
| 970 | if (rsa == NULL) | ||
| 971 | { | ||
| 972 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 973 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); | ||
| 974 | goto f_err; | ||
| 975 | } | ||
| 976 | r[0]=rsa->n; | ||
| 977 | r[1]=rsa->e; | ||
| 978 | s->s3->tmp.use_rsa_tmp=1; | ||
| 979 | } | ||
| 980 | else | ||
| 981 | #endif | ||
| 982 | #ifndef NO_DH | ||
| 983 | if (type & SSL_kEDH) | ||
| 984 | { | ||
| 985 | dhp=cert->dh_tmp; | ||
| 986 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | ||
| 987 | dhp=s->cert->dh_tmp_cb(s, | ||
| 988 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | ||
| 989 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
| 990 | if (dhp == NULL) | ||
| 991 | { | ||
| 992 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 993 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); | ||
| 994 | goto f_err; | ||
| 995 | } | ||
| 996 | |||
| 997 | if (s->s3->tmp.dh != NULL) | ||
| 998 | { | ||
| 999 | DH_free(dh); | ||
| 1000 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR); | ||
| 1001 | goto err; | ||
| 1002 | } | ||
| 1003 | |||
| 1004 | if ((dh=DHparams_dup(dhp)) == NULL) | ||
| 1005 | { | ||
| 1006 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); | ||
| 1007 | goto err; | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | s->s3->tmp.dh=dh; | ||
| 1011 | if ((dhp->pub_key == NULL || | ||
| 1012 | dhp->priv_key == NULL || | ||
| 1013 | (s->options & SSL_OP_SINGLE_DH_USE))) | ||
| 1014 | { | ||
| 1015 | if(!DH_generate_key(dh)) | ||
| 1016 | { | ||
| 1017 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
| 1018 | ERR_R_DH_LIB); | ||
| 1019 | goto err; | ||
| 1020 | } | ||
| 1021 | } | ||
| 1022 | else | ||
| 1023 | { | ||
| 1024 | dh->pub_key=BN_dup(dhp->pub_key); | ||
| 1025 | dh->priv_key=BN_dup(dhp->priv_key); | ||
| 1026 | if ((dh->pub_key == NULL) || | ||
| 1027 | (dh->priv_key == NULL)) | ||
| 1028 | { | ||
| 1029 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); | ||
| 1030 | goto err; | ||
| 1031 | } | ||
| 1032 | } | ||
| 1033 | r[0]=dh->p; | ||
| 1034 | r[1]=dh->g; | ||
| 1035 | r[2]=dh->pub_key; | ||
| 1036 | } | ||
| 1037 | else | ||
| 1038 | #endif | ||
| 1039 | { | ||
| 1040 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1041 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | ||
| 1042 | goto f_err; | ||
| 1043 | } | ||
| 1044 | for (i=0; r[i] != NULL; i++) | ||
| 1045 | { | ||
| 1046 | nr[i]=BN_num_bytes(r[i]); | ||
| 1047 | n+=2+nr[i]; | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | ||
| 1051 | { | ||
| 1052 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) | ||
| 1053 | == NULL) | ||
| 1054 | { | ||
| 1055 | al=SSL_AD_DECODE_ERROR; | ||
| 1056 | goto f_err; | ||
| 1057 | } | ||
| 1058 | kn=EVP_PKEY_size(pkey); | ||
| 1059 | } | ||
| 1060 | else | ||
| 1061 | { | ||
| 1062 | pkey=NULL; | ||
| 1063 | kn=0; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | if (!BUF_MEM_grow(buf,n+4+kn)) | ||
| 1067 | { | ||
| 1068 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); | ||
| 1069 | goto err; | ||
| 1070 | } | ||
| 1071 | d=(unsigned char *)s->init_buf->data; | ||
| 1072 | p= &(d[4]); | ||
| 1073 | |||
| 1074 | for (i=0; r[i] != NULL; i++) | ||
| 1075 | { | ||
| 1076 | s2n(nr[i],p); | ||
| 1077 | BN_bn2bin(r[i],p); | ||
| 1078 | p+=nr[i]; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | /* not anonymous */ | ||
| 1082 | if (pkey != NULL) | ||
| 1083 | { | ||
| 1084 | /* n is the length of the params, they start at &(d[4]) | ||
| 1085 | * and p points to the space at the end. */ | ||
| 1086 | #ifndef NO_RSA | ||
| 1087 | if (pkey->type == EVP_PKEY_RSA) | ||
| 1088 | { | ||
| 1089 | q=md_buf; | ||
| 1090 | j=0; | ||
| 1091 | for (num=2; num > 0; num--) | ||
| 1092 | { | ||
| 1093 | EVP_DigestInit(&md_ctx,(num == 2) | ||
| 1094 | ?s->ctx->md5:s->ctx->sha1); | ||
| 1095 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); | ||
| 1096 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | ||
| 1097 | EVP_DigestUpdate(&md_ctx,&(d[4]),n); | ||
| 1098 | EVP_DigestFinal(&md_ctx,q, | ||
| 1099 | (unsigned int *)&i); | ||
| 1100 | q+=i; | ||
| 1101 | j+=i; | ||
| 1102 | } | ||
| 1103 | if (RSA_sign(NID_md5_sha1, md_buf, j, | ||
| 1104 | &(p[2]), &u, pkey->pkey.rsa) <= 0) | ||
| 1105 | { | ||
| 1106 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); | ||
| 1107 | goto err; | ||
| 1108 | } | ||
| 1109 | s2n(u,p); | ||
| 1110 | n+=u+2; | ||
| 1111 | } | ||
| 1112 | else | ||
| 1113 | #endif | ||
| 1114 | #if !defined(NO_DSA) | ||
| 1115 | if (pkey->type == EVP_PKEY_DSA) | ||
| 1116 | { | ||
| 1117 | /* lets do DSS */ | ||
| 1118 | EVP_SignInit(&md_ctx,EVP_dss1()); | ||
| 1119 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); | ||
| 1120 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | ||
| 1121 | EVP_SignUpdate(&md_ctx,&(d[4]),n); | ||
| 1122 | if (!EVP_SignFinal(&md_ctx,&(p[2]), | ||
| 1123 | (unsigned int *)&i,pkey)) | ||
| 1124 | { | ||
| 1125 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); | ||
| 1126 | goto err; | ||
| 1127 | } | ||
| 1128 | s2n(i,p); | ||
| 1129 | n+=i+2; | ||
| 1130 | } | ||
| 1131 | else | ||
| 1132 | #endif | ||
| 1133 | { | ||
| 1134 | /* Is this error check actually needed? */ | ||
| 1135 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1136 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); | ||
| 1137 | goto f_err; | ||
| 1138 | } | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; | ||
| 1142 | l2n3(n,d); | ||
| 1143 | |||
| 1144 | /* we should now have things packed up, so lets send | ||
| 1145 | * it off */ | ||
| 1146 | s->init_num=n+4; | ||
| 1147 | s->init_off=0; | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | s->state = SSL3_ST_SW_KEY_EXCH_B; | ||
| 1151 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 1152 | f_err: | ||
| 1153 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
| 1154 | err: | ||
| 1155 | return(-1); | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | static int ssl3_send_certificate_request(SSL *s) | ||
| 1159 | { | ||
| 1160 | unsigned char *p,*d; | ||
| 1161 | int i,j,nl,off,n; | ||
| 1162 | STACK_OF(X509_NAME) *sk=NULL; | ||
| 1163 | X509_NAME *name; | ||
| 1164 | BUF_MEM *buf; | ||
| 1165 | |||
| 1166 | if (s->state == SSL3_ST_SW_CERT_REQ_A) | ||
| 1167 | { | ||
| 1168 | buf=s->init_buf; | ||
| 1169 | |||
| 1170 | d=p=(unsigned char *)&(buf->data[4]); | ||
| 1171 | |||
| 1172 | /* get the list of acceptable cert types */ | ||
| 1173 | p++; | ||
| 1174 | n=ssl3_get_req_cert_type(s,p); | ||
| 1175 | d[0]=n; | ||
| 1176 | p+=n; | ||
| 1177 | n++; | ||
| 1178 | |||
| 1179 | off=n; | ||
| 1180 | p+=2; | ||
| 1181 | n+=2; | ||
| 1182 | |||
| 1183 | sk=SSL_get_client_CA_list(s); | ||
| 1184 | nl=0; | ||
| 1185 | if (sk != NULL) | ||
| 1186 | { | ||
| 1187 | for (i=0; i<sk_X509_NAME_num(sk); i++) | ||
| 1188 | { | ||
| 1189 | name=sk_X509_NAME_value(sk,i); | ||
| 1190 | j=i2d_X509_NAME(name,NULL); | ||
| 1191 | if (!BUF_MEM_grow(buf,4+n+j+2)) | ||
| 1192 | { | ||
| 1193 | SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); | ||
| 1194 | goto err; | ||
| 1195 | } | ||
| 1196 | p=(unsigned char *)&(buf->data[4+n]); | ||
| 1197 | if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) | ||
| 1198 | { | ||
| 1199 | s2n(j,p); | ||
| 1200 | i2d_X509_NAME(name,&p); | ||
| 1201 | n+=2+j; | ||
| 1202 | nl+=2+j; | ||
| 1203 | } | ||
| 1204 | else | ||
| 1205 | { | ||
| 1206 | d=p; | ||
| 1207 | i2d_X509_NAME(name,&p); | ||
| 1208 | j-=2; s2n(j,d); j+=2; | ||
| 1209 | n+=j; | ||
| 1210 | nl+=j; | ||
| 1211 | } | ||
| 1212 | } | ||
| 1213 | } | ||
| 1214 | /* else no CA names */ | ||
| 1215 | p=(unsigned char *)&(buf->data[4+off]); | ||
| 1216 | s2n(nl,p); | ||
| 1217 | |||
| 1218 | d=(unsigned char *)buf->data; | ||
| 1219 | *(d++)=SSL3_MT_CERTIFICATE_REQUEST; | ||
| 1220 | l2n3(n,d); | ||
| 1221 | |||
| 1222 | /* we should now have things packed up, so lets send | ||
| 1223 | * it off */ | ||
| 1224 | |||
| 1225 | s->init_num=n+4; | ||
| 1226 | s->init_off=0; | ||
| 1227 | #ifdef NETSCAPE_HANG_BUG | ||
| 1228 | p=(unsigned char *)s->init_buf->data + s->init_num; | ||
| 1229 | |||
| 1230 | /* do the header */ | ||
| 1231 | *(p++)=SSL3_MT_SERVER_DONE; | ||
| 1232 | *(p++)=0; | ||
| 1233 | *(p++)=0; | ||
| 1234 | *(p++)=0; | ||
| 1235 | s->init_num += 4; | ||
| 1236 | #endif | ||
| 1237 | |||
| 1238 | } | ||
| 1239 | |||
| 1240 | /* SSL3_ST_SW_CERT_REQ_B */ | ||
| 1241 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 1242 | err: | ||
| 1243 | return(-1); | ||
| 1244 | } | ||
| 1245 | |||
| 1246 | static int ssl3_get_client_key_exchange(SSL *s) | ||
| 1247 | { | ||
| 1248 | int i,al,ok; | ||
| 1249 | long n; | ||
| 1250 | unsigned long l; | ||
| 1251 | unsigned char *p; | ||
| 1252 | #ifndef NO_RSA | ||
| 1253 | RSA *rsa=NULL; | ||
| 1254 | EVP_PKEY *pkey=NULL; | ||
| 1255 | #endif | ||
| 1256 | #ifndef NO_DH | ||
| 1257 | BIGNUM *pub=NULL; | ||
| 1258 | DH *dh_srvr; | ||
| 1259 | #endif | ||
| 1260 | |||
| 1261 | n=ssl3_get_message(s, | ||
| 1262 | SSL3_ST_SR_KEY_EXCH_A, | ||
| 1263 | SSL3_ST_SR_KEY_EXCH_B, | ||
| 1264 | SSL3_MT_CLIENT_KEY_EXCHANGE, | ||
| 1265 | 400, /* ???? */ | ||
| 1266 | &ok); | ||
| 1267 | |||
| 1268 | if (!ok) return((int)n); | ||
| 1269 | p=(unsigned char *)s->init_buf->data; | ||
| 1270 | |||
| 1271 | l=s->s3->tmp.new_cipher->algorithms; | ||
| 1272 | |||
| 1273 | #ifndef NO_RSA | ||
| 1274 | if (l & SSL_kRSA) | ||
| 1275 | { | ||
| 1276 | /* FIX THIS UP EAY EAY EAY EAY */ | ||
| 1277 | if (s->s3->tmp.use_rsa_tmp) | ||
| 1278 | { | ||
| 1279 | if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) | ||
| 1280 | rsa=s->cert->rsa_tmp; | ||
| 1281 | /* Don't do a callback because rsa_tmp should | ||
| 1282 | * be sent already */ | ||
| 1283 | if (rsa == NULL) | ||
| 1284 | { | ||
| 1285 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1286 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); | ||
| 1287 | goto f_err; | ||
| 1288 | |||
| 1289 | } | ||
| 1290 | } | ||
| 1291 | else | ||
| 1292 | { | ||
| 1293 | pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; | ||
| 1294 | if ( (pkey == NULL) || | ||
| 1295 | (pkey->type != EVP_PKEY_RSA) || | ||
| 1296 | (pkey->pkey.rsa == NULL)) | ||
| 1297 | { | ||
| 1298 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1299 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); | ||
| 1300 | goto f_err; | ||
| 1301 | } | ||
| 1302 | rsa=pkey->pkey.rsa; | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | /* TLS */ | ||
| 1306 | if (s->version > SSL3_VERSION) | ||
| 1307 | { | ||
| 1308 | n2s(p,i); | ||
| 1309 | if (n != i+2) | ||
| 1310 | { | ||
| 1311 | if (!(s->options & SSL_OP_TLS_D5_BUG)) | ||
| 1312 | { | ||
| 1313 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); | ||
| 1314 | goto err; | ||
| 1315 | } | ||
| 1316 | else | ||
| 1317 | p-=2; | ||
| 1318 | } | ||
| 1319 | else | ||
| 1320 | n=i; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); | ||
| 1324 | |||
| 1325 | al = -1; | ||
| 1326 | |||
| 1327 | if (i != SSL_MAX_MASTER_KEY_LENGTH) | ||
| 1328 | { | ||
| 1329 | al=SSL_AD_DECODE_ERROR; | ||
| 1330 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); | ||
| 1331 | } | ||
| 1332 | |||
| 1333 | if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff)))) | ||
| 1334 | { | ||
| 1335 | /* The premaster secret must contain the same version number as the | ||
| 1336 | * ClientHello to detect version rollback attacks (strangely, the | ||
| 1337 | * protocol does not offer such protection for DH ciphersuites). | ||
| 1338 | * However, buggy clients exist that send the negotiated protocol | ||
| 1339 | * version instead if the server does not support the requested | ||
| 1340 | * protocol version. | ||
| 1341 | * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */ | ||
| 1342 | if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && | ||
| 1343 | (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff)))) | ||
| 1344 | { | ||
| 1345 | al=SSL_AD_DECODE_ERROR; | ||
| 1346 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); | ||
| 1347 | goto f_err; | ||
| 1348 | } | ||
| 1349 | } | ||
| 1350 | |||
| 1351 | if (al != -1) | ||
| 1352 | { | ||
| 1353 | #if 0 | ||
| 1354 | goto f_err; | ||
| 1355 | #else | ||
| 1356 | /* Some decryption failure -- use random value instead as countermeasure | ||
| 1357 | * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding | ||
| 1358 | * (see RFC 2246, section 7.4.7.1). | ||
| 1359 | * But note that due to length and protocol version checking, the | ||
| 1360 | * attack is impractical anyway (see section 5 in D. Bleichenbacher: | ||
| 1361 | * "Chosen Ciphertext Attacks Against Protocols Based on the RSA | ||
| 1362 | * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12). | ||
| 1363 | */ | ||
| 1364 | ERR_clear_error(); | ||
| 1365 | i = SSL_MAX_MASTER_KEY_LENGTH; | ||
| 1366 | p[0] = s->client_version >> 8; | ||
| 1367 | p[1] = s->client_version & 0xff; | ||
| 1368 | RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */ | ||
| 1369 | #endif | ||
| 1370 | } | ||
| 1371 | |||
| 1372 | s->session->master_key_length= | ||
| 1373 | s->method->ssl3_enc->generate_master_secret(s, | ||
| 1374 | s->session->master_key, | ||
| 1375 | p,i); | ||
| 1376 | memset(p,0,i); | ||
| 1377 | } | ||
| 1378 | else | ||
| 1379 | #endif | ||
| 1380 | #ifndef NO_DH | ||
| 1381 | if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | ||
| 1382 | { | ||
| 1383 | n2s(p,i); | ||
| 1384 | if (n != i+2) | ||
| 1385 | { | ||
| 1386 | if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) | ||
| 1387 | { | ||
| 1388 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); | ||
| 1389 | goto err; | ||
| 1390 | } | ||
| 1391 | else | ||
| 1392 | { | ||
| 1393 | p-=2; | ||
| 1394 | i=(int)n; | ||
| 1395 | } | ||
| 1396 | } | ||
| 1397 | |||
| 1398 | if (n == 0L) /* the parameters are in the cert */ | ||
| 1399 | { | ||
| 1400 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1401 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); | ||
| 1402 | goto f_err; | ||
| 1403 | } | ||
| 1404 | else | ||
| 1405 | { | ||
| 1406 | if (s->s3->tmp.dh == NULL) | ||
| 1407 | { | ||
| 1408 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1409 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); | ||
| 1410 | goto f_err; | ||
| 1411 | } | ||
| 1412 | else | ||
| 1413 | dh_srvr=s->s3->tmp.dh; | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | pub=BN_bin2bn(p,i,NULL); | ||
| 1417 | if (pub == NULL) | ||
| 1418 | { | ||
| 1419 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); | ||
| 1420 | goto err; | ||
| 1421 | } | ||
| 1422 | |||
| 1423 | i=DH_compute_key(p,pub,dh_srvr); | ||
| 1424 | |||
| 1425 | if (i <= 0) | ||
| 1426 | { | ||
| 1427 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); | ||
| 1428 | goto err; | ||
| 1429 | } | ||
| 1430 | |||
| 1431 | DH_free(s->s3->tmp.dh); | ||
| 1432 | s->s3->tmp.dh=NULL; | ||
| 1433 | |||
| 1434 | BN_clear_free(pub); | ||
| 1435 | pub=NULL; | ||
| 1436 | s->session->master_key_length= | ||
| 1437 | s->method->ssl3_enc->generate_master_secret(s, | ||
| 1438 | s->session->master_key,p,i); | ||
| 1439 | memset(p,0,i); | ||
| 1440 | } | ||
| 1441 | else | ||
| 1442 | #endif | ||
| 1443 | { | ||
| 1444 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1445 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE); | ||
| 1446 | goto f_err; | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | return(1); | ||
| 1450 | f_err: | ||
| 1451 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
| 1452 | #if !defined(NO_DH) || !defined(NO_RSA) | ||
| 1453 | err: | ||
| 1454 | #endif | ||
| 1455 | return(-1); | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | static int ssl3_get_cert_verify(SSL *s) | ||
| 1459 | { | ||
| 1460 | EVP_PKEY *pkey=NULL; | ||
| 1461 | unsigned char *p; | ||
| 1462 | int al,ok,ret=0; | ||
| 1463 | long n; | ||
| 1464 | int type=0,i,j; | ||
| 1465 | X509 *peer; | ||
| 1466 | |||
| 1467 | n=ssl3_get_message(s, | ||
| 1468 | SSL3_ST_SR_CERT_VRFY_A, | ||
| 1469 | SSL3_ST_SR_CERT_VRFY_B, | ||
| 1470 | -1, | ||
| 1471 | 512, /* 512? */ | ||
| 1472 | &ok); | ||
| 1473 | |||
| 1474 | if (!ok) return((int)n); | ||
| 1475 | |||
| 1476 | if (s->session->peer != NULL) | ||
| 1477 | { | ||
| 1478 | peer=s->session->peer; | ||
| 1479 | pkey=X509_get_pubkey(peer); | ||
| 1480 | type=X509_certificate_type(peer,pkey); | ||
| 1481 | } | ||
| 1482 | else | ||
| 1483 | { | ||
| 1484 | peer=NULL; | ||
| 1485 | pkey=NULL; | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) | ||
| 1489 | { | ||
| 1490 | s->s3->tmp.reuse_message=1; | ||
| 1491 | if ((peer != NULL) && (type | EVP_PKT_SIGN)) | ||
| 1492 | { | ||
| 1493 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
| 1494 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); | ||
| 1495 | goto f_err; | ||
| 1496 | } | ||
| 1497 | ret=1; | ||
| 1498 | goto end; | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | if (peer == NULL) | ||
| 1502 | { | ||
| 1503 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); | ||
| 1504 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
| 1505 | goto f_err; | ||
| 1506 | } | ||
| 1507 | |||
| 1508 | if (!(type & EVP_PKT_SIGN)) | ||
| 1509 | { | ||
| 1510 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); | ||
| 1511 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
| 1512 | goto f_err; | ||
| 1513 | } | ||
| 1514 | |||
| 1515 | if (s->s3->change_cipher_spec) | ||
| 1516 | { | ||
| 1517 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); | ||
| 1518 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
| 1519 | goto f_err; | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | /* we now have a signature that we need to verify */ | ||
| 1523 | p=(unsigned char *)s->init_buf->data; | ||
| 1524 | n2s(p,i); | ||
| 1525 | n-=2; | ||
| 1526 | if (i > n) | ||
| 1527 | { | ||
| 1528 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); | ||
| 1529 | al=SSL_AD_DECODE_ERROR; | ||
| 1530 | goto f_err; | ||
| 1531 | } | ||
| 1532 | |||
| 1533 | j=EVP_PKEY_size(pkey); | ||
| 1534 | if ((i > j) || (n > j) || (n <= 0)) | ||
| 1535 | { | ||
| 1536 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); | ||
| 1537 | al=SSL_AD_DECODE_ERROR; | ||
| 1538 | goto f_err; | ||
| 1539 | } | ||
| 1540 | |||
| 1541 | #ifndef NO_RSA | ||
| 1542 | if (pkey->type == EVP_PKEY_RSA) | ||
| 1543 | { | ||
| 1544 | i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, | ||
| 1545 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, | ||
| 1546 | pkey->pkey.rsa); | ||
| 1547 | if (i < 0) | ||
| 1548 | { | ||
| 1549 | al=SSL_AD_DECRYPT_ERROR; | ||
| 1550 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); | ||
| 1551 | goto f_err; | ||
| 1552 | } | ||
| 1553 | if (i == 0) | ||
| 1554 | { | ||
| 1555 | al=SSL_AD_DECRYPT_ERROR; | ||
| 1556 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); | ||
| 1557 | goto f_err; | ||
| 1558 | } | ||
| 1559 | } | ||
| 1560 | else | ||
| 1561 | #endif | ||
| 1562 | #ifndef NO_DSA | ||
| 1563 | if (pkey->type == EVP_PKEY_DSA) | ||
| 1564 | { | ||
| 1565 | j=DSA_verify(pkey->save_type, | ||
| 1566 | &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), | ||
| 1567 | SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); | ||
| 1568 | if (j <= 0) | ||
| 1569 | { | ||
| 1570 | /* bad signature */ | ||
| 1571 | al=SSL_AD_DECRYPT_ERROR; | ||
| 1572 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); | ||
| 1573 | goto f_err; | ||
| 1574 | } | ||
| 1575 | } | ||
| 1576 | else | ||
| 1577 | #endif | ||
| 1578 | { | ||
| 1579 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR); | ||
| 1580 | al=SSL_AD_UNSUPPORTED_CERTIFICATE; | ||
| 1581 | goto f_err; | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | |||
| 1585 | ret=1; | ||
| 1586 | if (0) | ||
| 1587 | { | ||
| 1588 | f_err: | ||
| 1589 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
| 1590 | } | ||
| 1591 | end: | ||
| 1592 | EVP_PKEY_free(pkey); | ||
| 1593 | return(ret); | ||
| 1594 | } | ||
| 1595 | |||
| 1596 | static int ssl3_get_client_certificate(SSL *s) | ||
| 1597 | { | ||
| 1598 | int i,ok,al,ret= -1; | ||
| 1599 | X509 *x=NULL; | ||
| 1600 | unsigned long l,nc,llen,n; | ||
| 1601 | unsigned char *p,*d,*q; | ||
| 1602 | STACK_OF(X509) *sk=NULL; | ||
| 1603 | |||
| 1604 | n=ssl3_get_message(s, | ||
| 1605 | SSL3_ST_SR_CERT_A, | ||
| 1606 | SSL3_ST_SR_CERT_B, | ||
| 1607 | -1, | ||
| 1608 | #if defined(MSDOS) && !defined(WIN32) | ||
| 1609 | 1024*30, /* 30k max cert list :-) */ | ||
| 1610 | #else | ||
| 1611 | 1024*100, /* 100k max cert list :-) */ | ||
| 1612 | #endif | ||
| 1613 | &ok); | ||
| 1614 | |||
| 1615 | if (!ok) return((int)n); | ||
| 1616 | |||
| 1617 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) | ||
| 1618 | { | ||
| 1619 | if ( (s->verify_mode & SSL_VERIFY_PEER) && | ||
| 1620 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | ||
| 1621 | { | ||
| 1622 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | ||
| 1623 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1624 | goto f_err; | ||
| 1625 | } | ||
| 1626 | /* If tls asked for a client cert, the client must return a 0 list */ | ||
| 1627 | if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) | ||
| 1628 | { | ||
| 1629 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); | ||
| 1630 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
| 1631 | goto f_err; | ||
| 1632 | } | ||
| 1633 | s->s3->tmp.reuse_message=1; | ||
| 1634 | return(1); | ||
| 1635 | } | ||
| 1636 | |||
| 1637 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) | ||
| 1638 | { | ||
| 1639 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
| 1640 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); | ||
| 1641 | goto f_err; | ||
| 1642 | } | ||
| 1643 | d=p=(unsigned char *)s->init_buf->data; | ||
| 1644 | |||
| 1645 | if ((sk=sk_X509_new_null()) == NULL) | ||
| 1646 | { | ||
| 1647 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); | ||
| 1648 | goto err; | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | n2l3(p,llen); | ||
| 1652 | if (llen+3 != n) | ||
| 1653 | { | ||
| 1654 | al=SSL_AD_DECODE_ERROR; | ||
| 1655 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); | ||
| 1656 | goto f_err; | ||
| 1657 | } | ||
| 1658 | for (nc=0; nc<llen; ) | ||
| 1659 | { | ||
| 1660 | n2l3(p,l); | ||
| 1661 | if ((l+nc+3) > llen) | ||
| 1662 | { | ||
| 1663 | al=SSL_AD_DECODE_ERROR; | ||
| 1664 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); | ||
| 1665 | goto f_err; | ||
| 1666 | } | ||
| 1667 | |||
| 1668 | q=p; | ||
| 1669 | x=d2i_X509(NULL,&p,l); | ||
| 1670 | if (x == NULL) | ||
| 1671 | { | ||
| 1672 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); | ||
| 1673 | goto err; | ||
| 1674 | } | ||
| 1675 | if (p != (q+l)) | ||
| 1676 | { | ||
| 1677 | al=SSL_AD_DECODE_ERROR; | ||
| 1678 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); | ||
| 1679 | goto f_err; | ||
| 1680 | } | ||
| 1681 | if (!sk_X509_push(sk,x)) | ||
| 1682 | { | ||
| 1683 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); | ||
| 1684 | goto err; | ||
| 1685 | } | ||
| 1686 | x=NULL; | ||
| 1687 | nc+=l+3; | ||
| 1688 | } | ||
| 1689 | |||
| 1690 | if (sk_X509_num(sk) <= 0) | ||
| 1691 | { | ||
| 1692 | /* TLS does not mind 0 certs returned */ | ||
| 1693 | if (s->version == SSL3_VERSION) | ||
| 1694 | { | ||
| 1695 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1696 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED); | ||
| 1697 | goto f_err; | ||
| 1698 | } | ||
| 1699 | /* Fail for TLS only if we required a certificate */ | ||
| 1700 | else if ((s->verify_mode & SSL_VERIFY_PEER) && | ||
| 1701 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | ||
| 1702 | { | ||
| 1703 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | ||
| 1704 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
| 1705 | goto f_err; | ||
| 1706 | } | ||
| 1707 | } | ||
| 1708 | else | ||
| 1709 | { | ||
| 1710 | i=ssl_verify_cert_chain(s,sk); | ||
| 1711 | if (!i) | ||
| 1712 | { | ||
| 1713 | al=ssl_verify_alarm_type(s->verify_result); | ||
| 1714 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); | ||
| 1715 | goto f_err; | ||
| 1716 | } | ||
| 1717 | } | ||
| 1718 | |||
| 1719 | if (s->session->peer != NULL) /* This should not be needed */ | ||
| 1720 | X509_free(s->session->peer); | ||
| 1721 | s->session->peer=sk_X509_shift(sk); | ||
| 1722 | s->session->verify_result = s->verify_result; | ||
| 1723 | |||
| 1724 | /* With the current implementation, sess_cert will always be NULL | ||
| 1725 | * when we arrive here. */ | ||
| 1726 | if (s->session->sess_cert == NULL) | ||
| 1727 | { | ||
| 1728 | s->session->sess_cert = ssl_sess_cert_new(); | ||
| 1729 | if (s->session->sess_cert == NULL) | ||
| 1730 | { | ||
| 1731 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); | ||
| 1732 | goto err; | ||
| 1733 | } | ||
| 1734 | } | ||
| 1735 | if (s->session->sess_cert->cert_chain != NULL) | ||
| 1736 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); | ||
| 1737 | s->session->sess_cert->cert_chain=sk; | ||
| 1738 | /* Inconsistency alert: cert_chain does *not* include the | ||
| 1739 | * peer's own certificate, while we do include it in s3_clnt.c */ | ||
| 1740 | |||
| 1741 | sk=NULL; | ||
| 1742 | |||
| 1743 | ret=1; | ||
| 1744 | if (0) | ||
| 1745 | { | ||
| 1746 | f_err: | ||
| 1747 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
| 1748 | } | ||
| 1749 | err: | ||
| 1750 | if (x != NULL) X509_free(x); | ||
| 1751 | if (sk != NULL) sk_X509_pop_free(sk,X509_free); | ||
| 1752 | return(ret); | ||
| 1753 | } | ||
| 1754 | |||
| 1755 | int ssl3_send_server_certificate(SSL *s) | ||
| 1756 | { | ||
| 1757 | unsigned long l; | ||
| 1758 | X509 *x; | ||
| 1759 | |||
| 1760 | if (s->state == SSL3_ST_SW_CERT_A) | ||
| 1761 | { | ||
| 1762 | x=ssl_get_server_send_cert(s); | ||
| 1763 | if (x == NULL) | ||
| 1764 | { | ||
| 1765 | SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR); | ||
| 1766 | return(0); | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | l=ssl3_output_cert_chain(s,x); | ||
| 1770 | s->state=SSL3_ST_SW_CERT_B; | ||
| 1771 | s->init_num=(int)l; | ||
| 1772 | s->init_off=0; | ||
| 1773 | } | ||
| 1774 | |||
| 1775 | /* SSL3_ST_SW_CERT_B */ | ||
| 1776 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
| 1777 | } | ||
