1 /*! \file ssl/ssl_lib.c
2 * \brief Version independent SSL functions.
4 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
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)"
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
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.]
60 /* ====================================================================
61 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
67 * 1. Redistributions of source code must retain the above copyright
68 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in
72 * the documentation and/or other materials provided with the
75 * 3. All advertising materials mentioning features or use of this
76 * software must display the following acknowledgment:
77 * "This product includes software developed by the OpenSSL Project
78 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
80 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81 * endorse or promote products derived from this software without
82 * prior written permission. For written permission, please contact
83 * openssl-core@openssl.org.
85 * 5. Products derived from this software may not be called "OpenSSL"
86 * nor may "OpenSSL" appear in their names without prior written
87 * permission of the OpenSSL Project.
89 * 6. Redistributions of any form whatsoever must retain the following
91 * "This product includes software developed by the OpenSSL Project
92 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
94 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
98 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105 * OF THE POSSIBILITY OF SUCH DAMAGE.
106 * ====================================================================
108 * This product includes cryptographic software written by Eric Young
109 * (eay@cryptsoft.com). This product includes software written by Tim
110 * Hudson (tjh@cryptsoft.com).
113 /* ====================================================================
114 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115 * ECC cipher suite support in OpenSSL originally developed by
116 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
118 /* ====================================================================
119 * Copyright 2005 Nokia. All rights reserved.
121 * The portions of the attached software ("Contribution") is developed by
122 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
125 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
126 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
127 * support (see RFC 4279) to OpenSSL.
129 * No patent licenses or other rights except those expressly stated in
130 * the OpenSSL open source license shall be deemed granted or received
131 * expressly, by implication, estoppel, or otherwise.
133 * No assurances are provided by Nokia that the Contribution does not
134 * infringe the patent or other intellectual property rights of any third
135 * party or that the license provides you with all the necessary rights
136 * to make use of the Contribution.
138 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
139 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
140 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
141 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
149 #include "ssl_locl.h"
150 #include "kssl_lcl.h"
151 #include <openssl/objects.h>
152 #include <openssl/lhash.h>
153 #include <openssl/x509v3.h>
154 #ifndef OPENSSL_NO_DH
155 #include <openssl/dh.h>
158 const char *SSL_version_str
=OPENSSL_VERSION_TEXT
;
160 SSL3_ENC_METHOD ssl3_undef_enc_method
={
161 /* evil casts, but these functions are only called if there's a library bug */
162 (int (*)(SSL
*,int))ssl_undefined_function
,
163 (int (*)(SSL
*, unsigned char *, int))ssl_undefined_function
,
164 ssl_undefined_function
,
165 (int (*)(SSL
*, unsigned char *, unsigned char *, int))ssl_undefined_function
,
166 (int (*)(SSL
*, int))ssl_undefined_function
,
167 (int (*)(SSL
*, EVP_MD_CTX
*, EVP_MD_CTX
*, const char*, int, unsigned char *))ssl_undefined_function
,
168 0, /* finish_mac_length */
169 (int (*)(SSL
*, EVP_MD_CTX
*, unsigned char *))ssl_undefined_function
,
170 NULL
, /* client_finished_label */
171 0, /* client_finished_label_len */
172 NULL
, /* server_finished_label */
173 0, /* server_finished_label_len */
174 (int (*)(int))ssl_undefined_function
177 int SSL_clear(SSL
*s
)
180 if (s
->method
== NULL
)
182 SSLerr(SSL_F_SSL_CLEAR
,SSL_R_NO_METHOD_SPECIFIED
);
186 if (ssl_clear_bad_session(s
))
188 SSL_SESSION_free(s
->session
);
196 #if 0 /* Disabled since version 1.10 of this file (early return not
197 * needed because SSL_clear is not called when doing renegotiation) */
198 /* This is set if we are doing dynamic renegotiation so keep
199 * the old cipher. It is sort of a SSL_clear_lite :-) */
200 if (s
->new_session
) return(1);
204 SSLerr(SSL_F_SSL_CLEAR
,ERR_R_INTERNAL_ERROR
);
211 s
->state
=SSL_ST_BEFORE
|((s
->server
)?SSL_ST_ACCEPT
:SSL_ST_CONNECT
);
213 s
->version
=s
->method
->version
;
214 s
->client_version
=s
->version
;
215 s
->rwstate
=SSL_NOTHING
;
216 s
->rstate
=SSL_ST_READ_HEADER
;
218 s
->read_ahead
=s
->ctx
->read_ahead
;
221 if (s
->init_buf
!= NULL
)
223 BUF_MEM_free(s
->init_buf
);
227 ssl_clear_cipher_ctx(s
);
228 ssl_clear_hash_ctx(&s
->read_hash
);
229 ssl_clear_hash_ctx(&s
->write_hash
);
234 /* Check to see if we were changed into a different method, if
235 * so, revert back if we are not doing session-id reuse. */
236 if (!s
->in_handshake
&& (s
->session
== NULL
) && (s
->method
!= s
->ctx
->method
))
238 s
->method
->ssl_free(s
);
239 s
->method
=s
->ctx
->method
;
240 if (!s
->method
->ssl_new(s
))
245 s
->method
->ssl_clear(s
);
249 /** Used to change an SSL_CTXs default SSL method type */
250 int SSL_CTX_set_ssl_version(SSL_CTX
*ctx
,const SSL_METHOD
*meth
)
252 STACK_OF(SSL_CIPHER
) *sk
;
256 sk
=ssl_create_cipher_list(ctx
->method
,&(ctx
->cipher_list
),
257 &(ctx
->cipher_list_by_id
),SSL_DEFAULT_CIPHER_LIST
);
258 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= 0))
260 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION
,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
266 SSL
*SSL_new(SSL_CTX
*ctx
)
272 SSLerr(SSL_F_SSL_NEW
,SSL_R_NULL_SSL_CTX
);
275 if (ctx
->method
== NULL
)
277 SSLerr(SSL_F_SSL_NEW
,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION
);
281 s
=(SSL
*)OPENSSL_malloc(sizeof(SSL
));
282 if (s
== NULL
) goto err
;
283 memset(s
,0,sizeof(SSL
));
285 #ifndef OPENSSL_NO_KRB5
286 s
->kssl_ctx
= kssl_ctx_new();
287 #endif /* OPENSSL_NO_KRB5 */
289 s
->options
=ctx
->options
;
291 s
->max_cert_list
=ctx
->max_cert_list
;
293 if (ctx
->cert
!= NULL
)
295 /* Earlier library versions used to copy the pointer to
296 * the CERT, not its contents; only when setting new
297 * parameters for the per-SSL copy, ssl_cert_new would be
298 * called (and the direct reference to the per-SSL_CTX
299 * settings would be lost, but those still were indirectly
300 * accessed for various purposes, and for that reason they
301 * used to be known as s->ctx->default_cert).
302 * Now we don't look at the SSL_CTX's CERT after having
303 * duplicated it once. */
305 s
->cert
= ssl_cert_dup(ctx
->cert
);
310 s
->cert
=NULL
; /* Cannot really happen (see SSL_CTX_new) */
312 s
->read_ahead
=ctx
->read_ahead
;
313 s
->msg_callback
=ctx
->msg_callback
;
314 s
->msg_callback_arg
=ctx
->msg_callback_arg
;
315 s
->verify_mode
=ctx
->verify_mode
;
317 s
->verify_depth
=ctx
->verify_depth
;
319 s
->sid_ctx_length
=ctx
->sid_ctx_length
;
320 OPENSSL_assert(s
->sid_ctx_length
<= sizeof s
->sid_ctx
);
321 memcpy(&s
->sid_ctx
,&ctx
->sid_ctx
,sizeof(s
->sid_ctx
));
322 s
->verify_callback
=ctx
->default_verify_callback
;
323 s
->generate_session_id
=ctx
->generate_session_id
;
325 s
->param
= X509_VERIFY_PARAM_new();
328 X509_VERIFY_PARAM_inherit(s
->param
, ctx
->param
);
330 s
->purpose
= ctx
->purpose
;
331 s
->trust
= ctx
->trust
;
333 s
->quiet_shutdown
=ctx
->quiet_shutdown
;
334 s
->max_send_fragment
= ctx
->max_send_fragment
;
336 CRYPTO_add(&ctx
->references
,1,CRYPTO_LOCK_SSL_CTX
);
338 #ifndef OPENSSL_NO_TLSEXT
339 CRYPTO_add(&ctx
->references
,1,CRYPTO_LOCK_SSL_CTX
);
343 s
->verify_result
=X509_V_OK
;
345 s
->method
=ctx
->method
;
347 if (!s
->method
->ssl_new(s
))
351 s
->server
=(ctx
->method
->ssl_accept
== ssl_undefined_function
)?0:1;
355 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->ex_data
);
357 #ifndef OPENSSL_NO_PSK
358 s
->psk_client_callback
=ctx
->psk_client_callback
;
359 s
->psk_server_callback
=ctx
->psk_server_callback
;
367 ssl_cert_free(s
->cert
);
369 SSL_CTX_free(s
->ctx
); /* decrement reference count */
372 SSLerr(SSL_F_SSL_NEW
,ERR_R_MALLOC_FAILURE
);
376 int SSL_CTX_set_session_id_context(SSL_CTX
*ctx
,const unsigned char *sid_ctx
,
377 unsigned int sid_ctx_len
)
379 if(sid_ctx_len
> sizeof ctx
->sid_ctx
)
381 SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT
,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
384 ctx
->sid_ctx_length
=sid_ctx_len
;
385 memcpy(ctx
->sid_ctx
,sid_ctx
,sid_ctx_len
);
390 int SSL_set_session_id_context(SSL
*ssl
,const unsigned char *sid_ctx
,
391 unsigned int sid_ctx_len
)
393 if(sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
)
395 SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT
,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
398 ssl
->sid_ctx_length
=sid_ctx_len
;
399 memcpy(ssl
->sid_ctx
,sid_ctx
,sid_ctx_len
);
404 int SSL_CTX_set_generate_session_id(SSL_CTX
*ctx
, GEN_SESSION_CB cb
)
406 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX
);
407 ctx
->generate_session_id
= cb
;
408 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX
);
412 int SSL_set_generate_session_id(SSL
*ssl
, GEN_SESSION_CB cb
)
414 CRYPTO_w_lock(CRYPTO_LOCK_SSL
);
415 ssl
->generate_session_id
= cb
;
416 CRYPTO_w_unlock(CRYPTO_LOCK_SSL
);
420 int SSL_has_matching_session_id(const SSL
*ssl
, const unsigned char *id
,
423 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
424 * we can "construct" a session to give us the desired check - ie. to
425 * find if there's a session in the hash table that would conflict with
426 * any new session built out of this id/id_len and the ssl_version in
427 * use by this SSL. */
430 if(id_len
> sizeof r
.session_id
)
433 r
.ssl_version
= ssl
->version
;
434 r
.session_id_length
= id_len
;
435 memcpy(r
.session_id
, id
, id_len
);
436 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
437 * callback is calling us to check the uniqueness of a shorter ID, it
438 * must be compared as a padded-out ID because that is what it will be
439 * converted to when the callback has finished choosing it. */
440 if((r
.ssl_version
== SSL2_VERSION
) &&
441 (id_len
< SSL2_SSL_SESSION_ID_LENGTH
))
443 memset(r
.session_id
+ id_len
, 0,
444 SSL2_SSL_SESSION_ID_LENGTH
- id_len
);
445 r
.session_id_length
= SSL2_SSL_SESSION_ID_LENGTH
;
448 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX
);
449 p
= (SSL_SESSION
*)lh_retrieve(ssl
->ctx
->sessions
, &r
);
450 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX
);
454 int SSL_CTX_set_purpose(SSL_CTX
*s
, int purpose
)
456 return X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
);
459 int SSL_set_purpose(SSL
*s
, int purpose
)
461 return X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
);
464 int SSL_CTX_set_trust(SSL_CTX
*s
, int trust
)
466 return X509_VERIFY_PARAM_set_trust(s
->param
, trust
);
469 int SSL_set_trust(SSL
*s
, int trust
)
471 return X509_VERIFY_PARAM_set_trust(s
->param
, trust
);
474 void SSL_free(SSL
*s
)
481 i
=CRYPTO_add(&s
->references
,-1,CRYPTO_LOCK_SSL
);
489 fprintf(stderr
,"SSL_free, bad reference count\n");
495 X509_VERIFY_PARAM_free(s
->param
);
497 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->ex_data
);
501 /* If the buffering BIO is in place, pop it off */
502 if (s
->bbio
== s
->wbio
)
504 s
->wbio
=BIO_pop(s
->wbio
);
510 BIO_free_all(s
->rbio
);
511 if ((s
->wbio
!= NULL
) && (s
->wbio
!= s
->rbio
))
512 BIO_free_all(s
->wbio
);
514 if (s
->init_buf
!= NULL
) BUF_MEM_free(s
->init_buf
);
516 /* add extra stuff */
517 if (s
->cipher_list
!= NULL
) sk_SSL_CIPHER_free(s
->cipher_list
);
518 if (s
->cipher_list_by_id
!= NULL
) sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
520 /* Make the next call work :-) */
521 if (s
->session
!= NULL
)
523 ssl_clear_bad_session(s
);
524 SSL_SESSION_free(s
->session
);
527 ssl_clear_cipher_ctx(s
);
528 ssl_clear_hash_ctx(&s
->read_hash
);
529 ssl_clear_hash_ctx(&s
->write_hash
);
531 if (s
->cert
!= NULL
) ssl_cert_free(s
->cert
);
532 /* Free up if allocated */
534 if (s
->ctx
) SSL_CTX_free(s
->ctx
);
535 #ifndef OPENSSL_NO_TLSEXT
536 if (s
->initial_ctx
) SSL_CTX_free(s
->initial_ctx
);
537 #ifndef OPENSSL_NO_EC
538 if (s
->tlsext_ecpointformatlist
) OPENSSL_free(s
->tlsext_ecpointformatlist
);
539 if (s
->tlsext_ellipticcurvelist
) OPENSSL_free(s
->tlsext_ellipticcurvelist
);
540 #endif /* OPENSSL_NO_EC */
543 if (s
->client_CA
!= NULL
)
544 sk_X509_NAME_pop_free(s
->client_CA
,X509_NAME_free
);
546 if (s
->method
!= NULL
) s
->method
->ssl_free(s
);
548 #ifndef OPENSSL_NO_KRB5
549 if (s
->kssl_ctx
!= NULL
)
550 kssl_ctx_free(s
->kssl_ctx
);
551 #endif /* OPENSSL_NO_KRB5 */
556 void SSL_set_bio(SSL
*s
,BIO
*rbio
,BIO
*wbio
)
558 /* If the output buffering BIO is still in place, remove it
562 if (s
->wbio
== s
->bbio
)
564 s
->wbio
=s
->wbio
->next_bio
;
565 s
->bbio
->next_bio
=NULL
;
568 if ((s
->rbio
!= NULL
) && (s
->rbio
!= rbio
))
569 BIO_free_all(s
->rbio
);
570 if ((s
->wbio
!= NULL
) && (s
->wbio
!= wbio
) && (s
->rbio
!= s
->wbio
))
571 BIO_free_all(s
->wbio
);
576 BIO
*SSL_get_rbio(const SSL
*s
)
579 BIO
*SSL_get_wbio(const SSL
*s
)
582 int SSL_get_fd(const SSL
*s
)
584 return(SSL_get_rfd(s
));
587 int SSL_get_rfd(const SSL
*s
)
593 r
=BIO_find_type(b
,BIO_TYPE_DESCRIPTOR
);
599 int SSL_get_wfd(const SSL
*s
)
605 r
=BIO_find_type(b
,BIO_TYPE_DESCRIPTOR
);
611 #ifndef OPENSSL_NO_SOCK
612 int SSL_set_fd(SSL
*s
,int fd
)
617 bio
=BIO_new(BIO_s_socket());
621 SSLerr(SSL_F_SSL_SET_FD
,ERR_R_BUF_LIB
);
624 BIO_set_fd(bio
,fd
,BIO_NOCLOSE
);
625 SSL_set_bio(s
,bio
,bio
);
631 int SSL_set_wfd(SSL
*s
,int fd
)
636 if ((s
->rbio
== NULL
) || (BIO_method_type(s
->rbio
) != BIO_TYPE_SOCKET
)
637 || ((int)BIO_get_fd(s
->rbio
,NULL
) != fd
))
639 bio
=BIO_new(BIO_s_socket());
642 { SSLerr(SSL_F_SSL_SET_WFD
,ERR_R_BUF_LIB
); goto err
; }
643 BIO_set_fd(bio
,fd
,BIO_NOCLOSE
);
644 SSL_set_bio(s
,SSL_get_rbio(s
),bio
);
647 SSL_set_bio(s
,SSL_get_rbio(s
),SSL_get_rbio(s
));
653 int SSL_set_rfd(SSL
*s
,int fd
)
658 if ((s
->wbio
== NULL
) || (BIO_method_type(s
->wbio
) != BIO_TYPE_SOCKET
)
659 || ((int)BIO_get_fd(s
->wbio
,NULL
) != fd
))
661 bio
=BIO_new(BIO_s_socket());
665 SSLerr(SSL_F_SSL_SET_RFD
,ERR_R_BUF_LIB
);
668 BIO_set_fd(bio
,fd
,BIO_NOCLOSE
);
669 SSL_set_bio(s
,bio
,SSL_get_wbio(s
));
672 SSL_set_bio(s
,SSL_get_wbio(s
),SSL_get_wbio(s
));
680 /* return length of latest Finished message we sent, copy to 'buf' */
681 size_t SSL_get_finished(const SSL
*s
, void *buf
, size_t count
)
687 ret
= s
->s3
->tmp
.finish_md_len
;
690 memcpy(buf
, s
->s3
->tmp
.finish_md
, count
);
695 /* return length of latest Finished message we expected, copy to 'buf' */
696 size_t SSL_get_peer_finished(const SSL
*s
, void *buf
, size_t count
)
702 ret
= s
->s3
->tmp
.peer_finish_md_len
;
705 memcpy(buf
, s
->s3
->tmp
.peer_finish_md
, count
);
711 int SSL_get_verify_mode(const SSL
*s
)
713 return(s
->verify_mode
);
716 int SSL_get_verify_depth(const SSL
*s
)
718 return X509_VERIFY_PARAM_get_depth(s
->param
);
721 int (*SSL_get_verify_callback(const SSL
*s
))(int,X509_STORE_CTX
*)
723 return(s
->verify_callback
);
726 int SSL_CTX_get_verify_mode(const SSL_CTX
*ctx
)
728 return(ctx
->verify_mode
);
731 int SSL_CTX_get_verify_depth(const SSL_CTX
*ctx
)
733 return X509_VERIFY_PARAM_get_depth(ctx
->param
);
736 int (*SSL_CTX_get_verify_callback(const SSL_CTX
*ctx
))(int,X509_STORE_CTX
*)
738 return(ctx
->default_verify_callback
);
741 void SSL_set_verify(SSL
*s
,int mode
,
742 int (*callback
)(int ok
,X509_STORE_CTX
*ctx
))
745 if (callback
!= NULL
)
746 s
->verify_callback
=callback
;
749 void SSL_set_verify_depth(SSL
*s
,int depth
)
751 X509_VERIFY_PARAM_set_depth(s
->param
, depth
);
754 void SSL_set_read_ahead(SSL
*s
,int yes
)
759 int SSL_get_read_ahead(const SSL
*s
)
761 return(s
->read_ahead
);
764 int SSL_pending(const SSL
*s
)
766 /* SSL_pending cannot work properly if read-ahead is enabled
767 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
768 * and it is impossible to fix since SSL_pending cannot report
769 * errors that may be observed while scanning the new data.
770 * (Note that SSL_pending() is often used as a boolean value,
771 * so we'd better not return -1.)
773 return(s
->method
->ssl_pending(s
));
776 X509
*SSL_get_peer_certificate(const SSL
*s
)
780 if ((s
== NULL
) || (s
->session
== NULL
))
785 if (r
== NULL
) return(r
);
787 CRYPTO_add(&r
->references
,1,CRYPTO_LOCK_X509
);
792 STACK_OF(X509
) *SSL_get_peer_cert_chain(const SSL
*s
)
796 if ((s
== NULL
) || (s
->session
== NULL
) || (s
->session
->sess_cert
== NULL
))
799 r
=s
->session
->sess_cert
->cert_chain
;
801 /* If we are a client, cert_chain includes the peer's own
802 * certificate; if we are a server, it does not. */
807 /* Now in theory, since the calling process own 't' it should be safe to
808 * modify. We need to be able to read f without being hassled */
809 void SSL_copy_session_id(SSL
*t
,const SSL
*f
)
813 /* Do we need to to SSL locking? */
814 SSL_set_session(t
,SSL_get_session(f
));
816 /* what if we are setup as SSLv2 but want to talk SSLv3 or
818 if (t
->method
!= f
->method
)
820 t
->method
->ssl_free(t
); /* cleanup current */
821 t
->method
=f
->method
; /* change method */
822 t
->method
->ssl_new(t
); /* setup new */
828 CRYPTO_add(&f
->cert
->references
,1,CRYPTO_LOCK_SSL_CERT
);
833 if (tmp
!= NULL
) ssl_cert_free(tmp
);
834 SSL_set_session_id_context(t
,f
->sid_ctx
,f
->sid_ctx_length
);
837 /* Fix this so it checks all the valid key/cert options */
838 int SSL_CTX_check_private_key(const SSL_CTX
*ctx
)
840 if ( (ctx
== NULL
) ||
841 (ctx
->cert
== NULL
) ||
842 (ctx
->cert
->key
->x509
== NULL
))
844 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY
,SSL_R_NO_CERTIFICATE_ASSIGNED
);
847 if (ctx
->cert
->key
->privatekey
== NULL
)
849 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY
,SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
852 return(X509_check_private_key(ctx
->cert
->key
->x509
, ctx
->cert
->key
->privatekey
));
855 /* Fix this function so that it takes an optional type parameter */
856 int SSL_check_private_key(const SSL
*ssl
)
860 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
,ERR_R_PASSED_NULL_PARAMETER
);
863 if (ssl
->cert
== NULL
)
865 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
,SSL_R_NO_CERTIFICATE_ASSIGNED
);
868 if (ssl
->cert
->key
->x509
== NULL
)
870 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
,SSL_R_NO_CERTIFICATE_ASSIGNED
);
873 if (ssl
->cert
->key
->privatekey
== NULL
)
875 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
,SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
878 return(X509_check_private_key(ssl
->cert
->key
->x509
,
879 ssl
->cert
->key
->privatekey
));
882 int SSL_accept(SSL
*s
)
884 if (s
->handshake_func
== 0)
885 /* Not properly initialized yet */
886 SSL_set_accept_state(s
);
888 return(s
->method
->ssl_accept(s
));
891 int SSL_connect(SSL
*s
)
893 if (s
->handshake_func
== 0)
894 /* Not properly initialized yet */
895 SSL_set_connect_state(s
);
897 return(s
->method
->ssl_connect(s
));
900 long SSL_get_default_timeout(const SSL
*s
)
902 return(s
->method
->get_timeout());
905 int SSL_read(SSL
*s
,void *buf
,int num
)
907 if (s
->handshake_func
== 0)
909 SSLerr(SSL_F_SSL_READ
, SSL_R_UNINITIALIZED
);
913 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
)
915 s
->rwstate
=SSL_NOTHING
;
918 return(s
->method
->ssl_read(s
,buf
,num
));
921 int SSL_peek(SSL
*s
,void *buf
,int num
)
923 if (s
->handshake_func
== 0)
925 SSLerr(SSL_F_SSL_PEEK
, SSL_R_UNINITIALIZED
);
929 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
)
933 return(s
->method
->ssl_peek(s
,buf
,num
));
936 int SSL_write(SSL
*s
,const void *buf
,int num
)
938 if (s
->handshake_func
== 0)
940 SSLerr(SSL_F_SSL_WRITE
, SSL_R_UNINITIALIZED
);
944 if (s
->shutdown
& SSL_SENT_SHUTDOWN
)
946 s
->rwstate
=SSL_NOTHING
;
947 SSLerr(SSL_F_SSL_WRITE
,SSL_R_PROTOCOL_IS_SHUTDOWN
);
950 return(s
->method
->ssl_write(s
,buf
,num
));
953 int SSL_shutdown(SSL
*s
)
955 /* Note that this function behaves differently from what one might
956 * expect. Return values are 0 for no success (yet),
957 * 1 for success; but calling it once is usually not enough,
958 * even if blocking I/O is used (see ssl3_shutdown).
961 if (s
->handshake_func
== 0)
963 SSLerr(SSL_F_SSL_SHUTDOWN
, SSL_R_UNINITIALIZED
);
967 if ((s
!= NULL
) && !SSL_in_init(s
))
968 return(s
->method
->ssl_shutdown(s
));
973 int SSL_renegotiate(SSL
*s
)
975 if (s
->new_session
== 0)
979 return(s
->method
->ssl_renegotiate(s
));
982 int SSL_renegotiate_pending(SSL
*s
)
984 /* becomes true when negotiation is requested;
985 * false again once a handshake has finished */
986 return (s
->new_session
!= 0);
989 long SSL_ctrl(SSL
*s
,int cmd
,long larg
,void *parg
)
995 case SSL_CTRL_GET_READ_AHEAD
:
996 return(s
->read_ahead
);
997 case SSL_CTRL_SET_READ_AHEAD
:
1002 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1003 s
->msg_callback_arg
= parg
;
1006 case SSL_CTRL_OPTIONS
:
1007 return(s
->options
|=larg
);
1009 return(s
->mode
|=larg
);
1010 case SSL_CTRL_GET_MAX_CERT_LIST
:
1011 return(s
->max_cert_list
);
1012 case SSL_CTRL_SET_MAX_CERT_LIST
:
1014 s
->max_cert_list
=larg
;
1016 case SSL_CTRL_SET_MTU
:
1017 if (SSL_version(s
) == DTLS1_VERSION
)
1023 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1024 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
1026 s
->max_send_fragment
= larg
;
1029 return(s
->method
->ssl_ctrl(s
,cmd
,larg
,parg
));
1033 long SSL_callback_ctrl(SSL
*s
, int cmd
, void (*fp
)(void))
1037 case SSL_CTRL_SET_MSG_CALLBACK
:
1038 s
->msg_callback
= (void (*)(int write_p
, int version
, int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))(fp
);
1042 return(s
->method
->ssl_callback_ctrl(s
,cmd
,fp
));
1046 struct lhash_st
*SSL_CTX_sessions(SSL_CTX
*ctx
)
1048 return ctx
->sessions
;
1051 long SSL_CTX_ctrl(SSL_CTX
*ctx
,int cmd
,long larg
,void *parg
)
1057 case SSL_CTRL_GET_READ_AHEAD
:
1058 return(ctx
->read_ahead
);
1059 case SSL_CTRL_SET_READ_AHEAD
:
1061 ctx
->read_ahead
=larg
;
1064 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
1065 ctx
->msg_callback_arg
= parg
;
1068 case SSL_CTRL_GET_MAX_CERT_LIST
:
1069 return(ctx
->max_cert_list
);
1070 case SSL_CTRL_SET_MAX_CERT_LIST
:
1071 l
=ctx
->max_cert_list
;
1072 ctx
->max_cert_list
=larg
;
1075 case SSL_CTRL_SET_SESS_CACHE_SIZE
:
1076 l
=ctx
->session_cache_size
;
1077 ctx
->session_cache_size
=larg
;
1079 case SSL_CTRL_GET_SESS_CACHE_SIZE
:
1080 return(ctx
->session_cache_size
);
1081 case SSL_CTRL_SET_SESS_CACHE_MODE
:
1082 l
=ctx
->session_cache_mode
;
1083 ctx
->session_cache_mode
=larg
;
1085 case SSL_CTRL_GET_SESS_CACHE_MODE
:
1086 return(ctx
->session_cache_mode
);
1088 case SSL_CTRL_SESS_NUMBER
:
1089 return(ctx
->sessions
->num_items
);
1090 case SSL_CTRL_SESS_CONNECT
:
1091 return(ctx
->stats
.sess_connect
);
1092 case SSL_CTRL_SESS_CONNECT_GOOD
:
1093 return(ctx
->stats
.sess_connect_good
);
1094 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE
:
1095 return(ctx
->stats
.sess_connect_renegotiate
);
1096 case SSL_CTRL_SESS_ACCEPT
:
1097 return(ctx
->stats
.sess_accept
);
1098 case SSL_CTRL_SESS_ACCEPT_GOOD
:
1099 return(ctx
->stats
.sess_accept_good
);
1100 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE
:
1101 return(ctx
->stats
.sess_accept_renegotiate
);
1102 case SSL_CTRL_SESS_HIT
:
1103 return(ctx
->stats
.sess_hit
);
1104 case SSL_CTRL_SESS_CB_HIT
:
1105 return(ctx
->stats
.sess_cb_hit
);
1106 case SSL_CTRL_SESS_MISSES
:
1107 return(ctx
->stats
.sess_miss
);
1108 case SSL_CTRL_SESS_TIMEOUTS
:
1109 return(ctx
->stats
.sess_timeout
);
1110 case SSL_CTRL_SESS_CACHE_FULL
:
1111 return(ctx
->stats
.sess_cache_full
);
1112 case SSL_CTRL_OPTIONS
:
1113 return(ctx
->options
|=larg
);
1115 return(ctx
->mode
|=larg
);
1116 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
1117 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
1119 ctx
->max_send_fragment
= larg
;
1122 return(ctx
->method
->ssl_ctx_ctrl(ctx
,cmd
,larg
,parg
));
1126 long SSL_CTX_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
)(void))
1130 case SSL_CTRL_SET_MSG_CALLBACK
:
1131 ctx
->msg_callback
= (void (*)(int write_p
, int version
, int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))(fp
);
1135 return(ctx
->method
->ssl_ctx_callback_ctrl(ctx
,cmd
,fp
));
1139 int ssl_cipher_id_cmp(const SSL_CIPHER
*a
, const SSL_CIPHER
*b
)
1147 return((l
> 0)?1:-1);
1150 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER
* const *ap
,
1151 const SSL_CIPHER
* const *bp
)
1155 l
=(*ap
)->id
-(*bp
)->id
;
1159 return((l
> 0)?1:-1);
1162 /** return a STACK of the ciphers available for the SSL and in order of
1164 STACK_OF(SSL_CIPHER
) *SSL_get_ciphers(const SSL
*s
)
1168 if (s
->cipher_list
!= NULL
)
1170 return(s
->cipher_list
);
1172 else if ((s
->ctx
!= NULL
) &&
1173 (s
->ctx
->cipher_list
!= NULL
))
1175 return(s
->ctx
->cipher_list
);
1181 /** return a STACK of the ciphers available for the SSL and in order of
1183 STACK_OF(SSL_CIPHER
) *ssl_get_ciphers_by_id(SSL
*s
)
1187 if (s
->cipher_list_by_id
!= NULL
)
1189 return(s
->cipher_list_by_id
);
1191 else if ((s
->ctx
!= NULL
) &&
1192 (s
->ctx
->cipher_list_by_id
!= NULL
))
1194 return(s
->ctx
->cipher_list_by_id
);
1200 /** The old interface to get the same thing as SSL_get_ciphers() */
1201 const char *SSL_get_cipher_list(const SSL
*s
,int n
)
1204 STACK_OF(SSL_CIPHER
) *sk
;
1206 if (s
== NULL
) return(NULL
);
1207 sk
=SSL_get_ciphers(s
);
1208 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= n
))
1210 c
=sk_SSL_CIPHER_value(sk
,n
);
1211 if (c
== NULL
) return(NULL
);
1215 /** specify the ciphers to be used by default by the SSL_CTX */
1216 int SSL_CTX_set_cipher_list(SSL_CTX
*ctx
, const char *str
)
1218 STACK_OF(SSL_CIPHER
) *sk
;
1220 sk
=ssl_create_cipher_list(ctx
->method
,&ctx
->cipher_list
,
1221 &ctx
->cipher_list_by_id
,str
);
1222 /* ssl_create_cipher_list may return an empty stack if it
1223 * was unable to find a cipher matching the given rule string
1224 * (for example if the rule string specifies a cipher which
1225 * has been disabled). This is not an error as far as
1226 * ssl_create_cipher_list is concerned, and hence
1227 * ctx->cipher_list and ctx->cipher_list_by_id has been
1231 else if (sk_SSL_CIPHER_num(sk
) == 0)
1233 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST
, SSL_R_NO_CIPHER_MATCH
);
1239 /** specify the ciphers to be used by the SSL */
1240 int SSL_set_cipher_list(SSL
*s
,const char *str
)
1242 STACK_OF(SSL_CIPHER
) *sk
;
1244 sk
=ssl_create_cipher_list(s
->ctx
->method
,&s
->cipher_list
,
1245 &s
->cipher_list_by_id
,str
);
1246 /* see comment in SSL_CTX_set_cipher_list */
1249 else if (sk_SSL_CIPHER_num(sk
) == 0)
1251 SSLerr(SSL_F_SSL_SET_CIPHER_LIST
, SSL_R_NO_CIPHER_MATCH
);
1257 /* works well for SSLv2, not so good for SSLv3 */
1258 char *SSL_get_shared_ciphers(const SSL
*s
,char *buf
,int len
)
1262 STACK_OF(SSL_CIPHER
) *sk
;
1266 if ((s
->session
== NULL
) || (s
->session
->ciphers
== NULL
) ||
1271 sk
=s
->session
->ciphers
;
1272 for (i
=0; i
<sk_SSL_CIPHER_num(sk
); i
++)
1274 /* Decrement for either the ':' or a '\0' */
1276 c
=sk_SSL_CIPHER_value(sk
,i
);
1277 for (cp
=c
->name
; *cp
; )
1293 int ssl_cipher_list_to_bytes(SSL
*s
,STACK_OF(SSL_CIPHER
) *sk
,unsigned char *p
,
1294 int (*put_cb
)(const SSL_CIPHER
*, unsigned char *))
1299 #ifndef OPENSSL_NO_KRB5
1300 int nokrb5
= !kssl_tgt_is_available(s
->kssl_ctx
);
1301 #endif /* OPENSSL_NO_KRB5 */
1303 if (sk
== NULL
) return(0);
1306 for (i
=0; i
<sk_SSL_CIPHER_num(sk
); i
++)
1308 c
=sk_SSL_CIPHER_value(sk
,i
);
1309 #ifndef OPENSSL_NO_KRB5
1310 if (((c
->algorithm_mkey
& SSL_kKRB5
) || (c
->algorithm_auth
& SSL_aKRB5
)) &&
1313 #endif /* OPENSSL_NO_KRB5 */
1314 #ifndef OPENSSL_NO_PSK
1315 /* with PSK there must be client callback set */
1316 if (((c
->algorithm_mkey
& SSL_kPSK
) || (c
->algorithm_auth
& SSL_aPSK
)) &&
1317 s
->psk_client_callback
== NULL
)
1319 #endif /* OPENSSL_NO_PSK */
1320 j
= put_cb
? put_cb(c
,p
) : ssl_put_cipher_by_char(s
,c
,p
);
1326 STACK_OF(SSL_CIPHER
) *ssl_bytes_to_cipher_list(SSL
*s
,unsigned char *p
,int num
,
1327 STACK_OF(SSL_CIPHER
) **skp
)
1330 STACK_OF(SSL_CIPHER
) *sk
;
1333 n
=ssl_put_cipher_by_char(s
,NULL
,NULL
);
1336 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
1339 if ((skp
== NULL
) || (*skp
== NULL
))
1340 sk
=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1344 sk_SSL_CIPHER_zero(sk
);
1347 for (i
=0; i
<num
; i
+=n
)
1349 c
=ssl_get_cipher_by_char(s
,p
);
1353 if (!sk_SSL_CIPHER_push(sk
,c
))
1355 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST
,ERR_R_MALLOC_FAILURE
);
1365 if ((skp
== NULL
) || (*skp
== NULL
))
1366 sk_SSL_CIPHER_free(sk
);
1371 #ifndef OPENSSL_TLSEXT
1372 /** return a servername extension value if provided in Client Hello, or NULL.
1373 * So far, only host_name types are defined (RFC 3546).
1376 const char *SSL_get_servername(const SSL
*s
, const int type
)
1378 if (type
!= TLSEXT_NAMETYPE_host_name
)
1381 return s
->session
&& !s
->tlsext_hostname
?
1382 s
->session
->tlsext_hostname
:
1386 int SSL_get_servername_type(const SSL
*s
)
1388 if (s
->session
&& (!s
->tlsext_hostname
? s
->session
->tlsext_hostname
: s
->tlsext_hostname
))
1389 return TLSEXT_NAMETYPE_host_name
;
1394 unsigned long SSL_SESSION_hash(const SSL_SESSION
*a
)
1399 ((unsigned int) a
->session_id
[0] )|
1400 ((unsigned int) a
->session_id
[1]<< 8L)|
1401 ((unsigned long)a
->session_id
[2]<<16L)|
1402 ((unsigned long)a
->session_id
[3]<<24L);
1406 /* NB: If this function (or indeed the hash function which uses a sort of
1407 * coarser function than this one) is changed, ensure
1408 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1409 * able to construct an SSL_SESSION that will collide with any existing session
1410 * with a matching session ID. */
1411 int SSL_SESSION_cmp(const SSL_SESSION
*a
,const SSL_SESSION
*b
)
1413 if (a
->ssl_version
!= b
->ssl_version
)
1415 if (a
->session_id_length
!= b
->session_id_length
)
1417 return(memcmp(a
->session_id
,b
->session_id
,a
->session_id_length
));
1420 /* These wrapper functions should remain rather than redeclaring
1421 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1422 * variable. The reason is that the functions aren't static, they're exposed via
1424 static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash
, SSL_SESSION
*)
1425 static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp
, SSL_SESSION
*)
1427 SSL_CTX
*SSL_CTX_new(const SSL_METHOD
*meth
)
1433 SSLerr(SSL_F_SSL_CTX_NEW
,SSL_R_NULL_SSL_METHOD_PASSED
);
1437 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1439 SSLerr(SSL_F_SSL_CTX_NEW
,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS
);
1442 ret
=(SSL_CTX
*)OPENSSL_malloc(sizeof(SSL_CTX
));
1446 memset(ret
,0,sizeof(SSL_CTX
));
1450 ret
->cert_store
=NULL
;
1451 ret
->session_cache_mode
=SSL_SESS_CACHE_SERVER
;
1452 ret
->session_cache_size
=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT
;
1453 ret
->session_cache_head
=NULL
;
1454 ret
->session_cache_tail
=NULL
;
1456 /* We take the system default */
1457 ret
->session_timeout
=meth
->get_timeout();
1459 ret
->new_session_cb
=0;
1460 ret
->remove_session_cb
=0;
1461 ret
->get_session_cb
=0;
1462 ret
->generate_session_id
=0;
1464 memset((char *)&ret
->stats
,0,sizeof(ret
->stats
));
1467 ret
->quiet_shutdown
=0;
1469 /* ret->cipher=NULL;*/
1470 /* ret->s2->challenge=NULL;
1471 ret->master_key=NULL;
1473 ret->s2->conn_id=NULL; */
1475 ret
->info_callback
=NULL
;
1477 ret
->app_verify_callback
=0;
1478 ret
->app_verify_arg
=NULL
;
1480 ret
->max_cert_list
=SSL_MAX_CERT_LIST_DEFAULT
;
1482 ret
->msg_callback
=0;
1483 ret
->msg_callback_arg
=NULL
;
1484 ret
->verify_mode
=SSL_VERIFY_NONE
;
1486 ret
->verify_depth
=-1; /* Don't impose a limit (but x509_lu.c does) */
1488 ret
->sid_ctx_length
=0;
1489 ret
->default_verify_callback
=NULL
;
1490 if ((ret
->cert
=ssl_cert_new()) == NULL
)
1493 ret
->default_passwd_callback
=0;
1494 ret
->default_passwd_callback_userdata
=NULL
;
1495 ret
->client_cert_cb
=0;
1496 ret
->app_gen_cookie_cb
=0;
1497 ret
->app_verify_cookie_cb
=0;
1499 ret
->sessions
=lh_new(LHASH_HASH_FN(SSL_SESSION_hash
),
1500 LHASH_COMP_FN(SSL_SESSION_cmp
));
1501 if (ret
->sessions
== NULL
) goto err
;
1502 ret
->cert_store
=X509_STORE_new();
1503 if (ret
->cert_store
== NULL
) goto err
;
1505 ssl_create_cipher_list(ret
->method
,
1506 &ret
->cipher_list
,&ret
->cipher_list_by_id
,
1507 SSL_DEFAULT_CIPHER_LIST
);
1508 if (ret
->cipher_list
== NULL
1509 || sk_SSL_CIPHER_num(ret
->cipher_list
) <= 0)
1511 SSLerr(SSL_F_SSL_CTX_NEW
,SSL_R_LIBRARY_HAS_NO_CIPHERS
);
1515 ret
->param
= X509_VERIFY_PARAM_new();
1519 if ((ret
->rsa_md5
=EVP_get_digestbyname("ssl2-md5")) == NULL
)
1521 SSLerr(SSL_F_SSL_CTX_NEW
,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES
);
1524 if ((ret
->md5
=EVP_get_digestbyname("ssl3-md5")) == NULL
)
1526 SSLerr(SSL_F_SSL_CTX_NEW
,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES
);
1529 if ((ret
->sha1
=EVP_get_digestbyname("ssl3-sha1")) == NULL
)
1531 SSLerr(SSL_F_SSL_CTX_NEW
,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES
);
1535 if ((ret
->client_CA
=sk_X509_NAME_new_null()) == NULL
)
1538 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ret
, &ret
->ex_data
);
1540 ret
->extra_certs
=NULL
;
1541 ret
->comp_methods
=SSL_COMP_get_compression_methods();
1543 ret
->max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
1545 #ifndef OPENSSL_NO_TLSEXT
1546 ret
->tlsext_servername_callback
= 0;
1547 ret
->tlsext_servername_arg
= NULL
;
1549 #ifndef OPENSSL_NO_PSK
1550 ret
->psk_identity_hint
=NULL
;
1551 ret
->psk_client_callback
=NULL
;
1552 ret
->psk_server_callback
=NULL
;
1556 SSLerr(SSL_F_SSL_CTX_NEW
,ERR_R_MALLOC_FAILURE
);
1558 if (ret
!= NULL
) SSL_CTX_free(ret
);
1563 static void SSL_COMP_free(SSL_COMP
*comp
)
1564 { OPENSSL_free(comp
); }
1567 void SSL_CTX_free(SSL_CTX
*a
)
1571 if (a
== NULL
) return;
1573 i
=CRYPTO_add(&a
->references
,-1,CRYPTO_LOCK_SSL_CTX
);
1575 REF_PRINT("SSL_CTX",a
);
1581 fprintf(stderr
,"SSL_CTX_free, bad reference count\n");
1587 X509_VERIFY_PARAM_free(a
->param
);
1590 * Free internal session cache. However: the remove_cb() may reference
1591 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1592 * after the sessions were flushed.
1593 * As the ex_data handling routines might also touch the session cache,
1594 * the most secure solution seems to be: empty (flush) the cache, then
1595 * free ex_data, then finally free the cache.
1596 * (See ticket [openssl.org #212].)
1598 if (a
->sessions
!= NULL
)
1599 SSL_CTX_flush_sessions(a
,0);
1601 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, a
, &a
->ex_data
);
1603 if (a
->sessions
!= NULL
)
1604 lh_free(a
->sessions
);
1606 if (a
->cert_store
!= NULL
)
1607 X509_STORE_free(a
->cert_store
);
1608 if (a
->cipher_list
!= NULL
)
1609 sk_SSL_CIPHER_free(a
->cipher_list
);
1610 if (a
->cipher_list_by_id
!= NULL
)
1611 sk_SSL_CIPHER_free(a
->cipher_list_by_id
);
1612 if (a
->cert
!= NULL
)
1613 ssl_cert_free(a
->cert
);
1614 if (a
->client_CA
!= NULL
)
1615 sk_X509_NAME_pop_free(a
->client_CA
,X509_NAME_free
);
1616 if (a
->extra_certs
!= NULL
)
1617 sk_X509_pop_free(a
->extra_certs
,X509_free
);
1618 #if 0 /* This should never be done, since it removes a global database */
1619 if (a
->comp_methods
!= NULL
)
1620 sk_SSL_COMP_pop_free(a
->comp_methods
,SSL_COMP_free
);
1622 a
->comp_methods
= NULL
;
1625 #ifndef OPENSSL_NO_PSK
1626 if (a
->psk_identity_hint
)
1627 OPENSSL_free(a
->psk_identity_hint
);
1632 void SSL_CTX_set_default_passwd_cb(SSL_CTX
*ctx
, pem_password_cb
*cb
)
1634 ctx
->default_passwd_callback
=cb
;
1637 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX
*ctx
,void *u
)
1639 ctx
->default_passwd_callback_userdata
=u
;
1642 void SSL_CTX_set_cert_verify_callback(SSL_CTX
*ctx
, int (*cb
)(X509_STORE_CTX
*,void *), void *arg
)
1644 ctx
->app_verify_callback
=cb
;
1645 ctx
->app_verify_arg
=arg
;
1648 void SSL_CTX_set_verify(SSL_CTX
*ctx
,int mode
,int (*cb
)(int, X509_STORE_CTX
*))
1650 ctx
->verify_mode
=mode
;
1651 ctx
->default_verify_callback
=cb
;
1654 void SSL_CTX_set_verify_depth(SSL_CTX
*ctx
,int depth
)
1656 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
1659 void ssl_set_cert_masks(CERT
*c
, SSL_CIPHER
*cipher
)
1662 int rsa_enc
,rsa_tmp
,rsa_sign
,dh_tmp
,dh_rsa
,dh_dsa
,dsa_sign
;
1663 int rsa_enc_export
,dh_rsa_export
,dh_dsa_export
;
1664 int rsa_tmp_export
,dh_tmp_export
,kl
;
1665 unsigned long mask_k
,mask_a
,emask_k
,emask_a
;
1666 int have_ecc_cert
, ecdh_ok
, ecdsa_ok
, ecc_pkey_size
;
1667 #ifndef OPENSSL_NO_ECDH
1671 EVP_PKEY
*ecc_pkey
= NULL
;
1672 int signature_nid
= 0;
1674 if (c
== NULL
) return;
1676 kl
=SSL_C_EXPORT_PKEYLENGTH(cipher
);
1678 #ifndef OPENSSL_NO_RSA
1679 rsa_tmp
=(c
->rsa_tmp
!= NULL
|| c
->rsa_tmp_cb
!= NULL
);
1680 rsa_tmp_export
=(c
->rsa_tmp_cb
!= NULL
||
1681 (rsa_tmp
&& RSA_size(c
->rsa_tmp
)*8 <= kl
));
1683 rsa_tmp
=rsa_tmp_export
=0;
1685 #ifndef OPENSSL_NO_DH
1686 dh_tmp
=(c
->dh_tmp
!= NULL
|| c
->dh_tmp_cb
!= NULL
);
1687 dh_tmp_export
=(c
->dh_tmp_cb
!= NULL
||
1688 (dh_tmp
&& DH_size(c
->dh_tmp
)*8 <= kl
));
1690 dh_tmp
=dh_tmp_export
=0;
1693 #ifndef OPENSSL_NO_ECDH
1694 have_ecdh_tmp
=(c
->ecdh_tmp
!= NULL
|| c
->ecdh_tmp_cb
!= NULL
);
1696 cpk
= &(c
->pkeys
[SSL_PKEY_RSA_ENC
]);
1697 rsa_enc
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
1698 rsa_enc_export
=(rsa_enc
&& EVP_PKEY_size(cpk
->privatekey
)*8 <= kl
);
1699 cpk
= &(c
->pkeys
[SSL_PKEY_RSA_SIGN
]);
1700 rsa_sign
=(cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
1701 cpk
= &(c
->pkeys
[SSL_PKEY_DSA_SIGN
]);
1702 dsa_sign
=(cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
1703 cpk
= &(c
->pkeys
[SSL_PKEY_DH_RSA
]);
1704 dh_rsa
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
1705 dh_rsa_export
=(dh_rsa
&& EVP_PKEY_size(cpk
->privatekey
)*8 <= kl
);
1706 cpk
= &(c
->pkeys
[SSL_PKEY_DH_DSA
]);
1707 /* FIX THIS EAY EAY EAY */
1708 dh_dsa
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
1709 dh_dsa_export
=(dh_dsa
&& EVP_PKEY_size(cpk
->privatekey
)*8 <= kl
);
1710 cpk
= &(c
->pkeys
[SSL_PKEY_ECC
]);
1711 have_ecc_cert
= (cpk
->x509
!= NULL
&& cpk
->privatekey
!= NULL
);
1718 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1719 rsa_tmp
,rsa_tmp_export
,dh_tmp
,have_ecdh_tmp
,
1720 rsa_enc
,rsa_enc_export
,rsa_sign
,dsa_sign
,dh_rsa
,dh_dsa
);
1723 if (rsa_enc
|| (rsa_tmp
&& rsa_sign
))
1725 if (rsa_enc_export
|| (rsa_tmp_export
&& (rsa_sign
|| rsa_enc
)))
1729 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1730 if ( (dh_tmp
|| dh_rsa
|| dh_dsa
) &&
1731 (rsa_enc
|| rsa_sign
|| dsa_sign
))
1733 if ((dh_tmp_export
|| dh_rsa_export
|| dh_dsa_export
) &&
1734 (rsa_enc
|| rsa_sign
|| dsa_sign
))
1744 if (dh_rsa
) mask_k
|=SSL_kDHr
;
1745 if (dh_rsa_export
) emask_k
|=SSL_kDHr
;
1747 if (dh_dsa
) mask_k
|=SSL_kDHd
;
1748 if (dh_dsa_export
) emask_k
|=SSL_kDHd
;
1750 if (rsa_enc
|| rsa_sign
)
1765 #ifndef OPENSSL_NO_KRB5
1772 /* An ECC certificate may be usable for ECDH and/or
1773 * ECDSA cipher suites depending on the key usage extension.
1777 /* This call populates extension flags (ex_flags) */
1778 x
= (c
->pkeys
[SSL_PKEY_ECC
]).x509
;
1779 X509_check_purpose(x
, -1, 0);
1780 ecdh_ok
= (x
->ex_flags
& EXFLAG_KUSAGE
) ?
1781 (x
->ex_kusage
& X509v3_KU_KEY_AGREEMENT
) : 1;
1782 ecdsa_ok
= (x
->ex_flags
& EXFLAG_KUSAGE
) ?
1783 (x
->ex_kusage
& X509v3_KU_DIGITAL_SIGNATURE
) : 1;
1784 ecc_pkey
= X509_get_pubkey(x
);
1785 ecc_pkey_size
= (ecc_pkey
!= NULL
) ?
1786 EVP_PKEY_bits(ecc_pkey
) : 0;
1787 EVP_PKEY_free(ecc_pkey
);
1788 if ((x
->sig_alg
) && (x
->sig_alg
->algorithm
))
1789 signature_nid
= OBJ_obj2nid(x
->sig_alg
->algorithm
);
1790 #ifndef OPENSSL_NO_ECDH
1793 const char *sig
= OBJ_nid2ln(signature_nid
);
1800 if (strstr(sig
, "WithRSA"))
1804 if (ecc_pkey_size
<= 163)
1806 emask_k
|=SSL_kECDHr
;
1811 if (signature_nid
== NID_ecdsa_with_SHA1
)
1815 if (ecc_pkey_size
<= 163)
1817 emask_k
|=SSL_kECDHe
;
1823 #ifndef OPENSSL_NO_ECDSA
1827 emask_a
|=SSL_aECDSA
;
1832 #ifndef OPENSSL_NO_ECDH
1836 emask_k
|=SSL_kEECDH
;
1840 #ifndef OPENSSL_NO_PSK
1843 emask_k
|= SSL_kPSK
;
1844 emask_a
|= SSL_aPSK
;
1849 c
->export_mask_k
=emask_k
;
1850 c
->export_mask_a
=emask_a
;
1854 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
1855 #define ku_reject(x, usage) \
1856 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
1858 int ssl_check_srvr_ecc_cert_and_alg(X509
*x
, SSL_CIPHER
*cs
)
1860 unsigned long alg_k
, alg_a
;
1861 EVP_PKEY
*pkey
= NULL
;
1863 int signature_nid
= 0;
1865 alg_k
= cs
->algorithm_mkey
;
1866 alg_a
= cs
->algorithm_auth
;
1868 if (SSL_C_IS_EXPORT(cs
))
1870 /* ECDH key length in export ciphers must be <= 163 bits */
1871 pkey
= X509_get_pubkey(x
);
1872 if (pkey
== NULL
) return 0;
1873 keysize
= EVP_PKEY_bits(pkey
);
1874 EVP_PKEY_free(pkey
);
1875 if (keysize
> 163) return 0;
1878 /* This call populates the ex_flags field correctly */
1879 X509_check_purpose(x
, -1, 0);
1880 if ((x
->sig_alg
) && (x
->sig_alg
->algorithm
))
1881 signature_nid
= OBJ_obj2nid(x
->sig_alg
->algorithm
);
1882 if (alg_k
& SSL_kECDHe
|| alg_k
& SSL_kECDHr
)
1884 /* key usage, if present, must allow key agreement */
1885 if (ku_reject(x
, X509v3_KU_KEY_AGREEMENT
))
1887 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG
, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT
);
1890 if (alg_k
& SSL_kECDHe
)
1892 /* signature alg must be ECDSA */
1893 if (signature_nid
!= NID_ecdsa_with_SHA1
)
1895 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG
, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE
);
1899 if (alg_k
& SSL_kECDHr
)
1901 /* signature alg must be RSA */
1903 const char *sig
= OBJ_nid2ln(signature_nid
);
1909 if (strstr(sig
, "WithRSA") == NULL
)
1911 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG
, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE
);
1916 if (alg_a
& SSL_aECDSA
)
1918 /* key usage, if present, must allow signing */
1919 if (ku_reject(x
, X509v3_KU_DIGITAL_SIGNATURE
))
1921 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
1926 return 1; /* all checks are ok */
1929 /* THIS NEEDS CLEANING UP */
1930 X509
*ssl_get_server_send_cert(SSL
*s
)
1932 unsigned long alg_k
,alg_a
,mask_k
,mask_a
;
1937 ssl_set_cert_masks(c
, s
->s3
->tmp
.new_cipher
);
1938 is_export
=SSL_C_IS_EXPORT(s
->s3
->tmp
.new_cipher
);
1941 mask_k
= c
->export_mask_k
;
1942 mask_a
= c
->export_mask_a
;
1950 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1951 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1953 if (alg_k
& (SSL_kECDHr
|SSL_kECDHe
))
1955 /* we don't need to look at SSL_kEECDH
1956 * since no certificate is needed for
1957 * anon ECDH and for authenticated
1958 * EECDH, the check for the auth
1959 * algorithm will set i correctly
1960 * NOTE: For ECDH-RSA, we need an ECC
1961 * not an RSA cert but for EECDH-RSA
1962 * we need an RSA cert. Placing the
1963 * checks for SSL_kECDH before RSA
1964 * checks ensures the correct cert is chosen.
1968 else if (alg_a
& SSL_aECDSA
)
1972 else if (alg_k
& SSL_kDHr
)
1974 else if (alg_k
& SSL_kDHd
)
1976 else if (alg_a
& SSL_aDSS
)
1977 i
=SSL_PKEY_DSA_SIGN
;
1978 else if (alg_a
& SSL_aRSA
)
1980 if (c
->pkeys
[SSL_PKEY_RSA_ENC
].x509
== NULL
)
1981 i
=SSL_PKEY_RSA_SIGN
;
1985 else if (alg_a
& SSL_aKRB5
)
1987 /* VRS something else here? */
1990 else /* if (alg_a & SSL_aNULL) */
1992 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT
,ERR_R_INTERNAL_ERROR
);
1995 if (c
->pkeys
[i
].x509
== NULL
) return(NULL
);
1997 return(c
->pkeys
[i
].x509
);
2000 EVP_PKEY
*ssl_get_sign_pkey(SSL
*s
,SSL_CIPHER
*cipher
)
2002 unsigned long alg_a
;
2005 alg_a
= cipher
->algorithm_auth
;
2008 if ((alg_a
& SSL_aDSS
) &&
2009 (c
->pkeys
[SSL_PKEY_DSA_SIGN
].privatekey
!= NULL
))
2010 return(c
->pkeys
[SSL_PKEY_DSA_SIGN
].privatekey
);
2011 else if (alg_a
& SSL_aRSA
)
2013 if (c
->pkeys
[SSL_PKEY_RSA_SIGN
].privatekey
!= NULL
)
2014 return(c
->pkeys
[SSL_PKEY_RSA_SIGN
].privatekey
);
2015 else if (c
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
!= NULL
)
2016 return(c
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
);
2020 else if ((alg_a
& SSL_aECDSA
) &&
2021 (c
->pkeys
[SSL_PKEY_ECC
].privatekey
!= NULL
))
2022 return(c
->pkeys
[SSL_PKEY_ECC
].privatekey
);
2023 else /* if (alg_a & SSL_aNULL) */
2025 SSLerr(SSL_F_SSL_GET_SIGN_PKEY
,ERR_R_INTERNAL_ERROR
);
2030 void ssl_update_cache(SSL
*s
,int mode
)
2034 /* If the session_id_length is 0, we are not supposed to cache it,
2035 * and it would be rather hard to do anyway :-) */
2036 if (s
->session
->session_id_length
== 0) return;
2038 i
=s
->session_ctx
->session_cache_mode
;
2039 if ((i
& mode
) && (!s
->hit
)
2040 && ((i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
)
2041 || SSL_CTX_add_session(s
->session_ctx
,s
->session
))
2042 && (s
->session_ctx
->new_session_cb
!= NULL
))
2044 CRYPTO_add(&s
->session
->references
,1,CRYPTO_LOCK_SSL_SESSION
);
2045 if (!s
->session_ctx
->new_session_cb(s
,s
->session
))
2046 SSL_SESSION_free(s
->session
);
2049 /* auto flush every 255 connections */
2050 if ((!(i
& SSL_SESS_CACHE_NO_AUTO_CLEAR
)) &&
2051 ((i
& mode
) == mode
))
2053 if ( (((mode
& SSL_SESS_CACHE_CLIENT
)
2054 ?s
->session_ctx
->stats
.sess_connect_good
2055 :s
->session_ctx
->stats
.sess_accept_good
) & 0xff) == 0xff)
2057 SSL_CTX_flush_sessions(s
->session_ctx
,(unsigned long)time(NULL
));
2062 const SSL_METHOD
*SSL_get_ssl_method(SSL
*s
)
2067 int SSL_set_ssl_method(SSL
*s
, const SSL_METHOD
*meth
)
2072 if (s
->method
!= meth
)
2074 if (s
->handshake_func
!= NULL
)
2075 conn
=(s
->handshake_func
== s
->method
->ssl_connect
);
2077 if (s
->method
->version
== meth
->version
)
2081 s
->method
->ssl_free(s
);
2083 ret
=s
->method
->ssl_new(s
);
2087 s
->handshake_func
=meth
->ssl_connect
;
2089 s
->handshake_func
=meth
->ssl_accept
;
2094 int SSL_get_error(const SSL
*s
,int i
)
2100 if (i
> 0) return(SSL_ERROR_NONE
);
2102 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2103 * etc, where we do encode the error */
2104 if ((l
=ERR_peek_error()) != 0)
2106 if (ERR_GET_LIB(l
) == ERR_LIB_SYS
)
2107 return(SSL_ERROR_SYSCALL
);
2109 return(SSL_ERROR_SSL
);
2112 if ((i
< 0) && SSL_want_read(s
))
2114 bio
=SSL_get_rbio(s
);
2115 if (BIO_should_read(bio
))
2116 return(SSL_ERROR_WANT_READ
);
2117 else if (BIO_should_write(bio
))
2118 /* This one doesn't make too much sense ... We never try
2119 * to write to the rbio, and an application program where
2120 * rbio and wbio are separate couldn't even know what it
2122 * However if we ever set s->rwstate incorrectly
2123 * (so that we have SSL_want_read(s) instead of
2124 * SSL_want_write(s)) and rbio and wbio *are* the same,
2125 * this test works around that bug; so it might be safer
2127 return(SSL_ERROR_WANT_WRITE
);
2128 else if (BIO_should_io_special(bio
))
2130 reason
=BIO_get_retry_reason(bio
);
2131 if (reason
== BIO_RR_CONNECT
)
2132 return(SSL_ERROR_WANT_CONNECT
);
2133 else if (reason
== BIO_RR_ACCEPT
)
2134 return(SSL_ERROR_WANT_ACCEPT
);
2136 return(SSL_ERROR_SYSCALL
); /* unknown */
2140 if ((i
< 0) && SSL_want_write(s
))
2142 bio
=SSL_get_wbio(s
);
2143 if (BIO_should_write(bio
))
2144 return(SSL_ERROR_WANT_WRITE
);
2145 else if (BIO_should_read(bio
))
2146 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2147 return(SSL_ERROR_WANT_READ
);
2148 else if (BIO_should_io_special(bio
))
2150 reason
=BIO_get_retry_reason(bio
);
2151 if (reason
== BIO_RR_CONNECT
)
2152 return(SSL_ERROR_WANT_CONNECT
);
2153 else if (reason
== BIO_RR_ACCEPT
)
2154 return(SSL_ERROR_WANT_ACCEPT
);
2156 return(SSL_ERROR_SYSCALL
);
2159 if ((i
< 0) && SSL_want_x509_lookup(s
))
2161 return(SSL_ERROR_WANT_X509_LOOKUP
);
2166 if (s
->version
== SSL2_VERSION
)
2168 /* assume it is the socket being closed */
2169 return(SSL_ERROR_ZERO_RETURN
);
2173 if ((s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) &&
2174 (s
->s3
->warn_alert
== SSL_AD_CLOSE_NOTIFY
))
2175 return(SSL_ERROR_ZERO_RETURN
);
2178 return(SSL_ERROR_SYSCALL
);
2181 int SSL_do_handshake(SSL
*s
)
2185 if (s
->handshake_func
== NULL
)
2187 SSLerr(SSL_F_SSL_DO_HANDSHAKE
,SSL_R_CONNECTION_TYPE_NOT_SET
);
2191 s
->method
->ssl_renegotiate_check(s
);
2193 if (SSL_in_init(s
) || SSL_in_before(s
))
2195 ret
=s
->handshake_func(s
);
2200 /* For the next 2 functions, SSL_clear() sets shutdown and so
2201 * one of these calls will reset it */
2202 void SSL_set_accept_state(SSL
*s
)
2206 s
->state
=SSL_ST_ACCEPT
|SSL_ST_BEFORE
;
2207 s
->handshake_func
=s
->method
->ssl_accept
;
2208 /* clear the current cipher */
2209 ssl_clear_cipher_ctx(s
);
2210 ssl_clear_hash_ctx(&s
->read_hash
);
2211 ssl_clear_hash_ctx(&s
->write_hash
);
2214 void SSL_set_connect_state(SSL
*s
)
2218 s
->state
=SSL_ST_CONNECT
|SSL_ST_BEFORE
;
2219 s
->handshake_func
=s
->method
->ssl_connect
;
2220 /* clear the current cipher */
2221 ssl_clear_cipher_ctx(s
);
2222 ssl_clear_hash_ctx(&s
->read_hash
);
2223 ssl_clear_hash_ctx(&s
->write_hash
);
2226 int ssl_undefined_function(SSL
*s
)
2228 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION
,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2232 int ssl_undefined_void_function(void)
2234 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION
,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2238 int ssl_undefined_const_function(const SSL
*s
)
2240 SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION
,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2244 SSL_METHOD
*ssl_bad_method(int ver
)
2246 SSLerr(SSL_F_SSL_BAD_METHOD
,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2250 const char *SSL_get_version(const SSL
*s
)
2252 if (s
->version
== TLS1_VERSION
)
2254 else if (s
->version
== SSL3_VERSION
)
2256 else if (s
->version
== SSL2_VERSION
)
2262 SSL
*SSL_dup(SSL
*s
)
2264 STACK_OF(X509_NAME
) *sk
;
2269 if ((ret
=SSL_new(SSL_get_SSL_CTX(s
))) == NULL
)
2272 ret
->version
= s
->version
;
2273 ret
->type
= s
->type
;
2274 ret
->method
= s
->method
;
2276 if (s
->session
!= NULL
)
2278 /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2279 SSL_copy_session_id(ret
,s
);
2283 /* No session has been established yet, so we have to expect
2284 * that s->cert or ret->cert will be changed later --
2285 * they should not both point to the same object,
2286 * and thus we can't use SSL_copy_session_id. */
2288 ret
->method
->ssl_free(ret
);
2289 ret
->method
= s
->method
;
2290 ret
->method
->ssl_new(ret
);
2292 if (s
->cert
!= NULL
)
2294 if (ret
->cert
!= NULL
)
2296 ssl_cert_free(ret
->cert
);
2298 ret
->cert
= ssl_cert_dup(s
->cert
);
2299 if (ret
->cert
== NULL
)
2303 SSL_set_session_id_context(ret
,
2304 s
->sid_ctx
, s
->sid_ctx_length
);
2307 ret
->options
=s
->options
;
2309 SSL_set_max_cert_list(ret
,SSL_get_max_cert_list(s
));
2310 SSL_set_read_ahead(ret
,SSL_get_read_ahead(s
));
2311 ret
->msg_callback
= s
->msg_callback
;
2312 ret
->msg_callback_arg
= s
->msg_callback_arg
;
2313 SSL_set_verify(ret
,SSL_get_verify_mode(s
),
2314 SSL_get_verify_callback(s
));
2315 SSL_set_verify_depth(ret
,SSL_get_verify_depth(s
));
2316 ret
->generate_session_id
= s
->generate_session_id
;
2318 SSL_set_info_callback(ret
,SSL_get_info_callback(s
));
2320 ret
->debug
=s
->debug
;
2322 /* copy app data, a little dangerous perhaps */
2323 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL
, &ret
->ex_data
, &s
->ex_data
))
2326 /* setup rbio, and wbio */
2327 if (s
->rbio
!= NULL
)
2329 if (!BIO_dup_state(s
->rbio
,(char *)&ret
->rbio
))
2332 if (s
->wbio
!= NULL
)
2334 if (s
->wbio
!= s
->rbio
)
2336 if (!BIO_dup_state(s
->wbio
,(char *)&ret
->wbio
))
2340 ret
->wbio
=ret
->rbio
;
2342 ret
->rwstate
= s
->rwstate
;
2343 ret
->in_handshake
= s
->in_handshake
;
2344 ret
->handshake_func
= s
->handshake_func
;
2345 ret
->server
= s
->server
;
2346 ret
->new_session
= s
->new_session
;
2347 ret
->quiet_shutdown
= s
->quiet_shutdown
;
2348 ret
->shutdown
=s
->shutdown
;
2349 ret
->state
=s
->state
; /* SSL_dup does not really work at any state, though */
2350 ret
->rstate
=s
->rstate
;
2351 ret
->init_num
= 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
2354 X509_VERIFY_PARAM_inherit(ret
->param
, s
->param
);
2356 /* dup the cipher_list and cipher_list_by_id stacks */
2357 if (s
->cipher_list
!= NULL
)
2359 if ((ret
->cipher_list
=sk_SSL_CIPHER_dup(s
->cipher_list
)) == NULL
)
2362 if (s
->cipher_list_by_id
!= NULL
)
2363 if ((ret
->cipher_list_by_id
=sk_SSL_CIPHER_dup(s
->cipher_list_by_id
))
2367 /* Dup the client_CA list */
2368 if (s
->client_CA
!= NULL
)
2370 if ((sk
=sk_X509_NAME_dup(s
->client_CA
)) == NULL
) goto err
;
2372 for (i
=0; i
<sk_X509_NAME_num(sk
); i
++)
2374 xn
=sk_X509_NAME_value(sk
,i
);
2375 if (sk_X509_NAME_set(sk
,i
,X509_NAME_dup(xn
)) == NULL
)
2386 if (ret
!= NULL
) SSL_free(ret
);
2392 void ssl_clear_cipher_ctx(SSL
*s
)
2394 if (s
->enc_read_ctx
!= NULL
)
2396 EVP_CIPHER_CTX_cleanup(s
->enc_read_ctx
);
2397 OPENSSL_free(s
->enc_read_ctx
);
2398 s
->enc_read_ctx
=NULL
;
2400 if (s
->enc_write_ctx
!= NULL
)
2402 EVP_CIPHER_CTX_cleanup(s
->enc_write_ctx
);
2403 OPENSSL_free(s
->enc_write_ctx
);
2404 s
->enc_write_ctx
=NULL
;
2406 #ifndef OPENSSL_NO_COMP
2407 if (s
->expand
!= NULL
)
2409 COMP_CTX_free(s
->expand
);
2412 if (s
->compress
!= NULL
)
2414 COMP_CTX_free(s
->compress
);
2420 /* Fix this function so that it takes an optional type parameter */
2421 X509
*SSL_get_certificate(const SSL
*s
)
2423 if (s
->cert
!= NULL
)
2424 return(s
->cert
->key
->x509
);
2429 /* Fix this function so that it takes an optional type parameter */
2430 EVP_PKEY
*SSL_get_privatekey(SSL
*s
)
2432 if (s
->cert
!= NULL
)
2433 return(s
->cert
->key
->privatekey
);
2438 SSL_CIPHER
*SSL_get_current_cipher(const SSL
*s
)
2440 if ((s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
))
2441 return(s
->session
->cipher
);
2444 #ifdef OPENSSL_NO_COMP
2445 const void *SSL_get_current_compression(SSL
*s
)
2449 const void *SSL_get_current_expansion(SSL
*s
)
2455 const COMP_METHOD
*SSL_get_current_compression(SSL
*s
)
2457 if (s
->compress
!= NULL
)
2458 return(s
->compress
->meth
);
2462 const COMP_METHOD
*SSL_get_current_expansion(SSL
*s
)
2464 if (s
->expand
!= NULL
)
2465 return(s
->expand
->meth
);
2470 int ssl_init_wbio_buffer(SSL
*s
,int push
)
2474 if (s
->bbio
== NULL
)
2476 bbio
=BIO_new(BIO_f_buffer());
2477 if (bbio
== NULL
) return(0);
2483 if (s
->bbio
== s
->wbio
)
2484 s
->wbio
=BIO_pop(s
->wbio
);
2486 (void)BIO_reset(bbio
);
2487 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2488 if (!BIO_set_read_buffer_size(bbio
,1))
2490 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER
,ERR_R_BUF_LIB
);
2495 if (s
->wbio
!= bbio
)
2496 s
->wbio
=BIO_push(bbio
,s
->wbio
);
2500 if (s
->wbio
== bbio
)
2501 s
->wbio
=BIO_pop(bbio
);
2506 void ssl_free_wbio_buffer(SSL
*s
)
2508 if (s
->bbio
== NULL
) return;
2510 if (s
->bbio
== s
->wbio
)
2512 /* remove buffering */
2513 s
->wbio
=BIO_pop(s
->wbio
);
2514 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2515 assert(s
->wbio
!= NULL
);
2522 void SSL_CTX_set_quiet_shutdown(SSL_CTX
*ctx
,int mode
)
2524 ctx
->quiet_shutdown
=mode
;
2527 int SSL_CTX_get_quiet_shutdown(const SSL_CTX
*ctx
)
2529 return(ctx
->quiet_shutdown
);
2532 void SSL_set_quiet_shutdown(SSL
*s
,int mode
)
2534 s
->quiet_shutdown
=mode
;
2537 int SSL_get_quiet_shutdown(const SSL
*s
)
2539 return(s
->quiet_shutdown
);
2542 void SSL_set_shutdown(SSL
*s
,int mode
)
2547 int SSL_get_shutdown(const SSL
*s
)
2549 return(s
->shutdown
);
2552 int SSL_version(const SSL
*s
)
2557 SSL_CTX
*SSL_get_SSL_CTX(const SSL
*ssl
)
2562 SSL_CTX
*SSL_set_SSL_CTX(SSL
*ssl
, SSL_CTX
* ctx
)
2564 if (ssl
->ctx
== ctx
)
2567 ctx
= ssl
->initial_ctx
;
2568 if (ssl
->cert
!= NULL
)
2569 ssl_cert_free(ssl
->cert
);
2570 ssl
->cert
= ssl_cert_dup(ctx
->cert
);
2571 CRYPTO_add(&ctx
->references
,1,CRYPTO_LOCK_SSL_CTX
);
2572 if (ssl
->ctx
!= NULL
)
2573 SSL_CTX_free(ssl
->ctx
); /* decrement reference count */
2578 #ifndef OPENSSL_NO_STDIO
2579 int SSL_CTX_set_default_verify_paths(SSL_CTX
*ctx
)
2581 return(X509_STORE_set_default_paths(ctx
->cert_store
));
2584 int SSL_CTX_load_verify_locations(SSL_CTX
*ctx
, const char *CAfile
,
2587 return(X509_STORE_load_locations(ctx
->cert_store
,CAfile
,CApath
));
2591 void SSL_set_info_callback(SSL
*ssl
,
2592 void (*cb
)(const SSL
*ssl
,int type
,int val
))
2594 ssl
->info_callback
=cb
;
2597 /* One compiler (Diab DCC) doesn't like argument names in returned
2598 function pointer. */
2599 void (*SSL_get_info_callback(const SSL
*ssl
))(const SSL
* /*ssl*/,int /*type*/,int /*val*/)
2601 return ssl
->info_callback
;
2604 int SSL_state(const SSL
*ssl
)
2609 void SSL_set_verify_result(SSL
*ssl
,long arg
)
2611 ssl
->verify_result
=arg
;
2614 long SSL_get_verify_result(const SSL
*ssl
)
2616 return(ssl
->verify_result
);
2619 int SSL_get_ex_new_index(long argl
,void *argp
,CRYPTO_EX_new
*new_func
,
2620 CRYPTO_EX_dup
*dup_func
,CRYPTO_EX_free
*free_func
)
2622 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL
, argl
, argp
,
2623 new_func
, dup_func
, free_func
);
2626 int SSL_set_ex_data(SSL
*s
,int idx
,void *arg
)
2628 return(CRYPTO_set_ex_data(&s
->ex_data
,idx
,arg
));
2631 void *SSL_get_ex_data(const SSL
*s
,int idx
)
2633 return(CRYPTO_get_ex_data(&s
->ex_data
,idx
));
2636 int SSL_CTX_get_ex_new_index(long argl
,void *argp
,CRYPTO_EX_new
*new_func
,
2637 CRYPTO_EX_dup
*dup_func
,CRYPTO_EX_free
*free_func
)
2639 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX
, argl
, argp
,
2640 new_func
, dup_func
, free_func
);
2643 int SSL_CTX_set_ex_data(SSL_CTX
*s
,int idx
,void *arg
)
2645 return(CRYPTO_set_ex_data(&s
->ex_data
,idx
,arg
));
2648 void *SSL_CTX_get_ex_data(const SSL_CTX
*s
,int idx
)
2650 return(CRYPTO_get_ex_data(&s
->ex_data
,idx
));
2658 X509_STORE
*SSL_CTX_get_cert_store(const SSL_CTX
*ctx
)
2660 return(ctx
->cert_store
);
2663 void SSL_CTX_set_cert_store(SSL_CTX
*ctx
,X509_STORE
*store
)
2665 if (ctx
->cert_store
!= NULL
)
2666 X509_STORE_free(ctx
->cert_store
);
2667 ctx
->cert_store
=store
;
2670 int SSL_want(const SSL
*s
)
2676 * \brief Set the callback for generating temporary RSA keys.
2677 * \param ctx the SSL context.
2678 * \param cb the callback
2681 #ifndef OPENSSL_NO_RSA
2682 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX
*ctx
,RSA
*(*cb
)(SSL
*ssl
,
2686 SSL_CTX_callback_ctrl(ctx
,SSL_CTRL_SET_TMP_RSA_CB
,(void (*)(void))cb
);
2689 void SSL_set_tmp_rsa_callback(SSL
*ssl
,RSA
*(*cb
)(SSL
*ssl
,
2693 SSL_callback_ctrl(ssl
,SSL_CTRL_SET_TMP_RSA_CB
,(void (*)(void))cb
);
2699 * \brief The RSA temporary key callback function.
2700 * \param ssl the SSL session.
2701 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2702 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2703 * of the required key in bits.
2704 * \return the temporary RSA key.
2705 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2708 RSA
*cb(SSL
*ssl
,int is_export
,int keylength
)
2713 * \brief Set the callback for generating temporary DH keys.
2714 * \param ctx the SSL context.
2715 * \param dh the callback
2718 #ifndef OPENSSL_NO_DH
2719 void SSL_CTX_set_tmp_dh_callback(SSL_CTX
*ctx
,DH
*(*dh
)(SSL
*ssl
,int is_export
,
2722 SSL_CTX_callback_ctrl(ctx
,SSL_CTRL_SET_TMP_DH_CB
,(void (*)(void))dh
);
2725 void SSL_set_tmp_dh_callback(SSL
*ssl
,DH
*(*dh
)(SSL
*ssl
,int is_export
,
2728 SSL_callback_ctrl(ssl
,SSL_CTRL_SET_TMP_DH_CB
,(void (*)(void))dh
);
2732 #ifndef OPENSSL_NO_ECDH
2733 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX
*ctx
,EC_KEY
*(*ecdh
)(SSL
*ssl
,int is_export
,
2736 SSL_CTX_callback_ctrl(ctx
,SSL_CTRL_SET_TMP_ECDH_CB
,(void (*)(void))ecdh
);
2739 void SSL_set_tmp_ecdh_callback(SSL
*ssl
,EC_KEY
*(*ecdh
)(SSL
*ssl
,int is_export
,
2742 SSL_callback_ctrl(ssl
,SSL_CTRL_SET_TMP_ECDH_CB
,(void (*)(void))ecdh
);
2746 #ifndef OPENSSL_NO_PSK
2747 int SSL_CTX_use_psk_identity_hint(SSL_CTX
*ctx
, const char *identity_hint
)
2749 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
)
2751 SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT
, SSL_R_DATA_LENGTH_TOO_LONG
);
2754 if (ctx
->psk_identity_hint
!= NULL
)
2755 OPENSSL_free(ctx
->psk_identity_hint
);
2756 if (identity_hint
!= NULL
)
2758 ctx
->psk_identity_hint
= BUF_strdup(identity_hint
);
2759 if (ctx
->psk_identity_hint
== NULL
)
2763 ctx
->psk_identity_hint
= NULL
;
2767 int SSL_use_psk_identity_hint(SSL
*s
, const char *identity_hint
)
2772 if (s
->session
== NULL
)
2773 return 1; /* session not created yet, ignored */
2775 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
)
2777 SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT
, SSL_R_DATA_LENGTH_TOO_LONG
);
2780 if (s
->session
->psk_identity_hint
!= NULL
)
2781 OPENSSL_free(s
->session
->psk_identity_hint
);
2782 if (identity_hint
!= NULL
)
2784 s
->session
->psk_identity_hint
= BUF_strdup(identity_hint
);
2785 if (s
->session
->psk_identity_hint
== NULL
)
2789 s
->session
->psk_identity_hint
= NULL
;
2793 const char *SSL_get_psk_identity_hint(const SSL
*s
)
2795 if (s
== NULL
|| s
->session
== NULL
)
2797 return(s
->session
->psk_identity_hint
);
2800 const char *SSL_get_psk_identity(const SSL
*s
)
2802 if (s
== NULL
|| s
->session
== NULL
)
2804 return(s
->session
->psk_identity
);
2807 void SSL_set_psk_client_callback(SSL
*s
,
2808 unsigned int (*cb
)(SSL
*ssl
, const char *hint
,
2809 char *identity
, unsigned int max_identity_len
, unsigned char *psk
,
2810 unsigned int max_psk_len
))
2812 s
->psk_client_callback
= cb
;
2815 void SSL_CTX_set_psk_client_callback(SSL_CTX
*ctx
,
2816 unsigned int (*cb
)(SSL
*ssl
, const char *hint
,
2817 char *identity
, unsigned int max_identity_len
, unsigned char *psk
,
2818 unsigned int max_psk_len
))
2820 ctx
->psk_client_callback
= cb
;
2823 void SSL_set_psk_server_callback(SSL
*s
,
2824 unsigned int (*cb
)(SSL
*ssl
, const char *identity
,
2825 unsigned char *psk
, unsigned int max_psk_len
))
2827 s
->psk_server_callback
= cb
;
2830 void SSL_CTX_set_psk_server_callback(SSL_CTX
*ctx
,
2831 unsigned int (*cb
)(SSL
*ssl
, const char *identity
,
2832 unsigned char *psk
, unsigned int max_psk_len
))
2834 ctx
->psk_server_callback
= cb
;
2838 void SSL_CTX_set_msg_callback(SSL_CTX
*ctx
, void (*cb
)(int write_p
, int version
, int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))
2840 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
2842 void SSL_set_msg_callback(SSL
*ssl
, void (*cb
)(int write_p
, int version
, int content_type
, const void *buf
, size_t len
, SSL
*ssl
, void *arg
))
2844 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
2847 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
2848 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
2849 * any. If EVP_MD pointer is passed, initializes ctx with this md
2850 * Returns newly allocated ctx;
2853 EVP_MD_CTX
*ssl_replace_hash(EVP_MD_CTX
**hash
,const EVP_MD
*md
)
2855 ssl_clear_hash_ctx(hash
);
2856 *hash
= EVP_MD_CTX_create();
2857 if (md
) EVP_DigestInit_ex(*hash
,md
,NULL
);
2860 void ssl_clear_hash_ctx(EVP_MD_CTX
**hash
)
2863 if (*hash
) EVP_MD_CTX_destroy(*hash
);
2867 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
2868 #include "../crypto/bio/bss_file.c"
2871 IMPLEMENT_STACK_OF(SSL_CIPHER
)
2872 IMPLEMENT_STACK_OF(SSL_COMP
)