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