2 * WPA Supplicant / SSL/TLS interface functions for openssl
3 * Copyright (c) 2004-2008, 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) {
736 SSL_load_error_strings();
738 /* TODO: if /dev/urandom is available, PRNG is seeded
739 * automatically. If this is not the case, random data should
744 #endif /* PKCS12_FUNCS */
746 tls_openssl_ref_count++;
748 ssl = SSL_CTX_new(TLSv1_method());
752 SSL_CTX_set_info_callback(ssl, ssl_info_cb);
754 #ifndef OPENSSL_NO_ENGINE
756 (conf->opensc_engine_path || conf->pkcs11_engine_path ||
757 conf->pkcs11_module_path)) {
758 wpa_printf(MSG_DEBUG, "ENGINE: Loading dynamic engine");
759 ERR_load_ENGINE_strings();
760 ENGINE_load_dynamic();
762 if (tls_engine_load_dynamic_opensc(conf->opensc_engine_path) ||
763 tls_engine_load_dynamic_pkcs11(conf->pkcs11_engine_path,
764 conf->pkcs11_module_path)) {
769 #endif /* OPENSSL_NO_ENGINE */
775 void tls_deinit(void *ssl_ctx)
777 SSL_CTX *ssl = ssl_ctx;
780 tls_openssl_ref_count--;
781 if (tls_openssl_ref_count == 0) {
782 #ifndef OPENSSL_NO_ENGINE
784 #endif /* OPENSSL_NO_ENGINE */
785 CRYPTO_cleanup_all_ex_data();
793 static int tls_engine_init(struct tls_connection *conn, const char *engine_id,
794 const char *pin, const char *key_id,
795 const char *cert_id, const char *ca_cert_id)
797 #ifndef OPENSSL_NO_ENGINE
799 if (engine_id == NULL) {
800 wpa_printf(MSG_ERROR, "ENGINE: Engine ID not set");
804 wpa_printf(MSG_ERROR, "ENGINE: Smartcard PIN not set");
807 if (key_id == NULL) {
808 wpa_printf(MSG_ERROR, "ENGINE: Key Id not set");
813 conn->engine = ENGINE_by_id(engine_id);
815 wpa_printf(MSG_ERROR, "ENGINE: engine %s not available [%s]",
816 engine_id, ERR_error_string(ERR_get_error(), NULL));
819 if (ENGINE_init(conn->engine) != 1) {
820 wpa_printf(MSG_ERROR, "ENGINE: engine init failed "
821 "(engine: %s) [%s]", engine_id,
822 ERR_error_string(ERR_get_error(), NULL));
825 wpa_printf(MSG_DEBUG, "ENGINE: engine initialized");
827 if (ENGINE_ctrl_cmd_string(conn->engine, "PIN", pin, 0) == 0) {
828 wpa_printf(MSG_ERROR, "ENGINE: cannot set pin [%s]",
829 ERR_error_string(ERR_get_error(), NULL));
832 /* load private key first in-case PIN is required for cert */
833 conn->private_key = ENGINE_load_private_key(conn->engine,
835 if (!conn->private_key) {
836 wpa_printf(MSG_ERROR, "ENGINE: cannot load private key with id"
837 " '%s' [%s]", key_id,
838 ERR_error_string(ERR_get_error(), NULL));
839 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
843 /* handle a certificate and/or CA certificate */
844 if (cert_id || ca_cert_id) {
845 const char *cmd_name = "LOAD_CERT_CTRL";
847 /* test if the engine supports a LOAD_CERT_CTRL */
848 if (!ENGINE_ctrl(conn->engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
849 0, (void *)cmd_name, NULL)) {
850 wpa_printf(MSG_ERROR, "ENGINE: engine does not support"
851 " loading certificates");
852 ret = TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
861 ENGINE_free(conn->engine);
865 if (conn->private_key) {
866 EVP_PKEY_free(conn->private_key);
867 conn->private_key = NULL;
871 #else /* OPENSSL_NO_ENGINE */
873 #endif /* OPENSSL_NO_ENGINE */
877 static void tls_engine_deinit(struct tls_connection *conn)
879 #ifndef OPENSSL_NO_ENGINE
880 wpa_printf(MSG_DEBUG, "ENGINE: engine deinit");
881 if (conn->private_key) {
882 EVP_PKEY_free(conn->private_key);
883 conn->private_key = NULL;
886 ENGINE_finish(conn->engine);
889 #endif /* OPENSSL_NO_ENGINE */
893 int tls_get_errors(void *ssl_ctx)
898 while ((err = ERR_get_error())) {
899 wpa_printf(MSG_INFO, "TLS - SSL error: %s",
900 ERR_error_string(err, NULL));
907 struct tls_connection * tls_connection_init(void *ssl_ctx)
909 SSL_CTX *ssl = ssl_ctx;
910 struct tls_connection *conn;
913 conn = os_zalloc(sizeof(*conn));
916 conn->ssl = SSL_new(ssl);
917 if (conn->ssl == NULL) {
918 tls_show_errors(MSG_INFO, __func__,
919 "Failed to initialize new SSL connection");
924 SSL_set_app_data(conn->ssl, conn);
925 options = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
926 SSL_OP_SINGLE_DH_USE;
927 #ifdef SSL_OP_NO_COMPRESSION
928 options |= SSL_OP_NO_COMPRESSION;
929 #endif /* SSL_OP_NO_COMPRESSION */
930 SSL_set_options(conn->ssl, options);
932 conn->ssl_in = BIO_new(BIO_s_mem());
934 tls_show_errors(MSG_INFO, __func__,
935 "Failed to create a new BIO for ssl_in");
941 conn->ssl_out = BIO_new(BIO_s_mem());
942 if (!conn->ssl_out) {
943 tls_show_errors(MSG_INFO, __func__,
944 "Failed to create a new BIO for ssl_out");
946 BIO_free(conn->ssl_in);
951 SSL_set_bio(conn->ssl, conn->ssl_in, conn->ssl_out);
957 void tls_connection_deinit(void *ssl_ctx, struct tls_connection *conn)
962 tls_engine_deinit(conn);
963 os_free(conn->subject_match);
964 os_free(conn->altsubject_match);
965 os_free(conn->session_ticket);
970 int tls_connection_established(void *ssl_ctx, struct tls_connection *conn)
972 return conn ? SSL_is_init_finished(conn->ssl) : 0;
976 int tls_connection_shutdown(void *ssl_ctx, struct tls_connection *conn)
981 /* Shutdown previous TLS connection without notifying the peer
982 * because the connection was already terminated in practice
983 * and "close notify" shutdown alert would confuse AS. */
984 SSL_set_quiet_shutdown(conn->ssl, 1);
985 SSL_shutdown(conn->ssl);
990 static int tls_match_altsubject_component(X509 *cert, int type,
991 const char *value, size_t len)
997 ext = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
999 for (i = 0; ext && i < sk_GENERAL_NAME_num(ext); i++) {
1000 gen = sk_GENERAL_NAME_value(ext, i);
1001 if (gen->type != type)
1003 if (os_strlen((char *) gen->d.ia5->data) == len &&
1004 os_memcmp(value, gen->d.ia5->data, len) == 0)
1012 static int tls_match_altsubject(X509 *cert, const char *match)
1015 const char *pos, *end;
1020 if (os_strncmp(pos, "EMAIL:", 6) == 0) {
1023 } else if (os_strncmp(pos, "DNS:", 4) == 0) {
1026 } else if (os_strncmp(pos, "URI:", 4) == 0) {
1030 wpa_printf(MSG_INFO, "TLS: Invalid altSubjectName "
1034 end = os_strchr(pos, ';');
1036 if (os_strncmp(end + 1, "EMAIL:", 6) == 0 ||
1037 os_strncmp(end + 1, "DNS:", 4) == 0 ||
1038 os_strncmp(end + 1, "URI:", 4) == 0)
1040 end = os_strchr(end + 1, ';');
1045 len = os_strlen(pos);
1046 if (tls_match_altsubject_component(cert, type, pos, len) > 0)
1055 static int tls_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
1061 struct tls_connection *conn;
1062 char *match, *altmatch;
1064 err_cert = X509_STORE_CTX_get_current_cert(x509_ctx);
1065 err = X509_STORE_CTX_get_error(x509_ctx);
1066 depth = X509_STORE_CTX_get_error_depth(x509_ctx);
1067 ssl = X509_STORE_CTX_get_ex_data(x509_ctx,
1068 SSL_get_ex_data_X509_STORE_CTX_idx());
1069 X509_NAME_oneline(X509_get_subject_name(err_cert), buf, sizeof(buf));
1071 conn = SSL_get_app_data(ssl);
1072 match = conn ? conn->subject_match : NULL;
1073 altmatch = conn ? conn->altsubject_match : NULL;
1075 if (!preverify_ok) {
1076 wpa_printf(MSG_WARNING, "TLS: Certificate verification failed,"
1077 " error %d (%s) depth %d for '%s'", err,
1078 X509_verify_cert_error_string(err), depth, buf);
1080 wpa_printf(MSG_DEBUG, "TLS: tls_verify_cb - "
1081 "preverify_ok=%d err=%d (%s) depth=%d buf='%s'",
1083 X509_verify_cert_error_string(err), depth, buf);
1084 if (depth == 0 && match && os_strstr(buf, match) == NULL) {
1085 wpa_printf(MSG_WARNING, "TLS: Subject '%s' did not "
1086 "match with '%s'", buf, match);
1088 } else if (depth == 0 && altmatch &&
1089 !tls_match_altsubject(err_cert, altmatch)) {
1090 wpa_printf(MSG_WARNING, "TLS: altSubjectName match "
1091 "'%s' not found", altmatch);
1096 return preverify_ok;
1100 #ifndef OPENSSL_NO_STDIO
1101 static int tls_load_ca_der(void *_ssl_ctx, const char *ca_cert)
1103 SSL_CTX *ssl_ctx = _ssl_ctx;
1104 X509_LOOKUP *lookup;
1107 lookup = X509_STORE_add_lookup(ssl_ctx->cert_store,
1108 X509_LOOKUP_file());
1109 if (lookup == NULL) {
1110 tls_show_errors(MSG_WARNING, __func__,
1111 "Failed add lookup for X509 store");
1115 if (!X509_LOOKUP_load_file(lookup, ca_cert, X509_FILETYPE_ASN1)) {
1116 unsigned long err = ERR_peek_error();
1117 tls_show_errors(MSG_WARNING, __func__,
1118 "Failed load CA in DER format");
1119 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1120 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1121 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1122 "cert already in hash table error",
1130 #endif /* OPENSSL_NO_STDIO */
1133 static int tls_connection_ca_cert(void *_ssl_ctx, struct tls_connection *conn,
1134 const char *ca_cert, const u8 *ca_cert_blob,
1135 size_t ca_cert_blob_len, const char *ca_path)
1137 SSL_CTX *ssl_ctx = _ssl_ctx;
1140 * Remove previously configured trusted CA certificates before adding
1143 X509_STORE_free(ssl_ctx->cert_store);
1144 ssl_ctx->cert_store = X509_STORE_new();
1145 if (ssl_ctx->cert_store == NULL) {
1146 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1147 "certificate store", __func__);
1152 X509 *cert = d2i_X509(NULL, (OPENSSL_d2i_TYPE) &ca_cert_blob,
1155 tls_show_errors(MSG_WARNING, __func__,
1156 "Failed to parse ca_cert_blob");
1160 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1161 unsigned long err = ERR_peek_error();
1162 tls_show_errors(MSG_WARNING, __func__,
1163 "Failed to add ca_cert_blob to "
1164 "certificate store");
1165 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1166 ERR_GET_REASON(err) ==
1167 X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1168 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring "
1169 "cert already in hash table error",
1177 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added ca_cert_blob "
1178 "to certificate store", __func__);
1179 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1183 #ifdef CONFIG_NATIVE_WINDOWS
1184 if (ca_cert && tls_cryptoapi_ca_cert(ssl_ctx, conn->ssl, ca_cert) ==
1186 wpa_printf(MSG_DEBUG, "OpenSSL: Added CA certificates from "
1187 "system certificate store");
1188 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1191 #endif /* CONFIG_NATIVE_WINDOWS */
1193 if (ca_cert || ca_path) {
1194 #ifndef OPENSSL_NO_STDIO
1195 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, ca_path) !=
1197 tls_show_errors(MSG_WARNING, __func__,
1198 "Failed to load root certificates");
1200 tls_load_ca_der(ssl_ctx, ca_cert) == 0) {
1201 wpa_printf(MSG_DEBUG, "OpenSSL: %s - loaded "
1202 "DER format CA certificate",
1207 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1208 "certificate(s) loaded");
1209 tls_get_errors(ssl_ctx);
1211 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1212 #else /* OPENSSL_NO_STDIO */
1213 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1216 #endif /* OPENSSL_NO_STDIO */
1218 /* No ca_cert configured - do not try to verify server
1220 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1227 static int tls_global_ca_cert(SSL_CTX *ssl_ctx, const char *ca_cert)
1230 if (SSL_CTX_load_verify_locations(ssl_ctx, ca_cert, NULL) != 1)
1232 tls_show_errors(MSG_WARNING, __func__,
1233 "Failed to load root certificates");
1237 wpa_printf(MSG_DEBUG, "TLS: Trusted root "
1238 "certificate(s) loaded");
1240 #ifndef OPENSSL_NO_STDIO
1241 /* Add the same CAs to the client certificate requests */
1242 SSL_CTX_set_client_CA_list(ssl_ctx,
1243 SSL_load_client_CA_file(ca_cert));
1244 #endif /* OPENSSL_NO_STDIO */
1251 int tls_global_set_verify(void *ssl_ctx, int check_crl)
1256 X509_STORE *cs = SSL_CTX_get_cert_store(ssl_ctx);
1258 tls_show_errors(MSG_INFO, __func__, "Failed to get "
1259 "certificate store when enabling "
1263 flags = X509_V_FLAG_CRL_CHECK;
1265 flags |= X509_V_FLAG_CRL_CHECK_ALL;
1266 X509_STORE_set_flags(cs, flags);
1272 static int tls_connection_set_subject_match(struct tls_connection *conn,
1273 const char *subject_match,
1274 const char *altsubject_match)
1276 os_free(conn->subject_match);
1277 conn->subject_match = NULL;
1278 if (subject_match) {
1279 conn->subject_match = os_strdup(subject_match);
1280 if (conn->subject_match == NULL)
1284 os_free(conn->altsubject_match);
1285 conn->altsubject_match = NULL;
1286 if (altsubject_match) {
1287 conn->altsubject_match = os_strdup(altsubject_match);
1288 if (conn->altsubject_match == NULL)
1296 int tls_connection_set_verify(void *ssl_ctx, struct tls_connection *conn,
1299 static int counter = 0;
1305 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER |
1306 SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
1307 SSL_VERIFY_CLIENT_ONCE, tls_verify_cb);
1309 SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
1312 SSL_set_accept_state(conn->ssl);
1315 * Set session id context in order to avoid fatal errors when client
1316 * tries to resume a session. However, set the context to a unique
1317 * value in order to effectively disable session resumption for now
1318 * since not all areas of the server code are ready for it (e.g.,
1319 * EAP-TTLS needs special handling for Phase 2 after abbreviated TLS
1323 SSL_set_session_id_context(conn->ssl,
1324 (const unsigned char *) &counter,
1331 static int tls_connection_client_cert(struct tls_connection *conn,
1332 const char *client_cert,
1333 const u8 *client_cert_blob,
1334 size_t client_cert_blob_len)
1336 if (client_cert == NULL && client_cert_blob == NULL)
1339 if (client_cert_blob &&
1340 SSL_use_certificate_ASN1(conn->ssl, (u8 *) client_cert_blob,
1341 client_cert_blob_len) == 1) {
1342 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_ASN1 --> "
1345 } else if (client_cert_blob) {
1346 tls_show_errors(MSG_DEBUG, __func__,
1347 "SSL_use_certificate_ASN1 failed");
1350 if (client_cert == NULL)
1353 #ifndef OPENSSL_NO_STDIO
1354 if (SSL_use_certificate_file(conn->ssl, client_cert,
1355 SSL_FILETYPE_ASN1) == 1) {
1356 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (DER)"
1360 tls_show_errors(MSG_DEBUG, __func__,
1361 "SSL_use_certificate_file (DER) failed");
1364 if (SSL_use_certificate_file(conn->ssl, client_cert,
1365 SSL_FILETYPE_PEM) == 1) {
1366 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_certificate_file (PEM)"
1370 tls_show_errors(MSG_DEBUG, __func__,
1371 "SSL_use_certificate_file (PEM) failed");
1373 #else /* OPENSSL_NO_STDIO */
1374 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1375 #endif /* OPENSSL_NO_STDIO */
1381 static int tls_global_client_cert(SSL_CTX *ssl_ctx, const char *client_cert)
1383 #ifndef OPENSSL_NO_STDIO
1384 if (client_cert == NULL)
1387 if (SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1388 SSL_FILETYPE_ASN1) != 1 &&
1389 SSL_CTX_use_certificate_file(ssl_ctx, client_cert,
1390 SSL_FILETYPE_PEM) != 1) {
1391 tls_show_errors(MSG_INFO, __func__,
1392 "Failed to load client certificate");
1396 #else /* OPENSSL_NO_STDIO */
1397 if (client_cert == NULL)
1399 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO", __func__);
1401 #endif /* OPENSSL_NO_STDIO */
1405 static int tls_passwd_cb(char *buf, int size, int rwflag, void *password)
1407 if (password == NULL) {
1410 os_strlcpy(buf, (char *) password, size);
1411 return os_strlen(buf);
1416 static int tls_parse_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, PKCS12 *p12,
1421 STACK_OF(X509) *certs;
1428 if (!PKCS12_parse(p12, passwd, &pkey, &cert, &certs)) {
1429 tls_show_errors(MSG_DEBUG, __func__,
1430 "Failed to parse PKCS12 file");
1434 wpa_printf(MSG_DEBUG, "TLS: Successfully parsed PKCS12 data");
1437 X509_NAME_oneline(X509_get_subject_name(cert), buf,
1439 wpa_printf(MSG_DEBUG, "TLS: Got certificate from PKCS12: "
1440 "subject='%s'", buf);
1442 if (SSL_use_certificate(ssl, cert) != 1)
1445 if (SSL_CTX_use_certificate(ssl_ctx, cert) != 1)
1452 wpa_printf(MSG_DEBUG, "TLS: Got private key from PKCS12");
1454 if (SSL_use_PrivateKey(ssl, pkey) != 1)
1457 if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) != 1)
1460 EVP_PKEY_free(pkey);
1464 while ((cert = sk_X509_pop(certs)) != NULL) {
1465 X509_NAME_oneline(X509_get_subject_name(cert), buf,
1467 wpa_printf(MSG_DEBUG, "TLS: additional certificate"
1468 " from PKCS12: subject='%s'", buf);
1470 * There is no SSL equivalent for the chain cert - so
1471 * always add it to the context...
1473 if (SSL_CTX_add_extra_chain_cert(ssl_ctx, cert) != 1) {
1478 sk_X509_free(certs);
1484 tls_get_errors(ssl_ctx);
1488 #endif /* PKCS12_FUNCS */
1491 static int tls_read_pkcs12(SSL_CTX *ssl_ctx, SSL *ssl, const char *private_key,
1498 f = fopen(private_key, "rb");
1502 p12 = d2i_PKCS12_fp(f, NULL);
1506 tls_show_errors(MSG_INFO, __func__,
1507 "Failed to use PKCS#12 file");
1511 return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1513 #else /* PKCS12_FUNCS */
1514 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot read "
1517 #endif /* PKCS12_FUNCS */
1521 static int tls_read_pkcs12_blob(SSL_CTX *ssl_ctx, SSL *ssl,
1522 const u8 *blob, size_t len, const char *passwd)
1527 p12 = d2i_PKCS12(NULL, (OPENSSL_d2i_TYPE) &blob, len);
1529 tls_show_errors(MSG_INFO, __func__,
1530 "Failed to use PKCS#12 blob");
1534 return tls_parse_pkcs12(ssl_ctx, ssl, p12, passwd);
1536 #else /* PKCS12_FUNCS */
1537 wpa_printf(MSG_INFO, "TLS: PKCS12 support disabled - cannot parse "
1540 #endif /* PKCS12_FUNCS */
1544 #ifndef OPENSSL_NO_ENGINE
1545 static int tls_engine_get_cert(struct tls_connection *conn,
1546 const char *cert_id,
1549 /* this runs after the private key is loaded so no PIN is required */
1551 const char *cert_id;
1554 params.cert_id = cert_id;
1557 if (!ENGINE_ctrl_cmd(conn->engine, "LOAD_CERT_CTRL",
1558 0, ¶ms, NULL, 1)) {
1559 wpa_printf(MSG_ERROR, "ENGINE: cannot load client cert with id"
1560 " '%s' [%s]", cert_id,
1561 ERR_error_string(ERR_get_error(), NULL));
1562 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1565 wpa_printf(MSG_ERROR, "ENGINE: did not properly cert with id"
1567 return TLS_SET_PARAMS_ENGINE_PRV_INIT_FAILED;
1569 *cert = params.cert;
1572 #endif /* OPENSSL_NO_ENGINE */
1575 static int tls_connection_engine_client_cert(struct tls_connection *conn,
1576 const char *cert_id)
1578 #ifndef OPENSSL_NO_ENGINE
1581 if (tls_engine_get_cert(conn, cert_id, &cert))
1584 if (!SSL_use_certificate(conn->ssl, cert)) {
1585 tls_show_errors(MSG_ERROR, __func__,
1586 "SSL_use_certificate failed");
1591 wpa_printf(MSG_DEBUG, "ENGINE: SSL_use_certificate --> "
1595 #else /* OPENSSL_NO_ENGINE */
1597 #endif /* OPENSSL_NO_ENGINE */
1601 static int tls_connection_engine_ca_cert(void *_ssl_ctx,
1602 struct tls_connection *conn,
1603 const char *ca_cert_id)
1605 #ifndef OPENSSL_NO_ENGINE
1607 SSL_CTX *ssl_ctx = _ssl_ctx;
1609 if (tls_engine_get_cert(conn, ca_cert_id, &cert))
1612 /* start off the same as tls_connection_ca_cert */
1613 X509_STORE_free(ssl_ctx->cert_store);
1614 ssl_ctx->cert_store = X509_STORE_new();
1615 if (ssl_ctx->cert_store == NULL) {
1616 wpa_printf(MSG_DEBUG, "OpenSSL: %s - failed to allocate new "
1617 "certificate store", __func__);
1621 if (!X509_STORE_add_cert(ssl_ctx->cert_store, cert)) {
1622 unsigned long err = ERR_peek_error();
1623 tls_show_errors(MSG_WARNING, __func__,
1624 "Failed to add CA certificate from engine "
1625 "to certificate store");
1626 if (ERR_GET_LIB(err) == ERR_LIB_X509 &&
1627 ERR_GET_REASON(err) == X509_R_CERT_ALREADY_IN_HASH_TABLE) {
1628 wpa_printf(MSG_DEBUG, "OpenSSL: %s - ignoring cert"
1629 " already in hash table error",
1637 wpa_printf(MSG_DEBUG, "OpenSSL: %s - added CA certificate from engine "
1638 "to certificate store", __func__);
1639 SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, tls_verify_cb);
1642 #else /* OPENSSL_NO_ENGINE */
1644 #endif /* OPENSSL_NO_ENGINE */
1648 static int tls_connection_engine_private_key(struct tls_connection *conn)
1650 #ifndef OPENSSL_NO_ENGINE
1651 if (SSL_use_PrivateKey(conn->ssl, conn->private_key) != 1) {
1652 tls_show_errors(MSG_ERROR, __func__,
1653 "ENGINE: cannot use private key for TLS");
1656 if (!SSL_check_private_key(conn->ssl)) {
1657 tls_show_errors(MSG_INFO, __func__,
1658 "Private key failed verification");
1662 #else /* OPENSSL_NO_ENGINE */
1663 wpa_printf(MSG_ERROR, "SSL: Configuration uses engine, but "
1664 "engine support was not compiled in");
1666 #endif /* OPENSSL_NO_ENGINE */
1670 static int tls_connection_private_key(void *_ssl_ctx,
1671 struct tls_connection *conn,
1672 const char *private_key,
1673 const char *private_key_passwd,
1674 const u8 *private_key_blob,
1675 size_t private_key_blob_len)
1677 SSL_CTX *ssl_ctx = _ssl_ctx;
1681 if (private_key == NULL && private_key_blob == NULL)
1684 if (private_key_passwd) {
1685 passwd = os_strdup(private_key_passwd);
1691 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1692 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1695 while (private_key_blob) {
1696 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA, conn->ssl,
1697 (u8 *) private_key_blob,
1698 private_key_blob_len) == 1) {
1699 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1700 "ASN1(EVP_PKEY_RSA) --> OK");
1704 tls_show_errors(MSG_DEBUG, __func__,
1705 "SSL_use_PrivateKey_ASN1(EVP_PKEY_RSA)"
1709 if (SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA, conn->ssl,
1710 (u8 *) private_key_blob,
1711 private_key_blob_len) == 1) {
1712 wpa_printf(MSG_DEBUG, "OpenSSL: SSL_use_PrivateKey_"
1713 "ASN1(EVP_PKEY_DSA) --> OK");
1717 tls_show_errors(MSG_DEBUG, __func__,
1718 "SSL_use_PrivateKey_ASN1(EVP_PKEY_DSA)"
1722 if (SSL_use_RSAPrivateKey_ASN1(conn->ssl,
1723 (u8 *) private_key_blob,
1724 private_key_blob_len) == 1) {
1725 wpa_printf(MSG_DEBUG, "OpenSSL: "
1726 "SSL_use_RSAPrivateKey_ASN1 --> OK");
1730 tls_show_errors(MSG_DEBUG, __func__,
1731 "SSL_use_RSAPrivateKey_ASN1 failed");
1734 if (tls_read_pkcs12_blob(ssl_ctx, conn->ssl, private_key_blob,
1735 private_key_blob_len, passwd) == 0) {
1736 wpa_printf(MSG_DEBUG, "OpenSSL: PKCS#12 as blob --> "
1745 while (!ok && private_key) {
1746 #ifndef OPENSSL_NO_STDIO
1747 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1748 SSL_FILETYPE_ASN1) == 1) {
1749 wpa_printf(MSG_DEBUG, "OpenSSL: "
1750 "SSL_use_PrivateKey_File (DER) --> OK");
1754 tls_show_errors(MSG_DEBUG, __func__,
1755 "SSL_use_PrivateKey_File (DER) "
1759 if (SSL_use_PrivateKey_file(conn->ssl, private_key,
1760 SSL_FILETYPE_PEM) == 1) {
1761 wpa_printf(MSG_DEBUG, "OpenSSL: "
1762 "SSL_use_PrivateKey_File (PEM) --> OK");
1766 tls_show_errors(MSG_DEBUG, __func__,
1767 "SSL_use_PrivateKey_File (PEM) "
1770 #else /* OPENSSL_NO_STDIO */
1771 wpa_printf(MSG_DEBUG, "OpenSSL: %s - OPENSSL_NO_STDIO",
1773 #endif /* OPENSSL_NO_STDIO */
1775 if (tls_read_pkcs12(ssl_ctx, conn->ssl, private_key, passwd)
1777 wpa_printf(MSG_DEBUG, "OpenSSL: Reading PKCS#12 file "
1783 if (tls_cryptoapi_cert(conn->ssl, private_key) == 0) {
1784 wpa_printf(MSG_DEBUG, "OpenSSL: Using CryptoAPI to "
1785 "access certificate store --> OK");
1794 wpa_printf(MSG_INFO, "OpenSSL: Failed to load private key");
1800 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1803 if (!SSL_check_private_key(conn->ssl)) {
1804 tls_show_errors(MSG_INFO, __func__, "Private key failed "
1809 wpa_printf(MSG_DEBUG, "SSL: Private key loaded successfully");
1814 static int tls_global_private_key(SSL_CTX *ssl_ctx, const char *private_key,
1815 const char *private_key_passwd)
1819 if (private_key == NULL)
1822 if (private_key_passwd) {
1823 passwd = os_strdup(private_key_passwd);
1829 SSL_CTX_set_default_passwd_cb(ssl_ctx, tls_passwd_cb);
1830 SSL_CTX_set_default_passwd_cb_userdata(ssl_ctx, passwd);
1832 #ifndef OPENSSL_NO_STDIO
1833 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1834 SSL_FILETYPE_ASN1) != 1 &&
1835 SSL_CTX_use_PrivateKey_file(ssl_ctx, private_key,
1836 SSL_FILETYPE_PEM) != 1 &&
1837 #endif /* OPENSSL_NO_STDIO */
1838 tls_read_pkcs12(ssl_ctx, NULL, private_key, passwd)) {
1839 tls_show_errors(MSG_INFO, __func__,
1840 "Failed to load private key");
1847 SSL_CTX_set_default_passwd_cb(ssl_ctx, NULL);
1849 if (!SSL_CTX_check_private_key(ssl_ctx)) {
1850 tls_show_errors(MSG_INFO, __func__,
1851 "Private key failed verification");
1859 static int tls_connection_dh(struct tls_connection *conn, const char *dh_file)
1861 #ifdef OPENSSL_NO_DH
1862 if (dh_file == NULL)
1864 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1865 "dh_file specified");
1867 #else /* OPENSSL_NO_DH */
1871 /* TODO: add support for dh_blob */
1872 if (dh_file == NULL)
1877 bio = BIO_new_file(dh_file, "r");
1879 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1880 dh_file, ERR_error_string(ERR_get_error(), NULL));
1883 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1885 #ifndef OPENSSL_NO_DSA
1886 while (dh == NULL) {
1888 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1889 " trying to parse as DSA params", dh_file,
1890 ERR_error_string(ERR_get_error(), NULL));
1891 bio = BIO_new_file(dh_file, "r");
1894 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1897 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1898 "'%s': %s", dh_file,
1899 ERR_error_string(ERR_get_error(), NULL));
1903 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
1904 dh = DSA_dup_DH(dsa);
1907 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
1908 "params into DH params");
1913 #endif /* !OPENSSL_NO_DSA */
1915 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
1920 if (SSL_set_tmp_dh(conn->ssl, dh) != 1) {
1921 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
1923 ERR_error_string(ERR_get_error(), NULL));
1929 #endif /* OPENSSL_NO_DH */
1933 static int tls_global_dh(SSL_CTX *ssl_ctx, const char *dh_file)
1935 #ifdef OPENSSL_NO_DH
1936 if (dh_file == NULL)
1938 wpa_printf(MSG_ERROR, "TLS: openssl does not include DH support, but "
1939 "dh_file specified");
1941 #else /* OPENSSL_NO_DH */
1945 /* TODO: add support for dh_blob */
1946 if (dh_file == NULL)
1948 if (ssl_ctx == NULL)
1951 bio = BIO_new_file(dh_file, "r");
1953 wpa_printf(MSG_INFO, "TLS: Failed to open DH file '%s': %s",
1954 dh_file, ERR_error_string(ERR_get_error(), NULL));
1957 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1959 #ifndef OPENSSL_NO_DSA
1960 while (dh == NULL) {
1962 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DH file '%s': %s -"
1963 " trying to parse as DSA params", dh_file,
1964 ERR_error_string(ERR_get_error(), NULL));
1965 bio = BIO_new_file(dh_file, "r");
1968 dsa = PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
1971 wpa_printf(MSG_DEBUG, "TLS: Failed to parse DSA file "
1972 "'%s': %s", dh_file,
1973 ERR_error_string(ERR_get_error(), NULL));
1977 wpa_printf(MSG_DEBUG, "TLS: DH file in DSA param format");
1978 dh = DSA_dup_DH(dsa);
1981 wpa_printf(MSG_INFO, "TLS: Failed to convert DSA "
1982 "params into DH params");
1987 #endif /* !OPENSSL_NO_DSA */
1989 wpa_printf(MSG_INFO, "TLS: Failed to read/parse DH/DSA file "
1994 if (SSL_CTX_set_tmp_dh(ssl_ctx, dh) != 1) {
1995 wpa_printf(MSG_INFO, "TLS: Failed to set DH params from '%s': "
1997 ERR_error_string(ERR_get_error(), NULL));
2003 #endif /* OPENSSL_NO_DH */
2007 int tls_connection_get_keys(void *ssl_ctx, struct tls_connection *conn,
2008 struct tls_keys *keys)
2012 if (conn == NULL || keys == NULL)
2015 if (ssl == NULL || ssl->s3 == NULL || ssl->session == NULL)
2018 os_memset(keys, 0, sizeof(*keys));
2019 keys->master_key = ssl->session->master_key;
2020 keys->master_key_len = ssl->session->master_key_length;
2021 keys->client_random = ssl->s3->client_random;
2022 keys->client_random_len = SSL3_RANDOM_SIZE;
2023 keys->server_random = ssl->s3->server_random;
2024 keys->server_random_len = SSL3_RANDOM_SIZE;
2030 int tls_connection_prf(void *tls_ctx, struct tls_connection *conn,
2031 const char *label, int server_random_first,
2032 u8 *out, size_t out_len)
2038 u8 * tls_connection_handshake(void *ssl_ctx, struct tls_connection *conn,
2039 const u8 *in_data, size_t in_len,
2040 size_t *out_len, u8 **appl_data,
2041 size_t *appl_data_len)
2050 * Give TLS handshake data from the server (if available) to OpenSSL
2054 BIO_write(conn->ssl_in, in_data, in_len) < 0) {
2055 tls_show_errors(MSG_INFO, __func__,
2056 "Handshake failed - BIO_write");
2060 /* Initiate TLS handshake or continue the existing handshake */
2061 res = SSL_connect(conn->ssl);
2063 int err = SSL_get_error(conn->ssl, res);
2064 if (err == SSL_ERROR_WANT_READ)
2065 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want "
2067 else if (err == SSL_ERROR_WANT_WRITE)
2068 wpa_printf(MSG_DEBUG, "SSL: SSL_connect - want to "
2071 tls_show_errors(MSG_INFO, __func__, "SSL_connect");
2076 /* Get the TLS handshake data to be sent to the server */
2077 res = BIO_ctrl_pending(conn->ssl_out);
2078 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
2079 out_data = os_malloc(res == 0 ? 1 : res);
2080 if (out_data == NULL) {
2081 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
2082 "handshake output (%d bytes)", res);
2083 if (BIO_reset(conn->ssl_out) < 0) {
2084 tls_show_errors(MSG_INFO, __func__,
2085 "BIO_reset failed");
2090 res = res == 0 ? 0 : BIO_read(conn->ssl_out, out_data, res);
2092 tls_show_errors(MSG_INFO, __func__,
2093 "Handshake failed - BIO_read");
2094 if (BIO_reset(conn->ssl_out) < 0) {
2095 tls_show_errors(MSG_INFO, __func__,
2096 "BIO_reset failed");
2103 if (SSL_is_init_finished(conn->ssl) && appl_data) {
2104 *appl_data = os_malloc(in_len);
2106 res = SSL_read(conn->ssl, *appl_data, in_len);
2108 tls_show_errors(MSG_INFO, __func__,
2109 "Failed to read possible "
2110 "Application Data");
2111 os_free(*appl_data);
2114 *appl_data_len = res;
2115 wpa_hexdump_key(MSG_MSGDUMP, "SSL: Application"
2116 " Data in Finish message",
2117 *appl_data, *appl_data_len);
2126 u8 * tls_connection_server_handshake(void *ssl_ctx,
2127 struct tls_connection *conn,
2128 const u8 *in_data, size_t in_len,
2135 * Give TLS handshake data from the client (if available) to OpenSSL
2139 BIO_write(conn->ssl_in, in_data, in_len) < 0) {
2140 tls_show_errors(MSG_INFO, __func__,
2141 "Handshake failed - BIO_write");
2145 /* Initiate TLS handshake or continue the existing handshake */
2146 res = SSL_accept(conn->ssl);
2148 int err = SSL_get_error(conn->ssl, res);
2149 if (err == SSL_ERROR_WANT_READ)
2150 wpa_printf(MSG_DEBUG, "SSL: SSL_accept - want "
2152 else if (err == SSL_ERROR_WANT_WRITE)
2153 wpa_printf(MSG_DEBUG, "SSL: SSL_accept - want to "
2156 tls_show_errors(MSG_INFO, __func__, "SSL_accept");
2161 /* Get the TLS handshake data to be sent to the client */
2162 res = BIO_ctrl_pending(conn->ssl_out);
2163 wpa_printf(MSG_DEBUG, "SSL: %d bytes pending from ssl_out", res);
2164 out_data = os_malloc(res == 0 ? 1 : res);
2165 if (out_data == NULL) {
2166 wpa_printf(MSG_DEBUG, "SSL: Failed to allocate memory for "
2167 "handshake output (%d bytes)", res);
2168 if (BIO_reset(conn->ssl_out) < 0) {
2169 tls_show_errors(MSG_INFO, __func__,
2170 "BIO_reset failed");
2175 res = res == 0 ? 0 : BIO_read(conn->ssl_out, out_data, res);
2177 tls_show_errors(MSG_INFO, __func__,
2178 "Handshake failed - BIO_read");
2179 if (BIO_reset(conn->ssl_out) < 0) {
2180 tls_show_errors(MSG_INFO, __func__,
2181 "BIO_reset failed");
2191 int tls_connection_encrypt(void *ssl_ctx, struct tls_connection *conn,
2192 const u8 *in_data, size_t in_len,
2193 u8 *out_data, size_t out_len)
2200 /* Give plaintext data for OpenSSL to encrypt into the TLS tunnel. */
2201 if ((res = BIO_reset(conn->ssl_in)) < 0 ||
2202 (res = BIO_reset(conn->ssl_out)) < 0) {
2203 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
2206 res = SSL_write(conn->ssl, in_data, in_len);
2208 tls_show_errors(MSG_INFO, __func__,
2209 "Encryption failed - SSL_write");
2213 /* Read encrypted data to be sent to the server */
2214 res = BIO_read(conn->ssl_out, out_data, out_len);
2216 tls_show_errors(MSG_INFO, __func__,
2217 "Encryption failed - BIO_read");
2225 int tls_connection_decrypt(void *ssl_ctx, struct tls_connection *conn,
2226 const u8 *in_data, size_t in_len,
2227 u8 *out_data, size_t out_len)
2231 /* Give encrypted data from TLS tunnel for OpenSSL to decrypt. */
2232 res = BIO_write(conn->ssl_in, in_data, in_len);
2234 tls_show_errors(MSG_INFO, __func__,
2235 "Decryption failed - BIO_write");
2238 if (BIO_reset(conn->ssl_out) < 0) {
2239 tls_show_errors(MSG_INFO, __func__, "BIO_reset failed");
2243 /* Read decrypted data for further processing */
2244 res = SSL_read(conn->ssl, out_data, out_len);
2246 tls_show_errors(MSG_INFO, __func__,
2247 "Decryption failed - SSL_read");
2255 int tls_connection_resumed(void *ssl_ctx, struct tls_connection *conn)
2257 return conn ? conn->ssl->hit : 0;
2261 int tls_connection_set_cipher_list(void *tls_ctx, struct tls_connection *conn,
2264 char buf[100], *pos, *end;
2268 if (conn == NULL || conn->ssl == NULL || ciphers == NULL)
2273 end = pos + sizeof(buf);
2276 while (*c != TLS_CIPHER_NONE) {
2280 case TLS_CIPHER_RC4_SHA:
2283 case TLS_CIPHER_AES128_SHA:
2284 suite = "AES128-SHA";
2286 case TLS_CIPHER_RSA_DHE_AES128_SHA:
2287 suite = "DHE-RSA-AES128-SHA";
2289 case TLS_CIPHER_ANON_DH_AES128_SHA:
2290 suite = "ADH-AES128-SHA";
2293 wpa_printf(MSG_DEBUG, "TLS: Unsupported "
2294 "cipher selection: %d", *c);
2297 ret = os_snprintf(pos, end - pos, ":%s", suite);
2298 if (ret < 0 || ret >= end - pos)
2305 wpa_printf(MSG_DEBUG, "OpenSSL: cipher suites: %s", buf + 1);
2307 if (SSL_set_cipher_list(conn->ssl, buf + 1) != 1) {
2308 tls_show_errors(MSG_INFO, __func__,
2309 "Cipher suite configuration failed");
2317 int tls_get_cipher(void *ssl_ctx, struct tls_connection *conn,
2318 char *buf, size_t buflen)
2321 if (conn == NULL || conn->ssl == NULL)
2324 name = SSL_get_cipher(conn->ssl);
2328 os_strlcpy(buf, name, buflen);
2333 int tls_connection_enable_workaround(void *ssl_ctx,
2334 struct tls_connection *conn)
2336 SSL_set_options(conn->ssl, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
2342 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2343 /* ClientHello TLS extensions require a patch to openssl, so this function is
2344 * commented out unless explicitly needed for EAP-FAST in order to be able to
2345 * build this file with unmodified openssl. */
2346 int tls_connection_client_hello_ext(void *ssl_ctx, struct tls_connection *conn,
2347 int ext_type, const u8 *data,
2350 if (conn == NULL || conn->ssl == NULL || ext_type != 35)
2353 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2354 if (SSL_set_session_ticket_ext(conn->ssl, (void *) data,
2357 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2358 if (SSL_set_hello_extension(conn->ssl, ext_type, (void *) data,
2361 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2365 #endif /* EAP_FAST || EAP_FAST_DYNAMIC */
2368 int tls_connection_get_failed(void *ssl_ctx, struct tls_connection *conn)
2372 return conn->failed;
2376 int tls_connection_get_read_alerts(void *ssl_ctx, struct tls_connection *conn)
2380 return conn->read_alerts;
2384 int tls_connection_get_write_alerts(void *ssl_ctx, struct tls_connection *conn)
2388 return conn->write_alerts;
2392 int tls_connection_set_params(void *tls_ctx, struct tls_connection *conn,
2393 const struct tls_connection_params *params)
2401 while ((err = ERR_get_error())) {
2402 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2403 __func__, ERR_error_string(err, NULL));
2406 if (params->engine) {
2407 wpa_printf(MSG_DEBUG, "SSL: Initializing TLS engine");
2408 ret = tls_engine_init(conn, params->engine_id, params->pin,
2409 params->key_id, params->cert_id,
2410 params->ca_cert_id);
2414 if (tls_connection_set_subject_match(conn,
2415 params->subject_match,
2416 params->altsubject_match))
2419 if (params->engine && params->ca_cert_id) {
2420 if (tls_connection_engine_ca_cert(tls_ctx, conn,
2421 params->ca_cert_id))
2422 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2423 } else if (tls_connection_ca_cert(tls_ctx, conn, params->ca_cert,
2424 params->ca_cert_blob,
2425 params->ca_cert_blob_len,
2429 if (params->engine && params->cert_id) {
2430 if (tls_connection_engine_client_cert(conn, params->cert_id))
2431 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2432 } else if (tls_connection_client_cert(conn, params->client_cert,
2433 params->client_cert_blob,
2434 params->client_cert_blob_len))
2437 if (params->engine && params->key_id) {
2438 wpa_printf(MSG_DEBUG, "TLS: Using private key from engine");
2439 if (tls_connection_engine_private_key(conn))
2440 return TLS_SET_PARAMS_ENGINE_PRV_VERIFY_FAILED;
2441 } else if (tls_connection_private_key(tls_ctx, conn,
2442 params->private_key,
2443 params->private_key_passwd,
2444 params->private_key_blob,
2445 params->private_key_blob_len)) {
2446 wpa_printf(MSG_INFO, "TLS: Failed to load private key '%s'",
2447 params->private_key);
2451 if (tls_connection_dh(conn, params->dh_file)) {
2452 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2457 tls_get_errors(tls_ctx);
2463 int tls_global_set_params(void *tls_ctx,
2464 const struct tls_connection_params *params)
2466 SSL_CTX *ssl_ctx = tls_ctx;
2469 while ((err = ERR_get_error())) {
2470 wpa_printf(MSG_INFO, "%s: Clearing pending SSL error: %s",
2471 __func__, ERR_error_string(err, NULL));
2474 if (tls_global_ca_cert(ssl_ctx, params->ca_cert))
2477 if (tls_global_client_cert(ssl_ctx, params->client_cert))
2480 if (tls_global_private_key(ssl_ctx, params->private_key,
2481 params->private_key_passwd))
2484 if (tls_global_dh(ssl_ctx, params->dh_file)) {
2485 wpa_printf(MSG_INFO, "TLS: Failed to load DH file '%s'",
2494 int tls_connection_get_keyblock_size(void *tls_ctx,
2495 struct tls_connection *conn)
2497 const EVP_CIPHER *c;
2500 if (conn == NULL || conn->ssl == NULL ||
2501 conn->ssl->enc_read_ctx == NULL ||
2502 conn->ssl->enc_read_ctx->cipher == NULL ||
2503 conn->ssl->read_hash == NULL)
2506 c = conn->ssl->enc_read_ctx->cipher;
2507 #if OPENSSL_VERSION_NUMBER >= 0x00909000L
2508 h = EVP_MD_CTX_md(conn->ssl->read_hash);
2510 h = conn->ssl->read_hash;
2513 return 2 * (EVP_CIPHER_key_length(c) +
2515 EVP_CIPHER_iv_length(c));
2519 unsigned int tls_capabilities(void *tls_ctx)
2525 int tls_connection_set_ia(void *tls_ctx, struct tls_connection *conn,
2532 int tls_connection_ia_send_phase_finished(void *tls_ctx,
2533 struct tls_connection *conn,
2535 u8 *out_data, size_t out_len)
2541 int tls_connection_ia_final_phase_finished(void *tls_ctx,
2542 struct tls_connection *conn)
2548 int tls_connection_ia_permute_inner_secret(void *tls_ctx,
2549 struct tls_connection *conn,
2550 const u8 *key, size_t key_len)
2556 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2557 /* Pre-shared secred requires a patch to openssl, so this function is
2558 * commented out unless explicitly needed for EAP-FAST in order to be able to
2559 * build this file with unmodified openssl. */
2561 static int tls_sess_sec_cb(SSL *s, void *secret, int *secret_len,
2562 STACK_OF(SSL_CIPHER) *peer_ciphers,
2563 SSL_CIPHER **cipher, void *arg)
2565 struct tls_connection *conn = arg;
2568 if (conn == NULL || conn->session_ticket_cb == NULL)
2571 ret = conn->session_ticket_cb(conn->session_ticket_cb_ctx,
2572 conn->session_ticket,
2573 conn->session_ticket_len,
2574 s->s3->client_random,
2575 s->s3->server_random, secret);
2576 os_free(conn->session_ticket);
2577 conn->session_ticket = NULL;
2582 *secret_len = SSL_MAX_MASTER_KEY_LENGTH;
2587 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2588 static int tls_session_ticket_ext_cb(SSL *s, const unsigned char *data,
2591 struct tls_connection *conn = arg;
2593 if (conn == NULL || conn->session_ticket_cb == NULL)
2596 wpa_printf(MSG_DEBUG, "OpenSSL: %s: length=%d", __func__, len);
2598 os_free(conn->session_ticket);
2599 conn->session_ticket = NULL;
2601 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2602 "extension", data, len);
2604 conn->session_ticket = os_malloc(len);
2605 if (conn->session_ticket == NULL)
2608 os_memcpy(conn->session_ticket, data, len);
2609 conn->session_ticket_len = len;
2613 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2614 #ifdef SSL_OP_NO_TICKET
2615 static void tls_hello_ext_cb(SSL *s, int client_server, int type,
2616 unsigned char *data, int len, void *arg)
2618 struct tls_connection *conn = arg;
2620 if (conn == NULL || conn->session_ticket_cb == NULL)
2623 wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
2626 if (type == TLSEXT_TYPE_session_ticket && !client_server) {
2627 os_free(conn->session_ticket);
2628 conn->session_ticket = NULL;
2630 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2631 "extension", data, len);
2632 conn->session_ticket = os_malloc(len);
2633 if (conn->session_ticket == NULL)
2636 os_memcpy(conn->session_ticket, data, len);
2637 conn->session_ticket_len = len;
2640 #else /* SSL_OP_NO_TICKET */
2641 static int tls_hello_ext_cb(SSL *s, TLS_EXTENSION *ext, void *arg)
2643 struct tls_connection *conn = arg;
2645 if (conn == NULL || conn->session_ticket_cb == NULL)
2648 wpa_printf(MSG_DEBUG, "OpenSSL: %s: type=%d length=%d", __func__,
2649 ext->type, ext->length);
2651 os_free(conn->session_ticket);
2652 conn->session_ticket = NULL;
2654 if (ext->type == 35) {
2655 wpa_hexdump(MSG_DEBUG, "OpenSSL: ClientHello SessionTicket "
2656 "extension", ext->data, ext->length);
2657 conn->session_ticket = os_malloc(ext->length);
2658 if (conn->session_ticket == NULL)
2659 return SSL_AD_INTERNAL_ERROR;
2661 os_memcpy(conn->session_ticket, ext->data, ext->length);
2662 conn->session_ticket_len = ext->length;
2667 #endif /* SSL_OP_NO_TICKET */
2668 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2669 #endif /* EAP_FAST || EAP_FAST_DYNAMIC */
2672 int tls_connection_set_session_ticket_cb(void *tls_ctx,
2673 struct tls_connection *conn,
2674 tls_session_ticket_cb cb,
2677 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC)
2678 conn->session_ticket_cb = cb;
2679 conn->session_ticket_cb_ctx = ctx;
2682 if (SSL_set_session_secret_cb(conn->ssl, tls_sess_sec_cb,
2685 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2686 SSL_set_session_ticket_ext_cb(conn->ssl,
2687 tls_session_ticket_ext_cb, conn);
2688 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2689 #ifdef SSL_OP_NO_TICKET
2690 SSL_set_tlsext_debug_callback(conn->ssl, tls_hello_ext_cb);
2691 SSL_set_tlsext_debug_arg(conn->ssl, conn);
2692 #else /* SSL_OP_NO_TICKET */
2693 if (SSL_set_hello_extension_cb(conn->ssl, tls_hello_ext_cb,
2696 #endif /* SSL_OP_NO_TICKET */
2697 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2699 if (SSL_set_session_secret_cb(conn->ssl, NULL, NULL) != 1)
2701 #ifdef CONFIG_OPENSSL_TICKET_OVERRIDE
2702 SSL_set_session_ticket_ext_cb(conn->ssl, NULL, NULL);
2703 #else /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2704 #ifdef SSL_OP_NO_TICKET
2705 SSL_set_tlsext_debug_callback(conn->ssl, NULL);
2706 SSL_set_tlsext_debug_arg(conn->ssl, conn);
2707 #else /* SSL_OP_NO_TICKET */
2708 if (SSL_set_hello_extension_cb(conn->ssl, NULL, NULL) != 1)
2710 #endif /* SSL_OP_NO_TICKET */
2711 #endif /* CONFIG_OPENSSL_TICKET_OVERRIDE */
2715 #else /* EAP_FAST || EAP_FAST_DYNAMIC */
2717 #endif /* EAP_FAST || EAP_FAST_DYNAMIC */