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 */
106 #if OPENSSL_VERSION_NUMBER < 0x10100000L
108 static int RSA_bits(const RSA
*r
)
110 return BN_num_bits(r
->n
);
112 #endif /* CONFIG_SUITEB */
116 #include <openssl/pem.h>
117 #include <keystore/keystore_get.h>
119 static BIO
* BIO_from_keystore(const char *key
)
122 uint8_t *value
= NULL
;
123 int length
= keystore_get(key
, strlen(key
), &value
);
124 if (length
!= -1 && (bio
= BIO_new(BIO_s_mem())) != NULL
)
125 BIO_write(bio
, value
, length
);
131 static int tls_add_ca_from_keystore(X509_STORE
*ctx
, const char *key_alias
)
133 BIO
*bio
= BIO_from_keystore(key_alias
);
134 STACK_OF(X509_INFO
) *stack
= NULL
;
138 stack
= PEM_X509_INFO_read_bio(bio
, NULL
, NULL
, NULL
);
143 wpa_printf(MSG_WARNING
, "TLS: Failed to parse certificate: %s",
148 for (i
= 0; i
< sk_X509_INFO_num(stack
); ++i
) {
149 X509_INFO
*info
= sk_X509_INFO_value(stack
, i
);
152 X509_STORE_add_cert(ctx
, info
->x509
);
154 X509_STORE_add_crl(ctx
, info
->crl
);
157 sk_X509_INFO_pop_free(stack
, X509_INFO_free
);
163 static int tls_add_ca_from_keystore_encoded(X509_STORE
*ctx
,
164 const char *encoded_key_alias
)
167 int len
= os_strlen(encoded_key_alias
);
168 unsigned char *decoded_alias
;
171 wpa_printf(MSG_WARNING
, "Invalid hex-encoded alias: %s",
176 decoded_alias
= os_malloc(len
/ 2 + 1);
178 if (!hexstr2bin(encoded_key_alias
, decoded_alias
, len
/ 2)) {
179 decoded_alias
[len
/ 2] = '\0';
180 rc
= tls_add_ca_from_keystore(
181 ctx
, (const char *) decoded_alias
);
183 os_free(decoded_alias
);
191 static int tls_openssl_ref_count
= 0;
192 static int tls_ex_idx_session
= -1;
195 void (*event_cb
)(void *ctx
, enum tls_event ev
,
196 union tls_event_data
*data
);
199 char *ocsp_stapling_response
;
202 static struct tls_context
*tls_global
= NULL
;
207 unsigned int tls_session_lifetime
;
210 struct tls_connection
{
211 struct tls_context
*context
;
214 BIO
*ssl_in
, *ssl_out
;
215 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
216 ENGINE
*engine
; /* functional reference to the engine */
217 EVP_PKEY
*private_key
; /* the private key if using engine */
218 #endif /* OPENSSL_NO_ENGINE */
219 char *subject_match
, *altsubject_match
, *suffix_match
, *domain_match
;
220 int read_alerts
, write_alerts
, failed
;
222 tls_session_ticket_cb session_ticket_cb
;
223 void *session_ticket_cb_ctx
;
225 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
227 size_t session_ticket_len
;
229 unsigned int ca_cert_verify
:1;
230 unsigned int cert_probe
:1;
231 unsigned int server_cert_only
:1;
232 unsigned int invalid_hb_used
:1;
233 unsigned int success_data
:1;
235 u8 srv_cert_hash
[32];
241 X509
*peer_issuer_issuer
;
243 unsigned char client_random
[SSL3_RANDOM_SIZE
];
244 unsigned char server_random
[SSL3_RANDOM_SIZE
];
248 static struct tls_context
* tls_context_new(const struct tls_config
*conf
)
250 struct tls_context
*context
= os_zalloc(sizeof(*context
));
254 context
->event_cb
= conf
->event_cb
;
255 context
->cb_ctx
= conf
->cb_ctx
;
256 context
->cert_in_cb
= conf
->cert_in_cb
;
262 #ifdef CONFIG_NO_STDOUT_DEBUG
264 static void _tls_show_errors(void)
268 while ((err
= ERR_get_error())) {
269 /* Just ignore the errors, since stdout is disabled */
272 #define tls_show_errors(l, f, t) _tls_show_errors()
274 #else /* CONFIG_NO_STDOUT_DEBUG */
276 static void tls_show_errors(int level
, const char *func
, const char *txt
)
280 wpa_printf(level
, "OpenSSL: %s - %s %s",
281 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
283 while ((err
= ERR_get_error())) {
284 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
285 ERR_error_string(err
, NULL
));
289 #endif /* CONFIG_NO_STDOUT_DEBUG */
292 #ifdef CONFIG_NATIVE_WINDOWS
294 /* Windows CryptoAPI and access to certificate stores */
295 #include <wincrypt.h>
297 #ifdef __MINGW32_VERSION
299 * MinGW does not yet include all the needed definitions for CryptoAPI, so
300 * define here whatever extra is needed.
302 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
303 #define CERT_STORE_READONLY_FLAG 0x00008000
304 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
306 #endif /* __MINGW32_VERSION */
309 struct cryptoapi_rsa_data
{
310 const CERT_CONTEXT
*cert
;
311 HCRYPTPROV crypt_prov
;
313 BOOL free_crypt_prov
;
317 static void cryptoapi_error(const char *msg
)
319 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
320 msg
, (unsigned int) GetLastError());
324 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
325 unsigned char *to
, RSA
*rsa
, int padding
)
327 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
332 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
333 unsigned char *to
, RSA
*rsa
, int padding
)
335 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
340 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
341 unsigned char *to
, RSA
*rsa
, int padding
)
343 struct cryptoapi_rsa_data
*priv
=
344 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
346 DWORD hash_size
, len
, i
;
347 unsigned char *buf
= NULL
;
351 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
352 ERR_R_PASSED_NULL_PARAMETER
);
356 if (padding
!= RSA_PKCS1_PADDING
) {
357 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
358 RSA_R_UNKNOWN_PADDING_TYPE
);
362 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
363 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
365 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
366 RSA_R_INVALID_MESSAGE_LENGTH
);
370 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
372 cryptoapi_error("CryptCreateHash failed");
376 len
= sizeof(hash_size
);
377 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
379 cryptoapi_error("CryptGetHashParam failed");
383 if ((int) hash_size
!= flen
) {
384 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
385 (unsigned) hash_size
, flen
);
386 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
387 RSA_R_INVALID_MESSAGE_LENGTH
);
390 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
391 cryptoapi_error("CryptSetHashParam failed");
396 buf
= os_malloc(len
);
398 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
402 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
403 cryptoapi_error("CryptSignHash failed");
407 for (i
= 0; i
< len
; i
++)
408 to
[i
] = buf
[len
- i
- 1];
413 CryptDestroyHash(hash
);
419 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
420 unsigned char *to
, RSA
*rsa
, int padding
)
422 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
427 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
431 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
432 CryptReleaseContext(priv
->crypt_prov
, 0);
434 CertFreeCertificateContext(priv
->cert
);
439 static int cryptoapi_finish(RSA
*rsa
)
441 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
442 os_free((void *) rsa
->meth
);
448 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
451 const CERT_CONTEXT
*ret
= NULL
;
453 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
454 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
455 CERT_STORE_READONLY_FLAG
, L
"MY");
457 cryptoapi_error("Failed to open 'My system store'");
461 if (strncmp(name
, "cert://", 7) == 0) {
462 unsigned short wbuf
[255];
463 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
464 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
466 0, CERT_FIND_SUBJECT_STR
,
468 } else if (strncmp(name
, "hash://", 7) == 0) {
469 CRYPT_HASH_BLOB blob
;
471 const char *hash
= name
+ 7;
474 len
= os_strlen(hash
) / 2;
475 buf
= os_malloc(len
);
476 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
479 ret
= CertFindCertificateInStore(cs
,
488 CertCloseStore(cs
, 0);
494 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
497 RSA
*rsa
= NULL
, *pub_rsa
;
498 struct cryptoapi_rsa_data
*priv
;
499 RSA_METHOD
*rsa_meth
;
502 (strncmp(name
, "cert://", 7) != 0 &&
503 strncmp(name
, "hash://", 7) != 0))
506 priv
= os_zalloc(sizeof(*priv
));
507 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
508 if (priv
== NULL
|| rsa_meth
== NULL
) {
509 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
510 "for CryptoAPI RSA method");
516 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
517 if (priv
->cert
== NULL
) {
518 priv
->cert
= cryptoapi_find_cert(
519 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
521 if (priv
->cert
== NULL
) {
522 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
527 cert
= d2i_X509(NULL
,
528 (const unsigned char **) &priv
->cert
->pbCertEncoded
,
529 priv
->cert
->cbCertEncoded
);
531 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
536 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
537 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
538 NULL
, &priv
->crypt_prov
,
540 &priv
->free_crypt_prov
)) {
541 cryptoapi_error("Failed to acquire a private key for the "
546 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
547 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
548 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
549 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
550 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
551 rsa_meth
->finish
= cryptoapi_finish
;
552 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
553 rsa_meth
->app_data
= (char *) priv
;
557 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
558 ERR_R_MALLOC_FAILURE
);
562 if (!SSL_use_certificate(ssl
, cert
)) {
567 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
571 rsa
->n
= BN_dup(pub_rsa
->n
);
572 rsa
->e
= BN_dup(pub_rsa
->e
);
573 if (!RSA_set_method(rsa
, rsa_meth
))
576 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
589 cryptoapi_free_data(priv
);
595 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
598 PCCERT_CONTEXT ctx
= NULL
;
606 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
611 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
614 wsprintf(wstore
, L
"%S", store
);
615 cs
= CertOpenSystemStore(0, wstore
);
618 cs
= CertOpenSystemStore(0, store
);
621 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
622 "'%s': error=%d", __func__
, store
,
623 (int) GetLastError());
627 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
628 cert
= d2i_X509(NULL
,
629 (const unsigned char **) &ctx
->pbCertEncoded
,
632 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
633 "X509 DER encoding for CA cert");
637 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
639 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
640 "system certificate store: subject='%s'", buf
);
642 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
644 tls_show_errors(MSG_WARNING
, __func__
,
645 "Failed to add ca_cert to OpenSSL "
646 "certificate store");
652 if (!CertCloseStore(cs
, 0)) {
653 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
654 "'%s': error=%d", __func__
, name
+ 13,
655 (int) GetLastError());
662 #else /* CONFIG_NATIVE_WINDOWS */
664 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
669 #endif /* CONFIG_NATIVE_WINDOWS */
672 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
677 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
678 w
= where
& ~SSL_ST_MASK
;
679 if (w
& SSL_ST_CONNECT
)
681 else if (w
& SSL_ST_ACCEPT
)
686 if (where
& SSL_CB_LOOP
) {
687 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
688 str
, SSL_state_string_long(ssl
));
689 } else if (where
& SSL_CB_ALERT
) {
690 struct tls_connection
*conn
= SSL_get_app_data((SSL
*) ssl
);
691 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
692 where
& SSL_CB_READ
?
693 "read (remote end reported an error)" :
694 "write (local SSL3 detected an error)",
695 SSL_alert_type_string_long(ret
),
696 SSL_alert_desc_string_long(ret
));
697 if ((ret
>> 8) == SSL3_AL_FATAL
) {
698 if (where
& SSL_CB_READ
)
701 conn
->write_alerts
++;
703 if (conn
->context
->event_cb
!= NULL
) {
704 union tls_event_data ev
;
705 struct tls_context
*context
= conn
->context
;
706 os_memset(&ev
, 0, sizeof(ev
));
707 ev
.alert
.is_local
= !(where
& SSL_CB_READ
);
708 ev
.alert
.type
= SSL_alert_type_string_long(ret
);
709 ev
.alert
.description
= SSL_alert_desc_string_long(ret
);
710 context
->event_cb(context
->cb_ctx
, TLS_ALERT
, &ev
);
712 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
713 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
714 str
, ret
== 0 ? "failed" : "error",
715 SSL_state_string_long(ssl
));
720 #ifndef OPENSSL_NO_ENGINE
722 * tls_engine_load_dynamic_generic - load any openssl engine
723 * @pre: an array of commands and values that load an engine initialized
724 * in the engine specific function
725 * @post: an array of commands and values that initialize an already loaded
726 * engine (or %NULL if not required)
727 * @id: the engine id of the engine to load (only required if post is not %NULL
729 * This function is a generic function that loads any openssl engine.
731 * Returns: 0 on success, -1 on failure
733 static int tls_engine_load_dynamic_generic(const char *pre
[],
734 const char *post
[], const char *id
)
737 const char *dynamic_id
= "dynamic";
739 engine
= ENGINE_by_id(id
);
741 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
744 * If it was auto-loaded by ENGINE_by_id() we might still
745 * need to tell it which PKCS#11 module to use in legacy
746 * (non-p11-kit) environments. Do so now; even if it was
747 * properly initialised before, setting it again will be
754 engine
= ENGINE_by_id(dynamic_id
);
755 if (engine
== NULL
) {
756 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
758 ERR_error_string(ERR_get_error(), NULL
));
762 /* Perform the pre commands. This will load the engine. */
763 while (pre
&& pre
[0]) {
764 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
765 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
766 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
767 "%s %s [%s]", pre
[0], pre
[1],
768 ERR_error_string(ERR_get_error(), NULL
));
776 * Free the reference to the "dynamic" engine. The loaded engine can
777 * now be looked up using ENGINE_by_id().
781 engine
= ENGINE_by_id(id
);
782 if (engine
== NULL
) {
783 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
784 id
, ERR_error_string(ERR_get_error(), NULL
));
788 while (post
&& post
[0]) {
789 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
790 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
791 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
792 " %s %s [%s]", post
[0], post
[1],
793 ERR_error_string(ERR_get_error(), NULL
));
794 ENGINE_remove(engine
);
807 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
808 * @pkcs11_so_path: pksc11_so_path from the configuration
809 * @pcks11_module_path: pkcs11_module_path from the configuration
811 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
812 const char *pkcs11_module_path
)
814 char *engine_id
= "pkcs11";
815 const char *pre_cmd
[] = {
816 "SO_PATH", NULL
/* pkcs11_so_path */,
817 "ID", NULL
/* engine_id */,
819 /* "NO_VCHECK", "1", */
823 const char *post_cmd
[] = {
824 "MODULE_PATH", NULL
/* pkcs11_module_path */,
831 pre_cmd
[1] = pkcs11_so_path
;
832 pre_cmd
[3] = engine_id
;
833 if (pkcs11_module_path
)
834 post_cmd
[1] = pkcs11_module_path
;
838 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
841 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
846 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
847 * @opensc_so_path: opensc_so_path from the configuration
849 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
851 char *engine_id
= "opensc";
852 const char *pre_cmd
[] = {
853 "SO_PATH", NULL
/* opensc_so_path */,
854 "ID", NULL
/* engine_id */,
863 pre_cmd
[1] = opensc_so_path
;
864 pre_cmd
[3] = engine_id
;
866 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
869 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
871 #endif /* OPENSSL_NO_ENGINE */
874 static void remove_session_cb(SSL_CTX
*ctx
, SSL_SESSION
*sess
)
878 if (tls_ex_idx_session
< 0)
880 buf
= SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
883 wpa_printf(MSG_DEBUG
,
884 "OpenSSL: Free application session data %p (sess %p)",
888 SSL_SESSION_set_ex_data(sess
, tls_ex_idx_session
, NULL
);
892 void * tls_init(const struct tls_config
*conf
)
894 struct tls_data
*data
;
896 struct tls_context
*context
;
899 if (tls_openssl_ref_count
== 0) {
900 tls_global
= context
= tls_context_new(conf
);
905 if (conf
&& conf
->fips_mode
) {
906 static int fips_enabled
= 0;
908 if (!fips_enabled
&& !FIPS_mode_set(1)) {
909 wpa_printf(MSG_ERROR
, "Failed to enable FIPS "
911 ERR_load_crypto_strings();
912 ERR_print_errors_fp(stderr
);
917 wpa_printf(MSG_INFO
, "Running in FIPS mode");
921 #else /* OPENSSL_FIPS */
922 if (conf
&& conf
->fips_mode
) {
923 wpa_printf(MSG_ERROR
, "FIPS mode requested, but not "
929 #endif /* OPENSSL_FIPS */
930 #endif /* CONFIG_FIPS */
931 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
932 SSL_load_error_strings();
934 #ifndef OPENSSL_NO_SHA256
935 EVP_add_digest(EVP_sha256());
936 #endif /* OPENSSL_NO_SHA256 */
937 /* TODO: if /dev/urandom is available, PRNG is seeded
938 * automatically. If this is not the case, random data should
942 #ifndef OPENSSL_NO_RC2
944 * 40-bit RC2 is commonly used in PKCS#12 files, so enable it.
945 * This is enabled by PKCS12_PBE_add() in OpenSSL 0.9.8
946 * versions, but it looks like OpenSSL 1.0.0 does not do that
949 EVP_add_cipher(EVP_rc2_40_cbc());
950 #endif /* OPENSSL_NO_RC2 */
952 #endif /* PKCS12_FUNCS */
955 context
= tls_context_new(conf
);
959 tls_openssl_ref_count
++;
961 data
= os_zalloc(sizeof(*data
));
963 ssl
= SSL_CTX_new(SSLv23_method());
967 tls_openssl_ref_count
--;
968 if (context
!= tls_global
)
970 if (tls_openssl_ref_count
== 0) {
979 data
->tls_session_lifetime
= conf
->tls_session_lifetime
;
981 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv2
);
982 SSL_CTX_set_options(ssl
, SSL_OP_NO_SSLv3
);
984 #ifdef SSL_MODE_NO_AUTO_CHAIN
985 /* Number of deployed use cases assume the default OpenSSL behavior of
986 * auto chaining the local certificate is in use. BoringSSL removed this
987 * functionality by default, so we need to restore it here to avoid
988 * breaking existing use cases. */
989 SSL_CTX_clear_mode(ssl
, SSL_MODE_NO_AUTO_CHAIN
);
990 #endif /* SSL_MODE_NO_AUTO_CHAIN */
992 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
993 SSL_CTX_set_app_data(ssl
, context
);
994 if (data
->tls_session_lifetime
> 0) {
995 SSL_CTX_set_quiet_shutdown(ssl
, 1);
997 * Set default context here. In practice, this will be replaced
998 * by the per-EAP method context in tls_connection_set_verify().
1000 SSL_CTX_set_session_id_context(ssl
, (u8
*) "hostapd", 7);
1001 SSL_CTX_set_session_cache_mode(ssl
, SSL_SESS_CACHE_SERVER
);
1002 SSL_CTX_set_timeout(ssl
, data
->tls_session_lifetime
);
1003 SSL_CTX_sess_set_remove_cb(ssl
, remove_session_cb
);
1005 SSL_CTX_set_session_cache_mode(ssl
, SSL_SESS_CACHE_OFF
);
1008 if (tls_ex_idx_session
< 0) {
1009 tls_ex_idx_session
= SSL_SESSION_get_ex_new_index(
1010 0, NULL
, NULL
, NULL
, NULL
);
1011 if (tls_ex_idx_session
< 0) {
1017 #ifndef OPENSSL_NO_ENGINE
1018 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
1019 ERR_load_ENGINE_strings();
1020 ENGINE_load_dynamic();
1023 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
1024 conf
->pkcs11_module_path
)) {
1025 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
1026 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
1027 conf
->pkcs11_module_path
)) {
1032 #endif /* OPENSSL_NO_ENGINE */
1034 if (conf
&& conf
->openssl_ciphers
)
1035 ciphers
= conf
->openssl_ciphers
;
1037 ciphers
= TLS_DEFAULT_CIPHERS
;
1038 if (SSL_CTX_set_cipher_list(ssl
, ciphers
) != 1) {
1039 wpa_printf(MSG_ERROR
,
1040 "OpenSSL: Failed to set cipher string '%s'",
1050 void tls_deinit(void *ssl_ctx
)
1052 struct tls_data
*data
= ssl_ctx
;
1053 SSL_CTX
*ssl
= data
->ssl
;
1054 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1055 if (context
!= tls_global
)
1057 if (data
->tls_session_lifetime
> 0)
1058 SSL_CTX_flush_sessions(ssl
, 0);
1061 tls_openssl_ref_count
--;
1062 if (tls_openssl_ref_count
== 0) {
1063 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
1064 #ifndef OPENSSL_NO_ENGINE
1066 #endif /* OPENSSL_NO_ENGINE */
1067 CRYPTO_cleanup_all_ex_data();
1068 ERR_remove_thread_state(NULL
);
1071 #endif /* < 1.1.0 */
1072 os_free(tls_global
->ocsp_stapling_response
);
1073 tls_global
->ocsp_stapling_response
= NULL
;
1074 os_free(tls_global
);
1082 #ifndef OPENSSL_NO_ENGINE
1084 /* Cryptoki return values */
1085 #define CKR_PIN_INCORRECT 0x000000a0
1086 #define CKR_PIN_INVALID 0x000000a1
1087 #define CKR_PIN_LEN_RANGE 0x000000a2
1090 #define ERR_LIB_PKCS11 ERR_LIB_USER
1092 static int tls_is_pin_error(unsigned int err
)
1094 return ERR_GET_LIB(err
) == ERR_LIB_PKCS11
&&
1095 (ERR_GET_REASON(err
) == CKR_PIN_INCORRECT
||
1096 ERR_GET_REASON(err
) == CKR_PIN_INVALID
||
1097 ERR_GET_REASON(err
) == CKR_PIN_LEN_RANGE
);
1100 #endif /* OPENSSL_NO_ENGINE */
1104 /* EVP_PKEY_from_keystore comes from system/security/keystore-engine. */
1105 EVP_PKEY
* EVP_PKEY_from_keystore(const char *key_id
);
1106 #endif /* ANDROID */
1108 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
1109 const char *pin
, const char *key_id
,
1110 const char *cert_id
, const char *ca_cert_id
)
1112 #if defined(ANDROID) && defined(OPENSSL_IS_BORINGSSL)
1113 #if !defined(OPENSSL_NO_ENGINE)
1114 #error "This code depends on OPENSSL_NO_ENGINE being defined by BoringSSL."
1117 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1118 conn
->engine
= NULL
;
1119 conn
->private_key
= EVP_PKEY_from_keystore(key_id
);
1120 if (!conn
->private_key
) {
1121 wpa_printf(MSG_ERROR
,
1122 "ENGINE: cannot load private key with id '%s' [%s]",
1124 ERR_error_string(ERR_get_error(), NULL
));
1125 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1127 #endif /* ANDROID && OPENSSL_IS_BORINGSSL */
1129 #ifndef OPENSSL_NO_ENGINE
1131 if (engine_id
== NULL
) {
1132 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
1138 ENGINE_load_dynamic();
1140 conn
->engine
= ENGINE_by_id(engine_id
);
1141 if (!conn
->engine
) {
1142 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
1143 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
1146 if (ENGINE_init(conn
->engine
) != 1) {
1147 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
1148 "(engine: %s) [%s]", engine_id
,
1149 ERR_error_string(ERR_get_error(), NULL
));
1152 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
1155 if (pin
&& ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
1156 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
1157 ERR_error_string(ERR_get_error(), NULL
));
1163 * Ensure that the ENGINE does not attempt to use the OpenSSL
1164 * UI system to obtain a PIN, if we didn't provide one.
1167 const void *password
;
1168 const char *prompt_info
;
1169 } key_cb
= { "", NULL
};
1171 /* load private key first in-case PIN is required for cert */
1172 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
1175 if (!conn
->private_key
) {
1176 unsigned long err
= ERR_get_error();
1178 wpa_printf(MSG_ERROR
,
1179 "ENGINE: cannot load private key with id '%s' [%s]",
1181 ERR_error_string(err
, NULL
));
1182 if (tls_is_pin_error(err
))
1183 ret
= TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
1185 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1190 /* handle a certificate and/or CA certificate */
1191 if (cert_id
|| ca_cert_id
) {
1192 const char *cmd_name
= "LOAD_CERT_CTRL";
1194 /* test if the engine supports a LOAD_CERT_CTRL */
1195 if (!ENGINE_ctrl(conn
->engine
, ENGINE_CTRL_GET_CMD_FROM_NAME
,
1196 0, (void *)cmd_name
, NULL
)) {
1197 wpa_printf(MSG_ERROR
, "ENGINE: engine does not support"
1198 " loading certificates");
1199 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
1208 ENGINE_free(conn
->engine
);
1209 conn
->engine
= NULL
;
1212 if (conn
->private_key
) {
1213 EVP_PKEY_free(conn
->private_key
);
1214 conn
->private_key
= NULL
;
1218 #else /* OPENSSL_NO_ENGINE */
1220 #endif /* OPENSSL_NO_ENGINE */
1224 static void tls_engine_deinit(struct tls_connection
*conn
)
1226 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
1227 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
1228 if (conn
->private_key
) {
1229 EVP_PKEY_free(conn
->private_key
);
1230 conn
->private_key
= NULL
;
1233 #if !defined(OPENSSL_IS_BORINGSSL)
1234 ENGINE_finish(conn
->engine
);
1235 #endif /* !OPENSSL_IS_BORINGSSL */
1236 conn
->engine
= NULL
;
1238 #endif /* ANDROID || !OPENSSL_NO_ENGINE */
1242 int tls_get_errors(void *ssl_ctx
)
1247 while ((err
= ERR_get_error())) {
1248 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
1249 ERR_error_string(err
, NULL
));
1257 static const char * openssl_content_type(int content_type
)
1259 switch (content_type
) {
1261 return "change cipher spec";
1267 return "application data";
1271 return "TLS header info"; /* pseudo content type */
1278 static const char * openssl_handshake_type(int content_type
, const u8
*buf
,
1281 if (content_type
!= 22 || !buf
|| len
== 0)
1285 return "hello request";
1287 return "client hello";
1289 return "server hello";
1291 return "new session ticket";
1293 return "certificate";
1295 return "server key exchange";
1297 return "certificate request";
1299 return "server hello done";
1301 return "certificate verify";
1303 return "client key exchange";
1307 return "certificate url";
1309 return "certificate status";
1316 static void tls_msg_cb(int write_p
, int version
, int content_type
,
1317 const void *buf
, size_t len
, SSL
*ssl
, void *arg
)
1319 struct tls_connection
*conn
= arg
;
1320 const u8
*pos
= buf
;
1323 wpa_printf(MSG_DEBUG
,
1324 "OpenSSL: session ver=0x%x content_type=%d",
1325 version
, content_type
);
1326 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Data", buf
, len
);
1330 wpa_printf(MSG_DEBUG
, "OpenSSL: %s ver=0x%x content_type=%d (%s/%s)",
1331 write_p
? "TX" : "RX", version
, content_type
,
1332 openssl_content_type(content_type
),
1333 openssl_handshake_type(content_type
, buf
, len
));
1334 wpa_hexdump_key(MSG_MSGDUMP
, "OpenSSL: Message", buf
, len
);
1335 if (content_type
== 24 && len
>= 3 && pos
[0] == 1) {
1336 size_t payload_len
= WPA_GET_BE16(pos
+ 1);
1337 if (payload_len
+ 3 > len
) {
1338 wpa_printf(MSG_ERROR
, "OpenSSL: Heartbeat attack detected");
1339 conn
->invalid_hb_used
= 1;
1345 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
1347 struct tls_data
*data
= ssl_ctx
;
1348 SSL_CTX
*ssl
= data
->ssl
;
1349 struct tls_connection
*conn
;
1351 struct tls_context
*context
= SSL_CTX_get_app_data(ssl
);
1353 conn
= os_zalloc(sizeof(*conn
));
1356 conn
->ssl_ctx
= ssl
;
1357 conn
->ssl
= SSL_new(ssl
);
1358 if (conn
->ssl
== NULL
) {
1359 tls_show_errors(MSG_INFO
, __func__
,
1360 "Failed to initialize new SSL connection");
1365 conn
->context
= context
;
1366 SSL_set_app_data(conn
->ssl
, conn
);
1367 SSL_set_msg_callback(conn
->ssl
, tls_msg_cb
);
1368 SSL_set_msg_callback_arg(conn
->ssl
, conn
);
1369 options
= SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
1370 SSL_OP_SINGLE_DH_USE
;
1371 #ifdef SSL_OP_NO_COMPRESSION
1372 options
|= SSL_OP_NO_COMPRESSION
;
1373 #endif /* SSL_OP_NO_COMPRESSION */
1374 SSL_set_options(conn
->ssl
, options
);
1376 conn
->ssl_in
= BIO_new(BIO_s_mem());
1377 if (!conn
->ssl_in
) {
1378 tls_show_errors(MSG_INFO
, __func__
,
1379 "Failed to create a new BIO for ssl_in");
1380 SSL_free(conn
->ssl
);
1385 conn
->ssl_out
= BIO_new(BIO_s_mem());
1386 if (!conn
->ssl_out
) {
1387 tls_show_errors(MSG_INFO
, __func__
,
1388 "Failed to create a new BIO for ssl_out");
1389 SSL_free(conn
->ssl
);
1390 BIO_free(conn
->ssl_in
);
1395 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
1401 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
1405 if (conn
->success_data
) {
1407 * Make sure ssl_clear_bad_session() does not remove this
1410 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1411 SSL_shutdown(conn
->ssl
);
1413 SSL_free(conn
->ssl
);
1414 tls_engine_deinit(conn
);
1415 os_free(conn
->subject_match
);
1416 os_free(conn
->altsubject_match
);
1417 os_free(conn
->suffix_match
);
1418 os_free(conn
->domain_match
);
1419 os_free(conn
->session_ticket
);
1424 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
1426 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
1430 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
1435 /* Shutdown previous TLS connection without notifying the peer
1436 * because the connection was already terminated in practice
1437 * and "close notify" shutdown alert would confuse AS. */
1438 SSL_set_quiet_shutdown(conn
->ssl
, 1);
1439 SSL_shutdown(conn
->ssl
);
1440 return SSL_clear(conn
->ssl
) == 1 ? 0 : -1;
1444 static int tls_match_altsubject_component(X509
*cert
, int type
,
1445 const char *value
, size_t len
)
1452 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1454 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1455 gen
= sk_GENERAL_NAME_value(ext
, i
);
1456 if (gen
->type
!= type
)
1458 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
1459 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
1463 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1469 static int tls_match_altsubject(X509
*cert
, const char *match
)
1472 const char *pos
, *end
;
1477 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
1480 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
1483 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
1487 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
1491 end
= os_strchr(pos
, ';');
1493 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
1494 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
1495 os_strncmp(end
+ 1, "URI:", 4) == 0)
1497 end
= os_strchr(end
+ 1, ';');
1502 len
= os_strlen(pos
);
1503 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1512 #ifndef CONFIG_NATIVE_WINDOWS
1513 static int domain_suffix_match(const u8
*val
, size_t len
, const char *match
,
1516 size_t i
, match_len
;
1518 /* Check for embedded nuls that could mess up suffix matching */
1519 for (i
= 0; i
< len
; i
++) {
1520 if (val
[i
] == '\0') {
1521 wpa_printf(MSG_DEBUG
, "TLS: Embedded null in a string - reject");
1526 match_len
= os_strlen(match
);
1527 if (match_len
> len
|| (full
&& match_len
!= len
))
1530 if (os_strncasecmp((const char *) val
+ len
- match_len
, match
,
1532 return 0; /* no match */
1534 if (match_len
== len
)
1535 return 1; /* exact match */
1537 if (val
[len
- match_len
- 1] == '.')
1538 return 1; /* full label match completes suffix match */
1540 wpa_printf(MSG_DEBUG
, "TLS: Reject due to incomplete label match");
1543 #endif /* CONFIG_NATIVE_WINDOWS */
1546 static int tls_match_suffix(X509
*cert
, const char *match
, int full
)
1548 #ifdef CONFIG_NATIVE_WINDOWS
1549 /* wincrypt.h has conflicting X509_NAME definition */
1551 #else /* CONFIG_NATIVE_WINDOWS */
1559 wpa_printf(MSG_DEBUG
, "TLS: Match domain against %s%s",
1560 full
? "": "suffix ", match
);
1562 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
1564 for (j
= 0; ext
&& j
< sk_GENERAL_NAME_num(ext
); j
++) {
1565 gen
= sk_GENERAL_NAME_value(ext
, j
);
1566 if (gen
->type
!= GEN_DNS
)
1569 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate dNSName",
1570 gen
->d
.dNSName
->data
,
1571 gen
->d
.dNSName
->length
);
1572 if (domain_suffix_match(gen
->d
.dNSName
->data
,
1573 gen
->d
.dNSName
->length
, match
, full
) ==
1575 wpa_printf(MSG_DEBUG
, "TLS: %s in dNSName found",
1576 full
? "Match" : "Suffix match");
1577 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1581 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1584 wpa_printf(MSG_DEBUG
, "TLS: None of the dNSName(s) matched");
1588 name
= X509_get_subject_name(cert
);
1594 i
= X509_NAME_get_index_by_NID(name
, NID_commonName
, i
);
1597 e
= X509_NAME_get_entry(name
, i
);
1600 cn
= X509_NAME_ENTRY_get_data(e
);
1603 wpa_hexdump_ascii(MSG_DEBUG
, "TLS: Certificate commonName",
1604 cn
->data
, cn
->length
);
1605 if (domain_suffix_match(cn
->data
, cn
->length
, match
, full
) == 1)
1607 wpa_printf(MSG_DEBUG
, "TLS: %s in commonName found",
1608 full
? "Match" : "Suffix match");
1613 wpa_printf(MSG_DEBUG
, "TLS: No CommonName %smatch found",
1614 full
? "": "suffix ");
1616 #endif /* CONFIG_NATIVE_WINDOWS */
1620 static enum tls_fail_reason
openssl_tls_fail_reason(int err
)
1623 case X509_V_ERR_CERT_REVOKED
:
1624 return TLS_FAIL_REVOKED
;
1625 case X509_V_ERR_CERT_NOT_YET_VALID
:
1626 case X509_V_ERR_CRL_NOT_YET_VALID
:
1627 return TLS_FAIL_NOT_YET_VALID
;
1628 case X509_V_ERR_CERT_HAS_EXPIRED
:
1629 case X509_V_ERR_CRL_HAS_EXPIRED
:
1630 return TLS_FAIL_EXPIRED
;
1631 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1632 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1633 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1634 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1635 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1636 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1637 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1638 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1639 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1640 case X509_V_ERR_INVALID_CA
:
1641 return TLS_FAIL_UNTRUSTED
;
1642 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1643 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1644 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1645 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1646 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1647 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1648 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1649 case X509_V_ERR_CERT_UNTRUSTED
:
1650 case X509_V_ERR_CERT_REJECTED
:
1651 return TLS_FAIL_BAD_CERTIFICATE
;
1653 return TLS_FAIL_UNSPECIFIED
;
1658 static struct wpabuf
* get_x509_cert(X509
*cert
)
1663 int cert_len
= i2d_X509(cert
, NULL
);
1667 buf
= wpabuf_alloc(cert_len
);
1671 tmp
= wpabuf_put(buf
, cert_len
);
1672 i2d_X509(cert
, &tmp
);
1677 static void openssl_tls_fail_event(struct tls_connection
*conn
,
1678 X509
*err_cert
, int err
, int depth
,
1679 const char *subject
, const char *err_str
,
1680 enum tls_fail_reason reason
)
1682 union tls_event_data ev
;
1683 struct wpabuf
*cert
= NULL
;
1684 struct tls_context
*context
= conn
->context
;
1686 if (context
->event_cb
== NULL
)
1689 cert
= get_x509_cert(err_cert
);
1690 os_memset(&ev
, 0, sizeof(ev
));
1691 ev
.cert_fail
.reason
= reason
!= TLS_FAIL_UNSPECIFIED
?
1692 reason
: openssl_tls_fail_reason(err
);
1693 ev
.cert_fail
.depth
= depth
;
1694 ev
.cert_fail
.subject
= subject
;
1695 ev
.cert_fail
.reason_txt
= err_str
;
1696 ev
.cert_fail
.cert
= cert
;
1697 context
->event_cb(context
->cb_ctx
, TLS_CERT_CHAIN_FAILURE
, &ev
);
1702 static void openssl_tls_cert_event(struct tls_connection
*conn
,
1703 X509
*err_cert
, int depth
,
1704 const char *subject
)
1706 struct wpabuf
*cert
= NULL
;
1707 union tls_event_data ev
;
1708 struct tls_context
*context
= conn
->context
;
1709 char *altsubject
[TLS_MAX_ALT_SUBJECT
];
1710 int alt
, num_altsubject
= 0;
1714 #ifdef CONFIG_SHA256
1716 #endif /* CONFIG_SHA256 */
1718 if (context
->event_cb
== NULL
)
1721 os_memset(&ev
, 0, sizeof(ev
));
1722 if (conn
->cert_probe
|| (conn
->flags
& TLS_CONN_EXT_CERT_CHECK
) ||
1723 context
->cert_in_cb
) {
1724 cert
= get_x509_cert(err_cert
);
1725 ev
.peer_cert
.cert
= cert
;
1727 #ifdef CONFIG_SHA256
1731 addr
[0] = wpabuf_head(cert
);
1732 len
[0] = wpabuf_len(cert
);
1733 if (sha256_vector(1, addr
, len
, hash
) == 0) {
1734 ev
.peer_cert
.hash
= hash
;
1735 ev
.peer_cert
.hash_len
= sizeof(hash
);
1738 #endif /* CONFIG_SHA256 */
1739 ev
.peer_cert
.depth
= depth
;
1740 ev
.peer_cert
.subject
= subject
;
1742 ext
= X509_get_ext_d2i(err_cert
, NID_subject_alt_name
, NULL
, NULL
);
1743 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
1746 if (num_altsubject
== TLS_MAX_ALT_SUBJECT
)
1748 gen
= sk_GENERAL_NAME_value(ext
, i
);
1749 if (gen
->type
!= GEN_EMAIL
&&
1750 gen
->type
!= GEN_DNS
&&
1751 gen
->type
!= GEN_URI
)
1754 pos
= os_malloc(10 + gen
->d
.ia5
->length
+ 1);
1757 altsubject
[num_altsubject
++] = pos
;
1759 switch (gen
->type
) {
1761 os_memcpy(pos
, "EMAIL:", 6);
1765 os_memcpy(pos
, "DNS:", 4);
1769 os_memcpy(pos
, "URI:", 4);
1774 os_memcpy(pos
, gen
->d
.ia5
->data
, gen
->d
.ia5
->length
);
1775 pos
+= gen
->d
.ia5
->length
;
1778 sk_GENERAL_NAME_pop_free(ext
, GENERAL_NAME_free
);
1780 for (alt
= 0; alt
< num_altsubject
; alt
++)
1781 ev
.peer_cert
.altsubject
[alt
] = altsubject
[alt
];
1782 ev
.peer_cert
.num_altsubject
= num_altsubject
;
1784 context
->event_cb(context
->cb_ctx
, TLS_PEER_CERTIFICATE
, &ev
);
1786 for (alt
= 0; alt
< num_altsubject
; alt
++)
1787 os_free(altsubject
[alt
]);
1791 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1797 struct tls_connection
*conn
;
1798 struct tls_context
*context
;
1799 char *match
, *altmatch
, *suffix_match
, *domain_match
;
1800 const char *err_str
;
1802 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1806 err
= X509_STORE_CTX_get_error(x509_ctx
);
1807 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1808 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1809 SSL_get_ex_data_X509_STORE_CTX_idx());
1810 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1812 conn
= SSL_get_app_data(ssl
);
1817 conn
->peer_cert
= err_cert
;
1818 else if (depth
== 1)
1819 conn
->peer_issuer
= err_cert
;
1820 else if (depth
== 2)
1821 conn
->peer_issuer_issuer
= err_cert
;
1823 context
= conn
->context
;
1824 match
= conn
->subject_match
;
1825 altmatch
= conn
->altsubject_match
;
1826 suffix_match
= conn
->suffix_match
;
1827 domain_match
= conn
->domain_match
;
1829 if (!preverify_ok
&& !conn
->ca_cert_verify
)
1831 if (!preverify_ok
&& depth
> 0 && conn
->server_cert_only
)
1833 if (!preverify_ok
&& (conn
->flags
& TLS_CONN_DISABLE_TIME_CHECKS
) &&
1834 (err
== X509_V_ERR_CERT_HAS_EXPIRED
||
1835 err
== X509_V_ERR_CERT_NOT_YET_VALID
)) {
1836 wpa_printf(MSG_DEBUG
, "OpenSSL: Ignore certificate validity "
1841 err_str
= X509_verify_cert_error_string(err
);
1843 #ifdef CONFIG_SHA256
1845 * Do not require preverify_ok so we can explicity allow otherwise
1846 * invalid pinned server certificates.
1848 if (depth
== 0 && conn
->server_cert_only
) {
1849 struct wpabuf
*cert
;
1850 cert
= get_x509_cert(err_cert
);
1852 wpa_printf(MSG_DEBUG
, "OpenSSL: Could not fetch "
1853 "server certificate data");
1859 addr
[0] = wpabuf_head(cert
);
1860 len
[0] = wpabuf_len(cert
);
1861 if (sha256_vector(1, addr
, len
, hash
) < 0 ||
1862 os_memcmp(conn
->srv_cert_hash
, hash
, 32) != 0) {
1863 err_str
= "Server certificate mismatch";
1864 err
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
1866 } else if (!preverify_ok
) {
1868 * Certificate matches pinned certificate, allow
1869 * regardless of other problems.
1871 wpa_printf(MSG_DEBUG
,
1872 "OpenSSL: Ignore validation issues for a pinned server certificate");
1878 #endif /* CONFIG_SHA256 */
1880 if (!preverify_ok
) {
1881 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1882 " error %d (%s) depth %d for '%s'", err
, err_str
,
1884 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1885 err_str
, TLS_FAIL_UNSPECIFIED
);
1886 return preverify_ok
;
1889 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - preverify_ok=%d "
1890 "err=%d (%s) ca_cert_verify=%d depth=%d buf='%s'",
1891 preverify_ok
, err
, err_str
,
1892 conn
->ca_cert_verify
, depth
, buf
);
1893 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1894 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1895 "match with '%s'", buf
, match
);
1897 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1899 TLS_FAIL_SUBJECT_MISMATCH
);
1900 } else if (depth
== 0 && altmatch
&&
1901 !tls_match_altsubject(err_cert
, altmatch
)) {
1902 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1903 "'%s' not found", altmatch
);
1905 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1906 "AltSubject mismatch",
1907 TLS_FAIL_ALTSUBJECT_MISMATCH
);
1908 } else if (depth
== 0 && suffix_match
&&
1909 !tls_match_suffix(err_cert
, suffix_match
, 0)) {
1910 wpa_printf(MSG_WARNING
, "TLS: Domain suffix match '%s' not found",
1913 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1914 "Domain suffix mismatch",
1915 TLS_FAIL_DOMAIN_SUFFIX_MISMATCH
);
1916 } else if (depth
== 0 && domain_match
&&
1917 !tls_match_suffix(err_cert
, domain_match
, 1)) {
1918 wpa_printf(MSG_WARNING
, "TLS: Domain match '%s' not found",
1921 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1923 TLS_FAIL_DOMAIN_MISMATCH
);
1925 openssl_tls_cert_event(conn
, err_cert
, depth
, buf
);
1927 if (conn
->cert_probe
&& preverify_ok
&& depth
== 0) {
1928 wpa_printf(MSG_DEBUG
, "OpenSSL: Reject server certificate "
1929 "on probe-only run");
1931 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1932 "Server certificate chain probe",
1933 TLS_FAIL_SERVER_CHAIN_PROBE
);
1936 #ifdef CONFIG_SUITEB
1937 if (conn
->flags
& TLS_CONN_SUITEB
) {
1942 pk
= X509_get_pubkey(err_cert
);
1944 rsa
= EVP_PKEY_get1_RSA(pk
);
1946 len
= RSA_bits(rsa
);
1953 wpa_printf(MSG_DEBUG
,
1954 "OpenSSL: RSA modulus size: %d bits", len
);
1957 openssl_tls_fail_event(
1958 conn
, err_cert
, err
,
1960 "Insufficient RSA modulus size",
1961 TLS_FAIL_INSUFFICIENT_KEY_LEN
);
1965 #endif /* CONFIG_SUITEB */
1967 #ifdef OPENSSL_IS_BORINGSSL
1968 if (depth
== 0 && (conn
->flags
& TLS_CONN_REQUEST_OCSP
) &&
1970 enum ocsp_result res
;
1972 res
= check_ocsp_resp(conn
->ssl_ctx
, conn
->ssl
, err_cert
,
1974 conn
->peer_issuer_issuer
);
1975 if (res
== OCSP_REVOKED
) {
1977 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1978 "certificate revoked",
1980 if (err
== X509_V_OK
)
1981 X509_STORE_CTX_set_error(
1982 x509_ctx
, X509_V_ERR_CERT_REVOKED
);
1983 } else if (res
!= OCSP_GOOD
&&
1984 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
)) {
1986 openssl_tls_fail_event(conn
, err_cert
, err
, depth
, buf
,
1987 "bad certificate status response",
1988 TLS_FAIL_UNSPECIFIED
);
1991 #endif /* OPENSSL_IS_BORINGSSL */
1993 if (depth
== 0 && preverify_ok
&& context
->event_cb
!= NULL
)
1994 context
->event_cb(context
->cb_ctx
,
1995 TLS_CERT_CHAIN_SUCCESS
, NULL
);
1997 return preverify_ok
;
2001 #ifndef OPENSSL_NO_STDIO
2002 static int tls_load_ca_der(struct tls_data
*data
, const char *ca_cert
)
2004 SSL_CTX
*ssl_ctx
= data
->ssl
;
2005 X509_LOOKUP
*lookup
;
2008 lookup
= X509_STORE_add_lookup(SSL_CTX_get_cert_store(ssl_ctx
),
2009 X509_LOOKUP_file());
2010 if (lookup
== NULL
) {
2011 tls_show_errors(MSG_WARNING
, __func__
,
2012 "Failed add lookup for X509 store");
2016 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
2017 unsigned long err
= ERR_peek_error();
2018 tls_show_errors(MSG_WARNING
, __func__
,
2019 "Failed load CA in DER format");
2020 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2021 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2022 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
2023 "cert already in hash table error",
2031 #endif /* OPENSSL_NO_STDIO */
2034 static int tls_connection_ca_cert(struct tls_data
*data
,
2035 struct tls_connection
*conn
,
2036 const char *ca_cert
, const u8
*ca_cert_blob
,
2037 size_t ca_cert_blob_len
, const char *ca_path
)
2039 SSL_CTX
*ssl_ctx
= data
->ssl
;
2043 * Remove previously configured trusted CA certificates before adding
2046 store
= X509_STORE_new();
2047 if (store
== NULL
) {
2048 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2049 "certificate store", __func__
);
2052 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2054 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2055 conn
->ca_cert_verify
= 1;
2057 if (ca_cert
&& os_strncmp(ca_cert
, "probe://", 8) == 0) {
2058 wpa_printf(MSG_DEBUG
, "OpenSSL: Probe for server certificate "
2060 conn
->cert_probe
= 1;
2061 conn
->ca_cert_verify
= 0;
2065 if (ca_cert
&& os_strncmp(ca_cert
, "hash://", 7) == 0) {
2066 #ifdef CONFIG_SHA256
2067 const char *pos
= ca_cert
+ 7;
2068 if (os_strncmp(pos
, "server/sha256/", 14) != 0) {
2069 wpa_printf(MSG_DEBUG
, "OpenSSL: Unsupported ca_cert "
2070 "hash value '%s'", ca_cert
);
2074 if (os_strlen(pos
) != 32 * 2) {
2075 wpa_printf(MSG_DEBUG
, "OpenSSL: Unexpected SHA256 "
2076 "hash length in ca_cert '%s'", ca_cert
);
2079 if (hexstr2bin(pos
, conn
->srv_cert_hash
, 32) < 0) {
2080 wpa_printf(MSG_DEBUG
, "OpenSSL: Invalid SHA256 hash "
2081 "value in ca_cert '%s'", ca_cert
);
2084 conn
->server_cert_only
= 1;
2085 wpa_printf(MSG_DEBUG
, "OpenSSL: Checking only server "
2086 "certificate match");
2088 #else /* CONFIG_SHA256 */
2089 wpa_printf(MSG_INFO
, "No SHA256 included in the build - "
2090 "cannot validate server certificate hash");
2092 #endif /* CONFIG_SHA256 */
2096 X509
*cert
= d2i_X509(NULL
,
2097 (const unsigned char **) &ca_cert_blob
,
2100 tls_show_errors(MSG_WARNING
, __func__
,
2101 "Failed to parse ca_cert_blob");
2105 if (!X509_STORE_add_cert(SSL_CTX_get_cert_store(ssl_ctx
),
2107 unsigned long err
= ERR_peek_error();
2108 tls_show_errors(MSG_WARNING
, __func__
,
2109 "Failed to add ca_cert_blob to "
2110 "certificate store");
2111 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2112 ERR_GET_REASON(err
) ==
2113 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2114 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
2115 "cert already in hash table error",
2123 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
2124 "to certificate store", __func__
);
2130 if (ca_cert
&& os_strncmp("keystore://", ca_cert
, 11) == 0) {
2131 if (tls_add_ca_from_keystore(SSL_CTX_get_cert_store(ssl_ctx
),
2134 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2138 /* Multiple aliases separated by space */
2139 if (ca_cert
&& os_strncmp("keystores://", ca_cert
, 12) == 0) {
2140 char *aliases
= os_strdup(&ca_cert
[12]);
2141 const char *delim
= " ";
2148 alias
= strtok_r(aliases
, delim
, &savedptr
);
2149 for (; alias
; alias
= strtok_r(NULL
, delim
, &savedptr
)) {
2150 if (tls_add_ca_from_keystore_encoded(
2151 SSL_CTX_get_cert_store(ssl_ctx
), alias
)) {
2152 wpa_printf(MSG_WARNING
,
2153 "OpenSSL: %s - Failed to add ca_cert %s from keystore",
2163 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2166 #endif /* ANDROID */
2168 #ifdef CONFIG_NATIVE_WINDOWS
2169 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
2171 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
2172 "system certificate store");
2175 #endif /* CONFIG_NATIVE_WINDOWS */
2177 if (ca_cert
|| ca_path
) {
2178 #ifndef OPENSSL_NO_STDIO
2179 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
2181 tls_show_errors(MSG_WARNING
, __func__
,
2182 "Failed to load root certificates");
2184 tls_load_ca_der(data
, ca_cert
) == 0) {
2185 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
2186 "DER format CA certificate",
2191 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
2192 "certificate(s) loaded");
2193 tls_get_errors(data
);
2195 #else /* OPENSSL_NO_STDIO */
2196 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2199 #endif /* OPENSSL_NO_STDIO */
2201 /* No ca_cert configured - do not try to verify server
2203 conn
->ca_cert_verify
= 0;
2210 static int tls_global_ca_cert(struct tls_data
*data
, const char *ca_cert
)
2212 SSL_CTX
*ssl_ctx
= data
->ssl
;
2215 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
2217 tls_show_errors(MSG_WARNING
, __func__
,
2218 "Failed to load root certificates");
2222 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
2223 "certificate(s) loaded");
2225 #ifndef OPENSSL_NO_STDIO
2226 /* Add the same CAs to the client certificate requests */
2227 SSL_CTX_set_client_CA_list(ssl_ctx
,
2228 SSL_load_client_CA_file(ca_cert
));
2229 #endif /* OPENSSL_NO_STDIO */
2236 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
2241 struct tls_data
*data
= ssl_ctx
;
2242 X509_STORE
*cs
= SSL_CTX_get_cert_store(data
->ssl
);
2244 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
2245 "certificate store when enabling "
2249 flags
= X509_V_FLAG_CRL_CHECK
;
2251 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
2252 X509_STORE_set_flags(cs
, flags
);
2258 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
2259 const char *subject_match
,
2260 const char *altsubject_match
,
2261 const char *suffix_match
,
2262 const char *domain_match
)
2264 os_free(conn
->subject_match
);
2265 conn
->subject_match
= NULL
;
2266 if (subject_match
) {
2267 conn
->subject_match
= os_strdup(subject_match
);
2268 if (conn
->subject_match
== NULL
)
2272 os_free(conn
->altsubject_match
);
2273 conn
->altsubject_match
= NULL
;
2274 if (altsubject_match
) {
2275 conn
->altsubject_match
= os_strdup(altsubject_match
);
2276 if (conn
->altsubject_match
== NULL
)
2280 os_free(conn
->suffix_match
);
2281 conn
->suffix_match
= NULL
;
2283 conn
->suffix_match
= os_strdup(suffix_match
);
2284 if (conn
->suffix_match
== NULL
)
2288 os_free(conn
->domain_match
);
2289 conn
->domain_match
= NULL
;
2291 conn
->domain_match
= os_strdup(domain_match
);
2292 if (conn
->domain_match
== NULL
)
2300 static int tls_set_conn_flags(SSL
*ssl
, unsigned int flags
)
2302 #ifdef SSL_OP_NO_TICKET
2303 if (flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
2304 SSL_set_options(ssl
, SSL_OP_NO_TICKET
);
2306 SSL_clear_options(ssl
, SSL_OP_NO_TICKET
);
2307 #endif /* SSL_OP_NO_TICKET */
2309 #ifdef SSL_OP_NO_TLSv1
2310 if (flags
& TLS_CONN_DISABLE_TLSv1_0
)
2311 SSL_set_options(ssl
, SSL_OP_NO_TLSv1
);
2313 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1
);
2314 #endif /* SSL_OP_NO_TLSv1 */
2315 #ifdef SSL_OP_NO_TLSv1_1
2316 if (flags
& TLS_CONN_DISABLE_TLSv1_1
)
2317 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_1
);
2319 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1_1
);
2320 #endif /* SSL_OP_NO_TLSv1_1 */
2321 #ifdef SSL_OP_NO_TLSv1_2
2322 if (flags
& TLS_CONN_DISABLE_TLSv1_2
)
2323 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_2
);
2325 SSL_clear_options(ssl
, SSL_OP_NO_TLSv1_2
);
2326 #endif /* SSL_OP_NO_TLSv1_2 */
2327 #ifdef CONFIG_SUITEB
2328 if (flags
& TLS_CONN_SUITEB_NO_ECDH
) {
2329 const char *ciphers
= "DHE-RSA-AES256-GCM-SHA384";
2331 if (SSL_set_cipher_list(ssl
, ciphers
) != 1) {
2332 wpa_printf(MSG_INFO
,
2333 "OpenSSL: Failed to set Suite B ciphers");
2336 } else if (flags
& TLS_CONN_SUITEB
) {
2338 const char *ciphers
=
2339 "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384";
2341 if (SSL_set_cipher_list(ssl
, ciphers
) != 1) {
2342 wpa_printf(MSG_INFO
,
2343 "OpenSSL: Failed to set Suite B ciphers");
2347 if (SSL_set1_curves_list(ssl
, "P-384") != 1) {
2348 wpa_printf(MSG_INFO
,
2349 "OpenSSL: Failed to set Suite B curves");
2353 ecdh
= EC_KEY_new_by_curve_name(NID_secp384r1
);
2354 if (!ecdh
|| SSL_set_tmp_ecdh(ssl
, ecdh
) != 1) {
2356 wpa_printf(MSG_INFO
,
2357 "OpenSSL: Failed to set ECDH parameter");
2362 if (flags
& (TLS_CONN_SUITEB
| TLS_CONN_SUITEB_NO_ECDH
)) {
2363 /* ECDSA+SHA384 if need to add EC support here */
2364 if (SSL_set1_sigalgs_list(ssl
, "RSA+SHA384") != 1) {
2365 wpa_printf(MSG_INFO
,
2366 "OpenSSL: Failed to set Suite B sigalgs");
2370 SSL_set_options(ssl
, SSL_OP_NO_TLSv1
);
2371 SSL_set_options(ssl
, SSL_OP_NO_TLSv1_1
);
2373 #endif /* CONFIG_SUITEB */
2379 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
2380 int verify_peer
, unsigned int flags
,
2381 const u8
*session_ctx
, size_t session_ctx_len
)
2383 static int counter
= 0;
2384 struct tls_data
*data
= ssl_ctx
;
2390 conn
->ca_cert_verify
= 1;
2391 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
2392 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
2393 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
2395 conn
->ca_cert_verify
= 0;
2396 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
2399 if (tls_set_conn_flags(conn
->ssl
, flags
) < 0)
2401 conn
->flags
= flags
;
2403 SSL_set_accept_state(conn
->ssl
);
2405 if (data
->tls_session_lifetime
== 0) {
2407 * Set session id context to a unique value to make sure
2408 * session resumption cannot be used either through session
2409 * caching or TLS ticket extension.
2412 SSL_set_session_id_context(conn
->ssl
,
2413 (const unsigned char *) &counter
,
2415 } else if (session_ctx
) {
2416 SSL_set_session_id_context(conn
->ssl
, session_ctx
,
2424 static int tls_connection_client_cert(struct tls_connection
*conn
,
2425 const char *client_cert
,
2426 const u8
*client_cert_blob
,
2427 size_t client_cert_blob_len
)
2429 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
2433 #if OPENSSL_VERSION_NUMBER < 0x10002000L || defined(LIBRESSL_VERSION_NUMBER)
2435 * Clear previously set extra chain certificates, if any, from PKCS#12
2436 * processing in tls_parse_pkcs12() to allow OpenSSL to build a new
2439 SSL_CTX_clear_extra_chain_certs(conn
->ssl_ctx
);
2440 #endif /* OPENSSL_VERSION_NUMBER < 0x10002000L */
2441 #endif /* PKCS12_FUNCS */
2443 if (client_cert_blob
&&
2444 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
2445 client_cert_blob_len
) == 1) {
2446 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
2449 } else if (client_cert_blob
) {
2450 tls_show_errors(MSG_DEBUG
, __func__
,
2451 "SSL_use_certificate_ASN1 failed");
2454 if (client_cert
== NULL
)
2458 if (os_strncmp("keystore://", client_cert
, 11) == 0) {
2459 BIO
*bio
= BIO_from_keystore(&client_cert
[11]);
2463 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
2466 if (SSL_use_certificate(conn
->ssl
, x509
) == 1)
2471 /* Read additional certificates into the chain. */
2473 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
2475 /* Takes ownership of x509 */
2476 SSL_add0_chain_cert(conn
->ssl
, x509
);
2484 #endif /* ANDROID */
2486 #ifndef OPENSSL_NO_STDIO
2487 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2488 SSL_FILETYPE_ASN1
) == 1) {
2489 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
2494 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
2495 SSL_FILETYPE_PEM
) == 1) {
2497 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
2502 tls_show_errors(MSG_DEBUG
, __func__
,
2503 "SSL_use_certificate_file failed");
2504 #else /* OPENSSL_NO_STDIO */
2505 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2506 #endif /* OPENSSL_NO_STDIO */
2512 static int tls_global_client_cert(struct tls_data
*data
,
2513 const char *client_cert
)
2515 #ifndef OPENSSL_NO_STDIO
2516 SSL_CTX
*ssl_ctx
= data
->ssl
;
2518 if (client_cert
== NULL
)
2521 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2522 SSL_FILETYPE_ASN1
) != 1 &&
2523 SSL_CTX_use_certificate_chain_file(ssl_ctx
, client_cert
) != 1 &&
2524 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
2525 SSL_FILETYPE_PEM
) != 1) {
2526 tls_show_errors(MSG_INFO
, __func__
,
2527 "Failed to load client certificate");
2531 #else /* OPENSSL_NO_STDIO */
2532 if (client_cert
== NULL
)
2534 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
2536 #endif /* OPENSSL_NO_STDIO */
2540 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
2542 if (password
== NULL
) {
2545 os_strlcpy(buf
, (char *) password
, size
);
2546 return os_strlen(buf
);
2551 static int tls_parse_pkcs12(struct tls_data
*data
, SSL
*ssl
, PKCS12
*p12
,
2556 STACK_OF(X509
) *certs
;
2565 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
2566 tls_show_errors(MSG_DEBUG
, __func__
,
2567 "Failed to parse PKCS12 file");
2571 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
2574 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2576 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
2577 "subject='%s'", buf
);
2579 if (SSL_use_certificate(ssl
, cert
) != 1)
2582 if (SSL_CTX_use_certificate(data
->ssl
, cert
) != 1)
2589 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
2591 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
2594 if (SSL_CTX_use_PrivateKey(data
->ssl
, pkey
) != 1)
2597 EVP_PKEY_free(pkey
);
2601 #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(LIBRESSL_VERSION_NUMBER)
2603 SSL_clear_chain_certs(ssl
);
2605 SSL_CTX_clear_chain_certs(data
->ssl
);
2606 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2607 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2609 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2610 " from PKCS12: subject='%s'", buf
);
2611 if ((ssl
&& SSL_add1_chain_cert(ssl
, cert
) != 1) ||
2612 (!ssl
&& SSL_CTX_add1_chain_cert(data
->ssl
,
2614 tls_show_errors(MSG_DEBUG
, __func__
,
2615 "Failed to add additional certificate");
2623 /* Try to continue anyway */
2625 sk_X509_pop_free(certs
, X509_free
);
2626 #ifndef OPENSSL_IS_BORINGSSL
2628 res
= SSL_build_cert_chain(
2630 SSL_BUILD_CHAIN_FLAG_CHECK
|
2631 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
);
2633 res
= SSL_CTX_build_cert_chain(
2635 SSL_BUILD_CHAIN_FLAG_CHECK
|
2636 SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR
);
2638 tls_show_errors(MSG_DEBUG
, __func__
,
2639 "Failed to build certificate chain");
2640 } else if (res
== 2) {
2641 wpa_printf(MSG_DEBUG
,
2642 "TLS: Ignore certificate chain verification error when building chain with PKCS#12 extra certificates");
2644 #endif /* OPENSSL_IS_BORINGSSL */
2646 * Try to continue regardless of result since it is possible for
2647 * the extra certificates not to be required.
2650 #else /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2651 SSL_CTX_clear_extra_chain_certs(data
->ssl
);
2652 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
2653 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
2655 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
2656 " from PKCS12: subject='%s'", buf
);
2658 * There is no SSL equivalent for the chain cert - so
2659 * always add it to the context...
2661 if (SSL_CTX_add_extra_chain_cert(data
->ssl
, cert
) != 1)
2668 sk_X509_pop_free(certs
, X509_free
);
2669 #endif /* OPENSSL_VERSION_NUMBER >= 0x10002000L */
2675 tls_get_errors(data
);
2679 #endif /* PKCS12_FUNCS */
2682 static int tls_read_pkcs12(struct tls_data
*data
, SSL
*ssl
,
2683 const char *private_key
, const char *passwd
)
2689 f
= fopen(private_key
, "rb");
2693 p12
= d2i_PKCS12_fp(f
, NULL
);
2697 tls_show_errors(MSG_INFO
, __func__
,
2698 "Failed to use PKCS#12 file");
2702 return tls_parse_pkcs12(data
, ssl
, p12
, passwd
);
2704 #else /* PKCS12_FUNCS */
2705 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
2708 #endif /* PKCS12_FUNCS */
2712 static int tls_read_pkcs12_blob(struct tls_data
*data
, SSL
*ssl
,
2713 const u8
*blob
, size_t len
, const char *passwd
)
2718 p12
= d2i_PKCS12(NULL
, (const unsigned char **) &blob
, len
);
2720 tls_show_errors(MSG_INFO
, __func__
,
2721 "Failed to use PKCS#12 blob");
2725 return tls_parse_pkcs12(data
, ssl
, p12
, passwd
);
2727 #else /* PKCS12_FUNCS */
2728 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
2731 #endif /* PKCS12_FUNCS */
2735 #ifndef OPENSSL_NO_ENGINE
2736 static int tls_engine_get_cert(struct tls_connection
*conn
,
2737 const char *cert_id
,
2740 /* this runs after the private key is loaded so no PIN is required */
2742 const char *cert_id
;
2745 params
.cert_id
= cert_id
;
2748 if (!ENGINE_ctrl_cmd(conn
->engine
, "LOAD_CERT_CTRL",
2749 0, ¶ms
, NULL
, 1)) {
2750 unsigned long err
= ERR_get_error();
2752 wpa_printf(MSG_ERROR
, "ENGINE: cannot load client cert with id"
2753 " '%s' [%s]", cert_id
,
2754 ERR_error_string(err
, NULL
));
2755 if (tls_is_pin_error(err
))
2756 return TLS_SET_PARAMS_ENGINE_PRV_BAD_PIN
;
2757 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2760 wpa_printf(MSG_ERROR
, "ENGINE: did not properly cert with id"
2762 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
2764 *cert
= params
.cert
;
2767 #endif /* OPENSSL_NO_ENGINE */
2770 static int tls_connection_engine_client_cert(struct tls_connection
*conn
,
2771 const char *cert_id
)
2773 #ifndef OPENSSL_NO_ENGINE
2776 if (tls_engine_get_cert(conn
, cert_id
, &cert
))
2779 if (!SSL_use_certificate(conn
->ssl
, cert
)) {
2780 tls_show_errors(MSG_ERROR
, __func__
,
2781 "SSL_use_certificate failed");
2786 wpa_printf(MSG_DEBUG
, "ENGINE: SSL_use_certificate --> "
2790 #else /* OPENSSL_NO_ENGINE */
2792 #endif /* OPENSSL_NO_ENGINE */
2796 static int tls_connection_engine_ca_cert(struct tls_data
*data
,
2797 struct tls_connection
*conn
,
2798 const char *ca_cert_id
)
2800 #ifndef OPENSSL_NO_ENGINE
2802 SSL_CTX
*ssl_ctx
= data
->ssl
;
2805 if (tls_engine_get_cert(conn
, ca_cert_id
, &cert
))
2808 /* start off the same as tls_connection_ca_cert */
2809 store
= X509_STORE_new();
2810 if (store
== NULL
) {
2811 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - failed to allocate new "
2812 "certificate store", __func__
);
2816 SSL_CTX_set_cert_store(ssl_ctx
, store
);
2817 if (!X509_STORE_add_cert(store
, cert
)) {
2818 unsigned long err
= ERR_peek_error();
2819 tls_show_errors(MSG_WARNING
, __func__
,
2820 "Failed to add CA certificate from engine "
2821 "to certificate store");
2822 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
2823 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
2824 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring cert"
2825 " already in hash table error",
2833 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added CA certificate from engine "
2834 "to certificate store", __func__
);
2835 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
2836 conn
->ca_cert_verify
= 1;
2840 #else /* OPENSSL_NO_ENGINE */
2842 #endif /* OPENSSL_NO_ENGINE */
2846 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
2848 #if defined(ANDROID) || !defined(OPENSSL_NO_ENGINE)
2849 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
2850 tls_show_errors(MSG_ERROR
, __func__
,
2851 "ENGINE: cannot use private key for TLS");
2854 if (!SSL_check_private_key(conn
->ssl
)) {
2855 tls_show_errors(MSG_INFO
, __func__
,
2856 "Private key failed verification");
2860 #else /* OPENSSL_NO_ENGINE */
2861 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
2862 "engine support was not compiled in");
2864 #endif /* OPENSSL_NO_ENGINE */
2868 static void tls_clear_default_passwd_cb(SSL_CTX
*ssl_ctx
, SSL
*ssl
)
2870 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
2872 SSL_set_default_passwd_cb(ssl
, NULL
);
2873 SSL_set_default_passwd_cb_userdata(ssl
, NULL
);
2875 #endif /* >= 1.1.0f && !LibreSSL */
2876 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
2877 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, NULL
);
2881 static int tls_connection_private_key(struct tls_data
*data
,
2882 struct tls_connection
*conn
,
2883 const char *private_key
,
2884 const char *private_key_passwd
,
2885 const u8
*private_key_blob
,
2886 size_t private_key_blob_len
)
2888 SSL_CTX
*ssl_ctx
= data
->ssl
;
2892 if (private_key
== NULL
&& private_key_blob
== NULL
)
2895 if (private_key_passwd
) {
2896 passwd
= os_strdup(private_key_passwd
);
2902 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
2904 * In OpenSSL >= 1.1.0f SSL_use_PrivateKey_file() uses the callback
2905 * from the SSL object. See OpenSSL commit d61461a75253.
2907 SSL_set_default_passwd_cb(conn
->ssl
, tls_passwd_cb
);
2908 SSL_set_default_passwd_cb_userdata(conn
->ssl
, passwd
);
2909 #endif /* >= 1.1.0f && !LibreSSL */
2910 /* Keep these for OpenSSL < 1.1.0f */
2911 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
2912 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
2915 while (private_key_blob
) {
2916 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
2917 (u8
*) private_key_blob
,
2918 private_key_blob_len
) == 1) {
2919 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2920 "ASN1(EVP_PKEY_RSA) --> OK");
2925 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
2926 (u8
*) private_key_blob
,
2927 private_key_blob_len
) == 1) {
2928 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
2929 "ASN1(EVP_PKEY_DSA) --> OK");
2934 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
2935 (u8
*) private_key_blob
,
2936 private_key_blob_len
) == 1) {
2937 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2938 "SSL_use_RSAPrivateKey_ASN1 --> OK");
2943 if (tls_read_pkcs12_blob(data
, conn
->ssl
, private_key_blob
,
2944 private_key_blob_len
, passwd
) == 0) {
2945 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
2954 while (!ok
&& private_key
) {
2955 #ifndef OPENSSL_NO_STDIO
2956 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2957 SSL_FILETYPE_ASN1
) == 1) {
2958 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2959 "SSL_use_PrivateKey_File (DER) --> OK");
2964 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
2965 SSL_FILETYPE_PEM
) == 1) {
2966 wpa_printf(MSG_DEBUG
, "OpenSSL: "
2967 "SSL_use_PrivateKey_File (PEM) --> OK");
2971 #else /* OPENSSL_NO_STDIO */
2972 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
2974 #endif /* OPENSSL_NO_STDIO */
2976 if (tls_read_pkcs12(data
, conn
->ssl
, private_key
, passwd
)
2978 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
2984 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
2985 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
2986 "access certificate store --> OK");
2995 tls_show_errors(MSG_INFO
, __func__
,
2996 "Failed to load private key");
2997 tls_clear_default_passwd_cb(ssl_ctx
, conn
->ssl
);
3002 tls_clear_default_passwd_cb(ssl_ctx
, conn
->ssl
);
3005 if (!SSL_check_private_key(conn
->ssl
)) {
3006 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
3011 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
3016 static int tls_global_private_key(struct tls_data
*data
,
3017 const char *private_key
,
3018 const char *private_key_passwd
)
3020 SSL_CTX
*ssl_ctx
= data
->ssl
;
3023 if (private_key
== NULL
)
3026 if (private_key_passwd
) {
3027 passwd
= os_strdup(private_key_passwd
);
3033 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
3034 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
3036 #ifndef OPENSSL_NO_STDIO
3037 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
3038 SSL_FILETYPE_ASN1
) != 1 &&
3039 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
3040 SSL_FILETYPE_PEM
) != 1 &&
3041 #endif /* OPENSSL_NO_STDIO */
3042 tls_read_pkcs12(data
, NULL
, private_key
, passwd
)) {
3043 tls_show_errors(MSG_INFO
, __func__
,
3044 "Failed to load private key");
3045 tls_clear_default_passwd_cb(ssl_ctx
, NULL
);
3050 tls_clear_default_passwd_cb(ssl_ctx
, NULL
);
3054 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
3055 tls_show_errors(MSG_INFO
, __func__
,
3056 "Private key failed verification");
3064 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
3066 #ifdef OPENSSL_NO_DH
3067 if (dh_file
== NULL
)
3069 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
3070 "dh_file specified");
3072 #else /* OPENSSL_NO_DH */
3076 /* TODO: add support for dh_blob */
3077 if (dh_file
== NULL
)
3082 bio
= BIO_new_file(dh_file
, "r");
3084 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
3085 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
3088 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
3090 #ifndef OPENSSL_NO_DSA
3091 while (dh
== NULL
) {
3093 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
3094 " trying to parse as DSA params", dh_file
,
3095 ERR_error_string(ERR_get_error(), NULL
));
3096 bio
= BIO_new_file(dh_file
, "r");
3099 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
3102 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
3103 "'%s': %s", dh_file
,
3104 ERR_error_string(ERR_get_error(), NULL
));
3108 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
3109 dh
= DSA_dup_DH(dsa
);
3112 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
3113 "params into DH params");
3118 #endif /* !OPENSSL_NO_DSA */
3120 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
3125 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
3126 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
3128 ERR_error_string(ERR_get_error(), NULL
));
3134 #endif /* OPENSSL_NO_DH */
3138 static int tls_global_dh(struct tls_data
*data
, const char *dh_file
)
3140 #ifdef OPENSSL_NO_DH
3141 if (dh_file
== NULL
)
3143 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
3144 "dh_file specified");
3146 #else /* OPENSSL_NO_DH */
3147 SSL_CTX
*ssl_ctx
= data
->ssl
;
3151 /* TODO: add support for dh_blob */
3152 if (dh_file
== NULL
)
3154 if (ssl_ctx
== NULL
)
3157 bio
= BIO_new_file(dh_file
, "r");
3159 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
3160 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
3163 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
3165 #ifndef OPENSSL_NO_DSA
3166 while (dh
== NULL
) {
3168 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
3169 " trying to parse as DSA params", dh_file
,
3170 ERR_error_string(ERR_get_error(), NULL
));
3171 bio
= BIO_new_file(dh_file
, "r");
3174 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
3177 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
3178 "'%s': %s", dh_file
,
3179 ERR_error_string(ERR_get_error(), NULL
));
3183 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
3184 dh
= DSA_dup_DH(dsa
);
3187 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
3188 "params into DH params");
3193 #endif /* !OPENSSL_NO_DSA */
3195 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
3200 if (SSL_CTX_set_tmp_dh(ssl_ctx
, dh
) != 1) {
3201 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
3203 ERR_error_string(ERR_get_error(), NULL
));
3209 #endif /* OPENSSL_NO_DH */
3213 int tls_connection_get_random(void *ssl_ctx
, struct tls_connection
*conn
,
3214 struct tls_random
*keys
)
3218 if (conn
== NULL
|| keys
== NULL
)
3224 os_memset(keys
, 0, sizeof(*keys
));
3225 keys
->client_random
= conn
->client_random
;
3226 keys
->client_random_len
= SSL_get_client_random(
3227 ssl
, conn
->client_random
, sizeof(conn
->client_random
));
3228 keys
->server_random
= conn
->server_random
;
3229 keys
->server_random_len
= SSL_get_server_random(
3230 ssl
, conn
->server_random
, sizeof(conn
->server_random
));
3236 #ifdef OPENSSL_NEED_EAP_FAST_PRF
3237 static int openssl_get_keyblock_size(SSL
*ssl
)
3239 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
3240 const EVP_CIPHER
*c
;
3244 if (ssl
->enc_read_ctx
== NULL
|| ssl
->enc_read_ctx
->cipher
== NULL
||
3245 ssl
->read_hash
== NULL
)
3248 c
= ssl
->enc_read_ctx
->cipher
;
3249 h
= EVP_MD_CTX_md(ssl
->read_hash
);
3251 md_size
= EVP_MD_size(h
);
3253 md_size
= ssl
->s3
->tmp
.new_mac_secret_size
;
3257 wpa_printf(MSG_DEBUG
, "OpenSSL: keyblock size: key_len=%d MD_size=%d "
3258 "IV_len=%d", EVP_CIPHER_key_length(c
), md_size
,
3259 EVP_CIPHER_iv_length(c
));
3260 return 2 * (EVP_CIPHER_key_length(c
) +
3262 EVP_CIPHER_iv_length(c
));
3264 const SSL_CIPHER
*ssl_cipher
;
3266 const EVP_CIPHER
*c
;
3269 ssl_cipher
= SSL_get_current_cipher(ssl
);
3272 cipher
= SSL_CIPHER_get_cipher_nid(ssl_cipher
);
3273 digest
= SSL_CIPHER_get_digest_nid(ssl_cipher
);
3274 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher nid %d digest nid %d",
3276 if (cipher
< 0 || digest
< 0)
3278 c
= EVP_get_cipherbynid(cipher
);
3279 h
= EVP_get_digestbynid(digest
);
3283 wpa_printf(MSG_DEBUG
,
3284 "OpenSSL: keyblock size: key_len=%d MD_size=%d IV_len=%d",
3285 EVP_CIPHER_key_length(c
), EVP_MD_size(h
),
3286 EVP_CIPHER_iv_length(c
));
3287 return 2 * (EVP_CIPHER_key_length(c
) + EVP_MD_size(h
) +
3288 EVP_CIPHER_iv_length(c
));
3291 #endif /* OPENSSL_NEED_EAP_FAST_PRF */
3294 int tls_connection_export_key(void *tls_ctx
, struct tls_connection
*conn
,
3295 const char *label
, u8
*out
, size_t out_len
)
3298 SSL_export_keying_material(conn
->ssl
, out
, out_len
, label
,
3299 os_strlen(label
), NULL
, 0, 0) != 1)
3305 int tls_connection_get_eap_fast_key(void *tls_ctx
, struct tls_connection
*conn
,
3306 u8
*out
, size_t out_len
)
3308 #ifdef OPENSSL_NEED_EAP_FAST_PRF
3316 unsigned char client_random
[SSL3_RANDOM_SIZE
];
3317 unsigned char server_random
[SSL3_RANDOM_SIZE
];
3318 unsigned char master_key
[64];
3319 size_t master_key_len
;
3323 * TLS library did not support EAP-FAST key generation, so get the
3324 * needed TLS session parameters and use an internal implementation of
3325 * TLS PRF to derive the key.
3333 ver
= SSL_get_version(ssl
);
3334 sess
= SSL_get_session(ssl
);
3338 skip
= openssl_get_keyblock_size(ssl
);
3341 tmp_out
= os_malloc(skip
+ out_len
);
3346 rnd
= os_malloc(2 * SSL3_RANDOM_SIZE
);
3352 SSL_get_client_random(ssl
, client_random
, sizeof(client_random
));
3353 SSL_get_server_random(ssl
, server_random
, sizeof(server_random
));
3354 master_key_len
= SSL_SESSION_get_master_key(sess
, master_key
,
3355 sizeof(master_key
));
3357 os_memcpy(rnd
, server_random
, SSL3_RANDOM_SIZE
);
3358 os_memcpy(rnd
+ SSL3_RANDOM_SIZE
, client_random
, SSL3_RANDOM_SIZE
);
3360 if (os_strcmp(ver
, "TLSv1.2") == 0) {
3361 tls_prf_sha256(master_key
, master_key_len
,
3362 "key expansion", rnd
, 2 * SSL3_RANDOM_SIZE
,
3363 _out
, skip
+ out_len
);
3365 } else if (tls_prf_sha1_md5(master_key
, master_key_len
,
3366 "key expansion", rnd
, 2 * SSL3_RANDOM_SIZE
,
3367 _out
, skip
+ out_len
) == 0) {
3370 os_memset(master_key
, 0, sizeof(master_key
));
3373 os_memcpy(out
, _out
+ skip
, out_len
);
3374 bin_clear_free(tmp_out
, skip
);
3377 #else /* OPENSSL_NEED_EAP_FAST_PRF */
3378 wpa_printf(MSG_ERROR
,
3379 "OpenSSL: EAP-FAST keys cannot be exported in FIPS mode");
3381 #endif /* OPENSSL_NEED_EAP_FAST_PRF */
3385 static struct wpabuf
*
3386 openssl_handshake(struct tls_connection
*conn
, const struct wpabuf
*in_data
,
3390 struct wpabuf
*out_data
;
3393 * Give TLS handshake data from the server (if available) to OpenSSL
3396 if (in_data
&& wpabuf_len(in_data
) > 0 &&
3397 BIO_write(conn
->ssl_in
, wpabuf_head(in_data
), wpabuf_len(in_data
))
3399 tls_show_errors(MSG_INFO
, __func__
,
3400 "Handshake failed - BIO_write");
3404 /* Initiate TLS handshake or continue the existing handshake */
3406 res
= SSL_accept(conn
->ssl
);
3408 res
= SSL_connect(conn
->ssl
);
3410 int err
= SSL_get_error(conn
->ssl
, res
);
3411 if (err
== SSL_ERROR_WANT_READ
)
3412 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
3414 else if (err
== SSL_ERROR_WANT_WRITE
)
3415 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
3418 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
3423 /* Get the TLS handshake data to be sent to the server */
3424 res
= BIO_ctrl_pending(conn
->ssl_out
);
3425 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
3426 out_data
= wpabuf_alloc(res
);
3427 if (out_data
== NULL
) {
3428 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
3429 "handshake output (%d bytes)", res
);
3430 if (BIO_reset(conn
->ssl_out
) < 0) {
3431 tls_show_errors(MSG_INFO
, __func__
,
3432 "BIO_reset failed");
3436 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, wpabuf_mhead(out_data
),
3439 tls_show_errors(MSG_INFO
, __func__
,
3440 "Handshake failed - BIO_read");
3441 if (BIO_reset(conn
->ssl_out
) < 0) {
3442 tls_show_errors(MSG_INFO
, __func__
,
3443 "BIO_reset failed");
3445 wpabuf_free(out_data
);
3448 wpabuf_put(out_data
, res
);
3454 static struct wpabuf
*
3455 openssl_get_appl_data(struct tls_connection
*conn
, size_t max_len
)
3457 struct wpabuf
*appl_data
;
3460 appl_data
= wpabuf_alloc(max_len
+ 100);
3461 if (appl_data
== NULL
)
3464 res
= SSL_read(conn
->ssl
, wpabuf_mhead(appl_data
),
3465 wpabuf_size(appl_data
));
3467 int err
= SSL_get_error(conn
->ssl
, res
);
3468 if (err
== SSL_ERROR_WANT_READ
||
3469 err
== SSL_ERROR_WANT_WRITE
) {
3470 wpa_printf(MSG_DEBUG
, "SSL: No Application Data "
3473 tls_show_errors(MSG_INFO
, __func__
,
3474 "Failed to read possible "
3475 "Application Data");
3477 wpabuf_free(appl_data
);
3481 wpabuf_put(appl_data
, res
);
3482 wpa_hexdump_buf_key(MSG_MSGDUMP
, "SSL: Application Data in Finished "
3483 "message", appl_data
);
3489 static struct wpabuf
*
3490 openssl_connection_handshake(struct tls_connection
*conn
,
3491 const struct wpabuf
*in_data
,
3492 struct wpabuf
**appl_data
, int server
)
3494 struct wpabuf
*out_data
;
3499 out_data
= openssl_handshake(conn
, in_data
, server
);
3500 if (out_data
== NULL
)
3502 if (conn
->invalid_hb_used
) {
3503 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3504 wpabuf_free(out_data
);
3508 if (SSL_is_init_finished(conn
->ssl
)) {
3509 wpa_printf(MSG_DEBUG
,
3510 "OpenSSL: Handshake finished - resumed=%d",
3511 tls_connection_resumed(conn
->ssl_ctx
, conn
));
3512 if (appl_data
&& in_data
)
3513 *appl_data
= openssl_get_appl_data(conn
,
3514 wpabuf_len(in_data
));
3517 if (conn
->invalid_hb_used
) {
3518 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3520 wpabuf_free(*appl_data
);
3523 wpabuf_free(out_data
);
3532 tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
3533 const struct wpabuf
*in_data
,
3534 struct wpabuf
**appl_data
)
3536 return openssl_connection_handshake(conn
, in_data
, appl_data
, 0);
3540 struct wpabuf
* tls_connection_server_handshake(void *tls_ctx
,
3541 struct tls_connection
*conn
,
3542 const struct wpabuf
*in_data
,
3543 struct wpabuf
**appl_data
)
3545 return openssl_connection_handshake(conn
, in_data
, appl_data
, 1);
3549 struct wpabuf
* tls_connection_encrypt(void *tls_ctx
,
3550 struct tls_connection
*conn
,
3551 const struct wpabuf
*in_data
)
3559 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
3560 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
3561 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
3562 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3565 res
= SSL_write(conn
->ssl
, wpabuf_head(in_data
), wpabuf_len(in_data
));
3567 tls_show_errors(MSG_INFO
, __func__
,
3568 "Encryption failed - SSL_write");
3572 /* Read encrypted data to be sent to the server */
3573 buf
= wpabuf_alloc(wpabuf_len(in_data
) + 300);
3576 res
= BIO_read(conn
->ssl_out
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3578 tls_show_errors(MSG_INFO
, __func__
,
3579 "Encryption failed - BIO_read");
3583 wpabuf_put(buf
, res
);
3589 struct wpabuf
* tls_connection_decrypt(void *tls_ctx
,
3590 struct tls_connection
*conn
,
3591 const struct wpabuf
*in_data
)
3596 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
3597 res
= BIO_write(conn
->ssl_in
, wpabuf_head(in_data
),
3598 wpabuf_len(in_data
));
3600 tls_show_errors(MSG_INFO
, __func__
,
3601 "Decryption failed - BIO_write");
3604 if (BIO_reset(conn
->ssl_out
) < 0) {
3605 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
3609 /* Read decrypted data for further processing */
3611 * Even though we try to disable TLS compression, it is possible that
3612 * this cannot be done with all TLS libraries. Add extra buffer space
3613 * to handle the possibility of the decrypted data being longer than
3616 buf
= wpabuf_alloc((wpabuf_len(in_data
) + 500) * 3);
3619 res
= SSL_read(conn
->ssl
, wpabuf_mhead(buf
), wpabuf_size(buf
));
3621 tls_show_errors(MSG_INFO
, __func__
,
3622 "Decryption failed - SSL_read");
3626 wpabuf_put(buf
, res
);
3628 if (conn
->invalid_hb_used
) {
3629 wpa_printf(MSG_INFO
, "TLS: Heartbeat attack detected - do not send response");
3638 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
3640 return conn
? SSL_cache_hit(conn
->ssl
) : 0;
3644 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
3647 char buf
[500], *pos
, *end
;
3651 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
3656 end
= pos
+ sizeof(buf
);
3659 while (*c
!= TLS_CIPHER_NONE
) {
3663 case TLS_CIPHER_RC4_SHA
:
3666 case TLS_CIPHER_AES128_SHA
:
3667 suite
= "AES128-SHA";
3669 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
3670 suite
= "DHE-RSA-AES128-SHA";
3672 case TLS_CIPHER_ANON_DH_AES128_SHA
:
3673 suite
= "ADH-AES128-SHA";
3675 case TLS_CIPHER_RSA_DHE_AES256_SHA
:
3676 suite
= "DHE-RSA-AES256-SHA";
3678 case TLS_CIPHER_AES256_SHA
:
3679 suite
= "AES256-SHA";
3682 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
3683 "cipher selection: %d", *c
);
3686 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
3687 if (os_snprintf_error(end
- pos
, ret
))
3694 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
3696 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
3697 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3698 if (os_strstr(buf
, ":ADH-")) {
3700 * Need to drop to security level 0 to allow anonymous
3701 * cipher suites for EAP-FAST.
3703 SSL_set_security_level(conn
->ssl
, 0);
3704 } else if (SSL_get_security_level(conn
->ssl
) == 0) {
3705 /* Force at least security level 1 */
3706 SSL_set_security_level(conn
->ssl
, 1);
3708 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3711 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
3712 tls_show_errors(MSG_INFO
, __func__
,
3713 "Cipher suite configuration failed");
3721 int tls_get_version(void *ssl_ctx
, struct tls_connection
*conn
,
3722 char *buf
, size_t buflen
)
3725 if (conn
== NULL
|| conn
->ssl
== NULL
)
3728 name
= SSL_get_version(conn
->ssl
);
3732 os_strlcpy(buf
, name
, buflen
);
3737 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
3738 char *buf
, size_t buflen
)
3741 if (conn
== NULL
|| conn
->ssl
== NULL
)
3744 name
= SSL_get_cipher(conn
->ssl
);
3748 os_strlcpy(buf
, name
, buflen
);
3753 int tls_connection_enable_workaround(void *ssl_ctx
,
3754 struct tls_connection
*conn
)
3756 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
3762 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
3763 /* ClientHello TLS extensions require a patch to openssl, so this function is
3764 * commented out unless explicitly needed for EAP-FAST in order to be able to
3765 * build this file with unmodified openssl. */
3766 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
3767 int ext_type
, const u8
*data
,
3770 if (conn
== NULL
|| conn
->ssl
== NULL
|| ext_type
!= 35)
3773 if (SSL_set_session_ticket_ext(conn
->ssl
, (void *) data
,
3779 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
3782 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
3786 return conn
->failed
;
3790 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3794 return conn
->read_alerts
;
3798 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
3802 return conn
->write_alerts
;
3808 static void ocsp_debug_print_resp(OCSP_RESPONSE
*rsp
)
3810 #ifndef CONFIG_NO_STDOUT_DEBUG
3816 if (wpa_debug_level
> MSG_DEBUG
)
3819 out
= BIO_new(BIO_s_mem());
3823 OCSP_RESPONSE_print(out
, rsp
, 0);
3824 rlen
= BIO_ctrl_pending(out
);
3825 txt
= os_malloc(rlen
+ 1);
3831 res
= BIO_read(out
, txt
, rlen
);
3834 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP Response\n%s", txt
);
3838 #endif /* CONFIG_NO_STDOUT_DEBUG */
3842 static void debug_print_cert(X509
*cert
, const char *title
)
3844 #ifndef CONFIG_NO_STDOUT_DEBUG
3850 if (wpa_debug_level
> MSG_DEBUG
)
3853 out
= BIO_new(BIO_s_mem());
3857 X509_print(out
, cert
);
3858 rlen
= BIO_ctrl_pending(out
);
3859 txt
= os_malloc(rlen
+ 1);
3865 res
= BIO_read(out
, txt
, rlen
);
3868 wpa_printf(MSG_DEBUG
, "OpenSSL: %s\n%s", title
, txt
);
3873 #endif /* CONFIG_NO_STDOUT_DEBUG */
3877 static int ocsp_resp_cb(SSL
*s
, void *arg
)
3879 struct tls_connection
*conn
= arg
;
3880 const unsigned char *p
;
3881 int len
, status
, reason
, res
;
3883 OCSP_BASICRESP
*basic
;
3885 ASN1_GENERALIZEDTIME
*produced_at
, *this_update
, *next_update
;
3887 STACK_OF(X509
) *certs
= NULL
;
3889 len
= SSL_get_tlsext_status_ocsp_resp(s
, &p
);
3891 wpa_printf(MSG_DEBUG
, "OpenSSL: No OCSP response received");
3892 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
3895 wpa_hexdump(MSG_DEBUG
, "OpenSSL: OCSP response", p
, len
);
3897 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, len
);
3899 wpa_printf(MSG_INFO
, "OpenSSL: Failed to parse OCSP response");
3903 ocsp_debug_print_resp(rsp
);
3905 status
= OCSP_response_status(rsp
);
3906 if (status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
3907 wpa_printf(MSG_INFO
, "OpenSSL: OCSP responder error %d (%s)",
3908 status
, OCSP_response_status_str(status
));
3912 basic
= OCSP_response_get1_basic(rsp
);
3914 wpa_printf(MSG_INFO
, "OpenSSL: Could not find BasicOCSPResponse");
3918 store
= SSL_CTX_get_cert_store(conn
->ssl_ctx
);
3919 if (conn
->peer_issuer
) {
3920 debug_print_cert(conn
->peer_issuer
, "Add OCSP issuer");
3922 if (X509_STORE_add_cert(store
, conn
->peer_issuer
) != 1) {
3923 tls_show_errors(MSG_INFO
, __func__
,
3924 "OpenSSL: Could not add issuer to certificate store");
3926 certs
= sk_X509_new_null();
3929 cert
= X509_dup(conn
->peer_issuer
);
3930 if (cert
&& !sk_X509_push(certs
, cert
)) {
3933 "OpenSSL: Could not add issuer to OCSP responder trust store");
3935 sk_X509_free(certs
);
3938 if (certs
&& conn
->peer_issuer_issuer
) {
3939 cert
= X509_dup(conn
->peer_issuer_issuer
);
3940 if (cert
&& !sk_X509_push(certs
, cert
)) {
3943 "OpenSSL: Could not add issuer's issuer to OCSP responder trust store");
3950 status
= OCSP_basic_verify(basic
, certs
, store
, OCSP_TRUSTOTHER
);
3951 sk_X509_pop_free(certs
, X509_free
);
3953 tls_show_errors(MSG_INFO
, __func__
,
3954 "OpenSSL: OCSP response failed verification");
3955 OCSP_BASICRESP_free(basic
);
3956 OCSP_RESPONSE_free(rsp
);
3960 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP response verification succeeded");
3962 if (!conn
->peer_cert
) {
3963 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer certificate not available for OCSP status check");
3964 OCSP_BASICRESP_free(basic
);
3965 OCSP_RESPONSE_free(rsp
);
3969 if (!conn
->peer_issuer
) {
3970 wpa_printf(MSG_DEBUG
, "OpenSSL: Peer issuer certificate not available for OCSP status check");
3971 OCSP_BASICRESP_free(basic
);
3972 OCSP_RESPONSE_free(rsp
);
3976 id
= OCSP_cert_to_id(EVP_sha256(), conn
->peer_cert
, conn
->peer_issuer
);
3978 wpa_printf(MSG_DEBUG
,
3979 "OpenSSL: Could not create OCSP certificate identifier (SHA256)");
3980 OCSP_BASICRESP_free(basic
);
3981 OCSP_RESPONSE_free(rsp
);
3985 res
= OCSP_resp_find_status(basic
, id
, &status
, &reason
, &produced_at
,
3986 &this_update
, &next_update
);
3988 id
= OCSP_cert_to_id(NULL
, conn
->peer_cert
, conn
->peer_issuer
);
3990 wpa_printf(MSG_DEBUG
,
3991 "OpenSSL: Could not create OCSP certificate identifier (SHA1)");
3992 OCSP_BASICRESP_free(basic
);
3993 OCSP_RESPONSE_free(rsp
);
3997 res
= OCSP_resp_find_status(basic
, id
, &status
, &reason
,
3998 &produced_at
, &this_update
,
4003 wpa_printf(MSG_INFO
, "OpenSSL: Could not find current server certificate from OCSP response%s",
4004 (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? "" :
4005 " (OCSP not required)");
4006 OCSP_CERTID_free(id
);
4007 OCSP_BASICRESP_free(basic
);
4008 OCSP_RESPONSE_free(rsp
);
4009 return (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) ? 0 : 1;
4011 OCSP_CERTID_free(id
);
4013 if (!OCSP_check_validity(this_update
, next_update
, 5 * 60, -1)) {
4014 tls_show_errors(MSG_INFO
, __func__
,
4015 "OpenSSL: OCSP status times invalid");
4016 OCSP_BASICRESP_free(basic
);
4017 OCSP_RESPONSE_free(rsp
);
4021 OCSP_BASICRESP_free(basic
);
4022 OCSP_RESPONSE_free(rsp
);
4024 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status for server certificate: %s",
4025 OCSP_cert_status_str(status
));
4027 if (status
== V_OCSP_CERTSTATUS_GOOD
)
4029 if (status
== V_OCSP_CERTSTATUS_REVOKED
)
4031 if (conn
->flags
& TLS_CONN_REQUIRE_OCSP
) {
4032 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP required");
4035 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status unknown, but OCSP was not required, so allow connection to continue");
4040 static int ocsp_status_cb(SSL
*s
, void *arg
)
4046 if (tls_global
->ocsp_stapling_response
== NULL
) {
4047 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - no response configured");
4048 return SSL_TLSEXT_ERR_OK
;
4051 resp
= os_readfile(tls_global
->ocsp_stapling_response
, &len
);
4053 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - could not read response file");
4054 /* TODO: Build OCSPResponse with responseStatus = internalError
4056 return SSL_TLSEXT_ERR_OK
;
4058 wpa_printf(MSG_DEBUG
, "OpenSSL: OCSP status callback - send cached response");
4059 tmp
= OPENSSL_malloc(len
);
4062 return SSL_TLSEXT_ERR_ALERT_FATAL
;
4065 os_memcpy(tmp
, resp
, len
);
4067 SSL_set_tlsext_status_ocsp_resp(s
, tmp
, len
);
4069 return SSL_TLSEXT_ERR_OK
;
4072 #endif /* HAVE_OCSP */
4075 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
4076 const struct tls_connection_params
*params
)
4078 struct tls_data
*data
= tls_ctx
;
4082 const char *key_id
= params
->key_id
;
4083 const char *cert_id
= params
->cert_id
;
4084 const char *ca_cert_id
= params
->ca_cert_id
;
4085 const char *engine_id
= params
->engine
? params
->engine_id
: NULL
;
4090 if (params
->flags
& TLS_CONN_REQUIRE_OCSP_ALL
) {
4091 wpa_printf(MSG_INFO
,
4092 "OpenSSL: ocsp=3 not supported");
4097 * If the engine isn't explicitly configured, and any of the
4098 * cert/key fields are actually PKCS#11 URIs, then automatically
4099 * use the PKCS#11 ENGINE.
4101 if (!engine_id
|| os_strcmp(engine_id
, "pkcs11") == 0)
4104 if (!key_id
&& params
->private_key
&& can_pkcs11
&&
4105 os_strncmp(params
->private_key
, "pkcs11:", 7) == 0) {
4107 key_id
= params
->private_key
;
4110 if (!cert_id
&& params
->client_cert
&& can_pkcs11
&&
4111 os_strncmp(params
->client_cert
, "pkcs11:", 7) == 0) {
4113 cert_id
= params
->client_cert
;
4116 if (!ca_cert_id
&& params
->ca_cert
&& can_pkcs11
&&
4117 os_strncmp(params
->ca_cert
, "pkcs11:", 7) == 0) {
4119 ca_cert_id
= params
->ca_cert
;
4122 /* If we need to automatically enable the PKCS#11 ENGINE, do so. */
4123 if (can_pkcs11
== 2 && !engine_id
)
4124 engine_id
= "pkcs11";
4126 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4127 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
4128 if (params
->flags
& TLS_CONN_EAP_FAST
) {
4129 wpa_printf(MSG_DEBUG
,
4130 "OpenSSL: Use TLSv1_method() for EAP-FAST");
4131 if (SSL_set_ssl_method(conn
->ssl
, TLSv1_method()) != 1) {
4132 tls_show_errors(MSG_INFO
, __func__
,
4133 "Failed to set TLSv1_method() for EAP-FAST");
4138 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4140 while ((err
= ERR_get_error())) {
4141 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
4142 __func__
, ERR_error_string(err
, NULL
));
4146 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
4147 ret
= tls_engine_init(conn
, engine_id
, params
->pin
,
4148 key_id
, cert_id
, ca_cert_id
);
4152 if (tls_connection_set_subject_match(conn
,
4153 params
->subject_match
,
4154 params
->altsubject_match
,
4155 params
->suffix_match
,
4156 params
->domain_match
))
4159 if (engine_id
&& ca_cert_id
) {
4160 if (tls_connection_engine_ca_cert(data
, conn
, ca_cert_id
))
4161 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
4162 } else if (tls_connection_ca_cert(data
, conn
, params
->ca_cert
,
4163 params
->ca_cert_blob
,
4164 params
->ca_cert_blob_len
,
4168 if (engine_id
&& cert_id
) {
4169 if (tls_connection_engine_client_cert(conn
, cert_id
))
4170 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
4171 } else if (tls_connection_client_cert(conn
, params
->client_cert
,
4172 params
->client_cert_blob
,
4173 params
->client_cert_blob_len
))
4176 if (engine_id
&& key_id
) {
4177 wpa_printf(MSG_DEBUG
, "TLS: Using private key from engine");
4178 if (tls_connection_engine_private_key(conn
))
4179 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
4180 } else if (tls_connection_private_key(data
, conn
,
4181 params
->private_key
,
4182 params
->private_key_passwd
,
4183 params
->private_key_blob
,
4184 params
->private_key_blob_len
)) {
4185 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
4186 params
->private_key
);
4190 if (tls_connection_dh(conn
, params
->dh_file
)) {
4191 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
4196 if (params
->openssl_ciphers
&&
4197 SSL_set_cipher_list(conn
->ssl
, params
->openssl_ciphers
) != 1) {
4198 wpa_printf(MSG_INFO
,
4199 "OpenSSL: Failed to set cipher string '%s'",
4200 params
->openssl_ciphers
);
4204 if (tls_set_conn_flags(conn
->ssl
, params
->flags
) < 0)
4207 #ifdef OPENSSL_IS_BORINGSSL
4208 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
4209 SSL_enable_ocsp_stapling(conn
->ssl
);
4211 #else /* OPENSSL_IS_BORINGSSL */
4213 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
4214 SSL_CTX
*ssl_ctx
= data
->ssl
;
4215 SSL_set_tlsext_status_type(conn
->ssl
, TLSEXT_STATUSTYPE_ocsp
);
4216 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_resp_cb
);
4217 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, conn
);
4219 #else /* HAVE_OCSP */
4220 if (params
->flags
& TLS_CONN_REQUIRE_OCSP
) {
4221 wpa_printf(MSG_INFO
,
4222 "OpenSSL: No OCSP support included - reject configuration");
4225 if (params
->flags
& TLS_CONN_REQUEST_OCSP
) {
4226 wpa_printf(MSG_DEBUG
,
4227 "OpenSSL: No OCSP support included - allow optional OCSP case to continue");
4229 #endif /* HAVE_OCSP */
4230 #endif /* OPENSSL_IS_BORINGSSL */
4232 conn
->flags
= params
->flags
;
4234 tls_get_errors(data
);
4240 int tls_global_set_params(void *tls_ctx
,
4241 const struct tls_connection_params
*params
)
4243 struct tls_data
*data
= tls_ctx
;
4244 SSL_CTX
*ssl_ctx
= data
->ssl
;
4247 while ((err
= ERR_get_error())) {
4248 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
4249 __func__
, ERR_error_string(err
, NULL
));
4252 if (tls_global_ca_cert(data
, params
->ca_cert
) ||
4253 tls_global_client_cert(data
, params
->client_cert
) ||
4254 tls_global_private_key(data
, params
->private_key
,
4255 params
->private_key_passwd
) ||
4256 tls_global_dh(data
, params
->dh_file
)) {
4257 wpa_printf(MSG_INFO
, "TLS: Failed to set global parameters");
4261 if (params
->openssl_ciphers
&&
4262 SSL_CTX_set_cipher_list(ssl_ctx
, params
->openssl_ciphers
) != 1) {
4263 wpa_printf(MSG_INFO
,
4264 "OpenSSL: Failed to set cipher string '%s'",
4265 params
->openssl_ciphers
);
4269 #ifdef SSL_OP_NO_TICKET
4270 if (params
->flags
& TLS_CONN_DISABLE_SESSION_TICKET
)
4271 SSL_CTX_set_options(ssl_ctx
, SSL_OP_NO_TICKET
);
4273 SSL_CTX_clear_options(ssl_ctx
, SSL_OP_NO_TICKET
);
4274 #endif /* SSL_OP_NO_TICKET */
4277 SSL_CTX_set_tlsext_status_cb(ssl_ctx
, ocsp_status_cb
);
4278 SSL_CTX_set_tlsext_status_arg(ssl_ctx
, ssl_ctx
);
4279 os_free(tls_global
->ocsp_stapling_response
);
4280 if (params
->ocsp_stapling_response
)
4281 tls_global
->ocsp_stapling_response
=
4282 os_strdup(params
->ocsp_stapling_response
);
4284 tls_global
->ocsp_stapling_response
= NULL
;
4285 #endif /* HAVE_OCSP */
4291 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4292 /* Pre-shared secred requires a patch to openssl, so this function is
4293 * commented out unless explicitly needed for EAP-FAST in order to be able to
4294 * build this file with unmodified openssl. */
4296 #if (defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
4297 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
4298 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
4299 const SSL_CIPHER
**cipher
, void *arg
)
4300 #else /* OPENSSL_IS_BORINGSSL */
4301 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
4302 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
4303 SSL_CIPHER
**cipher
, void *arg
)
4304 #endif /* OPENSSL_IS_BORINGSSL */
4306 struct tls_connection
*conn
= arg
;
4309 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
4310 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4313 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
4314 conn
->session_ticket
,
4315 conn
->session_ticket_len
,
4316 s
->s3
->client_random
,
4317 s
->s3
->server_random
, secret
);
4319 unsigned char client_random
[SSL3_RANDOM_SIZE
];
4320 unsigned char server_random
[SSL3_RANDOM_SIZE
];
4322 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4325 SSL_get_client_random(s
, client_random
, sizeof(client_random
));
4326 SSL_get_server_random(s
, server_random
, sizeof(server_random
));
4328 ret
= conn
->session_ticket_cb(conn
->session_ticket_cb_ctx
,
4329 conn
->session_ticket
,
4330 conn
->session_ticket_len
,
4332 server_random
, secret
);
4335 os_free(conn
->session_ticket
);
4336 conn
->session_ticket
= NULL
;
4341 *secret_len
= SSL_MAX_MASTER_KEY_LENGTH
;
4346 static int tls_session_ticket_ext_cb(SSL
*s
, const unsigned char *data
,
4349 struct tls_connection
*conn
= arg
;
4351 if (conn
== NULL
|| conn
->session_ticket_cb
== NULL
)
4354 wpa_printf(MSG_DEBUG
, "OpenSSL: %s: length=%d", __func__
, len
);
4356 os_free(conn
->session_ticket
);
4357 conn
->session_ticket
= NULL
;
4359 wpa_hexdump(MSG_DEBUG
, "OpenSSL: ClientHello SessionTicket "
4360 "extension", data
, len
);
4362 conn
->session_ticket
= os_memdup(data
, len
);
4363 if (conn
->session_ticket
== NULL
)
4366 conn
->session_ticket_len
= len
;
4370 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4373 int tls_connection_set_session_ticket_cb(void *tls_ctx
,
4374 struct tls_connection
*conn
,
4375 tls_session_ticket_cb cb
,
4378 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
4379 conn
->session_ticket_cb
= cb
;
4380 conn
->session_ticket_cb_ctx
= ctx
;
4383 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
4386 SSL_set_session_ticket_ext_cb(conn
->ssl
,
4387 tls_session_ticket_ext_cb
, conn
);
4389 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
4391 SSL_set_session_ticket_ext_cb(conn
->ssl
, NULL
, NULL
);
4395 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4397 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
4401 int tls_get_library_version(char *buf
, size_t buf_len
)
4403 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
4404 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
4405 OPENSSL_VERSION_TEXT
,
4406 OpenSSL_version(OPENSSL_VERSION
));
4408 return os_snprintf(buf
, buf_len
, "OpenSSL build=%s run=%s",
4409 OPENSSL_VERSION_TEXT
,
4410 SSLeay_version(SSLEAY_VERSION
));
4415 void tls_connection_set_success_data(struct tls_connection
*conn
,
4416 struct wpabuf
*data
)
4421 if (tls_ex_idx_session
< 0)
4423 sess
= SSL_get_session(conn
->ssl
);
4426 old
= SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
4428 wpa_printf(MSG_DEBUG
, "OpenSSL: Replacing old success data %p",
4432 if (SSL_SESSION_set_ex_data(sess
, tls_ex_idx_session
, data
) != 1)
4435 wpa_printf(MSG_DEBUG
, "OpenSSL: Stored success data %p", data
);
4436 conn
->success_data
= 1;
4440 wpa_printf(MSG_INFO
, "OpenSSL: Failed to store success data");
4445 void tls_connection_set_success_data_resumed(struct tls_connection
*conn
)
4447 wpa_printf(MSG_DEBUG
,
4448 "OpenSSL: Success data accepted for resumed session");
4449 conn
->success_data
= 1;
4453 const struct wpabuf
*
4454 tls_connection_get_success_data(struct tls_connection
*conn
)
4458 if (tls_ex_idx_session
< 0 ||
4459 !(sess
= SSL_get_session(conn
->ssl
)))
4461 return SSL_SESSION_get_ex_data(sess
, tls_ex_idx_session
);
4465 void tls_connection_remove_session(struct tls_connection
*conn
)
4469 sess
= SSL_get_session(conn
->ssl
);
4473 if (SSL_CTX_remove_session(conn
->ssl_ctx
, sess
) != 1)
4474 wpa_printf(MSG_DEBUG
,
4475 "OpenSSL: Session was not cached");
4477 wpa_printf(MSG_DEBUG
,
4478 "OpenSSL: Removed cached session to disable session resumption");