really fix race condition
[openssl.git] / ssl / s2_srvr.c
1 /* ssl/s2_srvr.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/bio.h>
116 #include <openssl/rand.h>
117 #include <openssl/objects.h>
118 #include <openssl/evp.h>
119 #include "cryptlib.h"
120
121 static SSL_METHOD *ssl2_get_server_method(int ver);
122 static int get_client_master_key(SSL *s);
123 static int get_client_hello(SSL *s);
124 static int server_hello(SSL *s); 
125 static int get_client_finished(SSL *s);
126 static int server_verify(SSL *s);
127 static int server_finish(SSL *s);
128 static int request_certificate(SSL *s);
129 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
130         unsigned char *to,int padding);
131 #define BREAK   break
132
133 static SSL_METHOD *ssl2_get_server_method(int ver)
134         {
135         if (ver == SSL2_VERSION)
136                 return(SSLv2_server_method());
137         else
138                 return(NULL);
139         }
140
141 SSL_METHOD *SSLv2_server_method(void)
142         {
143         static int init=1;
144         static SSL_METHOD SSLv2_server_data;
145
146         if (init)
147                 {
148                 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
149
150                 memcpy((char *)&SSLv2_server_data,(char *)sslv2_base_method(),
151                         sizeof(SSL_METHOD));
152                 SSLv2_server_data.ssl_accept=ssl2_accept;
153                 SSLv2_server_data.get_ssl_method=ssl2_get_server_method;
154                 init=0;
155
156                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
157                 }
158         return(&SSLv2_server_data);
159         }
160
161 int ssl2_accept(SSL *s)
162         {
163         unsigned long l=time(NULL);
164         BUF_MEM *buf=NULL;
165         int ret= -1;
166         long num1;
167         void (*cb)(const SSL *ssl,int type,int val)=NULL;
168         int new_state,state;
169
170         RAND_add(&l,sizeof(l),0);
171         ERR_clear_error();
172         clear_sys_error();
173
174         if (s->info_callback != NULL)
175                 cb=s->info_callback;
176         else if (s->ctx->info_callback != NULL)
177                 cb=s->ctx->info_callback;
178
179         /* init things to blank */
180         s->in_handshake++;
181         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
182
183         if (s->cert == NULL)
184                 {
185                 SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
186                 return(-1);
187                 }
188
189         clear_sys_error();
190         for (;;)
191                 {
192                 state=s->state;
193
194                 switch (s->state)
195                         {
196                 case SSL_ST_BEFORE:
197                 case SSL_ST_ACCEPT:
198                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
199                 case SSL_ST_OK|SSL_ST_ACCEPT:
200
201                         s->server=1;
202                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
203
204                         s->version=SSL2_VERSION;
205                         s->type=SSL_ST_ACCEPT;
206
207                         buf=s->init_buf;
208                         if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
209                                 { ret= -1; goto end; }
210                         if (!BUF_MEM_grow(buf,(int)
211                                 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
212                                 { ret= -1; goto end; }
213                         s->init_buf=buf;
214                         s->init_num=0;
215                         s->ctx->stats.sess_accept++;
216                         s->handshake_func=ssl2_accept;
217                         s->state=SSL2_ST_GET_CLIENT_HELLO_A;
218                         BREAK;
219
220                 case SSL2_ST_GET_CLIENT_HELLO_A:
221                 case SSL2_ST_GET_CLIENT_HELLO_B:
222                 case SSL2_ST_GET_CLIENT_HELLO_C:
223                         s->shutdown=0;
224                         ret=get_client_hello(s);
225                         if (ret <= 0) goto end;
226                         s->init_num=0;
227                         s->state=SSL2_ST_SEND_SERVER_HELLO_A;
228                         BREAK;
229
230                 case SSL2_ST_SEND_SERVER_HELLO_A:
231                 case SSL2_ST_SEND_SERVER_HELLO_B:
232                         ret=server_hello(s);
233                         if (ret <= 0) goto end;
234                         s->init_num=0;
235                         if (!s->hit)
236                                 {
237                                 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
238                                 BREAK;
239                                 }
240                         else
241                                 {
242                                 s->state=SSL2_ST_SERVER_START_ENCRYPTION;
243                                 BREAK;
244                                 }
245                 case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
246                 case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
247                         ret=get_client_master_key(s);
248                         if (ret <= 0) goto end;
249                         s->init_num=0;
250                         s->state=SSL2_ST_SERVER_START_ENCRYPTION;
251                         BREAK;
252
253                 case SSL2_ST_SERVER_START_ENCRYPTION:
254                         /* Ok we how have sent all the stuff needed to
255                          * start encrypting, the next packet back will
256                          * be encrypted. */
257                         if (!ssl2_enc_init(s,0))
258                                 { ret= -1; goto end; }
259                         s->s2->clear_text=0;
260                         s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
261                         BREAK;
262
263                 case SSL2_ST_SEND_SERVER_VERIFY_A:
264                 case SSL2_ST_SEND_SERVER_VERIFY_B:
265                         ret=server_verify(s);
266                         if (ret <= 0) goto end;
267                         s->init_num=0;
268                         if (s->hit)
269                                 {
270                                 /* If we are in here, we have been
271                                  * buffering the output, so we need to
272                                  * flush it and remove buffering from
273                                  * future traffic */
274                                 s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
275                                 BREAK;
276                                 }
277                         else
278                                 {
279                                 s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
280                                 break;
281                                 }
282
283                 case SSL2_ST_SEND_SERVER_VERIFY_C:
284                         /* get the number of bytes to write */
285                         num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
286                         if (num1 != 0)
287                                 {
288                                 s->rwstate=SSL_WRITING;
289                                 num1=BIO_flush(s->wbio);
290                                 if (num1 <= 0) { ret= -1; goto end; }
291                                 s->rwstate=SSL_NOTHING;
292                                 }
293
294                         /* flushed and now remove buffering */
295                         s->wbio=BIO_pop(s->wbio);
296
297                         s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
298                         BREAK;
299
300                 case SSL2_ST_GET_CLIENT_FINISHED_A:
301                 case SSL2_ST_GET_CLIENT_FINISHED_B:
302                         ret=get_client_finished(s);
303                         if (ret <= 0)
304                                 goto end;
305                         s->init_num=0;
306                         s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
307                         BREAK;
308
309                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
310                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
311                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
312                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
313                         /* don't do a 'request certificate' if we
314                          * don't want to, or we already have one, and
315                          * we only want to do it once. */
316                         if (!(s->verify_mode & SSL_VERIFY_PEER) ||
317                                 ((s->session->peer != NULL) &&
318                                 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
319                                 {
320                                 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
321                                 break;
322                                 }
323                         else
324                                 {
325                                 ret=request_certificate(s);
326                                 if (ret <= 0) goto end;
327                                 s->init_num=0;
328                                 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
329                                 }
330                         BREAK;
331
332                 case SSL2_ST_SEND_SERVER_FINISHED_A:
333                 case SSL2_ST_SEND_SERVER_FINISHED_B:
334                         ret=server_finish(s);
335                         if (ret <= 0) goto end;
336                         s->init_num=0;
337                         s->state=SSL_ST_OK;
338                         break;
339
340                 case SSL_ST_OK:
341                         BUF_MEM_free(s->init_buf);
342                         ssl_free_wbio_buffer(s);
343                         s->init_buf=NULL;
344                         s->init_num=0;
345                 /*      ERR_clear_error();*/
346
347                         ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
348
349                         s->ctx->stats.sess_accept_good++;
350                         /* s->server=1; */
351                         ret=1;
352
353                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
354
355                         goto end;
356                         /* BREAK; */
357
358                 default:
359                         SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
360                         ret= -1;
361                         goto end;
362                         /* BREAK; */
363                         }
364                 
365                 if ((cb != NULL) && (s->state != state))
366                         {
367                         new_state=s->state;
368                         s->state=state;
369                         cb(s,SSL_CB_ACCEPT_LOOP,1);
370                         s->state=new_state;
371                         }
372                 }
373 end:
374         s->in_handshake--;
375         if (cb != NULL)
376                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
377         return(ret);
378         }
379
380 static int get_client_master_key(SSL *s)
381         {
382         int is_export,i,n,keya,ek;
383         unsigned long len;
384         unsigned char *p;
385         SSL_CIPHER *cp;
386         const EVP_CIPHER *c;
387         const EVP_MD *md;
388
389         p=(unsigned char *)s->init_buf->data;
390         if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
391                 {
392                 i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
393
394                 if (i < (10-s->init_num))
395                         return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
396                 s->init_num = 10;
397
398                 if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
399                         {
400                         if (p[-1] != SSL2_MT_ERROR)
401                                 {
402                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
403                                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
404                                 }
405                         else
406                                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_PEER_ERROR);
407                         return(-1);
408                         }
409
410                 cp=ssl2_get_cipher_by_char(p);
411                 if (cp == NULL)
412                         {
413                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
414                         SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_NO_CIPHER_MATCH);
415                         return(-1);
416                         }
417                 s->session->cipher= cp;
418
419                 p+=3;
420                 n2s(p,i); s->s2->tmp.clear=i;
421                 n2s(p,i); s->s2->tmp.enc=i;
422                 n2s(p,i); s->session->key_arg_length=i;
423                 if(s->session->key_arg_length > SSL_MAX_KEY_ARG_LENGTH)
424                         {
425                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
426                         SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_KEY_ARG_TOO_LONG);
427                         return -1;
428                         }
429                 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
430                 }
431
432         /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
433         p=(unsigned char *)s->init_buf->data;
434         if (s->init_buf->length < SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
435                 {
436                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
437                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
438                 return -1;
439                 }
440         keya=s->session->key_arg_length;
441         len = 10 + (unsigned long)s->s2->tmp.clear + (unsigned long)s->s2->tmp.enc + (unsigned long)keya;
442         if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
443                 {
444                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
445                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_MESSAGE_TOO_LONG);
446                 return -1;
447                 }
448         n = (int)len - s->init_num;
449         i = ssl2_read(s,(char *)&(p[s->init_num]),n);
450         if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
451         if (s->msg_callback)
452                 s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-MASTER-KEY */
453         p += 10;
454
455         memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
456                 (unsigned int)keya);
457
458         if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
459                 {
460                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
461                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
462                 return(-1);
463                 }
464         i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
465                 &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
466                 (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
467
468         is_export=SSL_C_IS_EXPORT(s->session->cipher);
469         
470         if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
471                 {
472                 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
473                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
474                 return(0);
475                 }
476
477         if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
478                 {
479                 is_export=1;
480                 ek=8;
481                 }
482         else
483                 ek=5;
484
485         /* bad decrypt */
486 #if 1
487         /* If a bad decrypt, continue with protocol but with a
488          * random master secret (Bleichenbacher attack) */
489         if ((i < 0) ||
490                 ((!is_export && (i != EVP_CIPHER_key_length(c)))
491                 || (is_export && ((i != ek) || (s->s2->tmp.clear+(unsigned int)i !=
492                         (unsigned int)EVP_CIPHER_key_length(c))))))
493                 {
494                 ERR_clear_error();
495                 if (is_export)
496                         i=ek;
497                 else
498                         i=EVP_CIPHER_key_length(c);
499                 RAND_pseudo_bytes(p,i);
500                 }
501 #else
502         if (i < 0)
503                 {
504                 error=1;
505                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
506                 }
507         /* incorrect number of key bytes for non export cipher */
508         else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
509                 || (is_export && ((i != ek) || (s->s2->tmp.clear+i !=
510                         EVP_CIPHER_key_length(c)))))
511                 {
512                 error=1;
513                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
514                 }
515         if (error)
516                 {
517                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
518                 return(-1);
519                 }
520 #endif
521
522         if (is_export) i+=s->s2->tmp.clear;
523
524         if (i > SSL_MAX_MASTER_KEY_LENGTH)
525                 {
526                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
527                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
528                 return -1;
529                 }
530         s->session->master_key_length=i;
531         memcpy(s->session->master_key,p,(unsigned int)i);
532         return(1);
533         }
534
535 static int get_client_hello(SSL *s)
536         {
537         int i,n;
538         unsigned long len;
539         unsigned char *p;
540         STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */
541         STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */
542         STACK_OF(SSL_CIPHER) *prio, *allow;
543         int z;
544
545         /* This is a bit of a hack to check for the correct packet
546          * type the first time round. */
547         if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
548                 {
549                 s->first_packet=1;
550                 s->state=SSL2_ST_GET_CLIENT_HELLO_B;
551                 }
552
553         p=(unsigned char *)s->init_buf->data;
554         if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
555                 {
556                 i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
557                 if (i < (9-s->init_num)) 
558                         return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
559                 s->init_num = 9;
560         
561                 if (*(p++) != SSL2_MT_CLIENT_HELLO)
562                         {
563                         if (p[-1] != SSL2_MT_ERROR)
564                                 {
565                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
566                                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
567                                 }
568                         else
569                                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
570                         return(-1);
571                         }
572                 n2s(p,i);
573                 if (i < s->version) s->version=i;
574                 n2s(p,i); s->s2->tmp.cipher_spec_length=i;
575                 n2s(p,i); s->s2->tmp.session_id_length=i;
576                 n2s(p,i); s->s2->challenge_length=i;
577                 if (    (i < SSL2_MIN_CHALLENGE_LENGTH) ||
578                         (i > SSL2_MAX_CHALLENGE_LENGTH))
579                         {
580                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
581                         SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
582                         return(-1);
583                         }
584                 s->state=SSL2_ST_GET_CLIENT_HELLO_C;
585                 }
586
587         /* SSL2_ST_GET_CLIENT_HELLO_C */
588         p=(unsigned char *)s->init_buf->data;
589         len = 9 + (unsigned long)s->s2->tmp.cipher_spec_length + (unsigned long)s->s2->challenge_length + (unsigned long)s->s2->tmp.session_id_length;
590         if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
591                 {
592                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
593                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_MESSAGE_TOO_LONG);
594                 return -1;
595                 }
596         n = (int)len - s->init_num;
597         i = ssl2_read(s,(char *)&(p[s->init_num]),n);
598         if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
599         if (s->msg_callback)
600                 s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-HELLO */
601         p += 9;
602
603         /* get session-id before cipher stuff so we can get out session
604          * structure if it is cached */
605         /* session-id */
606         if ((s->s2->tmp.session_id_length != 0) && 
607                 (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
608                 {
609                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
610                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
611                 return(-1);
612                 }
613
614         if (s->s2->tmp.session_id_length == 0)
615                 {
616                 if (!ssl_get_new_session(s,1))
617                         {
618                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
619                         return(-1);
620                         }
621                 }
622         else
623                 {
624                 i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
625                         s->s2->tmp.session_id_length);
626                 if (i == 1)
627                         { /* previous session */
628                         s->hit=1;
629                         }
630                 else if (i == -1)
631                         {
632                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
633                         return(-1);
634                         }
635                 else
636                         {
637                         if (s->cert == NULL)
638                                 {
639                                 ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
640                                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
641                                 return(-1);
642                                 }
643
644                         if (!ssl_get_new_session(s,1))
645                                 {
646                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
647                                 return(-1);
648                                 }
649                         }
650                 }
651
652         if (!s->hit)
653                 {
654                 cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
655                         &s->session->ciphers);
656                 if (cs == NULL) goto mem_err;
657
658                 cl=SSL_get_ciphers(s);
659
660                 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
661                     {
662                     prio=sk_SSL_CIPHER_dup(cl);
663                     if (prio == NULL) goto mem_err;
664                     allow = cs;
665                     }
666                 else
667                     {
668                     prio = cs;
669                     allow = cl;
670                     }
671                 for (z=0; z<sk_SSL_CIPHER_num(prio); z++)
672                         {
673                         if (sk_SSL_CIPHER_find(allow,sk_SSL_CIPHER_value(prio,z)) < 0)
674                                 {
675                                 sk_SSL_CIPHER_delete(prio,z);
676                                 z--;
677                                 }
678                         }
679                 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
680                     {
681                     sk_SSL_CIPHER_free(s->session->ciphers);
682                     s->session->ciphers = prio;
683                     }
684                 /* s->session->ciphers should now have a list of
685                  * ciphers that are on both the client and server.
686                  * This list is ordered by the order the client sent
687                  * the ciphers or in the order of the server's preference
688                  * if SSL_OP_CIPHER_SERVER_PREFERENCE was set.
689                  */
690                 }
691         p+=s->s2->tmp.cipher_spec_length;
692         /* done cipher selection */
693
694         /* session id extracted already */
695         p+=s->s2->tmp.session_id_length;
696
697         /* challenge */
698         if (s->s2->challenge_length > sizeof s->s2->challenge)
699                 {
700                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
701                 SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
702                 return -1;
703                 }
704         memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
705         return(1);
706 mem_err:
707         SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
708         return(0);
709         }
710
711 static int server_hello(SSL *s)
712         {
713         unsigned char *p,*d;
714         int n,hit;
715         STACK_OF(SSL_CIPHER) *sk;
716
717         p=(unsigned char *)s->init_buf->data;
718         if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
719                 {
720                 d=p+11;
721                 *(p++)=SSL2_MT_SERVER_HELLO;            /* type */
722                 hit=s->hit;
723                 *(p++)=(unsigned char)hit;
724 #if 1
725                 if (!hit)
726                         {
727                         if (s->session->sess_cert != NULL)
728                                 /* This can't really happen because get_client_hello
729                                  * has called ssl_get_new_session, which does not set
730                                  * sess_cert. */
731                                 ssl_sess_cert_free(s->session->sess_cert);
732                         s->session->sess_cert = ssl_sess_cert_new();
733                         if (s->session->sess_cert == NULL)
734                                 {
735                                 SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
736                                 return(-1);
737                                 }
738                         }
739                 /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
740                  * depending on whether it survived in the internal cache
741                  * or was retrieved from an external cache.
742                  * If it is NULL, we cannot put any useful data in it anyway,
743                  * so we don't touch it.
744                  */
745
746 #else /* That's what used to be done when cert_st and sess_cert_st were
747            * the same. */
748                 if (!hit)
749                         {                       /* else add cert to session */
750                         CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
751                         if (s->session->sess_cert != NULL)
752                                 ssl_cert_free(s->session->sess_cert);
753                         s->session->sess_cert=s->cert;          
754                         }
755                 else    /* We have a session id-cache hit, if the
756                          * session-id has no certificate listed against
757                          * the 'cert' structure, grab the 'old' one
758                          * listed against the SSL connection */
759                         {
760                         if (s->session->sess_cert == NULL)
761                                 {
762                                 CRYPTO_add(&s->cert->references,1,
763                                         CRYPTO_LOCK_SSL_CERT);
764                                 s->session->sess_cert=s->cert;
765                                 }
766                         }
767 #endif
768
769                 if (s->cert == NULL)
770                         {
771                         ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
772                         SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
773                         return(-1);
774                         }
775
776                 if (hit)
777                         {
778                         *(p++)=0;               /* no certificate type */
779                         s2n(s->version,p);      /* version */
780                         s2n(0,p);               /* cert len */
781                         s2n(0,p);               /* ciphers len */
782                         }
783                 else
784                         {
785                         /* EAY EAY */
786                         /* put certificate type */
787                         *(p++)=SSL2_CT_X509_CERTIFICATE;
788                         s2n(s->version,p);      /* version */
789                         n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
790                         s2n(n,p);               /* certificate length */
791                         i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
792                         n=0;
793                         
794                         /* lets send out the ciphers we like in the
795                          * prefered order */
796                         sk= s->session->ciphers;
797                         n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d);
798                         d+=n;
799                         s2n(n,p);               /* add cipher length */
800                         }
801
802                 /* make and send conn_id */
803                 s2n(SSL2_CONNECTION_ID_LENGTH,p);       /* add conn_id length */
804                 s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
805                 RAND_pseudo_bytes(s->s2->conn_id,(int)s->s2->conn_id_length);
806                 memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
807                 d+=SSL2_CONNECTION_ID_LENGTH;
808
809                 s->state=SSL2_ST_SEND_SERVER_HELLO_B;
810                 s->init_num=d-(unsigned char *)s->init_buf->data;
811                 s->init_off=0;
812                 }
813         /* SSL2_ST_SEND_SERVER_HELLO_B */
814         /* If we are using TCP/IP, the performance is bad if we do 2
815          * writes without a read between them.  This occurs when
816          * Session-id reuse is used, so I will put in a buffering module
817          */
818         if (s->hit)
819                 {
820                 if (!ssl_init_wbio_buffer(s,1)) return(-1);
821                 }
822  
823         return(ssl2_do_write(s));
824         }
825
826 static int get_client_finished(SSL *s)
827         {
828         unsigned char *p;
829         int i, n;
830         unsigned long len;
831
832         p=(unsigned char *)s->init_buf->data;
833         if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
834                 {
835                 i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
836                 if (i < 1-s->init_num)
837                         return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
838                 s->init_num += i;
839
840                 if (*p != SSL2_MT_CLIENT_FINISHED)
841                         {
842                         if (*p != SSL2_MT_ERROR)
843                                 {
844                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
845                                 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
846                                 }
847                         else
848                                 {
849                                 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
850                                 /* try to read the error message */
851                                 i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
852                                 return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
853                                 }
854                         return(-1);
855                         }
856                 s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
857                 }
858
859         /* SSL2_ST_GET_CLIENT_FINISHED_B */
860         if (s->s2->conn_id_length > sizeof s->s2->conn_id)
861                 {
862                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
863                 SSLerr(SSL_F_GET_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
864                 return -1;
865                 }
866         len = 1 + (unsigned long)s->s2->conn_id_length;
867         n = (int)len - s->init_num;
868         i = ssl2_read(s,(char *)&(p[s->init_num]),n);
869         if (i < n)
870                 {
871                 return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
872                 }
873         if (s->msg_callback)
874                 s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-FINISHED */
875         p += 1;
876         if (memcmp(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length) != 0)
877                 {
878                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
879                 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
880                 return(-1);
881                 }
882         return(1);
883         }
884
885 static int server_verify(SSL *s)
886         {
887         unsigned char *p;
888
889         if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
890                 {
891                 p=(unsigned char *)s->init_buf->data;
892                 *(p++)=SSL2_MT_SERVER_VERIFY;
893                 if (s->s2->challenge_length > sizeof s->s2->challenge)
894                         {
895                         SSLerr(SSL_F_SERVER_VERIFY, ERR_R_INTERNAL_ERROR);
896                         return -1;
897                         }
898                 memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
899                 /* p+=s->s2->challenge_length; */
900
901                 s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
902                 s->init_num=s->s2->challenge_length+1;
903                 s->init_off=0;
904                 }
905         return(ssl2_do_write(s));
906         }
907
908 static int server_finish(SSL *s)
909         {
910         unsigned char *p;
911
912         if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
913                 {
914                 p=(unsigned char *)s->init_buf->data;
915                 *(p++)=SSL2_MT_SERVER_FINISHED;
916
917                 if (s->session->session_id_length > sizeof s->session->session_id)
918                         {
919                         SSLerr(SSL_F_SERVER_FINISH, ERR_R_INTERNAL_ERROR);
920                         return -1;
921                         }
922                 memcpy(p,s->session->session_id, (unsigned int)s->session->session_id_length);
923                 /* p+=s->session->session_id_length; */
924
925                 s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
926                 s->init_num=s->session->session_id_length+1;
927                 s->init_off=0;
928                 }
929
930         /* SSL2_ST_SEND_SERVER_FINISHED_B */
931         return(ssl2_do_write(s));
932         }
933
934 /* send the request and check the response */
935 static int request_certificate(SSL *s)
936         {
937         unsigned char *p,*p2,*buf2;
938         unsigned char *ccd;
939         int i,j,ctype,ret= -1;
940         unsigned long len;
941         X509 *x509=NULL;
942         STACK_OF(X509) *sk=NULL;
943
944         ccd=s->s2->tmp.ccl;
945         if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
946                 {
947                 p=(unsigned char *)s->init_buf->data;
948                 *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
949                 *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
950                 RAND_pseudo_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
951                 memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
952
953                 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
954                 s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
955                 s->init_off=0;
956                 }
957
958         if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
959                 {
960                 i=ssl2_do_write(s);
961                 if (i <= 0)
962                         {
963                         ret=i;
964                         goto end;
965                         }
966
967                 s->init_num=0;
968                 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
969                 }
970
971         if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
972                 {
973                 p=(unsigned char *)s->init_buf->data;
974                 i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num); /* try to read 6 octets ... */
975                 if (i < 3-s->init_num) /* ... but don't call ssl2_part_read now if we got at least 3
976                                         * (probably NO-CERTIFICATE-ERROR) */
977                         {
978                         ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
979                         goto end;
980                         }
981                 s->init_num += i;
982
983                 if ((s->init_num >= 3) && (p[0] == SSL2_MT_ERROR))
984                         {
985                         n2s(p,i);
986                         if (i != SSL2_PE_NO_CERTIFICATE)
987                                 {
988                                 /* not the error message we expected -- let ssl2_part_read handle it */
989                                 s->init_num -= 3;
990                                 ret = ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE, 3);
991                                 goto end;
992                                 }
993
994                         if (s->msg_callback)
995                                 s->msg_callback(0, s->version, 0, p, 3, s, s->msg_callback_arg); /* ERROR */
996
997                         /* this is the one place where we can recover from an SSL 2.0 error */
998
999                         if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
1000                                 {
1001                                 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
1002                                 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1003                                 goto end;
1004                                 }
1005                         ret=1;
1006                         goto end;
1007                         }
1008                 if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (s->init_num < 6))
1009                         {
1010                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
1011                         SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
1012                         goto end;
1013                         }
1014                 if (s->init_num != 6)
1015                         {
1016                         SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1017                         goto end;
1018                         }
1019                 
1020                 /* ok we have a response */
1021                 /* certificate type, there is only one right now. */
1022                 ctype= *(p++);
1023                 if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
1024                         {
1025                         ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
1026                         SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
1027                         goto end;
1028                         }
1029                 n2s(p,i); s->s2->tmp.clen=i;
1030                 n2s(p,i); s->s2->tmp.rlen=i;
1031                 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
1032                 }
1033
1034         /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
1035         p=(unsigned char *)s->init_buf->data;
1036         len = 6 + (unsigned long)s->s2->tmp.clen + (unsigned long)s->s2->tmp.rlen;
1037         if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
1038                 {
1039                 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_MESSAGE_TOO_LONG);
1040                 goto end;
1041                 }
1042         j = (int)len - s->init_num;
1043         i = ssl2_read(s,(char *)&(p[s->init_num]),j);
1044         if (i < j) 
1045                 {
1046                 ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
1047                 goto end;
1048                 }
1049         if (s->msg_callback)
1050                 s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-CERTIFICATE */
1051         p += 6;
1052
1053         x509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);
1054         if (x509 == NULL)
1055                 {
1056                 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
1057                 goto msg_end;
1058                 }
1059
1060         if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))
1061                 {
1062                 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1063                 goto msg_end;
1064                 }
1065
1066         i=ssl_verify_cert_chain(s,sk);
1067
1068         if (i)  /* we like the packet, now check the chksum */
1069                 {
1070                 EVP_MD_CTX ctx;
1071                 EVP_PKEY *pkey=NULL;
1072
1073                 EVP_MD_CTX_init(&ctx);
1074                 EVP_VerifyInit_ex(&ctx,s->ctx->rsa_md5, NULL);
1075                 EVP_VerifyUpdate(&ctx,s->s2->key_material,
1076                         (unsigned int)s->s2->key_material_length);
1077                 EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
1078
1079                 i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
1080                 buf2=OPENSSL_malloc((unsigned int)i);
1081                 if (buf2 == NULL)
1082                         {
1083                         SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1084                         goto msg_end;
1085                         }
1086                 p2=buf2;
1087                 i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
1088                 EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
1089                 OPENSSL_free(buf2);
1090
1091                 pkey=X509_get_pubkey(x509);
1092                 if (pkey == NULL) goto end;
1093                 i=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);
1094                 EVP_PKEY_free(pkey);
1095                 EVP_MD_CTX_cleanup(&ctx);
1096
1097                 if (i) 
1098                         {
1099                         if (s->session->peer != NULL)
1100                                 X509_free(s->session->peer);
1101                         s->session->peer=x509;
1102                         CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
1103                         s->session->verify_result = s->verify_result;
1104                         ret=1;
1105                         goto end;
1106                         }
1107                 else
1108                         {
1109                         SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
1110                         goto msg_end;
1111                         }
1112                 }
1113         else
1114                 {
1115 msg_end:
1116                 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
1117                 }
1118 end:
1119         sk_X509_free(sk);
1120         X509_free(x509);
1121         return(ret);
1122         }
1123
1124 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
1125              unsigned char *to, int padding)
1126         {
1127         RSA *rsa;
1128         int i;
1129
1130         if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
1131                 {
1132                 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
1133                 return(-1);
1134                 }
1135         if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
1136                 {
1137                 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
1138                 return(-1);
1139                 }
1140         rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
1141
1142         /* we have the public key */
1143         i=RSA_private_decrypt(len,from,to,rsa,padding);
1144         if (i < 0)
1145                 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
1146         return(i);
1147         }
1148 #else /* !OPENSSL_NO_SSL2 */
1149
1150 # if PEDANTIC
1151 static void *dummy=&dummy;
1152 # endif
1153
1154 #endif