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 defined(SSL_CTX_get_app_data) && defined(SSL_CTX_set_app_data)
32 #define OPENSSL_SUPPORTS_CTX_APP_DATA
35 #if OPENSSL_VERSION_NUMBER < 0x10000000L
36 /* ERR_remove_thread_state replaces ERR_remove_state and the latter is
37 * deprecated. However, OpenSSL 0.9.8 doesn't include
38 * ERR_remove_thread_state. */
39 #define ERR_remove_thread_state(tid) ERR_remove_state(0)
42 #if defined(OPENSSL_IS_BORINGSSL)
43 /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
44 typedef size_t stack_index_t
;
46 typedef int stack_index_t
;
49 #ifdef SSL_set_tlsext_status_type
50 #ifndef OPENSSL_NO_TLSEXT
52 #include <openssl/ocsp.h>
53 #endif /* OPENSSL_NO_TLSEXT */
54 #endif /* SSL_set_tlsext_status_type */
57 #include <openssl/pem.h>
58 #include <keystore/keystore_get.h>
60 static BIO
* BIO_from_keystore(const char *key
)
63 uint8_t *value
= NULL
;
64 int length
= keystore_get(key
, strlen(key
), &value
);
65 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
66 BIO_write(bio
, value
, length
);
72 static int tls_openssl_ref_count
= 0;
75 void (*event_cb
)(void *ctx
, enum tls_event ev
,
76 union tls_event_data
*data
);
79 char *ocsp_stapling_response
;
82 static struct tls_context
*tls_global
= NULL
;
85 struct tls_connection
{
86 struct tls_context
*context
;
89 BIO
*ssl_in
, *ssl_out
;
90 #ifndef OPENSSL_NO_ENGINE
91 ENGINE
*engine
; /* functional reference to the engine */
92 EVP_PKEY
*private_key
; /* the private key if using engine */
93 #endif /* OPENSSL_NO_ENGINE */
94 char *subject_match
, *altsubject_match
, *suffix_match
, *domain_match
;
95 int read_alerts
, write_alerts
, failed
;
97 tls_session_ticket_cb session_ticket_cb
;
98 void *session_ticket_cb_ctx
;
100 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
102 size_t session_ticket_len
;
104 unsigned int ca_cert_verify
:1;
105 unsigned int cert_probe
:1;
106 unsigned int server_cert_only
:1;
107 unsigned int invalid_hb_used
:1;
109 u8 srv_cert_hash
[32];
115 X509
*peer_issuer_issuer
;
119 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
121 struct tls_context
*context
= os_zalloc(sizeof(*context
));
125 context
->event_cb
= conf
->event_cb
;
126 context
->cb_ctx
= conf
->cb_ctx
;
127 context
->cert_in_cb
= conf
->cert_in_cb
;
133 #ifdef CONFIG_NO_STDOUT_DEBUG
135 static void _tls_show_errors(void)
139 while ((err
= ERR_get_error())) {
140 /* Just ignore the errors, since stdout is disabled */
143 #define tls_show_errors(l, f, t) _tls_show_errors()
145 #else /* CONFIG_NO_STDOUT_DEBUG */
147 static void tls_show_errors(int level
, const char *func
, const char *txt
)
151 wpa_printf(level
, "OpenSSL: %s - %s %s",
152 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
154 while ((err
= ERR_get_error())) {
155 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
156 ERR_error_string(err
, NULL
));
160 #endif /* CONFIG_NO_STDOUT_DEBUG */
163 #ifdef CONFIG_NATIVE_WINDOWS
165 /* Windows CryptoAPI and access to certificate stores */
166 #include <wincrypt.h>
168 #ifdef __MINGW32_VERSION
170 * MinGW does not yet include all the needed definitions for CryptoAPI, so
171 * define here whatever extra is needed.
173 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
174 #define CERT_STORE_READONLY_FLAG 0x00008000
175 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
177 #endif /* __MINGW32_VERSION */
180 struct cryptoapi_rsa_data
{
181 const CERT_CONTEXT
*cert
;
182 HCRYPTPROV crypt_prov
;
184 BOOL free_crypt_prov
;
188 static void cryptoapi_error(const char *msg
)
190 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
191 msg
, (unsigned int) GetLastError());
195 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
196 unsigned char *to
, RSA
*rsa
, int padding
)
198 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
203 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
204 unsigned char *to
, RSA
*rsa
, int padding
)
206 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
211 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
212 unsigned char *to
, RSA
*rsa
, int padding
)
214 struct cryptoapi_rsa_data
*priv
=
215 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
217 DWORD hash_size
, len
, i
;
218 unsigned char *buf
= NULL
;
222 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
223 ERR_R_PASSED_NULL_PARAMETER
);
227 if (padding
!= RSA_PKCS1_PADDING
) {
228 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
229 RSA_R_UNKNOWN_PADDING_TYPE
);
233 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
234 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
236 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
237 RSA_R_INVALID_MESSAGE_LENGTH
);
241 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
243 cryptoapi_error("CryptCreateHash failed");
247 len
= sizeof(hash_size
);
248 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
250 cryptoapi_error("CryptGetHashParam failed");
254 if ((int) hash_size
!= flen
) {
255 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
256 (unsigned) hash_size
, flen
);
257 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
258 RSA_R_INVALID_MESSAGE_LENGTH
);
261 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
262 cryptoapi_error("CryptSetHashParam failed");
267 buf
= os_malloc(len
);
269 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
273 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
274 cryptoapi_error("CryptSignHash failed");
278 for (i
= 0; i
< len
; i
++)
279 to
[i
] = buf
[len
- i
- 1];
284 CryptDestroyHash(hash
);
290 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
291 unsigned char *to
, RSA
*rsa
, int padding
)
293 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
298 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
302 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
303 CryptReleaseContext(priv
->crypt_prov
, 0);
305 CertFreeCertificateContext(priv
->cert
);
310 static int cryptoapi_finish(RSA
*rsa
)
312 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
313 os_free((void *) rsa
->meth
);
319 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
322 const CERT_CONTEXT
*ret
= NULL
;
324 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
325 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
326 CERT_STORE_READONLY_FLAG
, L
"MY");
328 cryptoapi_error("Failed to open 'My system store'");
332 if (strncmp(name
, "cert://", 7) == 0) {
333 unsigned short wbuf
[255];
334 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
335 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
337 0, CERT_FIND_SUBJECT_STR
,
339 } else if (strncmp(name
, "hash://", 7) == 0) {
340 CRYPT_HASH_BLOB blob
;
342 const char *hash
= name
+ 7;
345 len
= os_strlen(hash
) / 2;
346 buf
= os_malloc(len
);
347 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
350 ret
= CertFindCertificateInStore(cs
,
359 CertCloseStore(cs
, 0);
365 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
368 RSA
*rsa
= NULL
, *pub_rsa
;
369 struct cryptoapi_rsa_data
*priv
;
370 RSA_METHOD
*rsa_meth
;
373 (strncmp(name
, "cert://", 7) != 0 &&
374 strncmp(name
, "hash://", 7) != 0))
377 priv
= os_zalloc(sizeof(*priv
));
378 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
379 if (priv
== NULL
|| rsa_meth
== NULL
) {
380 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
381 "for CryptoAPI RSA method");
387 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
388 if (priv
->cert
== NULL
) {
389 priv
->cert
= cryptoapi_find_cert(
390 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
392 if (priv
->cert
== NULL
) {
393 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
398 cert
= d2i_X509(NULL
,
399 (const unsigned char **) &priv
->cert
->pbCertEncoded
,
400 priv
->cert
->cbCertEncoded
);
402 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
407 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
408 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
409 NULL
, &priv
->crypt_prov
,
411 &priv
->free_crypt_prov
)) {
412 cryptoapi_error("Failed to acquire a private key for the "
417 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
418 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
419 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
420 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
421 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
422 rsa_meth
->finish
= cryptoapi_finish
;
423 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
424 rsa_meth
->app_data
= (char *) priv
;
428 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
429 ERR_R_MALLOC_FAILURE
);
433 if (!SSL_use_certificate(ssl
, cert
)) {
438 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
442 rsa
->n
= BN_dup(pub_rsa
->n
);
443 rsa
->e
= BN_dup(pub_rsa
->e
);
444 if (!RSA_set_method(rsa
, rsa_meth
))
447 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
460 cryptoapi_free_data(priv
);
466 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
469 PCCERT_CONTEXT ctx
= NULL
;
477 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
482 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
485 wsprintf(wstore
, L
"%S", store
);
486 cs
= CertOpenSystemStore(0, wstore
);
489 cs
= CertOpenSystemStore(0, store
);
492 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
493 "'%s': error=%d", __func__
, store
,
494 (int) GetLastError());
498 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
499 cert
= d2i_X509(NULL
,
500 (const unsigned char **) &ctx
->pbCertEncoded
,
503 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
504 "X509 DER encoding for CA cert");
508 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
510 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
511 "system certificate store: subject='%s'", buf
);
513 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
514 tls_show_errors(MSG_WARNING
, __func__
,
515 "Failed to add ca_cert to OpenSSL "
516 "certificate store");
522 if (!CertCloseStore(cs
, 0)) {
523 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
524 "'%s': error=%d", __func__
, name
+ 13,
525 (int) GetLastError());
532 #else /* CONFIG_NATIVE_WINDOWS */
534 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
539 #endif /* CONFIG_NATIVE_WINDOWS */
542 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
547 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
548 w
= where
& ~SSL_ST_MASK
;
549 if (w
& SSL_ST_CONNECT
)
551 else if (w
& SSL_ST_ACCEPT
)
556 if (where
& SSL_CB_LOOP
) {
557 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
558 str
, SSL_state_string_long(ssl
));
559 } else if (where
& SSL_CB_ALERT
) {
560 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
561 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
562 where
& SSL_CB_READ
?
563 "read (remote end reported an error)" :
564 "write (local SSL3 detected an error)",
565 SSL_alert_type_string_long(ret
),
566 SSL_alert_desc_string_long(ret
));
567 if ((ret
>> 8) == SSL3_AL_FATAL
) {
568 if (where
& SSL_CB_READ
)
571 conn
->write_alerts
++;
573 if (conn
->context
->event_cb
!= NULL
) {
574 union tls_event_data ev
;
575 struct tls_context
*context
= conn
->context
;
576 os_memset(&ev
, 0, sizeof(ev
));
577 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
578 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
579 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
580 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
582 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
583 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
584 str
, ret
== 0 ? "failed" : "error",
585 SSL_state_string_long(ssl
));
590 #ifndef OPENSSL_NO_ENGINE
592 * tls_engine_load_dynamic_generic - load any openssl engine
593 * @pre: an array of commands and values that load an engine initialized
594 * in the engine specific function
595 * @post: an array of commands and values that initialize an already loaded
596 * engine (or %NULL if not required)
597 * @id: the engine id of the engine to load (only required if post is not %NULL
599 * This function is a generic function that loads any openssl engine.
601 * Returns: 0 on success, -1 on failure
603 static int tls_engine_load_dynamic_generic(const char *pre
[],
604 const char *post
[], const char *id
)
607 const char *dynamic_id
= "dynamic";
609 engine
= ENGINE_by_id(id
);
612 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
618 engine
= ENGINE_by_id(dynamic_id
);
619 if (engine
== NULL
) {
620 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
622 ERR_error_string(ERR_get_error(), NULL
));
626 /* Perform the pre commands. This will load the engine. */
627 while (pre
&& pre
[0]) {
628 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
629 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
630 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
631 "%s %s [%s]", pre
[0], pre
[1],
632 ERR_error_string(ERR_get_error(), NULL
));
640 * Free the reference to the "dynamic" engine. The loaded engine can
641 * now be looked up using ENGINE_by_id().
645 engine
= ENGINE_by_id(id
);
646 if (engine
== NULL
) {
647 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
648 id
, ERR_error_string(ERR_get_error(), NULL
));
652 while (post
&& post
[0]) {
653 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
654 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
655 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
656 " %s %s [%s]", post
[0], post
[1],
657 ERR_error_string(ERR_get_error(), NULL
));
658 ENGINE_remove(engine
);
671 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
672 * @pkcs11_so_path: pksc11_so_path from the configuration
673 * @pcks11_module_path: pkcs11_module_path from the configuration
675 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
676 const char *pkcs11_module_path
)
678 char *engine_id
= "pkcs11";
679 const char *pre_cmd
[] = {
680 "SO_PATH", NULL
/* pkcs11_so_path */,
681 "ID", NULL
/* engine_id */,
683 /* "NO_VCHECK", "1", */
687 const char *post_cmd
[] = {
688 "MODULE_PATH", NULL
/* pkcs11_module_path */,
695 pre_cmd
[1] = pkcs11_so_path
;
696 pre_cmd
[3] = engine_id
;
697 if (pkcs11_module_path
)
698 post_cmd
[1] = pkcs11_module_path
;
702 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
705 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
710 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
711 * @opensc_so_path: opensc_so_path from the configuration
713 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
715 char *engine_id
= "opensc";
716 const char *pre_cmd
[] = {
717 "SO_PATH", NULL
/* opensc_so_path */,
718 "ID", NULL
/* engine_id */,
727 pre_cmd
[1] = opensc_so_path
;
728 pre_cmd
[3] = engine_id
;
730 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
733 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
735 #endif /* OPENSSL_NO_ENGINE */
738 void * tls_init(const struct tls_config
*conf
)
741 struct tls_context
*context
;
744 if (tls_openssl_ref_count
== 0) {
745 tls_global
= context
= tls_context_new(conf
);
750 if (conf
&& conf
->fips_mode
) {
751 if (!FIPS_mode_set(1)) {
752 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
754 ERR_load_crypto_strings();
755 ERR_print_errors_fp(stderr
);
760 wpa_printf(MSG_INFO
, "Running in FIPS mode");
762 #else /* OPENSSL_FIPS */
763 if (conf
&& conf
->fips_mode
) {
764 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
770 #endif /* OPENSSL_FIPS */
771 #endif /* CONFIG_FIPS */
772 SSL_load_error_strings();
774 #ifndef OPENSSL_NO_SHA256
775 EVP_add_digest(EVP_sha256());
776 #endif /* OPENSSL_NO_SHA256 */
777 /* TODO: if /dev/urandom is available, PRNG is seeded
778 * automatically. If this is not the case, random data should
782 #ifndef OPENSSL_NO_RC2
784 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
785 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
786 * versions, but it looks like OpenSSL 1.0.0 does not do that
789 EVP_add_cipher(EVP_rc2_40_cbc());
790 #endif /* OPENSSL_NO_RC2 */
792 #endif /* PKCS12_FUNCS */
794 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
795 /* Newer OpenSSL can store app-data per-SSL */
796 context
= tls_context_new(conf
);
799 #else /* OPENSSL_SUPPORTS_CTX_APP_DATA */
800 context
= tls_global
;
801 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
803 tls_openssl_ref_count
++;
805 ssl
= SSL_CTX_new(SSLv23_method());
807 tls_openssl_ref_count
--;
808 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
809 if (context
!= tls_global
)
811 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
812 if (tls_openssl_ref_count
== 0) {
819 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
820 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
822 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
823 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
824 SSL_CTX_set_app_data(ssl
, context
);
825 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
827 #ifndef OPENSSL_NO_ENGINE
828 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
829 ERR_load_ENGINE_strings();
830 ENGINE_load_dynamic();
833 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
834 conf
->pkcs11_module_path
)) {
835 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
836 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
837 conf
->pkcs11_module_path
)) {
842 #endif /* OPENSSL_NO_ENGINE */
844 if (conf
&& conf
->openssl_ciphers
)
845 ciphers
= conf
->openssl_ciphers
;
847 ciphers
= "DEFAULT:!EXP:!LOW";
848 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
849 wpa_printf(MSG_ERROR
,
850 "OpenSSL: Failed to set cipher string '%s'",
860 void tls_deinit(void *ssl_ctx
)
862 SSL_CTX
*ssl
= ssl_ctx
;
863 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
864 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
865 if (context
!= tls_global
)
867 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
870 tls_openssl_ref_count
--;
871 if (tls_openssl_ref_count
== 0) {
872 #ifndef OPENSSL_NO_ENGINE
874 #endif /* OPENSSL_NO_ENGINE */
875 CRYPTO_cleanup_all_ex_data();
876 ERR_remove_thread_state(NULL
);
879 os_free(tls_global
->ocsp_stapling_response
);
880 tls_global
->ocsp_stapling_response
= NULL
;
887 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
888 const char *pin
, const char *key_id
,
889 const char *cert_id
, const char *ca_cert_id
)
891 #ifndef OPENSSL_NO_ENGINE
893 if (engine_id
== NULL
) {
894 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
900 ENGINE_load_dynamic();
902 conn
->engine
= ENGINE_by_id(engine_id
);
904 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
905 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
908 if (ENGINE_init(conn
->engine
) != 1) {
909 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
910 "(engine: %s) [%s]", engine_id
,
911 ERR_error_string(ERR_get_error(), NULL
));
914 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
917 if (pin
&& ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
918 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
919 ERR_error_string(ERR_get_error(), NULL
));
925 * Ensure that the ENGINE does not attempt to use the OpenSSL
926 * UI system to obtain a PIN, if we didn't provide one.
929 const void *password
;
930 const char *prompt_info
;
931 } key_cb
= { "", NULL
};
933 /* load private key first in-case PIN is required for cert */
934 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
937 if (!conn
->private_key
) {
938 wpa_printf(MSG_ERROR
,
939 "ENGINE: cannot load private key with id '%s' [%s]",
941 ERR_error_string(ERR_get_error(), NULL
));
942 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
947 /* handle a certificate and/or CA certificate */
948 if (cert_id
|| ca_cert_id
) {
949 const char *cmd_name
= "LOAD_CERT_CTRL";
951 /* test if the engine supports a LOAD_CERT_CTRL */
952 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
953 0, (void *)cmd_name
, NULL
)) {
954 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
955 " loading certificates");
956 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
965 ENGINE_free(conn
->engine
);
969 if (conn
->private_key
) {
970 EVP_PKEY_free(conn
->private_key
);
971 conn
->private_key
= NULL
;
975 #else /* OPENSSL_NO_ENGINE */
977 #endif /* OPENSSL_NO_ENGINE */
981 static void tls_engine_deinit(struct tls_connection
*conn
)
983 #ifndef OPENSSL_NO_ENGINE
984 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
985 if (conn
->private_key
) {
986 EVP_PKEY_free(conn
->private_key
);
987 conn
->private_key
= NULL
;
990 ENGINE_finish(conn
->engine
);
993 #endif /* OPENSSL_NO_ENGINE */
997 int tls_get_errors(void *ssl_ctx
)
1002 while ((err
= ERR_get_error())) {
1003 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1004 ERR_error_string(err
, NULL
));
1012 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1013 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1015 struct tls_connection
*conn
= arg
;
1016 const u8
*pos
= buf
;
1018 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d",
1019 write_p
? "TX" : "RX", version
, content_type
);
1020 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1021 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1022 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1023 if (payload_len
+ 3 > len
) {
1024 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1025 conn
->invalid_hb_used
= 1;
1031 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1033 SSL_CTX
*ssl
= ssl_ctx
;
1034 struct tls_connection
*conn
;
1036 #ifdef OPENSSL_SUPPORTS_CTX_APP_DATA
1037 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1038 #else /* OPENSSL_SUPPORTS_CTX_APP_DATA */
1039 struct tls_context
*context
= tls_global
;
1040 #endif /* OPENSSL_SUPPORTS_CTX_APP_DATA */
1042 conn
= os_zalloc(sizeof(*conn
));
1045 conn
->ssl_ctx
= ssl_ctx
;
1046 conn
->ssl
= SSL_new(ssl
);
1047 if (conn
->ssl
== NULL
) {
1048 tls_show_errors(MSG_INFO
, __func__
,
1049 "Failed to initialize new SSL connection");
1054 conn
->context
= context
;
1055 SSL_set_app_data(conn
->ssl
, conn
);
1056 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1057 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1058 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1059 SSL_OP_SINGLE_DH_USE
;
1060 #ifdef SSL_OP_NO_COMPRESSION
1061 options
|= SSL_OP_NO_COMPRESSION
;
1062 #endif /* SSL_OP_NO_COMPRESSION */
1063 SSL_set_options(conn
->ssl
, options
);
1065 conn
->ssl_in
= BIO_new(BIO_s_mem());
1066 if (!conn
->ssl_in
) {
1067 tls_show_errors(MSG_INFO
, __func__
,
1068 "Failed to create a new BIO for ssl_in");
1069 SSL_free(conn
->ssl
);
1074 conn
->ssl_out
= BIO_new(BIO_s_mem());
1075 if (!conn
->ssl_out
) {
1076 tls_show_errors(MSG_INFO
, __func__
,
1077 "Failed to create a new BIO for ssl_out");
1078 SSL_free(conn
->ssl
);
1079 BIO_free(conn
->ssl_in
);
1084 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1090 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1094 SSL_free(conn
->ssl
);
1095 tls_engine_deinit(conn
);
1096 os_free(conn
->subject_match
);
1097 os_free(conn
->altsubject_match
);
1098 os_free(conn
->suffix_match
);
1099 os_free(conn
->domain_match
);
1100 os_free(conn
->session_ticket
);
1105 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1107 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1111 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1116 /* Shutdown previous TLS connection without notifying the peer
1117 * because the connection was already terminated in practice
1118 * and "close notify" shutdown alert would confuse AS. */
1119 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1120 SSL_shutdown(conn
->ssl
);
1125 static int tls_match_altsubject_component(X509
*cert
, int type
,
1126 const char *value
, size_t len
)
1133 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1135 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1136 gen
= sk_GENERAL_NAME_value(ext
, i
);
1137 if (gen
->type
!= type
)
1139 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1140 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1148 static int tls_match_altsubject(X509
*cert
, const char *match
)
1151 const char *pos
, *end
;
1156 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1159 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1162 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1166 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1170 end
= os_strchr(pos
, ';');
1172 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1173 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1174 os_strncmp(end
+ 1, "URI:", 4) == 0)
1176 end
= os_strchr(end
+ 1, ';');
1181 len
= os_strlen(pos
);
1182 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1191 #ifndef CONFIG_NATIVE_WINDOWS
1192 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
,
1195 size_t i
, match_len
;
1197 /* Check for embedded nuls that could mess up suffix matching */
1198 for (i
= 0; i
< len
; i
++) {
1199 if (val
[i
] == '\0') {
1200 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1205 match_len
= os_strlen(match
);
1206 if (match_len
> len
|| (full
&& match_len
!= len
))
1209 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1211 return 0; /* no match */
1213 if (match_len
== len
)
1214 return 1; /* exact match */
1216 if (val
[len
- match_len
- 1] == '.')
1217 return 1; /* full label match completes suffix match */
1219 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1222 #endif /* CONFIG_NATIVE_WINDOWS */
1225 static int tls_match_suffix(X509
*cert
, const char *match
, int full
)
1227 #ifdef CONFIG_NATIVE_WINDOWS
1228 /* wincrypt.h has conflicting X509_NAME definition */
1230 #else /* CONFIG_NATIVE_WINDOWS */
1238 wpa_printf(MSG_DEBUG
, "TLS: Match domain against %s%s",
1239 full
? "": "suffix ", match
);
1241 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1243 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1244 gen
= sk_GENERAL_NAME_value(ext
, j
);
1245 if (gen
->type
!= GEN_DNS
)
1248 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1249 gen
->d
.dNSName
->data
,
1250 gen
->d
.dNSName
->length
);
1251 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1252 gen
->d
.dNSName
->length
, match
, full
) ==
1254 wpa_printf(MSG_DEBUG
, "TLS: %s in dNSName found",
1255 full
? "Match" : "Suffix match");
1261 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1265 name
= X509_get_subject_name(cert
);
1271 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1274 e
= X509_NAME_get_entry(name
, i
);
1277 cn
= X509_NAME_ENTRY_get_data(e
);
1280 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1281 cn
->data
, cn
->length
);
1282 if (domain_suffix_match(cn
->data
, cn
->length
, match
, full
) == 1)
1284 wpa_printf(MSG_DEBUG
, "TLS: %s in commonName found",
1285 full
? "Match" : "Suffix match");
1290 wpa_printf(MSG_DEBUG
, "TLS: No CommonName %smatch found",
1291 full
? "": "suffix ");
1293 #endif /* CONFIG_NATIVE_WINDOWS */
1297 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1300 case X509_V_ERR_CERT_REVOKED
:
1301 return TLS_FAIL_REVOKED
;
1302 case X509_V_ERR_CERT_NOT_YET_VALID
:
1303 case X509_V_ERR_CRL_NOT_YET_VALID
:
1304 return TLS_FAIL_NOT_YET_VALID
;
1305 case X509_V_ERR_CERT_HAS_EXPIRED
:
1306 case X509_V_ERR_CRL_HAS_EXPIRED
:
1307 return TLS_FAIL_EXPIRED
;
1308 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1309 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1310 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1311 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1312 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1313 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1314 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1315 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1316 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1317 case X509_V_ERR_INVALID_CA
:
1318 return TLS_FAIL_UNTRUSTED
;
1319 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1320 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1321 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1322 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1323 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1324 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1325 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1326 case X509_V_ERR_CERT_UNTRUSTED
:
1327 case X509_V_ERR_CERT_REJECTED
:
1328 return TLS_FAIL_BAD_CERTIFICATE
;
1330 return TLS_FAIL_UNSPECIFIED
;
1335 static struct wpabuf
* get_x509_cert(X509
*cert
)
1340 int cert_len
= i2d_X509(cert
, NULL
);
1344 buf
= wpabuf_alloc(cert_len
);
1348 tmp
= wpabuf_put(buf
, cert_len
);
1349 i2d_X509(cert
, &tmp
);
1354 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1355 X509
*err_cert
, int err
, int depth
,
1356 const char *subject
, const char *err_str
,
1357 enum tls_fail_reason reason
)
1359 union tls_event_data ev
;
1360 struct wpabuf
*cert
= NULL
;
1361 struct tls_context
*context
= conn
->context
;
1363 if (context
->event_cb
== NULL
)
1366 cert
= get_x509_cert(err_cert
);
1367 os_memset(&ev
, 0, sizeof(ev
));
1368 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1369 reason
: openssl_tls_fail_reason(err
);
1370 ev
.cert_fail
.depth
= depth
;
1371 ev
.cert_fail
.subject
= subject
;
1372 ev
.cert_fail
.reason_txt
= err_str
;
1373 ev
.cert_fail
.cert
= cert
;
1374 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1379 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1380 X509
*err_cert
, int depth
,
1381 const char *subject
)
1383 struct wpabuf
*cert
= NULL
;
1384 union tls_event_data ev
;
1385 struct tls_context
*context
= conn
->context
;
1386 char *altsubject
[TLS_MAX_ALT_SUBJECT
];
1387 int alt
, num_altsubject
= 0;
1391 #ifdef CONFIG_SHA256
1393 #endif /* CONFIG_SHA256 */
1395 if (context
->event_cb
== NULL
)
1398 os_memset(&ev
, 0, sizeof(ev
));
1399 if (conn
->cert_probe
|| context
->cert_in_cb
) {
1400 cert
= get_x509_cert(err_cert
);
1401 ev
.peer_cert
.cert
= cert
;
1403 #ifdef CONFIG_SHA256
1407 addr
[0] = wpabuf_head(cert
);
1408 len
[0] = wpabuf_len(cert
);
1409 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1410 ev
.peer_cert
.hash
= hash
;
1411 ev
.peer_cert
.hash_len
= sizeof(hash
);
1414 #endif /* CONFIG_SHA256 */
1415 ev
.peer_cert
.depth
= depth
;
1416 ev
.peer_cert
.subject
= subject
;
1418 ext
= X509_get_ext_d2i(err_cert
, NID_subject_alt_name
, NULL
, NULL
);
1419 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1422 if (num_altsubject
== TLS_MAX_ALT_SUBJECT
)
1424 gen
= sk_GENERAL_NAME_value(ext
, i
);
1425 if (gen
->type
!= GEN_EMAIL
&&
1426 gen
->type
!= GEN_DNS
&&
1427 gen
->type
!= GEN_URI
)
1430 pos
= os_malloc(10 + gen
->d
.ia5
->length
+ 1);
1433 altsubject
[num_altsubject
++] = pos
;
1435 switch (gen
->type
) {
1437 os_memcpy(pos
, "EMAIL:", 6);
1441 os_memcpy(pos
, "DNS:", 4);
1445 os_memcpy(pos
, "URI:", 4);
1450 os_memcpy(pos
, gen
->d
.ia5
->data
, gen
->d
.ia5
->length
);
1451 pos
+= gen
->d
.ia5
->length
;
1455 for (alt
= 0; alt
< num_altsubject
; alt
++)
1456 ev
.peer_cert
.altsubject
[alt
] = altsubject
[alt
];
1457 ev
.peer_cert
.num_altsubject
= num_altsubject
;
1459 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1461 for (alt
= 0; alt
< num_altsubject
; alt
++)
1462 os_free(altsubject
[alt
]);
1466 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1472 struct tls_connection
*conn
;
1473 struct tls_context
*context
;
1474 char *match
, *altmatch
, *suffix_match
, *domain_match
;
1475 const char *err_str
;
1477 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1481 err
= X509_STORE_CTX_get_error(x509_ctx
);
1482 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1483 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1484 SSL_get_ex_data_X509_STORE_CTX_idx());
1485 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1487 conn
= SSL_get_app_data(ssl
);
1492 conn
->peer_cert
= err_cert
;
1493 else if (depth
== 1)
1494 conn
->peer_issuer
= err_cert
;
1495 else if (depth
== 2)
1496 conn
->peer_issuer_issuer
= err_cert
;
1498 context
= conn
->context
;
1499 match
= conn
->subject_match
;
1500 altmatch
= conn
->altsubject_match
;
1501 suffix_match
= conn
->suffix_match
;
1502 domain_match
= conn
->domain_match
;
1504 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1506 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1508 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1509 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1510 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1511 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1516 err_str
= X509_verify_cert_error_string(err
);
1518 #ifdef CONFIG_SHA256
1519 if (preverify_ok
&& depth
== 0 && conn
->server_cert_only
) {
1520 struct wpabuf
*cert
;
1521 cert
= get_x509_cert(err_cert
);
1523 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1524 "server certificate data");
1530 addr
[0] = wpabuf_head(cert
);
1531 len
[0] = wpabuf_len(cert
);
1532 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1533 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1534 err_str
= "Server certificate mismatch";
1535 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1541 #endif /* CONFIG_SHA256 */
1543 if (!preverify_ok
) {
1544 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1545 " error %d (%s) depth %d for '%s'", err
, err_str
,
1547 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1548 err_str
, TLS_FAIL_UNSPECIFIED
);
1549 return preverify_ok
;
1552 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1553 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1554 preverify_ok
, err
, err_str
,
1555 conn
->ca_cert_verify
, depth
, buf
);
1556 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1557 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1558 "match with '%s'", buf
, match
);
1560 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1562 TLS_FAIL_SUBJECT_MISMATCH
);
1563 } else if (depth
== 0 && altmatch
&&
1564 !tls_match_altsubject(err_cert
, altmatch
)) {
1565 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1566 "'%s' not found", altmatch
);
1568 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1569 "AltSubject mismatch",
1570 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1571 } else if (depth
== 0 && suffix_match
&&
1572 !tls_match_suffix(err_cert
, suffix_match
, 0)) {
1573 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1576 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1577 "Domain suffix mismatch",
1578 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1579 } else if (depth
== 0 && domain_match
&&
1580 !tls_match_suffix(err_cert
, domain_match
, 1)) {
1581 wpa_printf(MSG_WARNING
, "TLS: Domain match '%s' not found",
1584 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1586 TLS_FAIL_DOMAIN_MISMATCH
);
1588 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1590 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1591 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1592 "on probe-only run");
1594 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1595 "Server certificate chain probe",
1596 TLS_FAIL_SERVER_CHAIN_PROBE
);
1599 if (preverify_ok
&& context
->event_cb
!= NULL
)
1600 context
->event_cb(context
->cb_ctx
,
1601 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1603 return preverify_ok
;
1607 #ifndef OPENSSL_NO_STDIO
1608 static int tls_load_ca_der(void *_ssl_ctx
, const char *ca_cert
)
1610 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1611 X509_LOOKUP
*lookup
;
1614 lookup
= X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx
),
1615 X509_LOOKUP_file());
1616 if (lookup
== NULL
) {
1617 tls_show_errors(MSG_WARNING
, __func__
,
1618 "Failed add lookup for X509 store");
1622 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1623 unsigned long err
= ERR_peek_error();
1624 tls_show_errors(MSG_WARNING
, __func__
,
1625 "Failed load CA in DER format");
1626 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1627 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1628 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1629 "cert already in hash table error",
1637 #endif /* OPENSSL_NO_STDIO */
1640 static int tls_connection_ca_cert(void *_ssl_ctx
, struct tls_connection
*conn
,
1641 const char *ca_cert
, const u8
*ca_cert_blob
,
1642 size_t ca_cert_blob_len
, const char *ca_path
)
1644 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1648 * Remove previously configured trusted CA certificates before adding
1651 store
= X509_STORE_new();
1652 if (store
== NULL
) {
1653 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
1654 "certificate store", __func__
);
1657 SSL_CTX_set_cert_store(ssl_ctx
, store
);
1659 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1660 conn
->ca_cert_verify
= 1;
1662 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
1663 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
1665 conn
->cert_probe
= 1;
1666 conn
->ca_cert_verify
= 0;
1670 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
1671 #ifdef CONFIG_SHA256
1672 const char *pos
= ca_cert
+ 7;
1673 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
1674 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
1675 "hash value '%s'", ca_cert
);
1679 if (os_strlen(pos
) != 32 * 2) {
1680 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
1681 "hash length in ca_cert '%s'", ca_cert
);
1684 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
1685 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
1686 "value in ca_cert '%s'", ca_cert
);
1689 conn
->server_cert_only
= 1;
1690 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
1691 "certificate match");
1693 #else /* CONFIG_SHA256 */
1694 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
1695 "cannot validate server certificate hash");
1697 #endif /* CONFIG_SHA256 */
1701 X509
*cert
= d2i_X509(NULL
,
1702 (const unsigned char **) &ca_cert_blob
,
1705 tls_show_errors(MSG_WARNING
, __func__
,
1706 "Failed to parse ca_cert_blob");
1710 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
1712 unsigned long err
= ERR_peek_error();
1713 tls_show_errors(MSG_WARNING
, __func__
,
1714 "Failed to add ca_cert_blob to "
1715 "certificate store");
1716 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1717 ERR_GET_REASON(err
) ==
1718 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1719 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1720 "cert already in hash table error",
1728 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
1729 "to certificate store", __func__
);
1734 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
1735 BIO
*bio
= BIO_from_keystore(&ca_cert
[11]);
1736 STACK_OF(X509_INFO
) *stack
= NULL
;
1740 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
1746 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
1747 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
1749 X509_STORE_add_cert(ssl_ctx
->cert_store
,
1753 X509_STORE_add_crl(ssl_ctx
->cert_store
,
1757 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
1758 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1761 #endif /* ANDROID */
1763 #ifdef CONFIG_NATIVE_WINDOWS
1764 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
1766 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
1767 "system certificate store");
1770 #endif /* CONFIG_NATIVE_WINDOWS */
1772 if (ca_cert
|| ca_path
) {
1773 #ifndef OPENSSL_NO_STDIO
1774 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
1776 tls_show_errors(MSG_WARNING
, __func__
,
1777 "Failed to load root certificates");
1779 tls_load_ca_der(ssl_ctx
, ca_cert
) == 0) {
1780 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
1781 "DER format CA certificate",
1786 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1787 "certificate(s) loaded");
1788 tls_get_errors(ssl_ctx
);
1790 #else /* OPENSSL_NO_STDIO */
1791 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
1794 #endif /* OPENSSL_NO_STDIO */
1796 /* No ca_cert configured - do not try to verify server
1798 conn
->ca_cert_verify
= 0;
1805 static int tls_global_ca_cert(SSL_CTX
*ssl_ctx
, const char *ca_cert
)
1808 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
1810 tls_show_errors(MSG_WARNING
, __func__
,
1811 "Failed to load root certificates");
1815 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1816 "certificate(s) loaded");
1818 #ifndef OPENSSL_NO_STDIO
1819 /* Add the same CAs to the client certificate requests */
1820 SSL_CTX_set_client_CA_list(ssl_ctx
,
1821 SSL_load_client_CA_file(ca_cert
));
1822 #endif /* OPENSSL_NO_STDIO */
1829 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
1834 X509_STORE
*cs
= SSL_CTX_get_cert_store(ssl_ctx
);
1836 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
1837 "certificate store when enabling "
1841 flags
= X509_V_FLAG_CRL_CHECK
;
1843 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
1844 X509_STORE_set_flags(cs
, flags
);
1850 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
1851 const char *subject_match
,
1852 const char *altsubject_match
,
1853 const char *suffix_match
,
1854 const char *domain_match
)
1856 os_free(conn
->subject_match
);
1857 conn
->subject_match
= NULL
;
1858 if (subject_match
) {
1859 conn
->subject_match
= os_strdup(subject_match
);
1860 if (conn
->subject_match
== NULL
)
1864 os_free(conn
->altsubject_match
);
1865 conn
->altsubject_match
= NULL
;
1866 if (altsubject_match
) {
1867 conn
->altsubject_match
= os_strdup(altsubject_match
);
1868 if (conn
->altsubject_match
== NULL
)
1872 os_free(conn
->suffix_match
);
1873 conn
->suffix_match
= NULL
;
1875 conn
->suffix_match
= os_strdup(suffix_match
);
1876 if (conn
->suffix_match
== NULL
)
1880 os_free(conn
->domain_match
);
1881 conn
->domain_match
= NULL
;
1883 conn
->domain_match
= os_strdup(domain_match
);
1884 if (conn
->domain_match
== NULL
)
1892 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
1895 static int counter
= 0;
1901 conn
->ca_cert_verify
= 1;
1902 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
1903 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1904 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
1906 conn
->ca_cert_verify
= 0;
1907 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
1910 SSL_set_accept_state(conn
->ssl
);
1913 * Set session id context in order to avoid fatal errors when client
1914 * tries to resume a session. However, set the context to a unique
1915 * value in order to effectively disable session resumption for now
1916 * since not all areas of the server code are ready for it (e.g.,
1917 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1921 SSL_set_session_id_context(conn
->ssl
,
1922 (const unsigned char *) &counter
,
1929 static int tls_connection_client_cert(struct tls_connection
*conn
,
1930 const char *client_cert
,
1931 const u8
*client_cert_blob
,
1932 size_t client_cert_blob_len
)
1934 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
1937 if (client_cert_blob
&&
1938 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
1939 client_cert_blob_len
) == 1) {
1940 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
1943 } else if (client_cert_blob
) {
1944 tls_show_errors(MSG_DEBUG
, __func__
,
1945 "SSL_use_certificate_ASN1 failed");
1948 if (client_cert
== NULL
)
1952 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
1953 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
1957 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
1961 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
1967 #endif /* ANDROID */
1969 #ifndef OPENSSL_NO_STDIO
1970 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
1971 SSL_FILETYPE_ASN1
) == 1) {
1972 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
1977 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
1978 SSL_FILETYPE_PEM
) == 1) {
1980 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
1985 tls_show_errors(MSG_DEBUG
, __func__
,
1986 "SSL_use_certificate_file failed");
1987 #else /* OPENSSL_NO_STDIO */
1988 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
1989 #endif /* OPENSSL_NO_STDIO */
1995 static int tls_global_client_cert(SSL_CTX
*ssl_ctx
, const char *client_cert
)
1997 #ifndef OPENSSL_NO_STDIO
1998 if (client_cert
== NULL
)
2001 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2002 SSL_FILETYPE_ASN1
) != 1 &&
2003 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
2004 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2005 SSL_FILETYPE_PEM
) != 1) {
2006 tls_show_errors(MSG_INFO
, __func__
,
2007 "Failed to load client certificate");
2011 #else /* OPENSSL_NO_STDIO */
2012 if (client_cert
== NULL
)
2014 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2016 #endif /* OPENSSL_NO_STDIO */
2020 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
2022 if (password
== NULL
) {
2025 os_strlcpy(buf
, (char *) password
, size
);
2026 return os_strlen(buf
);
2031 static int tls_parse_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, PKCS12
*p12
,
2036 STACK_OF(X509
) *certs
;
2043 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
2044 tls_show_errors(MSG_DEBUG
, __func__
,
2045 "Failed to parse PKCS12 file");
2049 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
2052 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2054 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
2055 "subject='%s'", buf
);
2057 if (SSL_use_certificate(ssl
, cert
) != 1)
2060 if (SSL_CTX_use_certificate(ssl_ctx
, cert
) != 1)
2067 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
2069 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
2072 if (SSL_CTX_use_PrivateKey(ssl_ctx
, pkey
) != 1)
2075 EVP_PKEY_free(pkey
);
2079 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2080 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2082 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2083 " from PKCS12: subject='%s'", buf
);
2085 * There is no SSL equivalent for the chain cert - so
2086 * always add it to the context...
2088 if (SSL_CTX_add_extra_chain_cert(ssl_ctx
, cert
) != 1) {
2093 sk_X509_free(certs
);
2099 tls_get_errors(ssl_ctx
);
2103 #endif /* PKCS12_FUNCS */
2106 static int tls_read_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *private_key
,
2113 f
= fopen(private_key
, "rb");
2117 p12
= d2i_PKCS12_fp(f
, NULL
);
2121 tls_show_errors(MSG_INFO
, __func__
,
2122 "Failed to use PKCS#12 file");
2126 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2128 #else /* PKCS12_FUNCS */
2129 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2132 #endif /* PKCS12_FUNCS */
2136 static int tls_read_pkcs12_blob(SSL_CTX
*ssl_ctx
, SSL
*ssl
,
2137 const u8
*blob
, size_t len
, const char *passwd
)
2142 p12
= d2i_PKCS12(NULL
, (const unsigned char **) &blob
, len
);
2144 tls_show_errors(MSG_INFO
, __func__
,
2145 "Failed to use PKCS#12 blob");
2149 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2151 #else /* PKCS12_FUNCS */
2152 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2155 #endif /* PKCS12_FUNCS */
2159 #ifndef OPENSSL_NO_ENGINE
2160 static int tls_engine_get_cert(struct tls_connection
*conn
,
2161 const char *cert_id
,
2164 /* this runs after the private key is loaded so no PIN is required */
2166 const char *cert_id
;
2169 params
.cert_id
= cert_id
;
2172 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2173 0, ¶ms
, NULL
, 1)) {
2174 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2175 " '%s' [%s]", cert_id
,
2176 ERR_error_string(ERR_get_error(), NULL
));
2177 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2180 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2182 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2184 *cert
= params
.cert
;
2187 #endif /* OPENSSL_NO_ENGINE */
2190 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2191 const char *cert_id
)
2193 #ifndef OPENSSL_NO_ENGINE
2196 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2199 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2200 tls_show_errors(MSG_ERROR
, __func__
,
2201 "SSL_use_certificate failed");
2206 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2210 #else /* OPENSSL_NO_ENGINE */
2212 #endif /* OPENSSL_NO_ENGINE */
2216 static int tls_connection_engine_ca_cert(void *_ssl_ctx
,
2217 struct tls_connection
*conn
,
2218 const char *ca_cert_id
)
2220 #ifndef OPENSSL_NO_ENGINE
2222 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2225 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2228 /* start off the same as tls_connection_ca_cert */
2229 store
= X509_STORE_new();
2230 if (store
== NULL
) {
2231 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2232 "certificate store", __func__
);
2236 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2237 if (!X509_STORE_add_cert(store
, cert
)) {
2238 unsigned long err
= ERR_peek_error();
2239 tls_show_errors(MSG_WARNING
, __func__
,
2240 "Failed to add CA certificate from engine "
2241 "to certificate store");
2242 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2243 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2244 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2245 " already in hash table error",
2253 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2254 "to certificate store", __func__
);
2255 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2256 conn
->ca_cert_verify
= 1;
2260 #else /* OPENSSL_NO_ENGINE */
2262 #endif /* OPENSSL_NO_ENGINE */
2266 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2268 #ifndef OPENSSL_NO_ENGINE
2269 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2270 tls_show_errors(MSG_ERROR
, __func__
,
2271 "ENGINE: cannot use private key for TLS");
2274 if (!SSL_check_private_key(conn
->ssl
)) {
2275 tls_show_errors(MSG_INFO
, __func__
,
2276 "Private key failed verification");
2280 #else /* OPENSSL_NO_ENGINE */
2281 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2282 "engine support was not compiled in");
2284 #endif /* OPENSSL_NO_ENGINE */
2288 static int tls_connection_private_key(void *_ssl_ctx
,
2289 struct tls_connection
*conn
,
2290 const char *private_key
,
2291 const char *private_key_passwd
,
2292 const u8
*private_key_blob
,
2293 size_t private_key_blob_len
)
2295 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2299 if (private_key
== NULL
&& private_key_blob
== NULL
)
2302 if (private_key_passwd
) {
2303 passwd
= os_strdup(private_key_passwd
);
2309 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2310 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2313 while (private_key_blob
) {
2314 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2315 (u8
*) private_key_blob
,
2316 private_key_blob_len
) == 1) {
2317 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2318 "ASN1(EVP_PKEY_RSA) --> OK");
2323 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2324 (u8
*) private_key_blob
,
2325 private_key_blob_len
) == 1) {
2326 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2327 "ASN1(EVP_PKEY_DSA) --> OK");
2332 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2333 (u8
*) private_key_blob
,
2334 private_key_blob_len
) == 1) {
2335 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2336 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2341 if (tls_read_pkcs12_blob(ssl_ctx
, conn
->ssl
, private_key_blob
,
2342 private_key_blob_len
, passwd
) == 0) {
2343 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2352 while (!ok
&& private_key
) {
2353 #ifndef OPENSSL_NO_STDIO
2354 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2355 SSL_FILETYPE_ASN1
) == 1) {
2356 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2357 "SSL_use_PrivateKey_File (DER) --> OK");
2362 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2363 SSL_FILETYPE_PEM
) == 1) {
2364 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2365 "SSL_use_PrivateKey_File (PEM) --> OK");
2369 #else /* OPENSSL_NO_STDIO */
2370 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2372 #endif /* OPENSSL_NO_STDIO */
2374 if (tls_read_pkcs12(ssl_ctx
, conn
->ssl
, private_key
, passwd
)
2376 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2382 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2383 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2384 "access certificate store --> OK");
2393 tls_show_errors(MSG_INFO
, __func__
,
2394 "Failed to load private key");
2399 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2402 if (!SSL_check_private_key(conn
->ssl
)) {
2403 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
2408 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
2413 static int tls_global_private_key(SSL_CTX
*ssl_ctx
, const char *private_key
,
2414 const char *private_key_passwd
)
2418 if (private_key
== NULL
)
2421 if (private_key_passwd
) {
2422 passwd
= os_strdup(private_key_passwd
);
2428 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2429 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2431 #ifndef OPENSSL_NO_STDIO
2432 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2433 SSL_FILETYPE_ASN1
) != 1 &&
2434 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2435 SSL_FILETYPE_PEM
) != 1 &&
2436 #endif /* OPENSSL_NO_STDIO */
2437 tls_read_pkcs12(ssl_ctx
, NULL
, private_key
, passwd
)) {
2438 tls_show_errors(MSG_INFO
, __func__
,
2439 "Failed to load private key");
2446 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2448 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
2449 tls_show_errors(MSG_INFO
, __func__
,
2450 "Private key failed verification");
2458 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
2460 #ifdef OPENSSL_NO_DH
2461 if (dh_file
== NULL
)
2463 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2464 "dh_file specified");
2466 #else /* OPENSSL_NO_DH */
2470 /* TODO: add support for dh_blob */
2471 if (dh_file
== NULL
)
2476 bio
= BIO_new_file(dh_file
, "r");
2478 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2479 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2482 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2484 #ifndef OPENSSL_NO_DSA
2485 while (dh
== NULL
) {
2487 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2488 " trying to parse as DSA params", dh_file
,
2489 ERR_error_string(ERR_get_error(), NULL
));
2490 bio
= BIO_new_file(dh_file
, "r");
2493 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2496 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2497 "'%s': %s", dh_file
,
2498 ERR_error_string(ERR_get_error(), NULL
));
2502 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2503 dh
= DSA_dup_DH(dsa
);
2506 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2507 "params into DH params");
2512 #endif /* !OPENSSL_NO_DSA */
2514 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2519 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
2520 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2522 ERR_error_string(ERR_get_error(), NULL
));
2528 #endif /* OPENSSL_NO_DH */
2532 static int tls_global_dh(SSL_CTX
*ssl_ctx
, const char *dh_file
)
2534 #ifdef OPENSSL_NO_DH
2535 if (dh_file
== NULL
)
2537 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2538 "dh_file specified");
2540 #else /* OPENSSL_NO_DH */
2544 /* TODO: add support for dh_blob */
2545 if (dh_file
== NULL
)
2547 if (ssl_ctx
== NULL
)
2550 bio
= BIO_new_file(dh_file
, "r");
2552 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2553 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2556 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2558 #ifndef OPENSSL_NO_DSA
2559 while (dh
== NULL
) {
2561 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2562 " trying to parse as DSA params", dh_file
,
2563 ERR_error_string(ERR_get_error(), NULL
));
2564 bio
= BIO_new_file(dh_file
, "r");
2567 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2570 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2571 "'%s': %s", dh_file
,
2572 ERR_error_string(ERR_get_error(), NULL
));
2576 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2577 dh
= DSA_dup_DH(dsa
);
2580 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2581 "params into DH params");
2586 #endif /* !OPENSSL_NO_DSA */
2588 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2593 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
2594 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2596 ERR_error_string(ERR_get_error(), NULL
));
2602 #endif /* OPENSSL_NO_DH */
2606 int tls_connection_get_keys(void *ssl_ctx
, struct tls_connection
*conn
,
2607 struct tls_keys
*keys
)
2610 wpa_printf(MSG_ERROR
, "OpenSSL: TLS keys cannot be exported in FIPS "
2613 #else /* CONFIG_FIPS */
2616 if (conn
== NULL
|| keys
== NULL
)
2619 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
)
2622 os_memset(keys
, 0, sizeof(*keys
));
2623 keys
->master_key
= ssl
->session
->master_key
;
2624 keys
->master_key_len
= ssl
->session
->master_key_length
;
2625 keys
->client_random
= ssl
->s3
->client_random
;
2626 keys
->client_random_len
= SSL3_RANDOM_SIZE
;
2627 keys
->server_random
= ssl
->s3
->server_random
;
2628 keys
->server_random_len
= SSL3_RANDOM_SIZE
;
2631 #endif /* CONFIG_FIPS */
2635 int tls_connection_prf(void *tls_ctx
, struct tls_connection
*conn
,
2636 const char *label
, int server_random_first
,
2637 u8
*out
, size_t out_len
)
2639 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
2643 if (server_random_first
)
2646 if (SSL_export_keying_material(ssl
, out
, out_len
, label
,
2647 os_strlen(label
), NULL
, 0, 0) == 1) {
2648 wpa_printf(MSG_DEBUG
, "OpenSSL: Using internal PRF");
2656 static struct wpabuf
*
2657 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
2661 struct wpabuf
*out_data
;
2664 * Give TLS handshake data from the server (if available) to OpenSSL
2668 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
2670 tls_show_errors(MSG_INFO
, __func__
,
2671 "Handshake failed - BIO_write");
2675 /* Initiate TLS handshake or continue the existing handshake */
2677 res
= SSL_accept(conn
->ssl
);
2679 res
= SSL_connect(conn
->ssl
);
2681 int err
= SSL_get_error(conn
->ssl
, res
);
2682 if (err
== SSL_ERROR_WANT_READ
)
2683 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
2685 else if (err
== SSL_ERROR_WANT_WRITE
)
2686 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
2689 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
2694 /* Get the TLS handshake data to be sent to the server */
2695 res
= BIO_ctrl_pending(conn
->ssl_out
);
2696 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
2697 out_data
= wpabuf_alloc(res
);
2698 if (out_data
== NULL
) {
2699 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
2700 "handshake output (%d bytes)", res
);
2701 if (BIO_reset(conn
->ssl_out
) < 0) {
2702 tls_show_errors(MSG_INFO
, __func__
,
2703 "BIO_reset failed");
2707 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
2710 tls_show_errors(MSG_INFO
, __func__
,
2711 "Handshake failed - BIO_read");
2712 if (BIO_reset(conn
->ssl_out
) < 0) {
2713 tls_show_errors(MSG_INFO
, __func__
,
2714 "BIO_reset failed");
2716 wpabuf_free(out_data
);
2719 wpabuf_put(out_data
, res
);
2725 static struct wpabuf
*
2726 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
2728 struct wpabuf
*appl_data
;
2731 appl_data
= wpabuf_alloc(max_len
+ 100);
2732 if (appl_data
== NULL
)
2735 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
2736 wpabuf_size(appl_data
));
2738 int err
= SSL_get_error(conn
->ssl
, res
);
2739 if (err
== SSL_ERROR_WANT_READ
||
2740 err
== SSL_ERROR_WANT_WRITE
) {
2741 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
2744 tls_show_errors(MSG_INFO
, __func__
,
2745 "Failed to read possible "
2746 "Application Data");
2748 wpabuf_free(appl_data
);
2752 wpabuf_put(appl_data
, res
);
2753 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
2754 "message", appl_data
);
2760 static struct wpabuf
*
2761 openssl_connection_handshake(struct tls_connection
*conn
,
2762 const struct wpabuf
*in_data
,
2763 struct wpabuf
**appl_data
, int server
)
2765 struct wpabuf
*out_data
;
2770 out_data
= openssl_handshake(conn
, in_data
, server
);
2771 if (out_data
== NULL
)
2773 if (conn
->invalid_hb_used
) {
2774 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
2775 wpabuf_free(out_data
);
2779 if (SSL_is_init_finished(conn
->ssl
) && appl_data
&& in_data
)
2780 *appl_data
= openssl_get_appl_data(conn
, wpabuf_len(in_data
));
2782 if (conn
->invalid_hb_used
) {
2783 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
2785 wpabuf_free(*appl_data
);
2788 wpabuf_free(out_data
);
2797 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
2798 const struct wpabuf
*in_data
,
2799 struct wpabuf
**appl_data
)
2801 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
2805 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
2806 struct tls_connection
*conn
,
2807 const struct wpabuf
*in_data
,
2808 struct wpabuf
**appl_data
)
2810 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
2814 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
2815 struct tls_connection
*conn
,
2816 const struct wpabuf
*in_data
)
2824 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
2825 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
2826 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
2827 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
2830 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
2832 tls_show_errors(MSG_INFO
, __func__
,
2833 "Encryption failed - SSL_write");
2837 /* Read encrypted data to be sent to the server */
2838 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
2841 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
2843 tls_show_errors(MSG_INFO
, __func__
,
2844 "Encryption failed - BIO_read");
2848 wpabuf_put(buf
, res
);
2854 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
2855 struct tls_connection
*conn
,
2856 const struct wpabuf
*in_data
)
2861 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
2862 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
2863 wpabuf_len(in_data
));
2865 tls_show_errors(MSG_INFO
, __func__
,
2866 "Decryption failed - BIO_write");
2869 if (BIO_reset(conn
->ssl_out
) < 0) {
2870 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
2874 /* Read decrypted data for further processing */
2876 * Even though we try to disable TLS compression, it is possible that
2877 * this cannot be done with all TLS libraries. Add extra buffer space
2878 * to handle the possibility of the decrypted data being longer than
2881 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
2884 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
2886 tls_show_errors(MSG_INFO
, __func__
,
2887 "Decryption failed - SSL_read");
2891 wpabuf_put(buf
, res
);
2893 if (conn
->invalid_hb_used
) {
2894 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
2903 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
2905 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
2906 return conn
? SSL_cache_hit(conn
->ssl
) : 0;
2908 return conn
? conn
->ssl
->hit
: 0;
2913 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
2916 char buf
[100], *pos
, *end
;
2920 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
2925 end
= pos
+ sizeof(buf
);
2928 while (*c
!= TLS_CIPHER_NONE
) {
2932 case TLS_CIPHER_RC4_SHA
:
2935 case TLS_CIPHER_AES128_SHA
:
2936 suite
= "AES128-SHA";
2938 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
2939 suite
= "DHE-RSA-AES128-SHA";
2941 case TLS_CIPHER_ANON_DH_AES128_SHA
:
2942 suite
= "ADH-AES128-SHA";
2945 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
2946 "cipher selection: %d", *c
);
2949 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
2950 if (os_snprintf_error(end
- pos
, ret
))
2957 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
2959 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
2960 tls_show_errors(MSG_INFO
, __func__
,
2961 "Cipher suite configuration failed");
2969 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
2970 char *buf
, size_t buflen
)
2973 if (conn
== NULL
|| conn
->ssl
== NULL
)
2976 name
= SSL_get_cipher(conn
->ssl
);
2980 os_strlcpy(buf
, name
, buflen
);
2985 int tls_connection_enable_workaround(void *ssl_ctx
,
2986 struct tls_connection
*conn
)
2988 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
2994 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2995 /* ClientHello TLS extensions require a patch to openssl, so this function is
2996 * commented out unless explicitly needed for EAP-FAST in order to be able to
2997 * build this file with unmodified openssl. */
2998 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
2999 int ext_type
, const u8
*data
,
3002 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
3005 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
3011 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3014 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
3018 return conn
->failed
;
3022 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3026 return conn
->read_alerts
;
3030 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3034 return conn
->write_alerts
;
3040 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
3042 #ifndef CONFIG_NO_STDOUT_DEBUG
3048 if (wpa_debug_level
> MSG_DEBUG
)
3051 out
= BIO_new(BIO_s_mem());
3055 OCSP_RESPONSE_print(out
, rsp
, 0);
3056 rlen
= BIO_ctrl_pending(out
);
3057 txt
= os_malloc(rlen
+ 1);
3063 res
= BIO_read(out
, txt
, rlen
);
3066 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
3070 #endif /* CONFIG_NO_STDOUT_DEBUG */
3074 static void debug_print_cert(X509
*cert
, const char *title
)
3076 #ifndef CONFIG_NO_STDOUT_DEBUG
3082 if (wpa_debug_level
> MSG_DEBUG
)
3085 out
= BIO_new(BIO_s_mem());
3089 X509_print(out
, cert
);
3090 rlen
= BIO_ctrl_pending(out
);
3091 txt
= os_malloc(rlen
+ 1);
3097 res
= BIO_read(out
, txt
, rlen
);
3100 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3105 #endif /* CONFIG_NO_STDOUT_DEBUG */
3109 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3111 struct tls_connection
*conn
= arg
;
3112 const unsigned char *p
;
3113 int len
, status
, reason
;
3115 OCSP_BASICRESP
*basic
;
3117 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3119 STACK_OF(X509
) *certs
= NULL
;
3121 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3123 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3124 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3127 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3129 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3131 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3135 ocsp_debug_print_resp(rsp
);
3137 status
= OCSP_response_status(rsp
);
3138 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3139 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3140 status
, OCSP_response_status_str(status
));
3144 basic
= OCSP_response_get1_basic(rsp
);
3146 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3150 store
= SSL_CTX_get_cert_store(conn
->ssl_ctx
);
3151 if (conn
->peer_issuer
) {
3152 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3154 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3155 tls_show_errors(MSG_INFO
, __func__
,
3156 "OpenSSL: Could not add issuer to certificate store");
3158 certs
= sk_X509_new_null();
3161 cert
= X509_dup(conn
->peer_issuer
);
3162 if (cert
&& !sk_X509_push(certs
, cert
)) {
3165 "OpenSSL: Could not add issuer to OCSP responder trust store");
3167 sk_X509_free(certs
);
3170 if (certs
&& conn
->peer_issuer_issuer
) {
3171 cert
= X509_dup(conn
->peer_issuer_issuer
);
3172 if (cert
&& !sk_X509_push(certs
, cert
)) {
3175 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
3182 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3183 sk_X509_pop_free(certs
, X509_free
);
3185 tls_show_errors(MSG_INFO
, __func__
,
3186 "OpenSSL: OCSP response failed verification");
3187 OCSP_BASICRESP_free(basic
);
3188 OCSP_RESPONSE_free(rsp
);
3192 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3194 if (!conn
->peer_cert
) {
3195 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3196 OCSP_BASICRESP_free(basic
);
3197 OCSP_RESPONSE_free(rsp
);
3201 if (!conn
->peer_issuer
) {
3202 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3203 OCSP_BASICRESP_free(basic
);
3204 OCSP_RESPONSE_free(rsp
);
3208 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3210 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not create OCSP certificate identifier");
3211 OCSP_BASICRESP_free(basic
);
3212 OCSP_RESPONSE_free(rsp
);
3216 if (!OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3217 &this_update
, &next_update
)) {
3218 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
3219 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
3220 " (OCSP not required)");
3221 OCSP_BASICRESP_free(basic
);
3222 OCSP_RESPONSE_free(rsp
);
3223 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3226 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
3227 tls_show_errors(MSG_INFO
, __func__
,
3228 "OpenSSL: OCSP status times invalid");
3229 OCSP_BASICRESP_free(basic
);
3230 OCSP_RESPONSE_free(rsp
);
3234 OCSP_BASICRESP_free(basic
);
3235 OCSP_RESPONSE_free(rsp
);
3237 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
3238 OCSP_cert_status_str(status
));
3240 if (status
== V_OCSP_CERTSTATUS_GOOD
)
3242 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
3244 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3245 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
3248 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
3253 static int ocsp_status_cb(SSL
*s
, void *arg
)
3259 if (tls_global
->ocsp_stapling_response
== NULL
) {
3260 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
3261 return SSL_TLSEXT_ERR_OK
;
3264 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
3266 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
3267 /* TODO: Build OCSPResponse with responseStatus = internalError
3269 return SSL_TLSEXT_ERR_OK
;
3271 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
3272 tmp
= OPENSSL_malloc(len
);
3275 return SSL_TLSEXT_ERR_ALERT_FATAL
;
3278 os_memcpy(tmp
, resp
, len
);
3280 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
3282 return SSL_TLSEXT_ERR_OK
;
3285 #endif /* HAVE_OCSP */
3288 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
3289 const struct tls_connection_params
*params
)
3294 const char *key_id
= params
->key_id
;
3295 const char *cert_id
= params
->cert_id
;
3296 const char *ca_cert_id
= params
->ca_cert_id
;
3297 const char *engine_id
= params
->engine
? params
->engine_id
: NULL
;
3303 * If the engine isn't explicitly configured, and any of the
3304 * cert/key fields are actually PKCS#11 URIs, then automatically
3305 * use the PKCS#11 ENGINE.
3307 if (!engine_id
|| os_strcmp(engine_id
, "pkcs11") == 0)
3310 if (!key_id
&& params
->private_key
&& can_pkcs11
&&
3311 os_strncmp(params
->private_key
, "pkcs11:", 7) == 0) {
3313 key_id
= params
->private_key
;
3316 if (!cert_id
&& params
->client_cert
&& can_pkcs11
&&
3317 os_strncmp(params
->client_cert
, "pkcs11:", 7) == 0) {
3319 cert_id
= params
->client_cert
;
3322 if (!ca_cert_id
&& params
->ca_cert
&& can_pkcs11
&&
3323 os_strncmp(params
->ca_cert
, "pkcs11:", 7) == 0) {
3325 ca_cert_id
= params
->ca_cert
;
3328 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3329 if (can_pkcs11
== 2 && !engine_id
)
3330 engine_id
= "pkcs11";
3332 if (params
->flags
& TLS_CONN_EAP_FAST
) {
3333 wpa_printf(MSG_DEBUG
,
3334 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3335 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
3336 tls_show_errors(MSG_INFO
, __func__
,
3337 "Failed to set TLSv1_method() for EAP-FAST");
3342 while ((err
= ERR_get_error())) {
3343 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3344 __func__
, ERR_error_string(err
, NULL
));
3348 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
3349 ret
= tls_engine_init(conn
, engine_id
, params
->pin
,
3350 key_id
, cert_id
, ca_cert_id
);
3354 if (tls_connection_set_subject_match(conn
,
3355 params
->subject_match
,
3356 params
->altsubject_match
,
3357 params
->suffix_match
,
3358 params
->domain_match
))
3361 if (engine_id
&& ca_cert_id
) {
3362 if (tls_connection_engine_ca_cert(tls_ctx
, conn
,
3364 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3365 } else if (tls_connection_ca_cert(tls_ctx
, conn
, params
->ca_cert
,
3366 params
->ca_cert_blob
,
3367 params
->ca_cert_blob_len
,
3371 if (engine_id
&& cert_id
) {
3372 if (tls_connection_engine_client_cert(conn
, cert_id
))
3373 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3374 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
3375 params
->client_cert_blob
,
3376 params
->client_cert_blob_len
))
3379 if (engine_id
&& key_id
) {
3380 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
3381 if (tls_connection_engine_private_key(conn
))
3382 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3383 } else if (tls_connection_private_key(tls_ctx
, conn
,
3384 params
->private_key
,
3385 params
->private_key_passwd
,
3386 params
->private_key_blob
,
3387 params
->private_key_blob_len
)) {
3388 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
3389 params
->private_key
);
3393 if (tls_connection_dh(conn
, params
->dh_file
)) {
3394 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3399 if (params
->openssl_ciphers
&&
3400 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
3401 wpa_printf(MSG_INFO
,
3402 "OpenSSL: Failed to set cipher string '%s'",
3403 params
->openssl_ciphers
);
3407 #ifdef SSL_OP_NO_TICKET
3408 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3409 SSL_set_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3410 #ifdef SSL_clear_options
3412 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3413 #endif /* SSL_clear_options */
3414 #endif /* SSL_OP_NO_TICKET */
3416 #ifdef SSL_OP_NO_TLSv1_1
3417 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_1
)
3418 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3420 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3421 #endif /* SSL_OP_NO_TLSv1_1 */
3422 #ifdef SSL_OP_NO_TLSv1_2
3423 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_2
)
3424 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3426 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3427 #endif /* SSL_OP_NO_TLSv1_2 */
3430 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3431 SSL_CTX
*ssl_ctx
= tls_ctx
;
3432 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
3433 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
3434 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
3436 #endif /* HAVE_OCSP */
3438 conn
->flags
= params
->flags
;
3440 tls_get_errors(tls_ctx
);
3446 int tls_global_set_params(void *tls_ctx
,
3447 const struct tls_connection_params
*params
)
3449 SSL_CTX
*ssl_ctx
= tls_ctx
;
3452 while ((err
= ERR_get_error())) {
3453 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3454 __func__
, ERR_error_string(err
, NULL
));
3457 if (tls_global_ca_cert(ssl_ctx
, params
->ca_cert
))
3460 if (tls_global_client_cert(ssl_ctx
, params
->client_cert
))
3463 if (tls_global_private_key(ssl_ctx
, params
->private_key
,
3464 params
->private_key_passwd
))
3467 if (tls_global_dh(ssl_ctx
, params
->dh_file
)) {
3468 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3473 if (params
->openssl_ciphers
&&
3474 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
3475 wpa_printf(MSG_INFO
,
3476 "OpenSSL: Failed to set cipher string '%s'",
3477 params
->openssl_ciphers
);
3481 #ifdef SSL_OP_NO_TICKET
3482 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3483 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3484 #ifdef SSL_CTX_clear_options
3486 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3487 #endif /* SSL_clear_options */
3488 #endif /* SSL_OP_NO_TICKET */
3491 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
3492 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
3493 os_free(tls_global
->ocsp_stapling_response
);
3494 if (params
->ocsp_stapling_response
)
3495 tls_global
->ocsp_stapling_response
=
3496 os_strdup(params
->ocsp_stapling_response
);
3498 tls_global
->ocsp_stapling_response
= NULL
;
3499 #endif /* HAVE_OCSP */
3505 int tls_connection_get_keyblock_size(void *tls_ctx
,
3506 struct tls_connection
*conn
)
3508 const EVP_CIPHER
*c
;
3512 if (conn
== NULL
|| conn
->ssl
== NULL
||
3513 conn
->ssl
->enc_read_ctx
== NULL
||
3514 conn
->ssl
->enc_read_ctx
->cipher
== NULL
||
3515 conn
->ssl
->read_hash
== NULL
)
3518 c
= conn
->ssl
->enc_read_ctx
->cipher
;
3519 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
3520 h
= EVP_MD_CTX_md(conn
->ssl
->read_hash
);
3522 h
= conn
->ssl
->read_hash
;
3525 md_size
= EVP_MD_size(h
);
3526 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
3527 else if (conn
->ssl
->s3
)
3528 md_size
= conn
->ssl
->s3
->tmp
.new_mac_secret_size
;
3533 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3534 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
3535 EVP_CIPHER_iv_length(c
));
3536 return 2 * (EVP_CIPHER_key_length(c
) +
3538 EVP_CIPHER_iv_length(c
));
3542 unsigned int tls_capabilities(void *tls_ctx
)
3548 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3549 /* Pre-shared secred requires a patch to openssl, so this function is
3550 * commented out unless explicitly needed for EAP-FAST in order to be able to
3551 * build this file with unmodified openssl. */
3553 #ifdef OPENSSL_IS_BORINGSSL
3554 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3555 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3556 const SSL_CIPHER
**cipher
, void *arg
)
3557 #else /* OPENSSL_IS_BORINGSSL */
3558 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3559 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3560 SSL_CIPHER
**cipher
, void *arg
)
3561 #endif /* OPENSSL_IS_BORINGSSL */
3563 struct tls_connection
*conn
= arg
;
3566 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3569 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
3570 conn
->session_ticket
,
3571 conn
->session_ticket_len
,
3572 s
->s3
->client_random
,
3573 s
->s3
->server_random
, secret
);
3574 os_free(conn
->session_ticket
);
3575 conn
->session_ticket
= NULL
;
3580 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
3585 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
3588 struct tls_connection
*conn
= arg
;
3590 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3593 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
3595 os_free(conn
->session_ticket
);
3596 conn
->session_ticket
= NULL
;
3598 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
3599 "extension", data
, len
);
3601 conn
->session_ticket
= os_malloc(len
);
3602 if (conn
->session_ticket
== NULL
)
3605 os_memcpy(conn
->session_ticket
, data
, len
);
3606 conn
->session_ticket_len
= len
;
3610 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3613 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
3614 struct tls_connection
*conn
,
3615 tls_session_ticket_cb cb
,
3618 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3619 conn
->session_ticket_cb
= cb
;
3620 conn
->session_ticket_cb_ctx
= ctx
;
3623 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
3626 SSL_set_session_ticket_ext_cb(conn
->ssl
,
3627 tls_session_ticket_ext_cb
, conn
);
3629 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
3631 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
3635 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3637 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3641 int tls_get_library_version(char *buf
, size_t buf_len
)
3643 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
3644 OPENSSL_VERSION_TEXT
,
3645 SSLeay_version(SSLEAY_VERSION
));