2 * WPA Supplicant / SSL/TLS interface functions for openssl
3 * Copyright (c) 2004-2009, 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 #ifdef SSL_F_SSL_SET_SESSION_TICKET_EXT
41 #ifdef SSL_OP_NO_TICKET
43 * Session ticket override patch was merged into OpenSSL 0.9.9 tree on
44 * 2008-11-15. This version uses a bit different API compared to the old patch.
46 #define CONFIG_OPENSSL_TICKET_OVERRIDE
50 static int tls_openssl_ref_count = 0;
52 struct tls_connection {
54 BIO *ssl_in, *ssl_out;
55 #ifndef OPENSSL_NO_ENGINE
56 ENGINE *engine; /* functional reference to the engine */
57 EVP_PKEY *private_key; /* the private key if using engine */
58 #endif /* OPENSSL_NO_ENGINE */
59 char *subject_match, *altsubject_match;
60 int read_alerts, write_alerts, failed;
62 tls_session_ticket_cb session_ticket_cb;
63 void *session_ticket_cb_ctx;
65 /* SessionTicket received from OpenSSL hello_extension_cb (server) */
67 size_t session_ticket_len;
71 #ifdef CONFIG_NO_STDOUT_DEBUG
73 static void _tls_show_errors(void)
77 while ((err = ERR_get_error())) {
78 /* Just ignore the errors, since stdout is disabled */
81 #define tls_show_errors(l, f, t) _tls_show_errors()
83 #else /* CONFIG_NO_STDOUT_DEBUG */
85 static void tls_show_errors(int level, const char *func, const char *txt)
89 wpa_printf(level, "OpenSSL: %s - %s %s",
90 func, txt, ERR_error_string(ERR_get_error(), NULL));
92 while ((err = ERR_get_error())) {
93 wpa_printf(MSG_INFO, "OpenSSL: pending error: %s",
94 ERR_error_string(err, NULL));
98 #endif /* CONFIG_NO_STDOUT_DEBUG */
101 #ifdef CONFIG_NATIVE_WINDOWS
103 /* Windows CryptoAPI and access to certificate stores */
104 #include <wincrypt.h>
106 #ifdef __MINGW32_VERSION
108 * MinGW does not yet include all the needed definitions for CryptoAPI, so
109 * define here whatever extra is needed.
111 #define CALG_SSL3_SHAMD5 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SSL3SHAMD5)
112 #define CERT_SYSTEM_STORE_CURRENT_USER (1 << 16)
113 #define CERT_STORE_READONLY_FLAG 0x00008000
114 #define CERT_STORE_OPEN_EXISTING_FLAG 0x00004000
115 #define CRYPT_ACQUIRE_COMPARE_KEY_FLAG 0x00000004
118 (*CryptAcquireCertificatePrivateKey)(PCCERT_CONTEXT pCert, DWORD dwFlags,
119 void *pvReserved, HCRYPTPROV *phCryptProv,
120 DWORD *pdwKeySpec, BOOL *pfCallerFreeProv)
121 = NULL; /* to be loaded from crypt32.dll */
123 #ifdef CONFIG_MINGW32_LOAD_CERTENUM
124 static PCCERT_CONTEXT WINAPI
125 (*CertEnumCertificatesInStore)(HCERTSTORE hCertStore,
126 PCCERT_CONTEXT pPrevCertContext)
127 = NULL; /* to be loaded from crypt32.dll */
128 #endif /* CONFIG_MINGW32_LOAD_CERTENUM */
130 static int mingw_load_crypto_func(void)
134 /* MinGW does not yet have full CryptoAPI support, so load the needed
137 if (CryptAcquireCertificatePrivateKey)
140 dll = LoadLibrary("crypt32");
142 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not load crypt32 "
147 CryptAcquireCertificatePrivateKey = GetProcAddress(
148 dll, "CryptAcquireCertificatePrivateKey");
149 if (CryptAcquireCertificatePrivateKey == NULL) {
150 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
151 "CryptAcquireCertificatePrivateKey() address from "
156 #ifdef CONFIG_MINGW32_LOAD_CERTENUM
157 CertEnumCertificatesInStore = (void *) GetProcAddress(
158 dll, "CertEnumCertificatesInStore");
159 if (CertEnumCertificatesInStore == NULL) {
160 wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
161 "CertEnumCertificatesInStore() address from "
165 #endif /* CONFIG_MINGW32_LOAD_CERTENUM */
170 #else /* __MINGW32_VERSION */
172 static int mingw_load_crypto_func(void)
177 #endif /* __MINGW32_VERSION */
180 struct cryptoapi_rsa_data {
181 const CERT_CONTEXT *cert;
182 HCRYPTPROV crypt_prov;
184 BOOL free_crypt_prov;
188 static void cryptoapi_error(const char *msg)
190 wpa_printf(MSG_INFO, "CryptoAPI: %s; err=%u",
191 msg, (unsigned int) GetLastError());
195 static int cryptoapi_rsa_pub_enc(int flen, const unsigned char *from,
196 unsigned char *to, RSA *rsa, int padding)
198 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
203 static int cryptoapi_rsa_pub_dec(int flen, const unsigned char *from,
204 unsigned char *to, RSA *rsa, int padding)
206 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
211 static int cryptoapi_rsa_priv_enc(int flen, const unsigned char *from,
212 unsigned char *to, RSA *rsa, int padding)
214 struct cryptoapi_rsa_data *priv =
215 (struct cryptoapi_rsa_data *) rsa->meth->app_data;
217 DWORD hash_size, len, i;
218 unsigned char *buf = NULL;
222 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
223 ERR_R_PASSED_NULL_PARAMETER);
227 if (padding != RSA_PKCS1_PADDING) {
228 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
229 RSA_R_UNKNOWN_PADDING_TYPE);
233 if (flen != 16 /* MD5 */ + 20 /* SHA-1 */) {
234 wpa_printf(MSG_INFO, "%s - only MD5-SHA1 hash supported",
236 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
237 RSA_R_INVALID_MESSAGE_LENGTH);
241 if (!CryptCreateHash(priv->crypt_prov, CALG_SSL3_SHAMD5, 0, 0, &hash))
243 cryptoapi_error("CryptCreateHash failed");
247 len = sizeof(hash_size);
248 if (!CryptGetHashParam(hash, HP_HASHSIZE, (BYTE *) &hash_size, &len,
250 cryptoapi_error("CryptGetHashParam failed");
254 if ((int) hash_size != flen) {
255 wpa_printf(MSG_INFO, "CryptoAPI: Invalid hash size (%u != %d)",
256 (unsigned) hash_size, flen);
257 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
258 RSA_R_INVALID_MESSAGE_LENGTH);
261 if (!CryptSetHashParam(hash, HP_HASHVAL, (BYTE * ) from, 0)) {
262 cryptoapi_error("CryptSetHashParam failed");
267 buf = os_malloc(len);
269 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
273 if (!CryptSignHash(hash, priv->key_spec, NULL, 0, buf, &len)) {
274 cryptoapi_error("CryptSignHash failed");
278 for (i = 0; i < len; i++)
279 to[i] = buf[len - i - 1];
284 CryptDestroyHash(hash);
290 static int cryptoapi_rsa_priv_dec(int flen, const unsigned char *from,
291 unsigned char *to, RSA *rsa, int padding)
293 wpa_printf(MSG_DEBUG, "%s - not implemented", __func__);
298 static void cryptoapi_free_data(struct cryptoapi_rsa_data *priv)
302 if (priv->crypt_prov && priv->free_crypt_prov)
303 CryptReleaseContext(priv->crypt_prov, 0);
305 CertFreeCertificateContext(priv->cert);
310 static int cryptoapi_finish(RSA *rsa)
312 cryptoapi_free_data((struct cryptoapi_rsa_data *) rsa->meth->app_data);
313 os_free((void *) rsa->meth);
319 static const CERT_CONTEXT * cryptoapi_find_cert(const char *name, DWORD store)
322 const CERT_CONTEXT *ret = NULL;
324 cs = CertOpenStore((LPCSTR) CERT_STORE_PROV_SYSTEM, 0, 0,
325 store | CERT_STORE_OPEN_EXISTING_FLAG |
326 CERT_STORE_READONLY_FLAG, L"MY");
328 cryptoapi_error("Failed to open 'My system store'");
332 if (strncmp(name, "cert://", 7) == 0) {
333 unsigned short wbuf[255];
334 MultiByteToWideChar(CP_ACP, 0, name + 7, -1, wbuf, 255);
335 ret = CertFindCertificateInStore(cs, X509_ASN_ENCODING |
337 0, CERT_FIND_SUBJECT_STR,
339 } else if (strncmp(name, "hash://", 7) == 0) {
340 CRYPT_HASH_BLOB blob;
342 const char *hash = name + 7;
345 len = os_strlen(hash) / 2;
346 buf = os_malloc(len);
347 if (buf && hexstr2bin(hash, buf, len) == 0) {
350 ret = CertFindCertificateInStore(cs,
359 CertCloseStore(cs, 0);
365 static int tls_cryptoapi_cert(SSL *ssl, const char *name)
368 RSA *rsa = NULL, *pub_rsa;
369 struct cryptoapi_rsa_data *priv;
370 RSA_METHOD *rsa_meth;
373 (strncmp(name, "cert://", 7) != 0 &&
374 strncmp(name, "hash://", 7) != 0))
377 priv = os_zalloc(sizeof(*priv));
378 rsa_meth = os_zalloc(sizeof(*rsa_meth));
379 if (priv == NULL || rsa_meth == NULL) {
380 wpa_printf(MSG_WARNING, "CryptoAPI: Failed to allocate memory "
381 "for CryptoAPI RSA method");
387 priv->cert = cryptoapi_find_cert(name, CERT_SYSTEM_STORE_CURRENT_USER);
388 if (priv->cert == NULL) {
389 priv->cert = cryptoapi_find_cert(
390 name, CERT_SYSTEM_STORE_LOCAL_MACHINE);
392 if (priv->cert == NULL) {
393 wpa_printf(MSG_INFO, "CryptoAPI: Could not find certificate "
398 cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &priv->cert->pbCertEncoded,
399 priv->cert->cbCertEncoded);
401 wpa_printf(MSG_INFO, "CryptoAPI: Could not process X509 DER "
406 if (mingw_load_crypto_func())
409 if (!CryptAcquireCertificatePrivateKey(priv->cert,
410 CRYPT_ACQUIRE_COMPARE_KEY_FLAG,
411 NULL, &priv->crypt_prov,
413 &priv->free_crypt_prov)) {
414 cryptoapi_error("Failed to acquire a private key for the "
419 rsa_meth->name = "Microsoft CryptoAPI RSA Method";
420 rsa_meth->rsa_pub_enc = cryptoapi_rsa_pub_enc;
421 rsa_meth->rsa_pub_dec = cryptoapi_rsa_pub_dec;
422 rsa_meth->rsa_priv_enc = cryptoapi_rsa_priv_enc;
423 rsa_meth->rsa_priv_dec = cryptoapi_rsa_priv_dec;
424 rsa_meth->finish = cryptoapi_finish;
425 rsa_meth->flags = RSA_METHOD_FLAG_NO_CHECK;
426 rsa_meth->app_data = (char *) priv;
430 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE,
431 ERR_R_MALLOC_FAILURE);
435 if (!SSL_use_certificate(ssl, cert)) {
440 pub_rsa = cert->cert_info->key->pkey->pkey.rsa;
444 rsa->n = BN_dup(pub_rsa->n);
445 rsa->e = BN_dup(pub_rsa->e);
446 if (!RSA_set_method(rsa, rsa_meth))
449 if (!SSL_use_RSAPrivateKey(ssl, rsa))
462 cryptoapi_free_data(priv);
468 static int tls_cryptoapi_ca_cert(SSL_CTX *ssl_ctx, SSL *ssl, const char *name)
471 PCCERT_CONTEXT ctx = NULL;
479 if (mingw_load_crypto_func())
482 if (name == NULL || strncmp(name, "cert_store://", 13) != 0)
487 wstore = os_malloc((os_strlen(store) + 1) * sizeof(WCHAR));
490 wsprintf(wstore, L"%S", store);
491 cs = CertOpenSystemStore(0, wstore);
494 cs = CertOpenSystemStore(0, store);
497 wpa_printf(MSG_DEBUG, "%s: failed to open system cert store "
498 "'%s': error=%d", __func__, store,
499 (int) GetLastError());
503 while ((ctx = CertEnumCertificatesInStore(cs, ctx))) {
504 cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ctx->pbCertEncoded,
507 wpa_printf(MSG_INFO, "CryptoAPI: Could not process "
508 "X509 DER encoding for CA cert");
512 X509_NAME_oneline(X509_get_subject_name(cert), buf,
514 wpa_printf(MSG_DEBUG, "OpenSSL: Loaded CA certificate for "
515 "system certificate store: subject='%s'", buf);
517 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
518 tls_show_errors(MSG_WARNING, __func__,
519 "Failed to add ca_cert to OpenSSL "
520 "certificate store");
526 if (!CertCloseStore(cs, 0)) {
527 wpa_printf(MSG_DEBUG, "%s: failed to close system cert store "
528 "'%s': error=%d", __func__, name + 13,
529 (int) GetLastError());
536 #else /* CONFIG_NATIVE_WINDOWS */
538 static int tls_cryptoapi_cert(SSL *ssl, const char *name)
543 #endif /* CONFIG_NATIVE_WINDOWS */
546 static void ssl_info_cb(const SSL *ssl, int where, int ret)
551 wpa_printf(MSG_DEBUG, "SSL: (where=0x%x ret=0x%x)", where, ret);
552 w = where & ~SSL_ST_MASK;
553 if (w & SSL_ST_CONNECT)
555 else if (w & SSL_ST_ACCEPT)
560 if (where & SSL_CB_LOOP) {
561 wpa_printf(MSG_DEBUG, "SSL: %s:%s",
562 str, SSL_state_string_long(ssl));
563 } else if (where & SSL_CB_ALERT) {
564 wpa_printf(MSG_INFO, "SSL: SSL3 alert: %s:%s:%s",
565 where & SSL_CB_READ ?
566 "read (remote end reported an error)" :
567 "write (local SSL3 detected an error)",
568 SSL_alert_type_string_long(ret),
569 SSL_alert_desc_string_long(ret));
570 if ((ret >> 8) == SSL3_AL_FATAL) {
571 struct tls_connection *conn =
572 SSL_get_app_data((SSL *) ssl);
573 if (where & SSL_CB_READ)
576 conn->write_alerts++;
578 } else if (where & SSL_CB_EXIT && ret <= 0) {
579 wpa_printf(MSG_DEBUG, "SSL: %s:%s in %s",
580 str, ret == 0 ? "failed" : "error",
581 SSL_state_string_long(ssl));
586 #ifndef OPENSSL_NO_ENGINE
588 * tls_engine_load_dynamic_generic - load any openssl engine
589 * @pre: an array of commands and values that load an engine initialized
590 * in the engine specific function
591 * @post: an array of commands and values that initialize an already loaded
592 * engine (or %NULL if not required)
593 * @id: the engine id of the engine to load (only required if post is not %NULL
595 * This function is a generic function that loads any openssl engine.
597 * Returns: 0 on success, -1 on failure
599 static int tls_engine_load_dynamic_generic(const char *pre[],
600 const char *post[], const char *id)
603 const char *dynamic_id = "dynamic";
605 engine = ENGINE_by_id(id);
608 wpa_printf(MSG_DEBUG, "ENGINE: engine '%s' is already "
614 engine = ENGINE_by_id(dynamic_id);
615 if (engine == NULL) {
616 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
618 ERR_error_string(ERR_get_error(), NULL));
622 /* Perform the pre commands. This will load the engine. */
623 while (pre && pre[0]) {
624 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", pre[0], pre[1]);
625 if (ENGINE_ctrl_cmd_string(engine, pre[0], pre[1], 0) == 0) {
626 wpa_printf(MSG_INFO, "ENGINE: ctrl cmd_string failed: "
627 "%s %s [%s]", pre[0], pre[1],
628 ERR_error_string(ERR_get_error(), NULL));
636 * Free the reference to the "dynamic" engine. The loaded engine can
637 * now be looked up using ENGINE_by_id().
641 engine = ENGINE_by_id(id);
642 if (engine == NULL) {
643 wpa_printf(MSG_INFO, "ENGINE: Can't find engine %s [%s]",
644 id, ERR_error_string(ERR_get_error(), NULL));
648 while (post && post[0]) {
649 wpa_printf(MSG_DEBUG, "ENGINE: '%s' '%s'", post[0], post[1]);
650 if (ENGINE_ctrl_cmd_string(engine, post[0], post[1], 0) == 0) {
651 wpa_printf(MSG_DEBUG, "ENGINE: ctrl cmd_string failed:"
652 " %s %s [%s]", post[0], post[1],
653 ERR_error_string(ERR_get_error(), NULL));
654 ENGINE_remove(engine);
667 * tls_engine_load_dynamic_pkcs11 - load the pkcs11 engine provided by opensc
668 * @pkcs11_so_path: pksc11_so_path from the configuration
669 * @pcks11_module_path: pkcs11_module_path from the configuration
671 static int tls_engine_load_dynamic_pkcs11(const char *pkcs11_so_path,
672 const char *pkcs11_module_path)
674 char *engine_id = "pkcs11";
675 const char *pre_cmd[] = {
676 "SO_PATH", NULL /* pkcs11_so_path */,
677 "ID", NULL /* engine_id */,
679 /* "NO_VCHECK", "1", */
683 const char *post_cmd[] = {
684 "MODULE_PATH", NULL /* pkcs11_module_path */,
688 if (!pkcs11_so_path || !pkcs11_module_path)
691 pre_cmd[1] = pkcs11_so_path;
692 pre_cmd[3] = engine_id;
693 post_cmd[1] = pkcs11_module_path;
695 wpa_printf(MSG_DEBUG, "ENGINE: Loading pkcs11 Engine from %s",
698 return tls_engine_load_dynamic_generic(pre_cmd, post_cmd, engine_id);
703 * tls_engine_load_dynamic_opensc - load the opensc engine provided by opensc
704 * @opensc_so_path: opensc_so_path from the configuration
706 static int tls_engine_load_dynamic_opensc(const char *opensc_so_path)
708 char *engine_id = "opensc";
709 const char *pre_cmd[] = {
710 "SO_PATH", NULL /* opensc_so_path */,
711 "ID", NULL /* engine_id */,
720 pre_cmd[1] = opensc_so_path;
721 pre_cmd[3] = engine_id;
723 wpa_printf(MSG_DEBUG, "ENGINE: Loading OpenSC Engine from %s",
726 return tls_engine_load_dynamic_generic(pre_cmd, NULL, engine_id);
728 #endif /* OPENSSL_NO_ENGINE */
731 void * tls_init(const struct tls_config *conf)
735 if (tls_openssl_ref_count == 0) {
738 if (conf->fips_mode) {
739 if (!FIPS_mode_set(1)) {
740 wpa_printf(MSG_ERROR, "Failed to enable FIPS "
742 ERR_load_crypto_strings();
743 ERR_print_errors_fp(stderr);
746 wpa_printf(MSG_INFO, "Running in FIPS mode");
748 #else /* OPENSSL_FIPS */
749 if (conf->fips_mode) {
750 wpa_printf(MSG_ERROR, "FIPS mode requested, but not "
754 #endif /* OPENSSL_FIPS */
755 #endif /* CONFIG_FIPS */
756 SSL_load_error_strings();
758 #ifndef OPENSSL_NO_SHA256
759 EVP_add_digest(EVP_sha256());
760 #endif /* OPENSSL_NO_SHA256 */
761 /* TODO: if /dev/urandom is available, PRNG is seeded
762 * automatically. If this is not the case, random data should
767 #endif /* PKCS12_FUNCS */
769 tls_openssl_ref_count++;
771 ssl = SSL_CTX_new(TLSv1_method());
775 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
777 #ifndef OPENSSL_NO_ENGINE
779 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
780 conf->pkcs11_module_path)) {
781 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
782 ERR_load_ENGINE_strings();
783 ENGINE_load_dynamic();
785 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
786 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
787 conf->pkcs11_module_path)) {
792 #endif /* OPENSSL_NO_ENGINE */
798 void tls_deinit(void *ssl_ctx)
800 SSL_CTX *ssl = ssl_ctx;
803 tls_openssl_ref_count--;
804 if (tls_openssl_ref_count == 0) {
805 #ifndef OPENSSL_NO_ENGINE
807 #endif /* OPENSSL_NO_ENGINE */
808 CRYPTO_cleanup_all_ex_data();
816 static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
817 const char *pin, const char *key_id,
818 const char *cert_id, const char *ca_cert_id)
820 #ifndef OPENSSL_NO_ENGINE
822 if (engine_id == NULL) {
823 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
827 wpa_printf(MSG_ERROR, "ENGINE: Smartcard PIN not set");
830 if (key_id == NULL) {
831 wpa_printf(MSG_ERROR, "ENGINE: Key Id not set");
836 conn->engine = ENGINE_by_id(engine_id);
838 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
839 engine_id, ERR_error_string(ERR_get_error(), NULL));
842 if (ENGINE_init(conn->engine) != 1) {
843 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
844 "(engine: %s) [%s]", engine_id,
845 ERR_error_string(ERR_get_error(), NULL));
848 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
850 if (ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
851 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
852 ERR_error_string(ERR_get_error(), NULL));
855 /* load private key first in-case PIN is required for cert */
856 conn->private_key = ENGINE_load_private_key(conn->engine,
858 if (!conn->private_key) {
859 wpa_printf(MSG_ERROR, "ENGINE: cannot load private key with id"
860 " '%s' [%s]", key_id,
861 ERR_error_string(ERR_get_error(), NULL));
862 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
866 /* handle a certificate and/or CA certificate */
867 if (cert_id || ca_cert_id) {
868 const char *cmd_name = "LOAD_CERT_CTRL";
870 /* test if the engine supports a LOAD_CERT_CTRL */
871 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
872 0, (void *)cmd_name, NULL)) {
873 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
874 " loading certificates");
875 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
884 ENGINE_free(conn->engine);
888 if (conn->private_key) {
889 EVP_PKEY_free(conn->private_key);
890 conn->private_key = NULL;
894 #else /* OPENSSL_NO_ENGINE */
896 #endif /* OPENSSL_NO_ENGINE */
900 static void tls_engine_deinit(struct tls_connection *conn)
902 #ifndef OPENSSL_NO_ENGINE
903 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
904 if (conn->private_key) {
905 EVP_PKEY_free(conn->private_key);
906 conn->private_key = NULL;
909 ENGINE_finish(conn->engine);
912 #endif /* OPENSSL_NO_ENGINE */
916 int tls_get_errors(void *ssl_ctx)
921 while ((err = ERR_get_error())) {
922 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
923 ERR_error_string(err, NULL));
930 struct tls_connection * tls_connection_init(void *ssl_ctx)
932 SSL_CTX *ssl = ssl_ctx;
933 struct tls_connection *conn;
936 conn = os_zalloc(sizeof(*conn));
939 conn->ssl = SSL_new(ssl);
940 if (conn->ssl == NULL) {
941 tls_show_errors(MSG_INFO, __func__,
942 "Failed to initialize new SSL connection");
947 SSL_set_app_data(conn->ssl, conn);
948 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
949 SSL_OP_SINGLE_DH_USE;
950 #ifdef SSL_OP_NO_COMPRESSION
951 options |= SSL_OP_NO_COMPRESSION;
952 #endif /* SSL_OP_NO_COMPRESSION */
953 SSL_set_options(conn->ssl, options);
955 conn->ssl_in = BIO_new(BIO_s_mem());
957 tls_show_errors(MSG_INFO, __func__,
958 "Failed to create a new BIO for ssl_in");
964 conn->ssl_out = BIO_new(BIO_s_mem());
965 if (!conn->ssl_out) {
966 tls_show_errors(MSG_INFO, __func__,
967 "Failed to create a new BIO for ssl_out");
969 BIO_free(conn->ssl_in);
974 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
980 void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
985 tls_engine_deinit(conn);
986 os_free(conn->subject_match);
987 os_free(conn->altsubject_match);
988 os_free(conn->session_ticket);
993 int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
995 return conn ? SSL_is_init_finished(conn->ssl) : 0;
999 int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
1004 /* Shutdown previous TLS connection without notifying the peer
1005 * because the connection was already terminated in practice
1006 * and "close notify" shutdown alert would confuse AS. */
1007 SSL_set_quiet_shutdown(conn->ssl, 1);
1008 SSL_shutdown(conn->ssl);
1013 static int tls_match_altsubject_component(X509 *cert, int type,
1014 const char *value, size_t len)
1020 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1022 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1023 gen = sk_GENERAL_NAME_value(ext, i);
1024 if (gen->type != type)
1026 if (os_strlen((char *) gen->d.ia5->data) == len &&
1027 os_memcmp(value, gen->d.ia5->data, len) == 0)
1035 static int tls_match_altsubject(X509 *cert, const char *match)
1038 const char *pos, *end;
1043 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1046 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1049 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1053 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1057 end = os_strchr(pos, ';');
1059 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1060 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1061 os_strncmp(end + 1, "URI:", 4) == 0)
1063 end = os_strchr(end + 1, ';');
1068 len = os_strlen(pos);
1069 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1078 static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1084 struct tls_connection *conn;
1085 char *match, *altmatch;
1087 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1088 err = X509_STORE_CTX_get_error(x509_ctx);
1089 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1090 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1091 SSL_get_ex_data_X509_STORE_CTX_idx());
1092 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1094 conn = SSL_get_app_data(ssl);
1095 match = conn ? conn->subject_match : NULL;
1096 altmatch = conn ? conn->altsubject_match : NULL;
1098 if (!preverify_ok) {
1099 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1100 " error %d (%s) depth %d for '%s'", err,
1101 X509_verify_cert_error_string(err), depth, buf);
1103 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - "
1104 "preverify_ok=%d err=%d (%s) depth=%d buf='%s'",
1106 X509_verify_cert_error_string(err), depth, buf);
1107 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1108 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1109 "match with '%s'", buf, match);
1111 } else if (depth == 0 && altmatch &&
1112 !tls_match_altsubject(err_cert, altmatch)) {
1113 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1114 "'%s' not found", altmatch);
1119 return preverify_ok;
1123 #ifndef OPENSSL_NO_STDIO
1124 static int tls_load_ca_der(void *_ssl_ctx, const char *ca_cert)
1126 SSL_CTX *ssl_ctx = _ssl_ctx;
1127 X509_LOOKUP *lookup;
1130 lookup = X509_STORE_add_lookup(ssl_ctx->cert_store,
1131 X509_LOOKUP_file());
1132 if (lookup == NULL) {
1133 tls_show_errors(MSG_WARNING, __func__,
1134 "Failed add lookup for X509 store");
1138 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1139 unsigned long err = ERR_peek_error();
1140 tls_show_errors(MSG_WARNING, __func__,
1141 "Failed load CA in DER format");
1142 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1143 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1144 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1145 "cert already in hash table error",
1153 #endif /* OPENSSL_NO_STDIO */
1156 static int tls_connection_ca_cert(void *_ssl_ctx, struct tls_connection *conn,
1157 const char *ca_cert, const u8 *ca_cert_blob,
1158 size_t ca_cert_blob_len, const char *ca_path)
1160 SSL_CTX *ssl_ctx = _ssl_ctx;
1163 * Remove previously configured trusted CA certificates before adding
1166 X509_STORE_free(ssl_ctx->cert_store);
1167 ssl_ctx->cert_store = X509_STORE_new();
1168 if (ssl_ctx->cert_store == NULL) {
1169 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1170 "certificate store", __func__);
1175 X509 *cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ca_cert_blob,
1178 tls_show_errors(MSG_WARNING, __func__,
1179 "Failed to parse ca_cert_blob");
1183 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1184 unsigned long err = ERR_peek_error();
1185 tls_show_errors(MSG_WARNING, __func__,
1186 "Failed to add ca_cert_blob to "
1187 "certificate store");
1188 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1189 ERR_GET_REASON(err) ==
1190 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1191 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1192 "cert already in hash table error",
1200 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
1201 "to certificate store", __func__);
1202 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1206 #ifdef CONFIG_NATIVE_WINDOWS
1207 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
1209 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
1210 "system certificate store");
1211 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1214 #endif /* CONFIG_NATIVE_WINDOWS */
1216 if (ca_cert || ca_path) {
1217 #ifndef OPENSSL_NO_STDIO
1218 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
1220 tls_show_errors(MSG_WARNING, __func__,
1221 "Failed to load root certificates");
1223 tls_load_ca_der(ssl_ctx, ca_cert) == 0) {
1224 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
1225 "DER format CA certificate",
1230 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1231 "certificate(s) loaded");
1232 tls_get_errors(ssl_ctx);
1234 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1235 #else /* OPENSSL_NO_STDIO */
1236 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1239 #endif /* OPENSSL_NO_STDIO */
1241 /* No ca_cert configured - do not try to verify server
1243 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1250 static int tls_global_ca_cert(SSL_CTX *ssl_ctx, const char *ca_cert)
1253 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
1255 tls_show_errors(MSG_WARNING, __func__,
1256 "Failed to load root certificates");
1260 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1261 "certificate(s) loaded");
1263 #ifndef OPENSSL_NO_STDIO
1264 /* Add the same CAs to the client certificate requests */
1265 SSL_CTX_set_client_CA_list(ssl_ctx,
1266 SSL_load_client_CA_file(ca_cert));
1267 #endif /* OPENSSL_NO_STDIO */
1274 int tls_global_set_verify(void *ssl_ctx, int check_crl)
1279 X509_STORE *cs = SSL_CTX_get_cert_store(ssl_ctx);
1281 tls_show_errors(MSG_INFO, __func__, "Failed to get "
1282 "certificate store when enabling "
1286 flags = X509_V_FLAG_CRL_CHECK;
1288 flags |= X509_V_FLAG_CRL_CHECK_ALL;
1289 X509_STORE_set_flags(cs, flags);
1295 static int tls_connection_set_subject_match(struct tls_connection *conn,
1296 const char *subject_match,
1297 const char *altsubject_match)
1299 os_free(conn->subject_match);
1300 conn->subject_match = NULL;
1301 if (subject_match) {
1302 conn->subject_match = os_strdup(subject_match);
1303 if (conn->subject_match == NULL)
1307 os_free(conn->altsubject_match);
1308 conn->altsubject_match = NULL;
1309 if (altsubject_match) {
1310 conn->altsubject_match = os_strdup(altsubject_match);
1311 if (conn->altsubject_match == NULL)
1319 int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
1322 static int counter = 0;
1328 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
1329 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1330 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
1332 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1335 SSL_set_accept_state(conn->ssl);
1338 * Set session id context in order to avoid fatal errors when client
1339 * tries to resume a session. However, set the context to a unique
1340 * value in order to effectively disable session resumption for now
1341 * since not all areas of the server code are ready for it (e.g.,
1342 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1346 SSL_set_session_id_context(conn->ssl,
1347 (const unsigned char *) &counter,
1354 static int tls_connection_client_cert(struct tls_connection *conn,
1355 const char *client_cert,
1356 const u8 *client_cert_blob,
1357 size_t client_cert_blob_len)
1359 if (client_cert == NULL && client_cert_blob == NULL)
1362 if (client_cert_blob &&
1363 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
1364 client_cert_blob_len) == 1) {
1365 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
1368 } else if (client_cert_blob) {
1369 tls_show_errors(MSG_DEBUG, __func__,
1370 "SSL_use_certificate_ASN1 failed");
1373 if (client_cert == NULL)
1376 #ifndef OPENSSL_NO_STDIO
1377 if (SSL_use_certificate_file(conn->ssl, client_cert,
1378 SSL_FILETYPE_ASN1) == 1) {
1379 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
1383 tls_show_errors(MSG_DEBUG, __func__,
1384 "SSL_use_certificate_file (DER) failed");
1387 if (SSL_use_certificate_file(conn->ssl, client_cert,
1388 SSL_FILETYPE_PEM) == 1) {
1389 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
1393 tls_show_errors(MSG_DEBUG, __func__,
1394 "SSL_use_certificate_file (PEM) failed");
1396 #else /* OPENSSL_NO_STDIO */
1397 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1398 #endif /* OPENSSL_NO_STDIO */
1404 static int tls_global_client_cert(SSL_CTX *ssl_ctx, const char *client_cert)
1406 #ifndef OPENSSL_NO_STDIO
1407 if (client_cert == NULL)
1410 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1411 SSL_FILETYPE_ASN1) != 1 &&
1412 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1413 SSL_FILETYPE_PEM) != 1) {
1414 tls_show_errors(MSG_INFO, __func__,
1415 "Failed to load client certificate");
1419 #else /* OPENSSL_NO_STDIO */
1420 if (client_cert == NULL)
1422 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1424 #endif /* OPENSSL_NO_STDIO */
1428 static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
1430 if (password == NULL) {
1433 os_strlcpy(buf, (char *) password, size);
1434 return os_strlen(buf);
1439 static int tls_parse_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, PKCS12 *p12,
1444 STACK_OF(X509) *certs;
1451 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
1452 tls_show_errors(MSG_DEBUG, __func__,
1453 "Failed to parse PKCS12 file");
1457 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
1460 X509_NAME_oneline(X509_get_subject_name(cert), buf,
1462 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
1463 "subject='%s'", buf);
1465 if (SSL_use_certificate(ssl, cert) != 1)
1468 if (SSL_CTX_use_certificate(ssl_ctx, cert) != 1)
1475 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
1477 if (SSL_use_PrivateKey(ssl, pkey) != 1)
1480 if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1)
1483 EVP_PKEY_free(pkey);
1487 while ((cert = sk_X509_pop(certs)) != NULL) {
1488 X509_NAME_oneline(X509_get_subject_name(cert), buf,
1490 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
1491 " from PKCS12: subject='%s'", buf);
1493 * There is no SSL equivalent for the chain cert - so
1494 * always add it to the context...
1496 if (SSL_CTX_add_extra_chain_cert(ssl_ctx, cert) != 1) {
1501 sk_X509_free(certs);
1507 tls_get_errors(ssl_ctx);
1511 #endif /* PKCS12_FUNCS */
1514 static int tls_read_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, const char *private_key,
1521 f = fopen(private_key, "rb");
1525 p12 = d2i_PKCS12_fp(f, NULL);
1529 tls_show_errors(MSG_INFO, __func__,
1530 "Failed to use PKCS#12 file");
1534 return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1536 #else /* PKCS12_FUNCS */
1537 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
1540 #endif /* PKCS12_FUNCS */
1544 static int tls_read_pkcs12_blob(SSL_CTX *ssl_ctx, SSL *ssl,
1545 const u8 *blob, size_t len, const char *passwd)
1550 p12 = d2i_PKCS12(NULL, (OPENSSL_d2i_TYPE) &blob, len);
1552 tls_show_errors(MSG_INFO, __func__,
1553 "Failed to use PKCS#12 blob");
1557 return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1559 #else /* PKCS12_FUNCS */
1560 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
1563 #endif /* PKCS12_FUNCS */
1567 #ifndef OPENSSL_NO_ENGINE
1568 static int tls_engine_get_cert(struct tls_connection *conn,
1569 const char *cert_id,
1572 /* this runs after the private key is loaded so no PIN is required */
1574 const char *cert_id;
1577 params.cert_id = cert_id;
1580 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
1581 0, ¶ms, NULL, 1)) {
1582 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
1583 " '%s' [%s]", cert_id,
1584 ERR_error_string(ERR_get_error(), NULL));
1585 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1588 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
1590 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1592 *cert = params.cert;
1595 #endif /* OPENSSL_NO_ENGINE */
1598 static int tls_connection_engine_client_cert(struct tls_connection *conn,
1599 const char *cert_id)
1601 #ifndef OPENSSL_NO_ENGINE
1604 if (tls_engine_get_cert(conn, cert_id, &cert))
1607 if (!SSL_use_certificate(conn->ssl, cert)) {
1608 tls_show_errors(MSG_ERROR, __func__,
1609 "SSL_use_certificate failed");
1614 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
1618 #else /* OPENSSL_NO_ENGINE */
1620 #endif /* OPENSSL_NO_ENGINE */
1624 static int tls_connection_engine_ca_cert(void *_ssl_ctx,
1625 struct tls_connection *conn,
1626 const char *ca_cert_id)
1628 #ifndef OPENSSL_NO_ENGINE
1630 SSL_CTX *ssl_ctx = _ssl_ctx;
1632 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
1635 /* start off the same as tls_connection_ca_cert */
1636 X509_STORE_free(ssl_ctx->cert_store);
1637 ssl_ctx->cert_store = X509_STORE_new();
1638 if (ssl_ctx->cert_store == NULL) {
1639 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1640 "certificate store", __func__);
1644 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1645 unsigned long err = ERR_peek_error();
1646 tls_show_errors(MSG_WARNING, __func__,
1647 "Failed to add CA certificate from engine "
1648 "to certificate store");
1649 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1650 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1651 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
1652 " already in hash table error",
1660 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
1661 "to certificate store", __func__);
1662 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1665 #else /* OPENSSL_NO_ENGINE */
1667 #endif /* OPENSSL_NO_ENGINE */
1671 static int tls_connection_engine_private_key(struct tls_connection *conn)
1673 #ifndef OPENSSL_NO_ENGINE
1674 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
1675 tls_show_errors(MSG_ERROR, __func__,
1676 "ENGINE: cannot use private key for TLS");
1679 if (!SSL_check_private_key(conn->ssl)) {
1680 tls_show_errors(MSG_INFO, __func__,
1681 "Private key failed verification");
1685 #else /* OPENSSL_NO_ENGINE */
1686 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
1687 "engine support was not compiled in");
1689 #endif /* OPENSSL_NO_ENGINE */
1693 static int tls_connection_private_key(void *_ssl_ctx,
1694 struct tls_connection *conn,
1695 const char *private_key,
1696 const char *private_key_passwd,
1697 const u8 *private_key_blob,
1698 size_t private_key_blob_len)
1700 SSL_CTX *ssl_ctx = _ssl_ctx;
1704 if (private_key == NULL && private_key_blob == NULL)
1707 if (private_key_passwd) {
1708 passwd = os_strdup(private_key_passwd);
1714 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1715 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1718 while (private_key_blob) {
1719 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
1720 (u8 *) private_key_blob,
1721 private_key_blob_len) == 1) {
1722 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1723 "ASN1(EVP_PKEY_RSA) --> OK");
1727 tls_show_errors(MSG_DEBUG, __func__,
1728 "SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA)"
1732 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
1733 (u8 *) private_key_blob,
1734 private_key_blob_len) == 1) {
1735 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1736 "ASN1(EVP_PKEY_DSA) --> OK");
1740 tls_show_errors(MSG_DEBUG, __func__,
1741 "SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA)"
1745 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
1746 (u8 *) private_key_blob,
1747 private_key_blob_len) == 1) {
1748 wpa_printf(MSG_DEBUG, "OpenSSL: "
1749 "SSL_use_RSAPrivateKey_ASN1 --> OK");
1753 tls_show_errors(MSG_DEBUG, __func__,
1754 "SSL_use_RSAPrivateKey_ASN1 failed");
1757 if (tls_read_pkcs12_blob(ssl_ctx, conn->ssl, private_key_blob,
1758 private_key_blob_len, passwd) == 0) {
1759 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
1768 while (!ok && private_key) {
1769 #ifndef OPENSSL_NO_STDIO
1770 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1771 SSL_FILETYPE_ASN1) == 1) {
1772 wpa_printf(MSG_DEBUG, "OpenSSL: "
1773 "SSL_use_PrivateKey_File (DER) --> OK");
1777 tls_show_errors(MSG_DEBUG, __func__,
1778 "SSL_use_PrivateKey_File (DER) "
1782 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1783 SSL_FILETYPE_PEM) == 1) {
1784 wpa_printf(MSG_DEBUG, "OpenSSL: "
1785 "SSL_use_PrivateKey_File (PEM) --> OK");
1789 tls_show_errors(MSG_DEBUG, __func__,
1790 "SSL_use_PrivateKey_File (PEM) "
1793 #else /* OPENSSL_NO_STDIO */
1794 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1796 #endif /* OPENSSL_NO_STDIO */
1798 if (tls_read_pkcs12(ssl_ctx, conn->ssl, private_key, passwd)
1800 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
1806 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
1807 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
1808 "access certificate store --> OK");
1817 wpa_printf(MSG_INFO, "OpenSSL: Failed to load private key");
1823 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1826 if (!SSL_check_private_key(conn->ssl)) {
1827 tls_show_errors(MSG_INFO, __func__, "Private key failed "
1832 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
1837 static int tls_global_private_key(SSL_CTX *ssl_ctx, const char *private_key,
1838 const char *private_key_passwd)
1842 if (private_key == NULL)
1845 if (private_key_passwd) {
1846 passwd = os_strdup(private_key_passwd);
1852 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1853 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1855 #ifndef OPENSSL_NO_STDIO
1856 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1857 SSL_FILETYPE_ASN1) != 1 &&
1858 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1859 SSL_FILETYPE_PEM) != 1 &&
1860 #endif /* OPENSSL_NO_STDIO */
1861 tls_read_pkcs12(ssl_ctx, NULL, private_key, passwd)) {
1862 tls_show_errors(MSG_INFO, __func__,
1863 "Failed to load private key");
1870 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1872 if (!SSL_CTX_check_private_key(ssl_ctx)) {
1873 tls_show_errors(MSG_INFO, __func__,
1874 "Private key failed verification");
1882 static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
1884 #ifdef OPENSSL_NO_DH
1885 if (dh_file == NULL)
1887 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1888 "dh_file specified");
1890 #else /* OPENSSL_NO_DH */
1894 /* TODO: add support for dh_blob */
1895 if (dh_file == NULL)
1900 bio = BIO_new_file(dh_file, "r");
1902 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1903 dh_file, ERR_error_string(ERR_get_error(), NULL));
1906 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1908 #ifndef OPENSSL_NO_DSA
1909 while (dh == NULL) {
1911 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1912 " trying to parse as DSA params", dh_file,
1913 ERR_error_string(ERR_get_error(), NULL));
1914 bio = BIO_new_file(dh_file, "r");
1917 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1920 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1921 "'%s': %s", dh_file,
1922 ERR_error_string(ERR_get_error(), NULL));
1926 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
1927 dh = DSA_dup_DH(dsa);
1930 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
1931 "params into DH params");
1936 #endif /* !OPENSSL_NO_DSA */
1938 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
1943 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
1944 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
1946 ERR_error_string(ERR_get_error(), NULL));
1952 #endif /* OPENSSL_NO_DH */
1956 static int tls_global_dh(SSL_CTX *ssl_ctx, const char *dh_file)
1958 #ifdef OPENSSL_NO_DH
1959 if (dh_file == NULL)
1961 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1962 "dh_file specified");
1964 #else /* OPENSSL_NO_DH */
1968 /* TODO: add support for dh_blob */
1969 if (dh_file == NULL)
1971 if (ssl_ctx == NULL)
1974 bio = BIO_new_file(dh_file, "r");
1976 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1977 dh_file, ERR_error_string(ERR_get_error(), NULL));
1980 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1982 #ifndef OPENSSL_NO_DSA
1983 while (dh == NULL) {
1985 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1986 " trying to parse as DSA params", dh_file,
1987 ERR_error_string(ERR_get_error(), NULL));
1988 bio = BIO_new_file(dh_file, "r");
1991 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1994 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1995 "'%s': %s", dh_file,
1996 ERR_error_string(ERR_get_error(), NULL));
2000 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
2001 dh = DSA_dup_DH(dsa);
2004 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
2005 "params into DH params");
2010 #endif /* !OPENSSL_NO_DSA */
2012 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
2017 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
2018 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
2020 ERR_error_string(ERR_get_error(), NULL));
2026 #endif /* OPENSSL_NO_DH */
2030 int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
2031 struct tls_keys *keys)
2035 if (conn == NULL || keys == NULL)
2038 if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL)
2041 os_memset(keys, 0, sizeof(*keys));
2042 keys->master_key = ssl->session->master_key;
2043 keys->master_key_len = ssl->session->master_key_length;
2044 keys->client_random = ssl->s3->client_random;
2045 keys->client_random_len = SSL3_RANDOM_SIZE;
2046 keys->server_random = ssl->s3->server_random;
2047 keys->server_random_len = SSL3_RANDOM_SIZE;
2053 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
2054 const char *label, int server_random_first,
2055 u8 *out, size_t out_len)
2061 u8 * tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
2062 const u8 *in_data, size_t in_len,
2063 size_t *out_len, u8 **appl_data,
2064 size_t *appl_data_len)
2073 * Give TLS handshake data from the server (if available) to OpenSSL
2077 BIO_write(conn->ssl_in, in_data, in_len) < 0) {
2078 tls_show_errors(MSG_INFO, __func__,
2079 "Handshake failed - BIO_write");
2083 /* Initiate TLS handshake or continue the existing handshake */
2084 res = SSL_connect(conn->ssl);
2086 int err = SSL_get_error(conn->ssl, res);
2087 if (err == SSL_ERROR_WANT_READ)
2088 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
2090 else if (err == SSL_ERROR_WANT_WRITE)
2091 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
2094 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
2099 /* Get the TLS handshake data to be sent to the server */
2100 res = BIO_ctrl_pending(conn->ssl_out);
2101 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
2102 out_data = os_malloc(res == 0 ? 1 : res);
2103 if (out_data == NULL) {
2104 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
2105 "handshake output (%d bytes)", res);
2106 if (BIO_reset(conn->ssl_out) < 0) {
2107 tls_show_errors(MSG_INFO, __func__,
2108 "BIO_reset failed");
2113 res = res == 0 ? 0 : BIO_read(conn->ssl_out, out_data, res);
2115 tls_show_errors(MSG_INFO, __func__,
2116 "Handshake failed - BIO_read");
2117 if (BIO_reset(conn->ssl_out) < 0) {
2118 tls_show_errors(MSG_INFO, __func__,
2119 "BIO_reset failed");
2126 if (SSL_is_init_finished(conn->ssl) && appl_data) {
2127 *appl_data = os_malloc(in_len);
2129 res = SSL_read(conn->ssl, *appl_data, in_len);
2131 tls_show_errors(MSG_INFO, __func__,
2132 "Failed to read possible "
2133 "Application Data");
2134 os_free(*appl_data);
2137 *appl_data_len = res;
2138 wpa_hexdump_key(MSG_MSGDUMP, "SSL: Application"
2139 " Data in Finish message",
2140 *appl_data, *appl_data_len);
2149 u8 * tls_connection_server_handshake(void *ssl_ctx,
2150 struct tls_connection *conn,
2151 const u8 *in_data, size_t in_len,
2158 * Give TLS handshake data from the client (if available) to OpenSSL
2162 BIO_write(conn->ssl_in, in_data, in_len) < 0) {
2163 tls_show_errors(MSG_INFO, __func__,
2164 "Handshake failed - BIO_write");
2168 /* Initiate TLS handshake or continue the existing handshake */
2169 res = SSL_accept(conn->ssl);
2171 int err = SSL_get_error(conn->ssl, res);
2172 if (err == SSL_ERROR_WANT_READ)
2173 wpa_printf(MSG_DEBUG, "SSL: SSL_accept - want "
2175 else if (err == SSL_ERROR_WANT_WRITE)
2176 wpa_printf(MSG_DEBUG, "SSL: SSL_accept - want to "
2179 tls_show_errors(MSG_INFO, __func__, "SSL_accept");
2184 /* Get the TLS handshake data to be sent to the client */
2185 res = BIO_ctrl_pending(conn->ssl_out);
2186 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
2187 out_data = os_malloc(res == 0 ? 1 : res);
2188 if (out_data == NULL) {
2189 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
2190 "handshake output (%d bytes)", res);
2191 if (BIO_reset(conn->ssl_out) < 0) {
2192 tls_show_errors(MSG_INFO, __func__,
2193 "BIO_reset failed");
2198 res = res == 0 ? 0 : BIO_read(conn->ssl_out, out_data, res);
2200 tls_show_errors(MSG_INFO, __func__,
2201 "Handshake failed - BIO_read");
2202 if (BIO_reset(conn->ssl_out) < 0) {
2203 tls_show_errors(MSG_INFO, __func__,
2204 "BIO_reset failed");
2214 int tls_connection_encrypt(void *ssl_ctx, struct tls_connection *conn,
2215 const u8 *in_data, size_t in_len,
2216 u8 *out_data, size_t out_len)
2223 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
2224 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
2225 (res = BIO_reset(conn->ssl_out)) < 0) {
2226 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
2229 res = SSL_write(conn->ssl, in_data, in_len);
2231 tls_show_errors(MSG_INFO, __func__,
2232 "Encryption failed - SSL_write");
2236 /* Read encrypted data to be sent to the server */
2237 res = BIO_read(conn->ssl_out, out_data, out_len);
2239 tls_show_errors(MSG_INFO, __func__,
2240 "Encryption failed - BIO_read");
2248 int tls_connection_decrypt(void *ssl_ctx, struct tls_connection *conn,
2249 const u8 *in_data, size_t in_len,
2250 u8 *out_data, size_t out_len)
2254 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
2255 res = BIO_write(conn->ssl_in, in_data, in_len);
2257 tls_show_errors(MSG_INFO, __func__,
2258 "Decryption failed - BIO_write");
2261 if (BIO_reset(conn->ssl_out) < 0) {
2262 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
2266 /* Read decrypted data for further processing */
2267 res = SSL_read(conn->ssl, out_data, out_len);
2269 tls_show_errors(MSG_INFO, __func__,
2270 "Decryption failed - SSL_read");
2278 int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
2280 return conn ? conn->ssl->hit : 0;
2284 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
2287 char buf[100], *pos, *end;
2291 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
2296 end = pos + sizeof(buf);
2299 while (*c != TLS_CIPHER_NONE) {
2303 case TLS_CIPHER_RC4_SHA:
2306 case TLS_CIPHER_AES128_SHA:
2307 suite = "AES128-SHA";
2309 case TLS_CIPHER_RSA_DHE_AES128_SHA:
2310 suite = "DHE-RSA-AES128-SHA";
2312 case TLS_CIPHER_ANON_DH_AES128_SHA:
2313 suite = "ADH-AES128-SHA";
2316 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
2317 "cipher selection: %d", *c);
2320 ret = os_snprintf(pos, end - pos, ":%s", suite);
2321 if (ret < 0 || ret >= end - pos)
2328 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
2330 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
2331 tls_show_errors(MSG_INFO, __func__,
2332 "Cipher suite configuration failed");
2340 int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
2341 char *buf, size_t buflen)
2344 if (conn == NULL || conn->ssl == NULL)
2347 name = SSL_get_cipher(conn->ssl);
2351 os_strlcpy(buf, name, buflen);
2356 int tls_connection_enable_workaround(void *ssl_ctx,
2357 struct tls_connection *conn)
2359 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
2365 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2366 /* ClientHello TLS extensions require a patch to openssl, so this function is
2367 * commented out unless explicitly needed for EAP-FAST in order to be able to
2368 * build this file with unmodified openssl. */
2369 int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
2370 int ext_type, const u8 *data,
2373 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
2376 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2377 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
2380 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2381 if (SSL_set_hello_extension(conn->ssl, ext_type, (void *) data,
2384 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2388 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2391 int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
2395 return conn->failed;
2399 int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
2403 return conn->read_alerts;
2407 int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
2411 return conn->write_alerts;
2415 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
2416 const struct tls_connection_params *params)
2424 while ((err = ERR_get_error())) {
2425 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2426 __func__, ERR_error_string(err, NULL));
2429 if (params->engine) {
2430 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
2431 ret = tls_engine_init(conn, params->engine_id, params->pin,
2432 params->key_id, params->cert_id,
2433 params->ca_cert_id);
2437 if (tls_connection_set_subject_match(conn,
2438 params->subject_match,
2439 params->altsubject_match))
2442 if (params->engine && params->ca_cert_id) {
2443 if (tls_connection_engine_ca_cert(tls_ctx, conn,
2444 params->ca_cert_id))
2445 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2446 } else if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert,
2447 params->ca_cert_blob,
2448 params->ca_cert_blob_len,
2452 if (params->engine && params->cert_id) {
2453 if (tls_connection_engine_client_cert(conn, params->cert_id))
2454 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2455 } else if (tls_connection_client_cert(conn, params->client_cert,
2456 params->client_cert_blob,
2457 params->client_cert_blob_len))
2460 if (params->engine && params->key_id) {
2461 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
2462 if (tls_connection_engine_private_key(conn))
2463 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2464 } else if (tls_connection_private_key(tls_ctx, conn,
2465 params->private_key,
2466 params->private_key_passwd,
2467 params->private_key_blob,
2468 params->private_key_blob_len)) {
2469 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
2470 params->private_key);
2474 if (tls_connection_dh(conn, params->dh_file)) {
2475 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2480 tls_get_errors(tls_ctx);
2486 int tls_global_set_params(void *tls_ctx,
2487 const struct tls_connection_params *params)
2489 SSL_CTX *ssl_ctx = tls_ctx;
2492 while ((err = ERR_get_error())) {
2493 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2494 __func__, ERR_error_string(err, NULL));
2497 if (tls_global_ca_cert(ssl_ctx, params->ca_cert))
2500 if (tls_global_client_cert(ssl_ctx, params->client_cert))
2503 if (tls_global_private_key(ssl_ctx, params->private_key,
2504 params->private_key_passwd))
2507 if (tls_global_dh(ssl_ctx, params->dh_file)) {
2508 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2517 int tls_connection_get_keyblock_size(void *tls_ctx,
2518 struct tls_connection *conn)
2520 const EVP_CIPHER *c;
2523 if (conn == NULL || conn->ssl == NULL ||
2524 conn->ssl->enc_read_ctx == NULL ||
2525 conn->ssl->enc_read_ctx->cipher == NULL ||
2526 conn->ssl->read_hash == NULL)
2529 c = conn->ssl->enc_read_ctx->cipher;
2530 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
2531 h = EVP_MD_CTX_md(conn->ssl->read_hash);
2533 h = conn->ssl->read_hash;
2536 return 2 * (EVP_CIPHER_key_length(c) +
2538 EVP_CIPHER_iv_length(c));
2542 unsigned int tls_capabilities(void *tls_ctx)
2548 int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
2555 int tls_connection_ia_send_phase_finished(void *tls_ctx,
2556 struct tls_connection *conn,
2558 u8 *out_data, size_t out_len)
2564 int tls_connection_ia_final_phase_finished(void *tls_ctx,
2565 struct tls_connection *conn)
2571 int tls_connection_ia_permute_inner_secret(void *tls_ctx,
2572 struct tls_connection *conn,
2573 const u8 *key, size_t key_len)
2579 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2580 /* Pre-shared secred requires a patch to openssl, so this function is
2581 * commented out unless explicitly needed for EAP-FAST in order to be able to
2582 * build this file with unmodified openssl. */
2584 static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
2585 STACK_OF(SSL_CIPHER) *peer_ciphers,
2586 SSL_CIPHER **cipher, void *arg)
2588 struct tls_connection *conn = arg;
2591 if (conn == NULL || conn->session_ticket_cb == NULL)
2594 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
2595 conn->session_ticket,
2596 conn->session_ticket_len,
2597 s->s3->client_random,
2598 s->s3->server_random, secret);
2599 os_free(conn->session_ticket);
2600 conn->session_ticket = NULL;
2605 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
2610 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2611 static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
2614 struct tls_connection *conn = arg;
2616 if (conn == NULL || conn->session_ticket_cb == NULL)
2619 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
2621 os_free(conn->session_ticket);
2622 conn->session_ticket = NULL;
2624 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2625 "extension", data, len);
2627 conn->session_ticket = os_malloc(len);
2628 if (conn->session_ticket == NULL)
2631 os_memcpy(conn->session_ticket, data, len);
2632 conn->session_ticket_len = len;
2636 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2637 #ifdef SSL_OP_NO_TICKET
2638 static void tls_hello_ext_cb(SSL *s, int client_server, int type,
2639 unsigned char *data, int len, void *arg)
2641 struct tls_connection *conn = arg;
2643 if (conn == NULL || conn->session_ticket_cb == NULL)
2646 wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
2649 if (type == TLSEXT_TYPE_session_ticket && !client_server) {
2650 os_free(conn->session_ticket);
2651 conn->session_ticket = NULL;
2653 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2654 "extension", data, len);
2655 conn->session_ticket = os_malloc(len);
2656 if (conn->session_ticket == NULL)
2659 os_memcpy(conn->session_ticket, data, len);
2660 conn->session_ticket_len = len;
2663 #else /* SSL_OP_NO_TICKET */
2664 static int tls_hello_ext_cb(SSL *s, TLS_EXTENSION *ext, void *arg)
2666 struct tls_connection *conn = arg;
2668 if (conn == NULL || conn->session_ticket_cb == NULL)
2671 wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
2672 ext->type, ext->length);
2674 os_free(conn->session_ticket);
2675 conn->session_ticket = NULL;
2677 if (ext->type == 35) {
2678 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2679 "extension", ext->data, ext->length);
2680 conn->session_ticket = os_malloc(ext->length);
2681 if (conn->session_ticket == NULL)
2682 return SSL_AD_INTERNAL_ERROR;
2684 os_memcpy(conn->session_ticket, ext->data, ext->length);
2685 conn->session_ticket_len = ext->length;
2690 #endif /* SSL_OP_NO_TICKET */
2691 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2692 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2695 int tls_connection_set_session_ticket_cb(void *tls_ctx,
2696 struct tls_connection *conn,
2697 tls_session_ticket_cb cb,
2700 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST)
2701 conn->session_ticket_cb = cb;
2702 conn->session_ticket_cb_ctx = ctx;
2705 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
2708 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2709 SSL_set_session_ticket_ext_cb(conn->ssl,
2710 tls_session_ticket_ext_cb, conn);
2711 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2712 #ifdef SSL_OP_NO_TICKET
2713 SSL_set_tlsext_debug_callback(conn->ssl, tls_hello_ext_cb);
2714 SSL_set_tlsext_debug_arg(conn->ssl, conn);
2715 #else /* SSL_OP_NO_TICKET */
2716 if (SSL_set_hello_extension_cb(conn->ssl, tls_hello_ext_cb,
2719 #endif /* SSL_OP_NO_TICKET */
2720 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2722 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
2724 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2725 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
2726 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2727 #ifdef SSL_OP_NO_TICKET
2728 SSL_set_tlsext_debug_callback(conn->ssl, NULL);
2729 SSL_set_tlsext_debug_arg(conn->ssl, conn);
2730 #else /* SSL_OP_NO_TICKET */
2731 if (SSL_set_hello_extension_cb(conn->ssl, NULL, NULL) != 1)
2733 #endif /* SSL_OP_NO_TICKET */
2734 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2738 #else /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */
2740 #endif /* EAP_FAST || EAP_FAST_DYNAMIC || EAP_SERVER_FAST */