2 * SSL/TLS interface functions for OpenSSL
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #ifndef CONFIG_SMARTCARD
12 #ifndef OPENSSL_NO_ENGINE
14 #define OPENSSL_NO_ENGINE
19 #include <openssl/ssl.h>
20 #include <openssl/err.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/x509v3.h>
23 #ifndef OPENSSL_NO_ENGINE
24 #include <openssl/engine.h>
25 #endif /* OPENSSL_NO_ENGINE */
26 #ifndef OPENSSL_NO_DSA
27 #include <openssl/dsa.h>
30 #include <openssl/dh.h>
38 #include "tls_openssl.h"
40 #if defined(OPENSSL_IS_BORINGSSL)
41 /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
42 typedef size_t stack_index_t
;
44 typedef int stack_index_t
;
47 #ifdef SSL_set_tlsext_status_type
48 #ifndef OPENSSL_NO_TLSEXT
50 #include <openssl/ocsp.h>
51 #endif /* OPENSSL_NO_TLSEXT */
52 #endif /* SSL_set_tlsext_status_type */
54 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
56 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
57 * 1.1.0. Provide compatibility wrappers for older versions.
60 static size_t SSL_get_client_random(const SSL
*ssl
, unsigned char *out
,
63 if (!ssl
->s3
|| outlen
< SSL3_RANDOM_SIZE
)
65 os_memcpy(out
, ssl
->s3
->client_random
, SSL3_RANDOM_SIZE
);
66 return SSL3_RANDOM_SIZE
;
70 static size_t SSL_get_server_random(const SSL
*ssl
, unsigned char *out
,
73 if (!ssl
->s3
|| outlen
< SSL3_RANDOM_SIZE
)
75 os_memcpy(out
, ssl
->s3
->server_random
, SSL3_RANDOM_SIZE
);
76 return SSL3_RANDOM_SIZE
;
80 static size_t SSL_SESSION_get_master_key(const SSL_SESSION
*session
,
81 unsigned char *out
, size_t outlen
)
83 if (!session
|| session
->master_key_length
< 0 ||
84 (size_t) session
->master_key_length
> outlen
)
86 if ((size_t) session
->master_key_length
< outlen
)
87 outlen
= session
->master_key_length
;
88 os_memcpy(out
, session
->master_key
, outlen
);
95 #include <openssl/pem.h>
96 #include <keystore/keystore_get.h>
98 static BIO
* BIO_from_keystore(const char *key
)
101 uint8_t *value
= NULL
;
102 int length
= keystore_get(key
, strlen(key
), &value
);
103 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
104 BIO_write(bio
, value
, length
);
110 static int tls_openssl_ref_count
= 0;
111 static int tls_ex_idx_session
= -1;
114 void (*event_cb
)(void *ctx
, enum tls_event ev
,
115 union tls_event_data
*data
);
118 char *ocsp_stapling_response
;
121 static struct tls_context
*tls_global
= NULL
;
126 unsigned int tls_session_lifetime
;
129 struct tls_connection
{
130 struct tls_context
*context
;
133 BIO
*ssl_in
, *ssl_out
;
134 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
135 ENGINE
*engine
; /* functional reference to the engine */
136 EVP_PKEY
*private_key
; /* the private key if using engine */
137 #endif /* OPENSSL_NO_ENGINE */
138 char *subject_match
, *altsubject_match
, *suffix_match
, *domain_match
;
139 int read_alerts
, write_alerts
, failed
;
141 tls_session_ticket_cb session_ticket_cb
;
142 void *session_ticket_cb_ctx
;
144 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
146 size_t session_ticket_len
;
148 unsigned int ca_cert_verify
:1;
149 unsigned int cert_probe
:1;
150 unsigned int server_cert_only
:1;
151 unsigned int invalid_hb_used
:1;
152 unsigned int success_data
:1;
154 u8 srv_cert_hash
[32];
160 X509
*peer_issuer_issuer
;
162 unsigned char client_random
[SSL3_RANDOM_SIZE
];
163 unsigned char server_random
[SSL3_RANDOM_SIZE
];
167 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
169 struct tls_context
*context
= os_zalloc(sizeof(*context
));
173 context
->event_cb
= conf
->event_cb
;
174 context
->cb_ctx
= conf
->cb_ctx
;
175 context
->cert_in_cb
= conf
->cert_in_cb
;
181 #ifdef CONFIG_NO_STDOUT_DEBUG
183 static void _tls_show_errors(void)
187 while ((err
= ERR_get_error())) {
188 /* Just ignore the errors, since stdout is disabled */
191 #define tls_show_errors(l, f, t) _tls_show_errors()
193 #else /* CONFIG_NO_STDOUT_DEBUG */
195 static void tls_show_errors(int level
, const char *func
, const char *txt
)
199 wpa_printf(level
, "OpenSSL: %s - %s %s",
200 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
202 while ((err
= ERR_get_error())) {
203 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
204 ERR_error_string(err
, NULL
));
208 #endif /* CONFIG_NO_STDOUT_DEBUG */
211 #ifdef CONFIG_NATIVE_WINDOWS
213 /* Windows CryptoAPI and access to certificate stores */
214 #include <wincrypt.h>
216 #ifdef __MINGW32_VERSION
218 * MinGW does not yet include all the needed definitions for CryptoAPI, so
219 * define here whatever extra is needed.
221 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
222 #define CERT_STORE_READONLY_FLAG 0x00008000
223 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
225 #endif /* __MINGW32_VERSION */
228 struct cryptoapi_rsa_data
{
229 const CERT_CONTEXT
*cert
;
230 HCRYPTPROV crypt_prov
;
232 BOOL free_crypt_prov
;
236 static void cryptoapi_error(const char *msg
)
238 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
239 msg
, (unsigned int) GetLastError());
243 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
244 unsigned char *to
, RSA
*rsa
, int padding
)
246 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
251 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
252 unsigned char *to
, RSA
*rsa
, int padding
)
254 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
259 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
260 unsigned char *to
, RSA
*rsa
, int padding
)
262 struct cryptoapi_rsa_data
*priv
=
263 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
265 DWORD hash_size
, len
, i
;
266 unsigned char *buf
= NULL
;
270 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
271 ERR_R_PASSED_NULL_PARAMETER
);
275 if (padding
!= RSA_PKCS1_PADDING
) {
276 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
277 RSA_R_UNKNOWN_PADDING_TYPE
);
281 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
282 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
284 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
285 RSA_R_INVALID_MESSAGE_LENGTH
);
289 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
291 cryptoapi_error("CryptCreateHash failed");
295 len
= sizeof(hash_size
);
296 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
298 cryptoapi_error("CryptGetHashParam failed");
302 if ((int) hash_size
!= flen
) {
303 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
304 (unsigned) hash_size
, flen
);
305 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
306 RSA_R_INVALID_MESSAGE_LENGTH
);
309 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
310 cryptoapi_error("CryptSetHashParam failed");
315 buf
= os_malloc(len
);
317 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
321 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
322 cryptoapi_error("CryptSignHash failed");
326 for (i
= 0; i
< len
; i
++)
327 to
[i
] = buf
[len
- i
- 1];
332 CryptDestroyHash(hash
);
338 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
339 unsigned char *to
, RSA
*rsa
, int padding
)
341 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
346 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
350 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
351 CryptReleaseContext(priv
->crypt_prov
, 0);
353 CertFreeCertificateContext(priv
->cert
);
358 static int cryptoapi_finish(RSA
*rsa
)
360 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
361 os_free((void *) rsa
->meth
);
367 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
370 const CERT_CONTEXT
*ret
= NULL
;
372 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
373 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
374 CERT_STORE_READONLY_FLAG
, L
"MY");
376 cryptoapi_error("Failed to open 'My system store'");
380 if (strncmp(name
, "cert://", 7) == 0) {
381 unsigned short wbuf
[255];
382 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
383 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
385 0, CERT_FIND_SUBJECT_STR
,
387 } else if (strncmp(name
, "hash://", 7) == 0) {
388 CRYPT_HASH_BLOB blob
;
390 const char *hash
= name
+ 7;
393 len
= os_strlen(hash
) / 2;
394 buf
= os_malloc(len
);
395 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
398 ret
= CertFindCertificateInStore(cs
,
407 CertCloseStore(cs
, 0);
413 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
416 RSA
*rsa
= NULL
, *pub_rsa
;
417 struct cryptoapi_rsa_data
*priv
;
418 RSA_METHOD
*rsa_meth
;
421 (strncmp(name
, "cert://", 7) != 0 &&
422 strncmp(name
, "hash://", 7) != 0))
425 priv
= os_zalloc(sizeof(*priv
));
426 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
427 if (priv
== NULL
|| rsa_meth
== NULL
) {
428 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
429 "for CryptoAPI RSA method");
435 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
436 if (priv
->cert
== NULL
) {
437 priv
->cert
= cryptoapi_find_cert(
438 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
440 if (priv
->cert
== NULL
) {
441 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
446 cert
= d2i_X509(NULL
,
447 (const unsigned char **) &priv
->cert
->pbCertEncoded
,
448 priv
->cert
->cbCertEncoded
);
450 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
455 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
456 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
457 NULL
, &priv
->crypt_prov
,
459 &priv
->free_crypt_prov
)) {
460 cryptoapi_error("Failed to acquire a private key for the "
465 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
466 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
467 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
468 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
469 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
470 rsa_meth
->finish
= cryptoapi_finish
;
471 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
472 rsa_meth
->app_data
= (char *) priv
;
476 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
477 ERR_R_MALLOC_FAILURE
);
481 if (!SSL_use_certificate(ssl
, cert
)) {
486 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
490 rsa
->n
= BN_dup(pub_rsa
->n
);
491 rsa
->e
= BN_dup(pub_rsa
->e
);
492 if (!RSA_set_method(rsa
, rsa_meth
))
495 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
508 cryptoapi_free_data(priv
);
514 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
517 PCCERT_CONTEXT ctx
= NULL
;
525 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
530 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
533 wsprintf(wstore
, L
"%S", store
);
534 cs
= CertOpenSystemStore(0, wstore
);
537 cs
= CertOpenSystemStore(0, store
);
540 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
541 "'%s': error=%d", __func__
, store
,
542 (int) GetLastError());
546 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
547 cert
= d2i_X509(NULL
,
548 (const unsigned char **) &ctx
->pbCertEncoded
,
551 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
552 "X509 DER encoding for CA cert");
556 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
558 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
559 "system certificate store: subject='%s'", buf
);
561 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
562 tls_show_errors(MSG_WARNING
, __func__
,
563 "Failed to add ca_cert to OpenSSL "
564 "certificate store");
570 if (!CertCloseStore(cs
, 0)) {
571 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
572 "'%s': error=%d", __func__
, name
+ 13,
573 (int) GetLastError());
580 #else /* CONFIG_NATIVE_WINDOWS */
582 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
587 #endif /* CONFIG_NATIVE_WINDOWS */
590 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
595 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
596 w
= where
& ~SSL_ST_MASK
;
597 if (w
& SSL_ST_CONNECT
)
599 else if (w
& SSL_ST_ACCEPT
)
604 if (where
& SSL_CB_LOOP
) {
605 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
606 str
, SSL_state_string_long(ssl
));
607 } else if (where
& SSL_CB_ALERT
) {
608 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
609 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
610 where
& SSL_CB_READ
?
611 "read (remote end reported an error)" :
612 "write (local SSL3 detected an error)",
613 SSL_alert_type_string_long(ret
),
614 SSL_alert_desc_string_long(ret
));
615 if ((ret
>> 8) == SSL3_AL_FATAL
) {
616 if (where
& SSL_CB_READ
)
619 conn
->write_alerts
++;
621 if (conn
->context
->event_cb
!= NULL
) {
622 union tls_event_data ev
;
623 struct tls_context
*context
= conn
->context
;
624 os_memset(&ev
, 0, sizeof(ev
));
625 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
626 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
627 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
628 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
630 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
631 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
632 str
, ret
== 0 ? "failed" : "error",
633 SSL_state_string_long(ssl
));
638 #ifndef OPENSSL_NO_ENGINE
640 * tls_engine_load_dynamic_generic - load any openssl engine
641 * @pre: an array of commands and values that load an engine initialized
642 * in the engine specific function
643 * @post: an array of commands and values that initialize an already loaded
644 * engine (or %NULL if not required)
645 * @id: the engine id of the engine to load (only required if post is not %NULL
647 * This function is a generic function that loads any openssl engine.
649 * Returns: 0 on success, -1 on failure
651 static int tls_engine_load_dynamic_generic(const char *pre
[],
652 const char *post
[], const char *id
)
655 const char *dynamic_id
= "dynamic";
657 engine
= ENGINE_by_id(id
);
660 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
666 engine
= ENGINE_by_id(dynamic_id
);
667 if (engine
== NULL
) {
668 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
670 ERR_error_string(ERR_get_error(), NULL
));
674 /* Perform the pre commands. This will load the engine. */
675 while (pre
&& pre
[0]) {
676 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
677 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
678 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
679 "%s %s [%s]", pre
[0], pre
[1],
680 ERR_error_string(ERR_get_error(), NULL
));
688 * Free the reference to the "dynamic" engine. The loaded engine can
689 * now be looked up using ENGINE_by_id().
693 engine
= ENGINE_by_id(id
);
694 if (engine
== NULL
) {
695 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
696 id
, ERR_error_string(ERR_get_error(), NULL
));
700 while (post
&& post
[0]) {
701 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
702 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
703 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
704 " %s %s [%s]", post
[0], post
[1],
705 ERR_error_string(ERR_get_error(), NULL
));
706 ENGINE_remove(engine
);
719 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
720 * @pkcs11_so_path: pksc11_so_path from the configuration
721 * @pcks11_module_path: pkcs11_module_path from the configuration
723 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
724 const char *pkcs11_module_path
)
726 char *engine_id
= "pkcs11";
727 const char *pre_cmd
[] = {
728 "SO_PATH", NULL
/* pkcs11_so_path */,
729 "ID", NULL
/* engine_id */,
731 /* "NO_VCHECK", "1", */
735 const char *post_cmd
[] = {
736 "MODULE_PATH", NULL
/* pkcs11_module_path */,
743 pre_cmd
[1] = pkcs11_so_path
;
744 pre_cmd
[3] = engine_id
;
745 if (pkcs11_module_path
)
746 post_cmd
[1] = pkcs11_module_path
;
750 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
753 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
758 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
759 * @opensc_so_path: opensc_so_path from the configuration
761 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
763 char *engine_id
= "opensc";
764 const char *pre_cmd
[] = {
765 "SO_PATH", NULL
/* opensc_so_path */,
766 "ID", NULL
/* engine_id */,
775 pre_cmd
[1] = opensc_so_path
;
776 pre_cmd
[3] = engine_id
;
778 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
781 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
783 #endif /* OPENSSL_NO_ENGINE */
786 static void remove_session_cb(SSL_CTX
*ctx
, SSL_SESSION
*sess
)
790 if (tls_ex_idx_session
< 0)
792 buf
= SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
795 wpa_printf(MSG_DEBUG
,
796 "OpenSSL: Free application session data %p (sess %p)",
800 SSL_SESSION_set_ex_data(sess
, tls_ex_idx_session
, NULL
);
804 void * tls_init(const struct tls_config
*conf
)
806 struct tls_data
*data
;
808 struct tls_context
*context
;
811 if (tls_openssl_ref_count
== 0) {
812 tls_global
= context
= tls_context_new(conf
);
817 if (conf
&& conf
->fips_mode
) {
818 static int fips_enabled
= 0;
820 if (!fips_enabled
&& !FIPS_mode_set(1)) {
821 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
823 ERR_load_crypto_strings();
824 ERR_print_errors_fp(stderr
);
829 wpa_printf(MSG_INFO
, "Running in FIPS mode");
833 #else /* OPENSSL_FIPS */
834 if (conf
&& conf
->fips_mode
) {
835 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
841 #endif /* OPENSSL_FIPS */
842 #endif /* CONFIG_FIPS */
843 SSL_load_error_strings();
845 #ifndef OPENSSL_NO_SHA256
846 EVP_add_digest(EVP_sha256());
847 #endif /* OPENSSL_NO_SHA256 */
848 /* TODO: if /dev/urandom is available, PRNG is seeded
849 * automatically. If this is not the case, random data should
853 #ifndef OPENSSL_NO_RC2
855 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
856 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
857 * versions, but it looks like OpenSSL 1.0.0 does not do that
860 EVP_add_cipher(EVP_rc2_40_cbc());
861 #endif /* OPENSSL_NO_RC2 */
863 #endif /* PKCS12_FUNCS */
865 context
= tls_context_new(conf
);
869 tls_openssl_ref_count
++;
871 data
= os_zalloc(sizeof(*data
));
873 ssl
= SSL_CTX_new(SSLv23_method());
877 tls_openssl_ref_count
--;
878 if (context
!= tls_global
)
880 if (tls_openssl_ref_count
== 0) {
888 data
->tls_session_lifetime
= conf
->tls_session_lifetime
;
890 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
891 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
893 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
894 SSL_CTX_set_app_data(ssl
, context
);
895 if (data
->tls_session_lifetime
> 0) {
896 SSL_CTX_set_quiet_shutdown(ssl
, 1);
898 * Set default context here. In practice, this will be replaced
899 * by the per-EAP method context in tls_connection_set_verify().
901 SSL_CTX_set_session_id_context(ssl
, (u8
*) "hostapd", 7);
902 SSL_CTX_set_session_cache_mode(ssl
, SSL_SESS_CACHE_SERVER
);
903 SSL_CTX_set_timeout(ssl
, data
->tls_session_lifetime
);
904 SSL_CTX_sess_set_remove_cb(ssl
, remove_session_cb
);
906 SSL_CTX_set_session_cache_mode(ssl
, SSL_SESS_CACHE_OFF
);
909 if (tls_ex_idx_session
< 0) {
910 tls_ex_idx_session
= SSL_SESSION_get_ex_new_index(
911 0, NULL
, NULL
, NULL
, NULL
);
912 if (tls_ex_idx_session
< 0) {
918 #ifndef OPENSSL_NO_ENGINE
919 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
920 ERR_load_ENGINE_strings();
921 ENGINE_load_dynamic();
924 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
925 conf
->pkcs11_module_path
)) {
926 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
927 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
928 conf
->pkcs11_module_path
)) {
933 #endif /* OPENSSL_NO_ENGINE */
935 if (conf
&& conf
->openssl_ciphers
)
936 ciphers
= conf
->openssl_ciphers
;
938 ciphers
= "DEFAULT:!EXP:!LOW";
939 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
940 wpa_printf(MSG_ERROR
,
941 "OpenSSL: Failed to set cipher string '%s'",
951 void tls_deinit(void *ssl_ctx
)
953 struct tls_data
*data
= ssl_ctx
;
954 SSL_CTX
*ssl
= data
->ssl
;
955 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
956 if (context
!= tls_global
)
958 if (data
->tls_session_lifetime
> 0)
959 SSL_CTX_flush_sessions(ssl
, 0);
962 tls_openssl_ref_count
--;
963 if (tls_openssl_ref_count
== 0) {
964 #ifndef OPENSSL_NO_ENGINE
966 #endif /* OPENSSL_NO_ENGINE */
967 CRYPTO_cleanup_all_ex_data();
968 ERR_remove_thread_state(NULL
);
971 os_free(tls_global
->ocsp_stapling_response
);
972 tls_global
->ocsp_stapling_response
= NULL
;
981 #ifndef OPENSSL_NO_ENGINE
983 /* Cryptoki return values */
984 #define CKR_PIN_INCORRECT 0x000000a0
985 #define CKR_PIN_INVALID 0x000000a1
986 #define CKR_PIN_LEN_RANGE 0x000000a2
989 #define ERR_LIB_PKCS11 ERR_LIB_USER
991 static int tls_is_pin_error(unsigned int err
)
993 return ERR_GET_LIB(err
) == ERR_LIB_PKCS11
&&
994 (ERR_GET_REASON(err
) == CKR_PIN_INCORRECT
||
995 ERR_GET_REASON(err
) == CKR_PIN_INVALID
||
996 ERR_GET_REASON(err
) == CKR_PIN_LEN_RANGE
);
999 #endif /* OPENSSL_NO_ENGINE */
1003 /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1004 EVP_PKEY
* EVP_PKEY_from_keystore(const char *key_id
);
1005 #endif /* ANDROID */
1007 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
1008 const char *pin
, const char *key_id
,
1009 const char *cert_id
, const char *ca_cert_id
)
1011 #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1012 #if !defined(OPENSSL_NO_ENGINE)
1013 #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1016 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1017 conn
->engine
= NULL
;
1018 conn
->private_key
= EVP_PKEY_from_keystore(key_id
);
1019 if (!conn
->private_key
) {
1020 wpa_printf(MSG_ERROR
,
1021 "ENGINE: cannot load private key with id '%s' [%s]",
1023 ERR_error_string(ERR_get_error(), NULL
));
1024 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1026 #endif /* ANDROID && OPENSSL_IS_BORINGSSL */
1028 #ifndef OPENSSL_NO_ENGINE
1030 if (engine_id
== NULL
) {
1031 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
1037 ENGINE_load_dynamic();
1039 conn
->engine
= ENGINE_by_id(engine_id
);
1040 if (!conn
->engine
) {
1041 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
1042 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
1045 if (ENGINE_init(conn
->engine
) != 1) {
1046 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
1047 "(engine: %s) [%s]", engine_id
,
1048 ERR_error_string(ERR_get_error(), NULL
));
1051 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
1054 if (pin
&& ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
1055 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
1056 ERR_error_string(ERR_get_error(), NULL
));
1062 * Ensure that the ENGINE does not attempt to use the OpenSSL
1063 * UI system to obtain a PIN, if we didn't provide one.
1066 const void *password
;
1067 const char *prompt_info
;
1068 } key_cb
= { "", NULL
};
1070 /* load private key first in-case PIN is required for cert */
1071 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
1074 if (!conn
->private_key
) {
1075 unsigned long err
= ERR_get_error();
1077 wpa_printf(MSG_ERROR
,
1078 "ENGINE: cannot load private key with id '%s' [%s]",
1080 ERR_error_string(err
, NULL
));
1081 if (tls_is_pin_error(err
))
1082 ret
= TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
1084 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1089 /* handle a certificate and/or CA certificate */
1090 if (cert_id
|| ca_cert_id
) {
1091 const char *cmd_name
= "LOAD_CERT_CTRL";
1093 /* test if the engine supports a LOAD_CERT_CTRL */
1094 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
1095 0, (void *)cmd_name
, NULL
)) {
1096 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
1097 " loading certificates");
1098 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1107 ENGINE_free(conn
->engine
);
1108 conn
->engine
= NULL
;
1111 if (conn
->private_key
) {
1112 EVP_PKEY_free(conn
->private_key
);
1113 conn
->private_key
= NULL
;
1117 #else /* OPENSSL_NO_ENGINE */
1119 #endif /* OPENSSL_NO_ENGINE */
1123 static void tls_engine_deinit(struct tls_connection
*conn
)
1125 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
1126 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
1127 if (conn
->private_key
) {
1128 EVP_PKEY_free(conn
->private_key
);
1129 conn
->private_key
= NULL
;
1132 #if !defined(OPENSSL_IS_BORINGSSL)
1133 ENGINE_finish(conn
->engine
);
1134 #endif /* !OPENSSL_IS_BORINGSSL */
1135 conn
->engine
= NULL
;
1137 #endif /* ANDROID || !OPENSSL_NO_ENGINE */
1141 int tls_get_errors(void *ssl_ctx
)
1146 while ((err
= ERR_get_error())) {
1147 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1148 ERR_error_string(err
, NULL
));
1156 static const char * openssl_content_type(int content_type
)
1158 switch (content_type
) {
1160 return "change cipher spec";
1166 return "application data";
1170 return "TLS header info"; /* pseudo content type */
1177 static const char * openssl_handshake_type(int content_type
, const u8
*buf
,
1180 if (content_type
!= 22 || !buf
|| len
== 0)
1184 return "hello request";
1186 return "client hello";
1188 return "server hello";
1190 return "new session ticket";
1192 return "certificate";
1194 return "server key exchange";
1196 return "certificate request";
1198 return "server hello done";
1200 return "certificate verify";
1202 return "client key exchange";
1206 return "certificate url";
1208 return "certificate status";
1215 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1216 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1218 struct tls_connection
*conn
= arg
;
1219 const u8
*pos
= buf
;
1222 wpa_printf(MSG_DEBUG
,
1223 "OpenSSL: session ver=0x%x content_type=%d",
1224 version
, content_type
);
1225 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Data", buf
, len
);
1229 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1230 write_p
? "TX" : "RX", version
, content_type
,
1231 openssl_content_type(content_type
),
1232 openssl_handshake_type(content_type
, buf
, len
));
1233 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1234 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1235 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1236 if (payload_len
+ 3 > len
) {
1237 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1238 conn
->invalid_hb_used
= 1;
1244 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1246 struct tls_data
*data
= ssl_ctx
;
1247 SSL_CTX
*ssl
= data
->ssl
;
1248 struct tls_connection
*conn
;
1250 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1252 conn
= os_zalloc(sizeof(*conn
));
1255 conn
->ssl_ctx
= ssl
;
1256 conn
->ssl
= SSL_new(ssl
);
1257 if (conn
->ssl
== NULL
) {
1258 tls_show_errors(MSG_INFO
, __func__
,
1259 "Failed to initialize new SSL connection");
1264 conn
->context
= context
;
1265 SSL_set_app_data(conn
->ssl
, conn
);
1266 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1267 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1268 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1269 SSL_OP_SINGLE_DH_USE
;
1270 #ifdef SSL_OP_NO_COMPRESSION
1271 options
|= SSL_OP_NO_COMPRESSION
;
1272 #endif /* SSL_OP_NO_COMPRESSION */
1273 SSL_set_options(conn
->ssl
, options
);
1275 conn
->ssl_in
= BIO_new(BIO_s_mem());
1276 if (!conn
->ssl_in
) {
1277 tls_show_errors(MSG_INFO
, __func__
,
1278 "Failed to create a new BIO for ssl_in");
1279 SSL_free(conn
->ssl
);
1284 conn
->ssl_out
= BIO_new(BIO_s_mem());
1285 if (!conn
->ssl_out
) {
1286 tls_show_errors(MSG_INFO
, __func__
,
1287 "Failed to create a new BIO for ssl_out");
1288 SSL_free(conn
->ssl
);
1289 BIO_free(conn
->ssl_in
);
1294 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1300 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1304 if (conn
->success_data
) {
1306 * Make sure ssl_clear_bad_session() does not remove this
1309 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1310 SSL_shutdown(conn
->ssl
);
1312 SSL_free(conn
->ssl
);
1313 tls_engine_deinit(conn
);
1314 os_free(conn
->subject_match
);
1315 os_free(conn
->altsubject_match
);
1316 os_free(conn
->suffix_match
);
1317 os_free(conn
->domain_match
);
1318 os_free(conn
->session_ticket
);
1323 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1325 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1329 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1334 /* Shutdown previous TLS connection without notifying the peer
1335 * because the connection was already terminated in practice
1336 * and "close notify" shutdown alert would confuse AS. */
1337 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1338 SSL_shutdown(conn
->ssl
);
1339 return SSL_clear(conn
->ssl
) == 1 ? 0 : -1;
1343 static int tls_match_altsubject_component(X509
*cert
, int type
,
1344 const char *value
, size_t len
)
1351 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1353 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1354 gen
= sk_GENERAL_NAME_value(ext
, i
);
1355 if (gen
->type
!= type
)
1357 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1358 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1366 static int tls_match_altsubject(X509
*cert
, const char *match
)
1369 const char *pos
, *end
;
1374 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1377 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1380 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1384 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1388 end
= os_strchr(pos
, ';');
1390 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1391 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1392 os_strncmp(end
+ 1, "URI:", 4) == 0)
1394 end
= os_strchr(end
+ 1, ';');
1399 len
= os_strlen(pos
);
1400 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1409 #ifndef CONFIG_NATIVE_WINDOWS
1410 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
,
1413 size_t i
, match_len
;
1415 /* Check for embedded nuls that could mess up suffix matching */
1416 for (i
= 0; i
< len
; i
++) {
1417 if (val
[i
] == '\0') {
1418 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1423 match_len
= os_strlen(match
);
1424 if (match_len
> len
|| (full
&& match_len
!= len
))
1427 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1429 return 0; /* no match */
1431 if (match_len
== len
)
1432 return 1; /* exact match */
1434 if (val
[len
- match_len
- 1] == '.')
1435 return 1; /* full label match completes suffix match */
1437 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1440 #endif /* CONFIG_NATIVE_WINDOWS */
1443 static int tls_match_suffix(X509
*cert
, const char *match
, int full
)
1445 #ifdef CONFIG_NATIVE_WINDOWS
1446 /* wincrypt.h has conflicting X509_NAME definition */
1448 #else /* CONFIG_NATIVE_WINDOWS */
1456 wpa_printf(MSG_DEBUG
, "TLS: Match domain against %s%s",
1457 full
? "": "suffix ", match
);
1459 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1461 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1462 gen
= sk_GENERAL_NAME_value(ext
, j
);
1463 if (gen
->type
!= GEN_DNS
)
1466 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1467 gen
->d
.dNSName
->data
,
1468 gen
->d
.dNSName
->length
);
1469 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1470 gen
->d
.dNSName
->length
, match
, full
) ==
1472 wpa_printf(MSG_DEBUG
, "TLS: %s in dNSName found",
1473 full
? "Match" : "Suffix match");
1479 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1483 name
= X509_get_subject_name(cert
);
1489 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1492 e
= X509_NAME_get_entry(name
, i
);
1495 cn
= X509_NAME_ENTRY_get_data(e
);
1498 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1499 cn
->data
, cn
->length
);
1500 if (domain_suffix_match(cn
->data
, cn
->length
, match
, full
) == 1)
1502 wpa_printf(MSG_DEBUG
, "TLS: %s in commonName found",
1503 full
? "Match" : "Suffix match");
1508 wpa_printf(MSG_DEBUG
, "TLS: No CommonName %smatch found",
1509 full
? "": "suffix ");
1511 #endif /* CONFIG_NATIVE_WINDOWS */
1515 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1518 case X509_V_ERR_CERT_REVOKED
:
1519 return TLS_FAIL_REVOKED
;
1520 case X509_V_ERR_CERT_NOT_YET_VALID
:
1521 case X509_V_ERR_CRL_NOT_YET_VALID
:
1522 return TLS_FAIL_NOT_YET_VALID
;
1523 case X509_V_ERR_CERT_HAS_EXPIRED
:
1524 case X509_V_ERR_CRL_HAS_EXPIRED
:
1525 return TLS_FAIL_EXPIRED
;
1526 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1527 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1528 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1529 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1530 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1531 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1532 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1533 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1534 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1535 case X509_V_ERR_INVALID_CA
:
1536 return TLS_FAIL_UNTRUSTED
;
1537 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1538 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1539 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1540 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1541 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1542 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1543 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1544 case X509_V_ERR_CERT_UNTRUSTED
:
1545 case X509_V_ERR_CERT_REJECTED
:
1546 return TLS_FAIL_BAD_CERTIFICATE
;
1548 return TLS_FAIL_UNSPECIFIED
;
1553 static struct wpabuf
* get_x509_cert(X509
*cert
)
1558 int cert_len
= i2d_X509(cert
, NULL
);
1562 buf
= wpabuf_alloc(cert_len
);
1566 tmp
= wpabuf_put(buf
, cert_len
);
1567 i2d_X509(cert
, &tmp
);
1572 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1573 X509
*err_cert
, int err
, int depth
,
1574 const char *subject
, const char *err_str
,
1575 enum tls_fail_reason reason
)
1577 union tls_event_data ev
;
1578 struct wpabuf
*cert
= NULL
;
1579 struct tls_context
*context
= conn
->context
;
1581 if (context
->event_cb
== NULL
)
1584 cert
= get_x509_cert(err_cert
);
1585 os_memset(&ev
, 0, sizeof(ev
));
1586 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1587 reason
: openssl_tls_fail_reason(err
);
1588 ev
.cert_fail
.depth
= depth
;
1589 ev
.cert_fail
.subject
= subject
;
1590 ev
.cert_fail
.reason_txt
= err_str
;
1591 ev
.cert_fail
.cert
= cert
;
1592 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1597 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1598 X509
*err_cert
, int depth
,
1599 const char *subject
)
1601 struct wpabuf
*cert
= NULL
;
1602 union tls_event_data ev
;
1603 struct tls_context
*context
= conn
->context
;
1604 char *altsubject
[TLS_MAX_ALT_SUBJECT
];
1605 int alt
, num_altsubject
= 0;
1609 #ifdef CONFIG_SHA256
1611 #endif /* CONFIG_SHA256 */
1613 if (context
->event_cb
== NULL
)
1616 os_memset(&ev
, 0, sizeof(ev
));
1617 if (conn
->cert_probe
|| (conn
->flags
& TLS_CONN_EXT_CERT_CHECK
) ||
1618 context
->cert_in_cb
) {
1619 cert
= get_x509_cert(err_cert
);
1620 ev
.peer_cert
.cert
= cert
;
1622 #ifdef CONFIG_SHA256
1626 addr
[0] = wpabuf_head(cert
);
1627 len
[0] = wpabuf_len(cert
);
1628 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1629 ev
.peer_cert
.hash
= hash
;
1630 ev
.peer_cert
.hash_len
= sizeof(hash
);
1633 #endif /* CONFIG_SHA256 */
1634 ev
.peer_cert
.depth
= depth
;
1635 ev
.peer_cert
.subject
= subject
;
1637 ext
= X509_get_ext_d2i(err_cert
, NID_subject_alt_name
, NULL
, NULL
);
1638 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1641 if (num_altsubject
== TLS_MAX_ALT_SUBJECT
)
1643 gen
= sk_GENERAL_NAME_value(ext
, i
);
1644 if (gen
->type
!= GEN_EMAIL
&&
1645 gen
->type
!= GEN_DNS
&&
1646 gen
->type
!= GEN_URI
)
1649 pos
= os_malloc(10 + gen
->d
.ia5
->length
+ 1);
1652 altsubject
[num_altsubject
++] = pos
;
1654 switch (gen
->type
) {
1656 os_memcpy(pos
, "EMAIL:", 6);
1660 os_memcpy(pos
, "DNS:", 4);
1664 os_memcpy(pos
, "URI:", 4);
1669 os_memcpy(pos
, gen
->d
.ia5
->data
, gen
->d
.ia5
->length
);
1670 pos
+= gen
->d
.ia5
->length
;
1674 for (alt
= 0; alt
< num_altsubject
; alt
++)
1675 ev
.peer_cert
.altsubject
[alt
] = altsubject
[alt
];
1676 ev
.peer_cert
.num_altsubject
= num_altsubject
;
1678 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1680 for (alt
= 0; alt
< num_altsubject
; alt
++)
1681 os_free(altsubject
[alt
]);
1685 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1691 struct tls_connection
*conn
;
1692 struct tls_context
*context
;
1693 char *match
, *altmatch
, *suffix_match
, *domain_match
;
1694 const char *err_str
;
1696 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1700 err
= X509_STORE_CTX_get_error(x509_ctx
);
1701 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1702 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1703 SSL_get_ex_data_X509_STORE_CTX_idx());
1704 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1706 conn
= SSL_get_app_data(ssl
);
1711 conn
->peer_cert
= err_cert
;
1712 else if (depth
== 1)
1713 conn
->peer_issuer
= err_cert
;
1714 else if (depth
== 2)
1715 conn
->peer_issuer_issuer
= err_cert
;
1717 context
= conn
->context
;
1718 match
= conn
->subject_match
;
1719 altmatch
= conn
->altsubject_match
;
1720 suffix_match
= conn
->suffix_match
;
1721 domain_match
= conn
->domain_match
;
1723 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1725 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1727 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1728 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1729 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1730 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1735 err_str
= X509_verify_cert_error_string(err
);
1737 #ifdef CONFIG_SHA256
1739 * Do not require preverify_ok so we can explicity allow otherwise
1740 * invalid pinned server certificates.
1742 if (depth
== 0 && conn
->server_cert_only
) {
1743 struct wpabuf
*cert
;
1744 cert
= get_x509_cert(err_cert
);
1746 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1747 "server certificate data");
1753 addr
[0] = wpabuf_head(cert
);
1754 len
[0] = wpabuf_len(cert
);
1755 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1756 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1757 err_str
= "Server certificate mismatch";
1758 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1760 } else if (!preverify_ok
) {
1762 * Certificate matches pinned certificate, allow
1763 * regardless of other problems.
1765 wpa_printf(MSG_DEBUG
,
1766 "OpenSSL: Ignore validation issues for a pinned server certificate");
1772 #endif /* CONFIG_SHA256 */
1774 if (!preverify_ok
) {
1775 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1776 " error %d (%s) depth %d for '%s'", err
, err_str
,
1778 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1779 err_str
, TLS_FAIL_UNSPECIFIED
);
1780 return preverify_ok
;
1783 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1784 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1785 preverify_ok
, err
, err_str
,
1786 conn
->ca_cert_verify
, depth
, buf
);
1787 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1788 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1789 "match with '%s'", buf
, match
);
1791 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1793 TLS_FAIL_SUBJECT_MISMATCH
);
1794 } else if (depth
== 0 && altmatch
&&
1795 !tls_match_altsubject(err_cert
, altmatch
)) {
1796 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1797 "'%s' not found", altmatch
);
1799 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1800 "AltSubject mismatch",
1801 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1802 } else if (depth
== 0 && suffix_match
&&
1803 !tls_match_suffix(err_cert
, suffix_match
, 0)) {
1804 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1807 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1808 "Domain suffix mismatch",
1809 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1810 } else if (depth
== 0 && domain_match
&&
1811 !tls_match_suffix(err_cert
, domain_match
, 1)) {
1812 wpa_printf(MSG_WARNING
, "TLS: Domain match '%s' not found",
1815 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1817 TLS_FAIL_DOMAIN_MISMATCH
);
1819 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1821 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1822 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1823 "on probe-only run");
1825 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1826 "Server certificate chain probe",
1827 TLS_FAIL_SERVER_CHAIN_PROBE
);
1830 #ifdef OPENSSL_IS_BORINGSSL
1831 if (depth
== 0 && (conn
->flags
& TLS_CONN_REQUEST_OCSP
) &&
1833 enum ocsp_result res
;
1835 res
= check_ocsp_resp(conn
->ssl_ctx
, conn
->ssl
, err_cert
,
1837 conn
->peer_issuer_issuer
);
1838 if (res
== OCSP_REVOKED
) {
1840 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1841 "certificate revoked",
1843 if (err
== X509_V_OK
)
1844 X509_STORE_CTX_set_error(
1845 x509_ctx
, X509_V_ERR_CERT_REVOKED
);
1846 } else if (res
!= OCSP_GOOD
&&
1847 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
)) {
1849 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1850 "bad certificate status response",
1851 TLS_FAIL_UNSPECIFIED
);
1854 #endif /* OPENSSL_IS_BORINGSSL */
1856 if (depth
== 0 && preverify_ok
&& context
->event_cb
!= NULL
)
1857 context
->event_cb(context
->cb_ctx
,
1858 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1860 return preverify_ok
;
1864 #ifndef OPENSSL_NO_STDIO
1865 static int tls_load_ca_der(struct tls_data
*data
, const char *ca_cert
)
1867 SSL_CTX
*ssl_ctx
= data
->ssl
;
1868 X509_LOOKUP
*lookup
;
1871 lookup
= X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx
),
1872 X509_LOOKUP_file());
1873 if (lookup
== NULL
) {
1874 tls_show_errors(MSG_WARNING
, __func__
,
1875 "Failed add lookup for X509 store");
1879 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1880 unsigned long err
= ERR_peek_error();
1881 tls_show_errors(MSG_WARNING
, __func__
,
1882 "Failed load CA in DER format");
1883 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1884 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1885 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1886 "cert already in hash table error",
1894 #endif /* OPENSSL_NO_STDIO */
1897 static int tls_connection_ca_cert(struct tls_data
*data
,
1898 struct tls_connection
*conn
,
1899 const char *ca_cert
, const u8
*ca_cert_blob
,
1900 size_t ca_cert_blob_len
, const char *ca_path
)
1902 SSL_CTX
*ssl_ctx
= data
->ssl
;
1906 * Remove previously configured trusted CA certificates before adding
1909 store
= X509_STORE_new();
1910 if (store
== NULL
) {
1911 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
1912 "certificate store", __func__
);
1915 SSL_CTX_set_cert_store(ssl_ctx
, store
);
1917 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1918 conn
->ca_cert_verify
= 1;
1920 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
1921 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
1923 conn
->cert_probe
= 1;
1924 conn
->ca_cert_verify
= 0;
1928 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
1929 #ifdef CONFIG_SHA256
1930 const char *pos
= ca_cert
+ 7;
1931 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
1932 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
1933 "hash value '%s'", ca_cert
);
1937 if (os_strlen(pos
) != 32 * 2) {
1938 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
1939 "hash length in ca_cert '%s'", ca_cert
);
1942 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
1943 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
1944 "value in ca_cert '%s'", ca_cert
);
1947 conn
->server_cert_only
= 1;
1948 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
1949 "certificate match");
1951 #else /* CONFIG_SHA256 */
1952 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
1953 "cannot validate server certificate hash");
1955 #endif /* CONFIG_SHA256 */
1959 X509
*cert
= d2i_X509(NULL
,
1960 (const unsigned char **) &ca_cert_blob
,
1963 tls_show_errors(MSG_WARNING
, __func__
,
1964 "Failed to parse ca_cert_blob");
1968 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
1970 unsigned long err
= ERR_peek_error();
1971 tls_show_errors(MSG_WARNING
, __func__
,
1972 "Failed to add ca_cert_blob to "
1973 "certificate store");
1974 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1975 ERR_GET_REASON(err
) ==
1976 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1977 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1978 "cert already in hash table error",
1986 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
1987 "to certificate store", __func__
);
1992 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
1993 BIO
*bio
= BIO_from_keystore(&ca_cert
[11]);
1994 STACK_OF(X509_INFO
) *stack
= NULL
;
1998 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
2004 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
2005 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
2007 X509_STORE_add_cert(ssl_ctx
->cert_store
,
2011 X509_STORE_add_crl(ssl_ctx
->cert_store
,
2015 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
2016 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2019 #endif /* ANDROID */
2021 #ifdef CONFIG_NATIVE_WINDOWS
2022 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
2024 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
2025 "system certificate store");
2028 #endif /* CONFIG_NATIVE_WINDOWS */
2030 if (ca_cert
|| ca_path
) {
2031 #ifndef OPENSSL_NO_STDIO
2032 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
2034 tls_show_errors(MSG_WARNING
, __func__
,
2035 "Failed to load root certificates");
2037 tls_load_ca_der(data
, ca_cert
) == 0) {
2038 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
2039 "DER format CA certificate",
2044 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
2045 "certificate(s) loaded");
2046 tls_get_errors(data
);
2048 #else /* OPENSSL_NO_STDIO */
2049 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2052 #endif /* OPENSSL_NO_STDIO */
2054 /* No ca_cert configured - do not try to verify server
2056 conn
->ca_cert_verify
= 0;
2063 static int tls_global_ca_cert(struct tls_data
*data
, const char *ca_cert
)
2065 SSL_CTX
*ssl_ctx
= data
->ssl
;
2068 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
2070 tls_show_errors(MSG_WARNING
, __func__
,
2071 "Failed to load root certificates");
2075 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
2076 "certificate(s) loaded");
2078 #ifndef OPENSSL_NO_STDIO
2079 /* Add the same CAs to the client certificate requests */
2080 SSL_CTX_set_client_CA_list(ssl_ctx
,
2081 SSL_load_client_CA_file(ca_cert
));
2082 #endif /* OPENSSL_NO_STDIO */
2089 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
2094 struct tls_data
*data
= ssl_ctx
;
2095 X509_STORE
*cs
= SSL_CTX_get_cert_store(data
->ssl
);
2097 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
2098 "certificate store when enabling "
2102 flags
= X509_V_FLAG_CRL_CHECK
;
2104 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
2105 X509_STORE_set_flags(cs
, flags
);
2111 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
2112 const char *subject_match
,
2113 const char *altsubject_match
,
2114 const char *suffix_match
,
2115 const char *domain_match
)
2117 os_free(conn
->subject_match
);
2118 conn
->subject_match
= NULL
;
2119 if (subject_match
) {
2120 conn
->subject_match
= os_strdup(subject_match
);
2121 if (conn
->subject_match
== NULL
)
2125 os_free(conn
->altsubject_match
);
2126 conn
->altsubject_match
= NULL
;
2127 if (altsubject_match
) {
2128 conn
->altsubject_match
= os_strdup(altsubject_match
);
2129 if (conn
->altsubject_match
== NULL
)
2133 os_free(conn
->suffix_match
);
2134 conn
->suffix_match
= NULL
;
2136 conn
->suffix_match
= os_strdup(suffix_match
);
2137 if (conn
->suffix_match
== NULL
)
2141 os_free(conn
->domain_match
);
2142 conn
->domain_match
= NULL
;
2144 conn
->domain_match
= os_strdup(domain_match
);
2145 if (conn
->domain_match
== NULL
)
2153 static void tls_set_conn_flags(SSL
*ssl
, unsigned int flags
)
2155 #ifdef SSL_OP_NO_TICKET
2156 if (flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
2157 SSL_set_options(ssl
, SSL_OP_NO_TICKET
);
2158 #ifdef SSL_clear_options
2160 SSL_clear_options(ssl
, SSL_OP_NO_TICKET
);
2161 #endif /* SSL_clear_options */
2162 #endif /* SSL_OP_NO_TICKET */
2164 #ifdef SSL_OP_NO_TLSv1
2165 if (flags
& TLS_CONN_DISABLE_TLSv1_0
)
2166 SSL_set_options(ssl
, SSL_OP_NO_TLSv1
);
2168 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1
);
2169 #endif /* SSL_OP_NO_TLSv1 */
2170 #ifdef SSL_OP_NO_TLSv1_1
2171 if (flags
& TLS_CONN_DISABLE_TLSv1_1
)
2172 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_1
);
2174 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1_1
);
2175 #endif /* SSL_OP_NO_TLSv1_1 */
2176 #ifdef SSL_OP_NO_TLSv1_2
2177 if (flags
& TLS_CONN_DISABLE_TLSv1_2
)
2178 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_2
);
2180 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1_2
);
2181 #endif /* SSL_OP_NO_TLSv1_2 */
2185 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
2186 int verify_peer
, unsigned int flags
,
2187 const u8
*session_ctx
, size_t session_ctx_len
)
2189 static int counter
= 0;
2190 struct tls_data
*data
= ssl_ctx
;
2196 conn
->ca_cert_verify
= 1;
2197 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
2198 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
2199 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
2201 conn
->ca_cert_verify
= 0;
2202 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
2205 tls_set_conn_flags(conn
->ssl
, flags
);
2206 conn
->flags
= flags
;
2208 SSL_set_accept_state(conn
->ssl
);
2210 if (data
->tls_session_lifetime
== 0) {
2212 * Set session id context to a unique value to make sure
2213 * session resumption cannot be used either through session
2214 * caching or TLS ticket extension.
2217 SSL_set_session_id_context(conn
->ssl
,
2218 (const unsigned char *) &counter
,
2220 } else if (session_ctx
) {
2221 SSL_set_session_id_context(conn
->ssl
, session_ctx
,
2229 static int tls_connection_client_cert(struct tls_connection
*conn
,
2230 const char *client_cert
,
2231 const u8
*client_cert_blob
,
2232 size_t client_cert_blob_len
)
2234 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
2238 #if OPENSSL_VERSION_NUMBER < 0x10002000L
2240 * Clear previously set extra chain certificates, if any, from PKCS#12
2241 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2244 SSL_CTX_clear_extra_chain_certs(conn
->ssl_ctx
);
2245 #endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2246 #endif /* PKCS12_FUNCS */
2248 if (client_cert_blob
&&
2249 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
2250 client_cert_blob_len
) == 1) {
2251 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
2254 } else if (client_cert_blob
) {
2255 tls_show_errors(MSG_DEBUG
, __func__
,
2256 "SSL_use_certificate_ASN1 failed");
2259 if (client_cert
== NULL
)
2263 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
2264 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
2268 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
2272 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
2278 #endif /* ANDROID */
2280 #ifndef OPENSSL_NO_STDIO
2281 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2282 SSL_FILETYPE_ASN1
) == 1) {
2283 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
2288 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2289 SSL_FILETYPE_PEM
) == 1) {
2291 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
2296 tls_show_errors(MSG_DEBUG
, __func__
,
2297 "SSL_use_certificate_file failed");
2298 #else /* OPENSSL_NO_STDIO */
2299 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2300 #endif /* OPENSSL_NO_STDIO */
2306 static int tls_global_client_cert(struct tls_data
*data
,
2307 const char *client_cert
)
2309 #ifndef OPENSSL_NO_STDIO
2310 SSL_CTX
*ssl_ctx
= data
->ssl
;
2312 if (client_cert
== NULL
)
2315 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2316 SSL_FILETYPE_ASN1
) != 1 &&
2317 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
2318 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2319 SSL_FILETYPE_PEM
) != 1) {
2320 tls_show_errors(MSG_INFO
, __func__
,
2321 "Failed to load client certificate");
2325 #else /* OPENSSL_NO_STDIO */
2326 if (client_cert
== NULL
)
2328 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2330 #endif /* OPENSSL_NO_STDIO */
2334 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
2336 if (password
== NULL
) {
2339 os_strlcpy(buf
, (char *) password
, size
);
2340 return os_strlen(buf
);
2345 static int tls_parse_pkcs12(struct tls_data
*data
, SSL
*ssl
, PKCS12
*p12
,
2350 STACK_OF(X509
) *certs
;
2359 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
2360 tls_show_errors(MSG_DEBUG
, __func__
,
2361 "Failed to parse PKCS12 file");
2365 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
2368 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2370 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
2371 "subject='%s'", buf
);
2373 if (SSL_use_certificate(ssl
, cert
) != 1)
2376 if (SSL_CTX_use_certificate(data
->ssl
, cert
) != 1)
2383 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
2385 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
2388 if (SSL_CTX_use_PrivateKey(data
->ssl
, pkey
) != 1)
2391 EVP_PKEY_free(pkey
);
2395 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
2396 SSL_clear_chain_certs(ssl
);
2397 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2398 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2400 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2401 " from PKCS12: subject='%s'", buf
);
2402 if (SSL_add1_chain_cert(ssl
, cert
) != 1) {
2403 tls_show_errors(MSG_DEBUG
, __func__
,
2404 "Failed to add additional certificate");
2410 /* Try to continue anyway */
2412 sk_X509_free(certs
);
2413 #ifndef OPENSSL_IS_BORINGSSL
2414 res
= SSL_build_cert_chain(ssl
,
2415 SSL_BUILD_CHAIN_FLAG_CHECK
|
2416 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
);
2418 tls_show_errors(MSG_DEBUG
, __func__
,
2419 "Failed to build certificate chain");
2420 } else if (res
== 2) {
2421 wpa_printf(MSG_DEBUG
,
2422 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2424 #endif /* OPENSSL_IS_BORINGSSL */
2426 * Try to continue regardless of result since it is possible for
2427 * the extra certificates not to be required.
2430 #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2431 SSL_CTX_clear_extra_chain_certs(data
->ssl
);
2432 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2433 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2435 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2436 " from PKCS12: subject='%s'", buf
);
2438 * There is no SSL equivalent for the chain cert - so
2439 * always add it to the context...
2441 if (SSL_CTX_add_extra_chain_cert(data
->ssl
, cert
) != 1)
2447 sk_X509_free(certs
);
2448 #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2454 tls_get_errors(data
);
2458 #endif /* PKCS12_FUNCS */
2461 static int tls_read_pkcs12(struct tls_data
*data
, SSL
*ssl
,
2462 const char *private_key
, const char *passwd
)
2468 f
= fopen(private_key
, "rb");
2472 p12
= d2i_PKCS12_fp(f
, NULL
);
2476 tls_show_errors(MSG_INFO
, __func__
,
2477 "Failed to use PKCS#12 file");
2481 return tls_parse_pkcs12(data
, ssl
, p12
, passwd
);
2483 #else /* PKCS12_FUNCS */
2484 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2487 #endif /* PKCS12_FUNCS */
2491 static int tls_read_pkcs12_blob(struct tls_data
*data
, SSL
*ssl
,
2492 const u8
*blob
, size_t len
, const char *passwd
)
2497 p12
= d2i_PKCS12(NULL
, (const unsigned char **) &blob
, len
);
2499 tls_show_errors(MSG_INFO
, __func__
,
2500 "Failed to use PKCS#12 blob");
2504 return tls_parse_pkcs12(data
, ssl
, p12
, passwd
);
2506 #else /* PKCS12_FUNCS */
2507 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2510 #endif /* PKCS12_FUNCS */
2514 #ifndef OPENSSL_NO_ENGINE
2515 static int tls_engine_get_cert(struct tls_connection
*conn
,
2516 const char *cert_id
,
2519 /* this runs after the private key is loaded so no PIN is required */
2521 const char *cert_id
;
2524 params
.cert_id
= cert_id
;
2527 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2528 0, ¶ms
, NULL
, 1)) {
2529 unsigned long err
= ERR_get_error();
2531 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2532 " '%s' [%s]", cert_id
,
2533 ERR_error_string(err
, NULL
));
2534 if (tls_is_pin_error(err
))
2535 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
2536 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2539 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2541 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2543 *cert
= params
.cert
;
2546 #endif /* OPENSSL_NO_ENGINE */
2549 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2550 const char *cert_id
)
2552 #ifndef OPENSSL_NO_ENGINE
2555 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2558 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2559 tls_show_errors(MSG_ERROR
, __func__
,
2560 "SSL_use_certificate failed");
2565 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2569 #else /* OPENSSL_NO_ENGINE */
2571 #endif /* OPENSSL_NO_ENGINE */
2575 static int tls_connection_engine_ca_cert(struct tls_data
*data
,
2576 struct tls_connection
*conn
,
2577 const char *ca_cert_id
)
2579 #ifndef OPENSSL_NO_ENGINE
2581 SSL_CTX
*ssl_ctx
= data
->ssl
;
2584 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2587 /* start off the same as tls_connection_ca_cert */
2588 store
= X509_STORE_new();
2589 if (store
== NULL
) {
2590 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2591 "certificate store", __func__
);
2595 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2596 if (!X509_STORE_add_cert(store
, cert
)) {
2597 unsigned long err
= ERR_peek_error();
2598 tls_show_errors(MSG_WARNING
, __func__
,
2599 "Failed to add CA certificate from engine "
2600 "to certificate store");
2601 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2602 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2603 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2604 " already in hash table error",
2612 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2613 "to certificate store", __func__
);
2614 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2615 conn
->ca_cert_verify
= 1;
2619 #else /* OPENSSL_NO_ENGINE */
2621 #endif /* OPENSSL_NO_ENGINE */
2625 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2627 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
2628 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2629 tls_show_errors(MSG_ERROR
, __func__
,
2630 "ENGINE: cannot use private key for TLS");
2633 if (!SSL_check_private_key(conn
->ssl
)) {
2634 tls_show_errors(MSG_INFO
, __func__
,
2635 "Private key failed verification");
2639 #else /* OPENSSL_NO_ENGINE */
2640 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2641 "engine support was not compiled in");
2643 #endif /* OPENSSL_NO_ENGINE */
2647 static int tls_connection_private_key(struct tls_data
*data
,
2648 struct tls_connection
*conn
,
2649 const char *private_key
,
2650 const char *private_key_passwd
,
2651 const u8
*private_key_blob
,
2652 size_t private_key_blob_len
)
2654 SSL_CTX
*ssl_ctx
= data
->ssl
;
2658 if (private_key
== NULL
&& private_key_blob
== NULL
)
2661 if (private_key_passwd
) {
2662 passwd
= os_strdup(private_key_passwd
);
2668 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2669 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2672 while (private_key_blob
) {
2673 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2674 (u8
*) private_key_blob
,
2675 private_key_blob_len
) == 1) {
2676 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2677 "ASN1(EVP_PKEY_RSA) --> OK");
2682 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2683 (u8
*) private_key_blob
,
2684 private_key_blob_len
) == 1) {
2685 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2686 "ASN1(EVP_PKEY_DSA) --> OK");
2691 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2692 (u8
*) private_key_blob
,
2693 private_key_blob_len
) == 1) {
2694 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2695 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2700 if (tls_read_pkcs12_blob(data
, conn
->ssl
, private_key_blob
,
2701 private_key_blob_len
, passwd
) == 0) {
2702 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2711 while (!ok
&& private_key
) {
2712 #ifndef OPENSSL_NO_STDIO
2713 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2714 SSL_FILETYPE_ASN1
) == 1) {
2715 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2716 "SSL_use_PrivateKey_File (DER) --> OK");
2721 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2722 SSL_FILETYPE_PEM
) == 1) {
2723 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2724 "SSL_use_PrivateKey_File (PEM) --> OK");
2728 #else /* OPENSSL_NO_STDIO */
2729 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2731 #endif /* OPENSSL_NO_STDIO */
2733 if (tls_read_pkcs12(data
, conn
->ssl
, private_key
, passwd
)
2735 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2741 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2742 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2743 "access certificate store --> OK");
2752 tls_show_errors(MSG_INFO
, __func__
,
2753 "Failed to load private key");
2758 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2761 if (!SSL_check_private_key(conn
->ssl
)) {
2762 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
2767 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
2772 static int tls_global_private_key(struct tls_data
*data
,
2773 const char *private_key
,
2774 const char *private_key_passwd
)
2776 SSL_CTX
*ssl_ctx
= data
->ssl
;
2779 if (private_key
== NULL
)
2782 if (private_key_passwd
) {
2783 passwd
= os_strdup(private_key_passwd
);
2789 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2790 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2792 #ifndef OPENSSL_NO_STDIO
2793 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2794 SSL_FILETYPE_ASN1
) != 1 &&
2795 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2796 SSL_FILETYPE_PEM
) != 1 &&
2797 #endif /* OPENSSL_NO_STDIO */
2798 tls_read_pkcs12(data
, NULL
, private_key
, passwd
)) {
2799 tls_show_errors(MSG_INFO
, __func__
,
2800 "Failed to load private key");
2807 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2809 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
2810 tls_show_errors(MSG_INFO
, __func__
,
2811 "Private key failed verification");
2819 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
2821 #ifdef OPENSSL_NO_DH
2822 if (dh_file
== NULL
)
2824 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2825 "dh_file specified");
2827 #else /* OPENSSL_NO_DH */
2831 /* TODO: add support for dh_blob */
2832 if (dh_file
== NULL
)
2837 bio
= BIO_new_file(dh_file
, "r");
2839 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2840 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2843 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2845 #ifndef OPENSSL_NO_DSA
2846 while (dh
== NULL
) {
2848 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2849 " trying to parse as DSA params", dh_file
,
2850 ERR_error_string(ERR_get_error(), NULL
));
2851 bio
= BIO_new_file(dh_file
, "r");
2854 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2857 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2858 "'%s': %s", dh_file
,
2859 ERR_error_string(ERR_get_error(), NULL
));
2863 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2864 dh
= DSA_dup_DH(dsa
);
2867 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2868 "params into DH params");
2873 #endif /* !OPENSSL_NO_DSA */
2875 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2880 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
2881 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2883 ERR_error_string(ERR_get_error(), NULL
));
2889 #endif /* OPENSSL_NO_DH */
2893 static int tls_global_dh(struct tls_data
*data
, const char *dh_file
)
2895 #ifdef OPENSSL_NO_DH
2896 if (dh_file
== NULL
)
2898 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2899 "dh_file specified");
2901 #else /* OPENSSL_NO_DH */
2902 SSL_CTX
*ssl_ctx
= data
->ssl
;
2906 /* TODO: add support for dh_blob */
2907 if (dh_file
== NULL
)
2909 if (ssl_ctx
== NULL
)
2912 bio
= BIO_new_file(dh_file
, "r");
2914 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2915 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2918 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2920 #ifndef OPENSSL_NO_DSA
2921 while (dh
== NULL
) {
2923 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2924 " trying to parse as DSA params", dh_file
,
2925 ERR_error_string(ERR_get_error(), NULL
));
2926 bio
= BIO_new_file(dh_file
, "r");
2929 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2932 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2933 "'%s': %s", dh_file
,
2934 ERR_error_string(ERR_get_error(), NULL
));
2938 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2939 dh
= DSA_dup_DH(dsa
);
2942 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2943 "params into DH params");
2948 #endif /* !OPENSSL_NO_DSA */
2950 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2955 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
2956 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2958 ERR_error_string(ERR_get_error(), NULL
));
2964 #endif /* OPENSSL_NO_DH */
2968 int tls_connection_get_random(void *ssl_ctx
, struct tls_connection
*conn
,
2969 struct tls_random
*keys
)
2973 if (conn
== NULL
|| keys
== NULL
)
2979 os_memset(keys
, 0, sizeof(*keys
));
2980 keys
->client_random
= conn
->client_random
;
2981 keys
->client_random_len
= SSL_get_client_random(
2982 ssl
, conn
->client_random
, sizeof(conn
->client_random
));
2983 keys
->server_random
= conn
->server_random
;
2984 keys
->server_random_len
= SSL_get_server_random(
2985 ssl
, conn
->server_random
, sizeof(conn
->server_random
));
2992 static int openssl_get_keyblock_size(SSL
*ssl
)
2994 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
2995 const EVP_CIPHER
*c
;
2999 if (ssl
->enc_read_ctx
== NULL
|| ssl
->enc_read_ctx
->cipher
== NULL
||
3000 ssl
->read_hash
== NULL
)
3003 c
= ssl
->enc_read_ctx
->cipher
;
3004 h
= EVP_MD_CTX_md(ssl
->read_hash
);
3006 md_size
= EVP_MD_size(h
);
3008 md_size
= ssl
->s3
->tmp
.new_mac_secret_size
;
3012 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3013 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
3014 EVP_CIPHER_iv_length(c
));
3015 return 2 * (EVP_CIPHER_key_length(c
) +
3017 EVP_CIPHER_iv_length(c
));
3019 const SSL_CIPHER
*ssl_cipher
;
3021 const EVP_CIPHER
*c
;
3024 ssl_cipher
= SSL_get_current_cipher(ssl
);
3027 cipher
= SSL_CIPHER_get_cipher_nid(ssl_cipher
);
3028 digest
= SSL_CIPHER_get_digest_nid(ssl_cipher
);
3029 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher nid %d digest nid %d",
3031 if (cipher
< 0 || digest
< 0)
3033 c
= EVP_get_cipherbynid(cipher
);
3034 h
= EVP_get_digestbynid(digest
);
3038 wpa_printf(MSG_DEBUG
,
3039 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3040 EVP_CIPHER_key_length(c
), EVP_MD_size(h
),
3041 EVP_CIPHER_iv_length(c
));
3042 return 2 * (EVP_CIPHER_key_length(c
) + EVP_MD_size(h
) +
3043 EVP_CIPHER_iv_length(c
));
3046 #endif /* CONFIG_FIPS */
3049 static int openssl_tls_prf(struct tls_connection
*conn
,
3050 const char *label
, int server_random_first
,
3051 int skip_keyblock
, u8
*out
, size_t out_len
)
3054 wpa_printf(MSG_ERROR
, "OpenSSL: TLS keys cannot be exported in FIPS "
3057 #else /* CONFIG_FIPS */
3065 unsigned char client_random
[SSL3_RANDOM_SIZE
];
3066 unsigned char server_random
[SSL3_RANDOM_SIZE
];
3067 unsigned char master_key
[64];
3068 size_t master_key_len
;
3072 * TLS library did not support key generation, so get the needed TLS
3073 * session parameters and use an internal implementation of TLS PRF to
3082 ver
= SSL_get_version(ssl
);
3083 sess
= SSL_get_session(ssl
);
3087 if (skip_keyblock
) {
3088 skip
= openssl_get_keyblock_size(ssl
);
3091 tmp_out
= os_malloc(skip
+ out_len
);
3097 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
3103 SSL_get_client_random(ssl
, client_random
, sizeof(client_random
));
3104 SSL_get_server_random(ssl
, server_random
, sizeof(server_random
));
3105 master_key_len
= SSL_SESSION_get_master_key(sess
, master_key
,
3106 sizeof(master_key
));
3108 if (server_random_first
) {
3109 os_memcpy(rnd
, server_random
, SSL3_RANDOM_SIZE
);
3110 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, client_random
,
3113 os_memcpy(rnd
, client_random
, SSL3_RANDOM_SIZE
);
3114 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, server_random
,
3118 if (os_strcmp(ver
, "TLSv1.2") == 0) {
3119 tls_prf_sha256(master_key
, master_key_len
,
3120 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
3121 _out
, skip
+ out_len
);
3123 } else if (tls_prf_sha1_md5(master_key
, master_key_len
,
3124 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
3125 _out
, skip
+ out_len
) == 0) {
3128 os_memset(master_key
, 0, sizeof(master_key
));
3130 if (ret
== 0 && skip_keyblock
)
3131 os_memcpy(out
, _out
+ skip
, out_len
);
3132 bin_clear_free(tmp_out
, skip
);
3135 #endif /* CONFIG_FIPS */
3139 int tls_connection_prf(void *tls_ctx
, struct tls_connection
*conn
,
3140 const char *label
, int server_random_first
,
3141 int skip_keyblock
, u8
*out
, size_t out_len
)
3145 if (server_random_first
|| skip_keyblock
)
3146 return openssl_tls_prf(conn
, label
,
3147 server_random_first
, skip_keyblock
,
3149 if (SSL_export_keying_material(conn
->ssl
, out
, out_len
, label
,
3150 os_strlen(label
), NULL
, 0, 0) == 1) {
3151 wpa_printf(MSG_DEBUG
, "OpenSSL: Using internal PRF");
3154 return openssl_tls_prf(conn
, label
, server_random_first
,
3155 skip_keyblock
, out
, out_len
);
3159 static struct wpabuf
*
3160 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
3164 struct wpabuf
*out_data
;
3167 * Give TLS handshake data from the server (if available) to OpenSSL
3170 if (in_data
&& wpabuf_len(in_data
) > 0 &&
3171 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
3173 tls_show_errors(MSG_INFO
, __func__
,
3174 "Handshake failed - BIO_write");
3178 /* Initiate TLS handshake or continue the existing handshake */
3180 res
= SSL_accept(conn
->ssl
);
3182 res
= SSL_connect(conn
->ssl
);
3184 int err
= SSL_get_error(conn
->ssl
, res
);
3185 if (err
== SSL_ERROR_WANT_READ
)
3186 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
3188 else if (err
== SSL_ERROR_WANT_WRITE
)
3189 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
3192 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
3197 /* Get the TLS handshake data to be sent to the server */
3198 res
= BIO_ctrl_pending(conn
->ssl_out
);
3199 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
3200 out_data
= wpabuf_alloc(res
);
3201 if (out_data
== NULL
) {
3202 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
3203 "handshake output (%d bytes)", res
);
3204 if (BIO_reset(conn
->ssl_out
) < 0) {
3205 tls_show_errors(MSG_INFO
, __func__
,
3206 "BIO_reset failed");
3210 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
3213 tls_show_errors(MSG_INFO
, __func__
,
3214 "Handshake failed - BIO_read");
3215 if (BIO_reset(conn
->ssl_out
) < 0) {
3216 tls_show_errors(MSG_INFO
, __func__
,
3217 "BIO_reset failed");
3219 wpabuf_free(out_data
);
3222 wpabuf_put(out_data
, res
);
3228 static struct wpabuf
*
3229 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
3231 struct wpabuf
*appl_data
;
3234 appl_data
= wpabuf_alloc(max_len
+ 100);
3235 if (appl_data
== NULL
)
3238 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
3239 wpabuf_size(appl_data
));
3241 int err
= SSL_get_error(conn
->ssl
, res
);
3242 if (err
== SSL_ERROR_WANT_READ
||
3243 err
== SSL_ERROR_WANT_WRITE
) {
3244 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
3247 tls_show_errors(MSG_INFO
, __func__
,
3248 "Failed to read possible "
3249 "Application Data");
3251 wpabuf_free(appl_data
);
3255 wpabuf_put(appl_data
, res
);
3256 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
3257 "message", appl_data
);
3263 static struct wpabuf
*
3264 openssl_connection_handshake(struct tls_connection
*conn
,
3265 const struct wpabuf
*in_data
,
3266 struct wpabuf
**appl_data
, int server
)
3268 struct wpabuf
*out_data
;
3273 out_data
= openssl_handshake(conn
, in_data
, server
);
3274 if (out_data
== NULL
)
3276 if (conn
->invalid_hb_used
) {
3277 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3278 wpabuf_free(out_data
);
3282 if (SSL_is_init_finished(conn
->ssl
)) {
3283 wpa_printf(MSG_DEBUG
,
3284 "OpenSSL: Handshake finished - resumed=%d",
3285 tls_connection_resumed(conn
->ssl_ctx
, conn
));
3286 if (appl_data
&& in_data
)
3287 *appl_data
= openssl_get_appl_data(conn
,
3288 wpabuf_len(in_data
));
3291 if (conn
->invalid_hb_used
) {
3292 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3294 wpabuf_free(*appl_data
);
3297 wpabuf_free(out_data
);
3306 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
3307 const struct wpabuf
*in_data
,
3308 struct wpabuf
**appl_data
)
3310 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
3314 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
3315 struct tls_connection
*conn
,
3316 const struct wpabuf
*in_data
,
3317 struct wpabuf
**appl_data
)
3319 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
3323 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
3324 struct tls_connection
*conn
,
3325 const struct wpabuf
*in_data
)
3333 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3334 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
3335 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
3336 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3339 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
3341 tls_show_errors(MSG_INFO
, __func__
,
3342 "Encryption failed - SSL_write");
3346 /* Read encrypted data to be sent to the server */
3347 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
3350 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3352 tls_show_errors(MSG_INFO
, __func__
,
3353 "Encryption failed - BIO_read");
3357 wpabuf_put(buf
, res
);
3363 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
3364 struct tls_connection
*conn
,
3365 const struct wpabuf
*in_data
)
3370 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3371 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
3372 wpabuf_len(in_data
));
3374 tls_show_errors(MSG_INFO
, __func__
,
3375 "Decryption failed - BIO_write");
3378 if (BIO_reset(conn
->ssl_out
) < 0) {
3379 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3383 /* Read decrypted data for further processing */
3385 * Even though we try to disable TLS compression, it is possible that
3386 * this cannot be done with all TLS libraries. Add extra buffer space
3387 * to handle the possibility of the decrypted data being longer than
3390 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
3393 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3395 tls_show_errors(MSG_INFO
, __func__
,
3396 "Decryption failed - SSL_read");
3400 wpabuf_put(buf
, res
);
3402 if (conn
->invalid_hb_used
) {
3403 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3412 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
3414 return conn
? SSL_cache_hit(conn
->ssl
) : 0;
3418 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
3421 char buf
[500], *pos
, *end
;
3425 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
3430 end
= pos
+ sizeof(buf
);
3433 while (*c
!= TLS_CIPHER_NONE
) {
3437 case TLS_CIPHER_RC4_SHA
:
3440 case TLS_CIPHER_AES128_SHA
:
3441 suite
= "AES128-SHA";
3443 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
3444 suite
= "DHE-RSA-AES128-SHA";
3446 case TLS_CIPHER_ANON_DH_AES128_SHA
:
3447 suite
= "ADH-AES128-SHA";
3449 case TLS_CIPHER_RSA_DHE_AES256_SHA
:
3450 suite
= "DHE-RSA-AES256-SHA";
3452 case TLS_CIPHER_AES256_SHA
:
3453 suite
= "AES256-SHA";
3456 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
3457 "cipher selection: %d", *c
);
3460 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
3461 if (os_snprintf_error(end
- pos
, ret
))
3468 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
3470 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
3471 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3472 if (os_strstr(buf
, ":ADH-")) {
3474 * Need to drop to security level 0 to allow anonymous
3475 * cipher suites for EAP-FAST.
3477 SSL_set_security_level(conn
->ssl
, 0);
3478 } else if (SSL_get_security_level(conn
->ssl
) == 0) {
3479 /* Force at least security level 1 */
3480 SSL_set_security_level(conn
->ssl
, 1);
3482 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3485 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
3486 tls_show_errors(MSG_INFO
, __func__
,
3487 "Cipher suite configuration failed");
3495 int tls_get_version(void *ssl_ctx
, struct tls_connection
*conn
,
3496 char *buf
, size_t buflen
)
3499 if (conn
== NULL
|| conn
->ssl
== NULL
)
3502 name
= SSL_get_version(conn
->ssl
);
3506 os_strlcpy(buf
, name
, buflen
);
3511 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
3512 char *buf
, size_t buflen
)
3515 if (conn
== NULL
|| conn
->ssl
== NULL
)
3518 name
= SSL_get_cipher(conn
->ssl
);
3522 os_strlcpy(buf
, name
, buflen
);
3527 int tls_connection_enable_workaround(void *ssl_ctx
,
3528 struct tls_connection
*conn
)
3530 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3536 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3537 /* ClientHello TLS extensions require a patch to openssl, so this function is
3538 * commented out unless explicitly needed for EAP-FAST in order to be able to
3539 * build this file with unmodified openssl. */
3540 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
3541 int ext_type
, const u8
*data
,
3544 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
3547 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
3553 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3556 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
3560 return conn
->failed
;
3564 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3568 return conn
->read_alerts
;
3572 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3576 return conn
->write_alerts
;
3582 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
3584 #ifndef CONFIG_NO_STDOUT_DEBUG
3590 if (wpa_debug_level
> MSG_DEBUG
)
3593 out
= BIO_new(BIO_s_mem());
3597 OCSP_RESPONSE_print(out
, rsp
, 0);
3598 rlen
= BIO_ctrl_pending(out
);
3599 txt
= os_malloc(rlen
+ 1);
3605 res
= BIO_read(out
, txt
, rlen
);
3608 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
3612 #endif /* CONFIG_NO_STDOUT_DEBUG */
3616 static void debug_print_cert(X509
*cert
, const char *title
)
3618 #ifndef CONFIG_NO_STDOUT_DEBUG
3624 if (wpa_debug_level
> MSG_DEBUG
)
3627 out
= BIO_new(BIO_s_mem());
3631 X509_print(out
, cert
);
3632 rlen
= BIO_ctrl_pending(out
);
3633 txt
= os_malloc(rlen
+ 1);
3639 res
= BIO_read(out
, txt
, rlen
);
3642 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3647 #endif /* CONFIG_NO_STDOUT_DEBUG */
3651 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3653 struct tls_connection
*conn
= arg
;
3654 const unsigned char *p
;
3655 int len
, status
, reason
;
3657 OCSP_BASICRESP
*basic
;
3659 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3661 STACK_OF(X509
) *certs
= NULL
;
3663 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3665 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3666 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3669 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3671 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3673 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3677 ocsp_debug_print_resp(rsp
);
3679 status
= OCSP_response_status(rsp
);
3680 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3681 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3682 status
, OCSP_response_status_str(status
));
3686 basic
= OCSP_response_get1_basic(rsp
);
3688 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3692 store
= SSL_CTX_get_cert_store(conn
->ssl_ctx
);
3693 if (conn
->peer_issuer
) {
3694 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3696 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3697 tls_show_errors(MSG_INFO
, __func__
,
3698 "OpenSSL: Could not add issuer to certificate store");
3700 certs
= sk_X509_new_null();
3703 cert
= X509_dup(conn
->peer_issuer
);
3704 if (cert
&& !sk_X509_push(certs
, cert
)) {
3707 "OpenSSL: Could not add issuer to OCSP responder trust store");
3709 sk_X509_free(certs
);
3712 if (certs
&& conn
->peer_issuer_issuer
) {
3713 cert
= X509_dup(conn
->peer_issuer_issuer
);
3714 if (cert
&& !sk_X509_push(certs
, cert
)) {
3717 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
3724 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3725 sk_X509_pop_free(certs
, X509_free
);
3727 tls_show_errors(MSG_INFO
, __func__
,
3728 "OpenSSL: OCSP response failed verification");
3729 OCSP_BASICRESP_free(basic
);
3730 OCSP_RESPONSE_free(rsp
);
3734 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3736 if (!conn
->peer_cert
) {
3737 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3738 OCSP_BASICRESP_free(basic
);
3739 OCSP_RESPONSE_free(rsp
);
3743 if (!conn
->peer_issuer
) {
3744 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3745 OCSP_BASICRESP_free(basic
);
3746 OCSP_RESPONSE_free(rsp
);
3750 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3752 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not create OCSP certificate identifier");
3753 OCSP_BASICRESP_free(basic
);
3754 OCSP_RESPONSE_free(rsp
);
3758 if (!OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3759 &this_update
, &next_update
)) {
3760 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
3761 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
3762 " (OCSP not required)");
3763 OCSP_BASICRESP_free(basic
);
3764 OCSP_RESPONSE_free(rsp
);
3765 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3768 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
3769 tls_show_errors(MSG_INFO
, __func__
,
3770 "OpenSSL: OCSP status times invalid");
3771 OCSP_BASICRESP_free(basic
);
3772 OCSP_RESPONSE_free(rsp
);
3776 OCSP_BASICRESP_free(basic
);
3777 OCSP_RESPONSE_free(rsp
);
3779 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
3780 OCSP_cert_status_str(status
));
3782 if (status
== V_OCSP_CERTSTATUS_GOOD
)
3784 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
3786 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3787 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
3790 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
3795 static int ocsp_status_cb(SSL
*s
, void *arg
)
3801 if (tls_global
->ocsp_stapling_response
== NULL
) {
3802 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
3803 return SSL_TLSEXT_ERR_OK
;
3806 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
3808 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
3809 /* TODO: Build OCSPResponse with responseStatus = internalError
3811 return SSL_TLSEXT_ERR_OK
;
3813 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
3814 tmp
= OPENSSL_malloc(len
);
3817 return SSL_TLSEXT_ERR_ALERT_FATAL
;
3820 os_memcpy(tmp
, resp
, len
);
3822 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
3824 return SSL_TLSEXT_ERR_OK
;
3827 #endif /* HAVE_OCSP */
3830 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
3831 const struct tls_connection_params
*params
)
3833 struct tls_data
*data
= tls_ctx
;
3837 const char *key_id
= params
->key_id
;
3838 const char *cert_id
= params
->cert_id
;
3839 const char *ca_cert_id
= params
->ca_cert_id
;
3840 const char *engine_id
= params
->engine
? params
->engine_id
: NULL
;
3845 if (params
->flags
& TLS_CONN_REQUIRE_OCSP_ALL
) {
3846 wpa_printf(MSG_INFO
,
3847 "OpenSSL: ocsp=3 not supported");
3852 * If the engine isn't explicitly configured, and any of the
3853 * cert/key fields are actually PKCS#11 URIs, then automatically
3854 * use the PKCS#11 ENGINE.
3856 if (!engine_id
|| os_strcmp(engine_id
, "pkcs11") == 0)
3859 if (!key_id
&& params
->private_key
&& can_pkcs11
&&
3860 os_strncmp(params
->private_key
, "pkcs11:", 7) == 0) {
3862 key_id
= params
->private_key
;
3865 if (!cert_id
&& params
->client_cert
&& can_pkcs11
&&
3866 os_strncmp(params
->client_cert
, "pkcs11:", 7) == 0) {
3868 cert_id
= params
->client_cert
;
3871 if (!ca_cert_id
&& params
->ca_cert
&& can_pkcs11
&&
3872 os_strncmp(params
->ca_cert
, "pkcs11:", 7) == 0) {
3874 ca_cert_id
= params
->ca_cert
;
3877 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3878 if (can_pkcs11
== 2 && !engine_id
)
3879 engine_id
= "pkcs11";
3881 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3882 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3883 if (params
->flags
& TLS_CONN_EAP_FAST
) {
3884 wpa_printf(MSG_DEBUG
,
3885 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3886 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
3887 tls_show_errors(MSG_INFO
, __func__
,
3888 "Failed to set TLSv1_method() for EAP-FAST");
3893 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3895 while ((err
= ERR_get_error())) {
3896 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3897 __func__
, ERR_error_string(err
, NULL
));
3901 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
3902 ret
= tls_engine_init(conn
, engine_id
, params
->pin
,
3903 key_id
, cert_id
, ca_cert_id
);
3907 if (tls_connection_set_subject_match(conn
,
3908 params
->subject_match
,
3909 params
->altsubject_match
,
3910 params
->suffix_match
,
3911 params
->domain_match
))
3914 if (engine_id
&& ca_cert_id
) {
3915 if (tls_connection_engine_ca_cert(data
, conn
, ca_cert_id
))
3916 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3917 } else if (tls_connection_ca_cert(data
, conn
, params
->ca_cert
,
3918 params
->ca_cert_blob
,
3919 params
->ca_cert_blob_len
,
3923 if (engine_id
&& cert_id
) {
3924 if (tls_connection_engine_client_cert(conn
, cert_id
))
3925 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3926 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
3927 params
->client_cert_blob
,
3928 params
->client_cert_blob_len
))
3931 if (engine_id
&& key_id
) {
3932 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
3933 if (tls_connection_engine_private_key(conn
))
3934 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3935 } else if (tls_connection_private_key(data
, conn
,
3936 params
->private_key
,
3937 params
->private_key_passwd
,
3938 params
->private_key_blob
,
3939 params
->private_key_blob_len
)) {
3940 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
3941 params
->private_key
);
3945 if (tls_connection_dh(conn
, params
->dh_file
)) {
3946 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3951 if (params
->openssl_ciphers
&&
3952 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
3953 wpa_printf(MSG_INFO
,
3954 "OpenSSL: Failed to set cipher string '%s'",
3955 params
->openssl_ciphers
);
3959 tls_set_conn_flags(conn
->ssl
, params
->flags
);
3961 #ifdef OPENSSL_IS_BORINGSSL
3962 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3963 SSL_enable_ocsp_stapling(conn
->ssl
);
3965 #else /* OPENSSL_IS_BORINGSSL */
3967 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3968 SSL_CTX
*ssl_ctx
= data
->ssl
;
3969 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
3970 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
3971 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
3973 #else /* HAVE_OCSP */
3974 if (params
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3975 wpa_printf(MSG_INFO
,
3976 "OpenSSL: No OCSP support included - reject configuration");
3979 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3980 wpa_printf(MSG_DEBUG
,
3981 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
3983 #endif /* HAVE_OCSP */
3984 #endif /* OPENSSL_IS_BORINGSSL */
3986 conn
->flags
= params
->flags
;
3988 tls_get_errors(data
);
3994 int tls_global_set_params(void *tls_ctx
,
3995 const struct tls_connection_params
*params
)
3997 struct tls_data
*data
= tls_ctx
;
3998 SSL_CTX
*ssl_ctx
= data
->ssl
;
4001 while ((err
= ERR_get_error())) {
4002 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
4003 __func__
, ERR_error_string(err
, NULL
));
4006 if (tls_global_ca_cert(data
, params
->ca_cert
) ||
4007 tls_global_client_cert(data
, params
->client_cert
) ||
4008 tls_global_private_key(data
, params
->private_key
,
4009 params
->private_key_passwd
) ||
4010 tls_global_dh(data
, params
->dh_file
)) {
4011 wpa_printf(MSG_INFO
, "TLS: Failed to set global parameters");
4015 if (params
->openssl_ciphers
&&
4016 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
4017 wpa_printf(MSG_INFO
,
4018 "OpenSSL: Failed to set cipher string '%s'",
4019 params
->openssl_ciphers
);
4023 #ifdef SSL_OP_NO_TICKET
4024 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
4025 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
4026 #ifdef SSL_CTX_clear_options
4028 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
4029 #endif /* SSL_clear_options */
4030 #endif /* SSL_OP_NO_TICKET */
4033 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
4034 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
4035 os_free(tls_global
->ocsp_stapling_response
);
4036 if (params
->ocsp_stapling_response
)
4037 tls_global
->ocsp_stapling_response
=
4038 os_strdup(params
->ocsp_stapling_response
);
4040 tls_global
->ocsp_stapling_response
= NULL
;
4041 #endif /* HAVE_OCSP */
4047 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4048 /* Pre-shared secred requires a patch to openssl, so this function is
4049 * commented out unless explicitly needed for EAP-FAST in order to be able to
4050 * build this file with unmodified openssl. */
4052 #if defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L
4053 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
4054 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
4055 const SSL_CIPHER
**cipher
, void *arg
)
4056 #else /* OPENSSL_IS_BORINGSSL */
4057 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
4058 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
4059 SSL_CIPHER
**cipher
, void *arg
)
4060 #endif /* OPENSSL_IS_BORINGSSL */
4062 struct tls_connection
*conn
= arg
;
4065 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
4066 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4069 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
4070 conn
->session_ticket
,
4071 conn
->session_ticket_len
,
4072 s
->s3
->client_random
,
4073 s
->s3
->server_random
, secret
);
4075 unsigned char client_random
[SSL3_RANDOM_SIZE
];
4076 unsigned char server_random
[SSL3_RANDOM_SIZE
];
4078 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4081 SSL_get_client_random(s
, client_random
, sizeof(client_random
));
4082 SSL_get_server_random(s
, server_random
, sizeof(server_random
));
4084 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
4085 conn
->session_ticket
,
4086 conn
->session_ticket_len
,
4088 server_random
, secret
);
4091 os_free(conn
->session_ticket
);
4092 conn
->session_ticket
= NULL
;
4097 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
4102 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
4105 struct tls_connection
*conn
= arg
;
4107 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4110 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
4112 os_free(conn
->session_ticket
);
4113 conn
->session_ticket
= NULL
;
4115 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
4116 "extension", data
, len
);
4118 conn
->session_ticket
= os_malloc(len
);
4119 if (conn
->session_ticket
== NULL
)
4122 os_memcpy(conn
->session_ticket
, data
, len
);
4123 conn
->session_ticket_len
= len
;
4127 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4130 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
4131 struct tls_connection
*conn
,
4132 tls_session_ticket_cb cb
,
4135 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4136 conn
->session_ticket_cb
= cb
;
4137 conn
->session_ticket_cb_ctx
= ctx
;
4140 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
4143 SSL_set_session_ticket_ext_cb(conn
->ssl
,
4144 tls_session_ticket_ext_cb
, conn
);
4146 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
4148 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
4152 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4154 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4158 int tls_get_library_version(char *buf
, size_t buf_len
)
4160 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
4161 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
4162 OPENSSL_VERSION_TEXT
,
4163 OpenSSL_version(OPENSSL_VERSION
));
4165 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
4166 OPENSSL_VERSION_TEXT
,
4167 SSLeay_version(SSLEAY_VERSION
));
4172 void tls_connection_set_success_data(struct tls_connection
*conn
,
4173 struct wpabuf
*data
)
4178 if (tls_ex_idx_session
< 0)
4180 sess
= SSL_get_session(conn
->ssl
);
4183 old
= SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
4185 wpa_printf(MSG_DEBUG
, "OpenSSL: Replacing old success data %p",
4189 if (SSL_SESSION_set_ex_data(sess
, tls_ex_idx_session
, data
) != 1)
4192 wpa_printf(MSG_DEBUG
, "OpenSSL: Stored success data %p", data
);
4193 conn
->success_data
= 1;
4197 wpa_printf(MSG_INFO
, "OpenSSL: Failed to store success data");
4202 void tls_connection_set_success_data_resumed(struct tls_connection
*conn
)
4204 wpa_printf(MSG_DEBUG
,
4205 "OpenSSL: Success data accepted for resumed session");
4206 conn
->success_data
= 1;
4210 const struct wpabuf
*
4211 tls_connection_get_success_data(struct tls_connection
*conn
)
4215 if (tls_ex_idx_session
< 0 ||
4216 !(sess
= SSL_get_session(conn
->ssl
)))
4218 return SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
4222 void tls_connection_remove_session(struct tls_connection
*conn
)
4226 sess
= SSL_get_session(conn
->ssl
);
4230 if (SSL_CTX_remove_session(conn
->ssl_ctx
, sess
) != 1)
4231 wpa_printf(MSG_DEBUG
,
4232 "OpenSSL: Session was not cached");
4234 wpa_printf(MSG_DEBUG
,
4235 "OpenSSL: Removed cached session to disable session resumption");