From ce0bbc5b45b105ba8c650b6bec8710b225b26143 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 20 Jul 2016 14:59:12 +0200 Subject: [PATCH] config_method::m_preshared moved to heap, which in turn required shift to virtual methods for packing/unpacking BLOBs --- lib/EAPBase/include/Config.h | 463 +++++++++++++++--------------- lib/EAPBase/include/Credentials.h | 125 ++------ lib/EAPBase/include/EAPSerial.h | 53 ++-- lib/EAPBase/src/Config.cpp | 18 ++ lib/EAPBase/src/Credentials.cpp | 25 ++ lib/EAPBase_UI/include/EAP_UI.h | 26 +- lib/PAP/include/Config.h | 54 +--- lib/PAP/include/Credentials.h | 49 ---- lib/PAP/src/Config.cpp | 6 + lib/TLS/include/Config.h | 87 ++---- lib/TLS/include/Credentials.h | 79 ++--- lib/TLS/src/Config.cpp | 31 ++ lib/TLS/src/Credentials.cpp | 22 ++ lib/TTLS/include/Config.h | 110 ++----- lib/TTLS/include/Credentials.h | 110 ++----- lib/TTLS/src/Config.cpp | 55 ++++ lib/TTLS/src/Credentials.cpp | 55 ++++ 17 files changed, 620 insertions(+), 748 deletions(-) diff --git a/lib/EAPBase/include/Config.h b/lib/EAPBase/include/Config.h index bac9e45..a993614 100644 --- a/lib/EAPBase/include/Config.h +++ b/lib/EAPBase/include/Config.h @@ -69,81 +69,6 @@ namespace eapserial /// \param[out] val Configuration to unpack to /// inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config &val); - - /// - /// Packs a method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - template inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method<_Tcred> &val); - - /// - /// Returns packed size of a method configuration - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - template inline size_t get_pk_size(const eap::config_method<_Tcred> &val); - - /// - /// Unpacks a method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - template inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method<_Tcred> &val); - - /// - /// Packs a provider configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - template inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_provider<_Tmeth> &val); - - /// - /// Returns packed size of a provider configuration - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - template inline size_t get_pk_size(const eap::config_provider<_Tmeth> &val); - - /// - /// Unpacks a provider configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - template inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_provider<_Tmeth> &val); - - /// - /// Packs a providers configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - template inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_providers<_Tprov> &val); - - /// - /// Returns packed size of a providers configuration - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - template inline size_t get_pk_size(const eap::config_providers<_Tprov> &val); - - /// - /// Unpacks a providers configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - template inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_providers<_Tprov> &val); } #pragma once @@ -246,10 +171,37 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const; + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const; + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor); + + /// @} + public: module &m_module; ///< Reference of the EAP module }; + class credentials; template class config_method : public config @@ -268,8 +220,6 @@ namespace eap /// config_method(_In_ module &mod) : m_allow_save(true), - m_use_preshared(false), - m_preshared(mod), config(mod) { } @@ -283,8 +233,7 @@ namespace eap config_method(_In_ const config_method<_Tcred> &other) : m_allow_save(other.m_allow_save), m_anonymous_identity(other.m_anonymous_identity), - m_use_preshared(other.m_use_preshared), - m_preshared(other.m_preshared), + m_preshared(other.m_preshared ? (credentials*)other.m_preshared->clone() : nullptr), config(other) { } @@ -298,7 +247,6 @@ namespace eap config_method(_Inout_ config_method<_Tcred> &&other) : m_allow_save(std::move(other.m_allow_save)), m_anonymous_identity(std::move(other.m_anonymous_identity)), - m_use_preshared(std::move(other.m_use_preshared)), m_preshared(std::move(other.m_preshared)), config(std::move(other)) { @@ -318,8 +266,7 @@ namespace eap (config&)*this = other; m_allow_save = other.m_allow_save; m_anonymous_identity = other.m_anonymous_identity; - m_use_preshared = other.m_use_preshared; - m_preshared = other.m_preshared; + m_preshared.reset(other.m_preshared ? (credentials*)other.m_preshared->clone() : nullptr); } return *this; @@ -339,7 +286,6 @@ namespace eap (config&&)*this = std::move(other); m_allow_save = std::move(other.m_allow_save); m_anonymous_identity = std::move(other.m_anonymous_identity); - m_use_preshared = std::move(other.m_use_preshared); m_preshared = std::move(other.m_preshared); } @@ -393,8 +339,8 @@ namespace eap return false; } - if (m_use_preshared) - if (!m_preshared.save(pDoc, pXmlElClientSideCredential, ppEapError)) + if (m_preshared) + if (!m_preshared->save(pDoc, pXmlElClientSideCredential, ppEapError)) return false; return true; @@ -420,8 +366,7 @@ namespace eap return false; m_allow_save = true; - m_use_preshared = false; - m_preshared.clear(); + m_preshared.reset(nullptr); m_anonymous_identity.clear(); // @@ -437,14 +382,17 @@ namespace eap eapxml::get_element_value(pXmlElClientSideCredential, winstd::bstr(L"eap-metadata:AnonymousIdentity"), m_anonymous_identity); m_module.log_config((xpath + L"/AnonymousIdentity").c_str(), m_anonymous_identity.c_str()); - if (!m_preshared.load(pXmlElClientSideCredential, ppEapError)) { + std::unique_ptr preshared(make_credentials()); + assert(preshared); + if (preshared->load(pXmlElClientSideCredential, ppEapError)) { + m_preshared = std::move(preshared); + } else { // This is not really an error - merely an indication pre-shared credentials are unavailable. if (*ppEapError) { m_module.free_error_memory(*ppEapError); *ppEapError = NULL; } - } else - m_use_preshared = true; + } } return true; @@ -452,6 +400,73 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const + { + eap::config::pack(cursor); + eapserial::pack(cursor, m_allow_save ); + eapserial::pack(cursor, m_anonymous_identity); + if (m_preshared) { + eapserial::pack(cursor, true); + m_preshared->pack(cursor); + } else + eapserial::pack(cursor, false); + } + + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const + { + return + eap::config::get_pk_size() + + eapserial::get_pk_size(m_allow_save ) + + eapserial::get_pk_size(m_anonymous_identity) + + (m_preshared ? + eapserial::get_pk_size(true) + + m_preshared->get_pk_size() : + eapserial::get_pk_size(false)); + } + + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor) + { + eap::config::unpack(cursor); + eapserial::unpack(cursor, m_allow_save ); + eapserial::unpack(cursor, m_anonymous_identity); + + bool use_preshared; + eapserial::unpack(cursor, use_preshared); + if (use_preshared) { + m_preshared.reset(make_credentials()); + assert(m_preshared); + m_preshared->unpack(cursor); + } else + m_preshared.reset(nullptr); + } + + /// @} + + /// + /// Makes new set of credentials for the given method type + /// + virtual credentials* make_credentials() const = 0; + /// /// Returns EAP method type of this configuration /// @@ -460,10 +475,9 @@ namespace eap virtual type_t get_method_id() const = 0; public: - bool m_allow_save; ///< Are credentials allowed to be saved to Windows Credential Manager? - std::wstring m_anonymous_identity; ///< Anonymous identity - bool m_use_preshared; ///< Does configuration use pre-shared credentials? - _Tcred m_preshared; ///< Pre-shared credentials + bool m_allow_save; ///< Are credentials allowed to be saved to Windows Credential Manager? + std::wstring m_anonymous_identity; ///< Anonymous identity + std::unique_ptr m_preshared; ///< Pre-shared credentials }; @@ -830,6 +844,82 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const + { + eap::config::pack(cursor); + eapserial::pack(cursor, m_read_only ); + eapserial::pack(cursor, m_id ); + eapserial::pack(cursor, m_name ); + eapserial::pack(cursor, m_help_email ); + eapserial::pack(cursor, m_help_web ); + eapserial::pack(cursor, m_help_phone ); + eapserial::pack(cursor, m_lbl_alt_credential); + eapserial::pack(cursor, m_lbl_alt_identity ); + eapserial::pack(cursor, m_lbl_alt_password ); + eapserial::pack(cursor, m_methods ); + } + + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const + { + return + eap::config::get_pk_size() + + eapserial::get_pk_size(m_read_only ) + + eapserial::get_pk_size(m_id ) + + eapserial::get_pk_size(m_name ) + + eapserial::get_pk_size(m_help_email ) + + eapserial::get_pk_size(m_help_web ) + + eapserial::get_pk_size(m_help_phone ) + + eapserial::get_pk_size(m_lbl_alt_credential) + + eapserial::get_pk_size(m_lbl_alt_identity ) + + eapserial::get_pk_size(m_lbl_alt_password ) + + eapserial::get_pk_size(m_methods ); + } + + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor) + { + eap::config::unpack(cursor); + eapserial::unpack(cursor, m_read_only ); + eapserial::unpack(cursor, m_id ); + eapserial::unpack(cursor, m_name ); + eapserial::unpack(cursor, m_help_email ); + eapserial::unpack(cursor, m_help_web ); + eapserial::unpack(cursor, m_help_phone ); + eapserial::unpack(cursor, m_lbl_alt_credential); + eapserial::unpack(cursor, m_lbl_alt_identity ); + eapserial::unpack(cursor, m_lbl_alt_password ); + + std::list<_Tmeth>::size_type count; + eapserial::unpack(cursor, count); + m_methods.clear(); + for (std::list<_Tmeth>::size_type i = 0; i < count; i++) { + _Tmeth el(m_module); + el.unpack(cursor); + m_methods.push_back(std::move(el)); + } + } + + /// @} + public: bool m_read_only; ///< Is profile read-only std::wstring m_id; ///< Profile ID @@ -1018,6 +1108,55 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const + { + eap::config::pack(cursor); + eapserial::pack(cursor, m_providers); + } + + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const + { + return + eap::config::get_pk_size() + + eapserial::get_pk_size(m_providers); + } + + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor) + { + eap::config::unpack(cursor); + + std::list<_Tprov>::size_type count = *(const std::list<_Tprov>::size_type*&)cursor; + eapserial::unpack(cursor, count); + m_providers.clear(); + for (std::list<_Tprov>::size_type i = 0; i < count; i++) { + _Tprov el(m_module); + el.unpack(cursor); + m_providers.push_back(std::move(el)); + } + } + + /// @} + public: std::list<_Tprov> m_providers; ///< List of provider configurations }; @@ -1028,148 +1167,18 @@ namespace eapserial { inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config &val) { - UNREFERENCED_PARAMETER(cursor); - UNREFERENCED_PARAMETER(val ); + val.pack(cursor); } inline size_t get_pk_size(const eap::config &val) { - UNREFERENCED_PARAMETER(val); - return 0; + return val.get_pk_size(); } inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config &val) { - UNREFERENCED_PARAMETER(cursor); - UNREFERENCED_PARAMETER(val ); - } - - - template - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method<_Tcred> &val) - { - pack(cursor, (const eap::config&)val ); - pack(cursor, val.m_allow_save ); - pack(cursor, val.m_anonymous_identity); - pack(cursor, val.m_use_preshared ); - pack(cursor, val.m_preshared ); - } - - - template - inline size_t get_pk_size(const eap::config_method<_Tcred> &val) - { - return - get_pk_size((const eap::config&)val ) + - get_pk_size(val.m_allow_save ) + - get_pk_size(val.m_anonymous_identity) + - get_pk_size(val.m_use_preshared ) + - get_pk_size(val.m_preshared ); - } - - - template - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method<_Tcred> &val) - { - unpack(cursor, (eap::config&)val ); - unpack(cursor, val.m_allow_save ); - unpack(cursor, val.m_anonymous_identity); - unpack(cursor, val.m_use_preshared ); - unpack(cursor, val.m_preshared ); - } - - - template - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_provider<_Tmeth> &val) - { - pack(cursor, (const eap::config&)val ); - pack(cursor, val.m_read_only ); - pack(cursor, val.m_id ); - pack(cursor, val.m_name ); - pack(cursor, val.m_help_email ); - pack(cursor, val.m_help_web ); - pack(cursor, val.m_help_phone ); - pack(cursor, val.m_lbl_alt_credential); - pack(cursor, val.m_lbl_alt_identity ); - pack(cursor, val.m_lbl_alt_password ); - pack(cursor, val.m_methods ); - } - - - template - inline size_t get_pk_size(const eap::config_provider<_Tmeth> &val) - { - return - get_pk_size((const eap::config&)val ) + - get_pk_size(val.m_read_only ) + - get_pk_size(val.m_id ) + - get_pk_size(val.m_name ) + - get_pk_size(val.m_help_email ) + - get_pk_size(val.m_help_web ) + - get_pk_size(val.m_help_phone ) + - get_pk_size(val.m_lbl_alt_credential) + - get_pk_size(val.m_lbl_alt_identity ) + - get_pk_size(val.m_lbl_alt_password ) + - get_pk_size(val.m_methods ); - } - - - template - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_provider<_Tmeth> &val) - { - unpack(cursor, (eap::config&)val ); - unpack(cursor, val.m_read_only ); - unpack(cursor, val.m_id ); - unpack(cursor, val.m_name ); - unpack(cursor, val.m_help_email ); - unpack(cursor, val.m_help_web ); - unpack(cursor, val.m_help_phone ); - unpack(cursor, val.m_lbl_alt_credential); - unpack(cursor, val.m_lbl_alt_identity ); - unpack(cursor, val.m_lbl_alt_password ); - - std::list<_Tmeth>::size_type count = *(const std::list<_Tmeth>::size_type*&)cursor; - cursor += sizeof(std::list<_Tmeth>::size_type); - val.m_methods.clear(); - for (std::list<_Tmeth>::size_type i = 0; i < count; i++) { - _Tmeth el(val.m_module); - unpack(cursor, el); - val.m_methods.push_back(std::move(el)); - } - } - - - template - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_providers<_Tprov> &val) - { - pack(cursor, (const eap::config&)val); - pack(cursor, val.m_providers ); - } - - - template - inline size_t get_pk_size(const eap::config_providers<_Tprov> &val) - { - return - get_pk_size((const eap::config&)val) + - get_pk_size(val.m_providers ); - } - - - template - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_providers<_Tprov> &val) - { - unpack(cursor, (eap::config&)val); - - std::list<_Tprov>::size_type count = *(const std::list<_Tprov>::size_type*&)cursor; - cursor += sizeof(std::list<_Tprov>::size_type); - val.m_providers.clear(); - for (std::list<_Tprov>::size_type i = 0; i < count; i++) { - _Tprov el(val.m_module); - unpack(cursor, el); - val.m_providers.push_back(std::move(el)); - } + val.unpack(cursor); } } diff --git a/lib/EAPBase/include/Credentials.h b/lib/EAPBase/include/Credentials.h index 64f8f6d..16bd42d 100644 --- a/lib/EAPBase/include/Credentials.h +++ b/lib/EAPBase/include/Credentials.h @@ -33,59 +33,6 @@ namespace eap class credentials_pass; } -namespace eapserial -{ - /// - /// Packs a method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Credentials to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials &val); - - /// - /// Returns packed size of a method credentials - /// - /// \param[in] val Credentials to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::credentials &val); - - /// - /// Unpacks a method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Credentials to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials &val); - - /// - /// Packs a password based method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Credentials to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_pass &val); - - /// - /// Returns packed size of a password based method credentials - /// - /// \param[in] val Credentials to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::credentials_pass &val); - - /// - /// Unpacks a password based method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Credentials to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_pass &val); -} - #pragma once #include "Config.h" @@ -339,6 +286,32 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const; + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const; + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor); + + /// @} + /// \name Storage /// @{ @@ -383,49 +356,3 @@ namespace eap /// \endcond }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials &val) - { - pack(cursor, (const eap::config&)val); - } - - - inline size_t get_pk_size(const eap::credentials &val) - { - return get_pk_size((const eap::config&)val); - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials &val) - { - unpack(cursor, (eap::config&)val); - } - - - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_pass &val) - { - pack(cursor, (const eap::credentials&)val); - pack(cursor, val.m_identity ); - pack(cursor, val.m_password ); - } - - - inline size_t get_pk_size(const eap::credentials_pass &val) - { - return - get_pk_size((const eap::credentials&)val) + - get_pk_size(val.m_identity ) + - get_pk_size(val.m_password ); - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_pass &val) - { - unpack(cursor, (eap::credentials&)val); - unpack(cursor, val.m_identity ); - unpack(cursor, val.m_password ); - } -} diff --git a/lib/EAPBase/include/EAPSerial.h b/lib/EAPBase/include/EAPSerial.h index 95d6692..0a8c541 100644 --- a/lib/EAPBase/include/EAPSerial.h +++ b/lib/EAPBase/include/EAPSerial.h @@ -403,8 +403,7 @@ namespace eapserial inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::vector<_Ty, _Ax> &val) { std::vector<_Ty, _Ax>::size_type count = val.size(); - *(std::vector<_Ty, _Ax>::size_type*&)cursor = count; - cursor += sizeof(std::vector<_Ty, _Ax>::size_type); + pack(cursor, count); // Since we do not know wheter vector elements are primitives or objects, iterate instead of memcpy. // For performance critical vectors of flat opaque data types write specialized template instantiation. @@ -418,8 +417,9 @@ namespace eapserial { // Since we do not know wheter vector elements are primitives or objects, iterate instead of sizeof(). // For performance critical vectors of flat opaque data types write specialized template instantiation. - size_t size = sizeof(std::vector<_Ty, _Ax>::size_type); - for (std::vector<_Ty, _Ax>::size_type i = 0, count = val.size(); i < count; i++) + std::vector<_Ty, _Ax>::size_type count = val.size(); + size_t size = get_pk_size(count); + for (std::vector<_Ty, _Ax>::size_type i = 0; i < count; i++) size += get_pk_size(val[i]); return size; } @@ -428,8 +428,8 @@ namespace eapserial template inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::vector<_Ty, _Ax> &val) { - std::vector<_Ty, _Ax>::size_type count = *(const std::vector<_Ty, _Ax>::size_type*&)cursor; - cursor += sizeof(std::vector<_Ty, _Ax>::size_type); + std::vector<_Ty, _Ax>::size_type count; + unpack(cursor, count); // Since we do not know wheter vector elements are primitives or objects, iterate instead of assign(). // For performance critical vectors of flat opaque data types write specialized template instantiation. @@ -447,8 +447,7 @@ namespace eapserial inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::list<_Ty, _Ax> &val) { std::list<_Ty, _Ax>::size_type count = val.size(); - *(std::list<_Ty, _Ax>::size_type*&)cursor = count; - cursor += sizeof(std::list<_Ty, _Ax>::size_type); + pack(cursor, count); // Since we do not know wheter list elements are primitives or objects, iterate instead of memcpy. // For performance critical vectors of flat opaque data types write specialized template instantiation. @@ -462,7 +461,8 @@ namespace eapserial { // Since we do not know wheter list elements are primitives or objects, iterate instead of sizeof(). // For performance critical vectors of flat opaque data types write specialized template instantiation. - size_t size = sizeof(std::list<_Ty, _Ax>::size_type); + std::list<_Ty, _Ax>::size_type count = val.size(); + size_t size = get_pk_size(count); for (std::list<_Ty, _Ax>::const_iterator i = val.cbegin(), i_end = val.cend(); i != i_end; ++i) size += get_pk_size(*i); return size; @@ -472,8 +472,8 @@ namespace eapserial template inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::list<_Ty, _Ax> &val) { - std::list<_Ty, _Ax>::size_type count = *(const std::list<_Ty, _Ax>::size_type*&)cursor; - cursor += sizeof(std::list<_Ty, _Ax>::size_type); + std::list<_Ty, _Ax>::size_type count; + unpack(cursor, count); // Since we do not know wheter list elements are primitives or objects, iterate instead of assign(). // For performance critical vectors of flat opaque data types write specialized template instantiation. @@ -489,37 +489,36 @@ namespace eapserial inline void pack(_Inout_ unsigned char *&cursor, _In_ const winstd::cert_context &val) { if (val) { - *(DWORD*&)cursor = val->dwCertEncodingType; - cursor += sizeof(DWORD); - - *(DWORD*&)cursor = val->cbCertEncoded; - cursor += sizeof(DWORD); - + pack(cursor, (unsigned int)val->dwCertEncodingType); + pack(cursor, (unsigned int)val->cbCertEncoded ); memcpy(cursor, val->pbCertEncoded, val->cbCertEncoded); cursor += val->cbCertEncoded; } else { - *(DWORD*&)cursor = 0; - cursor += sizeof(DWORD); - - *(DWORD*&)cursor = 0; - cursor += sizeof(DWORD); + pack(cursor, (unsigned int)0); + pack(cursor, (unsigned int)0); } } inline size_t get_pk_size(const winstd::cert_context &val) { - return sizeof(DWORD) + sizeof(DWORD) + (val ? val->cbCertEncoded : 0); + return + val ? + get_pk_size((unsigned int)val->dwCertEncodingType) + + get_pk_size((unsigned int)val->cbCertEncoded ) + + val->cbCertEncoded : + get_pk_size((unsigned int)0) + + get_pk_size((unsigned int)0); } inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ winstd::cert_context &val) { - DWORD dwCertEncodingType = *(DWORD*&)cursor; - cursor += sizeof(DWORD); + DWORD dwCertEncodingType; + unpack(cursor, (unsigned int&)dwCertEncodingType); - DWORD dwCertEncodedSize = *(DWORD*&)cursor; - cursor += sizeof(DWORD); + DWORD dwCertEncodedSize; + unpack(cursor, (unsigned int&)dwCertEncodedSize); if (dwCertEncodedSize) { val.create(dwCertEncodingType, (BYTE*)cursor, dwCertEncodedSize); diff --git a/lib/EAPBase/src/Config.cpp b/lib/EAPBase/src/Config.cpp index 8961c3c..bc01642 100644 --- a/lib/EAPBase/src/Config.cpp +++ b/lib/EAPBase/src/Config.cpp @@ -79,3 +79,21 @@ bool eap::config::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR **ppEapErr return true; } + + +void eap::config::pack(_Inout_ unsigned char *&cursor) const +{ + UNREFERENCED_PARAMETER(cursor); +} + + +size_t eap::config::get_pk_size() const +{ + return 0; +} + + +void eap::config::unpack(_Inout_ const unsigned char *&cursor) +{ + UNREFERENCED_PARAMETER(cursor); +} diff --git a/lib/EAPBase/src/Credentials.cpp b/lib/EAPBase/src/Credentials.cpp index 036f454..f8cdcf3 100644 --- a/lib/EAPBase/src/Credentials.cpp +++ b/lib/EAPBase/src/Credentials.cpp @@ -233,6 +233,31 @@ bool eap::credentials_pass::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR } +void eap::credentials_pass::pack(_Inout_ unsigned char *&cursor) const +{ + eap::credentials::pack(cursor); + eapserial::pack(cursor, m_identity); + eapserial::pack(cursor, m_password); +} + + +size_t eap::credentials_pass::get_pk_size() const +{ + return + eap::credentials::get_pk_size() + + eapserial::get_pk_size(m_identity) + + eapserial::get_pk_size(m_password); +} + + +void eap::credentials_pass::unpack(_Inout_ const unsigned char *&cursor) +{ + eap::credentials::unpack(cursor); + eapserial::unpack(cursor, m_identity); + eapserial::unpack(cursor, m_password); +} + + bool eap::credentials_pass::store(_In_ LPCTSTR pszTargetName, _Out_ EAP_ERROR **ppEapError) const { assert(pszTargetName); diff --git a/lib/EAPBase_UI/include/EAP_UI.h b/lib/EAPBase_UI/include/EAP_UI.h index 642c260..71f3f34 100644 --- a/lib/EAPBase_UI/include/EAP_UI.h +++ b/lib/EAPBase_UI/include/EAP_UI.h @@ -345,7 +345,7 @@ public: m_prov(prov), m_cfg(cfg), m_target(pszCredTarget), - m_cred(m_cfg.m_module), + m_cred(cfg.make_credentials()), wxEAPCredentialsConfigPanelBase(parent) { // Load and set icon. @@ -366,12 +366,11 @@ protected: m_preshared_set ->Enable(false); } - if (!m_cfg.m_use_preshared) { + if (!m_cfg.m_preshared) { m_own->SetValue(true); - m_cred.clear(); } else { m_preshared->SetValue(true); - m_cred = m_cfg.m_preshared; + m_cred.reset((eap::credentials*)m_cfg.m_preshared->clone()); } return wxEAPCredentialsConfigPanelBase::TransferDataToWindow(); @@ -384,12 +383,7 @@ protected: if (!m_prov.m_read_only) { // This is not a provider-locked configuration. Save the data. - if (m_own->GetValue()) { - m_cfg.m_use_preshared = false; - } else { - m_cfg.m_use_preshared = true; - m_cfg.m_preshared = m_cred; - } + m_cfg.m_preshared.reset(m_own->GetValue() ? nullptr : (eap::credentials*)m_cred->clone()); } return true; @@ -404,7 +398,7 @@ protected: if (m_cfg.m_allow_save) { bool has_own; std::unique_ptr > cred; - if (CredRead(m_cred.target_name(m_target.c_str()).c_str(), CRED_TYPE_GENERIC, 0, (PCREDENTIAL*)&cred)) { + if (CredRead(m_cred->target_name(m_target.c_str()).c_str(), CRED_TYPE_GENERIC, 0, (PCREDENTIAL*)&cred)) { m_own_identity->SetValue(cred->UserName && cred->UserName[0] != 0 ? cred->UserName : _("")); has_own = true; } else if ((dwResult = GetLastError()) == ERROR_NOT_FOUND) { @@ -432,7 +426,7 @@ protected: m_own_clear ->Enable(false); } - m_preshared_identity->SetValue(!m_cred.empty() ? m_cred.get_name() : _("")); + m_preshared_identity->SetValue(!m_cred->empty() ? m_cred->get_name() : _("")); if (!m_prov.m_read_only) { // This is not a provider-locked configuration. Selectively enable/disable controls. @@ -453,7 +447,7 @@ protected: wxEAPCredentialsDialog<_Tprov> dlg(m_prov, this); - _wxT *panel = new _wxT(m_prov, m_cred, m_target.c_str(), &dlg, true); + _wxT *panel = new _wxT(m_prov, (typename _Tmeth::credentials_type&)*m_cred, m_target.c_str(), &dlg, true); dlg.AddContents((wxPanel**)&panel, 1); dlg.ShowModal(); @@ -464,7 +458,7 @@ protected: { UNREFERENCED_PARAMETER(event); - if (!CredDelete(m_cred.target_name(m_target.c_str()).c_str(), CRED_TYPE_GENERIC, 0)) + if (!CredDelete(m_cred->target_name(m_target.c_str()).c_str(), CRED_TYPE_GENERIC, 0)) wxLogError(_("Deleting credentials failed (error %u)."), GetLastError()); } @@ -475,7 +469,7 @@ protected: wxEAPCredentialsDialog<_Tprov> dlg(m_prov, this); - _wxT *panel = new _wxT(m_prov, m_cred, _T(""), &dlg, true); + _wxT *panel = new _wxT(m_prov, (typename _Tmeth::credentials_type&)*m_cred, _T(""), &dlg, true); dlg.AddContents((wxPanel**)&panel, 1); dlg.ShowModal(); @@ -491,7 +485,7 @@ protected: winstd::tstring m_target; ///< Credential Manager target private: - typename _Tmeth::credentials_type m_cred; ///< Temporary credential data + std::unique_ptr m_cred; ///< Temporary credential data }; diff --git a/lib/PAP/include/Config.h b/lib/PAP/include/Config.h index 59e8725..9c64f30 100644 --- a/lib/PAP/include/Config.h +++ b/lib/PAP/include/Config.h @@ -28,34 +28,6 @@ namespace eap class config_method_pap; } -namespace eapserial -{ - /// - /// Packs a PAP based method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method_pap &val); - - /// - /// Returns packed size of a PAP based method configuration - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::config_method_pap &val); - - /// - /// Unpacks a PAP based method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method_pap &val); -} - #pragma once #include "Credentials.h" @@ -117,6 +89,11 @@ namespace eap /// virtual config* clone() const; + /// + /// Makes new set of credentials for the given method type + /// + virtual credentials* make_credentials() const; + /// /// Returns EAP method type of this configuration /// @@ -125,24 +102,3 @@ namespace eap virtual eap::type_t get_method_id() const; }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method_pap &val) - { - pack(cursor, (const eap::config_method&)val); - } - - - inline size_t get_pk_size(const eap::config_method_pap &val) - { - return get_pk_size((const eap::config_method&)val); - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method_pap &val) - { - unpack(cursor, (eap::config_method&)val); - } -} diff --git a/lib/PAP/include/Credentials.h b/lib/PAP/include/Credentials.h index e9a8f08..d65ec3a 100644 --- a/lib/PAP/include/Credentials.h +++ b/lib/PAP/include/Credentials.h @@ -29,34 +29,6 @@ namespace eap class credentials_pap; } -namespace eapserial -{ - /// - /// Packs a PAP method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Credentials to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_pap &val); - - /// - /// Returns packed size of a PAP method credentials - /// - /// \param[in] val Credentials to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::credentials_pap &val); - - /// - /// Unpacks a PAP method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Credentials to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_pap &val); -} - #pragma once #include "../../EAPBase/include/Credentials.h" @@ -129,24 +101,3 @@ namespace eap /// @} }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_pap &val) - { - pack(cursor, (const eap::credentials_pass&)val); - } - - - inline size_t get_pk_size(const eap::credentials_pap &val) - { - return get_pk_size((const eap::credentials_pass&)val); - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_pap &val) - { - unpack(cursor, (eap::credentials_pass&)val); - } -} diff --git a/lib/PAP/src/Config.cpp b/lib/PAP/src/Config.cpp index bc40134..377280b 100644 --- a/lib/PAP/src/Config.cpp +++ b/lib/PAP/src/Config.cpp @@ -66,6 +66,12 @@ eap::config* eap::config_method_pap::clone() const } +eap::credentials* eap::config_method_pap::make_credentials() const +{ + return new credentials_pap(m_module); +} + + eap::type_t eap::config_method_pap::get_method_id() const { return eap::type_pap; diff --git a/lib/TLS/include/Config.h b/lib/TLS/include/Config.h index fe07cc8..39f26dc 100644 --- a/lib/TLS/include/Config.h +++ b/lib/TLS/include/Config.h @@ -38,34 +38,6 @@ namespace eap winstd::tstring get_cert_title(PCCERT_CONTEXT cert); } -namespace eapserial -{ - /// - /// Packs a TLS method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method_tls &val); - - /// - /// Returns packed size of a TLS method configuration - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::config_method_tls &val); - - /// - /// Unpacks a TLS method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method_tls &val); -} - #pragma once #include "Credentials.h" @@ -161,6 +133,37 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const; + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const; + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor); + + /// @} + + /// + /// Makes new set of credentials for the given method type + /// + virtual credentials* make_credentials() const; + /// /// Returns EAP method type of this configuration /// @@ -180,31 +183,3 @@ namespace eap std::list m_server_names; ///< Acceptable authenticating server names }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method_tls &val) - { - pack(cursor, (const eap::config_method&)val); - pack(cursor, val.m_trusted_root_ca); - pack(cursor, val.m_server_names ); - } - - - inline size_t get_pk_size(const eap::config_method_tls &val) - { - return - get_pk_size((const eap::config_method&)val) + - get_pk_size(val.m_trusted_root_ca) + - get_pk_size(val.m_server_names ); - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method_tls &val) - { - unpack(cursor, (eap::config_method&)val); - unpack(cursor, val.m_trusted_root_ca); - unpack(cursor, val.m_server_names ); - } -} diff --git a/lib/TLS/include/Credentials.h b/lib/TLS/include/Credentials.h index 5bfd224..df6abb2 100644 --- a/lib/TLS/include/Credentials.h +++ b/lib/TLS/include/Credentials.h @@ -28,34 +28,6 @@ namespace eap class credentials_tls; } -namespace eapserial -{ - /// - /// Packs a TLS method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Credentials to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_tls &val); - - /// - /// Returns packed size of a TLS method credentials - /// - /// \param[in] val Credentials to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::credentials_tls &val); - - /// - /// Unpacks a TLS method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Credentials to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_tls &val); -} - #pragma once #include "../../EAPBase/include/Credentials.h" @@ -158,6 +130,32 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const; + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const; + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor); + + /// @} + /// \name Storage /// @{ @@ -217,28 +215,3 @@ namespace eap /// \endcond }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_tls &val) - { - pack(cursor, (const eap::credentials&)val); - pack(cursor, val.m_cert ); - } - - - inline size_t get_pk_size(const eap::credentials_tls &val) - { - return - get_pk_size((const eap::credentials&)val) + - get_pk_size(val.m_cert ); - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_tls &val) - { - unpack(cursor, (eap::credentials&)val); - unpack(cursor, val.m_cert ); - } -} diff --git a/lib/TLS/src/Config.cpp b/lib/TLS/src/Config.cpp index 44603e8..6c82fe6 100644 --- a/lib/TLS/src/Config.cpp +++ b/lib/TLS/src/Config.cpp @@ -256,6 +256,37 @@ bool eap::config_method_tls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR } +void eap::config_method_tls::pack(_Inout_ unsigned char *&cursor) const +{ + eap::config_method::pack(cursor); + eapserial::pack(cursor, m_trusted_root_ca); + eapserial::pack(cursor, m_server_names ); +} + + +size_t eap::config_method_tls::get_pk_size() const +{ + return + eap::config_method::get_pk_size() + + eapserial::get_pk_size(m_trusted_root_ca) + + eapserial::get_pk_size(m_server_names ); +} + + +void eap::config_method_tls::unpack(_Inout_ const unsigned char *&cursor) +{ + eap::config_method::unpack(cursor); + eapserial::unpack(cursor, m_trusted_root_ca); + eapserial::unpack(cursor, m_server_names ); +} + + +eap::credentials* eap::config_method_tls::make_credentials() const +{ + return new credentials_tls(m_module); +} + + eap::type_t eap::config_method_tls::get_method_id() const { return eap::type_tls; diff --git a/lib/TLS/src/Credentials.cpp b/lib/TLS/src/Credentials.cpp index 04bfccd..8acf5b4 100644 --- a/lib/TLS/src/Credentials.cpp +++ b/lib/TLS/src/Credentials.cpp @@ -167,6 +167,28 @@ bool eap::credentials_tls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR * } +void eap::credentials_tls::pack(_Inout_ unsigned char *&cursor) const +{ + eap::credentials::pack(cursor); + eapserial::pack(cursor, m_cert); +} + + +size_t eap::credentials_tls::get_pk_size() const +{ + return + eap::credentials::get_pk_size() + + eapserial::get_pk_size(m_cert); +} + + +void eap::credentials_tls::unpack(_Inout_ const unsigned char *&cursor) +{ + eap::credentials::unpack(cursor); + eapserial::unpack(cursor, m_cert); +} + + bool eap::credentials_tls::store(_In_ LPCTSTR pszTargetName, _Out_ EAP_ERROR **ppEapError) const { assert(pszTargetName); diff --git a/lib/TTLS/include/Config.h b/lib/TTLS/include/Config.h index db4697d..2094405 100644 --- a/lib/TTLS/include/Config.h +++ b/lib/TTLS/include/Config.h @@ -28,34 +28,6 @@ namespace eap class config_method_ttls; } -namespace eapserial -{ - /// - /// Packs a TTLS based method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method_ttls &val); - - /// - /// Returns packed size of a TTLS based method configuration - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::config_method_ttls &val); - - /// - /// Unpacks a TTLS based method configuration - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method_ttls &val); -} - #pragma once #include "../../TLS/include/Config.h" @@ -147,6 +119,30 @@ namespace eap { /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const; + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const; + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor); + /// /// Returns EAP method type of this configuration /// @@ -158,61 +154,3 @@ namespace eap { std::unique_ptr m_inner; ///< Inner authentication configuration }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config_method_ttls &val) - { - pack(cursor, (const eap::config_method_tls&)val); - if (val.m_inner) { - if (dynamic_cast(val.m_inner.get())) { - pack(cursor, eap::type_pap); - pack(cursor, (const eap::config_method_pap&)*val.m_inner); - } else { - assert(0); // Unsupported inner authentication method type. - pack(cursor, eap::type_undefined); - } - } else - pack(cursor, eap::type_undefined); - } - - - inline size_t get_pk_size(const eap::config_method_ttls &val) - { - size_t size_inner; - if (val.m_inner) { - if (dynamic_cast(val.m_inner.get())) { - size_inner = - get_pk_size(eap::type_pap) + - get_pk_size((const eap::config_method_pap&)*val.m_inner); - } else { - assert(0); // Unsupported inner authentication method type. - size_inner = get_pk_size(eap::type_undefined); - } - } else - size_inner = get_pk_size(eap::type_undefined); - - return - get_pk_size((const eap::config_method_tls&)val) + - size_inner; - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config_method_ttls &val) - { - unpack(cursor, (eap::config_method_tls&)val); - - eap::type_t eap_type; - unpack(cursor, eap_type); - switch (eap_type) { - case eap::type_pap: - val.m_inner.reset(new eap::config_method_pap(val.m_module)); - unpack(cursor, (eap::config_method_pap&)*val.m_inner); - break; - default: - assert(0); // Unsupported inner authentication method type. - val.m_inner.reset(nullptr); - } - } -} diff --git a/lib/TTLS/include/Credentials.h b/lib/TTLS/include/Credentials.h index bc03a36..7ac9615 100644 --- a/lib/TTLS/include/Credentials.h +++ b/lib/TTLS/include/Credentials.h @@ -26,34 +26,6 @@ namespace eap class credentials_ttls; } -namespace eapserial -{ - /// - /// Packs a TTLS based method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[in] val Configuration to pack - /// - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_ttls &val); - - /// - /// Returns packed size of a TTLS based method credentials - /// - /// \param[in] val Configuration to pack - /// - /// \returns Size of data when packed (in bytes) - /// - inline size_t get_pk_size(const eap::credentials_ttls &val); - - /// - /// Unpacks a TTLS based method credentials - /// - /// \param[inout] cursor Memory cursor - /// \param[out] val Configuration to unpack to - /// - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_ttls &val); -} - #pragma once #include "../../TLS/include/Credentials.h" @@ -153,6 +125,30 @@ namespace eap /// @} + /// \name BLOB management + /// @{ + + /// + /// Packs a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void pack(_Inout_ unsigned char *&cursor) const; + + /// + /// Returns packed size of a configuration + /// + /// \returns Size of data when packed (in bytes) + /// + virtual size_t get_pk_size() const; + + /// + /// Unpacks a configuration + /// + /// \param[inout] cursor Memory cursor + /// + virtual void unpack(_Inout_ const unsigned char *&cursor); + /// \name Storage /// @{ @@ -186,61 +182,3 @@ namespace eap std::unique_ptr m_inner; ///< Inner credentials }; } - - -namespace eapserial -{ - inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::credentials_ttls &val) - { - pack(cursor, (const eap::credentials_tls&)val); - if (val.m_inner) { - if (dynamic_cast(val.m_inner.get())) { - pack(cursor, eap::type_pap); - pack(cursor, (const eap::credentials_pap&)*val.m_inner); - } else { - assert(0); // Unsupported inner authentication method type. - pack(cursor, eap::type_undefined); - } - } else - pack(cursor, eap::type_undefined); - } - - - inline size_t get_pk_size(const eap::credentials_ttls &val) - { - size_t size_inner; - if (val.m_inner) { - if (dynamic_cast(val.m_inner.get())) { - size_inner = - get_pk_size(eap::type_pap) + - get_pk_size((const eap::credentials_pap&)*val.m_inner); - } else { - assert(0); // Unsupported inner authentication method type. - size_inner = get_pk_size(eap::type_undefined); - } - } else - size_inner = get_pk_size(eap::type_undefined); - - return - get_pk_size((const eap::credentials_tls&)val) + - size_inner; - } - - - inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::credentials_ttls &val) - { - unpack(cursor, (eap::credentials_tls&)val); - - eap::type_t eap_type; - unpack(cursor, eap_type); - switch (eap_type) { - case eap::type_pap: - val.m_inner.reset(new eap::credentials_pap(val.m_module)); - unpack(cursor, (eap::credentials_pap&)*val.m_inner); - break; - default: - assert(0); // Unsupported inner authentication method type. - val.m_inner.reset(nullptr); - } - } -} diff --git a/lib/TTLS/src/Config.cpp b/lib/TTLS/src/Config.cpp index 7ca467c..8aa5040 100644 --- a/lib/TTLS/src/Config.cpp +++ b/lib/TTLS/src/Config.cpp @@ -158,6 +158,61 @@ bool eap::config_method_ttls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERRO } +void eap::config_method_ttls::pack(_Inout_ unsigned char *&cursor) const +{ + eap::config_method_tls::pack(cursor); + if (m_inner) { + if (dynamic_cast(m_inner.get())) { + eapserial::pack(cursor, eap::type_pap); + m_inner->pack(cursor); + } else { + assert(0); // Unsupported inner authentication method type. + eapserial::pack(cursor, eap::type_undefined); + } + } else + eapserial::pack(cursor, eap::type_undefined); +} + + +size_t eap::config_method_ttls::get_pk_size() const +{ + size_t size_inner; + if (m_inner) { + if (dynamic_cast(m_inner.get())) { + size_inner = + eapserial::get_pk_size(eap::type_pap) + + m_inner->get_pk_size(); + } else { + assert(0); // Unsupported inner authentication method type. + size_inner = eapserial::get_pk_size(eap::type_undefined); + } + } else + size_inner = eapserial::get_pk_size(eap::type_undefined); + + return + eap::config_method_tls::get_pk_size() + + size_inner; +} + + +void eap::config_method_ttls::unpack(_Inout_ const unsigned char *&cursor) +{ + eap::config_method_tls::unpack(cursor); + + eap::type_t eap_type; + eapserial::unpack(cursor, eap_type); + switch (eap_type) { + case eap::type_pap: + m_inner.reset(new eap::config_method_pap(m_module)); + m_inner->unpack(cursor); + break; + default: + assert(0); // Unsupported inner authentication method type. + m_inner.reset(nullptr); + } +} + + eap::type_t eap::config_method_ttls::get_method_id() const { return eap::type_ttls; diff --git a/lib/TTLS/src/Credentials.cpp b/lib/TTLS/src/Credentials.cpp index 44b21bf..e9b5b79 100644 --- a/lib/TTLS/src/Credentials.cpp +++ b/lib/TTLS/src/Credentials.cpp @@ -149,6 +149,61 @@ bool eap::credentials_ttls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR } +void eap::credentials_ttls::pack(_Inout_ unsigned char *&cursor) const +{ + eap::credentials_tls::pack(cursor); + if (m_inner) { + if (dynamic_cast(m_inner.get())) { + eapserial::pack(cursor, eap::type_pap); + m_inner->pack(cursor); + } else { + assert(0); // Unsupported inner authentication method type. + eapserial::pack(cursor, eap::type_undefined); + } + } else + eapserial::pack(cursor, eap::type_undefined); +} + + +size_t eap::credentials_ttls::get_pk_size() const +{ + size_t size_inner; + if (m_inner) { + if (dynamic_cast(m_inner.get())) { + size_inner = + eapserial::get_pk_size(eap::type_pap) + + m_inner->get_pk_size(); + } else { + assert(0); // Unsupported inner authentication method type. + size_inner = eapserial::get_pk_size(eap::type_undefined); + } + } else + size_inner = eapserial::get_pk_size(eap::type_undefined); + + return + eap::credentials_tls::get_pk_size() + + size_inner; +} + + +void eap::credentials_ttls::unpack(_Inout_ const unsigned char *&cursor) +{ + eap::credentials_tls::unpack(cursor); + + eap::type_t eap_type; + eapserial::unpack(cursor, eap_type); + switch (eap_type) { + case eap::type_pap: + m_inner.reset(new eap::credentials_pap(m_module)); + m_inner->unpack(cursor); + break; + default: + assert(0); // Unsupported inner authentication method type. + m_inner.reset(nullptr); + } +} + + bool eap::credentials_ttls::store(_In_ LPCTSTR pszTargetName, _Out_ EAP_ERROR **ppEapError) const { if (!credentials_tls::store(pszTargetName, ppEapError))