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/opensslv.h>
22 #include <openssl/pkcs12.h>
23 #include <openssl/x509v3.h>
24 #ifndef OPENSSL_NO_ENGINE
25 #include <openssl/engine.h>
26 #endif /* OPENSSL_NO_ENGINE */
27 #ifndef OPENSSL_NO_DSA
28 #include <openssl/dsa.h>
31 #include <openssl/dh.h>
39 #include "tls_openssl.h"
41 #if !defined(CONFIG_FIPS) && \
42 (defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || \
43 defined(EAP_SERVER_FAST))
44 #define OPENSSL_NEED_EAP_FAST_PRF
47 #if defined(OPENSSL_IS_BORINGSSL)
48 /* stack_index_t is the return type of OpenSSL's sk_XXX_num() functions. */
49 typedef size_t stack_index_t
;
51 typedef int stack_index_t
;
54 #ifdef SSL_set_tlsext_status_type
55 #ifndef OPENSSL_NO_TLSEXT
57 #include <openssl/ocsp.h>
58 #endif /* OPENSSL_NO_TLSEXT */
59 #endif /* SSL_set_tlsext_status_type */
61 #if (OPENSSL_VERSION_NUMBER < 0x10100000L || \
62 defined(LIBRESSL_VERSION_NUMBER)) && \
63 !defined(BORINGSSL_API_VERSION)
65 * SSL_get_client_random() and SSL_get_server_random() were added in OpenSSL
66 * 1.1.0 and newer BoringSSL revisions. Provide compatibility wrappers for
70 static size_t SSL_get_client_random(const SSL
*ssl
, unsigned char *out
,
73 if (!ssl
->s3
|| outlen
< SSL3_RANDOM_SIZE
)
75 os_memcpy(out
, ssl
->s3
->client_random
, SSL3_RANDOM_SIZE
);
76 return SSL3_RANDOM_SIZE
;
80 static size_t SSL_get_server_random(const SSL
*ssl
, unsigned char *out
,
83 if (!ssl
->s3
|| outlen
< SSL3_RANDOM_SIZE
)
85 os_memcpy(out
, ssl
->s3
->server_random
, SSL3_RANDOM_SIZE
);
86 return SSL3_RANDOM_SIZE
;
90 #ifdef OPENSSL_NEED_EAP_FAST_PRF
91 static size_t SSL_SESSION_get_master_key(const SSL_SESSION
*session
,
92 unsigned char *out
, size_t outlen
)
94 if (!session
|| session
->master_key_length
< 0 ||
95 (size_t) session
->master_key_length
> outlen
)
97 if ((size_t) session
->master_key_length
< outlen
)
98 outlen
= session
->master_key_length
;
99 os_memcpy(out
, session
->master_key
, outlen
);
102 #endif /* OPENSSL_NEED_EAP_FAST_PRF */
107 #include <openssl/pem.h>
108 #include <keystore/keystore_get.h>
110 static BIO
* BIO_from_keystore(const char *key
)
113 uint8_t *value
= NULL
;
114 int length
= keystore_get(key
, strlen(key
), &value
);
115 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
116 BIO_write(bio
, value
, length
);
122 static int tls_add_ca_from_keystore(X509_STORE
*ctx
, const char *key_alias
)
124 BIO
*bio
= BIO_from_keystore(key_alias
);
125 STACK_OF(X509_INFO
) *stack
= NULL
;
129 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
134 wpa_printf(MSG_WARNING
, "TLS: Failed to parse certificate: %s",
139 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
140 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
143 X509_STORE_add_cert(ctx
, info
->x509
);
145 X509_STORE_add_crl(ctx
, info
->crl
);
148 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
154 static int tls_add_ca_from_keystore_encoded(X509_STORE
*ctx
,
155 const char *encoded_key_alias
)
158 int len
= os_strlen(encoded_key_alias
);
159 unsigned char *decoded_alias
;
162 wpa_printf(MSG_WARNING
, "Invalid hex-encoded alias: %s",
167 decoded_alias
= os_malloc(len
/ 2 + 1);
169 if (!hexstr2bin(encoded_key_alias
, decoded_alias
, len
/ 2)) {
170 decoded_alias
[len
/ 2] = '\0';
171 rc
= tls_add_ca_from_keystore(
172 ctx
, (const char *) decoded_alias
);
174 os_free(decoded_alias
);
182 static int tls_openssl_ref_count
= 0;
183 static int tls_ex_idx_session
= -1;
186 void (*event_cb
)(void *ctx
, enum tls_event ev
,
187 union tls_event_data
*data
);
190 char *ocsp_stapling_response
;
193 static struct tls_context
*tls_global
= NULL
;
198 unsigned int tls_session_lifetime
;
201 struct tls_connection
{
202 struct tls_context
*context
;
205 BIO
*ssl_in
, *ssl_out
;
206 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
207 ENGINE
*engine
; /* functional reference to the engine */
208 EVP_PKEY
*private_key
; /* the private key if using engine */
209 #endif /* OPENSSL_NO_ENGINE */
210 char *subject_match
, *altsubject_match
, *suffix_match
, *domain_match
;
211 int read_alerts
, write_alerts
, failed
;
213 tls_session_ticket_cb session_ticket_cb
;
214 void *session_ticket_cb_ctx
;
216 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
218 size_t session_ticket_len
;
220 unsigned int ca_cert_verify
:1;
221 unsigned int cert_probe
:1;
222 unsigned int server_cert_only
:1;
223 unsigned int invalid_hb_used
:1;
224 unsigned int success_data
:1;
226 u8 srv_cert_hash
[32];
232 X509
*peer_issuer_issuer
;
234 unsigned char client_random
[SSL3_RANDOM_SIZE
];
235 unsigned char server_random
[SSL3_RANDOM_SIZE
];
239 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
241 struct tls_context
*context
= os_zalloc(sizeof(*context
));
245 context
->event_cb
= conf
->event_cb
;
246 context
->cb_ctx
= conf
->cb_ctx
;
247 context
->cert_in_cb
= conf
->cert_in_cb
;
253 #ifdef CONFIG_NO_STDOUT_DEBUG
255 static void _tls_show_errors(void)
259 while ((err
= ERR_get_error())) {
260 /* Just ignore the errors, since stdout is disabled */
263 #define tls_show_errors(l, f, t) _tls_show_errors()
265 #else /* CONFIG_NO_STDOUT_DEBUG */
267 static void tls_show_errors(int level
, const char *func
, const char *txt
)
271 wpa_printf(level
, "OpenSSL: %s - %s %s",
272 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
274 while ((err
= ERR_get_error())) {
275 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
276 ERR_error_string(err
, NULL
));
280 #endif /* CONFIG_NO_STDOUT_DEBUG */
283 #ifdef CONFIG_NATIVE_WINDOWS
285 /* Windows CryptoAPI and access to certificate stores */
286 #include <wincrypt.h>
288 #ifdef __MINGW32_VERSION
290 * MinGW does not yet include all the needed definitions for CryptoAPI, so
291 * define here whatever extra is needed.
293 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
294 #define CERT_STORE_READONLY_FLAG 0x00008000
295 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
297 #endif /* __MINGW32_VERSION */
300 struct cryptoapi_rsa_data
{
301 const CERT_CONTEXT
*cert
;
302 HCRYPTPROV crypt_prov
;
304 BOOL free_crypt_prov
;
308 static void cryptoapi_error(const char *msg
)
310 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
311 msg
, (unsigned int) GetLastError());
315 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
316 unsigned char *to
, RSA
*rsa
, int padding
)
318 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
323 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
324 unsigned char *to
, RSA
*rsa
, int padding
)
326 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
331 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
332 unsigned char *to
, RSA
*rsa
, int padding
)
334 struct cryptoapi_rsa_data
*priv
=
335 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
337 DWORD hash_size
, len
, i
;
338 unsigned char *buf
= NULL
;
342 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
343 ERR_R_PASSED_NULL_PARAMETER
);
347 if (padding
!= RSA_PKCS1_PADDING
) {
348 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
349 RSA_R_UNKNOWN_PADDING_TYPE
);
353 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
354 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
356 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
357 RSA_R_INVALID_MESSAGE_LENGTH
);
361 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
363 cryptoapi_error("CryptCreateHash failed");
367 len
= sizeof(hash_size
);
368 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
370 cryptoapi_error("CryptGetHashParam failed");
374 if ((int) hash_size
!= flen
) {
375 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
376 (unsigned) hash_size
, flen
);
377 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
378 RSA_R_INVALID_MESSAGE_LENGTH
);
381 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
382 cryptoapi_error("CryptSetHashParam failed");
387 buf
= os_malloc(len
);
389 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
393 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
394 cryptoapi_error("CryptSignHash failed");
398 for (i
= 0; i
< len
; i
++)
399 to
[i
] = buf
[len
- i
- 1];
404 CryptDestroyHash(hash
);
410 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
411 unsigned char *to
, RSA
*rsa
, int padding
)
413 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
418 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
422 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
423 CryptReleaseContext(priv
->crypt_prov
, 0);
425 CertFreeCertificateContext(priv
->cert
);
430 static int cryptoapi_finish(RSA
*rsa
)
432 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
433 os_free((void *) rsa
->meth
);
439 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
442 const CERT_CONTEXT
*ret
= NULL
;
444 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
445 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
446 CERT_STORE_READONLY_FLAG
, L
"MY");
448 cryptoapi_error("Failed to open 'My system store'");
452 if (strncmp(name
, "cert://", 7) == 0) {
453 unsigned short wbuf
[255];
454 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
455 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
457 0, CERT_FIND_SUBJECT_STR
,
459 } else if (strncmp(name
, "hash://", 7) == 0) {
460 CRYPT_HASH_BLOB blob
;
462 const char *hash
= name
+ 7;
465 len
= os_strlen(hash
) / 2;
466 buf
= os_malloc(len
);
467 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
470 ret
= CertFindCertificateInStore(cs
,
479 CertCloseStore(cs
, 0);
485 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
488 RSA
*rsa
= NULL
, *pub_rsa
;
489 struct cryptoapi_rsa_data
*priv
;
490 RSA_METHOD
*rsa_meth
;
493 (strncmp(name
, "cert://", 7) != 0 &&
494 strncmp(name
, "hash://", 7) != 0))
497 priv
= os_zalloc(sizeof(*priv
));
498 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
499 if (priv
== NULL
|| rsa_meth
== NULL
) {
500 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
501 "for CryptoAPI RSA method");
507 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
508 if (priv
->cert
== NULL
) {
509 priv
->cert
= cryptoapi_find_cert(
510 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
512 if (priv
->cert
== NULL
) {
513 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
518 cert
= d2i_X509(NULL
,
519 (const unsigned char **) &priv
->cert
->pbCertEncoded
,
520 priv
->cert
->cbCertEncoded
);
522 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
527 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
528 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
529 NULL
, &priv
->crypt_prov
,
531 &priv
->free_crypt_prov
)) {
532 cryptoapi_error("Failed to acquire a private key for the "
537 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
538 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
539 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
540 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
541 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
542 rsa_meth
->finish
= cryptoapi_finish
;
543 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
544 rsa_meth
->app_data
= (char *) priv
;
548 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
549 ERR_R_MALLOC_FAILURE
);
553 if (!SSL_use_certificate(ssl
, cert
)) {
558 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
562 rsa
->n
= BN_dup(pub_rsa
->n
);
563 rsa
->e
= BN_dup(pub_rsa
->e
);
564 if (!RSA_set_method(rsa
, rsa_meth
))
567 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
580 cryptoapi_free_data(priv
);
586 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
589 PCCERT_CONTEXT ctx
= NULL
;
597 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
602 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
605 wsprintf(wstore
, L
"%S", store
);
606 cs
= CertOpenSystemStore(0, wstore
);
609 cs
= CertOpenSystemStore(0, store
);
612 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
613 "'%s': error=%d", __func__
, store
,
614 (int) GetLastError());
618 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
619 cert
= d2i_X509(NULL
,
620 (const unsigned char **) &ctx
->pbCertEncoded
,
623 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
624 "X509 DER encoding for CA cert");
628 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
630 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
631 "system certificate store: subject='%s'", buf
);
633 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
635 tls_show_errors(MSG_WARNING
, __func__
,
636 "Failed to add ca_cert to OpenSSL "
637 "certificate store");
643 if (!CertCloseStore(cs
, 0)) {
644 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
645 "'%s': error=%d", __func__
, name
+ 13,
646 (int) GetLastError());
653 #else /* CONFIG_NATIVE_WINDOWS */
655 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
660 #endif /* CONFIG_NATIVE_WINDOWS */
663 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
668 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
669 w
= where
& ~SSL_ST_MASK
;
670 if (w
& SSL_ST_CONNECT
)
672 else if (w
& SSL_ST_ACCEPT
)
677 if (where
& SSL_CB_LOOP
) {
678 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
679 str
, SSL_state_string_long(ssl
));
680 } else if (where
& SSL_CB_ALERT
) {
681 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
682 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
683 where
& SSL_CB_READ
?
684 "read (remote end reported an error)" :
685 "write (local SSL3 detected an error)",
686 SSL_alert_type_string_long(ret
),
687 SSL_alert_desc_string_long(ret
));
688 if ((ret
>> 8) == SSL3_AL_FATAL
) {
689 if (where
& SSL_CB_READ
)
692 conn
->write_alerts
++;
694 if (conn
->context
->event_cb
!= NULL
) {
695 union tls_event_data ev
;
696 struct tls_context
*context
= conn
->context
;
697 os_memset(&ev
, 0, sizeof(ev
));
698 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
699 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
700 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
701 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
703 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
704 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
705 str
, ret
== 0 ? "failed" : "error",
706 SSL_state_string_long(ssl
));
711 #ifndef OPENSSL_NO_ENGINE
713 * tls_engine_load_dynamic_generic - load any openssl engine
714 * @pre: an array of commands and values that load an engine initialized
715 * in the engine specific function
716 * @post: an array of commands and values that initialize an already loaded
717 * engine (or %NULL if not required)
718 * @id: the engine id of the engine to load (only required if post is not %NULL
720 * This function is a generic function that loads any openssl engine.
722 * Returns: 0 on success, -1 on failure
724 static int tls_engine_load_dynamic_generic(const char *pre
[],
725 const char *post
[], const char *id
)
728 const char *dynamic_id
= "dynamic";
730 engine
= ENGINE_by_id(id
);
732 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
735 * If it was auto-loaded by ENGINE_by_id() we might still
736 * need to tell it which PKCS#11 module to use in legacy
737 * (non-p11-kit) environments. Do so now; even if it was
738 * properly initialised before, setting it again will be
745 engine
= ENGINE_by_id(dynamic_id
);
746 if (engine
== NULL
) {
747 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
749 ERR_error_string(ERR_get_error(), NULL
));
753 /* Perform the pre commands. This will load the engine. */
754 while (pre
&& pre
[0]) {
755 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
756 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
757 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
758 "%s %s [%s]", pre
[0], pre
[1],
759 ERR_error_string(ERR_get_error(), NULL
));
767 * Free the reference to the "dynamic" engine. The loaded engine can
768 * now be looked up using ENGINE_by_id().
772 engine
= ENGINE_by_id(id
);
773 if (engine
== NULL
) {
774 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
775 id
, ERR_error_string(ERR_get_error(), NULL
));
779 while (post
&& post
[0]) {
780 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
781 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
782 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
783 " %s %s [%s]", post
[0], post
[1],
784 ERR_error_string(ERR_get_error(), NULL
));
785 ENGINE_remove(engine
);
798 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
799 * @pkcs11_so_path: pksc11_so_path from the configuration
800 * @pcks11_module_path: pkcs11_module_path from the configuration
802 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
803 const char *pkcs11_module_path
)
805 char *engine_id
= "pkcs11";
806 const char *pre_cmd
[] = {
807 "SO_PATH", NULL
/* pkcs11_so_path */,
808 "ID", NULL
/* engine_id */,
810 /* "NO_VCHECK", "1", */
814 const char *post_cmd
[] = {
815 "MODULE_PATH", NULL
/* pkcs11_module_path */,
822 pre_cmd
[1] = pkcs11_so_path
;
823 pre_cmd
[3] = engine_id
;
824 if (pkcs11_module_path
)
825 post_cmd
[1] = pkcs11_module_path
;
829 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
832 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
837 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
838 * @opensc_so_path: opensc_so_path from the configuration
840 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
842 char *engine_id
= "opensc";
843 const char *pre_cmd
[] = {
844 "SO_PATH", NULL
/* opensc_so_path */,
845 "ID", NULL
/* engine_id */,
854 pre_cmd
[1] = opensc_so_path
;
855 pre_cmd
[3] = engine_id
;
857 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
860 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
862 #endif /* OPENSSL_NO_ENGINE */
865 static void remove_session_cb(SSL_CTX
*ctx
, SSL_SESSION
*sess
)
869 if (tls_ex_idx_session
< 0)
871 buf
= SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
874 wpa_printf(MSG_DEBUG
,
875 "OpenSSL: Free application session data %p (sess %p)",
879 SSL_SESSION_set_ex_data(sess
, tls_ex_idx_session
, NULL
);
883 void * tls_init(const struct tls_config
*conf
)
885 struct tls_data
*data
;
887 struct tls_context
*context
;
890 if (tls_openssl_ref_count
== 0) {
891 tls_global
= context
= tls_context_new(conf
);
896 if (conf
&& conf
->fips_mode
) {
897 static int fips_enabled
= 0;
899 if (!fips_enabled
&& !FIPS_mode_set(1)) {
900 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
902 ERR_load_crypto_strings();
903 ERR_print_errors_fp(stderr
);
908 wpa_printf(MSG_INFO
, "Running in FIPS mode");
912 #else /* OPENSSL_FIPS */
913 if (conf
&& conf
->fips_mode
) {
914 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
920 #endif /* OPENSSL_FIPS */
921 #endif /* CONFIG_FIPS */
922 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
923 SSL_load_error_strings();
925 #ifndef OPENSSL_NO_SHA256
926 EVP_add_digest(EVP_sha256());
927 #endif /* OPENSSL_NO_SHA256 */
928 /* TODO: if /dev/urandom is available, PRNG is seeded
929 * automatically. If this is not the case, random data should
933 #ifndef OPENSSL_NO_RC2
935 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
936 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
937 * versions, but it looks like OpenSSL 1.0.0 does not do that
940 EVP_add_cipher(EVP_rc2_40_cbc());
941 #endif /* OPENSSL_NO_RC2 */
943 #endif /* PKCS12_FUNCS */
946 context
= tls_context_new(conf
);
950 tls_openssl_ref_count
++;
952 data
= os_zalloc(sizeof(*data
));
954 ssl
= SSL_CTX_new(SSLv23_method());
958 tls_openssl_ref_count
--;
959 if (context
!= tls_global
)
961 if (tls_openssl_ref_count
== 0) {
970 data
->tls_session_lifetime
= conf
->tls_session_lifetime
;
972 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
973 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
975 #ifdef SSL_MODE_NO_AUTO_CHAIN
976 /* Number of deployed use cases assume the default OpenSSL behavior of
977 * auto chaining the local certificate is in use. BoringSSL removed this
978 * functionality by default, so we need to restore it here to avoid
979 * breaking existing use cases. */
980 SSL_CTX_clear_mode(ssl
, SSL_MODE_NO_AUTO_CHAIN
);
981 #endif /* SSL_MODE_NO_AUTO_CHAIN */
983 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
984 SSL_CTX_set_app_data(ssl
, context
);
985 if (data
->tls_session_lifetime
> 0) {
986 SSL_CTX_set_quiet_shutdown(ssl
, 1);
988 * Set default context here. In practice, this will be replaced
989 * by the per-EAP method context in tls_connection_set_verify().
991 SSL_CTX_set_session_id_context(ssl
, (u8
*) "hostapd", 7);
992 SSL_CTX_set_session_cache_mode(ssl
, SSL_SESS_CACHE_SERVER
);
993 SSL_CTX_set_timeout(ssl
, data
->tls_session_lifetime
);
994 SSL_CTX_sess_set_remove_cb(ssl
, remove_session_cb
);
996 SSL_CTX_set_session_cache_mode(ssl
, SSL_SESS_CACHE_OFF
);
999 if (tls_ex_idx_session
< 0) {
1000 tls_ex_idx_session
= SSL_SESSION_get_ex_new_index(
1001 0, NULL
, NULL
, NULL
, NULL
);
1002 if (tls_ex_idx_session
< 0) {
1008 #ifndef OPENSSL_NO_ENGINE
1009 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
1010 ERR_load_ENGINE_strings();
1011 ENGINE_load_dynamic();
1014 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
1015 conf
->pkcs11_module_path
)) {
1016 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
1017 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
1018 conf
->pkcs11_module_path
)) {
1023 #endif /* OPENSSL_NO_ENGINE */
1025 if (conf
&& conf
->openssl_ciphers
)
1026 ciphers
= conf
->openssl_ciphers
;
1028 ciphers
= "DEFAULT:!EXP:!LOW";
1029 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
1030 wpa_printf(MSG_ERROR
,
1031 "OpenSSL: Failed to set cipher string '%s'",
1041 void tls_deinit(void *ssl_ctx
)
1043 struct tls_data
*data
= ssl_ctx
;
1044 SSL_CTX
*ssl
= data
->ssl
;
1045 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1046 if (context
!= tls_global
)
1048 if (data
->tls_session_lifetime
> 0)
1049 SSL_CTX_flush_sessions(ssl
, 0);
1052 tls_openssl_ref_count
--;
1053 if (tls_openssl_ref_count
== 0) {
1054 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
1055 #ifndef OPENSSL_NO_ENGINE
1057 #endif /* OPENSSL_NO_ENGINE */
1058 CRYPTO_cleanup_all_ex_data();
1059 ERR_remove_thread_state(NULL
);
1062 #endif /* < 1.1.0 */
1063 os_free(tls_global
->ocsp_stapling_response
);
1064 tls_global
->ocsp_stapling_response
= NULL
;
1065 os_free(tls_global
);
1073 #ifndef OPENSSL_NO_ENGINE
1075 /* Cryptoki return values */
1076 #define CKR_PIN_INCORRECT 0x000000a0
1077 #define CKR_PIN_INVALID 0x000000a1
1078 #define CKR_PIN_LEN_RANGE 0x000000a2
1081 #define ERR_LIB_PKCS11 ERR_LIB_USER
1083 static int tls_is_pin_error(unsigned int err
)
1085 return ERR_GET_LIB(err
) == ERR_LIB_PKCS11
&&
1086 (ERR_GET_REASON(err
) == CKR_PIN_INCORRECT
||
1087 ERR_GET_REASON(err
) == CKR_PIN_INVALID
||
1088 ERR_GET_REASON(err
) == CKR_PIN_LEN_RANGE
);
1091 #endif /* OPENSSL_NO_ENGINE */
1095 /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1096 EVP_PKEY
* EVP_PKEY_from_keystore(const char *key_id
);
1097 #endif /* ANDROID */
1099 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
1100 const char *pin
, const char *key_id
,
1101 const char *cert_id
, const char *ca_cert_id
)
1103 #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1104 #if !defined(OPENSSL_NO_ENGINE)
1105 #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1108 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1109 conn
->engine
= NULL
;
1110 conn
->private_key
= EVP_PKEY_from_keystore(key_id
);
1111 if (!conn
->private_key
) {
1112 wpa_printf(MSG_ERROR
,
1113 "ENGINE: cannot load private key with id '%s' [%s]",
1115 ERR_error_string(ERR_get_error(), NULL
));
1116 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1118 #endif /* ANDROID && OPENSSL_IS_BORINGSSL */
1120 #ifndef OPENSSL_NO_ENGINE
1122 if (engine_id
== NULL
) {
1123 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
1129 ENGINE_load_dynamic();
1131 conn
->engine
= ENGINE_by_id(engine_id
);
1132 if (!conn
->engine
) {
1133 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
1134 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
1137 if (ENGINE_init(conn
->engine
) != 1) {
1138 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
1139 "(engine: %s) [%s]", engine_id
,
1140 ERR_error_string(ERR_get_error(), NULL
));
1143 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
1146 if (pin
&& ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
1147 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
1148 ERR_error_string(ERR_get_error(), NULL
));
1154 * Ensure that the ENGINE does not attempt to use the OpenSSL
1155 * UI system to obtain a PIN, if we didn't provide one.
1158 const void *password
;
1159 const char *prompt_info
;
1160 } key_cb
= { "", NULL
};
1162 /* load private key first in-case PIN is required for cert */
1163 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
1166 if (!conn
->private_key
) {
1167 unsigned long err
= ERR_get_error();
1169 wpa_printf(MSG_ERROR
,
1170 "ENGINE: cannot load private key with id '%s' [%s]",
1172 ERR_error_string(err
, NULL
));
1173 if (tls_is_pin_error(err
))
1174 ret
= TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
1176 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1181 /* handle a certificate and/or CA certificate */
1182 if (cert_id
|| ca_cert_id
) {
1183 const char *cmd_name
= "LOAD_CERT_CTRL";
1185 /* test if the engine supports a LOAD_CERT_CTRL */
1186 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
1187 0, (void *)cmd_name
, NULL
)) {
1188 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
1189 " loading certificates");
1190 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1199 ENGINE_free(conn
->engine
);
1200 conn
->engine
= NULL
;
1203 if (conn
->private_key
) {
1204 EVP_PKEY_free(conn
->private_key
);
1205 conn
->private_key
= NULL
;
1209 #else /* OPENSSL_NO_ENGINE */
1211 #endif /* OPENSSL_NO_ENGINE */
1215 static void tls_engine_deinit(struct tls_connection
*conn
)
1217 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
1218 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
1219 if (conn
->private_key
) {
1220 EVP_PKEY_free(conn
->private_key
);
1221 conn
->private_key
= NULL
;
1224 #if !defined(OPENSSL_IS_BORINGSSL)
1225 ENGINE_finish(conn
->engine
);
1226 #endif /* !OPENSSL_IS_BORINGSSL */
1227 conn
->engine
= NULL
;
1229 #endif /* ANDROID || !OPENSSL_NO_ENGINE */
1233 int tls_get_errors(void *ssl_ctx
)
1238 while ((err
= ERR_get_error())) {
1239 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1240 ERR_error_string(err
, NULL
));
1248 static const char * openssl_content_type(int content_type
)
1250 switch (content_type
) {
1252 return "change cipher spec";
1258 return "application data";
1262 return "TLS header info"; /* pseudo content type */
1269 static const char * openssl_handshake_type(int content_type
, const u8
*buf
,
1272 if (content_type
!= 22 || !buf
|| len
== 0)
1276 return "hello request";
1278 return "client hello";
1280 return "server hello";
1282 return "new session ticket";
1284 return "certificate";
1286 return "server key exchange";
1288 return "certificate request";
1290 return "server hello done";
1292 return "certificate verify";
1294 return "client key exchange";
1298 return "certificate url";
1300 return "certificate status";
1307 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1308 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1310 struct tls_connection
*conn
= arg
;
1311 const u8
*pos
= buf
;
1314 wpa_printf(MSG_DEBUG
,
1315 "OpenSSL: session ver=0x%x content_type=%d",
1316 version
, content_type
);
1317 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Data", buf
, len
);
1321 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1322 write_p
? "TX" : "RX", version
, content_type
,
1323 openssl_content_type(content_type
),
1324 openssl_handshake_type(content_type
, buf
, len
));
1325 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1326 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1327 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1328 if (payload_len
+ 3 > len
) {
1329 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1330 conn
->invalid_hb_used
= 1;
1336 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1338 struct tls_data
*data
= ssl_ctx
;
1339 SSL_CTX
*ssl
= data
->ssl
;
1340 struct tls_connection
*conn
;
1342 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1344 conn
= os_zalloc(sizeof(*conn
));
1347 conn
->ssl_ctx
= ssl
;
1348 conn
->ssl
= SSL_new(ssl
);
1349 if (conn
->ssl
== NULL
) {
1350 tls_show_errors(MSG_INFO
, __func__
,
1351 "Failed to initialize new SSL connection");
1356 conn
->context
= context
;
1357 SSL_set_app_data(conn
->ssl
, conn
);
1358 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1359 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1360 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1361 SSL_OP_SINGLE_DH_USE
;
1362 #ifdef SSL_OP_NO_COMPRESSION
1363 options
|= SSL_OP_NO_COMPRESSION
;
1364 #endif /* SSL_OP_NO_COMPRESSION */
1365 SSL_set_options(conn
->ssl
, options
);
1367 conn
->ssl_in
= BIO_new(BIO_s_mem());
1368 if (!conn
->ssl_in
) {
1369 tls_show_errors(MSG_INFO
, __func__
,
1370 "Failed to create a new BIO for ssl_in");
1371 SSL_free(conn
->ssl
);
1376 conn
->ssl_out
= BIO_new(BIO_s_mem());
1377 if (!conn
->ssl_out
) {
1378 tls_show_errors(MSG_INFO
, __func__
,
1379 "Failed to create a new BIO for ssl_out");
1380 SSL_free(conn
->ssl
);
1381 BIO_free(conn
->ssl_in
);
1386 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1392 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1396 if (conn
->success_data
) {
1398 * Make sure ssl_clear_bad_session() does not remove this
1401 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1402 SSL_shutdown(conn
->ssl
);
1404 SSL_free(conn
->ssl
);
1405 tls_engine_deinit(conn
);
1406 os_free(conn
->subject_match
);
1407 os_free(conn
->altsubject_match
);
1408 os_free(conn
->suffix_match
);
1409 os_free(conn
->domain_match
);
1410 os_free(conn
->session_ticket
);
1415 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1417 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1421 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1426 /* Shutdown previous TLS connection without notifying the peer
1427 * because the connection was already terminated in practice
1428 * and "close notify" shutdown alert would confuse AS. */
1429 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1430 SSL_shutdown(conn
->ssl
);
1431 return SSL_clear(conn
->ssl
) == 1 ? 0 : -1;
1435 static int tls_match_altsubject_component(X509
*cert
, int type
,
1436 const char *value
, size_t len
)
1443 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1445 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1446 gen
= sk_GENERAL_NAME_value(ext
, i
);
1447 if (gen
->type
!= type
)
1449 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1450 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1454 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1460 static int tls_match_altsubject(X509
*cert
, const char *match
)
1463 const char *pos
, *end
;
1468 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1471 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1474 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1478 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1482 end
= os_strchr(pos
, ';');
1484 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1485 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1486 os_strncmp(end
+ 1, "URI:", 4) == 0)
1488 end
= os_strchr(end
+ 1, ';');
1493 len
= os_strlen(pos
);
1494 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1503 #ifndef CONFIG_NATIVE_WINDOWS
1504 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
,
1507 size_t i
, match_len
;
1509 /* Check for embedded nuls that could mess up suffix matching */
1510 for (i
= 0; i
< len
; i
++) {
1511 if (val
[i
] == '\0') {
1512 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1517 match_len
= os_strlen(match
);
1518 if (match_len
> len
|| (full
&& match_len
!= len
))
1521 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1523 return 0; /* no match */
1525 if (match_len
== len
)
1526 return 1; /* exact match */
1528 if (val
[len
- match_len
- 1] == '.')
1529 return 1; /* full label match completes suffix match */
1531 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1534 #endif /* CONFIG_NATIVE_WINDOWS */
1537 static int tls_match_suffix(X509
*cert
, const char *match
, int full
)
1539 #ifdef CONFIG_NATIVE_WINDOWS
1540 /* wincrypt.h has conflicting X509_NAME definition */
1542 #else /* CONFIG_NATIVE_WINDOWS */
1550 wpa_printf(MSG_DEBUG
, "TLS: Match domain against %s%s",
1551 full
? "": "suffix ", match
);
1553 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1555 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1556 gen
= sk_GENERAL_NAME_value(ext
, j
);
1557 if (gen
->type
!= GEN_DNS
)
1560 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1561 gen
->d
.dNSName
->data
,
1562 gen
->d
.dNSName
->length
);
1563 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1564 gen
->d
.dNSName
->length
, match
, full
) ==
1566 wpa_printf(MSG_DEBUG
, "TLS: %s in dNSName found",
1567 full
? "Match" : "Suffix match");
1568 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1572 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1575 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1579 name
= X509_get_subject_name(cert
);
1585 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1588 e
= X509_NAME_get_entry(name
, i
);
1591 cn
= X509_NAME_ENTRY_get_data(e
);
1594 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1595 cn
->data
, cn
->length
);
1596 if (domain_suffix_match(cn
->data
, cn
->length
, match
, full
) == 1)
1598 wpa_printf(MSG_DEBUG
, "TLS: %s in commonName found",
1599 full
? "Match" : "Suffix match");
1604 wpa_printf(MSG_DEBUG
, "TLS: No CommonName %smatch found",
1605 full
? "": "suffix ");
1607 #endif /* CONFIG_NATIVE_WINDOWS */
1611 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1614 case X509_V_ERR_CERT_REVOKED
:
1615 return TLS_FAIL_REVOKED
;
1616 case X509_V_ERR_CERT_NOT_YET_VALID
:
1617 case X509_V_ERR_CRL_NOT_YET_VALID
:
1618 return TLS_FAIL_NOT_YET_VALID
;
1619 case X509_V_ERR_CERT_HAS_EXPIRED
:
1620 case X509_V_ERR_CRL_HAS_EXPIRED
:
1621 return TLS_FAIL_EXPIRED
;
1622 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1623 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1624 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1625 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1626 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1627 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1628 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1629 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1630 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1631 case X509_V_ERR_INVALID_CA
:
1632 return TLS_FAIL_UNTRUSTED
;
1633 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1634 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1635 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1636 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1637 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1638 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1639 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1640 case X509_V_ERR_CERT_UNTRUSTED
:
1641 case X509_V_ERR_CERT_REJECTED
:
1642 return TLS_FAIL_BAD_CERTIFICATE
;
1644 return TLS_FAIL_UNSPECIFIED
;
1649 static struct wpabuf
* get_x509_cert(X509
*cert
)
1654 int cert_len
= i2d_X509(cert
, NULL
);
1658 buf
= wpabuf_alloc(cert_len
);
1662 tmp
= wpabuf_put(buf
, cert_len
);
1663 i2d_X509(cert
, &tmp
);
1668 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1669 X509
*err_cert
, int err
, int depth
,
1670 const char *subject
, const char *err_str
,
1671 enum tls_fail_reason reason
)
1673 union tls_event_data ev
;
1674 struct wpabuf
*cert
= NULL
;
1675 struct tls_context
*context
= conn
->context
;
1677 if (context
->event_cb
== NULL
)
1680 cert
= get_x509_cert(err_cert
);
1681 os_memset(&ev
, 0, sizeof(ev
));
1682 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1683 reason
: openssl_tls_fail_reason(err
);
1684 ev
.cert_fail
.depth
= depth
;
1685 ev
.cert_fail
.subject
= subject
;
1686 ev
.cert_fail
.reason_txt
= err_str
;
1687 ev
.cert_fail
.cert
= cert
;
1688 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1693 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1694 X509
*err_cert
, int depth
,
1695 const char *subject
)
1697 struct wpabuf
*cert
= NULL
;
1698 union tls_event_data ev
;
1699 struct tls_context
*context
= conn
->context
;
1700 char *altsubject
[TLS_MAX_ALT_SUBJECT
];
1701 int alt
, num_altsubject
= 0;
1705 #ifdef CONFIG_SHA256
1707 #endif /* CONFIG_SHA256 */
1709 if (context
->event_cb
== NULL
)
1712 os_memset(&ev
, 0, sizeof(ev
));
1713 if (conn
->cert_probe
|| (conn
->flags
& TLS_CONN_EXT_CERT_CHECK
) ||
1714 context
->cert_in_cb
) {
1715 cert
= get_x509_cert(err_cert
);
1716 ev
.peer_cert
.cert
= cert
;
1718 #ifdef CONFIG_SHA256
1722 addr
[0] = wpabuf_head(cert
);
1723 len
[0] = wpabuf_len(cert
);
1724 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1725 ev
.peer_cert
.hash
= hash
;
1726 ev
.peer_cert
.hash_len
= sizeof(hash
);
1729 #endif /* CONFIG_SHA256 */
1730 ev
.peer_cert
.depth
= depth
;
1731 ev
.peer_cert
.subject
= subject
;
1733 ext
= X509_get_ext_d2i(err_cert
, NID_subject_alt_name
, NULL
, NULL
);
1734 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1737 if (num_altsubject
== TLS_MAX_ALT_SUBJECT
)
1739 gen
= sk_GENERAL_NAME_value(ext
, i
);
1740 if (gen
->type
!= GEN_EMAIL
&&
1741 gen
->type
!= GEN_DNS
&&
1742 gen
->type
!= GEN_URI
)
1745 pos
= os_malloc(10 + gen
->d
.ia5
->length
+ 1);
1748 altsubject
[num_altsubject
++] = pos
;
1750 switch (gen
->type
) {
1752 os_memcpy(pos
, "EMAIL:", 6);
1756 os_memcpy(pos
, "DNS:", 4);
1760 os_memcpy(pos
, "URI:", 4);
1765 os_memcpy(pos
, gen
->d
.ia5
->data
, gen
->d
.ia5
->length
);
1766 pos
+= gen
->d
.ia5
->length
;
1769 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1771 for (alt
= 0; alt
< num_altsubject
; alt
++)
1772 ev
.peer_cert
.altsubject
[alt
] = altsubject
[alt
];
1773 ev
.peer_cert
.num_altsubject
= num_altsubject
;
1775 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1777 for (alt
= 0; alt
< num_altsubject
; alt
++)
1778 os_free(altsubject
[alt
]);
1782 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1788 struct tls_connection
*conn
;
1789 struct tls_context
*context
;
1790 char *match
, *altmatch
, *suffix_match
, *domain_match
;
1791 const char *err_str
;
1793 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1797 err
= X509_STORE_CTX_get_error(x509_ctx
);
1798 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1799 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1800 SSL_get_ex_data_X509_STORE_CTX_idx());
1801 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1803 conn
= SSL_get_app_data(ssl
);
1808 conn
->peer_cert
= err_cert
;
1809 else if (depth
== 1)
1810 conn
->peer_issuer
= err_cert
;
1811 else if (depth
== 2)
1812 conn
->peer_issuer_issuer
= err_cert
;
1814 context
= conn
->context
;
1815 match
= conn
->subject_match
;
1816 altmatch
= conn
->altsubject_match
;
1817 suffix_match
= conn
->suffix_match
;
1818 domain_match
= conn
->domain_match
;
1820 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1822 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1824 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1825 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1826 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1827 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1832 err_str
= X509_verify_cert_error_string(err
);
1834 #ifdef CONFIG_SHA256
1836 * Do not require preverify_ok so we can explicity allow otherwise
1837 * invalid pinned server certificates.
1839 if (depth
== 0 && conn
->server_cert_only
) {
1840 struct wpabuf
*cert
;
1841 cert
= get_x509_cert(err_cert
);
1843 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1844 "server certificate data");
1850 addr
[0] = wpabuf_head(cert
);
1851 len
[0] = wpabuf_len(cert
);
1852 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1853 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1854 err_str
= "Server certificate mismatch";
1855 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1857 } else if (!preverify_ok
) {
1859 * Certificate matches pinned certificate, allow
1860 * regardless of other problems.
1862 wpa_printf(MSG_DEBUG
,
1863 "OpenSSL: Ignore validation issues for a pinned server certificate");
1869 #endif /* CONFIG_SHA256 */
1871 if (!preverify_ok
) {
1872 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1873 " error %d (%s) depth %d for '%s'", err
, err_str
,
1875 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1876 err_str
, TLS_FAIL_UNSPECIFIED
);
1877 return preverify_ok
;
1880 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1881 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1882 preverify_ok
, err
, err_str
,
1883 conn
->ca_cert_verify
, depth
, buf
);
1884 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1885 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1886 "match with '%s'", buf
, match
);
1888 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1890 TLS_FAIL_SUBJECT_MISMATCH
);
1891 } else if (depth
== 0 && altmatch
&&
1892 !tls_match_altsubject(err_cert
, altmatch
)) {
1893 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1894 "'%s' not found", altmatch
);
1896 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1897 "AltSubject mismatch",
1898 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1899 } else if (depth
== 0 && suffix_match
&&
1900 !tls_match_suffix(err_cert
, suffix_match
, 0)) {
1901 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1904 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1905 "Domain suffix mismatch",
1906 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1907 } else if (depth
== 0 && domain_match
&&
1908 !tls_match_suffix(err_cert
, domain_match
, 1)) {
1909 wpa_printf(MSG_WARNING
, "TLS: Domain match '%s' not found",
1912 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1914 TLS_FAIL_DOMAIN_MISMATCH
);
1916 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1918 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1919 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1920 "on probe-only run");
1922 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1923 "Server certificate chain probe",
1924 TLS_FAIL_SERVER_CHAIN_PROBE
);
1927 #ifdef OPENSSL_IS_BORINGSSL
1928 if (depth
== 0 && (conn
->flags
& TLS_CONN_REQUEST_OCSP
) &&
1930 enum ocsp_result res
;
1932 res
= check_ocsp_resp(conn
->ssl_ctx
, conn
->ssl
, err_cert
,
1934 conn
->peer_issuer_issuer
);
1935 if (res
== OCSP_REVOKED
) {
1937 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1938 "certificate revoked",
1940 if (err
== X509_V_OK
)
1941 X509_STORE_CTX_set_error(
1942 x509_ctx
, X509_V_ERR_CERT_REVOKED
);
1943 } else if (res
!= OCSP_GOOD
&&
1944 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
)) {
1946 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1947 "bad certificate status response",
1948 TLS_FAIL_UNSPECIFIED
);
1951 #endif /* OPENSSL_IS_BORINGSSL */
1953 if (depth
== 0 && preverify_ok
&& context
->event_cb
!= NULL
)
1954 context
->event_cb(context
->cb_ctx
,
1955 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1957 return preverify_ok
;
1961 #ifndef OPENSSL_NO_STDIO
1962 static int tls_load_ca_der(struct tls_data
*data
, const char *ca_cert
)
1964 SSL_CTX
*ssl_ctx
= data
->ssl
;
1965 X509_LOOKUP
*lookup
;
1968 lookup
= X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx
),
1969 X509_LOOKUP_file());
1970 if (lookup
== NULL
) {
1971 tls_show_errors(MSG_WARNING
, __func__
,
1972 "Failed add lookup for X509 store");
1976 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1977 unsigned long err
= ERR_peek_error();
1978 tls_show_errors(MSG_WARNING
, __func__
,
1979 "Failed load CA in DER format");
1980 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1981 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1982 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1983 "cert already in hash table error",
1991 #endif /* OPENSSL_NO_STDIO */
1994 static int tls_connection_ca_cert(struct tls_data
*data
,
1995 struct tls_connection
*conn
,
1996 const char *ca_cert
, const u8
*ca_cert_blob
,
1997 size_t ca_cert_blob_len
, const char *ca_path
)
1999 SSL_CTX
*ssl_ctx
= data
->ssl
;
2003 * Remove previously configured trusted CA certificates before adding
2006 store
= X509_STORE_new();
2007 if (store
== NULL
) {
2008 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2009 "certificate store", __func__
);
2012 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2014 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2015 conn
->ca_cert_verify
= 1;
2017 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
2018 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
2020 conn
->cert_probe
= 1;
2021 conn
->ca_cert_verify
= 0;
2025 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
2026 #ifdef CONFIG_SHA256
2027 const char *pos
= ca_cert
+ 7;
2028 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
2029 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
2030 "hash value '%s'", ca_cert
);
2034 if (os_strlen(pos
) != 32 * 2) {
2035 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
2036 "hash length in ca_cert '%s'", ca_cert
);
2039 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
2040 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
2041 "value in ca_cert '%s'", ca_cert
);
2044 conn
->server_cert_only
= 1;
2045 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
2046 "certificate match");
2048 #else /* CONFIG_SHA256 */
2049 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
2050 "cannot validate server certificate hash");
2052 #endif /* CONFIG_SHA256 */
2056 X509
*cert
= d2i_X509(NULL
,
2057 (const unsigned char **) &ca_cert_blob
,
2060 tls_show_errors(MSG_WARNING
, __func__
,
2061 "Failed to parse ca_cert_blob");
2065 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
2067 unsigned long err
= ERR_peek_error();
2068 tls_show_errors(MSG_WARNING
, __func__
,
2069 "Failed to add ca_cert_blob to "
2070 "certificate store");
2071 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2072 ERR_GET_REASON(err
) ==
2073 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2074 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
2075 "cert already in hash table error",
2083 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
2084 "to certificate store", __func__
);
2090 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
2091 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx
),
2094 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2098 /* Multiple aliases separated by space */
2099 if (ca_cert
&& os_strncmp("keystores://", ca_cert
, 12) == 0) {
2100 char *aliases
= os_strdup(&ca_cert
[12]);
2101 const char *delim
= " ";
2108 alias
= strtok_r(aliases
, delim
, &savedptr
);
2109 for (; alias
; alias
= strtok_r(NULL
, delim
, &savedptr
)) {
2110 if (tls_add_ca_from_keystore_encoded(
2111 SSL_CTX_get_cert_store(ssl_ctx
), alias
)) {
2112 wpa_printf(MSG_WARNING
,
2113 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2123 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2126 #endif /* ANDROID */
2128 #ifdef CONFIG_NATIVE_WINDOWS
2129 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
2131 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
2132 "system certificate store");
2135 #endif /* CONFIG_NATIVE_WINDOWS */
2137 if (ca_cert
|| ca_path
) {
2138 #ifndef OPENSSL_NO_STDIO
2139 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
2141 tls_show_errors(MSG_WARNING
, __func__
,
2142 "Failed to load root certificates");
2144 tls_load_ca_der(data
, ca_cert
) == 0) {
2145 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
2146 "DER format CA certificate",
2151 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
2152 "certificate(s) loaded");
2153 tls_get_errors(data
);
2155 #else /* OPENSSL_NO_STDIO */
2156 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2159 #endif /* OPENSSL_NO_STDIO */
2161 /* No ca_cert configured - do not try to verify server
2163 conn
->ca_cert_verify
= 0;
2170 static int tls_global_ca_cert(struct tls_data
*data
, const char *ca_cert
)
2172 SSL_CTX
*ssl_ctx
= data
->ssl
;
2175 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
2177 tls_show_errors(MSG_WARNING
, __func__
,
2178 "Failed to load root certificates");
2182 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
2183 "certificate(s) loaded");
2185 #ifndef OPENSSL_NO_STDIO
2186 /* Add the same CAs to the client certificate requests */
2187 SSL_CTX_set_client_CA_list(ssl_ctx
,
2188 SSL_load_client_CA_file(ca_cert
));
2189 #endif /* OPENSSL_NO_STDIO */
2196 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
2201 struct tls_data
*data
= ssl_ctx
;
2202 X509_STORE
*cs
= SSL_CTX_get_cert_store(data
->ssl
);
2204 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
2205 "certificate store when enabling "
2209 flags
= X509_V_FLAG_CRL_CHECK
;
2211 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
2212 X509_STORE_set_flags(cs
, flags
);
2218 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
2219 const char *subject_match
,
2220 const char *altsubject_match
,
2221 const char *suffix_match
,
2222 const char *domain_match
)
2224 os_free(conn
->subject_match
);
2225 conn
->subject_match
= NULL
;
2226 if (subject_match
) {
2227 conn
->subject_match
= os_strdup(subject_match
);
2228 if (conn
->subject_match
== NULL
)
2232 os_free(conn
->altsubject_match
);
2233 conn
->altsubject_match
= NULL
;
2234 if (altsubject_match
) {
2235 conn
->altsubject_match
= os_strdup(altsubject_match
);
2236 if (conn
->altsubject_match
== NULL
)
2240 os_free(conn
->suffix_match
);
2241 conn
->suffix_match
= NULL
;
2243 conn
->suffix_match
= os_strdup(suffix_match
);
2244 if (conn
->suffix_match
== NULL
)
2248 os_free(conn
->domain_match
);
2249 conn
->domain_match
= NULL
;
2251 conn
->domain_match
= os_strdup(domain_match
);
2252 if (conn
->domain_match
== NULL
)
2260 static void tls_set_conn_flags(SSL
*ssl
, unsigned int flags
)
2262 #ifdef SSL_OP_NO_TICKET
2263 if (flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
2264 SSL_set_options(ssl
, SSL_OP_NO_TICKET
);
2266 SSL_clear_options(ssl
, SSL_OP_NO_TICKET
);
2267 #endif /* SSL_OP_NO_TICKET */
2269 #ifdef SSL_OP_NO_TLSv1
2270 if (flags
& TLS_CONN_DISABLE_TLSv1_0
)
2271 SSL_set_options(ssl
, SSL_OP_NO_TLSv1
);
2273 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1
);
2274 #endif /* SSL_OP_NO_TLSv1 */
2275 #ifdef SSL_OP_NO_TLSv1_1
2276 if (flags
& TLS_CONN_DISABLE_TLSv1_1
)
2277 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_1
);
2279 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1_1
);
2280 #endif /* SSL_OP_NO_TLSv1_1 */
2281 #ifdef SSL_OP_NO_TLSv1_2
2282 if (flags
& TLS_CONN_DISABLE_TLSv1_2
)
2283 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_2
);
2285 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1_2
);
2286 #endif /* SSL_OP_NO_TLSv1_2 */
2290 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
2291 int verify_peer
, unsigned int flags
,
2292 const u8
*session_ctx
, size_t session_ctx_len
)
2294 static int counter
= 0;
2295 struct tls_data
*data
= ssl_ctx
;
2301 conn
->ca_cert_verify
= 1;
2302 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
2303 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
2304 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
2306 conn
->ca_cert_verify
= 0;
2307 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
2310 tls_set_conn_flags(conn
->ssl
, flags
);
2311 conn
->flags
= flags
;
2313 SSL_set_accept_state(conn
->ssl
);
2315 if (data
->tls_session_lifetime
== 0) {
2317 * Set session id context to a unique value to make sure
2318 * session resumption cannot be used either through session
2319 * caching or TLS ticket extension.
2322 SSL_set_session_id_context(conn
->ssl
,
2323 (const unsigned char *) &counter
,
2325 } else if (session_ctx
) {
2326 SSL_set_session_id_context(conn
->ssl
, session_ctx
,
2334 static int tls_connection_client_cert(struct tls_connection
*conn
,
2335 const char *client_cert
,
2336 const u8
*client_cert_blob
,
2337 size_t client_cert_blob_len
)
2339 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
2343 #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
2345 * Clear previously set extra chain certificates, if any, from PKCS#12
2346 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2349 SSL_CTX_clear_extra_chain_certs(conn
->ssl_ctx
);
2350 #endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2351 #endif /* PKCS12_FUNCS */
2353 if (client_cert_blob
&&
2354 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
2355 client_cert_blob_len
) == 1) {
2356 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
2359 } else if (client_cert_blob
) {
2360 tls_show_errors(MSG_DEBUG
, __func__
,
2361 "SSL_use_certificate_ASN1 failed");
2364 if (client_cert
== NULL
)
2368 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
2369 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
2373 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
2376 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
2381 /* Read additional certificates into the chain. */
2383 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
2385 /* Takes ownership of x509 */
2386 SSL_add0_chain_cert(conn
->ssl
, x509
);
2394 #endif /* ANDROID */
2396 #ifndef OPENSSL_NO_STDIO
2397 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2398 SSL_FILETYPE_ASN1
) == 1) {
2399 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
2404 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2405 SSL_FILETYPE_PEM
) == 1) {
2407 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
2412 tls_show_errors(MSG_DEBUG
, __func__
,
2413 "SSL_use_certificate_file failed");
2414 #else /* OPENSSL_NO_STDIO */
2415 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2416 #endif /* OPENSSL_NO_STDIO */
2422 static int tls_global_client_cert(struct tls_data
*data
,
2423 const char *client_cert
)
2425 #ifndef OPENSSL_NO_STDIO
2426 SSL_CTX
*ssl_ctx
= data
->ssl
;
2428 if (client_cert
== NULL
)
2431 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2432 SSL_FILETYPE_ASN1
) != 1 &&
2433 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
2434 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2435 SSL_FILETYPE_PEM
) != 1) {
2436 tls_show_errors(MSG_INFO
, __func__
,
2437 "Failed to load client certificate");
2441 #else /* OPENSSL_NO_STDIO */
2442 if (client_cert
== NULL
)
2444 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2446 #endif /* OPENSSL_NO_STDIO */
2450 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
2452 if (password
== NULL
) {
2455 os_strlcpy(buf
, (char *) password
, size
);
2456 return os_strlen(buf
);
2461 static int tls_parse_pkcs12(struct tls_data
*data
, SSL
*ssl
, PKCS12
*p12
,
2466 STACK_OF(X509
) *certs
;
2475 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
2476 tls_show_errors(MSG_DEBUG
, __func__
,
2477 "Failed to parse PKCS12 file");
2481 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
2484 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2486 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
2487 "subject='%s'", buf
);
2489 if (SSL_use_certificate(ssl
, cert
) != 1)
2492 if (SSL_CTX_use_certificate(data
->ssl
, cert
) != 1)
2499 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
2501 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
2504 if (SSL_CTX_use_PrivateKey(data
->ssl
, pkey
) != 1)
2507 EVP_PKEY_free(pkey
);
2511 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
2513 SSL_clear_chain_certs(ssl
);
2515 SSL_CTX_clear_chain_certs(data
->ssl
);
2516 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2517 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2519 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2520 " from PKCS12: subject='%s'", buf
);
2521 if ((ssl
&& SSL_add1_chain_cert(ssl
, cert
) != 1) ||
2522 (!ssl
&& SSL_CTX_add1_chain_cert(data
->ssl
,
2524 tls_show_errors(MSG_DEBUG
, __func__
,
2525 "Failed to add additional certificate");
2533 /* Try to continue anyway */
2535 sk_X509_pop_free(certs
, X509_free
);
2536 #ifndef OPENSSL_IS_BORINGSSL
2538 res
= SSL_build_cert_chain(
2540 SSL_BUILD_CHAIN_FLAG_CHECK
|
2541 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
);
2543 res
= SSL_CTX_build_cert_chain(
2545 SSL_BUILD_CHAIN_FLAG_CHECK
|
2546 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
);
2548 tls_show_errors(MSG_DEBUG
, __func__
,
2549 "Failed to build certificate chain");
2550 } else if (res
== 2) {
2551 wpa_printf(MSG_DEBUG
,
2552 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2554 #endif /* OPENSSL_IS_BORINGSSL */
2556 * Try to continue regardless of result since it is possible for
2557 * the extra certificates not to be required.
2560 #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2561 SSL_CTX_clear_extra_chain_certs(data
->ssl
);
2562 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2563 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2565 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2566 " from PKCS12: subject='%s'", buf
);
2568 * There is no SSL equivalent for the chain cert - so
2569 * always add it to the context...
2571 if (SSL_CTX_add_extra_chain_cert(data
->ssl
, cert
) != 1)
2578 sk_X509_pop_free(certs
, X509_free
);
2579 #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2585 tls_get_errors(data
);
2589 #endif /* PKCS12_FUNCS */
2592 static int tls_read_pkcs12(struct tls_data
*data
, SSL
*ssl
,
2593 const char *private_key
, const char *passwd
)
2599 f
= fopen(private_key
, "rb");
2603 p12
= d2i_PKCS12_fp(f
, NULL
);
2607 tls_show_errors(MSG_INFO
, __func__
,
2608 "Failed to use PKCS#12 file");
2612 return tls_parse_pkcs12(data
, ssl
, p12
, passwd
);
2614 #else /* PKCS12_FUNCS */
2615 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2618 #endif /* PKCS12_FUNCS */
2622 static int tls_read_pkcs12_blob(struct tls_data
*data
, SSL
*ssl
,
2623 const u8
*blob
, size_t len
, const char *passwd
)
2628 p12
= d2i_PKCS12(NULL
, (const unsigned char **) &blob
, len
);
2630 tls_show_errors(MSG_INFO
, __func__
,
2631 "Failed to use PKCS#12 blob");
2635 return tls_parse_pkcs12(data
, ssl
, p12
, passwd
);
2637 #else /* PKCS12_FUNCS */
2638 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2641 #endif /* PKCS12_FUNCS */
2645 #ifndef OPENSSL_NO_ENGINE
2646 static int tls_engine_get_cert(struct tls_connection
*conn
,
2647 const char *cert_id
,
2650 /* this runs after the private key is loaded so no PIN is required */
2652 const char *cert_id
;
2655 params
.cert_id
= cert_id
;
2658 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2659 0, ¶ms
, NULL
, 1)) {
2660 unsigned long err
= ERR_get_error();
2662 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2663 " '%s' [%s]", cert_id
,
2664 ERR_error_string(err
, NULL
));
2665 if (tls_is_pin_error(err
))
2666 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
2667 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2670 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2672 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2674 *cert
= params
.cert
;
2677 #endif /* OPENSSL_NO_ENGINE */
2680 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2681 const char *cert_id
)
2683 #ifndef OPENSSL_NO_ENGINE
2686 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2689 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2690 tls_show_errors(MSG_ERROR
, __func__
,
2691 "SSL_use_certificate failed");
2696 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2700 #else /* OPENSSL_NO_ENGINE */
2702 #endif /* OPENSSL_NO_ENGINE */
2706 static int tls_connection_engine_ca_cert(struct tls_data
*data
,
2707 struct tls_connection
*conn
,
2708 const char *ca_cert_id
)
2710 #ifndef OPENSSL_NO_ENGINE
2712 SSL_CTX
*ssl_ctx
= data
->ssl
;
2715 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2718 /* start off the same as tls_connection_ca_cert */
2719 store
= X509_STORE_new();
2720 if (store
== NULL
) {
2721 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2722 "certificate store", __func__
);
2726 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2727 if (!X509_STORE_add_cert(store
, cert
)) {
2728 unsigned long err
= ERR_peek_error();
2729 tls_show_errors(MSG_WARNING
, __func__
,
2730 "Failed to add CA certificate from engine "
2731 "to certificate store");
2732 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2733 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2734 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2735 " already in hash table error",
2743 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2744 "to certificate store", __func__
);
2745 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2746 conn
->ca_cert_verify
= 1;
2750 #else /* OPENSSL_NO_ENGINE */
2752 #endif /* OPENSSL_NO_ENGINE */
2756 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2758 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
2759 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2760 tls_show_errors(MSG_ERROR
, __func__
,
2761 "ENGINE: cannot use private key for TLS");
2764 if (!SSL_check_private_key(conn
->ssl
)) {
2765 tls_show_errors(MSG_INFO
, __func__
,
2766 "Private key failed verification");
2770 #else /* OPENSSL_NO_ENGINE */
2771 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2772 "engine support was not compiled in");
2774 #endif /* OPENSSL_NO_ENGINE */
2778 static int tls_connection_private_key(struct tls_data
*data
,
2779 struct tls_connection
*conn
,
2780 const char *private_key
,
2781 const char *private_key_passwd
,
2782 const u8
*private_key_blob
,
2783 size_t private_key_blob_len
)
2785 SSL_CTX
*ssl_ctx
= data
->ssl
;
2789 if (private_key
== NULL
&& private_key_blob
== NULL
)
2792 if (private_key_passwd
) {
2793 passwd
= os_strdup(private_key_passwd
);
2799 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2800 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2803 while (private_key_blob
) {
2804 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2805 (u8
*) private_key_blob
,
2806 private_key_blob_len
) == 1) {
2807 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2808 "ASN1(EVP_PKEY_RSA) --> OK");
2813 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2814 (u8
*) private_key_blob
,
2815 private_key_blob_len
) == 1) {
2816 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2817 "ASN1(EVP_PKEY_DSA) --> OK");
2822 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2823 (u8
*) private_key_blob
,
2824 private_key_blob_len
) == 1) {
2825 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2826 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2831 if (tls_read_pkcs12_blob(data
, conn
->ssl
, private_key_blob
,
2832 private_key_blob_len
, passwd
) == 0) {
2833 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2842 while (!ok
&& private_key
) {
2843 #ifndef OPENSSL_NO_STDIO
2844 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2845 SSL_FILETYPE_ASN1
) == 1) {
2846 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2847 "SSL_use_PrivateKey_File (DER) --> OK");
2852 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2853 SSL_FILETYPE_PEM
) == 1) {
2854 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2855 "SSL_use_PrivateKey_File (PEM) --> OK");
2859 #else /* OPENSSL_NO_STDIO */
2860 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2862 #endif /* OPENSSL_NO_STDIO */
2864 if (tls_read_pkcs12(data
, conn
->ssl
, private_key
, passwd
)
2866 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2872 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2873 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2874 "access certificate store --> OK");
2883 tls_show_errors(MSG_INFO
, __func__
,
2884 "Failed to load private key");
2889 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2892 if (!SSL_check_private_key(conn
->ssl
)) {
2893 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
2898 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
2903 static int tls_global_private_key(struct tls_data
*data
,
2904 const char *private_key
,
2905 const char *private_key_passwd
)
2907 SSL_CTX
*ssl_ctx
= data
->ssl
;
2910 if (private_key
== NULL
)
2913 if (private_key_passwd
) {
2914 passwd
= os_strdup(private_key_passwd
);
2920 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2921 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2923 #ifndef OPENSSL_NO_STDIO
2924 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2925 SSL_FILETYPE_ASN1
) != 1 &&
2926 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
2927 SSL_FILETYPE_PEM
) != 1 &&
2928 #endif /* OPENSSL_NO_STDIO */
2929 tls_read_pkcs12(data
, NULL
, private_key
, passwd
)) {
2930 tls_show_errors(MSG_INFO
, __func__
,
2931 "Failed to load private key");
2938 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2940 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
2941 tls_show_errors(MSG_INFO
, __func__
,
2942 "Private key failed verification");
2950 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
2952 #ifdef OPENSSL_NO_DH
2953 if (dh_file
== NULL
)
2955 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
2956 "dh_file specified");
2958 #else /* OPENSSL_NO_DH */
2962 /* TODO: add support for dh_blob */
2963 if (dh_file
== NULL
)
2968 bio
= BIO_new_file(dh_file
, "r");
2970 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
2971 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
2974 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
2976 #ifndef OPENSSL_NO_DSA
2977 while (dh
== NULL
) {
2979 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
2980 " trying to parse as DSA params", dh_file
,
2981 ERR_error_string(ERR_get_error(), NULL
));
2982 bio
= BIO_new_file(dh_file
, "r");
2985 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
2988 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
2989 "'%s': %s", dh_file
,
2990 ERR_error_string(ERR_get_error(), NULL
));
2994 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
2995 dh
= DSA_dup_DH(dsa
);
2998 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
2999 "params into DH params");
3004 #endif /* !OPENSSL_NO_DSA */
3006 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
3011 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
3012 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
3014 ERR_error_string(ERR_get_error(), NULL
));
3020 #endif /* OPENSSL_NO_DH */
3024 static int tls_global_dh(struct tls_data
*data
, const char *dh_file
)
3026 #ifdef OPENSSL_NO_DH
3027 if (dh_file
== NULL
)
3029 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
3030 "dh_file specified");
3032 #else /* OPENSSL_NO_DH */
3033 SSL_CTX
*ssl_ctx
= data
->ssl
;
3037 /* TODO: add support for dh_blob */
3038 if (dh_file
== NULL
)
3040 if (ssl_ctx
== NULL
)
3043 bio
= BIO_new_file(dh_file
, "r");
3045 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
3046 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
3049 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
3051 #ifndef OPENSSL_NO_DSA
3052 while (dh
== NULL
) {
3054 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
3055 " trying to parse as DSA params", dh_file
,
3056 ERR_error_string(ERR_get_error(), NULL
));
3057 bio
= BIO_new_file(dh_file
, "r");
3060 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
3063 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
3064 "'%s': %s", dh_file
,
3065 ERR_error_string(ERR_get_error(), NULL
));
3069 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
3070 dh
= DSA_dup_DH(dsa
);
3073 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
3074 "params into DH params");
3079 #endif /* !OPENSSL_NO_DSA */
3081 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
3086 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
3087 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
3089 ERR_error_string(ERR_get_error(), NULL
));
3095 #endif /* OPENSSL_NO_DH */
3099 int tls_connection_get_random(void *ssl_ctx
, struct tls_connection
*conn
,
3100 struct tls_random
*keys
)
3104 if (conn
== NULL
|| keys
== NULL
)
3110 os_memset(keys
, 0, sizeof(*keys
));
3111 keys
->client_random
= conn
->client_random
;
3112 keys
->client_random_len
= SSL_get_client_random(
3113 ssl
, conn
->client_random
, sizeof(conn
->client_random
));
3114 keys
->server_random
= conn
->server_random
;
3115 keys
->server_random_len
= SSL_get_server_random(
3116 ssl
, conn
->server_random
, sizeof(conn
->server_random
));
3122 #ifdef OPENSSL_NEED_EAP_FAST_PRF
3123 static int openssl_get_keyblock_size(SSL
*ssl
)
3125 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
3126 const EVP_CIPHER
*c
;
3130 if (ssl
->enc_read_ctx
== NULL
|| ssl
->enc_read_ctx
->cipher
== NULL
||
3131 ssl
->read_hash
== NULL
)
3134 c
= ssl
->enc_read_ctx
->cipher
;
3135 h
= EVP_MD_CTX_md(ssl
->read_hash
);
3137 md_size
= EVP_MD_size(h
);
3139 md_size
= ssl
->s3
->tmp
.new_mac_secret_size
;
3143 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3144 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
3145 EVP_CIPHER_iv_length(c
));
3146 return 2 * (EVP_CIPHER_key_length(c
) +
3148 EVP_CIPHER_iv_length(c
));
3150 const SSL_CIPHER
*ssl_cipher
;
3152 const EVP_CIPHER
*c
;
3155 ssl_cipher
= SSL_get_current_cipher(ssl
);
3158 cipher
= SSL_CIPHER_get_cipher_nid(ssl_cipher
);
3159 digest
= SSL_CIPHER_get_digest_nid(ssl_cipher
);
3160 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher nid %d digest nid %d",
3162 if (cipher
< 0 || digest
< 0)
3164 c
= EVP_get_cipherbynid(cipher
);
3165 h
= EVP_get_digestbynid(digest
);
3169 wpa_printf(MSG_DEBUG
,
3170 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3171 EVP_CIPHER_key_length(c
), EVP_MD_size(h
),
3172 EVP_CIPHER_iv_length(c
));
3173 return 2 * (EVP_CIPHER_key_length(c
) + EVP_MD_size(h
) +
3174 EVP_CIPHER_iv_length(c
));
3177 #endif /* OPENSSL_NEED_EAP_FAST_PRF */
3180 int tls_connection_export_key(void *tls_ctx
, struct tls_connection
*conn
,
3181 const char *label
, u8
*out
, size_t out_len
)
3184 SSL_export_keying_material(conn
->ssl
, out
, out_len
, label
,
3185 os_strlen(label
), NULL
, 0, 0) != 1)
3191 int tls_connection_get_eap_fast_key(void *tls_ctx
, struct tls_connection
*conn
,
3192 u8
*out
, size_t out_len
)
3194 #ifdef OPENSSL_NEED_EAP_FAST_PRF
3202 unsigned char client_random
[SSL3_RANDOM_SIZE
];
3203 unsigned char server_random
[SSL3_RANDOM_SIZE
];
3204 unsigned char master_key
[64];
3205 size_t master_key_len
;
3209 * TLS library did not support EAP-FAST key generation, so get the
3210 * needed TLS session parameters and use an internal implementation of
3211 * TLS PRF to derive the key.
3219 ver
= SSL_get_version(ssl
);
3220 sess
= SSL_get_session(ssl
);
3224 skip
= openssl_get_keyblock_size(ssl
);
3227 tmp_out
= os_malloc(skip
+ out_len
);
3232 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
3238 SSL_get_client_random(ssl
, client_random
, sizeof(client_random
));
3239 SSL_get_server_random(ssl
, server_random
, sizeof(server_random
));
3240 master_key_len
= SSL_SESSION_get_master_key(sess
, master_key
,
3241 sizeof(master_key
));
3243 os_memcpy(rnd
, server_random
, SSL3_RANDOM_SIZE
);
3244 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, client_random
, SSL3_RANDOM_SIZE
);
3246 if (os_strcmp(ver
, "TLSv1.2") == 0) {
3247 tls_prf_sha256(master_key
, master_key_len
,
3248 "key expansion", rnd
, 2 * SSL3_RANDOM_SIZE
,
3249 _out
, skip
+ out_len
);
3251 } else if (tls_prf_sha1_md5(master_key
, master_key_len
,
3252 "key expansion", rnd
, 2 * SSL3_RANDOM_SIZE
,
3253 _out
, skip
+ out_len
) == 0) {
3256 os_memset(master_key
, 0, sizeof(master_key
));
3259 os_memcpy(out
, _out
+ skip
, out_len
);
3260 bin_clear_free(tmp_out
, skip
);
3263 #else /* OPENSSL_NEED_EAP_FAST_PRF */
3264 wpa_printf(MSG_ERROR
,
3265 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3267 #endif /* OPENSSL_NEED_EAP_FAST_PRF */
3271 static struct wpabuf
*
3272 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
3276 struct wpabuf
*out_data
;
3279 * Give TLS handshake data from the server (if available) to OpenSSL
3282 if (in_data
&& wpabuf_len(in_data
) > 0 &&
3283 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
3285 tls_show_errors(MSG_INFO
, __func__
,
3286 "Handshake failed - BIO_write");
3290 /* Initiate TLS handshake or continue the existing handshake */
3292 res
= SSL_accept(conn
->ssl
);
3294 res
= SSL_connect(conn
->ssl
);
3296 int err
= SSL_get_error(conn
->ssl
, res
);
3297 if (err
== SSL_ERROR_WANT_READ
)
3298 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
3300 else if (err
== SSL_ERROR_WANT_WRITE
)
3301 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
3304 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
3309 /* Get the TLS handshake data to be sent to the server */
3310 res
= BIO_ctrl_pending(conn
->ssl_out
);
3311 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
3312 out_data
= wpabuf_alloc(res
);
3313 if (out_data
== NULL
) {
3314 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
3315 "handshake output (%d bytes)", res
);
3316 if (BIO_reset(conn
->ssl_out
) < 0) {
3317 tls_show_errors(MSG_INFO
, __func__
,
3318 "BIO_reset failed");
3322 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
3325 tls_show_errors(MSG_INFO
, __func__
,
3326 "Handshake failed - BIO_read");
3327 if (BIO_reset(conn
->ssl_out
) < 0) {
3328 tls_show_errors(MSG_INFO
, __func__
,
3329 "BIO_reset failed");
3331 wpabuf_free(out_data
);
3334 wpabuf_put(out_data
, res
);
3340 static struct wpabuf
*
3341 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
3343 struct wpabuf
*appl_data
;
3346 appl_data
= wpabuf_alloc(max_len
+ 100);
3347 if (appl_data
== NULL
)
3350 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
3351 wpabuf_size(appl_data
));
3353 int err
= SSL_get_error(conn
->ssl
, res
);
3354 if (err
== SSL_ERROR_WANT_READ
||
3355 err
== SSL_ERROR_WANT_WRITE
) {
3356 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
3359 tls_show_errors(MSG_INFO
, __func__
,
3360 "Failed to read possible "
3361 "Application Data");
3363 wpabuf_free(appl_data
);
3367 wpabuf_put(appl_data
, res
);
3368 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
3369 "message", appl_data
);
3375 static struct wpabuf
*
3376 openssl_connection_handshake(struct tls_connection
*conn
,
3377 const struct wpabuf
*in_data
,
3378 struct wpabuf
**appl_data
, int server
)
3380 struct wpabuf
*out_data
;
3385 out_data
= openssl_handshake(conn
, in_data
, server
);
3386 if (out_data
== NULL
)
3388 if (conn
->invalid_hb_used
) {
3389 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3390 wpabuf_free(out_data
);
3394 if (SSL_is_init_finished(conn
->ssl
)) {
3395 wpa_printf(MSG_DEBUG
,
3396 "OpenSSL: Handshake finished - resumed=%d",
3397 tls_connection_resumed(conn
->ssl_ctx
, conn
));
3398 if (appl_data
&& in_data
)
3399 *appl_data
= openssl_get_appl_data(conn
,
3400 wpabuf_len(in_data
));
3403 if (conn
->invalid_hb_used
) {
3404 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3406 wpabuf_free(*appl_data
);
3409 wpabuf_free(out_data
);
3418 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
3419 const struct wpabuf
*in_data
,
3420 struct wpabuf
**appl_data
)
3422 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
3426 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
3427 struct tls_connection
*conn
,
3428 const struct wpabuf
*in_data
,
3429 struct wpabuf
**appl_data
)
3431 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
3435 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
3436 struct tls_connection
*conn
,
3437 const struct wpabuf
*in_data
)
3445 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3446 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
3447 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
3448 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3451 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
3453 tls_show_errors(MSG_INFO
, __func__
,
3454 "Encryption failed - SSL_write");
3458 /* Read encrypted data to be sent to the server */
3459 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
3462 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3464 tls_show_errors(MSG_INFO
, __func__
,
3465 "Encryption failed - BIO_read");
3469 wpabuf_put(buf
, res
);
3475 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
3476 struct tls_connection
*conn
,
3477 const struct wpabuf
*in_data
)
3482 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3483 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
3484 wpabuf_len(in_data
));
3486 tls_show_errors(MSG_INFO
, __func__
,
3487 "Decryption failed - BIO_write");
3490 if (BIO_reset(conn
->ssl_out
) < 0) {
3491 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3495 /* Read decrypted data for further processing */
3497 * Even though we try to disable TLS compression, it is possible that
3498 * this cannot be done with all TLS libraries. Add extra buffer space
3499 * to handle the possibility of the decrypted data being longer than
3502 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
3505 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3507 tls_show_errors(MSG_INFO
, __func__
,
3508 "Decryption failed - SSL_read");
3512 wpabuf_put(buf
, res
);
3514 if (conn
->invalid_hb_used
) {
3515 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3524 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
3526 return conn
? SSL_cache_hit(conn
->ssl
) : 0;
3530 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
3533 char buf
[500], *pos
, *end
;
3537 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
3542 end
= pos
+ sizeof(buf
);
3545 while (*c
!= TLS_CIPHER_NONE
) {
3549 case TLS_CIPHER_RC4_SHA
:
3552 case TLS_CIPHER_AES128_SHA
:
3553 suite
= "AES128-SHA";
3555 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
3556 suite
= "DHE-RSA-AES128-SHA";
3558 case TLS_CIPHER_ANON_DH_AES128_SHA
:
3559 suite
= "ADH-AES128-SHA";
3561 case TLS_CIPHER_RSA_DHE_AES256_SHA
:
3562 suite
= "DHE-RSA-AES256-SHA";
3564 case TLS_CIPHER_AES256_SHA
:
3565 suite
= "AES256-SHA";
3568 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
3569 "cipher selection: %d", *c
);
3572 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
3573 if (os_snprintf_error(end
- pos
, ret
))
3580 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
3582 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
3583 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3584 if (os_strstr(buf
, ":ADH-")) {
3586 * Need to drop to security level 0 to allow anonymous
3587 * cipher suites for EAP-FAST.
3589 SSL_set_security_level(conn
->ssl
, 0);
3590 } else if (SSL_get_security_level(conn
->ssl
) == 0) {
3591 /* Force at least security level 1 */
3592 SSL_set_security_level(conn
->ssl
, 1);
3594 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3597 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
3598 tls_show_errors(MSG_INFO
, __func__
,
3599 "Cipher suite configuration failed");
3607 int tls_get_version(void *ssl_ctx
, struct tls_connection
*conn
,
3608 char *buf
, size_t buflen
)
3611 if (conn
== NULL
|| conn
->ssl
== NULL
)
3614 name
= SSL_get_version(conn
->ssl
);
3618 os_strlcpy(buf
, name
, buflen
);
3623 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
3624 char *buf
, size_t buflen
)
3627 if (conn
== NULL
|| conn
->ssl
== NULL
)
3630 name
= SSL_get_cipher(conn
->ssl
);
3634 os_strlcpy(buf
, name
, buflen
);
3639 int tls_connection_enable_workaround(void *ssl_ctx
,
3640 struct tls_connection
*conn
)
3642 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3648 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3649 /* ClientHello TLS extensions require a patch to openssl, so this function is
3650 * commented out unless explicitly needed for EAP-FAST in order to be able to
3651 * build this file with unmodified openssl. */
3652 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
3653 int ext_type
, const u8
*data
,
3656 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
3659 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
3665 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3668 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
3672 return conn
->failed
;
3676 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3680 return conn
->read_alerts
;
3684 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3688 return conn
->write_alerts
;
3694 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
3696 #ifndef CONFIG_NO_STDOUT_DEBUG
3702 if (wpa_debug_level
> MSG_DEBUG
)
3705 out
= BIO_new(BIO_s_mem());
3709 OCSP_RESPONSE_print(out
, rsp
, 0);
3710 rlen
= BIO_ctrl_pending(out
);
3711 txt
= os_malloc(rlen
+ 1);
3717 res
= BIO_read(out
, txt
, rlen
);
3720 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
3724 #endif /* CONFIG_NO_STDOUT_DEBUG */
3728 static void debug_print_cert(X509
*cert
, const char *title
)
3730 #ifndef CONFIG_NO_STDOUT_DEBUG
3736 if (wpa_debug_level
> MSG_DEBUG
)
3739 out
= BIO_new(BIO_s_mem());
3743 X509_print(out
, cert
);
3744 rlen
= BIO_ctrl_pending(out
);
3745 txt
= os_malloc(rlen
+ 1);
3751 res
= BIO_read(out
, txt
, rlen
);
3754 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3759 #endif /* CONFIG_NO_STDOUT_DEBUG */
3763 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3765 struct tls_connection
*conn
= arg
;
3766 const unsigned char *p
;
3767 int len
, status
, reason
;
3769 OCSP_BASICRESP
*basic
;
3771 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3773 STACK_OF(X509
) *certs
= NULL
;
3775 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3777 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3778 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3781 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3783 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3785 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3789 ocsp_debug_print_resp(rsp
);
3791 status
= OCSP_response_status(rsp
);
3792 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3793 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3794 status
, OCSP_response_status_str(status
));
3798 basic
= OCSP_response_get1_basic(rsp
);
3800 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3804 store
= SSL_CTX_get_cert_store(conn
->ssl_ctx
);
3805 if (conn
->peer_issuer
) {
3806 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3808 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3809 tls_show_errors(MSG_INFO
, __func__
,
3810 "OpenSSL: Could not add issuer to certificate store");
3812 certs
= sk_X509_new_null();
3815 cert
= X509_dup(conn
->peer_issuer
);
3816 if (cert
&& !sk_X509_push(certs
, cert
)) {
3819 "OpenSSL: Could not add issuer to OCSP responder trust store");
3821 sk_X509_free(certs
);
3824 if (certs
&& conn
->peer_issuer_issuer
) {
3825 cert
= X509_dup(conn
->peer_issuer_issuer
);
3826 if (cert
&& !sk_X509_push(certs
, cert
)) {
3829 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
3836 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3837 sk_X509_pop_free(certs
, X509_free
);
3839 tls_show_errors(MSG_INFO
, __func__
,
3840 "OpenSSL: OCSP response failed verification");
3841 OCSP_BASICRESP_free(basic
);
3842 OCSP_RESPONSE_free(rsp
);
3846 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3848 if (!conn
->peer_cert
) {
3849 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3850 OCSP_BASICRESP_free(basic
);
3851 OCSP_RESPONSE_free(rsp
);
3855 if (!conn
->peer_issuer
) {
3856 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3857 OCSP_BASICRESP_free(basic
);
3858 OCSP_RESPONSE_free(rsp
);
3862 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3864 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not create OCSP certificate identifier");
3865 OCSP_BASICRESP_free(basic
);
3866 OCSP_RESPONSE_free(rsp
);
3870 if (!OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3871 &this_update
, &next_update
)) {
3872 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
3873 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
3874 " (OCSP not required)");
3875 OCSP_CERTID_free(id
);
3876 OCSP_BASICRESP_free(basic
);
3877 OCSP_RESPONSE_free(rsp
);
3878 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3880 OCSP_CERTID_free(id
);
3882 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
3883 tls_show_errors(MSG_INFO
, __func__
,
3884 "OpenSSL: OCSP status times invalid");
3885 OCSP_BASICRESP_free(basic
);
3886 OCSP_RESPONSE_free(rsp
);
3890 OCSP_BASICRESP_free(basic
);
3891 OCSP_RESPONSE_free(rsp
);
3893 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
3894 OCSP_cert_status_str(status
));
3896 if (status
== V_OCSP_CERTSTATUS_GOOD
)
3898 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
3900 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
3901 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
3904 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
3909 static int ocsp_status_cb(SSL
*s
, void *arg
)
3915 if (tls_global
->ocsp_stapling_response
== NULL
) {
3916 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
3917 return SSL_TLSEXT_ERR_OK
;
3920 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
3922 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
3923 /* TODO: Build OCSPResponse with responseStatus = internalError
3925 return SSL_TLSEXT_ERR_OK
;
3927 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
3928 tmp
= OPENSSL_malloc(len
);
3931 return SSL_TLSEXT_ERR_ALERT_FATAL
;
3934 os_memcpy(tmp
, resp
, len
);
3936 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
3938 return SSL_TLSEXT_ERR_OK
;
3941 #endif /* HAVE_OCSP */
3944 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
3945 const struct tls_connection_params
*params
)
3947 struct tls_data
*data
= tls_ctx
;
3951 const char *key_id
= params
->key_id
;
3952 const char *cert_id
= params
->cert_id
;
3953 const char *ca_cert_id
= params
->ca_cert_id
;
3954 const char *engine_id
= params
->engine
? params
->engine_id
: NULL
;
3959 if (params
->flags
& TLS_CONN_REQUIRE_OCSP_ALL
) {
3960 wpa_printf(MSG_INFO
,
3961 "OpenSSL: ocsp=3 not supported");
3966 * If the engine isn't explicitly configured, and any of the
3967 * cert/key fields are actually PKCS#11 URIs, then automatically
3968 * use the PKCS#11 ENGINE.
3970 if (!engine_id
|| os_strcmp(engine_id
, "pkcs11") == 0)
3973 if (!key_id
&& params
->private_key
&& can_pkcs11
&&
3974 os_strncmp(params
->private_key
, "pkcs11:", 7) == 0) {
3976 key_id
= params
->private_key
;
3979 if (!cert_id
&& params
->client_cert
&& can_pkcs11
&&
3980 os_strncmp(params
->client_cert
, "pkcs11:", 7) == 0) {
3982 cert_id
= params
->client_cert
;
3985 if (!ca_cert_id
&& params
->ca_cert
&& can_pkcs11
&&
3986 os_strncmp(params
->ca_cert
, "pkcs11:", 7) == 0) {
3988 ca_cert_id
= params
->ca_cert
;
3991 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
3992 if (can_pkcs11
== 2 && !engine_id
)
3993 engine_id
= "pkcs11";
3995 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3996 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
3997 if (params
->flags
& TLS_CONN_EAP_FAST
) {
3998 wpa_printf(MSG_DEBUG
,
3999 "OpenSSL: Use TLSv1_method() for EAP-FAST");
4000 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
4001 tls_show_errors(MSG_INFO
, __func__
,
4002 "Failed to set TLSv1_method() for EAP-FAST");
4007 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4009 while ((err
= ERR_get_error())) {
4010 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
4011 __func__
, ERR_error_string(err
, NULL
));
4015 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
4016 ret
= tls_engine_init(conn
, engine_id
, params
->pin
,
4017 key_id
, cert_id
, ca_cert_id
);
4021 if (tls_connection_set_subject_match(conn
,
4022 params
->subject_match
,
4023 params
->altsubject_match
,
4024 params
->suffix_match
,
4025 params
->domain_match
))
4028 if (engine_id
&& ca_cert_id
) {
4029 if (tls_connection_engine_ca_cert(data
, conn
, ca_cert_id
))
4030 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
4031 } else if (tls_connection_ca_cert(data
, conn
, params
->ca_cert
,
4032 params
->ca_cert_blob
,
4033 params
->ca_cert_blob_len
,
4037 if (engine_id
&& cert_id
) {
4038 if (tls_connection_engine_client_cert(conn
, cert_id
))
4039 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
4040 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
4041 params
->client_cert_blob
,
4042 params
->client_cert_blob_len
))
4045 if (engine_id
&& key_id
) {
4046 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
4047 if (tls_connection_engine_private_key(conn
))
4048 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
4049 } else if (tls_connection_private_key(data
, conn
,
4050 params
->private_key
,
4051 params
->private_key_passwd
,
4052 params
->private_key_blob
,
4053 params
->private_key_blob_len
)) {
4054 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
4055 params
->private_key
);
4059 if (tls_connection_dh(conn
, params
->dh_file
)) {
4060 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
4065 if (params
->openssl_ciphers
&&
4066 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
4067 wpa_printf(MSG_INFO
,
4068 "OpenSSL: Failed to set cipher string '%s'",
4069 params
->openssl_ciphers
);
4073 tls_set_conn_flags(conn
->ssl
, params
->flags
);
4075 #ifdef OPENSSL_IS_BORINGSSL
4076 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
4077 SSL_enable_ocsp_stapling(conn
->ssl
);
4079 #else /* OPENSSL_IS_BORINGSSL */
4081 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
4082 SSL_CTX
*ssl_ctx
= data
->ssl
;
4083 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
4084 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
4085 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
4087 #else /* HAVE_OCSP */
4088 if (params
->flags
& TLS_CONN_REQUIRE_OCSP
) {
4089 wpa_printf(MSG_INFO
,
4090 "OpenSSL: No OCSP support included - reject configuration");
4093 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
4094 wpa_printf(MSG_DEBUG
,
4095 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4097 #endif /* HAVE_OCSP */
4098 #endif /* OPENSSL_IS_BORINGSSL */
4100 conn
->flags
= params
->flags
;
4102 tls_get_errors(data
);
4108 int tls_global_set_params(void *tls_ctx
,
4109 const struct tls_connection_params
*params
)
4111 struct tls_data
*data
= tls_ctx
;
4112 SSL_CTX
*ssl_ctx
= data
->ssl
;
4115 while ((err
= ERR_get_error())) {
4116 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
4117 __func__
, ERR_error_string(err
, NULL
));
4120 if (tls_global_ca_cert(data
, params
->ca_cert
) ||
4121 tls_global_client_cert(data
, params
->client_cert
) ||
4122 tls_global_private_key(data
, params
->private_key
,
4123 params
->private_key_passwd
) ||
4124 tls_global_dh(data
, params
->dh_file
)) {
4125 wpa_printf(MSG_INFO
, "TLS: Failed to set global parameters");
4129 if (params
->openssl_ciphers
&&
4130 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
4131 wpa_printf(MSG_INFO
,
4132 "OpenSSL: Failed to set cipher string '%s'",
4133 params
->openssl_ciphers
);
4137 #ifdef SSL_OP_NO_TICKET
4138 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
4139 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
4141 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
4142 #endif /* SSL_OP_NO_TICKET */
4145 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
4146 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
4147 os_free(tls_global
->ocsp_stapling_response
);
4148 if (params
->ocsp_stapling_response
)
4149 tls_global
->ocsp_stapling_response
=
4150 os_strdup(params
->ocsp_stapling_response
);
4152 tls_global
->ocsp_stapling_response
= NULL
;
4153 #endif /* HAVE_OCSP */
4159 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4160 /* Pre-shared secred requires a patch to openssl, so this function is
4161 * commented out unless explicitly needed for EAP-FAST in order to be able to
4162 * build this file with unmodified openssl. */
4164 #if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
4165 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
4166 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
4167 const SSL_CIPHER
**cipher
, void *arg
)
4168 #else /* OPENSSL_IS_BORINGSSL */
4169 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
4170 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
4171 SSL_CIPHER
**cipher
, void *arg
)
4172 #endif /* OPENSSL_IS_BORINGSSL */
4174 struct tls_connection
*conn
= arg
;
4177 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
4178 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4181 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
4182 conn
->session_ticket
,
4183 conn
->session_ticket_len
,
4184 s
->s3
->client_random
,
4185 s
->s3
->server_random
, secret
);
4187 unsigned char client_random
[SSL3_RANDOM_SIZE
];
4188 unsigned char server_random
[SSL3_RANDOM_SIZE
];
4190 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4193 SSL_get_client_random(s
, client_random
, sizeof(client_random
));
4194 SSL_get_server_random(s
, server_random
, sizeof(server_random
));
4196 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
4197 conn
->session_ticket
,
4198 conn
->session_ticket_len
,
4200 server_random
, secret
);
4203 os_free(conn
->session_ticket
);
4204 conn
->session_ticket
= NULL
;
4209 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
4214 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
4217 struct tls_connection
*conn
= arg
;
4219 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4222 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
4224 os_free(conn
->session_ticket
);
4225 conn
->session_ticket
= NULL
;
4227 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
4228 "extension", data
, len
);
4230 conn
->session_ticket
= os_memdup(data
, len
);
4231 if (conn
->session_ticket
== NULL
)
4234 conn
->session_ticket_len
= len
;
4238 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4241 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
4242 struct tls_connection
*conn
,
4243 tls_session_ticket_cb cb
,
4246 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4247 conn
->session_ticket_cb
= cb
;
4248 conn
->session_ticket_cb_ctx
= ctx
;
4251 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
4254 SSL_set_session_ticket_ext_cb(conn
->ssl
,
4255 tls_session_ticket_ext_cb
, conn
);
4257 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
4259 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
4263 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4265 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4269 int tls_get_library_version(char *buf
, size_t buf_len
)
4271 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
4272 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
4273 OPENSSL_VERSION_TEXT
,
4274 OpenSSL_version(OPENSSL_VERSION
));
4276 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
4277 OPENSSL_VERSION_TEXT
,
4278 SSLeay_version(SSLEAY_VERSION
));
4283 void tls_connection_set_success_data(struct tls_connection
*conn
,
4284 struct wpabuf
*data
)
4289 if (tls_ex_idx_session
< 0)
4291 sess
= SSL_get_session(conn
->ssl
);
4294 old
= SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
4296 wpa_printf(MSG_DEBUG
, "OpenSSL: Replacing old success data %p",
4300 if (SSL_SESSION_set_ex_data(sess
, tls_ex_idx_session
, data
) != 1)
4303 wpa_printf(MSG_DEBUG
, "OpenSSL: Stored success data %p", data
);
4304 conn
->success_data
= 1;
4308 wpa_printf(MSG_INFO
, "OpenSSL: Failed to store success data");
4313 void tls_connection_set_success_data_resumed(struct tls_connection
*conn
)
4315 wpa_printf(MSG_DEBUG
,
4316 "OpenSSL: Success data accepted for resumed session");
4317 conn
->success_data
= 1;
4321 const struct wpabuf
*
4322 tls_connection_get_success_data(struct tls_connection
*conn
)
4326 if (tls_ex_idx_session
< 0 ||
4327 !(sess
= SSL_get_session(conn
->ssl
)))
4329 return SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
4333 void tls_connection_remove_session(struct tls_connection
*conn
)
4337 sess
= SSL_get_session(conn
->ssl
);
4341 if (SSL_CTX_remove_session(conn
->ssl_ctx
, sess
) != 1)
4342 wpa_printf(MSG_DEBUG
,
4343 "OpenSSL: Session was not cached");
4345 wpa_printf(MSG_DEBUG
,
4346 "OpenSSL: Removed cached session to disable session resumption");