(linenum→info "unix/slp.c:2238")

openssl/0.9.8g/ssl/s2_clnt.c

    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 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: static SSL_METHOD *ssl2_get_client_method(int ver)
  133:         {
  134:         if (ver == SSL2_VERSION)
  135:                 return(SSLv2_client_method());
  136:         else
  137:                 return(NULL);
  138:         }
  139: 
  140: IMPLEMENT_ssl2_meth_func(SSLv2_client_method,
  141:                         ssl_undefined_function,
  142:                         ssl2_connect,
  143:                         ssl2_get_client_method)
  144: 
  145: int ssl2_connect(SSL *s)
  146:         {
  147:         unsigned long l=(unsigned long)time(NULL);
  148:         BUF_MEM *buf=NULL;
  149:         int ret= -1;
  150:         void (*cb)(const SSL *ssl,int type,int val)=NULL;
  151:         int new_state,state;
  152: 
  153:         RAND_add(&l,sizeof(l),0);
  154:         ERR_clear_error();
  155:         clear_sys_error();
  156: 
  157:         if (s->info_callback != NULL)
  158:                 cb=s->info_callback;
  159:         else if (s->ctx->info_callback != NULL)
  160:                 cb=s->ctx->info_callback;
  161: 
  162:         /* init things to blank */
  163:         s->in_handshake++;
  164:         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  165: 
  166:         for (;;)
  167:                 {
  168:                 state=s->state;
  169: 
  170:                 switch (s->state)
  171:                         {
  172:                 case SSL_ST_BEFORE:
  173:                 case SSL_ST_CONNECT:
  174:                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
  175:                 case SSL_ST_OK|SSL_ST_CONNECT:
  176: 
  177:                         s->server=0;
  178:                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  179: 
  180:                         s->version=SSL2_VERSION;
  181:                         s->type=SSL_ST_CONNECT;
  182: 
  183:                         buf=s->init_buf;
  184:                         if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
  185:                                 {
  186:                                 ret= -1;
  187:                                 goto end;
  188:                                 }
  189:                         if (!BUF_MEM_grow(buf,
  190:                                 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
  191:                                 {
  192:                                 if (buf == s->init_buf)
  193:                                         buf=NULL;
  194:                                 ret= -1;
  195:                                 goto end;
  196:                                 }
  197:                         s->init_buf=buf;
  198:                         buf=NULL;
  199:                         s->init_num=0;
  200:                         s->state=SSL2_ST_SEND_CLIENT_HELLO_A;
  201:                         s->ctx->stats.sess_connect++;
  202:                         s->handshake_func=ssl2_connect;
  203:                         BREAK;
  204: 
  205:                 case SSL2_ST_SEND_CLIENT_HELLO_A:
  206:                 case SSL2_ST_SEND_CLIENT_HELLO_B:
  207:                         s->shutdown=0;
  208:                         ret=client_hello(s);
  209:                         if (ret <= 0) goto end;
  210:                         s->init_num=0;
  211:                         s->state=SSL2_ST_GET_SERVER_HELLO_A;
  212:                         BREAK;
  213:                 
  214:                 case SSL2_ST_GET_SERVER_HELLO_A:
  215:                 case SSL2_ST_GET_SERVER_HELLO_B:
  216:                         ret=get_server_hello(s);
  217:                         if (ret <= 0) goto end;
  218:                         s->init_num=0;
  219:                         if (!s->hit) /* new session */
  220:                                 {
  221:                                 s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
  222:                                 BREAK; 
  223:                                 }
  224:                         else
  225:                                 {
  226:                                 s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
  227:                                 break;
  228:                                 }
  229:         
  230:                 case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
  231:                 case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
  232:                         ret=client_master_key(s);
  233:                         if (ret <= 0) goto end;
  234:                         s->init_num=0;
  235:                         s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
  236:                         break;
  237: 
  238:                 case SSL2_ST_CLIENT_START_ENCRYPTION:
  239:                         /* Ok, we now have all the stuff needed to
  240:                          * start encrypting, so lets fire it up :-) */
  241:                         if (!ssl2_enc_init(s,1))
  242:                                 {
  243:                                 ret= -1;
  244:                                 goto end;
  245:                                 }
  246:                         s->s2->clear_text=0;
  247:                         s->state=SSL2_ST_SEND_CLIENT_FINISHED_A;
  248:                         break;
  249: 
  250:                 case SSL2_ST_SEND_CLIENT_FINISHED_A:
  251:                 case SSL2_ST_SEND_CLIENT_FINISHED_B:
  252:                         ret=client_finished(s);
  253:                         if (ret <= 0) goto end;
  254:                         s->init_num=0;
  255:                         s->state=SSL2_ST_GET_SERVER_VERIFY_A;
  256:                         break;
  257: 
  258:                 case SSL2_ST_GET_SERVER_VERIFY_A:
  259:                 case SSL2_ST_GET_SERVER_VERIFY_B:
  260:                         ret=get_server_verify(s);
  261:                         if (ret <= 0) goto end;
  262:                         s->init_num=0;
  263:                         s->state=SSL2_ST_GET_SERVER_FINISHED_A;
  264:                         break;
  265: 
  266:                 case SSL2_ST_GET_SERVER_FINISHED_A:
  267:                 case SSL2_ST_GET_SERVER_FINISHED_B:
  268:                         ret=get_server_finished(s);
  269:                         if (ret <= 0) 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:                         ret=client_certificate(s);
  278:                         if (ret <= 0) goto end;
  279:                         s->init_num=0;
  280:                         s->state=SSL2_ST_GET_SERVER_FINISHED_A;
  281:                         break;
  282: 
  283:                 case SSL_ST_OK:
  284:                         if (s->init_buf != NULL)
  285:                                 {
  286:                                 BUF_MEM_free(s->init_buf);
  287:                                 s->init_buf=NULL;
  288:                                 }
  289:                         s->init_num=0;
  290:                 /*    ERR_clear_error();*/
  291: 
  292:                         /* If we want to cache session-ids in the client
  293:                          * and we successfully add the session-id to the
  294:                          * cache, and there is a callback, then pass it out.
  295:                          * 26/11/96 - eay - only add if not a re-used session.
  296:                          */
  297: 
  298:                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  299:                         if (s->hit) s->ctx->stats.sess_hit++;
  300: 
  301:                         ret=1;
  302:                         /* s->server=0; */
  303:                         s->ctx->stats.sess_connect_good++;
  304: 
  305:                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  306: 
  307:                         goto end;
  308:                         /* break; */
  309:                 default:
  310:                         SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);
  311:                         return(-1);
  312:                         /* break; */
  313:                         }
  314: 
  315:                 if ((cb != NULL) && (s->state != state))
  316:                         {
  317:                         new_state=s->state;
  318:                         s->state=state;
  319:                         cb(s,SSL_CB_CONNECT_LOOP,1);
  320:                         s->state=new_state;
  321:                         }
  322:                 }
  323: end:
  324:         s->in_handshake--;
  325:         if (buf != NULL)
  326:                 BUF_MEM_free(buf);
  327:         if (cb != NULL) 
  328:                 cb(s,SSL_CB_CONNECT_EXIT,ret);
  329:         return(ret);
  330:         }
  331: 
  332: static int get_server_hello(SSL *s)
  333:         {
  334:         unsigned char *buf;
  335:         unsigned char *p;
  336:         int i,j;
  337:         unsigned long len;
  338:         STACK_OF(SSL_CIPHER) *sk=NULL,*cl, *prio, *allow;
  339: 
  340:         buf=(unsigned char *)s->init_buf->data;
  341:         p=buf;
  342:         if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
  343:                 {
  344:                 i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num);
  345:                 if (i < (11-s->init_num)) 
  346:                         return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
  347:                 s->init_num = 11;
  348: 
  349:                 if (*(p++) != SSL2_MT_SERVER_HELLO)
  350:                         {
  351:                         if (p[-1] != SSL2_MT_ERROR)
  352:                                 {
  353:                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  354:                                 SSLerr(SSL_F_GET_SERVER_HELLO,
  355:                                         SSL_R_READ_WRONG_PACKET_TYPE);
  356:                                 }
  357:                         else
  358:                                 SSLerr(SSL_F_GET_SERVER_HELLO,
  359:                                         SSL_R_PEER_ERROR);
  360:                         return(-1);
  361:                         }
  362: #ifdef __APPLE_CC__
  363:                 /* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug
  364:                  * workaround. <appro@fy.chalmers.se> */
  365:                 s->hit=(i=*(p++))?1:0;
  366: #else
  367:                 s->hit=(*(p++))?1:0;
  368: #endif
  369:                 s->s2->tmp.cert_type= *(p++);
  370:                 n2s(p,i);
  371:                 if (i < s->version) s->version=i;
  372:                 n2s(p,i); s->s2->tmp.cert_length=i;
  373:                 n2s(p,i); s->s2->tmp.csl=i;
  374:                 n2s(p,i); s->s2->tmp.conn_id_length=i;
  375:                 s->state=SSL2_ST_GET_SERVER_HELLO_B;
  376:                 }
  377: 
  378:         /* SSL2_ST_GET_SERVER_HELLO_B */
  379:         len = 11 + (unsigned long)s->s2->tmp.cert_length + (unsigned long)s->s2->tmp.csl + (unsigned long)s->s2->tmp.conn_id_length;
  380:         if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
  381:                 {
  382:                 SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_MESSAGE_TOO_LONG);
  383:                 return -1;
  384:                 }
  385:         j = (int)len - s->init_num;
  386:         i = ssl2_read(s,(char *)&(buf[s->init_num]),j);
  387:         if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
  388:         if (s->msg_callback)
  389:                 s->msg_callback(0, s->version, 0, buf, (size_t)len, s, s->msg_callback_arg); /* SERVER-HELLO */
  390: 
  391:         /* things are looking good */
  392: 
  393:         p = buf + 11;
  394:         if (s->hit)
  395:                 {
  396:                 if (s->s2->tmp.cert_length != 0) 
  397:                         {
  398:                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
  399:                         return(-1);
  400:                         }
  401:                 if (s->s2->tmp.cert_type != 0)
  402:                         {
  403:                         if (!(s->options &
  404:                                 SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG))
  405:                                 {
  406:                                 SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
  407:                                 return(-1);
  408:                                 }
  409:                         }
  410:                 if (s->s2->tmp.csl != 0)
  411:                         {
  412:                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
  413:                         return(-1);
  414:                         }
  415:                 }
  416:         else
  417:                 {
  418: #ifdef undef
  419:                 /* very bad */
  420:                 memset(s->session->session_id,0,
  421:                         SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
  422:                 s->session->session_id_length=0;
  423:                 */
  424: #endif
  425: 
  426:                 /* we need to do this in case we were trying to reuse a 
  427:                  * client session but others are already reusing it.
  428:                  * If this was a new 'blank' session ID, the session-id
  429:                  * length will still be 0 */
  430:                 if (s->session->session_id_length > 0)
  431:                         {
  432:                         if (!ssl_get_new_session(s,0))
  433:                                 {
  434:                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  435:                                 return(-1);
  436:                                 }
  437:                         }
  438: 
  439:                 if (ssl2_set_certificate(s,s->s2->tmp.cert_type,
  440:                         s->s2->tmp.cert_length,p) <= 0)
  441:                         {
  442:                         ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
  443:                         return(-1);
  444:                         }
  445:                 p+=s->s2->tmp.cert_length;
  446: 
  447:                 if (s->s2->tmp.csl == 0)
  448:                         {
  449:                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  450:                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST);
  451:                         return(-1);
  452:                         }
  453: 
  454:                 /* We have just received a list of ciphers back from the
  455:                  * server.  We need to get the ones that match, then select
  456:                  * the one we want the most :-). */
  457: 
  458:                 /* load the ciphers */
  459:                 sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl,
  460:                                             &s->session->ciphers);
  461:                 p+=s->s2->tmp.csl;
  462:                 if (sk == NULL)
  463:                         {
  464:                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
  465:                         SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
  466:                         return(-1);
  467:                         }
  468: 
  469:                 (void)sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp);
  470: 
  471:                 /* get the array of ciphers we will accept */
  472:                 cl=SSL_get_ciphers(s);
  473:                 (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:                 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
  481:                     {
  482:                     prio = sk;
  483:                     allow = cl;
  484:                     }
  485:                 else
  486:                     {
  487:                     prio = cl;
  488:                     allow = sk;
  489:                     }
  490:                 /* In theory we could have ciphers sent back that we
  491:                  * don't want to use but that does not matter since we
  492:                  * will check against the list we originally sent and
  493:                  * for performance reasons we should not bother to match
  494:                  * the two lists up just to check. */
  495:                 for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
  496:                         {
  497:                         if (sk_SSL_CIPHER_find(allow,
  498:                                              sk_SSL_CIPHER_value(prio,i)) >= 0)
  499:                                 break;
  500:                         }
  501: 
  502:                 if (i >= sk_SSL_CIPHER_num(prio))
  503:                         {
  504:                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
  505:                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH);
  506:                         return(-1);
  507:                         }
  508:                 s->session->cipher=sk_SSL_CIPHER_value(prio,i);
  509: 
  510: 
  511:                 if (s->session->peer != NULL) /* can't happen*/
  512:                         {
  513:                         ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  514:                         SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  515:                         return(-1);
  516:                         }
  517: 
  518:                 s->session->peer = s->session->sess_cert->peer_key->x509;
  519:                 /* peer_key->x509 has been set by ssl2_set_certificate. */
  520:                 CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
  521:                 }
  522: 
  523:         if (s->session->sess_cert == NULL 
  524:       || s->session->peer != s->session->sess_cert->peer_key->x509)
  525:                 /* can't happen */
  526:                 {
  527:                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  528:                 SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
  529:                 return(-1);
  530:                 }
  531:                 
  532:         s->s2->conn_id_length=s->s2->tmp.conn_id_length;
  533:         if (s->s2->conn_id_length > sizeof s->s2->conn_id)
  534:                 {
  535:                 ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
  536:                 SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG);
  537:                 return -1;
  538:                 }
  539:         memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length);
  540:         return(1);
  541:         }
  542: 
  543: static int client_hello(SSL *s)
  544:         {
  545:         unsigned char *buf;
  546:         unsigned char *p,*d;
  547: /*      CIPHER **cipher;*/
  548:         int i,n,j;
  549: 
  550:         buf=(unsigned char *)s->init_buf->data;
  551:         if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)