10Sstevel@tonic-gate /* ssl/s2_srvr.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate * are met:
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate * the documentation and/or other materials provided with the
710Sstevel@tonic-gate * distribution.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate * software must display the following acknowledgment:
750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate * endorse or promote products derived from this software without
800Sstevel@tonic-gate * prior written permission. For written permission, please contact
810Sstevel@tonic-gate * openssl-core@openssl.org.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate * permission of the OpenSSL Project.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate * acknowledgment:
890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate * ====================================================================
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate #include "ssl_locl.h"
1130Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
1140Sstevel@tonic-gate #include <stdio.h>
1150Sstevel@tonic-gate #include <openssl/bio.h>
1160Sstevel@tonic-gate #include <openssl/rand.h>
1170Sstevel@tonic-gate #include <openssl/objects.h>
1180Sstevel@tonic-gate #include <openssl/evp.h>
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate static SSL_METHOD *ssl2_get_server_method(int ver);
1210Sstevel@tonic-gate static int get_client_master_key(SSL *s);
1220Sstevel@tonic-gate static int get_client_hello(SSL *s);
1230Sstevel@tonic-gate static int server_hello(SSL *s);
1240Sstevel@tonic-gate static int get_client_finished(SSL *s);
1250Sstevel@tonic-gate static int server_verify(SSL *s);
1260Sstevel@tonic-gate static int server_finish(SSL *s);
1270Sstevel@tonic-gate static int request_certificate(SSL *s);
1280Sstevel@tonic-gate static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
1290Sstevel@tonic-gate unsigned char *to,int padding);
1300Sstevel@tonic-gate #define BREAK break
1310Sstevel@tonic-gate
ssl2_get_server_method(int ver)1320Sstevel@tonic-gate static SSL_METHOD *ssl2_get_server_method(int ver)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate if (ver == SSL2_VERSION)
1350Sstevel@tonic-gate return(SSLv2_server_method());
1360Sstevel@tonic-gate else
1370Sstevel@tonic-gate return(NULL);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate
IMPLEMENT_ssl2_meth_func(SSLv2_server_method,ssl2_accept,ssl_undefined_function,ssl2_get_server_method)1402139Sjp161948 IMPLEMENT_ssl2_meth_func(SSLv2_server_method,
1412139Sjp161948 ssl2_accept,
1422139Sjp161948 ssl_undefined_function,
1432139Sjp161948 ssl2_get_server_method)
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate int ssl2_accept(SSL *s)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate unsigned long l=time(NULL);
1480Sstevel@tonic-gate BUF_MEM *buf=NULL;
1490Sstevel@tonic-gate int ret= -1;
1500Sstevel@tonic-gate long num1;
1510Sstevel@tonic-gate void (*cb)(const SSL *ssl,int type,int val)=NULL;
1520Sstevel@tonic-gate int new_state,state;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate RAND_add(&l,sizeof(l),0);
1550Sstevel@tonic-gate ERR_clear_error();
1560Sstevel@tonic-gate clear_sys_error();
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if (s->info_callback != NULL)
1590Sstevel@tonic-gate cb=s->info_callback;
1600Sstevel@tonic-gate else if (s->ctx->info_callback != NULL)
1610Sstevel@tonic-gate cb=s->ctx->info_callback;
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /* init things to blank */
1640Sstevel@tonic-gate s->in_handshake++;
1650Sstevel@tonic-gate if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (s->cert == NULL)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
1700Sstevel@tonic-gate return(-1);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate clear_sys_error();
1740Sstevel@tonic-gate for (;;)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate state=s->state;
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate switch (s->state)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate case SSL_ST_BEFORE:
1810Sstevel@tonic-gate case SSL_ST_ACCEPT:
1820Sstevel@tonic-gate case SSL_ST_BEFORE|SSL_ST_ACCEPT:
1830Sstevel@tonic-gate case SSL_ST_OK|SSL_ST_ACCEPT:
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate s->server=1;
1860Sstevel@tonic-gate if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate s->version=SSL2_VERSION;
1890Sstevel@tonic-gate s->type=SSL_ST_ACCEPT;
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate buf=s->init_buf;
1920Sstevel@tonic-gate if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
1930Sstevel@tonic-gate { ret= -1; goto end; }
1940Sstevel@tonic-gate if (!BUF_MEM_grow(buf,(int)
1950Sstevel@tonic-gate SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
1960Sstevel@tonic-gate { ret= -1; goto end; }
1970Sstevel@tonic-gate s->init_buf=buf;
1980Sstevel@tonic-gate s->init_num=0;
1990Sstevel@tonic-gate s->ctx->stats.sess_accept++;
2000Sstevel@tonic-gate s->handshake_func=ssl2_accept;
2010Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_HELLO_A;
2020Sstevel@tonic-gate BREAK;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_HELLO_A:
2050Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_HELLO_B:
2060Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_HELLO_C:
2070Sstevel@tonic-gate s->shutdown=0;
2080Sstevel@tonic-gate ret=get_client_hello(s);
2090Sstevel@tonic-gate if (ret <= 0) goto end;
2100Sstevel@tonic-gate s->init_num=0;
2110Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_HELLO_A;
2120Sstevel@tonic-gate BREAK;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_HELLO_A:
2150Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_HELLO_B:
2160Sstevel@tonic-gate ret=server_hello(s);
2170Sstevel@tonic-gate if (ret <= 0) goto end;
2180Sstevel@tonic-gate s->init_num=0;
2190Sstevel@tonic-gate if (!s->hit)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
2220Sstevel@tonic-gate BREAK;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate else
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate s->state=SSL2_ST_SERVER_START_ENCRYPTION;
2270Sstevel@tonic-gate BREAK;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
2300Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
2310Sstevel@tonic-gate ret=get_client_master_key(s);
2320Sstevel@tonic-gate if (ret <= 0) goto end;
2330Sstevel@tonic-gate s->init_num=0;
2340Sstevel@tonic-gate s->state=SSL2_ST_SERVER_START_ENCRYPTION;
2350Sstevel@tonic-gate BREAK;
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate case SSL2_ST_SERVER_START_ENCRYPTION:
2380Sstevel@tonic-gate /* Ok we how have sent all the stuff needed to
2390Sstevel@tonic-gate * start encrypting, the next packet back will
2400Sstevel@tonic-gate * be encrypted. */
2410Sstevel@tonic-gate if (!ssl2_enc_init(s,0))
2420Sstevel@tonic-gate { ret= -1; goto end; }
2430Sstevel@tonic-gate s->s2->clear_text=0;
2440Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
2450Sstevel@tonic-gate BREAK;
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_VERIFY_A:
2480Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_VERIFY_B:
2490Sstevel@tonic-gate ret=server_verify(s);
2500Sstevel@tonic-gate if (ret <= 0) goto end;
2510Sstevel@tonic-gate s->init_num=0;
2520Sstevel@tonic-gate if (s->hit)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate /* If we are in here, we have been
2550Sstevel@tonic-gate * buffering the output, so we need to
2560Sstevel@tonic-gate * flush it and remove buffering from
2570Sstevel@tonic-gate * future traffic */
2580Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
2590Sstevel@tonic-gate BREAK;
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate else
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
2640Sstevel@tonic-gate break;
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_VERIFY_C:
2680Sstevel@tonic-gate /* get the number of bytes to write */
2690Sstevel@tonic-gate num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
2700Sstevel@tonic-gate if (num1 != 0)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate s->rwstate=SSL_WRITING;
2730Sstevel@tonic-gate num1=BIO_flush(s->wbio);
2740Sstevel@tonic-gate if (num1 <= 0) { ret= -1; goto end; }
2750Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate /* flushed and now remove buffering */
2790Sstevel@tonic-gate s->wbio=BIO_pop(s->wbio);
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
2820Sstevel@tonic-gate BREAK;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_FINISHED_A:
2850Sstevel@tonic-gate case SSL2_ST_GET_CLIENT_FINISHED_B:
2860Sstevel@tonic-gate ret=get_client_finished(s);
2870Sstevel@tonic-gate if (ret <= 0)
2880Sstevel@tonic-gate goto end;
2890Sstevel@tonic-gate s->init_num=0;
2900Sstevel@tonic-gate s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
2910Sstevel@tonic-gate BREAK;
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
2940Sstevel@tonic-gate case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
2950Sstevel@tonic-gate case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
2960Sstevel@tonic-gate case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
2970Sstevel@tonic-gate /* don't do a 'request certificate' if we
2980Sstevel@tonic-gate * don't want to, or we already have one, and
2990Sstevel@tonic-gate * we only want to do it once. */
3000Sstevel@tonic-gate if (!(s->verify_mode & SSL_VERIFY_PEER) ||
3010Sstevel@tonic-gate ((s->session->peer != NULL) &&
3020Sstevel@tonic-gate (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
3050Sstevel@tonic-gate break;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate else
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate ret=request_certificate(s);
3100Sstevel@tonic-gate if (ret <= 0) goto end;
3110Sstevel@tonic-gate s->init_num=0;
3120Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate BREAK;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_FINISHED_A:
3170Sstevel@tonic-gate case SSL2_ST_SEND_SERVER_FINISHED_B:
3180Sstevel@tonic-gate ret=server_finish(s);
3190Sstevel@tonic-gate if (ret <= 0) goto end;
3200Sstevel@tonic-gate s->init_num=0;
3210Sstevel@tonic-gate s->state=SSL_ST_OK;
3220Sstevel@tonic-gate break;
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate case SSL_ST_OK:
3250Sstevel@tonic-gate BUF_MEM_free(s->init_buf);
3260Sstevel@tonic-gate ssl_free_wbio_buffer(s);
3270Sstevel@tonic-gate s->init_buf=NULL;
3280Sstevel@tonic-gate s->init_num=0;
3290Sstevel@tonic-gate /* ERR_clear_error();*/
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate s->ctx->stats.sess_accept_good++;
3340Sstevel@tonic-gate /* s->server=1; */
3350Sstevel@tonic-gate ret=1;
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate goto end;
3400Sstevel@tonic-gate /* BREAK; */
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate default:
3430Sstevel@tonic-gate SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
3440Sstevel@tonic-gate ret= -1;
3450Sstevel@tonic-gate goto end;
3460Sstevel@tonic-gate /* BREAK; */
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate if ((cb != NULL) && (s->state != state))
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate new_state=s->state;
3520Sstevel@tonic-gate s->state=state;
3530Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_LOOP,1);
3540Sstevel@tonic-gate s->state=new_state;
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate end:
3580Sstevel@tonic-gate s->in_handshake--;
3590Sstevel@tonic-gate if (cb != NULL)
3600Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_EXIT,ret);
3610Sstevel@tonic-gate return(ret);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
get_client_master_key(SSL * s)3640Sstevel@tonic-gate static int get_client_master_key(SSL *s)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate int is_export,i,n,keya,ek;
3670Sstevel@tonic-gate unsigned long len;
3680Sstevel@tonic-gate unsigned char *p;
3690Sstevel@tonic-gate SSL_CIPHER *cp;
3700Sstevel@tonic-gate const EVP_CIPHER *c;
3710Sstevel@tonic-gate const EVP_MD *md;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
3740Sstevel@tonic-gate if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (i < (10-s->init_num))
3790Sstevel@tonic-gate return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
3800Sstevel@tonic-gate s->init_num = 10;
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate if (p[-1] != SSL2_MT_ERROR)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
3870Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate else
3900Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_PEER_ERROR);
3910Sstevel@tonic-gate return(-1);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate cp=ssl2_get_cipher_by_char(p);
3950Sstevel@tonic-gate if (cp == NULL)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_NO_CIPHER);
3980Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_NO_CIPHER_MATCH);
3990Sstevel@tonic-gate return(-1);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate s->session->cipher= cp;
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate p+=3;
4040Sstevel@tonic-gate n2s(p,i); s->s2->tmp.clear=i;
4050Sstevel@tonic-gate n2s(p,i); s->s2->tmp.enc=i;
4060Sstevel@tonic-gate n2s(p,i); s->session->key_arg_length=i;
4070Sstevel@tonic-gate if(s->session->key_arg_length > SSL_MAX_KEY_ARG_LENGTH)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
4100Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_KEY_ARG_TOO_LONG);
4110Sstevel@tonic-gate return -1;
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
4170Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
4180Sstevel@tonic-gate if (s->init_buf->length < SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
4210Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
4220Sstevel@tonic-gate return -1;
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate keya=s->session->key_arg_length;
4250Sstevel@tonic-gate len = 10 + (unsigned long)s->s2->tmp.clear + (unsigned long)s->s2->tmp.enc + (unsigned long)keya;
4260Sstevel@tonic-gate if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
4290Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_MESSAGE_TOO_LONG);
4300Sstevel@tonic-gate return -1;
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate n = (int)len - s->init_num;
4330Sstevel@tonic-gate i = ssl2_read(s,(char *)&(p[s->init_num]),n);
4340Sstevel@tonic-gate if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
4350Sstevel@tonic-gate if (s->msg_callback)
4360Sstevel@tonic-gate s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-MASTER-KEY */
4370Sstevel@tonic-gate p += 10;
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
4400Sstevel@tonic-gate (unsigned int)keya);
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
4450Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
4460Sstevel@tonic-gate return(-1);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
4490Sstevel@tonic-gate &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
4500Sstevel@tonic-gate (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
4510Sstevel@tonic-gate
4520Sstevel@tonic-gate is_export=SSL_C_IS_EXPORT(s->session->cipher);
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_NO_CIPHER);
4570Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
4580Sstevel@tonic-gate return(0);
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate is_export=1;
4640Sstevel@tonic-gate ek=8;
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate else
4670Sstevel@tonic-gate ek=5;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate /* bad decrypt */
4700Sstevel@tonic-gate #if 1
4710Sstevel@tonic-gate /* If a bad decrypt, continue with protocol but with a
4720Sstevel@tonic-gate * random master secret (Bleichenbacher attack) */
4730Sstevel@tonic-gate if ((i < 0) ||
4740Sstevel@tonic-gate ((!is_export && (i != EVP_CIPHER_key_length(c)))
4750Sstevel@tonic-gate || (is_export && ((i != ek) || (s->s2->tmp.clear+(unsigned int)i !=
4760Sstevel@tonic-gate (unsigned int)EVP_CIPHER_key_length(c))))))
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate ERR_clear_error();
4790Sstevel@tonic-gate if (is_export)
4800Sstevel@tonic-gate i=ek;
4810Sstevel@tonic-gate else
4820Sstevel@tonic-gate i=EVP_CIPHER_key_length(c);
4832139Sjp161948 if (RAND_pseudo_bytes(p,i) <= 0)
4842139Sjp161948 return 0;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate #else
4870Sstevel@tonic-gate if (i < 0)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate error=1;
4900Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate /* incorrect number of key bytes for non export cipher */
4930Sstevel@tonic-gate else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
4940Sstevel@tonic-gate || (is_export && ((i != ek) || (s->s2->tmp.clear+i !=
4950Sstevel@tonic-gate EVP_CIPHER_key_length(c)))))
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate error=1;
4980Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate if (error)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5030Sstevel@tonic-gate return(-1);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate #endif
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate if (is_export) i+=s->s2->tmp.clear;
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate if (i > SSL_MAX_MASTER_KEY_LENGTH)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5120Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
5130Sstevel@tonic-gate return -1;
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate s->session->master_key_length=i;
5160Sstevel@tonic-gate memcpy(s->session->master_key,p,(unsigned int)i);
5170Sstevel@tonic-gate return(1);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate
get_client_hello(SSL * s)5200Sstevel@tonic-gate static int get_client_hello(SSL *s)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate int i,n;
5230Sstevel@tonic-gate unsigned long len;
5240Sstevel@tonic-gate unsigned char *p;
5250Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */
5260Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */
5270Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *prio, *allow;
5280Sstevel@tonic-gate int z;
5290Sstevel@tonic-gate
5300Sstevel@tonic-gate /* This is a bit of a hack to check for the correct packet
5310Sstevel@tonic-gate * type the first time round. */
5320Sstevel@tonic-gate if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate s->first_packet=1;
5350Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_HELLO_B;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
5390Sstevel@tonic-gate if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
5420Sstevel@tonic-gate if (i < (9-s->init_num))
5430Sstevel@tonic-gate return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
5440Sstevel@tonic-gate s->init_num = 9;
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate if (*(p++) != SSL2_MT_CLIENT_HELLO)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate if (p[-1] != SSL2_MT_ERROR)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5510Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate else
5540Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
5550Sstevel@tonic-gate return(-1);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate n2s(p,i);
5580Sstevel@tonic-gate if (i < s->version) s->version=i;
5590Sstevel@tonic-gate n2s(p,i); s->s2->tmp.cipher_spec_length=i;
5600Sstevel@tonic-gate n2s(p,i); s->s2->tmp.session_id_length=i;
5610Sstevel@tonic-gate n2s(p,i); s->s2->challenge_length=i;
5620Sstevel@tonic-gate if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
5630Sstevel@tonic-gate (i > SSL2_MAX_CHALLENGE_LENGTH))
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5660Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
5670Sstevel@tonic-gate return(-1);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_HELLO_C;
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate /* SSL2_ST_GET_CLIENT_HELLO_C */
5730Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
5740Sstevel@tonic-gate len = 9 + (unsigned long)s->s2->tmp.cipher_spec_length + (unsigned long)s->s2->challenge_length + (unsigned long)s->s2->tmp.session_id_length;
5750Sstevel@tonic-gate if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5780Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_MESSAGE_TOO_LONG);
5790Sstevel@tonic-gate return -1;
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate n = (int)len - s->init_num;
5820Sstevel@tonic-gate i = ssl2_read(s,(char *)&(p[s->init_num]),n);
5830Sstevel@tonic-gate if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
5840Sstevel@tonic-gate if (s->msg_callback)
5850Sstevel@tonic-gate s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-HELLO */
5860Sstevel@tonic-gate p += 9;
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate /* get session-id before cipher stuff so we can get out session
5890Sstevel@tonic-gate * structure if it is cached */
5900Sstevel@tonic-gate /* session-id */
5910Sstevel@tonic-gate if ((s->s2->tmp.session_id_length != 0) &&
5920Sstevel@tonic-gate (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5950Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
5960Sstevel@tonic-gate return(-1);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate if (s->s2->tmp.session_id_length == 0)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate if (!ssl_get_new_session(s,1))
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6040Sstevel@tonic-gate return(-1);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate else
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
6100Sstevel@tonic-gate s->s2->tmp.session_id_length);
6110Sstevel@tonic-gate if (i == 1)
6120Sstevel@tonic-gate { /* previous session */
6130Sstevel@tonic-gate s->hit=1;
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate else if (i == -1)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6180Sstevel@tonic-gate return(-1);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate else
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate if (s->cert == NULL)
6230Sstevel@tonic-gate {
6240Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
6250Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
6260Sstevel@tonic-gate return(-1);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate if (!ssl_get_new_session(s,1))
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6320Sstevel@tonic-gate return(-1);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate if (!s->hit)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
6400Sstevel@tonic-gate &s->session->ciphers);
6410Sstevel@tonic-gate if (cs == NULL) goto mem_err;
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate cl=SSL_get_ciphers(s);
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate prio=sk_SSL_CIPHER_dup(cl);
6480Sstevel@tonic-gate if (prio == NULL) goto mem_err;
6490Sstevel@tonic-gate allow = cs;
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate else
6520Sstevel@tonic-gate {
6530Sstevel@tonic-gate prio = cs;
6540Sstevel@tonic-gate allow = cl;
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate for (z=0; z<sk_SSL_CIPHER_num(prio); z++)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate if (sk_SSL_CIPHER_find(allow,sk_SSL_CIPHER_value(prio,z)) < 0)
6590Sstevel@tonic-gate {
6600Sstevel@tonic-gate sk_SSL_CIPHER_delete(prio,z);
6610Sstevel@tonic-gate z--;
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate sk_SSL_CIPHER_free(s->session->ciphers);
6670Sstevel@tonic-gate s->session->ciphers = prio;
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate /* s->session->ciphers should now have a list of
6700Sstevel@tonic-gate * ciphers that are on both the client and server.
6710Sstevel@tonic-gate * This list is ordered by the order the client sent
6720Sstevel@tonic-gate * the ciphers or in the order of the server's preference
6730Sstevel@tonic-gate * if SSL_OP_CIPHER_SERVER_PREFERENCE was set.
6740Sstevel@tonic-gate */
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate p+=s->s2->tmp.cipher_spec_length;
6770Sstevel@tonic-gate /* done cipher selection */
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate /* session id extracted already */
6800Sstevel@tonic-gate p+=s->s2->tmp.session_id_length;
6810Sstevel@tonic-gate
6820Sstevel@tonic-gate /* challenge */
6830Sstevel@tonic-gate if (s->s2->challenge_length > sizeof s->s2->challenge)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6860Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
6870Sstevel@tonic-gate return -1;
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
6900Sstevel@tonic-gate return(1);
6910Sstevel@tonic-gate mem_err:
6920Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
6930Sstevel@tonic-gate return(0);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate
server_hello(SSL * s)6960Sstevel@tonic-gate static int server_hello(SSL *s)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate unsigned char *p,*d;
6990Sstevel@tonic-gate int n,hit;
7000Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *sk;
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
7030Sstevel@tonic-gate if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate d=p+11;
7060Sstevel@tonic-gate *(p++)=SSL2_MT_SERVER_HELLO; /* type */
7070Sstevel@tonic-gate hit=s->hit;
7080Sstevel@tonic-gate *(p++)=(unsigned char)hit;
7090Sstevel@tonic-gate #if 1
7100Sstevel@tonic-gate if (!hit)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate if (s->session->sess_cert != NULL)
7130Sstevel@tonic-gate /* This can't really happen because get_client_hello
7140Sstevel@tonic-gate * has called ssl_get_new_session, which does not set
7150Sstevel@tonic-gate * sess_cert. */
7160Sstevel@tonic-gate ssl_sess_cert_free(s->session->sess_cert);
7170Sstevel@tonic-gate s->session->sess_cert = ssl_sess_cert_new();
7180Sstevel@tonic-gate if (s->session->sess_cert == NULL)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
7210Sstevel@tonic-gate return(-1);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
7250Sstevel@tonic-gate * depending on whether it survived in the internal cache
7260Sstevel@tonic-gate * or was retrieved from an external cache.
7270Sstevel@tonic-gate * If it is NULL, we cannot put any useful data in it anyway,
7280Sstevel@tonic-gate * so we don't touch it.
7290Sstevel@tonic-gate */
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate #else /* That's what used to be done when cert_st and sess_cert_st were
7320Sstevel@tonic-gate * the same. */
7330Sstevel@tonic-gate if (!hit)
7340Sstevel@tonic-gate { /* else add cert to session */
7350Sstevel@tonic-gate CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
7360Sstevel@tonic-gate if (s->session->sess_cert != NULL)
7370Sstevel@tonic-gate ssl_cert_free(s->session->sess_cert);
7380Sstevel@tonic-gate s->session->sess_cert=s->cert;
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate else /* We have a session id-cache hit, if the
7410Sstevel@tonic-gate * session-id has no certificate listed against
7420Sstevel@tonic-gate * the 'cert' structure, grab the 'old' one
7430Sstevel@tonic-gate * listed against the SSL connection */
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate if (s->session->sess_cert == NULL)
7460Sstevel@tonic-gate {
7470Sstevel@tonic-gate CRYPTO_add(&s->cert->references,1,
7480Sstevel@tonic-gate CRYPTO_LOCK_SSL_CERT);
7490Sstevel@tonic-gate s->session->sess_cert=s->cert;
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate #endif
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate if (s->cert == NULL)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
7570Sstevel@tonic-gate SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
7580Sstevel@tonic-gate return(-1);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate
7610Sstevel@tonic-gate if (hit)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate *(p++)=0; /* no certificate type */
7640Sstevel@tonic-gate s2n(s->version,p); /* version */
7650Sstevel@tonic-gate s2n(0,p); /* cert len */
7660Sstevel@tonic-gate s2n(0,p); /* ciphers len */
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate else
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate /* EAY EAY */
7710Sstevel@tonic-gate /* put certificate type */
7720Sstevel@tonic-gate *(p++)=SSL2_CT_X509_CERTIFICATE;
7730Sstevel@tonic-gate s2n(s->version,p); /* version */
7740Sstevel@tonic-gate n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
7750Sstevel@tonic-gate s2n(n,p); /* certificate length */
7760Sstevel@tonic-gate i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
7770Sstevel@tonic-gate n=0;
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate /* lets send out the ciphers we like in the
7800Sstevel@tonic-gate * prefered order */
7810Sstevel@tonic-gate sk= s->session->ciphers;
7822139Sjp161948 n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d,0);
7830Sstevel@tonic-gate d+=n;
7840Sstevel@tonic-gate s2n(n,p); /* add cipher length */
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate /* make and send conn_id */
7880Sstevel@tonic-gate s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */
7890Sstevel@tonic-gate s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
7902139Sjp161948 if (RAND_pseudo_bytes(s->s2->conn_id,(int)s->s2->conn_id_length) <= 0)
7912139Sjp161948 return -1;
7920Sstevel@tonic-gate memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
7930Sstevel@tonic-gate d+=SSL2_CONNECTION_ID_LENGTH;
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_HELLO_B;
7960Sstevel@tonic-gate s->init_num=d-(unsigned char *)s->init_buf->data;
7970Sstevel@tonic-gate s->init_off=0;
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate /* SSL2_ST_SEND_SERVER_HELLO_B */
8000Sstevel@tonic-gate /* If we are using TCP/IP, the performance is bad if we do 2
8010Sstevel@tonic-gate * writes without a read between them. This occurs when
8020Sstevel@tonic-gate * Session-id reuse is used, so I will put in a buffering module
8030Sstevel@tonic-gate */
8040Sstevel@tonic-gate if (s->hit)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate if (!ssl_init_wbio_buffer(s,1)) return(-1);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate return(ssl2_do_write(s));
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate
get_client_finished(SSL * s)8120Sstevel@tonic-gate static int get_client_finished(SSL *s)
8130Sstevel@tonic-gate {
8140Sstevel@tonic-gate unsigned char *p;
8150Sstevel@tonic-gate int i, n;
8160Sstevel@tonic-gate unsigned long len;
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
8190Sstevel@tonic-gate if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
8220Sstevel@tonic-gate if (i < 1-s->init_num)
8230Sstevel@tonic-gate return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
8240Sstevel@tonic-gate s->init_num += i;
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate if (*p != SSL2_MT_CLIENT_FINISHED)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate if (*p != SSL2_MT_ERROR)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
8310Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
8320Sstevel@tonic-gate }
8330Sstevel@tonic-gate else
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
8360Sstevel@tonic-gate /* try to read the error message */
8370Sstevel@tonic-gate i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
8380Sstevel@tonic-gate return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate return(-1);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate /* SSL2_ST_GET_CLIENT_FINISHED_B */
8460Sstevel@tonic-gate if (s->s2->conn_id_length > sizeof s->s2->conn_id)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
8490Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
8500Sstevel@tonic-gate return -1;
8510Sstevel@tonic-gate }
8520Sstevel@tonic-gate len = 1 + (unsigned long)s->s2->conn_id_length;
8530Sstevel@tonic-gate n = (int)len - s->init_num;
8540Sstevel@tonic-gate i = ssl2_read(s,(char *)&(p[s->init_num]),n);
8550Sstevel@tonic-gate if (i < n)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate if (s->msg_callback)
8600Sstevel@tonic-gate s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-FINISHED */
8610Sstevel@tonic-gate p += 1;
8620Sstevel@tonic-gate if (memcmp(p,s->s2->conn_id,s->s2->conn_id_length) != 0)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
8650Sstevel@tonic-gate SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
8660Sstevel@tonic-gate return(-1);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate return(1);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
server_verify(SSL * s)8710Sstevel@tonic-gate static int server_verify(SSL *s)
8720Sstevel@tonic-gate {
8730Sstevel@tonic-gate unsigned char *p;
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
8780Sstevel@tonic-gate *(p++)=SSL2_MT_SERVER_VERIFY;
8790Sstevel@tonic-gate if (s->s2->challenge_length > sizeof s->s2->challenge)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate SSLerr(SSL_F_SERVER_VERIFY, ERR_R_INTERNAL_ERROR);
8820Sstevel@tonic-gate return -1;
8830Sstevel@tonic-gate }
8840Sstevel@tonic-gate memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
8850Sstevel@tonic-gate /* p+=s->s2->challenge_length; */
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
8880Sstevel@tonic-gate s->init_num=s->s2->challenge_length+1;
8890Sstevel@tonic-gate s->init_off=0;
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate return(ssl2_do_write(s));
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
server_finish(SSL * s)8940Sstevel@tonic-gate static int server_finish(SSL *s)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate unsigned char *p;
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
9010Sstevel@tonic-gate *(p++)=SSL2_MT_SERVER_FINISHED;
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate if (s->session->session_id_length > sizeof s->session->session_id)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate SSLerr(SSL_F_SERVER_FINISH, ERR_R_INTERNAL_ERROR);
9060Sstevel@tonic-gate return -1;
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate memcpy(p,s->session->session_id, (unsigned int)s->session->session_id_length);
9090Sstevel@tonic-gate /* p+=s->session->session_id_length; */
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
9120Sstevel@tonic-gate s->init_num=s->session->session_id_length+1;
9130Sstevel@tonic-gate s->init_off=0;
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate /* SSL2_ST_SEND_SERVER_FINISHED_B */
9170Sstevel@tonic-gate return(ssl2_do_write(s));
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate /* send the request and check the response */
request_certificate(SSL * s)9210Sstevel@tonic-gate static int request_certificate(SSL *s)
9220Sstevel@tonic-gate {
9232139Sjp161948 const unsigned char *cp;
9240Sstevel@tonic-gate unsigned char *p,*p2,*buf2;
9250Sstevel@tonic-gate unsigned char *ccd;
9260Sstevel@tonic-gate int i,j,ctype,ret= -1;
9270Sstevel@tonic-gate unsigned long len;
9280Sstevel@tonic-gate X509 *x509=NULL;
9290Sstevel@tonic-gate STACK_OF(X509) *sk=NULL;
9300Sstevel@tonic-gate
9310Sstevel@tonic-gate ccd=s->s2->tmp.ccl;
9320Sstevel@tonic-gate if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
9350Sstevel@tonic-gate *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
9360Sstevel@tonic-gate *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
9372139Sjp161948 if (RAND_pseudo_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH) <= 0)
9382139Sjp161948 return -1;
9390Sstevel@tonic-gate memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
9420Sstevel@tonic-gate s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
9430Sstevel@tonic-gate s->init_off=0;
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate i=ssl2_do_write(s);
9490Sstevel@tonic-gate if (i <= 0)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate ret=i;
9520Sstevel@tonic-gate goto end;
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate s->init_num=0;
9560Sstevel@tonic-gate s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
9600Sstevel@tonic-gate {
9610Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
9620Sstevel@tonic-gate i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num); /* try to read 6 octets ... */
9630Sstevel@tonic-gate if (i < 3-s->init_num) /* ... but don't call ssl2_part_read now if we got at least 3
9640Sstevel@tonic-gate * (probably NO-CERTIFICATE-ERROR) */
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
9670Sstevel@tonic-gate goto end;
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate s->init_num += i;
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate if ((s->init_num >= 3) && (p[0] == SSL2_MT_ERROR))
9720Sstevel@tonic-gate {
9730Sstevel@tonic-gate n2s(p,i);
9740Sstevel@tonic-gate if (i != SSL2_PE_NO_CERTIFICATE)
9750Sstevel@tonic-gate {
9760Sstevel@tonic-gate /* not the error message we expected -- let ssl2_part_read handle it */
9770Sstevel@tonic-gate s->init_num -= 3;
9780Sstevel@tonic-gate ret = ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE, 3);
9790Sstevel@tonic-gate goto end;
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate if (s->msg_callback)
9830Sstevel@tonic-gate s->msg_callback(0, s->version, 0, p, 3, s, s->msg_callback_arg); /* ERROR */
9840Sstevel@tonic-gate
9850Sstevel@tonic-gate /* this is the one place where we can recover from an SSL 2.0 error */
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
9880Sstevel@tonic-gate {
9890Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
9900Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
9910Sstevel@tonic-gate goto end;
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate ret=1;
9940Sstevel@tonic-gate goto end;
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (s->init_num < 6))
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
9990Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
10000Sstevel@tonic-gate goto end;
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate if (s->init_num != 6)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_INTERNAL_ERROR);
10050Sstevel@tonic-gate goto end;
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate
10080Sstevel@tonic-gate /* ok we have a response */
10090Sstevel@tonic-gate /* certificate type, there is only one right now. */
10100Sstevel@tonic-gate ctype= *(p++);
10110Sstevel@tonic-gate if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
10140Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
10150Sstevel@tonic-gate goto end;
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate n2s(p,i); s->s2->tmp.clen=i;
10180Sstevel@tonic-gate n2s(p,i); s->s2->tmp.rlen=i;
10190Sstevel@tonic-gate s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate
10220Sstevel@tonic-gate /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
10230Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data;
10240Sstevel@tonic-gate len = 6 + (unsigned long)s->s2->tmp.clen + (unsigned long)s->s2->tmp.rlen;
10250Sstevel@tonic-gate if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_MESSAGE_TOO_LONG);
10280Sstevel@tonic-gate goto end;
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate j = (int)len - s->init_num;
10310Sstevel@tonic-gate i = ssl2_read(s,(char *)&(p[s->init_num]),j);
10320Sstevel@tonic-gate if (i < j)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
10350Sstevel@tonic-gate goto end;
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate if (s->msg_callback)
10380Sstevel@tonic-gate s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-CERTIFICATE */
10390Sstevel@tonic-gate p += 6;
10400Sstevel@tonic-gate
10412139Sjp161948 cp = p;
10422139Sjp161948 x509=(X509 *)d2i_X509(NULL,&cp,(long)s->s2->tmp.clen);
10430Sstevel@tonic-gate if (x509 == NULL)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
10460Sstevel@tonic-gate goto msg_end;
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
10520Sstevel@tonic-gate goto msg_end;
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate i=ssl_verify_cert_chain(s,sk);
10560Sstevel@tonic-gate
1057*8545SJan.Pechanec@Sun.COM if (i > 0) /* we like the packet, now check the chksum */
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate EVP_MD_CTX ctx;
10600Sstevel@tonic-gate EVP_PKEY *pkey=NULL;
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate EVP_MD_CTX_init(&ctx);
10630Sstevel@tonic-gate EVP_VerifyInit_ex(&ctx,s->ctx->rsa_md5, NULL);
10640Sstevel@tonic-gate EVP_VerifyUpdate(&ctx,s->s2->key_material,
10650Sstevel@tonic-gate s->s2->key_material_length);
10660Sstevel@tonic-gate EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
10690Sstevel@tonic-gate buf2=OPENSSL_malloc((unsigned int)i);
10700Sstevel@tonic-gate if (buf2 == NULL)
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
10730Sstevel@tonic-gate goto msg_end;
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate p2=buf2;
10760Sstevel@tonic-gate i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
10770Sstevel@tonic-gate EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
10780Sstevel@tonic-gate OPENSSL_free(buf2);
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate pkey=X509_get_pubkey(x509);
10810Sstevel@tonic-gate if (pkey == NULL) goto end;
10822139Sjp161948 i=EVP_VerifyFinal(&ctx,cp,s->s2->tmp.rlen,pkey);
10830Sstevel@tonic-gate EVP_PKEY_free(pkey);
10840Sstevel@tonic-gate EVP_MD_CTX_cleanup(&ctx);
10850Sstevel@tonic-gate
1086*8545SJan.Pechanec@Sun.COM if (i > 0)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate if (s->session->peer != NULL)
10890Sstevel@tonic-gate X509_free(s->session->peer);
10900Sstevel@tonic-gate s->session->peer=x509;
10910Sstevel@tonic-gate CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
10920Sstevel@tonic-gate s->session->verify_result = s->verify_result;
10930Sstevel@tonic-gate ret=1;
10940Sstevel@tonic-gate goto end;
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate else
10970Sstevel@tonic-gate {
10980Sstevel@tonic-gate SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
10990Sstevel@tonic-gate goto msg_end;
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate else
11030Sstevel@tonic-gate {
11040Sstevel@tonic-gate msg_end:
11050Sstevel@tonic-gate ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate end:
11080Sstevel@tonic-gate sk_X509_free(sk);
11090Sstevel@tonic-gate X509_free(x509);
11100Sstevel@tonic-gate return(ret);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate
ssl_rsa_private_decrypt(CERT * c,int len,unsigned char * from,unsigned char * to,int padding)11130Sstevel@tonic-gate static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
11140Sstevel@tonic-gate unsigned char *to, int padding)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate RSA *rsa;
11170Sstevel@tonic-gate int i;
11180Sstevel@tonic-gate
11190Sstevel@tonic-gate if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
11220Sstevel@tonic-gate return(-1);
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
11270Sstevel@tonic-gate return(-1);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate /* we have the public key */
11320Sstevel@tonic-gate i=RSA_private_decrypt(len,from,to,rsa,padding);
11330Sstevel@tonic-gate if (i < 0)
11340Sstevel@tonic-gate SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
11350Sstevel@tonic-gate return(i);
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate #else /* !OPENSSL_NO_SSL2 */
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate # if PEDANTIC
11400Sstevel@tonic-gate static void *dummy=&dummy;
11410Sstevel@tonic-gate # endif
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate #endif
1144