prepare for additional RFC3546 alerts
[openssl.git] / ssl / ssl_lib.c
1 /*! \file ssl/ssl_lib.c
2  *  \brief Version independent SSL functions.
3  */
4 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5  * All rights reserved.
6  *
7  * This package is an SSL implementation written
8  * by Eric Young (eay@cryptsoft.com).
9  * The implementation was written so as to conform with Netscapes SSL.
10  * 
11  * This library is free for commercial and non-commercial use as long as
12  * the following conditions are aheared to.  The following conditions
13  * apply to all code found in this distribution, be it the RC4, RSA,
14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15  * included with this distribution is covered by the same copyright terms
16  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17  * 
18  * Copyright remains Eric Young's, and as such any Copyright notices in
19  * the code are not to be removed.
20  * If this package is used in a product, Eric Young should be given attribution
21  * as the author of the parts of the library used.
22  * This can be in the form of a textual message at program startup or
23  * in documentation (online or textual) provided with the package.
24  * 
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. All advertising materials mentioning features or use of this software
34  *    must display the following acknowledgement:
35  *    "This product includes cryptographic software written by
36  *     Eric Young (eay@cryptsoft.com)"
37  *    The word 'cryptographic' can be left out if the rouines from the library
38  *    being used are not cryptographic related :-).
39  * 4. If you include any Windows specific code (or a derivative thereof) from 
40  *    the apps directory (application code) you must include an acknowledgement:
41  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42  * 
43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  * 
55  * The licence and distribution terms for any publically available version or
56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57  * copied and put under another distribution licence
58  * [including the GNU Public Licence.]
59  */
60 /* ====================================================================
61  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  * 1. Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer. 
69  *
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in
72  *    the documentation and/or other materials provided with the
73  *    distribution.
74  *
75  * 3. All advertising materials mentioning features or use of this
76  *    software must display the following acknowledgment:
77  *    "This product includes software developed by the OpenSSL Project
78  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79  *
80  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81  *    endorse or promote products derived from this software without
82  *    prior written permission. For written permission, please contact
83  *    openssl-core@openssl.org.
84  *
85  * 5. Products derived from this software may not be called "OpenSSL"
86  *    nor may "OpenSSL" appear in their names without prior written
87  *    permission of the OpenSSL Project.
88  *
89  * 6. Redistributions of any form whatsoever must retain the following
90  *    acknowledgment:
91  *    "This product includes software developed by the OpenSSL Project
92  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93  *
94  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105  * OF THE POSSIBILITY OF SUCH DAMAGE.
106  * ====================================================================
107  *
108  * This product includes cryptographic software written by Eric Young
109  * (eay@cryptsoft.com).  This product includes software written by Tim
110  * Hudson (tjh@cryptsoft.com).
111  *
112  */
113 /* ====================================================================
114  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115  * ECC cipher suite support in OpenSSL originally developed by 
116  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117  */
118
119 #ifdef REF_CHECK
120 #  include <assert.h>
121 #endif
122 #include <stdio.h>
123 #include "ssl_locl.h"
124 #include "kssl_lcl.h"
125 #include <openssl/objects.h>
126 #include <openssl/lhash.h>
127 #include <openssl/x509v3.h>
128 #ifndef OPENSSL_NO_DH
129 #include <openssl/dh.h>
130 #endif
131
132 const char *SSL_version_str=OPENSSL_VERSION_TEXT;
133
134 SSL3_ENC_METHOD ssl3_undef_enc_method={
135         /* evil casts, but these functions are only called if there's a library bug */
136         (int (*)(SSL *,int))ssl_undefined_function,
137         (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
138         ssl_undefined_function,
139         (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
140         (int (*)(SSL*, int))ssl_undefined_function,
141         (int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function,
142         0,      /* finish_mac_length */
143         (int (*)(SSL *, EVP_MD_CTX *, unsigned char *))ssl_undefined_function,
144         NULL,   /* client_finished_label */
145         0,      /* client_finished_label_len */
146         NULL,   /* server_finished_label */
147         0,      /* server_finished_label_len */
148         (int (*)(int))ssl_undefined_function
149         };
150
151 int SSL_clear(SSL *s)
152         {
153
154         if (s->method == NULL)
155                 {
156                 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
157                 return(0);
158                 }
159
160         if (ssl_clear_bad_session(s))
161                 {
162                 SSL_SESSION_free(s->session);
163                 s->session=NULL;
164                 }
165
166         s->error=0;
167         s->hit=0;
168         s->shutdown=0;
169
170 #if 0 /* Disabled since version 1.10 of this file (early return not
171        * needed because SSL_clear is not called when doing renegotiation) */
172         /* This is set if we are doing dynamic renegotiation so keep
173          * the old cipher.  It is sort of a SSL_clear_lite :-) */
174         if (s->new_session) return(1);
175 #else
176         if (s->new_session)
177                 {
178                 SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
179                 return 0;
180                 }
181 #endif
182
183         s->type=0;
184
185         s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
186
187         s->version=s->method->version;
188         s->client_version=s->version;
189         s->rwstate=SSL_NOTHING;
190         s->rstate=SSL_ST_READ_HEADER;
191 #if 0
192         s->read_ahead=s->ctx->read_ahead;
193 #endif
194
195         if (s->init_buf != NULL)
196                 {
197                 BUF_MEM_free(s->init_buf);
198                 s->init_buf=NULL;
199                 }
200
201         ssl_clear_cipher_ctx(s);
202
203         s->first_packet=0;
204
205 #if 1
206         /* Check to see if we were changed into a different method, if
207          * so, revert back if we are not doing session-id reuse. */
208         if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
209                 {
210                 s->method->ssl_free(s);
211                 s->method=s->ctx->method;
212                 if (!s->method->ssl_new(s))
213                         return(0);
214                 }
215         else
216 #endif
217                 s->method->ssl_clear(s);
218         return(1);
219         }
220
221 /** Used to change an SSL_CTXs default SSL method type */
222 int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
223         {
224         STACK_OF(SSL_CIPHER) *sk;
225
226         ctx->method=meth;
227
228         sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
229                 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
230         if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
231                 {
232                 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
233                 return(0);
234                 }
235         return(1);
236         }
237
238 SSL *SSL_new(SSL_CTX *ctx)
239         {
240         SSL *s;
241
242         if (ctx == NULL)
243                 {
244                 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
245                 return(NULL);
246                 }
247         if (ctx->method == NULL)
248                 {
249                 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
250                 return(NULL);
251                 }
252
253         s=(SSL *)OPENSSL_malloc(sizeof(SSL));
254         if (s == NULL) goto err;
255         memset(s,0,sizeof(SSL));
256
257 #ifndef OPENSSL_NO_KRB5
258         s->kssl_ctx = kssl_ctx_new();
259 #endif  /* OPENSSL_NO_KRB5 */
260
261         s->options=ctx->options;
262         s->mode=ctx->mode;
263         s->max_cert_list=ctx->max_cert_list;
264
265         if (ctx->cert != NULL)
266                 {
267                 /* Earlier library versions used to copy the pointer to
268                  * the CERT, not its contents; only when setting new
269                  * parameters for the per-SSL copy, ssl_cert_new would be
270                  * called (and the direct reference to the per-SSL_CTX
271                  * settings would be lost, but those still were indirectly
272                  * accessed for various purposes, and for that reason they
273                  * used to be known as s->ctx->default_cert).
274                  * Now we don't look at the SSL_CTX's CERT after having
275                  * duplicated it once. */
276
277                 s->cert = ssl_cert_dup(ctx->cert);
278                 if (s->cert == NULL)
279                         goto err;
280                 }
281         else
282                 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
283
284         s->read_ahead=ctx->read_ahead;
285         s->msg_callback=ctx->msg_callback;
286         s->msg_callback_arg=ctx->msg_callback_arg;
287         s->verify_mode=ctx->verify_mode;
288 #if 0
289         s->verify_depth=ctx->verify_depth;
290 #endif
291         s->sid_ctx_length=ctx->sid_ctx_length;
292         OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
293         memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
294         s->verify_callback=ctx->default_verify_callback;
295         s->generate_session_id=ctx->generate_session_id;
296
297         s->param = X509_VERIFY_PARAM_new();
298         if (!s->param)
299                 goto err;
300         X509_VERIFY_PARAM_inherit(s->param, ctx->param);
301 #if 0
302         s->purpose = ctx->purpose;
303         s->trust = ctx->trust;
304 #endif
305         s->quiet_shutdown=ctx->quiet_shutdown;
306         s->max_send_fragment = ctx->max_send_fragment;
307
308         CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
309         s->ctx=ctx;
310 #ifndef OPENSSL_NO_TLSEXT
311         CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
312         s->initial_ctx=ctx;
313 #endif
314
315         s->verify_result=X509_V_OK;
316
317         s->method=ctx->method;
318
319         if (!s->method->ssl_new(s))
320                 goto err;
321
322         s->references=1;
323         s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
324
325         SSL_clear(s);
326
327         CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
328
329         return(s);
330 err:
331         if (s != NULL)
332                 {
333                 if (s->cert != NULL)
334                         ssl_cert_free(s->cert);
335                 if (s->ctx != NULL)
336                         SSL_CTX_free(s->ctx); /* decrement reference count */
337                 OPENSSL_free(s);
338                 }
339         SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
340         return(NULL);
341         }
342
343 int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
344                                    unsigned int sid_ctx_len)
345     {
346     if(sid_ctx_len > sizeof ctx->sid_ctx)
347         {
348         SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
349         return 0;
350         }
351     ctx->sid_ctx_length=sid_ctx_len;
352     memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
353
354     return 1;
355     }
356
357 int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
358                                unsigned int sid_ctx_len)
359     {
360     if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
361         {
362         SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
363         return 0;
364         }
365     ssl->sid_ctx_length=sid_ctx_len;
366     memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
367
368     return 1;
369     }
370
371 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
372         {
373         CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
374         ctx->generate_session_id = cb;
375         CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
376         return 1;
377         }
378
379 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
380         {
381         CRYPTO_w_lock(CRYPTO_LOCK_SSL);
382         ssl->generate_session_id = cb;
383         CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
384         return 1;
385         }
386
387 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
388                                 unsigned int id_len)
389         {
390         /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
391          * we can "construct" a session to give us the desired check - ie. to
392          * find if there's a session in the hash table that would conflict with
393          * any new session built out of this id/id_len and the ssl_version in
394          * use by this SSL. */
395         SSL_SESSION r, *p;
396
397         if(id_len > sizeof r.session_id)
398                 return 0;
399
400         r.ssl_version = ssl->version;
401         r.session_id_length = id_len;
402         memcpy(r.session_id, id, id_len);
403         /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
404          * callback is calling us to check the uniqueness of a shorter ID, it
405          * must be compared as a padded-out ID because that is what it will be
406          * converted to when the callback has finished choosing it. */
407         if((r.ssl_version == SSL2_VERSION) &&
408                         (id_len < SSL2_SSL_SESSION_ID_LENGTH))
409                 {
410                 memset(r.session_id + id_len, 0,
411                         SSL2_SSL_SESSION_ID_LENGTH - id_len);
412                 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
413                 }
414
415         CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
416         p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);
417         CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
418         return (p != NULL);
419         }
420
421 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
422         {
423         return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
424         }
425
426 int SSL_set_purpose(SSL *s, int purpose)
427         {
428         return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
429         }
430
431 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
432         {
433         return X509_VERIFY_PARAM_set_trust(s->param, trust);
434         }
435
436 int SSL_set_trust(SSL *s, int trust)
437         {
438         return X509_VERIFY_PARAM_set_trust(s->param, trust);
439         }
440
441 void SSL_free(SSL *s)
442         {
443         int i;
444
445         if(s == NULL)
446             return;
447
448         i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
449 #ifdef REF_PRINT
450         REF_PRINT("SSL",s);
451 #endif
452         if (i > 0) return;
453 #ifdef REF_CHECK
454         if (i < 0)
455                 {
456                 fprintf(stderr,"SSL_free, bad reference count\n");
457                 abort(); /* ok */
458                 }
459 #endif
460
461         if (s->param)
462                 X509_VERIFY_PARAM_free(s->param);
463
464         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
465
466         if (s->bbio != NULL)
467                 {
468                 /* If the buffering BIO is in place, pop it off */
469                 if (s->bbio == s->wbio)
470                         {
471                         s->wbio=BIO_pop(s->wbio);
472                         }
473                 BIO_free(s->bbio);
474                 s->bbio=NULL;
475                 }
476         if (s->rbio != NULL)
477                 BIO_free_all(s->rbio);
478         if ((s->wbio != NULL) && (s->wbio != s->rbio))
479                 BIO_free_all(s->wbio);
480
481         if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
482
483         /* add extra stuff */
484         if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
485         if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
486
487         /* Make the next call work :-) */
488         if (s->session != NULL)
489                 {
490                 ssl_clear_bad_session(s);
491                 SSL_SESSION_free(s->session);
492                 }
493
494         ssl_clear_cipher_ctx(s);
495
496         if (s->cert != NULL) ssl_cert_free(s->cert);
497         /* Free up if allocated */
498
499         if (s->ctx) SSL_CTX_free(s->ctx);
500 #ifndef OPENSSL_NO_TLSEXT
501         if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
502 #endif
503
504         if (s->client_CA != NULL)
505                 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
506
507         if (s->method != NULL) s->method->ssl_free(s);
508
509 #ifndef OPENSSL_NO_KRB5
510         if (s->kssl_ctx != NULL)
511                 kssl_ctx_free(s->kssl_ctx);
512 #endif  /* OPENSSL_NO_KRB5 */
513
514         OPENSSL_free(s);
515         }
516
517 void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
518         {
519         /* If the output buffering BIO is still in place, remove it
520          */
521         if (s->bbio != NULL)
522                 {
523                 if (s->wbio == s->bbio)
524                         {
525                         s->wbio=s->wbio->next_bio;
526                         s->bbio->next_bio=NULL;
527                         }
528                 }
529         if ((s->rbio != NULL) && (s->rbio != rbio))
530                 BIO_free_all(s->rbio);
531         if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
532                 BIO_free_all(s->wbio);
533         s->rbio=rbio;
534         s->wbio=wbio;
535         }
536
537 BIO *SSL_get_rbio(const SSL *s)
538         { return(s->rbio); }
539
540 BIO *SSL_get_wbio(const SSL *s)
541         { return(s->wbio); }
542
543 int SSL_get_fd(const SSL *s)
544         {
545         return(SSL_get_rfd(s));
546         }
547
548 int SSL_get_rfd(const SSL *s)
549         {
550         int ret= -1;
551         BIO *b,*r;
552
553         b=SSL_get_rbio(s);
554         r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
555         if (r != NULL)
556                 BIO_get_fd(r,&ret);
557         return(ret);
558         }
559
560 int SSL_get_wfd(const SSL *s)
561         {
562         int ret= -1;
563         BIO *b,*r;
564
565         b=SSL_get_wbio(s);
566         r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
567         if (r != NULL)
568                 BIO_get_fd(r,&ret);
569         return(ret);
570         }
571
572 #ifndef OPENSSL_NO_SOCK
573 int SSL_set_fd(SSL *s,int fd)
574         {
575         int ret=0;
576         BIO *bio=NULL;
577
578         bio=BIO_new(BIO_s_socket());
579
580         if (bio == NULL)
581                 {
582                 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
583                 goto err;
584                 }
585         BIO_set_fd(bio,fd,BIO_NOCLOSE);
586         SSL_set_bio(s,bio,bio);
587         ret=1;
588 err:
589         return(ret);
590         }
591
592 int SSL_set_wfd(SSL *s,int fd)
593         {
594         int ret=0;
595         BIO *bio=NULL;
596
597         if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
598                 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
599                 {
600                 bio=BIO_new(BIO_s_socket());
601
602                 if (bio == NULL)
603                         { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
604                 BIO_set_fd(bio,fd,BIO_NOCLOSE);
605                 SSL_set_bio(s,SSL_get_rbio(s),bio);
606                 }
607         else
608                 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
609         ret=1;
610 err:
611         return(ret);
612         }
613
614 int SSL_set_rfd(SSL *s,int fd)
615         {
616         int ret=0;
617         BIO *bio=NULL;
618
619         if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
620                 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
621                 {
622                 bio=BIO_new(BIO_s_socket());
623
624                 if (bio == NULL)
625                         {
626                         SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
627                         goto err;
628                         }
629                 BIO_set_fd(bio,fd,BIO_NOCLOSE);
630                 SSL_set_bio(s,bio,SSL_get_wbio(s));
631                 }
632         else
633                 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
634         ret=1;
635 err:
636         return(ret);
637         }
638 #endif
639
640
641 /* return length of latest Finished message we sent, copy to 'buf' */
642 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
643         {
644         size_t ret = 0;
645         
646         if (s->s3 != NULL)
647                 {
648                 ret = s->s3->tmp.finish_md_len;
649                 if (count > ret)
650                         count = ret;
651                 memcpy(buf, s->s3->tmp.finish_md, count);
652                 }
653         return ret;
654         }
655
656 /* return length of latest Finished message we expected, copy to 'buf' */
657 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
658         {
659         size_t ret = 0;
660         
661         if (s->s3 != NULL)
662                 {
663                 ret = s->s3->tmp.peer_finish_md_len;
664                 if (count > ret)
665                         count = ret;
666                 memcpy(buf, s->s3->tmp.peer_finish_md, count);
667                 }
668         return ret;
669         }
670
671
672 int SSL_get_verify_mode(const SSL *s)
673         {
674         return(s->verify_mode);
675         }
676
677 int SSL_get_verify_depth(const SSL *s)
678         {
679         return X509_VERIFY_PARAM_get_depth(s->param);
680         }
681
682 int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
683         {
684         return(s->verify_callback);
685         }
686
687 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
688         {
689         return(ctx->verify_mode);
690         }
691
692 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
693         {
694         return X509_VERIFY_PARAM_get_depth(ctx->param);
695         }
696
697 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
698         {
699         return(ctx->default_verify_callback);
700         }
701
702 void SSL_set_verify(SSL *s,int mode,
703                     int (*callback)(int ok,X509_STORE_CTX *ctx))
704         {
705         s->verify_mode=mode;
706         if (callback != NULL)
707                 s->verify_callback=callback;
708         }
709
710 void SSL_set_verify_depth(SSL *s,int depth)
711         {
712         X509_VERIFY_PARAM_set_depth(s->param, depth);
713         }
714
715 void SSL_set_read_ahead(SSL *s,int yes)
716         {
717         s->read_ahead=yes;
718         }
719
720 int SSL_get_read_ahead(const SSL *s)
721         {
722         return(s->read_ahead);
723         }
724
725 int SSL_pending(const SSL *s)
726         {
727         /* SSL_pending cannot work properly if read-ahead is enabled
728          * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
729          * and it is impossible to fix since SSL_pending cannot report
730          * errors that may be observed while scanning the new data.
731          * (Note that SSL_pending() is often used as a boolean value,
732          * so we'd better not return -1.)
733          */
734         return(s->method->ssl_pending(s));
735         }
736
737 X509 *SSL_get_peer_certificate(const SSL *s)
738         {
739         X509 *r;
740         
741         if ((s == NULL) || (s->session == NULL))
742                 r=NULL;
743         else
744                 r=s->session->peer;
745
746         if (r == NULL) return(r);
747
748         CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
749
750         return(r);
751         }
752
753 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
754         {
755         STACK_OF(X509) *r;
756         
757         if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
758                 r=NULL;
759         else
760                 r=s->session->sess_cert->cert_chain;
761
762         /* If we are a client, cert_chain includes the peer's own
763          * certificate; if we are a server, it does not. */
764         
765         return(r);
766         }
767
768 /* Now in theory, since the calling process own 't' it should be safe to
769  * modify.  We need to be able to read f without being hassled */
770 void SSL_copy_session_id(SSL *t,const SSL *f)
771         {
772         CERT *tmp;
773
774         /* Do we need to to SSL locking? */
775         SSL_set_session(t,SSL_get_session(f));
776
777         /* what if we are setup as SSLv2 but want to talk SSLv3 or
778          * vice-versa */
779         if (t->method != f->method)
780                 {
781                 t->method->ssl_free(t); /* cleanup current */
782                 t->method=f->method;    /* change method */
783                 t->method->ssl_new(t);  /* setup new */
784                 }
785
786         tmp=t->cert;
787         if (f->cert != NULL)
788                 {
789                 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
790                 t->cert=f->cert;
791                 }
792         else
793                 t->cert=NULL;
794         if (tmp != NULL) ssl_cert_free(tmp);
795         SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
796         }
797
798 /* Fix this so it checks all the valid key/cert options */
799 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
800         {
801         if (    (ctx == NULL) ||
802                 (ctx->cert == NULL) ||
803                 (ctx->cert->key->x509 == NULL))
804                 {
805                 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
806                 return(0);
807                 }
808         if      (ctx->cert->key->privatekey == NULL)
809                 {
810                 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
811                 return(0);
812                 }
813         return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
814         }
815
816 /* Fix this function so that it takes an optional type parameter */
817 int SSL_check_private_key(const SSL *ssl)
818         {
819         if (ssl == NULL)
820                 {
821                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
822                 return(0);
823                 }
824         if (ssl->cert == NULL)
825                 {
826                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
827                 return 0;
828                 }
829         if (ssl->cert->key->x509 == NULL)
830                 {
831                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
832                 return(0);
833                 }
834         if (ssl->cert->key->privatekey == NULL)
835                 {
836                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
837                 return(0);
838                 }
839         return(X509_check_private_key(ssl->cert->key->x509,
840                 ssl->cert->key->privatekey));
841         }
842
843 int SSL_accept(SSL *s)
844         {
845         if (s->handshake_func == 0)
846                 /* Not properly initialized yet */
847                 SSL_set_accept_state(s);
848
849         return(s->method->ssl_accept(s));
850         }
851
852 int SSL_connect(SSL *s)
853         {
854         if (s->handshake_func == 0)
855                 /* Not properly initialized yet */
856                 SSL_set_connect_state(s);
857
858         return(s->method->ssl_connect(s));
859         }
860
861 long SSL_get_default_timeout(const SSL *s)
862         {
863         return(s->method->get_timeout());
864         }
865
866 int SSL_read(SSL *s,void *buf,int num)
867         {
868         if (s->handshake_func == 0)
869                 {
870                 SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
871                 return -1;
872                 }
873
874         if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
875                 {
876                 s->rwstate=SSL_NOTHING;
877                 return(0);
878                 }
879         return(s->method->ssl_read(s,buf,num));
880         }
881
882 int SSL_peek(SSL *s,void *buf,int num)
883         {
884         if (s->handshake_func == 0)
885                 {
886                 SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
887                 return -1;
888                 }
889
890         if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
891                 {
892                 return(0);
893                 }
894         return(s->method->ssl_peek(s,buf,num));
895         }
896
897 int SSL_write(SSL *s,const void *buf,int num)
898         {
899         if (s->handshake_func == 0)
900                 {
901                 SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
902                 return -1;
903                 }
904
905         if (s->shutdown & SSL_SENT_SHUTDOWN)
906                 {
907                 s->rwstate=SSL_NOTHING;
908                 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
909                 return(-1);
910                 }
911         return(s->method->ssl_write(s,buf,num));
912         }
913
914 int SSL_shutdown(SSL *s)
915         {
916         /* Note that this function behaves differently from what one might
917          * expect.  Return values are 0 for no success (yet),
918          * 1 for success; but calling it once is usually not enough,
919          * even if blocking I/O is used (see ssl3_shutdown).
920          */
921
922         if (s->handshake_func == 0)
923                 {
924                 SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
925                 return -1;
926                 }
927
928         if ((s != NULL) && !SSL_in_init(s))
929                 return(s->method->ssl_shutdown(s));
930         else
931                 return(1);
932         }
933
934 int SSL_renegotiate(SSL *s)
935         {
936         if (s->new_session == 0)
937                 {
938                 s->new_session=1;
939                 }
940         return(s->method->ssl_renegotiate(s));
941         }
942
943 int SSL_renegotiate_pending(SSL *s)
944         {
945         /* becomes true when negotiation is requested;
946          * false again once a handshake has finished */
947         return (s->new_session != 0);
948         }
949
950 long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
951         {
952         long l;
953
954         switch (cmd)
955                 {
956         case SSL_CTRL_GET_READ_AHEAD:
957                 return(s->read_ahead);
958         case SSL_CTRL_SET_READ_AHEAD:
959                 l=s->read_ahead;
960                 s->read_ahead=larg;
961                 return(l);
962
963         case SSL_CTRL_SET_MSG_CALLBACK_ARG:
964                 s->msg_callback_arg = parg;
965                 return 1;
966
967         case SSL_CTRL_OPTIONS:
968                 return(s->options|=larg);
969         case SSL_CTRL_MODE:
970                 return(s->mode|=larg);
971         case SSL_CTRL_GET_MAX_CERT_LIST:
972                 return(s->max_cert_list);
973         case SSL_CTRL_SET_MAX_CERT_LIST:
974                 l=s->max_cert_list;
975                 s->max_cert_list=larg;
976                 return(l);
977         case SSL_CTRL_SET_MTU:
978                 if (SSL_version(s) == DTLS1_VERSION)
979                         {
980                         s->d1->mtu = larg;
981                         return larg;
982                         }
983                 return 0;
984         case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
985                 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
986                         return 0;
987                 s->max_send_fragment = larg;
988                 return 1;
989         default:
990                 return(s->method->ssl_ctrl(s,cmd,larg,parg));
991                 }
992         }
993
994 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
995         {
996         switch(cmd)
997                 {
998         case SSL_CTRL_SET_MSG_CALLBACK:
999                 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1000                 return 1;
1001                 
1002         default:
1003                 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1004                 }
1005         }
1006
1007 struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx)
1008         {
1009         return ctx->sessions;
1010         }
1011
1012 long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1013         {
1014         long l;
1015
1016         switch (cmd)
1017                 {
1018         case SSL_CTRL_GET_READ_AHEAD:
1019                 return(ctx->read_ahead);
1020         case SSL_CTRL_SET_READ_AHEAD:
1021                 l=ctx->read_ahead;
1022                 ctx->read_ahead=larg;
1023                 return(l);
1024                 
1025         case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1026                 ctx->msg_callback_arg = parg;
1027                 return 1;
1028
1029         case SSL_CTRL_GET_MAX_CERT_LIST:
1030                 return(ctx->max_cert_list);
1031         case SSL_CTRL_SET_MAX_CERT_LIST:
1032                 l=ctx->max_cert_list;
1033                 ctx->max_cert_list=larg;
1034                 return(l);
1035
1036         case SSL_CTRL_SET_SESS_CACHE_SIZE:
1037                 l=ctx->session_cache_size;
1038                 ctx->session_cache_size=larg;
1039                 return(l);
1040         case SSL_CTRL_GET_SESS_CACHE_SIZE:
1041                 return(ctx->session_cache_size);
1042         case SSL_CTRL_SET_SESS_CACHE_MODE:
1043                 l=ctx->session_cache_mode;
1044                 ctx->session_cache_mode=larg;
1045                 return(l);
1046         case SSL_CTRL_GET_SESS_CACHE_MODE:
1047                 return(ctx->session_cache_mode);
1048
1049         case SSL_CTRL_SESS_NUMBER:
1050                 return(ctx->sessions->num_items);
1051         case SSL_CTRL_SESS_CONNECT:
1052                 return(ctx->stats.sess_connect);
1053         case SSL_CTRL_SESS_CONNECT_GOOD:
1054                 return(ctx->stats.sess_connect_good);
1055         case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1056                 return(ctx->stats.sess_connect_renegotiate);
1057         case SSL_CTRL_SESS_ACCEPT:
1058                 return(ctx->stats.sess_accept);
1059         case SSL_CTRL_SESS_ACCEPT_GOOD:
1060                 return(ctx->stats.sess_accept_good);
1061         case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1062                 return(ctx->stats.sess_accept_renegotiate);
1063         case SSL_CTRL_SESS_HIT:
1064                 return(ctx->stats.sess_hit);
1065         case SSL_CTRL_SESS_CB_HIT:
1066                 return(ctx->stats.sess_cb_hit);
1067         case SSL_CTRL_SESS_MISSES:
1068                 return(ctx->stats.sess_miss);
1069         case SSL_CTRL_SESS_TIMEOUTS:
1070                 return(ctx->stats.sess_timeout);
1071         case SSL_CTRL_SESS_CACHE_FULL:
1072                 return(ctx->stats.sess_cache_full);
1073         case SSL_CTRL_OPTIONS:
1074                 return(ctx->options|=larg);
1075         case SSL_CTRL_MODE:
1076                 return(ctx->mode|=larg);
1077         case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1078                 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1079                         return 0;
1080                 ctx->max_send_fragment = larg;
1081                 return 1;
1082         default:
1083                 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1084                 }
1085         }
1086
1087 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1088         {
1089         switch(cmd)
1090                 {
1091         case SSL_CTRL_SET_MSG_CALLBACK:
1092                 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1093                 return 1;
1094
1095         default:
1096                 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1097                 }
1098         }
1099
1100 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1101         {
1102         long l;
1103
1104         l=a->id-b->id;
1105         if (l == 0L)
1106                 return(0);
1107         else
1108                 return((l > 0)?1:-1);
1109         }
1110
1111 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
1112                         const SSL_CIPHER * const *bp)
1113         {
1114         long l;
1115
1116         l=(*ap)->id-(*bp)->id;
1117         if (l == 0L)
1118                 return(0);
1119         else
1120                 return((l > 0)?1:-1);
1121         }
1122
1123 /** return a STACK of the ciphers available for the SSL and in order of
1124  * preference */
1125 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1126         {
1127         if (s != NULL)
1128                 {
1129                 if (s->cipher_list != NULL)
1130                         {
1131                         return(s->cipher_list);
1132                         }
1133                 else if ((s->ctx != NULL) &&
1134                         (s->ctx->cipher_list != NULL))
1135                         {
1136                         return(s->ctx->cipher_list);
1137                         }
1138                 }
1139         return(NULL);
1140         }
1141
1142 /** return a STACK of the ciphers available for the SSL and in order of
1143  * algorithm id */
1144 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1145         {
1146         if (s != NULL)
1147                 {
1148                 if (s->cipher_list_by_id != NULL)
1149                         {
1150                         return(s->cipher_list_by_id);
1151                         }
1152                 else if ((s->ctx != NULL) &&
1153                         (s->ctx->cipher_list_by_id != NULL))
1154                         {
1155                         return(s->ctx->cipher_list_by_id);
1156                         }
1157                 }
1158         return(NULL);
1159         }
1160
1161 /** The old interface to get the same thing as SSL_get_ciphers() */
1162 const char *SSL_get_cipher_list(const SSL *s,int n)
1163         {
1164         SSL_CIPHER *c;
1165         STACK_OF(SSL_CIPHER) *sk;
1166
1167         if (s == NULL) return(NULL);
1168         sk=SSL_get_ciphers(s);
1169         if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1170                 return(NULL);
1171         c=sk_SSL_CIPHER_value(sk,n);
1172         if (c == NULL) return(NULL);
1173         return(c->name);
1174         }
1175
1176 /** specify the ciphers to be used by default by the SSL_CTX */
1177 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1178         {
1179         STACK_OF(SSL_CIPHER) *sk;
1180         
1181         sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1182                 &ctx->cipher_list_by_id,str);
1183         /* ssl_create_cipher_list may return an empty stack if it
1184          * was unable to find a cipher matching the given rule string
1185          * (for example if the rule string specifies a cipher which
1186          * has been disabled). This is not an error as far as 
1187          * ssl_create_cipher_list is concerned, and hence 
1188          * ctx->cipher_list and ctx->cipher_list_by_id has been
1189          * updated. */
1190         if (sk == NULL)
1191                 return 0;
1192         else if (sk_SSL_CIPHER_num(sk) == 0)
1193                 {
1194                 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1195                 return 0;
1196                 }
1197         return 1;
1198         }
1199
1200 /** specify the ciphers to be used by the SSL */
1201 int SSL_set_cipher_list(SSL *s,const char *str)
1202         {
1203         STACK_OF(SSL_CIPHER) *sk;
1204         
1205         sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1206                 &s->cipher_list_by_id,str);
1207         /* see comment in SSL_CTX_set_cipher_list */
1208         if (sk == NULL)
1209                 return 0;
1210         else if (sk_SSL_CIPHER_num(sk) == 0)
1211                 {
1212                 SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1213                 return 0;
1214                 }
1215         return 1;
1216         }
1217
1218 /* works well for SSLv2, not so good for SSLv3 */
1219 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1220         {
1221         char *p;
1222         const char *cp;
1223         STACK_OF(SSL_CIPHER) *sk;
1224         SSL_CIPHER *c;
1225         int i;
1226
1227         if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1228                 (len < 2))
1229                 return(NULL);
1230
1231         p=buf;
1232         sk=s->session->ciphers;
1233         for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1234                 {
1235                 /* Decrement for either the ':' or a '\0' */
1236                 len--;
1237                 c=sk_SSL_CIPHER_value(sk,i);
1238                 for (cp=c->name; *cp; )
1239                         {
1240                         if (len-- == 0)
1241                                 {
1242                                 *p='\0';
1243                                 return(buf);
1244                                 }
1245                         else
1246                                 *(p++)= *(cp++);
1247                         }
1248                 *(p++)=':';
1249                 }
1250         p[-1]='\0';
1251         return(buf);
1252         }
1253
1254 int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1255                              int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1256         {
1257         int i,j=0;
1258         SSL_CIPHER *c;
1259         unsigned char *q;
1260 #ifndef OPENSSL_NO_KRB5
1261         int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1262 #endif /* OPENSSL_NO_KRB5 */
1263
1264         if (sk == NULL) return(0);
1265         q=p;
1266
1267         for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1268                 {
1269                 c=sk_SSL_CIPHER_value(sk,i);
1270 #ifndef OPENSSL_NO_KRB5
1271                 if ((c->algorithms & SSL_KRB5) && nokrb5)
1272                     continue;
1273 #endif /* OPENSSL_NO_KRB5 */                    
1274
1275                 j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
1276                 p+=j;
1277                 }
1278         return(p-q);
1279         }
1280
1281 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1282                                                STACK_OF(SSL_CIPHER) **skp)
1283         {
1284         SSL_CIPHER *c;
1285         STACK_OF(SSL_CIPHER) *sk;
1286         int i,n;
1287
1288         n=ssl_put_cipher_by_char(s,NULL,NULL);
1289         if ((num%n) != 0)
1290                 {
1291                 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1292                 return(NULL);
1293                 }
1294         if ((skp == NULL) || (*skp == NULL))
1295                 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1296         else
1297                 {
1298                 sk= *skp;
1299                 sk_SSL_CIPHER_zero(sk);
1300                 }
1301
1302         for (i=0; i<num; i+=n)
1303                 {
1304                 c=ssl_get_cipher_by_char(s,p);
1305                 p+=n;
1306                 if (c != NULL)
1307                         {
1308                         if (!sk_SSL_CIPHER_push(sk,c))
1309                                 {
1310                                 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
1311                                 goto err;
1312                                 }
1313                         }
1314                 }
1315
1316         if (skp != NULL)
1317                 *skp=sk;
1318         return(sk);
1319 err:
1320         if ((skp == NULL) || (*skp == NULL))
1321                 sk_SSL_CIPHER_free(sk);
1322         return(NULL);
1323         }
1324
1325
1326 #ifndef OPENSSL_TLSEXT
1327 /** return a servername extension value if provided in Client Hello, or NULL. 
1328  * So far, only host_name types are defined (RFC 3546).
1329  */
1330
1331 const char *SSL_get_servername(const SSL *s, const int type)
1332         {
1333         if (type != TLSEXT_NAMETYPE_host_name)
1334                 return NULL;
1335         /* XXX cf. SSL_CTRL_GET_TLSEXT_HOSTNAME case in ssl3_ctrl (s3_lib.c) */
1336         return s->session /*&&s->session->tlsext_hostname*/ ?
1337                 s->session->tlsext_hostname :
1338                 s->tlsext_hostname;
1339         }
1340
1341 int SSL_get_servername_type(const SSL *s)
1342         {
1343         if (s->session &&s->session->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname) 
1344                 return TLSEXT_NAMETYPE_host_name;
1345         return -1;
1346         }
1347 #endif
1348
1349 unsigned long SSL_SESSION_hash(const SSL_SESSION *a)
1350         {
1351         unsigned long l;
1352
1353         l=(unsigned long)
1354                 ((unsigned int) a->session_id[0]     )|
1355                 ((unsigned int) a->session_id[1]<< 8L)|
1356                 ((unsigned long)a->session_id[2]<<16L)|
1357                 ((unsigned long)a->session_id[3]<<24L);
1358         return(l);
1359         }
1360
1361 /* NB: If this function (or indeed the hash function which uses a sort of
1362  * coarser function than this one) is changed, ensure
1363  * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1364  * able to construct an SSL_SESSION that will collide with any existing session
1365  * with a matching session ID. */
1366 int SSL_SESSION_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
1367         {
1368         if (a->ssl_version != b->ssl_version)
1369                 return(1);
1370         if (a->session_id_length != b->session_id_length)
1371                 return(1);
1372         return(memcmp(a->session_id,b->session_id,a->session_id_length));
1373         }
1374
1375 /* These wrapper functions should remain rather than redeclaring
1376  * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1377  * variable. The reason is that the functions aren't static, they're exposed via
1378  * ssl.h. */
1379 static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
1380 static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)
1381
1382 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1383         {
1384         SSL_CTX *ret=NULL;
1385         
1386         if (meth == NULL)
1387                 {
1388                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
1389                 return(NULL);
1390                 }
1391
1392         if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1393                 {
1394                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1395                 goto err;
1396                 }
1397         ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1398         if (ret == NULL)
1399                 goto err;
1400
1401         memset(ret,0,sizeof(SSL_CTX));
1402
1403         ret->method=meth;
1404
1405         ret->cert_store=NULL;
1406         ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1407         ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1408         ret->session_cache_head=NULL;
1409         ret->session_cache_tail=NULL;
1410
1411         /* We take the system default */
1412         ret->session_timeout=meth->get_timeout();
1413
1414         ret->new_session_cb=0;
1415         ret->remove_session_cb=0;
1416         ret->get_session_cb=0;
1417         ret->generate_session_id=0;
1418
1419         memset((char *)&ret->stats,0,sizeof(ret->stats));
1420
1421         ret->references=1;
1422         ret->quiet_shutdown=0;
1423
1424 /*      ret->cipher=NULL;*/
1425 /*      ret->s2->challenge=NULL;
1426         ret->master_key=NULL;
1427         ret->key_arg=NULL;
1428         ret->s2->conn_id=NULL; */
1429
1430         ret->info_callback=NULL;
1431
1432         ret->app_verify_callback=0;
1433         ret->app_verify_arg=NULL;
1434
1435         ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1436         ret->read_ahead=0;
1437         ret->msg_callback=0;
1438         ret->msg_callback_arg=NULL;
1439         ret->verify_mode=SSL_VERIFY_NONE;
1440 #if 0
1441         ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1442 #endif
1443         ret->sid_ctx_length=0;
1444         ret->default_verify_callback=NULL;
1445         if ((ret->cert=ssl_cert_new()) == NULL)
1446                 goto err;
1447
1448         ret->default_passwd_callback=0;
1449         ret->default_passwd_callback_userdata=NULL;
1450         ret->client_cert_cb=0;
1451         ret->app_gen_cookie_cb=0;
1452         ret->app_verify_cookie_cb=0;
1453
1454         ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),
1455                         LHASH_COMP_FN(SSL_SESSION_cmp));
1456         if (ret->sessions == NULL) goto err;
1457         ret->cert_store=X509_STORE_new();
1458         if (ret->cert_store == NULL) goto err;
1459
1460         ssl_create_cipher_list(ret->method,
1461                 &ret->cipher_list,&ret->cipher_list_by_id,
1462                 SSL_DEFAULT_CIPHER_LIST);
1463         if (ret->cipher_list == NULL
1464             || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
1465                 {
1466                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1467                 goto err2;
1468                 }
1469
1470         ret->param = X509_VERIFY_PARAM_new();
1471         if (!ret->param)
1472                 goto err;
1473
1474         if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1475                 {
1476                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1477                 goto err2;
1478                 }
1479         if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1480                 {
1481                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1482                 goto err2;
1483                 }
1484         if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1485                 {
1486                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1487                 goto err2;
1488                 }
1489
1490         if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1491                 goto err;
1492
1493         CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1494
1495         ret->extra_certs=NULL;
1496         ret->comp_methods=SSL_COMP_get_compression_methods();
1497
1498         ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1499
1500 #ifndef OPENSSL_NO_TLSEXT
1501         ret->tlsext_servername_callback = 0;
1502         ret->tlsext_servername_arg = NULL;
1503 #endif
1504         return(ret);
1505 err:
1506         SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1507 err2:
1508         if (ret != NULL) SSL_CTX_free(ret);
1509         return(NULL);
1510         }
1511
1512 #if 0
1513 static void SSL_COMP_free(SSL_COMP *comp)
1514     { OPENSSL_free(comp); }
1515 #endif
1516
1517 void SSL_CTX_free(SSL_CTX *a)
1518         {
1519         int i;
1520
1521         if (a == NULL) return;
1522
1523         i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1524 #ifdef REF_PRINT
1525         REF_PRINT("SSL_CTX",a);
1526 #endif
1527         if (i > 0) return;
1528 #ifdef REF_CHECK
1529         if (i < 0)
1530                 {
1531                 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1532                 abort(); /* ok */
1533                 }
1534 #endif
1535
1536         if (a->param)
1537                 X509_VERIFY_PARAM_free(a->param);
1538
1539         /*
1540          * Free internal session cache. However: the remove_cb() may reference
1541          * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1542          * after the sessions were flushed.
1543          * As the ex_data handling routines might also touch the session cache,
1544          * the most secure solution seems to be: empty (flush) the cache, then
1545          * free ex_data, then finally free the cache.
1546          * (See ticket [openssl.org #212].)
1547          */
1548         if (a->sessions != NULL)
1549                 SSL_CTX_flush_sessions(a,0);
1550
1551         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1552
1553         if (a->sessions != NULL)
1554                 lh_free(a->sessions);
1555
1556         if (a->cert_store != NULL)
1557                 X509_STORE_free(a->cert_store);
1558         if (a->cipher_list != NULL)
1559                 sk_SSL_CIPHER_free(a->cipher_list);
1560         if (a->cipher_list_by_id != NULL)
1561                 sk_SSL_CIPHER_free(a->cipher_list_by_id);
1562         if (a->cert != NULL)
1563                 ssl_cert_free(a->cert);
1564         if (a->client_CA != NULL)
1565                 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
1566         if (a->extra_certs != NULL)
1567                 sk_X509_pop_free(a->extra_certs,X509_free);
1568 #if 0 /* This should never be done, since it removes a global database */
1569         if (a->comp_methods != NULL)
1570                 sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
1571 #else
1572         a->comp_methods = NULL;
1573 #endif
1574         OPENSSL_free(a);
1575         }
1576
1577 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
1578         {
1579         ctx->default_passwd_callback=cb;
1580         }
1581
1582 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
1583         {
1584         ctx->default_passwd_callback_userdata=u;
1585         }
1586
1587 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
1588         {
1589         ctx->app_verify_callback=cb;
1590         ctx->app_verify_arg=arg;
1591         }
1592
1593 void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
1594         {
1595         ctx->verify_mode=mode;
1596         ctx->default_verify_callback=cb;
1597         }
1598
1599 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
1600         {
1601         X509_VERIFY_PARAM_set_depth(ctx->param, depth);
1602         }
1603
1604 void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
1605         {
1606         CERT_PKEY *cpk;
1607         int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1608         int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1609         int rsa_tmp_export,dh_tmp_export,kl;
1610         unsigned long mask,emask;
1611         int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
1612 #ifndef OPENSSL_NO_ECDH
1613         int have_ecdh_tmp;
1614 #endif
1615         X509 *x = NULL;
1616         EVP_PKEY *ecc_pkey = NULL;
1617         int signature_nid = 0;
1618
1619         if (c == NULL) return;
1620
1621         kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1622
1623 #ifndef OPENSSL_NO_RSA
1624         rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1625         rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1626                 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
1627 #else
1628         rsa_tmp=rsa_tmp_export=0;
1629 #endif
1630 #ifndef OPENSSL_NO_DH
1631         dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1632         dh_tmp_export=(c->dh_tmp_cb != NULL ||
1633                 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
1634 #else
1635         dh_tmp=dh_tmp_export=0;
1636 #endif
1637
1638 #ifndef OPENSSL_NO_ECDH
1639         have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
1640 #endif
1641         cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1642         rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1643         rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1644         cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1645         rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1646         cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1647         dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1648         cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1649         dh_rsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
1650         dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1651         cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1652 /* FIX THIS EAY EAY EAY */
1653         dh_dsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
1654         dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1655         cpk= &(c->pkeys[SSL_PKEY_ECC]);
1656         have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
1657         mask=0;
1658         emask=0;
1659
1660 #ifdef CIPHER_DEBUG
1661         printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1662                 rsa_tmp,rsa_tmp_export,dh_tmp,
1663                 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1664 #endif
1665
1666         if (rsa_enc || (rsa_tmp && rsa_sign))
1667                 mask|=SSL_kRSA;
1668         if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
1669                 emask|=SSL_kRSA;
1670
1671 #if 0
1672         /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1673         if (    (dh_tmp || dh_rsa || dh_dsa) && 
1674                 (rsa_enc || rsa_sign || dsa_sign))
1675                 mask|=SSL_kEDH;
1676         if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1677                 (rsa_enc || rsa_sign || dsa_sign))
1678                 emask|=SSL_kEDH;
1679 #endif
1680
1681         if (dh_tmp_export) 
1682                 emask|=SSL_kEDH;
1683
1684         if (dh_tmp)
1685                 mask|=SSL_kEDH;
1686
1687         if (dh_rsa) mask|=SSL_kDHr;
1688         if (dh_rsa_export) emask|=SSL_kDHr;
1689
1690         if (dh_dsa) mask|=SSL_kDHd;
1691         if (dh_dsa_export) emask|=SSL_kDHd;
1692
1693         if (rsa_enc || rsa_sign)
1694                 {
1695                 mask|=SSL_aRSA;
1696                 emask|=SSL_aRSA;
1697                 }
1698
1699         if (dsa_sign)
1700                 {
1701                 mask|=SSL_aDSS;
1702                 emask|=SSL_aDSS;
1703                 }
1704
1705         mask|=SSL_aNULL;
1706         emask|=SSL_aNULL;
1707
1708 #ifndef OPENSSL_NO_KRB5
1709         mask|=SSL_kKRB5|SSL_aKRB5;
1710         emask|=SSL_kKRB5|SSL_aKRB5;
1711 #endif
1712
1713         /* An ECC certificate may be usable for ECDH and/or
1714          * ECDSA cipher suites depending on the key usage extension.
1715          */
1716         if (have_ecc_cert)
1717                 {
1718                 /* This call populates extension flags (ex_flags) */
1719                 x = (c->pkeys[SSL_PKEY_ECC]).x509;
1720                 X509_check_purpose(x, -1, 0);
1721                 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1722                     (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
1723                 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
1724                     (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
1725                 ecc_pkey = X509_get_pubkey(x);
1726                 ecc_pkey_size = (ecc_pkey != NULL) ? 
1727                     EVP_PKEY_bits(ecc_pkey) : 0;
1728                 EVP_PKEY_free(ecc_pkey);
1729                 if ((x->sig_alg) && (x->sig_alg->algorithm))
1730                         signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1731 #ifndef OPENSSL_NO_ECDH
1732                 if (ecdh_ok)
1733                         {
1734                         if ((signature_nid == NID_md5WithRSAEncryption) ||
1735                             (signature_nid == NID_md4WithRSAEncryption) ||
1736                             (signature_nid == NID_md2WithRSAEncryption))
1737                                 {
1738                                 mask|=SSL_kECDH|SSL_aRSA;
1739                                 if (ecc_pkey_size <= 163)
1740                                         emask|=SSL_kECDH|SSL_aRSA;
1741                                 }
1742                         if (signature_nid == NID_ecdsa_with_SHA1)
1743                                 {
1744                                 mask|=SSL_kECDH|SSL_aECDSA;
1745                                 if (ecc_pkey_size <= 163)
1746                                         emask|=SSL_kECDH|SSL_aECDSA;
1747                                 }
1748                         }
1749 #endif
1750 #ifndef OPENSSL_NO_ECDSA
1751                 if (ecdsa_ok)
1752                         {
1753                         mask|=SSL_aECDSA;
1754                         emask|=SSL_aECDSA;
1755                         }
1756 #endif
1757                 }
1758
1759 #ifndef OPENSSL_NO_ECDH
1760         if (have_ecdh_tmp)
1761                 {
1762                 mask|=SSL_kECDHE;
1763                 emask|=SSL_kECDHE;
1764                 }
1765 #endif
1766         c->mask=mask;
1767         c->export_mask=emask;
1768         c->valid=1;
1769         }
1770
1771 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1772 #define ku_reject(x, usage) \
1773         (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
1774
1775 int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs)
1776         {
1777         unsigned long alg = cs->algorithms;
1778         EVP_PKEY *pkey = NULL;
1779         int keysize = 0;
1780         int signature_nid = 0;
1781
1782         if (SSL_C_IS_EXPORT(cs))
1783                 {
1784                 /* ECDH key length in export ciphers must be <= 163 bits */
1785                 pkey = X509_get_pubkey(x);
1786                 if (pkey == NULL) return 0;
1787                 keysize = EVP_PKEY_bits(pkey);
1788                 EVP_PKEY_free(pkey);
1789                 if (keysize > 163) return 0;
1790                 }
1791
1792         /* This call populates the ex_flags field correctly */
1793         X509_check_purpose(x, -1, 0);
1794         if ((x->sig_alg) && (x->sig_alg->algorithm))
1795                 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
1796         if (alg & SSL_kECDH) 
1797                 {
1798                 /* key usage, if present, must allow key agreement */
1799                 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
1800                         {
1801                         return 0;
1802                         }
1803                 if (alg & SSL_aECDSA) 
1804                         {
1805                         /* signature alg must be ECDSA */
1806                         if (signature_nid != NID_ecdsa_with_SHA1)
1807                                 {
1808                                 return 0;
1809                                 }
1810                         }
1811                 if (alg & SSL_aRSA)
1812                         {
1813                         /* signature alg must be RSA */
1814                         if ((signature_nid != NID_md5WithRSAEncryption) &&
1815                             (signature_nid != NID_md4WithRSAEncryption) &&
1816                             (signature_nid != NID_md2WithRSAEncryption))
1817                                 {
1818                                 return 0;
1819                                 }
1820                         }
1821                 } 
1822         else if (alg & SSL_aECDSA)
1823                 {
1824                 /* key usage, if present, must allow signing */
1825                 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
1826                         {
1827                         return 0;
1828                         }
1829                 }
1830
1831         return 1;  /* all checks are ok */
1832         }
1833
1834 /* THIS NEEDS CLEANING UP */
1835 X509 *ssl_get_server_send_cert(SSL *s)
1836         {
1837         unsigned long alg,mask,kalg;
1838         CERT *c;
1839         int i,is_export;
1840
1841         c=s->cert;
1842         ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
1843         alg=s->s3->tmp.new_cipher->algorithms;
1844         is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
1845         mask=is_export?c->export_mask:c->mask;
1846         kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1847
1848         if (kalg & SSL_kECDH)
1849                 {
1850                 /* we don't need to look at SSL_kECDHE 
1851                  * since no certificate is needed for
1852                  * anon ECDH and for authenticated
1853                  * ECDHE, the check for the auth 
1854                  * algorithm will set i correctly
1855                  * NOTE: For ECDH-RSA, we need an ECC
1856                  * not an RSA cert but for ECDHE-RSA
1857                  * we need an RSA cert. Placing the
1858                  * checks for SSL_kECDH before RSA
1859                  * checks ensures the correct cert is chosen.
1860                  */
1861                 i=SSL_PKEY_ECC;
1862                 }
1863         else if (kalg & SSL_aECDSA)
1864                 {
1865                 i=SSL_PKEY_ECC;
1866                 }
1867         else if (kalg & SSL_kDHr)
1868                 i=SSL_PKEY_DH_RSA;
1869         else if (kalg & SSL_kDHd)
1870                 i=SSL_PKEY_DH_DSA;
1871         else if (kalg & SSL_aDSS)
1872                 i=SSL_PKEY_DSA_SIGN;
1873         else if (kalg & SSL_aRSA)
1874                 {
1875                 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1876                         i=SSL_PKEY_RSA_SIGN;
1877                 else
1878                         i=SSL_PKEY_RSA_ENC;
1879                 }
1880         else if (kalg & SSL_aKRB5)
1881                 {
1882                 /* VRS something else here? */
1883                 return(NULL);
1884                 }
1885         else /* if (kalg & SSL_aNULL) */
1886                 {
1887                 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
1888                 return(NULL);
1889                 }
1890         if (c->pkeys[i].x509 == NULL) return(NULL);
1891
1892         return(c->pkeys[i].x509);
1893         }
1894
1895 EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
1896         {
1897         unsigned long alg;
1898         CERT *c;
1899
1900         alg=cipher->algorithms;
1901         c=s->cert;
1902
1903         if ((alg & SSL_aDSS) &&
1904                 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1905                 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1906         else if (alg & SSL_aRSA)
1907                 {
1908                 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1909                         return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1910                 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1911                         return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1912                 else
1913                         return(NULL);
1914                 }
1915         else if ((alg & SSL_aECDSA) &&
1916                  (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
1917                 return(c->pkeys[SSL_PKEY_ECC].privatekey);
1918         else /* if (alg & SSL_aNULL) */
1919                 {
1920                 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
1921                 return(NULL);
1922                 }
1923         }
1924
1925 void ssl_update_cache(SSL *s,int mode)
1926         {
1927         int i;
1928
1929         /* If the session_id_length is 0, we are not supposed to cache it,
1930          * and it would be rather hard to do anyway :-) */
1931         if (s->session->session_id_length == 0) return;
1932
1933         i=s->ctx->session_cache_mode;
1934         if ((i & mode) && (!s->hit)
1935                 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
1936                     || SSL_CTX_add_session(s->ctx,s->session))
1937                 && (s->ctx->new_session_cb != NULL))
1938                 {
1939                 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1940                 if (!s->ctx->new_session_cb(s,s->session))
1941                         SSL_SESSION_free(s->session);
1942                 }
1943
1944         /* auto flush every 255 connections */
1945         if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1946                 ((i & mode) == mode))
1947                 {
1948                 if (  (((mode & SSL_SESS_CACHE_CLIENT)
1949                         ?s->ctx->stats.sess_connect_good
1950                         :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
1951                         {
1952                         SSL_CTX_flush_sessions(s->ctx,(unsigned long)time(NULL));
1953                         }
1954                 }
1955         }
1956
1957 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
1958         {
1959         return(s->method);
1960         }
1961
1962 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
1963         {
1964         int conn= -1;
1965         int ret=1;
1966
1967         if (s->method != meth)
1968                 {
1969                 if (s->handshake_func != NULL)
1970                         conn=(s->handshake_func == s->method->ssl_connect);
1971
1972                 if (s->method->version == meth->version)
1973                         s->method=meth;
1974                 else
1975                         {
1976                         s->method->ssl_free(s);
1977                         s->method=meth;
1978                         ret=s->method->ssl_new(s);
1979                         }
1980
1981                 if (conn == 1)
1982                         s->handshake_func=meth->ssl_connect;
1983                 else if (conn == 0)
1984                         s->handshake_func=meth->ssl_accept;
1985                 }
1986         return(ret);
1987         }
1988
1989 int SSL_get_error(const SSL *s,int i)
1990         {
1991         int reason;
1992         unsigned long l;
1993         BIO *bio;
1994
1995         if (i > 0) return(SSL_ERROR_NONE);
1996
1997         /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1998          * etc, where we do encode the error */
1999         if ((l=ERR_peek_error()) != 0)
2000                 {
2001                 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2002                         return(SSL_ERROR_SYSCALL);
2003                 else
2004                         return(SSL_ERROR_SSL);
2005                 }
2006
2007         if ((i < 0) && SSL_want_read(s))
2008                 {
2009                 bio=SSL_get_rbio(s);
2010                 if (BIO_should_read(bio))
2011                         return(SSL_ERROR_WANT_READ);
2012                 else if (BIO_should_write(bio))
2013                         /* This one doesn't make too much sense ... We never try
2014                          * to write to the rbio, and an application program where
2015                          * rbio and wbio are separate couldn't even know what it
2016                          * should wait for.
2017                          * However if we ever set s->rwstate incorrectly
2018                          * (so that we have SSL_want_read(s) instead of
2019                          * SSL_want_write(s)) and rbio and wbio *are* the same,
2020                          * this test works around that bug; so it might be safer
2021                          * to keep it. */
2022                         return(SSL_ERROR_WANT_WRITE);
2023                 else if (BIO_should_io_special(bio))
2024                         {
2025                         reason=BIO_get_retry_reason(bio);
2026                         if (reason == BIO_RR_CONNECT)
2027                                 return(SSL_ERROR_WANT_CONNECT);
2028                         else if (reason == BIO_RR_ACCEPT)
2029                                 return(SSL_ERROR_WANT_ACCEPT);
2030                         else
2031                                 return(SSL_ERROR_SYSCALL); /* unknown */
2032                         }
2033                 }
2034
2035         if ((i < 0) && SSL_want_write(s))
2036                 {
2037                 bio=SSL_get_wbio(s);
2038                 if (BIO_should_write(bio))
2039                         return(SSL_ERROR_WANT_WRITE);
2040                 else if (BIO_should_read(bio))
2041                         /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2042                         return(SSL_ERROR_WANT_READ);
2043                 else if (BIO_should_io_special(bio))
2044                         {
2045                         reason=BIO_get_retry_reason(bio);
2046                         if (reason == BIO_RR_CONNECT)
2047                                 return(SSL_ERROR_WANT_CONNECT);
2048                         else if (reason == BIO_RR_ACCEPT)
2049                                 return(SSL_ERROR_WANT_ACCEPT);
2050                         else
2051                                 return(SSL_ERROR_SYSCALL);
2052                         }
2053                 }
2054         if ((i < 0) && SSL_want_x509_lookup(s))
2055                 {
2056                 return(SSL_ERROR_WANT_X509_LOOKUP);
2057                 }
2058
2059         if (i == 0)
2060                 {
2061                 if (s->version == SSL2_VERSION)
2062                         {
2063                         /* assume it is the socket being closed */
2064                         return(SSL_ERROR_ZERO_RETURN);
2065                         }
2066                 else
2067                         {
2068                         if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2069                                 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2070                                 return(SSL_ERROR_ZERO_RETURN);
2071                         }
2072                 }
2073         return(SSL_ERROR_SYSCALL);
2074         }
2075
2076 int SSL_do_handshake(SSL *s)
2077         {
2078         int ret=1;
2079
2080         if (s->handshake_func == NULL)
2081                 {
2082                 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
2083                 return(-1);
2084                 }
2085
2086         s->method->ssl_renegotiate_check(s);
2087
2088         if (SSL_in_init(s) || SSL_in_before(s))
2089                 {
2090                 ret=s->handshake_func(s);
2091                 }
2092         return(ret);
2093         }
2094
2095 /* For the next 2 functions, SSL_clear() sets shutdown and so
2096  * one of these calls will reset it */
2097 void SSL_set_accept_state(SSL *s)
2098         {
2099         s->server=1;
2100         s->shutdown=0;
2101         s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2102         s->handshake_func=s->method->ssl_accept;
2103         /* clear the current cipher */
2104         ssl_clear_cipher_ctx(s);
2105         }
2106
2107 void SSL_set_connect_state(SSL *s)
2108         {
2109         s->server=0;
2110         s->shutdown=0;
2111         s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2112         s->handshake_func=s->method->ssl_connect;
2113         /* clear the current cipher */
2114         ssl_clear_cipher_ctx(s);
2115         }
2116
2117 int ssl_undefined_function(SSL *s)
2118         {
2119         SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2120         return(0);
2121         }
2122
2123 int ssl_undefined_void_function(void)
2124         {
2125         SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2126         return(0);
2127         }
2128
2129 int ssl_undefined_const_function(const SSL *s)
2130         {
2131         SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2132         return(0);
2133         }
2134
2135 SSL_METHOD *ssl_bad_method(int ver)
2136         {
2137         SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2138         return(NULL);
2139         }
2140
2141 const char *SSL_get_version(const SSL *s)
2142         {
2143         if (s->version == TLS1_VERSION)
2144                 return("TLSv1");
2145         else if (s->version == SSL3_VERSION)
2146                 return("SSLv3");
2147         else if (s->version == SSL2_VERSION)
2148                 return("SSLv2");
2149         else
2150                 return("unknown");
2151         }
2152
2153 SSL *SSL_dup(SSL *s)
2154         {
2155         STACK_OF(X509_NAME) *sk;
2156         X509_NAME *xn;
2157         SSL *ret;
2158         int i;
2159                  
2160         if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2161             return(NULL);
2162
2163         ret->version = s->version;
2164         ret->type = s->type;
2165         ret->method = s->method;
2166
2167         if (s->session != NULL)
2168                 {
2169                 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2170                 SSL_copy_session_id(ret,s);
2171                 }
2172         else
2173                 {
2174                 /* No session has been established yet, so we have to expect
2175                  * that s->cert or ret->cert will be changed later --
2176                  * they should not both point to the same object,
2177                  * and thus we can't use SSL_copy_session_id. */
2178
2179                 ret->method->ssl_free(ret);
2180                 ret->method = s->method;
2181                 ret->method->ssl_new(ret);
2182
2183                 if (s->cert != NULL)
2184                         {
2185                         if (ret->cert != NULL)
2186                                 {
2187                                 ssl_cert_free(ret->cert);
2188                                 }
2189                         ret->cert = ssl_cert_dup(s->cert);
2190                         if (ret->cert == NULL)
2191                                 goto err;
2192                         }
2193                                 
2194                 SSL_set_session_id_context(ret,
2195                         s->sid_ctx, s->sid_ctx_length);
2196                 }
2197
2198         ret->options=s->options;
2199         ret->mode=s->mode;
2200         SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
2201         SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
2202         ret->msg_callback = s->msg_callback;
2203         ret->msg_callback_arg = s->msg_callback_arg;
2204         SSL_set_verify(ret,SSL_get_verify_mode(s),
2205                 SSL_get_verify_callback(s));
2206         SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
2207         ret->generate_session_id = s->generate_session_id;
2208
2209         SSL_set_info_callback(ret,SSL_get_info_callback(s));
2210         
2211         ret->debug=s->debug;
2212
2213         /* copy app data, a little dangerous perhaps */
2214         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
2215                 goto err;
2216
2217         /* setup rbio, and wbio */
2218         if (s->rbio != NULL)
2219                 {
2220                 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2221                         goto err;
2222                 }
2223         if (s->wbio != NULL)
2224                 {
2225                 if (s->wbio != s->rbio)
2226                         {
2227                         if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2228                                 goto err;
2229                         }
2230                 else
2231                         ret->wbio=ret->rbio;
2232                 }
2233         ret->rwstate = s->rwstate;
2234         ret->in_handshake = s->in_handshake;
2235         ret->handshake_func = s->handshake_func;
2236         ret->server = s->server;
2237         ret->new_session = s->new_session;
2238         ret->quiet_shutdown = s->quiet_shutdown;
2239         ret->shutdown=s->shutdown;
2240         ret->state=s->state; /* SSL_dup does not really work at any state, though */
2241         ret->rstate=s->rstate;
2242         ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
2243         ret->hit=s->hit;
2244
2245         X509_VERIFY_PARAM_inherit(ret->param, s->param);
2246
2247         /* dup the cipher_list and cipher_list_by_id stacks */
2248         if (s->cipher_list != NULL)
2249                 {
2250                 if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2251                         goto err;
2252                 }
2253         if (s->cipher_list_by_id != NULL)
2254                 if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
2255                         == NULL)
2256                         goto err;
2257
2258         /* Dup the client_CA list */
2259         if (s->client_CA != NULL)
2260                 {
2261                 if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
2262                 ret->client_CA=sk;
2263                 for (i=0; i<sk_X509_NAME_num(sk); i++)
2264                         {
2265                         xn=sk_X509_NAME_value(sk,i);
2266                         if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
2267                                 {
2268                                 X509_NAME_free(xn);
2269                                 goto err;
2270                                 }
2271                         }
2272                 }
2273
2274         if (0)
2275                 {
2276 err:
2277                 if (ret != NULL) SSL_free(ret);
2278                 ret=NULL;
2279                 }
2280         return(ret);
2281         }
2282
2283 void ssl_clear_cipher_ctx(SSL *s)
2284         {
2285         if (s->enc_read_ctx != NULL)
2286                 {
2287                 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2288                 OPENSSL_free(s->enc_read_ctx);
2289                 s->enc_read_ctx=NULL;
2290                 }
2291         if (s->enc_write_ctx != NULL)
2292                 {
2293                 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2294                 OPENSSL_free(s->enc_write_ctx);
2295                 s->enc_write_ctx=NULL;
2296                 }
2297 #ifndef OPENSSL_NO_COMP
2298         if (s->expand != NULL)
2299                 {
2300                 COMP_CTX_free(s->expand);
2301                 s->expand=NULL;
2302                 }
2303         if (s->compress != NULL)
2304                 {
2305                 COMP_CTX_free(s->compress);
2306                 s->compress=NULL;
2307                 }
2308 #endif
2309         }
2310
2311 /* Fix this function so that it takes an optional type parameter */
2312 X509 *SSL_get_certificate(const SSL *s)
2313         {
2314         if (s->cert != NULL)
2315                 return(s->cert->key->x509);
2316         else
2317                 return(NULL);
2318         }
2319
2320 /* Fix this function so that it takes an optional type parameter */
2321 EVP_PKEY *SSL_get_privatekey(SSL *s)
2322         {
2323         if (s->cert != NULL)
2324                 return(s->cert->key->privatekey);
2325         else
2326                 return(NULL);
2327         }
2328
2329 SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2330         {
2331         if ((s->session != NULL) && (s->session->cipher != NULL))
2332                 return(s->session->cipher);
2333         return(NULL);
2334         }
2335 #ifdef OPENSSL_NO_COMP
2336 const void *SSL_get_current_compression(SSL *s)
2337         {
2338         return NULL;
2339         }
2340 const void *SSL_get_current_expansion(SSL *s)
2341         {
2342         return NULL;
2343         }
2344 #else
2345
2346 const COMP_METHOD *SSL_get_current_compression(SSL *s)
2347         {
2348         if (s->compress != NULL)
2349                 return(s->compress->meth);
2350         return(NULL);
2351         }
2352
2353 const COMP_METHOD *SSL_get_current_expansion(SSL *s)
2354         {
2355         if (s->expand != NULL)
2356                 return(s->expand->meth);
2357         return(NULL);
2358         }
2359 #endif
2360
2361 int ssl_init_wbio_buffer(SSL *s,int push)
2362         {
2363         BIO *bbio;
2364
2365         if (s->bbio == NULL)
2366                 {
2367                 bbio=BIO_new(BIO_f_buffer());
2368                 if (bbio == NULL) return(0);
2369                 s->bbio=bbio;
2370                 }
2371         else
2372                 {
2373                 bbio=s->bbio;
2374                 if (s->bbio == s->wbio)
2375                         s->wbio=BIO_pop(s->wbio);
2376                 }
2377         (void)BIO_reset(bbio);
2378 /*      if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2379         if (!BIO_set_read_buffer_size(bbio,1))
2380                 {
2381                 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
2382                 return(0);
2383                 }
2384         if (push)
2385                 {
2386                 if (s->wbio != bbio)
2387                         s->wbio=BIO_push(bbio,s->wbio);
2388                 }
2389         else
2390                 {
2391                 if (s->wbio == bbio)
2392                         s->wbio=BIO_pop(bbio);
2393                 }
2394         return(1);
2395         }
2396
2397 void ssl_free_wbio_buffer(SSL *s)
2398         {
2399         if (s->bbio == NULL) return;
2400
2401         if (s->bbio == s->wbio)
2402                 {
2403                 /* remove buffering */
2404                 s->wbio=BIO_pop(s->wbio);
2405 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2406                 assert(s->wbio != NULL);
2407 #endif  
2408         }
2409         BIO_free(s->bbio);
2410         s->bbio=NULL;
2411         }
2412         
2413 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2414         {
2415         ctx->quiet_shutdown=mode;
2416         }
2417
2418 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2419         {
2420         return(ctx->quiet_shutdown);
2421         }
2422
2423 void SSL_set_quiet_shutdown(SSL *s,int mode)
2424         {
2425         s->quiet_shutdown=mode;
2426         }
2427
2428 int SSL_get_quiet_shutdown(const SSL *s)
2429         {
2430         return(s->quiet_shutdown);
2431         }
2432
2433 void SSL_set_shutdown(SSL *s,int mode)
2434         {
2435         s->shutdown=mode;
2436         }
2437
2438 int SSL_get_shutdown(const SSL *s)
2439         {
2440         return(s->shutdown);
2441         }
2442
2443 int SSL_version(const SSL *s)
2444         {
2445         return(s->version);
2446         }
2447
2448 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2449         {
2450         return(ssl->ctx);
2451         }
2452
2453 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2454         {
2455         if (ssl->cert != NULL)
2456                 ssl_cert_free(ssl->cert);
2457         ssl->cert = ssl_cert_dup(ctx->cert);
2458         CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2459         if (ssl->ctx != NULL)
2460                 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2461         ssl->ctx = ctx;
2462         return(ssl->ctx);
2463         }
2464
2465 #ifndef OPENSSL_NO_STDIO
2466 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2467         {
2468         return(X509_STORE_set_default_paths(ctx->cert_store));
2469         }
2470
2471 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2472                 const char *CApath)
2473         {
2474         return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2475         }
2476 #endif
2477
2478 void SSL_set_info_callback(SSL *ssl,
2479                            void (*cb)(const SSL *ssl,int type,int val))
2480         {
2481         ssl->info_callback=cb;
2482         }
2483
2484 /* One compiler (Diab DCC) doesn't like argument names in returned
2485    function pointer.  */
2486 void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2487         {
2488         return ssl->info_callback;
2489         }
2490
2491 int SSL_state(const SSL *ssl)
2492         {
2493         return(ssl->state);
2494         }
2495
2496 void SSL_set_verify_result(SSL *ssl,long arg)
2497         {
2498         ssl->verify_result=arg;
2499         }
2500
2501 long SSL_get_verify_result(const SSL *ssl)
2502         {
2503         return(ssl->verify_result);
2504         }
2505
2506 int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2507                          CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2508         {
2509         return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2510                                 new_func, dup_func, free_func);
2511         }
2512
2513 int SSL_set_ex_data(SSL *s,int idx,void *arg)
2514         {
2515         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2516         }
2517
2518 void *SSL_get_ex_data(const SSL *s,int idx)
2519         {
2520         return(CRYPTO_get_ex_data(&s->ex_data,idx));
2521         }
2522
2523 int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2524                              CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2525         {
2526         return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2527                                 new_func, dup_func, free_func);
2528         }
2529
2530 int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2531         {
2532         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2533         }
2534
2535 void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2536         {
2537         return(CRYPTO_get_ex_data(&s->ex_data,idx));
2538         }
2539
2540 int ssl_ok(SSL *s)
2541         {
2542         return(1);
2543         }
2544
2545 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2546         {
2547         return(ctx->cert_store);
2548         }
2549
2550 void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2551         {
2552         if (ctx->cert_store != NULL)
2553                 X509_STORE_free(ctx->cert_store);
2554         ctx->cert_store=store;
2555         }
2556
2557 int SSL_want(const SSL *s)
2558         {
2559         return(s->rwstate);
2560         }
2561
2562 /*!
2563  * \brief Set the callback for generating temporary RSA keys.
2564  * \param ctx the SSL context.
2565  * \param cb the callback
2566  */
2567
2568 #ifndef OPENSSL_NO_RSA
2569 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2570                                                           int is_export,
2571                                                           int keylength))
2572     {
2573     SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2574     }
2575
2576 void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2577                                                   int is_export,
2578                                                   int keylength))
2579     {
2580     SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2581     }
2582 #endif
2583
2584 #ifdef DOXYGEN
2585 /*!
2586  * \brief The RSA temporary key callback function.
2587  * \param ssl the SSL session.
2588  * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2589  * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2590  * of the required key in bits.
2591  * \return the temporary RSA key.
2592  * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2593  */
2594
2595 RSA *cb(SSL *ssl,int is_export,int keylength)
2596     {}
2597 #endif
2598
2599 /*!
2600  * \brief Set the callback for generating temporary DH keys.
2601  * \param ctx the SSL context.
2602  * \param dh the callback
2603  */
2604
2605 #ifndef OPENSSL_NO_DH
2606 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2607                                                         int keylength))
2608         {
2609         SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2610         }
2611
2612 void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2613                                                 int keylength))
2614         {
2615         SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2616         }
2617 #endif
2618
2619 #ifndef OPENSSL_NO_ECDH
2620 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2621                                                         int keylength))
2622         {
2623         SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2624         }
2625
2626 void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2627                                                 int keylength))
2628         {
2629         SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2630         }
2631 #endif
2632
2633
2634 void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2635         {
2636         SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2637         }
2638 void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2639         {
2640         SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2641         }
2642
2643
2644
2645 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
2646 #include "../crypto/bio/bss_file.c"
2647 #endif
2648
2649 IMPLEMENT_STACK_OF(SSL_CIPHER)
2650 IMPLEMENT_STACK_OF(SSL_COMP)