2 * WPA Supplicant / SSL/TLS interface functions for openssl
3 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
17 #ifndef CONFIG_SMARTCARD
18 #ifndef OPENSSL_NO_ENGINE
19 #define OPENSSL_NO_ENGINE
23 #include <openssl/ssl.h>
24 #include <openssl/err.h>
25 #include <openssl/pkcs12.h>
26 #include <openssl/x509v3.h>
27 #ifndef OPENSSL_NO_ENGINE
28 #include <openssl/engine.h>
29 #endif /* OPENSSL_NO_ENGINE */
34 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
35 #define OPENSSL_d2i_TYPE const unsigned char **
37 #define OPENSSL_d2i_TYPE unsigned char **
40 static int tls_openssl_ref_count
= 0;
42 struct tls_connection
{
44 BIO
*ssl_in
, *ssl_out
;
45 #ifndef OPENSSL_NO_ENGINE
46 ENGINE
*engine
; /* functional reference to the engine */
47 EVP_PKEY
*private_key
; /* the private key if using engine */
48 #endif /* OPENSSL_NO_ENGINE */
49 char *subject_match
, *altsubject_match
;
50 int read_alerts
, write_alerts
, failed
;
52 u8
*pre_shared_secret
;
53 size_t pre_shared_secret_len
;
57 #ifdef CONFIG_NO_STDOUT_DEBUG
59 static void _tls_show_errors(void)
63 while ((err
= ERR_get_error())) {
64 /* Just ignore the errors, since stdout is disabled */
67 #define tls_show_errors(l, f, t) _tls_show_errors()
69 #else /* CONFIG_NO_STDOUT_DEBUG */
71 static void tls_show_errors(int level
, const char *func
, const char *txt
)
75 wpa_printf(level
, "OpenSSL: %s - %s %s",
76 func
, txt
, ERR_error_string(ERR_get_error(), NULL
));
78 while ((err
= ERR_get_error())) {
79 wpa_printf(MSG_INFO
, "OpenSSL: pending error: %s",
80 ERR_error_string(err
, NULL
));
84 #endif /* CONFIG_NO_STDOUT_DEBUG */
87 #ifdef CONFIG_NATIVE_WINDOWS
89 /* Windows CryptoAPI and access to certificate stores */
92 #ifdef __MINGW32_VERSION
94 * MinGW does not yet include all the needed definitions for CryptoAPI, so
95 * define here whatever extra is needed.
97 #define CALG_SSL3_SHAMD5 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SSL3SHAMD5)
98 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
99 #define CERT_STORE_READONLY_FLAG 0x00008000
100 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
101 #define CRYPT_ACQUIRE_COMPARE_KEY_FLAG 0x00000004
104 (*CryptAcquireCertificatePrivateKey
)(PCCERT_CONTEXT pCert
, DWORD dwFlags
,
105 void *pvReserved
, HCRYPTPROV
*phCryptProv
,
106 DWORD
*pdwKeySpec
, BOOL
*pfCallerFreeProv
)
107 = NULL
; /* to be loaded from crypt32.dll */
109 static PCCERT_CONTEXT WINAPI
110 (*CertEnumCertificatesInStore
)(HCERTSTORE hCertStore
,
111 PCCERT_CONTEXT pPrevCertContext
)
112 = NULL
; /* to be loaded from crypt32.dll */
114 static int mingw_load_crypto_func(void)
118 /* MinGW does not yet have full CryptoAPI support, so load the needed
121 if (CryptAcquireCertificatePrivateKey
)
124 dll
= LoadLibrary("crypt32");
126 wpa_printf(MSG_DEBUG
, "CryptoAPI: Could not load crypt32 "
131 CryptAcquireCertificatePrivateKey
= GetProcAddress(
132 dll
, "CryptAcquireCertificatePrivateKey");
133 if (CryptAcquireCertificatePrivateKey
== NULL
) {
134 wpa_printf(MSG_DEBUG
, "CryptoAPI: Could not get "
135 "CryptAcquireCertificatePrivateKey() address from "
140 CertEnumCertificatesInStore
= (void *) GetProcAddress(
141 dll
, "CertEnumCertificatesInStore");
142 if (CertEnumCertificatesInStore
== NULL
) {
143 wpa_printf(MSG_DEBUG
, "CryptoAPI: Could not get "
144 "CertEnumCertificatesInStore() address from "
152 #else /* __MINGW32_VERSION */
154 static int mingw_load_crypto_func(void)
159 #endif /* __MINGW32_VERSION */
162 struct cryptoapi_rsa_data
{
163 const CERT_CONTEXT
*cert
;
164 HCRYPTPROV crypt_prov
;
166 BOOL free_crypt_prov
;
170 static void cryptoapi_error(const char *msg
)
172 wpa_printf(MSG_INFO
, "CryptoAPI: %s; err=%u",
173 msg
, (unsigned int) GetLastError());
177 static int cryptoapi_rsa_pub_enc(int flen
, const unsigned char *from
,
178 unsigned char *to
, RSA
*rsa
, int padding
)
180 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
185 static int cryptoapi_rsa_pub_dec(int flen
, const unsigned char *from
,
186 unsigned char *to
, RSA
*rsa
, int padding
)
188 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
193 static int cryptoapi_rsa_priv_enc(int flen
, const unsigned char *from
,
194 unsigned char *to
, RSA
*rsa
, int padding
)
196 struct cryptoapi_rsa_data
*priv
=
197 (struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
;
199 DWORD hash_size
, len
, i
;
200 unsigned char *buf
= NULL
;
204 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
205 ERR_R_PASSED_NULL_PARAMETER
);
209 if (padding
!= RSA_PKCS1_PADDING
) {
210 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
211 RSA_R_UNKNOWN_PADDING_TYPE
);
215 if (flen
!= 16 /* MD5 */ + 20 /* SHA-1 */) {
216 wpa_printf(MSG_INFO
, "%s - only MD5-SHA1 hash supported",
218 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
219 RSA_R_INVALID_MESSAGE_LENGTH
);
223 if (!CryptCreateHash(priv
->crypt_prov
, CALG_SSL3_SHAMD5
, 0, 0, &hash
))
225 cryptoapi_error("CryptCreateHash failed");
229 len
= sizeof(hash_size
);
230 if (!CryptGetHashParam(hash
, HP_HASHSIZE
, (BYTE
*) &hash_size
, &len
,
232 cryptoapi_error("CryptGetHashParam failed");
236 if ((int) hash_size
!= flen
) {
237 wpa_printf(MSG_INFO
, "CryptoAPI: Invalid hash size (%u != %d)",
238 (unsigned) hash_size
, flen
);
239 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
,
240 RSA_R_INVALID_MESSAGE_LENGTH
);
243 if (!CryptSetHashParam(hash
, HP_HASHVAL
, (BYTE
* ) from
, 0)) {
244 cryptoapi_error("CryptSetHashParam failed");
249 buf
= os_malloc(len
);
251 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT
, ERR_R_MALLOC_FAILURE
);
255 if (!CryptSignHash(hash
, priv
->key_spec
, NULL
, 0, buf
, &len
)) {
256 cryptoapi_error("CryptSignHash failed");
260 for (i
= 0; i
< len
; i
++)
261 to
[i
] = buf
[len
- i
- 1];
266 CryptDestroyHash(hash
);
272 static int cryptoapi_rsa_priv_dec(int flen
, const unsigned char *from
,
273 unsigned char *to
, RSA
*rsa
, int padding
)
275 wpa_printf(MSG_DEBUG
, "%s - not implemented", __func__
);
280 static void cryptoapi_free_data(struct cryptoapi_rsa_data
*priv
)
284 if (priv
->crypt_prov
&& priv
->free_crypt_prov
)
285 CryptReleaseContext(priv
->crypt_prov
, 0);
287 CertFreeCertificateContext(priv
->cert
);
292 static int cryptoapi_finish(RSA
*rsa
)
294 cryptoapi_free_data((struct cryptoapi_rsa_data
*) rsa
->meth
->app_data
);
295 os_free((void *) rsa
->meth
);
301 static const CERT_CONTEXT
* cryptoapi_find_cert(const char *name
, DWORD store
)
304 const CERT_CONTEXT
*ret
= NULL
;
306 cs
= CertOpenStore((LPCSTR
) CERT_STORE_PROV_SYSTEM
, 0, 0,
307 store
| CERT_STORE_OPEN_EXISTING_FLAG
|
308 CERT_STORE_READONLY_FLAG
, L
"MY");
310 cryptoapi_error("Failed to open 'My system store'");
314 if (strncmp(name
, "cert://", 7) == 0) {
315 unsigned short wbuf
[255];
316 MultiByteToWideChar(CP_ACP
, 0, name
+ 7, -1, wbuf
, 255);
317 ret
= CertFindCertificateInStore(cs
, X509_ASN_ENCODING
|
319 0, CERT_FIND_SUBJECT_STR
,
321 } else if (strncmp(name
, "hash://", 7) == 0) {
322 CRYPT_HASH_BLOB blob
;
324 const char *hash
= name
+ 7;
327 len
= os_strlen(hash
) / 2;
328 buf
= os_malloc(len
);
329 if (buf
&& hexstr2bin(hash
, buf
, len
) == 0) {
332 ret
= CertFindCertificateInStore(cs
,
341 CertCloseStore(cs
, 0);
347 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
350 RSA
*rsa
= NULL
, *pub_rsa
;
351 struct cryptoapi_rsa_data
*priv
;
352 RSA_METHOD
*rsa_meth
;
355 (strncmp(name
, "cert://", 7) != 0 &&
356 strncmp(name
, "hash://", 7) != 0))
359 priv
= os_zalloc(sizeof(*priv
));
360 rsa_meth
= os_zalloc(sizeof(*rsa_meth
));
361 if (priv
== NULL
|| rsa_meth
== NULL
) {
362 wpa_printf(MSG_WARNING
, "CryptoAPI: Failed to allocate memory "
363 "for CryptoAPI RSA method");
369 priv
->cert
= cryptoapi_find_cert(name
, CERT_SYSTEM_STORE_CURRENT_USER
);
370 if (priv
->cert
== NULL
) {
371 priv
->cert
= cryptoapi_find_cert(
372 name
, CERT_SYSTEM_STORE_LOCAL_MACHINE
);
374 if (priv
->cert
== NULL
) {
375 wpa_printf(MSG_INFO
, "CryptoAPI: Could not find certificate "
380 cert
= d2i_X509(NULL
, (OPENSSL_d2i_TYPE
) &priv
->cert
->pbCertEncoded
,
381 priv
->cert
->cbCertEncoded
);
383 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process X509 DER "
388 if (mingw_load_crypto_func())
391 if (!CryptAcquireCertificatePrivateKey(priv
->cert
,
392 CRYPT_ACQUIRE_COMPARE_KEY_FLAG
,
393 NULL
, &priv
->crypt_prov
,
395 &priv
->free_crypt_prov
)) {
396 cryptoapi_error("Failed to acquire a private key for the "
401 rsa_meth
->name
= "Microsoft CryptoAPI RSA Method";
402 rsa_meth
->rsa_pub_enc
= cryptoapi_rsa_pub_enc
;
403 rsa_meth
->rsa_pub_dec
= cryptoapi_rsa_pub_dec
;
404 rsa_meth
->rsa_priv_enc
= cryptoapi_rsa_priv_enc
;
405 rsa_meth
->rsa_priv_dec
= cryptoapi_rsa_priv_dec
;
406 rsa_meth
->finish
= cryptoapi_finish
;
407 rsa_meth
->flags
= RSA_METHOD_FLAG_NO_CHECK
;
408 rsa_meth
->app_data
= (char *) priv
;
412 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,
413 ERR_R_MALLOC_FAILURE
);
417 if (!SSL_use_certificate(ssl
, cert
)) {
422 pub_rsa
= cert
->cert_info
->key
->pkey
->pkey
.rsa
;
426 rsa
->n
= BN_dup(pub_rsa
->n
);
427 rsa
->e
= BN_dup(pub_rsa
->e
);
428 if (!RSA_set_method(rsa
, rsa_meth
))
431 if (!SSL_use_RSAPrivateKey(ssl
, rsa
))
444 cryptoapi_free_data(priv
);
450 static int tls_cryptoapi_ca_cert(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *name
)
453 PCCERT_CONTEXT ctx
= NULL
;
461 if (mingw_load_crypto_func())
464 if (name
== NULL
|| strncmp(name
, "cert_store://", 13) != 0)
469 wstore
= os_malloc((os_strlen(store
) + 1) * sizeof(WCHAR
));
472 wsprintf(wstore
, L
"%S", store
);
473 cs
= CertOpenSystemStore(0, wstore
);
476 cs
= CertOpenSystemStore(0, store
);
479 wpa_printf(MSG_DEBUG
, "%s: failed to open system cert store "
480 "'%s': error=%d", __func__
, store
,
481 (int) GetLastError());
485 while ((ctx
= CertEnumCertificatesInStore(cs
, ctx
))) {
486 cert
= d2i_X509(NULL
, (OPENSSL_d2i_TYPE
) &ctx
->pbCertEncoded
,
489 wpa_printf(MSG_INFO
, "CryptoAPI: Could not process "
490 "X509 DER encoding for CA cert");
494 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
496 wpa_printf(MSG_DEBUG
, "OpenSSL: Loaded CA certificate for "
497 "system certificate store: subject='%s'", buf
);
499 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
500 tls_show_errors(MSG_WARNING
, __func__
,
501 "Failed to add ca_cert to OpenSSL "
502 "certificate store");
508 if (!CertCloseStore(cs
, 0)) {
509 wpa_printf(MSG_DEBUG
, "%s: failed to close system cert store "
510 "'%s': error=%d", __func__
, name
+ 13,
511 (int) GetLastError());
518 #else /* CONFIG_NATIVE_WINDOWS */
520 static int tls_cryptoapi_cert(SSL
*ssl
, const char *name
)
525 #endif /* CONFIG_NATIVE_WINDOWS */
528 static void ssl_info_cb(const SSL
*ssl
, int where
, int ret
)
533 wpa_printf(MSG_DEBUG
, "SSL: (where=0x%x ret=0x%x)", where
, ret
);
534 w
= where
& ~SSL_ST_MASK
;
535 if (w
& SSL_ST_CONNECT
)
537 else if (w
& SSL_ST_ACCEPT
)
542 if (where
& SSL_CB_LOOP
) {
543 wpa_printf(MSG_DEBUG
, "SSL: %s:%s",
544 str
, SSL_state_string_long(ssl
));
545 } else if (where
& SSL_CB_ALERT
) {
546 wpa_printf(MSG_INFO
, "SSL: SSL3 alert: %s:%s:%s",
547 where
& SSL_CB_READ
?
548 "read (remote end reported an error)" :
549 "write (local SSL3 detected an error)",
550 SSL_alert_type_string_long(ret
),
551 SSL_alert_desc_string_long(ret
));
552 if ((ret
>> 8) == SSL3_AL_FATAL
) {
553 struct tls_connection
*conn
=
554 SSL_get_app_data((SSL
*) ssl
);
555 if (where
& SSL_CB_READ
)
558 conn
->write_alerts
++;
560 } else if (where
& SSL_CB_EXIT
&& ret
<= 0) {
561 wpa_printf(MSG_DEBUG
, "SSL: %s:%s in %s",
562 str
, ret
== 0 ? "failed" : "error",
563 SSL_state_string_long(ssl
));
568 #ifndef OPENSSL_NO_ENGINE
570 * tls_engine_load_dynamic_generic - load any openssl engine
571 * @pre: an array of commands and values that load an engine initialized
572 * in the engine specific function
573 * @post: an array of commands and values that initialize an already loaded
574 * engine (or %NULL if not required)
575 * @id: the engine id of the engine to load (only required if post is not %NULL
577 * This function is a generic function that loads any openssl engine.
579 * Returns: 0 on success, -1 on failure
581 static int tls_engine_load_dynamic_generic(const char *pre
[],
582 const char *post
[], const char *id
)
585 const char *dynamic_id
= "dynamic";
587 engine
= ENGINE_by_id(id
);
590 wpa_printf(MSG_DEBUG
, "ENGINE: engine '%s' is already "
596 engine
= ENGINE_by_id(dynamic_id
);
597 if (engine
== NULL
) {
598 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
600 ERR_error_string(ERR_get_error(), NULL
));
604 /* Perform the pre commands. This will load the engine. */
605 while (pre
&& pre
[0]) {
606 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", pre
[0], pre
[1]);
607 if (ENGINE_ctrl_cmd_string(engine
, pre
[0], pre
[1], 0) == 0) {
608 wpa_printf(MSG_INFO
, "ENGINE: ctrl cmd_string failed: "
609 "%s %s [%s]", pre
[0], pre
[1],
610 ERR_error_string(ERR_get_error(), NULL
));
618 * Free the reference to the "dynamic" engine. The loaded engine can
619 * now be looked up using ENGINE_by_id().
623 engine
= ENGINE_by_id(id
);
624 if (engine
== NULL
) {
625 wpa_printf(MSG_INFO
, "ENGINE: Can't find engine %s [%s]",
626 id
, ERR_error_string(ERR_get_error(), NULL
));
630 while (post
&& post
[0]) {
631 wpa_printf(MSG_DEBUG
, "ENGINE: '%s' '%s'", post
[0], post
[1]);
632 if (ENGINE_ctrl_cmd_string(engine
, post
[0], post
[1], 0) == 0) {
633 wpa_printf(MSG_DEBUG
, "ENGINE: ctrl cmd_string failed:"
634 " %s %s [%s]", post
[0], post
[1],
635 ERR_error_string(ERR_get_error(), NULL
));
636 ENGINE_remove(engine
);
649 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
650 * @pkcs11_so_path: pksc11_so_path from the configuration
651 * @pcks11_module_path: pkcs11_module_path from the configuration
653 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path
,
654 const char *pkcs11_module_path
)
656 char *engine_id
= "pkcs11";
657 const char *pre_cmd
[] = {
658 "SO_PATH", NULL
/* pkcs11_so_path */,
659 "ID", NULL
/* engine_id */,
661 /* "NO_VCHECK", "1", */
665 const char *post_cmd
[] = {
666 "MODULE_PATH", NULL
/* pkcs11_module_path */,
670 if (!pkcs11_so_path
|| !pkcs11_module_path
)
673 pre_cmd
[1] = pkcs11_so_path
;
674 pre_cmd
[3] = engine_id
;
675 post_cmd
[1] = pkcs11_module_path
;
677 wpa_printf(MSG_DEBUG
, "ENGINE: Loading pkcs11 Engine from %s",
680 return tls_engine_load_dynamic_generic(pre_cmd
, post_cmd
, engine_id
);
685 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
686 * @opensc_so_path: opensc_so_path from the configuration
688 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path
)
690 char *engine_id
= "opensc";
691 const char *pre_cmd
[] = {
692 "SO_PATH", NULL
/* opensc_so_path */,
693 "ID", NULL
/* engine_id */,
702 pre_cmd
[1] = opensc_so_path
;
703 pre_cmd
[3] = engine_id
;
705 wpa_printf(MSG_DEBUG
, "ENGINE: Loading OpenSC Engine from %s",
708 return tls_engine_load_dynamic_generic(pre_cmd
, NULL
, engine_id
);
710 #endif /* OPENSSL_NO_ENGINE */
713 void * tls_init(const struct tls_config
*conf
)
717 if (tls_openssl_ref_count
== 0) {
718 SSL_load_error_strings();
720 /* TODO: if /dev/urandom is available, PRNG is seeded
721 * automatically. If this is not the case, random data should
726 #endif /* PKCS12_FUNCS */
728 tls_openssl_ref_count
++;
730 ssl
= SSL_CTX_new(TLSv1_method());
734 SSL_CTX_set_info_callback(ssl
, ssl_info_cb
);
736 #ifndef OPENSSL_NO_ENGINE
738 (conf
->opensc_engine_path
|| conf
->pkcs11_engine_path
||
739 conf
->pkcs11_module_path
)) {
740 wpa_printf(MSG_DEBUG
, "ENGINE: Loading dynamic engine");
741 ERR_load_ENGINE_strings();
742 ENGINE_load_dynamic();
744 if (tls_engine_load_dynamic_opensc(conf
->opensc_engine_path
) ||
745 tls_engine_load_dynamic_pkcs11(conf
->pkcs11_engine_path
,
746 conf
->pkcs11_module_path
)) {
751 #endif /* OPENSSL_NO_ENGINE */
757 void tls_deinit(void *ssl_ctx
)
759 SSL_CTX
*ssl
= ssl_ctx
;
762 tls_openssl_ref_count
--;
763 if (tls_openssl_ref_count
== 0) {
764 #ifndef OPENSSL_NO_ENGINE
766 #endif /* OPENSSL_NO_ENGINE */
773 static int tls_engine_init(struct tls_connection
*conn
, const char *engine_id
,
774 const char *pin
, const char *key_id
)
776 #ifndef OPENSSL_NO_ENGINE
778 if (engine_id
== NULL
) {
779 wpa_printf(MSG_ERROR
, "ENGINE: Engine ID not set");
783 wpa_printf(MSG_ERROR
, "ENGINE: Smartcard PIN not set");
786 if (key_id
== NULL
) {
787 wpa_printf(MSG_ERROR
, "ENGINE: Key Id not set");
792 conn
->engine
= ENGINE_by_id(engine_id
);
794 wpa_printf(MSG_ERROR
, "ENGINE: engine %s not available [%s]",
795 engine_id
, ERR_error_string(ERR_get_error(), NULL
));
798 if (ENGINE_init(conn
->engine
) != 1) {
799 wpa_printf(MSG_ERROR
, "ENGINE: engine init failed "
800 "(engine: %s) [%s]", engine_id
,
801 ERR_error_string(ERR_get_error(), NULL
));
804 wpa_printf(MSG_DEBUG
, "ENGINE: engine initialized");
806 if (ENGINE_ctrl_cmd_string(conn
->engine
, "PIN", pin
, 0) == 0) {
807 wpa_printf(MSG_ERROR
, "ENGINE: cannot set pin [%s]",
808 ERR_error_string(ERR_get_error(), NULL
));
811 conn
->private_key
= ENGINE_load_private_key(conn
->engine
,
813 if (!conn
->private_key
) {
814 wpa_printf(MSG_ERROR
, "ENGINE: cannot load private key with id"
815 " '%s' [%s]", key_id
,
816 ERR_error_string(ERR_get_error(), NULL
));
817 ret
= TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED
;
824 ENGINE_free(conn
->engine
);
828 if (conn
->private_key
) {
829 EVP_PKEY_free(conn
->private_key
);
830 conn
->private_key
= NULL
;
834 #else /* OPENSSL_NO_ENGINE */
836 #endif /* OPENSSL_NO_ENGINE */
840 static void tls_engine_deinit(struct tls_connection
*conn
)
842 #ifndef OPENSSL_NO_ENGINE
843 wpa_printf(MSG_DEBUG
, "ENGINE: engine deinit");
844 if (conn
->private_key
) {
845 EVP_PKEY_free(conn
->private_key
);
846 conn
->private_key
= NULL
;
849 ENGINE_finish(conn
->engine
);
852 #endif /* OPENSSL_NO_ENGINE */
856 int tls_get_errors(void *ssl_ctx
)
861 while ((err
= ERR_get_error())) {
862 wpa_printf(MSG_INFO
, "TLS - SSL error: %s",
863 ERR_error_string(err
, NULL
));
870 struct tls_connection
* tls_connection_init(void *ssl_ctx
)
872 SSL_CTX
*ssl
= ssl_ctx
;
873 struct tls_connection
*conn
;
875 conn
= os_zalloc(sizeof(*conn
));
878 conn
->ssl
= SSL_new(ssl
);
879 if (conn
->ssl
== NULL
) {
880 tls_show_errors(MSG_INFO
, __func__
,
881 "Failed to initialize new SSL connection");
886 SSL_set_app_data(conn
->ssl
, conn
);
887 SSL_set_options(conn
->ssl
,
888 SSL_OP_NO_SSLv2
| SSL_OP_NO_SSLv3
|
889 SSL_OP_SINGLE_DH_USE
);
891 conn
->ssl_in
= BIO_new(BIO_s_mem());
893 tls_show_errors(MSG_INFO
, __func__
,
894 "Failed to create a new BIO for ssl_in");
900 conn
->ssl_out
= BIO_new(BIO_s_mem());
901 if (!conn
->ssl_out
) {
902 tls_show_errors(MSG_INFO
, __func__
,
903 "Failed to create a new BIO for ssl_out");
905 BIO_free(conn
->ssl_in
);
910 SSL_set_bio(conn
->ssl
, conn
->ssl_in
, conn
->ssl_out
);
916 void tls_connection_deinit(void *ssl_ctx
, struct tls_connection
*conn
)
920 os_free(conn
->pre_shared_secret
);
922 tls_engine_deinit(conn
);
923 os_free(conn
->subject_match
);
924 os_free(conn
->altsubject_match
);
929 int tls_connection_established(void *ssl_ctx
, struct tls_connection
*conn
)
931 return conn
? SSL_is_init_finished(conn
->ssl
) : 0;
935 int tls_connection_shutdown(void *ssl_ctx
, struct tls_connection
*conn
)
940 /* Shutdown previous TLS connection without notifying the peer
941 * because the connection was already terminated in practice
942 * and "close notify" shutdown alert would confuse AS. */
943 SSL_set_quiet_shutdown(conn
->ssl
, 1);
944 SSL_shutdown(conn
->ssl
);
949 static int tls_match_altsubject_component(X509
*cert
, int type
,
950 const char *value
, size_t len
)
956 ext
= X509_get_ext_d2i(cert
, NID_subject_alt_name
, NULL
, NULL
);
958 for (i
= 0; ext
&& i
< sk_GENERAL_NAME_num(ext
); i
++) {
959 gen
= sk_GENERAL_NAME_value(ext
, i
);
960 if (gen
->type
!= type
)
962 if (os_strlen((char *) gen
->d
.ia5
->data
) == len
&&
963 os_memcmp(value
, gen
->d
.ia5
->data
, len
) == 0)
971 static int tls_match_altsubject(X509
*cert
, const char *match
)
974 const char *pos
, *end
;
979 if (os_strncmp(pos
, "EMAIL:", 6) == 0) {
982 } else if (os_strncmp(pos
, "DNS:", 4) == 0) {
985 } else if (os_strncmp(pos
, "URI:", 4) == 0) {
989 wpa_printf(MSG_INFO
, "TLS: Invalid altSubjectName "
993 end
= os_strchr(pos
, ';');
995 if (os_strncmp(end
+ 1, "EMAIL:", 6) == 0 ||
996 os_strncmp(end
+ 1, "DNS:", 4) == 0 ||
997 os_strncmp(end
+ 1, "URI:", 4) == 0)
999 end
= os_strchr(end
+ 1, ';');
1004 len
= os_strlen(pos
);
1005 if (tls_match_altsubject_component(cert
, type
, pos
, len
) > 0)
1014 static int tls_verify_cb(int preverify_ok
, X509_STORE_CTX
*x509_ctx
)
1020 struct tls_connection
*conn
;
1021 char *match
, *altmatch
;
1023 err_cert
= X509_STORE_CTX_get_current_cert(x509_ctx
);
1024 err
= X509_STORE_CTX_get_error(x509_ctx
);
1025 depth
= X509_STORE_CTX_get_error_depth(x509_ctx
);
1026 ssl
= X509_STORE_CTX_get_ex_data(x509_ctx
,
1027 SSL_get_ex_data_X509_STORE_CTX_idx());
1028 X509_NAME_oneline(X509_get_subject_name(err_cert
), buf
, sizeof(buf
));
1030 conn
= SSL_get_app_data(ssl
);
1031 match
= conn
? conn
->subject_match
: NULL
;
1032 altmatch
= conn
? conn
->altsubject_match
: NULL
;
1034 if (!preverify_ok
) {
1035 wpa_printf(MSG_WARNING
, "TLS: Certificate verification failed,"
1036 " error %d (%s) depth %d for '%s'", err
,
1037 X509_verify_cert_error_string(err
), depth
, buf
);
1039 wpa_printf(MSG_DEBUG
, "TLS: tls_verify_cb - "
1040 "preverify_ok=%d err=%d (%s) depth=%d buf='%s'",
1042 X509_verify_cert_error_string(err
), depth
, buf
);
1043 if (depth
== 0 && match
&& os_strstr(buf
, match
) == NULL
) {
1044 wpa_printf(MSG_WARNING
, "TLS: Subject '%s' did not "
1045 "match with '%s'", buf
, match
);
1047 } else if (depth
== 0 && altmatch
&&
1048 !tls_match_altsubject(err_cert
, altmatch
)) {
1049 wpa_printf(MSG_WARNING
, "TLS: altSubjectName match "
1050 "'%s' not found", altmatch
);
1055 return preverify_ok
;
1059 #ifndef OPENSSL_NO_STDIO
1060 static int tls_load_ca_der(void *_ssl_ctx
, const char *ca_cert
)
1062 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1063 X509_LOOKUP
*lookup
;
1066 lookup
= X509_STORE_add_lookup(ssl_ctx
->cert_store
,
1067 X509_LOOKUP_file());
1068 if (lookup
== NULL
) {
1069 tls_show_errors(MSG_WARNING
, __func__
,
1070 "Failed add lookup for X509 store");
1074 if (!X509_LOOKUP_load_file(lookup
, ca_cert
, X509_FILETYPE_ASN1
)) {
1075 unsigned long err
= ERR_peek_error();
1076 tls_show_errors(MSG_WARNING
, __func__
,
1077 "Failed load CA in DER format");
1078 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1079 ERR_GET_REASON(err
) == X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1080 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1081 "cert already in hash table error",
1089 #endif /* OPENSSL_NO_STDIO */
1092 static int tls_connection_ca_cert(void *_ssl_ctx
, struct tls_connection
*conn
,
1093 const char *ca_cert
, const u8
*ca_cert_blob
,
1094 size_t ca_cert_blob_len
, const char *ca_path
)
1096 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1099 X509
*cert
= d2i_X509(NULL
, (OPENSSL_d2i_TYPE
) &ca_cert_blob
,
1102 tls_show_errors(MSG_WARNING
, __func__
,
1103 "Failed to parse ca_cert_blob");
1107 if (!X509_STORE_add_cert(ssl_ctx
->cert_store
, cert
)) {
1108 unsigned long err
= ERR_peek_error();
1109 tls_show_errors(MSG_WARNING
, __func__
,
1110 "Failed to add ca_cert_blob to "
1111 "certificate store");
1112 if (ERR_GET_LIB(err
) == ERR_LIB_X509
&&
1113 ERR_GET_REASON(err
) ==
1114 X509_R_CERT_ALREADY_IN_HASH_TABLE
) {
1115 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - ignoring "
1116 "cert already in hash table error",
1124 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - added ca_cert_blob "
1125 "to certificate store", __func__
);
1126 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1130 #ifdef CONFIG_NATIVE_WINDOWS
1131 if (ca_cert
&& tls_cryptoapi_ca_cert(ssl_ctx
, conn
->ssl
, ca_cert
) ==
1133 wpa_printf(MSG_DEBUG
, "OpenSSL: Added CA certificates from "
1134 "system certificate store");
1135 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1138 #endif /* CONFIG_NATIVE_WINDOWS */
1140 if (ca_cert
|| ca_path
) {
1141 #ifndef OPENSSL_NO_STDIO
1142 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, ca_path
) !=
1144 tls_show_errors(MSG_WARNING
, __func__
,
1145 "Failed to load root certificates");
1147 tls_load_ca_der(ssl_ctx
, ca_cert
) == 0) {
1148 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - loaded "
1149 "DER format CA certificate",
1154 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1155 "certificate(s) loaded");
1156 tls_get_errors(ssl_ctx
);
1158 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
, tls_verify_cb
);
1159 #else /* OPENSSL_NO_STDIO */
1160 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
1163 #endif /* OPENSSL_NO_STDIO */
1165 /* No ca_cert configured - do not try to verify server
1167 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
1174 static int tls_global_ca_cert(SSL_CTX
*ssl_ctx
, const char *ca_cert
)
1177 if (SSL_CTX_load_verify_locations(ssl_ctx
, ca_cert
, NULL
) != 1)
1179 tls_show_errors(MSG_WARNING
, __func__
,
1180 "Failed to load root certificates");
1184 wpa_printf(MSG_DEBUG
, "TLS: Trusted root "
1185 "certificate(s) loaded");
1187 #ifndef OPENSSL_NO_STDIO
1188 /* Add the same CAs to the client certificate requests */
1189 SSL_CTX_set_client_CA_list(ssl_ctx
,
1190 SSL_load_client_CA_file(ca_cert
));
1191 #endif /* OPENSSL_NO_STDIO */
1198 int tls_global_set_verify(void *ssl_ctx
, int check_crl
)
1203 X509_STORE
*cs
= SSL_CTX_get_cert_store(ssl_ctx
);
1205 tls_show_errors(MSG_INFO
, __func__
, "Failed to get "
1206 "certificate store when enabling "
1210 flags
= X509_V_FLAG_CRL_CHECK
;
1212 flags
|= X509_V_FLAG_CRL_CHECK_ALL
;
1213 X509_STORE_set_flags(cs
, flags
);
1219 static int tls_connection_set_subject_match(struct tls_connection
*conn
,
1220 const char *subject_match
,
1221 const char *altsubject_match
)
1223 os_free(conn
->subject_match
);
1224 conn
->subject_match
= NULL
;
1225 if (subject_match
) {
1226 conn
->subject_match
= os_strdup(subject_match
);
1227 if (conn
->subject_match
== NULL
)
1231 os_free(conn
->altsubject_match
);
1232 conn
->altsubject_match
= NULL
;
1233 if (altsubject_match
) {
1234 conn
->altsubject_match
= os_strdup(altsubject_match
);
1235 if (conn
->altsubject_match
== NULL
)
1243 int tls_connection_set_verify(void *ssl_ctx
, struct tls_connection
*conn
,
1250 SSL_set_verify(conn
->ssl
, SSL_VERIFY_PEER
|
1251 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
|
1252 SSL_VERIFY_CLIENT_ONCE
, tls_verify_cb
);
1254 SSL_set_verify(conn
->ssl
, SSL_VERIFY_NONE
, NULL
);
1257 SSL_set_accept_state(conn
->ssl
);
1263 static int tls_connection_client_cert(struct tls_connection
*conn
,
1264 const char *client_cert
,
1265 const u8
*client_cert_blob
,
1266 size_t client_cert_blob_len
)
1268 if (client_cert
== NULL
&& client_cert_blob
== NULL
)
1271 if (client_cert_blob
&&
1272 SSL_use_certificate_ASN1(conn
->ssl
, (u8
*) client_cert_blob
,
1273 client_cert_blob_len
) == 1) {
1274 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_ASN1 --> "
1277 } else if (client_cert_blob
) {
1278 tls_show_errors(MSG_DEBUG
, __func__
,
1279 "SSL_use_certificate_ASN1 failed");
1282 if (client_cert
== NULL
)
1285 #ifndef OPENSSL_NO_STDIO
1286 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
1287 SSL_FILETYPE_ASN1
) == 1) {
1288 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (DER)"
1292 tls_show_errors(MSG_DEBUG
, __func__
,
1293 "SSL_use_certificate_file (DER) failed");
1296 if (SSL_use_certificate_file(conn
->ssl
, client_cert
,
1297 SSL_FILETYPE_PEM
) == 1) {
1298 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_certificate_file (PEM)"
1302 tls_show_errors(MSG_DEBUG
, __func__
,
1303 "SSL_use_certificate_file (PEM) failed");
1305 #else /* OPENSSL_NO_STDIO */
1306 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
1307 #endif /* OPENSSL_NO_STDIO */
1313 static int tls_global_client_cert(SSL_CTX
*ssl_ctx
, const char *client_cert
)
1315 #ifndef OPENSSL_NO_STDIO
1316 if (client_cert
== NULL
)
1319 if (SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
1320 SSL_FILETYPE_ASN1
) != 1 &&
1321 SSL_CTX_use_certificate_file(ssl_ctx
, client_cert
,
1322 SSL_FILETYPE_PEM
) != 1) {
1323 tls_show_errors(MSG_INFO
, __func__
,
1324 "Failed to load client certificate");
1328 #else /* OPENSSL_NO_STDIO */
1329 if (client_cert
== NULL
)
1331 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__
);
1333 #endif /* OPENSSL_NO_STDIO */
1337 static int tls_passwd_cb(char *buf
, int size
, int rwflag
, void *password
)
1339 if (password
== NULL
) {
1342 os_strncpy(buf
, (char *) password
, size
);
1343 buf
[size
- 1] = '\0';
1344 return os_strlen(buf
);
1349 static int tls_parse_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, PKCS12
*p12
,
1354 STACK_OF(X509
) *certs
;
1361 if (!PKCS12_parse(p12
, passwd
, &pkey
, &cert
, &certs
)) {
1362 tls_show_errors(MSG_DEBUG
, __func__
,
1363 "Failed to parse PKCS12 file");
1367 wpa_printf(MSG_DEBUG
, "TLS: Successfully parsed PKCS12 data");
1370 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
1372 wpa_printf(MSG_DEBUG
, "TLS: Got certificate from PKCS12: "
1373 "subject='%s'", buf
);
1375 if (SSL_use_certificate(ssl
, cert
) != 1)
1378 if (SSL_CTX_use_certificate(ssl_ctx
, cert
) != 1)
1385 wpa_printf(MSG_DEBUG
, "TLS: Got private key from PKCS12");
1387 if (SSL_use_PrivateKey(ssl
, pkey
) != 1)
1390 if (SSL_CTX_use_PrivateKey(ssl_ctx
, pkey
) != 1)
1393 EVP_PKEY_free(pkey
);
1397 while ((cert
= sk_X509_pop(certs
)) != NULL
) {
1398 X509_NAME_oneline(X509_get_subject_name(cert
), buf
,
1400 wpa_printf(MSG_DEBUG
, "TLS: additional certificate"
1401 " from PKCS12: subject='%s'", buf
);
1403 * There is no SSL equivalent for the chain cert - so
1404 * always add it to the context...
1406 if (SSL_CTX_add_extra_chain_cert(ssl_ctx
, cert
) != 1) {
1411 sk_X509_free(certs
);
1417 tls_get_errors(ssl_ctx
);
1421 #endif /* PKCS12_FUNCS */
1424 static int tls_read_pkcs12(SSL_CTX
*ssl_ctx
, SSL
*ssl
, const char *private_key
,
1431 f
= fopen(private_key
, "rb");
1435 p12
= d2i_PKCS12_fp(f
, NULL
);
1439 tls_show_errors(MSG_INFO
, __func__
,
1440 "Failed to use PKCS#12 file");
1444 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
1446 #else /* PKCS12_FUNCS */
1447 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot read "
1450 #endif /* PKCS12_FUNCS */
1454 static int tls_read_pkcs12_blob(SSL_CTX
*ssl_ctx
, SSL
*ssl
,
1455 const u8
*blob
, size_t len
, const char *passwd
)
1460 p12
= d2i_PKCS12(NULL
, (OPENSSL_d2i_TYPE
) &blob
, len
);
1462 tls_show_errors(MSG_INFO
, __func__
,
1463 "Failed to use PKCS#12 blob");
1467 return tls_parse_pkcs12(ssl_ctx
, ssl
, p12
, passwd
);
1469 #else /* PKCS12_FUNCS */
1470 wpa_printf(MSG_INFO
, "TLS: PKCS12 support disabled - cannot parse "
1473 #endif /* PKCS12_FUNCS */
1477 static int tls_connection_engine_private_key(struct tls_connection
*conn
)
1479 #ifndef OPENSSL_NO_ENGINE
1480 if (SSL_use_PrivateKey(conn
->ssl
, conn
->private_key
) != 1) {
1481 tls_show_errors(MSG_ERROR
, __func__
,
1482 "ENGINE: cannot use private key for TLS");
1485 if (!SSL_check_private_key(conn
->ssl
)) {
1486 tls_show_errors(MSG_INFO
, __func__
,
1487 "Private key failed verification");
1491 #else /* OPENSSL_NO_ENGINE */
1492 wpa_printf(MSG_ERROR
, "SSL: Configuration uses engine, but "
1493 "engine support was not compiled in");
1495 #endif /* OPENSSL_NO_ENGINE */
1499 static int tls_connection_private_key(void *_ssl_ctx
,
1500 struct tls_connection
*conn
,
1501 const char *private_key
,
1502 const char *private_key_passwd
,
1503 const u8
*private_key_blob
,
1504 size_t private_key_blob_len
)
1506 SSL_CTX
*ssl_ctx
= _ssl_ctx
;
1510 if (private_key
== NULL
&& private_key_blob
== NULL
)
1513 if (private_key_passwd
) {
1514 passwd
= os_strdup(private_key_passwd
);
1520 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
1521 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
1524 while (private_key_blob
) {
1525 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA
, conn
->ssl
,
1526 (u8
*) private_key_blob
,
1527 private_key_blob_len
) == 1) {
1528 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
1529 "ASN1(EVP_PKEY_RSA) --> OK");
1533 tls_show_errors(MSG_DEBUG
, __func__
,
1534 "SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA)"
1538 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA
, conn
->ssl
,
1539 (u8
*) private_key_blob
,
1540 private_key_blob_len
) == 1) {
1541 wpa_printf(MSG_DEBUG
, "OpenSSL: SSL_use_PrivateKey_"
1542 "ASN1(EVP_PKEY_DSA) --> OK");
1546 tls_show_errors(MSG_DEBUG
, __func__
,
1547 "SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA)"
1551 if (SSL_use_RSAPrivateKey_ASN1(conn
->ssl
,
1552 (u8
*) private_key_blob
,
1553 private_key_blob_len
) == 1) {
1554 wpa_printf(MSG_DEBUG
, "OpenSSL: "
1555 "SSL_use_RSAPrivateKey_ASN1 --> OK");
1559 tls_show_errors(MSG_DEBUG
, __func__
,
1560 "SSL_use_RSAPrivateKey_ASN1 failed");
1563 if (tls_read_pkcs12_blob(ssl_ctx
, conn
->ssl
, private_key_blob
,
1564 private_key_blob_len
, passwd
) == 0) {
1565 wpa_printf(MSG_DEBUG
, "OpenSSL: PKCS#12 as blob --> "
1574 while (!ok
&& private_key
) {
1575 #ifndef OPENSSL_NO_STDIO
1576 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
1577 SSL_FILETYPE_ASN1
) == 1) {
1578 wpa_printf(MSG_DEBUG
, "OpenSSL: "
1579 "SSL_use_PrivateKey_File (DER) --> OK");
1583 tls_show_errors(MSG_DEBUG
, __func__
,
1584 "SSL_use_PrivateKey_File (DER) "
1588 if (SSL_use_PrivateKey_file(conn
->ssl
, private_key
,
1589 SSL_FILETYPE_PEM
) == 1) {
1590 wpa_printf(MSG_DEBUG
, "OpenSSL: "
1591 "SSL_use_PrivateKey_File (PEM) --> OK");
1595 tls_show_errors(MSG_DEBUG
, __func__
,
1596 "SSL_use_PrivateKey_File (PEM) "
1599 #else /* OPENSSL_NO_STDIO */
1600 wpa_printf(MSG_DEBUG
, "OpenSSL: %s - OPENSSL_NO_STDIO",
1602 #endif /* OPENSSL_NO_STDIO */
1604 if (tls_read_pkcs12(ssl_ctx
, conn
->ssl
, private_key
, passwd
)
1606 wpa_printf(MSG_DEBUG
, "OpenSSL: Reading PKCS#12 file "
1612 if (tls_cryptoapi_cert(conn
->ssl
, private_key
) == 0) {
1613 wpa_printf(MSG_DEBUG
, "OpenSSL: Using CryptoAPI to "
1614 "access certificate store --> OK");
1623 wpa_printf(MSG_INFO
, "OpenSSL: Failed to load private key");
1629 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
1632 if (!SSL_check_private_key(conn
->ssl
)) {
1633 tls_show_errors(MSG_INFO
, __func__
, "Private key failed "
1638 wpa_printf(MSG_DEBUG
, "SSL: Private key loaded successfully");
1643 static int tls_global_private_key(SSL_CTX
*ssl_ctx
, const char *private_key
,
1644 const char *private_key_passwd
)
1648 if (private_key
== NULL
)
1651 if (private_key_passwd
) {
1652 passwd
= os_strdup(private_key_passwd
);
1658 SSL_CTX_set_default_passwd_cb(ssl_ctx
, tls_passwd_cb
);
1659 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx
, passwd
);
1661 #ifndef OPENSSL_NO_STDIO
1662 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
1663 SSL_FILETYPE_ASN1
) != 1 &&
1664 SSL_CTX_use_PrivateKey_file(ssl_ctx
, private_key
,
1665 SSL_FILETYPE_PEM
) != 1 &&
1666 #endif /* OPENSSL_NO_STDIO */
1667 tls_read_pkcs12(ssl_ctx
, NULL
, private_key
, passwd
)) {
1668 tls_show_errors(MSG_INFO
, __func__
,
1669 "Failed to load private key");
1676 SSL_CTX_set_default_passwd_cb(ssl_ctx
, NULL
);
1678 if (!SSL_CTX_check_private_key(ssl_ctx
)) {
1679 tls_show_errors(MSG_INFO
, __func__
,
1680 "Private key failed verification");
1688 static int tls_connection_dh(struct tls_connection
*conn
, const char *dh_file
)
1690 #ifdef OPENSSL_NO_DH
1691 if (dh_file
== NULL
)
1693 wpa_printf(MSG_ERROR
, "TLS: openssl does not include DH support, but "
1694 "dh_file specified");
1696 #else /* OPENSSL_NO_DH */
1700 /* TODO: add support for dh_blob */
1701 if (dh_file
== NULL
)
1706 bio
= BIO_new_file(dh_file
, "r");
1708 wpa_printf(MSG_INFO
, "TLS: Failed to open DH file '%s': %s",
1709 dh_file
, ERR_error_string(ERR_get_error(), NULL
));
1712 dh
= PEM_read_bio_DHparams(bio
, NULL
, NULL
, NULL
);
1714 #ifndef OPENSSL_NO_DSA
1715 while (dh
== NULL
) {
1717 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DH file '%s': %s -"
1718 " trying to parse as DSA params", dh_file
,
1719 ERR_error_string(ERR_get_error(), NULL
));
1720 bio
= BIO_new_file(dh_file
, "r");
1723 dsa
= PEM_read_bio_DSAparams(bio
, NULL
, NULL
, NULL
);
1726 wpa_printf(MSG_DEBUG
, "TLS: Failed to parse DSA file "
1727 "'%s': %s", dh_file
,
1728 ERR_error_string(ERR_get_error(), NULL
));
1732 wpa_printf(MSG_DEBUG
, "TLS: DH file in DSA param format");
1733 dh
= DSA_dup_DH(dsa
);
1736 wpa_printf(MSG_INFO
, "TLS: Failed to convert DSA "
1737 "params into DH params");
1742 #endif /* !OPENSSL_NO_DSA */
1744 wpa_printf(MSG_INFO
, "TLS: Failed to read/parse DH/DSA file "
1749 if (SSL_set_tmp_dh(conn
->ssl
, dh
) != 1) {
1750 wpa_printf(MSG_INFO
, "TLS: Failed to set DH params from '%s': "
1752 ERR_error_string(ERR_get_error(), NULL
));
1758 #endif /* OPENSSL_NO_DH */
1762 int tls_connection_get_keys(void *ssl_ctx
, struct tls_connection
*conn
,
1763 struct tls_keys
*keys
)
1767 if (conn
== NULL
|| keys
== NULL
)
1770 if (ssl
== NULL
|| ssl
->s3
== NULL
|| ssl
->session
== NULL
)
1773 os_memset(keys
, 0, sizeof(*keys
));
1774 keys
->master_key
= ssl
->session
->master_key
;
1775 keys
->master_key_len
= ssl
->session
->master_key_length
;
1776 keys
->client_random
= ssl
->s3
->client_random
;
1777 keys
->client_random_len
= SSL3_RANDOM_SIZE
;
1778 keys
->server_random
= ssl
->s3
->server_random
;
1779 keys
->server_random_len
= SSL3_RANDOM_SIZE
;
1785 int tls_connection_prf(void *tls_ctx
, struct tls_connection
*conn
,
1786 const char *label
, int server_random_first
,
1787 u8
*out
, size_t out_len
)
1793 u8
* tls_connection_handshake(void *ssl_ctx
, struct tls_connection
*conn
,
1794 const u8
*in_data
, size_t in_len
,
1795 size_t *out_len
, u8
**appl_data
,
1796 size_t *appl_data_len
)
1805 * Give TLS handshake data from the server (if available) to OpenSSL
1809 BIO_write(conn
->ssl_in
, in_data
, in_len
) < 0) {
1810 tls_show_errors(MSG_INFO
, __func__
,
1811 "Handshake failed - BIO_write");
1815 /* Initiate TLS handshake or continue the existing handshake */
1816 res
= SSL_connect(conn
->ssl
);
1818 int err
= SSL_get_error(conn
->ssl
, res
);
1819 if (err
== SSL_ERROR_WANT_READ
)
1820 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want "
1822 else if (err
== SSL_ERROR_WANT_WRITE
)
1823 wpa_printf(MSG_DEBUG
, "SSL: SSL_connect - want to "
1826 tls_show_errors(MSG_INFO
, __func__
, "SSL_connect");
1831 /* Get the TLS handshake data to be sent to the server */
1832 res
= BIO_ctrl_pending(conn
->ssl_out
);
1833 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
1834 out_data
= os_malloc(res
== 0 ? 1 : res
);
1835 if (out_data
== NULL
) {
1836 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
1837 "handshake output (%d bytes)", res
);
1838 if (BIO_reset(conn
->ssl_out
) < 0) {
1839 tls_show_errors(MSG_INFO
, __func__
,
1840 "BIO_reset failed");
1845 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, out_data
, res
);
1847 tls_show_errors(MSG_INFO
, __func__
,
1848 "Handshake failed - BIO_read");
1849 if (BIO_reset(conn
->ssl_out
) < 0) {
1850 tls_show_errors(MSG_INFO
, __func__
,
1851 "BIO_reset failed");
1858 if (SSL_is_init_finished(conn
->ssl
) && appl_data
) {
1859 *appl_data
= os_malloc(in_len
);
1861 res
= SSL_read(conn
->ssl
, *appl_data
, in_len
);
1863 tls_show_errors(MSG_INFO
, __func__
,
1864 "Failed to read possible "
1865 "Application Data");
1866 os_free(*appl_data
);
1869 *appl_data_len
= res
;
1870 wpa_hexdump_key(MSG_MSGDUMP
, "SSL: Application"
1871 " Data in Finish message",
1872 *appl_data
, *appl_data_len
);
1881 u8
* tls_connection_server_handshake(void *ssl_ctx
,
1882 struct tls_connection
*conn
,
1883 const u8
*in_data
, size_t in_len
,
1891 BIO_write(conn
->ssl_in
, in_data
, in_len
) < 0) {
1892 tls_show_errors(MSG_INFO
, __func__
,
1893 "Handshake failed - BIO_write");
1897 res
= SSL_read(conn
->ssl
, buf
, sizeof(buf
));
1899 wpa_printf(MSG_DEBUG
, "SSL: Unexpected data from SSL_read "
1903 res
= BIO_ctrl_pending(conn
->ssl_out
);
1904 wpa_printf(MSG_DEBUG
, "SSL: %d bytes pending from ssl_out", res
);
1905 out_data
= os_malloc(res
== 0 ? 1 : res
);
1906 if (out_data
== NULL
) {
1907 wpa_printf(MSG_DEBUG
, "SSL: Failed to allocate memory for "
1908 "handshake output (%d bytes)", res
);
1909 if (BIO_reset(conn
->ssl_out
) < 0) {
1910 tls_show_errors(MSG_INFO
, __func__
,
1911 "BIO_reset failed");
1916 res
= res
== 0 ? 0 : BIO_read(conn
->ssl_out
, out_data
, res
);
1918 tls_show_errors(MSG_INFO
, __func__
,
1919 "Handshake failed - BIO_read");
1920 if (BIO_reset(conn
->ssl_out
) < 0) {
1921 tls_show_errors(MSG_INFO
, __func__
,
1922 "BIO_reset failed");
1932 int tls_connection_encrypt(void *ssl_ctx
, struct tls_connection
*conn
,
1933 const u8
*in_data
, size_t in_len
,
1934 u8
*out_data
, size_t out_len
)
1941 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
1942 if ((res
= BIO_reset(conn
->ssl_in
)) < 0 ||
1943 (res
= BIO_reset(conn
->ssl_out
)) < 0) {
1944 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
1947 res
= SSL_write(conn
->ssl
, in_data
, in_len
);
1949 tls_show_errors(MSG_INFO
, __func__
,
1950 "Encryption failed - SSL_write");
1954 /* Read encrypted data to be sent to the server */
1955 res
= BIO_read(conn
->ssl_out
, out_data
, out_len
);
1957 tls_show_errors(MSG_INFO
, __func__
,
1958 "Encryption failed - BIO_read");
1966 int tls_connection_decrypt(void *ssl_ctx
, struct tls_connection
*conn
,
1967 const u8
*in_data
, size_t in_len
,
1968 u8
*out_data
, size_t out_len
)
1972 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
1973 res
= BIO_write(conn
->ssl_in
, in_data
, in_len
);
1975 tls_show_errors(MSG_INFO
, __func__
,
1976 "Decryption failed - BIO_write");
1979 if (BIO_reset(conn
->ssl_out
) < 0) {
1980 tls_show_errors(MSG_INFO
, __func__
, "BIO_reset failed");
1984 /* Read decrypted data for further processing */
1985 res
= SSL_read(conn
->ssl
, out_data
, out_len
);
1987 tls_show_errors(MSG_INFO
, __func__
,
1988 "Decryption failed - SSL_read");
1996 int tls_connection_resumed(void *ssl_ctx
, struct tls_connection
*conn
)
1998 return conn
? conn
->ssl
->hit
: 0;
2002 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2003 /* Pre-shared secred requires a patch to openssl, so this function is
2004 * commented out unless explicitly needed for EAP-FAST in order to be able to
2005 * build this file with unmodified openssl. */
2007 static int tls_sess_sec_cb(SSL
*s
, void *secret
, int *secret_len
,
2008 STACK_OF(SSL_CIPHER
) *peer_ciphers
,
2009 SSL_CIPHER
**cipher
, void *arg
)
2011 struct tls_connection
*conn
= arg
;
2013 if (conn
== NULL
|| conn
->pre_shared_secret
== 0)
2016 os_memcpy(secret
, conn
->pre_shared_secret
,
2017 conn
->pre_shared_secret_len
);
2018 *secret_len
= conn
->pre_shared_secret_len
;
2024 int tls_connection_set_master_key(void *ssl_ctx
, struct tls_connection
*conn
,
2025 const u8
*key
, size_t key_len
)
2027 if (conn
== NULL
|| key_len
> SSL_MAX_MASTER_KEY_LENGTH
)
2030 os_free(conn
->pre_shared_secret
);
2031 conn
->pre_shared_secret
= NULL
;
2032 conn
->pre_shared_secret_len
= 0;
2035 conn
->pre_shared_secret
= os_malloc(key_len
);
2036 if (conn
->pre_shared_secret
) {
2037 os_memcpy(conn
->pre_shared_secret
, key
, key_len
);
2038 conn
->pre_shared_secret_len
= key_len
;
2040 if (SSL_set_session_secret_cb(conn
->ssl
, tls_sess_sec_cb
,
2044 if (SSL_set_session_secret_cb(conn
->ssl
, NULL
, NULL
) != 1)
2050 #endif /* EAP_FAST || EAP_FAST_DYNAMIC */
2053 int tls_connection_set_cipher_list(void *tls_ctx
, struct tls_connection
*conn
,
2056 char buf
[100], *pos
, *end
;
2060 if (conn
== NULL
|| conn
->ssl
== NULL
|| ciphers
== NULL
)
2065 end
= pos
+ sizeof(buf
);
2068 while (*c
!= TLS_CIPHER_NONE
) {
2072 case TLS_CIPHER_RC4_SHA
:
2075 case TLS_CIPHER_AES128_SHA
:
2076 suite
= "AES128-SHA";
2078 case TLS_CIPHER_RSA_DHE_AES128_SHA
:
2079 suite
= "DHE-RSA-AES128-SHA";
2081 case TLS_CIPHER_ANON_DH_AES128_SHA
:
2082 suite
= "ADH-AES128-SHA";
2085 wpa_printf(MSG_DEBUG
, "TLS: Unsupported "
2086 "cipher selection: %d", *c
);
2089 ret
= os_snprintf(pos
, end
- pos
, ":%s", suite
);
2090 if (ret
< 0 || ret
>= end
- pos
)
2097 wpa_printf(MSG_DEBUG
, "OpenSSL: cipher suites: %s", buf
+ 1);
2099 if (SSL_set_cipher_list(conn
->ssl
, buf
+ 1) != 1) {
2100 tls_show_errors(MSG_INFO
, __func__
,
2101 "Cipher suite configuration failed");
2109 int tls_get_cipher(void *ssl_ctx
, struct tls_connection
*conn
,
2110 char *buf
, size_t buflen
)
2113 if (conn
== NULL
|| conn
->ssl
== NULL
)
2116 name
= SSL_get_cipher(conn
->ssl
);
2120 os_snprintf(buf
, buflen
, "%s", name
);
2121 buf
[buflen
- 1] = '\0';
2126 int tls_connection_enable_workaround(void *ssl_ctx
,
2127 struct tls_connection
*conn
)
2129 SSL_set_options(conn
->ssl
, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
);
2135 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2136 /* ClientHello TLS extensions require a patch to openssl, so this function is
2137 * commented out unless explicitly needed for EAP-FAST in order to be able to
2138 * build this file with unmodified openssl. */
2139 int tls_connection_client_hello_ext(void *ssl_ctx
, struct tls_connection
*conn
,
2140 int ext_type
, const u8
*data
,
2143 if (conn
== NULL
|| conn
->ssl
== NULL
)
2146 if (SSL_set_hello_extension(conn
->ssl
, ext_type
, (void *) data
,
2152 #endif /* EAP_FAST || EAP_FAST_DYNAMIC */
2155 int tls_connection_get_failed(void *ssl_ctx
, struct tls_connection
*conn
)
2159 return conn
->failed
;
2163 int tls_connection_get_read_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
2167 return conn
->read_alerts
;
2171 int tls_connection_get_write_alerts(void *ssl_ctx
, struct tls_connection
*conn
)
2175 return conn
->write_alerts
;
2179 int tls_connection_set_params(void *tls_ctx
, struct tls_connection
*conn
,
2180 const struct tls_connection_params
*params
)
2188 while ((err
= ERR_get_error())) {
2189 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
2190 __func__
, ERR_error_string(err
, NULL
));
2193 if (tls_connection_set_subject_match(conn
,
2194 params
->subject_match
,
2195 params
->altsubject_match
))
2197 if (tls_connection_ca_cert(tls_ctx
, conn
, params
->ca_cert
,
2198 params
->ca_cert_blob
,
2199 params
->ca_cert_blob_len
,
2202 if (tls_connection_client_cert(conn
, params
->client_cert
,
2203 params
->client_cert_blob
,
2204 params
->client_cert_blob_len
))
2207 if (params
->engine
) {
2208 wpa_printf(MSG_DEBUG
, "SSL: Initializing TLS engine");
2209 ret
= tls_engine_init(conn
, params
->engine_id
, params
->pin
,
2213 if (tls_connection_engine_private_key(conn
))
2214 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED
;
2215 } else if (tls_connection_private_key(tls_ctx
, conn
,
2216 params
->private_key
,
2217 params
->private_key_passwd
,
2218 params
->private_key_blob
,
2219 params
->private_key_blob_len
)) {
2220 wpa_printf(MSG_INFO
, "TLS: Failed to load private key '%s'",
2221 params
->private_key
);
2225 if (tls_connection_dh(conn
, params
->dh_file
)) {
2226 wpa_printf(MSG_INFO
, "TLS: Failed to load DH file '%s'",
2231 tls_get_errors(tls_ctx
);
2237 int tls_global_set_params(void *tls_ctx
,
2238 const struct tls_connection_params
*params
)
2240 SSL_CTX
*ssl_ctx
= tls_ctx
;
2243 while ((err
= ERR_get_error())) {
2244 wpa_printf(MSG_INFO
, "%s: Clearing pending SSL error: %s",
2245 __func__
, ERR_error_string(err
, NULL
));
2248 if (tls_global_ca_cert(ssl_ctx
, params
->ca_cert
))
2251 if (tls_global_client_cert(ssl_ctx
, params
->client_cert
))
2254 if (tls_global_private_key(ssl_ctx
, params
->private_key
,
2255 params
->private_key_passwd
))
2262 int tls_connection_get_keyblock_size(void *tls_ctx
,
2263 struct tls_connection
*conn
)
2265 const EVP_CIPHER
*c
;
2268 if (conn
== NULL
|| conn
->ssl
== NULL
||
2269 conn
->ssl
->enc_read_ctx
== NULL
||
2270 conn
->ssl
->enc_read_ctx
->cipher
== NULL
||
2271 conn
->ssl
->read_hash
== NULL
)
2274 c
= conn
->ssl
->enc_read_ctx
->cipher
;
2275 h
= conn
->ssl
->read_hash
;
2277 return 2 * (EVP_CIPHER_key_length(c
) +
2279 EVP_CIPHER_iv_length(c
));
2283 unsigned int tls_capabilities(void *tls_ctx
)
2289 int tls_connection_set_ia(void *tls_ctx
, struct tls_connection
*conn
,
2296 int tls_connection_ia_send_phase_finished(void *tls_ctx
,
2297 struct tls_connection
*conn
,
2299 u8
*out_data
, size_t out_len
)
2305 int tls_connection_ia_final_phase_finished(void *tls_ctx
,
2306 struct tls_connection
*conn
)
2312 int tls_connection_ia_permute_inner_secret(void *tls_ctx
,
2313 struct tls_connection
*conn
,
2314 const u8
*key
, size_t key_len
)