Development continues...

This commit is contained in:
2016-05-19 23:39:02 +02:00
parent aef4f36f45
commit 61fa67a415
10 changed files with 876 additions and 125 deletions

View File

@@ -20,94 +20,40 @@
#include <StdAfx.h>
using namespace std;
using namespace winstd;
#pragma comment(lib, "Comctl32.lib")
#pragma comment(lib, "Shlwapi.lib")
#if EAPMETHOD_TYPE==21
#define _EAPMETHOD_PEER eap::peer_ttls
#else
#error Unknown EAP Method type.
#endif
#define ETW_FN_VOID event_fn_auto < &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN > _event_auto(*g_ep, __FUNCTION__)
#define ETW_FN_DWORD(res) event_fn_auto_ret<DWORD , &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN_DWORD > _event_auto(*g_ep, __FUNCTION__, res)
#define ETW_FN_HRESULT(res) event_fn_auto_ret<HRESULT, &EAPMETHOD_TRACE_EVT_FN_CALL, &EAPMETHOD_TRACE_EVT_FN_RETURN_HRESULT> _event_auto(*g_ep, __FUNCTION__, res)
event_provider *g_ep = NULL;
// event_fn_auto actually and winstd::event_auto_res<> do not need an assignment operator actually, so the C4512 warning is safely ignored.
#pragma warning(push)
#pragma warning(disable: 4512)
///
/// Helper class to write an event on entry/exit of scope.
///
/// It writes one string event at creation and another at destruction.
///
template <const EVENT_DESCRIPTOR *event_cons, const EVENT_DESCRIPTOR *event_dest>
class event_fn_auto
{
public:
inline event_fn_auto(_In_ event_provider &ep, _In_z_ LPCSTR pszFnName) : m_ep(ep)
{
EventDataDescCreate(&m_fn_name, pszFnName, (ULONG)(strlen(pszFnName) + 1)*sizeof(*pszFnName));
m_ep.write(event_cons, 1, &m_fn_name);
}
inline ~event_fn_auto()
{
m_ep.write(event_dest, 1, &m_fn_name);
}
protected:
event_provider &m_ep; ///< Reference to event provider in use
EVENT_DATA_DESCRIPTOR m_fn_name; ///< Function name
};
_EAPMETHOD_PEER g_peer;
///
/// Helper template to write an event on entry/exit of scope with one parameter (typically result).
/// DLL main entry point
///
/// It writes one string event at creation and another at destruction, with allowing one sprintf type parameter for string event at destruction.
/// \sa [DllMain entry point](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx)
///
template<class T, const EVENT_DESCRIPTOR *event_cons, const EVENT_DESCRIPTOR *event_dest>
class event_fn_auto_ret
{
public:
inline event_fn_auto_ret(_In_ event_provider &ep, _In_z_ LPCSTR pszFnName, T &result) : m_ep(ep), m_result(result)
{
EventDataDescCreate(m_desc + 0, pszFnName, (ULONG)(strlen(pszFnName) + 1)*sizeof(*pszFnName));
m_ep.write(event_cons, 1, m_desc);
}
inline ~event_fn_auto_ret()
{
EventDataDescCreate(m_desc + 1, &m_result, sizeof(T));
m_ep.write(event_dest, 2, m_desc);
}
protected:
event_provider &m_ep; ///< Reference to event provider in use
T &m_result; ///< Function result
EVENT_DATA_DESCRIPTOR m_desc[2]; ///< Function name and return value
};
#pragma warning(pop)
BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved)
{
UNREFERENCED_PARAMETER(hinstDLL);
UNREFERENCED_PARAMETER(lpvReserved);
if (fdwReason == DLL_PROCESS_ATTACH) {
g_ep = new event_provider();
assert(g_ep);
g_ep->create(&EAPMETHOD_TRACE_EVENT_PROVIDER);
g_ep->write(&EAPMETHOD_TRACE_EVT_MODULE_LOAD, event_data((unsigned int)EAPMETHOD_TYPE), event_data::blank);
} else if (fdwReason == DLL_PROCESS_DETACH) {
assert(g_ep);
g_ep->write(&EAPMETHOD_TRACE_EVT_MODULE_UNLOAD, event_data((unsigned int)EAPMETHOD_TYPE), event_data::blank);
delete g_ep;
//// Load EAPMethodEvents.dll
//tstring path;
//GetModuleFileName(hinstDLL, path);
//LPTSTR pszFilename = PathFindFileName(path.c_str());
//path.resize(pszFilename ? (size_t)(pszFilename - (LPCTSTR)path.c_str()) : 0);
//path += _T("EAPMethodEvents.dll");
//g_EAPMethodEvents.load(path.c_str(), NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if (!g_peer.create())
return FALSE;
} else if (fdwReason == DLL_PROCESS_DETACH) {
assert(!_CrtDumpMemoryLeaks());
}
@@ -115,66 +61,183 @@ BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID l
}
//#pragma comment(linker, "/EXPORT:DllRegisterServer,PRIVATE")
//#pragma comment(linker, "/EXPORT:DllUnregisterServer,PRIVATE")
extern "C"
///
/// Releases all memory associated with an opaque user interface context data buffer.
///
/// \sa [EapPeerFreeMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363606.aspx)
///
VOID WINAPI EapPeerFreeMemory(_In_ void *pUIContextData)
{
/////
///// Registers the EAP method.
/////
//HRESULT STDAPICALLTYPE DllRegisterServer()
//{
// HRESULT hr = S_OK;
// ETW_FN_HRESULT(hr);
g_peer.free_memory(pUIContextData);
}
// return hr;
//}
#pragma comment(linker, "/EXPORT:EapPeerFreeMemory")
/////
///// Unregisters the EAP method.
/////
//HRESULT STDAPICALLTYPE DllUnregisterServer()
//{
// HRESULT hr = S_OK;
// ETW_FN_HRESULT(hr);
///
/// Releases error-specific memory allocated by the EAP peer method.
///
/// \sa [EapPeerFreeErrorMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363605.aspx)
///
VOID WINAPI EapPeerFreeErrorMemory(_In_ EAP_ERROR *ppEapError)
{
g_peer.free_error_memory(ppEapError);
}
// return hr;
//}
#pragma comment(linker, "/EXPORT:EapPeerFreeErrorMemory")
///
/// Releases all memory associated with an opaque user interface context data buffer.
///
/// \sa [EapPeerFreeMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363606.aspx)
///
__declspec(dllexport) VOID WINAPI EapPeerFreeMemory(_In_ void *pUIContextData)
{
ETW_FN_VOID;
///
/// Obtains a set of function pointers for an implementation of the EAP peer method currently loaded on the EAPHost service.
///
/// \sa [EapPeerGetInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363608.aspx)
///
DWORD WINAPI EapPeerGetInfo(_In_ EAP_TYPE* pEapType, _Out_ EAP_PEER_METHOD_ROUTINES* pEapPeerMethodRoutines, _Out_ EAP_ERROR **ppEapError)
{
DWORD dwResult = NO_ERROR;
#ifdef _DEBUG
//Sleep(10000);
#endif
if (pUIContextData) {
// Since we do security here and some of the BLOBs contain credentials, sanitize every memory block before freeing.
HANDLE hHeap = GetProcessHeap();
SecureZeroMemory(pUIContextData, HeapSize(hHeap, 0, pUIContextData));
HeapFree(hHeap, 0, pUIContextData);
}
// Parameter check
if (!ppEapError) {
dwResult = ERROR_INVALID_PARAMETER;
} else if (!pEapType) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pEapType is NULL."), NULL);
} else if (pEapType->type != EAPMETHOD_TYPE) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" Input EAP type (%d) does not match the supported EAP type (%d)."), (int)pEapType->type, (int)EAPMETHOD_TYPE).c_str(), NULL);
} else if (!pEapPeerMethodRoutines) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pEapPeerMethodRoutines is NULL."), NULL);
} else
g_peer.get_info(pEapPeerMethodRoutines);
return dwResult;
}
#pragma comment(linker, "/EXPORT:EapPeerGetInfo")
///
/// Raises the EAP method's specific connection configuration user interface dialog on the client.
///
/// \sa [EapPeerInvokeConfigUI function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363614.aspx)
///
DWORD WINAPI EapPeerInvokeConfigUI(_In_ EAP_METHOD_TYPE *pEapType, _In_ HWND hwndParent, _In_ DWORD dwFlags, _In_ DWORD dwSizeOfConnectionDataIn, _In_ BYTE *pConnectionDataIn, _Out_ DWORD *pdwSizeOfConnectionDataOut, _Out_ BYTE **ppConnectionDataOut, _Out_ EAP_ERROR **ppEapError)
{
DWORD dwResult = NO_ERROR;
#ifdef _DEBUG
//MessageBox(NULL, _T("Attach debugger!"), _T(__FUNCTION__), MB_OK);
#endif
// Parameter check
if (!ppEapError) {
dwResult = ERROR_INVALID_PARAMETER;
} else if (!pEapType) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pEapType is NULL."), NULL);
} else if (pEapType->eapType.type != EAPMETHOD_TYPE) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" Input EAP type (%d) does not match the supported EAP type (%d)."), (int)pEapType->eapType.type, (int)EAPMETHOD_TYPE).c_str(), NULL);
} else if (pEapType->dwAuthorId != 67532) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" EAP author (%d) does not match the supported author (%d)."), (int)pEapType->dwAuthorId, (int)67532).c_str(), NULL);
} else if (!pdwSizeOfConnectionDataOut) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pdwSizeOfConnectionDataOut is NULL."), NULL);
} else if (!ppConnectionDataOut) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" ppConnectionDataOut is NULL."), NULL);
} else {
UNREFERENCED_PARAMETER(dwFlags);
UNREFERENCED_PARAMETER(dwSizeOfConnectionDataIn);
UNREFERENCED_PARAMETER(pConnectionDataIn);
InitCommonControls();
MessageBox(hwndParent, _T(PRODUCT_NAME_STR) _T(" configuration goes here!"), _T(PRODUCT_NAME_STR) _T(" Settings"), MB_OK);
}
return dwResult;
}
///
/// Releases error-specific memory allocated by the EAP peer method.
///
/// \sa [EapPeerFreeErrorMemory function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363605.aspx)
///
__declspec(dllexport) VOID WINAPI EapPeerFreeErrorMemory(_In_ EAP_ERROR *ppEapError)
{
ETW_FN_VOID;
#pragma comment(linker, "/EXPORT:EapPeerInvokeConfigUI")
if (ppEapError) {
// pRootCauseString and pRepairString always trail the ppEapError to reduce number of (de)allocations.
HeapFree(GetProcessHeap(), 0, ppEapError);
}
///
/// Raises a custom interactive user interface dialog to obtain user identity information for the EAP method on the client.
///
/// \sa [EapPeerInvokeIdentityUI function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363615.aspx)
///
DWORD WINAPI EapPeerInvokeIdentityUI(_In_ EAP_METHOD_TYPE *pEapType, _In_ DWORD dwFlags, _In_ HWND hwndParent, _In_ DWORD dwSizeOfConnectionData, _In_ const BYTE *pConnectionData, _Out_ DWORD dwSizeOfUserData, _In_ const BYTE *pUserData, _Out_ DWORD *pdwSizeOfUserDataOut, _Out_ BYTE **ppUserDataOut, _Out_ LPWSTR *ppwszIdentity, _Out_ EAP_ERROR **ppEapError)
{
DWORD dwResult = NO_ERROR;
#ifdef _DEBUG
//MessageBox(NULL, _T("Attach debugger!"), _T(__FUNCTION__), MB_OK);
#endif
// Parameter check
if (!ppEapError) {
dwResult = ERROR_INVALID_PARAMETER;
} else if (!pEapType) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pEapType is NULL."), NULL);
} else if (pEapType->eapType.type != EAPMETHOD_TYPE) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" Input EAP type (%d) does not match the supported EAP type (%d)."), (int)pEapType->eapType.type, (int)EAPMETHOD_TYPE).c_str(), NULL);
} else if (pEapType->dwAuthorId != 67532) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" EAP author (%d) does not match the supported author (%d)."), (int)pEapType->dwAuthorId, (int)67532).c_str(), NULL);
} else if (!pdwSizeOfUserDataOut) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pdwSizeOfUserDataOut is NULL."), NULL);
} else if (!ppUserDataOut) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" ppUserDataOut is NULL."), NULL);
} else if (!ppwszIdentity) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" ppwszIdentity is NULL."), NULL);
} else {
UNREFERENCED_PARAMETER(dwFlags);
UNREFERENCED_PARAMETER(pUserData);
UNREFERENCED_PARAMETER(dwSizeOfUserData);
UNREFERENCED_PARAMETER(pConnectionData);
UNREFERENCED_PARAMETER(dwSizeOfConnectionData);
InitCommonControls();
MessageBox(hwndParent, _T(PRODUCT_NAME_STR) _T(" credential prompt goes here!"), _T(PRODUCT_NAME_STR) _T(" Credentials"), MB_OK);
}
};
return dwResult;
}
#pragma comment(linker, "/EXPORT:EapPeerInvokeIdentityUI")
///
/// Raises a custom interactive user interface dialog for the EAP method on the client.
///
/// \sa [EapPeerInvokeInteractiveUI function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363616.aspx)
///
DWORD WINAPI EapPeerInvokeInteractiveUI(_In_ EAP_METHOD_TYPE *pEapType, _In_ HWND hwndParent, _In_ DWORD dwSizeofUIContextData, _In_ BYTE *pUIContextData, _Out_ DWORD *pdwSizeOfDataFromInteractiveUI, _Out_ BYTE **ppDataFromInteractiveUI, _Out_ EAP_ERROR **ppEapError)
{
DWORD dwResult = NO_ERROR;
#ifdef _DEBUG
//MessageBox(NULL, _T("Attach debugger!"), _T(__FUNCTION__), MB_OK);
#endif
// Parameter check
if (!ppEapError) {
dwResult = ERROR_INVALID_PARAMETER;
} else if (!pEapType) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pEapType is NULL."), NULL);
} else if (pEapType->eapType.type != EAPMETHOD_TYPE) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" Input EAP type (%d) does not match the supported EAP type (%d)."), (int)pEapType->eapType.type, (int)EAPMETHOD_TYPE).c_str(), NULL);
} else if (pEapType->dwAuthorId != 67532) {
*ppEapError = g_peer.make_error(dwResult = ERROR_NOT_SUPPORTED, 0, NULL, NULL, NULL, winstd::wstring_printf(_T(__FUNCTION__) _T(" EAP author (%d) does not match the supported author (%d)."), (int)pEapType->dwAuthorId, (int)67532).c_str(), NULL);
} else if (!pdwSizeOfDataFromInteractiveUI) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" pdwSizeOfDataFromInteractiveUI is NULL."), NULL);
} else if (!ppDataFromInteractiveUI) {
*ppEapError = g_peer.make_error(dwResult = ERROR_INVALID_PARAMETER, 0, NULL, NULL, NULL, _T(__FUNCTION__) _T(" ppDataFromInteractiveUI is NULL."), NULL);
} else {
UNREFERENCED_PARAMETER(dwSizeofUIContextData);
UNREFERENCED_PARAMETER(pUIContextData);
InitCommonControls();
MessageBox(hwndParent, _T(PRODUCT_NAME_STR) _T(" interactive UI goes here!"), _T(PRODUCT_NAME_STR) _T(" Prompt"), MB_OK);
}
return dwResult;
}
#pragma comment(linker, "/EXPORT:EapPeerInvokeInteractiveUI")