diff --git a/lib/EAPBase/include/Method.h b/lib/EAPBase/include/Method.h index c042c49..19a0c87 100644 --- a/lib/EAPBase/include/Method.h +++ b/lib/EAPBase/include/Method.h @@ -51,11 +51,10 @@ namespace eap /// Constructs an EAP method /// /// \param[in] mod EAP module to use for global services - /// \param[in] cfg Connection configuration + /// \param[in] cfg Method configuration /// \param[in] cred User credentials /// - method(_In_ module &module, _In_ config_connection &cfg, _In_ credentials &cred); - + method(_In_ module &module, _In_ config_method_with_cred &cfg, _In_ credentials &cred); /// /// Moves an EAP method @@ -130,8 +129,9 @@ namespace eap method& operator=(_In_ const method &other); public: - module &m_module; ///< EAP module - config_connection &m_cfg; ///< Connection configuration - credentials &m_cred; ///< User credentials + module &m_module; ///< EAP module + config_method_with_cred &m_cfg; ///< Connection configuration + credentials &m_cred; ///< User credentials + std::vector m_eap_attr; ///< EAP attributes }; } diff --git a/lib/EAPBase/src/Method.cpp b/lib/EAPBase/src/Method.cpp index 6b84417..c4c6a53 100644 --- a/lib/EAPBase/src/Method.cpp +++ b/lib/EAPBase/src/Method.cpp @@ -28,7 +28,7 @@ using namespace winstd; // eap::method ////////////////////////////////////////////////////////////////////// -eap::method::method(_In_ module &module, _In_ config_connection &cfg, _In_ credentials &cred) : +eap::method::method(_In_ module &module, _In_ config_method_with_cred &cfg, _In_ credentials &cred) : m_module(module), m_cfg(cfg), m_cred(cred) @@ -37,9 +37,10 @@ eap::method::method(_In_ module &module, _In_ config_connection &cfg, _In_ crede eap::method::method(_Inout_ method &&other) : - m_module(other.m_module), - m_cfg(other.m_cfg), - m_cred(other.m_cred) + m_module ( other.m_module ), + m_cfg ( other.m_cfg ), + m_cred ( other.m_cred ), + m_eap_attr(std::move(other.m_eap_attr)) { } @@ -50,6 +51,7 @@ eap::method& eap::method::operator=(_Inout_ method &&other) assert(std::addressof(m_module) == std::addressof(other.m_module)); // Move method within same module only! assert(std::addressof(m_cfg ) == std::addressof(other.m_cfg )); // Move method with same configuration only! assert(std::addressof(m_cred ) == std::addressof(other.m_cred )); // Move method with same credentials only! + m_eap_attr = std::move(other.m_eap_attr); } return *this; diff --git a/lib/Events/res/EventsETW.man b/lib/Events/res/EventsETW.man index 44756f6..bb6628d 100644 Binary files a/lib/Events/res/EventsETW.man and b/lib/Events/res/EventsETW.man differ diff --git a/lib/PAP/build/PAP.vcxproj b/lib/PAP/build/PAP.vcxproj index 3255540..094c02e 100644 --- a/lib/PAP/build/PAP.vcxproj +++ b/lib/PAP/build/PAP.vcxproj @@ -81,11 +81,13 @@ + + Create Create diff --git a/lib/PAP/build/PAP.vcxproj.filters b/lib/PAP/build/PAP.vcxproj.filters index 3761eec..8c96e75 100644 --- a/lib/PAP/build/PAP.vcxproj.filters +++ b/lib/PAP/build/PAP.vcxproj.filters @@ -20,6 +20,9 @@ Header Files + + Header Files + @@ -31,5 +34,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/lib/PAP/include/Method.h b/lib/PAP/include/Method.h new file mode 100644 index 0000000..c39efb3 --- /dev/null +++ b/lib/PAP/include/Method.h @@ -0,0 +1,167 @@ +/* + Copyright 2015-2016 Amebis + Copyright 2016 GÉANT + + This file is part of GÉANTLink. + + GÉANTLink is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + GÉANTLink is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GÉANTLink. If not, see . +*/ + +namespace eap +{ + /// + /// PAP method + /// + class method_pap; +} + + +#pragma once + +#include "Config.h" +#include "Credentials.h" + +#include "../../EAPBase/include/Method.h" + + +namespace eap +{ + class method_pap : public method + { + public: + /// + /// EAP-PAP packet (data) + /// + class packet + { + public: + /// + /// Constructs an empty packet + /// + packet(); + + /// + /// Copies a packet + /// + /// \param[in] other Packet to copy from + /// + packet(_In_ const packet &other); + + /// + /// Moves a packet + /// + /// \param[in] other Packet to move from + /// + packet(_Inout_ packet &&other); + + /// + /// Copies a packet + /// + /// \param[in] other Packet to copy from + /// + /// \returns Reference to this object + /// + packet& operator=(_In_ const packet &other); + + /// + /// Moves a packet + /// + /// \param[in] other Packet to move from + /// + /// \returns Reference to this object + /// + packet& operator=(_Inout_ packet &&other); + + /// + /// Empty the packet + /// + void clear(); + + public: + EapCode m_code; ///< Packet code + unsigned char m_id; ///< Packet ID + sanitizing_blob m_data; ///< Packet data + }; + + public: + /// + /// Constructs an EAP method + /// + /// \param[in] mod EAP module to use for global services + /// \param[in] cfg Method configuration + /// \param[in] cred User credentials + /// + method_pap(_In_ module &module, _In_ config_method_pap &cfg, _In_ credentials_pap &cred); + + /// + /// Moves an EAP method + /// + /// \param[in] other EAP method to move from + /// + method_pap(_Inout_ method_pap &&other); + + /// + /// Moves an EAP method + /// + /// \param[in] other EAP method to move from + /// + /// \returns Reference to this object + /// + method_pap& operator=(_Inout_ method_pap &&other); + + /// \name Packet processing + /// @{ + + /// + /// Processes a packet received by EapHost from a supplicant. + /// + /// \sa [EapPeerProcessRequestPacket function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363621.aspx) + /// + virtual void process_request_packet( + _In_bytecount_(dwReceivedPacketSize) const EapPacket *pReceivedPacket, + _In_ DWORD dwReceivedPacketSize, + _Inout_ EapPeerMethodOutput *pEapOutput); + + /// + /// Obtains a response packet from the EAP method. + /// + /// \sa [EapPeerGetResponsePacket function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363610.aspx) + /// + virtual void get_response_packet( + _Inout_bytecap_(*dwSendPacketSize) EapPacket *pSendPacket, + _Inout_ DWORD *pdwSendPacketSize); + + /// + /// Obtains the result of an authentication session from the EAP method. + /// + /// \sa [EapPeerGetResult function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363611.aspx) + /// + virtual void get_result( + _In_ EapPeerMethodResultReason reason, + _Inout_ EapPeerMethodResult *ppResult); + + /// @} + + protected: + credentials_pap &m_cred; ///< EAP-TLS user credentials + + packet m_packet_res; ///< Response packet + + enum { + phase_unknown = -1, ///< Unknown phase + phase_init = 0, ///< Handshake initialize + phase_finished, ///< Connection shut down + } m_phase, m_phase_prev; ///< What phase is our communication at? + }; +} diff --git a/lib/PAP/src/Method.cpp b/lib/PAP/src/Method.cpp new file mode 100644 index 0000000..07ebbd0 --- /dev/null +++ b/lib/PAP/src/Method.cpp @@ -0,0 +1,283 @@ +/* + Copyright 2015-2016 Amebis + Copyright 2016 GÉANT + + This file is part of GÉANTLink. + + GÉANTLink is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + GÉANTLink is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GÉANTLink. If not, see . +*/ + +#include "StdAfx.h" + +using namespace std; +using namespace winstd; + + +////////////////////////////////////////////////////////////////////// +// eap::method_pap::packet +////////////////////////////////////////////////////////////////////// + +eap::method_pap::packet::packet() : + m_code((EapCode)0), + m_id(0) +{ +} + + +eap::method_pap::packet::packet(_In_ const packet &other) : + m_code(other.m_code), + m_id (other.m_id ), + m_data(other.m_data) +{ +} + + +eap::method_pap::packet::packet(_Inout_ packet &&other) : + m_code(std::move(other.m_code)), + m_id (std::move(other.m_id )), + m_data(std::move(other.m_data)) +{ +} + + +eap::method_pap::packet& eap::method_pap::packet::operator=(_In_ const packet &other) +{ + if (this != std::addressof(other)) { + m_code = other.m_code; + m_id = other.m_id ; + m_data = other.m_data; + } + + return *this; +} + + +eap::method_pap::packet& eap::method_pap::packet::operator=(_Inout_ packet &&other) +{ + if (this != std::addressof(other)) { + m_code = std::move(other.m_code); + m_id = std::move(other.m_id ); + m_data = std::move(other.m_data); + } + + return *this; +} + + +void eap::method_pap::packet::clear() +{ + m_code = (EapCode)0; + m_id = 0; + m_data.clear(); +} + + +////////////////////////////////////////////////////////////////////// +// eap::method_pap +////////////////////////////////////////////////////////////////////// + +eap::method_pap::method_pap(_In_ module &module, _In_ config_method_pap &cfg, _In_ credentials_pap &cred) : + m_cred(cred), + m_phase(phase_unknown), + m_phase_prev(phase_unknown), + method(module, cfg, cred) +{ +} + + +eap::method_pap::method_pap(_Inout_ method_pap &&other) : + m_cred ( other.m_cred ), + m_packet_res(std::move(other.m_packet_res)), + m_phase (std::move(other.m_phase )), + m_phase_prev(std::move(other.m_phase_prev)), + method (std::move(other )) +{ +} + + +eap::method_pap& eap::method_pap::operator=(_Inout_ method_pap &&other) +{ + if (this != std::addressof(other)) { + assert(std::addressof(m_cred) == std::addressof(other.m_cred)); // Move method with same credentials only! + (method&)*this = std::move(other ); + m_packet_res = std::move(other.m_packet_res); + m_phase = std::move(other.m_phase ); + m_phase_prev = std::move(other.m_phase_prev); + } + + return *this; +} + + +void eap::method_pap::process_request_packet( + _In_bytecount_(dwReceivedPacketSize) const EapPacket *pReceivedPacket, + _In_ DWORD dwReceivedPacketSize, + _Inout_ EapPeerMethodOutput *pEapOutput) +{ + assert(pReceivedPacket && dwReceivedPacketSize >= 4); + assert(pEapOutput); + + m_module.log_event(&EAPMETHOD_PACKET_RECV, event_data((unsigned int)eap_type_pap), event_data((unsigned int)dwReceivedPacketSize - 4), event_data::blank); + + if (pReceivedPacket->Id == 0) { + m_module.log_event(&EAPMETHOD_METHOD_HANDSHAKE_START2, event_data((unsigned int)eap_type_pap), event_data::blank); + m_phase = phase_init; + } + + m_phase_prev = m_phase; + switch (m_phase) { + case phase_init: { + // Convert username and password to UTF-8. + sanitizing_string identity_utf8, password_utf8; + WideCharToMultiByte(CP_UTF8, 0, m_cred.m_identity.c_str(), (int)m_cred.m_identity.length(), identity_utf8, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, m_cred.m_password.c_str(), (int)m_cred.m_password.length(), password_utf8, NULL, NULL); + + // PAP passwords must be padded to 16B boundary according to RFC 5281. Will not add random extra padding here, as length obfuscation should be done by outer transport layers. + size_t padding_password_ex = (16 - password_utf8.length()) % 16; + password_utf8.append(padding_password_ex, 0); + + size_t + size_identity = identity_utf8.length(), + size_password = password_utf8.length(), + padding_identity = (4 - size_identity ) % 4, + padding_password = (4 - password_utf8.length()) % 4, + size_identity_outer, + size_password_outer; + + m_packet_res.m_code = EapCodeResponse; + m_packet_res.m_id = pReceivedPacket->Id; + m_packet_res.m_data.clear(); + m_packet_res.m_data.reserve( + (size_identity_outer = + 4 + // Diameter AVP Code + 4 + // Diameter AVP Flags & Length + size_identity) + // Identity + padding_identity + // Identity padding + (size_password_outer = + 4 + // Diameter AVP Code + 4 + // Diameter AVP Flags & Length + size_password) + // Password + padding_password); // Password padding + + // Diameter AVP Code User-Name (0x00000001) + m_packet_res.m_data.push_back(0x00); + m_packet_res.m_data.push_back(0x00); + m_packet_res.m_data.push_back(0x00); + m_packet_res.m_data.push_back(0x01); + + // Diameter AVP Flags & Length + unsigned int identity_hdr = htonl((diameter_avp_flag_mandatory << 24) | (unsigned int)size_identity_outer); + m_packet_res.m_data.insert(m_packet_res.m_data.end(), (unsigned char*)&identity_hdr, (unsigned char*)(&identity_hdr + 1)); + + // Identity + m_packet_res.m_data.insert(m_packet_res.m_data.end(), identity_utf8.begin(), identity_utf8.end()); + m_packet_res.m_data.insert(m_packet_res.m_data.end(), padding_identity, 0); + + // Diameter AVP Code User-Password (0x00000002) + m_packet_res.m_data.push_back(0x00); + m_packet_res.m_data.push_back(0x00); + m_packet_res.m_data.push_back(0x00); + m_packet_res.m_data.push_back(0x02); + + // Diameter AVP Flags & Length + unsigned int password_hdr = htonl((diameter_avp_flag_mandatory << 24) | (unsigned int)size_password_outer); + m_packet_res.m_data.insert(m_packet_res.m_data.end(), (unsigned char*)&password_hdr, (unsigned char*)(&password_hdr + 1)); + + // Password + m_packet_res.m_data.insert(m_packet_res.m_data.end(), password_utf8.begin(), password_utf8.end()); + m_packet_res.m_data.insert(m_packet_res.m_data.end(), padding_password, 0); + + m_phase = phase_finished; + break; + } + + case phase_finished: + break; + } + + pEapOutput->fAllowNotifications = TRUE; + pEapOutput->action = EapPeerMethodResponseActionSend; +} + + +void eap::method_pap::get_response_packet( + _Inout_bytecap_(*dwSendPacketSize) EapPacket *pSendPacket, + _Inout_ DWORD *pdwSendPacketSize) +{ + assert(pdwSendPacketSize); + assert(pSendPacket); + + unsigned int + size_data = (unsigned int)m_packet_res.m_data.size(), + size_packet = size_data + 4; + unsigned short size_packet_limit = (unsigned short)std::min(*pdwSendPacketSize, USHRT_MAX); + + // Not fragmented. + if (size_packet <= size_packet_limit) { + // No need to fragment the packet. + m_module.log_event(&EAPMETHOD_PACKET_SEND, event_data((unsigned int)eap_type_pap), event_data((unsigned int)size_data), event_data::blank); + } else { + // But it should be fragmented. + throw com_runtime_error(TYPE_E_SIZETOOBIG, __FUNCTION__ " PAP message exceeds 64kB."); + } + + pSendPacket->Code = (BYTE)m_packet_res.m_code; + pSendPacket->Id = m_packet_res.m_id; + *(unsigned short*)pSendPacket->Length = htons((unsigned short)size_packet); + memcpy(pSendPacket->Data, m_packet_res.m_data.data(), size_data); + m_packet_res.m_data.erase(m_packet_res.m_data.begin(), m_packet_res.m_data.begin() + size_data); + *pdwSendPacketSize = size_packet; +} + + +void eap::method_pap::get_result( + _In_ EapPeerMethodResultReason reason, + _Inout_ EapPeerMethodResult *ppResult) +{ + assert(ppResult); + + switch (reason) { + case EapPeerMethodResultSuccess: { + m_module.log_event(&EAPMETHOD_METHOD_SUCCESS, event_data((unsigned int)eap_type_pap), event_data::blank); + m_cfg.m_auth_failed = false; + + ppResult->fIsSuccess = TRUE; + ppResult->dwFailureReasonCode = ERROR_SUCCESS; + + break; + } + + case EapPeerMethodResultFailure: + m_module.log_event( + m_phase_prev < phase_finished ? &EAPMETHOD_METHOD_FAILURE_INIT : &EAPMETHOD_METHOD_FAILURE, + event_data((unsigned int)eap_type_pap), event_data::blank); + + // Mark credentials as failed, so GUI can re-prompt user. + // But be careful: do so only after credentials were actually tried. + m_cfg.m_auth_failed = m_phase == phase_finished; + + // Do not report failure to EapHost, as it will not save updated configuration then. But we need it to save it, to alert user on next connection attempt. + // EapHost is well aware of the failed condition. + //ppResult->fIsSuccess = FALSE; + //ppResult->dwFailureReasonCode = EAP_E_AUTHENTICATION_FAILED; + + break; + + default: + throw win_runtime_error(ERROR_NOT_SUPPORTED, __FUNCTION__ " Not supported."); + } + + // Always ask EAP host to save the connection data. + ppResult->fSaveConnectionData = TRUE; +} diff --git a/lib/PAP/src/StdAfx.h b/lib/PAP/src/StdAfx.h index 65b8ccf..cc1d372 100644 --- a/lib/PAP/src/StdAfx.h +++ b/lib/PAP/src/StdAfx.h @@ -22,3 +22,7 @@ #include "../include/Config.h" #include "../include/Credentials.h" +#include "../include/Method.h" + +#include +#include // include after Windows.h diff --git a/lib/TLS/include/Method.h b/lib/TLS/include/Method.h index 95a17f0..e328473 100644 --- a/lib/TLS/include/Method.h +++ b/lib/TLS/include/Method.h @@ -146,10 +146,10 @@ namespace eap /// Constructs an EAP method /// /// \param[in] mod EAP module to use for global services - /// \param[in] cfg Connection configuration + /// \param[in] cfg Method configuration /// \param[in] cred User credentials /// - method_tls(_In_ module &module, _In_ config_connection &cfg, _In_ credentials_tls &cred); + method_tls(_In_ module &module, _In_ config_method_tls &cfg, _In_ credentials_tls &cred); /// /// Moves an EAP method @@ -158,11 +158,6 @@ namespace eap /// method_tls(_Inout_ method_tls &&other); - /// - /// Destructor - /// - virtual ~method_tls(); - /// /// Moves an EAP method /// @@ -490,6 +485,7 @@ namespace eap #endif protected: + config_method_tls &m_cfg; ///< EAP-TLS method configuration credentials_tls &m_cred; ///< EAP-TLS user credentials HANDLE m_user_ctx; ///< Handle to user context @@ -551,13 +547,5 @@ namespace eap phase_shutdown, ///< Connection shut down } m_phase, m_phase_prev; ///< What phase is our communication at? #endif - - // The following members are required to avoid memory leakage in get_result() - EAP_ATTRIBUTES m_eap_attr_desc; ///< EAP Radius attributes descriptor - std::vector m_eap_attr; ///< EAP Radius attributes - BYTE *m_blob_cfg; ///< Configuration BLOB -#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE - BYTE *m_blob_cred; ///< Credentials BLOB -#endif }; } diff --git a/lib/TLS/src/Method.cpp b/lib/TLS/src/Method.cpp index 2ea32c1..2388805 100644 --- a/lib/TLS/src/Method.cpp +++ b/lib/TLS/src/Method.cpp @@ -125,7 +125,8 @@ void eap::method_tls::packet::clear() // eap::method_tls ////////////////////////////////////////////////////////////////////// -eap::method_tls::method_tls(_In_ module &module, _In_ config_connection &cfg, _In_ credentials_tls &cred) : +eap::method_tls::method_tls(_In_ module &module, _In_ config_method_tls &cfg, _In_ credentials_tls &cred) : + m_cfg(cfg), m_cred(cred), m_user_ctx(NULL), #if EAP_TLS < EAP_TLS_SCHANNEL @@ -135,10 +136,6 @@ eap::method_tls::method_tls(_In_ module &module, _In_ config_connection &cfg, _I #else m_phase(phase_unknown), m_phase_prev(phase_unknown), -#endif - m_blob_cfg(NULL), -#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE - m_blob_cred(NULL), #endif method(module, cfg, cred) { @@ -153,6 +150,7 @@ eap::method_tls::method_tls(_In_ module &module, _In_ config_connection &cfg, _I eap::method_tls::method_tls(_Inout_ method_tls &&other) : m_cred ( other.m_cred ), + m_cfg ( other.m_cfg ), m_user_ctx (std::move(other.m_user_ctx )), m_packet_req (std::move(other.m_packet_req )), m_packet_res (std::move(other.m_packet_res )), @@ -199,18 +197,6 @@ eap::method_tls::method_tls(_Inout_ method_tls &&other) : } -eap::method_tls::~method_tls() -{ - if (m_blob_cfg) - m_module.free_memory(m_blob_cfg); - -#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE - if (m_blob_cred) - m_module.free_memory(m_blob_cred); -#endif -} - - eap::method_tls& eap::method_tls::operator=(_Inout_ method_tls &&other) { if (this != std::addressof(other)) { @@ -273,13 +259,6 @@ void eap::method_tls::begin_session( m_user_ctx = hTokenImpersonateUser; user_impersonator impersonating(m_user_ctx); - // Get method configuration. - if (m_cfg.m_providers.empty() || m_cfg.m_providers.front().m_methods.empty()) - throw invalid_argument(__FUNCTION__ " Configuration has no providers and/or methods."); - const config_provider &cfg_prov(m_cfg.m_providers.front()); - const config_method_tls *cfg_method = dynamic_cast(cfg_prov.m_methods.front().get()); - assert(cfg_method); - #if EAP_TLS < EAP_TLS_SCHANNEL // Create cryptographics provider for support needs (handshake hashing, client random, temporary keys...). if (!m_cp.create(NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) @@ -291,13 +270,13 @@ void eap::method_tls::begin_session( throw win_runtime_error(__FUNCTION__ " Error creating exponent-of-one key."); // Restore previous session ID and master secret. We might get lucky. - m_session_id = cfg_method->m_session_id; - m_master_secret = cfg_method->m_master_secret; + m_session_id = m_cfg.m_session_id; + m_master_secret = m_cfg.m_master_secret; #else // Build (expected) server name(s) for Schannel. m_sc_target_name.clear(); - for (list::const_iterator name = cfg_method->m_server_names.cbegin(), name_end = cfg_method->m_server_names.cend(); name != name_end; ++name) { - if (name != cfg_method->m_server_names.cbegin()) + for (list::const_iterator name = m_cfg.m_server_names.cbegin(), name_end = m_cfg.m_server_names.cend(); name != name_end; ++name) { + if (name != m_cfg.m_server_names.cbegin()) m_sc_target_name += _T(';'); #ifdef _UNICODE m_sc_target_name.insert(m_sc_target_name.end(), name->begin(), name->end()); @@ -311,30 +290,30 @@ void eap::method_tls::begin_session( // Prepare client credentials for Schannel. PCCERT_CONTEXT certs[] = { m_cred.m_cert ? m_cred.m_cert : NULL }; SCHANNEL_CRED cred = { - SCHANNEL_CRED_VERSION, // dwVersion - m_cred.m_cert ? 1 : 0, // cCreds - certs, // paCred - NULL, // hRootStore: Not valid for client credentials - 0, // cMappers - NULL, // aphMappers - 0, // cSupportedAlgs: Use system configured default - NULL, // palgSupportedAlgs: Use system configured default - 0, // grbitEnabledProtocols: Use default - 0, // dwMinimumCipherStrength: Use system configured default - 0, // dwMaximumCipherStrength: Use system configured default - 0, // dwSessionLifespan: Use system configured default = 10hr + SCHANNEL_CRED_VERSION, // dwVersion + m_cred.m_cert ? 1 : 0, // cCreds + certs, // paCred + NULL, // hRootStore: Not valid for client credentials + 0, // cMappers + NULL, // aphMappers + 0, // cSupportedAlgs: Use system configured default + NULL, // palgSupportedAlgs: Use system configured default + 0, // grbitEnabledProtocols: Use default + 0, // dwMinimumCipherStrength: Use system configured default + 0, // dwMaximumCipherStrength: Use system configured default + 0, // dwSessionLifespan: Use system configured default = 10hr #if EAP_TLS >= EAP_TLS_SCHANNEL_FULL - SCH_CRED_AUTO_CRED_VALIDATION | // dwFlags: Let Schannel verify server certificate + SCH_CRED_AUTO_CRED_VALIDATION | // dwFlags: Let Schannel verify server certificate #else - SCH_CRED_MANUAL_CRED_VALIDATION | // dwFlags: Prevent Schannel verify server certificate (we want to use custom root CA store and multiple name checking) + SCH_CRED_MANUAL_CRED_VALIDATION | // dwFlags: Prevent Schannel verify server certificate (we want to use custom root CA store and multiple name checking) #endif - SCH_CRED_CACHE_ONLY_URL_RETRIEVAL_ON_CREATE | // dwFlags: Do not attempt online revocation check - we do not expect to have network connection yet - SCH_CRED_IGNORE_NO_REVOCATION_CHECK | // dwFlags: Ignore no-revocation-check errors (TODO: Test if this flag is required.) - SCH_CRED_IGNORE_REVOCATION_OFFLINE | // dwFlags: Ignore offline-revocation errors - we do not expect to have network connection yet - SCH_CRED_NO_DEFAULT_CREDS | // dwFlags: If client certificate we provided is not acceptable, do not try to select one on your own - (cfg_method->m_server_names.empty() ? SCH_CRED_NO_SERVERNAME_CHECK : 0) | // dwFlags: When no expected server name is given, do not do the server name check. - 0x00400000 /*SCH_USE_STRONG_CRYPTO*/, // dwFlags: Do not use broken ciphers - 0 // dwCredFormat + SCH_CRED_CACHE_ONLY_URL_RETRIEVAL_ON_CREATE | // dwFlags: Do not attempt online revocation check - we do not expect to have network connection yet + SCH_CRED_IGNORE_NO_REVOCATION_CHECK | // dwFlags: Ignore no-revocation-check errors (TODO: Test if this flag is required.) + SCH_CRED_IGNORE_REVOCATION_OFFLINE | // dwFlags: Ignore offline-revocation errors - we do not expect to have network connection yet + SCH_CRED_NO_DEFAULT_CREDS | // dwFlags: If client certificate we provided is not acceptable, do not try to select one on your own + (m_cfg.m_server_names.empty() ? SCH_CRED_NO_SERVERNAME_CHECK : 0) | // dwFlags: When no expected server name is given, do not do the server name check. + 0x00400000 /*SCH_USE_STRONG_CRYPTO*/, // dwFlags: Do not use broken ciphers + 0 // dwCredFormat }; SECURITY_STATUS stat = m_sc_cred.acquire(NULL, UNISP_NAME, SECPKG_CRED_OUTBOUND, NULL, &cred); if (FAILED(stat)) @@ -378,14 +357,14 @@ void eap::method_tls::process_request_packet( // Preallocate data according to the Length field. size_t size_tot = ntohl(*(unsigned int*)(pReceivedPacket->Data + 2)); m_packet_req.m_data.reserve(size_tot); - m_module.log_event(&EAPMETHOD_TLS_PACKET_RECV_FRAG_FIRST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data((unsigned int)size_tot), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_RECV_FRAG_FIRST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data((unsigned int)size_tot), event_data::blank); } else { // The Length field was not included. Odd. Nevermind, no pre-allocation then. - m_module.log_event(&EAPMETHOD_TLS_PACKET_RECV_FRAG_FIRST1, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_RECV_FRAG_FIRST1, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data::blank); } } else { // Mid fragment received. - m_module.log_event(&EAPMETHOD_TLS_PACKET_RECV_FRAG_MID, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data((unsigned int)m_packet_req.m_data.size()), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_RECV_FRAG_MID, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data((unsigned int)m_packet_req.m_data.size()), event_data::blank); } m_packet_req.m_data.insert(m_packet_req.m_data.end(), packet_data_ptr, packet_data_ptr + packet_data_size); @@ -400,11 +379,11 @@ void eap::method_tls::process_request_packet( } else if (!m_packet_req.m_data.empty()) { // Last fragment received. Append data. m_packet_req.m_data.insert(m_packet_req.m_data.end(), packet_data_ptr, packet_data_ptr + packet_data_size); - m_module.log_event(&EAPMETHOD_TLS_PACKET_RECV_FRAG_LAST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data((unsigned int)m_packet_req.m_data.size()), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_RECV_FRAG_LAST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data((unsigned int)m_packet_req.m_data.size()), event_data::blank); } else { // This is a complete non-fragmented packet. m_packet_req.m_data.assign(packet_data_ptr, packet_data_ptr + packet_data_size); - m_module.log_event(&EAPMETHOD_TLS_PACKET_RECV, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_RECV, event_data((unsigned int)eap_type_tls), event_data((unsigned int)packet_data_size), event_data::blank); } m_packet_req.m_code = (EapCode)pReceivedPacket->Code; @@ -436,7 +415,7 @@ void eap::method_tls::process_request_packet( #if EAP_TLS < EAP_TLS_SCHANNEL if (pReceivedPacket->Code == EapCodeRequest && (m_packet_req.m_flags & flags_req_start)) { // This is the EAP-TLS start message: (re)initialize method. - m_module.log_event(&EAPMETHOD_TLS_HANDSHAKE_START2, event_data((unsigned int)eap_type_tls), event_data::blank); + m_module.log_event(&EAPMETHOD_METHOD_HANDSHAKE_START2, event_data((unsigned int)eap_type_tls), event_data::blank); m_phase = phase_client_hello; } else { // Process the packet. @@ -582,7 +561,7 @@ void eap::method_tls::process_request_packet( #else if (pReceivedPacket->Code == EapCodeRequest && (m_packet_req.m_flags & flags_req_start)) { // This is the EAP-TLS start message: (re)initialize method. - m_module.log_event(&EAPMETHOD_TLS_HANDSHAKE_START2, event_data((unsigned int)eap_type_tls), event_data::blank); + m_module.log_event(&EAPMETHOD_METHOD_HANDSHAKE_START2, event_data((unsigned int)eap_type_tls), event_data::blank); m_phase = phase_handshake_init; m_sc_queue.assign(m_packet_req.m_data.begin(), m_packet_req.m_data.end()); } else @@ -628,7 +607,7 @@ void eap::method_tls::get_response_packet( // No need to fragment the packet. m_packet_res.m_flags &= ~flags_res_length_incl; // No need to explicitly include the Length field either. data_dst = pSendPacket->Data + 2; - m_module.log_event(&EAPMETHOD_TLS_PACKET_SEND, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_SEND, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data::blank); } else { // But it should be fragmented. m_packet_res.m_flags |= flags_res_length_incl | flags_res_more_frag; @@ -636,7 +615,7 @@ void eap::method_tls::get_response_packet( data_dst = pSendPacket->Data + 6; size_data = size_packet_limit - 10; size_packet = size_packet_limit; - m_module.log_event(&EAPMETHOD_TLS_PACKET_SEND_FRAG_FIRST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data((unsigned int)(m_packet_res.m_data.size() - size_data)), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_SEND_FRAG_FIRST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data((unsigned int)(m_packet_res.m_data.size() - size_data)), event_data::blank); } } else { // Continuing the fragmented packet... @@ -645,11 +624,11 @@ void eap::method_tls::get_response_packet( m_packet_res.m_flags &= ~flags_res_length_incl; size_data = size_packet_limit - 6; size_packet = size_packet_limit; - m_module.log_event(&EAPMETHOD_TLS_PACKET_SEND_FRAG_MID, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data((unsigned int)(m_packet_res.m_data.size() - size_data)), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_SEND_FRAG_MID, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data((unsigned int)(m_packet_res.m_data.size() - size_data)), event_data::blank); } else { // This is the last fragment. m_packet_res.m_flags &= ~(flags_res_length_incl | flags_res_more_frag); - m_module.log_event(&EAPMETHOD_TLS_PACKET_SEND_FRAG_LAST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data((unsigned int)(m_packet_res.m_data.size() - size_data)), event_data::blank); + m_module.log_event(&EAPMETHOD_PACKET_SEND_FRAG_LAST, event_data((unsigned int)eap_type_tls), event_data((unsigned int)size_data), event_data((unsigned int)(m_packet_res.m_data.size() - size_data)), event_data::blank); } data_dst = pSendPacket->Data + 2; } @@ -671,13 +650,9 @@ void eap::method_tls::get_result( { assert(ppResult); - config_provider &cfg_prov(m_cfg.m_providers.front()); - config_method_tls *cfg_method = dynamic_cast(cfg_prov.m_methods.front().get()); - assert(cfg_method); - switch (reason) { case EapPeerMethodResultSuccess: { - m_module.log_event(&EAPMETHOD_TLS_SUCCESS, event_data((unsigned int)eap_type_tls), event_data::blank); + m_module.log_event(&EAPMETHOD_METHOD_SUCCESS, event_data((unsigned int)eap_type_tls), event_data::blank); #if EAP_TLS < EAP_TLS_SCHANNEL // Derive MSK/EMSK for line encryption. @@ -685,8 +660,7 @@ void eap::method_tls::get_result( // Fill array with RADIUS attributes. eap_attr a; - m_eap_attr.clear(); - m_eap_attr.reserve(3); + m_eap_attr.reserve(m_eap_attr.size() + 3); a.create_ms_mppe_key(16, (LPCBYTE)&m_key_mppe_client, sizeof(tls_random)); m_eap_attr.push_back(std::move(a)); a.create_ms_mppe_key(17, (LPCBYTE)&m_key_mppe_server, sizeof(tls_random)); @@ -702,8 +676,7 @@ void eap::method_tls::get_result( // Fill array with RADIUS attributes. eap_attr a; - m_eap_attr.clear(); - m_eap_attr.reserve(3); + m_eap_attr.reserve(m_eap_attr.size() + 3); a.create_ms_mppe_key(16, _key_block, sizeof(tls_random)); m_eap_attr.push_back(std::move(a)); _key_block += sizeof(tls_random); @@ -713,20 +686,16 @@ void eap::method_tls::get_result( m_eap_attr.push_back(eap_attr::blank); #endif - m_eap_attr_desc.dwNumberOfAttributes = (DWORD)m_eap_attr.size(); - m_eap_attr_desc.pAttribs = m_eap_attr.data(); - ppResult->pAttribArray = &m_eap_attr_desc; - // Clear credentials as failed. - cfg_method->m_auth_failed = false; + m_cfg.m_auth_failed = false; ppResult->fIsSuccess = TRUE; ppResult->dwFailureReasonCode = ERROR_SUCCESS; #if EAP_TLS < EAP_TLS_SCHANNEL // Update configuration with session resumption data and prepare BLOB. - cfg_method->m_session_id = m_session_id; - cfg_method->m_master_secret = m_master_secret; + m_cfg.m_session_id = m_session_id; + m_cfg.m_master_secret = m_master_secret; #endif break; @@ -734,19 +703,19 @@ void eap::method_tls::get_result( case EapPeerMethodResultFailure: m_module.log_event( - m_phase_prev < phase_handshake_cont ? &EAPMETHOD_TLS_FAILURE_INIT : - m_phase_prev < phase_application_data ? &EAPMETHOD_TLS_FAILURE_HANDSHAKE : &EAPMETHOD_TLS_FAILURE, + m_phase_prev < phase_handshake_cont ? &EAPMETHOD_METHOD_FAILURE_INIT : + m_phase_prev < phase_application_data ? &EAPMETHOD_METHOD_FAILURE_HANDSHAKE : &EAPMETHOD_METHOD_FAILURE, event_data((unsigned int)eap_type_tls), event_data::blank); #if EAP_TLS < EAP_TLS_SCHANNEL // Clear session resumption data. - cfg_method->m_session_id.clear(); - cfg_method->m_master_secret.clear(); + m_cfg.m_session_id.clear(); + m_cfg.m_master_secret.clear(); #endif // Mark credentials as failed, so GUI can re-prompt user. // But be careful: do so only if this happened after transition from handshake to application data phase. - cfg_method->m_auth_failed = m_phase_prev < phase_application_data && m_phase >= phase_application_data; + m_cfg.m_auth_failed = m_phase_prev < phase_application_data && m_phase >= phase_application_data; // Do not report failure to EapHost, as it will not save updated configuration then. But we need it to save it, to alert user on next connection attempt. // EapHost is well aware of the failed condition. @@ -761,18 +730,6 @@ void eap::method_tls::get_result( // Always ask EAP host to save the connection data. ppResult->fSaveConnectionData = TRUE; - m_module.pack(m_cfg, &ppResult->pConnectionData, &ppResult->dwSizeofConnectionData); - if (m_blob_cfg) - m_module.free_memory(m_blob_cfg); - m_blob_cfg = ppResult->pConnectionData; - -#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE - ppResult->fSaveUserData = TRUE; - m_module.pack(m_cred, &ppResult->pUserData, &ppResult->dwSizeofUserData); - if (m_blob_cred) - m_module.free_memory(m_blob_cred); - m_blob_cred = ppResult->pUserData; -#endif } @@ -1483,12 +1440,8 @@ void eap::method_tls::verify_server_trust() const throw sec_runtime_error(status, __FUNCTION__ " Error retrieving server certificate from Schannel."); #endif - const config_provider &cfg_prov(m_cfg.m_providers.front()); - const config_method_tls *cfg_method = dynamic_cast(cfg_prov.m_methods.front().get()); - assert(cfg_method); - // Check server name. - if (!cfg_method->m_server_names.empty()) { + if (!m_cfg.m_server_names.empty()) { bool has_san = false, found = false; @@ -1526,7 +1479,7 @@ void eap::method_tls::verify_server_trust() const } has_san = true; - for (list::const_iterator s = cfg_method->m_server_names.cbegin(), s_end = cfg_method->m_server_names.cend(); !found && s != s_end; ++s) { + for (list::const_iterator s = m_cfg.m_server_names.cbegin(), s_end = m_cfg.m_server_names.cend(); !found && s != s_end; ++s) { for (DWORD idx_entry = 0; !found && idx_entry < san_info->cAltEntry; idx_entry++) { if (san_info->rgAltEntry[idx_entry].dwAltNameChoice == CERT_ALT_NAME_DNS_NAME && _wcsicmp(s->c_str(), san_info->rgAltEntry[idx_entry].pwszDNSName) == 0) @@ -1544,7 +1497,7 @@ void eap::method_tls::verify_server_trust() const if (!CertGetNameStringW(cert, CERT_NAME_DNS_TYPE, CERT_NAME_STR_ENABLE_PUNYCODE_FLAG, NULL, subj)) throw win_runtime_error(__FUNCTION__ " Error retrieving server's certificate subject name."); - for (list::const_iterator s = cfg_method->m_server_names.cbegin(), s_end = cfg_method->m_server_names.cend(); !found && s != s_end; ++s) { + for (list::const_iterator s = m_cfg.m_server_names.cbegin(), s_end = m_cfg.m_server_names.cend(); !found && s != s_end; ++s) { if (_wcsicmp(s->c_str(), subj.c_str()) == 0) { m_module.log_event(&EAPMETHOD_TLS_SERVER_NAME_TRUSTED1, event_data(subj), event_data::blank); found = true; @@ -1564,7 +1517,7 @@ void eap::method_tls::verify_server_trust() const cert_store store; if (!store.create(CERT_STORE_PROV_MEMORY, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, NULL, 0, NULL)) throw win_runtime_error(__FUNCTION__ " Error creating temporary certificate store."); - for (list::const_iterator c = cfg_method->m_trusted_root_ca.cbegin(), c_end = cfg_method->m_trusted_root_ca.cend(); c != c_end; ++c) + for (list::const_iterator c = m_cfg.m_trusted_root_ca.cbegin(), c_end = m_cfg.m_trusted_root_ca.cend(); c != c_end; ++c) CertAddCertificateContextToStore(store, *c, CERT_STORE_ADD_REPLACE_EXISTING, NULL); // Add all intermediate certificates from the server's certificate chain. @@ -1636,7 +1589,7 @@ void eap::method_tls::verify_server_trust() const throw sec_runtime_error(SEC_E_CERT_UNKNOWN, __FUNCTION__ " Can not verify empty certificate chain."); PCCERT_CONTEXT cert_root = context->rgpChain[0]->rgpElement[context->rgpChain[0]->cElement-1]->pCertContext; - for (list::const_iterator c = cfg_method->m_trusted_root_ca.cbegin(), c_end = cfg_method->m_trusted_root_ca.cend();; ++c) { + for (list::const_iterator c = m_cfg.m_trusted_root_ca.cbegin(), c_end = m_cfg.m_trusted_root_ca.cend();; ++c) { if (c != c_end) { if (cert_root->cbCertEncoded == (*c)->cbCertEncoded && memcmp(cert_root->pbCertEncoded, (*c)->pbCertEncoded, cert_root->cbCertEncoded) == 0) diff --git a/lib/TTLS/include/Method.h b/lib/TTLS/include/Method.h index ea9a4cb..d4ec1f6 100644 --- a/lib/TTLS/include/Method.h +++ b/lib/TTLS/include/Method.h @@ -58,10 +58,10 @@ namespace eap /// Constructs an EAP method /// /// \param[in] mod EAP module to use for global services - /// \param[in] cfg Connection configuration + /// \param[in] cfg Method configuration /// \param[in] cred User credentials /// - method_ttls(_In_ module &module, _In_ config_connection &cfg, _In_ credentials_ttls &cred); + method_ttls(_In_ module &module, _In_ config_method_ttls &cfg, _In_ credentials_ttls &cred); /// /// Moves an EAP method @@ -82,6 +82,24 @@ namespace eap /// \name Packet processing /// @{ + /// + /// Starts an EAP authentication session on the peer EapHost using the EAP method. + /// + /// \sa [EapPeerBeginSession function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363600.aspx) + /// + virtual void begin_session( + _In_ DWORD dwFlags, + _In_ const EapAttributes *pAttributeArray, + _In_ HANDLE hTokenImpersonateUser, + _In_ DWORD dwMaxSendPacketSize); + + /// + /// Ends an EAP authentication session for the EAP method. + /// + /// \sa [EapPeerEndSession function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363604.aspx) + /// + virtual void end_session(); + /// /// Processes a packet received by EapHost from a supplicant. /// @@ -134,21 +152,17 @@ namespace eap #endif - /// - /// Makes a PAP client message - /// - /// \sa [Extensible Authentication Protocol Tunneled Transport Layer Security Authenticated Protocol Version 0 (EAP-TTLSv0) (Chapter 11.2.5. PAP)](https://tools.ietf.org/html/rfc5281#section-11.2.5) - /// - /// \returns PAP client message - /// - sanitizing_blob make_pap_client() const; - - public: - credentials_ttls &m_cred; ///< TTLS credentials + protected: + config_method_ttls &m_cfg; ///< EAP-TTLS method configuration + credentials_ttls &m_cred; ///< EAP-TTLS credentials #pragma warning(suppress: 4480) enum version_t :unsigned char { version_0 = 0, ///< EAP-TTLS v0 } m_version; ///< EAP-TTLS version + + std::unique_ptr m_inner; ///< Inner authentication method + unsigned char m_inner_packet_id; ///< Inner packet ID + DWORD m_size_inner_packet_max; ///< Maximum size of inner response packet }; } diff --git a/lib/TTLS/include/Module.h b/lib/TTLS/include/Module.h index a60472a..0668816 100644 --- a/lib/TTLS/include/Module.h +++ b/lib/TTLS/include/Module.h @@ -214,16 +214,21 @@ namespace eap protected: class session { public: - inline session(_In_ module &mod) : - m_cfg(mod), - m_cred(mod), - m_method(mod, m_cfg, m_cred) - {} + session(_In_ module &mod); + virtual ~session(); public: - config_connection m_cfg; ///< Connection configuration - credentials_ttls m_cred; ///< User credentials - method_ttls m_method; ///< EAP-TTLS method + module &m_module; ///< Module + config_connection m_cfg; ///< Connection configuration + credentials_ttls m_cred; ///< User credentials + std::unique_ptr m_method; ///< EAP-TTLS method + + // The following members are required to avoid memory leakage in get_result() + EAP_ATTRIBUTES m_eap_attr_desc; ///< EAP attributes descriptor + BYTE *m_blob_cfg; ///< Configuration BLOB +#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE + BYTE *m_blob_cred; ///< Credentials BLOB +#endif }; }; } diff --git a/lib/TTLS/src/Method.cpp b/lib/TTLS/src/Method.cpp index 903d304..0781b74 100644 --- a/lib/TTLS/src/Method.cpp +++ b/lib/TTLS/src/Method.cpp @@ -28,18 +28,25 @@ using namespace winstd; // eap::method_ttls ////////////////////////////////////////////////////////////////////// -eap::method_ttls::method_ttls(_In_ module &module, _In_ config_connection &cfg, _In_ credentials_ttls &cred) : +eap::method_ttls::method_ttls(_In_ module &module, _In_ config_method_ttls &cfg, _In_ credentials_ttls &cred) : + m_cfg(cfg), m_cred(cred), m_version(version_0), + m_inner_packet_id(0), + m_size_inner_packet_max(0), method_tls(module, cfg, cred) { } eap::method_ttls::method_ttls(_Inout_ method_ttls &&other) : - m_cred(other.m_cred), - m_version(std::move(other.m_version)), - method_tls(std::move(other)) + m_cfg ( other.m_cfg ), + m_cred ( other.m_cred ), + m_version (std::move(other.m_version )), + m_inner (std::move(other.m_inner )), + m_inner_packet_id (std::move(other.m_inner_packet_id )), + m_size_inner_packet_max(std::move(other.m_size_inner_packet_max)), + method_tls (std::move(other )) { } @@ -47,14 +54,42 @@ eap::method_ttls::method_ttls(_Inout_ method_ttls &&other) : eap::method_ttls& eap::method_ttls::operator=(_Inout_ method_ttls &&other) { if (this != std::addressof(other)) { - (method_tls&)*this = std::move(other); - m_version = std::move(other.m_version); + (method_tls&)*this = std::move(other ); + m_version = std::move(other.m_version ); + m_inner = std::move(other.m_inner ); + m_inner_packet_id = std::move(other.m_inner_packet_id ); + m_size_inner_packet_max = std::move(other.m_size_inner_packet_max); } return *this; } +void eap::method_ttls::begin_session( + _In_ DWORD dwFlags, + _In_ const EapAttributes *pAttributeArray, + _In_ HANDLE hTokenImpersonateUser, + _In_ DWORD dwMaxSendPacketSize) +{ + method_tls::begin_session(dwFlags, pAttributeArray, hTokenImpersonateUser, dwMaxSendPacketSize); + + // Initialize inner method. + switch (m_cfg.m_inner->get_method_id()) { + case eap_type_pap: m_inner.reset(new method_pap(m_module, (config_method_pap&)*m_cfg.m_inner, (credentials_pap&)*m_cred.m_inner.get())); + default: invalid_argument(__FUNCTION__ " Unsupported inner authentication method."); + } + m_inner->begin_session(dwFlags, pAttributeArray, hTokenImpersonateUser, m_size_inner_packet_max = dwMaxSendPacketSize); // TODO: Maximum inner packet size should have subtracted TLS overhead + m_inner_packet_id = 0; +} + + +void eap::method_ttls::end_session() +{ + m_inner->end_session(); + method_tls::end_session(); +} + + void eap::method_ttls::process_request_packet( _In_bytecount_(dwReceivedPacketSize) const EapPacket *pReceivedPacket, _In_ DWORD dwReceivedPacketSize, @@ -76,7 +111,7 @@ void eap::method_ttls::process_request_packet( if (m_phase == phase_application_data) { // Send inner authentication. if (!m_state_client.m_alg_encrypt) - throw runtime_error(__FUNCTION__ " Refusing to send credentials unencrypted."); + throw runtime_error(__FUNCTION__ " Refusing to continue with inner authentication unencrypted."); m_module.log_event(&EAPMETHOD_TTLS_INNER_CRED, event_data((unsigned int)eap_type_ttls), event_data(m_cred.m_inner->get_name()), event_data::blank); @@ -111,29 +146,12 @@ void eap::method_ttls::get_result( // Do the TLS. method_tls::get_result(reason, ppResult); } else { - // The TLS finished, this is inner authentication's bussines. - config_provider &cfg_prov(m_cfg.m_providers.front()); - config_method_ttls *cfg_method = dynamic_cast(cfg_prov.m_methods.front().get()); - assert(cfg_method); + // Get inner method result. + EapPeerMethodResult result = {}; + m_inner->get_result(reason, &result); - switch (reason) { - case EapPeerMethodResultSuccess: { - m_module.log_event(&EAPMETHOD_TTLS_INNER_SUCCESS, event_data((unsigned int)eap_type_ttls), event_data::blank); - cfg_method->m_inner->m_auth_failed = false; - break; - } - - case EapPeerMethodResultFailure: - m_module.log_event(&EAPMETHOD_TTLS_INNER_FAILURE, event_data((unsigned int)eap_type_ttls), event_data::blank); - - // Mark credentials as failed, so GUI can re-prompt user. - // But be careful: do so only if this happened after transition from handshake to application data phase. - cfg_method->m_inner->m_auth_failed = m_phase_prev < phase_application_data; - break; - - default: - throw win_runtime_error(ERROR_NOT_SUPPORTED, __FUNCTION__ " Not supported."); - } + if (result.fSaveConnectionData) + ppResult->fSaveConnectionData = TRUE; #if EAP_TLS >= EAP_TLS_SCHANNEL // EAP-TTLS uses different label in PRF for MSK derivation than EAP-TLS. @@ -143,8 +161,6 @@ void eap::method_ttls::get_result( if (FAILED(status)) throw sec_runtime_error(status, __FUNCTION__ "Error setting EAP-TTLS PRF in Schannel."); #endif - - // The TLS was OK. method_tls::get_result(EapPeerMethodResultSuccess, ppResult); // Do not report failure to EapHost, as it will not save updated configuration then. But we need it to save it, to alert user on next connection attempt. @@ -196,117 +212,76 @@ void eap::method_ttls::derive_msk() void eap::method_ttls::process_application_data(_In_bytecount_(size_msg) const void *msg, _In_ size_t size_msg) { - UNREFERENCED_PARAMETER(msg); - UNREFERENCED_PARAMETER(size_msg); - // Prepare inner authentication. if (!(m_sc_ctx.m_attrib & ISC_RET_CONFIDENTIALITY)) - throw runtime_error(__FUNCTION__ " Refusing to send credentials unencrypted."); + throw runtime_error(__FUNCTION__ " Refusing to continue with inner authentication unencrypted."); - m_module.log_event(&EAPMETHOD_TTLS_INNER_CRED, event_data((unsigned int)eap_type_ttls), event_data(m_cred.m_inner->get_name()), event_data::blank); + EapPeerMethodOutput eap_output = {}; + eap_type_t eap_type = m_cfg.m_inner->get_method_id(); + if (eap_type_noneap_start <= eap_type && eap_type < eap_type_noneap_end) { + // Inner method is natively non-EAP. Server sent raw data, but all our eap::method derived classes expect EAP encapsulated. + // Encapsulate in an EAP packet. + assert(size_msg < 0xffff); + unsigned short size_packet = (unsigned short)size_msg + 4; + sanitizing_blob packet; + packet.reserve(size_packet); + packet.push_back(EapCodeRequest); + packet.push_back(m_inner_packet_id++); + unsigned short size2 = htons(size_packet); + packet.insert(packet.end(), (unsigned char*)&size2, (unsigned char*)(&size2 + 1)); + packet.insert(packet.end(), (unsigned char*)msg, (unsigned char*)msg + size_msg); + m_inner->process_request_packet((const EapPacket*)packet.data(), size_packet, &eap_output); + } else { + // Inner packet is EAP-aware. + m_inner->process_request_packet((const EapPacket*)msg, (DWORD)size_msg, &eap_output); + } - SECURITY_STATUS status; + switch (eap_output.action) { + case EapPeerMethodResponseActionSend: { + // Retrieve inner packet and send it. + SECURITY_STATUS status; - // Get maximum message sizes. - SecPkgContext_StreamSizes sizes; - status = QueryContextAttributes(m_sc_ctx, SECPKG_ATTR_STREAM_SIZES, &sizes); - if (FAILED(status)) - throw sec_runtime_error(status, __FUNCTION__ " Error getting Schannel required encryption sizes."); + // Get maximum message sizes. + SecPkgContext_StreamSizes sizes; + status = QueryContextAttributes(m_sc_ctx, SECPKG_ATTR_STREAM_SIZES, &sizes); + if (FAILED(status)) + throw sec_runtime_error(status, __FUNCTION__ " Error getting Schannel required encryption sizes."); - // Make PAP message. - sanitizing_blob msg_pap(make_pap_client()); - assert(msg_pap.size() < sizes.cbMaximumMessage); - unsigned long size_data = std::min(sizes.cbMaximumMessage, (unsigned long)msg_pap.size()); // Truncate + sanitizing_blob data(sizes.cbHeader + m_size_inner_packet_max + sizes.cbTrailer, 0); + DWORD size_data = m_size_inner_packet_max; + unsigned char *ptr_data = data.data() + sizes.cbHeader; + m_inner->get_response_packet((EapPacket*)ptr_data, &size_data); - sanitizing_blob data(sizes.cbHeader + size_data + sizes.cbTrailer, 0); - memcpy(data.data() + sizes.cbHeader, msg_pap.data(), size_data); + if (eap_type_noneap_start <= eap_type && eap_type < eap_type_noneap_end) { + // Inner method is non-EAP. Strip EAP header, since server expect raw data. + memmove(ptr_data, ptr_data + 4, size_data -= 4); + } - // Prepare input/output buffer(s). - SecBuffer buf[] = { - { sizes.cbHeader, SECBUFFER_STREAM_HEADER , data.data() }, - { size_data, SECBUFFER_DATA , data.data() + sizes.cbHeader }, - { sizes.cbTrailer, SECBUFFER_STREAM_TRAILER, data.data() + sizes.cbHeader + size_data }, - { 0, SECBUFFER_EMPTY , NULL }, - }; - SecBufferDesc buf_desc = { - SECBUFFER_VERSION, - _countof(buf), - buf - }; + // Prepare input/output buffer(s). + SecBuffer buf[] = { + { sizes.cbHeader, SECBUFFER_STREAM_HEADER , data.data() }, + { size_data, SECBUFFER_DATA , ptr_data }, + { sizes.cbTrailer, SECBUFFER_STREAM_TRAILER, ptr_data + size_data }, + { 0, SECBUFFER_EMPTY , NULL }, + }; + SecBufferDesc buf_desc = { + SECBUFFER_VERSION, + _countof(buf), + buf + }; - // Encrypt the message. - status = EncryptMessage(m_sc_ctx, 0, &buf_desc, 0); - if (FAILED(status)) - throw sec_runtime_error(status, __FUNCTION__ " Error encrypting message."); - m_packet_res.m_data.insert(m_packet_res.m_data.end(), (const unsigned char*)buf[0].pvBuffer, (const unsigned char*)buf[0].pvBuffer + buf[0].cbBuffer + buf[1].cbBuffer + buf[2].cbBuffer); + // Encrypt the message. + status = EncryptMessage(m_sc_ctx, 0, &buf_desc, 0); + if (FAILED(status)) + throw sec_runtime_error(status, __FUNCTION__ " Error encrypting message."); + m_packet_res.m_data.insert(m_packet_res.m_data.end(), (const unsigned char*)buf[0].pvBuffer, (const unsigned char*)buf[0].pvBuffer + buf[0].cbBuffer + buf[1].cbBuffer + buf[2].cbBuffer); + + break; + } + + default: + throw invalid_argument(string_printf(__FUNCTION__ " Inner method returned an unsupported action (action %u).", eap_output.action).c_str()); + } } #endif - - -eap::sanitizing_blob eap::method_ttls::make_pap_client() const -{ - const credentials_pap *cred = dynamic_cast(m_cred.m_inner.get()); - if (!cred) - throw invalid_argument(__FUNCTION__ " Inner credentials missing or not PAP."); - - // Convert username and password to UTF-8. - sanitizing_string identity_utf8, password_utf8; - WideCharToMultiByte(CP_UTF8, 0, cred->m_identity.c_str(), (int)cred->m_identity.length(), identity_utf8, NULL, NULL); - WideCharToMultiByte(CP_UTF8, 0, cred->m_password.c_str(), (int)cred->m_password.length(), password_utf8, NULL, NULL); - - // PAP passwords must be padded to 16B boundary according to RFC 5281. Will not add random extra padding here, as length obfuscation should be done by TLS encryption layer. - size_t padding_password_ex = (16 - password_utf8.length()) % 16; - password_utf8.append(padding_password_ex, 0); - - size_t - size_identity = identity_utf8.length(), - size_password = password_utf8.length(), - padding_identity = (4 - size_identity ) % 4, - padding_password = (4 - password_utf8.length()) % 4, - size_identity_outer, - size_password_outer; - - sanitizing_blob msg; - msg.reserve( - (size_identity_outer = - 4 + // Diameter AVP Code - 4 + // Diameter AVP Flags & Length - size_identity) + // Identity - padding_identity + // Identity padding - (size_password_outer = - 4 + // Diameter AVP Code - 4 + // Diameter AVP Flags & Length - size_password) + // Password - padding_password); // Password padding - - // Diameter AVP Code User-Name (0x00000001) - msg.push_back(0x00); - msg.push_back(0x00); - msg.push_back(0x00); - msg.push_back(0x01); - - // Diameter AVP Flags & Length - unsigned int identity_hdr = htonl((diameter_avp_flag_mandatory << 24) | (unsigned int)size_identity_outer); - msg.insert(msg.end(), (unsigned char*)&identity_hdr, (unsigned char*)(&identity_hdr + 1)); - - // Identity - msg.insert(msg.end(), identity_utf8.begin(), identity_utf8.end()); - msg.insert(msg.end(), padding_identity, 0); - - // Diameter AVP Code User-Password (0x00000002) - msg.push_back(0x00); - msg.push_back(0x00); - msg.push_back(0x00); - msg.push_back(0x02); - - // Diameter AVP Flags & Length - unsigned int password_hdr = htonl((diameter_avp_flag_mandatory << 24) | (unsigned int)size_password_outer); - msg.insert(msg.end(), (unsigned char*)&password_hdr, (unsigned char*)(&password_hdr + 1)); - - // Password - msg.insert(msg.end(), password_utf8.begin(), password_utf8.end()); - msg.insert(msg.end(), padding_password, 0); - - return msg; -} diff --git a/lib/TTLS/src/Module.cpp b/lib/TTLS/src/Module.cpp index 898e27c..43d023b 100644 --- a/lib/TTLS/src/Module.cpp +++ b/lib/TTLS/src/Module.cpp @@ -271,16 +271,19 @@ EAP_SESSION_HANDLE eap::peer_ttls::begin_session( // Get method configuration. if (s->m_cfg.m_providers.empty() || s->m_cfg.m_providers.front().m_methods.empty()) throw invalid_argument(__FUNCTION__ " Configuration has no providers and/or methods."); - const config_provider &cfg_prov(s->m_cfg.m_providers.front()); - const config_method_ttls *cfg_method = dynamic_cast(cfg_prov.m_methods.front().get()); + config_provider &cfg_prov(s->m_cfg.m_providers.front()); + config_method_ttls *cfg_method = dynamic_cast(cfg_prov.m_methods.front().get()); assert(cfg_method); // Unpack credentials. s->m_cred.m_inner.reset(cfg_method->m_inner->make_credentials()); unpack(s->m_cred, pUserData, dwUserDataSize); + // We have configuration, we have credentials, create method. + s->m_method.reset(new method_ttls(*this, *cfg_method, s->m_cred)); + // Initialize method. - s->m_method.begin_session(dwFlags, pAttributeArray, hTokenImpersonateUser, dwMaxSendPacketSize); + s->m_method->begin_session(dwFlags, pAttributeArray, hTokenImpersonateUser, dwMaxSendPacketSize); return s.release(); } @@ -304,7 +307,7 @@ void eap::peer_ttls::process_request_packet( _Inout_ EapPeerMethodOutput *pEapOutput) { assert(dwReceivedPacketSize == ntohs(*(WORD*)pReceivedPacket->Length)); - static_cast(hSession)->m_method.process_request_packet(pReceivedPacket, dwReceivedPacketSize, pEapOutput); + static_cast(hSession)->m_method->process_request_packet(pReceivedPacket, dwReceivedPacketSize, pEapOutput); } @@ -313,7 +316,7 @@ void eap::peer_ttls::get_response_packet( _Inout_bytecap_(*dwSendPacketSize) EapPacket *pSendPacket, _Inout_ DWORD *pdwSendPacketSize) { - static_cast(hSession)->m_method.get_response_packet(pSendPacket, pdwSendPacketSize); + static_cast(hSession)->m_method->get_response_packet(pSendPacket, pdwSendPacketSize); } @@ -322,7 +325,27 @@ void eap::peer_ttls::get_result( _In_ EapPeerMethodResultReason reason, _Inout_ EapPeerMethodResult *ppResult) { - static_cast(hSession)->m_method.get_result(reason, ppResult); + session *s = static_cast(hSession); + + s->m_method->get_result(reason, ppResult); + s->m_eap_attr_desc.dwNumberOfAttributes = (DWORD)s->m_method->m_eap_attr.size(); + s->m_eap_attr_desc.pAttribs = s->m_method->m_eap_attr.data(); + ppResult->pAttribArray = &s->m_eap_attr_desc; + + if (ppResult->fSaveConnectionData) { + pack(s->m_cfg, &ppResult->pConnectionData, &ppResult->dwSizeofConnectionData); + if (s->m_blob_cfg) + free_memory(s->m_blob_cfg); + s->m_blob_cfg = ppResult->pConnectionData; + } + +#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE + ppResult->fSaveUserData = TRUE; + pack(s->m_cred, &ppResult->pUserData, &ppResult->dwSizeofUserData); + if (s->m_blob_cred) + free_memory(s->m_blob_cred); + s->m_blob_cred = ppResult->pUserData; +#endif } @@ -376,3 +399,30 @@ void eap::peer_ttls::set_response_attributes( throw win_runtime_error(ERROR_NOT_SUPPORTED, __FUNCTION__ " Not supported."); } + + +////////////////////////////////////////////////////////////////////// +// eap::peer_ttls::session +////////////////////////////////////////////////////////////////////// + +eap::peer_ttls::session::session(_In_ module &mod) : + m_module(mod), + m_cfg(mod), + m_cred(mod), + m_blob_cfg(NULL) +#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE + , m_blob_cred(NULL) +#endif +{} + + +eap::peer_ttls::session::~session() +{ + if (m_blob_cfg) + m_module.free_memory(m_blob_cfg); + +#ifdef EAP_USE_NATIVE_CREDENTIAL_CACHE + if (m_blob_cred) + m_module.free_memory(m_blob_cred); +#endif +} diff --git a/lib/TTLS/src/StdAfx.h b/lib/TTLS/src/StdAfx.h index daa33fe..5a8b430 100644 --- a/lib/TTLS/src/StdAfx.h +++ b/lib/TTLS/src/StdAfx.h @@ -27,6 +27,7 @@ #include "../../PAP/include/Config.h" #include "../../PAP/include/Credentials.h" +#include "../../PAP/include/Method.h" #include "../../EAPBase/include/EAPXML.h"