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