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