2 * SSL/TLS interface functions for OpenSSL
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
11 #ifndef CONFIG_SMARTCARD
12 #ifndef OPENSSL_NO_ENGINE
14 #define OPENSSL_NO_ENGINE
19 #include <openssl/ssl.h>
20 #include <openssl/err.h>
21 #include <openssl/pkcs12.h>
22 #include <openssl/x509v3.h>
23 #ifndef OPENSSL_NO_ENGINE
24 #include <openssl/engine.h>
25 #endif /* OPENSSL_NO_ENGINE */
26 #ifndef OPENSSL_NO_DSA
27 #include <openssl/dsa.h>
30 #include <openssl/dh.h>
39 #if OPENSSL_VERSION_NUMBER < 0x10000000L
40 /* ERR_remove_thread_state replaces ERR_remove_state and the latter is
41 * deprecated. However, OpenSSL 0.9.8 doesn't include
42 * ERR_remove_thread_state. */
43 #define ERR_remove_thread_state(tid) ERR_remove_state(0)
46 #if defined(OPENSSL_IS_BORINGSSL)
47 /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
48 typedef size_t stack_index_t
;
50 typedef int stack_index_t
;
53 #ifdef SSL_set_tlsext_status_type
54 #ifndef OPENSSL_NO_TLSEXT
56 #include <openssl/ocsp.h>
57 #endif /* OPENSSL_NO_TLSEXT */
58 #endif /* SSL_set_tlsext_status_type */
61 #include <openssl/pem.h>
62 #include <keystore/keystore_get.h>
64 static BIO
* BIO_from_keystore(const char *key
)
67 uint8_t *value
= NULL
;
68 int length
= keystore_get(key
, strlen(key
), &value
);
69 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
70 BIO_write(bio
, value
, length
);
76 static int tls_openssl_ref_count
= 0;
79 void (*event_cb
)(void *ctx
, enum tls_event ev
,
80 union tls_event_data
*data
);
83 char *ocsp_stapling_response
;
86 static struct tls_context
*tls_global
= NULL
;
89 struct tls_connection
{
90 struct tls_context
*context
;
93 BIO
*ssl_in
, *ssl_out
;
94 #ifndef OPENSSL_NO_ENGINE
95 ENGINE
*engine
; /* functional reference to the engine */
96 EVP_PKEY
*private_key
; /* the private key if using engine */
97 #endif /* OPENSSL_NO_ENGINE */
98 char *subject_match
, *altsubject_match
, *suffix_match
, *domain_match
;
99 int read_alerts
, write_alerts
, failed
;
101 tls_session_ticket_cb session_ticket_cb
;
102 void *session_ticket_cb_ctx
;
104 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
106 size_t session_ticket_len
;
108 unsigned int ca_cert_verify
:1;
109 unsigned int cert_probe
:1;
110 unsigned int server_cert_only
:1;
111 unsigned int invalid_hb_used
:1;
113 u8 srv_cert_hash
[32];
119 X509
*peer_issuer_issuer
;
121 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
122 unsigned char client_random
[SSL3_RANDOM_SIZE
];
123 unsigned char server_random
[SSL3_RANDOM_SIZE
];
128 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
130 struct tls_context
*context
= os_zalloc(sizeof(*context
));
134 context
->event_cb
= conf
->event_cb
;
135 context
->cb_ctx
= conf
->cb_ctx
;
136 context
->cert_in_cb
= conf
->cert_in_cb
;
142 #ifdef CONFIG_NO_STDOUT_DEBUG
144 static void _tls_show_errors(void)
148 while ((err
= ERR_get_error())) {
149 /* Just ignore the errors, since stdout is disabled */
152 #define tls_show_errors(l, f, t) _tls_show_errors()
154 #else /* CONFIG_NO_STDOUT_DEBUG */
156 static void tls_show_errors(int level
, const char *func
, const char *txt
)
160 wpa_printf(level
, "OpenSSL: %s - %s %s",
161 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
163 while ((err
= ERR_get_error())) {
164 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
165 ERR_error_string(err
, NULL
));
169 #endif /* CONFIG_NO_STDOUT_DEBUG */
172 #ifdef CONFIG_NATIVE_WINDOWS
174 /* Windows CryptoAPI and access to certificate stores */
175 #include <wincrypt.h>
177 #ifdef __MINGW32_VERSION
179 * MinGW does not yet include all the needed definitions for CryptoAPI, so
180 * define here whatever extra is needed.
182 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
183 #define CERT_STORE_READONLY_FLAG 0x00008000
184 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
186 #endif /* __MINGW32_VERSION */
189 struct cryptoapi_rsa_data
{
190 const CERT_CONTEXT
*cert
;
191 HCRYPTPROV crypt_prov
;
193 BOOL free_crypt_prov
;
197 static void cryptoapi_error(const char *msg
)
199 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
200 msg
, (unsigned int) GetLastError());
204 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
205 unsigned char *to
, RSA
*rsa
, int padding
)
207 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
212 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
213 unsigned char *to
, RSA
*rsa
, int padding
)
215 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
220 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
221 unsigned char *to
, RSA
*rsa
, int padding
)
223 struct cryptoapi_rsa_data
*priv
=
224 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
226 DWORD hash_size
, len
, i
;
227 unsigned char *buf
= NULL
;
231 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
232 ERR_R_PASSED_NULL_PARAMETER
);
236 if (padding
!= RSA_PKCS1_PADDING
) {
237 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
238 RSA_R_UNKNOWN_PADDING_TYPE
);
242 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
243 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
245 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
246 RSA_R_INVALID_MESSAGE_LENGTH
);
250 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
252 cryptoapi_error("CryptCreateHash failed");
256 len
= sizeof(hash_size
);
257 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
259 cryptoapi_error("CryptGetHashParam failed");
263 if ((int) hash_size
!= flen
) {
264 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
265 (unsigned) hash_size
, flen
);
266 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
267 RSA_R_INVALID_MESSAGE_LENGTH
);
270 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
271 cryptoapi_error("CryptSetHashParam failed");
276 buf
= os_malloc(len
);
278 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
282 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
283 cryptoapi_error("CryptSignHash failed");
287 for (i
= 0; i
< len
; i
++)
288 to
[i
] = buf
[len
- i
- 1];
293 CryptDestroyHash(hash
);
299 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
300 unsigned char *to
, RSA
*rsa
, int padding
)
302 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
307 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
311 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
312 CryptReleaseContext(priv
->crypt_prov
, 0);
314 CertFreeCertificateContext(priv
->cert
);
319 static int cryptoapi_finish(RSA
*rsa
)
321 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
322 os_free((void *) rsa
->meth
);
328 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
331 const CERT_CONTEXT
*ret
= NULL
;
333 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
334 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
335 CERT_STORE_READONLY_FLAG
, L
"MY");
337 cryptoapi_error("Failed to open 'My system store'");
341 if (strncmp(name
, "cert://", 7) == 0) {
342 unsigned short wbuf
[255];
343 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
344 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
346 0, CERT_FIND_SUBJECT_STR
,
348 } else if (strncmp(name
, "hash://", 7) == 0) {
349 CRYPT_HASH_BLOB blob
;
351 const char *hash
= name
+ 7;
354 len
= os_strlen(hash
) / 2;
355 buf
= os_malloc(len
);
356 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
359 ret
= CertFindCertificateInStore(cs
,
368 CertCloseStore(cs
, 0);
374 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
377 RSA
*rsa
= NULL
, *pub_rsa
;
378 struct cryptoapi_rsa_data
*priv
;
379 RSA_METHOD
*rsa_meth
;
382 (strncmp(name
, "cert://", 7) != 0 &&
383 strncmp(name
, "hash://", 7) != 0))
386 priv
= os_zalloc(sizeof(*priv
));
387 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
388 if (priv
== NULL
|| rsa_meth
== NULL
) {
389 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
390 "for CryptoAPI RSA method");
396 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
397 if (priv
->cert
== NULL
) {
398 priv
->cert
= cryptoapi_find_cert(
399 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
401 if (priv
->cert
== NULL
) {
402 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
407 cert
= d2i_X509(NULL
,
408 (const unsigned char **) &priv
->cert
->pbCertEncoded
,
409 priv
->cert
->cbCertEncoded
);
411 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
416 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
417 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
418 NULL
, &priv
->crypt_prov
,
420 &priv
->free_crypt_prov
)) {
421 cryptoapi_error("Failed to acquire a private key for the "
426 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
427 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
428 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
429 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
430 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
431 rsa_meth
->finish
= cryptoapi_finish
;
432 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
433 rsa_meth
->app_data
= (char *) priv
;
437 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
438 ERR_R_MALLOC_FAILURE
);
442 if (!SSL_use_certificate(ssl
, cert
)) {
447 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
451 rsa
->n
= BN_dup(pub_rsa
->n
);
452 rsa
->e
= BN_dup(pub_rsa
->e
);
453 if (!RSA_set_method(rsa
, rsa_meth
))
456 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
469 cryptoapi_free_data(priv
);
475 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
478 PCCERT_CONTEXT ctx
= NULL
;
486 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
491 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
494 wsprintf(wstore
, L
"%S", store
);
495 cs
= CertOpenSystemStore(0, wstore
);
498 cs
= CertOpenSystemStore(0, store
);
501 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
502 "'%s': error=%d", __func__
, store
,
503 (int) GetLastError());
507 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
508 cert
= d2i_X509(NULL
,
509 (const unsigned char **) &ctx
->pbCertEncoded
,
512 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
513 "X509 DER encoding for CA cert");
517 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
519 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
520 "system certificate store: subject='%s'", buf
);
522 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
523 tls_show_errors(MSG_WARNING
, __func__
,
524 "Failed to add ca_cert to OpenSSL "
525 "certificate store");
531 if (!CertCloseStore(cs
, 0)) {
532 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
533 "'%s': error=%d", __func__
, name
+ 13,
534 (int) GetLastError());
541 #else /* CONFIG_NATIVE_WINDOWS */
543 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
548 #endif /* CONFIG_NATIVE_WINDOWS */
551 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
556 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
557 w
= where
& ~SSL_ST_MASK
;
558 if (w
& SSL_ST_CONNECT
)
560 else if (w
& SSL_ST_ACCEPT
)
565 if (where
& SSL_CB_LOOP
) {
566 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
567 str
, SSL_state_string_long(ssl
));
568 } else if (where
& SSL_CB_ALERT
) {
569 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
570 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
571 where
& SSL_CB_READ
?
572 "read (remote end reported an error)" :
573 "write (local SSL3 detected an error)",
574 SSL_alert_type_string_long(ret
),
575 SSL_alert_desc_string_long(ret
));
576 if ((ret
>> 8) == SSL3_AL_FATAL
) {
577 if (where
& SSL_CB_READ
)
580 conn
->write_alerts
++;
582 if (conn
->context
->event_cb
!= NULL
) {
583 union tls_event_data ev
;
584 struct tls_context
*context
= conn
->context
;
585 os_memset(&ev
, 0, sizeof(ev
));
586 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
587 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
588 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
589 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
591 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
592 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
593 str
, ret
== 0 ? "failed" : "error",
594 SSL_state_string_long(ssl
));
599 #ifndef OPENSSL_NO_ENGINE
601 * tls_engine_load_dynamic_generic - load any openssl engine
602 * @pre: an array of commands and values that load an engine initialized
603 * in the engine specific function
604 * @post: an array of commands and values that initialize an already loaded
605 * engine (or %NULL if not required)
606 * @id: the engine id of the engine to load (only required if post is not %NULL
608 * This function is a generic function that loads any openssl engine.
610 * Returns: 0 on success, -1 on failure
612 static int tls_engine_load_dynamic_generic(const char *pre
[],
613 const char *post
[], const char *id
)
616 const char *dynamic_id
= "dynamic";
618 engine
= ENGINE_by_id(id
);
621 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
627 engine
= ENGINE_by_id(dynamic_id
);
628 if (engine
== NULL
) {
629 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
631 ERR_error_string(ERR_get_error(), NULL
));
635 /* Perform the pre commands. This will load the engine. */
636 while (pre
&& pre
[0]) {
637 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
638 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
639 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
640 "%s %s [%s]", pre
[0], pre
[1],
641 ERR_error_string(ERR_get_error(), NULL
));
649 * Free the reference to the "dynamic" engine. The loaded engine can
650 * now be looked up using ENGINE_by_id().
654 engine
= ENGINE_by_id(id
);
655 if (engine
== NULL
) {
656 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
657 id
, ERR_error_string(ERR_get_error(), NULL
));
661 while (post
&& post
[0]) {
662 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
663 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
664 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
665 " %s %s [%s]", post
[0], post
[1],
666 ERR_error_string(ERR_get_error(), NULL
));
667 ENGINE_remove(engine
);
680 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
681 * @pkcs11_so_path: pksc11_so_path from the configuration
682 * @pcks11_module_path: pkcs11_module_path from the configuration
684 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
685 const char *pkcs11_module_path
)
687 char *engine_id
= "pkcs11";
688 const char *pre_cmd
[] = {
689 "SO_PATH", NULL
/* pkcs11_so_path */,
690 "ID", NULL
/* engine_id */,
692 /* "NO_VCHECK", "1", */
696 const char *post_cmd
[] = {
697 "MODULE_PATH", NULL
/* pkcs11_module_path */,
704 pre_cmd
[1] = pkcs11_so_path
;
705 pre_cmd
[3] = engine_id
;
706 if (pkcs11_module_path
)
707 post_cmd
[1] = pkcs11_module_path
;
711 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
714 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
719 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
720 * @opensc_so_path: opensc_so_path from the configuration
722 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
724 char *engine_id
= "opensc";
725 const char *pre_cmd
[] = {
726 "SO_PATH", NULL
/* opensc_so_path */,
727 "ID", NULL
/* engine_id */,
736 pre_cmd
[1] = opensc_so_path
;
737 pre_cmd
[3] = engine_id
;
739 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
742 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
744 #endif /* OPENSSL_NO_ENGINE */
747 void * tls_init(const struct tls_config
*conf
)
750 struct tls_context
*context
;
753 if (tls_openssl_ref_count
== 0) {
754 tls_global
= context
= tls_context_new(conf
);
759 if (conf
&& conf
->fips_mode
) {
760 static int fips_enabled
= 0;
762 if (!fips_enabled
&& !FIPS_mode_set(1)) {
763 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
765 ERR_load_crypto_strings();
766 ERR_print_errors_fp(stderr
);
771 wpa_printf(MSG_INFO
, "Running in FIPS mode");
775 #else /* OPENSSL_FIPS */
776 if (conf
&& conf
->fips_mode
) {
777 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
783 #endif /* OPENSSL_FIPS */
784 #endif /* CONFIG_FIPS */
785 SSL_load_error_strings();
787 #ifndef OPENSSL_NO_SHA256
788 EVP_add_digest(EVP_sha256());
789 #endif /* OPENSSL_NO_SHA256 */
790 /* TODO: if /dev/urandom is available, PRNG is seeded
791 * automatically. If this is not the case, random data should
795 #ifndef OPENSSL_NO_RC2
797 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
798 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
799 * versions, but it looks like OpenSSL 1.0.0 does not do that
802 EVP_add_cipher(EVP_rc2_40_cbc());
803 #endif /* OPENSSL_NO_RC2 */
805 #endif /* PKCS12_FUNCS */
807 context
= tls_context_new(conf
);
811 tls_openssl_ref_count
++;
813 ssl
= SSL_CTX_new(SSLv23_method());
815 tls_openssl_ref_count
--;
816 if (context
!= tls_global
)
818 if (tls_openssl_ref_count
== 0) {
825 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
826 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
828 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
829 SSL_CTX_set_app_data(ssl
, context
);
831 #ifndef OPENSSL_NO_ENGINE
832 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
833 ERR_load_ENGINE_strings();
834 ENGINE_load_dynamic();
837 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
838 conf
->pkcs11_module_path
)) {
839 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
840 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
841 conf
->pkcs11_module_path
)) {
846 #endif /* OPENSSL_NO_ENGINE */
848 if (conf
&& conf
->openssl_ciphers
)
849 ciphers
= conf
->openssl_ciphers
;
851 ciphers
= "DEFAULT:!EXP:!LOW";
852 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
853 wpa_printf(MSG_ERROR
,
854 "OpenSSL: Failed to set cipher string '%s'",
864 void tls_deinit(void *ssl_ctx
)
866 SSL_CTX
*ssl
= ssl_ctx
;
867 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
868 if (context
!= tls_global
)
872 tls_openssl_ref_count
--;
873 if (tls_openssl_ref_count
== 0) {
874 #ifndef OPENSSL_NO_ENGINE
876 #endif /* OPENSSL_NO_ENGINE */
877 CRYPTO_cleanup_all_ex_data();
878 ERR_remove_thread_state(NULL
);
881 os_free(tls_global
->ocsp_stapling_response
);
882 tls_global
->ocsp_stapling_response
= NULL
;
889 #ifndef OPENSSL_NO_ENGINE
891 /* Cryptoki return values */
892 #define CKR_PIN_INCORRECT 0x000000a0
893 #define CKR_PIN_INVALID 0x000000a1
894 #define CKR_PIN_LEN_RANGE 0x000000a2
897 #define ERR_LIB_PKCS11 ERR_LIB_USER
899 static int tls_is_pin_error(unsigned int err
)
901 return ERR_GET_LIB(err
) == ERR_LIB_PKCS11
&&
902 (ERR_GET_REASON(err
) == CKR_PIN_INCORRECT
||
903 ERR_GET_REASON(err
) == CKR_PIN_INVALID
||
904 ERR_GET_REASON(err
) == CKR_PIN_LEN_RANGE
);
907 #endif /* OPENSSL_NO_ENGINE */
910 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
911 const char *pin
, const char *key_id
,
912 const char *cert_id
, const char *ca_cert_id
)
914 #ifndef OPENSSL_NO_ENGINE
916 if (engine_id
== NULL
) {
917 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
923 ENGINE_load_dynamic();
925 conn
->engine
= ENGINE_by_id(engine_id
);
927 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
928 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
931 if (ENGINE_init(conn
->engine
) != 1) {
932 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
933 "(engine: %s) [%s]", engine_id
,
934 ERR_error_string(ERR_get_error(), NULL
));
937 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
940 if (pin
&& ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
941 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
942 ERR_error_string(ERR_get_error(), NULL
));
948 * Ensure that the ENGINE does not attempt to use the OpenSSL
949 * UI system to obtain a PIN, if we didn't provide one.
952 const void *password
;
953 const char *prompt_info
;
954 } key_cb
= { "", NULL
};
956 /* load private key first in-case PIN is required for cert */
957 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
960 if (!conn
->private_key
) {
961 unsigned long err
= ERR_get_error();
963 wpa_printf(MSG_ERROR
,
964 "ENGINE: cannot load private key with id '%s' [%s]",
966 ERR_error_string(err
, NULL
));
967 if (tls_is_pin_error(err
))
968 ret
= TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
970 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
975 /* handle a certificate and/or CA certificate */
976 if (cert_id
|| ca_cert_id
) {
977 const char *cmd_name
= "LOAD_CERT_CTRL";
979 /* test if the engine supports a LOAD_CERT_CTRL */
980 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
981 0, (void *)cmd_name
, NULL
)) {
982 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
983 " loading certificates");
984 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
993 ENGINE_free(conn
->engine
);
997 if (conn
->private_key
) {
998 EVP_PKEY_free(conn
->private_key
);
999 conn
->private_key
= NULL
;
1003 #else /* OPENSSL_NO_ENGINE */
1005 #endif /* OPENSSL_NO_ENGINE */
1009 static void tls_engine_deinit(struct tls_connection
*conn
)
1011 #ifndef OPENSSL_NO_ENGINE
1012 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
1013 if (conn
->private_key
) {
1014 EVP_PKEY_free(conn
->private_key
);
1015 conn
->private_key
= NULL
;
1018 ENGINE_finish(conn
->engine
);
1019 conn
->engine
= NULL
;
1021 #endif /* OPENSSL_NO_ENGINE */
1025 int tls_get_errors(void *ssl_ctx
)
1030 while ((err
= ERR_get_error())) {
1031 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1032 ERR_error_string(err
, NULL
));
1040 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1041 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1043 struct tls_connection
*conn
= arg
;
1044 const u8
*pos
= buf
;
1046 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d",
1047 write_p
? "TX" : "RX", version
, content_type
);
1048 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1049 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1050 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1051 if (payload_len
+ 3 > len
) {
1052 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1053 conn
->invalid_hb_used
= 1;
1059 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1061 SSL_CTX
*ssl
= ssl_ctx
;
1062 struct tls_connection
*conn
;
1064 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1066 conn
= os_zalloc(sizeof(*conn
));
1069 conn
->ssl_ctx
= ssl_ctx
;
1070 conn
->ssl
= SSL_new(ssl
);
1071 if (conn
->ssl
== NULL
) {
1072 tls_show_errors(MSG_INFO
, __func__
,
1073 "Failed to initialize new SSL connection");
1078 conn
->context
= context
;
1079 SSL_set_app_data(conn
->ssl
, conn
);
1080 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1081 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1082 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1083 SSL_OP_SINGLE_DH_USE
;
1084 #ifdef SSL_OP_NO_COMPRESSION
1085 options
|= SSL_OP_NO_COMPRESSION
;
1086 #endif /* SSL_OP_NO_COMPRESSION */
1087 SSL_set_options(conn
->ssl
, options
);
1089 conn
->ssl_in
= BIO_new(BIO_s_mem());
1090 if (!conn
->ssl_in
) {
1091 tls_show_errors(MSG_INFO
, __func__
,
1092 "Failed to create a new BIO for ssl_in");
1093 SSL_free(conn
->ssl
);
1098 conn
->ssl_out
= BIO_new(BIO_s_mem());
1099 if (!conn
->ssl_out
) {
1100 tls_show_errors(MSG_INFO
, __func__
,
1101 "Failed to create a new BIO for ssl_out");
1102 SSL_free(conn
->ssl
);
1103 BIO_free(conn
->ssl_in
);
1108 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1114 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1118 SSL_free(conn
->ssl
);
1119 tls_engine_deinit(conn
);
1120 os_free(conn
->subject_match
);
1121 os_free(conn
->altsubject_match
);
1122 os_free(conn
->suffix_match
);
1123 os_free(conn
->domain_match
);
1124 os_free(conn
->session_ticket
);
1129 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1131 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1135 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1140 /* Shutdown previous TLS connection without notifying the peer
1141 * because the connection was already terminated in practice
1142 * and "close notify" shutdown alert would confuse AS. */
1143 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1144 SSL_shutdown(conn
->ssl
);
1145 return SSL_clear(conn
->ssl
) == 1 ? 0 : -1;
1149 static int tls_match_altsubject_component(X509
*cert
, int type
,
1150 const char *value
, size_t len
)
1157 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1159 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1160 gen
= sk_GENERAL_NAME_value(ext
, i
);
1161 if (gen
->type
!= type
)
1163 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1164 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1172 static int tls_match_altsubject(X509
*cert
, const char *match
)
1175 const char *pos
, *end
;
1180 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1183 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1186 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1190 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1194 end
= os_strchr(pos
, ';');
1196 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1197 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1198 os_strncmp(end
+ 1, "URI:", 4) == 0)
1200 end
= os_strchr(end
+ 1, ';');
1205 len
= os_strlen(pos
);
1206 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1215 #ifndef CONFIG_NATIVE_WINDOWS
1216 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
,
1219 size_t i
, match_len
;
1221 /* Check for embedded nuls that could mess up suffix matching */
1222 for (i
= 0; i
< len
; i
++) {
1223 if (val
[i
] == '\0') {
1224 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1229 match_len
= os_strlen(match
);
1230 if (match_len
> len
|| (full
&& match_len
!= len
))
1233 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1235 return 0; /* no match */
1237 if (match_len
== len
)
1238 return 1; /* exact match */
1240 if (val
[len
- match_len
- 1] == '.')
1241 return 1; /* full label match completes suffix match */
1243 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1246 #endif /* CONFIG_NATIVE_WINDOWS */
1249 static int tls_match_suffix(X509
*cert
, const char *match
, int full
)
1251 #ifdef CONFIG_NATIVE_WINDOWS
1252 /* wincrypt.h has conflicting X509_NAME definition */
1254 #else /* CONFIG_NATIVE_WINDOWS */
1262 wpa_printf(MSG_DEBUG
, "TLS: Match domain against %s%s",
1263 full
? "": "suffix ", match
);
1265 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1267 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1268 gen
= sk_GENERAL_NAME_value(ext
, j
);
1269 if (gen
->type
!= GEN_DNS
)
1272 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1273 gen
->d
.dNSName
->data
,
1274 gen
->d
.dNSName
->length
);
1275 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1276 gen
->d
.dNSName
->length
, match
, full
) ==
1278 wpa_printf(MSG_DEBUG
, "TLS: %s in dNSName found",
1279 full
? "Match" : "Suffix match");
1285 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1289 name
= X509_get_subject_name(cert
);
1295 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1298 e
= X509_NAME_get_entry(name
, i
);
1301 cn
= X509_NAME_ENTRY_get_data(e
);
1304 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1305 cn
->data
, cn
->length
);
1306 if (domain_suffix_match(cn
->data
, cn
->length
, match
, full
) == 1)
1308 wpa_printf(MSG_DEBUG
, "TLS: %s in commonName found",
1309 full
? "Match" : "Suffix match");
1314 wpa_printf(MSG_DEBUG
, "TLS: No CommonName %smatch found",
1315 full
? "": "suffix ");
1317 #endif /* CONFIG_NATIVE_WINDOWS */
1321 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1324 case X509_V_ERR_CERT_REVOKED
:
1325 return TLS_FAIL_REVOKED
;
1326 case X509_V_ERR_CERT_NOT_YET_VALID
:
1327 case X509_V_ERR_CRL_NOT_YET_VALID
:
1328 return TLS_FAIL_NOT_YET_VALID
;
1329 case X509_V_ERR_CERT_HAS_EXPIRED
:
1330 case X509_V_ERR_CRL_HAS_EXPIRED
:
1331 return TLS_FAIL_EXPIRED
;
1332 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1333 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1334 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1335 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1336 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1337 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1338 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1339 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1340 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1341 case X509_V_ERR_INVALID_CA
:
1342 return TLS_FAIL_UNTRUSTED
;
1343 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1344 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1345 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1346 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1347 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1348 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1349 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1350 case X509_V_ERR_CERT_UNTRUSTED
:
1351 case X509_V_ERR_CERT_REJECTED
:
1352 return TLS_FAIL_BAD_CERTIFICATE
;
1354 return TLS_FAIL_UNSPECIFIED
;
1359 static struct wpabuf
* get_x509_cert(X509
*cert
)
1364 int cert_len
= i2d_X509(cert
, NULL
);
1368 buf
= wpabuf_alloc(cert_len
);
1372 tmp
= wpabuf_put(buf
, cert_len
);
1373 i2d_X509(cert
, &tmp
);
1378 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1379 X509
*err_cert
, int err
, int depth
,
1380 const char *subject
, const char *err_str
,
1381 enum tls_fail_reason reason
)
1383 union tls_event_data ev
;
1384 struct wpabuf
*cert
= NULL
;
1385 struct tls_context
*context
= conn
->context
;
1387 if (context
->event_cb
== NULL
)
1390 cert
= get_x509_cert(err_cert
);
1391 os_memset(&ev
, 0, sizeof(ev
));
1392 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1393 reason
: openssl_tls_fail_reason(err
);
1394 ev
.cert_fail
.depth
= depth
;
1395 ev
.cert_fail
.subject
= subject
;
1396 ev
.cert_fail
.reason_txt
= err_str
;
1397 ev
.cert_fail
.cert
= cert
;
1398 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1403 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1404 X509
*err_cert
, int depth
,
1405 const char *subject
)
1407 struct wpabuf
*cert
= NULL
;
1408 union tls_event_data ev
;
1409 struct tls_context
*context
= conn
->context
;
1410 char *altsubject
[TLS_MAX_ALT_SUBJECT
];
1411 int alt
, num_altsubject
= 0;
1415 #ifdef CONFIG_SHA256
1417 #endif /* CONFIG_SHA256 */
1419 if (context
->event_cb
== NULL
)
1422 os_memset(&ev
, 0, sizeof(ev
));
1423 if (conn
->cert_probe
|| context
->cert_in_cb
) {
1424 cert
= get_x509_cert(err_cert
);
1425 ev
.peer_cert
.cert
= cert
;
1427 #ifdef CONFIG_SHA256
1431 addr
[0] = wpabuf_head(cert
);
1432 len
[0] = wpabuf_len(cert
);
1433 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1434 ev
.peer_cert
.hash
= hash
;
1435 ev
.peer_cert
.hash_len
= sizeof(hash
);
1438 #endif /* CONFIG_SHA256 */
1439 ev
.peer_cert
.depth
= depth
;
1440 ev
.peer_cert
.subject
= subject
;
1442 ext
= X509_get_ext_d2i(err_cert
, NID_subject_alt_name
, NULL
, NULL
);
1443 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1446 if (num_altsubject
== TLS_MAX_ALT_SUBJECT
)
1448 gen
= sk_GENERAL_NAME_value(ext
, i
);
1449 if (gen
->type
!= GEN_EMAIL
&&
1450 gen
->type
!= GEN_DNS
&&
1451 gen
->type
!= GEN_URI
)
1454 pos
= os_malloc(10 + gen
->d
.ia5
->length
+ 1);
1457 altsubject
[num_altsubject
++] = pos
;
1459 switch (gen
->type
) {
1461 os_memcpy(pos
, "EMAIL:", 6);
1465 os_memcpy(pos
, "DNS:", 4);
1469 os_memcpy(pos
, "URI:", 4);
1474 os_memcpy(pos
, gen
->d
.ia5
->data
, gen
->d
.ia5
->length
);
1475 pos
+= gen
->d
.ia5
->length
;
1479 for (alt
= 0; alt
< num_altsubject
; alt
++)
1480 ev
.peer_cert
.altsubject
[alt
] = altsubject
[alt
];
1481 ev
.peer_cert
.num_altsubject
= num_altsubject
;
1483 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1485 for (alt
= 0; alt
< num_altsubject
; alt
++)
1486 os_free(altsubject
[alt
]);
1490 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1496 struct tls_connection
*conn
;
1497 struct tls_context
*context
;
1498 char *match
, *altmatch
, *suffix_match
, *domain_match
;
1499 const char *err_str
;
1501 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1505 err
= X509_STORE_CTX_get_error(x509_ctx
);
1506 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1507 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1508 SSL_get_ex_data_X509_STORE_CTX_idx());
1509 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1511 conn
= SSL_get_app_data(ssl
);
1516 conn
->peer_cert
= err_cert
;
1517 else if (depth
== 1)
1518 conn
->peer_issuer
= err_cert
;
1519 else if (depth
== 2)
1520 conn
->peer_issuer_issuer
= err_cert
;
1522 context
= conn
->context
;
1523 match
= conn
->subject_match
;
1524 altmatch
= conn
->altsubject_match
;
1525 suffix_match
= conn
->suffix_match
;
1526 domain_match
= conn
->domain_match
;
1528 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1530 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1532 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1533 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1534 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1535 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1540 err_str
= X509_verify_cert_error_string(err
);
1542 #ifdef CONFIG_SHA256
1544 * Do not require preverify_ok so we can explicity allow otherwise
1545 * invalid pinned server certificates.
1547 if (depth
== 0 && conn
->server_cert_only
) {
1548 struct wpabuf
*cert
;
1549 cert
= get_x509_cert(err_cert
);
1551 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1552 "server certificate data");
1558 addr
[0] = wpabuf_head(cert
);
1559 len
[0] = wpabuf_len(cert
);
1560 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1561 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1562 err_str
= "Server certificate mismatch";
1563 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1565 } else if (!preverify_ok
) {
1567 * Certificate matches pinned certificate, allow
1568 * regardless of other problems.
1570 wpa_printf(MSG_DEBUG
,
1571 "OpenSSL: Ignore validation issues for a pinned server certificate");
1577 #endif /* CONFIG_SHA256 */
1579 if (!preverify_ok
) {
1580 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1581 " error %d (%s) depth %d for '%s'", err
, err_str
,
1583 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1584 err_str
, TLS_FAIL_UNSPECIFIED
);
1585 return preverify_ok
;
1588 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1589 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1590 preverify_ok
, err
, err_str
,
1591 conn
->ca_cert_verify
, depth
, buf
);
1592 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1593 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1594 "match with '%s'", buf
, match
);
1596 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1598 TLS_FAIL_SUBJECT_MISMATCH
);
1599 } else if (depth
== 0 && altmatch
&&
1600 !tls_match_altsubject(err_cert
, altmatch
)) {
1601 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1602 "'%s' not found", altmatch
);
1604 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1605 "AltSubject mismatch",
1606 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1607 } else if (depth
== 0 && suffix_match
&&
1608 !tls_match_suffix(err_cert
, suffix_match
, 0)) {
1609 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1612 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1613 "Domain suffix mismatch",
1614 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1615 } else if (depth
== 0 && domain_match
&&
1616 !tls_match_suffix(err_cert
, domain_match
, 1)) {
1617 wpa_printf(MSG_WARNING
, "TLS: Domain match '%s' not found",
1620 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1622 TLS_FAIL_DOMAIN_MISMATCH
);
1624 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1626 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1627 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1628 "on probe-only run");
1630 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1631 "Server certificate chain probe",
1632 TLS_FAIL_SERVER_CHAIN_PROBE
);
1635 if (preverify_ok
&& context
->event_cb
!= NULL
)
1636 context
->event_cb(context
->cb_ctx
,
1637 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1639 return preverify_ok
;
1643 #ifndef OPENSSL_NO_STDIO
1644 static int tls_load_ca_der(void *_ssl_ctx
, const char *ca_cert
)
1646 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1647 X509_LOOKUP
*lookup
;
1650 lookup
= X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx
),
1651 X509_LOOKUP_file());
1652 if (lookup
== NULL
) {
1653 tls_show_errors(MSG_WARNING
, __func__
,
1654 "Failed add lookup for X509 store");
1658 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1659 unsigned long err
= ERR_peek_error();
1660 tls_show_errors(MSG_WARNING
, __func__
,
1661 "Failed load CA in DER format");
1662 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1663 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1664 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1665 "cert already in hash table error",
1673 #endif /* OPENSSL_NO_STDIO */
1676 static int tls_connection_ca_cert(void *_ssl_ctx
, struct tls_connection
*conn
,
1677 const char *ca_cert
, const u8
*ca_cert_blob
,
1678 size_t ca_cert_blob_len
, const char *ca_path
)
1680 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1684 * Remove previously configured trusted CA certificates before adding
1687 store
= X509_STORE_new();
1688 if (store
== NULL
) {
1689 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
1690 "certificate store", __func__
);
1693 SSL_CTX_set_cert_store(ssl_ctx
, store
);
1695 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1696 conn
->ca_cert_verify
= 1;
1698 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
1699 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
1701 conn
->cert_probe
= 1;
1702 conn
->ca_cert_verify
= 0;
1706 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
1707 #ifdef CONFIG_SHA256
1708 const char *pos
= ca_cert
+ 7;
1709 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
1710 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
1711 "hash value '%s'", ca_cert
);
1715 if (os_strlen(pos
) != 32 * 2) {
1716 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
1717 "hash length in ca_cert '%s'", ca_cert
);
1720 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
1721 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
1722 "value in ca_cert '%s'", ca_cert
);
1725 conn
->server_cert_only
= 1;
1726 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
1727 "certificate match");
1729 #else /* CONFIG_SHA256 */
1730 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
1731 "cannot validate server certificate hash");
1733 #endif /* CONFIG_SHA256 */
1737 X509
*cert
= d2i_X509(NULL
,
1738 (const unsigned char **) &ca_cert_blob
,
1741 tls_show_errors(MSG_WARNING
, __func__
,
1742 "Failed to parse ca_cert_blob");
1746 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
1748 unsigned long err
= ERR_peek_error();
1749 tls_show_errors(MSG_WARNING
, __func__
,
1750 "Failed to add ca_cert_blob to "
1751 "certificate store");
1752 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1753 ERR_GET_REASON(err
) ==
1754 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1755 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1756 "cert already in hash table error",
1764 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
1765 "to certificate store", __func__
);
1770 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
1771 BIO
*bio
= BIO_from_keystore(&ca_cert
[11]);
1772 STACK_OF(X509_INFO
) *stack
= NULL
;
1776 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
1782 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
1783 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
1785 X509_STORE_add_cert(ssl_ctx
->cert_store
,
1789 X509_STORE_add_crl(ssl_ctx
->cert_store
,
1793 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
1794 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1797 #endif /* ANDROID */
1799 #ifdef CONFIG_NATIVE_WINDOWS
1800 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
1802 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
1803 "system certificate store");
1806 #endif /* CONFIG_NATIVE_WINDOWS */
1808 if (ca_cert
|| ca_path
) {
1809 #ifndef OPENSSL_NO_STDIO
1810 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
1812 tls_show_errors(MSG_WARNING
, __func__
,
1813 "Failed to load root certificates");
1815 tls_load_ca_der(ssl_ctx
, ca_cert
) == 0) {
1816 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
1817 "DER format CA certificate",
1822 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1823 "certificate(s) loaded");
1824 tls_get_errors(ssl_ctx
);
1826 #else /* OPENSSL_NO_STDIO */
1827 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
1830 #endif /* OPENSSL_NO_STDIO */
1832 /* No ca_cert configured - do not try to verify server
1834 conn
->ca_cert_verify
= 0;
1841 static int tls_global_ca_cert(SSL_CTX
*ssl_ctx
, const char *ca_cert
)
1844 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
1846 tls_show_errors(MSG_WARNING
, __func__
,
1847 "Failed to load root certificates");
1851 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1852 "certificate(s) loaded");
1854 #ifndef OPENSSL_NO_STDIO
1855 /* Add the same CAs to the client certificate requests */
1856 SSL_CTX_set_client_CA_list(ssl_ctx
,
1857 SSL_load_client_CA_file(ca_cert
));
1858 #endif /* OPENSSL_NO_STDIO */
1865 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
1870 X509_STORE
*cs
= SSL_CTX_get_cert_store(ssl_ctx
);
1872 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
1873 "certificate store when enabling "
1877 flags
= X509_V_FLAG_CRL_CHECK
;
1879 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
1880 X509_STORE_set_flags(cs
, flags
);
1886 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
1887 const char *subject_match
,
1888 const char *altsubject_match
,
1889 const char *suffix_match
,
1890 const char *domain_match
)
1892 os_free(conn
->subject_match
);
1893 conn
->subject_match
= NULL
;
1894 if (subject_match
) {
1895 conn
->subject_match
= os_strdup(subject_match
);
1896 if (conn
->subject_match
== NULL
)
1900 os_free(conn
->altsubject_match
);
1901 conn
->altsubject_match
= NULL
;
1902 if (altsubject_match
) {
1903 conn
->altsubject_match
= os_strdup(altsubject_match
);
1904 if (conn
->altsubject_match
== NULL
)
1908 os_free(conn
->suffix_match
);
1909 conn
->suffix_match
= NULL
;
1911 conn
->suffix_match
= os_strdup(suffix_match
);
1912 if (conn
->suffix_match
== NULL
)
1916 os_free(conn
->domain_match
);
1917 conn
->domain_match
= NULL
;
1919 conn
->domain_match
= os_strdup(domain_match
);
1920 if (conn
->domain_match
== NULL
)
1928 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
1931 static int counter
= 0;
1937 conn
->ca_cert_verify
= 1;
1938 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
1939 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1940 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
1942 conn
->ca_cert_verify
= 0;
1943 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
1946 SSL_set_accept_state(conn
->ssl
);
1949 * Set session id context in order to avoid fatal errors when client
1950 * tries to resume a session. However, set the context to a unique
1951 * value in order to effectively disable session resumption for now
1952 * since not all areas of the server code are ready for it (e.g.,
1953 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1957 SSL_set_session_id_context(conn
->ssl
,
1958 (const unsigned char *) &counter
,
1965 static int tls_connection_client_cert(struct tls_connection
*conn
,
1966 const char *client_cert
,
1967 const u8
*client_cert_blob
,
1968 size_t client_cert_blob_len
)
1970 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
1973 if (client_cert_blob
&&
1974 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
1975 client_cert_blob_len
) == 1) {
1976 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
1979 } else if (client_cert_blob
) {
1980 tls_show_errors(MSG_DEBUG
, __func__
,
1981 "SSL_use_certificate_ASN1 failed");
1984 if (client_cert
== NULL
)
1988 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
1989 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
1993 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
1997 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
2003 #endif /* ANDROID */
2005 #ifndef OPENSSL_NO_STDIO
2006 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2007 SSL_FILETYPE_ASN1
) == 1) {
2008 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
2013 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2014 SSL_FILETYPE_PEM
) == 1) {
2016 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
2021 tls_show_errors(MSG_DEBUG
, __func__
,
2022 "SSL_use_certificate_file failed");
2023 #else /* OPENSSL_NO_STDIO */
2024 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2025 #endif /* OPENSSL_NO_STDIO */
2031 static int tls_global_client_cert(SSL_CTX
*ssl_ctx
, const char *client_cert
)
2033 #ifndef OPENSSL_NO_STDIO
2034 if (client_cert
== NULL
)
2037 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2038 SSL_FILETYPE_ASN1
) != 1 &&
2039 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
2040 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2041 SSL_FILETYPE_PEM
) != 1) {
2042 tls_show_errors(MSG_INFO
, __func__
,
2043 "Failed to load client certificate");
2047 #else /* OPENSSL_NO_STDIO */
2048 if (client_cert
== NULL
)
2050 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2052 #endif /* OPENSSL_NO_STDIO */
2056 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
2058 if (password
== NULL
) {
2061 os_strlcpy(buf
, (char *) password
, size
);
2062 return os_strlen(buf
);
2067 static int tls_parse_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, PKCS12
*p12
,
2072 STACK_OF(X509
) *certs
;
2079 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
2080 tls_show_errors(MSG_DEBUG
, __func__
,
2081 "Failed to parse PKCS12 file");
2085 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
2088 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2090 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
2091 "subject='%s'", buf
);
2093 if (SSL_use_certificate(ssl
, cert
) != 1)
2096 if (SSL_CTX_use_certificate(ssl_ctx
, cert
) != 1)
2103 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
2105 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
2108 if (SSL_CTX_use_PrivateKey(ssl_ctx
, pkey
) != 1)
2111 EVP_PKEY_free(pkey
);
2115 #if OPENSSL_VERSION_NUMBER >= 0x10002000L
2116 SSL_clear_chain_certs(ssl
);
2117 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2118 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2120 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2121 " from PKCS12: subject='%s'", buf
);
2122 if (SSL_add1_chain_cert(ssl
, cert
) != 1) {
2127 sk_X509_free(certs
);
2128 #ifndef OPENSSL_IS_BORINGSSL
2129 res
= SSL_build_cert_chain(ssl
,
2130 SSL_BUILD_CHAIN_FLAG_CHECK
|
2131 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
);
2133 tls_show_errors(MSG_DEBUG
, __func__
,
2134 "Failed to build certificate chain");
2135 } else if (res
== 2) {
2136 wpa_printf(MSG_DEBUG
,
2137 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2139 #endif /* OPENSSL_IS_BORINGSSL */
2141 * Try to continue regardless of result since it is possible for
2142 * the extra certificates not to be required.
2145 #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2146 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
2147 SSL_CTX_clear_extra_chain_certs(ssl_ctx
);
2148 #endif /* OPENSSL_VERSION_NUMBER >= 0x10001000L */
2149 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2150 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2152 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2153 " from PKCS12: subject='%s'", buf
);
2155 * There is no SSL equivalent for the chain cert - so
2156 * always add it to the context...
2158 if (SSL_CTX_add_extra_chain_cert(ssl_ctx
, cert
) != 1) {
2163 sk_X509_free(certs
);
2164 #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2170 tls_get_errors(ssl_ctx
);
2174 #endif /* PKCS12_FUNCS */
2177 static int tls_read_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *private_key
,
2184 f
= fopen(private_key
, "rb");
2188 p12
= d2i_PKCS12_fp(f
, NULL
);
2192 tls_show_errors(MSG_INFO
, __func__
,
2193 "Failed to use PKCS#12 file");
2197 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2199 #else /* PKCS12_FUNCS */
2200 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2203 #endif /* PKCS12_FUNCS */
2207 static int tls_read_pkcs12_blob(SSL_CTX
*ssl_ctx
, SSL
*ssl
,
2208 const u8
*blob
, size_t len
, const char *passwd
)
2213 p12
= d2i_PKCS12(NULL
, (const unsigned char **) &blob
, len
);
2215 tls_show_errors(MSG_INFO
, __func__
,
2216 "Failed to use PKCS#12 blob");
2220 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
2222 #else /* PKCS12_FUNCS */
2223 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2226 #endif /* PKCS12_FUNCS */
2230 #ifndef OPENSSL_NO_ENGINE
2231 static int tls_engine_get_cert(struct tls_connection
*conn
,
2232 const char *cert_id
,
2235 /* this runs after the private key is loaded so no PIN is required */
2237 const char *cert_id
;
2240 params
.cert_id
= cert_id
;
2243 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2244 0, ¶ms
, NULL
, 1)) {
2245 unsigned long err
= ERR_get_error();
2247 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2248 " '%s' [%s]", cert_id
,
2249 ERR_error_string(err
, NULL
));
2250 if (tls_is_pin_error(err
))
2251 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
2252 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2255 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2257 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2259 *cert
= params
.cert
;
2262 #endif /* OPENSSL_NO_ENGINE */
2265 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2266 const char *cert_id
)
2268 #ifndef OPENSSL_NO_ENGINE
2271 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2274 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2275 tls_show_errors(MSG_ERROR
, __func__
,
2276 "SSL_use_certificate failed");
2281 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2285 #else /* OPENSSL_NO_ENGINE */
2287 #endif /* OPENSSL_NO_ENGINE */
2291 static int tls_connection_engine_ca_cert(void *_ssl_ctx
,
2292 struct tls_connection
*conn
,
2293 const char *ca_cert_id
)
2295 #ifndef OPENSSL_NO_ENGINE
2297 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2300 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2303 /* start off the same as tls_connection_ca_cert */
2304 store
= X509_STORE_new();
2305 if (store
== NULL
) {
2306 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2307 "certificate store", __func__
);
2311 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2312 if (!X509_STORE_add_cert(store
, cert
)) {
2313 unsigned long err
= ERR_peek_error();
2314 tls_show_errors(MSG_WARNING
, __func__
,
2315 "Failed to add CA certificate from engine "
2316 "to certificate store");
2317 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2318 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2319 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2320 " already in hash table error",
2328 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2329 "to certificate store", __func__
);
2330 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2331 conn
->ca_cert_verify
= 1;
2335 #else /* OPENSSL_NO_ENGINE */
2337 #endif /* OPENSSL_NO_ENGINE */
2341 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2343 #ifndef OPENSSL_NO_ENGINE
2344 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2345 tls_show_errors(MSG_ERROR
, __func__
,
2346 "ENGINE: cannot use private key for TLS");
2349 if (!SSL_check_private_key(conn
->ssl
)) {
2350 tls_show_errors(MSG_INFO
, __func__
,
2351 "Private key failed verification");
2355 #else /* OPENSSL_NO_ENGINE */
2356 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2357 "engine support was not compiled in");
2359 #endif /* OPENSSL_NO_ENGINE */
2363 static int tls_connection_private_key(void *_ssl_ctx
,
2364 struct tls_connection
*conn
,
2365 const char *private_key
,
2366 const char *private_key_passwd
,
2367 const u8
*private_key_blob
,
2368 size_t private_key_blob_len
)
2370 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
2374 if (private_key
== NULL
&& private_key_blob
== NULL
)
2377 if (private_key_passwd
) {
2378 passwd
= os_strdup(private_key_passwd
);
2384 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2385 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2388 while (private_key_blob
) {
2389 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2390 (u8
*) private_key_blob
,
2391 private_key_blob_len
) == 1) {
2392 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2393 "ASN1(EVP_PKEY_RSA) --> OK");
2398 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2399 (u8
*) private_key_blob
,
2400 private_key_blob_len
) == 1) {
2401 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2402 "ASN1(EVP_PKEY_DSA) --> OK");
2407 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2408 (u8
*) private_key_blob
,
2409 private_key_blob_len
) == 1) {
2410 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2411 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2416 if (tls_read_pkcs12_blob(ssl_ctx
, conn
->ssl
, private_key_blob
,
2417 private_key_blob_len
, passwd
) == 0) {
2418 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2427 while (!ok
&& private_key
) {
2428 #ifndef OPENSSL_NO_STDIO
2429 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2430 SSL_FILETYPE_ASN1
) == 1) {
2431 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2432 "SSL_use_PrivateKey_File (DER) --> OK");
2437 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2438 SSL_FILETYPE_PEM
) == 1) {
2439 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2440 "SSL_use_PrivateKey_File (PEM) --> OK");
2444 #else /* OPENSSL_NO_STDIO */
2445 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2447 #endif /* OPENSSL_NO_STDIO */
2449 if (tls_read_pkcs12(ssl_ctx
, conn
->ssl
, private_key
, passwd
)
2451 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2457 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2458 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2459 "access certificate store --> OK");
2468 tls_show_errors(MSG_INFO
, __func__
,
2469 "Failed to load private key");
2474 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2477 if (!SSL_check_private_key(conn
->ssl
)) {
2478 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
2483 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
2488 static int tls_global_private_key(SSL_CTX
*ssl_ctx
, const char *private_key
,
2489 const char *private_key_passwd
)
2493 if (private_key
== NULL
)
2496 if (private_key_passwd
) {
2497 passwd
= os_strdup(private_key_passwd
);
2503 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2504 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2506 #ifndef OPENSSL_NO_STDIO
2507 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2508 SSL_FILETYPE_ASN1
) != 1 &&
2509 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2510 SSL_FILETYPE_PEM
) != 1 &&
2511 #endif /* OPENSSL_NO_STDIO */
2512 tls_read_pkcs12(ssl_ctx
, NULL
, private_key
, passwd
)) {
2513 tls_show_errors(MSG_INFO
, __func__
,
2514 "Failed to load private key");
2521 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2523 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
2524 tls_show_errors(MSG_INFO
, __func__
,
2525 "Private key failed verification");
2533 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
2535 #ifdef OPENSSL_NO_DH
2536 if (dh_file
== NULL
)
2538 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2539 "dh_file specified");
2541 #else /* OPENSSL_NO_DH */
2545 /* TODO: add support for dh_blob */
2546 if (dh_file
== NULL
)
2551 bio
= BIO_new_file(dh_file
, "r");
2553 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2554 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2557 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2559 #ifndef OPENSSL_NO_DSA
2560 while (dh
== NULL
) {
2562 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2563 " trying to parse as DSA params", dh_file
,
2564 ERR_error_string(ERR_get_error(), NULL
));
2565 bio
= BIO_new_file(dh_file
, "r");
2568 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2571 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2572 "'%s': %s", dh_file
,
2573 ERR_error_string(ERR_get_error(), NULL
));
2577 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2578 dh
= DSA_dup_DH(dsa
);
2581 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2582 "params into DH params");
2587 #endif /* !OPENSSL_NO_DSA */
2589 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2594 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
2595 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2597 ERR_error_string(ERR_get_error(), NULL
));
2603 #endif /* OPENSSL_NO_DH */
2607 static int tls_global_dh(SSL_CTX
*ssl_ctx
, const char *dh_file
)
2609 #ifdef OPENSSL_NO_DH
2610 if (dh_file
== NULL
)
2612 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2613 "dh_file specified");
2615 #else /* OPENSSL_NO_DH */
2619 /* TODO: add support for dh_blob */
2620 if (dh_file
== NULL
)
2622 if (ssl_ctx
== NULL
)
2625 bio
= BIO_new_file(dh_file
, "r");
2627 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2628 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2631 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2633 #ifndef OPENSSL_NO_DSA
2634 while (dh
== NULL
) {
2636 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2637 " trying to parse as DSA params", dh_file
,
2638 ERR_error_string(ERR_get_error(), NULL
));
2639 bio
= BIO_new_file(dh_file
, "r");
2642 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2645 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2646 "'%s': %s", dh_file
,
2647 ERR_error_string(ERR_get_error(), NULL
));
2651 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2652 dh
= DSA_dup_DH(dsa
);
2655 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2656 "params into DH params");
2661 #endif /* !OPENSSL_NO_DSA */
2663 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
2668 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
2669 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
2671 ERR_error_string(ERR_get_error(), NULL
));
2677 #endif /* OPENSSL_NO_DH */
2681 int tls_connection_get_random(void *ssl_ctx
, struct tls_connection
*conn
,
2682 struct tls_random
*keys
)
2686 if (conn
== NULL
|| keys
== NULL
)
2689 #if OPENSSL_VERSION_NUMBER < 0x10100000L
2690 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
)
2693 os_memset(keys
, 0, sizeof(*keys
));
2694 keys
->client_random
= ssl
->s3
->client_random
;
2695 keys
->client_random_len
= SSL3_RANDOM_SIZE
;
2696 keys
->server_random
= ssl
->s3
->server_random
;
2697 keys
->server_random_len
= SSL3_RANDOM_SIZE
;
2702 os_memset(keys
, 0, sizeof(*keys
));
2703 keys
->client_random
= conn
->client_random
;
2704 keys
->client_random_len
= SSL_get_client_random(
2705 ssl
, conn
->client_random
, sizeof(conn
->client_random
));
2706 keys
->server_random
= conn
->server_random
;
2707 keys
->server_random_len
= SSL_get_server_random(
2708 ssl
, conn
->server_random
, sizeof(conn
->server_random
));
2716 static int openssl_get_keyblock_size(SSL
*ssl
)
2718 #if OPENSSL_VERSION_NUMBER < 0x10100000L
2719 const EVP_CIPHER
*c
;
2723 if (ssl
->enc_read_ctx
== NULL
|| ssl
->enc_read_ctx
->cipher
== NULL
||
2724 ssl
->read_hash
== NULL
)
2727 c
= ssl
->enc_read_ctx
->cipher
;
2728 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
2729 h
= EVP_MD_CTX_md(ssl
->read_hash
);
2734 md_size
= EVP_MD_size(h
);
2735 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
2737 md_size
= ssl
->s3
->tmp
.new_mac_secret_size
;
2742 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
2743 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
2744 EVP_CIPHER_iv_length(c
));
2745 return 2 * (EVP_CIPHER_key_length(c
) +
2747 EVP_CIPHER_iv_length(c
));
2749 const SSL_CIPHER
*ssl_cipher
;
2751 const EVP_CIPHER
*c
;
2754 ssl_cipher
= SSL_get_current_cipher(ssl
);
2757 cipher
= SSL_CIPHER_get_cipher_nid(ssl_cipher
);
2758 digest
= SSL_CIPHER_get_digest_nid(ssl_cipher
);
2759 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher nid %d digest nid %d",
2761 if (cipher
< 0 || digest
< 0)
2763 c
= EVP_get_cipherbynid(cipher
);
2764 h
= EVP_get_digestbynid(digest
);
2768 wpa_printf(MSG_DEBUG
,
2769 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
2770 EVP_CIPHER_key_length(c
), EVP_MD_size(h
),
2771 EVP_CIPHER_iv_length(c
));
2772 return 2 * (EVP_CIPHER_key_length(c
) + EVP_MD_size(h
) +
2773 EVP_CIPHER_iv_length(c
));
2776 #endif /* CONFIG_FIPS */
2779 static int openssl_tls_prf(void *tls_ctx
, struct tls_connection
*conn
,
2780 const char *label
, int server_random_first
,
2781 int skip_keyblock
, u8
*out
, size_t out_len
)
2784 wpa_printf(MSG_ERROR
, "OpenSSL: TLS keys cannot be exported in FIPS "
2787 #else /* CONFIG_FIPS */
2788 #if OPENSSL_VERSION_NUMBER < 0x10100000L
2798 * TLS library did not support key generation, so get the needed TLS
2799 * session parameters and use an internal implementation of TLS PRF to
2806 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
||
2807 ssl
->session
->master_key_length
<= 0)
2809 ver
= SSL_get_version(ssl
);
2811 if (skip_keyblock
) {
2812 skip
= openssl_get_keyblock_size(ssl
);
2815 tmp_out
= os_malloc(skip
+ out_len
);
2821 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
2827 if (server_random_first
) {
2828 os_memcpy(rnd
, ssl
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2829 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, ssl
->s3
->client_random
,
2832 os_memcpy(rnd
, ssl
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2833 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, ssl
->s3
->server_random
,
2837 if (os_strcmp(ver
, "TLSv1.2") == 0) {
2838 tls_prf_sha256(ssl
->session
->master_key
,
2839 ssl
->session
->master_key_length
,
2840 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2841 _out
, skip
+ out_len
);
2843 } else if (tls_prf_sha1_md5(ssl
->session
->master_key
,
2844 ssl
->session
->master_key_length
,
2845 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2846 _out
, skip
+ out_len
) == 0) {
2850 if (ret
== 0 && skip_keyblock
)
2851 os_memcpy(out
, _out
+ skip
, out_len
);
2852 bin_clear_free(tmp_out
, skip
);
2863 unsigned char client_random
[SSL3_RANDOM_SIZE
];
2864 unsigned char server_random
[SSL3_RANDOM_SIZE
];
2865 unsigned char master_key
[64];
2866 size_t master_key_len
;
2870 * TLS library did not support key generation, so get the needed TLS
2871 * session parameters and use an internal implementation of TLS PRF to
2880 ver
= SSL_get_version(ssl
);
2881 sess
= SSL_get_session(ssl
);
2885 if (skip_keyblock
) {
2886 skip
= openssl_get_keyblock_size(ssl
);
2889 tmp_out
= os_malloc(skip
+ out_len
);
2895 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
2901 SSL_get_client_random(ssl
, client_random
, sizeof(client_random
));
2902 SSL_get_server_random(ssl
, server_random
, sizeof(server_random
));
2903 master_key_len
= SSL_SESSION_get_master_key(sess
, master_key
,
2904 sizeof(master_key
));
2906 if (server_random_first
) {
2907 os_memcpy(rnd
, server_random
, SSL3_RANDOM_SIZE
);
2908 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, client_random
,
2911 os_memcpy(rnd
, client_random
, SSL3_RANDOM_SIZE
);
2912 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, server_random
,
2916 if (os_strcmp(ver
, "TLSv1.2") == 0) {
2917 tls_prf_sha256(master_key
, master_key_len
,
2918 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2919 _out
, skip
+ out_len
);
2921 } else if (tls_prf_sha1_md5(master_key
, master_key_len
,
2922 label
, rnd
, 2 * SSL3_RANDOM_SIZE
,
2923 _out
, skip
+ out_len
) == 0) {
2926 os_memset(master_key
, 0, sizeof(master_key
));
2928 if (ret
== 0 && skip_keyblock
)
2929 os_memcpy(out
, _out
+ skip
, out_len
);
2930 bin_clear_free(tmp_out
, skip
);
2934 #endif /* CONFIG_FIPS */
2938 int tls_connection_prf(void *tls_ctx
, struct tls_connection
*conn
,
2939 const char *label
, int server_random_first
,
2940 int skip_keyblock
, u8
*out
, size_t out_len
)
2942 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
2946 if (server_random_first
|| skip_keyblock
)
2947 return openssl_tls_prf(tls_ctx
, conn
, label
,
2948 server_random_first
, skip_keyblock
,
2951 if (SSL_export_keying_material(ssl
, out
, out_len
, label
,
2952 os_strlen(label
), NULL
, 0, 0) == 1) {
2953 wpa_printf(MSG_DEBUG
, "OpenSSL: Using internal PRF");
2957 return openssl_tls_prf(tls_ctx
, conn
, label
, server_random_first
,
2958 skip_keyblock
, out
, out_len
);
2962 static struct wpabuf
*
2963 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
2967 struct wpabuf
*out_data
;
2970 * Give TLS handshake data from the server (if available) to OpenSSL
2973 if (in_data
&& wpabuf_len(in_data
) > 0 &&
2974 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
2976 tls_show_errors(MSG_INFO
, __func__
,
2977 "Handshake failed - BIO_write");
2981 /* Initiate TLS handshake or continue the existing handshake */
2983 res
= SSL_accept(conn
->ssl
);
2985 res
= SSL_connect(conn
->ssl
);
2987 int err
= SSL_get_error(conn
->ssl
, res
);
2988 if (err
== SSL_ERROR_WANT_READ
)
2989 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
2991 else if (err
== SSL_ERROR_WANT_WRITE
)
2992 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
2995 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
3000 /* Get the TLS handshake data to be sent to the server */
3001 res
= BIO_ctrl_pending(conn
->ssl_out
);
3002 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
3003 out_data
= wpabuf_alloc(res
);
3004 if (out_data
== NULL
) {
3005 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
3006 "handshake output (%d bytes)", res
);
3007 if (BIO_reset(conn
->ssl_out
) < 0) {
3008 tls_show_errors(MSG_INFO
, __func__
,
3009 "BIO_reset failed");
3013 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
3016 tls_show_errors(MSG_INFO
, __func__
,
3017 "Handshake failed - BIO_read");
3018 if (BIO_reset(conn
->ssl_out
) < 0) {
3019 tls_show_errors(MSG_INFO
, __func__
,
3020 "BIO_reset failed");
3022 wpabuf_free(out_data
);
3025 wpabuf_put(out_data
, res
);
3031 static struct wpabuf
*
3032 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
3034 struct wpabuf
*appl_data
;
3037 appl_data
= wpabuf_alloc(max_len
+ 100);
3038 if (appl_data
== NULL
)
3041 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
3042 wpabuf_size(appl_data
));
3044 int err
= SSL_get_error(conn
->ssl
, res
);
3045 if (err
== SSL_ERROR_WANT_READ
||
3046 err
== SSL_ERROR_WANT_WRITE
) {
3047 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
3050 tls_show_errors(MSG_INFO
, __func__
,
3051 "Failed to read possible "
3052 "Application Data");
3054 wpabuf_free(appl_data
);
3058 wpabuf_put(appl_data
, res
);
3059 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
3060 "message", appl_data
);
3066 static struct wpabuf
*
3067 openssl_connection_handshake(struct tls_connection
*conn
,
3068 const struct wpabuf
*in_data
,
3069 struct wpabuf
**appl_data
, int server
)
3071 struct wpabuf
*out_data
;
3076 out_data
= openssl_handshake(conn
, in_data
, server
);
3077 if (out_data
== NULL
)
3079 if (conn
->invalid_hb_used
) {
3080 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3081 wpabuf_free(out_data
);
3085 if (SSL_is_init_finished(conn
->ssl
) && appl_data
&& in_data
)
3086 *appl_data
= openssl_get_appl_data(conn
, wpabuf_len(in_data
));
3088 if (conn
->invalid_hb_used
) {
3089 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3091 wpabuf_free(*appl_data
);
3094 wpabuf_free(out_data
);
3103 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
3104 const struct wpabuf
*in_data
,
3105 struct wpabuf
**appl_data
)
3107 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
3111 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
3112 struct tls_connection
*conn
,
3113 const struct wpabuf
*in_data
,
3114 struct wpabuf
**appl_data
)
3116 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
3120 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
3121 struct tls_connection
*conn
,
3122 const struct wpabuf
*in_data
)
3130 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3131 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
3132 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
3133 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3136 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
3138 tls_show_errors(MSG_INFO
, __func__
,
3139 "Encryption failed - SSL_write");
3143 /* Read encrypted data to be sent to the server */
3144 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
3147 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3149 tls_show_errors(MSG_INFO
, __func__
,
3150 "Encryption failed - BIO_read");
3154 wpabuf_put(buf
, res
);
3160 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
3161 struct tls_connection
*conn
,
3162 const struct wpabuf
*in_data
)
3167 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3168 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
3169 wpabuf_len(in_data
));
3171 tls_show_errors(MSG_INFO
, __func__
,
3172 "Decryption failed - BIO_write");
3175 if (BIO_reset(conn
->ssl_out
) < 0) {
3176 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3180 /* Read decrypted data for further processing */
3182 * Even though we try to disable TLS compression, it is possible that
3183 * this cannot be done with all TLS libraries. Add extra buffer space
3184 * to handle the possibility of the decrypted data being longer than
3187 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
3190 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3192 tls_show_errors(MSG_INFO
, __func__
,
3193 "Decryption failed - SSL_read");
3197 wpabuf_put(buf
, res
);
3199 if (conn
->invalid_hb_used
) {
3200 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3209 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
3211 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
3212 return conn
? SSL_cache_hit(conn
->ssl
) : 0;
3214 return conn
? conn
->ssl
->hit
: 0;
3219 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
3222 char buf
[100], *pos
, *end
;
3226 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
3231 end
= pos
+ sizeof(buf
);
3234 while (*c
!= TLS_CIPHER_NONE
) {
3238 case TLS_CIPHER_RC4_SHA
:
3241 case TLS_CIPHER_AES128_SHA
:
3242 suite
= "AES128-SHA";
3244 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
3245 suite
= "DHE-RSA-AES128-SHA";
3247 case TLS_CIPHER_ANON_DH_AES128_SHA
:
3248 suite
= "ADH-AES128-SHA";
3251 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
3252 "cipher selection: %d", *c
);
3255 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
3256 if (os_snprintf_error(end
- pos
, ret
))
3263 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
3265 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3266 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3267 if (os_strstr(buf
, ":ADH-")) {
3269 * Need to drop to security level 0 to allow anonymous
3270 * cipher suites for EAP-FAST.
3272 SSL_set_security_level(conn
->ssl
, 0);
3273 } else if (SSL_get_security_level(conn
->ssl
) == 0) {
3274 /* Force at least security level 1 */
3275 SSL_set_security_level(conn
->ssl
, 1);
3277 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3280 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
3281 tls_show_errors(MSG_INFO
, __func__
,
3282 "Cipher suite configuration failed");
3290 int tls_get_version(void *ssl_ctx
, struct tls_connection
*conn
,
3291 char *buf
, size_t buflen
)
3294 if (conn
== NULL
|| conn
->ssl
== NULL
)
3297 name
= SSL_get_version(conn
->ssl
);
3301 os_strlcpy(buf
, name
, buflen
);
3306 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
3307 char *buf
, size_t buflen
)
3310 if (conn
== NULL
|| conn
->ssl
== NULL
)
3313 name
= SSL_get_cipher(conn
->ssl
);
3317 os_strlcpy(buf
, name
, buflen
);
3322 int tls_connection_enable_workaround(void *ssl_ctx
,
3323 struct tls_connection
*conn
)
3325 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3331 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3332 /* ClientHello TLS extensions require a patch to openssl, so this function is
3333 * commented out unless explicitly needed for EAP-FAST in order to be able to
3334 * build this file with unmodified openssl. */
3335 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
3336 int ext_type
, const u8
*data
,
3339 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
3342 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
3348 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3351 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
3355 return conn
->failed
;
3359 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3363 return conn
->read_alerts
;
3367 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3371 return conn
->write_alerts
;
3377 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
3379 #ifndef CONFIG_NO_STDOUT_DEBUG
3385 if (wpa_debug_level
> MSG_DEBUG
)
3388 out
= BIO_new(BIO_s_mem());
3392 OCSP_RESPONSE_print(out
, rsp
, 0);
3393 rlen
= BIO_ctrl_pending(out
);
3394 txt
= os_malloc(rlen
+ 1);
3400 res
= BIO_read(out
, txt
, rlen
);
3403 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
3407 #endif /* CONFIG_NO_STDOUT_DEBUG */
3411 static void debug_print_cert(X509
*cert
, const char *title
)
3413 #ifndef CONFIG_NO_STDOUT_DEBUG
3419 if (wpa_debug_level
> MSG_DEBUG
)
3422 out
= BIO_new(BIO_s_mem());
3426 X509_print(out
, cert
);
3427 rlen
= BIO_ctrl_pending(out
);
3428 txt
= os_malloc(rlen
+ 1);
3434 res
= BIO_read(out
, txt
, rlen
);
3437 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3442 #endif /* CONFIG_NO_STDOUT_DEBUG */
3446 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3448 struct tls_connection
*conn
= arg
;
3449 const unsigned char *p
;
3450 int len
, status
, reason
;
3452 OCSP_BASICRESP
*basic
;
3454 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3456 STACK_OF(X509
) *certs
= NULL
;
3458 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3460 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3461 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3464 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3466 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3468 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3472 ocsp_debug_print_resp(rsp
);
3474 status
= OCSP_response_status(rsp
);
3475 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3476 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3477 status
, OCSP_response_status_str(status
));
3481 basic
= OCSP_response_get1_basic(rsp
);
3483 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3487 store
= SSL_CTX_get_cert_store(conn
->ssl_ctx
);
3488 if (conn
->peer_issuer
) {
3489 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3491 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3492 tls_show_errors(MSG_INFO
, __func__
,
3493 "OpenSSL: Could not add issuer to certificate store");
3495 certs
= sk_X509_new_null();
3498 cert
= X509_dup(conn
->peer_issuer
);
3499 if (cert
&& !sk_X509_push(certs
, cert
)) {
3502 "OpenSSL: Could not add issuer to OCSP responder trust store");
3504 sk_X509_free(certs
);
3507 if (certs
&& conn
->peer_issuer_issuer
) {
3508 cert
= X509_dup(conn
->peer_issuer_issuer
);
3509 if (cert
&& !sk_X509_push(certs
, cert
)) {
3512 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
3519 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3520 sk_X509_pop_free(certs
, X509_free
);
3522 tls_show_errors(MSG_INFO
, __func__
,
3523 "OpenSSL: OCSP response failed verification");
3524 OCSP_BASICRESP_free(basic
);
3525 OCSP_RESPONSE_free(rsp
);
3529 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3531 if (!conn
->peer_cert
) {
3532 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3533 OCSP_BASICRESP_free(basic
);
3534 OCSP_RESPONSE_free(rsp
);
3538 if (!conn
->peer_issuer
) {
3539 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3540 OCSP_BASICRESP_free(basic
);
3541 OCSP_RESPONSE_free(rsp
);
3545 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3547 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not create OCSP certificate identifier");
3548 OCSP_BASICRESP_free(basic
);
3549 OCSP_RESPONSE_free(rsp
);
3553 if (!OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3554 &this_update
, &next_update
)) {
3555 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
3556 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
3557 " (OCSP not required)");
3558 OCSP_BASICRESP_free(basic
);
3559 OCSP_RESPONSE_free(rsp
);
3560 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3563 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
3564 tls_show_errors(MSG_INFO
, __func__
,
3565 "OpenSSL: OCSP status times invalid");
3566 OCSP_BASICRESP_free(basic
);
3567 OCSP_RESPONSE_free(rsp
);
3571 OCSP_BASICRESP_free(basic
);
3572 OCSP_RESPONSE_free(rsp
);
3574 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
3575 OCSP_cert_status_str(status
));
3577 if (status
== V_OCSP_CERTSTATUS_GOOD
)
3579 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
3581 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3582 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
3585 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
3590 static int ocsp_status_cb(SSL
*s
, void *arg
)
3596 if (tls_global
->ocsp_stapling_response
== NULL
) {
3597 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
3598 return SSL_TLSEXT_ERR_OK
;
3601 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
3603 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
3604 /* TODO: Build OCSPResponse with responseStatus = internalError
3606 return SSL_TLSEXT_ERR_OK
;
3608 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
3609 tmp
= OPENSSL_malloc(len
);
3612 return SSL_TLSEXT_ERR_ALERT_FATAL
;
3615 os_memcpy(tmp
, resp
, len
);
3617 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
3619 return SSL_TLSEXT_ERR_OK
;
3622 #endif /* HAVE_OCSP */
3625 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
3626 const struct tls_connection_params
*params
)
3631 const char *key_id
= params
->key_id
;
3632 const char *cert_id
= params
->cert_id
;
3633 const char *ca_cert_id
= params
->ca_cert_id
;
3634 const char *engine_id
= params
->engine
? params
->engine_id
: NULL
;
3640 * If the engine isn't explicitly configured, and any of the
3641 * cert/key fields are actually PKCS#11 URIs, then automatically
3642 * use the PKCS#11 ENGINE.
3644 if (!engine_id
|| os_strcmp(engine_id
, "pkcs11") == 0)
3647 if (!key_id
&& params
->private_key
&& can_pkcs11
&&
3648 os_strncmp(params
->private_key
, "pkcs11:", 7) == 0) {
3650 key_id
= params
->private_key
;
3653 if (!cert_id
&& params
->client_cert
&& can_pkcs11
&&
3654 os_strncmp(params
->client_cert
, "pkcs11:", 7) == 0) {
3656 cert_id
= params
->client_cert
;
3659 if (!ca_cert_id
&& params
->ca_cert
&& can_pkcs11
&&
3660 os_strncmp(params
->ca_cert
, "pkcs11:", 7) == 0) {
3662 ca_cert_id
= params
->ca_cert
;
3665 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3666 if (can_pkcs11
== 2 && !engine_id
)
3667 engine_id
= "pkcs11";
3669 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3670 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3671 if (params
->flags
& TLS_CONN_EAP_FAST
) {
3672 wpa_printf(MSG_DEBUG
,
3673 "OpenSSL: Use TLSv1_method() for EAP-FAST");
3674 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
3675 tls_show_errors(MSG_INFO
, __func__
,
3676 "Failed to set TLSv1_method() for EAP-FAST");
3681 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3683 while ((err
= ERR_get_error())) {
3684 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3685 __func__
, ERR_error_string(err
, NULL
));
3689 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
3690 ret
= tls_engine_init(conn
, engine_id
, params
->pin
,
3691 key_id
, cert_id
, ca_cert_id
);
3695 if (tls_connection_set_subject_match(conn
,
3696 params
->subject_match
,
3697 params
->altsubject_match
,
3698 params
->suffix_match
,
3699 params
->domain_match
))
3702 if (engine_id
&& ca_cert_id
) {
3703 if (tls_connection_engine_ca_cert(tls_ctx
, conn
,
3705 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3706 } else if (tls_connection_ca_cert(tls_ctx
, conn
, params
->ca_cert
,
3707 params
->ca_cert_blob
,
3708 params
->ca_cert_blob_len
,
3712 if (engine_id
&& cert_id
) {
3713 if (tls_connection_engine_client_cert(conn
, cert_id
))
3714 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3715 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
3716 params
->client_cert_blob
,
3717 params
->client_cert_blob_len
))
3720 if (engine_id
&& key_id
) {
3721 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
3722 if (tls_connection_engine_private_key(conn
))
3723 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
3724 } else if (tls_connection_private_key(tls_ctx
, conn
,
3725 params
->private_key
,
3726 params
->private_key_passwd
,
3727 params
->private_key_blob
,
3728 params
->private_key_blob_len
)) {
3729 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
3730 params
->private_key
);
3734 if (tls_connection_dh(conn
, params
->dh_file
)) {
3735 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
3740 if (params
->openssl_ciphers
&&
3741 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
3742 wpa_printf(MSG_INFO
,
3743 "OpenSSL: Failed to set cipher string '%s'",
3744 params
->openssl_ciphers
);
3748 #ifdef SSL_OP_NO_TICKET
3749 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3750 SSL_set_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3751 #ifdef SSL_clear_options
3753 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TICKET
);
3754 #endif /* SSL_clear_options */
3755 #endif /* SSL_OP_NO_TICKET */
3757 #ifdef SSL_OP_NO_TLSv1
3758 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_0
)
3759 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1
);
3761 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1
);
3762 #endif /* SSL_OP_NO_TLSv1 */
3763 #ifdef SSL_OP_NO_TLSv1_1
3764 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_1
)
3765 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3767 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_1
);
3768 #endif /* SSL_OP_NO_TLSv1_1 */
3769 #ifdef SSL_OP_NO_TLSv1_2
3770 if (params
->flags
& TLS_CONN_DISABLE_TLSv1_2
)
3771 SSL_set_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3773 SSL_clear_options(conn
->ssl
, SSL_OP_NO_TLSv1_2
);
3774 #endif /* SSL_OP_NO_TLSv1_2 */
3777 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
3778 SSL_CTX
*ssl_ctx
= tls_ctx
;
3779 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
3780 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
3781 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
3783 #endif /* HAVE_OCSP */
3785 conn
->flags
= params
->flags
;
3787 tls_get_errors(tls_ctx
);
3793 int tls_global_set_params(void *tls_ctx
,
3794 const struct tls_connection_params
*params
)
3796 SSL_CTX
*ssl_ctx
= tls_ctx
;
3799 while ((err
= ERR_get_error())) {
3800 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
3801 __func__
, ERR_error_string(err
, NULL
));
3804 if (tls_global_ca_cert(ssl_ctx
, params
->ca_cert
) ||
3805 tls_global_client_cert(ssl_ctx
, params
->client_cert
) ||
3806 tls_global_private_key(ssl_ctx
, params
->private_key
,
3807 params
->private_key_passwd
) ||
3808 tls_global_dh(ssl_ctx
, params
->dh_file
)) {
3809 wpa_printf(MSG_INFO
, "TLS: Failed to set global parameters");
3813 if (params
->openssl_ciphers
&&
3814 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
3815 wpa_printf(MSG_INFO
,
3816 "OpenSSL: Failed to set cipher string '%s'",
3817 params
->openssl_ciphers
);
3821 #ifdef SSL_OP_NO_TICKET
3822 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
3823 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3824 #ifdef SSL_CTX_clear_options
3826 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
3827 #endif /* SSL_clear_options */
3828 #endif /* SSL_OP_NO_TICKET */
3831 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
3832 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
3833 os_free(tls_global
->ocsp_stapling_response
);
3834 if (params
->ocsp_stapling_response
)
3835 tls_global
->ocsp_stapling_response
=
3836 os_strdup(params
->ocsp_stapling_response
);
3838 tls_global
->ocsp_stapling_response
= NULL
;
3839 #endif /* HAVE_OCSP */
3845 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3846 /* Pre-shared secred requires a patch to openssl, so this function is
3847 * commented out unless explicitly needed for EAP-FAST in order to be able to
3848 * build this file with unmodified openssl. */
3850 #ifdef OPENSSL_IS_BORINGSSL
3851 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3852 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3853 const SSL_CIPHER
**cipher
, void *arg
)
3854 #else /* OPENSSL_IS_BORINGSSL */
3855 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
3856 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
3857 SSL_CIPHER
**cipher
, void *arg
)
3858 #endif /* OPENSSL_IS_BORINGSSL */
3860 struct tls_connection
*conn
= arg
;
3863 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3864 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3867 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
3868 conn
->session_ticket
,
3869 conn
->session_ticket_len
,
3870 s
->s3
->client_random
,
3871 s
->s3
->server_random
, secret
);
3873 unsigned char client_random
[SSL3_RANDOM_SIZE
];
3874 unsigned char server_random
[SSL3_RANDOM_SIZE
];
3876 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3879 SSL_get_client_random(s
, client_random
, sizeof(client_random
));
3880 SSL_get_server_random(s
, server_random
, sizeof(server_random
));
3882 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
3883 conn
->session_ticket
,
3884 conn
->session_ticket_len
,
3886 server_random
, secret
);
3889 os_free(conn
->session_ticket
);
3890 conn
->session_ticket
= NULL
;
3895 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
3900 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
3903 struct tls_connection
*conn
= arg
;
3905 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
3908 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
3910 os_free(conn
->session_ticket
);
3911 conn
->session_ticket
= NULL
;
3913 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
3914 "extension", data
, len
);
3916 conn
->session_ticket
= os_malloc(len
);
3917 if (conn
->session_ticket
== NULL
)
3920 os_memcpy(conn
->session_ticket
, data
, len
);
3921 conn
->session_ticket_len
= len
;
3925 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3928 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
3929 struct tls_connection
*conn
,
3930 tls_session_ticket_cb cb
,
3933 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3934 conn
->session_ticket_cb
= cb
;
3935 conn
->session_ticket_cb_ctx
= ctx
;
3938 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
3941 SSL_set_session_ticket_ext_cb(conn
->ssl
,
3942 tls_session_ticket_ext_cb
, conn
);
3944 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
3946 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
3950 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3952 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3956 int tls_get_library_version(char *buf
, size_t buf_len
)
3958 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
3959 OPENSSL_VERSION_TEXT
,
3960 SSLeay_version(SSLEAY_VERSION
));