]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_lib.c
doxygen configuration file.
[thirdparty/openssl.git] / ssl / ssl_lib.c
CommitLineData
4f43d0e7
BL
1/*! \file ssl/ssl_lib.c
2 * \brief Version independent SSL functions.
3 */
58964a49 4/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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#include <stdio.h>
62#include "objects.h"
63#include "lhash.h"
64#include "ssl_locl.h"
65
9cb0969f 66char *SSL_version_str="OpenSSL 0.9.2 31-Dec-1998";
58964a49
RE
67
68static STACK *ssl_meth=NULL;
69static STACK *ssl_ctx_meth=NULL;
70static int ssl_meth_num=0;
71static int ssl_ctx_meth_num=0;
d02b48c6 72
58964a49
RE
73SSL3_ENC_METHOD ssl3_undef_enc_method={
74 ssl_undefined_function,
75 ssl_undefined_function,
76 ssl_undefined_function,
77 ssl_undefined_function,
78 ssl_undefined_function,
79 ssl_undefined_function,
80 };
d02b48c6 81
4f43d0e7 82int SSL_clear(SSL *s)
d02b48c6
RE
83 {
84 int state;
85
413c4f45
MC
86 if (s->method == NULL)
87 {
88 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
89 return(0);
90 }
d02b48c6
RE
91
92 s->error=0;
93 s->hit=0;
413c4f45 94 s->shutdown=0;
d02b48c6 95
413c4f45 96#if 0
d02b48c6
RE
97 /* This is set if we are doing dynamic renegotiation so keep
98 * the old cipher. It is sort of a SSL_clear_lite :-) */
413c4f45
MC
99 if (s->new_session) return(1);
100#endif
d02b48c6
RE
101
102 state=s->state; /* Keep to check if we throw away the session-id */
103 s->type=0;
104
413c4f45
MC
105 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
106
d02b48c6 107 s->version=s->method->version;
413c4f45 108 s->client_version=s->version;
d02b48c6 109 s->rwstate=SSL_NOTHING;
d02b48c6 110 s->rstate=SSL_ST_READ_HEADER;
413c4f45 111 s->read_ahead=s->ctx->read_ahead;
d02b48c6
RE
112
113 if (s->init_buf != NULL)
114 {
115 BUF_MEM_free(s->init_buf);
116 s->init_buf=NULL;
117 }
118
119 ssl_clear_cipher_ctx(s);
120
121 if (ssl_clear_bad_session(s))
122 {
123 SSL_SESSION_free(s->session);
124 s->session=NULL;
125 }
126
d02b48c6
RE
127 s->first_packet=0;
128
413c4f45
MC
129#if 1
130 /* Check to see if we were changed into a different method, if
131 * so, revert back if we are not doing session-id reuse. */
132 if ((s->session == NULL) && (s->method != s->ctx->method))
133 {
134 s->method->ssl_free(s);
135 s->method=s->ctx->method;
136 if (!s->method->ssl_new(s))
137 return(0);
138 }
139 else
140#endif
141 s->method->ssl_clear(s);
142 return(1);
d02b48c6
RE
143 }
144
4f43d0e7
BL
145/** Used to change an SSL_CTXs default SSL method type */
146int SSL_CTX_set_ssl_version(SSL_CTX *ctx,SSL_METHOD *meth)
d02b48c6
RE
147 {
148 STACK *sk;
149
150 ctx->method=meth;
151
152 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
153 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
154 if ((sk == NULL) || (sk_num(sk) <= 0))
155 {
156 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
157 return(0);
158 }
159 return(1);
160 }
161
4f43d0e7 162SSL *SSL_new(SSL_CTX *ctx)
d02b48c6
RE
163 {
164 SSL *s;
165
166 if (ctx == NULL)
167 {
168 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
169 return(NULL);
170 }
171 if (ctx->method == NULL)
172 {
173 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
174 return(NULL);
175 }
176
177 s=(SSL *)Malloc(sizeof(SSL));
178 if (s == NULL) goto err;
179 memset(s,0,sizeof(SSL));
180
181 if (ctx->default_cert != NULL)
182 {
183 CRYPTO_add(&ctx->default_cert->references,1,
184 CRYPTO_LOCK_SSL_CERT);
185 s->cert=ctx->default_cert;
186 }
187 else
188 s->cert=NULL;
413c4f45 189 s->verify_mode=ctx->verify_mode;
d02b48c6
RE
190 s->verify_callback=ctx->default_verify_callback;
191 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
192 s->ctx=ctx;
193
194 s->verify_result=X509_V_OK;
195
196 s->method=ctx->method;
197
198 if (!s->method->ssl_new(s))
199 {
200 SSL_CTX_free(ctx);
201 Free(s);
202 goto err;
203 }
204
205 s->quiet_shutdown=ctx->quiet_shutdown;
58964a49 206 s->references=1;
413c4f45 207 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
58964a49 208 s->options=ctx->options;
d02b48c6 209 SSL_clear(s);
58964a49
RE
210
211 CRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);
212
d02b48c6
RE
213 return(s);
214err:
215 SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
216 return(NULL);
217 }
218
4f43d0e7 219void SSL_free(SSL *s)
d02b48c6 220 {
58964a49
RE
221 int i;
222
e03ddfae
BL
223 if(s == NULL)
224 return;
225
58964a49
RE
226 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
227#ifdef REF_PRINT
228 REF_PRINT("SSL",s);
229#endif
230 if (i > 0) return;
231#ifdef REF_CHECK
232 if (i < 0)
233 {
234 fprintf(stderr,"SSL_free, bad reference count\n");
235 abort(); /* ok */
236 }
237#endif
238
239 CRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);
240
d02b48c6
RE
241 if (s->bbio != NULL)
242 {
243 /* If the buffering BIO is in place, pop it off */
244 if (s->bbio == s->wbio)
245 {
246 s->wbio=BIO_pop(s->wbio);
247 }
248 BIO_free(s->bbio);
58964a49 249 s->bbio=NULL;
d02b48c6
RE
250 }
251 if (s->rbio != NULL)
252 BIO_free_all(s->rbio);
253 if ((s->wbio != NULL) && (s->wbio != s->rbio))
254 BIO_free_all(s->wbio);
255
256 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
257
258 /* add extra stuff */
259 if (s->cipher_list != NULL) sk_free(s->cipher_list);
260 if (s->cipher_list_by_id != NULL) sk_free(s->cipher_list_by_id);
261
262 /* Make the next call work :-) */
263 if (s->session != NULL)
264 {
265 ssl_clear_bad_session(s);
266 SSL_SESSION_free(s->session);
267 }
268
269 ssl_clear_cipher_ctx(s);
270
271 if (s->cert != NULL) ssl_cert_free(s->cert);
272 /* Free up if allocated */
273
274 if (s->ctx) SSL_CTX_free(s->ctx);
275
276 if (s->client_CA != NULL)
277 sk_pop_free(s->client_CA,X509_NAME_free);
278
279 if (s->method != NULL) s->method->ssl_free(s);
280
281 Free((char *)s);
282 }
283
4f43d0e7 284void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
d02b48c6
RE
285 {
286 /* If the output buffering BIO is still in place, remove it
287 */
288 if (s->bbio != NULL)
289 {
290 if (s->wbio == s->bbio)
291 {
292 s->wbio=s->wbio->next_bio;
293 s->bbio->next_bio=NULL;
294 }
295 }
296 if ((s->rbio != NULL) && (s->rbio != rbio))
297 BIO_free_all(s->rbio);
298 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
299 BIO_free_all(s->wbio);
300 s->rbio=rbio;
301 s->wbio=wbio;
302 }
303
4f43d0e7 304BIO *SSL_get_rbio(SSL *s)
d02b48c6
RE
305 { return(s->rbio); }
306
4f43d0e7 307BIO *SSL_get_wbio(SSL *s)
d02b48c6
RE
308 { return(s->wbio); }
309
4f43d0e7 310int SSL_get_fd(SSL *s)
d02b48c6
RE
311 {
312 int ret= -1;
313 BIO *b,*r;
314
315 b=SSL_get_rbio(s);
316 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
317 if (r != NULL)
318 BIO_get_fd(r,&ret);
319 return(ret);
320 }
321
322#ifndef NO_SOCK
4f43d0e7 323int SSL_set_fd(SSL *s,int fd)
d02b48c6
RE
324 {
325 int ret=0;
326 BIO *bio=NULL;
327
328 bio=BIO_new(BIO_s_socket());
329
330 if (bio == NULL)
331 {
332 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
333 goto err;
334 }
335 BIO_set_fd(bio,fd,BIO_NOCLOSE);
336 SSL_set_bio(s,bio,bio);
337 ret=1;
338err:
339 return(ret);
340 }
341
4f43d0e7 342int SSL_set_wfd(SSL *s,int fd)
d02b48c6
RE
343 {
344 int ret=0;
345 BIO *bio=NULL;
346
58964a49
RE
347 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
348 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
349 {
350 bio=BIO_new(BIO_s_socket());
d02b48c6 351
58964a49
RE
352 if (bio == NULL)
353 { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
354 BIO_set_fd(bio,fd,BIO_NOCLOSE);
355 SSL_set_bio(s,SSL_get_rbio(s),bio);
356 }
357 else
358 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
d02b48c6
RE
359 ret=1;
360err:
361 return(ret);
362 }
363
4f43d0e7 364int SSL_set_rfd(SSL *s,int fd)
d02b48c6
RE
365 {
366 int ret=0;
367 BIO *bio=NULL;
368
58964a49
RE
369 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
370 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
d02b48c6 371 {
58964a49
RE
372 bio=BIO_new(BIO_s_socket());
373
374 if (bio == NULL)
375 {
376 SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
377 goto err;
378 }
379 BIO_set_fd(bio,fd,BIO_NOCLOSE);
380 SSL_set_bio(s,bio,SSL_get_wbio(s));
d02b48c6 381 }
58964a49
RE
382 else
383 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
d02b48c6
RE
384 ret=1;
385err:
386 return(ret);
387 }
388#endif
389
4f43d0e7 390int SSL_get_verify_mode(SSL *s)
d02b48c6
RE
391 {
392 return(s->verify_mode);
393 }
394
4f43d0e7 395int (*SSL_get_verify_callback(SSL *s))()
d02b48c6
RE
396 {
397 return(s->verify_callback);
398 }
399
4f43d0e7 400int SSL_CTX_get_verify_mode(SSL_CTX *ctx)
d02b48c6 401 {
413c4f45 402 return(ctx->verify_mode);
d02b48c6
RE
403 }
404
4f43d0e7 405int (*SSL_CTX_get_verify_callback(SSL_CTX *ctx))()
d02b48c6
RE
406 {
407 return(ctx->default_verify_callback);
408 }
409
4f43d0e7 410void SSL_set_verify(SSL *s,int mode,int (*callback)())
d02b48c6
RE
411 {
412 s->verify_mode=mode;
413 if (callback != NULL)
414 s->verify_callback=callback;
415 }
416
4f43d0e7 417void SSL_set_read_ahead(SSL *s,int yes)
d02b48c6
RE
418 {
419 s->read_ahead=yes;
420 }
421
4f43d0e7 422int SSL_get_read_ahead(SSL *s)
d02b48c6
RE
423 {
424 return(s->read_ahead);
425 }
426
4f43d0e7 427int SSL_pending(SSL *s)
d02b48c6
RE
428 {
429 return(s->method->ssl_pending(s));
430 }
431
4f43d0e7 432X509 *SSL_get_peer_certificate(SSL *s)
d02b48c6
RE
433 {
434 X509 *r;
435
436 if ((s == NULL) || (s->session == NULL))
437 r=NULL;
438 else
439 r=s->session->peer;
440
441 if (r == NULL) return(r);
442
443 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
444
445 return(r);
446 }
447
4f43d0e7 448STACK *SSL_get_peer_cert_chain(SSL *s)
d02b48c6
RE
449 {
450 STACK *r;
451
452 if ((s == NULL) || (s->session == NULL) || (s->session->cert == NULL))
453 r=NULL;
454 else
455 r=s->session->cert->cert_chain;
456
457 return(r);
458 }
459
460/* Now in theory, since the calling process own 't' it should be safe to
461 * modify. We need to be able to read f without being hassled */
4f43d0e7 462void SSL_copy_session_id(SSL *t,SSL *f)
d02b48c6
RE
463 {
464 CERT *tmp;
465
466 /* Do we need to to SSL locking? */
467 SSL_set_session(t,SSL_get_session(f));
468
469 /* what if we are setup as SSLv2 but want to talk SSLv3 or
470 * vice-versa */
471 if (t->method != f->method)
472 {
473 t->method->ssl_free(t); /* cleanup current */
474 t->method=f->method; /* change method */
475 t->method->ssl_new(t); /* setup new */
476 }
477
478 tmp=t->cert;
479 if (f->cert != NULL)
480 {
481 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
482 t->cert=f->cert;
483 }
484 else
485 t->cert=NULL;
486 if (tmp != NULL) ssl_cert_free(tmp);
487 }
488
58964a49 489/* Fix this so it checks all the valid key/cert options */
4f43d0e7 490int SSL_CTX_check_private_key(SSL_CTX *ctx)
d02b48c6
RE
491 {
492 if ( (ctx == NULL) ||
493 (ctx->default_cert == NULL) ||
494 (ctx->default_cert->key->x509 == NULL))
495 {
496 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
497 return(0);
498 }
499 if (ctx->default_cert->key->privatekey == NULL)
500 {
501 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
502 return(0);
503 }
504 return(X509_check_private_key(ctx->default_cert->key->x509, ctx->default_cert->key->privatekey));
505 }
506
58964a49 507/* Fix this function so that it takes an optional type parameter */
4f43d0e7 508int SSL_check_private_key(SSL *ssl)
d02b48c6
RE
509 {
510 if (ssl == NULL)
511 {
512 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
513 return(0);
514 }
515 if (ssl->cert == NULL)
516 return(SSL_CTX_check_private_key(ssl->ctx));
517 if (ssl->cert->key->x509 == NULL)
518 {
519 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
520 return(0);
521 }
522 if (ssl->cert->key->privatekey == NULL)
523 {
524 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
525 return(0);
526 }
527 return(X509_check_private_key(ssl->cert->key->x509,
528 ssl->cert->key->privatekey));
529 }
530
4f43d0e7 531int SSL_accept(SSL *s)
d02b48c6
RE
532 {
533 return(s->method->ssl_accept(s));
534 }
535
4f43d0e7 536int SSL_connect(SSL *s)
d02b48c6
RE
537 {
538 return(s->method->ssl_connect(s));
539 }
540
4f43d0e7 541long SSL_get_default_timeout(SSL *s)
d02b48c6
RE
542 {
543 return(s->method->get_timeout());
544 }
545
4f43d0e7 546int SSL_read(SSL *s,char *buf,int num)
d02b48c6
RE
547 {
548 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
549 {
550 s->rwstate=SSL_NOTHING;
551 return(0);
552 }
553 return(s->method->ssl_read(s,buf,num));
554 }
555
4f43d0e7 556int SSL_peek(SSL *s,char *buf,int num)
d02b48c6
RE
557 {
558 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
559 {
560 return(0);
561 }
562 return(s->method->ssl_peek(s,buf,num));
563 }
564
4f43d0e7 565int SSL_write(SSL *s,const char *buf,int num)
d02b48c6
RE
566 {
567 if (s->shutdown & SSL_SENT_SHUTDOWN)
568 {
569 s->rwstate=SSL_NOTHING;
570 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
571 return(-1);
572 }
573 return(s->method->ssl_write(s,buf,num));
574 }
575
4f43d0e7 576int SSL_shutdown(SSL *s)
d02b48c6
RE
577 {
578 if ((s != NULL) && !SSL_in_init(s))
579 return(s->method->ssl_shutdown(s));
580 else
581 return(1);
582 }
583
4f43d0e7 584int SSL_renegotiate(SSL *s)
d02b48c6 585 {
58964a49 586 s->new_session=1;
d02b48c6
RE
587 return(s->method->ssl_renegotiate(s));
588 }
589
4f43d0e7 590long SSL_ctrl(SSL *s,int cmd,long larg,char *parg)
d02b48c6 591 {
413c4f45
MC
592 long l;
593
594 switch (cmd)
595 {
596 case SSL_CTRL_GET_READ_AHEAD:
597 return(s->read_ahead);
598 case SSL_CTRL_SET_READ_AHEAD:
599 l=s->read_ahead;
600 s->read_ahead=larg;
601 return(l);
602 case SSL_CTRL_OPTIONS:
603 return(s->options|=larg);
604 default:
605 return(s->method->ssl_ctrl(s,cmd,larg,parg));
606 }
607 return(0);
d02b48c6
RE
608 }
609
4f43d0e7 610long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,char *parg)
d02b48c6 611 {
413c4f45
MC
612 long l;
613
614 switch (cmd)
615 {
616 case SSL_CTRL_GET_READ_AHEAD:
617 return(ctx->read_ahead);
618 case SSL_CTRL_SET_READ_AHEAD:
619 l=ctx->read_ahead;
620 ctx->read_ahead=larg;
621 return(l);
622
623 case SSL_CTRL_SET_SESS_CACHE_SIZE:
624 l=ctx->session_cache_size;
625 ctx->session_cache_size=larg;
626 return(l);
627 case SSL_CTRL_GET_SESS_CACHE_SIZE:
628 return(ctx->session_cache_size);
629 case SSL_CTRL_SET_SESS_CACHE_MODE:
630 l=ctx->session_cache_mode;
631 ctx->session_cache_mode=larg;
632 return(l);
633 case SSL_CTRL_GET_SESS_CACHE_MODE:
634 return(ctx->session_cache_mode);
635
636 case SSL_CTRL_SESS_NUMBER:
637 return(ctx->sessions->num_items);
638 case SSL_CTRL_SESS_CONNECT:
639 return(ctx->stats.sess_connect);
640 case SSL_CTRL_SESS_CONNECT_GOOD:
641 return(ctx->stats.sess_connect_good);
642 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
643 return(ctx->stats.sess_connect_renegotiate);
644 case SSL_CTRL_SESS_ACCEPT:
645 return(ctx->stats.sess_accept);
646 case SSL_CTRL_SESS_ACCEPT_GOOD:
647 return(ctx->stats.sess_accept_good);
648 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
649 return(ctx->stats.sess_accept_renegotiate);
650 case SSL_CTRL_SESS_HIT:
651 return(ctx->stats.sess_hit);
652 case SSL_CTRL_SESS_CB_HIT:
653 return(ctx->stats.sess_cb_hit);
654 case SSL_CTRL_SESS_MISSES:
655 return(ctx->stats.sess_miss);
656 case SSL_CTRL_SESS_TIMEOUTS:
657 return(ctx->stats.sess_timeout);
658 case SSL_CTRL_SESS_CACHE_FULL:
659 return(ctx->stats.sess_cache_full);
660 case SSL_CTRL_OPTIONS:
661 return(ctx->options|=larg);
662 default:
663 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
664 }
665 return(0);
d02b48c6
RE
666 }
667
4f43d0e7 668int ssl_cipher_id_cmp(SSL_CIPHER *a,SSL_CIPHER *b)
d02b48c6
RE
669 {
670 long l;
671
672 l=a->id-b->id;
673 if (l == 0L)
674 return(0);
675 else
676 return((l > 0)?1:-1);
677 }
678
4f43d0e7 679int ssl_cipher_ptr_id_cmp(SSL_CIPHER **ap,SSL_CIPHER **bp)
d02b48c6
RE
680 {
681 long l;
682
683 l=(*ap)->id-(*bp)->id;
684 if (l == 0L)
685 return(0);
686 else
687 return((l > 0)?1:-1);
688 }
689
4f43d0e7 690/** return a STACK of the ciphers available for the SSL and in order of
d02b48c6 691 * preference */
4f43d0e7 692STACK *SSL_get_ciphers(SSL *s)
d02b48c6
RE
693 {
694 if ((s != NULL) && (s->cipher_list != NULL))
695 {
696 return(s->cipher_list);
697 }
58964a49 698 else if ((s->ctx != NULL) &&
d02b48c6
RE
699 (s->ctx->cipher_list != NULL))
700 {
701 return(s->ctx->cipher_list);
702 }
703 return(NULL);
704 }
705
4f43d0e7 706/** return a STACK of the ciphers available for the SSL and in order of
d02b48c6 707 * algorithm id */
4f43d0e7 708STACK *ssl_get_ciphers_by_id(SSL *s)
d02b48c6
RE
709 {
710 if ((s != NULL) && (s->cipher_list_by_id != NULL))
711 {
712 return(s->cipher_list_by_id);
713 }
714 else if ((s != NULL) && (s->ctx != NULL) &&
715 (s->ctx->cipher_list_by_id != NULL))
716 {
717 return(s->ctx->cipher_list_by_id);
718 }
719 return(NULL);
720 }
721
4f43d0e7
BL
722/** The old interface to get the same thing as SSL_get_ciphers() */
723char *SSL_get_cipher_list(SSL *s,int n)
d02b48c6
RE
724 {
725 SSL_CIPHER *c;
726 STACK *sk;
727
728 if (s == NULL) return(NULL);
729 sk=SSL_get_ciphers(s);
730 if ((sk == NULL) || (sk_num(sk) <= n))
731 return(NULL);
732 c=(SSL_CIPHER *)sk_value(sk,n);
733 if (c == NULL) return(NULL);
734 return(c->name);
735 }
736
4f43d0e7
BL
737/** specify the ciphers to be used by defaut by the SSL_CTX */
738int SSL_CTX_set_cipher_list(SSL_CTX *ctx,char *str)
d02b48c6
RE
739 {
740 STACK *sk;
741
742 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
743 &ctx->cipher_list_by_id,str);
744/* XXXX */
745 return((sk == NULL)?0:1);
746 }
747
4f43d0e7
BL
748/** specify the ciphers to be used by the SSL */
749int SSL_set_cipher_list(SSL *s,char *str)
d02b48c6
RE
750 {
751 STACK *sk;
752
753 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
754 &s->cipher_list_by_id,str);
755/* XXXX */
756 return((sk == NULL)?0:1);
757 }
758
759/* works well for SSLv2, not so good for SSLv3 */
4f43d0e7 760char *SSL_get_shared_ciphers(SSL *s,char *buf,int len)
d02b48c6
RE
761 {
762 char *p,*cp;
763 STACK *sk;
764 SSL_CIPHER *c;
765 int i;
766
767 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
768 (len < 2))
769 return(NULL);
770
771 p=buf;
772 sk=s->session->ciphers;
d02b48c6
RE
773 for (i=0; i<sk_num(sk); i++)
774 {
58964a49
RE
775 /* Decrement for either the ':' or a '\0' */
776 len--;
d02b48c6
RE
777 c=(SSL_CIPHER *)sk_value(sk,i);
778 for (cp=c->name; *cp; )
779 {
58964a49 780 if (len-- == 0)
d02b48c6
RE
781 {
782 *p='\0';
783 return(buf);
784 }
785 else
786 *(p++)= *(cp++);
787 }
788 *(p++)=':';
789 }
790 p[-1]='\0';
791 return(buf);
792 }
793
4f43d0e7 794int ssl_cipher_list_to_bytes(SSL *s,STACK *sk,unsigned char *p)
d02b48c6
RE
795 {
796 int i,j=0;
797 SSL_CIPHER *c;
798 unsigned char *q;
799
800 if (sk == NULL) return(0);
801 q=p;
802
803 for (i=0; i<sk_num(sk); i++)
804 {
805 c=(SSL_CIPHER *)sk_value(sk,i);
806 j=ssl_put_cipher_by_char(s,c,p);
807 p+=j;
808 }
809 return(p-q);
810 }
811
4f43d0e7 812STACK *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,STACK **skp)
d02b48c6
RE
813 {
814 SSL_CIPHER *c;
815 STACK *sk;
816 int i,n;
817
818 n=ssl_put_cipher_by_char(s,NULL,NULL);
819 if ((num%n) != 0)
820 {
821 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
822 return(NULL);
823 }
824 if ((skp == NULL) || (*skp == NULL))
825 sk=sk_new(NULL); /* change perhaps later */
826 else
827 {
828 sk= *skp;
829 sk_zero(sk);
830 }
831
832 for (i=0; i<num; i+=n)
833 {
834 c=ssl_get_cipher_by_char(s,p);
835 p+=n;
836 if (c != NULL)
837 {
838 if (!sk_push(sk,(char *)c))
839 {
840 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
841 goto err;
842 }
843 }
844 }
845
846 if (skp != NULL)
847 *skp=sk;
848 return(sk);
849err:
850 if ((skp == NULL) || (*skp == NULL))
851 sk_free(sk);
852 return(NULL);
853 }
854
4f43d0e7 855unsigned long SSL_SESSION_hash(SSL_SESSION *a)
d02b48c6
RE
856 {
857 unsigned long l;
858
dfeab068
RE
859 l=(unsigned long)
860 ((unsigned int) a->session_id[0] )|
861 ((unsigned int) a->session_id[1]<< 8L)|
862 ((unsigned long)a->session_id[2]<<16L)|
863 ((unsigned long)a->session_id[3]<<24L);
d02b48c6
RE
864 return(l);
865 }
866
4f43d0e7 867int SSL_SESSION_cmp(SSL_SESSION *a,SSL_SESSION *b)
d02b48c6 868 {
58964a49
RE
869 if (a->ssl_version != b->ssl_version)
870 return(1);
871 if (a->session_id_length != b->session_id_length)
872 return(1);
873 return(memcmp(a->session_id,b->session_id,a->session_id_length));
d02b48c6
RE
874 }
875
4f43d0e7 876SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)
d02b48c6 877 {
dfeab068 878 SSL_CTX *ret=NULL;
d02b48c6
RE
879
880 if (meth == NULL)
881 {
882 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
883 return(NULL);
884 }
dfeab068
RE
885
886 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
887 {
888 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
889 goto err;
890 }
d02b48c6
RE
891 ret=(SSL_CTX *)Malloc(sizeof(SSL_CTX));
892 if (ret == NULL)
893 goto err;
894
895 memset(ret,0,sizeof(SSL_CTX));
896
897 ret->method=meth;
898
899 ret->cert_store=NULL;
900 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
58964a49
RE
901 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
902 ret->session_cache_head=NULL;
903 ret->session_cache_tail=NULL;
d02b48c6
RE
904
905 /* We take the system default */
906 ret->session_timeout=meth->get_timeout();
907
908 ret->new_session_cb=NULL;
909 ret->remove_session_cb=NULL;
910 ret->get_session_cb=NULL;
911
413c4f45 912 memset((char *)&ret->stats,0,sizeof(ret->stats));
d02b48c6
RE
913
914 ret->references=1;
915 ret->quiet_shutdown=0;
916
917/* ret->cipher=NULL;*/
918/* ret->s2->challenge=NULL;
919 ret->master_key=NULL;
920 ret->key_arg=NULL;
921 ret->s2->conn_id=NULL; */
922
923 ret->info_callback=NULL;
924
925 ret->app_verify_callback=NULL;
926 ret->app_verify_arg=NULL;
927
413c4f45
MC
928 ret->read_ahead=0;
929 ret->verify_mode=SSL_VERIFY_NONE;
d02b48c6
RE
930 ret->default_verify_callback=NULL;
931 if ((ret->default_cert=ssl_cert_new()) == NULL)
932 goto err;
933
934 ret->default_passwd_callback=NULL;
935 ret->client_cert_cb=NULL;
936
58964a49 937 ret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp);
d02b48c6
RE
938 if (ret->sessions == NULL) goto err;
939 ret->cert_store=X509_STORE_new();
940 if (ret->cert_store == NULL) goto err;
941
942 ssl_create_cipher_list(ret->method,
943 &ret->cipher_list,&ret->cipher_list_by_id,
944 SSL_DEFAULT_CIPHER_LIST);
945 if ((ret->cipher_list == NULL) || (sk_num(ret->cipher_list) <= 0))
946 {
947 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
948 goto err2;
949 }
950
58964a49
RE
951 if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
952 {
953 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
954 goto err2;
955 }
956 if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
957 {
958 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
959 goto err2;
960 }
961 if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
962 {
963 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
964 goto err2;
965 }
966
d02b48c6
RE
967 if ((ret->client_CA=sk_new_null()) == NULL)
968 goto err;
969
58964a49
RE
970 CRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);
971
dfeab068 972 ret->extra_certs=NULL;
413c4f45 973 ret->comp_methods=SSL_COMP_get_compression_methods();
dfeab068 974
d02b48c6
RE
975 return(ret);
976err:
977 SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
978err2:
979 if (ret != NULL) SSL_CTX_free(ret);
980 return(NULL);
981 }
982
4f43d0e7 983void SSL_CTX_free(SSL_CTX *a)
d02b48c6
RE
984 {
985 int i;
986
987 if (a == NULL) return;
988
989 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
58964a49
RE
990#ifdef REF_PRINT
991 REF_PRINT("SSL_CTX",a);
992#endif
d02b48c6
RE
993 if (i > 0) return;
994#ifdef REF_CHECK
995 if (i < 0)
996 {
997 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
998 abort(); /* ok */
999 }
1000#endif
58964a49 1001 CRYPTO_free_ex_data(ssl_ctx_meth,(char *)a,&a->ex_data);
d02b48c6
RE
1002
1003 if (a->sessions != NULL)
1004 {
1005 SSL_CTX_flush_sessions(a,0);
1006 lh_free(a->sessions);
1007 }
1008 if (a->cert_store != NULL)
1009 X509_STORE_free(a->cert_store);
1010 if (a->cipher_list != NULL)
1011 sk_free(a->cipher_list);
1012 if (a->cipher_list_by_id != NULL)
1013 sk_free(a->cipher_list_by_id);
1014 if (a->default_cert != NULL)
1015 ssl_cert_free(a->default_cert);
1016 if (a->client_CA != NULL)
1017 sk_pop_free(a->client_CA,X509_NAME_free);
dfeab068
RE
1018 if (a->extra_certs != NULL)
1019 sk_pop_free(a->extra_certs,X509_free);
413c4f45
MC
1020 if (a->comp_methods != NULL)
1021 sk_pop_free(a->comp_methods,free);
d02b48c6
RE
1022 Free((char *)a);
1023 }
1024
4f43d0e7 1025void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx,int (*cb)())
d02b48c6
RE
1026 {
1027 ctx->default_passwd_callback=cb;
1028 }
1029
4f43d0e7 1030void SSL_CTX_set_cert_verify_cb(SSL_CTX *ctx,int (*cb)(),char *arg)
d02b48c6
RE
1031 {
1032 ctx->app_verify_callback=cb;
1033 ctx->app_verify_arg=arg;
1034 }
1035
4f43d0e7 1036void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
d02b48c6 1037 {
413c4f45 1038 ctx->verify_mode=mode;
d02b48c6
RE
1039 ctx->default_verify_callback=cb;
1040 /* This needs cleaning up EAY EAY EAY */
1041 X509_STORE_set_verify_cb_func(ctx->cert_store,cb);
1042 }
1043
4f43d0e7 1044void ssl_set_cert_masks(CERT *c,SSL_CIPHER *cipher)
d02b48c6
RE
1045 {
1046 CERT_PKEY *cpk;
1047 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1048 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
60e31c3a 1049 int rsa_tmp_export,dh_tmp_export,kl;
d02b48c6
RE
1050 unsigned long mask,emask;
1051
1052 if ((c == NULL) || (c->valid)) return;
1053
60e31c3a
BL
1054 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1055
d02b48c6 1056#ifndef NO_RSA
60e31c3a
BL
1057 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1058 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1059 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
d02b48c6
RE
1060#else
1061 rsa_tmp=rsa_tmp_export=0;
1062#endif
1063#ifndef NO_DH
60e31c3a
BL
1064 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1065 dh_tmp_export=(c->dh_tmp_cb != NULL ||
1066 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
d02b48c6
RE
1067#else
1068 dh_tmp=dh_tmp_export=0;
1069#endif
1070
1071 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
60e31c3a
BL
1072 rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1073 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
d02b48c6 1074 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
60e31c3a 1075 rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
d02b48c6 1076 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
60e31c3a 1077 dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
d02b48c6 1078 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
60e31c3a
BL
1079 dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1080 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
d02b48c6
RE
1081 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1082/* FIX THIS EAY EAY EAY */
60e31c3a
BL
1083 dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL);
1084 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
d02b48c6
RE
1085
1086 mask=0;
1087 emask=0;
1088
1089#ifdef CIPHER_DEBUG
1090 printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1091 rsa_tmp,dh_tmp,
1092 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1093#endif
1094
1095 if (rsa_enc || (rsa_tmp && rsa_sign))
1096 mask|=SSL_kRSA;
1097 if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1098 emask|=SSL_kRSA;
1099
1100#if 0
1101 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1102 if ( (dh_tmp || dh_rsa || dh_dsa) &&
1103 (rsa_enc || rsa_sign || dsa_sign))
1104 mask|=SSL_kEDH;
1105 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1106 (rsa_enc || rsa_sign || dsa_sign))
1107 emask|=SSL_kEDH;
1108#endif
1109
1110 if (dh_tmp_export)
1111 emask|=SSL_kEDH;
1112
1113 if (dh_tmp)
1114 mask|=SSL_kEDH;
1115
1116 if (dh_rsa) mask|=SSL_kDHr;
1117 if (dh_rsa_export) emask|=SSL_kDHr;
1118
1119 if (dh_dsa) mask|=SSL_kDHd;
1120 if (dh_dsa_export) emask|=SSL_kDHd;
1121
1122 if (rsa_enc || rsa_sign)
1123 {
1124 mask|=SSL_aRSA;
1125 emask|=SSL_aRSA;
1126 }
1127
1128 if (dsa_sign)
1129 {
1130 mask|=SSL_aDSS;
1131 emask|=SSL_aDSS;
1132 }
1133
1134#ifdef SSL_ALLOW_ADH
1135 mask|=SSL_aNULL;
1136 emask|=SSL_aNULL;
1137#endif
1138
1139 c->mask=mask;
1140 c->export_mask=emask;
1141 c->valid=1;
1142 }
1143
1144/* THIS NEEDS CLEANING UP */
4f43d0e7 1145X509 *ssl_get_server_send_cert(SSL *s)
d02b48c6
RE
1146 {
1147 unsigned long alg,mask,kalg;
1148 CERT *c;
60e31c3a 1149 int i,export;
d02b48c6
RE
1150
1151 c=s->cert;
60e31c3a 1152 ssl_set_cert_masks(c,s->s3->tmp.new_cipher);
d02b48c6 1153 alg=s->s3->tmp.new_cipher->algorithms;
60e31c3a
BL
1154 export=SSL_IS_EXPORT(alg);
1155 mask=export?c->export_mask:c->mask;
d02b48c6
RE
1156 kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1157
1158 if (kalg & SSL_kDHr)
1159 i=SSL_PKEY_DH_RSA;
1160 else if (kalg & SSL_kDHd)
1161 i=SSL_PKEY_DH_DSA;
1162 else if (kalg & SSL_aDSS)
1163 i=SSL_PKEY_DSA_SIGN;
1164 else if (kalg & SSL_aRSA)
1165 {
1166 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1167 i=SSL_PKEY_RSA_SIGN;
1168 else
1169 i=SSL_PKEY_RSA_ENC;
1170 }
1171 else /* if (kalg & SSL_aNULL) */
1172 {
1173 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1174 return(NULL);
1175 }
1176 if (c->pkeys[i].x509 == NULL) return(NULL);
1177 return(c->pkeys[i].x509);
1178 }
1179
4f43d0e7 1180EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
d02b48c6
RE
1181 {
1182 unsigned long alg;
1183 CERT *c;
1184
1185 alg=cipher->algorithms;
1186 c=s->cert;
1187
1188 if ((alg & SSL_aDSS) &&
1189 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1190 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1191 else if (alg & SSL_aRSA)
1192 {
1193 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1194 return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1195 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1196 return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1197 else
1198 return(NULL);
1199 }
1200 else /* if (alg & SSL_aNULL) */
1201 {
1202 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1203 return(NULL);
1204 }
1205 }
1206
4f43d0e7 1207void ssl_update_cache(SSL *s,int mode)
d02b48c6 1208 {
58964a49
RE
1209 int i;
1210
1211 /* If the session_id_length is 0, we are not supposed to cache it,
1212 * and it would be rather hard to do anyway :-) */
1213 if (s->session->session_id_length == 0) return;
1214
d02b48c6
RE
1215 if ((s->ctx->session_cache_mode & mode)
1216 && (!s->hit)
1217 && SSL_CTX_add_session(s->ctx,s->session)
1218 && (s->ctx->new_session_cb != NULL))
1219 {
58964a49 1220 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
d02b48c6
RE
1221 if (!s->ctx->new_session_cb(s,s->session))
1222 SSL_SESSION_free(s->session);
1223 }
1224
1225 /* auto flush every 255 connections */
58964a49
RE
1226 i=s->ctx->session_cache_mode;
1227 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1228 ((i & mode) == mode))
1229 {
1230 if ( (((mode & SSL_SESS_CACHE_CLIENT)
413c4f45
MC
1231 ?s->ctx->stats.sess_connect_good
1232 :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
58964a49
RE
1233 {
1234 SSL_CTX_flush_sessions(s->ctx,time(NULL));
1235 }
1236 }
d02b48c6
RE
1237 }
1238
4f43d0e7 1239SSL_METHOD *SSL_get_ssl_method(SSL *s)
d02b48c6
RE
1240 {
1241 return(s->method);
1242 }
1243
4f43d0e7 1244int SSL_set_ssl_method(SSL *s,SSL_METHOD *meth)
d02b48c6
RE
1245 {
1246 int conn= -1;
1247 int ret=1;
1248
1249 if (s->method != meth)
1250 {
1251 if (s->handshake_func != NULL)
1252 conn=(s->handshake_func == s->method->ssl_connect);
1253
1254 if (s->method->version == meth->version)
1255 s->method=meth;
1256 else
1257 {
1258 s->method->ssl_free(s);
1259 s->method=meth;
1260 ret=s->method->ssl_new(s);
1261 }
1262
1263 if (conn == 1)
1264 s->handshake_func=meth->ssl_connect;
1265 else if (conn == 0)
1266 s->handshake_func=meth->ssl_accept;
1267 }
1268 return(ret);
1269 }
1270
4f43d0e7 1271int SSL_get_error(SSL *s,int i)
d02b48c6
RE
1272 {
1273 int reason;
413c4f45 1274 unsigned long l;
d02b48c6
RE
1275 BIO *bio;
1276
1277 if (i > 0) return(SSL_ERROR_NONE);
1278
413c4f45
MC
1279 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1280 * etc, where we do encode the error */
1281 if ((l=ERR_peek_error()) != 0)
1282 {
1283 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
1284 return(SSL_ERROR_SYSCALL);
1285 else
1286 return(SSL_ERROR_SSL);
1287 }
d02b48c6
RE
1288
1289 if ((i < 0) && SSL_want_read(s))
1290 {
1291 bio=SSL_get_rbio(s);
1292 if (BIO_should_read(bio))
1293 return(SSL_ERROR_WANT_READ);
1294 else if (BIO_should_write(bio))
1295 return(SSL_ERROR_WANT_WRITE);
1296 else if (BIO_should_io_special(bio))
1297 {
1298 reason=BIO_get_retry_reason(bio);
1299 if (reason == BIO_RR_CONNECT)
1300 return(SSL_ERROR_WANT_CONNECT);
1301 else
1302 return(SSL_ERROR_SYSCALL); /* unknown */
1303 }
1304 }
1305
1306 if ((i < 0) && SSL_want_write(s))
1307 {
1308 bio=SSL_get_wbio(s);
1309 if (BIO_should_write(bio))
1310 return(SSL_ERROR_WANT_WRITE);
1311 else if (BIO_should_read(bio))
1312 return(SSL_ERROR_WANT_READ);
1313 else if (BIO_should_io_special(bio))
1314 {
1315 reason=BIO_get_retry_reason(bio);
1316 if (reason == BIO_RR_CONNECT)
1317 return(SSL_ERROR_WANT_CONNECT);
1318 else
1319 return(SSL_ERROR_SYSCALL);
1320 }
1321 }
1322 if ((i < 0) && SSL_want_x509_lookup(s))
1323 {
1324 return(SSL_ERROR_WANT_X509_LOOKUP);
1325 }
1326
1327 if (i == 0)
1328 {
58964a49 1329 if (s->version == SSL2_VERSION)
d02b48c6
RE
1330 {
1331 /* assume it is the socket being closed */
1332 return(SSL_ERROR_ZERO_RETURN);
1333 }
1334 else
1335 {
1336 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
58964a49 1337 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
d02b48c6
RE
1338 return(SSL_ERROR_ZERO_RETURN);
1339 }
1340 }
1341 return(SSL_ERROR_SYSCALL);
1342 }
1343
4f43d0e7 1344int SSL_do_handshake(SSL *s)
d02b48c6 1345 {
58964a49
RE
1346 int ret=1;
1347
d02b48c6
RE
1348 if (s->handshake_func == NULL)
1349 {
58964a49 1350 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
d02b48c6
RE
1351 return(-1);
1352 }
dfeab068
RE
1353
1354 s->method->ssl_renegotiate_check(s);
1355
d02b48c6 1356 if (SSL_in_init(s) || SSL_in_before(s))
58964a49
RE
1357 {
1358 ret=s->handshake_func(s);
1359 }
1360 return(ret);
d02b48c6
RE
1361 }
1362
1363/* For the next 2 functions, SSL_clear() sets shutdown and so
1364 * one of these calls will reset it */
4f43d0e7 1365void SSL_set_accept_state(SSL *s)
d02b48c6 1366 {
413c4f45 1367 s->server=1;
d02b48c6
RE
1368 s->shutdown=0;
1369 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1370 s->handshake_func=s->method->ssl_accept;
1371 /* clear the current cipher */
1372 ssl_clear_cipher_ctx(s);
1373 }
1374
4f43d0e7 1375void SSL_set_connect_state(SSL *s)
d02b48c6 1376 {
413c4f45 1377 s->server=0;
d02b48c6
RE
1378 s->shutdown=0;
1379 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1380 s->handshake_func=s->method->ssl_connect;
1381 /* clear the current cipher */
1382 ssl_clear_cipher_ctx(s);
1383 }
1384
4f43d0e7 1385int ssl_undefined_function(SSL *s)
d02b48c6
RE
1386 {
1387 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1388 return(0);
1389 }
1390
4f43d0e7 1391SSL_METHOD *ssl_bad_method(int ver)
d02b48c6
RE
1392 {
1393 SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1394 return(NULL);
1395 }
1396
4f43d0e7 1397char *SSL_get_version(SSL *s)
d02b48c6 1398 {
58964a49
RE
1399 if (s->version == TLS1_VERSION)
1400 return("TLSv1");
1401 else if (s->version == SSL3_VERSION)
d02b48c6 1402 return("SSLv3");
58964a49 1403 else if (s->version == SSL2_VERSION)
d02b48c6
RE
1404 return("SSLv2");
1405 else
1406 return("unknown");
1407 }
1408
4f43d0e7 1409SSL *SSL_dup(SSL *s)
d02b48c6
RE
1410 {
1411 STACK *sk;
1412 X509_NAME *xn;
1413 SSL *ret;
1414 int i;
1415
1416 if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1417
1418 /* This copies version, session-id, SSL_METHOD and 'cert' */
1419 SSL_copy_session_id(ret,s);
1420
1421 SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1422 SSL_set_verify(ret,SSL_get_verify_mode(s),
1423 SSL_get_verify_callback(s));
1424
1425 SSL_set_info_callback(ret,SSL_get_info_callback(s));
1426
1427 ret->debug=s->debug;
58964a49 1428 ret->options=s->options;
d02b48c6
RE
1429
1430 /* copy app data, a little dangerous perhaps */
58964a49
RE
1431 if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data))
1432 goto err;
d02b48c6
RE
1433
1434 /* setup rbio, and wbio */
1435 if (s->rbio != NULL)
1436 {
1437 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1438 goto err;
1439 }
1440 if (s->wbio != NULL)
1441 {
1442 if (s->wbio != s->rbio)
1443 {
58964a49 1444 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
d02b48c6
RE
1445 goto err;
1446 }
1447 else
1448 ret->wbio=ret->rbio;
1449 }
1450
1451 /* dup the cipher_list and cipher_list_by_id stacks */
1452 if (s->cipher_list != NULL)
1453 {
1454 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1455 goto err;
1456 }
1457 if (s->cipher_list_by_id != NULL)
1458 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1459 == NULL)
1460 goto err;
1461
1462 /* Dup the client_CA list */
1463 if (s->client_CA != NULL)
1464 {
1465 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1466 ret->client_CA=sk;
1467 for (i=0; i<sk_num(sk); i++)
1468 {
1469 xn=(X509_NAME *)sk_value(sk,i);
1470 if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1471 {
1472 X509_NAME_free(xn);
1473 goto err;
1474 }
1475 }
1476 }
1477
1478 ret->shutdown=s->shutdown;
1479 ret->state=s->state;
1480 ret->handshake_func=s->handshake_func;
413c4f45 1481 ret->server=s->server;
d02b48c6
RE
1482
1483 if (0)
1484 {
1485err:
1486 if (ret != NULL) SSL_free(ret);
1487 ret=NULL;
1488 }
1489 return(ret);
1490 }
1491
4f43d0e7 1492void ssl_clear_cipher_ctx(SSL *s)
d02b48c6
RE
1493 {
1494 if (s->enc_read_ctx != NULL)
1495 {
1496 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1497 Free(s->enc_read_ctx);
1498 s->enc_read_ctx=NULL;
1499 }
1500 if (s->enc_write_ctx != NULL)
1501 {
1502 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1503 Free(s->enc_write_ctx);
1504 s->enc_write_ctx=NULL;
1505 }
413c4f45
MC
1506 if (s->expand != NULL)
1507 {
1508 COMP_CTX_free(s->expand);
1509 s->expand=NULL;
1510 }
1511 if (s->compress != NULL)
1512 {
1513 COMP_CTX_free(s->compress);
1514 s->compress=NULL;
1515 }
d02b48c6
RE
1516 }
1517
58964a49 1518/* Fix this function so that it takes an optional type parameter */
4f43d0e7 1519X509 *SSL_get_certificate(SSL *s)
d02b48c6
RE
1520 {
1521 if (s->cert != NULL)
1522 return(s->cert->key->x509);
1523 else
1524 return(NULL);
1525 }
1526
58964a49 1527/* Fix this function so that it takes an optional type parameter */
4f43d0e7 1528EVP_PKEY *SSL_get_privatekey(SSL *s)
d02b48c6
RE
1529 {
1530 if (s->cert != NULL)
1531 return(s->cert->key->privatekey);
1532 else
1533 return(NULL);
1534 }
1535
4f43d0e7 1536SSL_CIPHER *SSL_get_current_cipher(SSL *s)
d02b48c6
RE
1537 {
1538 if ((s->session != NULL) && (s->session->cipher != NULL))
1539 return(s->session->cipher);
1540 return(NULL);
1541 }
1542
4f43d0e7 1543int ssl_init_wbio_buffer(SSL *s,int push)
58964a49
RE
1544 {
1545 BIO *bbio;
1546
1547 if (s->bbio == NULL)
1548 {
1549 bbio=BIO_new(BIO_f_buffer());
1550 if (bbio == NULL) return(0);
1551 s->bbio=bbio;
1552 }
1553 else
1554 {
1555 bbio=s->bbio;
1556 if (s->bbio == s->wbio)
1557 s->wbio=BIO_pop(s->wbio);
1558 }
1559 BIO_reset(bbio);
1560/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
1561 if (!BIO_set_read_buffer_size(bbio,1))
1562 {
1563 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
1564 return(0);
1565 }
1566 if (push)
1567 {
1568 if (s->wbio != bbio)
1569 s->wbio=BIO_push(bbio,s->wbio);
1570 }
1571 else
1572 {
1573 if (s->wbio == bbio)
1574 s->wbio=BIO_pop(bbio);
1575 }
1576 return(1);
1577 }
413c4f45 1578
4f43d0e7 1579void ssl_free_wbio_buffer(SSL *s)
413c4f45
MC
1580 {
1581 BIO *under;
1582
1583 if (s->bbio == NULL) return;
1584
1585 if (s->bbio == s->wbio)
1586 {
1587 /* remove buffering */
1588 under=BIO_pop(s->wbio);
1589 if (under != NULL)
1590 s->wbio=under;
1591 else
1592 abort(); /* ok */
1593 }
1594 BIO_free(s->bbio);
1595 s->bbio=NULL;
1596 }
58964a49 1597
4f43d0e7 1598void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
58964a49
RE
1599 {
1600 ctx->quiet_shutdown=mode;
1601 }
1602
4f43d0e7 1603int SSL_CTX_get_quiet_shutdown(SSL_CTX *ctx)
58964a49
RE
1604 {
1605 return(ctx->quiet_shutdown);
1606 }
1607
4f43d0e7 1608void SSL_set_quiet_shutdown(SSL *s,int mode)
58964a49
RE
1609 {
1610 s->quiet_shutdown=mode;
1611 }
1612
4f43d0e7 1613int SSL_get_quiet_shutdown(SSL *s)
58964a49
RE
1614 {
1615 return(s->quiet_shutdown);
1616 }
1617
4f43d0e7 1618void SSL_set_shutdown(SSL *s,int mode)
58964a49
RE
1619 {
1620 s->shutdown=mode;
1621 }
1622
4f43d0e7 1623int SSL_get_shutdown(SSL *s)
58964a49
RE
1624 {
1625 return(s->shutdown);
1626 }
1627
4f43d0e7 1628int SSL_version(SSL *s)
58964a49
RE
1629 {
1630 return(s->version);
1631 }
1632
4f43d0e7 1633SSL_CTX *SSL_get_SSL_CTX(SSL *ssl)
58964a49
RE
1634 {
1635 return(ssl->ctx);
1636 }
1637
dfeab068 1638#ifndef NO_STDIO
4f43d0e7 1639int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
58964a49
RE
1640 {
1641 return(X509_STORE_set_default_paths(ctx->cert_store));
1642 }
1643
4f43d0e7 1644int SSL_CTX_load_verify_locations(SSL_CTX *ctx,char *CAfile,char *CApath)
58964a49
RE
1645 {
1646 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
1647 }
dfeab068 1648#endif
58964a49 1649
4f43d0e7 1650void SSL_set_info_callback(SSL *ssl,void (*cb)())
58964a49
RE
1651 {
1652 ssl->info_callback=cb;
1653 }
1654
4f43d0e7 1655void (*SSL_get_info_callback(SSL *ssl))()
58964a49 1656 {
dfeab068 1657 return((void (*)())ssl->info_callback);
58964a49
RE
1658 }
1659
4f43d0e7 1660int SSL_state(SSL *ssl)
58964a49
RE
1661 {
1662 return(ssl->state);
1663 }
1664
4f43d0e7 1665void SSL_set_verify_result(SSL *ssl,long arg)
58964a49
RE
1666 {
1667 ssl->verify_result=arg;
1668 }
1669
4f43d0e7 1670long SSL_get_verify_result(SSL *ssl)
58964a49
RE
1671 {
1672 return(ssl->verify_result);
1673 }
1674
4f43d0e7
BL
1675int SSL_get_ex_new_index(long argl,char *argp,int (*new_func)(),
1676 int (*dup_func)(),void (*free_func)())
58964a49
RE
1677 {
1678 ssl_meth_num++;
1679 return(CRYPTO_get_ex_new_index(ssl_meth_num-1,
1680 &ssl_meth,argl,argp,new_func,dup_func,free_func));
1681 }
1682
4f43d0e7 1683int SSL_set_ex_data(SSL *s,int idx,void *arg)
58964a49
RE
1684 {
1685 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1686 }
1687
4f43d0e7 1688void *SSL_get_ex_data(SSL *s,int idx)
58964a49
RE
1689 {
1690 return(CRYPTO_get_ex_data(&s->ex_data,idx));
1691 }
1692
4f43d0e7
BL
1693int SSL_CTX_get_ex_new_index(long argl,char *argp,int (*new_func)(),
1694 int (*dup_func)(),void (*free_func)())
58964a49
RE
1695 {
1696 ssl_ctx_meth_num++;
1697 return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1,
1698 &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func));
1699 }
1700
4f43d0e7 1701int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
58964a49
RE
1702 {
1703 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1704 }
1705
4f43d0e7 1706void *SSL_CTX_get_ex_data(SSL_CTX *s,int idx)
58964a49
RE
1707 {
1708 return(CRYPTO_get_ex_data(&s->ex_data,idx));
1709 }
1710
4f43d0e7 1711int ssl_ok(SSL *s)
dfeab068
RE
1712 {
1713 return(1);
1714 }
1715
4f43d0e7 1716X509_STORE *SSL_CTX_get_cert_store(SSL_CTX *ctx)
413c4f45
MC
1717 {
1718 return(ctx->cert_store);
1719 }
1720
4f43d0e7 1721void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
413c4f45
MC
1722 {
1723 if (ctx->cert_store != NULL)
1724 X509_STORE_free(ctx->cert_store);
1725 ctx->cert_store=store;
1726 }
1727
4f43d0e7 1728int SSL_want(SSL *s)
413c4f45
MC
1729 {
1730 return(s->rwstate);
1731 }
1732
4f43d0e7
BL
1733/*!
1734 * \brief Set the callback for generating temporary RSA keys.
1735 * \param ctx the SSL context.
1736 * \param cb the callback
1737 */
1738
60e31c3a
BL
1739void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,int export,
1740 int keylength))
f8c3c05d
BL
1741 { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,0,(char *)cb); }
1742
4f43d0e7
BL
1743#ifdef DOXYGEN
1744/*!
1745 * \brief The RSA temporary key callback function.
1746 * \param ssl the SSL session.
1747 * \param export \c TRUE if the temp RSA key is for an export ciphersuite.
1748 * \param keylength if \c export is \c TRUE, then \c keylength is the size of
1749 * the required key in bits.
1750 * \return the temporary RSA key.
1751 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
1752 */
1753
1754RSA *cb(SSL *ssl,int export,int keylength)
1755 {}
1756#endif
1757
1758/*!
1759 * \brief Set the callback for generating temporary DH keys.
1760 * \param ctx the SSL context.
1761 * \param dh the callback
1762 */
1763
60e31c3a
BL
1764void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int export,
1765 int keylength))
f8c3c05d
BL
1766 { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,0,(char *)dh); }
1767
15d21c2d
RE
1768void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,int export,
1769 int keylength))
1770 { SSL_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,0,(char *)cb); }
1771
1772void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int export,
1773 int keylength))
1774 { SSL_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,0,(char *)dh); }
1775
58964a49
RE
1776#if defined(_WINDLL) && defined(WIN16)
1777#include "../crypto/bio/bss_file.c"
1778#endif