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