155714Skris/* ssl/s2_srvr.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/bio.h>
116296465Sdelphij# include <openssl/rand.h>
117296465Sdelphij# include <openssl/objects.h>
118296465Sdelphij# include <openssl/evp.h>
11955714Skris
12055714Skrisstatic SSL_METHOD *ssl2_get_server_method(int ver);
12155714Skrisstatic int get_client_master_key(SSL *s);
12255714Skrisstatic int get_client_hello(SSL *s);
123296465Sdelphijstatic int server_hello(SSL *s);
12455714Skrisstatic int get_client_finished(SSL *s);
12555714Skrisstatic int server_verify(SSL *s);
12655714Skrisstatic int server_finish(SSL *s);
12755714Skrisstatic int request_certificate(SSL *s);
12855714Skrisstatic int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
129296465Sdelphij                                   unsigned char *to, int padding);
130296465Sdelphij# define BREAK   break
13155714Skris
13255714Skrisstatic SSL_METHOD *ssl2_get_server_method(int ver)
133296465Sdelphij{
134296465Sdelphij    if (ver == SSL2_VERSION)
135296465Sdelphij        return (SSLv2_server_method());
136296465Sdelphij    else
137296465Sdelphij        return (NULL);
138296465Sdelphij}
13955714Skris
140160817SsimonIMPLEMENT_ssl2_meth_func(SSLv2_server_method,
141296465Sdelphij                         ssl2_accept,
142296465Sdelphij                         ssl_undefined_function, ssl2_get_server_method)
14355714Skris
14455714Skrisint ssl2_accept(SSL *s)
145296465Sdelphij{
146296465Sdelphij    unsigned long l = (unsigned long)time(NULL);
147296465Sdelphij    BUF_MEM *buf = NULL;
148296465Sdelphij    int ret = -1;
149296465Sdelphij    long num1;
150296465Sdelphij    void (*cb) (const SSL *ssl, int type, int val) = NULL;
151296465Sdelphij    int new_state, state;
15255714Skris
153296465Sdelphij    RAND_add(&l, sizeof(l), 0);
154296465Sdelphij    ERR_clear_error();
155296465Sdelphij    clear_sys_error();
15655714Skris
157296465Sdelphij    if (s->info_callback != NULL)
158296465Sdelphij        cb = s->info_callback;
159296465Sdelphij    else if (s->ctx->info_callback != NULL)
160296465Sdelphij        cb = s->ctx->info_callback;
16155714Skris
162296465Sdelphij    /* init things to blank */
163296465Sdelphij    s->in_handshake++;
164296465Sdelphij    if (!SSL_in_init(s) || SSL_in_before(s))
165296465Sdelphij        SSL_clear(s);
16655714Skris
167296465Sdelphij    if (s->cert == NULL) {
168296465Sdelphij        SSLerr(SSL_F_SSL2_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
169296465Sdelphij        return (-1);
170296465Sdelphij    }
17155714Skris
172296465Sdelphij    clear_sys_error();
173296465Sdelphij    for (;;) {
174296465Sdelphij        state = s->state;
17555714Skris
176296465Sdelphij        switch (s->state) {
177296465Sdelphij        case SSL_ST_BEFORE:
178296465Sdelphij        case SSL_ST_ACCEPT:
179296465Sdelphij        case SSL_ST_BEFORE | SSL_ST_ACCEPT:
180296465Sdelphij        case SSL_ST_OK | SSL_ST_ACCEPT:
18155714Skris
182296465Sdelphij            s->server = 1;
183296465Sdelphij            if (cb != NULL)
184296465Sdelphij                cb(s, SSL_CB_HANDSHAKE_START, 1);
18555714Skris
186296465Sdelphij            s->version = SSL2_VERSION;
187296465Sdelphij            s->type = SSL_ST_ACCEPT;
18855714Skris
189296465Sdelphij            buf = s->init_buf;
190296465Sdelphij            if ((buf == NULL) && ((buf = BUF_MEM_new()) == NULL)) {
191296465Sdelphij                ret = -1;
192296465Sdelphij                goto end;
193296465Sdelphij            }
194296465Sdelphij            if (!BUF_MEM_grow(buf, (int)
195296465Sdelphij                              SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) {
196296465Sdelphij                ret = -1;
197296465Sdelphij                goto end;
198296465Sdelphij            }
199296465Sdelphij            s->init_buf = buf;
200296465Sdelphij            s->init_num = 0;
201296465Sdelphij            s->ctx->stats.sess_accept++;
202296465Sdelphij            s->handshake_func = ssl2_accept;
203296465Sdelphij            s->state = SSL2_ST_GET_CLIENT_HELLO_A;
204296465Sdelphij            BREAK;
20555714Skris
206296465Sdelphij        case SSL2_ST_GET_CLIENT_HELLO_A:
207296465Sdelphij        case SSL2_ST_GET_CLIENT_HELLO_B:
208296465Sdelphij        case SSL2_ST_GET_CLIENT_HELLO_C:
209296465Sdelphij            s->shutdown = 0;
210296465Sdelphij            ret = get_client_hello(s);
211296465Sdelphij            if (ret <= 0)
212296465Sdelphij                goto end;
213296465Sdelphij            s->init_num = 0;
214296465Sdelphij            s->state = SSL2_ST_SEND_SERVER_HELLO_A;
215296465Sdelphij            BREAK;
21655714Skris
217296465Sdelphij        case SSL2_ST_SEND_SERVER_HELLO_A:
218296465Sdelphij        case SSL2_ST_SEND_SERVER_HELLO_B:
219296465Sdelphij            ret = server_hello(s);
220296465Sdelphij            if (ret <= 0)
221296465Sdelphij                goto end;
222296465Sdelphij            s->init_num = 0;
223296465Sdelphij            if (!s->hit) {
224296465Sdelphij                s->state = SSL2_ST_GET_CLIENT_MASTER_KEY_A;
225296465Sdelphij                BREAK;
226296465Sdelphij            } else {
227296465Sdelphij                s->state = SSL2_ST_SERVER_START_ENCRYPTION;
228296465Sdelphij                BREAK;
229296465Sdelphij            }
230296465Sdelphij        case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
231296465Sdelphij        case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
232296465Sdelphij            ret = get_client_master_key(s);
233296465Sdelphij            if (ret <= 0)
234296465Sdelphij                goto end;
235296465Sdelphij            s->init_num = 0;
236296465Sdelphij            s->state = SSL2_ST_SERVER_START_ENCRYPTION;
237296465Sdelphij            BREAK;
23855714Skris
239296465Sdelphij        case SSL2_ST_SERVER_START_ENCRYPTION:
240296465Sdelphij            /*
241296465Sdelphij             * Ok we how have sent all the stuff needed to start encrypting,
242296465Sdelphij             * the next packet back will be encrypted.
243296465Sdelphij             */
244296465Sdelphij            if (!ssl2_enc_init(s, 0)) {
245296465Sdelphij                ret = -1;
246296465Sdelphij                goto end;
247296465Sdelphij            }
248296465Sdelphij            s->s2->clear_text = 0;
249296465Sdelphij            s->state = SSL2_ST_SEND_SERVER_VERIFY_A;
250296465Sdelphij            BREAK;
25155714Skris
252296465Sdelphij        case SSL2_ST_SEND_SERVER_VERIFY_A:
253296465Sdelphij        case SSL2_ST_SEND_SERVER_VERIFY_B:
254296465Sdelphij            ret = server_verify(s);
255296465Sdelphij            if (ret <= 0)
256296465Sdelphij                goto end;
257296465Sdelphij            s->init_num = 0;
258296465Sdelphij            if (s->hit) {
259296465Sdelphij                /*
260296465Sdelphij                 * If we are in here, we have been buffering the output, so
261296465Sdelphij                 * we need to flush it and remove buffering from future
262296465Sdelphij                 * traffic
263296465Sdelphij                 */
264296465Sdelphij                s->state = SSL2_ST_SEND_SERVER_VERIFY_C;
265296465Sdelphij                BREAK;
266296465Sdelphij            } else {
267296465Sdelphij                s->state = SSL2_ST_GET_CLIENT_FINISHED_A;
268296465Sdelphij                break;
269296465Sdelphij            }
27055714Skris
271296465Sdelphij        case SSL2_ST_SEND_SERVER_VERIFY_C:
272296465Sdelphij            /* get the number of bytes to write */
273296465Sdelphij            num1 = BIO_ctrl(s->wbio, BIO_CTRL_INFO, 0, NULL);
274296465Sdelphij            if (num1 > 0) {
275296465Sdelphij                s->rwstate = SSL_WRITING;
276296465Sdelphij                num1 = BIO_flush(s->wbio);
277296465Sdelphij                if (num1 <= 0) {
278296465Sdelphij                    ret = -1;
279296465Sdelphij                    goto end;
280296465Sdelphij                }
281296465Sdelphij                s->rwstate = SSL_NOTHING;
282296465Sdelphij            }
28355714Skris
284296465Sdelphij            /* flushed and now remove buffering */
285296465Sdelphij            s->wbio = BIO_pop(s->wbio);
28655714Skris
287296465Sdelphij            s->state = SSL2_ST_GET_CLIENT_FINISHED_A;
288296465Sdelphij            BREAK;
28955714Skris
290296465Sdelphij        case SSL2_ST_GET_CLIENT_FINISHED_A:
291296465Sdelphij        case SSL2_ST_GET_CLIENT_FINISHED_B:
292296465Sdelphij            ret = get_client_finished(s);
293296465Sdelphij            if (ret <= 0)
294296465Sdelphij                goto end;
295296465Sdelphij            s->init_num = 0;
296296465Sdelphij            s->state = SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
297296465Sdelphij            BREAK;
29855714Skris
299296465Sdelphij        case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
300296465Sdelphij        case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
301296465Sdelphij        case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
302296465Sdelphij        case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
303296465Sdelphij            /*
304296465Sdelphij             * don't do a 'request certificate' if we don't want to, or we
305296465Sdelphij             * already have one, and we only want to do it once.
306296465Sdelphij             */
307296465Sdelphij            if (!(s->verify_mode & SSL_VERIFY_PEER) ||
308296465Sdelphij                ((s->session->peer != NULL) &&
309296465Sdelphij                 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE))) {
310296465Sdelphij                s->state = SSL2_ST_SEND_SERVER_FINISHED_A;
311296465Sdelphij                break;
312296465Sdelphij            } else {
313296465Sdelphij                ret = request_certificate(s);
314296465Sdelphij                if (ret <= 0)
315296465Sdelphij                    goto end;
316296465Sdelphij                s->init_num = 0;
317296465Sdelphij                s->state = SSL2_ST_SEND_SERVER_FINISHED_A;
318296465Sdelphij            }
319296465Sdelphij            BREAK;
32055714Skris
321296465Sdelphij        case SSL2_ST_SEND_SERVER_FINISHED_A:
322296465Sdelphij        case SSL2_ST_SEND_SERVER_FINISHED_B:
323296465Sdelphij            ret = server_finish(s);
324296465Sdelphij            if (ret <= 0)
325296465Sdelphij                goto end;
326296465Sdelphij            s->init_num = 0;
327296465Sdelphij            s->state = SSL_ST_OK;
328296465Sdelphij            break;
32955714Skris
330296465Sdelphij        case SSL_ST_OK:
331296465Sdelphij            BUF_MEM_free(s->init_buf);
332296465Sdelphij            ssl_free_wbio_buffer(s);
333296465Sdelphij            s->init_buf = NULL;
334296465Sdelphij            s->init_num = 0;
335296465Sdelphij            /*      ERR_clear_error(); */
33655714Skris
337296465Sdelphij            ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
33855714Skris
339296465Sdelphij            s->ctx->stats.sess_accept_good++;
340296465Sdelphij            /* s->server=1; */
341296465Sdelphij            ret = 1;
34255714Skris
343296465Sdelphij            if (cb != NULL)
344296465Sdelphij                cb(s, SSL_CB_HANDSHAKE_DONE, 1);
34555714Skris
346296465Sdelphij            goto end;
347296465Sdelphij            /* BREAK; */
34855714Skris
349296465Sdelphij        default:
350296465Sdelphij            SSLerr(SSL_F_SSL2_ACCEPT, SSL_R_UNKNOWN_STATE);
351296465Sdelphij            ret = -1;
352296465Sdelphij            goto end;
353296465Sdelphij            /* BREAK; */
354296465Sdelphij        }
35555714Skris
356296465Sdelphij        if ((cb != NULL) && (s->state != state)) {
357296465Sdelphij            new_state = s->state;
358296465Sdelphij            s->state = state;
359296465Sdelphij            cb(s, SSL_CB_ACCEPT_LOOP, 1);
360296465Sdelphij            s->state = new_state;
361296465Sdelphij        }
362296465Sdelphij    }
363296465Sdelphij end:
364296465Sdelphij    s->in_handshake--;
365296465Sdelphij    if (cb != NULL)
366296465Sdelphij        cb(s, SSL_CB_ACCEPT_EXIT, ret);
367296465Sdelphij    return (ret);
368296465Sdelphij}
369296465Sdelphij
37055714Skrisstatic int get_client_master_key(SSL *s)
371296465Sdelphij{
372296465Sdelphij    int is_export, i, n, keya;
373296465Sdelphij    unsigned int ek;
374296465Sdelphij    unsigned long len;
375296465Sdelphij    unsigned char *p;
376296465Sdelphij    SSL_CIPHER *cp;
377296465Sdelphij    const EVP_CIPHER *c;
378296465Sdelphij    const EVP_MD *md;
37955714Skris
380296465Sdelphij    p = (unsigned char *)s->init_buf->data;
381296465Sdelphij    if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A) {
382296465Sdelphij        i = ssl2_read(s, (char *)&(p[s->init_num]), 10 - s->init_num);
38355714Skris
384296465Sdelphij        if (i < (10 - s->init_num))
385296465Sdelphij            return (ssl2_part_read(s, SSL_F_GET_CLIENT_MASTER_KEY, i));
386296465Sdelphij        s->init_num = 10;
38789840Skris
388296465Sdelphij        if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY) {
389296465Sdelphij            if (p[-1] != SSL2_MT_ERROR) {
390296465Sdelphij                ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
391296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
392296465Sdelphij                       SSL_R_READ_WRONG_PACKET_TYPE);
393296465Sdelphij            } else
394296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_PEER_ERROR);
395296465Sdelphij            return (-1);
396296465Sdelphij        }
39755714Skris
398296465Sdelphij        cp = ssl2_get_cipher_by_char(p);
399296465Sdelphij        if (cp == NULL || sk_SSL_CIPHER_find(s->session->ciphers, cp) < 0) {
400296465Sdelphij            ssl2_return_error(s, SSL2_PE_NO_CIPHER);
401296465Sdelphij            SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_NO_CIPHER_MATCH);
402296465Sdelphij            return (-1);
403296465Sdelphij        }
404296465Sdelphij        s->session->cipher = cp;
40555714Skris
406296465Sdelphij        p += 3;
407296465Sdelphij        n2s(p, i);
408296465Sdelphij        s->s2->tmp.clear = i;
409296465Sdelphij        n2s(p, i);
410296465Sdelphij        s->s2->tmp.enc = i;
411296465Sdelphij        n2s(p, i);
412296465Sdelphij        if (i > SSL_MAX_KEY_ARG_LENGTH) {
413296465Sdelphij            ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
414296465Sdelphij            SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_KEY_ARG_TOO_LONG);
415296465Sdelphij            return -1;
416296465Sdelphij        }
417296465Sdelphij        s->session->key_arg_length = i;
418296465Sdelphij        s->state = SSL2_ST_GET_CLIENT_MASTER_KEY_B;
419296465Sdelphij    }
42055714Skris
421296465Sdelphij    /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
422296465Sdelphij    p = (unsigned char *)s->init_buf->data;
423296465Sdelphij    if (s->init_buf->length < SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) {
424296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
425296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
426296465Sdelphij        return -1;
427296465Sdelphij    }
428296465Sdelphij    keya = s->session->key_arg_length;
429296465Sdelphij    len =
430296465Sdelphij        10 + (unsigned long)s->s2->tmp.clear + (unsigned long)s->s2->tmp.enc +
431296465Sdelphij        (unsigned long)keya;
432296465Sdelphij    if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) {
433296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
434296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_MESSAGE_TOO_LONG);
435296465Sdelphij        return -1;
436296465Sdelphij    }
437296465Sdelphij    n = (int)len - s->init_num;
438296465Sdelphij    i = ssl2_read(s, (char *)&(p[s->init_num]), n);
439296465Sdelphij    if (i != n)
440296465Sdelphij        return (ssl2_part_read(s, SSL_F_GET_CLIENT_MASTER_KEY, i));
441296465Sdelphij    if (s->msg_callback) {
442296465Sdelphij        /* CLIENT-MASTER-KEY */
443296465Sdelphij        s->msg_callback(0, s->version, 0, p, (size_t)len, s,
444296465Sdelphij                        s->msg_callback_arg);
445296465Sdelphij    }
446296465Sdelphij    p += 10;
44755714Skris
448296465Sdelphij    memcpy(s->session->key_arg, &(p[s->s2->tmp.clear + s->s2->tmp.enc]),
449296465Sdelphij           (unsigned int)keya);
45055714Skris
451296465Sdelphij    if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) {
452296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
453296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_NO_PRIVATEKEY);
454296465Sdelphij        return (-1);
455296465Sdelphij    }
45655714Skris
457296465Sdelphij    is_export = SSL_C_IS_EXPORT(s->session->cipher);
45855714Skris
459296465Sdelphij    if (!ssl_cipher_get_evp(s->session, &c, &md, NULL)) {
460296465Sdelphij        ssl2_return_error(s, SSL2_PE_NO_CIPHER);
461296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
462296465Sdelphij               SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
463296465Sdelphij        return (0);
464296465Sdelphij    }
46555714Skris
466296465Sdelphij    if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC) {
467296465Sdelphij        is_export = 1;
468296465Sdelphij        ek = 8;
469296465Sdelphij    } else
470296465Sdelphij        ek = 5;
471280268Sdelphij
472296465Sdelphij    /*
473296465Sdelphij     * The format of the CLIENT-MASTER-KEY message is
474296465Sdelphij     * 1 byte message type
475296465Sdelphij     * 3 bytes cipher
476296465Sdelphij     * 2-byte clear key length (stored in s->s2->tmp.clear)
477296465Sdelphij     * 2-byte encrypted key length (stored in s->s2->tmp.enc)
478296465Sdelphij     * 2-byte key args length (IV etc)
479296465Sdelphij     * clear key
480296465Sdelphij     * encrypted key
481296465Sdelphij     * key args
482296465Sdelphij     *
483296465Sdelphij     * If the cipher is an export cipher, then the encrypted key bytes
484296465Sdelphij     * are a fixed portion of the total key (5 or 8 bytes). The size of
485296465Sdelphij     * this portion is in |ek|. If the cipher is not an export cipher,
486296465Sdelphij     * then the entire key material is encrypted (i.e., clear key length
487296465Sdelphij     * must be zero).
488296465Sdelphij     */
489296465Sdelphij    if ((!is_export && s->s2->tmp.clear != 0) ||
490296465Sdelphij        (is_export && s->s2->tmp.clear + ek != (unsigned int)EVP_CIPHER_key_length(c))) {
491296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
492296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_LENGTH);
493296465Sdelphij        return -1;
494296465Sdelphij    }
495296465Sdelphij    /*
496296465Sdelphij     * The encrypted blob must decrypt to the encrypted portion of the key.
497296465Sdelphij     * Decryption can't be expanding, so if we don't have enough encrypted
498296465Sdelphij     * bytes to fit the key in the buffer, stop now.
499296465Sdelphij     */
500296465Sdelphij    if ((is_export && s->s2->tmp.enc < ek) ||
501296465Sdelphij        (!is_export && s->s2->tmp.enc < (unsigned int)EVP_CIPHER_key_length(c))) {
502296465Sdelphij        ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
503296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_LENGTH_TOO_SHORT);
504296465Sdelphij        return -1;
505296465Sdelphij    }
506296465Sdelphij
507296465Sdelphij    i = ssl_rsa_private_decrypt(s->cert, s->s2->tmp.enc,
508280268Sdelphij                                &(p[s->s2->tmp.clear]),
509280268Sdelphij                                &(p[s->s2->tmp.clear]),
510280268Sdelphij                                (s->s2->ssl2_rollback) ? RSA_SSLV23_PADDING :
511280268Sdelphij                                RSA_PKCS1_PADDING);
512280268Sdelphij
513296465Sdelphij    /* bad decrypt */
514296465Sdelphij# if 1
515296465Sdelphij    /*
516296465Sdelphij     * If a bad decrypt, continue with protocol but with a random master
517296465Sdelphij     * secret (Bleichenbacher attack)
518296465Sdelphij     */
519296465Sdelphij    if ((i < 0) || ((!is_export && i != EVP_CIPHER_key_length(c))
520296465Sdelphij                    || (is_export && i != (int)ek))) {
521296465Sdelphij        ERR_clear_error();
522296465Sdelphij        if (is_export)
523296465Sdelphij            i = ek;
524296465Sdelphij        else
525296465Sdelphij            i = EVP_CIPHER_key_length(c);
526296465Sdelphij        if (RAND_pseudo_bytes(&p[s->s2->tmp.clear], i) <= 0)
527296465Sdelphij            return 0;
528296465Sdelphij    }
529296465Sdelphij# else
530296465Sdelphij    if (i < 0) {
531296465Sdelphij        error = 1;
532296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_BAD_RSA_DECRYPT);
533296465Sdelphij    }
534296465Sdelphij    /* incorrect number of key bytes for non export cipher */
535296465Sdelphij    else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
536296465Sdelphij             || (is_export && ((i != ek) || (s->s2->tmp.clear + i !=
537296465Sdelphij                                             EVP_CIPHER_key_length(c))))) {
538296465Sdelphij        error = 1;
539296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_WRONG_NUMBER_OF_KEY_BITS);
540296465Sdelphij    }
541296465Sdelphij    if (error) {
542296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
543296465Sdelphij        return (-1);
544296465Sdelphij    }
545296465Sdelphij# endif
54655714Skris
547296465Sdelphij    if (is_export)
548296465Sdelphij        i = EVP_CIPHER_key_length(c);
549101621Snectar
550296465Sdelphij    if (i > SSL_MAX_MASTER_KEY_LENGTH) {
551296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
552296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
553296465Sdelphij        return -1;
554296465Sdelphij    }
555296465Sdelphij    s->session->master_key_length = i;
556296465Sdelphij    memcpy(s->session->master_key, p, (unsigned int)i);
557296465Sdelphij    return (1);
558296465Sdelphij}
55955714Skris
56055714Skrisstatic int get_client_hello(SSL *s)
561296465Sdelphij{
562296465Sdelphij    int i, n;
563296465Sdelphij    unsigned long len;
564296465Sdelphij    unsigned char *p;
565296465Sdelphij    STACK_OF(SSL_CIPHER) *cs;   /* a stack of SSL_CIPHERS */
566296465Sdelphij    STACK_OF(SSL_CIPHER) *cl;   /* the ones we want to use */
567296465Sdelphij    STACK_OF(SSL_CIPHER) *prio, *allow;
568296465Sdelphij    int z;
56955714Skris
570296465Sdelphij    /*
571296465Sdelphij     * This is a bit of a hack to check for the correct packet type the first
572296465Sdelphij     * time round.
573296465Sdelphij     */
574296465Sdelphij    if (s->state == SSL2_ST_GET_CLIENT_HELLO_A) {
575296465Sdelphij        s->first_packet = 1;
576296465Sdelphij        s->state = SSL2_ST_GET_CLIENT_HELLO_B;
577296465Sdelphij    }
57855714Skris
579296465Sdelphij    p = (unsigned char *)s->init_buf->data;
580296465Sdelphij    if (s->state == SSL2_ST_GET_CLIENT_HELLO_B) {
581296465Sdelphij        i = ssl2_read(s, (char *)&(p[s->init_num]), 9 - s->init_num);
582296465Sdelphij        if (i < (9 - s->init_num))
583296465Sdelphij            return (ssl2_part_read(s, SSL_F_GET_CLIENT_HELLO, i));
584296465Sdelphij        s->init_num = 9;
58555714Skris
586296465Sdelphij        if (*(p++) != SSL2_MT_CLIENT_HELLO) {
587296465Sdelphij            if (p[-1] != SSL2_MT_ERROR) {
588296465Sdelphij                ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
589296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_READ_WRONG_PACKET_TYPE);
590296465Sdelphij            } else
591296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_PEER_ERROR);
592296465Sdelphij            return (-1);
593296465Sdelphij        }
594296465Sdelphij        n2s(p, i);
595296465Sdelphij        if (i < s->version)
596296465Sdelphij            s->version = i;
597296465Sdelphij        n2s(p, i);
598296465Sdelphij        s->s2->tmp.cipher_spec_length = i;
599296465Sdelphij        n2s(p, i);
600296465Sdelphij        s->s2->tmp.session_id_length = i;
601296465Sdelphij        n2s(p, i);
602296465Sdelphij        s->s2->challenge_length = i;
603296465Sdelphij        if ((i < SSL2_MIN_CHALLENGE_LENGTH) ||
604296465Sdelphij            (i > SSL2_MAX_CHALLENGE_LENGTH)) {
605296465Sdelphij            ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
606296465Sdelphij            SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_INVALID_CHALLENGE_LENGTH);
607296465Sdelphij            return (-1);
608296465Sdelphij        }
609296465Sdelphij        s->state = SSL2_ST_GET_CLIENT_HELLO_C;
610296465Sdelphij    }
61155714Skris
612296465Sdelphij    /* SSL2_ST_GET_CLIENT_HELLO_C */
613296465Sdelphij    p = (unsigned char *)s->init_buf->data;
614296465Sdelphij    len =
615296465Sdelphij        9 + (unsigned long)s->s2->tmp.cipher_spec_length +
616296465Sdelphij        (unsigned long)s->s2->challenge_length +
617296465Sdelphij        (unsigned long)s->s2->tmp.session_id_length;
618296465Sdelphij    if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) {
619296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
620296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_MESSAGE_TOO_LONG);
621296465Sdelphij        return -1;
622296465Sdelphij    }
623296465Sdelphij    n = (int)len - s->init_num;
624296465Sdelphij    i = ssl2_read(s, (char *)&(p[s->init_num]), n);
625296465Sdelphij    if (i != n)
626296465Sdelphij        return (ssl2_part_read(s, SSL_F_GET_CLIENT_HELLO, i));
627296465Sdelphij    if (s->msg_callback) {
628296465Sdelphij        /* CLIENT-HELLO */
629296465Sdelphij        s->msg_callback(0, s->version, 0, p, (size_t)len, s,
630296465Sdelphij                        s->msg_callback_arg);
631296465Sdelphij    }
632296465Sdelphij    p += 9;
63355714Skris
634296465Sdelphij    /*
635296465Sdelphij     * get session-id before cipher stuff so we can get out session structure
636296465Sdelphij     * if it is cached
637296465Sdelphij     */
638296465Sdelphij    /* session-id */
639296465Sdelphij    if ((s->s2->tmp.session_id_length != 0) &&
640296465Sdelphij        (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH)) {
641296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
642296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_BAD_SSL_SESSION_ID_LENGTH);
643296465Sdelphij        return (-1);
644296465Sdelphij    }
64555714Skris
646296465Sdelphij    if (s->s2->tmp.session_id_length == 0) {
647296465Sdelphij        if (!ssl_get_new_session(s, 1)) {
648296465Sdelphij            ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
649296465Sdelphij            return (-1);
650296465Sdelphij        }
651296465Sdelphij    } else {
652296465Sdelphij        i = ssl_get_prev_session(s, &(p[s->s2->tmp.cipher_spec_length]),
653296465Sdelphij                                 s->s2->tmp.session_id_length, NULL);
654296465Sdelphij        if (i == 1) {           /* previous session */
655296465Sdelphij            s->hit = 1;
656296465Sdelphij        } else if (i == -1) {
657296465Sdelphij            ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
658296465Sdelphij            return (-1);
659296465Sdelphij        } else {
660296465Sdelphij            if (s->cert == NULL) {
661296465Sdelphij                ssl2_return_error(s, SSL2_PE_NO_CERTIFICATE);
662296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_NO_CERTIFICATE_SET);
663296465Sdelphij                return (-1);
664296465Sdelphij            }
66555714Skris
666296465Sdelphij            if (!ssl_get_new_session(s, 1)) {
667296465Sdelphij                ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
668296465Sdelphij                return (-1);
669296465Sdelphij            }
670296465Sdelphij        }
671296465Sdelphij    }
67255714Skris
673296465Sdelphij    if (!s->hit) {
674296465Sdelphij        cs = ssl_bytes_to_cipher_list(s, p, s->s2->tmp.cipher_spec_length,
675296465Sdelphij                                      &s->session->ciphers);
676296465Sdelphij        if (cs == NULL)
677296465Sdelphij            goto mem_err;
67855714Skris
679296465Sdelphij        cl = SSL_get_ciphers(s);
680295061Sdelphij
681296465Sdelphij        if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
682296465Sdelphij            prio = sk_SSL_CIPHER_dup(cl);
683296465Sdelphij            if (prio == NULL)
684296465Sdelphij                goto mem_err;
685296465Sdelphij            allow = cs;
686296465Sdelphij        } else {
687296465Sdelphij            prio = cs;
688296465Sdelphij            allow = cl;
689296465Sdelphij        }
69055714Skris
691296465Sdelphij        /* Generate list of SSLv2 ciphers shared between client and server */
692296465Sdelphij        for (z = 0; z < sk_SSL_CIPHER_num(prio); z++) {
693296465Sdelphij            const SSL_CIPHER *cp = sk_SSL_CIPHER_value(prio, z);
694296465Sdelphij            if ((cp->algorithms & SSL_SSLV2) == 0 ||
695296465Sdelphij                sk_SSL_CIPHER_find(allow, cp) < 0) {
696296465Sdelphij                (void)sk_SSL_CIPHER_delete(prio, z);
697296465Sdelphij                z--;
698296465Sdelphij            }
699296465Sdelphij        }
700296465Sdelphij        if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
701296465Sdelphij            sk_SSL_CIPHER_free(s->session->ciphers);
702296465Sdelphij            s->session->ciphers = prio;
703296465Sdelphij        }
70455714Skris
705296465Sdelphij        /* Make sure we have at least one cipher in common */
706296465Sdelphij        if (sk_SSL_CIPHER_num(s->session->ciphers) == 0) {
707296465Sdelphij            ssl2_return_error(s, SSL2_PE_NO_CIPHER);
708296465Sdelphij            SSLerr(SSL_F_GET_CLIENT_HELLO, SSL_R_NO_CIPHER_MATCH);
709296465Sdelphij            return -1;
710296465Sdelphij        }
711296465Sdelphij        /*
712296465Sdelphij         * s->session->ciphers should now have a list of ciphers that are on
713296465Sdelphij         * both the client and server. This list is ordered by the order the
714296465Sdelphij         * client sent the ciphers or in the order of the server's preference
715296465Sdelphij         * if SSL_OP_CIPHER_SERVER_PREFERENCE was set.
716296465Sdelphij         */
717296465Sdelphij    }
718296465Sdelphij    p += s->s2->tmp.cipher_spec_length;
719296465Sdelphij    /* done cipher selection */
72055714Skris
721296465Sdelphij    /* session id extracted already */
722296465Sdelphij    p += s->s2->tmp.session_id_length;
723296465Sdelphij
724296465Sdelphij    /* challenge */
725296465Sdelphij    if (s->s2->challenge_length > sizeof s->s2->challenge) {
726296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
727296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
728296465Sdelphij        return -1;
729296465Sdelphij    }
730296465Sdelphij    memcpy(s->s2->challenge, p, (unsigned int)s->s2->challenge_length);
731296465Sdelphij    return (1);
732296465Sdelphij mem_err:
733296465Sdelphij    SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_MALLOC_FAILURE);
734296465Sdelphij    return (0);
735296465Sdelphij}
736296465Sdelphij
73755714Skrisstatic int server_hello(SSL *s)
738296465Sdelphij{
739296465Sdelphij    unsigned char *p, *d;
740296465Sdelphij    int n, hit;
74155714Skris
742296465Sdelphij    p = (unsigned char *)s->init_buf->data;
743296465Sdelphij    if (s->state == SSL2_ST_SEND_SERVER_HELLO_A) {
744296465Sdelphij        d = p + 11;
745296465Sdelphij        *(p++) = SSL2_MT_SERVER_HELLO; /* type */
746296465Sdelphij        hit = s->hit;
747296465Sdelphij        *(p++) = (unsigned char)hit;
748296465Sdelphij# if 1
749296465Sdelphij        if (!hit) {
750296465Sdelphij            if (s->session->sess_cert != NULL)
751296465Sdelphij                /*
752296465Sdelphij                 * This can't really happen because get_client_hello has
753296465Sdelphij                 * called ssl_get_new_session, which does not set sess_cert.
754296465Sdelphij                 */
755296465Sdelphij                ssl_sess_cert_free(s->session->sess_cert);
756296465Sdelphij            s->session->sess_cert = ssl_sess_cert_new();
757296465Sdelphij            if (s->session->sess_cert == NULL) {
758296465Sdelphij                SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
759296465Sdelphij                return (-1);
760296465Sdelphij            }
761296465Sdelphij        }
762296465Sdelphij        /*
763296465Sdelphij         * If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
764296465Sdelphij         * depending on whether it survived in the internal cache or was
765296465Sdelphij         * retrieved from an external cache. If it is NULL, we cannot put any
766296465Sdelphij         * useful data in it anyway, so we don't touch it.
767296465Sdelphij         */
76855714Skris
769296465Sdelphij# else                          /* That's what used to be done when cert_st
770296465Sdelphij                                 * and sess_cert_st were * the same. */
771296465Sdelphij        if (!hit) {             /* else add cert to session */
772296465Sdelphij            CRYPTO_add(&s->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
773296465Sdelphij            if (s->session->sess_cert != NULL)
774296465Sdelphij                ssl_cert_free(s->session->sess_cert);
775296465Sdelphij            s->session->sess_cert = s->cert;
776296465Sdelphij        } else {                /* We have a session id-cache hit, if the *
777296465Sdelphij                                 * session-id has no certificate listed
778296465Sdelphij                                 * against * the 'cert' structure, grab the
779296465Sdelphij                                 * 'old' one * listed against the SSL
780296465Sdelphij                                 * connection */
781296465Sdelphij            if (s->session->sess_cert == NULL) {
782296465Sdelphij                CRYPTO_add(&s->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
783296465Sdelphij                s->session->sess_cert = s->cert;
784296465Sdelphij            }
785296465Sdelphij        }
786296465Sdelphij# endif
78755714Skris
788296465Sdelphij        if (s->cert == NULL) {
789296465Sdelphij            ssl2_return_error(s, SSL2_PE_NO_CERTIFICATE);
790296465Sdelphij            SSLerr(SSL_F_SERVER_HELLO, SSL_R_NO_CERTIFICATE_SPECIFIED);
791296465Sdelphij            return (-1);
792296465Sdelphij        }
79355714Skris
794296465Sdelphij        if (hit) {
795296465Sdelphij            *(p++) = 0;         /* no certificate type */
796296465Sdelphij            s2n(s->version, p); /* version */
797296465Sdelphij            s2n(0, p);          /* cert len */
798296465Sdelphij            s2n(0, p);          /* ciphers len */
799296465Sdelphij        } else {
800296465Sdelphij            /* EAY EAY */
801296465Sdelphij            /* put certificate type */
802296465Sdelphij            *(p++) = SSL2_CT_X509_CERTIFICATE;
803296465Sdelphij            s2n(s->version, p); /* version */
804296465Sdelphij            n = i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509, NULL);
805296465Sdelphij            s2n(n, p);          /* certificate length */
806296465Sdelphij            i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509, &d);
807296465Sdelphij            n = 0;
80855714Skris
809296465Sdelphij            /*
810296465Sdelphij             * lets send out the ciphers we like in the prefered order
811296465Sdelphij             */
812296465Sdelphij            n = ssl_cipher_list_to_bytes(s, s->session->ciphers, d, 0);
813296465Sdelphij            d += n;
814296465Sdelphij            s2n(n, p);          /* add cipher length */
815296465Sdelphij        }
81655714Skris
817296465Sdelphij        /* make and send conn_id */
818296465Sdelphij        s2n(SSL2_CONNECTION_ID_LENGTH, p); /* add conn_id length */
819296465Sdelphij        s->s2->conn_id_length = SSL2_CONNECTION_ID_LENGTH;
820296465Sdelphij        if (RAND_pseudo_bytes(s->s2->conn_id, (int)s->s2->conn_id_length) <=
821296465Sdelphij            0)
822296465Sdelphij            return -1;
823296465Sdelphij        memcpy(d, s->s2->conn_id, SSL2_CONNECTION_ID_LENGTH);
824296465Sdelphij        d += SSL2_CONNECTION_ID_LENGTH;
82555714Skris
826296465Sdelphij        s->state = SSL2_ST_SEND_SERVER_HELLO_B;
827296465Sdelphij        s->init_num = d - (unsigned char *)s->init_buf->data;
828296465Sdelphij        s->init_off = 0;
829296465Sdelphij    }
830296465Sdelphij    /* SSL2_ST_SEND_SERVER_HELLO_B */
831296465Sdelphij    /*
832296465Sdelphij     * If we are using TCP/IP, the performance is bad if we do 2 writes
833296465Sdelphij     * without a read between them.  This occurs when Session-id reuse is
834296465Sdelphij     * used, so I will put in a buffering module
835296465Sdelphij     */
836296465Sdelphij    if (s->hit) {
837296465Sdelphij        if (!ssl_init_wbio_buffer(s, 1))
838296465Sdelphij            return (-1);
839296465Sdelphij    }
840296465Sdelphij
841296465Sdelphij    return (ssl2_do_write(s));
842296465Sdelphij}
843296465Sdelphij
84455714Skrisstatic int get_client_finished(SSL *s)
845296465Sdelphij{
846296465Sdelphij    unsigned char *p;
847296465Sdelphij    int i, n;
848296465Sdelphij    unsigned long len;
84955714Skris
850296465Sdelphij    p = (unsigned char *)s->init_buf->data;
851296465Sdelphij    if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A) {
852296465Sdelphij        i = ssl2_read(s, (char *)&(p[s->init_num]), 1 - s->init_num);
853296465Sdelphij        if (i < 1 - s->init_num)
854296465Sdelphij            return (ssl2_part_read(s, SSL_F_GET_CLIENT_FINISHED, i));
855296465Sdelphij        s->init_num += i;
85655714Skris
857296465Sdelphij        if (*p != SSL2_MT_CLIENT_FINISHED) {
858296465Sdelphij            if (*p != SSL2_MT_ERROR) {
859296465Sdelphij                ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
860296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_FINISHED,
861296465Sdelphij                       SSL_R_READ_WRONG_PACKET_TYPE);
862296465Sdelphij            } else {
863296465Sdelphij                SSLerr(SSL_F_GET_CLIENT_FINISHED, SSL_R_PEER_ERROR);
864296465Sdelphij                /* try to read the error message */
865296465Sdelphij                i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num);
866296465Sdelphij                return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i);
867296465Sdelphij            }
868296465Sdelphij            return (-1);
869296465Sdelphij        }
870296465Sdelphij        s->state = SSL2_ST_GET_CLIENT_FINISHED_B;
871296465Sdelphij    }
87255714Skris
873296465Sdelphij    /* SSL2_ST_GET_CLIENT_FINISHED_B */
874296465Sdelphij    if (s->s2->conn_id_length > sizeof s->s2->conn_id) {
875296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
876296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
877296465Sdelphij        return -1;
878296465Sdelphij    }
879296465Sdelphij    len = 1 + (unsigned long)s->s2->conn_id_length;
880296465Sdelphij    n = (int)len - s->init_num;
881296465Sdelphij    i = ssl2_read(s, (char *)&(p[s->init_num]), n);
882296465Sdelphij    if (i < n) {
883296465Sdelphij        return (ssl2_part_read(s, SSL_F_GET_CLIENT_FINISHED, i));
884296465Sdelphij    }
885296465Sdelphij    if (s->msg_callback) {
886296465Sdelphij        /* CLIENT-FINISHED */
887296465Sdelphij        s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg);
888296465Sdelphij    }
889296465Sdelphij    p += 1;
890296465Sdelphij    if (memcmp(p, s->s2->conn_id, s->s2->conn_id_length) != 0) {
891296465Sdelphij        ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
892296465Sdelphij        SSLerr(SSL_F_GET_CLIENT_FINISHED, SSL_R_CONNECTION_ID_IS_DIFFERENT);
893296465Sdelphij        return (-1);
894296465Sdelphij    }
895296465Sdelphij    return (1);
896296465Sdelphij}
89755714Skris
89855714Skrisstatic int server_verify(SSL *s)
899296465Sdelphij{
900296465Sdelphij    unsigned char *p;
90155714Skris
902296465Sdelphij    if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A) {
903296465Sdelphij        p = (unsigned char *)s->init_buf->data;
904296465Sdelphij        *(p++) = SSL2_MT_SERVER_VERIFY;
905296465Sdelphij        if (s->s2->challenge_length > sizeof s->s2->challenge) {
906296465Sdelphij            SSLerr(SSL_F_SERVER_VERIFY, ERR_R_INTERNAL_ERROR);
907296465Sdelphij            return -1;
908296465Sdelphij        }
909296465Sdelphij        memcpy(p, s->s2->challenge, (unsigned int)s->s2->challenge_length);
910296465Sdelphij        /* p+=s->s2->challenge_length; */
91155714Skris
912296465Sdelphij        s->state = SSL2_ST_SEND_SERVER_VERIFY_B;
913296465Sdelphij        s->init_num = s->s2->challenge_length + 1;
914296465Sdelphij        s->init_off = 0;
915296465Sdelphij    }
916296465Sdelphij    return (ssl2_do_write(s));
917296465Sdelphij}
91855714Skris
91955714Skrisstatic int server_finish(SSL *s)
920296465Sdelphij{
921296465Sdelphij    unsigned char *p;
92255714Skris
923296465Sdelphij    if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A) {
924296465Sdelphij        p = (unsigned char *)s->init_buf->data;
925296465Sdelphij        *(p++) = SSL2_MT_SERVER_FINISHED;
92655714Skris
927296465Sdelphij        if (s->session->session_id_length > sizeof s->session->session_id) {
928296465Sdelphij            SSLerr(SSL_F_SERVER_FINISH, ERR_R_INTERNAL_ERROR);
929296465Sdelphij            return -1;
930296465Sdelphij        }
931296465Sdelphij        memcpy(p, s->session->session_id,
932296465Sdelphij               (unsigned int)s->session->session_id_length);
933296465Sdelphij        /* p+=s->session->session_id_length; */
93455714Skris
935296465Sdelphij        s->state = SSL2_ST_SEND_SERVER_FINISHED_B;
936296465Sdelphij        s->init_num = s->session->session_id_length + 1;
937296465Sdelphij        s->init_off = 0;
938296465Sdelphij    }
93955714Skris
940296465Sdelphij    /* SSL2_ST_SEND_SERVER_FINISHED_B */
941296465Sdelphij    return (ssl2_do_write(s));
942296465Sdelphij}
94355714Skris
94455714Skris/* send the request and check the response */
94555714Skrisstatic int request_certificate(SSL *s)
946296465Sdelphij{
947296465Sdelphij    const unsigned char *cp;
948296465Sdelphij    unsigned char *p, *p2, *buf2;
949296465Sdelphij    unsigned char *ccd;
950296465Sdelphij    int i, j, ctype, ret = -1;
951296465Sdelphij    unsigned long len;
952296465Sdelphij    X509 *x509 = NULL;
953296465Sdelphij    STACK_OF(X509) *sk = NULL;
95455714Skris
955296465Sdelphij    ccd = s->s2->tmp.ccl;
956296465Sdelphij    if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A) {
957296465Sdelphij        p = (unsigned char *)s->init_buf->data;
958296465Sdelphij        *(p++) = SSL2_MT_REQUEST_CERTIFICATE;
959296465Sdelphij        *(p++) = SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
960296465Sdelphij        if (RAND_pseudo_bytes(ccd, SSL2_MIN_CERT_CHALLENGE_LENGTH) <= 0)
961296465Sdelphij            return -1;
962296465Sdelphij        memcpy(p, ccd, SSL2_MIN_CERT_CHALLENGE_LENGTH);
96355714Skris
964296465Sdelphij        s->state = SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
965296465Sdelphij        s->init_num = SSL2_MIN_CERT_CHALLENGE_LENGTH + 2;
966296465Sdelphij        s->init_off = 0;
967296465Sdelphij    }
96855714Skris
969296465Sdelphij    if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B) {
970296465Sdelphij        i = ssl2_do_write(s);
971296465Sdelphij        if (i <= 0) {
972296465Sdelphij            ret = i;
973296465Sdelphij            goto end;
974296465Sdelphij        }
97555714Skris
976296465Sdelphij        s->init_num = 0;
977296465Sdelphij        s->state = SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
978296465Sdelphij    }
97955714Skris
980296465Sdelphij    if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C) {
981296465Sdelphij        p = (unsigned char *)s->init_buf->data;
982296465Sdelphij        /* try to read 6 octets ... */
983296465Sdelphij        i = ssl2_read(s, (char *)&(p[s->init_num]), 6 - s->init_num);
984296465Sdelphij        /*
985296465Sdelphij         * ... but don't call ssl2_part_read now if we got at least 3
986296465Sdelphij         * (probably NO-CERTIFICATE-ERROR)
987296465Sdelphij         */
988296465Sdelphij        if (i < 3 - s->init_num) {
989296465Sdelphij            ret = ssl2_part_read(s, SSL_F_REQUEST_CERTIFICATE, i);
990296465Sdelphij            goto end;
991296465Sdelphij        }
992296465Sdelphij        s->init_num += i;
99355714Skris
994296465Sdelphij        if ((s->init_num >= 3) && (p[0] == SSL2_MT_ERROR)) {
995296465Sdelphij            n2s(p, i);
996296465Sdelphij            if (i != SSL2_PE_NO_CERTIFICATE) {
997296465Sdelphij                /*
998296465Sdelphij                 * not the error message we expected -- let ssl2_part_read
999296465Sdelphij                 * handle it
1000296465Sdelphij                 */
1001296465Sdelphij                s->init_num -= 3;
1002296465Sdelphij                ret = ssl2_part_read(s, SSL_F_REQUEST_CERTIFICATE, 3);
1003296465Sdelphij                goto end;
1004296465Sdelphij            }
100589840Skris
1006296465Sdelphij            if (s->msg_callback) {
1007296465Sdelphij                /* ERROR */
1008296465Sdelphij                s->msg_callback(0, s->version, 0, p, 3, s,
1009296465Sdelphij                                s->msg_callback_arg);
1010296465Sdelphij            }
1011110007Smarkm
1012296465Sdelphij            /*
1013296465Sdelphij             * this is the one place where we can recover from an SSL 2.0
1014296465Sdelphij             * error
1015296465Sdelphij             */
101689840Skris
1017296465Sdelphij            if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
1018296465Sdelphij                ssl2_return_error(s, SSL2_PE_BAD_CERTIFICATE);
1019296465Sdelphij                SSLerr(SSL_F_REQUEST_CERTIFICATE,
1020296465Sdelphij                       SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1021296465Sdelphij                goto end;
1022296465Sdelphij            }
1023296465Sdelphij            ret = 1;
1024296465Sdelphij            goto end;
1025296465Sdelphij        }
1026296465Sdelphij        if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (s->init_num < 6)) {
1027296465Sdelphij            ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
1028296465Sdelphij            SSLerr(SSL_F_REQUEST_CERTIFICATE, SSL_R_SHORT_READ);
1029296465Sdelphij            goto end;
1030296465Sdelphij        }
1031296465Sdelphij        if (s->init_num != 6) {
1032296465Sdelphij            SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1033296465Sdelphij            goto end;
1034296465Sdelphij        }
103555714Skris
1036296465Sdelphij        /* ok we have a response */
1037296465Sdelphij        /* certificate type, there is only one right now. */
1038296465Sdelphij        ctype = *(p++);
1039296465Sdelphij        if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) {
1040296465Sdelphij            ssl2_return_error(s, SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
1041296465Sdelphij            SSLerr(SSL_F_REQUEST_CERTIFICATE, SSL_R_BAD_RESPONSE_ARGUMENT);
1042296465Sdelphij            goto end;
1043296465Sdelphij        }
1044296465Sdelphij        n2s(p, i);
1045296465Sdelphij        s->s2->tmp.clen = i;
1046296465Sdelphij        n2s(p, i);
1047296465Sdelphij        s->s2->tmp.rlen = i;
1048296465Sdelphij        s->state = SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
1049296465Sdelphij    }
105055714Skris
1051296465Sdelphij    /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
1052296465Sdelphij    p = (unsigned char *)s->init_buf->data;
1053296465Sdelphij    len = 6 + (unsigned long)s->s2->tmp.clen + (unsigned long)s->s2->tmp.rlen;
1054296465Sdelphij    if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) {
1055296465Sdelphij        SSLerr(SSL_F_REQUEST_CERTIFICATE, SSL_R_MESSAGE_TOO_LONG);
1056296465Sdelphij        goto end;
1057296465Sdelphij    }
1058296465Sdelphij    j = (int)len - s->init_num;
1059296465Sdelphij    i = ssl2_read(s, (char *)&(p[s->init_num]), j);
1060296465Sdelphij    if (i < j) {
1061296465Sdelphij        ret = ssl2_part_read(s, SSL_F_REQUEST_CERTIFICATE, i);
1062296465Sdelphij        goto end;
1063296465Sdelphij    }
1064296465Sdelphij    if (s->msg_callback) {
1065296465Sdelphij        /* CLIENT-CERTIFICATE */
1066296465Sdelphij        s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg);
1067296465Sdelphij    }
1068296465Sdelphij    p += 6;
106955714Skris
1070296465Sdelphij    cp = p;
1071296465Sdelphij    x509 = (X509 *)d2i_X509(NULL, &cp, (long)s->s2->tmp.clen);
1072296465Sdelphij    if (x509 == NULL) {
1073296465Sdelphij        SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_X509_LIB);
1074296465Sdelphij        goto msg_end;
1075296465Sdelphij    }
107655714Skris
1077296465Sdelphij    if (((sk = sk_X509_new_null()) == NULL) || (!sk_X509_push(sk, x509))) {
1078296465Sdelphij        SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1079296465Sdelphij        goto msg_end;
1080296465Sdelphij    }
108155714Skris
1082296465Sdelphij    i = ssl_verify_cert_chain(s, sk);
108355714Skris
1084296465Sdelphij    if (i > 0) {                /* we like the packet, now check the chksum */
1085296465Sdelphij        EVP_MD_CTX ctx;
1086296465Sdelphij        EVP_PKEY *pkey = NULL;
108755714Skris
1088296465Sdelphij        EVP_MD_CTX_init(&ctx);
1089296465Sdelphij        EVP_VerifyInit_ex(&ctx, s->ctx->rsa_md5, NULL);
1090296465Sdelphij        EVP_VerifyUpdate(&ctx, s->s2->key_material,
1091296465Sdelphij                         s->s2->key_material_length);
1092296465Sdelphij        EVP_VerifyUpdate(&ctx, ccd, SSL2_MIN_CERT_CHALLENGE_LENGTH);
109355714Skris
1094296465Sdelphij        i = i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509, NULL);
1095296465Sdelphij        buf2 = OPENSSL_malloc((unsigned int)i);
1096296465Sdelphij        if (buf2 == NULL) {
1097296465Sdelphij            SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1098296465Sdelphij            goto msg_end;
1099296465Sdelphij        }
1100296465Sdelphij        p2 = buf2;
1101296465Sdelphij        i = i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509, &p2);
1102296465Sdelphij        EVP_VerifyUpdate(&ctx, buf2, (unsigned int)i);
1103296465Sdelphij        OPENSSL_free(buf2);
110455714Skris
1105296465Sdelphij        pkey = X509_get_pubkey(x509);
1106296465Sdelphij        if (pkey == NULL)
1107296465Sdelphij            goto end;
1108296465Sdelphij        i = EVP_VerifyFinal(&ctx, cp, s->s2->tmp.rlen, pkey);
1109296465Sdelphij        EVP_PKEY_free(pkey);
1110296465Sdelphij        EVP_MD_CTX_cleanup(&ctx);
111155714Skris
1112296465Sdelphij        if (i > 0) {
1113296465Sdelphij            if (s->session->peer != NULL)
1114296465Sdelphij                X509_free(s->session->peer);
1115296465Sdelphij            s->session->peer = x509;
1116296465Sdelphij            CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
1117296465Sdelphij            s->session->verify_result = s->verify_result;
1118296465Sdelphij            ret = 1;
1119296465Sdelphij            goto end;
1120296465Sdelphij        } else {
1121296465Sdelphij            SSLerr(SSL_F_REQUEST_CERTIFICATE, SSL_R_BAD_CHECKSUM);
1122296465Sdelphij            goto msg_end;
1123296465Sdelphij        }
1124296465Sdelphij    } else {
1125296465Sdelphij msg_end:
1126296465Sdelphij        ssl2_return_error(s, SSL2_PE_BAD_CERTIFICATE);
1127296465Sdelphij    }
1128296465Sdelphij end:
1129296465Sdelphij    sk_X509_free(sk);
1130296465Sdelphij    X509_free(x509);
1131296465Sdelphij    return (ret);
1132296465Sdelphij}
1133296465Sdelphij
113455714Skrisstatic int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
1135296465Sdelphij                                   unsigned char *to, int padding)
1136296465Sdelphij{
1137296465Sdelphij    RSA *rsa;
1138296465Sdelphij    int i;
113955714Skris
1140296465Sdelphij    if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)) {
1141296465Sdelphij        SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT, SSL_R_NO_PRIVATEKEY);
1142296465Sdelphij        return (-1);
1143296465Sdelphij    }
1144296465Sdelphij    if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA) {
1145296465Sdelphij        SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT, SSL_R_PUBLIC_KEY_IS_NOT_RSA);
1146296465Sdelphij        return (-1);
1147296465Sdelphij    }
1148296465Sdelphij    rsa = c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
114955714Skris
1150296465Sdelphij    /* we have the public key */
1151296465Sdelphij    i = RSA_private_decrypt(len, from, to, rsa, padding);
1152296465Sdelphij    if (i < 0)
1153296465Sdelphij        SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT, ERR_R_RSA_LIB);
1154296465Sdelphij    return (i);
1155296465Sdelphij}
1156296465Sdelphij#else                           /* !OPENSSL_NO_SSL2 */
115759194Skris
115859194Skris# if PEDANTIC
1159296465Sdelphijstatic void *dummy = &dummy;
116059194Skris# endif
116159194Skris
116255714Skris#endif
1163