155714Skris/* ssl/s2_clnt.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296465Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296465Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296465Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296465Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296465Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5889840Skris/* ==================================================================== 5989840Skris * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 6089840Skris * 6189840Skris * Redistribution and use in source and binary forms, with or without 6289840Skris * modification, are permitted provided that the following conditions 6389840Skris * are met: 6489840Skris * 6589840Skris * 1. Redistributions of source code must retain the above copyright 66296465Sdelphij * notice, this list of conditions and the following disclaimer. 6789840Skris * 6889840Skris * 2. Redistributions in binary form must reproduce the above copyright 6989840Skris * notice, this list of conditions and the following disclaimer in 7089840Skris * the documentation and/or other materials provided with the 7189840Skris * distribution. 7289840Skris * 7389840Skris * 3. All advertising materials mentioning features or use of this 7489840Skris * software must display the following acknowledgment: 7589840Skris * "This product includes software developed by the OpenSSL Project 7689840Skris * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 7789840Skris * 7889840Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 7989840Skris * endorse or promote products derived from this software without 8089840Skris * prior written permission. For written permission, please contact 8189840Skris * openssl-core@openssl.org. 8289840Skris * 8389840Skris * 5. Products derived from this software may not be called "OpenSSL" 8489840Skris * nor may "OpenSSL" appear in their names without prior written 8589840Skris * permission of the OpenSSL Project. 8689840Skris * 8789840Skris * 6. Redistributions of any form whatsoever must retain the following 8889840Skris * acknowledgment: 8989840Skris * "This product includes software developed by the OpenSSL Project 9089840Skris * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 9189840Skris * 9289840Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 9389840Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9489840Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 9589840Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 9689840Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 9789840Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 9889840Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 9989840Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 10089840Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 10189840Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10289840Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 10389840Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 10489840Skris * ==================================================================== 10589840Skris * 10689840Skris * This product includes cryptographic software written by Eric Young 10789840Skris * (eay@cryptsoft.com). This product includes software written by Tim 10889840Skris * Hudson (tjh@cryptsoft.com). 10989840Skris * 11089840Skris */ 11155714Skris 11259194Skris#include "ssl_locl.h" 113110007Smarkm#ifndef OPENSSL_NO_SSL2 114296465Sdelphij# include <stdio.h> 115296465Sdelphij# include <openssl/rand.h> 116296465Sdelphij# include <openssl/buffer.h> 117296465Sdelphij# include <openssl/objects.h> 118296465Sdelphij# include <openssl/evp.h> 11955714Skris 12055714Skrisstatic SSL_METHOD *ssl2_get_client_method(int ver); 12155714Skrisstatic int get_server_finished(SSL *s); 12255714Skrisstatic int get_server_verify(SSL *s); 12355714Skrisstatic int get_server_hello(SSL *s); 124296465Sdelphijstatic int client_hello(SSL *s); 12555714Skrisstatic int client_master_key(SSL *s); 12655714Skrisstatic int client_finished(SSL *s); 12755714Skrisstatic int client_certificate(SSL *s); 12855714Skrisstatic int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from, 129296465Sdelphij unsigned char *to, int padding); 130296465Sdelphij# define BREAK break 13155714Skris 13255714Skrisstatic SSL_METHOD *ssl2_get_client_method(int ver) 133296465Sdelphij{ 134296465Sdelphij if (ver == SSL2_VERSION) 135296465Sdelphij return (SSLv2_client_method()); 136296465Sdelphij else 137296465Sdelphij return (NULL); 138296465Sdelphij} 13955714Skris 140160817SsimonIMPLEMENT_ssl2_meth_func(SSLv2_client_method, 141296465Sdelphij ssl_undefined_function, 142296465Sdelphij ssl2_connect, ssl2_get_client_method) 14355714Skris 14455714Skrisint ssl2_connect(SSL *s) 145296465Sdelphij{ 146296465Sdelphij unsigned long l = (unsigned long)time(NULL); 147296465Sdelphij BUF_MEM *buf = NULL; 148296465Sdelphij int ret = -1; 149296465Sdelphij void (*cb) (const SSL *ssl, int type, int val) = NULL; 150296465Sdelphij int new_state, state; 15155714Skris 152296465Sdelphij RAND_add(&l, sizeof(l), 0); 153296465Sdelphij ERR_clear_error(); 154296465Sdelphij clear_sys_error(); 15555714Skris 156296465Sdelphij if (s->info_callback != NULL) 157296465Sdelphij cb = s->info_callback; 158296465Sdelphij else if (s->ctx->info_callback != NULL) 159296465Sdelphij cb = s->ctx->info_callback; 16055714Skris 161296465Sdelphij /* init things to blank */ 162296465Sdelphij s->in_handshake++; 163296465Sdelphij if (!SSL_in_init(s) || SSL_in_before(s)) 164296465Sdelphij SSL_clear(s); 16555714Skris 166296465Sdelphij for (;;) { 167296465Sdelphij state = s->state; 16855714Skris 169296465Sdelphij switch (s->state) { 170296465Sdelphij case SSL_ST_BEFORE: 171296465Sdelphij case SSL_ST_CONNECT: 172296465Sdelphij case SSL_ST_BEFORE | SSL_ST_CONNECT: 173296465Sdelphij case SSL_ST_OK | SSL_ST_CONNECT: 17455714Skris 175296465Sdelphij s->server = 0; 176296465Sdelphij if (cb != NULL) 177296465Sdelphij cb(s, SSL_CB_HANDSHAKE_START, 1); 17855714Skris 179296465Sdelphij s->version = SSL2_VERSION; 180296465Sdelphij s->type = SSL_ST_CONNECT; 18155714Skris 182296465Sdelphij buf = s->init_buf; 183296465Sdelphij if ((buf == NULL) && ((buf = BUF_MEM_new()) == NULL)) { 184296465Sdelphij ret = -1; 185296465Sdelphij goto end; 186296465Sdelphij } 187296465Sdelphij if (!BUF_MEM_grow(buf, SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) { 188296465Sdelphij if (buf == s->init_buf) 189296465Sdelphij buf = NULL; 190296465Sdelphij ret = -1; 191296465Sdelphij goto end; 192296465Sdelphij } 193296465Sdelphij s->init_buf = buf; 194296465Sdelphij buf = NULL; 195296465Sdelphij s->init_num = 0; 196296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_HELLO_A; 197296465Sdelphij s->ctx->stats.sess_connect++; 198296465Sdelphij s->handshake_func = ssl2_connect; 199296465Sdelphij BREAK; 20055714Skris 201296465Sdelphij case SSL2_ST_SEND_CLIENT_HELLO_A: 202296465Sdelphij case SSL2_ST_SEND_CLIENT_HELLO_B: 203296465Sdelphij s->shutdown = 0; 204296465Sdelphij ret = client_hello(s); 205296465Sdelphij if (ret <= 0) 206296465Sdelphij goto end; 207296465Sdelphij s->init_num = 0; 208296465Sdelphij s->state = SSL2_ST_GET_SERVER_HELLO_A; 209296465Sdelphij BREAK; 21055714Skris 211296465Sdelphij case SSL2_ST_GET_SERVER_HELLO_A: 212296465Sdelphij case SSL2_ST_GET_SERVER_HELLO_B: 213296465Sdelphij ret = get_server_hello(s); 214296465Sdelphij if (ret <= 0) 215296465Sdelphij goto end; 216296465Sdelphij s->init_num = 0; 217296465Sdelphij if (!s->hit) { /* new session */ 218296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_MASTER_KEY_A; 219296465Sdelphij BREAK; 220296465Sdelphij } else { 221296465Sdelphij s->state = SSL2_ST_CLIENT_START_ENCRYPTION; 222296465Sdelphij break; 223296465Sdelphij } 22455714Skris 225296465Sdelphij case SSL2_ST_SEND_CLIENT_MASTER_KEY_A: 226296465Sdelphij case SSL2_ST_SEND_CLIENT_MASTER_KEY_B: 227296465Sdelphij ret = client_master_key(s); 228296465Sdelphij if (ret <= 0) 229296465Sdelphij goto end; 230296465Sdelphij s->init_num = 0; 231296465Sdelphij s->state = SSL2_ST_CLIENT_START_ENCRYPTION; 232296465Sdelphij break; 23355714Skris 234296465Sdelphij case SSL2_ST_CLIENT_START_ENCRYPTION: 235296465Sdelphij /* 236296465Sdelphij * Ok, we now have all the stuff needed to start encrypting, so 237296465Sdelphij * lets fire it up :-) 238296465Sdelphij */ 239296465Sdelphij if (!ssl2_enc_init(s, 1)) { 240296465Sdelphij ret = -1; 241296465Sdelphij goto end; 242296465Sdelphij } 243296465Sdelphij s->s2->clear_text = 0; 244296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_FINISHED_A; 245296465Sdelphij break; 24655714Skris 247296465Sdelphij case SSL2_ST_SEND_CLIENT_FINISHED_A: 248296465Sdelphij case SSL2_ST_SEND_CLIENT_FINISHED_B: 249296465Sdelphij ret = client_finished(s); 250296465Sdelphij if (ret <= 0) 251296465Sdelphij goto end; 252296465Sdelphij s->init_num = 0; 253296465Sdelphij s->state = SSL2_ST_GET_SERVER_VERIFY_A; 254296465Sdelphij break; 25555714Skris 256296465Sdelphij case SSL2_ST_GET_SERVER_VERIFY_A: 257296465Sdelphij case SSL2_ST_GET_SERVER_VERIFY_B: 258296465Sdelphij ret = get_server_verify(s); 259296465Sdelphij if (ret <= 0) 260296465Sdelphij goto end; 261296465Sdelphij s->init_num = 0; 262296465Sdelphij s->state = SSL2_ST_GET_SERVER_FINISHED_A; 263296465Sdelphij break; 26455714Skris 265296465Sdelphij case SSL2_ST_GET_SERVER_FINISHED_A: 266296465Sdelphij case SSL2_ST_GET_SERVER_FINISHED_B: 267296465Sdelphij ret = get_server_finished(s); 268296465Sdelphij if (ret <= 0) 269296465Sdelphij goto end; 270296465Sdelphij break; 27155714Skris 272296465Sdelphij case SSL2_ST_SEND_CLIENT_CERTIFICATE_A: 273296465Sdelphij case SSL2_ST_SEND_CLIENT_CERTIFICATE_B: 274296465Sdelphij case SSL2_ST_SEND_CLIENT_CERTIFICATE_C: 275296465Sdelphij case SSL2_ST_SEND_CLIENT_CERTIFICATE_D: 276296465Sdelphij case SSL2_ST_X509_GET_CLIENT_CERTIFICATE: 277296465Sdelphij ret = client_certificate(s); 278296465Sdelphij if (ret <= 0) 279296465Sdelphij goto end; 280296465Sdelphij s->init_num = 0; 281296465Sdelphij s->state = SSL2_ST_GET_SERVER_FINISHED_A; 282296465Sdelphij break; 28355714Skris 284296465Sdelphij case SSL_ST_OK: 285296465Sdelphij if (s->init_buf != NULL) { 286296465Sdelphij BUF_MEM_free(s->init_buf); 287296465Sdelphij s->init_buf = NULL; 288296465Sdelphij } 289296465Sdelphij s->init_num = 0; 290296465Sdelphij /* ERR_clear_error(); */ 29155714Skris 292296465Sdelphij /* 293296465Sdelphij * If we want to cache session-ids in the client and we 294296465Sdelphij * successfully add the session-id to the cache, and there is a 295296465Sdelphij * callback, then pass it out. 26/11/96 - eay - only add if not a 296296465Sdelphij * re-used session. 297296465Sdelphij */ 29855714Skris 299296465Sdelphij ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); 300296465Sdelphij if (s->hit) 301296465Sdelphij s->ctx->stats.sess_hit++; 30255714Skris 303296465Sdelphij ret = 1; 304296465Sdelphij /* s->server=0; */ 305296465Sdelphij s->ctx->stats.sess_connect_good++; 30655714Skris 307296465Sdelphij if (cb != NULL) 308296465Sdelphij cb(s, SSL_CB_HANDSHAKE_DONE, 1); 30955714Skris 310296465Sdelphij goto end; 311296465Sdelphij /* break; */ 312296465Sdelphij default: 313296465Sdelphij SSLerr(SSL_F_SSL2_CONNECT, SSL_R_UNKNOWN_STATE); 314296465Sdelphij return (-1); 315296465Sdelphij /* break; */ 316296465Sdelphij } 317296465Sdelphij 318296465Sdelphij if ((cb != NULL) && (s->state != state)) { 319296465Sdelphij new_state = s->state; 320296465Sdelphij s->state = state; 321296465Sdelphij cb(s, SSL_CB_CONNECT_LOOP, 1); 322296465Sdelphij s->state = new_state; 323296465Sdelphij } 324296465Sdelphij } 325296465Sdelphij end: 326296465Sdelphij s->in_handshake--; 327296465Sdelphij if (buf != NULL) 328296465Sdelphij BUF_MEM_free(buf); 329296465Sdelphij if (cb != NULL) 330296465Sdelphij cb(s, SSL_CB_CONNECT_EXIT, ret); 331296465Sdelphij return (ret); 332296465Sdelphij} 333296465Sdelphij 33455714Skrisstatic int get_server_hello(SSL *s) 335296465Sdelphij{ 336296465Sdelphij unsigned char *buf; 337296465Sdelphij unsigned char *p; 338296465Sdelphij int i, j; 339296465Sdelphij unsigned long len; 340296465Sdelphij STACK_OF(SSL_CIPHER) *sk = NULL, *cl, *prio, *allow; 34155714Skris 342296465Sdelphij buf = (unsigned char *)s->init_buf->data; 343296465Sdelphij p = buf; 344296465Sdelphij if (s->state == SSL2_ST_GET_SERVER_HELLO_A) { 345296465Sdelphij i = ssl2_read(s, (char *)&(buf[s->init_num]), 11 - s->init_num); 346296465Sdelphij if (i < (11 - s->init_num)) 347296465Sdelphij return (ssl2_part_read(s, SSL_F_GET_SERVER_HELLO, i)); 348296465Sdelphij s->init_num = 11; 34955714Skris 350296465Sdelphij if (*(p++) != SSL2_MT_SERVER_HELLO) { 351296465Sdelphij if (p[-1] != SSL2_MT_ERROR) { 352296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 353296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_READ_WRONG_PACKET_TYPE); 354296465Sdelphij } else 355296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_PEER_ERROR); 356296465Sdelphij return (-1); 357296465Sdelphij } 358296465Sdelphij# ifdef __APPLE_CC__ 359296465Sdelphij /* 360296465Sdelphij * The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug workaround. 361296465Sdelphij * <appro@fy.chalmers.se> 362296465Sdelphij */ 363296465Sdelphij s->hit = (i = *(p++)) ? 1 : 0; 364296465Sdelphij# else 365296465Sdelphij s->hit = (*(p++)) ? 1 : 0; 366296465Sdelphij# endif 367296465Sdelphij s->s2->tmp.cert_type = *(p++); 368296465Sdelphij n2s(p, i); 369296465Sdelphij if (i < s->version) 370296465Sdelphij s->version = i; 371296465Sdelphij n2s(p, i); 372296465Sdelphij s->s2->tmp.cert_length = i; 373296465Sdelphij n2s(p, i); 374296465Sdelphij s->s2->tmp.csl = i; 375296465Sdelphij n2s(p, i); 376296465Sdelphij s->s2->tmp.conn_id_length = i; 377296465Sdelphij s->state = SSL2_ST_GET_SERVER_HELLO_B; 378296465Sdelphij } 37955714Skris 380296465Sdelphij /* SSL2_ST_GET_SERVER_HELLO_B */ 381296465Sdelphij len = 382296465Sdelphij 11 + (unsigned long)s->s2->tmp.cert_length + 383296465Sdelphij (unsigned long)s->s2->tmp.csl + 384296465Sdelphij (unsigned long)s->s2->tmp.conn_id_length; 385296465Sdelphij if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) { 386296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_MESSAGE_TOO_LONG); 387296465Sdelphij return -1; 388296465Sdelphij } 389296465Sdelphij j = (int)len - s->init_num; 390296465Sdelphij i = ssl2_read(s, (char *)&(buf[s->init_num]), j); 391296465Sdelphij if (i != j) 392296465Sdelphij return (ssl2_part_read(s, SSL_F_GET_SERVER_HELLO, i)); 393296465Sdelphij if (s->msg_callback) { 394296465Sdelphij /* SERVER-HELLO */ 395296465Sdelphij s->msg_callback(0, s->version, 0, buf, (size_t)len, s, 396296465Sdelphij s->msg_callback_arg); 397296465Sdelphij } 39855714Skris 399296465Sdelphij /* things are looking good */ 40055714Skris 401296465Sdelphij p = buf + 11; 402296465Sdelphij if (s->hit) { 403296465Sdelphij if (s->s2->tmp.cert_length != 0) { 404296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_REUSE_CERT_LENGTH_NOT_ZERO); 405296465Sdelphij return (-1); 406296465Sdelphij } 407296465Sdelphij if (s->s2->tmp.cert_type != 0) { 408296465Sdelphij if (!(s->options & SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG)) { 409296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, 410296465Sdelphij SSL_R_REUSE_CERT_TYPE_NOT_ZERO); 411296465Sdelphij return (-1); 412296465Sdelphij } 413296465Sdelphij } 414296465Sdelphij if (s->s2->tmp.csl != 0) { 415296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_REUSE_CIPHER_LIST_NOT_ZERO); 416296465Sdelphij return (-1); 417296465Sdelphij } 418296465Sdelphij } else { 419296465Sdelphij# ifdef undef 420296465Sdelphij /* very bad */ 421296465Sdelphij memset(s->session->session_id, 0, 422296465Sdelphij SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES); 423296465Sdelphij s->session->session_id_length = 0; 424296465Sdelphij */ 425296465Sdelphij# endif 426296465Sdelphij /* 427296465Sdelphij * we need to do this in case we were trying to reuse a client 428296465Sdelphij * session but others are already reusing it. If this was a new 429296465Sdelphij * 'blank' session ID, the session-id length will still be 0 430296465Sdelphij */ 431296465Sdelphij if (s->session->session_id_length > 0) { 432296465Sdelphij if (!ssl_get_new_session(s, 0)) { 433296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 434296465Sdelphij return (-1); 435296465Sdelphij } 436296465Sdelphij } 43755714Skris 438296465Sdelphij if (ssl2_set_certificate(s, s->s2->tmp.cert_type, 439296465Sdelphij s->s2->tmp.cert_length, p) <= 0) { 440296465Sdelphij ssl2_return_error(s, SSL2_PE_BAD_CERTIFICATE); 441296465Sdelphij return (-1); 442296465Sdelphij } 443296465Sdelphij p += s->s2->tmp.cert_length; 44455714Skris 445296465Sdelphij if (s->s2->tmp.csl == 0) { 446296465Sdelphij ssl2_return_error(s, SSL2_PE_NO_CIPHER); 447296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_NO_CIPHER_LIST); 448296465Sdelphij return (-1); 449296465Sdelphij } 45055714Skris 451296465Sdelphij /* 452296465Sdelphij * We have just received a list of ciphers back from the server. We 453296465Sdelphij * need to get the ones that match, then select the one we want the 454296465Sdelphij * most :-). 455296465Sdelphij */ 45655714Skris 457296465Sdelphij /* load the ciphers */ 458296465Sdelphij sk = ssl_bytes_to_cipher_list(s, p, s->s2->tmp.csl, 459296465Sdelphij &s->session->ciphers); 460296465Sdelphij p += s->s2->tmp.csl; 461296465Sdelphij if (sk == NULL) { 462296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 463296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_MALLOC_FAILURE); 464296465Sdelphij return (-1); 465296465Sdelphij } 46655714Skris 467296465Sdelphij (void)sk_SSL_CIPHER_set_cmp_func(sk, ssl_cipher_ptr_id_cmp); 46855714Skris 469296465Sdelphij /* get the array of ciphers we will accept */ 470296465Sdelphij cl = SSL_get_ciphers(s); 471296465Sdelphij (void)sk_SSL_CIPHER_set_cmp_func(cl, ssl_cipher_ptr_id_cmp); 47255714Skris 473296465Sdelphij /* 474296465Sdelphij * If server preference flag set, choose the first 475296465Sdelphij * (highest priority) cipher the server sends, otherwise 476296465Sdelphij * client preference has priority. 477296465Sdelphij */ 478296465Sdelphij if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { 479296465Sdelphij prio = sk; 480296465Sdelphij allow = cl; 481296465Sdelphij } else { 482296465Sdelphij prio = cl; 483296465Sdelphij allow = sk; 484296465Sdelphij } 485296465Sdelphij /* 486296465Sdelphij * In theory we could have ciphers sent back that we don't want to 487296465Sdelphij * use but that does not matter since we will check against the list 488296465Sdelphij * we originally sent and for performance reasons we should not 489296465Sdelphij * bother to match the two lists up just to check. 490296465Sdelphij */ 491296465Sdelphij for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) { 492296465Sdelphij if (sk_SSL_CIPHER_find(allow, sk_SSL_CIPHER_value(prio, i)) >= 0) 493296465Sdelphij break; 494296465Sdelphij } 49555714Skris 496296465Sdelphij if (i >= sk_SSL_CIPHER_num(prio)) { 497296465Sdelphij ssl2_return_error(s, SSL2_PE_NO_CIPHER); 498296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_NO_CIPHER_MATCH); 499296465Sdelphij return (-1); 500296465Sdelphij } 501296465Sdelphij s->session->cipher = sk_SSL_CIPHER_value(prio, i); 50255714Skris 503296465Sdelphij if (s->session->peer != NULL) { /* can't happen */ 504296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 505296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 506296465Sdelphij return (-1); 507296465Sdelphij } 50855714Skris 509296465Sdelphij s->session->peer = s->session->sess_cert->peer_key->x509; 510296465Sdelphij /* peer_key->x509 has been set by ssl2_set_certificate. */ 511296465Sdelphij CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509); 512296465Sdelphij } 51355714Skris 514296465Sdelphij if (s->session->sess_cert == NULL 515296465Sdelphij || s->session->peer != s->session->sess_cert->peer_key->x509) 516296465Sdelphij /* can't happen */ 517296465Sdelphij { 518296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 519296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 520296465Sdelphij return (-1); 521296465Sdelphij } 52255714Skris 523296465Sdelphij s->s2->conn_id_length = s->s2->tmp.conn_id_length; 524296465Sdelphij if (s->s2->conn_id_length > sizeof s->s2->conn_id) { 525296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 526296465Sdelphij SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG); 527296465Sdelphij return -1; 528296465Sdelphij } 529296465Sdelphij memcpy(s->s2->conn_id, p, s->s2->tmp.conn_id_length); 530296465Sdelphij return (1); 531296465Sdelphij} 53259194Skris 53355714Skrisstatic int client_hello(SSL *s) 534296465Sdelphij{ 535296465Sdelphij unsigned char *buf; 536296465Sdelphij unsigned char *p, *d; 537296465Sdelphij/* CIPHER **cipher;*/ 538296465Sdelphij int i, n, j; 53955714Skris 540296465Sdelphij buf = (unsigned char *)s->init_buf->data; 541296465Sdelphij if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A) { 542296465Sdelphij if ((s->session == NULL) || (s->session->ssl_version != s->version)) { 543296465Sdelphij if (!ssl_get_new_session(s, 0)) { 544296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 545296465Sdelphij return (-1); 546296465Sdelphij } 547296465Sdelphij } 548296465Sdelphij /* else use the pre-loaded session */ 54955714Skris 550296465Sdelphij p = buf; /* header */ 551296465Sdelphij d = p + 9; /* data section */ 552296465Sdelphij *(p++) = SSL2_MT_CLIENT_HELLO; /* type */ 553296465Sdelphij s2n(SSL2_VERSION, p); /* version */ 554296465Sdelphij n = j = 0; 55555714Skris 556296465Sdelphij n = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), d, 0); 557296465Sdelphij d += n; 55855714Skris 559296465Sdelphij if (n == 0) { 560296465Sdelphij SSLerr(SSL_F_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); 561296465Sdelphij return (-1); 562296465Sdelphij } 56355714Skris 564296465Sdelphij s2n(n, p); /* cipher spec num bytes */ 56555714Skris 566296465Sdelphij if ((s->session->session_id_length > 0) && 567296465Sdelphij (s->session->session_id_length <= 568296465Sdelphij SSL2_MAX_SSL_SESSION_ID_LENGTH)) { 569296465Sdelphij i = s->session->session_id_length; 570296465Sdelphij s2n(i, p); /* session id length */ 571296465Sdelphij memcpy(d, s->session->session_id, (unsigned int)i); 572296465Sdelphij d += i; 573296465Sdelphij } else { 574296465Sdelphij s2n(0, p); 575296465Sdelphij } 57655714Skris 577296465Sdelphij s->s2->challenge_length = SSL2_CHALLENGE_LENGTH; 578296465Sdelphij s2n(SSL2_CHALLENGE_LENGTH, p); /* challenge length */ 579296465Sdelphij /* 580296465Sdelphij * challenge id data 581296465Sdelphij */ 582296465Sdelphij if (RAND_pseudo_bytes(s->s2->challenge, SSL2_CHALLENGE_LENGTH) <= 0) 583296465Sdelphij return -1; 584296465Sdelphij memcpy(d, s->s2->challenge, SSL2_CHALLENGE_LENGTH); 585296465Sdelphij d += SSL2_CHALLENGE_LENGTH; 58655714Skris 587296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_HELLO_B; 588296465Sdelphij s->init_num = d - buf; 589296465Sdelphij s->init_off = 0; 590296465Sdelphij } 591296465Sdelphij /* SSL2_ST_SEND_CLIENT_HELLO_B */ 592296465Sdelphij return (ssl2_do_write(s)); 593296465Sdelphij} 59455714Skris 59555714Skrisstatic int client_master_key(SSL *s) 596296465Sdelphij{ 597296465Sdelphij unsigned char *buf; 598296465Sdelphij unsigned char *p, *d; 599296465Sdelphij int clear, enc, karg, i; 600296465Sdelphij SSL_SESSION *sess; 601296465Sdelphij const EVP_CIPHER *c; 602296465Sdelphij const EVP_MD *md; 60355714Skris 604296465Sdelphij buf = (unsigned char *)s->init_buf->data; 605296465Sdelphij if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A) { 60655714Skris 607296465Sdelphij if (!ssl_cipher_get_evp(s->session, &c, &md, NULL)) { 608296465Sdelphij ssl2_return_error(s, SSL2_PE_NO_CIPHER); 609296465Sdelphij SSLerr(SSL_F_CLIENT_MASTER_KEY, 610296465Sdelphij SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS); 611296465Sdelphij return (-1); 612296465Sdelphij } 613296465Sdelphij sess = s->session; 614296465Sdelphij p = buf; 615296465Sdelphij d = p + 10; 616296465Sdelphij *(p++) = SSL2_MT_CLIENT_MASTER_KEY; /* type */ 61755714Skris 618296465Sdelphij i = ssl_put_cipher_by_char(s, sess->cipher, p); 619296465Sdelphij p += i; 62055714Skris 621296465Sdelphij /* make key_arg data */ 622296465Sdelphij i = EVP_CIPHER_iv_length(c); 623296465Sdelphij sess->key_arg_length = i; 624296465Sdelphij if (i > SSL_MAX_KEY_ARG_LENGTH) { 625296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 626296465Sdelphij SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); 627296465Sdelphij return -1; 628296465Sdelphij } 629296465Sdelphij if (i > 0) 630296465Sdelphij if (RAND_pseudo_bytes(sess->key_arg, i) <= 0) 631296465Sdelphij return -1; 63255714Skris 633296465Sdelphij /* make a master key */ 634296465Sdelphij i = EVP_CIPHER_key_length(c); 635296465Sdelphij sess->master_key_length = i; 636296465Sdelphij if (i > 0) { 637296465Sdelphij if (i > (int)sizeof(sess->master_key)) { 638296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 639296465Sdelphij SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); 640296465Sdelphij return -1; 641296465Sdelphij } 642296465Sdelphij if (RAND_bytes(sess->master_key, i) <= 0) { 643296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 644296465Sdelphij return (-1); 645296465Sdelphij } 646296465Sdelphij } 64755714Skris 648296465Sdelphij if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC) 649296465Sdelphij enc = 8; 650296465Sdelphij else if (SSL_C_IS_EXPORT(sess->cipher)) 651296465Sdelphij enc = 5; 652296465Sdelphij else 653296465Sdelphij enc = i; 65455714Skris 655296465Sdelphij if ((int)i < enc) { 656296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 657296465Sdelphij SSLerr(SSL_F_CLIENT_MASTER_KEY, SSL_R_CIPHER_TABLE_SRC_ERROR); 658296465Sdelphij return (-1); 659296465Sdelphij } 660296465Sdelphij clear = i - enc; 661296465Sdelphij s2n(clear, p); 662296465Sdelphij memcpy(d, sess->master_key, (unsigned int)clear); 663296465Sdelphij d += clear; 66455714Skris 665296465Sdelphij enc = ssl_rsa_public_encrypt(sess->sess_cert, enc, 666296465Sdelphij &(sess->master_key[clear]), d, 667296465Sdelphij (s-> 668296465Sdelphij s2->ssl2_rollback) ? RSA_SSLV23_PADDING 669296465Sdelphij : RSA_PKCS1_PADDING); 670296465Sdelphij if (enc <= 0) { 671296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 672296465Sdelphij SSLerr(SSL_F_CLIENT_MASTER_KEY, SSL_R_PUBLIC_KEY_ENCRYPT_ERROR); 673296465Sdelphij return (-1); 674296465Sdelphij } 675296465Sdelphij# ifdef PKCS1_CHECK 676296465Sdelphij if (s->options & SSL_OP_PKCS1_CHECK_1) 677296465Sdelphij d[1]++; 678296465Sdelphij if (s->options & SSL_OP_PKCS1_CHECK_2) 679296465Sdelphij sess->master_key[clear]++; 680296465Sdelphij# endif 681296465Sdelphij s2n(enc, p); 682296465Sdelphij d += enc; 683296465Sdelphij karg = sess->key_arg_length; 684296465Sdelphij s2n(karg, p); /* key arg size */ 685296465Sdelphij if (karg > (int)sizeof(sess->key_arg)) { 686296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 687296465Sdelphij SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); 688296465Sdelphij return -1; 689296465Sdelphij } 690296465Sdelphij memcpy(d, sess->key_arg, (unsigned int)karg); 691296465Sdelphij d += karg; 69255714Skris 693296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_MASTER_KEY_B; 694296465Sdelphij s->init_num = d - buf; 695296465Sdelphij s->init_off = 0; 696296465Sdelphij } 69755714Skris 698296465Sdelphij /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */ 699296465Sdelphij return (ssl2_do_write(s)); 700296465Sdelphij} 70155714Skris 70255714Skrisstatic int client_finished(SSL *s) 703296465Sdelphij{ 704296465Sdelphij unsigned char *p; 70555714Skris 706296465Sdelphij if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A) { 707296465Sdelphij p = (unsigned char *)s->init_buf->data; 708296465Sdelphij *(p++) = SSL2_MT_CLIENT_FINISHED; 709296465Sdelphij if (s->s2->conn_id_length > sizeof s->s2->conn_id) { 710296465Sdelphij SSLerr(SSL_F_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR); 711296465Sdelphij return -1; 712296465Sdelphij } 713296465Sdelphij memcpy(p, s->s2->conn_id, (unsigned int)s->s2->conn_id_length); 71455714Skris 715296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_FINISHED_B; 716296465Sdelphij s->init_num = s->s2->conn_id_length + 1; 717296465Sdelphij s->init_off = 0; 718296465Sdelphij } 719296465Sdelphij return (ssl2_do_write(s)); 720296465Sdelphij} 72155714Skris 72255714Skris/* read the data and then respond */ 72355714Skrisstatic int client_certificate(SSL *s) 724296465Sdelphij{ 725296465Sdelphij unsigned char *buf; 726296465Sdelphij unsigned char *p, *d; 727296465Sdelphij int i; 728296465Sdelphij unsigned int n; 729296465Sdelphij int cert_ch_len; 730296465Sdelphij unsigned char *cert_ch; 73155714Skris 732296465Sdelphij buf = (unsigned char *)s->init_buf->data; 73355714Skris 734296465Sdelphij /* 735296465Sdelphij * We have a cert associated with the SSL, so attach it to the session if 736296465Sdelphij * it does not have one 737296465Sdelphij */ 73855714Skris 739296465Sdelphij if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A) { 740296465Sdelphij i = ssl2_read(s, (char *)&(buf[s->init_num]), 741296465Sdelphij SSL2_MAX_CERT_CHALLENGE_LENGTH + 2 - s->init_num); 742296465Sdelphij if (i < (SSL2_MIN_CERT_CHALLENGE_LENGTH + 2 - s->init_num)) 743296465Sdelphij return (ssl2_part_read(s, SSL_F_CLIENT_CERTIFICATE, i)); 744296465Sdelphij s->init_num += i; 745296465Sdelphij if (s->msg_callback) { 746296465Sdelphij /* REQUEST-CERTIFICATE */ 747296465Sdelphij s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, 748296465Sdelphij s->msg_callback_arg); 749296465Sdelphij } 75055714Skris 751296465Sdelphij /* type=buf[0]; */ 752296465Sdelphij /* type eq x509 */ 753296465Sdelphij if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) { 754296465Sdelphij ssl2_return_error(s, SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE); 755296465Sdelphij SSLerr(SSL_F_CLIENT_CERTIFICATE, SSL_R_BAD_AUTHENTICATION_TYPE); 756296465Sdelphij return (-1); 757296465Sdelphij } 75855714Skris 759296465Sdelphij if ((s->cert == NULL) || 760296465Sdelphij (s->cert->key->x509 == NULL) || 761296465Sdelphij (s->cert->key->privatekey == NULL)) { 762296465Sdelphij s->state = SSL2_ST_X509_GET_CLIENT_CERTIFICATE; 763296465Sdelphij } else 764296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_C; 765296465Sdelphij } 76655714Skris 767296465Sdelphij cert_ch = buf + 2; 768296465Sdelphij cert_ch_len = s->init_num - 2; 76989840Skris 770296465Sdelphij if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE) { 771296465Sdelphij X509 *x509 = NULL; 772296465Sdelphij EVP_PKEY *pkey = NULL; 77355714Skris 774296465Sdelphij /* 775296465Sdelphij * If we get an error we need to ssl->rwstate=SSL_X509_LOOKUP; 776296465Sdelphij * return(error); We should then be retried when things are ok and we 777296465Sdelphij * can get a cert or not 778296465Sdelphij */ 77955714Skris 780296465Sdelphij i = 0; 781296465Sdelphij if (s->ctx->client_cert_cb != NULL) { 782296465Sdelphij i = s->ctx->client_cert_cb(s, &(x509), &(pkey)); 783296465Sdelphij } 78455714Skris 785296465Sdelphij if (i < 0) { 786296465Sdelphij s->rwstate = SSL_X509_LOOKUP; 787296465Sdelphij return (-1); 788296465Sdelphij } 789296465Sdelphij s->rwstate = SSL_NOTHING; 79055714Skris 791296465Sdelphij if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { 792296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_C; 793296465Sdelphij if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) { 794296465Sdelphij i = 0; 795296465Sdelphij } 796296465Sdelphij X509_free(x509); 797296465Sdelphij EVP_PKEY_free(pkey); 798296465Sdelphij } else if (i == 1) { 799296465Sdelphij if (x509 != NULL) 800296465Sdelphij X509_free(x509); 801296465Sdelphij if (pkey != NULL) 802296465Sdelphij EVP_PKEY_free(pkey); 803296465Sdelphij SSLerr(SSL_F_CLIENT_CERTIFICATE, 804296465Sdelphij SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 805296465Sdelphij i = 0; 806296465Sdelphij } 80755714Skris 808296465Sdelphij if (i == 0) { 809296465Sdelphij /* 810296465Sdelphij * We have no client certificate to respond with so send the 811296465Sdelphij * correct error message back 812296465Sdelphij */ 813296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_B; 814296465Sdelphij p = buf; 815296465Sdelphij *(p++) = SSL2_MT_ERROR; 816296465Sdelphij s2n(SSL2_PE_NO_CERTIFICATE, p); 817296465Sdelphij s->init_off = 0; 818296465Sdelphij s->init_num = 3; 819296465Sdelphij /* Write is done at the end */ 820296465Sdelphij } 821296465Sdelphij } 82255714Skris 823296465Sdelphij if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B) { 824296465Sdelphij return (ssl2_do_write(s)); 825296465Sdelphij } 82655714Skris 827296465Sdelphij if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C) { 828296465Sdelphij EVP_MD_CTX ctx; 82955714Skris 830296465Sdelphij /* 831296465Sdelphij * ok, now we calculate the checksum do it first so we can reuse buf 832296465Sdelphij * :-) 833296465Sdelphij */ 834296465Sdelphij p = buf; 835296465Sdelphij EVP_MD_CTX_init(&ctx); 836296465Sdelphij EVP_SignInit_ex(&ctx, s->ctx->rsa_md5, NULL); 837296465Sdelphij EVP_SignUpdate(&ctx, s->s2->key_material, s->s2->key_material_length); 838296465Sdelphij EVP_SignUpdate(&ctx, cert_ch, (unsigned int)cert_ch_len); 839296465Sdelphij n = i2d_X509(s->session->sess_cert->peer_key->x509, &p); 840296465Sdelphij EVP_SignUpdate(&ctx, buf, (unsigned int)n); 84155714Skris 842296465Sdelphij p = buf; 843296465Sdelphij d = p + 6; 844296465Sdelphij *(p++) = SSL2_MT_CLIENT_CERTIFICATE; 845296465Sdelphij *(p++) = SSL2_CT_X509_CERTIFICATE; 846296465Sdelphij n = i2d_X509(s->cert->key->x509, &d); 847296465Sdelphij s2n(n, p); 84855714Skris 849296465Sdelphij if (!EVP_SignFinal(&ctx, d, &n, s->cert->key->privatekey)) { 850296465Sdelphij /* 851296465Sdelphij * this is not good. If things have failed it means there so 852296465Sdelphij * something wrong with the key. We will continue with a 0 length 853296465Sdelphij * signature 854296465Sdelphij */ 855296465Sdelphij } 856296465Sdelphij EVP_MD_CTX_cleanup(&ctx); 857296465Sdelphij s2n(n, p); 858296465Sdelphij d += n; 85955714Skris 860296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_D; 861296465Sdelphij s->init_num = d - buf; 862296465Sdelphij s->init_off = 0; 863296465Sdelphij } 864296465Sdelphij /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */ 865296465Sdelphij return (ssl2_do_write(s)); 866296465Sdelphij} 86755714Skris 86855714Skrisstatic int get_server_verify(SSL *s) 869296465Sdelphij{ 870296465Sdelphij unsigned char *p; 871296465Sdelphij int i, n, len; 87255714Skris 873296465Sdelphij p = (unsigned char *)s->init_buf->data; 874296465Sdelphij if (s->state == SSL2_ST_GET_SERVER_VERIFY_A) { 875296465Sdelphij i = ssl2_read(s, (char *)&(p[s->init_num]), 1 - s->init_num); 876296465Sdelphij if (i < (1 - s->init_num)) 877296465Sdelphij return (ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i)); 878296465Sdelphij s->init_num += i; 87955714Skris 880296465Sdelphij s->state = SSL2_ST_GET_SERVER_VERIFY_B; 881296465Sdelphij if (*p != SSL2_MT_SERVER_VERIFY) { 882296465Sdelphij if (p[0] != SSL2_MT_ERROR) { 883296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 884296465Sdelphij SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_READ_WRONG_PACKET_TYPE); 885296465Sdelphij } else { 886296465Sdelphij SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_PEER_ERROR); 887296465Sdelphij /* try to read the error message */ 888296465Sdelphij i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num); 889296465Sdelphij return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i); 890296465Sdelphij } 891296465Sdelphij return (-1); 892296465Sdelphij } 893296465Sdelphij } 89489840Skris 895296465Sdelphij p = (unsigned char *)s->init_buf->data; 896296465Sdelphij len = 1 + s->s2->challenge_length; 897296465Sdelphij n = len - s->init_num; 898296465Sdelphij i = ssl2_read(s, (char *)&(p[s->init_num]), n); 899296465Sdelphij if (i < n) 900296465Sdelphij return (ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i)); 901296465Sdelphij if (s->msg_callback) { 902296465Sdelphij /* SERVER-VERIFY */ 903296465Sdelphij s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); 904296465Sdelphij } 905296465Sdelphij p += 1; 90655714Skris 907296465Sdelphij if (CRYPTO_memcmp(p, s->s2->challenge, s->s2->challenge_length) != 0) { 908296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 909296465Sdelphij SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_CHALLENGE_IS_DIFFERENT); 910296465Sdelphij return (-1); 911296465Sdelphij } 912296465Sdelphij return (1); 913296465Sdelphij} 914296465Sdelphij 91555714Skrisstatic int get_server_finished(SSL *s) 916296465Sdelphij{ 917296465Sdelphij unsigned char *buf; 918296465Sdelphij unsigned char *p; 919296465Sdelphij int i, n, len; 92055714Skris 921296465Sdelphij buf = (unsigned char *)s->init_buf->data; 922296465Sdelphij p = buf; 923296465Sdelphij if (s->state == SSL2_ST_GET_SERVER_FINISHED_A) { 924296465Sdelphij i = ssl2_read(s, (char *)&(buf[s->init_num]), 1 - s->init_num); 925296465Sdelphij if (i < (1 - s->init_num)) 926296465Sdelphij return (ssl2_part_read(s, SSL_F_GET_SERVER_FINISHED, i)); 927296465Sdelphij s->init_num += i; 92889840Skris 929296465Sdelphij if (*p == SSL2_MT_REQUEST_CERTIFICATE) { 930296465Sdelphij s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_A; 931296465Sdelphij return (1); 932296465Sdelphij } else if (*p != SSL2_MT_SERVER_FINISHED) { 933296465Sdelphij if (p[0] != SSL2_MT_ERROR) { 934296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 935296465Sdelphij SSLerr(SSL_F_GET_SERVER_FINISHED, 936296465Sdelphij SSL_R_READ_WRONG_PACKET_TYPE); 937296465Sdelphij } else { 938296465Sdelphij SSLerr(SSL_F_GET_SERVER_FINISHED, SSL_R_PEER_ERROR); 939296465Sdelphij /* try to read the error message */ 940296465Sdelphij i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num); 941296465Sdelphij return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i); 942296465Sdelphij } 943296465Sdelphij return (-1); 944296465Sdelphij } 945296465Sdelphij s->state = SSL2_ST_GET_SERVER_FINISHED_B; 946296465Sdelphij } 94755714Skris 948296465Sdelphij len = 1 + SSL2_SSL_SESSION_ID_LENGTH; 949296465Sdelphij n = len - s->init_num; 950296465Sdelphij i = ssl2_read(s, (char *)&(buf[s->init_num]), n); 951296465Sdelphij if (i < n) { 952296465Sdelphij /* 953296465Sdelphij * XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH, 954296465Sdelphij * that's the maximum 955296465Sdelphij */ 956296465Sdelphij return (ssl2_part_read(s, SSL_F_GET_SERVER_FINISHED, i)); 957296465Sdelphij } 958296465Sdelphij s->init_num += i; 959296465Sdelphij if (s->msg_callback) { 960296465Sdelphij /* SERVER-FINISHED */ 961296465Sdelphij s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, 962296465Sdelphij s->msg_callback_arg); 963296465Sdelphij } 96455714Skris 965296465Sdelphij if (!s->hit) { /* new session */ 966296465Sdelphij /* new session-id */ 967296465Sdelphij /* 968296465Sdelphij * Make sure we were not trying to re-use an old SSL_SESSION or bad 969296465Sdelphij * things can happen 970296465Sdelphij */ 971296465Sdelphij /* ZZZZZZZZZZZZZ */ 972296465Sdelphij s->session->session_id_length = SSL2_SSL_SESSION_ID_LENGTH; 973296465Sdelphij memcpy(s->session->session_id, p + 1, SSL2_SSL_SESSION_ID_LENGTH); 974296465Sdelphij } else { 975296465Sdelphij if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG)) { 976296465Sdelphij if ((s->session->session_id_length > 977296465Sdelphij sizeof s->session->session_id) 978296465Sdelphij || (0 != 979296465Sdelphij memcmp(buf + 1, s->session->session_id, 980296465Sdelphij (unsigned int)s->session->session_id_length))) { 981296465Sdelphij ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); 982296465Sdelphij SSLerr(SSL_F_GET_SERVER_FINISHED, 983296465Sdelphij SSL_R_SSL_SESSION_ID_IS_DIFFERENT); 984296465Sdelphij return (-1); 985296465Sdelphij } 986296465Sdelphij } 987296465Sdelphij } 988296465Sdelphij s->state = SSL_ST_OK; 989296465Sdelphij return (1); 990296465Sdelphij} 99155714Skris 99255714Skris/* loads in the certificate from the server */ 993160817Ssimonint ssl2_set_certificate(SSL *s, int type, int len, const unsigned char *data) 994296465Sdelphij{ 995296465Sdelphij STACK_OF(X509) *sk = NULL; 996296465Sdelphij EVP_PKEY *pkey = NULL; 997296465Sdelphij SESS_CERT *sc = NULL; 998296465Sdelphij int i; 999296465Sdelphij X509 *x509 = NULL; 1000296465Sdelphij int ret = 0; 100155714Skris 1002296465Sdelphij x509 = d2i_X509(NULL, &data, (long)len); 1003296465Sdelphij if (x509 == NULL) { 1004296465Sdelphij SSLerr(SSL_F_SSL2_SET_CERTIFICATE, ERR_R_X509_LIB); 1005296465Sdelphij goto err; 1006296465Sdelphij } 100755714Skris 1008296465Sdelphij if ((sk = sk_X509_new_null()) == NULL || !sk_X509_push(sk, x509)) { 1009296465Sdelphij SSLerr(SSL_F_SSL2_SET_CERTIFICATE, ERR_R_MALLOC_FAILURE); 1010296465Sdelphij goto err; 1011296465Sdelphij } 101255714Skris 1013296465Sdelphij i = ssl_verify_cert_chain(s, sk); 101455714Skris 1015296465Sdelphij if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) { 1016296465Sdelphij SSLerr(SSL_F_SSL2_SET_CERTIFICATE, SSL_R_CERTIFICATE_VERIFY_FAILED); 1017296465Sdelphij goto err; 1018296465Sdelphij } 1019296465Sdelphij ERR_clear_error(); /* but we keep s->verify_result */ 1020296465Sdelphij s->session->verify_result = s->verify_result; 102155714Skris 1022296465Sdelphij /* server's cert for this session */ 1023296465Sdelphij sc = ssl_sess_cert_new(); 1024296465Sdelphij if (sc == NULL) { 1025296465Sdelphij ret = -1; 1026296465Sdelphij goto err; 1027296465Sdelphij } 1028296465Sdelphij if (s->session->sess_cert) 1029296465Sdelphij ssl_sess_cert_free(s->session->sess_cert); 1030296465Sdelphij s->session->sess_cert = sc; 103155714Skris 1032296465Sdelphij sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509 = x509; 1033296465Sdelphij sc->peer_key = &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]); 103455714Skris 1035296465Sdelphij pkey = X509_get_pubkey(x509); 1036296465Sdelphij x509 = NULL; 1037296465Sdelphij if (pkey == NULL) { 1038296465Sdelphij SSLerr(SSL_F_SSL2_SET_CERTIFICATE, 1039296465Sdelphij SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY); 1040296465Sdelphij goto err; 1041296465Sdelphij } 1042296465Sdelphij if (pkey->type != EVP_PKEY_RSA) { 1043296465Sdelphij SSLerr(SSL_F_SSL2_SET_CERTIFICATE, SSL_R_PUBLIC_KEY_NOT_RSA); 1044296465Sdelphij goto err; 1045296465Sdelphij } 1046296465Sdelphij 1047296465Sdelphij if (!ssl_set_peer_cert_type(sc, SSL2_CT_X509_CERTIFICATE)) 1048296465Sdelphij goto err; 1049296465Sdelphij ret = 1; 1050296465Sdelphij err: 1051296465Sdelphij sk_X509_free(sk); 1052296465Sdelphij X509_free(x509); 1053296465Sdelphij EVP_PKEY_free(pkey); 1054296465Sdelphij return (ret); 1055296465Sdelphij} 1056296465Sdelphij 105755714Skrisstatic int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from, 1058296465Sdelphij unsigned char *to, int padding) 1059296465Sdelphij{ 1060296465Sdelphij EVP_PKEY *pkey = NULL; 1061296465Sdelphij int i = -1; 106255714Skris 1063296465Sdelphij if ((sc == NULL) || (sc->peer_key->x509 == NULL) || 1064296465Sdelphij ((pkey = X509_get_pubkey(sc->peer_key->x509)) == NULL)) { 1065296465Sdelphij SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, SSL_R_NO_PUBLICKEY); 1066296465Sdelphij return (-1); 1067296465Sdelphij } 1068296465Sdelphij if (pkey->type != EVP_PKEY_RSA) { 1069296465Sdelphij SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, SSL_R_PUBLIC_KEY_IS_NOT_RSA); 1070296465Sdelphij goto end; 1071296465Sdelphij } 107255714Skris 1073296465Sdelphij /* we have the public key */ 1074296465Sdelphij i = RSA_public_encrypt(len, from, to, pkey->pkey.rsa, padding); 1075296465Sdelphij if (i < 0) 1076296465Sdelphij SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, ERR_R_RSA_LIB); 1077296465Sdelphij end: 1078296465Sdelphij EVP_PKEY_free(pkey); 1079296465Sdelphij return (i); 1080296465Sdelphij} 1081296465Sdelphij#else /* !OPENSSL_NO_SSL2 */ 108259194Skris 108359194Skris# if PEDANTIC 1084296465Sdelphijstatic void *dummy = &dummy; 108559194Skris# endif 108659194Skris 108755714Skris#endif 1088