LCOV - code coverage report
Current view: top level - third_party/openssl/ssl - s2_clnt.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 0 479 0.0 %
Date: 2015-10-10 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /* ssl/s2_clnt.c */
       2             : /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
       3             :  * All rights reserved.
       4             :  *
       5             :  * This package is an SSL implementation written
       6             :  * by Eric Young (eay@cryptsoft.com).
       7             :  * The implementation was written so as to conform with Netscapes SSL.
       8             :  *
       9             :  * This library is free for commercial and non-commercial use as long as
      10             :  * the following conditions are aheared to.  The following conditions
      11             :  * apply to all code found in this distribution, be it the RC4, RSA,
      12             :  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
      13             :  * included with this distribution is covered by the same copyright terms
      14             :  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
      15             :  *
      16             :  * Copyright remains Eric Young's, and as such any Copyright notices in
      17             :  * the code are not to be removed.
      18             :  * If this package is used in a product, Eric Young should be given attribution
      19             :  * as the author of the parts of the library used.
      20             :  * This can be in the form of a textual message at program startup or
      21             :  * in documentation (online or textual) provided with the package.
      22             :  *
      23             :  * Redistribution and use in source and binary forms, with or without
      24             :  * modification, are permitted provided that the following conditions
      25             :  * are met:
      26             :  * 1. Redistributions of source code must retain the copyright
      27             :  *    notice, this list of conditions and the following disclaimer.
      28             :  * 2. Redistributions in binary form must reproduce the above copyright
      29             :  *    notice, this list of conditions and the following disclaimer in the
      30             :  *    documentation and/or other materials provided with the distribution.
      31             :  * 3. All advertising materials mentioning features or use of this software
      32             :  *    must display the following acknowledgement:
      33             :  *    "This product includes cryptographic software written by
      34             :  *     Eric Young (eay@cryptsoft.com)"
      35             :  *    The word 'cryptographic' can be left out if the rouines from the library
      36             :  *    being used are not cryptographic related :-).
      37             :  * 4. If you include any Windows specific code (or a derivative thereof) from
      38             :  *    the apps directory (application code) you must include an acknowledgement:
      39             :  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
      40             :  *
      41             :  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
      42             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      43             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      44             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      45             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      46             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      47             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      48             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      49             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      50             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      51             :  * SUCH DAMAGE.
      52             :  *
      53             :  * The licence and distribution terms for any publically available version or
      54             :  * derivative of this code cannot be changed.  i.e. this code cannot simply be
      55             :  * copied and put under another distribution licence
      56             :  * [including the GNU Public Licence.]
      57             :  */
      58             : /* ====================================================================
      59             :  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
      60             :  *
      61             :  * Redistribution and use in source and binary forms, with or without
      62             :  * modification, are permitted provided that the following conditions
      63             :  * are met:
      64             :  *
      65             :  * 1. Redistributions of source code must retain the above copyright
      66             :  *    notice, this list of conditions and the following disclaimer.
      67             :  *
      68             :  * 2. Redistributions in binary form must reproduce the above copyright
      69             :  *    notice, this list of conditions and the following disclaimer in
      70             :  *    the documentation and/or other materials provided with the
      71             :  *    distribution.
      72             :  *
      73             :  * 3. All advertising materials mentioning features or use of this
      74             :  *    software must display the following acknowledgment:
      75             :  *    "This product includes software developed by the OpenSSL Project
      76             :  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
      77             :  *
      78             :  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
      79             :  *    endorse or promote products derived from this software without
      80             :  *    prior written permission. For written permission, please contact
      81             :  *    openssl-core@openssl.org.
      82             :  *
      83             :  * 5. Products derived from this software may not be called "OpenSSL"
      84             :  *    nor may "OpenSSL" appear in their names without prior written
      85             :  *    permission of the OpenSSL Project.
      86             :  *
      87             :  * 6. Redistributions of any form whatsoever must retain the following
      88             :  *    acknowledgment:
      89             :  *    "This product includes software developed by the OpenSSL Project
      90             :  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
      91             :  *
      92             :  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
      93             :  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      94             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
      95             :  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
      96             :  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      97             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      98             :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      99             :  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     100             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     101             :  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     102             :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     103             :  * OF THE POSSIBILITY OF SUCH DAMAGE.
     104             :  * ====================================================================
     105             :  *
     106             :  * This product includes cryptographic software written by Eric Young
     107             :  * (eay@cryptsoft.com).  This product includes software written by Tim
     108             :  * Hudson (tjh@cryptsoft.com).
     109             :  *
     110             :  */
     111             : 
     112             : #include "ssl_locl.h"
     113             : #ifndef OPENSSL_NO_SSL2
     114             : # include <stdio.h>
     115             : # include <openssl/rand.h>
     116             : # include <openssl/buffer.h>
     117             : # include <openssl/objects.h>
     118             : # include <openssl/evp.h>
     119             : 
     120             : static const SSL_METHOD *ssl2_get_client_method(int ver);
     121             : static int get_server_finished(SSL *s);
     122             : static int get_server_verify(SSL *s);
     123             : static int get_server_hello(SSL *s);
     124             : static int client_hello(SSL *s);
     125             : static int client_master_key(SSL *s);
     126             : static int client_finished(SSL *s);
     127             : static int client_certificate(SSL *s);
     128             : static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
     129             :                                   unsigned char *to, int padding);
     130             : # define BREAK   break
     131             : 
     132           0 : static const SSL_METHOD *ssl2_get_client_method(int ver)
     133             : {
     134           0 :     if (ver == SSL2_VERSION)
     135             :         return (SSLv2_client_method());
     136             :     else
     137           0 :         return (NULL);
     138             : }
     139             : 
     140           0 : IMPLEMENT_ssl2_meth_func(SSLv2_client_method,
     141             :                          ssl_undefined_function,
     142             :                          ssl2_connect, ssl2_get_client_method)
     143             : 
     144           0 : int ssl2_connect(SSL *s)
     145             : {
     146           0 :     unsigned long l = (unsigned long)time(NULL);
     147             :     BUF_MEM *buf = NULL;
     148             :     int ret = -1;
     149             :     void (*cb) (const SSL *ssl, int type, int val) = NULL;
     150             :     int new_state, state;
     151             : 
     152           0 :     RAND_add(&l, sizeof(l), 0);
     153           0 :     ERR_clear_error();
     154           0 :     clear_sys_error();
     155             : 
     156           0 :     if (s->info_callback != NULL)
     157             :         cb = s->info_callback;
     158           0 :     else if (s->ctx->info_callback != NULL)
     159             :         cb = s->ctx->info_callback;
     160             : 
     161             :     /* init things to blank */
     162           0 :     s->in_handshake++;
     163           0 :     if (!SSL_in_init(s) || SSL_in_before(s))
     164           0 :         SSL_clear(s);
     165             : 
     166             :     for (;;) {
     167           0 :         state = s->state;
     168             : 
     169           0 :         switch (s->state) {
     170             :         case SSL_ST_BEFORE:
     171             :         case SSL_ST_CONNECT:
     172             :         case SSL_ST_BEFORE | SSL_ST_CONNECT:
     173             :         case SSL_ST_OK | SSL_ST_CONNECT:
     174             : 
     175           0 :             s->server = 0;
     176           0 :             if (cb != NULL)
     177           0 :                 cb(s, SSL_CB_HANDSHAKE_START, 1);
     178             : 
     179           0 :             s->version = SSL2_VERSION;
     180           0 :             s->type = SSL_ST_CONNECT;
     181             : 
     182           0 :             buf = s->init_buf;
     183           0 :             if ((buf == NULL) && ((buf = BUF_MEM_new()) == NULL)) {
     184             :                 ret = -1;
     185             :                 goto end;
     186             :             }
     187           0 :             if (!BUF_MEM_grow(buf, SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) {
     188           0 :                 if (buf == s->init_buf)
     189             :                     buf = NULL;
     190             :                 ret = -1;
     191             :                 goto end;
     192             :             }
     193           0 :             s->init_buf = buf;
     194             :             buf = NULL;
     195           0 :             s->init_num = 0;
     196           0 :             s->state = SSL2_ST_SEND_CLIENT_HELLO_A;
     197           0 :             s->ctx->stats.sess_connect++;
     198           0 :             s->handshake_func = ssl2_connect;
     199           0 :             BREAK;
     200             : 
     201             :         case SSL2_ST_SEND_CLIENT_HELLO_A:
     202             :         case SSL2_ST_SEND_CLIENT_HELLO_B:
     203           0 :             s->shutdown = 0;
     204           0 :             ret = client_hello(s);
     205           0 :             if (ret <= 0)
     206             :                 goto end;
     207           0 :             s->init_num = 0;
     208           0 :             s->state = SSL2_ST_GET_SERVER_HELLO_A;
     209           0 :             BREAK;
     210             : 
     211             :         case SSL2_ST_GET_SERVER_HELLO_A:
     212             :         case SSL2_ST_GET_SERVER_HELLO_B:
     213           0 :             ret = get_server_hello(s);
     214           0 :             if (ret <= 0)
     215             :                 goto end;
     216           0 :             s->init_num = 0;
     217           0 :             if (!s->hit) {      /* new session */
     218           0 :                 s->state = SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
     219           0 :                 BREAK;
     220             :             } else {
     221           0 :                 s->state = SSL2_ST_CLIENT_START_ENCRYPTION;
     222           0 :                 break;
     223             :             }
     224             : 
     225             :         case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
     226             :         case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
     227           0 :             ret = client_master_key(s);
     228           0 :             if (ret <= 0)
     229             :                 goto end;
     230           0 :             s->init_num = 0;
     231           0 :             s->state = SSL2_ST_CLIENT_START_ENCRYPTION;
     232           0 :             break;
     233             : 
     234             :         case SSL2_ST_CLIENT_START_ENCRYPTION:
     235             :             /*
     236             :              * Ok, we now have all the stuff needed to start encrypting, so
     237             :              * lets fire it up :-)
     238             :              */
     239           0 :             if (!ssl2_enc_init(s, 1)) {
     240             :                 ret = -1;
     241             :                 goto end;
     242             :             }
     243           0 :             s->s2->clear_text = 0;
     244           0 :             s->state = SSL2_ST_SEND_CLIENT_FINISHED_A;
     245           0 :             break;
     246             : 
     247             :         case SSL2_ST_SEND_CLIENT_FINISHED_A:
     248             :         case SSL2_ST_SEND_CLIENT_FINISHED_B:
     249           0 :             ret = client_finished(s);
     250           0 :             if (ret <= 0)
     251             :                 goto end;
     252           0 :             s->init_num = 0;
     253           0 :             s->state = SSL2_ST_GET_SERVER_VERIFY_A;
     254           0 :             break;
     255             : 
     256             :         case SSL2_ST_GET_SERVER_VERIFY_A:
     257             :         case SSL2_ST_GET_SERVER_VERIFY_B:
     258           0 :             ret = get_server_verify(s);
     259           0 :             if (ret <= 0)
     260             :                 goto end;
     261           0 :             s->init_num = 0;
     262           0 :             s->state = SSL2_ST_GET_SERVER_FINISHED_A;
     263           0 :             break;
     264             : 
     265             :         case SSL2_ST_GET_SERVER_FINISHED_A:
     266             :         case SSL2_ST_GET_SERVER_FINISHED_B:
     267           0 :             ret = get_server_finished(s);
     268           0 :             if (ret <= 0)
     269             :                 goto end;
     270             :             break;
     271             : 
     272             :         case SSL2_ST_SEND_CLIENT_CERTIFICATE_A:
     273             :         case SSL2_ST_SEND_CLIENT_CERTIFICATE_B:
     274             :         case SSL2_ST_SEND_CLIENT_CERTIFICATE_C:
     275             :         case SSL2_ST_SEND_CLIENT_CERTIFICATE_D:
     276             :         case SSL2_ST_X509_GET_CLIENT_CERTIFICATE:
     277           0 :             ret = client_certificate(s);
     278           0 :             if (ret <= 0)
     279             :                 goto end;
     280           0 :             s->init_num = 0;
     281           0 :             s->state = SSL2_ST_GET_SERVER_FINISHED_A;
     282           0 :             break;
     283             : 
     284             :         case SSL_ST_OK:
     285           0 :             if (s->init_buf != NULL) {
     286           0 :                 BUF_MEM_free(s->init_buf);
     287           0 :                 s->init_buf = NULL;
     288             :             }
     289           0 :             s->init_num = 0;
     290             :             /*      ERR_clear_error(); */
     291             : 
     292             :             /*
     293             :              * If we want to cache session-ids in the client and we
     294             :              * successfully add the session-id to the cache, and there is a
     295             :              * callback, then pass it out. 26/11/96 - eay - only add if not a
     296             :              * re-used session.
     297             :              */
     298             : 
     299           0 :             ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
     300           0 :             if (s->hit)
     301           0 :                 s->ctx->stats.sess_hit++;
     302             : 
     303             :             ret = 1;
     304             :             /* s->server=0; */
     305           0 :             s->ctx->stats.sess_connect_good++;
     306             : 
     307           0 :             if (cb != NULL)
     308           0 :                 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
     309             : 
     310             :             goto end;
     311             :             /* break; */
     312             :         default:
     313           0 :             SSLerr(SSL_F_SSL2_CONNECT, SSL_R_UNKNOWN_STATE);
     314           0 :             return (-1);
     315             :             /* break; */
     316             :         }
     317             : 
     318           0 :         if ((cb != NULL) && (s->state != state)) {
     319             :             new_state = s->state;
     320           0 :             s->state = state;
     321           0 :             cb(s, SSL_CB_CONNECT_LOOP, 1);
     322           0 :             s->state = new_state;
     323             :         }
     324             :     }
     325             :  end:
     326           0 :     s->in_handshake--;
     327           0 :     if (buf != NULL)
     328           0 :         BUF_MEM_free(buf);
     329           0 :     if (cb != NULL)
     330           0 :         cb(s, SSL_CB_CONNECT_EXIT, ret);
     331           0 :     return (ret);
     332             : }
     333             : 
     334           0 : static int get_server_hello(SSL *s)
     335             : {
     336             :     unsigned char *buf;
     337             :     unsigned char *p;
     338             :     int i, j;
     339             :     unsigned long len;
     340             :     STACK_OF(SSL_CIPHER) *sk = NULL, *cl, *prio, *allow;
     341             : 
     342           0 :     buf = (unsigned char *)s->init_buf->data;
     343             :     p = buf;
     344           0 :     if (s->state == SSL2_ST_GET_SERVER_HELLO_A) {
     345           0 :         i = ssl2_read(s, (char *)&(buf[s->init_num]), 11 - s->init_num);
     346           0 :         if (i < (11 - s->init_num))
     347           0 :             return (ssl2_part_read(s, SSL_F_GET_SERVER_HELLO, i));
     348           0 :         s->init_num = 11;
     349             : 
     350           0 :         if (*(p++) != SSL2_MT_SERVER_HELLO) {
     351           0 :             if (p[-1] != SSL2_MT_ERROR) {
     352           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     353           0 :                 SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_READ_WRONG_PACKET_TYPE);
     354             :             } else
     355           0 :                 SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_PEER_ERROR);
     356             :             return (-1);
     357             :         }
     358             : # if 0
     359             :         s->hit = (*(p++)) ? 1 : 0;
     360             :         /*
     361             :          * Some [PPC?] compilers fail to increment p in above statement, e.g.
     362             :          * one provided with Rhapsody 5.5, but most recent example XL C 11.1
     363             :          * for AIX, even without optimization flag...
     364             :          */
     365             : # else
     366           0 :         s->hit = (*p) ? 1 : 0;
     367             :         p++;
     368             : # endif
     369           0 :         s->s2->tmp.cert_type = *(p++);
     370           0 :         n2s(p, i);
     371           0 :         if (i < s->version)
     372           0 :             s->version = i;
     373           0 :         n2s(p, i);
     374           0 :         s->s2->tmp.cert_length = i;
     375           0 :         n2s(p, i);
     376           0 :         s->s2->tmp.csl = i;
     377           0 :         n2s(p, i);
     378           0 :         s->s2->tmp.conn_id_length = i;
     379           0 :         s->state = SSL2_ST_GET_SERVER_HELLO_B;
     380             :     }
     381             : 
     382             :     /* SSL2_ST_GET_SERVER_HELLO_B */
     383           0 :     len =
     384           0 :         11 + (unsigned long)s->s2->tmp.cert_length +
     385           0 :         (unsigned long)s->s2->tmp.csl +
     386           0 :         (unsigned long)s->s2->tmp.conn_id_length;
     387           0 :     if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) {
     388           0 :         SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_MESSAGE_TOO_LONG);
     389           0 :         return -1;
     390             :     }
     391           0 :     j = (int)len - s->init_num;
     392           0 :     i = ssl2_read(s, (char *)&(buf[s->init_num]), j);
     393           0 :     if (i != j)
     394           0 :         return (ssl2_part_read(s, SSL_F_GET_SERVER_HELLO, i));
     395           0 :     if (s->msg_callback) {
     396             :         /* SERVER-HELLO */
     397           0 :         s->msg_callback(0, s->version, 0, buf, (size_t)len, s,
     398             :                         s->msg_callback_arg);
     399             :     }
     400             : 
     401             :     /* things are looking good */
     402             : 
     403           0 :     p = buf + 11;
     404           0 :     if (s->hit) {
     405           0 :         if (s->s2->tmp.cert_length != 0) {
     406           0 :             SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
     407           0 :             return (-1);
     408             :         }
     409           0 :         if (s->s2->tmp.cert_type != 0) {
     410             :             if (!(s->options & SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG)) {
     411           0 :                 SSLerr(SSL_F_GET_SERVER_HELLO,
     412             :                        SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
     413           0 :                 return (-1);
     414             :             }
     415             :         }
     416           0 :         if (s->s2->tmp.csl != 0) {
     417           0 :             SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
     418           0 :             return (-1);
     419             :         }
     420             :     } else {
     421             : # if 0
     422             :         /* very bad */
     423             :         memset(s->session->session_id, 0,
     424             :                SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
     425             :         s->session->session_id_length = 0;
     426             : # endif
     427             : 
     428             :         /*
     429             :          * we need to do this in case we were trying to reuse a client
     430             :          * session but others are already reusing it. If this was a new
     431             :          * 'blank' session ID, the session-id length will still be 0
     432             :          */
     433           0 :         if (s->session->session_id_length > 0) {
     434           0 :             if (!ssl_get_new_session(s, 0)) {
     435           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     436           0 :                 return (-1);
     437             :             }
     438             :         }
     439             : 
     440           0 :         if (ssl2_set_certificate(s, s->s2->tmp.cert_type,
     441           0 :                                  s->s2->tmp.cert_length, p) <= 0) {
     442           0 :             ssl2_return_error(s, SSL2_PE_BAD_CERTIFICATE);
     443           0 :             return (-1);
     444             :         }
     445           0 :         p += s->s2->tmp.cert_length;
     446             : 
     447           0 :         if (s->s2->tmp.csl == 0) {
     448           0 :             ssl2_return_error(s, SSL2_PE_NO_CIPHER);
     449           0 :             SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_NO_CIPHER_LIST);
     450           0 :             return (-1);
     451             :         }
     452             : 
     453             :         /*
     454             :          * We have just received a list of ciphers back from the server.  We
     455             :          * need to get the ones that match, then select the one we want the
     456             :          * most :-).
     457             :          */
     458             : 
     459             :         /* load the ciphers */
     460           0 :         sk = ssl_bytes_to_cipher_list(s, p, s->s2->tmp.csl,
     461           0 :                                       &s->session->ciphers);
     462           0 :         p += s->s2->tmp.csl;
     463           0 :         if (sk == NULL) {
     464           0 :             ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     465           0 :             SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
     466           0 :             return (-1);
     467             :         }
     468             : 
     469           0 :         (void)sk_SSL_CIPHER_set_cmp_func(sk, ssl_cipher_ptr_id_cmp);
     470             : 
     471             :         /* get the array of ciphers we will accept */
     472           0 :         cl = SSL_get_ciphers(s);
     473           0 :         (void)sk_SSL_CIPHER_set_cmp_func(cl, ssl_cipher_ptr_id_cmp);
     474             : 
     475             :         /*
     476             :          * If server preference flag set, choose the first
     477             :          * (highest priority) cipher the server sends, otherwise
     478             :          * client preference has priority.
     479             :          */
     480           0 :         if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
     481             :             prio = sk;
     482             :             allow = cl;
     483             :         } else {
     484             :             prio = cl;
     485             :             allow = sk;
     486             :         }
     487             :         /*
     488             :          * In theory we could have ciphers sent back that we don't want to
     489             :          * use but that does not matter since we will check against the list
     490             :          * we originally sent and for performance reasons we should not
     491             :          * bother to match the two lists up just to check.
     492             :          */
     493           0 :         for (i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
     494           0 :             if (sk_SSL_CIPHER_find(allow, sk_SSL_CIPHER_value(prio, i)) >= 0)
     495             :                 break;
     496             :         }
     497             : 
     498           0 :         if (i >= sk_SSL_CIPHER_num(prio)) {
     499           0 :             ssl2_return_error(s, SSL2_PE_NO_CIPHER);
     500           0 :             SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_NO_CIPHER_MATCH);
     501           0 :             return (-1);
     502             :         }
     503           0 :         s->session->cipher = sk_SSL_CIPHER_value(prio, i);
     504             : 
     505           0 :         if (s->session->peer != NULL) { /* can't happen */
     506           0 :             ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     507           0 :             SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
     508           0 :             return (-1);
     509             :         }
     510             : 
     511           0 :         s->session->peer = s->session->sess_cert->peer_key->x509;
     512             :         /* peer_key->x509 has been set by ssl2_set_certificate. */
     513           0 :         CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
     514             :     }
     515             : 
     516           0 :     if (s->session->sess_cert == NULL
     517           0 :         || s->session->peer != s->session->sess_cert->peer_key->x509)
     518             :         /* can't happen */
     519             :     {
     520           0 :         ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     521           0 :         SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
     522           0 :         return (-1);
     523             :     }
     524             : 
     525           0 :     s->s2->conn_id_length = s->s2->tmp.conn_id_length;
     526           0 :     if (s->s2->conn_id_length > sizeof s->s2->conn_id) {
     527           0 :         ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     528           0 :         SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG);
     529           0 :         return -1;
     530             :     }
     531           0 :     memcpy(s->s2->conn_id, p, s->s2->tmp.conn_id_length);
     532           0 :     return (1);
     533             : }
     534             : 
     535           0 : static int client_hello(SSL *s)
     536             : {
     537             :     unsigned char *buf;
     538             :     unsigned char *p, *d;
     539             : /*      CIPHER **cipher;*/
     540             :     int i, n, j;
     541             : 
     542           0 :     buf = (unsigned char *)s->init_buf->data;
     543           0 :     if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A) {
     544           0 :         if ((s->session == NULL) || (s->session->ssl_version != s->version)) {
     545           0 :             if (!ssl_get_new_session(s, 0)) {
     546           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     547           0 :                 return (-1);
     548             :             }
     549             :         }
     550             :         /* else use the pre-loaded session */
     551             : 
     552             :         p = buf;                /* header */
     553           0 :         d = p + 9;              /* data section */
     554           0 :         *(p++) = SSL2_MT_CLIENT_HELLO; /* type */
     555           0 :         s2n(SSL2_VERSION, p);   /* version */
     556             :         n = j = 0;
     557             : 
     558           0 :         n = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), d, 0);
     559           0 :         d += n;
     560             : 
     561           0 :         if (n == 0) {
     562           0 :             SSLerr(SSL_F_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
     563           0 :             return (-1);
     564             :         }
     565             : 
     566           0 :         s2n(n, p);              /* cipher spec num bytes */
     567             : 
     568           0 :         if ((s->session->session_id_length > 0) &&
     569             :             (s->session->session_id_length <=
     570             :              SSL2_MAX_SSL_SESSION_ID_LENGTH)) {
     571           0 :             i = s->session->session_id_length;
     572           0 :             s2n(i, p);          /* session id length */
     573           0 :             memcpy(d, s->session->session_id, (unsigned int)i);
     574           0 :             d += i;
     575             :         } else {
     576           0 :             s2n(0, p);
     577             :         }
     578             : 
     579           0 :         s->s2->challenge_length = SSL2_CHALLENGE_LENGTH;
     580           0 :         s2n(SSL2_CHALLENGE_LENGTH, p); /* challenge length */
     581             :         /*
     582             :          * challenge id data
     583             :          */
     584           0 :         if (RAND_pseudo_bytes(s->s2->challenge, SSL2_CHALLENGE_LENGTH) <= 0)
     585             :             return -1;
     586           0 :         memcpy(d, s->s2->challenge, SSL2_CHALLENGE_LENGTH);
     587           0 :         d += SSL2_CHALLENGE_LENGTH;
     588             : 
     589           0 :         s->state = SSL2_ST_SEND_CLIENT_HELLO_B;
     590           0 :         s->init_num = d - buf;
     591           0 :         s->init_off = 0;
     592             :     }
     593             :     /* SSL2_ST_SEND_CLIENT_HELLO_B */
     594           0 :     return (ssl2_do_write(s));
     595             : }
     596             : 
     597           0 : static int client_master_key(SSL *s)
     598             : {
     599             :     unsigned char *buf;
     600             :     unsigned char *p, *d;
     601             :     int clear, enc, karg, i;
     602             :     SSL_SESSION *sess;
     603             :     const EVP_CIPHER *c;
     604             :     const EVP_MD *md;
     605             : 
     606           0 :     buf = (unsigned char *)s->init_buf->data;
     607           0 :     if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A) {
     608             : 
     609           0 :         if (!ssl_cipher_get_evp(s->session, &c, &md, NULL, NULL, NULL)) {
     610           0 :             ssl2_return_error(s, SSL2_PE_NO_CIPHER);
     611           0 :             SSLerr(SSL_F_CLIENT_MASTER_KEY,
     612             :                    SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
     613           0 :             return (-1);
     614             :         }
     615           0 :         sess = s->session;
     616             :         p = buf;
     617           0 :         d = p + 10;
     618           0 :         *(p++) = SSL2_MT_CLIENT_MASTER_KEY; /* type */
     619             : 
     620           0 :         i = ssl_put_cipher_by_char(s, sess->cipher, p);
     621           0 :         p += i;
     622             : 
     623             :         /* make key_arg data */
     624           0 :         i = EVP_CIPHER_iv_length(c);
     625           0 :         sess->key_arg_length = i;
     626           0 :         if (i > SSL_MAX_KEY_ARG_LENGTH) {
     627           0 :             ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     628           0 :             SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
     629           0 :             return -1;
     630             :         }
     631           0 :         if (i > 0)
     632           0 :             if (RAND_pseudo_bytes(sess->key_arg, i) <= 0)
     633             :                 return -1;
     634             : 
     635             :         /* make a master key */
     636           0 :         i = EVP_CIPHER_key_length(c);
     637           0 :         sess->master_key_length = i;
     638           0 :         if (i > 0) {
     639           0 :             if (i > (int)sizeof(sess->master_key)) {
     640           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     641           0 :                 SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
     642           0 :                 return -1;
     643             :             }
     644           0 :             if (RAND_bytes(sess->master_key, i) <= 0) {
     645           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     646           0 :                 return (-1);
     647             :             }
     648             :         }
     649             : 
     650           0 :         if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
     651             :             enc = 8;
     652           0 :         else if (SSL_C_IS_EXPORT(sess->cipher))
     653             :             enc = 5;
     654             :         else
     655             :             enc = i;
     656             : 
     657           0 :         if ((int)i < enc) {
     658           0 :             ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     659           0 :             SSLerr(SSL_F_CLIENT_MASTER_KEY, SSL_R_CIPHER_TABLE_SRC_ERROR);
     660           0 :             return (-1);
     661             :         }
     662           0 :         clear = i - enc;
     663           0 :         s2n(clear, p);
     664           0 :         memcpy(d, sess->master_key, (unsigned int)clear);
     665           0 :         d += clear;
     666             : 
     667           0 :         enc = ssl_rsa_public_encrypt(sess->sess_cert, enc,
     668             :                                      &(sess->master_key[clear]), d,
     669           0 :                                      (s->
     670           0 :                                       s2->ssl2_rollback) ? RSA_SSLV23_PADDING
     671             :                                      : RSA_PKCS1_PADDING);
     672           0 :         if (enc <= 0) {
     673           0 :             ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     674           0 :             SSLerr(SSL_F_CLIENT_MASTER_KEY, SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
     675           0 :             return (-1);
     676             :         }
     677             : # ifdef PKCS1_CHECK
     678             :         if (s->options & SSL_OP_PKCS1_CHECK_1)
     679             :             d[1]++;
     680             :         if (s->options & SSL_OP_PKCS1_CHECK_2)
     681             :             sess->master_key[clear]++;
     682             : # endif
     683           0 :         s2n(enc, p);
     684           0 :         d += enc;
     685           0 :         karg = sess->key_arg_length;
     686           0 :         s2n(karg, p);           /* key arg size */
     687           0 :         if (karg > (int)sizeof(sess->key_arg)) {
     688           0 :             ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     689           0 :             SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
     690           0 :             return -1;
     691             :         }
     692           0 :         memcpy(d, sess->key_arg, (unsigned int)karg);
     693           0 :         d += karg;
     694             : 
     695           0 :         s->state = SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
     696           0 :         s->init_num = d - buf;
     697           0 :         s->init_off = 0;
     698             :     }
     699             : 
     700             :     /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
     701           0 :     return (ssl2_do_write(s));
     702             : }
     703             : 
     704           0 : static int client_finished(SSL *s)
     705             : {
     706             :     unsigned char *p;
     707             : 
     708           0 :     if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A) {
     709           0 :         p = (unsigned char *)s->init_buf->data;
     710           0 :         *(p++) = SSL2_MT_CLIENT_FINISHED;
     711           0 :         if (s->s2->conn_id_length > sizeof s->s2->conn_id) {
     712           0 :             SSLerr(SSL_F_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
     713           0 :             return -1;
     714             :         }
     715           0 :         memcpy(p, s->s2->conn_id, (unsigned int)s->s2->conn_id_length);
     716             : 
     717           0 :         s->state = SSL2_ST_SEND_CLIENT_FINISHED_B;
     718           0 :         s->init_num = s->s2->conn_id_length + 1;
     719           0 :         s->init_off = 0;
     720             :     }
     721           0 :     return (ssl2_do_write(s));
     722             : }
     723             : 
     724             : /* read the data and then respond */
     725           0 : static int client_certificate(SSL *s)
     726             : {
     727             :     unsigned char *buf;
     728             :     unsigned char *p, *d;
     729             :     int i;
     730             :     unsigned int n;
     731             :     int cert_ch_len;
     732             :     unsigned char *cert_ch;
     733             : 
     734           0 :     buf = (unsigned char *)s->init_buf->data;
     735             : 
     736             :     /*
     737             :      * We have a cert associated with the SSL, so attach it to the session if
     738             :      * it does not have one
     739             :      */
     740             : 
     741           0 :     if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A) {
     742           0 :         i = ssl2_read(s, (char *)&(buf[s->init_num]),
     743           0 :                       SSL2_MAX_CERT_CHALLENGE_LENGTH + 2 - s->init_num);
     744           0 :         if (i < (SSL2_MIN_CERT_CHALLENGE_LENGTH + 2 - s->init_num))
     745           0 :             return (ssl2_part_read(s, SSL_F_CLIENT_CERTIFICATE, i));
     746           0 :         s->init_num += i;
     747           0 :         if (s->msg_callback) {
     748             :             /* REQUEST-CERTIFICATE */
     749           0 :             s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s,
     750             :                             s->msg_callback_arg);
     751             :         }
     752             : 
     753             :         /* type=buf[0]; */
     754             :         /* type eq x509 */
     755           0 :         if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) {
     756           0 :             ssl2_return_error(s, SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
     757           0 :             SSLerr(SSL_F_CLIENT_CERTIFICATE, SSL_R_BAD_AUTHENTICATION_TYPE);
     758           0 :             return (-1);
     759             :         }
     760             : 
     761           0 :         if ((s->cert == NULL) ||
     762           0 :             (s->cert->key->x509 == NULL) ||
     763           0 :             (s->cert->key->privatekey == NULL)) {
     764           0 :             s->state = SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
     765             :         } else
     766           0 :             s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
     767             :     }
     768             : 
     769           0 :     cert_ch = buf + 2;
     770           0 :     cert_ch_len = s->init_num - 2;
     771             : 
     772           0 :     if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE) {
     773           0 :         X509 *x509 = NULL;
     774           0 :         EVP_PKEY *pkey = NULL;
     775             : 
     776             :         /*
     777             :          * If we get an error we need to ssl->rwstate=SSL_X509_LOOKUP;
     778             :          * return(error); We should then be retried when things are ok and we
     779             :          * can get a cert or not
     780             :          */
     781             : 
     782             :         i = 0;
     783           0 :         if (s->ctx->client_cert_cb != NULL) {
     784           0 :             i = s->ctx->client_cert_cb(s, &(x509), &(pkey));
     785             :         }
     786             : 
     787           0 :         if (i < 0) {
     788           0 :             s->rwstate = SSL_X509_LOOKUP;
     789           0 :             return (-1);
     790             :         }
     791           0 :         s->rwstate = SSL_NOTHING;
     792             : 
     793           0 :         if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
     794           0 :             s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
     795           0 :             if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
     796             :                 i = 0;
     797             :             }
     798           0 :             X509_free(x509);
     799           0 :             EVP_PKEY_free(pkey);
     800           0 :         } else if (i == 1) {
     801           0 :             if (x509 != NULL)
     802           0 :                 X509_free(x509);
     803           0 :             if (pkey != NULL)
     804           0 :                 EVP_PKEY_free(pkey);
     805           0 :             SSLerr(SSL_F_CLIENT_CERTIFICATE,
     806             :                    SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
     807             :             i = 0;
     808             :         }
     809             : 
     810           0 :         if (i == 0) {
     811             :             /*
     812             :              * We have no client certificate to respond with so send the
     813             :              * correct error message back
     814             :              */
     815           0 :             s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
     816           0 :             p = buf;
     817           0 :             *(p++) = SSL2_MT_ERROR;
     818           0 :             s2n(SSL2_PE_NO_CERTIFICATE, p);
     819           0 :             s->init_off = 0;
     820           0 :             s->init_num = 3;
     821             :             /* Write is done at the end */
     822             :         }
     823             :     }
     824             : 
     825           0 :     if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B) {
     826           0 :         return (ssl2_do_write(s));
     827             :     }
     828             : 
     829           0 :     if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C) {
     830             :         EVP_MD_CTX ctx;
     831             : 
     832             :         /*
     833             :          * ok, now we calculate the checksum do it first so we can reuse buf
     834             :          * :-)
     835             :          */
     836           0 :         p = buf;
     837           0 :         EVP_MD_CTX_init(&ctx);
     838           0 :         EVP_SignInit_ex(&ctx, s->ctx->rsa_md5, NULL);
     839           0 :         EVP_SignUpdate(&ctx, s->s2->key_material, s->s2->key_material_length);
     840           0 :         EVP_SignUpdate(&ctx, cert_ch, (unsigned int)cert_ch_len);
     841           0 :         i = i2d_X509(s->session->sess_cert->peer_key->x509, &p);
     842             :         /*
     843             :          * Don't update the signature if it fails - FIXME: probably should
     844             :          * handle this better
     845             :          */
     846           0 :         if (i > 0)
     847           0 :             EVP_SignUpdate(&ctx, buf, (unsigned int)i);
     848             : 
     849           0 :         p = buf;
     850           0 :         d = p + 6;
     851           0 :         *(p++) = SSL2_MT_CLIENT_CERTIFICATE;
     852           0 :         *(p++) = SSL2_CT_X509_CERTIFICATE;
     853           0 :         n = i2d_X509(s->cert->key->x509, &d);
     854           0 :         s2n(n, p);
     855             : 
     856           0 :         if (!EVP_SignFinal(&ctx, d, &n, s->cert->key->privatekey)) {
     857             :             /*
     858             :              * this is not good.  If things have failed it means there so
     859             :              * something wrong with the key. We will continue with a 0 length
     860             :              * signature
     861             :              */
     862             :         }
     863           0 :         EVP_MD_CTX_cleanup(&ctx);
     864           0 :         s2n(n, p);
     865           0 :         d += n;
     866             : 
     867           0 :         s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
     868           0 :         s->init_num = d - buf;
     869           0 :         s->init_off = 0;
     870             :     }
     871             :     /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
     872           0 :     return (ssl2_do_write(s));
     873             : }
     874             : 
     875           0 : static int get_server_verify(SSL *s)
     876             : {
     877             :     unsigned char *p;
     878             :     int i, n, len;
     879             : 
     880           0 :     p = (unsigned char *)s->init_buf->data;
     881           0 :     if (s->state == SSL2_ST_GET_SERVER_VERIFY_A) {
     882           0 :         i = ssl2_read(s, (char *)&(p[s->init_num]), 1 - s->init_num);
     883           0 :         if (i < (1 - s->init_num))
     884           0 :             return (ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i));
     885           0 :         s->init_num += i;
     886             : 
     887           0 :         s->state = SSL2_ST_GET_SERVER_VERIFY_B;
     888           0 :         if (*p != SSL2_MT_SERVER_VERIFY) {
     889           0 :             if (p[0] != SSL2_MT_ERROR) {
     890           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     891           0 :                 SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_READ_WRONG_PACKET_TYPE);
     892             :             } else {
     893           0 :                 SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_PEER_ERROR);
     894             :                 /* try to read the error message */
     895           0 :                 i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num);
     896           0 :                 return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i);
     897             :             }
     898           0 :             return (-1);
     899             :         }
     900             :     }
     901             : 
     902           0 :     p = (unsigned char *)s->init_buf->data;
     903           0 :     len = 1 + s->s2->challenge_length;
     904           0 :     n = len - s->init_num;
     905           0 :     i = ssl2_read(s, (char *)&(p[s->init_num]), n);
     906           0 :     if (i < n)
     907           0 :         return (ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i));
     908           0 :     if (s->msg_callback) {
     909             :         /* SERVER-VERIFY */
     910           0 :         s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg);
     911             :     }
     912           0 :     p += 1;
     913             : 
     914           0 :     if (CRYPTO_memcmp(p, s->s2->challenge, s->s2->challenge_length) != 0) {
     915           0 :         ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     916           0 :         SSLerr(SSL_F_GET_SERVER_VERIFY, SSL_R_CHALLENGE_IS_DIFFERENT);
     917           0 :         return (-1);
     918             :     }
     919             :     return (1);
     920             : }
     921             : 
     922           0 : static int get_server_finished(SSL *s)
     923             : {
     924             :     unsigned char *buf;
     925             :     unsigned char *p;
     926             :     int i, n, len;
     927             : 
     928           0 :     buf = (unsigned char *)s->init_buf->data;
     929             :     p = buf;
     930           0 :     if (s->state == SSL2_ST_GET_SERVER_FINISHED_A) {
     931           0 :         i = ssl2_read(s, (char *)&(buf[s->init_num]), 1 - s->init_num);
     932           0 :         if (i < (1 - s->init_num))
     933           0 :             return (ssl2_part_read(s, SSL_F_GET_SERVER_FINISHED, i));
     934           0 :         s->init_num += i;
     935             : 
     936           0 :         if (*p == SSL2_MT_REQUEST_CERTIFICATE) {
     937           0 :             s->state = SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
     938           0 :             return (1);
     939           0 :         } else if (*p != SSL2_MT_SERVER_FINISHED) {
     940           0 :             if (p[0] != SSL2_MT_ERROR) {
     941           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     942           0 :                 SSLerr(SSL_F_GET_SERVER_FINISHED,
     943             :                        SSL_R_READ_WRONG_PACKET_TYPE);
     944             :             } else {
     945           0 :                 SSLerr(SSL_F_GET_SERVER_FINISHED, SSL_R_PEER_ERROR);
     946             :                 /* try to read the error message */
     947           0 :                 i = ssl2_read(s, (char *)&(p[s->init_num]), 3 - s->init_num);
     948           0 :                 return ssl2_part_read(s, SSL_F_GET_SERVER_VERIFY, i);
     949             :             }
     950           0 :             return (-1);
     951             :         }
     952           0 :         s->state = SSL2_ST_GET_SERVER_FINISHED_B;
     953             :     }
     954             : 
     955             :     len = 1 + SSL2_SSL_SESSION_ID_LENGTH;
     956           0 :     n = len - s->init_num;
     957           0 :     i = ssl2_read(s, (char *)&(buf[s->init_num]), n);
     958           0 :     if (i < n) {
     959             :         /*
     960             :          * XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH,
     961             :          * that's the maximum
     962             :          */
     963           0 :         return (ssl2_part_read(s, SSL_F_GET_SERVER_FINISHED, i));
     964             :     }
     965           0 :     s->init_num += i;
     966           0 :     if (s->msg_callback) {
     967             :         /* SERVER-FINISHED */
     968           0 :         s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s,
     969             :                         s->msg_callback_arg);
     970             :     }
     971             : 
     972           0 :     if (!s->hit) {              /* new session */
     973             :         /* new session-id */
     974             :         /*
     975             :          * Make sure we were not trying to re-use an old SSL_SESSION or bad
     976             :          * things can happen
     977             :          */
     978             :         /* ZZZZZZZZZZZZZ */
     979           0 :         s->session->session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
     980           0 :         memcpy(s->session->session_id, p + 1, SSL2_SSL_SESSION_ID_LENGTH);
     981             :     } else {
     982           0 :         if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG)) {
     983           0 :             if ((s->session->session_id_length >
     984             :                  sizeof s->session->session_id)
     985           0 :                 || (0 !=
     986           0 :                     memcmp(buf + 1, s->session->session_id,
     987             :                            (unsigned int)s->session->session_id_length))) {
     988           0 :                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
     989           0 :                 SSLerr(SSL_F_GET_SERVER_FINISHED,
     990             :                        SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
     991           0 :                 return (-1);
     992             :             }
     993             :         }
     994             :     }
     995           0 :     s->state = SSL_ST_OK;
     996           0 :     return (1);
     997             : }
     998             : 
     999             : /* loads in the certificate from the server */
    1000           0 : int ssl2_set_certificate(SSL *s, int type, int len, const unsigned char *data)
    1001             : {
    1002             :     STACK_OF(X509) *sk = NULL;
    1003             :     EVP_PKEY *pkey = NULL;
    1004             :     SESS_CERT *sc = NULL;
    1005             :     int i;
    1006             :     X509 *x509 = NULL;
    1007             :     int ret = 0;
    1008             : 
    1009           0 :     x509 = d2i_X509(NULL, &data, (long)len);
    1010           0 :     if (x509 == NULL) {
    1011           0 :         SSLerr(SSL_F_SSL2_SET_CERTIFICATE, ERR_R_X509_LIB);
    1012           0 :         goto err;
    1013             :     }
    1014             : 
    1015           0 :     if ((sk = sk_X509_new_null()) == NULL || !sk_X509_push(sk, x509)) {
    1016           0 :         SSLerr(SSL_F_SSL2_SET_CERTIFICATE, ERR_R_MALLOC_FAILURE);
    1017           0 :         goto err;
    1018             :     }
    1019             : 
    1020           0 :     i = ssl_verify_cert_chain(s, sk);
    1021             : 
    1022           0 :     if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)) {
    1023           0 :         SSLerr(SSL_F_SSL2_SET_CERTIFICATE, SSL_R_CERTIFICATE_VERIFY_FAILED);
    1024           0 :         goto err;
    1025             :     }
    1026           0 :     ERR_clear_error();          /* but we keep s->verify_result */
    1027           0 :     s->session->verify_result = s->verify_result;
    1028             : 
    1029             :     /* server's cert for this session */
    1030           0 :     sc = ssl_sess_cert_new();
    1031           0 :     if (sc == NULL) {
    1032             :         ret = -1;
    1033             :         goto err;
    1034             :     }
    1035           0 :     if (s->session->sess_cert)
    1036           0 :         ssl_sess_cert_free(s->session->sess_cert);
    1037           0 :     s->session->sess_cert = sc;
    1038             : 
    1039           0 :     sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509 = x509;
    1040           0 :     sc->peer_key = &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]);
    1041             : 
    1042           0 :     pkey = X509_get_pubkey(x509);
    1043             :     x509 = NULL;
    1044           0 :     if (pkey == NULL) {
    1045           0 :         SSLerr(SSL_F_SSL2_SET_CERTIFICATE,
    1046             :                SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
    1047           0 :         goto err;
    1048             :     }
    1049           0 :     if (pkey->type != EVP_PKEY_RSA) {
    1050           0 :         SSLerr(SSL_F_SSL2_SET_CERTIFICATE, SSL_R_PUBLIC_KEY_NOT_RSA);
    1051           0 :         goto err;
    1052             :     }
    1053             : 
    1054           0 :     if (!ssl_set_peer_cert_type(sc, SSL2_CT_X509_CERTIFICATE))
    1055             :         goto err;
    1056             :     ret = 1;
    1057             :  err:
    1058           0 :     sk_X509_free(sk);
    1059           0 :     X509_free(x509);
    1060           0 :     EVP_PKEY_free(pkey);
    1061           0 :     return (ret);
    1062             : }
    1063             : 
    1064           0 : static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
    1065             :                                   unsigned char *to, int padding)
    1066             : {
    1067             :     EVP_PKEY *pkey = NULL;
    1068             :     int i = -1;
    1069             : 
    1070           0 :     if ((sc == NULL) || (sc->peer_key->x509 == NULL) ||
    1071             :         ((pkey = X509_get_pubkey(sc->peer_key->x509)) == NULL)) {
    1072           0 :         SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, SSL_R_NO_PUBLICKEY);
    1073           0 :         return (-1);
    1074             :     }
    1075           0 :     if (pkey->type != EVP_PKEY_RSA) {
    1076           0 :         SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, SSL_R_PUBLIC_KEY_IS_NOT_RSA);
    1077           0 :         goto end;
    1078             :     }
    1079             : 
    1080             :     /* we have the public key */
    1081           0 :     i = RSA_public_encrypt(len, from, to, pkey->pkey.rsa, padding);
    1082           0 :     if (i < 0)
    1083           0 :         SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT, ERR_R_RSA_LIB);
    1084             :  end:
    1085           0 :     EVP_PKEY_free(pkey);
    1086           0 :     return (i);
    1087             : }
    1088             : #else                           /* !OPENSSL_NO_SSL2 */
    1089             : 
    1090             : # if PEDANTIC
    1091             : static void *dummy = &dummy;
    1092             : # endif
    1093             : 
    1094             : #endif

Generated by: LCOV version 1.10