2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
6 * Licensed under the OpenSSL license (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
14 #include <openssl/objects.h>
15 #include <openssl/lhash.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/rand.h>
18 #include <openssl/ocsp.h>
19 #include <openssl/dh.h>
20 #include <openssl/engine.h>
21 #include <openssl/async.h>
22 #include <openssl/ct.h>
23 #include "internal/cryptlib.h"
24 #include "internal/rand.h"
26 const char SSL_version_str
[] = OPENSSL_VERSION_TEXT
;
28 SSL3_ENC_METHOD ssl3_undef_enc_method
= {
30 * evil casts, but these functions are only called if there's a library
33 (int (*)(SSL
*, SSL3_RECORD
*, size_t, int))ssl_undefined_function
,
34 (int (*)(SSL
*, SSL3_RECORD
*, unsigned char *, int))ssl_undefined_function
,
35 ssl_undefined_function
,
36 (int (*)(SSL
*, unsigned char *, unsigned char *, size_t, size_t *))
37 ssl_undefined_function
,
38 (int (*)(SSL
*, int))ssl_undefined_function
,
39 (size_t (*)(SSL
*, const char *, size_t, unsigned char *))
40 ssl_undefined_function
,
41 NULL
, /* client_finished_label */
42 0, /* client_finished_label_len */
43 NULL
, /* server_finished_label */
44 0, /* server_finished_label_len */
45 (int (*)(int))ssl_undefined_function
,
46 (int (*)(SSL
*, unsigned char *, size_t, const char *,
47 size_t, const unsigned char *, size_t,
48 int use_context
))ssl_undefined_function
,
51 struct ssl_async_args
{
55 enum { READFUNC
, WRITEFUNC
, OTHERFUNC
} type
;
57 int (*func_read
) (SSL
*, void *, size_t, size_t *);
58 int (*func_write
) (SSL
*, const void *, size_t, size_t *);
59 int (*func_other
) (SSL
*);
69 DANETLS_MATCHING_FULL
, 0, NID_undef
72 DANETLS_MATCHING_2256
, 1, NID_sha256
75 DANETLS_MATCHING_2512
, 2, NID_sha512
79 static int dane_ctx_enable(struct dane_ctx_st
*dctx
)
83 uint8_t mdmax
= DANETLS_MATCHING_LAST
;
84 int n
= ((int)mdmax
) + 1; /* int to handle PrivMatch(255) */
87 if (dctx
->mdevp
!= NULL
)
90 mdevp
= OPENSSL_zalloc(n
* sizeof(*mdevp
));
91 mdord
= OPENSSL_zalloc(n
* sizeof(*mdord
));
93 if (mdord
== NULL
|| mdevp
== NULL
) {
96 SSLerr(SSL_F_DANE_CTX_ENABLE
, ERR_R_MALLOC_FAILURE
);
100 /* Install default entries */
101 for (i
= 0; i
< OSSL_NELEM(dane_mds
); ++i
) {
104 if (dane_mds
[i
].nid
== NID_undef
||
105 (md
= EVP_get_digestbynid(dane_mds
[i
].nid
)) == NULL
)
107 mdevp
[dane_mds
[i
].mtype
] = md
;
108 mdord
[dane_mds
[i
].mtype
] = dane_mds
[i
].ord
;
118 static void dane_ctx_final(struct dane_ctx_st
*dctx
)
120 OPENSSL_free(dctx
->mdevp
);
123 OPENSSL_free(dctx
->mdord
);
128 static void tlsa_free(danetls_record
*t
)
132 OPENSSL_free(t
->data
);
133 EVP_PKEY_free(t
->spki
);
137 static void dane_final(SSL_DANE
*dane
)
139 sk_danetls_record_pop_free(dane
->trecs
, tlsa_free
);
142 sk_X509_pop_free(dane
->certs
, X509_free
);
145 X509_free(dane
->mcert
);
153 * dane_copy - Copy dane configuration, sans verification state.
155 static int ssl_dane_dup(SSL
*to
, SSL
*from
)
160 if (!DANETLS_ENABLED(&from
->dane
))
163 dane_final(&to
->dane
);
164 to
->dane
.flags
= from
->dane
.flags
;
165 to
->dane
.dctx
= &to
->ctx
->dane
;
166 to
->dane
.trecs
= sk_danetls_record_new_null();
168 if (to
->dane
.trecs
== NULL
) {
169 SSLerr(SSL_F_SSL_DANE_DUP
, ERR_R_MALLOC_FAILURE
);
173 num
= sk_danetls_record_num(from
->dane
.trecs
);
174 for (i
= 0; i
< num
; ++i
) {
175 danetls_record
*t
= sk_danetls_record_value(from
->dane
.trecs
, i
);
177 if (SSL_dane_tlsa_add(to
, t
->usage
, t
->selector
, t
->mtype
,
178 t
->data
, t
->dlen
) <= 0)
184 static int dane_mtype_set(struct dane_ctx_st
*dctx
,
185 const EVP_MD
*md
, uint8_t mtype
, uint8_t ord
)
189 if (mtype
== DANETLS_MATCHING_FULL
&& md
!= NULL
) {
190 SSLerr(SSL_F_DANE_MTYPE_SET
, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL
);
194 if (mtype
> dctx
->mdmax
) {
195 const EVP_MD
**mdevp
;
197 int n
= ((int)mtype
) + 1;
199 mdevp
= OPENSSL_realloc(dctx
->mdevp
, n
* sizeof(*mdevp
));
201 SSLerr(SSL_F_DANE_MTYPE_SET
, ERR_R_MALLOC_FAILURE
);
206 mdord
= OPENSSL_realloc(dctx
->mdord
, n
* sizeof(*mdord
));
208 SSLerr(SSL_F_DANE_MTYPE_SET
, ERR_R_MALLOC_FAILURE
);
213 /* Zero-fill any gaps */
214 for (i
= dctx
->mdmax
+ 1; i
< mtype
; ++i
) {
222 dctx
->mdevp
[mtype
] = md
;
223 /* Coerce ordinal of disabled matching types to 0 */
224 dctx
->mdord
[mtype
] = (md
== NULL
) ? 0 : ord
;
229 static const EVP_MD
*tlsa_md_get(SSL_DANE
*dane
, uint8_t mtype
)
231 if (mtype
> dane
->dctx
->mdmax
)
233 return dane
->dctx
->mdevp
[mtype
];
236 static int dane_tlsa_add(SSL_DANE
*dane
,
239 uint8_t mtype
, unsigned char *data
, size_t dlen
)
242 const EVP_MD
*md
= NULL
;
243 int ilen
= (int)dlen
;
247 if (dane
->trecs
== NULL
) {
248 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_NOT_ENABLED
);
252 if (ilen
< 0 || dlen
!= (size_t)ilen
) {
253 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_DATA_LENGTH
);
257 if (usage
> DANETLS_USAGE_LAST
) {
258 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE
);
262 if (selector
> DANETLS_SELECTOR_LAST
) {
263 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_SELECTOR
);
267 if (mtype
!= DANETLS_MATCHING_FULL
) {
268 md
= tlsa_md_get(dane
, mtype
);
270 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE
);
275 if (md
!= NULL
&& dlen
!= (size_t)EVP_MD_size(md
)) {
276 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH
);
280 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_NULL_DATA
);
284 if ((t
= OPENSSL_zalloc(sizeof(*t
))) == NULL
) {
285 SSLerr(SSL_F_DANE_TLSA_ADD
, ERR_R_MALLOC_FAILURE
);
290 t
->selector
= selector
;
292 t
->data
= OPENSSL_malloc(dlen
);
293 if (t
->data
== NULL
) {
295 SSLerr(SSL_F_DANE_TLSA_ADD
, ERR_R_MALLOC_FAILURE
);
298 memcpy(t
->data
, data
, dlen
);
301 /* Validate and cache full certificate or public key */
302 if (mtype
== DANETLS_MATCHING_FULL
) {
303 const unsigned char *p
= data
;
305 EVP_PKEY
*pkey
= NULL
;
308 case DANETLS_SELECTOR_CERT
:
309 if (!d2i_X509(&cert
, &p
, ilen
) || p
< data
||
310 dlen
!= (size_t)(p
- data
)) {
312 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_CERTIFICATE
);
315 if (X509_get0_pubkey(cert
) == NULL
) {
317 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_CERTIFICATE
);
321 if ((DANETLS_USAGE_BIT(usage
) & DANETLS_TA_MASK
) == 0) {
327 * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
328 * records that contain full certificates of trust-anchors that are
329 * not present in the wire chain. For usage PKIX-TA(0), we augment
330 * the chain with untrusted Full(0) certificates from DNS, in case
331 * they are missing from the chain.
333 if ((dane
->certs
== NULL
&&
334 (dane
->certs
= sk_X509_new_null()) == NULL
) ||
335 !sk_X509_push(dane
->certs
, cert
)) {
336 SSLerr(SSL_F_DANE_TLSA_ADD
, ERR_R_MALLOC_FAILURE
);
343 case DANETLS_SELECTOR_SPKI
:
344 if (!d2i_PUBKEY(&pkey
, &p
, ilen
) || p
< data
||
345 dlen
!= (size_t)(p
- data
)) {
347 SSLerr(SSL_F_DANE_TLSA_ADD
, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY
);
352 * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
353 * records that contain full bare keys of trust-anchors that are
354 * not present in the wire chain.
356 if (usage
== DANETLS_USAGE_DANE_TA
)
365 * Find the right insertion point for the new record.
367 * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
368 * they can be processed first, as they require no chain building, and no
369 * expiration or hostname checks. Because DANE-EE(3) is numerically
370 * largest, this is accomplished via descending sort by "usage".
372 * We also sort in descending order by matching ordinal to simplify
373 * the implementation of digest agility in the verification code.
375 * The choice of order for the selector is not significant, so we
376 * use the same descending order for consistency.
378 num
= sk_danetls_record_num(dane
->trecs
);
379 for (i
= 0; i
< num
; ++i
) {
380 danetls_record
*rec
= sk_danetls_record_value(dane
->trecs
, i
);
382 if (rec
->usage
> usage
)
384 if (rec
->usage
< usage
)
386 if (rec
->selector
> selector
)
388 if (rec
->selector
< selector
)
390 if (dane
->dctx
->mdord
[rec
->mtype
] > dane
->dctx
->mdord
[mtype
])
395 if (!sk_danetls_record_insert(dane
->trecs
, t
, i
)) {
397 SSLerr(SSL_F_DANE_TLSA_ADD
, ERR_R_MALLOC_FAILURE
);
400 dane
->umask
|= DANETLS_USAGE_BIT(usage
);
406 * Return 0 if there is only one version configured and it was disabled
407 * at configure time. Return 1 otherwise.
409 static int ssl_check_allowed_versions(int min_version
, int max_version
)
411 int minisdtls
= 0, maxisdtls
= 0;
413 /* Figure out if we're doing DTLS versions or TLS versions */
414 if (min_version
== DTLS1_BAD_VER
415 || min_version
>> 8 == DTLS1_VERSION_MAJOR
)
417 if (max_version
== DTLS1_BAD_VER
418 || max_version
>> 8 == DTLS1_VERSION_MAJOR
)
420 /* A wildcard version of 0 could be DTLS or TLS. */
421 if ((minisdtls
&& !maxisdtls
&& max_version
!= 0)
422 || (maxisdtls
&& !minisdtls
&& min_version
!= 0)) {
423 /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
427 if (minisdtls
|| maxisdtls
) {
428 /* Do DTLS version checks. */
429 if (min_version
== 0)
430 /* Ignore DTLS1_BAD_VER */
431 min_version
= DTLS1_VERSION
;
432 if (max_version
== 0)
433 max_version
= DTLS1_2_VERSION
;
434 #ifdef OPENSSL_NO_DTLS1_2
435 if (max_version
== DTLS1_2_VERSION
)
436 max_version
= DTLS1_VERSION
;
438 #ifdef OPENSSL_NO_DTLS1
439 if (min_version
== DTLS1_VERSION
)
440 min_version
= DTLS1_2_VERSION
;
442 /* Done massaging versions; do the check. */
444 #ifdef OPENSSL_NO_DTLS1
445 || (DTLS_VERSION_GE(min_version
, DTLS1_VERSION
)
446 && DTLS_VERSION_GE(DTLS1_VERSION
, max_version
))
448 #ifdef OPENSSL_NO_DTLS1_2
449 || (DTLS_VERSION_GE(min_version
, DTLS1_2_VERSION
)
450 && DTLS_VERSION_GE(DTLS1_2_VERSION
, max_version
))
455 /* Regular TLS version checks. */
456 if (min_version
== 0)
457 min_version
= SSL3_VERSION
;
458 if (max_version
== 0)
459 max_version
= TLS1_3_VERSION
;
460 #ifdef OPENSSL_NO_TLS1_3
461 if (max_version
== TLS1_3_VERSION
)
462 max_version
= TLS1_2_VERSION
;
464 #ifdef OPENSSL_NO_TLS1_2
465 if (max_version
== TLS1_2_VERSION
)
466 max_version
= TLS1_1_VERSION
;
468 #ifdef OPENSSL_NO_TLS1_1
469 if (max_version
== TLS1_1_VERSION
)
470 max_version
= TLS1_VERSION
;
472 #ifdef OPENSSL_NO_TLS1
473 if (max_version
== TLS1_VERSION
)
474 max_version
= SSL3_VERSION
;
476 #ifdef OPENSSL_NO_SSL3
477 if (min_version
== SSL3_VERSION
)
478 min_version
= TLS1_VERSION
;
480 #ifdef OPENSSL_NO_TLS1
481 if (min_version
== TLS1_VERSION
)
482 min_version
= TLS1_1_VERSION
;
484 #ifdef OPENSSL_NO_TLS1_1
485 if (min_version
== TLS1_1_VERSION
)
486 min_version
= TLS1_2_VERSION
;
488 #ifdef OPENSSL_NO_TLS1_2
489 if (min_version
== TLS1_2_VERSION
)
490 min_version
= TLS1_3_VERSION
;
492 /* Done massaging versions; do the check. */
494 #ifdef OPENSSL_NO_SSL3
495 || (min_version
<= SSL3_VERSION
&& SSL3_VERSION
<= max_version
)
497 #ifdef OPENSSL_NO_TLS1
498 || (min_version
<= TLS1_VERSION
&& TLS1_VERSION
<= max_version
)
500 #ifdef OPENSSL_NO_TLS1_1
501 || (min_version
<= TLS1_1_VERSION
&& TLS1_1_VERSION
<= max_version
)
503 #ifdef OPENSSL_NO_TLS1_2
504 || (min_version
<= TLS1_2_VERSION
&& TLS1_2_VERSION
<= max_version
)
506 #ifdef OPENSSL_NO_TLS1_3
507 || (min_version
<= TLS1_3_VERSION
&& TLS1_3_VERSION
<= max_version
)
515 static void clear_ciphers(SSL
*s
)
517 /* clear the current cipher */
518 ssl_clear_cipher_ctx(s
);
519 ssl_clear_hash_ctx(&s
->read_hash
);
520 ssl_clear_hash_ctx(&s
->write_hash
);
523 int SSL_clear(SSL
*s
)
525 if (s
->method
== NULL
) {
526 SSLerr(SSL_F_SSL_CLEAR
, SSL_R_NO_METHOD_SPECIFIED
);
530 if (ssl_clear_bad_session(s
)) {
531 SSL_SESSION_free(s
->session
);
534 SSL_SESSION_free(s
->psksession
);
535 s
->psksession
= NULL
;
541 if (s
->renegotiate
) {
542 SSLerr(SSL_F_SSL_CLEAR
, ERR_R_INTERNAL_ERROR
);
546 ossl_statem_clear(s
);
548 s
->version
= s
->method
->version
;
549 s
->client_version
= s
->version
;
550 s
->rwstate
= SSL_NOTHING
;
552 BUF_MEM_free(s
->init_buf
);
557 s
->key_update
= SSL_KEY_UPDATE_NONE
;
559 /* Reset DANE verification result state */
562 X509_free(s
->dane
.mcert
);
563 s
->dane
.mcert
= NULL
;
564 s
->dane
.mtlsa
= NULL
;
566 /* Clear the verification result peername */
567 X509_VERIFY_PARAM_move_peername(s
->param
, NULL
);
570 * Check to see if we were changed into a different method, if so, revert
573 if (s
->method
!= s
->ctx
->method
) {
574 s
->method
->ssl_free(s
);
575 s
->method
= s
->ctx
->method
;
576 if (!s
->method
->ssl_new(s
))
579 if (!s
->method
->ssl_clear(s
))
583 RECORD_LAYER_clear(&s
->rlayer
);
588 /** Used to change an SSL_CTXs default SSL method type */
589 int SSL_CTX_set_ssl_version(SSL_CTX
*ctx
, const SSL_METHOD
*meth
)
591 STACK_OF(SSL_CIPHER
) *sk
;
595 sk
= ssl_create_cipher_list(ctx
->method
, &(ctx
->cipher_list
),
596 &(ctx
->cipher_list_by_id
),
597 SSL_DEFAULT_CIPHER_LIST
, ctx
->cert
);
598 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= 0)) {
599 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION
, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
605 SSL
*SSL_new(SSL_CTX
*ctx
)
610 SSLerr(SSL_F_SSL_NEW
, SSL_R_NULL_SSL_CTX
);
613 if (ctx
->method
== NULL
) {
614 SSLerr(SSL_F_SSL_NEW
, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION
);
618 s
= OPENSSL_zalloc(sizeof(*s
));
622 s
->lock
= CRYPTO_THREAD_lock_new();
627 * If not using the standard RAND (say for fuzzing), then don't use a
630 if (RAND_get_rand_method() == RAND_OpenSSL()) {
631 s
->drbg
= RAND_DRBG_new(NID_aes_128_ctr
, RAND_DRBG_FLAG_CTR_USE_DF
,
632 RAND_DRBG_get0_global());
633 if (s
->drbg
== NULL
) {
634 CRYPTO_THREAD_lock_free(s
->lock
);
639 RECORD_LAYER_init(&s
->rlayer
, s
);
641 s
->options
= ctx
->options
;
642 s
->dane
.flags
= ctx
->dane
.flags
;
643 s
->min_proto_version
= ctx
->min_proto_version
;
644 s
->max_proto_version
= ctx
->max_proto_version
;
646 s
->max_cert_list
= ctx
->max_cert_list
;
648 s
->max_early_data
= ctx
->max_early_data
;
651 * Earlier library versions used to copy the pointer to the CERT, not
652 * its contents; only when setting new parameters for the per-SSL
653 * copy, ssl_cert_new would be called (and the direct reference to
654 * the per-SSL_CTX settings would be lost, but those still were
655 * indirectly accessed for various purposes, and for that reason they
656 * used to be known as s->ctx->default_cert). Now we don't look at the
657 * SSL_CTX's CERT after having duplicated it once.
659 s
->cert
= ssl_cert_dup(ctx
->cert
);
663 RECORD_LAYER_set_read_ahead(&s
->rlayer
, ctx
->read_ahead
);
664 s
->msg_callback
= ctx
->msg_callback
;
665 s
->msg_callback_arg
= ctx
->msg_callback_arg
;
666 s
->verify_mode
= ctx
->verify_mode
;
667 s
->not_resumable_session_cb
= ctx
->not_resumable_session_cb
;
668 s
->record_padding_cb
= ctx
->record_padding_cb
;
669 s
->record_padding_arg
= ctx
->record_padding_arg
;
670 s
->block_padding
= ctx
->block_padding
;
671 s
->sid_ctx_length
= ctx
->sid_ctx_length
;
672 if (!ossl_assert(s
->sid_ctx_length
<= sizeof s
->sid_ctx
))
674 memcpy(&s
->sid_ctx
, &ctx
->sid_ctx
, sizeof(s
->sid_ctx
));
675 s
->verify_callback
= ctx
->default_verify_callback
;
676 s
->generate_session_id
= ctx
->generate_session_id
;
678 s
->param
= X509_VERIFY_PARAM_new();
679 if (s
->param
== NULL
)
681 X509_VERIFY_PARAM_inherit(s
->param
, ctx
->param
);
682 s
->quiet_shutdown
= ctx
->quiet_shutdown
;
683 s
->max_send_fragment
= ctx
->max_send_fragment
;
684 s
->split_send_fragment
= ctx
->split_send_fragment
;
685 s
->max_pipelines
= ctx
->max_pipelines
;
686 if (s
->max_pipelines
> 1)
687 RECORD_LAYER_set_read_ahead(&s
->rlayer
, 1);
688 if (ctx
->default_read_buf_len
> 0)
689 SSL_set_default_read_buffer_len(s
, ctx
->default_read_buf_len
);
694 s
->ext
.debug_arg
= NULL
;
695 s
->ext
.ticket_expected
= 0;
696 s
->ext
.status_type
= ctx
->ext
.status_type
;
697 s
->ext
.status_expected
= 0;
698 s
->ext
.ocsp
.ids
= NULL
;
699 s
->ext
.ocsp
.exts
= NULL
;
700 s
->ext
.ocsp
.resp
= NULL
;
701 s
->ext
.ocsp
.resp_len
= 0;
703 s
->session_ctx
= ctx
;
704 #ifndef OPENSSL_NO_EC
705 if (ctx
->ext
.ecpointformats
) {
706 s
->ext
.ecpointformats
=
707 OPENSSL_memdup(ctx
->ext
.ecpointformats
,
708 ctx
->ext
.ecpointformats_len
);
709 if (!s
->ext
.ecpointformats
)
711 s
->ext
.ecpointformats_len
=
712 ctx
->ext
.ecpointformats_len
;
714 if (ctx
->ext
.supportedgroups
) {
715 s
->ext
.supportedgroups
=
716 OPENSSL_memdup(ctx
->ext
.supportedgroups
,
717 ctx
->ext
.supportedgroups_len
);
718 if (!s
->ext
.supportedgroups
)
720 s
->ext
.supportedgroups_len
= ctx
->ext
.supportedgroups_len
;
723 #ifndef OPENSSL_NO_NEXTPROTONEG
727 if (s
->ctx
->ext
.alpn
) {
728 s
->ext
.alpn
= OPENSSL_malloc(s
->ctx
->ext
.alpn_len
);
729 if (s
->ext
.alpn
== NULL
)
731 memcpy(s
->ext
.alpn
, s
->ctx
->ext
.alpn
, s
->ctx
->ext
.alpn_len
);
732 s
->ext
.alpn_len
= s
->ctx
->ext
.alpn_len
;
735 s
->verified_chain
= NULL
;
736 s
->verify_result
= X509_V_OK
;
738 s
->default_passwd_callback
= ctx
->default_passwd_callback
;
739 s
->default_passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
741 s
->method
= ctx
->method
;
743 s
->key_update
= SSL_KEY_UPDATE_NONE
;
745 if (!s
->method
->ssl_new(s
))
748 s
->server
= (ctx
->method
->ssl_accept
== ssl_undefined_function
) ? 0 : 1;
753 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->ex_data
))
756 #ifndef OPENSSL_NO_PSK
757 s
->psk_client_callback
= ctx
->psk_client_callback
;
758 s
->psk_server_callback
= ctx
->psk_server_callback
;
760 s
->psk_find_session_cb
= ctx
->psk_find_session_cb
;
761 s
->psk_use_session_cb
= ctx
->psk_use_session_cb
;
765 #ifndef OPENSSL_NO_CT
766 if (!SSL_set_ct_validation_callback(s
, ctx
->ct_validation_callback
,
767 ctx
->ct_validation_callback_arg
))
774 SSLerr(SSL_F_SSL_NEW
, ERR_R_MALLOC_FAILURE
);
778 int SSL_is_dtls(const SSL
*s
)
780 return SSL_IS_DTLS(s
) ? 1 : 0;
783 int SSL_up_ref(SSL
*s
)
787 if (CRYPTO_UP_REF(&s
->references
, &i
, s
->lock
) <= 0)
790 REF_PRINT_COUNT("SSL", s
);
791 REF_ASSERT_ISNT(i
< 2);
792 return ((i
> 1) ? 1 : 0);
795 int SSL_CTX_set_session_id_context(SSL_CTX
*ctx
, const unsigned char *sid_ctx
,
796 unsigned int sid_ctx_len
)
798 if (sid_ctx_len
> sizeof ctx
->sid_ctx
) {
799 SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT
,
800 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
803 ctx
->sid_ctx_length
= sid_ctx_len
;
804 memcpy(ctx
->sid_ctx
, sid_ctx
, sid_ctx_len
);
809 int SSL_set_session_id_context(SSL
*ssl
, const unsigned char *sid_ctx
,
810 unsigned int sid_ctx_len
)
812 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
813 SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT
,
814 SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
817 ssl
->sid_ctx_length
= sid_ctx_len
;
818 memcpy(ssl
->sid_ctx
, sid_ctx
, sid_ctx_len
);
823 int SSL_CTX_set_generate_session_id(SSL_CTX
*ctx
, GEN_SESSION_CB cb
)
825 CRYPTO_THREAD_write_lock(ctx
->lock
);
826 ctx
->generate_session_id
= cb
;
827 CRYPTO_THREAD_unlock(ctx
->lock
);
831 int SSL_set_generate_session_id(SSL
*ssl
, GEN_SESSION_CB cb
)
833 CRYPTO_THREAD_write_lock(ssl
->lock
);
834 ssl
->generate_session_id
= cb
;
835 CRYPTO_THREAD_unlock(ssl
->lock
);
839 int SSL_has_matching_session_id(const SSL
*ssl
, const unsigned char *id
,
843 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
844 * we can "construct" a session to give us the desired check - i.e. to
845 * find if there's a session in the hash table that would conflict with
846 * any new session built out of this id/id_len and the ssl_version in use
851 if (id_len
> sizeof r
.session_id
)
854 r
.ssl_version
= ssl
->version
;
855 r
.session_id_length
= id_len
;
856 memcpy(r
.session_id
, id
, id_len
);
858 CRYPTO_THREAD_read_lock(ssl
->session_ctx
->lock
);
859 p
= lh_SSL_SESSION_retrieve(ssl
->session_ctx
->sessions
, &r
);
860 CRYPTO_THREAD_unlock(ssl
->session_ctx
->lock
);
864 int SSL_CTX_set_purpose(SSL_CTX
*s
, int purpose
)
866 return X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
);
869 int SSL_set_purpose(SSL
*s
, int purpose
)
871 return X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
);
874 int SSL_CTX_set_trust(SSL_CTX
*s
, int trust
)
876 return X509_VERIFY_PARAM_set_trust(s
->param
, trust
);
879 int SSL_set_trust(SSL
*s
, int trust
)
881 return X509_VERIFY_PARAM_set_trust(s
->param
, trust
);
884 int SSL_set1_host(SSL
*s
, const char *hostname
)
886 return X509_VERIFY_PARAM_set1_host(s
->param
, hostname
, 0);
889 int SSL_add1_host(SSL
*s
, const char *hostname
)
891 return X509_VERIFY_PARAM_add1_host(s
->param
, hostname
, 0);
894 void SSL_set_hostflags(SSL
*s
, unsigned int flags
)
896 X509_VERIFY_PARAM_set_hostflags(s
->param
, flags
);
899 const char *SSL_get0_peername(SSL
*s
)
901 return X509_VERIFY_PARAM_get0_peername(s
->param
);
904 int SSL_CTX_dane_enable(SSL_CTX
*ctx
)
906 return dane_ctx_enable(&ctx
->dane
);
909 unsigned long SSL_CTX_dane_set_flags(SSL_CTX
*ctx
, unsigned long flags
)
911 unsigned long orig
= ctx
->dane
.flags
;
913 ctx
->dane
.flags
|= flags
;
917 unsigned long SSL_CTX_dane_clear_flags(SSL_CTX
*ctx
, unsigned long flags
)
919 unsigned long orig
= ctx
->dane
.flags
;
921 ctx
->dane
.flags
&= ~flags
;
925 int SSL_dane_enable(SSL
*s
, const char *basedomain
)
927 SSL_DANE
*dane
= &s
->dane
;
929 if (s
->ctx
->dane
.mdmax
== 0) {
930 SSLerr(SSL_F_SSL_DANE_ENABLE
, SSL_R_CONTEXT_NOT_DANE_ENABLED
);
933 if (dane
->trecs
!= NULL
) {
934 SSLerr(SSL_F_SSL_DANE_ENABLE
, SSL_R_DANE_ALREADY_ENABLED
);
939 * Default SNI name. This rejects empty names, while set1_host below
940 * accepts them and disables host name checks. To avoid side-effects with
941 * invalid input, set the SNI name first.
943 if (s
->ext
.hostname
== NULL
) {
944 if (!SSL_set_tlsext_host_name(s
, basedomain
)) {
945 SSLerr(SSL_F_SSL_DANE_ENABLE
, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN
);
950 /* Primary RFC6125 reference identifier */
951 if (!X509_VERIFY_PARAM_set1_host(s
->param
, basedomain
, 0)) {
952 SSLerr(SSL_F_SSL_DANE_ENABLE
, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN
);
958 dane
->dctx
= &s
->ctx
->dane
;
959 dane
->trecs
= sk_danetls_record_new_null();
961 if (dane
->trecs
== NULL
) {
962 SSLerr(SSL_F_SSL_DANE_ENABLE
, ERR_R_MALLOC_FAILURE
);
968 unsigned long SSL_dane_set_flags(SSL
*ssl
, unsigned long flags
)
970 unsigned long orig
= ssl
->dane
.flags
;
972 ssl
->dane
.flags
|= flags
;
976 unsigned long SSL_dane_clear_flags(SSL
*ssl
, unsigned long flags
)
978 unsigned long orig
= ssl
->dane
.flags
;
980 ssl
->dane
.flags
&= ~flags
;
984 int SSL_get0_dane_authority(SSL
*s
, X509
**mcert
, EVP_PKEY
**mspki
)
986 SSL_DANE
*dane
= &s
->dane
;
988 if (!DANETLS_ENABLED(dane
) || s
->verify_result
!= X509_V_OK
)
992 *mcert
= dane
->mcert
;
994 *mspki
= (dane
->mcert
== NULL
) ? dane
->mtlsa
->spki
: NULL
;
999 int SSL_get0_dane_tlsa(SSL
*s
, uint8_t *usage
, uint8_t *selector
,
1000 uint8_t *mtype
, unsigned const char **data
, size_t *dlen
)
1002 SSL_DANE
*dane
= &s
->dane
;
1004 if (!DANETLS_ENABLED(dane
) || s
->verify_result
!= X509_V_OK
)
1008 *usage
= dane
->mtlsa
->usage
;
1010 *selector
= dane
->mtlsa
->selector
;
1012 *mtype
= dane
->mtlsa
->mtype
;
1014 *data
= dane
->mtlsa
->data
;
1016 *dlen
= dane
->mtlsa
->dlen
;
1021 SSL_DANE
*SSL_get0_dane(SSL
*s
)
1026 int SSL_dane_tlsa_add(SSL
*s
, uint8_t usage
, uint8_t selector
,
1027 uint8_t mtype
, unsigned char *data
, size_t dlen
)
1029 return dane_tlsa_add(&s
->dane
, usage
, selector
, mtype
, data
, dlen
);
1032 int SSL_CTX_dane_mtype_set(SSL_CTX
*ctx
, const EVP_MD
*md
, uint8_t mtype
,
1035 return dane_mtype_set(&ctx
->dane
, md
, mtype
, ord
);
1038 int SSL_CTX_set1_param(SSL_CTX
*ctx
, X509_VERIFY_PARAM
*vpm
)
1040 return X509_VERIFY_PARAM_set1(ctx
->param
, vpm
);
1043 int SSL_set1_param(SSL
*ssl
, X509_VERIFY_PARAM
*vpm
)
1045 return X509_VERIFY_PARAM_set1(ssl
->param
, vpm
);
1048 X509_VERIFY_PARAM
*SSL_CTX_get0_param(SSL_CTX
*ctx
)
1053 X509_VERIFY_PARAM
*SSL_get0_param(SSL
*ssl
)
1058 void SSL_certs_clear(SSL
*s
)
1060 ssl_cert_clear_certs(s
->cert
);
1063 void SSL_free(SSL
*s
)
1070 CRYPTO_DOWN_REF(&s
->references
, &i
, s
->lock
);
1071 REF_PRINT_COUNT("SSL", s
);
1074 REF_ASSERT_ISNT(i
< 0);
1076 X509_VERIFY_PARAM_free(s
->param
);
1077 dane_final(&s
->dane
);
1078 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->ex_data
);
1080 /* Ignore return value */
1081 ssl_free_wbio_buffer(s
);
1083 BIO_free_all(s
->wbio
);
1084 BIO_free_all(s
->rbio
);
1086 BUF_MEM_free(s
->init_buf
);
1088 /* add extra stuff */
1089 sk_SSL_CIPHER_free(s
->cipher_list
);
1090 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1092 /* Make the next call work :-) */
1093 if (s
->session
!= NULL
) {
1094 ssl_clear_bad_session(s
);
1095 SSL_SESSION_free(s
->session
);
1097 SSL_SESSION_free(s
->psksession
);
1101 ssl_cert_free(s
->cert
);
1102 /* Free up if allocated */
1104 OPENSSL_free(s
->ext
.hostname
);
1105 SSL_CTX_free(s
->session_ctx
);
1106 #ifndef OPENSSL_NO_EC
1107 OPENSSL_free(s
->ext
.ecpointformats
);
1108 OPENSSL_free(s
->ext
.supportedgroups
);
1109 #endif /* OPENSSL_NO_EC */
1110 sk_X509_EXTENSION_pop_free(s
->ext
.ocsp
.exts
, X509_EXTENSION_free
);
1111 #ifndef OPENSSL_NO_OCSP
1112 sk_OCSP_RESPID_pop_free(s
->ext
.ocsp
.ids
, OCSP_RESPID_free
);
1114 #ifndef OPENSSL_NO_CT
1115 SCT_LIST_free(s
->scts
);
1116 OPENSSL_free(s
->ext
.scts
);
1118 OPENSSL_free(s
->ext
.ocsp
.resp
);
1119 OPENSSL_free(s
->ext
.alpn
);
1120 OPENSSL_free(s
->ext
.tls13_cookie
);
1121 OPENSSL_free(s
->clienthello
);
1123 sk_X509_NAME_pop_free(s
->ca_names
, X509_NAME_free
);
1125 sk_X509_pop_free(s
->verified_chain
, X509_free
);
1127 if (s
->method
!= NULL
)
1128 s
->method
->ssl_free(s
);
1130 RECORD_LAYER_release(&s
->rlayer
);
1132 SSL_CTX_free(s
->ctx
);
1134 ASYNC_WAIT_CTX_free(s
->waitctx
);
1136 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1137 OPENSSL_free(s
->ext
.npn
);
1140 #ifndef OPENSSL_NO_SRTP
1141 sk_SRTP_PROTECTION_PROFILE_free(s
->srtp_profiles
);
1144 RAND_DRBG_free(s
->drbg
);
1145 CRYPTO_THREAD_lock_free(s
->lock
);
1150 void SSL_set0_rbio(SSL
*s
, BIO
*rbio
)
1152 BIO_free_all(s
->rbio
);
1156 void SSL_set0_wbio(SSL
*s
, BIO
*wbio
)
1159 * If the output buffering BIO is still in place, remove it
1161 if (s
->bbio
!= NULL
)
1162 s
->wbio
= BIO_pop(s
->wbio
);
1164 BIO_free_all(s
->wbio
);
1167 /* Re-attach |bbio| to the new |wbio|. */
1168 if (s
->bbio
!= NULL
)
1169 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
1172 void SSL_set_bio(SSL
*s
, BIO
*rbio
, BIO
*wbio
)
1175 * For historical reasons, this function has many different cases in
1176 * ownership handling.
1179 /* If nothing has changed, do nothing */
1180 if (rbio
== SSL_get_rbio(s
) && wbio
== SSL_get_wbio(s
))
1184 * If the two arguments are equal then one fewer reference is granted by the
1185 * caller than we want to take
1187 if (rbio
!= NULL
&& rbio
== wbio
)
1191 * If only the wbio is changed only adopt one reference.
1193 if (rbio
== SSL_get_rbio(s
)) {
1194 SSL_set0_wbio(s
, wbio
);
1198 * There is an asymmetry here for historical reasons. If only the rbio is
1199 * changed AND the rbio and wbio were originally different, then we only
1200 * adopt one reference.
1202 if (wbio
== SSL_get_wbio(s
) && SSL_get_rbio(s
) != SSL_get_wbio(s
)) {
1203 SSL_set0_rbio(s
, rbio
);
1207 /* Otherwise, adopt both references. */
1208 SSL_set0_rbio(s
, rbio
);
1209 SSL_set0_wbio(s
, wbio
);
1212 BIO
*SSL_get_rbio(const SSL
*s
)
1217 BIO
*SSL_get_wbio(const SSL
*s
)
1219 if (s
->bbio
!= NULL
) {
1221 * If |bbio| is active, the true caller-configured BIO is its
1224 return BIO_next(s
->bbio
);
1229 int SSL_get_fd(const SSL
*s
)
1231 return SSL_get_rfd(s
);
1234 int SSL_get_rfd(const SSL
*s
)
1239 b
= SSL_get_rbio(s
);
1240 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
1242 BIO_get_fd(r
, &ret
);
1246 int SSL_get_wfd(const SSL
*s
)
1251 b
= SSL_get_wbio(s
);
1252 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
1254 BIO_get_fd(r
, &ret
);
1258 #ifndef OPENSSL_NO_SOCK
1259 int SSL_set_fd(SSL
*s
, int fd
)
1264 bio
= BIO_new(BIO_s_socket());
1267 SSLerr(SSL_F_SSL_SET_FD
, ERR_R_BUF_LIB
);
1270 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1271 SSL_set_bio(s
, bio
, bio
);
1277 int SSL_set_wfd(SSL
*s
, int fd
)
1279 BIO
*rbio
= SSL_get_rbio(s
);
1281 if (rbio
== NULL
|| BIO_method_type(rbio
) != BIO_TYPE_SOCKET
1282 || (int)BIO_get_fd(rbio
, NULL
) != fd
) {
1283 BIO
*bio
= BIO_new(BIO_s_socket());
1286 SSLerr(SSL_F_SSL_SET_WFD
, ERR_R_BUF_LIB
);
1289 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1290 SSL_set0_wbio(s
, bio
);
1293 SSL_set0_wbio(s
, rbio
);
1298 int SSL_set_rfd(SSL
*s
, int fd
)
1300 BIO
*wbio
= SSL_get_wbio(s
);
1302 if (wbio
== NULL
|| BIO_method_type(wbio
) != BIO_TYPE_SOCKET
1303 || ((int)BIO_get_fd(wbio
, NULL
) != fd
)) {
1304 BIO
*bio
= BIO_new(BIO_s_socket());
1307 SSLerr(SSL_F_SSL_SET_RFD
, ERR_R_BUF_LIB
);
1310 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1311 SSL_set0_rbio(s
, bio
);
1314 SSL_set0_rbio(s
, wbio
);
1321 /* return length of latest Finished message we sent, copy to 'buf' */
1322 size_t SSL_get_finished(const SSL
*s
, void *buf
, size_t count
)
1326 if (s
->s3
!= NULL
) {
1327 ret
= s
->s3
->tmp
.finish_md_len
;
1330 memcpy(buf
, s
->s3
->tmp
.finish_md
, count
);
1335 /* return length of latest Finished message we expected, copy to 'buf' */
1336 size_t SSL_get_peer_finished(const SSL
*s
, void *buf
, size_t count
)
1340 if (s
->s3
!= NULL
) {
1341 ret
= s
->s3
->tmp
.peer_finish_md_len
;
1344 memcpy(buf
, s
->s3
->tmp
.peer_finish_md
, count
);
1349 int SSL_get_verify_mode(const SSL
*s
)
1351 return (s
->verify_mode
);
1354 int SSL_get_verify_depth(const SSL
*s
)
1356 return X509_VERIFY_PARAM_get_depth(s
->param
);
1359 int (*SSL_get_verify_callback(const SSL
*s
)) (int, X509_STORE_CTX
*) {
1360 return (s
->verify_callback
);
1363 int SSL_CTX_get_verify_mode(const SSL_CTX
*ctx
)
1365 return (ctx
->verify_mode
);
1368 int SSL_CTX_get_verify_depth(const SSL_CTX
*ctx
)
1370 return X509_VERIFY_PARAM_get_depth(ctx
->param
);
1373 int (*SSL_CTX_get_verify_callback(const SSL_CTX
*ctx
)) (int, X509_STORE_CTX
*) {
1374 return (ctx
->default_verify_callback
);
1377 void SSL_set_verify(SSL
*s
, int mode
,
1378 int (*callback
) (int ok
, X509_STORE_CTX
*ctx
))
1380 s
->verify_mode
= mode
;
1381 if (callback
!= NULL
)
1382 s
->verify_callback
= callback
;
1385 void SSL_set_verify_depth(SSL
*s
, int depth
)
1387 X509_VERIFY_PARAM_set_depth(s
->param
, depth
);
1390 void SSL_set_read_ahead(SSL
*s
, int yes
)
1392 RECORD_LAYER_set_read_ahead(&s
->rlayer
, yes
);
1395 int SSL_get_read_ahead(const SSL
*s
)
1397 return RECORD_LAYER_get_read_ahead(&s
->rlayer
);
1400 int SSL_pending(const SSL
*s
)
1402 size_t pending
= s
->method
->ssl_pending(s
);
1405 * SSL_pending cannot work properly if read-ahead is enabled
1406 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
1407 * impossible to fix since SSL_pending cannot report errors that may be
1408 * observed while scanning the new data. (Note that SSL_pending() is
1409 * often used as a boolean value, so we'd better not return -1.)
1411 * SSL_pending also cannot work properly if the value >INT_MAX. In that case
1412 * we just return INT_MAX.
1414 return pending
< INT_MAX
? (int)pending
: INT_MAX
;
1417 int SSL_has_pending(const SSL
*s
)
1420 * Similar to SSL_pending() but returns a 1 to indicate that we have
1421 * unprocessed data available or 0 otherwise (as opposed to the number of
1422 * bytes available). Unlike SSL_pending() this will take into account
1423 * read_ahead data. A 1 return simply indicates that we have unprocessed
1424 * data. That data may not result in any application data, or we may fail
1425 * to parse the records for some reason.
1427 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
))
1430 return RECORD_LAYER_read_pending(&s
->rlayer
);
1433 X509
*SSL_get_peer_certificate(const SSL
*s
)
1437 if ((s
== NULL
) || (s
->session
== NULL
))
1440 r
= s
->session
->peer
;
1450 STACK_OF(X509
) *SSL_get_peer_cert_chain(const SSL
*s
)
1454 if ((s
== NULL
) || (s
->session
== NULL
))
1457 r
= s
->session
->peer_chain
;
1460 * If we are a client, cert_chain includes the peer's own certificate; if
1461 * we are a server, it does not.
1468 * Now in theory, since the calling process own 't' it should be safe to
1469 * modify. We need to be able to read f without being hassled
1471 int SSL_copy_session_id(SSL
*t
, const SSL
*f
)
1474 /* Do we need to to SSL locking? */
1475 if (!SSL_set_session(t
, SSL_get_session(f
))) {
1480 * what if we are setup for one protocol version but want to talk another
1482 if (t
->method
!= f
->method
) {
1483 t
->method
->ssl_free(t
);
1484 t
->method
= f
->method
;
1485 if (t
->method
->ssl_new(t
) == 0)
1489 CRYPTO_UP_REF(&f
->cert
->references
, &i
, f
->cert
->lock
);
1490 ssl_cert_free(t
->cert
);
1492 if (!SSL_set_session_id_context(t
, f
->sid_ctx
, (int)f
->sid_ctx_length
)) {
1499 /* Fix this so it checks all the valid key/cert options */
1500 int SSL_CTX_check_private_key(const SSL_CTX
*ctx
)
1502 if ((ctx
== NULL
) || (ctx
->cert
->key
->x509
== NULL
)) {
1503 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
1506 if (ctx
->cert
->key
->privatekey
== NULL
) {
1507 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
1510 return (X509_check_private_key
1511 (ctx
->cert
->key
->x509
, ctx
->cert
->key
->privatekey
));
1514 /* Fix this function so that it takes an optional type parameter */
1515 int SSL_check_private_key(const SSL
*ssl
)
1518 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
, ERR_R_PASSED_NULL_PARAMETER
);
1521 if (ssl
->cert
->key
->x509
== NULL
) {
1522 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
1525 if (ssl
->cert
->key
->privatekey
== NULL
) {
1526 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
1529 return (X509_check_private_key(ssl
->cert
->key
->x509
,
1530 ssl
->cert
->key
->privatekey
));
1533 int SSL_waiting_for_async(SSL
*s
)
1541 int SSL_get_all_async_fds(SSL
*s
, OSSL_ASYNC_FD
*fds
, size_t *numfds
)
1543 ASYNC_WAIT_CTX
*ctx
= s
->waitctx
;
1547 return ASYNC_WAIT_CTX_get_all_fds(ctx
, fds
, numfds
);
1550 int SSL_get_changed_async_fds(SSL
*s
, OSSL_ASYNC_FD
*addfd
, size_t *numaddfds
,
1551 OSSL_ASYNC_FD
*delfd
, size_t *numdelfds
)
1553 ASYNC_WAIT_CTX
*ctx
= s
->waitctx
;
1557 return ASYNC_WAIT_CTX_get_changed_fds(ctx
, addfd
, numaddfds
, delfd
,
1561 int SSL_accept(SSL
*s
)
1563 if (s
->handshake_func
== NULL
) {
1564 /* Not properly initialized yet */
1565 SSL_set_accept_state(s
);
1568 return SSL_do_handshake(s
);
1571 int SSL_connect(SSL
*s
)
1573 if (s
->handshake_func
== NULL
) {
1574 /* Not properly initialized yet */
1575 SSL_set_connect_state(s
);
1578 return SSL_do_handshake(s
);
1581 long SSL_get_default_timeout(const SSL
*s
)
1583 return (s
->method
->get_timeout());
1586 static int ssl_start_async_job(SSL
*s
, struct ssl_async_args
*args
,
1587 int (*func
) (void *))
1590 if (s
->waitctx
== NULL
) {
1591 s
->waitctx
= ASYNC_WAIT_CTX_new();
1592 if (s
->waitctx
== NULL
)
1595 switch (ASYNC_start_job(&s
->job
, s
->waitctx
, &ret
, func
, args
,
1596 sizeof(struct ssl_async_args
))) {
1598 s
->rwstate
= SSL_NOTHING
;
1599 SSLerr(SSL_F_SSL_START_ASYNC_JOB
, SSL_R_FAILED_TO_INIT_ASYNC
);
1602 s
->rwstate
= SSL_ASYNC_PAUSED
;
1605 s
->rwstate
= SSL_ASYNC_NO_JOBS
;
1611 s
->rwstate
= SSL_NOTHING
;
1612 SSLerr(SSL_F_SSL_START_ASYNC_JOB
, ERR_R_INTERNAL_ERROR
);
1613 /* Shouldn't happen */
1618 static int ssl_io_intern(void *vargs
)
1620 struct ssl_async_args
*args
;
1625 args
= (struct ssl_async_args
*)vargs
;
1629 switch (args
->type
) {
1631 return args
->f
.func_read(s
, buf
, num
, &s
->asyncrw
);
1633 return args
->f
.func_write(s
, buf
, num
, &s
->asyncrw
);
1635 return args
->f
.func_other(s
);
1640 int ssl_read_internal(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
1642 if (s
->handshake_func
== NULL
) {
1643 SSLerr(SSL_F_SSL_READ_INTERNAL
, SSL_R_UNINITIALIZED
);
1647 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
1648 s
->rwstate
= SSL_NOTHING
;
1652 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECT_RETRY
1653 || s
->early_data_state
== SSL_EARLY_DATA_ACCEPT_RETRY
) {
1654 SSLerr(SSL_F_SSL_READ_INTERNAL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1658 * If we are a client and haven't received the ServerHello etc then we
1661 ossl_statem_check_finish_init(s
, 0);
1663 if ((s
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
1664 struct ssl_async_args args
;
1670 args
.type
= READFUNC
;
1671 args
.f
.func_read
= s
->method
->ssl_read
;
1673 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
1674 *readbytes
= s
->asyncrw
;
1677 return s
->method
->ssl_read(s
, buf
, num
, readbytes
);
1681 int SSL_read(SSL
*s
, void *buf
, int num
)
1687 SSLerr(SSL_F_SSL_READ
, SSL_R_BAD_LENGTH
);
1691 ret
= ssl_read_internal(s
, buf
, (size_t)num
, &readbytes
);
1694 * The cast is safe here because ret should be <= INT_MAX because num is
1698 ret
= (int)readbytes
;
1703 int SSL_read_ex(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
1705 int ret
= ssl_read_internal(s
, buf
, num
, readbytes
);
1712 int SSL_read_early_data(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
1717 SSLerr(SSL_F_SSL_READ_EARLY_DATA
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1718 return SSL_READ_EARLY_DATA_ERROR
;
1721 switch (s
->early_data_state
) {
1722 case SSL_EARLY_DATA_NONE
:
1723 if (!SSL_in_before(s
)) {
1724 SSLerr(SSL_F_SSL_READ_EARLY_DATA
,
1725 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1726 return SSL_READ_EARLY_DATA_ERROR
;
1730 case SSL_EARLY_DATA_ACCEPT_RETRY
:
1731 s
->early_data_state
= SSL_EARLY_DATA_ACCEPTING
;
1732 ret
= SSL_accept(s
);
1735 s
->early_data_state
= SSL_EARLY_DATA_ACCEPT_RETRY
;
1736 return SSL_READ_EARLY_DATA_ERROR
;
1740 case SSL_EARLY_DATA_READ_RETRY
:
1741 if (s
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
1742 s
->early_data_state
= SSL_EARLY_DATA_READING
;
1743 ret
= SSL_read_ex(s
, buf
, num
, readbytes
);
1745 * State machine will update early_data_state to
1746 * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
1749 if (ret
> 0 || (ret
<= 0 && s
->early_data_state
1750 != SSL_EARLY_DATA_FINISHED_READING
)) {
1751 s
->early_data_state
= SSL_EARLY_DATA_READ_RETRY
;
1752 return ret
> 0 ? SSL_READ_EARLY_DATA_SUCCESS
1753 : SSL_READ_EARLY_DATA_ERROR
;
1756 s
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
1759 return SSL_READ_EARLY_DATA_FINISH
;
1762 SSLerr(SSL_F_SSL_READ_EARLY_DATA
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1763 return SSL_READ_EARLY_DATA_ERROR
;
1767 int SSL_get_early_data_status(const SSL
*s
)
1769 return s
->ext
.early_data
;
1772 static int ssl_peek_internal(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
1774 if (s
->handshake_func
== NULL
) {
1775 SSLerr(SSL_F_SSL_PEEK_INTERNAL
, SSL_R_UNINITIALIZED
);
1779 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
1782 if ((s
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
1783 struct ssl_async_args args
;
1789 args
.type
= READFUNC
;
1790 args
.f
.func_read
= s
->method
->ssl_peek
;
1792 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
1793 *readbytes
= s
->asyncrw
;
1796 return s
->method
->ssl_peek(s
, buf
, num
, readbytes
);
1800 int SSL_peek(SSL
*s
, void *buf
, int num
)
1806 SSLerr(SSL_F_SSL_PEEK
, SSL_R_BAD_LENGTH
);
1810 ret
= ssl_peek_internal(s
, buf
, (size_t)num
, &readbytes
);
1813 * The cast is safe here because ret should be <= INT_MAX because num is
1817 ret
= (int)readbytes
;
1823 int SSL_peek_ex(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
1825 int ret
= ssl_peek_internal(s
, buf
, num
, readbytes
);
1832 int ssl_write_internal(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
1834 if (s
->handshake_func
== NULL
) {
1835 SSLerr(SSL_F_SSL_WRITE_INTERNAL
, SSL_R_UNINITIALIZED
);
1839 if (s
->shutdown
& SSL_SENT_SHUTDOWN
) {
1840 s
->rwstate
= SSL_NOTHING
;
1841 SSLerr(SSL_F_SSL_WRITE_INTERNAL
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
1845 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECT_RETRY
1846 || s
->early_data_state
== SSL_EARLY_DATA_ACCEPT_RETRY
1847 || s
->early_data_state
== SSL_EARLY_DATA_READ_RETRY
) {
1848 SSLerr(SSL_F_SSL_WRITE_INTERNAL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1851 /* If we are a client and haven't sent the Finished we better do that */
1852 ossl_statem_check_finish_init(s
, 1);
1854 if ((s
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
1856 struct ssl_async_args args
;
1859 args
.buf
= (void *)buf
;
1861 args
.type
= WRITEFUNC
;
1862 args
.f
.func_write
= s
->method
->ssl_write
;
1864 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
1865 *written
= s
->asyncrw
;
1868 return s
->method
->ssl_write(s
, buf
, num
, written
);
1872 int SSL_write(SSL
*s
, const void *buf
, int num
)
1878 SSLerr(SSL_F_SSL_WRITE
, SSL_R_BAD_LENGTH
);
1882 ret
= ssl_write_internal(s
, buf
, (size_t)num
, &written
);
1885 * The cast is safe here because ret should be <= INT_MAX because num is
1894 int SSL_write_ex(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
1896 int ret
= ssl_write_internal(s
, buf
, num
, written
);
1903 int SSL_write_early_data(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
1905 int ret
, early_data_state
;
1907 switch (s
->early_data_state
) {
1908 case SSL_EARLY_DATA_NONE
:
1910 || !SSL_in_before(s
)
1911 || s
->session
== NULL
1912 || s
->session
->ext
.max_early_data
== 0) {
1913 SSLerr(SSL_F_SSL_WRITE_EARLY_DATA
,
1914 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1919 case SSL_EARLY_DATA_CONNECT_RETRY
:
1920 s
->early_data_state
= SSL_EARLY_DATA_CONNECTING
;
1921 ret
= SSL_connect(s
);
1924 s
->early_data_state
= SSL_EARLY_DATA_CONNECT_RETRY
;
1929 case SSL_EARLY_DATA_WRITE_RETRY
:
1930 s
->early_data_state
= SSL_EARLY_DATA_WRITING
;
1931 ret
= SSL_write_ex(s
, buf
, num
, written
);
1932 s
->early_data_state
= SSL_EARLY_DATA_WRITE_RETRY
;
1935 case SSL_EARLY_DATA_FINISHED_READING
:
1936 case SSL_EARLY_DATA_READ_RETRY
:
1937 early_data_state
= s
->early_data_state
;
1938 /* We are a server writing to an unauthenticated client */
1939 s
->early_data_state
= SSL_EARLY_DATA_UNAUTH_WRITING
;
1940 ret
= SSL_write_ex(s
, buf
, num
, written
);
1941 s
->early_data_state
= early_data_state
;
1945 SSLerr(SSL_F_SSL_WRITE_EARLY_DATA
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
1950 int SSL_shutdown(SSL
*s
)
1953 * Note that this function behaves differently from what one might
1954 * expect. Return values are 0 for no success (yet), 1 for success; but
1955 * calling it once is usually not enough, even if blocking I/O is used
1956 * (see ssl3_shutdown).
1959 if (s
->handshake_func
== NULL
) {
1960 SSLerr(SSL_F_SSL_SHUTDOWN
, SSL_R_UNINITIALIZED
);
1964 if (!SSL_in_init(s
)) {
1965 if ((s
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
1966 struct ssl_async_args args
;
1969 args
.type
= OTHERFUNC
;
1970 args
.f
.func_other
= s
->method
->ssl_shutdown
;
1972 return ssl_start_async_job(s
, &args
, ssl_io_intern
);
1974 return s
->method
->ssl_shutdown(s
);
1977 SSLerr(SSL_F_SSL_SHUTDOWN
, SSL_R_SHUTDOWN_WHILE_IN_INIT
);
1982 int SSL_key_update(SSL
*s
, int updatetype
)
1985 * TODO(TLS1.3): How will applications know whether TLSv1.3 has been
1986 * negotiated, and that it is appropriate to call SSL_key_update() instead
1987 * of SSL_renegotiate().
1989 if (!SSL_IS_TLS13(s
)) {
1990 SSLerr(SSL_F_SSL_KEY_UPDATE
, SSL_R_WRONG_SSL_VERSION
);
1994 if (updatetype
!= SSL_KEY_UPDATE_NOT_REQUESTED
1995 && updatetype
!= SSL_KEY_UPDATE_REQUESTED
) {
1996 SSLerr(SSL_F_SSL_KEY_UPDATE
, SSL_R_INVALID_KEY_UPDATE_TYPE
);
2000 if (!SSL_is_init_finished(s
)) {
2001 SSLerr(SSL_F_SSL_KEY_UPDATE
, SSL_R_STILL_IN_INIT
);
2005 ossl_statem_set_in_init(s
, 1);
2006 s
->key_update
= updatetype
;
2010 int SSL_get_key_update_type(SSL
*s
)
2012 return s
->key_update
;
2015 int SSL_renegotiate(SSL
*s
)
2017 if (SSL_IS_TLS13(s
)) {
2018 SSLerr(SSL_F_SSL_RENEGOTIATE
, SSL_R_WRONG_SSL_VERSION
);
2022 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
)) {
2023 SSLerr(SSL_F_SSL_RENEGOTIATE
, SSL_R_NO_RENEGOTIATION
);
2030 return (s
->method
->ssl_renegotiate(s
));
2033 int SSL_renegotiate_abbreviated(SSL
*s
)
2035 if (SSL_IS_TLS13(s
)) {
2036 SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED
, SSL_R_WRONG_SSL_VERSION
);
2040 if ((s
->options
& SSL_OP_NO_RENEGOTIATION
)) {
2041 SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED
, SSL_R_NO_RENEGOTIATION
);
2048 return (s
->method
->ssl_renegotiate(s
));
2051 int SSL_renegotiate_pending(SSL
*s
)
2054 * becomes true when negotiation is requested; false again once a
2055 * handshake has finished
2057 return (s
->renegotiate
!= 0);
2060 long SSL_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
2065 case SSL_CTRL_GET_READ_AHEAD
:
2066 return (RECORD_LAYER_get_read_ahead(&s
->rlayer
));
2067 case SSL_CTRL_SET_READ_AHEAD
:
2068 l
= RECORD_LAYER_get_read_ahead(&s
->rlayer
);
2069 RECORD_LAYER_set_read_ahead(&s
->rlayer
, larg
);
2072 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
2073 s
->msg_callback_arg
= parg
;
2077 return (s
->mode
|= larg
);
2078 case SSL_CTRL_CLEAR_MODE
:
2079 return (s
->mode
&= ~larg
);
2080 case SSL_CTRL_GET_MAX_CERT_LIST
:
2081 return (long)(s
->max_cert_list
);
2082 case SSL_CTRL_SET_MAX_CERT_LIST
:
2085 l
= (long)s
->max_cert_list
;
2086 s
->max_cert_list
= (size_t)larg
;
2088 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
2089 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
2091 s
->max_send_fragment
= larg
;
2092 if (s
->max_send_fragment
< s
->split_send_fragment
)
2093 s
->split_send_fragment
= s
->max_send_fragment
;
2095 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
2096 if ((size_t)larg
> s
->max_send_fragment
|| larg
== 0)
2098 s
->split_send_fragment
= larg
;
2100 case SSL_CTRL_SET_MAX_PIPELINES
:
2101 if (larg
< 1 || larg
> SSL_MAX_PIPELINES
)
2103 s
->max_pipelines
= larg
;
2105 RECORD_LAYER_set_read_ahead(&s
->rlayer
, 1);
2107 case SSL_CTRL_GET_RI_SUPPORT
:
2109 return s
->s3
->send_connection_binding
;
2112 case SSL_CTRL_CERT_FLAGS
:
2113 return (s
->cert
->cert_flags
|= larg
);
2114 case SSL_CTRL_CLEAR_CERT_FLAGS
:
2115 return (s
->cert
->cert_flags
&= ~larg
);
2117 case SSL_CTRL_GET_RAW_CIPHERLIST
:
2119 if (s
->s3
->tmp
.ciphers_raw
== NULL
)
2121 *(unsigned char **)parg
= s
->s3
->tmp
.ciphers_raw
;
2122 return (int)s
->s3
->tmp
.ciphers_rawlen
;
2124 return TLS_CIPHER_LEN
;
2126 case SSL_CTRL_GET_EXTMS_SUPPORT
:
2127 if (!s
->session
|| SSL_in_init(s
) || ossl_statem_get_in_handshake(s
))
2129 if (s
->session
->flags
& SSL_SESS_FLAG_EXTMS
)
2133 case SSL_CTRL_SET_MIN_PROTO_VERSION
:
2134 return ssl_check_allowed_versions(larg
, s
->max_proto_version
)
2135 && ssl_set_version_bound(s
->ctx
->method
->version
, (int)larg
,
2136 &s
->min_proto_version
);
2137 case SSL_CTRL_SET_MAX_PROTO_VERSION
:
2138 return ssl_check_allowed_versions(s
->min_proto_version
, larg
)
2139 && ssl_set_version_bound(s
->ctx
->method
->version
, (int)larg
,
2140 &s
->max_proto_version
);
2142 return (s
->method
->ssl_ctrl(s
, cmd
, larg
, parg
));
2146 long SSL_callback_ctrl(SSL
*s
, int cmd
, void (*fp
) (void))
2149 case SSL_CTRL_SET_MSG_CALLBACK
:
2150 s
->msg_callback
= (void (*)
2151 (int write_p
, int version
, int content_type
,
2152 const void *buf
, size_t len
, SSL
*ssl
,
2157 return (s
->method
->ssl_callback_ctrl(s
, cmd
, fp
));
2161 LHASH_OF(SSL_SESSION
) *SSL_CTX_sessions(SSL_CTX
*ctx
)
2163 return ctx
->sessions
;
2166 long SSL_CTX_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
2169 /* For some cases with ctx == NULL perform syntax checks */
2172 #ifndef OPENSSL_NO_EC
2173 case SSL_CTRL_SET_GROUPS_LIST
:
2174 return tls1_set_groups_list(NULL
, NULL
, parg
);
2176 case SSL_CTRL_SET_SIGALGS_LIST
:
2177 case SSL_CTRL_SET_CLIENT_SIGALGS_LIST
:
2178 return tls1_set_sigalgs_list(NULL
, parg
, 0);
2185 case SSL_CTRL_GET_READ_AHEAD
:
2186 return (ctx
->read_ahead
);
2187 case SSL_CTRL_SET_READ_AHEAD
:
2188 l
= ctx
->read_ahead
;
2189 ctx
->read_ahead
= larg
;
2192 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
2193 ctx
->msg_callback_arg
= parg
;
2196 case SSL_CTRL_GET_MAX_CERT_LIST
:
2197 return (long)(ctx
->max_cert_list
);
2198 case SSL_CTRL_SET_MAX_CERT_LIST
:
2201 l
= (long)ctx
->max_cert_list
;
2202 ctx
->max_cert_list
= (size_t)larg
;
2205 case SSL_CTRL_SET_SESS_CACHE_SIZE
:
2208 l
= (long)ctx
->session_cache_size
;
2209 ctx
->session_cache_size
= (size_t)larg
;
2211 case SSL_CTRL_GET_SESS_CACHE_SIZE
:
2212 return (long)(ctx
->session_cache_size
);
2213 case SSL_CTRL_SET_SESS_CACHE_MODE
:
2214 l
= ctx
->session_cache_mode
;
2215 ctx
->session_cache_mode
= larg
;
2217 case SSL_CTRL_GET_SESS_CACHE_MODE
:
2218 return (ctx
->session_cache_mode
);
2220 case SSL_CTRL_SESS_NUMBER
:
2221 return (lh_SSL_SESSION_num_items(ctx
->sessions
));
2222 case SSL_CTRL_SESS_CONNECT
:
2223 return (ctx
->stats
.sess_connect
);
2224 case SSL_CTRL_SESS_CONNECT_GOOD
:
2225 return (ctx
->stats
.sess_connect_good
);
2226 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE
:
2227 return (ctx
->stats
.sess_connect_renegotiate
);
2228 case SSL_CTRL_SESS_ACCEPT
:
2229 return (ctx
->stats
.sess_accept
);
2230 case SSL_CTRL_SESS_ACCEPT_GOOD
:
2231 return (ctx
->stats
.sess_accept_good
);
2232 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE
:
2233 return (ctx
->stats
.sess_accept_renegotiate
);
2234 case SSL_CTRL_SESS_HIT
:
2235 return (ctx
->stats
.sess_hit
);
2236 case SSL_CTRL_SESS_CB_HIT
:
2237 return (ctx
->stats
.sess_cb_hit
);
2238 case SSL_CTRL_SESS_MISSES
:
2239 return (ctx
->stats
.sess_miss
);
2240 case SSL_CTRL_SESS_TIMEOUTS
:
2241 return (ctx
->stats
.sess_timeout
);
2242 case SSL_CTRL_SESS_CACHE_FULL
:
2243 return (ctx
->stats
.sess_cache_full
);
2245 return (ctx
->mode
|= larg
);
2246 case SSL_CTRL_CLEAR_MODE
:
2247 return (ctx
->mode
&= ~larg
);
2248 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
2249 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
2251 ctx
->max_send_fragment
= larg
;
2252 if (ctx
->max_send_fragment
< ctx
->split_send_fragment
)
2253 ctx
->split_send_fragment
= ctx
->max_send_fragment
;
2255 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
2256 if ((size_t)larg
> ctx
->max_send_fragment
|| larg
== 0)
2258 ctx
->split_send_fragment
= larg
;
2260 case SSL_CTRL_SET_MAX_PIPELINES
:
2261 if (larg
< 1 || larg
> SSL_MAX_PIPELINES
)
2263 ctx
->max_pipelines
= larg
;
2265 case SSL_CTRL_CERT_FLAGS
:
2266 return (ctx
->cert
->cert_flags
|= larg
);
2267 case SSL_CTRL_CLEAR_CERT_FLAGS
:
2268 return (ctx
->cert
->cert_flags
&= ~larg
);
2269 case SSL_CTRL_SET_MIN_PROTO_VERSION
:
2270 return ssl_check_allowed_versions(larg
, ctx
->max_proto_version
)
2271 && ssl_set_version_bound(ctx
->method
->version
, (int)larg
,
2272 &ctx
->min_proto_version
);
2273 case SSL_CTRL_SET_MAX_PROTO_VERSION
:
2274 return ssl_check_allowed_versions(ctx
->min_proto_version
, larg
)
2275 && ssl_set_version_bound(ctx
->method
->version
, (int)larg
,
2276 &ctx
->max_proto_version
);
2278 return (ctx
->method
->ssl_ctx_ctrl(ctx
, cmd
, larg
, parg
));
2282 long SSL_CTX_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
) (void))
2285 case SSL_CTRL_SET_MSG_CALLBACK
:
2286 ctx
->msg_callback
= (void (*)
2287 (int write_p
, int version
, int content_type
,
2288 const void *buf
, size_t len
, SSL
*ssl
,
2293 return (ctx
->method
->ssl_ctx_callback_ctrl(ctx
, cmd
, fp
));
2297 int ssl_cipher_id_cmp(const SSL_CIPHER
*a
, const SSL_CIPHER
*b
)
2306 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER
*const *ap
,
2307 const SSL_CIPHER
*const *bp
)
2309 if ((*ap
)->id
> (*bp
)->id
)
2311 if ((*ap
)->id
< (*bp
)->id
)
2316 /** return a STACK of the ciphers available for the SSL and in order of
2318 STACK_OF(SSL_CIPHER
) *SSL_get_ciphers(const SSL
*s
)
2321 if (s
->cipher_list
!= NULL
) {
2322 return (s
->cipher_list
);
2323 } else if ((s
->ctx
!= NULL
) && (s
->ctx
->cipher_list
!= NULL
)) {
2324 return (s
->ctx
->cipher_list
);
2330 STACK_OF(SSL_CIPHER
) *SSL_get_client_ciphers(const SSL
*s
)
2332 if ((s
== NULL
) || (s
->session
== NULL
) || !s
->server
)
2334 return s
->session
->ciphers
;
2337 STACK_OF(SSL_CIPHER
) *SSL_get1_supported_ciphers(SSL
*s
)
2339 STACK_OF(SSL_CIPHER
) *sk
= NULL
, *ciphers
;
2341 ciphers
= SSL_get_ciphers(s
);
2344 ssl_set_client_disabled(s
);
2345 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
2346 const SSL_CIPHER
*c
= sk_SSL_CIPHER_value(ciphers
, i
);
2347 if (!ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_SUPPORTED
, 0)) {
2349 sk
= sk_SSL_CIPHER_new_null();
2352 if (!sk_SSL_CIPHER_push(sk
, c
)) {
2353 sk_SSL_CIPHER_free(sk
);
2361 /** return a STACK of the ciphers available for the SSL and in order of
2363 STACK_OF(SSL_CIPHER
) *ssl_get_ciphers_by_id(SSL
*s
)
2366 if (s
->cipher_list_by_id
!= NULL
) {
2367 return (s
->cipher_list_by_id
);
2368 } else if ((s
->ctx
!= NULL
) && (s
->ctx
->cipher_list_by_id
!= NULL
)) {
2369 return (s
->ctx
->cipher_list_by_id
);
2375 /** The old interface to get the same thing as SSL_get_ciphers() */
2376 const char *SSL_get_cipher_list(const SSL
*s
, int n
)
2378 const SSL_CIPHER
*c
;
2379 STACK_OF(SSL_CIPHER
) *sk
;
2383 sk
= SSL_get_ciphers(s
);
2384 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= n
))
2386 c
= sk_SSL_CIPHER_value(sk
, n
);
2392 /** return a STACK of the ciphers available for the SSL_CTX and in order of
2394 STACK_OF(SSL_CIPHER
) *SSL_CTX_get_ciphers(const SSL_CTX
*ctx
)
2397 return ctx
->cipher_list
;
2401 /** specify the ciphers to be used by default by the SSL_CTX */
2402 int SSL_CTX_set_cipher_list(SSL_CTX
*ctx
, const char *str
)
2404 STACK_OF(SSL_CIPHER
) *sk
;
2406 sk
= ssl_create_cipher_list(ctx
->method
, &ctx
->cipher_list
,
2407 &ctx
->cipher_list_by_id
, str
, ctx
->cert
);
2409 * ssl_create_cipher_list may return an empty stack if it was unable to
2410 * find a cipher matching the given rule string (for example if the rule
2411 * string specifies a cipher which has been disabled). This is not an
2412 * error as far as ssl_create_cipher_list is concerned, and hence
2413 * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
2417 else if (sk_SSL_CIPHER_num(sk
) == 0) {
2418 SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST
, SSL_R_NO_CIPHER_MATCH
);
2424 /** specify the ciphers to be used by the SSL */
2425 int SSL_set_cipher_list(SSL
*s
, const char *str
)
2427 STACK_OF(SSL_CIPHER
) *sk
;
2429 sk
= ssl_create_cipher_list(s
->ctx
->method
, &s
->cipher_list
,
2430 &s
->cipher_list_by_id
, str
, s
->cert
);
2431 /* see comment in SSL_CTX_set_cipher_list */
2434 else if (sk_SSL_CIPHER_num(sk
) == 0) {
2435 SSLerr(SSL_F_SSL_SET_CIPHER_LIST
, SSL_R_NO_CIPHER_MATCH
);
2441 char *SSL_get_shared_ciphers(const SSL
*s
, char *buf
, int len
)
2444 STACK_OF(SSL_CIPHER
) *sk
;
2445 const SSL_CIPHER
*c
;
2448 if ((s
->session
== NULL
) || (s
->session
->ciphers
== NULL
) || (len
< 2))
2452 sk
= s
->session
->ciphers
;
2454 if (sk_SSL_CIPHER_num(sk
) == 0)
2457 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); i
++) {
2460 c
= sk_SSL_CIPHER_value(sk
, i
);
2461 n
= strlen(c
->name
);
2468 memcpy(p
, c
->name
, n
+ 1);
2477 /** return a servername extension value if provided in Client Hello, or NULL.
2478 * So far, only host_name types are defined (RFC 3546).
2481 const char *SSL_get_servername(const SSL
*s
, const int type
)
2483 if (type
!= TLSEXT_NAMETYPE_host_name
)
2486 return s
->session
&& !s
->ext
.hostname
?
2487 s
->session
->ext
.hostname
: s
->ext
.hostname
;
2490 int SSL_get_servername_type(const SSL
*s
)
2493 && (!s
->ext
.hostname
? s
->session
->
2494 ext
.hostname
: s
->ext
.hostname
))
2495 return TLSEXT_NAMETYPE_host_name
;
2500 * SSL_select_next_proto implements the standard protocol selection. It is
2501 * expected that this function is called from the callback set by
2502 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
2503 * vector of 8-bit, length prefixed byte strings. The length byte itself is
2504 * not included in the length. A byte string of length 0 is invalid. No byte
2505 * string may be truncated. The current, but experimental algorithm for
2506 * selecting the protocol is: 1) If the server doesn't support NPN then this
2507 * is indicated to the callback. In this case, the client application has to
2508 * abort the connection or have a default application level protocol. 2) If
2509 * the server supports NPN, but advertises an empty list then the client
2510 * selects the first protocol in its list, but indicates via the API that this
2511 * fallback case was enacted. 3) Otherwise, the client finds the first
2512 * protocol in the server's list that it supports and selects this protocol.
2513 * This is because it's assumed that the server has better information about
2514 * which protocol a client should use. 4) If the client doesn't support any
2515 * of the server's advertised protocols, then this is treated the same as
2516 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
2517 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
2519 int SSL_select_next_proto(unsigned char **out
, unsigned char *outlen
,
2520 const unsigned char *server
,
2521 unsigned int server_len
,
2522 const unsigned char *client
, unsigned int client_len
)
2525 const unsigned char *result
;
2526 int status
= OPENSSL_NPN_UNSUPPORTED
;
2529 * For each protocol in server preference order, see if we support it.
2531 for (i
= 0; i
< server_len
;) {
2532 for (j
= 0; j
< client_len
;) {
2533 if (server
[i
] == client
[j
] &&
2534 memcmp(&server
[i
+ 1], &client
[j
+ 1], server
[i
]) == 0) {
2535 /* We found a match */
2536 result
= &server
[i
];
2537 status
= OPENSSL_NPN_NEGOTIATED
;
2547 /* There's no overlap between our protocols and the server's list. */
2549 status
= OPENSSL_NPN_NO_OVERLAP
;
2552 *out
= (unsigned char *)result
+ 1;
2553 *outlen
= result
[0];
2557 #ifndef OPENSSL_NO_NEXTPROTONEG
2559 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
2560 * client's requested protocol for this connection and returns 0. If the
2561 * client didn't request any protocol, then *data is set to NULL. Note that
2562 * the client can request any protocol it chooses. The value returned from
2563 * this function need not be a member of the list of supported protocols
2564 * provided by the callback.
2566 void SSL_get0_next_proto_negotiated(const SSL
*s
, const unsigned char **data
,
2573 *len
= (unsigned int)s
->ext
.npn_len
;
2578 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
2579 * a TLS server needs a list of supported protocols for Next Protocol
2580 * Negotiation. The returned list must be in wire format. The list is
2581 * returned by setting |out| to point to it and |outlen| to its length. This
2582 * memory will not be modified, but one should assume that the SSL* keeps a
2583 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
2584 * wishes to advertise. Otherwise, no such extension will be included in the
2587 void SSL_CTX_set_npn_advertised_cb(SSL_CTX
*ctx
,
2588 SSL_CTX_npn_advertised_cb_func cb
,
2591 ctx
->ext
.npn_advertised_cb
= cb
;
2592 ctx
->ext
.npn_advertised_cb_arg
= arg
;
2596 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
2597 * client needs to select a protocol from the server's provided list. |out|
2598 * must be set to point to the selected protocol (which may be within |in|).
2599 * The length of the protocol name must be written into |outlen|. The
2600 * server's advertised protocols are provided in |in| and |inlen|. The
2601 * callback can assume that |in| is syntactically valid. The client must
2602 * select a protocol. It is fatal to the connection if this callback returns
2603 * a value other than SSL_TLSEXT_ERR_OK.
2605 void SSL_CTX_set_npn_select_cb(SSL_CTX
*ctx
,
2606 SSL_CTX_npn_select_cb_func cb
,
2609 ctx
->ext
.npn_select_cb
= cb
;
2610 ctx
->ext
.npn_select_cb_arg
= arg
;
2615 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
2616 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
2617 * length-prefixed strings). Returns 0 on success.
2619 int SSL_CTX_set_alpn_protos(SSL_CTX
*ctx
, const unsigned char *protos
,
2620 unsigned int protos_len
)
2622 OPENSSL_free(ctx
->ext
.alpn
);
2623 ctx
->ext
.alpn
= OPENSSL_memdup(protos
, protos_len
);
2624 if (ctx
->ext
.alpn
== NULL
) {
2625 SSLerr(SSL_F_SSL_CTX_SET_ALPN_PROTOS
, ERR_R_MALLOC_FAILURE
);
2628 ctx
->ext
.alpn_len
= protos_len
;
2634 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
2635 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
2636 * length-prefixed strings). Returns 0 on success.
2638 int SSL_set_alpn_protos(SSL
*ssl
, const unsigned char *protos
,
2639 unsigned int protos_len
)
2641 OPENSSL_free(ssl
->ext
.alpn
);
2642 ssl
->ext
.alpn
= OPENSSL_memdup(protos
, protos_len
);
2643 if (ssl
->ext
.alpn
== NULL
) {
2644 SSLerr(SSL_F_SSL_SET_ALPN_PROTOS
, ERR_R_MALLOC_FAILURE
);
2647 ssl
->ext
.alpn_len
= protos_len
;
2653 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
2654 * called during ClientHello processing in order to select an ALPN protocol
2655 * from the client's list of offered protocols.
2657 void SSL_CTX_set_alpn_select_cb(SSL_CTX
*ctx
,
2658 SSL_CTX_alpn_select_cb_func cb
,
2661 ctx
->ext
.alpn_select_cb
= cb
;
2662 ctx
->ext
.alpn_select_cb_arg
= arg
;
2666 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
2667 * On return it sets |*data| to point to |*len| bytes of protocol name
2668 * (not including the leading length-prefix byte). If the server didn't
2669 * respond with a negotiated protocol then |*len| will be zero.
2671 void SSL_get0_alpn_selected(const SSL
*ssl
, const unsigned char **data
,
2676 *data
= ssl
->s3
->alpn_selected
;
2680 *len
= (unsigned int)ssl
->s3
->alpn_selected_len
;
2683 int SSL_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
2684 const char *label
, size_t llen
,
2685 const unsigned char *context
, size_t contextlen
,
2688 if (s
->version
< TLS1_VERSION
&& s
->version
!= DTLS1_BAD_VER
)
2691 return s
->method
->ssl3_enc
->export_keying_material(s
, out
, olen
, label
,
2693 contextlen
, use_context
);
2696 static unsigned long ssl_session_hash(const SSL_SESSION
*a
)
2698 const unsigned char *session_id
= a
->session_id
;
2700 unsigned char tmp_storage
[4];
2702 if (a
->session_id_length
< sizeof(tmp_storage
)) {
2703 memset(tmp_storage
, 0, sizeof(tmp_storage
));
2704 memcpy(tmp_storage
, a
->session_id
, a
->session_id_length
);
2705 session_id
= tmp_storage
;
2709 ((unsigned long)session_id
[0]) |
2710 ((unsigned long)session_id
[1] << 8L) |
2711 ((unsigned long)session_id
[2] << 16L) |
2712 ((unsigned long)session_id
[3] << 24L);
2717 * NB: If this function (or indeed the hash function which uses a sort of
2718 * coarser function than this one) is changed, ensure
2719 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
2720 * being able to construct an SSL_SESSION that will collide with any existing
2721 * session with a matching session ID.
2723 static int ssl_session_cmp(const SSL_SESSION
*a
, const SSL_SESSION
*b
)
2725 if (a
->ssl_version
!= b
->ssl_version
)
2727 if (a
->session_id_length
!= b
->session_id_length
)
2729 return (memcmp(a
->session_id
, b
->session_id
, a
->session_id_length
));
2733 * These wrapper functions should remain rather than redeclaring
2734 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
2735 * variable. The reason is that the functions aren't static, they're exposed
2739 SSL_CTX
*SSL_CTX_new(const SSL_METHOD
*meth
)
2741 SSL_CTX
*ret
= NULL
;
2744 SSLerr(SSL_F_SSL_CTX_NEW
, SSL_R_NULL_SSL_METHOD_PASSED
);
2748 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
, NULL
))
2751 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
2752 SSLerr(SSL_F_SSL_CTX_NEW
, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS
);
2755 ret
= OPENSSL_zalloc(sizeof(*ret
));
2760 ret
->min_proto_version
= 0;
2761 ret
->max_proto_version
= 0;
2762 ret
->session_cache_mode
= SSL_SESS_CACHE_SERVER
;
2763 ret
->session_cache_size
= SSL_SESSION_CACHE_MAX_SIZE_DEFAULT
;
2764 /* We take the system default. */
2765 ret
->session_timeout
= meth
->get_timeout();
2766 ret
->references
= 1;
2767 ret
->lock
= CRYPTO_THREAD_lock_new();
2768 if (ret
->lock
== NULL
) {
2769 SSLerr(SSL_F_SSL_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
2773 ret
->max_cert_list
= SSL_MAX_CERT_LIST_DEFAULT
;
2774 ret
->verify_mode
= SSL_VERIFY_NONE
;
2775 if ((ret
->cert
= ssl_cert_new()) == NULL
)
2778 ret
->sessions
= lh_SSL_SESSION_new(ssl_session_hash
, ssl_session_cmp
);
2779 if (ret
->sessions
== NULL
)
2781 ret
->cert_store
= X509_STORE_new();
2782 if (ret
->cert_store
== NULL
)
2784 #ifndef OPENSSL_NO_CT
2785 ret
->ctlog_store
= CTLOG_STORE_new();
2786 if (ret
->ctlog_store
== NULL
)
2789 if (!ssl_create_cipher_list(ret
->method
,
2790 &ret
->cipher_list
, &ret
->cipher_list_by_id
,
2791 SSL_DEFAULT_CIPHER_LIST
, ret
->cert
)
2792 || sk_SSL_CIPHER_num(ret
->cipher_list
) <= 0) {
2793 SSLerr(SSL_F_SSL_CTX_NEW
, SSL_R_LIBRARY_HAS_NO_CIPHERS
);
2797 ret
->param
= X509_VERIFY_PARAM_new();
2798 if (ret
->param
== NULL
)
2801 if ((ret
->md5
= EVP_get_digestbyname("ssl3-md5")) == NULL
) {
2802 SSLerr(SSL_F_SSL_CTX_NEW
, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES
);
2805 if ((ret
->sha1
= EVP_get_digestbyname("ssl3-sha1")) == NULL
) {
2806 SSLerr(SSL_F_SSL_CTX_NEW
, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES
);
2810 if ((ret
->ca_names
= sk_X509_NAME_new_null()) == NULL
)
2813 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ret
, &ret
->ex_data
))
2816 /* No compression for DTLS */
2817 if (!(meth
->ssl3_enc
->enc_flags
& SSL_ENC_FLAG_DTLS
))
2818 ret
->comp_methods
= SSL_COMP_get_compression_methods();
2820 ret
->max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
2821 ret
->split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
2823 /* Setup RFC5077 ticket keys */
2824 if ((RAND_bytes(ret
->ext
.tick_key_name
,
2825 sizeof(ret
->ext
.tick_key_name
)) <= 0)
2826 || (RAND_bytes(ret
->ext
.tick_hmac_key
,
2827 sizeof(ret
->ext
.tick_hmac_key
)) <= 0)
2828 || (RAND_bytes(ret
->ext
.tick_aes_key
,
2829 sizeof(ret
->ext
.tick_aes_key
)) <= 0))
2830 ret
->options
|= SSL_OP_NO_TICKET
;
2832 #ifndef OPENSSL_NO_SRP
2833 if (!SSL_CTX_SRP_CTX_init(ret
))
2836 #ifndef OPENSSL_NO_ENGINE
2837 # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2838 # define eng_strx(x) #x
2839 # define eng_str(x) eng_strx(x)
2840 /* Use specific client engine automatically... ignore errors */
2843 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
2846 ENGINE_load_builtin_engines();
2847 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
2849 if (!eng
|| !SSL_CTX_set_client_cert_engine(ret
, eng
))
2855 * Default is to connect to non-RI servers. When RI is more widely
2856 * deployed might change this.
2858 ret
->options
|= SSL_OP_LEGACY_SERVER_CONNECT
;
2860 * Disable compression by default to prevent CRIME. Applications can
2861 * re-enable compression by configuring
2862 * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
2863 * or by using the SSL_CONF library.
2865 ret
->options
|= SSL_OP_NO_COMPRESSION
;
2867 ret
->ext
.status_type
= TLSEXT_STATUSTYPE_nothing
;
2870 * Default max early data is a fully loaded single record. Could be split
2871 * across multiple records in practice
2873 ret
->max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
2877 SSLerr(SSL_F_SSL_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
2883 int SSL_CTX_up_ref(SSL_CTX
*ctx
)
2887 if (CRYPTO_UP_REF(&ctx
->references
, &i
, ctx
->lock
) <= 0)
2890 REF_PRINT_COUNT("SSL_CTX", ctx
);
2891 REF_ASSERT_ISNT(i
< 2);
2892 return ((i
> 1) ? 1 : 0);
2895 void SSL_CTX_free(SSL_CTX
*a
)
2902 CRYPTO_DOWN_REF(&a
->references
, &i
, a
->lock
);
2903 REF_PRINT_COUNT("SSL_CTX", a
);
2906 REF_ASSERT_ISNT(i
< 0);
2908 X509_VERIFY_PARAM_free(a
->param
);
2909 dane_ctx_final(&a
->dane
);
2912 * Free internal session cache. However: the remove_cb() may reference
2913 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2914 * after the sessions were flushed.
2915 * As the ex_data handling routines might also touch the session cache,
2916 * the most secure solution seems to be: empty (flush) the cache, then
2917 * free ex_data, then finally free the cache.
2918 * (See ticket [openssl.org #212].)
2920 if (a
->sessions
!= NULL
)
2921 SSL_CTX_flush_sessions(a
, 0);
2923 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, a
, &a
->ex_data
);
2924 lh_SSL_SESSION_free(a
->sessions
);
2925 X509_STORE_free(a
->cert_store
);
2926 #ifndef OPENSSL_NO_CT
2927 CTLOG_STORE_free(a
->ctlog_store
);
2929 sk_SSL_CIPHER_free(a
->cipher_list
);
2930 sk_SSL_CIPHER_free(a
->cipher_list_by_id
);
2931 ssl_cert_free(a
->cert
);
2932 sk_X509_NAME_pop_free(a
->ca_names
, X509_NAME_free
);
2933 sk_X509_pop_free(a
->extra_certs
, X509_free
);
2934 a
->comp_methods
= NULL
;
2935 #ifndef OPENSSL_NO_SRTP
2936 sk_SRTP_PROTECTION_PROFILE_free(a
->srtp_profiles
);
2938 #ifndef OPENSSL_NO_SRP
2939 SSL_CTX_SRP_CTX_free(a
);
2941 #ifndef OPENSSL_NO_ENGINE
2942 ENGINE_finish(a
->client_cert_engine
);
2945 #ifndef OPENSSL_NO_EC
2946 OPENSSL_free(a
->ext
.ecpointformats
);
2947 OPENSSL_free(a
->ext
.supportedgroups
);
2949 OPENSSL_free(a
->ext
.alpn
);
2951 CRYPTO_THREAD_lock_free(a
->lock
);
2956 void SSL_CTX_set_default_passwd_cb(SSL_CTX
*ctx
, pem_password_cb
*cb
)
2958 ctx
->default_passwd_callback
= cb
;
2961 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX
*ctx
, void *u
)
2963 ctx
->default_passwd_callback_userdata
= u
;
2966 pem_password_cb
*SSL_CTX_get_default_passwd_cb(SSL_CTX
*ctx
)
2968 return ctx
->default_passwd_callback
;
2971 void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX
*ctx
)
2973 return ctx
->default_passwd_callback_userdata
;
2976 void SSL_set_default_passwd_cb(SSL
*s
, pem_password_cb
*cb
)
2978 s
->default_passwd_callback
= cb
;
2981 void SSL_set_default_passwd_cb_userdata(SSL
*s
, void *u
)
2983 s
->default_passwd_callback_userdata
= u
;
2986 pem_password_cb
*SSL_get_default_passwd_cb(SSL
*s
)
2988 return s
->default_passwd_callback
;
2991 void *SSL_get_default_passwd_cb_userdata(SSL
*s
)
2993 return s
->default_passwd_callback_userdata
;
2996 void SSL_CTX_set_cert_verify_callback(SSL_CTX
*ctx
,
2997 int (*cb
) (X509_STORE_CTX
*, void *),
3000 ctx
->app_verify_callback
= cb
;
3001 ctx
->app_verify_arg
= arg
;
3004 void SSL_CTX_set_verify(SSL_CTX
*ctx
, int mode
,
3005 int (*cb
) (int, X509_STORE_CTX
*))
3007 ctx
->verify_mode
= mode
;
3008 ctx
->default_verify_callback
= cb
;
3011 void SSL_CTX_set_verify_depth(SSL_CTX
*ctx
, int depth
)
3013 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
3016 void SSL_CTX_set_cert_cb(SSL_CTX
*c
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
3018 ssl_cert_set_cert_cb(c
->cert
, cb
, arg
);
3021 void SSL_set_cert_cb(SSL
*s
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
3023 ssl_cert_set_cert_cb(s
->cert
, cb
, arg
);
3026 void ssl_set_masks(SSL
*s
)
3029 uint32_t *pvalid
= s
->s3
->tmp
.valid_flags
;
3030 int rsa_enc
, rsa_sign
, dh_tmp
, dsa_sign
;
3031 unsigned long mask_k
, mask_a
;
3032 #ifndef OPENSSL_NO_EC
3033 int have_ecc_cert
, ecdsa_ok
;
3038 #ifndef OPENSSL_NO_DH
3039 dh_tmp
= (c
->dh_tmp
!= NULL
|| c
->dh_tmp_cb
!= NULL
|| c
->dh_tmp_auto
);
3044 rsa_enc
= pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_VALID
;
3045 rsa_sign
= pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_VALID
;
3046 dsa_sign
= pvalid
[SSL_PKEY_DSA_SIGN
] & CERT_PKEY_VALID
;
3047 #ifndef OPENSSL_NO_EC
3048 have_ecc_cert
= pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_VALID
;
3054 fprintf(stderr
, "dht=%d re=%d rs=%d ds=%d\n",
3055 dh_tmp
, rsa_enc
, rsa_sign
, dsa_sign
);
3058 #ifndef OPENSSL_NO_GOST
3059 if (ssl_has_cert(s
, SSL_PKEY_GOST12_512
)) {
3060 mask_k
|= SSL_kGOST
;
3061 mask_a
|= SSL_aGOST12
;
3063 if (ssl_has_cert(s
, SSL_PKEY_GOST12_256
)) {
3064 mask_k
|= SSL_kGOST
;
3065 mask_a
|= SSL_aGOST12
;
3067 if (ssl_has_cert(s
, SSL_PKEY_GOST01
)) {
3068 mask_k
|= SSL_kGOST
;
3069 mask_a
|= SSL_aGOST01
;
3079 if (rsa_enc
|| rsa_sign
) {
3087 mask_a
|= SSL_aNULL
;
3090 * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
3091 * depending on the key usage extension.
3093 #ifndef OPENSSL_NO_EC
3094 if (have_ecc_cert
) {
3096 ex_kusage
= X509_get_key_usage(c
->pkeys
[SSL_PKEY_ECC
].x509
);
3097 ecdsa_ok
= ex_kusage
& X509v3_KU_DIGITAL_SIGNATURE
;
3098 if (!(pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_SIGN
))
3101 mask_a
|= SSL_aECDSA
;
3103 /* Allow Ed25519 for TLS 1.2 if peer supports it */
3104 if (!(mask_a
& SSL_aECDSA
) && ssl_has_cert(s
, SSL_PKEY_ED25519
)
3105 && pvalid
[SSL_PKEY_ED25519
] & CERT_PKEY_EXPLICIT_SIGN
3106 && TLS1_get_version(s
) == TLS1_2_VERSION
)
3107 mask_a
|= SSL_aECDSA
;
3110 #ifndef OPENSSL_NO_EC
3111 mask_k
|= SSL_kECDHE
;
3114 #ifndef OPENSSL_NO_PSK
3117 if (mask_k
& SSL_kRSA
)
3118 mask_k
|= SSL_kRSAPSK
;
3119 if (mask_k
& SSL_kDHE
)
3120 mask_k
|= SSL_kDHEPSK
;
3121 if (mask_k
& SSL_kECDHE
)
3122 mask_k
|= SSL_kECDHEPSK
;
3125 s
->s3
->tmp
.mask_k
= mask_k
;
3126 s
->s3
->tmp
.mask_a
= mask_a
;
3129 #ifndef OPENSSL_NO_EC
3131 int ssl_check_srvr_ecc_cert_and_alg(X509
*x
, SSL
*s
)
3133 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aECDSA
) {
3134 /* key usage, if present, must allow signing */
3135 if (!(X509_get_key_usage(x
) & X509v3_KU_DIGITAL_SIGNATURE
)) {
3136 SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG
,
3137 SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
3141 return 1; /* all checks are ok */
3146 int ssl_get_server_cert_serverinfo(SSL
*s
, const unsigned char **serverinfo
,
3147 size_t *serverinfo_length
)
3149 CERT_PKEY
*cpk
= s
->s3
->tmp
.cert
;
3150 *serverinfo_length
= 0;
3152 if (cpk
== NULL
|| cpk
->serverinfo
== NULL
)
3155 *serverinfo
= cpk
->serverinfo
;
3156 *serverinfo_length
= cpk
->serverinfo_length
;
3160 void ssl_update_cache(SSL
*s
, int mode
)
3165 * If the session_id_length is 0, we are not supposed to cache it, and it
3166 * would be rather hard to do anyway :-)
3168 if (s
->session
->session_id_length
== 0)
3171 i
= s
->session_ctx
->session_cache_mode
;
3173 && (!s
->hit
|| SSL_IS_TLS13(s
))
3174 && ((i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) != 0
3175 || SSL_CTX_add_session(s
->session_ctx
, s
->session
))
3176 && s
->session_ctx
->new_session_cb
!= NULL
) {
3177 SSL_SESSION_up_ref(s
->session
);
3178 if (!s
->session_ctx
->new_session_cb(s
, s
->session
))
3179 SSL_SESSION_free(s
->session
);
3182 /* auto flush every 255 connections */
3183 if ((!(i
& SSL_SESS_CACHE_NO_AUTO_CLEAR
)) && ((i
& mode
) == mode
)) {
3184 if ((((mode
& SSL_SESS_CACHE_CLIENT
)
3185 ? s
->session_ctx
->stats
.sess_connect_good
3186 : s
->session_ctx
->stats
.sess_accept_good
) & 0xff) == 0xff) {
3187 SSL_CTX_flush_sessions(s
->session_ctx
, (unsigned long)time(NULL
));
3192 const SSL_METHOD
*SSL_CTX_get_ssl_method(SSL_CTX
*ctx
)
3197 const SSL_METHOD
*SSL_get_ssl_method(SSL
*s
)
3202 int SSL_set_ssl_method(SSL
*s
, const SSL_METHOD
*meth
)
3206 if (s
->method
!= meth
) {
3207 const SSL_METHOD
*sm
= s
->method
;
3208 int (*hf
) (SSL
*) = s
->handshake_func
;
3210 if (sm
->version
== meth
->version
)
3215 ret
= s
->method
->ssl_new(s
);
3218 if (hf
== sm
->ssl_connect
)
3219 s
->handshake_func
= meth
->ssl_connect
;
3220 else if (hf
== sm
->ssl_accept
)
3221 s
->handshake_func
= meth
->ssl_accept
;
3226 int SSL_get_error(const SSL
*s
, int i
)
3233 return (SSL_ERROR_NONE
);
3236 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
3237 * where we do encode the error
3239 if ((l
= ERR_peek_error()) != 0) {
3240 if (ERR_GET_LIB(l
) == ERR_LIB_SYS
)
3241 return (SSL_ERROR_SYSCALL
);
3243 return (SSL_ERROR_SSL
);
3246 if (SSL_want_read(s
)) {
3247 bio
= SSL_get_rbio(s
);
3248 if (BIO_should_read(bio
))
3249 return (SSL_ERROR_WANT_READ
);
3250 else if (BIO_should_write(bio
))
3252 * This one doesn't make too much sense ... We never try to write
3253 * to the rbio, and an application program where rbio and wbio
3254 * are separate couldn't even know what it should wait for.
3255 * However if we ever set s->rwstate incorrectly (so that we have
3256 * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
3257 * wbio *are* the same, this test works around that bug; so it
3258 * might be safer to keep it.
3260 return (SSL_ERROR_WANT_WRITE
);
3261 else if (BIO_should_io_special(bio
)) {
3262 reason
= BIO_get_retry_reason(bio
);
3263 if (reason
== BIO_RR_CONNECT
)
3264 return (SSL_ERROR_WANT_CONNECT
);
3265 else if (reason
== BIO_RR_ACCEPT
)
3266 return (SSL_ERROR_WANT_ACCEPT
);
3268 return (SSL_ERROR_SYSCALL
); /* unknown */
3272 if (SSL_want_write(s
)) {
3273 /* Access wbio directly - in order to use the buffered bio if present */
3275 if (BIO_should_write(bio
))
3276 return (SSL_ERROR_WANT_WRITE
);
3277 else if (BIO_should_read(bio
))
3279 * See above (SSL_want_read(s) with BIO_should_write(bio))
3281 return (SSL_ERROR_WANT_READ
);
3282 else if (BIO_should_io_special(bio
)) {
3283 reason
= BIO_get_retry_reason(bio
);
3284 if (reason
== BIO_RR_CONNECT
)
3285 return (SSL_ERROR_WANT_CONNECT
);
3286 else if (reason
== BIO_RR_ACCEPT
)
3287 return (SSL_ERROR_WANT_ACCEPT
);
3289 return (SSL_ERROR_SYSCALL
);
3292 if (SSL_want_x509_lookup(s
))
3293 return (SSL_ERROR_WANT_X509_LOOKUP
);
3294 if (SSL_want_async(s
))
3295 return SSL_ERROR_WANT_ASYNC
;
3296 if (SSL_want_async_job(s
))
3297 return SSL_ERROR_WANT_ASYNC_JOB
;
3298 if (SSL_want_early(s
))
3299 return SSL_ERROR_WANT_EARLY
;
3301 if ((s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) &&
3302 (s
->s3
->warn_alert
== SSL_AD_CLOSE_NOTIFY
))
3303 return (SSL_ERROR_ZERO_RETURN
);
3305 return (SSL_ERROR_SYSCALL
);
3308 static int ssl_do_handshake_intern(void *vargs
)
3310 struct ssl_async_args
*args
;
3313 args
= (struct ssl_async_args
*)vargs
;
3316 return s
->handshake_func(s
);
3319 int SSL_do_handshake(SSL
*s
)
3323 if (s
->handshake_func
== NULL
) {
3324 SSLerr(SSL_F_SSL_DO_HANDSHAKE
, SSL_R_CONNECTION_TYPE_NOT_SET
);
3328 ossl_statem_check_finish_init(s
, -1);
3330 s
->method
->ssl_renegotiate_check(s
, 0);
3332 if (SSL_is_server(s
)) {
3333 /* clear SNI settings at server-side */
3334 OPENSSL_free(s
->ext
.hostname
);
3335 s
->ext
.hostname
= NULL
;
3338 if (SSL_in_init(s
) || SSL_in_before(s
)) {
3339 if ((s
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
3340 struct ssl_async_args args
;
3344 ret
= ssl_start_async_job(s
, &args
, ssl_do_handshake_intern
);
3346 ret
= s
->handshake_func(s
);
3352 void SSL_set_accept_state(SSL
*s
)
3356 ossl_statem_clear(s
);
3357 s
->handshake_func
= s
->method
->ssl_accept
;
3361 void SSL_set_connect_state(SSL
*s
)
3365 ossl_statem_clear(s
);
3366 s
->handshake_func
= s
->method
->ssl_connect
;
3370 int ssl_undefined_function(SSL
*s
)
3372 SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
3376 int ssl_undefined_void_function(void)
3378 SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION
,
3379 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
3383 int ssl_undefined_const_function(const SSL
*s
)
3388 const SSL_METHOD
*ssl_bad_method(int ver
)
3390 SSLerr(SSL_F_SSL_BAD_METHOD
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
3394 const char *ssl_protocol_to_string(int version
)
3398 case TLS1_3_VERSION
:
3401 case TLS1_2_VERSION
:
3404 case TLS1_1_VERSION
:
3419 case DTLS1_2_VERSION
:
3427 const char *SSL_get_version(const SSL
*s
)
3429 return ssl_protocol_to_string(s
->version
);
3432 SSL
*SSL_dup(SSL
*s
)
3434 STACK_OF(X509_NAME
) *sk
;
3439 /* If we're not quiescent, just up_ref! */
3440 if (!SSL_in_init(s
) || !SSL_in_before(s
)) {
3441 CRYPTO_UP_REF(&s
->references
, &i
, s
->lock
);
3446 * Otherwise, copy configuration state, and session if set.
3448 if ((ret
= SSL_new(SSL_get_SSL_CTX(s
))) == NULL
)
3451 if (s
->session
!= NULL
) {
3453 * Arranges to share the same session via up_ref. This "copies"
3454 * session-id, SSL_METHOD, sid_ctx, and 'cert'
3456 if (!SSL_copy_session_id(ret
, s
))
3460 * No session has been established yet, so we have to expect that
3461 * s->cert or ret->cert will be changed later -- they should not both
3462 * point to the same object, and thus we can't use
3463 * SSL_copy_session_id.
3465 if (!SSL_set_ssl_method(ret
, s
->method
))
3468 if (s
->cert
!= NULL
) {
3469 ssl_cert_free(ret
->cert
);
3470 ret
->cert
= ssl_cert_dup(s
->cert
);
3471 if (ret
->cert
== NULL
)
3475 if (!SSL_set_session_id_context(ret
, s
->sid_ctx
,
3476 (int)s
->sid_ctx_length
))
3480 if (!ssl_dane_dup(ret
, s
))
3482 ret
->version
= s
->version
;
3483 ret
->options
= s
->options
;
3484 ret
->mode
= s
->mode
;
3485 SSL_set_max_cert_list(ret
, SSL_get_max_cert_list(s
));
3486 SSL_set_read_ahead(ret
, SSL_get_read_ahead(s
));
3487 ret
->msg_callback
= s
->msg_callback
;
3488 ret
->msg_callback_arg
= s
->msg_callback_arg
;
3489 SSL_set_verify(ret
, SSL_get_verify_mode(s
), SSL_get_verify_callback(s
));
3490 SSL_set_verify_depth(ret
, SSL_get_verify_depth(s
));
3491 ret
->generate_session_id
= s
->generate_session_id
;
3493 SSL_set_info_callback(ret
, SSL_get_info_callback(s
));
3495 /* copy app data, a little dangerous perhaps */
3496 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL
, &ret
->ex_data
, &s
->ex_data
))
3499 /* setup rbio, and wbio */
3500 if (s
->rbio
!= NULL
) {
3501 if (!BIO_dup_state(s
->rbio
, (char *)&ret
->rbio
))
3504 if (s
->wbio
!= NULL
) {
3505 if (s
->wbio
!= s
->rbio
) {
3506 if (!BIO_dup_state(s
->wbio
, (char *)&ret
->wbio
))
3509 BIO_up_ref(ret
->rbio
);
3510 ret
->wbio
= ret
->rbio
;
3514 ret
->server
= s
->server
;
3515 if (s
->handshake_func
) {
3517 SSL_set_accept_state(ret
);
3519 SSL_set_connect_state(ret
);
3521 ret
->shutdown
= s
->shutdown
;
3524 ret
->default_passwd_callback
= s
->default_passwd_callback
;
3525 ret
->default_passwd_callback_userdata
= s
->default_passwd_callback_userdata
;
3527 X509_VERIFY_PARAM_inherit(ret
->param
, s
->param
);
3529 /* dup the cipher_list and cipher_list_by_id stacks */
3530 if (s
->cipher_list
!= NULL
) {
3531 if ((ret
->cipher_list
= sk_SSL_CIPHER_dup(s
->cipher_list
)) == NULL
)
3534 if (s
->cipher_list_by_id
!= NULL
)
3535 if ((ret
->cipher_list_by_id
= sk_SSL_CIPHER_dup(s
->cipher_list_by_id
))
3539 /* Dup the client_CA list */
3540 if (s
->ca_names
!= NULL
) {
3541 if ((sk
= sk_X509_NAME_dup(s
->ca_names
)) == NULL
)
3544 for (i
= 0; i
< sk_X509_NAME_num(sk
); i
++) {
3545 xn
= sk_X509_NAME_value(sk
, i
);
3546 if (sk_X509_NAME_set(sk
, i
, X509_NAME_dup(xn
)) == NULL
) {
3559 void ssl_clear_cipher_ctx(SSL
*s
)
3561 if (s
->enc_read_ctx
!= NULL
) {
3562 EVP_CIPHER_CTX_free(s
->enc_read_ctx
);
3563 s
->enc_read_ctx
= NULL
;
3565 if (s
->enc_write_ctx
!= NULL
) {
3566 EVP_CIPHER_CTX_free(s
->enc_write_ctx
);
3567 s
->enc_write_ctx
= NULL
;
3569 #ifndef OPENSSL_NO_COMP
3570 COMP_CTX_free(s
->expand
);
3572 COMP_CTX_free(s
->compress
);
3577 X509
*SSL_get_certificate(const SSL
*s
)
3579 if (s
->cert
!= NULL
)
3580 return (s
->cert
->key
->x509
);
3585 EVP_PKEY
*SSL_get_privatekey(const SSL
*s
)
3587 if (s
->cert
!= NULL
)
3588 return (s
->cert
->key
->privatekey
);
3593 X509
*SSL_CTX_get0_certificate(const SSL_CTX
*ctx
)
3595 if (ctx
->cert
!= NULL
)
3596 return ctx
->cert
->key
->x509
;
3601 EVP_PKEY
*SSL_CTX_get0_privatekey(const SSL_CTX
*ctx
)
3603 if (ctx
->cert
!= NULL
)
3604 return ctx
->cert
->key
->privatekey
;
3609 const SSL_CIPHER
*SSL_get_current_cipher(const SSL
*s
)
3611 if ((s
->session
!= NULL
) && (s
->session
->cipher
!= NULL
))
3612 return (s
->session
->cipher
);
3616 const COMP_METHOD
*SSL_get_current_compression(SSL
*s
)
3618 #ifndef OPENSSL_NO_COMP
3619 return s
->compress
? COMP_CTX_get_method(s
->compress
) : NULL
;
3625 const COMP_METHOD
*SSL_get_current_expansion(SSL
*s
)
3627 #ifndef OPENSSL_NO_COMP
3628 return s
->expand
? COMP_CTX_get_method(s
->expand
) : NULL
;
3634 int ssl_init_wbio_buffer(SSL
*s
)
3638 if (s
->bbio
!= NULL
) {
3639 /* Already buffered. */
3643 bbio
= BIO_new(BIO_f_buffer());
3644 if (bbio
== NULL
|| !BIO_set_read_buffer_size(bbio
, 1)) {
3646 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER
, ERR_R_BUF_LIB
);
3650 s
->wbio
= BIO_push(bbio
, s
->wbio
);
3655 int ssl_free_wbio_buffer(SSL
*s
)
3657 /* callers ensure s is never null */
3658 if (s
->bbio
== NULL
)
3661 s
->wbio
= BIO_pop(s
->wbio
);
3662 if (!ossl_assert(s
->wbio
!= NULL
))
3670 void SSL_CTX_set_quiet_shutdown(SSL_CTX
*ctx
, int mode
)
3672 ctx
->quiet_shutdown
= mode
;
3675 int SSL_CTX_get_quiet_shutdown(const SSL_CTX
*ctx
)
3677 return (ctx
->quiet_shutdown
);
3680 void SSL_set_quiet_shutdown(SSL
*s
, int mode
)
3682 s
->quiet_shutdown
= mode
;
3685 int SSL_get_quiet_shutdown(const SSL
*s
)
3687 return (s
->quiet_shutdown
);
3690 void SSL_set_shutdown(SSL
*s
, int mode
)
3695 int SSL_get_shutdown(const SSL
*s
)
3700 int SSL_version(const SSL
*s
)
3705 int SSL_client_version(const SSL
*s
)
3707 return s
->client_version
;
3710 SSL_CTX
*SSL_get_SSL_CTX(const SSL
*ssl
)
3715 SSL_CTX
*SSL_set_SSL_CTX(SSL
*ssl
, SSL_CTX
*ctx
)
3718 if (ssl
->ctx
== ctx
)
3721 ctx
= ssl
->session_ctx
;
3722 new_cert
= ssl_cert_dup(ctx
->cert
);
3723 if (new_cert
== NULL
) {
3727 if (!custom_exts_copy_flags(&new_cert
->custext
, &ssl
->cert
->custext
)) {
3728 ssl_cert_free(new_cert
);
3732 ssl_cert_free(ssl
->cert
);
3733 ssl
->cert
= new_cert
;
3736 * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
3737 * so setter APIs must prevent invalid lengths from entering the system.
3739 if (!ossl_assert(ssl
->sid_ctx_length
<= sizeof(ssl
->sid_ctx
)))
3743 * If the session ID context matches that of the parent SSL_CTX,
3744 * inherit it from the new SSL_CTX as well. If however the context does
3745 * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
3746 * leave it unchanged.
3748 if ((ssl
->ctx
!= NULL
) &&
3749 (ssl
->sid_ctx_length
== ssl
->ctx
->sid_ctx_length
) &&
3750 (memcmp(ssl
->sid_ctx
, ssl
->ctx
->sid_ctx
, ssl
->sid_ctx_length
) == 0)) {
3751 ssl
->sid_ctx_length
= ctx
->sid_ctx_length
;
3752 memcpy(&ssl
->sid_ctx
, &ctx
->sid_ctx
, sizeof(ssl
->sid_ctx
));
3755 SSL_CTX_up_ref(ctx
);
3756 SSL_CTX_free(ssl
->ctx
); /* decrement reference count */
3762 int SSL_CTX_set_default_verify_paths(SSL_CTX
*ctx
)
3764 return (X509_STORE_set_default_paths(ctx
->cert_store
));
3767 int SSL_CTX_set_default_verify_dir(SSL_CTX
*ctx
)
3769 X509_LOOKUP
*lookup
;
3771 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_hash_dir());
3774 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
3776 /* Clear any errors if the default directory does not exist */
3782 int SSL_CTX_set_default_verify_file(SSL_CTX
*ctx
)
3784 X509_LOOKUP
*lookup
;
3786 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_file());
3790 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
3792 /* Clear any errors if the default file does not exist */
3798 int SSL_CTX_load_verify_locations(SSL_CTX
*ctx
, const char *CAfile
,
3801 return (X509_STORE_load_locations(ctx
->cert_store
, CAfile
, CApath
));
3804 void SSL_set_info_callback(SSL
*ssl
,
3805 void (*cb
) (const SSL
*ssl
, int type
, int val
))
3807 ssl
->info_callback
= cb
;
3811 * One compiler (Diab DCC) doesn't like argument names in returned function
3814 void (*SSL_get_info_callback(const SSL
*ssl
)) (const SSL
* /* ssl */ ,
3817 return ssl
->info_callback
;
3820 void SSL_set_verify_result(SSL
*ssl
, long arg
)
3822 ssl
->verify_result
= arg
;
3825 long SSL_get_verify_result(const SSL
*ssl
)
3827 return (ssl
->verify_result
);
3830 size_t SSL_get_client_random(const SSL
*ssl
, unsigned char *out
, size_t outlen
)
3833 return sizeof(ssl
->s3
->client_random
);
3834 if (outlen
> sizeof(ssl
->s3
->client_random
))
3835 outlen
= sizeof(ssl
->s3
->client_random
);
3836 memcpy(out
, ssl
->s3
->client_random
, outlen
);
3840 size_t SSL_get_server_random(const SSL
*ssl
, unsigned char *out
, size_t outlen
)
3843 return sizeof(ssl
->s3
->server_random
);
3844 if (outlen
> sizeof(ssl
->s3
->server_random
))
3845 outlen
= sizeof(ssl
->s3
->server_random
);
3846 memcpy(out
, ssl
->s3
->server_random
, outlen
);
3850 size_t SSL_SESSION_get_master_key(const SSL_SESSION
*session
,
3851 unsigned char *out
, size_t outlen
)
3854 return session
->master_key_length
;
3855 if (outlen
> session
->master_key_length
)
3856 outlen
= session
->master_key_length
;
3857 memcpy(out
, session
->master_key
, outlen
);
3861 int SSL_SESSION_set1_master_key(SSL_SESSION
*sess
, const unsigned char *in
,
3864 if (len
> sizeof(sess
->master_key
))
3867 memcpy(sess
->master_key
, in
, len
);
3868 sess
->master_key_length
= len
;
3873 int SSL_set_ex_data(SSL
*s
, int idx
, void *arg
)
3875 return (CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
));
3878 void *SSL_get_ex_data(const SSL
*s
, int idx
)
3880 return (CRYPTO_get_ex_data(&s
->ex_data
, idx
));
3883 int SSL_CTX_set_ex_data(SSL_CTX
*s
, int idx
, void *arg
)
3885 return (CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
));
3888 void *SSL_CTX_get_ex_data(const SSL_CTX
*s
, int idx
)
3890 return (CRYPTO_get_ex_data(&s
->ex_data
, idx
));
3893 X509_STORE
*SSL_CTX_get_cert_store(const SSL_CTX
*ctx
)
3895 return (ctx
->cert_store
);
3898 void SSL_CTX_set_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
3900 X509_STORE_free(ctx
->cert_store
);
3901 ctx
->cert_store
= store
;
3904 void SSL_CTX_set1_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
3907 X509_STORE_up_ref(store
);
3908 SSL_CTX_set_cert_store(ctx
, store
);
3911 int SSL_want(const SSL
*s
)
3913 return (s
->rwstate
);
3917 * \brief Set the callback for generating temporary DH keys.
3918 * \param ctx the SSL context.
3919 * \param dh the callback
3922 #ifndef OPENSSL_NO_DH
3923 void SSL_CTX_set_tmp_dh_callback(SSL_CTX
*ctx
,
3924 DH
*(*dh
) (SSL
*ssl
, int is_export
,
3927 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_TMP_DH_CB
, (void (*)(void))dh
);
3930 void SSL_set_tmp_dh_callback(SSL
*ssl
, DH
*(*dh
) (SSL
*ssl
, int is_export
,
3933 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_TMP_DH_CB
, (void (*)(void))dh
);
3937 #ifndef OPENSSL_NO_PSK
3938 int SSL_CTX_use_psk_identity_hint(SSL_CTX
*ctx
, const char *identity_hint
)
3940 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
3941 SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT
, SSL_R_DATA_LENGTH_TOO_LONG
);
3944 OPENSSL_free(ctx
->cert
->psk_identity_hint
);
3945 if (identity_hint
!= NULL
) {
3946 ctx
->cert
->psk_identity_hint
= OPENSSL_strdup(identity_hint
);
3947 if (ctx
->cert
->psk_identity_hint
== NULL
)
3950 ctx
->cert
->psk_identity_hint
= NULL
;
3954 int SSL_use_psk_identity_hint(SSL
*s
, const char *identity_hint
)
3959 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
3960 SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT
, SSL_R_DATA_LENGTH_TOO_LONG
);
3963 OPENSSL_free(s
->cert
->psk_identity_hint
);
3964 if (identity_hint
!= NULL
) {
3965 s
->cert
->psk_identity_hint
= OPENSSL_strdup(identity_hint
);
3966 if (s
->cert
->psk_identity_hint
== NULL
)
3969 s
->cert
->psk_identity_hint
= NULL
;
3973 const char *SSL_get_psk_identity_hint(const SSL
*s
)
3975 if (s
== NULL
|| s
->session
== NULL
)
3977 return (s
->session
->psk_identity_hint
);
3980 const char *SSL_get_psk_identity(const SSL
*s
)
3982 if (s
== NULL
|| s
->session
== NULL
)
3984 return (s
->session
->psk_identity
);
3987 void SSL_set_psk_client_callback(SSL
*s
, SSL_psk_client_cb_func cb
)
3989 s
->psk_client_callback
= cb
;
3992 void SSL_CTX_set_psk_client_callback(SSL_CTX
*ctx
, SSL_psk_client_cb_func cb
)
3994 ctx
->psk_client_callback
= cb
;
3997 void SSL_set_psk_server_callback(SSL
*s
, SSL_psk_server_cb_func cb
)
3999 s
->psk_server_callback
= cb
;
4002 void SSL_CTX_set_psk_server_callback(SSL_CTX
*ctx
, SSL_psk_server_cb_func cb
)
4004 ctx
->psk_server_callback
= cb
;
4008 void SSL_set_psk_find_session_callback(SSL
*s
, SSL_psk_find_session_cb_func cb
)
4010 s
->psk_find_session_cb
= cb
;
4013 void SSL_CTX_set_psk_find_session_callback(SSL_CTX
*ctx
,
4014 SSL_psk_find_session_cb_func cb
)
4016 ctx
->psk_find_session_cb
= cb
;
4019 void SSL_set_psk_use_session_callback(SSL
*s
, SSL_psk_use_session_cb_func cb
)
4021 s
->psk_use_session_cb
= cb
;
4024 void SSL_CTX_set_psk_use_session_callback(SSL_CTX
*ctx
,
4025 SSL_psk_use_session_cb_func cb
)
4027 ctx
->psk_use_session_cb
= cb
;
4030 void SSL_CTX_set_msg_callback(SSL_CTX
*ctx
,
4031 void (*cb
) (int write_p
, int version
,
4032 int content_type
, const void *buf
,
4033 size_t len
, SSL
*ssl
, void *arg
))
4035 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
4038 void SSL_set_msg_callback(SSL
*ssl
,
4039 void (*cb
) (int write_p
, int version
,
4040 int content_type
, const void *buf
,
4041 size_t len
, SSL
*ssl
, void *arg
))
4043 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
4046 void SSL_CTX_set_not_resumable_session_callback(SSL_CTX
*ctx
,
4047 int (*cb
) (SSL
*ssl
,
4051 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB
,
4052 (void (*)(void))cb
);
4055 void SSL_set_not_resumable_session_callback(SSL
*ssl
,
4056 int (*cb
) (SSL
*ssl
,
4057 int is_forward_secure
))
4059 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB
,
4060 (void (*)(void))cb
);
4063 void SSL_CTX_set_record_padding_callback(SSL_CTX
*ctx
,
4064 size_t (*cb
) (SSL
*ssl
, int type
,
4065 size_t len
, void *arg
))
4067 ctx
->record_padding_cb
= cb
;
4070 void SSL_CTX_set_record_padding_callback_arg(SSL_CTX
*ctx
, void *arg
)
4072 ctx
->record_padding_arg
= arg
;
4075 void *SSL_CTX_get_record_padding_callback_arg(SSL_CTX
*ctx
)
4077 return ctx
->record_padding_arg
;
4080 int SSL_CTX_set_block_padding(SSL_CTX
*ctx
, size_t block_size
)
4082 /* block size of 0 or 1 is basically no padding */
4083 if (block_size
== 1)
4084 ctx
->block_padding
= 0;
4085 else if (block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
)
4086 ctx
->block_padding
= block_size
;
4092 void SSL_set_record_padding_callback(SSL
*ssl
,
4093 size_t (*cb
) (SSL
*ssl
, int type
,
4094 size_t len
, void *arg
))
4096 ssl
->record_padding_cb
= cb
;
4099 void SSL_set_record_padding_callback_arg(SSL
*ssl
, void *arg
)
4101 ssl
->record_padding_arg
= arg
;
4104 void *SSL_get_record_padding_callback_arg(SSL
*ssl
)
4106 return ssl
->record_padding_arg
;
4109 int SSL_set_block_padding(SSL
*ssl
, size_t block_size
)
4111 /* block size of 0 or 1 is basically no padding */
4112 if (block_size
== 1)
4113 ssl
->block_padding
= 0;
4114 else if (block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
)
4115 ssl
->block_padding
= block_size
;
4122 * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
4123 * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
4124 * If EVP_MD pointer is passed, initializes ctx with this |md|.
4125 * Returns the newly allocated ctx;
4128 EVP_MD_CTX
*ssl_replace_hash(EVP_MD_CTX
**hash
, const EVP_MD
*md
)
4130 ssl_clear_hash_ctx(hash
);
4131 *hash
= EVP_MD_CTX_new();
4132 if (*hash
== NULL
|| (md
&& EVP_DigestInit_ex(*hash
, md
, NULL
) <= 0)) {
4133 EVP_MD_CTX_free(*hash
);
4140 void ssl_clear_hash_ctx(EVP_MD_CTX
**hash
)
4143 EVP_MD_CTX_free(*hash
);
4147 /* Retrieve handshake hashes */
4148 int ssl_handshake_hash(SSL
*s
, unsigned char *out
, size_t outlen
,
4151 EVP_MD_CTX
*ctx
= NULL
;
4152 EVP_MD_CTX
*hdgst
= s
->s3
->handshake_dgst
;
4153 int hashleni
= EVP_MD_CTX_size(hdgst
);
4156 if (hashleni
< 0 || (size_t)hashleni
> outlen
)
4159 ctx
= EVP_MD_CTX_new();
4163 if (!EVP_MD_CTX_copy_ex(ctx
, hdgst
)
4164 || EVP_DigestFinal_ex(ctx
, out
, NULL
) <= 0)
4167 *hashlen
= hashleni
;
4171 EVP_MD_CTX_free(ctx
);
4175 int SSL_session_reused(SSL
*s
)
4180 int SSL_is_server(const SSL
*s
)
4185 #if OPENSSL_API_COMPAT < 0x10100000L
4186 void SSL_set_debug(SSL
*s
, int debug
)
4188 /* Old function was do-nothing anyway... */
4194 void SSL_set_security_level(SSL
*s
, int level
)
4196 s
->cert
->sec_level
= level
;
4199 int SSL_get_security_level(const SSL
*s
)
4201 return s
->cert
->sec_level
;
4204 void SSL_set_security_callback(SSL
*s
,
4205 int (*cb
) (const SSL
*s
, const SSL_CTX
*ctx
,
4206 int op
, int bits
, int nid
,
4207 void *other
, void *ex
))
4209 s
->cert
->sec_cb
= cb
;
4212 int (*SSL_get_security_callback(const SSL
*s
)) (const SSL
*s
,
4213 const SSL_CTX
*ctx
, int op
,
4214 int bits
, int nid
, void *other
,
4216 return s
->cert
->sec_cb
;
4219 void SSL_set0_security_ex_data(SSL
*s
, void *ex
)
4221 s
->cert
->sec_ex
= ex
;
4224 void *SSL_get0_security_ex_data(const SSL
*s
)
4226 return s
->cert
->sec_ex
;
4229 void SSL_CTX_set_security_level(SSL_CTX
*ctx
, int level
)
4231 ctx
->cert
->sec_level
= level
;
4234 int SSL_CTX_get_security_level(const SSL_CTX
*ctx
)
4236 return ctx
->cert
->sec_level
;
4239 void SSL_CTX_set_security_callback(SSL_CTX
*ctx
,
4240 int (*cb
) (const SSL
*s
, const SSL_CTX
*ctx
,
4241 int op
, int bits
, int nid
,
4242 void *other
, void *ex
))
4244 ctx
->cert
->sec_cb
= cb
;
4247 int (*SSL_CTX_get_security_callback(const SSL_CTX
*ctx
)) (const SSL
*s
,
4253 return ctx
->cert
->sec_cb
;
4256 void SSL_CTX_set0_security_ex_data(SSL_CTX
*ctx
, void *ex
)
4258 ctx
->cert
->sec_ex
= ex
;
4261 void *SSL_CTX_get0_security_ex_data(const SSL_CTX
*ctx
)
4263 return ctx
->cert
->sec_ex
;
4267 * Get/Set/Clear options in SSL_CTX or SSL, formerly macros, now functions that
4268 * can return unsigned long, instead of the generic long return value from the
4269 * control interface.
4271 unsigned long SSL_CTX_get_options(const SSL_CTX
*ctx
)
4273 return ctx
->options
;
4276 unsigned long SSL_get_options(const SSL
*s
)
4281 unsigned long SSL_CTX_set_options(SSL_CTX
*ctx
, unsigned long op
)
4283 return ctx
->options
|= op
;
4286 unsigned long SSL_set_options(SSL
*s
, unsigned long op
)
4288 return s
->options
|= op
;
4291 unsigned long SSL_CTX_clear_options(SSL_CTX
*ctx
, unsigned long op
)
4293 return ctx
->options
&= ~op
;
4296 unsigned long SSL_clear_options(SSL
*s
, unsigned long op
)
4298 return s
->options
&= ~op
;
4301 STACK_OF(X509
) *SSL_get0_verified_chain(const SSL
*s
)
4303 return s
->verified_chain
;
4306 IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER
, SSL_CIPHER
, ssl_cipher_id
);
4308 #ifndef OPENSSL_NO_CT
4311 * Moves SCTs from the |src| stack to the |dst| stack.
4312 * The source of each SCT will be set to |origin|.
4313 * If |dst| points to a NULL pointer, a new stack will be created and owned by
4315 * Returns the number of SCTs moved, or a negative integer if an error occurs.
4317 static int ct_move_scts(STACK_OF(SCT
) **dst
, STACK_OF(SCT
) *src
,
4318 sct_source_t origin
)
4324 *dst
= sk_SCT_new_null();
4326 SSLerr(SSL_F_CT_MOVE_SCTS
, ERR_R_MALLOC_FAILURE
);
4331 while ((sct
= sk_SCT_pop(src
)) != NULL
) {
4332 if (SCT_set_source(sct
, origin
) != 1)
4335 if (sk_SCT_push(*dst
, sct
) <= 0)
4343 sk_SCT_push(src
, sct
); /* Put the SCT back */
4348 * Look for data collected during ServerHello and parse if found.
4349 * Returns the number of SCTs extracted.
4351 static int ct_extract_tls_extension_scts(SSL
*s
)
4353 int scts_extracted
= 0;
4355 if (s
->ext
.scts
!= NULL
) {
4356 const unsigned char *p
= s
->ext
.scts
;
4357 STACK_OF(SCT
) *scts
= o2i_SCT_LIST(NULL
, &p
, s
->ext
.scts_len
);
4359 scts_extracted
= ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_TLS_EXTENSION
);
4361 SCT_LIST_free(scts
);
4364 return scts_extracted
;
4368 * Checks for an OCSP response and then attempts to extract any SCTs found if it
4369 * contains an SCT X509 extension. They will be stored in |s->scts|.
4371 * - The number of SCTs extracted, assuming an OCSP response exists.
4372 * - 0 if no OCSP response exists or it contains no SCTs.
4373 * - A negative integer if an error occurs.
4375 static int ct_extract_ocsp_response_scts(SSL
*s
)
4377 # ifndef OPENSSL_NO_OCSP
4378 int scts_extracted
= 0;
4379 const unsigned char *p
;
4380 OCSP_BASICRESP
*br
= NULL
;
4381 OCSP_RESPONSE
*rsp
= NULL
;
4382 STACK_OF(SCT
) *scts
= NULL
;
4385 if (s
->ext
.ocsp
.resp
== NULL
|| s
->ext
.ocsp
.resp_len
== 0)
4388 p
= s
->ext
.ocsp
.resp
;
4389 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, (int)s
->ext
.ocsp
.resp_len
);
4393 br
= OCSP_response_get1_basic(rsp
);
4397 for (i
= 0; i
< OCSP_resp_count(br
); ++i
) {
4398 OCSP_SINGLERESP
*single
= OCSP_resp_get0(br
, i
);
4404 OCSP_SINGLERESP_get1_ext_d2i(single
, NID_ct_cert_scts
, NULL
, NULL
);
4406 ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_OCSP_STAPLED_RESPONSE
);
4407 if (scts_extracted
< 0)
4411 SCT_LIST_free(scts
);
4412 OCSP_BASICRESP_free(br
);
4413 OCSP_RESPONSE_free(rsp
);
4414 return scts_extracted
;
4416 /* Behave as if no OCSP response exists */
4422 * Attempts to extract SCTs from the peer certificate.
4423 * Return the number of SCTs extracted, or a negative integer if an error
4426 static int ct_extract_x509v3_extension_scts(SSL
*s
)
4428 int scts_extracted
= 0;
4429 X509
*cert
= s
->session
!= NULL
? s
->session
->peer
: NULL
;
4432 STACK_OF(SCT
) *scts
=
4433 X509_get_ext_d2i(cert
, NID_ct_precert_scts
, NULL
, NULL
);
4436 ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_X509V3_EXTENSION
);
4438 SCT_LIST_free(scts
);
4441 return scts_extracted
;
4445 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
4446 * response (if it exists) and X509v3 extensions in the certificate.
4447 * Returns NULL if an error occurs.
4449 const STACK_OF(SCT
) *SSL_get0_peer_scts(SSL
*s
)
4451 if (!s
->scts_parsed
) {
4452 if (ct_extract_tls_extension_scts(s
) < 0 ||
4453 ct_extract_ocsp_response_scts(s
) < 0 ||
4454 ct_extract_x509v3_extension_scts(s
) < 0)
4464 static int ct_permissive(const CT_POLICY_EVAL_CTX
* ctx
,
4465 const STACK_OF(SCT
) *scts
, void *unused_arg
)
4470 static int ct_strict(const CT_POLICY_EVAL_CTX
* ctx
,
4471 const STACK_OF(SCT
) *scts
, void *unused_arg
)
4473 int count
= scts
!= NULL
? sk_SCT_num(scts
) : 0;
4476 for (i
= 0; i
< count
; ++i
) {
4477 SCT
*sct
= sk_SCT_value(scts
, i
);
4478 int status
= SCT_get_validation_status(sct
);
4480 if (status
== SCT_VALIDATION_STATUS_VALID
)
4483 SSLerr(SSL_F_CT_STRICT
, SSL_R_NO_VALID_SCTS
);
4487 int SSL_set_ct_validation_callback(SSL
*s
, ssl_ct_validation_cb callback
,
4491 * Since code exists that uses the custom extension handler for CT, look
4492 * for this and throw an error if they have already registered to use CT.
4494 if (callback
!= NULL
&& SSL_CTX_has_client_custom_ext(s
->ctx
,
4495 TLSEXT_TYPE_signed_certificate_timestamp
))
4497 SSLerr(SSL_F_SSL_SET_CT_VALIDATION_CALLBACK
,
4498 SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED
);
4502 if (callback
!= NULL
) {
4504 * If we are validating CT, then we MUST accept SCTs served via OCSP
4506 if (!SSL_set_tlsext_status_type(s
, TLSEXT_STATUSTYPE_ocsp
))
4510 s
->ct_validation_callback
= callback
;
4511 s
->ct_validation_callback_arg
= arg
;
4516 int SSL_CTX_set_ct_validation_callback(SSL_CTX
*ctx
,
4517 ssl_ct_validation_cb callback
, void *arg
)
4520 * Since code exists that uses the custom extension handler for CT, look for
4521 * this and throw an error if they have already registered to use CT.
4523 if (callback
!= NULL
&& SSL_CTX_has_client_custom_ext(ctx
,
4524 TLSEXT_TYPE_signed_certificate_timestamp
))
4526 SSLerr(SSL_F_SSL_CTX_SET_CT_VALIDATION_CALLBACK
,
4527 SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED
);
4531 ctx
->ct_validation_callback
= callback
;
4532 ctx
->ct_validation_callback_arg
= arg
;
4536 int SSL_ct_is_enabled(const SSL
*s
)
4538 return s
->ct_validation_callback
!= NULL
;
4541 int SSL_CTX_ct_is_enabled(const SSL_CTX
*ctx
)
4543 return ctx
->ct_validation_callback
!= NULL
;
4546 int ssl_validate_ct(SSL
*s
)
4549 X509
*cert
= s
->session
!= NULL
? s
->session
->peer
: NULL
;
4551 SSL_DANE
*dane
= &s
->dane
;
4552 CT_POLICY_EVAL_CTX
*ctx
= NULL
;
4553 const STACK_OF(SCT
) *scts
;
4556 * If no callback is set, the peer is anonymous, or its chain is invalid,
4557 * skip SCT validation - just return success. Applications that continue
4558 * handshakes without certificates, with unverified chains, or pinned leaf
4559 * certificates are outside the scope of the WebPKI and CT.
4561 * The above exclusions notwithstanding the vast majority of peers will
4562 * have rather ordinary certificate chains validated by typical
4563 * applications that perform certificate verification and therefore will
4564 * process SCTs when enabled.
4566 if (s
->ct_validation_callback
== NULL
|| cert
== NULL
||
4567 s
->verify_result
!= X509_V_OK
||
4568 s
->verified_chain
== NULL
|| sk_X509_num(s
->verified_chain
) <= 1)
4572 * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
4573 * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
4575 if (DANETLS_ENABLED(dane
) && dane
->mtlsa
!= NULL
) {
4576 switch (dane
->mtlsa
->usage
) {
4577 case DANETLS_USAGE_DANE_TA
:
4578 case DANETLS_USAGE_DANE_EE
:
4583 ctx
= CT_POLICY_EVAL_CTX_new();
4585 SSLerr(SSL_F_SSL_VALIDATE_CT
, ERR_R_MALLOC_FAILURE
);
4589 issuer
= sk_X509_value(s
->verified_chain
, 1);
4590 CT_POLICY_EVAL_CTX_set1_cert(ctx
, cert
);
4591 CT_POLICY_EVAL_CTX_set1_issuer(ctx
, issuer
);
4592 CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx
, s
->ctx
->ctlog_store
);
4593 CT_POLICY_EVAL_CTX_set_time(
4594 ctx
, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s
)) * 1000);
4596 scts
= SSL_get0_peer_scts(s
);
4599 * This function returns success (> 0) only when all the SCTs are valid, 0
4600 * when some are invalid, and < 0 on various internal errors (out of
4601 * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
4602 * reason to abort the handshake, that decision is up to the callback.
4603 * Therefore, we error out only in the unexpected case that the return
4604 * value is negative.
4606 * XXX: One might well argue that the return value of this function is an
4607 * unfortunate design choice. Its job is only to determine the validation
4608 * status of each of the provided SCTs. So long as it correctly separates
4609 * the wheat from the chaff it should return success. Failure in this case
4610 * ought to correspond to an inability to carry out its duties.
4612 if (SCT_LIST_validate(scts
, ctx
) < 0) {
4613 SSLerr(SSL_F_SSL_VALIDATE_CT
, SSL_R_SCT_VERIFICATION_FAILED
);
4617 ret
= s
->ct_validation_callback(ctx
, scts
, s
->ct_validation_callback_arg
);
4619 ret
= 0; /* This function returns 0 on failure */
4622 CT_POLICY_EVAL_CTX_free(ctx
);
4624 * With SSL_VERIFY_NONE the session may be cached and re-used despite a
4625 * failure return code here. Also the application may wish the complete
4626 * the handshake, and then disconnect cleanly at a higher layer, after
4627 * checking the verification status of the completed connection.
4629 * We therefore force a certificate verification failure which will be
4630 * visible via SSL_get_verify_result() and cached as part of any resumed
4633 * Note: the permissive callback is for information gathering only, always
4634 * returns success, and does not affect verification status. Only the
4635 * strict callback or a custom application-specified callback can trigger
4636 * connection failure or record a verification error.
4639 s
->verify_result
= X509_V_ERR_NO_VALID_SCTS
;
4643 int SSL_CTX_enable_ct(SSL_CTX
*ctx
, int validation_mode
)
4645 switch (validation_mode
) {
4647 SSLerr(SSL_F_SSL_CTX_ENABLE_CT
, SSL_R_INVALID_CT_VALIDATION_TYPE
);
4649 case SSL_CT_VALIDATION_PERMISSIVE
:
4650 return SSL_CTX_set_ct_validation_callback(ctx
, ct_permissive
, NULL
);
4651 case SSL_CT_VALIDATION_STRICT
:
4652 return SSL_CTX_set_ct_validation_callback(ctx
, ct_strict
, NULL
);
4656 int SSL_enable_ct(SSL
*s
, int validation_mode
)
4658 switch (validation_mode
) {
4660 SSLerr(SSL_F_SSL_ENABLE_CT
, SSL_R_INVALID_CT_VALIDATION_TYPE
);
4662 case SSL_CT_VALIDATION_PERMISSIVE
:
4663 return SSL_set_ct_validation_callback(s
, ct_permissive
, NULL
);
4664 case SSL_CT_VALIDATION_STRICT
:
4665 return SSL_set_ct_validation_callback(s
, ct_strict
, NULL
);
4669 int SSL_CTX_set_default_ctlog_list_file(SSL_CTX
*ctx
)
4671 return CTLOG_STORE_load_default_file(ctx
->ctlog_store
);
4674 int SSL_CTX_set_ctlog_list_file(SSL_CTX
*ctx
, const char *path
)
4676 return CTLOG_STORE_load_file(ctx
->ctlog_store
, path
);
4679 void SSL_CTX_set0_ctlog_store(SSL_CTX
*ctx
, CTLOG_STORE
* logs
)
4681 CTLOG_STORE_free(ctx
->ctlog_store
);
4682 ctx
->ctlog_store
= logs
;
4685 const CTLOG_STORE
*SSL_CTX_get0_ctlog_store(const SSL_CTX
*ctx
)
4687 return ctx
->ctlog_store
;
4690 #endif /* OPENSSL_NO_CT */
4692 void SSL_CTX_set_early_cb(SSL_CTX
*c
, SSL_early_cb_fn cb
, void *arg
)
4695 c
->early_cb_arg
= arg
;
4698 int SSL_early_isv2(SSL
*s
)
4700 if (s
->clienthello
== NULL
)
4702 return s
->clienthello
->isv2
;
4705 unsigned int SSL_early_get0_legacy_version(SSL
*s
)
4707 if (s
->clienthello
== NULL
)
4709 return s
->clienthello
->legacy_version
;
4712 size_t SSL_early_get0_random(SSL
*s
, const unsigned char **out
)
4714 if (s
->clienthello
== NULL
)
4717 *out
= s
->clienthello
->random
;
4718 return SSL3_RANDOM_SIZE
;
4721 size_t SSL_early_get0_session_id(SSL
*s
, const unsigned char **out
)
4723 if (s
->clienthello
== NULL
)
4726 *out
= s
->clienthello
->session_id
;
4727 return s
->clienthello
->session_id_len
;
4730 size_t SSL_early_get0_ciphers(SSL
*s
, const unsigned char **out
)
4732 if (s
->clienthello
== NULL
)
4735 *out
= PACKET_data(&s
->clienthello
->ciphersuites
);
4736 return PACKET_remaining(&s
->clienthello
->ciphersuites
);
4739 size_t SSL_early_get0_compression_methods(SSL
*s
, const unsigned char **out
)
4741 if (s
->clienthello
== NULL
)
4744 *out
= s
->clienthello
->compressions
;
4745 return s
->clienthello
->compressions_len
;
4748 int SSL_early_get1_extensions_present(SSL
*s
, int **out
, size_t *outlen
)
4754 if (s
->clienthello
== NULL
|| out
== NULL
|| outlen
== NULL
)
4756 for (i
= 0; i
< s
->clienthello
->pre_proc_exts_len
; i
++) {
4757 ext
= s
->clienthello
->pre_proc_exts
+ i
;
4761 present
= OPENSSL_malloc(sizeof(*present
) * num
);
4762 if (present
== NULL
)
4764 for (i
= 0; i
< s
->clienthello
->pre_proc_exts_len
; i
++) {
4765 ext
= s
->clienthello
->pre_proc_exts
+ i
;
4767 if (ext
->received_order
>= num
)
4769 present
[ext
->received_order
] = ext
->type
;
4776 OPENSSL_free(present
);
4780 int SSL_early_get0_ext(SSL
*s
, unsigned int type
, const unsigned char **out
,
4786 if (s
->clienthello
== NULL
)
4788 for (i
= 0; i
< s
->clienthello
->pre_proc_exts_len
; ++i
) {
4789 r
= s
->clienthello
->pre_proc_exts
+ i
;
4790 if (r
->present
&& r
->type
== type
) {
4792 *out
= PACKET_data(&r
->data
);
4794 *outlen
= PACKET_remaining(&r
->data
);
4801 int SSL_free_buffers(SSL
*ssl
)
4803 RECORD_LAYER
*rl
= &ssl
->rlayer
;
4805 if (RECORD_LAYER_read_pending(rl
) || RECORD_LAYER_write_pending(rl
))
4808 RECORD_LAYER_release(rl
);
4812 int SSL_alloc_buffers(SSL
*ssl
)
4814 return ssl3_setup_buffers(ssl
);
4817 void SSL_CTX_set_keylog_callback(SSL_CTX
*ctx
, SSL_CTX_keylog_cb_func cb
)
4819 ctx
->keylog_callback
= cb
;
4822 SSL_CTX_keylog_cb_func
SSL_CTX_get_keylog_callback(const SSL_CTX
*ctx
)
4824 return ctx
->keylog_callback
;
4827 static int nss_keylog_int(const char *prefix
,
4829 const uint8_t *parameter_1
,
4830 size_t parameter_1_len
,
4831 const uint8_t *parameter_2
,
4832 size_t parameter_2_len
)
4835 char *cursor
= NULL
;
4840 if (ssl
->ctx
->keylog_callback
== NULL
) return 1;
4843 * Our output buffer will contain the following strings, rendered with
4844 * space characters in between, terminated by a NULL character: first the
4845 * prefix, then the first parameter, then the second parameter. The
4846 * meaning of each parameter depends on the specific key material being
4847 * logged. Note that the first and second parameters are encoded in
4848 * hexadecimal, so we need a buffer that is twice their lengths.
4850 prefix_len
= strlen(prefix
);
4851 out_len
= prefix_len
+ (2*parameter_1_len
) + (2*parameter_2_len
) + 3;
4852 if ((out
= cursor
= OPENSSL_malloc(out_len
)) == NULL
) {
4853 SSLerr(SSL_F_NSS_KEYLOG_INT
, ERR_R_MALLOC_FAILURE
);
4857 strcpy(cursor
, prefix
);
4858 cursor
+= prefix_len
;
4861 for (i
= 0; i
< parameter_1_len
; i
++) {
4862 sprintf(cursor
, "%02x", parameter_1
[i
]);
4867 for (i
= 0; i
< parameter_2_len
; i
++) {
4868 sprintf(cursor
, "%02x", parameter_2
[i
]);
4873 ssl
->ctx
->keylog_callback(ssl
, (const char *)out
);
4879 int ssl_log_rsa_client_key_exchange(SSL
*ssl
,
4880 const uint8_t *encrypted_premaster
,
4881 size_t encrypted_premaster_len
,
4882 const uint8_t *premaster
,
4883 size_t premaster_len
)
4885 if (encrypted_premaster_len
< 8) {
4886 SSLerr(SSL_F_SSL_LOG_RSA_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
4890 /* We only want the first 8 bytes of the encrypted premaster as a tag. */
4891 return nss_keylog_int("RSA",
4893 encrypted_premaster
,
4899 int ssl_log_secret(SSL
*ssl
,
4901 const uint8_t *secret
,
4904 return nss_keylog_int(label
,
4906 ssl
->s3
->client_random
,
4912 #define SSLV2_CIPHER_LEN 3
4914 int ssl_cache_cipherlist(SSL
*s
, PACKET
*cipher_suites
, int sslv2format
,
4919 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
4921 if (PACKET_remaining(cipher_suites
) == 0) {
4922 SSLerr(SSL_F_SSL_CACHE_CIPHERLIST
, SSL_R_NO_CIPHERS_SPECIFIED
);
4923 *al
= SSL_AD_ILLEGAL_PARAMETER
;
4927 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
4928 SSLerr(SSL_F_SSL_CACHE_CIPHERLIST
,
4929 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
4930 *al
= SSL_AD_DECODE_ERROR
;
4934 OPENSSL_free(s
->s3
->tmp
.ciphers_raw
);
4935 s
->s3
->tmp
.ciphers_raw
= NULL
;
4936 s
->s3
->tmp
.ciphers_rawlen
= 0;
4939 size_t numciphers
= PACKET_remaining(cipher_suites
) / n
;
4940 PACKET sslv2ciphers
= *cipher_suites
;
4941 unsigned int leadbyte
;
4945 * We store the raw ciphers list in SSLv3+ format so we need to do some
4946 * preprocessing to convert the list first. If there are any SSLv2 only
4947 * ciphersuites with a non-zero leading byte then we are going to
4948 * slightly over allocate because we won't store those. But that isn't a
4951 raw
= OPENSSL_malloc(numciphers
* TLS_CIPHER_LEN
);
4952 s
->s3
->tmp
.ciphers_raw
= raw
;
4954 *al
= SSL_AD_INTERNAL_ERROR
;
4957 for (s
->s3
->tmp
.ciphers_rawlen
= 0;
4958 PACKET_remaining(&sslv2ciphers
) > 0;
4959 raw
+= TLS_CIPHER_LEN
) {
4960 if (!PACKET_get_1(&sslv2ciphers
, &leadbyte
)
4962 && !PACKET_copy_bytes(&sslv2ciphers
, raw
,
4965 && !PACKET_forward(&sslv2ciphers
, TLS_CIPHER_LEN
))) {
4966 *al
= SSL_AD_DECODE_ERROR
;
4967 OPENSSL_free(s
->s3
->tmp
.ciphers_raw
);
4968 s
->s3
->tmp
.ciphers_raw
= NULL
;
4969 s
->s3
->tmp
.ciphers_rawlen
= 0;
4973 s
->s3
->tmp
.ciphers_rawlen
+= TLS_CIPHER_LEN
;
4975 } else if (!PACKET_memdup(cipher_suites
, &s
->s3
->tmp
.ciphers_raw
,
4976 &s
->s3
->tmp
.ciphers_rawlen
)) {
4977 *al
= SSL_AD_INTERNAL_ERROR
;
4985 int SSL_bytes_to_cipher_list(SSL
*s
, const unsigned char *bytes
, size_t len
,
4986 int isv2format
, STACK_OF(SSL_CIPHER
) **sk
,
4987 STACK_OF(SSL_CIPHER
) **scsvs
)
4992 if (!PACKET_buf_init(&pkt
, bytes
, len
))
4994 return bytes_to_cipher_list(s
, &pkt
, sk
, scsvs
, isv2format
, &alert
);
4997 int bytes_to_cipher_list(SSL
*s
, PACKET
*cipher_suites
,
4998 STACK_OF(SSL_CIPHER
) **skp
,
4999 STACK_OF(SSL_CIPHER
) **scsvs_out
,
5000 int sslv2format
, int *al
)
5002 const SSL_CIPHER
*c
;
5003 STACK_OF(SSL_CIPHER
) *sk
= NULL
;
5004 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
5006 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
5007 unsigned char cipher
[SSLV2_CIPHER_LEN
];
5009 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
5011 if (PACKET_remaining(cipher_suites
) == 0) {
5012 SSLerr(SSL_F_BYTES_TO_CIPHER_LIST
, SSL_R_NO_CIPHERS_SPECIFIED
);
5013 *al
= SSL_AD_ILLEGAL_PARAMETER
;
5017 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
5018 SSLerr(SSL_F_BYTES_TO_CIPHER_LIST
,
5019 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
5020 *al
= SSL_AD_DECODE_ERROR
;
5024 sk
= sk_SSL_CIPHER_new_null();
5025 scsvs
= sk_SSL_CIPHER_new_null();
5026 if (sk
== NULL
|| scsvs
== NULL
) {
5027 SSLerr(SSL_F_BYTES_TO_CIPHER_LIST
, ERR_R_MALLOC_FAILURE
);
5028 *al
= SSL_AD_INTERNAL_ERROR
;
5032 while (PACKET_copy_bytes(cipher_suites
, cipher
, n
)) {
5034 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
5035 * first byte set to zero, while true SSLv2 ciphers have a non-zero
5036 * first byte. We don't support any true SSLv2 ciphers, so skip them.
5038 if (sslv2format
&& cipher
[0] != '\0')
5041 /* For SSLv2-compat, ignore leading 0-byte. */
5042 c
= ssl_get_cipher_by_char(s
, sslv2format
? &cipher
[1] : cipher
, 1);
5044 if ((c
->valid
&& !sk_SSL_CIPHER_push(sk
, c
)) ||
5045 (!c
->valid
&& !sk_SSL_CIPHER_push(scsvs
, c
))) {
5046 SSLerr(SSL_F_BYTES_TO_CIPHER_LIST
, ERR_R_MALLOC_FAILURE
);
5047 *al
= SSL_AD_INTERNAL_ERROR
;
5052 if (PACKET_remaining(cipher_suites
) > 0) {
5053 *al
= SSL_AD_DECODE_ERROR
;
5054 SSLerr(SSL_F_BYTES_TO_CIPHER_LIST
, SSL_R_BAD_LENGTH
);
5061 sk_SSL_CIPHER_free(sk
);
5062 if (scsvs_out
!= NULL
)
5065 sk_SSL_CIPHER_free(scsvs
);
5068 sk_SSL_CIPHER_free(sk
);
5069 sk_SSL_CIPHER_free(scsvs
);
5073 int SSL_CTX_set_max_early_data(SSL_CTX
*ctx
, uint32_t max_early_data
)
5075 ctx
->max_early_data
= max_early_data
;
5080 uint32_t SSL_CTX_get_max_early_data(const SSL_CTX
*ctx
)
5082 return ctx
->max_early_data
;
5085 int SSL_set_max_early_data(SSL
*s
, uint32_t max_early_data
)
5087 s
->max_early_data
= max_early_data
;
5092 uint32_t SSL_get_max_early_data(const SSL
*s
)
5094 return s
->max_early_data
;
5097 int ssl_randbytes(SSL
*s
, unsigned char *rnd
, size_t size
)
5099 if (s
->drbg
!= NULL
)
5100 return RAND_DRBG_generate(s
->drbg
, rnd
, size
, 0, NULL
, 0);
5101 return RAND_bytes(rnd
, (int)size
);