From ac3ff2d3ca7e7f21ef3f2ceb65fa3ffe916995f8 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 2 Sep 2016 14:45:01 +0200 Subject: [PATCH] Diameter AVP generation moved to method_noneap::append_avp() --- lib/EAPBase/include/Method.h | 11 ++++++++++ lib/EAPBase/src/Method.cpp | 26 +++++++++++++++++++++++ lib/MSCHAPv2/src/Method.cpp | 41 +++--------------------------------- lib/PAP/src/Method.cpp | 39 +++------------------------------- 4 files changed, 43 insertions(+), 74 deletions(-) diff --git a/lib/EAPBase/include/Method.h b/lib/EAPBase/include/Method.h index 4c5c582..a3aee83 100644 --- a/lib/EAPBase/include/Method.h +++ b/lib/EAPBase/include/Method.h @@ -183,6 +183,17 @@ namespace eap /// @} + protected: + /// + /// Appends Diameter AVP to response packet + /// + /// \param[in] code AVP code + /// \param[in] flags AVP flags + /// \param[in] data AVP data (<16777212B) + /// \param[in] padding Padding to use (must be multiple of 4B) + /// + void append_avp(_In_ unsigned int code, _In_ unsigned char flags, _In_bytecount_(size) const void *data, _In_ unsigned int size); + protected: sanitizing_blob m_packet_res; ///< Response packet }; diff --git a/lib/EAPBase/src/Method.cpp b/lib/EAPBase/src/Method.cpp index e6941e9..d1581de 100644 --- a/lib/EAPBase/src/Method.cpp +++ b/lib/EAPBase/src/Method.cpp @@ -119,3 +119,29 @@ void eap::method_noneap::get_response_packet( *pdwSendPacketSize = (DWORD)size_packet; m_packet_res.clear(); } + + +void eap::method_noneap::append_avp(_In_ unsigned int code, _In_ unsigned char flags, _In_bytecount_(size) const void *data, _In_ unsigned int size) +{ + unsigned int + padding = (unsigned int)((4 - size) % 4), + size_outer; + + m_packet_res.reserve( + m_packet_res.size() + + (size_outer = + sizeof(diameter_avp_header) + // Diameter header + size) + // Data + padding); // Data padding + + // Diameter AVP header + diameter_avp_header hdr; + *(unsigned int*)hdr.code = htonl(code); + hdr.flags = flags; + hton24(size_outer, hdr.length); + m_packet_res.insert(m_packet_res.end(), (unsigned char*)&hdr, (unsigned char*)(&hdr + 1)); + + // Data + m_packet_res.insert(m_packet_res.end(), (unsigned char*)data, (unsigned char*)data + size); + m_packet_res.insert(m_packet_res.end(), padding, 0); +} diff --git a/lib/MSCHAPv2/src/Method.cpp b/lib/MSCHAPv2/src/Method.cpp index 910d5e3..31ebd4b 100644 --- a/lib/MSCHAPv2/src/Method.cpp +++ b/lib/MSCHAPv2/src/Method.cpp @@ -94,44 +94,9 @@ void eap::method_mschapv2::process_request_packet( 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.clear(); - m_packet_res.reserve( - (size_identity_outer = - sizeof(diameter_avp_header) + // Diameter header - size_identity) + // Identity - padding_identity + // Identity padding - (size_password_outer = - sizeof(diameter_avp_header) + // Diameter header - size_password) + // Password - padding_password); // Password padding - - // Diameter AVP Code User-Name (0x00000001) - diameter_avp_header hdr; - *(unsigned int*)hdr.code = htonl(0x00000001); - hdr.flags = diameter_avp_flag_mandatory; - hton24((unsigned int)size_identity_outer, hdr.length); - m_packet_res.insert(m_packet_res.end(), (unsigned char*)&hdr, (unsigned char*)(&hdr + 1)); - - // Identity - m_packet_res.insert(m_packet_res.end(), identity_utf8.begin(), identity_utf8.end()); - m_packet_res.insert(m_packet_res.end(), padding_identity, 0); - - // Diameter AVP Code User-Password (0x00000002) - *(unsigned int*)hdr.code = htonl(0x00000002); - hton24((unsigned int)size_password_outer, hdr.length); - m_packet_res.insert(m_packet_res.end(), (unsigned char*)&hdr, (unsigned char*)(&hdr + 1)); - - // Password - m_packet_res.insert(m_packet_res.end(), password_utf8.begin(), password_utf8.end()); - m_packet_res.insert(m_packet_res.end(), padding_password, 0); + // Diameter AVP (User-Name=0x00000001, User-Password=0x00000002) + append_avp(0x00000001, diameter_avp_flag_mandatory, identity_utf8.data(), (unsigned int)identity_utf8.size()); + append_avp(0x00000002, diameter_avp_flag_mandatory, password_utf8.data(), (unsigned int)password_utf8.size()); m_phase = phase_finished; break; diff --git a/lib/PAP/src/Method.cpp b/lib/PAP/src/Method.cpp index cbfc7cc..0230057 100644 --- a/lib/PAP/src/Method.cpp +++ b/lib/PAP/src/Method.cpp @@ -94,44 +94,11 @@ void eap::method_pap::process_request_packet( 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.clear(); - m_packet_res.reserve( - (size_identity_outer = - sizeof(diameter_avp_header) + // Diameter header - size_identity) + // Identity - padding_identity + // Identity padding - (size_password_outer = - sizeof(diameter_avp_header) + // Diameter header - size_password) + // Password - padding_password); // Password padding - // Diameter AVP Code User-Name (0x00000001) - diameter_avp_header hdr; - *(unsigned int*)hdr.code = htonl(0x00000001); - hdr.flags = diameter_avp_flag_mandatory; - hton24((unsigned int)size_identity_outer, hdr.length); - m_packet_res.insert(m_packet_res.end(), (unsigned char*)&hdr, (unsigned char*)(&hdr + 1)); - - // Identity - m_packet_res.insert(m_packet_res.end(), identity_utf8.begin(), identity_utf8.end()); - m_packet_res.insert(m_packet_res.end(), padding_identity, 0); - - // Diameter AVP Code User-Password (0x00000002) - *(unsigned int*)hdr.code = htonl(0x00000002); - hton24((unsigned int)size_password_outer, hdr.length); - m_packet_res.insert(m_packet_res.end(), (unsigned char*)&hdr, (unsigned char*)(&hdr + 1)); - - // Password - m_packet_res.insert(m_packet_res.end(), password_utf8.begin(), password_utf8.end()); - m_packet_res.insert(m_packet_res.end(), padding_password, 0); + // Diameter AVP (User-Name=0x00000001, User-Password=0x00000002) + append_avp(0x00000001, diameter_avp_flag_mandatory, identity_utf8.data(), (unsigned int)identity_utf8.size()); + append_avp(0x00000002, diameter_avp_flag_mandatory, password_utf8.data(), (unsigned int)password_utf8.size()); m_phase = phase_finished; break;