inet6: only mark autoconf addresses tentative if detached
[dragonfly.git] / crypto / libressl / ssl / ssl_lib.c
blobf5f7bf66c1c0e98ec65f1fad6f5984c5c05acbc1
1 /* $OpenBSD: ssl_lib.c,v 1.305 2022/09/10 15:29:33 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * ECC cipher suite support in OpenSSL originally developed by
114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
116 /* ====================================================================
117 * Copyright 2005 Nokia. All rights reserved.
119 * The portions of the attached software ("Contribution") is developed by
120 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121 * license.
123 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125 * support (see RFC 4279) to OpenSSL.
127 * No patent licenses or other rights except those expressly stated in
128 * the OpenSSL open source license shall be deemed granted or received
129 * expressly, by implication, estoppel, or otherwise.
131 * No assurances are provided by Nokia that the Contribution does not
132 * infringe the patent or other intellectual property rights of any third
133 * party or that the license provides you with all the necessary rights
134 * to make use of the Contribution.
136 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140 * OTHERWISE.
143 #include <arpa/inet.h>
144 #include <sys/socket.h>
145 #include <netinet/in.h>
147 #include <limits.h>
148 #include <stdio.h>
150 #include <openssl/dh.h>
151 #include <openssl/lhash.h>
152 #include <openssl/objects.h>
153 #include <openssl/ocsp.h>
154 #include <openssl/opensslconf.h>
155 #include <openssl/x509v3.h>
157 #ifndef OPENSSL_NO_ENGINE
158 #include <openssl/engine.h>
159 #endif
161 #include "bytestring.h"
162 #include "dtls_locl.h"
163 #include "ssl_locl.h"
164 #include "ssl_sigalgs.h"
165 #include "ssl_tlsext.h"
167 const char *SSL_version_str = OPENSSL_VERSION_TEXT;
170 SSL_clear(SSL *s)
172 if (s->method == NULL) {
173 SSLerror(s, SSL_R_NO_METHOD_SPECIFIED);
174 return (0);
177 if (ssl_clear_bad_session(s)) {
178 SSL_SESSION_free(s->session);
179 s->session = NULL;
182 s->error = 0;
183 s->internal->hit = 0;
184 s->internal->shutdown = 0;
186 if (s->internal->renegotiate) {
187 SSLerror(s, ERR_R_INTERNAL_ERROR);
188 return (0);
191 s->version = s->method->version;
192 s->client_version = s->version;
193 s->internal->rwstate = SSL_NOTHING;
194 s->internal->rstate = SSL_ST_READ_HEADER;
196 tls13_ctx_free(s->internal->tls13);
197 s->internal->tls13 = NULL;
199 ssl3_release_init_buffer(s);
201 ssl_clear_cipher_state(s);
203 s->internal->first_packet = 0;
206 * Check to see if we were changed into a different method, if
207 * so, revert back if we are not doing session-id reuse.
209 if (!s->internal->in_handshake && (s->session == NULL) &&
210 (s->method != s->ctx->method)) {
211 s->method->ssl_free(s);
212 s->method = s->ctx->method;
213 if (!s->method->ssl_new(s))
214 return (0);
215 } else
216 s->method->ssl_clear(s);
218 return (1);
221 /* Used to change an SSL_CTXs default SSL method type */
223 SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
225 STACK_OF(SSL_CIPHER) *ciphers;
227 ctx->method = meth;
229 ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
230 ctx->internal->cipher_list_tls13, SSL_DEFAULT_CIPHER_LIST,
231 ctx->internal->cert);
232 if (ciphers == NULL || sk_SSL_CIPHER_num(ciphers) <= 0) {
233 SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
234 return (0);
236 return (1);
239 SSL *
240 SSL_new(SSL_CTX *ctx)
242 SSL *s;
243 CBS cbs;
245 if (ctx == NULL) {
246 SSLerrorx(SSL_R_NULL_SSL_CTX);
247 return (NULL);
249 if (ctx->method == NULL) {
250 SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
251 return (NULL);
254 if ((s = calloc(1, sizeof(*s))) == NULL)
255 goto err;
256 if ((s->internal = calloc(1, sizeof(*s->internal))) == NULL)
257 goto err;
259 if ((s->internal->rl = tls12_record_layer_new()) == NULL)
260 goto err;
262 s->internal->min_tls_version = ctx->internal->min_tls_version;
263 s->internal->max_tls_version = ctx->internal->max_tls_version;
264 s->internal->min_proto_version = ctx->internal->min_proto_version;
265 s->internal->max_proto_version = ctx->internal->max_proto_version;
267 s->internal->options = ctx->internal->options;
268 s->internal->mode = ctx->internal->mode;
269 s->internal->max_cert_list = ctx->internal->max_cert_list;
270 s->internal->num_tickets = ctx->internal->num_tickets;
272 if ((s->cert = ssl_cert_dup(ctx->internal->cert)) == NULL)
273 goto err;
275 s->internal->read_ahead = ctx->internal->read_ahead;
276 s->internal->msg_callback = ctx->internal->msg_callback;
277 s->internal->msg_callback_arg = ctx->internal->msg_callback_arg;
278 s->verify_mode = ctx->verify_mode;
279 s->sid_ctx_length = ctx->sid_ctx_length;
280 OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
282 s->internal->verify_callback = ctx->internal->default_verify_callback;
283 s->internal->generate_session_id = ctx->internal->generate_session_id;
285 s->param = X509_VERIFY_PARAM_new();
286 if (!s->param)
287 goto err;
288 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
289 s->internal->quiet_shutdown = ctx->internal->quiet_shutdown;
290 s->max_send_fragment = ctx->internal->max_send_fragment;
292 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
293 s->ctx = ctx;
294 s->internal->tlsext_debug_cb = 0;
295 s->internal->tlsext_debug_arg = NULL;
296 s->internal->tlsext_ticket_expected = 0;
297 s->tlsext_status_type = -1;
298 s->internal->tlsext_status_expected = 0;
299 s->internal->tlsext_ocsp_ids = NULL;
300 s->internal->tlsext_ocsp_exts = NULL;
301 s->internal->tlsext_ocsp_resp = NULL;
302 s->internal->tlsext_ocsp_resp_len = 0;
303 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
304 s->initial_ctx = ctx;
306 if (ctx->internal->tlsext_ecpointformatlist != NULL) {
307 s->internal->tlsext_ecpointformatlist =
308 calloc(ctx->internal->tlsext_ecpointformatlist_length,
309 sizeof(ctx->internal->tlsext_ecpointformatlist[0]));
310 if (s->internal->tlsext_ecpointformatlist == NULL)
311 goto err;
312 memcpy(s->internal->tlsext_ecpointformatlist,
313 ctx->internal->tlsext_ecpointformatlist,
314 ctx->internal->tlsext_ecpointformatlist_length *
315 sizeof(ctx->internal->tlsext_ecpointformatlist[0]));
316 s->internal->tlsext_ecpointformatlist_length =
317 ctx->internal->tlsext_ecpointformatlist_length;
319 if (ctx->internal->tlsext_supportedgroups != NULL) {
320 s->internal->tlsext_supportedgroups =
321 calloc(ctx->internal->tlsext_supportedgroups_length,
322 sizeof(ctx->internal->tlsext_supportedgroups[0]));
323 if (s->internal->tlsext_supportedgroups == NULL)
324 goto err;
325 memcpy(s->internal->tlsext_supportedgroups,
326 ctx->internal->tlsext_supportedgroups,
327 ctx->internal->tlsext_supportedgroups_length *
328 sizeof(ctx->internal->tlsext_supportedgroups[0]));
329 s->internal->tlsext_supportedgroups_length =
330 ctx->internal->tlsext_supportedgroups_length;
333 CBS_init(&cbs, ctx->internal->alpn_client_proto_list,
334 ctx->internal->alpn_client_proto_list_len);
335 if (!CBS_stow(&cbs, &s->internal->alpn_client_proto_list,
336 &s->internal->alpn_client_proto_list_len))
337 goto err;
339 s->verify_result = X509_V_OK;
341 s->method = ctx->method;
342 s->quic_method = ctx->quic_method;
344 if (!s->method->ssl_new(s))
345 goto err;
347 s->references = 1;
348 s->server = ctx->method->server;
350 SSL_clear(s);
352 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->internal->ex_data);
354 return (s);
356 err:
357 SSL_free(s);
358 SSLerrorx(ERR_R_MALLOC_FAILURE);
359 return (NULL);
363 SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
364 unsigned int sid_ctx_len)
366 if (sid_ctx_len > sizeof ctx->sid_ctx) {
367 SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
368 return (0);
370 ctx->sid_ctx_length = sid_ctx_len;
371 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
373 return (1);
377 SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
378 unsigned int sid_ctx_len)
380 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
381 SSLerror(ssl, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
382 return (0);
384 ssl->sid_ctx_length = sid_ctx_len;
385 memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
387 return (1);
391 SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
393 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
394 ctx->internal->generate_session_id = cb;
395 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
396 return (1);
400 SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
402 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
403 ssl->internal->generate_session_id = cb;
404 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
405 return (1);
409 SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
410 unsigned int id_len)
413 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp
414 * shows how we can "construct" a session to give us the desired
415 * check - ie. to find if there's a session in the hash table
416 * that would conflict with any new session built out of this
417 * id/id_len and the ssl_version in use by this SSL.
419 SSL_SESSION r, *p;
421 if (id_len > sizeof r.session_id)
422 return (0);
424 r.ssl_version = ssl->version;
425 r.session_id_length = id_len;
426 memcpy(r.session_id, id, id_len);
428 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
429 p = lh_SSL_SESSION_retrieve(ssl->ctx->internal->sessions, &r);
430 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
431 return (p != NULL);
435 SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
437 return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
441 SSL_set_purpose(SSL *s, int purpose)
443 return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
447 SSL_CTX_set_trust(SSL_CTX *s, int trust)
449 return (X509_VERIFY_PARAM_set_trust(s->param, trust));
453 SSL_set_trust(SSL *s, int trust)
455 return (X509_VERIFY_PARAM_set_trust(s->param, trust));
459 SSL_set1_host(SSL *s, const char *hostname)
461 struct in_addr ina;
462 struct in6_addr in6a;
464 if (hostname != NULL && *hostname != '\0' &&
465 (inet_pton(AF_INET, hostname, &ina) == 1 ||
466 inet_pton(AF_INET6, hostname, &in6a) == 1))
467 return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
468 else
469 return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
472 void
473 SSL_set_hostflags(SSL *s, unsigned int flags)
475 X509_VERIFY_PARAM_set_hostflags(s->param, flags);
478 const char *
479 SSL_get0_peername(SSL *s)
481 return X509_VERIFY_PARAM_get0_peername(s->param);
484 X509_VERIFY_PARAM *
485 SSL_CTX_get0_param(SSL_CTX *ctx)
487 return (ctx->param);
491 SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
493 return (X509_VERIFY_PARAM_set1(ctx->param, vpm));
496 X509_VERIFY_PARAM *
497 SSL_get0_param(SSL *ssl)
499 return (ssl->param);
503 SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
505 return (X509_VERIFY_PARAM_set1(ssl->param, vpm));
508 void
509 SSL_free(SSL *s)
511 int i;
513 if (s == NULL)
514 return;
516 i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
517 if (i > 0)
518 return;
520 X509_VERIFY_PARAM_free(s->param);
522 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->internal->ex_data);
524 if (s->bbio != NULL) {
525 /* If the buffering BIO is in place, pop it off */
526 if (s->bbio == s->wbio) {
527 s->wbio = BIO_pop(s->wbio);
529 BIO_free(s->bbio);
530 s->bbio = NULL;
533 if (s->rbio != s->wbio)
534 BIO_free_all(s->rbio);
535 BIO_free_all(s->wbio);
537 tls13_ctx_free(s->internal->tls13);
539 ssl3_release_init_buffer(s);
541 sk_SSL_CIPHER_free(s->cipher_list);
542 sk_SSL_CIPHER_free(s->internal->cipher_list_tls13);
544 /* Make the next call work :-) */
545 if (s->session != NULL) {
546 ssl_clear_bad_session(s);
547 SSL_SESSION_free(s->session);
550 ssl_clear_cipher_state(s);
552 ssl_cert_free(s->cert);
554 free(s->tlsext_hostname);
555 SSL_CTX_free(s->initial_ctx);
557 free(s->internal->tlsext_ecpointformatlist);
558 free(s->internal->tlsext_supportedgroups);
560 sk_X509_EXTENSION_pop_free(s->internal->tlsext_ocsp_exts,
561 X509_EXTENSION_free);
562 sk_OCSP_RESPID_pop_free(s->internal->tlsext_ocsp_ids, OCSP_RESPID_free);
563 free(s->internal->tlsext_ocsp_resp);
565 sk_X509_NAME_pop_free(s->internal->client_CA, X509_NAME_free);
567 if (s->method != NULL)
568 s->method->ssl_free(s);
570 SSL_CTX_free(s->ctx);
572 free(s->internal->alpn_client_proto_list);
574 free(s->internal->quic_transport_params);
576 #ifndef OPENSSL_NO_SRTP
577 sk_SRTP_PROTECTION_PROFILE_free(s->internal->srtp_profiles);
578 #endif
580 tls12_record_layer_free(s->internal->rl);
582 free(s->internal);
583 free(s);
587 SSL_up_ref(SSL *s)
589 int refs = CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL);
590 return (refs > 1) ? 1 : 0;
593 void
594 SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
596 /* If the output buffering BIO is still in place, remove it */
597 if (s->bbio != NULL) {
598 if (s->wbio == s->bbio) {
599 s->wbio = BIO_next(s->wbio);
600 BIO_set_next(s->bbio, NULL);
604 if (s->rbio != rbio && s->rbio != s->wbio)
605 BIO_free_all(s->rbio);
606 if (s->wbio != wbio)
607 BIO_free_all(s->wbio);
608 s->rbio = rbio;
609 s->wbio = wbio;
612 BIO *
613 SSL_get_rbio(const SSL *s)
615 return (s->rbio);
618 void
619 SSL_set0_rbio(SSL *s, BIO *rbio)
621 BIO_free_all(s->rbio);
622 s->rbio = rbio;
625 BIO *
626 SSL_get_wbio(const SSL *s)
628 return (s->wbio);
632 SSL_get_fd(const SSL *s)
634 return (SSL_get_rfd(s));
638 SSL_get_rfd(const SSL *s)
640 int ret = -1;
641 BIO *b, *r;
643 b = SSL_get_rbio(s);
644 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
645 if (r != NULL)
646 BIO_get_fd(r, &ret);
647 return (ret);
651 SSL_get_wfd(const SSL *s)
653 int ret = -1;
654 BIO *b, *r;
656 b = SSL_get_wbio(s);
657 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
658 if (r != NULL)
659 BIO_get_fd(r, &ret);
660 return (ret);
664 SSL_set_fd(SSL *s, int fd)
666 int ret = 0;
667 BIO *bio = NULL;
669 bio = BIO_new(BIO_s_socket());
671 if (bio == NULL) {
672 SSLerror(s, ERR_R_BUF_LIB);
673 goto err;
675 BIO_set_fd(bio, fd, BIO_NOCLOSE);
676 SSL_set_bio(s, bio, bio);
677 ret = 1;
678 err:
679 return (ret);
683 SSL_set_wfd(SSL *s, int fd)
685 int ret = 0;
686 BIO *bio = NULL;
688 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
689 || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
690 bio = BIO_new(BIO_s_socket());
692 if (bio == NULL) {
693 SSLerror(s, ERR_R_BUF_LIB);
694 goto err;
696 BIO_set_fd(bio, fd, BIO_NOCLOSE);
697 SSL_set_bio(s, SSL_get_rbio(s), bio);
698 } else
699 SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
700 ret = 1;
701 err:
702 return (ret);
706 SSL_set_rfd(SSL *s, int fd)
708 int ret = 0;
709 BIO *bio = NULL;
711 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
712 || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
713 bio = BIO_new(BIO_s_socket());
715 if (bio == NULL) {
716 SSLerror(s, ERR_R_BUF_LIB);
717 goto err;
719 BIO_set_fd(bio, fd, BIO_NOCLOSE);
720 SSL_set_bio(s, bio, SSL_get_wbio(s));
721 } else
722 SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
723 ret = 1;
724 err:
725 return (ret);
729 /* return length of latest Finished message we sent, copy to 'buf' */
730 size_t
731 SSL_get_finished(const SSL *s, void *buf, size_t count)
733 size_t ret;
735 ret = s->s3->hs.finished_len;
736 if (count > ret)
737 count = ret;
738 memcpy(buf, s->s3->hs.finished, count);
739 return (ret);
742 /* return length of latest Finished message we expected, copy to 'buf' */
743 size_t
744 SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
746 size_t ret;
748 ret = s->s3->hs.peer_finished_len;
749 if (count > ret)
750 count = ret;
751 memcpy(buf, s->s3->hs.peer_finished, count);
752 return (ret);
757 SSL_get_verify_mode(const SSL *s)
759 return (s->verify_mode);
763 SSL_get_verify_depth(const SSL *s)
765 return (X509_VERIFY_PARAM_get_depth(s->param));
769 (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *)
771 return (s->internal->verify_callback);
774 void
775 SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
777 ctx->internal->keylog_callback = cb;
780 SSL_CTX_keylog_cb_func
781 SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
783 return (ctx->internal->keylog_callback);
787 SSL_set_num_tickets(SSL *s, size_t num_tickets)
789 s->internal->num_tickets = num_tickets;
791 return 1;
794 size_t
795 SSL_get_num_tickets(const SSL *s)
797 return s->internal->num_tickets;
801 SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
803 ctx->internal->num_tickets = num_tickets;
805 return 1;
808 size_t
809 SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
811 return ctx->internal->num_tickets;
815 SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
817 return (ctx->verify_mode);
821 SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
823 return (X509_VERIFY_PARAM_get_depth(ctx->param));
827 (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *)
829 return (ctx->internal->default_verify_callback);
832 void
833 SSL_set_verify(SSL *s, int mode,
834 int (*callback)(int ok, X509_STORE_CTX *ctx))
836 s->verify_mode = mode;
837 if (callback != NULL)
838 s->internal->verify_callback = callback;
841 void
842 SSL_set_verify_depth(SSL *s, int depth)
844 X509_VERIFY_PARAM_set_depth(s->param, depth);
847 void
848 SSL_set_read_ahead(SSL *s, int yes)
850 s->internal->read_ahead = yes;
854 SSL_get_read_ahead(const SSL *s)
856 return (s->internal->read_ahead);
860 SSL_pending(const SSL *s)
862 return (s->method->ssl_pending(s));
865 X509 *
866 SSL_get_peer_certificate(const SSL *s)
868 X509 *cert;
870 if (s == NULL || s->session == NULL)
871 return NULL;
873 if ((cert = s->session->peer_cert) == NULL)
874 return NULL;
876 X509_up_ref(cert);
878 return cert;
881 STACK_OF(X509) *
882 SSL_get_peer_cert_chain(const SSL *s)
884 if (s == NULL)
885 return NULL;
888 * Achtung! Due to API inconsistency, a client includes the peer's leaf
889 * certificate in the peer certificate chain, while a server does not.
891 if (!s->server)
892 return s->s3->hs.peer_certs;
894 return s->s3->hs.peer_certs_no_leaf;
897 STACK_OF(X509) *
898 SSL_get0_verified_chain(const SSL *s)
900 return s->internal->verified_chain;
904 * Now in theory, since the calling process own 't' it should be safe to
905 * modify. We need to be able to read f without being hassled
908 SSL_copy_session_id(SSL *t, const SSL *f)
910 SSL_CERT *tmp;
912 /* Do we need to do SSL locking? */
913 if (!SSL_set_session(t, SSL_get_session(f)))
914 return 0;
916 /* What if we are set up for one protocol but want to talk another? */
917 if (t->method != f->method) {
918 t->method->ssl_free(t);
919 t->method = f->method;
920 if (!t->method->ssl_new(t))
921 return 0;
924 tmp = t->cert;
925 if (f->cert != NULL) {
926 CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
927 t->cert = f->cert;
928 } else
929 t->cert = NULL;
930 ssl_cert_free(tmp);
932 if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length))
933 return 0;
935 return 1;
938 /* Fix this so it checks all the valid key/cert options */
940 SSL_CTX_check_private_key(const SSL_CTX *ctx)
942 if ((ctx == NULL) || (ctx->internal->cert == NULL) ||
943 (ctx->internal->cert->key->x509 == NULL)) {
944 SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED);
945 return (0);
947 if (ctx->internal->cert->key->privatekey == NULL) {
948 SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED);
949 return (0);
951 return (X509_check_private_key(ctx->internal->cert->key->x509,
952 ctx->internal->cert->key->privatekey));
955 /* Fix this function so that it takes an optional type parameter */
957 SSL_check_private_key(const SSL *ssl)
959 if (ssl == NULL) {
960 SSLerrorx(ERR_R_PASSED_NULL_PARAMETER);
961 return (0);
963 if (ssl->cert == NULL) {
964 SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
965 return (0);
967 if (ssl->cert->key->x509 == NULL) {
968 SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
969 return (0);
971 if (ssl->cert->key->privatekey == NULL) {
972 SSLerror(ssl, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
973 return (0);
975 return (X509_check_private_key(ssl->cert->key->x509,
976 ssl->cert->key->privatekey));
980 SSL_accept(SSL *s)
982 if (s->internal->handshake_func == NULL)
983 SSL_set_accept_state(s); /* Not properly initialized yet */
985 return (s->method->ssl_accept(s));
989 SSL_connect(SSL *s)
991 if (s->internal->handshake_func == NULL)
992 SSL_set_connect_state(s); /* Not properly initialized yet */
994 return (s->method->ssl_connect(s));
998 SSL_is_dtls(const SSL *s)
1000 return s->method->dtls;
1004 SSL_is_server(const SSL *s)
1006 return s->server;
1009 static long
1010 ssl_get_default_timeout()
1013 * 2 hours, the 24 hours mentioned in the TLSv1 spec
1014 * is way too long for http, the cache would over fill.
1016 return (2 * 60 * 60);
1019 long
1020 SSL_get_default_timeout(const SSL *s)
1022 return (ssl_get_default_timeout());
1026 SSL_read(SSL *s, void *buf, int num)
1028 if (num < 0) {
1029 SSLerror(s, SSL_R_BAD_LENGTH);
1030 return -1;
1033 if (SSL_is_quic(s)) {
1034 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1035 return (-1);
1038 if (s->internal->handshake_func == NULL) {
1039 SSLerror(s, SSL_R_UNINITIALIZED);
1040 return (-1);
1043 if (s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) {
1044 s->internal->rwstate = SSL_NOTHING;
1045 return (0);
1047 return ssl3_read(s, buf, num);
1051 SSL_read_ex(SSL *s, void *buf, size_t num, size_t *bytes_read)
1053 int ret;
1055 /* We simply don't bother supporting enormous reads */
1056 if (num > INT_MAX) {
1057 SSLerror(s, SSL_R_BAD_LENGTH);
1058 return 0;
1061 ret = SSL_read(s, buf, (int)num);
1062 if (ret < 0)
1063 ret = 0;
1064 *bytes_read = ret;
1066 return ret > 0;
1070 SSL_peek(SSL *s, void *buf, int num)
1072 if (num < 0) {
1073 SSLerror(s, SSL_R_BAD_LENGTH);
1074 return -1;
1077 if (SSL_is_quic(s)) {
1078 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1079 return (-1);
1082 if (s->internal->handshake_func == NULL) {
1083 SSLerror(s, SSL_R_UNINITIALIZED);
1084 return (-1);
1087 if (s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) {
1088 return (0);
1090 return ssl3_peek(s, buf, num);
1094 SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *bytes_peeked)
1096 int ret;
1098 /* We simply don't bother supporting enormous peeks */
1099 if (num > INT_MAX) {
1100 SSLerror(s, SSL_R_BAD_LENGTH);
1101 return 0;
1104 ret = SSL_peek(s, buf, (int)num);
1105 if (ret < 0)
1106 ret = 0;
1107 *bytes_peeked = ret;
1109 return ret > 0;
1113 SSL_write(SSL *s, const void *buf, int num)
1115 if (num < 0) {
1116 SSLerror(s, SSL_R_BAD_LENGTH);
1117 return -1;
1120 if (SSL_is_quic(s)) {
1121 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1122 return (-1);
1125 if (s->internal->handshake_func == NULL) {
1126 SSLerror(s, SSL_R_UNINITIALIZED);
1127 return (-1);
1130 if (s->internal->shutdown & SSL_SENT_SHUTDOWN) {
1131 s->internal->rwstate = SSL_NOTHING;
1132 SSLerror(s, SSL_R_PROTOCOL_IS_SHUTDOWN);
1133 return (-1);
1135 return ssl3_write(s, buf, num);
1139 SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *bytes_written)
1141 int ret;
1143 /* We simply don't bother supporting enormous writes */
1144 if (num > INT_MAX) {
1145 SSLerror(s, SSL_R_BAD_LENGTH);
1146 return 0;
1149 if (num == 0) {
1150 /* This API is special */
1151 bytes_written = 0;
1152 return 1;
1155 ret = SSL_write(s, buf, (int)num);
1156 if (ret < 0)
1157 ret = 0;
1158 *bytes_written = ret;
1160 return ret > 0;
1163 uint32_t
1164 SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
1166 return 0;
1170 SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
1172 return 1;
1175 uint32_t
1176 SSL_get_max_early_data(const SSL *s)
1178 return 0;
1182 SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
1184 return 1;
1188 SSL_get_early_data_status(const SSL *s)
1190 return SSL_EARLY_DATA_REJECTED;
1194 SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
1196 *readbytes = 0;
1198 if (!s->server) {
1199 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1200 return SSL_READ_EARLY_DATA_ERROR;
1203 return SSL_READ_EARLY_DATA_FINISH;
1207 SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
1209 *written = 0;
1210 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1211 return 0;
1215 SSL_shutdown(SSL *s)
1218 * Note that this function behaves differently from what one might
1219 * expect. Return values are 0 for no success (yet),
1220 * 1 for success; but calling it once is usually not enough,
1221 * even if blocking I/O is used (see ssl3_shutdown).
1224 if (s->internal->handshake_func == NULL) {
1225 SSLerror(s, SSL_R_UNINITIALIZED);
1226 return (-1);
1229 if (s != NULL && !SSL_in_init(s))
1230 return (s->method->ssl_shutdown(s));
1232 return (1);
1236 SSL_renegotiate(SSL *s)
1238 if (s->internal->renegotiate == 0)
1239 s->internal->renegotiate = 1;
1241 s->internal->new_session = 1;
1243 return (s->method->ssl_renegotiate(s));
1247 SSL_renegotiate_abbreviated(SSL *s)
1249 if (s->internal->renegotiate == 0)
1250 s->internal->renegotiate = 1;
1252 s->internal->new_session = 0;
1254 return (s->method->ssl_renegotiate(s));
1258 SSL_renegotiate_pending(SSL *s)
1261 * Becomes true when negotiation is requested;
1262 * false again once a handshake has finished.
1264 return (s->internal->renegotiate != 0);
1267 long
1268 SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
1270 long l;
1272 switch (cmd) {
1273 case SSL_CTRL_GET_READ_AHEAD:
1274 return (s->internal->read_ahead);
1275 case SSL_CTRL_SET_READ_AHEAD:
1276 l = s->internal->read_ahead;
1277 s->internal->read_ahead = larg;
1278 return (l);
1280 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1281 s->internal->msg_callback_arg = parg;
1282 return (1);
1284 case SSL_CTRL_OPTIONS:
1285 return (s->internal->options|=larg);
1286 case SSL_CTRL_CLEAR_OPTIONS:
1287 return (s->internal->options&=~larg);
1288 case SSL_CTRL_MODE:
1289 return (s->internal->mode|=larg);
1290 case SSL_CTRL_CLEAR_MODE:
1291 return (s->internal->mode &=~larg);
1292 case SSL_CTRL_GET_MAX_CERT_LIST:
1293 return (s->internal->max_cert_list);
1294 case SSL_CTRL_SET_MAX_CERT_LIST:
1295 l = s->internal->max_cert_list;
1296 s->internal->max_cert_list = larg;
1297 return (l);
1298 case SSL_CTRL_SET_MTU:
1299 #ifndef OPENSSL_NO_DTLS1
1300 if (larg < (long)dtls1_min_mtu())
1301 return (0);
1302 #endif
1303 if (SSL_is_dtls(s)) {
1304 s->d1->mtu = larg;
1305 return (larg);
1307 return (0);
1308 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1309 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1310 return (0);
1311 s->max_send_fragment = larg;
1312 return (1);
1313 case SSL_CTRL_GET_RI_SUPPORT:
1314 if (s->s3)
1315 return (s->s3->send_connection_binding);
1316 else return (0);
1317 default:
1318 if (SSL_is_dtls(s))
1319 return dtls1_ctrl(s, cmd, larg, parg);
1320 return ssl3_ctrl(s, cmd, larg, parg);
1324 long
1325 SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1327 switch (cmd) {
1328 case SSL_CTRL_SET_MSG_CALLBACK:
1329 s->internal->msg_callback = (ssl_msg_callback_fn *)(fp);
1330 return (1);
1332 default:
1333 return (ssl3_callback_ctrl(s, cmd, fp));
1337 struct lhash_st_SSL_SESSION *
1338 SSL_CTX_sessions(SSL_CTX *ctx)
1340 return (ctx->internal->sessions);
1343 long
1344 SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1346 long l;
1348 switch (cmd) {
1349 case SSL_CTRL_GET_READ_AHEAD:
1350 return (ctx->internal->read_ahead);
1351 case SSL_CTRL_SET_READ_AHEAD:
1352 l = ctx->internal->read_ahead;
1353 ctx->internal->read_ahead = larg;
1354 return (l);
1356 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1357 ctx->internal->msg_callback_arg = parg;
1358 return (1);
1360 case SSL_CTRL_GET_MAX_CERT_LIST:
1361 return (ctx->internal->max_cert_list);
1362 case SSL_CTRL_SET_MAX_CERT_LIST:
1363 l = ctx->internal->max_cert_list;
1364 ctx->internal->max_cert_list = larg;
1365 return (l);
1367 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1368 l = ctx->internal->session_cache_size;
1369 ctx->internal->session_cache_size = larg;
1370 return (l);
1371 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1372 return (ctx->internal->session_cache_size);
1373 case SSL_CTRL_SET_SESS_CACHE_MODE:
1374 l = ctx->internal->session_cache_mode;
1375 ctx->internal->session_cache_mode = larg;
1376 return (l);
1377 case SSL_CTRL_GET_SESS_CACHE_MODE:
1378 return (ctx->internal->session_cache_mode);
1380 case SSL_CTRL_SESS_NUMBER:
1381 return (lh_SSL_SESSION_num_items(ctx->internal->sessions));
1382 case SSL_CTRL_SESS_CONNECT:
1383 return (ctx->internal->stats.sess_connect);
1384 case SSL_CTRL_SESS_CONNECT_GOOD:
1385 return (ctx->internal->stats.sess_connect_good);
1386 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1387 return (ctx->internal->stats.sess_connect_renegotiate);
1388 case SSL_CTRL_SESS_ACCEPT:
1389 return (ctx->internal->stats.sess_accept);
1390 case SSL_CTRL_SESS_ACCEPT_GOOD:
1391 return (ctx->internal->stats.sess_accept_good);
1392 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1393 return (ctx->internal->stats.sess_accept_renegotiate);
1394 case SSL_CTRL_SESS_HIT:
1395 return (ctx->internal->stats.sess_hit);
1396 case SSL_CTRL_SESS_CB_HIT:
1397 return (ctx->internal->stats.sess_cb_hit);
1398 case SSL_CTRL_SESS_MISSES:
1399 return (ctx->internal->stats.sess_miss);
1400 case SSL_CTRL_SESS_TIMEOUTS:
1401 return (ctx->internal->stats.sess_timeout);
1402 case SSL_CTRL_SESS_CACHE_FULL:
1403 return (ctx->internal->stats.sess_cache_full);
1404 case SSL_CTRL_OPTIONS:
1405 return (ctx->internal->options|=larg);
1406 case SSL_CTRL_CLEAR_OPTIONS:
1407 return (ctx->internal->options&=~larg);
1408 case SSL_CTRL_MODE:
1409 return (ctx->internal->mode|=larg);
1410 case SSL_CTRL_CLEAR_MODE:
1411 return (ctx->internal->mode&=~larg);
1412 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1413 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1414 return (0);
1415 ctx->internal->max_send_fragment = larg;
1416 return (1);
1417 default:
1418 return (ssl3_ctx_ctrl(ctx, cmd, larg, parg));
1422 long
1423 SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1425 switch (cmd) {
1426 case SSL_CTRL_SET_MSG_CALLBACK:
1427 ctx->internal->msg_callback = (ssl_msg_callback_fn *)fp;
1428 return (1);
1430 default:
1431 return (ssl3_ctx_callback_ctrl(ctx, cmd, fp));
1436 ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1438 long l;
1440 l = a->id - b->id;
1441 if (l == 0L)
1442 return (0);
1443 else
1444 return ((l > 0) ? 1:-1);
1447 STACK_OF(SSL_CIPHER) *
1448 SSL_get_ciphers(const SSL *s)
1450 if (s == NULL)
1451 return (NULL);
1452 if (s->cipher_list != NULL)
1453 return (s->cipher_list);
1455 return (s->ctx->cipher_list);
1458 STACK_OF(SSL_CIPHER) *
1459 SSL_get_client_ciphers(const SSL *s)
1461 if (s == NULL || s->session == NULL || !s->server)
1462 return NULL;
1463 return s->session->ciphers;
1466 STACK_OF(SSL_CIPHER) *
1467 SSL_get1_supported_ciphers(SSL *s)
1469 STACK_OF(SSL_CIPHER) *supported_ciphers = NULL, *ciphers;
1470 SSL_CIPHER *cipher;
1471 uint16_t min_vers, max_vers;
1472 int i;
1474 if (s == NULL)
1475 return NULL;
1476 if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers))
1477 return NULL;
1478 if ((ciphers = SSL_get_ciphers(s)) == NULL)
1479 return NULL;
1480 if ((supported_ciphers = sk_SSL_CIPHER_new_null()) == NULL)
1481 return NULL;
1483 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1484 if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL)
1485 goto err;
1486 if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers,
1487 max_vers))
1488 continue;
1489 if (!ssl_security_supported_cipher(s, cipher))
1490 continue;
1491 if (!sk_SSL_CIPHER_push(supported_ciphers, cipher))
1492 goto err;
1495 if (sk_SSL_CIPHER_num(supported_ciphers) > 0)
1496 return supported_ciphers;
1498 err:
1499 sk_SSL_CIPHER_free(supported_ciphers);
1500 return NULL;
1503 /* See if we have any ECC cipher suites. */
1505 ssl_has_ecc_ciphers(SSL *s)
1507 STACK_OF(SSL_CIPHER) *ciphers;
1508 unsigned long alg_k, alg_a;
1509 SSL_CIPHER *cipher;
1510 int i;
1512 if ((ciphers = SSL_get_ciphers(s)) == NULL)
1513 return 0;
1515 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1516 cipher = sk_SSL_CIPHER_value(ciphers, i);
1518 alg_k = cipher->algorithm_mkey;
1519 alg_a = cipher->algorithm_auth;
1521 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1522 return 1;
1525 return 0;
1528 /* The old interface to get the same thing as SSL_get_ciphers(). */
1529 const char *
1530 SSL_get_cipher_list(const SSL *s, int n)
1532 STACK_OF(SSL_CIPHER) *ciphers;
1533 const SSL_CIPHER *cipher;
1535 if ((ciphers = SSL_get_ciphers(s)) == NULL)
1536 return (NULL);
1537 if ((cipher = sk_SSL_CIPHER_value(ciphers, n)) == NULL)
1538 return (NULL);
1540 return (cipher->name);
1543 STACK_OF(SSL_CIPHER) *
1544 SSL_CTX_get_ciphers(const SSL_CTX *ctx)
1546 if (ctx == NULL)
1547 return NULL;
1548 return ctx->cipher_list;
1551 /* Specify the ciphers to be used by default by the SSL_CTX. */
1553 SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1555 STACK_OF(SSL_CIPHER) *ciphers;
1558 * ssl_create_cipher_list may return an empty stack if it was unable to
1559 * find a cipher matching the given rule string (for example if the
1560 * rule string specifies a cipher which has been disabled). This is not
1561 * an error as far as ssl_create_cipher_list is concerned, and hence
1562 * ctx->cipher_list has been updated.
1564 ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1565 ctx->internal->cipher_list_tls13, str, ctx->internal->cert);
1566 if (ciphers == NULL) {
1567 return (0);
1568 } else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1569 SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1570 return (0);
1572 return (1);
1576 SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1578 if (!ssl_parse_ciphersuites(&ctx->internal->cipher_list_tls13, str)) {
1579 SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1580 return 0;
1582 if (!ssl_merge_cipherlists(ctx->cipher_list,
1583 ctx->internal->cipher_list_tls13, &ctx->cipher_list))
1584 return 0;
1586 return 1;
1589 /* Specify the ciphers to be used by the SSL. */
1591 SSL_set_cipher_list(SSL *s, const char *str)
1593 STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13;
1595 if ((ciphers_tls13 = s->internal->cipher_list_tls13) == NULL)
1596 ciphers_tls13 = s->ctx->internal->cipher_list_tls13;
1598 /* See comment in SSL_CTX_set_cipher_list. */
1599 ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1600 ciphers_tls13, str, s->cert);
1601 if (ciphers == NULL) {
1602 return (0);
1603 } else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1604 SSLerror(s, SSL_R_NO_CIPHER_MATCH);
1605 return (0);
1607 return (1);
1611 SSL_set_ciphersuites(SSL *s, const char *str)
1613 STACK_OF(SSL_CIPHER) *ciphers;
1615 if ((ciphers = s->cipher_list) == NULL)
1616 ciphers = s->ctx->cipher_list;
1618 if (!ssl_parse_ciphersuites(&s->internal->cipher_list_tls13, str)) {
1619 SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1620 return (0);
1622 if (!ssl_merge_cipherlists(ciphers, s->internal->cipher_list_tls13,
1623 &s->cipher_list))
1624 return 0;
1626 return 1;
1629 char *
1630 SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1632 STACK_OF(SSL_CIPHER) *client_ciphers, *server_ciphers;
1633 const SSL_CIPHER *cipher;
1634 size_t curlen = 0;
1635 char *end;
1636 int i;
1638 if (!s->server || s->session == NULL || len < 2)
1639 return NULL;
1641 if ((client_ciphers = s->session->ciphers) == NULL)
1642 return NULL;
1643 if ((server_ciphers = SSL_get_ciphers(s)) == NULL)
1644 return NULL;
1645 if (sk_SSL_CIPHER_num(client_ciphers) == 0 ||
1646 sk_SSL_CIPHER_num(server_ciphers) == 0)
1647 return NULL;
1649 buf[0] = '\0';
1650 for (i = 0; i < sk_SSL_CIPHER_num(client_ciphers); i++) {
1651 cipher = sk_SSL_CIPHER_value(client_ciphers, i);
1653 if (sk_SSL_CIPHER_find(server_ciphers, cipher) < 0)
1654 continue;
1656 end = buf + curlen;
1657 if (strlcat(buf, cipher->name, len) >= len ||
1658 (curlen = strlcat(buf, ":", len)) >= len) {
1659 /* remove truncated cipher from list */
1660 *end = '\0';
1661 break;
1664 /* remove trailing colon */
1665 if ((end = strrchr(buf, ':')) != NULL)
1666 *end = '\0';
1667 return buf;
1671 * Return a servername extension value if provided in Client Hello, or NULL.
1672 * So far, only host_name types are defined (RFC 3546).
1674 const char *
1675 SSL_get_servername(const SSL *s, const int type)
1677 if (type != TLSEXT_NAMETYPE_host_name)
1678 return (NULL);
1680 return (s->session && !s->tlsext_hostname ?
1681 s->session->tlsext_hostname :
1682 s->tlsext_hostname);
1686 SSL_get_servername_type(const SSL *s)
1688 if (s->session &&
1689 (!s->tlsext_hostname ?
1690 s->session->tlsext_hostname : s->tlsext_hostname))
1691 return (TLSEXT_NAMETYPE_host_name);
1692 return (-1);
1696 * SSL_select_next_proto implements standard protocol selection. It is
1697 * expected that this function is called from the callback set by
1698 * SSL_CTX_set_alpn_select_cb.
1700 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1701 * strings. The length byte itself is not included in the length. A byte
1702 * string of length 0 is invalid. No byte string may be truncated.
1704 * It returns either:
1705 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1706 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1709 SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1710 const unsigned char *server, unsigned int server_len,
1711 const unsigned char *client, unsigned int client_len)
1713 unsigned int i, j;
1714 const unsigned char *result;
1715 int status = OPENSSL_NPN_UNSUPPORTED;
1718 * For each protocol in server preference order,
1719 * see if we support it.
1721 for (i = 0; i < server_len; ) {
1722 for (j = 0; j < client_len; ) {
1723 if (server[i] == client[j] &&
1724 memcmp(&server[i + 1],
1725 &client[j + 1], server[i]) == 0) {
1726 /* We found a match */
1727 result = &server[i];
1728 status = OPENSSL_NPN_NEGOTIATED;
1729 goto found;
1731 j += client[j];
1732 j++;
1734 i += server[i];
1735 i++;
1738 /* There's no overlap between our protocols and the server's list. */
1739 result = client;
1740 status = OPENSSL_NPN_NO_OVERLAP;
1742 found:
1743 *out = (unsigned char *) result + 1;
1744 *outlen = result[0];
1745 return (status);
1748 /* SSL_get0_next_proto_negotiated is deprecated. */
1749 void
1750 SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1751 unsigned int *len)
1753 *data = NULL;
1754 *len = 0;
1757 /* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1758 void
1759 SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl,
1760 const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1764 /* SSL_CTX_set_next_proto_select_cb is deprecated. */
1765 void
1766 SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s,
1767 unsigned char **out, unsigned char *outlen, const unsigned char *in,
1768 unsigned int inlen, void *arg), void *arg)
1773 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1774 * protocols, which must be in wire-format (i.e. a series of non-empty,
1775 * 8-bit length-prefixed strings). Returns 0 on success.
1778 SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
1779 unsigned int protos_len)
1781 CBS cbs;
1782 int failed = 1;
1784 if (protos == NULL)
1785 protos_len = 0;
1787 CBS_init(&cbs, protos, protos_len);
1789 if (protos_len > 0) {
1790 if (!tlsext_alpn_check_format(&cbs))
1791 goto err;
1794 if (!CBS_stow(&cbs, &ctx->internal->alpn_client_proto_list,
1795 &ctx->internal->alpn_client_proto_list_len))
1796 goto err;
1798 failed = 0;
1800 err:
1801 /* NOTE: Return values are the reverse of what you expect. */
1802 return failed;
1806 * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1807 * protocols, which must be in wire-format (i.e. a series of non-empty,
1808 * 8-bit length-prefixed strings). Returns 0 on success.
1811 SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
1812 unsigned int protos_len)
1814 CBS cbs;
1815 int failed = 1;
1817 if (protos == NULL)
1818 protos_len = 0;
1820 CBS_init(&cbs, protos, protos_len);
1822 if (protos_len > 0) {
1823 if (!tlsext_alpn_check_format(&cbs))
1824 goto err;
1827 if (!CBS_stow(&cbs, &ssl->internal->alpn_client_proto_list,
1828 &ssl->internal->alpn_client_proto_list_len))
1829 goto err;
1831 failed = 0;
1833 err:
1834 /* NOTE: Return values are the reverse of what you expect. */
1835 return failed;
1839 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1840 * ClientHello processing in order to select an ALPN protocol from the
1841 * client's list of offered protocols.
1843 void
1844 SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1845 int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen,
1846 const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1848 ctx->internal->alpn_select_cb = cb;
1849 ctx->internal->alpn_select_cb_arg = arg;
1853 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1854 * it sets data to point to len bytes of protocol name (not including the
1855 * leading length-prefix byte). If the server didn't respond with* a negotiated
1856 * protocol then len will be zero.
1858 void
1859 SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1860 unsigned int *len)
1862 *data = ssl->s3->alpn_selected;
1863 *len = ssl->s3->alpn_selected_len;
1866 void
1867 SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
1869 return;
1873 SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1874 const char *label, size_t llen, const unsigned char *p, size_t plen,
1875 int use_context)
1877 if (s->internal->tls13 != NULL && s->version == TLS1_3_VERSION) {
1878 if (!use_context) {
1879 p = NULL;
1880 plen = 0;
1882 return tls13_exporter(s->internal->tls13, label, llen, p, plen,
1883 out, olen);
1886 return (tls1_export_keying_material(s, out, olen, label, llen, p, plen,
1887 use_context));
1890 static unsigned long
1891 ssl_session_hash(const SSL_SESSION *a)
1893 unsigned long l;
1895 l = (unsigned long)
1896 ((unsigned int) a->session_id[0] )|
1897 ((unsigned int) a->session_id[1]<< 8L)|
1898 ((unsigned long)a->session_id[2]<<16L)|
1899 ((unsigned long)a->session_id[3]<<24L);
1900 return (l);
1904 * NB: If this function (or indeed the hash function which uses a sort of
1905 * coarser function than this one) is changed, ensure
1906 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1907 * able to construct an SSL_SESSION that will collide with any existing session
1908 * with a matching session ID.
1910 static int
1911 ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1913 if (a->ssl_version != b->ssl_version)
1914 return (1);
1915 if (a->session_id_length != b->session_id_length)
1916 return (1);
1917 if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0)
1918 return (1);
1919 return (0);
1923 * These wrapper functions should remain rather than redeclaring
1924 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1925 * variable. The reason is that the functions aren't static, they're exposed via
1926 * ssl.h.
1928 static unsigned long
1929 ssl_session_LHASH_HASH(const void *arg)
1931 const SSL_SESSION *a = arg;
1933 return ssl_session_hash(a);
1936 static int
1937 ssl_session_LHASH_COMP(const void *arg1, const void *arg2)
1939 const SSL_SESSION *a = arg1;
1940 const SSL_SESSION *b = arg2;
1942 return ssl_session_cmp(a, b);
1945 SSL_CTX *
1946 SSL_CTX_new(const SSL_METHOD *meth)
1948 SSL_CTX *ret;
1950 if (!OPENSSL_init_ssl(0, NULL)) {
1951 SSLerrorx(SSL_R_LIBRARY_BUG);
1952 return (NULL);
1955 if (meth == NULL) {
1956 SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED);
1957 return (NULL);
1960 if ((ret = calloc(1, sizeof(*ret))) == NULL) {
1961 SSLerrorx(ERR_R_MALLOC_FAILURE);
1962 return (NULL);
1964 if ((ret->internal = calloc(1, sizeof(*ret->internal))) == NULL) {
1965 free(ret);
1966 SSLerrorx(ERR_R_MALLOC_FAILURE);
1967 return (NULL);
1970 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1971 SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1972 goto err;
1975 ret->method = meth;
1976 ret->internal->min_tls_version = meth->min_tls_version;
1977 ret->internal->max_tls_version = meth->max_tls_version;
1978 ret->internal->min_proto_version = 0;
1979 ret->internal->max_proto_version = 0;
1980 ret->internal->mode = SSL_MODE_AUTO_RETRY;
1982 ret->cert_store = NULL;
1983 ret->internal->session_cache_mode = SSL_SESS_CACHE_SERVER;
1984 ret->internal->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1985 ret->internal->session_cache_head = NULL;
1986 ret->internal->session_cache_tail = NULL;
1988 /* We take the system default */
1989 ret->session_timeout = ssl_get_default_timeout();
1991 ret->internal->new_session_cb = 0;
1992 ret->internal->remove_session_cb = 0;
1993 ret->internal->get_session_cb = 0;
1994 ret->internal->generate_session_id = 0;
1996 memset((char *)&ret->internal->stats, 0, sizeof(ret->internal->stats));
1998 ret->references = 1;
1999 ret->internal->quiet_shutdown = 0;
2001 ret->internal->info_callback = NULL;
2003 ret->internal->app_verify_callback = 0;
2004 ret->internal->app_verify_arg = NULL;
2006 ret->internal->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
2007 ret->internal->read_ahead = 0;
2008 ret->internal->msg_callback = 0;
2009 ret->internal->msg_callback_arg = NULL;
2010 ret->verify_mode = SSL_VERIFY_NONE;
2011 ret->sid_ctx_length = 0;
2012 ret->internal->default_verify_callback = NULL;
2014 if ((ret->internal->cert = ssl_cert_new()) == NULL)
2015 goto err;
2017 ret->default_passwd_callback = 0;
2018 ret->default_passwd_callback_userdata = NULL;
2019 ret->internal->client_cert_cb = 0;
2020 ret->internal->app_gen_cookie_cb = 0;
2021 ret->internal->app_verify_cookie_cb = 0;
2023 ret->internal->sessions = lh_SSL_SESSION_new();
2024 if (ret->internal->sessions == NULL)
2025 goto err;
2026 ret->cert_store = X509_STORE_new();
2027 if (ret->cert_store == NULL)
2028 goto err;
2030 ssl_create_cipher_list(ret->method, &ret->cipher_list,
2031 NULL, SSL_DEFAULT_CIPHER_LIST, ret->internal->cert);
2032 if (ret->cipher_list == NULL ||
2033 sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
2034 SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS);
2035 goto err2;
2038 ret->param = X509_VERIFY_PARAM_new();
2039 if (!ret->param)
2040 goto err;
2042 if ((ret->internal->client_CA = sk_X509_NAME_new_null()) == NULL)
2043 goto err;
2045 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->internal->ex_data);
2047 ret->extra_certs = NULL;
2049 ret->internal->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2051 ret->internal->tlsext_servername_callback = 0;
2052 ret->internal->tlsext_servername_arg = NULL;
2054 /* Setup RFC4507 ticket keys */
2055 arc4random_buf(ret->internal->tlsext_tick_key_name, 16);
2056 arc4random_buf(ret->internal->tlsext_tick_hmac_key, 16);
2057 arc4random_buf(ret->internal->tlsext_tick_aes_key, 16);
2059 ret->internal->tlsext_status_cb = 0;
2060 ret->internal->tlsext_status_arg = NULL;
2062 #ifndef OPENSSL_NO_ENGINE
2063 ret->internal->client_cert_engine = NULL;
2064 #ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2065 #define eng_strx(x) #x
2066 #define eng_str(x) eng_strx(x)
2067 /* Use specific client engine automatically... ignore errors */
2069 ENGINE *eng;
2070 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2071 if (!eng) {
2072 ERR_clear_error();
2073 ENGINE_load_builtin_engines();
2074 eng = ENGINE_by_id(eng_str(
2075 OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2077 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2078 ERR_clear_error();
2080 #endif
2081 #endif
2083 * Default is to connect to non-RI servers. When RI is more widely
2084 * deployed might change this.
2086 ret->internal->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2088 return (ret);
2089 err:
2090 SSLerrorx(ERR_R_MALLOC_FAILURE);
2091 err2:
2092 SSL_CTX_free(ret);
2093 return (NULL);
2096 void
2097 SSL_CTX_free(SSL_CTX *ctx)
2099 int i;
2101 if (ctx == NULL)
2102 return;
2104 i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX);
2105 if (i > 0)
2106 return;
2108 X509_VERIFY_PARAM_free(ctx->param);
2111 * Free internal session cache. However: the remove_cb() may reference
2112 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2113 * after the sessions were flushed.
2114 * As the ex_data handling routines might also touch the session cache,
2115 * the most secure solution seems to be: empty (flush) the cache, then
2116 * free ex_data, then finally free the cache.
2117 * (See ticket [openssl.org #212].)
2119 if (ctx->internal->sessions != NULL)
2120 SSL_CTX_flush_sessions(ctx, 0);
2122 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->internal->ex_data);
2124 lh_SSL_SESSION_free(ctx->internal->sessions);
2126 X509_STORE_free(ctx->cert_store);
2127 sk_SSL_CIPHER_free(ctx->cipher_list);
2128 sk_SSL_CIPHER_free(ctx->internal->cipher_list_tls13);
2129 ssl_cert_free(ctx->internal->cert);
2130 sk_X509_NAME_pop_free(ctx->internal->client_CA, X509_NAME_free);
2131 sk_X509_pop_free(ctx->extra_certs, X509_free);
2133 #ifndef OPENSSL_NO_SRTP
2134 if (ctx->internal->srtp_profiles)
2135 sk_SRTP_PROTECTION_PROFILE_free(ctx->internal->srtp_profiles);
2136 #endif
2138 #ifndef OPENSSL_NO_ENGINE
2139 ENGINE_finish(ctx->internal->client_cert_engine);
2140 #endif
2142 free(ctx->internal->tlsext_ecpointformatlist);
2143 free(ctx->internal->tlsext_supportedgroups);
2145 free(ctx->internal->alpn_client_proto_list);
2147 free(ctx->internal);
2148 free(ctx);
2152 SSL_CTX_up_ref(SSL_CTX *ctx)
2154 int refs = CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2155 return ((refs > 1) ? 1 : 0);
2158 pem_password_cb *
2159 SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
2161 return (ctx->default_passwd_callback);
2164 void
2165 SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2167 ctx->default_passwd_callback = cb;
2170 void *
2171 SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
2173 return ctx->default_passwd_callback_userdata;
2176 void
2177 SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
2179 ctx->default_passwd_callback_userdata = u;
2182 void
2183 SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2184 int (*cb)(X509_STORE_CTX *, void *), void *arg)
2186 ctx->internal->app_verify_callback = cb;
2187 ctx->internal->app_verify_arg = arg;
2190 void
2191 SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *))
2193 ctx->verify_mode = mode;
2194 ctx->internal->default_verify_callback = cb;
2197 void
2198 SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
2200 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2203 void
2204 ssl_set_cert_masks(SSL_CERT *c, const SSL_CIPHER *cipher)
2206 unsigned long mask_a, mask_k;
2207 SSL_CERT_PKEY *cpk;
2209 if (c == NULL)
2210 return;
2212 mask_a = SSL_aNULL | SSL_aTLS1_3;
2213 mask_k = SSL_kECDHE | SSL_kTLS1_3;
2215 if (c->dhe_params != NULL || c->dhe_params_cb != NULL ||
2216 c->dhe_params_auto != 0)
2217 mask_k |= SSL_kDHE;
2219 cpk = &(c->pkeys[SSL_PKEY_ECC]);
2220 if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2221 /* Key usage, if present, must allow signing. */
2222 if (X509_get_key_usage(cpk->x509) & X509v3_KU_DIGITAL_SIGNATURE)
2223 mask_a |= SSL_aECDSA;
2226 cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2227 if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2228 mask_k |= SSL_kGOST;
2229 mask_a |= SSL_aGOST01;
2232 cpk = &(c->pkeys[SSL_PKEY_RSA]);
2233 if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2234 mask_a |= SSL_aRSA;
2235 mask_k |= SSL_kRSA;
2238 c->mask_k = mask_k;
2239 c->mask_a = mask_a;
2240 c->valid = 1;
2243 /* See if this handshake is using an ECC cipher suite. */
2245 ssl_using_ecc_cipher(SSL *s)
2247 unsigned long alg_a, alg_k;
2249 alg_a = s->s3->hs.cipher->algorithm_auth;
2250 alg_k = s->s3->hs.cipher->algorithm_mkey;
2252 return s->session->tlsext_ecpointformatlist != NULL &&
2253 s->session->tlsext_ecpointformatlist_length > 0 &&
2254 ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2258 ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x)
2260 const SSL_CIPHER *cs = s->s3->hs.cipher;
2261 unsigned long alg_a;
2263 alg_a = cs->algorithm_auth;
2265 if (alg_a & SSL_aECDSA) {
2266 /* Key usage, if present, must allow signing. */
2267 if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
2268 SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2269 return (0);
2273 return (1);
2276 SSL_CERT_PKEY *
2277 ssl_get_server_send_pkey(const SSL *s)
2279 unsigned long alg_a;
2280 SSL_CERT *c;
2281 int i;
2283 c = s->cert;
2284 ssl_set_cert_masks(c, s->s3->hs.cipher);
2286 alg_a = s->s3->hs.cipher->algorithm_auth;
2288 if (alg_a & SSL_aECDSA) {
2289 i = SSL_PKEY_ECC;
2290 } else if (alg_a & SSL_aRSA) {
2291 i = SSL_PKEY_RSA;
2292 } else if (alg_a & SSL_aGOST01) {
2293 i = SSL_PKEY_GOST01;
2294 } else { /* if (alg_a & SSL_aNULL) */
2295 SSLerror(s, ERR_R_INTERNAL_ERROR);
2296 return (NULL);
2299 return (c->pkeys + i);
2302 EVP_PKEY *
2303 ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd,
2304 const struct ssl_sigalg **sap)
2306 const struct ssl_sigalg *sigalg = NULL;
2307 EVP_PKEY *pkey = NULL;
2308 unsigned long alg_a;
2309 SSL_CERT *c;
2310 int idx = -1;
2312 alg_a = cipher->algorithm_auth;
2313 c = s->cert;
2315 if (alg_a & SSL_aRSA) {
2316 idx = SSL_PKEY_RSA;
2317 } else if ((alg_a & SSL_aECDSA) &&
2318 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2319 idx = SSL_PKEY_ECC;
2320 if (idx == -1) {
2321 SSLerror(s, ERR_R_INTERNAL_ERROR);
2322 return (NULL);
2325 pkey = c->pkeys[idx].privatekey;
2326 if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) {
2327 SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2328 return (NULL);
2330 *pmd = sigalg->md();
2331 *sap = sigalg;
2333 return (pkey);
2336 size_t
2337 ssl_dhe_params_auto_key_bits(SSL *s)
2339 SSL_CERT_PKEY *cpk;
2340 int key_bits;
2342 if (s->cert->dhe_params_auto == 2) {
2343 key_bits = 1024;
2344 } else if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) {
2345 key_bits = 1024;
2346 if (s->s3->hs.cipher->strength_bits == 256)
2347 key_bits = 3072;
2348 } else {
2349 if ((cpk = ssl_get_server_send_pkey(s)) == NULL)
2350 return 0;
2351 if (cpk->privatekey == NULL ||
2352 EVP_PKEY_get0_RSA(cpk->privatekey) == NULL)
2353 return 0;
2354 if ((key_bits = EVP_PKEY_bits(cpk->privatekey)) <= 0)
2355 return 0;
2358 return key_bits;
2361 static int
2362 ssl_should_update_external_cache(SSL *s, int mode)
2364 int cache_mode;
2366 cache_mode = s->session_ctx->internal->session_cache_mode;
2368 /* Don't cache if mode says not to */
2369 if ((cache_mode & mode) == 0)
2370 return 0;
2372 /* if it is not already cached, cache it */
2373 if (!s->internal->hit)
2374 return 1;
2376 /* If it's TLS 1.3, do it to match OpenSSL */
2377 if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION)
2378 return 1;
2380 return 0;
2383 static int
2384 ssl_should_update_internal_cache(SSL *s, int mode)
2386 int cache_mode;
2388 cache_mode = s->session_ctx->internal->session_cache_mode;
2390 /* Don't cache if mode says not to */
2391 if ((cache_mode & mode) == 0)
2392 return 0;
2394 /* If it is already cached, don't cache it again */
2395 if (s->internal->hit)
2396 return 0;
2398 if ((cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
2399 return 0;
2401 /* If we are lesser than TLS 1.3, Cache it. */
2402 if (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION)
2403 return 1;
2405 /* Below this we consider TLS 1.3 or later */
2407 /* If it's not a server, add it? OpenSSL does this. */
2408 if (!s->server)
2409 return 1;
2411 /* XXX if we support early data / PSK need to add */
2414 * If we have the remove session callback, we will want
2415 * to know about this even if it's a stateless ticket
2416 * from 1.3 so we can know when it is removed.
2418 if (s->session_ctx->internal->remove_session_cb != NULL)
2419 return 1;
2421 /* If we have set OP_NO_TICKET, cache it. */
2422 if ((s->internal->options & SSL_OP_NO_TICKET) != 0)
2423 return 1;
2425 /* Otherwise do not cache */
2426 return 0;
2429 void
2430 ssl_update_cache(SSL *s, int mode)
2432 int cache_mode, do_callback;
2434 if (s->session->session_id_length == 0)
2435 return;
2437 cache_mode = s->session_ctx->internal->session_cache_mode;
2438 do_callback = ssl_should_update_external_cache(s, mode);
2440 if (ssl_should_update_internal_cache(s, mode)) {
2442 * XXX should we fail if the add to the internal cache
2443 * fails? OpenSSL doesn't care..
2445 (void) SSL_CTX_add_session(s->session_ctx, s->session);
2449 * Update the "external cache" by calling the new session
2450 * callback if present, even with TLS 1.3 without early data
2451 * "because some application just want to know about the
2452 * creation of a session and aren't doing a full cache".
2453 * Apparently, if they are doing a full cache, they'll have
2454 * some fun, but we endeavour to give application writers the
2455 * same glorious experience they expect from OpenSSL which
2456 * does it this way.
2458 if (do_callback && s->session_ctx->internal->new_session_cb != NULL) {
2459 CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2460 if (!s->session_ctx->internal->new_session_cb(s, s->session))
2461 SSL_SESSION_free(s->session);
2464 /* Auto flush every 255 connections. */
2465 if (!(cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2466 (cache_mode & mode) != 0) {
2467 int connections;
2468 if (mode & SSL_SESS_CACHE_CLIENT)
2469 connections = s->session_ctx->internal->stats.sess_connect_good;
2470 else
2471 connections = s->session_ctx->internal->stats.sess_accept_good;
2472 if ((connections & 0xff) == 0xff)
2473 SSL_CTX_flush_sessions(s->session_ctx, time(NULL));
2477 const SSL_METHOD *
2478 SSL_get_ssl_method(SSL *s)
2480 return (s->method);
2484 SSL_set_ssl_method(SSL *s, const SSL_METHOD *method)
2486 int (*handshake_func)(SSL *) = NULL;
2487 int ret = 1;
2489 if (s->method == method)
2490 return (ret);
2492 if (s->internal->handshake_func == s->method->ssl_connect)
2493 handshake_func = method->ssl_connect;
2494 else if (s->internal->handshake_func == s->method->ssl_accept)
2495 handshake_func = method->ssl_accept;
2497 if (s->method->version == method->version) {
2498 s->method = method;
2499 } else {
2500 s->method->ssl_free(s);
2501 s->method = method;
2502 ret = s->method->ssl_new(s);
2504 s->internal->handshake_func = handshake_func;
2506 return (ret);
2510 SSL_get_error(const SSL *s, int i)
2512 unsigned long l;
2513 int reason;
2514 BIO *bio;
2516 if (i > 0)
2517 return (SSL_ERROR_NONE);
2520 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2521 * etc, where we do encode the error.
2523 if ((l = ERR_peek_error()) != 0) {
2524 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2525 return (SSL_ERROR_SYSCALL);
2526 else
2527 return (SSL_ERROR_SSL);
2530 if (SSL_want_read(s)) {
2531 bio = SSL_get_rbio(s);
2532 if (BIO_should_read(bio)) {
2533 return (SSL_ERROR_WANT_READ);
2534 } else if (BIO_should_write(bio)) {
2536 * This one doesn't make too much sense... We never
2537 * try to write to the rbio, and an application
2538 * program where rbio and wbio are separate couldn't
2539 * even know what it should wait for. However if we
2540 * ever set s->internal->rwstate incorrectly (so that we have
2541 * SSL_want_read(s) instead of SSL_want_write(s))
2542 * and rbio and wbio *are* the same, this test works
2543 * around that bug; so it might be safer to keep it.
2545 return (SSL_ERROR_WANT_WRITE);
2546 } else if (BIO_should_io_special(bio)) {
2547 reason = BIO_get_retry_reason(bio);
2548 if (reason == BIO_RR_CONNECT)
2549 return (SSL_ERROR_WANT_CONNECT);
2550 else if (reason == BIO_RR_ACCEPT)
2551 return (SSL_ERROR_WANT_ACCEPT);
2552 else
2553 return (SSL_ERROR_SYSCALL); /* unknown */
2557 if (SSL_want_write(s)) {
2558 bio = SSL_get_wbio(s);
2559 if (BIO_should_write(bio)) {
2560 return (SSL_ERROR_WANT_WRITE);
2561 } else if (BIO_should_read(bio)) {
2563 * See above (SSL_want_read(s) with
2564 * BIO_should_write(bio))
2566 return (SSL_ERROR_WANT_READ);
2567 } else if (BIO_should_io_special(bio)) {
2568 reason = BIO_get_retry_reason(bio);
2569 if (reason == BIO_RR_CONNECT)
2570 return (SSL_ERROR_WANT_CONNECT);
2571 else if (reason == BIO_RR_ACCEPT)
2572 return (SSL_ERROR_WANT_ACCEPT);
2573 else
2574 return (SSL_ERROR_SYSCALL);
2578 if (SSL_want_x509_lookup(s))
2579 return (SSL_ERROR_WANT_X509_LOOKUP);
2581 if ((s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2582 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2583 return (SSL_ERROR_ZERO_RETURN);
2585 return (SSL_ERROR_SYSCALL);
2589 SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method)
2591 if (ctx->method->dtls)
2592 return 0;
2594 ctx->quic_method = quic_method;
2596 return 1;
2600 SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method)
2602 if (ssl->method->dtls)
2603 return 0;
2605 ssl->quic_method = quic_method;
2607 return 1;
2610 size_t
2611 SSL_quic_max_handshake_flight_len(const SSL *ssl,
2612 enum ssl_encryption_level_t level)
2614 size_t flight_len;
2616 /* Limit flights to 16K when there are no large certificate messages. */
2617 flight_len = 16384;
2619 switch (level) {
2620 case ssl_encryption_initial:
2621 return flight_len;
2623 case ssl_encryption_early_data:
2624 /* QUIC does not send EndOfEarlyData. */
2625 return 0;
2627 case ssl_encryption_handshake:
2628 if (ssl->server) {
2630 * Servers may receive Certificate message if configured
2631 * to request client certificates.
2633 if ((SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER) != 0 &&
2634 ssl->internal->max_cert_list > flight_len)
2635 flight_len = ssl->internal->max_cert_list;
2636 } else {
2638 * Clients may receive both Certificate message and a
2639 * CertificateRequest message.
2641 if (ssl->internal->max_cert_list * 2 > flight_len)
2642 flight_len = ssl->internal->max_cert_list * 2;
2644 return flight_len;
2645 case ssl_encryption_application:
2647 * Note there is not actually a bound on the number of
2648 * NewSessionTickets one may send in a row. This level may need
2649 * more involved flow control.
2651 return flight_len;
2654 return 0;
2657 enum ssl_encryption_level_t
2658 SSL_quic_read_level(const SSL *ssl)
2660 return ssl->s3->hs.tls13.quic_read_level;
2663 enum ssl_encryption_level_t
2664 SSL_quic_write_level(const SSL *ssl)
2666 return ssl->s3->hs.tls13.quic_write_level;
2670 SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
2671 const uint8_t *data, size_t len)
2673 if (!SSL_is_quic(ssl)) {
2674 SSLerror(ssl, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2675 return 0;
2678 if (level != SSL_quic_read_level(ssl)) {
2679 SSLerror(ssl, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
2680 return 0;
2683 if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2684 ssl->s3->hs.tls13.quic_read_buffer = tls_buffer_new(0);
2685 if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2686 SSLerror(ssl, ERR_R_MALLOC_FAILURE);
2687 return 0;
2691 /* XXX - note that this does not currently downsize. */
2692 tls_buffer_set_capacity_limit(ssl->s3->hs.tls13.quic_read_buffer,
2693 SSL_quic_max_handshake_flight_len(ssl, level));
2696 * XXX - an append that fails due to exceeding capacity should set
2697 * SSL_R_EXCESSIVE_MESSAGE_SIZE.
2699 return tls_buffer_append(ssl->s3->hs.tls13.quic_read_buffer, data, len);
2703 SSL_process_quic_post_handshake(SSL *ssl)
2705 /* XXX - this needs to run PHH received. */
2706 return 1;
2710 SSL_do_handshake(SSL *s)
2712 if (s->internal->handshake_func == NULL) {
2713 SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET);
2714 return (-1);
2717 s->method->ssl_renegotiate_check(s);
2719 if (!SSL_in_init(s) && !SSL_in_before(s))
2720 return 1;
2722 return s->internal->handshake_func(s);
2726 * For the next 2 functions, SSL_clear() sets shutdown and so
2727 * one of these calls will reset it
2729 void
2730 SSL_set_accept_state(SSL *s)
2732 s->server = 1;
2733 s->internal->shutdown = 0;
2734 s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE;
2735 s->internal->handshake_func = s->method->ssl_accept;
2736 ssl_clear_cipher_state(s);
2739 void
2740 SSL_set_connect_state(SSL *s)
2742 s->server = 0;
2743 s->internal->shutdown = 0;
2744 s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE;
2745 s->internal->handshake_func = s->method->ssl_connect;
2746 ssl_clear_cipher_state(s);
2750 ssl_undefined_function(SSL *s)
2752 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2753 return (0);
2757 ssl_undefined_void_function(void)
2759 SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2760 return (0);
2764 ssl_undefined_const_function(const SSL *s)
2766 SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2767 return (0);
2770 const char *
2771 ssl_version_string(int ver)
2773 switch (ver) {
2774 case TLS1_VERSION:
2775 return (SSL_TXT_TLSV1);
2776 case TLS1_1_VERSION:
2777 return (SSL_TXT_TLSV1_1);
2778 case TLS1_2_VERSION:
2779 return (SSL_TXT_TLSV1_2);
2780 case TLS1_3_VERSION:
2781 return (SSL_TXT_TLSV1_3);
2782 case DTLS1_VERSION:
2783 return (SSL_TXT_DTLS1);
2784 case DTLS1_2_VERSION:
2785 return (SSL_TXT_DTLS1_2);
2786 default:
2787 return ("unknown");
2791 const char *
2792 SSL_get_version(const SSL *s)
2794 return ssl_version_string(s->version);
2797 SSL *
2798 SSL_dup(SSL *s)
2800 STACK_OF(X509_NAME) *sk;
2801 X509_NAME *xn;
2802 SSL *ret;
2803 int i;
2805 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2806 goto err;
2808 ret->version = s->version;
2809 ret->method = s->method;
2811 if (s->session != NULL) {
2812 if (!SSL_copy_session_id(ret, s))
2813 goto err;
2814 } else {
2816 * No session has been established yet, so we have to expect
2817 * that s->cert or ret->cert will be changed later --
2818 * they should not both point to the same object,
2819 * and thus we can't use SSL_copy_session_id.
2822 ret->method->ssl_free(ret);
2823 ret->method = s->method;
2824 ret->method->ssl_new(ret);
2826 ssl_cert_free(ret->cert);
2827 if ((ret->cert = ssl_cert_dup(s->cert)) == NULL)
2828 goto err;
2830 if (!SSL_set_session_id_context(ret, s->sid_ctx,
2831 s->sid_ctx_length))
2832 goto err;
2835 ret->internal->options = s->internal->options;
2836 ret->internal->mode = s->internal->mode;
2837 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2838 SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2839 ret->internal->msg_callback = s->internal->msg_callback;
2840 ret->internal->msg_callback_arg = s->internal->msg_callback_arg;
2841 SSL_set_verify(ret, SSL_get_verify_mode(s),
2842 SSL_get_verify_callback(s));
2843 SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2844 ret->internal->generate_session_id = s->internal->generate_session_id;
2846 SSL_set_info_callback(ret, SSL_get_info_callback(s));
2848 ret->internal->debug = s->internal->debug;
2850 /* copy app data, a little dangerous perhaps */
2851 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL,
2852 &ret->internal->ex_data, &s->internal->ex_data))
2853 goto err;
2855 /* setup rbio, and wbio */
2856 if (s->rbio != NULL) {
2857 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2858 goto err;
2860 if (s->wbio != NULL) {
2861 if (s->wbio != s->rbio) {
2862 if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2863 goto err;
2864 } else
2865 ret->wbio = ret->rbio;
2867 ret->internal->rwstate = s->internal->rwstate;
2868 ret->internal->in_handshake = s->internal->in_handshake;
2869 ret->internal->handshake_func = s->internal->handshake_func;
2870 ret->server = s->server;
2871 ret->internal->renegotiate = s->internal->renegotiate;
2872 ret->internal->new_session = s->internal->new_session;
2873 ret->internal->quiet_shutdown = s->internal->quiet_shutdown;
2874 ret->internal->shutdown = s->internal->shutdown;
2875 /* SSL_dup does not really work at any state, though */
2876 ret->s3->hs.state = s->s3->hs.state;
2877 ret->internal->rstate = s->internal->rstate;
2880 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2881 * ret->init_off
2883 ret->internal->init_num = 0;
2885 ret->internal->hit = s->internal->hit;
2887 X509_VERIFY_PARAM_inherit(ret->param, s->param);
2889 if (s->cipher_list != NULL) {
2890 if ((ret->cipher_list =
2891 sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2892 goto err;
2894 if (s->internal->cipher_list_tls13 != NULL) {
2895 if ((ret->internal->cipher_list_tls13 =
2896 sk_SSL_CIPHER_dup(s->internal->cipher_list_tls13)) == NULL)
2897 goto err;
2900 /* Dup the client_CA list */
2901 if (s->internal->client_CA != NULL) {
2902 if ((sk = sk_X509_NAME_dup(s->internal->client_CA)) == NULL) goto err;
2903 ret->internal->client_CA = sk;
2904 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2905 xn = sk_X509_NAME_value(sk, i);
2906 if (sk_X509_NAME_set(sk, i,
2907 X509_NAME_dup(xn)) == NULL) {
2908 X509_NAME_free(xn);
2909 goto err;
2914 return ret;
2915 err:
2916 SSL_free(ret);
2917 return NULL;
2920 void
2921 ssl_clear_cipher_state(SSL *s)
2923 tls12_record_layer_clear_read_state(s->internal->rl);
2924 tls12_record_layer_clear_write_state(s->internal->rl);
2927 void
2928 ssl_info_callback(const SSL *s, int type, int value)
2930 ssl_info_callback_fn *cb;
2932 if ((cb = s->internal->info_callback) == NULL)
2933 cb = s->ctx->internal->info_callback;
2934 if (cb != NULL)
2935 cb(s, type, value);
2938 void
2939 ssl_msg_callback(SSL *s, int is_write, int content_type,
2940 const void *msg_buf, size_t msg_len)
2942 if (s->internal->msg_callback == NULL)
2943 return;
2945 s->internal->msg_callback(is_write, s->version, content_type,
2946 msg_buf, msg_len, s, s->internal->msg_callback_arg);
2949 void
2950 ssl_msg_callback_cbs(SSL *s, int is_write, int content_type, CBS *cbs)
2952 ssl_msg_callback(s, is_write, content_type, CBS_data(cbs), CBS_len(cbs));
2955 /* Fix this function so that it takes an optional type parameter */
2956 X509 *
2957 SSL_get_certificate(const SSL *s)
2959 return (s->cert->key->x509);
2962 /* Fix this function so that it takes an optional type parameter */
2963 EVP_PKEY *
2964 SSL_get_privatekey(const SSL *s)
2966 return (s->cert->key->privatekey);
2969 const SSL_CIPHER *
2970 SSL_get_current_cipher(const SSL *s)
2972 if ((s->session != NULL) && (s->session->cipher != NULL))
2973 return (s->session->cipher);
2974 return (NULL);
2976 const void *
2977 SSL_get_current_compression(SSL *s)
2979 return (NULL);
2982 const void *
2983 SSL_get_current_expansion(SSL *s)
2985 return (NULL);
2988 size_t
2989 SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out)
2991 size_t len = sizeof(s->s3->client_random);
2993 if (out == NULL)
2994 return len;
2996 if (len > max_out)
2997 len = max_out;
2999 memcpy(out, s->s3->client_random, len);
3001 return len;
3004 size_t
3005 SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out)
3007 size_t len = sizeof(s->s3->server_random);
3009 if (out == NULL)
3010 return len;
3012 if (len > max_out)
3013 len = max_out;
3015 memcpy(out, s->s3->server_random, len);
3017 return len;
3021 ssl_init_wbio_buffer(SSL *s, int push)
3023 BIO *bbio;
3025 if (s->bbio == NULL) {
3026 bbio = BIO_new(BIO_f_buffer());
3027 if (bbio == NULL)
3028 return (0);
3029 s->bbio = bbio;
3030 } else {
3031 bbio = s->bbio;
3032 if (s->bbio == s->wbio)
3033 s->wbio = BIO_pop(s->wbio);
3035 (void)BIO_reset(bbio);
3036 /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3037 if (!BIO_set_read_buffer_size(bbio, 1)) {
3038 SSLerror(s, ERR_R_BUF_LIB);
3039 return (0);
3041 if (push) {
3042 if (s->wbio != bbio)
3043 s->wbio = BIO_push(bbio, s->wbio);
3044 } else {
3045 if (s->wbio == bbio)
3046 s->wbio = BIO_pop(bbio);
3048 return (1);
3051 void
3052 ssl_free_wbio_buffer(SSL *s)
3054 if (s == NULL)
3055 return;
3057 if (s->bbio == NULL)
3058 return;
3060 if (s->bbio == s->wbio) {
3061 /* remove buffering */
3062 s->wbio = BIO_pop(s->wbio);
3064 BIO_free(s->bbio);
3065 s->bbio = NULL;
3068 void
3069 SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
3071 ctx->internal->quiet_shutdown = mode;
3075 SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
3077 return (ctx->internal->quiet_shutdown);
3080 void
3081 SSL_set_quiet_shutdown(SSL *s, int mode)
3083 s->internal->quiet_shutdown = mode;
3087 SSL_get_quiet_shutdown(const SSL *s)
3089 return (s->internal->quiet_shutdown);
3092 void
3093 SSL_set_shutdown(SSL *s, int mode)
3095 s->internal->shutdown = mode;
3099 SSL_get_shutdown(const SSL *s)
3101 return (s->internal->shutdown);
3105 SSL_version(const SSL *s)
3107 return (s->version);
3110 SSL_CTX *
3111 SSL_get_SSL_CTX(const SSL *ssl)
3113 return (ssl->ctx);
3116 SSL_CTX *
3117 SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3119 SSL_CERT *new_cert;
3121 if (ctx == NULL)
3122 ctx = ssl->initial_ctx;
3123 if (ssl->ctx == ctx)
3124 return (ssl->ctx);
3126 if ((new_cert = ssl_cert_dup(ctx->internal->cert)) == NULL)
3127 return NULL;
3128 ssl_cert_free(ssl->cert);
3129 ssl->cert = new_cert;
3131 SSL_CTX_up_ref(ctx);
3132 SSL_CTX_free(ssl->ctx); /* decrement reference count */
3133 ssl->ctx = ctx;
3135 return (ssl->ctx);
3139 SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3141 return (X509_STORE_set_default_paths(ctx->cert_store));
3145 SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3146 const char *CApath)
3148 return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
3152 SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
3154 return (X509_STORE_load_mem(ctx->cert_store, buf, len));
3157 void
3158 SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val))
3160 ssl->internal->info_callback = cb;
3163 void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val)
3165 return (ssl->internal->info_callback);
3169 SSL_state(const SSL *ssl)
3171 return (ssl->s3->hs.state);
3174 void
3175 SSL_set_state(SSL *ssl, int state)
3177 ssl->s3->hs.state = state;
3180 void
3181 SSL_set_verify_result(SSL *ssl, long arg)
3183 ssl->verify_result = arg;
3186 long
3187 SSL_get_verify_result(const SSL *ssl)
3189 return (ssl->verify_result);
3193 SSL_verify_client_post_handshake(SSL *ssl)
3195 return 0;
3198 void
3199 SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
3201 return;
3204 void
3205 SSL_set_post_handshake_auth(SSL *ssl, int val)
3207 return;
3211 SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3212 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3214 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3215 new_func, dup_func, free_func));
3219 SSL_set_ex_data(SSL *s, int idx, void *arg)
3221 return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
3224 void *
3225 SSL_get_ex_data(const SSL *s, int idx)
3227 return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
3231 SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3232 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3234 return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3235 new_func, dup_func, free_func));
3239 SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3241 return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
3244 void *
3245 SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3247 return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
3251 ssl_ok(SSL *s)
3253 return (1);
3256 X509_STORE *
3257 SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3259 return (ctx->cert_store);
3262 void
3263 SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3265 X509_STORE_free(ctx->cert_store);
3266 ctx->cert_store = store;
3269 X509 *
3270 SSL_CTX_get0_certificate(const SSL_CTX *ctx)
3272 if (ctx->internal->cert == NULL)
3273 return NULL;
3275 return ctx->internal->cert->key->x509;
3278 EVP_PKEY *
3279 SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
3281 if (ctx->internal->cert == NULL)
3282 return NULL;
3284 return ctx->internal->cert->key->privatekey;
3288 SSL_want(const SSL *s)
3290 return (s->internal->rwstate);
3293 void
3294 SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export,
3295 int keylength))
3297 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3300 void
3301 SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
3302 int keylength))
3304 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3307 void
3308 SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export,
3309 int keylength))
3311 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3314 void
3315 SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
3316 int keylength))
3318 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3321 void
3322 SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl,
3323 int is_export, int keylength))
3325 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3326 (void (*)(void))ecdh);
3329 void
3330 SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export,
3331 int keylength))
3333 SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3337 void
3338 SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version,
3339 int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3341 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK,
3342 (void (*)(void))cb);
3345 void
3346 SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version,
3347 int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3349 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3352 void
3353 SSL_set_debug(SSL *s, int debug)
3355 s->internal->debug = debug;
3359 SSL_cache_hit(SSL *s)
3361 return (s->internal->hit);
3365 SSL_CTX_get_min_proto_version(SSL_CTX *ctx)
3367 return ctx->internal->min_proto_version;
3371 SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
3373 return ssl_version_set_min(ctx->method, version,
3374 ctx->internal->max_tls_version, &ctx->internal->min_tls_version,
3375 &ctx->internal->min_proto_version);
3379 SSL_CTX_get_max_proto_version(SSL_CTX *ctx)
3381 return ctx->internal->max_proto_version;
3385 SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
3387 return ssl_version_set_max(ctx->method, version,
3388 ctx->internal->min_tls_version, &ctx->internal->max_tls_version,
3389 &ctx->internal->max_proto_version);
3393 SSL_get_min_proto_version(SSL *ssl)
3395 return ssl->internal->min_proto_version;
3399 SSL_set_min_proto_version(SSL *ssl, uint16_t version)
3401 return ssl_version_set_min(ssl->method, version,
3402 ssl->internal->max_tls_version, &ssl->internal->min_tls_version,
3403 &ssl->internal->min_proto_version);
3406 SSL_get_max_proto_version(SSL *ssl)
3408 return ssl->internal->max_proto_version;
3412 SSL_set_max_proto_version(SSL *ssl, uint16_t version)
3414 return ssl_version_set_max(ssl->method, version,
3415 ssl->internal->min_tls_version, &ssl->internal->max_tls_version,
3416 &ssl->internal->max_proto_version);
3419 const SSL_METHOD *
3420 SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
3422 return ctx->method;
3426 SSL_CTX_get_security_level(const SSL_CTX *ctx)
3428 return ctx->internal->cert->security_level;
3431 void
3432 SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
3434 ctx->internal->cert->security_level = level;
3438 SSL_get_security_level(const SSL *ssl)
3440 return ssl->cert->security_level;
3443 void
3444 SSL_set_security_level(SSL *ssl, int level)
3446 ssl->cert->security_level = level;
3450 SSL_is_quic(const SSL *ssl)
3452 return ssl->quic_method != NULL;
3456 SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
3457 size_t params_len)
3459 freezero(ssl->internal->quic_transport_params,
3460 ssl->internal->quic_transport_params_len);
3461 ssl->internal->quic_transport_params = NULL;
3462 ssl->internal->quic_transport_params_len = 0;
3464 if ((ssl->internal->quic_transport_params = malloc(params_len)) == NULL)
3465 return 0;
3467 memcpy(ssl->internal->quic_transport_params, params, params_len);
3468 ssl->internal->quic_transport_params_len = params_len;
3470 return 1;
3473 void
3474 SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params,
3475 size_t *out_params_len)
3477 *out_params = ssl->s3->peer_quic_transport_params;
3478 *out_params_len = ssl->s3->peer_quic_transport_params_len;
3481 void
3482 SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy)
3484 /* Not supported. */
3487 static int
3488 ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
3490 SSL_CIPHER const *a = a_;
3491 SSL_CIPHER const *b = b_;
3492 return ssl_cipher_id_cmp(a, b);
3495 SSL_CIPHER *
3496 OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base, int num)
3498 return (SSL_CIPHER *)OBJ_bsearch_(key, base, num, sizeof(SSL_CIPHER),
3499 ssl_cipher_id_cmp_BSEARCH_CMP_FN);