2 * SSL/TLS interface functions for OpenSSL
3 * Copyright (c) 2004-2013, 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 */
31 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
32 #define OPENSSL_d2i_TYPE const unsigned char **
34 #define OPENSSL_d2i_TYPE unsigned char **
37 #if defined(SSL_CTX_get_app_data) && defined(SSL_CTX_set_app_data)
38 #define OPENSSL_SUPPORTS_CTX_APP_DATA
41 #if OPENSSL_VERSION_NUMBER < 0x10000000L
42 /* ERR_remove_thread_state replaces ERR_remove_state and the latter is
43 * deprecated. However, OpenSSL 0.9.8 doesn't include
44 * ERR_remove_thread_state. */
45 #define ERR_remove_thread_state(tid) ERR_remove_state(0)
48 #if defined(OPENSSL_IS_BORINGSSL)
49 /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
50 typedef size_t stack_index_t
;
52 typedef int stack_index_t
;
55 #ifdef SSL_set_tlsext_status_type
56 #ifndef OPENSSL_NO_TLSEXT
58 #include <openssl/ocsp.h>
59 #endif /* OPENSSL_NO_TLSEXT */
60 #endif /* SSL_set_tlsext_status_type */
63 #include <openssl/pem.h>
64 #include <keystore/keystore_get.h>
66 static BIO
* BIO_from_keystore(const char *key
)
69 uint8_t *value
= NULL
;
70 int length
= keystore_get(key
, strlen(key
), &value
);
71 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
72 BIO_write(bio
, value
, length
);
78 static int tls_openssl_ref_count
= 0;
81 void (*event_cb
)(void *ctx
, enum tls_event ev
,
82 union tls_event_data
*data
);
85 char *ocsp_stapling_response
;
88 static struct tls_context
*tls_global
= NULL
;
91 struct tls_connection
{
92 struct tls_context
*context
;
94 BIO
*ssl_in
, *ssl_out
;
95 #ifndef OPENSSL_NO_ENGINE
96 ENGINE
*engine
; /* functional reference to the engine */
97 EVP_PKEY
*private_key
; /* the private key if using engine */
98 #endif /* OPENSSL_NO_ENGINE */
99 char *subject_match
, *altsubject_match
, *suffix_match
;
100 int read_alerts
, write_alerts
, failed
;
102 tls_session_ticket_cb session_ticket_cb
;
103 void *session_ticket_cb_ctx
;
105 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
107 size_t session_ticket_len
;
109 unsigned int ca_cert_verify
:1;
110 unsigned int cert_probe
:1;
111 unsigned int server_cert_only
:1;
112 unsigned int invalid_hb_used
:1;
114 u8 srv_cert_hash
[32];
120 X509
*peer_issuer_issuer
;
124 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
126 struct tls_context
*context
= os_zalloc(sizeof(*context
));
130 context
->event_cb
= conf
->event_cb
;
131 context
->cb_ctx
= conf
->cb_ctx
;
132 context
->cert_in_cb
= conf
->cert_in_cb
;
138 #ifdef CONFIG_NO_STDOUT_DEBUG
140 static void _tls_show_errors(void)
144 while ((err
= ERR_get_error())) {
145 /* Just ignore the errors, since stdout is disabled */
148 #define tls_show_errors(l, f, t) _tls_show_errors()
150 #else /* CONFIG_NO_STDOUT_DEBUG */
152 static void tls_show_errors(int level
, const char *func
, const char *txt
)
156 wpa_printf(level
, "OpenSSL: %s - %s %s",
157 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
159 while ((err
= ERR_get_error())) {
160 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
161 ERR_error_string(err
, NULL
));
165 #endif /* CONFIG_NO_STDOUT_DEBUG */
168 #ifdef CONFIG_NATIVE_WINDOWS
170 /* Windows CryptoAPI and access to certificate stores */
171 #include <wincrypt.h>
173 #ifdef __MINGW32_VERSION
175 * MinGW does not yet include all the needed definitions for CryptoAPI, so
176 * define here whatever extra is needed.
178 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
179 #define CERT_STORE_READONLY_FLAG 0x00008000
180 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
182 #endif /* __MINGW32_VERSION */
185 struct cryptoapi_rsa_data
{
186 const CERT_CONTEXT
*cert
;
187 HCRYPTPROV crypt_prov
;
189 BOOL free_crypt_prov
;
193 static void cryptoapi_error(const char *msg
)
195 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
196 msg
, (unsigned int) GetLastError());
200 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
201 unsigned char *to
, RSA
*rsa
, int padding
)
203 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
208 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
209 unsigned char *to
, RSA
*rsa
, int padding
)
211 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
216 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
217 unsigned char *to
, RSA
*rsa
, int padding
)
219 struct cryptoapi_rsa_data
*priv
=
220 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
222 DWORD hash_size
, len
, i
;
223 unsigned char *buf
= NULL
;
227 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
228 ERR_R_PASSED_NULL_PARAMETER
);
232 if (padding
!= RSA_PKCS1_PADDING
) {
233 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
234 RSA_R_UNKNOWN_PADDING_TYPE
);
238 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
239 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
241 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
242 RSA_R_INVALID_MESSAGE_LENGTH
);
246 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
248 cryptoapi_error("CryptCreateHash failed");
252 len
= sizeof(hash_size
);
253 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
255 cryptoapi_error("CryptGetHashParam failed");
259 if ((int) hash_size
!= flen
) {
260 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
261 (unsigned) hash_size
, flen
);
262 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
263 RSA_R_INVALID_MESSAGE_LENGTH
);
266 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
267 cryptoapi_error("CryptSetHashParam failed");
272 buf
= os_malloc(len
);
274 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
278 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
279 cryptoapi_error("CryptSignHash failed");
283 for (i
= 0; i
< len
; i
++)
284 to
[i
] = buf
[len
- i
- 1];
289 CryptDestroyHash(hash
);
295 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
296 unsigned char *to
, RSA
*rsa
, int padding
)
298 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
303 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
307 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
308 CryptReleaseContext(priv
->crypt_prov
, 0);
310 CertFreeCertificateContext(priv
->cert
);
315 static int cryptoapi_finish(RSA
*rsa
)
317 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
318 os_free((void *) rsa
->meth
);
324 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
327 const CERT_CONTEXT
*ret
= NULL
;
329 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
330 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
331 CERT_STORE_READONLY_FLAG
, L
"MY");
333 cryptoapi_error("Failed to open 'My system store'");
337 if (strncmp(name
, "cert://", 7) == 0) {
338 unsigned short wbuf
[255];
339 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
340 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
342 0, CERT_FIND_SUBJECT_STR
,
344 } else if (strncmp(name
, "hash://", 7) == 0) {
345 CRYPT_HASH_BLOB blob
;
347 const char *hash
= name
+ 7;
350 len
= os_strlen(hash
) / 2;
351 buf
= os_malloc(len
);
352 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
355 ret
= CertFindCertificateInStore(cs
,
364 CertCloseStore(cs
, 0);
370 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
373 RSA
*rsa
= NULL
, *pub_rsa
;
374 struct cryptoapi_rsa_data
*priv
;
375 RSA_METHOD
*rsa_meth
;
378 (strncmp(name
, "cert://", 7) != 0 &&
379 strncmp(name
, "hash://", 7) != 0))
382 priv
= os_zalloc(sizeof(*priv
));
383 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
384 if (priv
== NULL
|| rsa_meth
== NULL
) {
385 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
386 "for CryptoAPI RSA method");
392 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
393 if (priv
->cert
== NULL
) {
394 priv
->cert
= cryptoapi_find_cert(
395 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
397 if (priv
->cert
== NULL
) {
398 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
403 cert
= d2i_X509(NULL
, (OPENSSL_d2i_TYPE
) &priv
->cert
->pbCertEncoded
,
404 priv
->cert
->cbCertEncoded
);
406 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
411 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
412 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
413 NULL
, &priv
->crypt_prov
,
415 &priv
->free_crypt_prov
)) {
416 cryptoapi_error("Failed to acquire a private key for the "
421 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
422 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
423 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
424 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
425 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
426 rsa_meth
->finish
= cryptoapi_finish
;
427 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
428 rsa_meth
->app_data
= (char *) priv
;
432 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
433 ERR_R_MALLOC_FAILURE
);
437 if (!SSL_use_certificate(ssl
, cert
)) {
442 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
446 rsa
->n
= BN_dup(pub_rsa
->n
);
447 rsa
->e
= BN_dup(pub_rsa
->e
);
448 if (!RSA_set_method(rsa
, rsa_meth
))
451 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
464 cryptoapi_free_data(priv
);
470 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
473 PCCERT_CONTEXT ctx
= NULL
;
481 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
486 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
489 wsprintf(wstore
, L
"%S", store
);
490 cs
= CertOpenSystemStore(0, wstore
);
493 cs
= CertOpenSystemStore(0, store
);
496 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
497 "'%s': error=%d", __func__
, store
,
498 (int) GetLastError());
502 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
503 cert
= d2i_X509(NULL
, (OPENSSL_d2i_TYPE
) &ctx
->pbCertEncoded
,
506 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
507 "X509 DER encoding for CA cert");
511 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
513 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
514 "system certificate store: subject='%s'", buf
);
516 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
517 tls_show_errors(MSG_WARNING
, __func__
,
518 "Failed to add ca_cert to OpenSSL "
519 "certificate store");
525 if (!CertCloseStore(cs
, 0)) {
526 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
527 "'%s': error=%d", __func__
, name
+ 13,
528 (int) GetLastError());
535 #else /* CONFIG_NATIVE_WINDOWS */
537 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
542 #endif /* CONFIG_NATIVE_WINDOWS */
545 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
550 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
551 w
= where
& ~SSL_ST_MASK
;
552 if (w
& SSL_ST_CONNECT
)
554 else if (w
& SSL_ST_ACCEPT
)
559 if (where
& SSL_CB_LOOP
) {
560 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
561 str
, SSL_state_string_long(ssl
));
562 } else if (where
& SSL_CB_ALERT
) {
563 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
564 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
565 where
& SSL_CB_READ
?
566 "read (remote end reported an error)" :
567 "write (local SSL3 detected an error)",
568 SSL_alert_type_string_long(ret
),
569 SSL_alert_desc_string_long(ret
));
570 if ((ret
>> 8) == SSL3_AL_FATAL
) {
571 if (where
& SSL_CB_READ
)
574 conn
->write_alerts
++;
576 if (conn
->context
->event_cb
!= NULL
) {
577 union tls_event_data ev
;
578 struct tls_context
*context
= conn
->context
;
579 os_memset(&ev
, 0, sizeof(ev
));
580 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
581 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
582 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
583 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
585 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
586 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
587 str
, ret
== 0 ? "failed" : "error",
588 SSL_state_string_long(ssl
));
593 #ifndef OPENSSL_NO_ENGINE
595 * tls_engine_load_dynamic_generic - load any openssl engine
596 * @pre: an array of commands and values that load an engine initialized
597 * in the engine specific function
598 * @post: an array of commands and values that initialize an already loaded
599 * engine (or %NULL if not required)
600 * @id: the engine id of the engine to load (only required if post is not %NULL
602 * This function is a generic function that loads any openssl engine.
604 * Returns: 0 on success, -1 on failure
606 static int tls_engine_load_dynamic_generic(const char *pre
[],
607 const char *post
[], const char *id
)
610 const char *dynamic_id
= "dynamic";
612 engine
= ENGINE_by_id(id
);
615 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
621 engine
= ENGINE_by_id(dynamic_id
);
622 if (engine
== NULL
) {
623 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
625 ERR_error_string(ERR_get_error(), NULL
));
629 /* Perform the pre commands. This will load the engine. */
630 while (pre
&& pre
[0]) {
631 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
632 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
633 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
634 "%s %s [%s]", pre
[0], pre
[1],
635 ERR_error_string(ERR_get_error(), NULL
));
643 * Free the reference to the "dynamic" engine. The loaded engine can
644 * now be looked up using ENGINE_by_id().
648 engine
= ENGINE_by_id(id
);
649 if (engine
== NULL
) {
650 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
651 id
, ERR_error_string(ERR_get_error(), NULL
));
655 while (post
&& post
[0]) {
656 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
657 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
658 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
659 " %s %s [%s]", post
[0], post
[1],
660 ERR_error_string(ERR_get_error(), NULL
));
661 ENGINE_remove(engine
);
674 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
675 * @pkcs11_so_path: pksc11_so_path from the configuration
676 * @pcks11_module_path: pkcs11_module_path from the configuration
678 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
679 const char *pkcs11_module_path
)
681 char *engine_id
= "pkcs11";
682 const char *pre_cmd
[] = {
683 "SO_PATH", NULL
/* pkcs11_so_path */,
684 "ID", NULL
/* engine_id */,
686 /* "NO_VCHECK", "1", */
690 const char *post_cmd
[] = {
691 "MODULE_PATH", NULL
/* pkcs11_module_path */,
698 pre_cmd
[1] = pkcs11_so_path
;
699 pre_cmd
[3] = engine_id
;
700 if (pkcs11_module_path
)
701 post_cmd
[1] = pkcs11_module_path
;
705 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
708 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
713 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
714 * @opensc_so_path: opensc_so_path from the configuration
716 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
718 char *engine_id
= "opensc";
719 const char *pre_cmd
[] = {
720 "SO_PATH", NULL
/* opensc_so_path */,
721 "ID", NULL
/* engine_id */,
730 pre_cmd
[1] = opensc_so_path
;
731 pre_cmd
[3] = engine_id
;
733 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
736 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
738 #endif /* OPENSSL_NO_ENGINE */
741 void * tls_init(const struct tls_config
*conf
)
744 struct tls_context
*context
;
747 if (tls_openssl_ref_count
== 0) {
748 tls_global
= context
= tls_context_new(conf
);
753 if (conf
&& conf
->fips_mode
) {
754 if (!FIPS_mode_set(1)) {
755 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
757 ERR_load_crypto_strings();
758 ERR_print_errors_fp(stderr
);
763 wpa_printf(MSG_INFO
, "Running in FIPS mode");
765 #else /* OPENSSL_FIPS */
766 if (conf
&& conf
->fips_mode
) {
767 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
773 #endif /* OPENSSL_FIPS */
774 #endif /* CONFIG_FIPS */
775 SSL_load_error_strings();
777 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256)
778 EVP_add_digest(EVP_sha256());
779 #endif /* OPENSSL_NO_SHA256 */
780 /* TODO: if /dev/urandom is available, PRNG is seeded
781 * automatically. If this is not the case, random data should
785 #ifndef OPENSSL_NO_RC2
787 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
788 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
789 * versions, but it looks like OpenSSL 1.0.0 does not do that
792 EVP_add_cipher(EVP_rc2_40_cbc());
793 #endif /* OPENSSL_NO_RC2 */
795 #endif /* PKCS12_FUNCS */
797 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
798 /* Newer OpenSSL can store app-data per-SSL */
799 context
= tls_context_new(conf
);
802 #else /* OPENSSL_SUPPORTS_CTX_APP_DATA */
803 context
= tls_global
;
804 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
806 tls_openssl_ref_count
++;
808 ssl
= SSL_CTX_new(SSLv23_method());
810 tls_openssl_ref_count
--;
811 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
812 if (context
!= tls_global
)
814 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
815 if (tls_openssl_ref_count
== 0) {
822 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
823 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
825 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
826 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
827 SSL_CTX_set_app_data(ssl
, context
);
828 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
830 #ifndef OPENSSL_NO_ENGINE
832 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
833 conf
->pkcs11_module_path
)) {
834 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
835 ERR_load_ENGINE_strings();
836 ENGINE_load_dynamic();
838 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
839 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
840 conf
->pkcs11_module_path
)) {
845 #endif /* OPENSSL_NO_ENGINE */
847 if (conf
&& conf
->openssl_ciphers
)
848 ciphers
= conf
->openssl_ciphers
;
850 ciphers
= "DEFAULT:!EXP:!LOW";
851 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
852 wpa_printf(MSG_ERROR
,
853 "OpenSSL: Failed to set cipher string '%s'",
863 void tls_deinit(void *ssl_ctx
)
865 SSL_CTX
*ssl
= ssl_ctx
;
866 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
867 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
868 if (context
!= tls_global
)
870 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
873 tls_openssl_ref_count
--;
874 if (tls_openssl_ref_count
== 0) {
875 #ifndef OPENSSL_NO_ENGINE
877 #endif /* OPENSSL_NO_ENGINE */
878 CRYPTO_cleanup_all_ex_data();
879 ERR_remove_thread_state(NULL
);
882 os_free(tls_global
->ocsp_stapling_response
);
883 tls_global
->ocsp_stapling_response
= NULL
;
890 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
891 const char *pin
, const char *key_id
,
892 const char *cert_id
, const char *ca_cert_id
)
894 #ifndef OPENSSL_NO_ENGINE
896 if (engine_id
== NULL
) {
897 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
902 wpa_printf(MSG_ERROR
, "ENGINE: Smartcard PIN not set");
909 ENGINE_load_dynamic();
911 conn
->engine
= ENGINE_by_id(engine_id
);
913 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
914 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
917 if (ENGINE_init(conn
->engine
) != 1) {
918 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
919 "(engine: %s) [%s]", engine_id
,
920 ERR_error_string(ERR_get_error(), NULL
));
923 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
926 if (ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
927 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
928 ERR_error_string(ERR_get_error(), NULL
));
933 /* load private key first in-case PIN is required for cert */
934 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
936 if (!conn
->private_key
) {
937 wpa_printf(MSG_ERROR
,
938 "ENGINE: cannot load private key with id '%s' [%s]",
940 ERR_error_string(ERR_get_error(), NULL
));
941 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
946 /* handle a certificate and/or CA certificate */
947 if (cert_id
|| ca_cert_id
) {
948 const char *cmd_name
= "LOAD_CERT_CTRL";
950 /* test if the engine supports a LOAD_CERT_CTRL */
951 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
952 0, (void *)cmd_name
, NULL
)) {
953 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
954 " loading certificates");
955 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
964 ENGINE_free(conn
->engine
);
968 if (conn
->private_key
) {
969 EVP_PKEY_free(conn
->private_key
);
970 conn
->private_key
= NULL
;
974 #else /* OPENSSL_NO_ENGINE */
976 #endif /* OPENSSL_NO_ENGINE */
980 static void tls_engine_deinit(struct tls_connection
*conn
)
982 #ifndef OPENSSL_NO_ENGINE
983 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
984 if (conn
->private_key
) {
985 EVP_PKEY_free(conn
->private_key
);
986 conn
->private_key
= NULL
;
989 ENGINE_finish(conn
->engine
);
992 #endif /* OPENSSL_NO_ENGINE */
996 int tls_get_errors(void *ssl_ctx
)
1001 while ((err
= ERR_get_error())) {
1002 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1003 ERR_error_string(err
, NULL
));
1011 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1012 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1014 struct tls_connection
*conn
= arg
;
1015 const u8
*pos
= buf
;
1017 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d",
1018 write_p
? "TX" : "RX", version
, content_type
);
1019 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1020 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1021 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1022 if (payload_len
+ 3 > len
) {
1023 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1024 conn
->invalid_hb_used
= 1;
1030 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1032 SSL_CTX
*ssl
= ssl_ctx
;
1033 struct tls_connection
*conn
;
1035 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
1036 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1037 #else /* OPENSSL_SUPPORTS_CTX_APP_DATA */
1038 struct tls_context
*context
= tls_global
;
1039 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
1041 conn
= os_zalloc(sizeof(*conn
));
1044 conn
->ssl
= SSL_new(ssl
);
1045 if (conn
->ssl
== NULL
) {
1046 tls_show_errors(MSG_INFO
, __func__
,
1047 "Failed to initialize new SSL connection");
1052 conn
->context
= context
;
1053 SSL_set_app_data(conn
->ssl
, conn
);
1054 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1055 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1056 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1057 SSL_OP_SINGLE_DH_USE
;
1058 #ifdef SSL_OP_NO_COMPRESSION
1059 options
|= SSL_OP_NO_COMPRESSION
;
1060 #endif /* SSL_OP_NO_COMPRESSION */
1061 SSL_set_options(conn
->ssl
, options
);
1063 conn
->ssl_in
= BIO_new(BIO_s_mem());
1064 if (!conn
->ssl_in
) {
1065 tls_show_errors(MSG_INFO
, __func__
,
1066 "Failed to create a new BIO for ssl_in");
1067 SSL_free(conn
->ssl
);
1072 conn
->ssl_out
= BIO_new(BIO_s_mem());
1073 if (!conn
->ssl_out
) {
1074 tls_show_errors(MSG_INFO
, __func__
,
1075 "Failed to create a new BIO for ssl_out");
1076 SSL_free(conn
->ssl
);
1077 BIO_free(conn
->ssl_in
);
1082 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1088 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1092 SSL_free(conn
->ssl
);
1093 tls_engine_deinit(conn
);
1094 os_free(conn
->subject_match
);
1095 os_free(conn
->altsubject_match
);
1096 os_free(conn
->suffix_match
);
1097 os_free(conn
->session_ticket
);
1102 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1104 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1108 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1113 /* Shutdown previous TLS connection without notifying the peer
1114 * because the connection was already terminated in practice
1115 * and "close notify" shutdown alert would confuse AS. */
1116 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1117 SSL_shutdown(conn
->ssl
);
1122 static int tls_match_altsubject_component(X509
*cert
, int type
,
1123 const char *value
, size_t len
)
1130 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1132 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1133 gen
= sk_GENERAL_NAME_value(ext
, i
);
1134 if (gen
->type
!= type
)
1136 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1137 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1145 static int tls_match_altsubject(X509
*cert
, const char *match
)
1148 const char *pos
, *end
;
1153 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1156 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1159 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1163 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1167 end
= os_strchr(pos
, ';');
1169 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1170 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1171 os_strncmp(end
+ 1, "URI:", 4) == 0)
1173 end
= os_strchr(end
+ 1, ';');
1178 len
= os_strlen(pos
);
1179 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1188 #ifndef CONFIG_NATIVE_WINDOWS
1189 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
)
1191 size_t i
, match_len
;
1193 /* Check for embedded nuls that could mess up suffix matching */
1194 for (i
= 0; i
< len
; i
++) {
1195 if (val
[i
] == '\0') {
1196 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1201 match_len
= os_strlen(match
);
1202 if (match_len
> len
)
1205 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1207 return 0; /* no match */
1209 if (match_len
== len
)
1210 return 1; /* exact match */
1212 if (val
[len
- match_len
- 1] == '.')
1213 return 1; /* full label match completes suffix match */
1215 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1218 #endif /* CONFIG_NATIVE_WINDOWS */
1221 static int tls_match_suffix(X509
*cert
, const char *match
)
1223 #ifdef CONFIG_NATIVE_WINDOWS
1224 /* wincrypt.h has conflicting X509_NAME definition */
1226 #else /* CONFIG_NATIVE_WINDOWS */
1234 wpa_printf(MSG_DEBUG
, "TLS: Match domain against suffix %s", match
);
1236 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1238 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1239 gen
= sk_GENERAL_NAME_value(ext
, j
);
1240 if (gen
->type
!= GEN_DNS
)
1243 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1244 gen
->d
.dNSName
->data
,
1245 gen
->d
.dNSName
->length
);
1246 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1247 gen
->d
.dNSName
->length
, match
) == 1) {
1248 wpa_printf(MSG_DEBUG
, "TLS: Suffix match in dNSName found");
1254 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1258 name
= X509_get_subject_name(cert
);
1264 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1267 e
= X509_NAME_get_entry(name
, i
);
1270 cn
= X509_NAME_ENTRY_get_data(e
);
1273 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1274 cn
->data
, cn
->length
);
1275 if (domain_suffix_match(cn
->data
, cn
->length
, match
) == 1) {
1276 wpa_printf(MSG_DEBUG
, "TLS: Suffix match in commonName found");
1281 wpa_printf(MSG_DEBUG
, "TLS: No CommonName suffix match found");
1283 #endif /* CONFIG_NATIVE_WINDOWS */
1287 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1290 case X509_V_ERR_CERT_REVOKED
:
1291 return TLS_FAIL_REVOKED
;
1292 case X509_V_ERR_CERT_NOT_YET_VALID
:
1293 case X509_V_ERR_CRL_NOT_YET_VALID
:
1294 return TLS_FAIL_NOT_YET_VALID
;
1295 case X509_V_ERR_CERT_HAS_EXPIRED
:
1296 case X509_V_ERR_CRL_HAS_EXPIRED
:
1297 return TLS_FAIL_EXPIRED
;
1298 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1299 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1300 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1301 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1302 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1303 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1304 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1305 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1306 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1307 case X509_V_ERR_INVALID_CA
:
1308 return TLS_FAIL_UNTRUSTED
;
1309 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1310 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1311 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1312 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1313 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1314 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1315 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1316 case X509_V_ERR_CERT_UNTRUSTED
:
1317 case X509_V_ERR_CERT_REJECTED
:
1318 return TLS_FAIL_BAD_CERTIFICATE
;
1320 return TLS_FAIL_UNSPECIFIED
;
1325 static struct wpabuf
* get_x509_cert(X509
*cert
)
1330 int cert_len
= i2d_X509(cert
, NULL
);
1334 buf
= wpabuf_alloc(cert_len
);
1338 tmp
= wpabuf_put(buf
, cert_len
);
1339 i2d_X509(cert
, &tmp
);
1344 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1345 X509
*err_cert
, int err
, int depth
,
1346 const char *subject
, const char *err_str
,
1347 enum tls_fail_reason reason
)
1349 union tls_event_data ev
;
1350 struct wpabuf
*cert
= NULL
;
1351 struct tls_context
*context
= conn
->context
;
1353 if (context
->event_cb
== NULL
)
1356 cert
= get_x509_cert(err_cert
);
1357 os_memset(&ev
, 0, sizeof(ev
));
1358 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1359 reason
: openssl_tls_fail_reason(err
);
1360 ev
.cert_fail
.depth
= depth
;
1361 ev
.cert_fail
.subject
= subject
;
1362 ev
.cert_fail
.reason_txt
= err_str
;
1363 ev
.cert_fail
.cert
= cert
;
1364 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1369 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1370 X509
*err_cert
, int depth
,
1371 const char *subject
)
1373 struct wpabuf
*cert
= NULL
;
1374 union tls_event_data ev
;
1375 struct tls_context
*context
= conn
->context
;
1376 #ifdef CONFIG_SHA256
1378 #endif /* CONFIG_SHA256 */
1380 if (context
->event_cb
== NULL
)
1383 os_memset(&ev
, 0, sizeof(ev
));
1384 if (conn
->cert_probe
|| context
->cert_in_cb
) {
1385 cert
= get_x509_cert(err_cert
);
1386 ev
.peer_cert
.cert
= cert
;
1388 #ifdef CONFIG_SHA256
1392 addr
[0] = wpabuf_head(cert
);
1393 len
[0] = wpabuf_len(cert
);
1394 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1395 ev
.peer_cert
.hash
= hash
;
1396 ev
.peer_cert
.hash_len
= sizeof(hash
);
1399 #endif /* CONFIG_SHA256 */
1400 ev
.peer_cert
.depth
= depth
;
1401 ev
.peer_cert
.subject
= subject
;
1402 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1407 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1413 struct tls_connection
*conn
;
1414 struct tls_context
*context
;
1415 char *match
, *altmatch
, *suffix_match
;
1416 const char *err_str
;
1418 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1422 err
= X509_STORE_CTX_get_error(x509_ctx
);
1423 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1424 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1425 SSL_get_ex_data_X509_STORE_CTX_idx());
1426 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1428 conn
= SSL_get_app_data(ssl
);
1433 conn
->peer_cert
= err_cert
;
1434 else if (depth
== 1)
1435 conn
->peer_issuer
= err_cert
;
1436 else if (depth
== 2)
1437 conn
->peer_issuer_issuer
= err_cert
;
1439 context
= conn
->context
;
1440 match
= conn
->subject_match
;
1441 altmatch
= conn
->altsubject_match
;
1442 suffix_match
= conn
->suffix_match
;
1444 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1446 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1448 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1449 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1450 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1451 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1456 err_str
= X509_verify_cert_error_string(err
);
1458 #ifdef CONFIG_SHA256
1459 if (preverify_ok
&& depth
== 0 && conn
->server_cert_only
) {
1460 struct wpabuf
*cert
;
1461 cert
= get_x509_cert(err_cert
);
1463 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1464 "server certificate data");
1470 addr
[0] = wpabuf_head(cert
);
1471 len
[0] = wpabuf_len(cert
);
1472 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1473 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1474 err_str
= "Server certificate mismatch";
1475 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1481 #endif /* CONFIG_SHA256 */
1483 if (!preverify_ok
) {
1484 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1485 " error %d (%s) depth %d for '%s'", err
, err_str
,
1487 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1488 err_str
, TLS_FAIL_UNSPECIFIED
);
1489 return preverify_ok
;
1492 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1493 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1494 preverify_ok
, err
, err_str
,
1495 conn
->ca_cert_verify
, depth
, buf
);
1496 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1497 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1498 "match with '%s'", buf
, match
);
1500 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1502 TLS_FAIL_SUBJECT_MISMATCH
);
1503 } else if (depth
== 0 && altmatch
&&
1504 !tls_match_altsubject(err_cert
, altmatch
)) {
1505 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1506 "'%s' not found", altmatch
);
1508 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1509 "AltSubject mismatch",
1510 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1511 } else if (depth
== 0 && suffix_match
&&
1512 !tls_match_suffix(err_cert
, suffix_match
)) {
1513 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1516 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1517 "Domain suffix mismatch",
1518 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1520 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1522 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1523 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1524 "on probe-only run");
1526 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1527 "Server certificate chain probe",
1528 TLS_FAIL_SERVER_CHAIN_PROBE
);
1531 if (preverify_ok
&& context
->event_cb
!= NULL
)
1532 context
->event_cb(context
->cb_ctx
,
1533 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1535 return preverify_ok
;
1539 #ifndef OPENSSL_NO_STDIO
1540 static int tls_load_ca_der(void *_ssl_ctx
, const char *ca_cert
)
1542 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1543 X509_LOOKUP
*lookup
;
1546 lookup
= X509_STORE_add_lookup(ssl_ctx
->cert_store
,
1547 X509_LOOKUP_file());
1548 if (lookup
== NULL
) {
1549 tls_show_errors(MSG_WARNING
, __func__
,
1550 "Failed add lookup for X509 store");
1554 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1555 unsigned long err
= ERR_peek_error();
1556 tls_show_errors(MSG_WARNING
, __func__
,
1557 "Failed load CA in DER format");
1558 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1559 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1560 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1561 "cert already in hash table error",
1569 #endif /* OPENSSL_NO_STDIO */
1572 static int tls_connection_ca_cert(void *_ssl_ctx
, struct tls_connection
*conn
,
1573 const char *ca_cert
, const u8
*ca_cert_blob
,
1574 size_t ca_cert_blob_len
, const char *ca_path
)
1576 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1579 * Remove previously configured trusted CA certificates before adding
1582 X509_STORE_free(ssl_ctx
->cert_store
);
1583 ssl_ctx
->cert_store
= X509_STORE_new();
1584 if (ssl_ctx
->cert_store
== NULL
) {
1585 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
1586 "certificate store", __func__
);
1590 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1591 conn
->ca_cert_verify
= 1;
1593 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
1594 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
1596 conn
->cert_probe
= 1;
1597 conn
->ca_cert_verify
= 0;
1601 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
1602 #ifdef CONFIG_SHA256
1603 const char *pos
= ca_cert
+ 7;
1604 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
1605 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
1606 "hash value '%s'", ca_cert
);
1610 if (os_strlen(pos
) != 32 * 2) {
1611 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
1612 "hash length in ca_cert '%s'", ca_cert
);
1615 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
1616 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
1617 "value in ca_cert '%s'", ca_cert
);
1620 conn
->server_cert_only
= 1;
1621 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
1622 "certificate match");
1624 #else /* CONFIG_SHA256 */
1625 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
1626 "cannot validate server certificate hash");
1628 #endif /* CONFIG_SHA256 */
1632 X509
*cert
= d2i_X509(NULL
, (OPENSSL_d2i_TYPE
) &ca_cert_blob
,
1635 tls_show_errors(MSG_WARNING
, __func__
,
1636 "Failed to parse ca_cert_blob");
1640 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
1641 unsigned long err
= ERR_peek_error();
1642 tls_show_errors(MSG_WARNING
, __func__
,
1643 "Failed to add ca_cert_blob to "
1644 "certificate store");
1645 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1646 ERR_GET_REASON(err
) ==
1647 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1648 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1649 "cert already in hash table error",
1657 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
1658 "to certificate store", __func__
);
1663 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
1664 BIO
*bio
= BIO_from_keystore(&ca_cert
[11]);
1665 STACK_OF(X509_INFO
) *stack
= NULL
;
1669 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
1675 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
1676 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
1678 X509_STORE_add_cert(ssl_ctx
->cert_store
,
1682 X509_STORE_add_crl(ssl_ctx
->cert_store
,
1686 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
1687 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1690 #endif /* ANDROID */
1692 #ifdef CONFIG_NATIVE_WINDOWS
1693 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
1695 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
1696 "system certificate store");
1699 #endif /* CONFIG_NATIVE_WINDOWS */
1701 if (ca_cert
|| ca_path
) {
1702 #ifndef OPENSSL_NO_STDIO
1703 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
1705 tls_show_errors(MSG_WARNING
, __func__
,
1706 "Failed to load root certificates");
1708 tls_load_ca_der(ssl_ctx
, ca_cert
) == 0) {
1709 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
1710 "DER format CA certificate",
1715 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1716 "certificate(s) loaded");
1717 tls_get_errors(ssl_ctx
);
1719 #else /* OPENSSL_NO_STDIO */
1720 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
1723 #endif /* OPENSSL_NO_STDIO */
1725 /* No ca_cert configured - do not try to verify server
1727 conn
->ca_cert_verify
= 0;
1734 static int tls_global_ca_cert(SSL_CTX
*ssl_ctx
, const char *ca_cert
)
1737 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
1739 tls_show_errors(MSG_WARNING
, __func__
,
1740 "Failed to load root certificates");
1744 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1745 "certificate(s) loaded");
1747 #ifndef OPENSSL_NO_STDIO
1748 /* Add the same CAs to the client certificate requests */
1749 SSL_CTX_set_client_CA_list(ssl_ctx
,
1750 SSL_load_client_CA_file(ca_cert
));
1751 #endif /* OPENSSL_NO_STDIO */
1758 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
1763 X509_STORE
*cs
= SSL_CTX_get_cert_store(ssl_ctx
);
1765 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
1766 "certificate store when enabling "
1770 flags
= X509_V_FLAG_CRL_CHECK
;
1772 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
1773 X509_STORE_set_flags(cs
, flags
);
1779 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
1780 const char *subject_match
,
1781 const char *altsubject_match
,
1782 const char *suffix_match
)
1784 os_free(conn
->subject_match
);
1785 conn
->subject_match
= NULL
;
1786 if (subject_match
) {
1787 conn
->subject_match
= os_strdup(subject_match
);
1788 if (conn
->subject_match
== NULL
)
1792 os_free(conn
->altsubject_match
);
1793 conn
->altsubject_match
= NULL
;
1794 if (altsubject_match
) {
1795 conn
->altsubject_match
= os_strdup(altsubject_match
);
1796 if (conn
->altsubject_match
== NULL
)
1800 os_free(conn
->suffix_match
);
1801 conn
->suffix_match
= NULL
;
1803 conn
->suffix_match
= os_strdup(suffix_match
);
1804 if (conn
->suffix_match
== NULL
)
1812 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
1815 static int counter
= 0;
1821 conn
->ca_cert_verify
= 1;
1822 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
1823 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1824 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
1826 conn
->ca_cert_verify
= 0;
1827 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
1830 SSL_set_accept_state(conn
->ssl
);
1833 * Set session id context in order to avoid fatal errors when client
1834 * tries to resume a session. However, set the context to a unique
1835 * value in order to effectively disable session resumption for now
1836 * since not all areas of the server code are ready for it (e.g.,
1837 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1841 SSL_set_session_id_context(conn
->ssl
,
1842 (const unsigned char *) &counter
,
1849 static int tls_connection_client_cert(struct tls_connection
*conn
,
1850 const char *client_cert
,
1851 const u8
*client_cert_blob
,
1852 size_t client_cert_blob_len
)
1854 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
1857 if (client_cert_blob
&&
1858 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
1859 client_cert_blob_len
) == 1) {
1860 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
1863 } else if (client_cert_blob
) {
1864 tls_show_errors(MSG_DEBUG
, __func__
,
1865 "SSL_use_certificate_ASN1 failed");
1868 if (client_cert
== NULL
)
1872 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
1873 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
1877 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
1881 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
1887 #endif /* ANDROID */
1889 #ifndef OPENSSL_NO_STDIO
1890 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
1891 SSL_FILETYPE_ASN1
) == 1) {
1892 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
1897 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
1898 SSL_FILETYPE_PEM
) == 1) {
1900 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
1905 tls_show_errors(MSG_DEBUG
, __func__
,
1906 "SSL_use_certificate_file failed");
1907 #else /* OPENSSL_NO_STDIO */
1908 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
1909 #endif /* OPENSSL_NO_STDIO */
1915 static int tls_global_client_cert(SSL_CTX
*ssl_ctx
, const char *client_cert
)
1917 #ifndef OPENSSL_NO_STDIO
1918 if (client_cert
== NULL
)
1921 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
1922 SSL_FILETYPE_ASN1
) != 1 &&
1923 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
1924 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
1925 SSL_FILETYPE_PEM
) != 1) {
1926 tls_show_errors(MSG_INFO
, __func__
,
1927 "Failed to load client certificate");
1931 #else /* OPENSSL_NO_STDIO */
1932 if (client_cert
== NULL
)
1934 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
1936 #endif /* OPENSSL_NO_STDIO */
1940 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
1942 if (password
== NULL
) {
1945 os_strlcpy(buf
, (char *) password
, size
);
1946 return os_strlen(buf
);
1951 static int tls_parse_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, PKCS12
*p12
,
1956 STACK_OF(X509
) *certs
;
1963 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
1964 tls_show_errors(MSG_DEBUG
, __func__
,
1965 "Failed to parse PKCS12 file");
1969 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
1972 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
1974 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
1975 "subject='%s'", buf
);
1977 if (SSL_use_certificate(ssl
, cert
) != 1)
1980 if (SSL_CTX_use_certificate(ssl_ctx
, cert
) != 1)
1987 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
1989 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
1992 if (SSL_CTX_use_PrivateKey(ssl_ctx
, pkey
) != 1)
1995 EVP_PKEY_free(pkey
);
1999 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2000 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2002 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2003 " from PKCS12: subject='%s'", buf
);
2005 * There is no SSL equivalent for the chain cert - so
2006 * always add it to the context...
2008 if (SSL_CTX_add_extra_chain_cert(ssl_ctx
, cert
) != 1) {
2013 sk_X509_free(certs
);
2019 tls_get_errors(ssl_ctx
);
2023 #endif /* PKCS12_FUNCS */
2026 static int tls_read_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *private_key
,
2033 f
= fopen(private_key
, "rb");
2037 p12
= d2i_PKCS12_fp(f
, NULL
);
2041 tls_show_errors(MSG_INFO
, __func__
,
2042 "Failed to use PKCS#12 file");
2046 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2048 #else /* PKCS12_FUNCS */
2049 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2052 #endif /* PKCS12_FUNCS */
2056 static int tls_read_pkcs12_blob(SSL_CTX
*ssl_ctx
, SSL
*ssl
,
2057 const u8
*blob
, size_t len
, const char *passwd
)
2062 p12
= d2i_PKCS12(NULL
, (OPENSSL_d2i_TYPE
) &blob
, len
);
2064 tls_show_errors(MSG_INFO
, __func__
,
2065 "Failed to use PKCS#12 blob");
2069 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2071 #else /* PKCS12_FUNCS */
2072 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2075 #endif /* PKCS12_FUNCS */
2079 #ifndef OPENSSL_NO_ENGINE
2080 static int tls_engine_get_cert(struct tls_connection
*conn
,
2081 const char *cert_id
,
2084 /* this runs after the private key is loaded so no PIN is required */
2086 const char *cert_id
;
2089 params
.cert_id
= cert_id
;
2092 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2093 0, ¶ms
, NULL
, 1)) {
2094 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2095 " '%s' [%s]", cert_id
,
2096 ERR_error_string(ERR_get_error(), NULL
));
2097 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2100 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2102 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2104 *cert
= params
.cert
;
2107 #endif /* OPENSSL_NO_ENGINE */
2110 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2111 const char *cert_id
)
2113 #ifndef OPENSSL_NO_ENGINE
2116 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2119 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2120 tls_show_errors(MSG_ERROR
, __func__
,
2121 "SSL_use_certificate failed");
2126 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2130 #else /* OPENSSL_NO_ENGINE */
2132 #endif /* OPENSSL_NO_ENGINE */
2136 static int tls_connection_engine_ca_cert(void *_ssl_ctx
,
2137 struct tls_connection
*conn
,
2138 const char *ca_cert_id
)
2140 #ifndef OPENSSL_NO_ENGINE
2142 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2144 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2147 /* start off the same as tls_connection_ca_cert */
2148 X509_STORE_free(ssl_ctx
->cert_store
);
2149 ssl_ctx
->cert_store
= X509_STORE_new();
2150 if (ssl_ctx
->cert_store
== NULL
) {
2151 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2152 "certificate store", __func__
);
2156 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
2157 unsigned long err
= ERR_peek_error();
2158 tls_show_errors(MSG_WARNING
, __func__
,
2159 "Failed to add CA certificate from engine "
2160 "to certificate store");
2161 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2162 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2163 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2164 " already in hash table error",
2172 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2173 "to certificate store", __func__
);
2174 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2175 conn
->ca_cert_verify
= 1;
2179 #else /* OPENSSL_NO_ENGINE */
2181 #endif /* OPENSSL_NO_ENGINE */
2185 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2187 #ifndef OPENSSL_NO_ENGINE
2188 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2189 tls_show_errors(MSG_ERROR
, __func__
,
2190 "ENGINE: cannot use private key for TLS");
2193 if (!SSL_check_private_key(conn
->ssl
)) {
2194 tls_show_errors(MSG_INFO
, __func__
,
2195 "Private key failed verification");
2199 #else /* OPENSSL_NO_ENGINE */
2200 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2201 "engine support was not compiled in");
2203 #endif /* OPENSSL_NO_ENGINE */
2207 static int tls_connection_private_key(void *_ssl_ctx
,
2208 struct tls_connection
*conn
,
2209 const char *private_key
,
2210 const char *private_key_passwd
,
2211 const u8
*private_key_blob
,
2212 size_t private_key_blob_len
)
2214 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2218 if (private_key
== NULL
&& private_key_blob
== NULL
)
2221 if (private_key_passwd
) {
2222 passwd
= os_strdup(private_key_passwd
);
2228 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2229 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2232 while (private_key_blob
) {
2233 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2234 (u8
*) private_key_blob
,
2235 private_key_blob_len
) == 1) {
2236 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2237 "ASN1(EVP_PKEY_RSA) --> OK");
2242 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2243 (u8
*) private_key_blob
,
2244 private_key_blob_len
) == 1) {
2245 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2246 "ASN1(EVP_PKEY_DSA) --> OK");
2251 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2252 (u8
*) private_key_blob
,
2253 private_key_blob_len
) == 1) {
2254 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2255 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2260 if (tls_read_pkcs12_blob(ssl_ctx
, conn
->ssl
, private_key_blob
,
2261 private_key_blob_len
, passwd
) == 0) {
2262 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2271 while (!ok
&& private_key
) {
2272 #ifndef OPENSSL_NO_STDIO
2273 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2274 SSL_FILETYPE_ASN1
) == 1) {
2275 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2276 "SSL_use_PrivateKey_File (DER) --> OK");
2281 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2282 SSL_FILETYPE_PEM
) == 1) {
2283 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2284 "SSL_use_PrivateKey_File (PEM) --> OK");
2288 #else /* OPENSSL_NO_STDIO */
2289 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2291 #endif /* OPENSSL_NO_STDIO */
2293 if (tls_read_pkcs12(ssl_ctx
, conn
->ssl
, private_key
, passwd
)
2295 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2301 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2302 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2303 "access certificate store --> OK");
2312 tls_show_errors(MSG_INFO
, __func__
,
2313 "Failed to load private key");
2318 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2321 if (!SSL_check_private_key(conn
->ssl
)) {
2322 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
2327 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
2332 static int tls_global_private_key(SSL_CTX
*ssl_ctx
, const char *private_key
,
2333 const char *private_key_passwd
)
2337 if (private_key
== NULL
)
2340 if (private_key_passwd
) {
2341 passwd
= os_strdup(private_key_passwd
);
2347 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2348 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2350 #ifndef OPENSSL_NO_STDIO
2351 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2352 SSL_FILETYPE_ASN1
) != 1 &&
2353 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2354 SSL_FILETYPE_PEM
) != 1 &&
2355 #endif /* OPENSSL_NO_STDIO */
2356 tls_read_pkcs12(ssl_ctx
, NULL
, private_key
, passwd
)) {
2357 tls_show_errors(MSG_INFO
, __func__
,
2358 "Failed to load private key");
2365 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2367 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
2368 tls_show_errors(MSG_INFO
, __func__
,
2369 "Private key failed verification");
2377 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
2379 #ifdef OPENSSL_NO_DH
2380 if (dh_file
== NULL
)
2382 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2383 "dh_file specified");
2385 #else /* OPENSSL_NO_DH */
2389 /* TODO: add support for dh_blob */
2390 if (dh_file
== NULL
)
2395 bio
= BIO_new_file(dh_file
, "r");
2397 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2398 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2401 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2403 #ifndef OPENSSL_NO_DSA
2404 while (dh
== NULL
) {
2406 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2407 " trying to parse as DSA params", dh_file
,
2408 ERR_error_string(ERR_get_error(), NULL
));
2409 bio
= BIO_new_file(dh_file
, "r");
2412 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2415 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2416 "'%s': %s", dh_file
,
2417 ERR_error_string(ERR_get_error(), NULL
));
2421 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2422 dh
= DSA_dup_DH(dsa
);
2425 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2426 "params into DH params");
2431 #endif /* !OPENSSL_NO_DSA */
2433 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2438 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
2439 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2441 ERR_error_string(ERR_get_error(), NULL
));
2447 #endif /* OPENSSL_NO_DH */
2451 static int tls_global_dh(SSL_CTX
*ssl_ctx
, const char *dh_file
)
2453 #ifdef OPENSSL_NO_DH
2454 if (dh_file
== NULL
)
2456 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2457 "dh_file specified");
2459 #else /* OPENSSL_NO_DH */
2463 /* TODO: add support for dh_blob */
2464 if (dh_file
== NULL
)
2466 if (ssl_ctx
== NULL
)
2469 bio
= BIO_new_file(dh_file
, "r");
2471 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2472 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2475 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2477 #ifndef OPENSSL_NO_DSA
2478 while (dh
== NULL
) {
2480 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2481 " trying to parse as DSA params", dh_file
,
2482 ERR_error_string(ERR_get_error(), NULL
));
2483 bio
= BIO_new_file(dh_file
, "r");
2486 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2489 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2490 "'%s': %s", dh_file
,
2491 ERR_error_string(ERR_get_error(), NULL
));
2495 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2496 dh
= DSA_dup_DH(dsa
);
2499 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2500 "params into DH params");
2505 #endif /* !OPENSSL_NO_DSA */
2507 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2512 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
2513 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2515 ERR_error_string(ERR_get_error(), NULL
));
2521 #endif /* OPENSSL_NO_DH */
2525 int tls_connection_get_keys(void *ssl_ctx
, struct tls_connection
*conn
,
2526 struct tls_keys
*keys
)
2529 wpa_printf(MSG_ERROR
, "OpenSSL: TLS keys cannot be exported in FIPS "
2532 #else /* CONFIG_FIPS */
2535 if (conn
== NULL
|| keys
== NULL
)
2538 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
)
2541 os_memset(keys
, 0, sizeof(*keys
));
2542 keys
->master_key
= ssl
->session
->master_key
;
2543 keys
->master_key_len
= ssl
->session
->master_key_length
;
2544 keys
->client_random
= ssl
->s3
->client_random
;
2545 keys
->client_random_len
= SSL3_RANDOM_SIZE
;
2546 keys
->server_random
= ssl
->s3
->server_random
;
2547 keys
->server_random_len
= SSL3_RANDOM_SIZE
;
2550 #endif /* CONFIG_FIPS */
2554 int tls_connection_prf(void *tls_ctx
, struct tls_connection
*conn
,
2555 const char *label
, int server_random_first
,
2556 u8
*out
, size_t out_len
)
2558 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
2562 if (server_random_first
)
2565 if (SSL_export_keying_material(ssl
, out
, out_len
, label
,
2566 os_strlen(label
), NULL
, 0, 0) == 1) {
2567 wpa_printf(MSG_DEBUG
, "OpenSSL: Using internal PRF");
2575 static struct wpabuf
*
2576 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
2580 struct wpabuf
*out_data
;
2583 * Give TLS handshake data from the server (if available) to OpenSSL
2587 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
2589 tls_show_errors(MSG_INFO
, __func__
,
2590 "Handshake failed - BIO_write");
2594 /* Initiate TLS handshake or continue the existing handshake */
2596 res
= SSL_accept(conn
->ssl
);
2598 res
= SSL_connect(conn
->ssl
);
2600 int err
= SSL_get_error(conn
->ssl
, res
);
2601 if (err
== SSL_ERROR_WANT_READ
)
2602 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
2604 else if (err
== SSL_ERROR_WANT_WRITE
)
2605 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
2608 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
2613 /* Get the TLS handshake data to be sent to the server */
2614 res
= BIO_ctrl_pending(conn
->ssl_out
);
2615 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
2616 out_data
= wpabuf_alloc(res
);
2617 if (out_data
== NULL
) {
2618 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
2619 "handshake output (%d bytes)", res
);
2620 if (BIO_reset(conn
->ssl_out
) < 0) {
2621 tls_show_errors(MSG_INFO
, __func__
,
2622 "BIO_reset failed");
2626 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
2629 tls_show_errors(MSG_INFO
, __func__
,
2630 "Handshake failed - BIO_read");
2631 if (BIO_reset(conn
->ssl_out
) < 0) {
2632 tls_show_errors(MSG_INFO
, __func__
,
2633 "BIO_reset failed");
2635 wpabuf_free(out_data
);
2638 wpabuf_put(out_data
, res
);
2644 static struct wpabuf
*
2645 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
2647 struct wpabuf
*appl_data
;
2650 appl_data
= wpabuf_alloc(max_len
+ 100);
2651 if (appl_data
== NULL
)
2654 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
2655 wpabuf_size(appl_data
));
2657 int err
= SSL_get_error(conn
->ssl
, res
);
2658 if (err
== SSL_ERROR_WANT_READ
||
2659 err
== SSL_ERROR_WANT_WRITE
) {
2660 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
2663 tls_show_errors(MSG_INFO
, __func__
,
2664 "Failed to read possible "
2665 "Application Data");
2667 wpabuf_free(appl_data
);
2671 wpabuf_put(appl_data
, res
);
2672 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
2673 "message", appl_data
);
2679 static struct wpabuf
*
2680 openssl_connection_handshake(struct tls_connection
*conn
,
2681 const struct wpabuf
*in_data
,
2682 struct wpabuf
**appl_data
, int server
)
2684 struct wpabuf
*out_data
;
2689 out_data
= openssl_handshake(conn
, in_data
, server
);
2690 if (out_data
== NULL
)
2692 if (conn
->invalid_hb_used
) {
2693 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
2694 wpabuf_free(out_data
);
2698 if (SSL_is_init_finished(conn
->ssl
) && appl_data
&& in_data
)
2699 *appl_data
= openssl_get_appl_data(conn
, wpabuf_len(in_data
));
2701 if (conn
->invalid_hb_used
) {
2702 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
2704 wpabuf_free(*appl_data
);
2707 wpabuf_free(out_data
);
2716 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
2717 const struct wpabuf
*in_data
,
2718 struct wpabuf
**appl_data
)
2720 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
2724 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
2725 struct tls_connection
*conn
,
2726 const struct wpabuf
*in_data
,
2727 struct wpabuf
**appl_data
)
2729 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
2733 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
2734 struct tls_connection
*conn
,
2735 const struct wpabuf
*in_data
)
2743 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
2744 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
2745 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
2746 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
2749 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
2751 tls_show_errors(MSG_INFO
, __func__
,
2752 "Encryption failed - SSL_write");
2756 /* Read encrypted data to be sent to the server */
2757 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
2760 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
2762 tls_show_errors(MSG_INFO
, __func__
,
2763 "Encryption failed - BIO_read");
2767 wpabuf_put(buf
, res
);
2773 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
2774 struct tls_connection
*conn
,
2775 const struct wpabuf
*in_data
)
2780 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
2781 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
2782 wpabuf_len(in_data
));
2784 tls_show_errors(MSG_INFO
, __func__
,
2785 "Decryption failed - BIO_write");
2788 if (BIO_reset(conn
->ssl_out
) < 0) {
2789 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
2793 /* Read decrypted data for further processing */
2795 * Even though we try to disable TLS compression, it is possible that
2796 * this cannot be done with all TLS libraries. Add extra buffer space
2797 * to handle the possibility of the decrypted data being longer than
2800 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
2803 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
2805 tls_show_errors(MSG_INFO
, __func__
,
2806 "Decryption failed - SSL_read");
2810 wpabuf_put(buf
, res
);
2812 if (conn
->invalid_hb_used
) {
2813 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
2822 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
2824 return conn
? conn
->ssl
->hit
: 0;
2828 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
2831 char buf
[100], *pos
, *end
;
2835 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
2840 end
= pos
+ sizeof(buf
);
2843 while (*c
!= TLS_CIPHER_NONE
) {
2847 case TLS_CIPHER_RC4_SHA
:
2850 case TLS_CIPHER_AES128_SHA
:
2851 suite
= "AES128-SHA";
2853 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
2854 suite
= "DHE-RSA-AES128-SHA";
2856 case TLS_CIPHER_ANON_DH_AES128_SHA
:
2857 suite
= "ADH-AES128-SHA";
2860 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
2861 "cipher selection: %d", *c
);
2864 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
2865 if (os_snprintf_error(end
- pos
, ret
))
2872 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
2874 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
2875 tls_show_errors(MSG_INFO
, __func__
,
2876 "Cipher suite configuration failed");
2884 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
2885 char *buf
, size_t buflen
)
2888 if (conn
== NULL
|| conn
->ssl
== NULL
)
2891 name
= SSL_get_cipher(conn
->ssl
);
2895 os_strlcpy(buf
, name
, buflen
);
2900 int tls_connection_enable_workaround(void *ssl_ctx
,
2901 struct tls_connection
*conn
)
2903 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
2909 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2910 /* ClientHello TLS extensions require a patch to openssl, so this function is
2911 * commented out unless explicitly needed for EAP-FAST in order to be able to
2912 * build this file with unmodified openssl. */
2913 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
2914 int ext_type
, const u8
*data
,
2917 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
2920 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
2926 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2929 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
2933 return conn
->failed
;
2937 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
2941 return conn
->read_alerts
;
2945 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
2949 return conn
->write_alerts
;
2955 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
2957 #ifndef CONFIG_NO_STDOUT_DEBUG
2963 if (wpa_debug_level
> MSG_DEBUG
)
2966 out
= BIO_new(BIO_s_mem());
2970 OCSP_RESPONSE_print(out
, rsp
, 0);
2971 rlen
= BIO_ctrl_pending(out
);
2972 txt
= os_malloc(rlen
+ 1);
2978 res
= BIO_read(out
, txt
, rlen
);
2981 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
2985 #endif /* CONFIG_NO_STDOUT_DEBUG */
2989 static void debug_print_cert(X509
*cert
, const char *title
)
2991 #ifndef CONFIG_NO_STDOUT_DEBUG
2997 if (wpa_debug_level
> MSG_DEBUG
)
3000 out
= BIO_new(BIO_s_mem());
3004 X509_print(out
, cert
);
3005 rlen
= BIO_ctrl_pending(out
);
3006 txt
= os_malloc(rlen
+ 1);
3012 res
= BIO_read(out
, txt
, rlen
);
3015 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3020 #endif /* CONFIG_NO_STDOUT_DEBUG */
3024 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3026 struct tls_connection
*conn
= arg
;
3027 const unsigned char *p
;
3028 int len
, status
, reason
;
3030 OCSP_BASICRESP
*basic
;
3032 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3034 STACK_OF(X509
) *certs
= NULL
;
3036 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3038 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3039 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3042 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3044 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3046 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3050 ocsp_debug_print_resp(rsp
);
3052 status
= OCSP_response_status(rsp
);
3053 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3054 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3055 status
, OCSP_response_status_str(status
));
3059 basic
= OCSP_response_get1_basic(rsp
);
3061 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3065 store
= SSL_CTX_get_cert_store(s
->ctx
);
3066 if (conn
->peer_issuer
) {
3067 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3069 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3070 tls_show_errors(MSG_INFO
, __func__
,
3071 "OpenSSL: Could not add issuer to certificate store\n");
3073 certs
= sk_X509_new_null();
3076 cert
= X509_dup(conn
->peer_issuer
);
3077 if (cert
&& !sk_X509_push(certs
, cert
)) {
3080 "OpenSSL: Could not add issuer to OCSP responder trust store\n");
3082 sk_X509_free(certs
);
3085 if (conn
->peer_issuer_issuer
) {
3086 cert
= X509_dup(conn
->peer_issuer_issuer
);
3087 if (cert
&& !sk_X509_push(certs
, cert
)) {
3090 "OpenSSL: Could not add issuer to OCSP responder trust store\n");
3097 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3098 sk_X509_pop_free(certs
, X509_free
);
3100 tls_show_errors(MSG_INFO
, __func__
,
3101 "OpenSSL: OCSP response failed verification");
3102 OCSP_BASICRESP_free(basic
);
3103 OCSP_RESPONSE_free(rsp
);
3107 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3109 if (!conn
->peer_cert
) {
3110 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3111 OCSP_BASICRESP_free(basic
);
3112 OCSP_RESPONSE_free(rsp
);
3116 if (!conn
->peer_issuer
) {
3117 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3118 OCSP_BASICRESP_free(basic
);
3119 OCSP_RESPONSE_free(rsp
);
3123 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3125 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not create OCSP certificate identifier");
3126 OCSP_BASICRESP_free(basic
);
3127 OCSP_RESPONSE_free(rsp
);
3131 if (!OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3132 &this_update
, &next_update
)) {
3133 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
3134 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
3135 " (OCSP not required)");
3136 OCSP_BASICRESP_free(basic
);
3137 OCSP_RESPONSE_free(rsp
);
3138 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3141 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
3142 tls_show_errors(MSG_INFO
, __func__
,
3143 "OpenSSL: OCSP status times invalid");
3144 OCSP_BASICRESP_free(basic
);
3145 OCSP_RESPONSE_free(rsp
);
3149 OCSP_BASICRESP_free(basic
);
3150 OCSP_RESPONSE_free(rsp
);
3152 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
3153 OCSP_cert_status_str(status
));
3155 if (status
== V_OCSP_CERTSTATUS_GOOD
)
3157 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
3159 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3160 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
3163 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
3168 static int ocsp_status_cb(SSL
*s
, void *arg
)
3174 if (tls_global
->ocsp_stapling_response
== NULL
) {
3175 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
3176 return SSL_TLSEXT_ERR_OK
;
3179 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
3181 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
3182 /* TODO: Build OCSPResponse with responseStatus = internalError
3184 return SSL_TLSEXT_ERR_OK
;
3186 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
3187 tmp
= OPENSSL_malloc(len
);
3190 return SSL_TLSEXT_ERR_ALERT_FATAL
;
3193 os_memcpy(tmp
, resp
, len
);
3195 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
3197 return SSL_TLSEXT_ERR_OK
;
3200 #endif /* HAVE_OCSP */
3203 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
3204 const struct tls_connection_params
*params
)
3212 if (params
->flags
& TLS_CONN_EAP_FAST
) {
3213 wpa_printf(MSG_DEBUG
,
3214 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3215 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
3216 tls_show_errors(MSG_INFO
, __func__
,
3217 "Failed to set TLSv1_method() for EAP-FAST");
3222 while ((err
= ERR_get_error())) {
3223 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3224 __func__
, ERR_error_string(err
, NULL
));
3227 if (params
->engine
) {
3228 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
3229 ret
= tls_engine_init(conn
, params
->engine_id
, params
->pin
,
3230 params
->key_id
, params
->cert_id
,
3231 params
->ca_cert_id
);
3235 if (tls_connection_set_subject_match(conn
,
3236 params
->subject_match
,
3237 params
->altsubject_match
,
3238 params
->suffix_match
))
3241 if (params
->engine
&& params
->ca_cert_id
) {
3242 if (tls_connection_engine_ca_cert(tls_ctx
, conn
,
3243 params
->ca_cert_id
))
3244 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3245 } else if (tls_connection_ca_cert(tls_ctx
, conn
, params
->ca_cert
,
3246 params
->ca_cert_blob
,
3247 params
->ca_cert_blob_len
,
3251 if (params
->engine
&& params
->cert_id
) {
3252 if (tls_connection_engine_client_cert(conn
, params
->cert_id
))
3253 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3254 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
3255 params
->client_cert_blob
,
3256 params
->client_cert_blob_len
))
3259 if (params
->engine
&& params
->key_id
) {
3260 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
3261 if (tls_connection_engine_private_key(conn
))
3262 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3263 } else if (tls_connection_private_key(tls_ctx
, conn
,
3264 params
->private_key
,
3265 params
->private_key_passwd
,
3266 params
->private_key_blob
,
3267 params
->private_key_blob_len
)) {
3268 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
3269 params
->private_key
);
3273 if (tls_connection_dh(conn
, params
->dh_file
)) {
3274 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3279 if (params
->openssl_ciphers
&&
3280 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
3281 wpa_printf(MSG_INFO
,
3282 "OpenSSL: Failed to set cipher string '%s'",
3283 params
->openssl_ciphers
);
3287 #ifdef SSL_OP_NO_TICKET
3288 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3289 SSL_set_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3290 #ifdef SSL_clear_options
3292 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3293 #endif /* SSL_clear_options */
3294 #endif /* SSL_OP_NO_TICKET */
3296 #ifdef SSL_OP_NO_TLSv1_1
3297 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_1
)
3298 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3300 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3301 #endif /* SSL_OP_NO_TLSv1_1 */
3302 #ifdef SSL_OP_NO_TLSv1_2
3303 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_2
)
3304 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3306 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3307 #endif /* SSL_OP_NO_TLSv1_2 */
3310 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3311 SSL_CTX
*ssl_ctx
= tls_ctx
;
3312 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
3313 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
3314 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
3316 #endif /* HAVE_OCSP */
3318 conn
->flags
= params
->flags
;
3320 tls_get_errors(tls_ctx
);
3326 int tls_global_set_params(void *tls_ctx
,
3327 const struct tls_connection_params
*params
)
3329 SSL_CTX
*ssl_ctx
= tls_ctx
;
3332 while ((err
= ERR_get_error())) {
3333 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3334 __func__
, ERR_error_string(err
, NULL
));
3337 if (tls_global_ca_cert(ssl_ctx
, params
->ca_cert
))
3340 if (tls_global_client_cert(ssl_ctx
, params
->client_cert
))
3343 if (tls_global_private_key(ssl_ctx
, params
->private_key
,
3344 params
->private_key_passwd
))
3347 if (tls_global_dh(ssl_ctx
, params
->dh_file
)) {
3348 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3353 if (params
->openssl_ciphers
&&
3354 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
3355 wpa_printf(MSG_INFO
,
3356 "OpenSSL: Failed to set cipher string '%s'",
3357 params
->openssl_ciphers
);
3361 #ifdef SSL_OP_NO_TICKET
3362 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3363 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3364 #ifdef SSL_CTX_clear_options
3366 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3367 #endif /* SSL_clear_options */
3368 #endif /* SSL_OP_NO_TICKET */
3371 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
3372 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
3373 os_free(tls_global
->ocsp_stapling_response
);
3374 if (params
->ocsp_stapling_response
)
3375 tls_global
->ocsp_stapling_response
=
3376 os_strdup(params
->ocsp_stapling_response
);
3378 tls_global
->ocsp_stapling_response
= NULL
;
3379 #endif /* HAVE_OCSP */
3385 int tls_connection_get_keyblock_size(void *tls_ctx
,
3386 struct tls_connection
*conn
)
3388 const EVP_CIPHER
*c
;
3392 if (conn
== NULL
|| conn
->ssl
== NULL
||
3393 conn
->ssl
->enc_read_ctx
== NULL
||
3394 conn
->ssl
->enc_read_ctx
->cipher
== NULL
||
3395 conn
->ssl
->read_hash
== NULL
)
3398 c
= conn
->ssl
->enc_read_ctx
->cipher
;
3399 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
3400 h
= EVP_MD_CTX_md(conn
->ssl
->read_hash
);
3402 h
= conn
->ssl
->read_hash
;
3405 md_size
= EVP_MD_size(h
);
3406 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
3407 else if (conn
->ssl
->s3
)
3408 md_size
= conn
->ssl
->s3
->tmp
.new_mac_secret_size
;
3413 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3414 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
3415 EVP_CIPHER_iv_length(c
));
3416 return 2 * (EVP_CIPHER_key_length(c
) +
3418 EVP_CIPHER_iv_length(c
));
3422 unsigned int tls_capabilities(void *tls_ctx
)
3428 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3429 /* Pre-shared secred requires a patch to openssl, so this function is
3430 * commented out unless explicitly needed for EAP-FAST in order to be able to
3431 * build this file with unmodified openssl. */
3433 #ifdef OPENSSL_IS_BORINGSSL
3434 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3435 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3436 const SSL_CIPHER
**cipher
, void *arg
)
3437 #else /* OPENSSL_IS_BORINGSSL */
3438 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3439 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3440 SSL_CIPHER
**cipher
, void *arg
)
3441 #endif /* OPENSSL_IS_BORINGSSL */
3443 struct tls_connection
*conn
= arg
;
3446 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3449 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
3450 conn
->session_ticket
,
3451 conn
->session_ticket_len
,
3452 s
->s3
->client_random
,
3453 s
->s3
->server_random
, secret
);
3454 os_free(conn
->session_ticket
);
3455 conn
->session_ticket
= NULL
;
3460 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
3465 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
3468 struct tls_connection
*conn
= arg
;
3470 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3473 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
3475 os_free(conn
->session_ticket
);
3476 conn
->session_ticket
= NULL
;
3478 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
3479 "extension", data
, len
);
3481 conn
->session_ticket
= os_malloc(len
);
3482 if (conn
->session_ticket
== NULL
)
3485 os_memcpy(conn
->session_ticket
, data
, len
);
3486 conn
->session_ticket_len
= len
;
3490 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3493 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
3494 struct tls_connection
*conn
,
3495 tls_session_ticket_cb cb
,
3498 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3499 conn
->session_ticket_cb
= cb
;
3500 conn
->session_ticket_cb_ctx
= ctx
;
3503 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
3506 SSL_set_session_ticket_ext_cb(conn
->ssl
,
3507 tls_session_ticket_ext_cb
, conn
);
3509 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
3511 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
3515 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3517 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */