Resolve code analysis reported warnings

This commit is contained in:
Simon Rozman 2018-12-18 09:05:00 +01:00
parent 51a82c242f
commit aa7cd261f2
12 changed files with 8580 additions and 8556 deletions

View File

@ -1,280 +1,287 @@
/* /*
Copyright 1991-2018 Amebis Copyright 1991-2018 Amebis
Copyright 2016 GÉANT Copyright 2016 GÉANT
This file is part of WinStd. This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Setup is distributed in the hope that it will be useful, Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>. along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/ */
/// ///
/// \defgroup WinStdBase64 Base64 conversion /// \defgroup WinStdBase64 Base64 conversion
/// Provides Base64 conversion for WinStd classes /// Provides Base64 conversion for WinStd classes
/// ///
#include "Common.h" #include "Common.h"
#include <string> #include <string>
#include <vector> #include <vector>
namespace winstd namespace winstd
{ {
class WINSTD_API base64_enc; class WINSTD_API base64_enc;
class WINSTD_API base64_dec; class WINSTD_API base64_dec;
} }
#pragma once #pragma once
namespace winstd namespace winstd
{ {
/// \addtogroup WinStdBase64 /// \addtogroup WinStdBase64
/// @{ /// @{
/// ///
/// Base64 encoding session /// Base64 encoding session
/// ///
class WINSTD_API base64_enc class WINSTD_API base64_enc
{ {
public: public:
/// ///
/// Constructs blank encoding session /// Constructs blank encoding session
/// ///
inline base64_enc() : num(0) inline base64_enc() : num(0)
{ {
} buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
/// }
/// Encodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output ///
/// \param[in ] data Data to encode /// Encodes one block of information, and _appends_ it to the output
/// \param[in ] size Length of `data` in bytes ///
/// \param[in ] is_last Is this the last block of data? /// \param[out] out Output
/// /// \param[in ] data Data to encode
template<class _Elem, class _Traits, class _Ax> /// \param[in ] size Length of `data` in bytes
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true) /// \param[in ] is_last Is this the last block of data?
{ ///
assert(data || !size); template<class _Elem, class _Traits, class _Ax>
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true)
// Preallocate output {
out.reserve(out.size() + enc_size(size)); assert(data || !size);
// Convert data character by character. // Preallocate output
for (size_t i = 0;; i++) { out.reserve(out.size() + enc_size(size));
if (num >= 3) {
encode(out); // Convert data character by character.
num = 0; for (size_t i = 0;; i++) {
} if (num >= 3) {
encode(out);
if (i >= size) num = 0;
break; }
buf[num++] = reinterpret_cast<const unsigned char*>(data)[i]; if (i >= size)
} break;
// If this is the last block, flush the buffer. buf[num++] = reinterpret_cast<const unsigned char*>(data)[i];
if (is_last && num) { }
encode(out, num);
num = 0; // If this is the last block, flush the buffer.
} if (is_last && num) {
} encode(out, num);
num = 0;
}
/// }
/// Resets encoding session
///
inline void clear() ///
{ /// Resets encoding session
num = 0; ///
} inline void clear()
{
num = 0;
/// }
/// Returns maximum encoded size
///
/// \param size Number of bytes to encode ///
/// /// Returns maximum encoded size
/// \returns Maximum number of bytes for the encoded data of `size` length ///
/// /// \param size Number of bytes to encode
inline size_t enc_size(size_t size) const ///
{ /// \returns Maximum number of bytes for the encoded data of `size` length
return ((num + size + 2)/3)*4; ///
} inline size_t enc_size(size_t size) const
{
return ((num + size + 2)/3)*4;
protected: }
///
/// Encodes one complete internal buffer of data
/// protected:
template<class _Elem, class _Traits, class _Ax> ///
inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out) /// Encodes one complete internal buffer of data
{ ///
out += lookup[ buf[0] >> 2 ]; template<class _Elem, class _Traits, class _Ax>
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f]; inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out)
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f]; {
out += lookup[ buf[2] & 0x3f]; out += lookup[ buf[0] >> 2 ];
} out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += lookup[ buf[2] & 0x3f];
/// }
/// Encodes partial internal buffer of data
///
template<class _Elem, class _Traits, class _Ax> ///
inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size) /// Encodes partial internal buffer of data
{ ///
if (size > 0) { template<class _Elem, class _Traits, class _Ax>
out += lookup[buf[0] >> 2]; inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size)
if (size > 1) { {
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f]; if (size > 0) {
if (size > 2) { out += lookup[buf[0] >> 2];
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f]; if (size > 1) {
out += lookup[buf[2] & 0x3f]; out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
} else { if (size > 2) {
out += lookup[(buf[1] << 2) & 0x3f]; out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += '='; out += lookup[buf[2] & 0x3f];
} } else {
} else { out += lookup[(buf[1] << 2) & 0x3f];
out += lookup[(buf[0] << 4) & 0x3f]; out += '=';
out += '='; }
out += '='; } else {
} out += lookup[(buf[0] << 4) & 0x3f];
} else { out += '=';
out += '='; out += '=';
out += '='; }
out += '='; } else {
out += '='; out += '=';
} out += '=';
} out += '=';
out += '=';
}
protected: }
unsigned char buf[3]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf`
protected:
/// \cond internal unsigned char buf[3]; ///< Internal buffer
static const char lookup[64]; size_t num; ///< Number of bytes used in `buf`
/// \endcond
}; /// \cond internal
static const char lookup[64];
/// \endcond
/// };
/// Base64 decoding session
///
class WINSTD_API base64_dec ///
{ /// Base64 decoding session
public: ///
/// class WINSTD_API base64_dec
/// Constructs blank decoding session {
/// public:
inline base64_dec() : num(0) ///
{ /// Constructs blank decoding session
} ///
inline base64_dec() : num(0)
{
/// buf[0] = 0;
/// Decodes one block of information, and _appends_ it to the output buf[1] = 0;
/// buf[2] = 0;
/// \param[out] out Output buf[3] = 0;
/// \param[in ] is_last Was this the last block of data? }
/// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes
/// ///
template<class _Ty, class _Ax, class _Tchr> /// Decodes one block of information, and _appends_ it to the output
inline void decode(_Out_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size) ///
{ /// \param[out] out Output
is_last = false; /// \param[in ] is_last Was this the last block of data?
/// \param[in ] data Data to decode
// Trim data size to first terminator. /// \param[in ] size Length of `data` in bytes
for (size_t k = 0; k < size; k++) ///
if (!data[k]) { size = k; break; } template<class _Ty, class _Ax, class _Tchr>
inline void decode(_Out_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
// Preallocate output {
out.reserve(out.size() + dec_size(size)); is_last = false;
for (size_t i = 0;; i++) { // Trim data size to first terminator.
if (num >= 4) { for (size_t k = 0; k < size; k++)
// Buffer full; decode it. if (!data[k]) { size = k; break; }
size_t nibbles = decode(out);
num = 0; // Preallocate output
if (nibbles < 3) { out.reserve(out.size() + dec_size(size));
is_last = true;
break; for (size_t i = 0;; i++) {
} if (num >= 4) {
} // Buffer full; decode it.
size_t nibbles = decode(out);
if (i >= size) num = 0;
break; if (nibbles < 3) {
is_last = true;
int x = data[i]; break;
if ((buf[num] = x < _countof(lookup) ? lookup[x] : 255) != 255) }
num++; }
}
} if (i >= size)
break;
/// int x = data[i];
/// Resets decoding session if ((buf[num] = x < _countof(lookup) ? lookup[x] : 255) != 255)
/// num++;
inline void clear() }
{ }
num = 0;
}
///
/// Resets decoding session
/// ///
/// Returns maximum decoded size inline void clear()
/// {
/// \param size Number of bytes to decode num = 0;
/// }
/// \returns Maximum number of bytes for the decoded data of `size` length
///
inline size_t dec_size(size_t size) const ///
{ /// Returns maximum decoded size
return ((num + size + 3)/4)*3; ///
} /// \param size Number of bytes to decode
///
/// \returns Maximum number of bytes for the decoded data of `size` length
protected: ///
/// inline size_t dec_size(size_t size) const
/// Decodes one complete internal buffer of data {
/// return ((num + size + 3)/4)*3;
template<class _Ty, class _Ax> }
inline size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
{
out.push_back((_Ty)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff)); protected:
if (buf[2] < 64) { ///
out.push_back((_Ty)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff)); /// Decodes one complete internal buffer of data
if (buf[3] < 64) { ///
out.push_back((_Ty)(((buf[2] << 6) | buf[3]) & 0xff)); template<class _Ty, class _Ax>
return 3; inline size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
} else {
return 2; out.push_back((_Ty)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff));
} else if (buf[2] < 64) {
return 1; out.push_back((_Ty)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff));
} if (buf[3] < 64) {
out.push_back((_Ty)(((buf[2] << 6) | buf[3]) & 0xff));
return 3;
protected: } else
unsigned char buf[4]; ///< Internal buffer return 2;
size_t num; ///< Number of bytes used in `buf` } else
return 1;
/// \cond internal }
static const unsigned char lookup[256];
/// \endcond
}; protected:
unsigned char buf[4]; ///< Internal buffer
/// @} size_t num; ///< Number of bytes used in `buf`
}
/// \cond internal
static const unsigned char lookup[256];
/// \endcond
};
/// @}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,251 +1,248 @@
/* /*
Copyright 1991-2018 Amebis Copyright 1991-2018 Amebis
Copyright 2016 GÉANT Copyright 2016 GÉANT
This file is part of WinStd. This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Setup is distributed in the hope that it will be useful, Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>. along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/ */
/// ///
/// \defgroup WinStdCryptoAPI Cryptography API /// \defgroup WinStdCryptoAPI Cryptography API
/// Integrates WinStd classes with Microsoft Cryptography API /// Integrates WinStd classes with Microsoft Cryptography API
/// ///
#include "Common.h" #include "Common.h"
#include <wincred.h> #include <wincred.h>
#include <memory> #include <memory>
namespace winstd namespace winstd
{ {
template <class _Ty> struct CredFree_delete; template <class _Ty> struct CredFree_delete;
template <class _Ty> struct CredFree_delete<_Ty[]>; template <class _Ty> struct CredFree_delete<_Ty[]>;
} }
/// \addtogroup WinStdCryptoAPI /// \addtogroup WinStdCryptoAPI
/// @{ /// @{
/// ///
/// Enumerates the credentials from the user's credential set. The credential set used is the one associated with the logon session of the current token. The token must not have the user's SID disabled. /// Enumerates the credentials from the user's credential set. The credential set used is the one associated with the logon session of the current token. The token must not have the user's SID disabled.
/// ///
/// \sa [CredEnumerate function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374794.aspx) /// \sa [CredEnumerate function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374794.aspx)
/// ///
inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _In_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials); inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials);
/// @copydoc CredProtectW() /// @copydoc CredProtectW()
template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType); template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType);
/// ///
/// Encrypts the specified credentials so that only the current security context can decrypt them. /// Encrypts the specified credentials so that only the current security context can decrypt them.
/// ///
/// \sa [CredProtect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374803.aspx) /// \sa [CredProtect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374803.aspx)
/// ///
template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType); template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType);
/// @copydoc CredUnprotectW() /// @copydoc CredUnprotectW()
template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials); template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials);
/// ///
/// Decrypts credentials that were previously encrypted by using the CredProtect function. /// Decrypts credentials that were previously encrypted by using the CredProtect function.
/// ///
/// \sa [CredUnprotect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa375186.aspx) /// \sa [CredUnprotect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa375186.aspx)
/// ///
template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials); template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials);
/// @} /// @}
#pragma once #pragma once
namespace winstd namespace winstd
{ {
/// \addtogroup WinStdCryptoAPI /// \addtogroup WinStdCryptoAPI
/// @{ /// @{
/// ///
/// Deleter for unique_ptr using CredFree /// Deleter for unique_ptr using CredFree
/// ///
template <class _Ty> struct CredFree_delete template <class _Ty> struct CredFree_delete
{ {
typedef CredFree_delete<_Ty> _Myt; ///< This type typedef CredFree_delete<_Ty> _Myt; ///< This type
/// ///
/// Default construct /// Default construct
/// ///
CredFree_delete() {} CredFree_delete() {}
/// ///
/// Construct from another CredFree_delete /// Construct from another CredFree_delete
/// ///
template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {} template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
/// ///
/// Delete a pointer /// Delete a pointer
/// ///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx) /// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
/// ///
void operator()(_Ty *_Ptr) const void operator()(_Ty *_Ptr) const
{ {
CredFree(_Ptr); CredFree(_Ptr);
} }
}; };
/// ///
/// Deleter for unique_ptr to array of unknown size using CredFree /// Deleter for unique_ptr to array of unknown size using CredFree
/// ///
template <class _Ty> struct CredFree_delete<_Ty[]> template <class _Ty> struct CredFree_delete<_Ty[]>
{ {
typedef CredFree_delete<_Ty> _Myt; ///< This type typedef CredFree_delete<_Ty> _Myt; ///< This type
/// ///
/// Default construct /// Default construct
/// ///
CredFree_delete() {} CredFree_delete() {}
/// ///
/// Delete a pointer /// Delete a pointer
/// ///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx) /// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
/// ///
void operator()(_Ty *_Ptr) const void operator()(_Ty *_Ptr) const
{ {
CredFree(_Ptr); CredFree(_Ptr);
} }
/// ///
/// Delete a pointer of another type /// Delete a pointer of another type
/// ///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx) /// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
/// ///
template<class _Other> template<class _Other>
void operator()(_Other *) const void operator()(_Other *) const
{ {
CredFree(_Ptr); CredFree(_Ptr);
} }
}; };
/// @} /// @}
} }
inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _In_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials) inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials)
{ {
PCREDENTIAL *pCredentials; PCREDENTIAL *pCredentials = NULL;
if (CredEnumerate(Filter, Flags, Count, &pCredentials)) { BOOL bResult = CredEnumerate(Filter, Flags, Count, &pCredentials);
std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > cred(pCredentials); cCredentials.reset(pCredentials);
cCredentials.swap(cred); return bResult;
return TRUE; }
} else
return FALSE;
} template<class _Elem, class _Traits, class _Ax>
inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
template<class _Elem, class _Traits, class _Ax> _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType) DWORD dwSize = _countof(buf);
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)]; // Try with the stack buffer first.
DWORD dwSize = _countof(buf); if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
// Try with the stack buffer first. sProtectedCredentials.assign(buf, dwSize - 1);
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) { return TRUE;
// Copy from stack. } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
sProtectedCredentials.assign(buf, dwSize - 1); // Allocate on heap and retry.
return TRUE; std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
// Allocate on heap and retry. sProtectedCredentials.assign(buf.get(), dwSize - 1);
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]); return TRUE;
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) { }
sProtectedCredentials.assign(buf.get(), dwSize - 1); }
return TRUE;
} return FALSE;
} }
return FALSE;
} template<class _Elem, class _Traits, class _Ax>
inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
template<class _Elem, class _Traits, class _Ax> _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType) DWORD dwSize = _countof(buf);
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)]; // Try with the stack buffer first.
DWORD dwSize = _countof(buf); if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
// Try with the stack buffer first. sProtectedCredentials.assign(buf, dwSize - 1);
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) { return TRUE;
// Copy from stack. } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
sProtectedCredentials.assign(buf, dwSize - 1); // Allocate on heap and retry.
return TRUE; std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
// Allocate on heap and retry. sProtectedCredentials.assign(buf.get(), dwSize - 1);
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]); return TRUE;
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) { }
sProtectedCredentials.assign(buf.get(), dwSize - 1); }
return TRUE;
} return FALSE;
} }
return FALSE;
} template<class _Elem, class _Traits, class _Ax>
inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials)
{
template<class _Elem, class _Traits, class _Ax> _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials) DWORD dwSize = _countof(buf);
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)]; // Try with the stack buffer first.
DWORD dwSize = _countof(buf); if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
// Try with the stack buffer first. sCredentials.assign(buf, dwSize);
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) { return TRUE;
// Copy from stack. } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
sCredentials.assign(buf, dwSize); // Allocate on heap and retry.
return TRUE; std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
// Allocate on heap and retry. sCredentials.assign(buf.get(), dwSize);
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]); return TRUE;
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) { }
sCredentials.assign(buf.get(), dwSize); }
return TRUE;
} return FALSE;
} }
return FALSE;
} template<class _Elem, class _Traits, class _Ax>
inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials)
{
template<class _Elem, class _Traits, class _Ax> _Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials) DWORD dwSize = _countof(buf);
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)]; // Try with the stack buffer first.
DWORD dwSize = _countof(buf); if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
// Try with the stack buffer first. sCredentials.assign(buf, dwSize);
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) { return TRUE;
// Copy from stack. } else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
sCredentials.assign(buf, dwSize); // Allocate on heap and retry.
return TRUE; std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
// Allocate on heap and retry. sCredentials.assign(buf.get(), dwSize);
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]); return TRUE;
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) { }
sCredentials.assign(buf.get(), dwSize); }
return TRUE;
} return FALSE;
} }
return FALSE;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,190 +1,192 @@
/* /*
Copyright 1991-2018 Amebis Copyright 1991-2018 Amebis
Copyright 2016 GÉANT Copyright 2016 GÉANT
This file is part of WinStd. This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Setup is distributed in the hope that it will be useful, Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>. along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/ */
/// ///
/// \defgroup WinStdHexadecimal Hexadecimal conversion /// \defgroup WinStdHexadecimal Hexadecimal conversion
/// Provides Hexadecimal conversion for WinStd classes /// Provides Hexadecimal conversion for WinStd classes
/// ///
#include "Common.h" #include "Common.h"
#include <string> #include <string>
#include <vector> #include <vector>
namespace winstd namespace winstd
{ {
class WINSTD_API hex_enc; class WINSTD_API hex_enc;
class WINSTD_API hex_dec; class WINSTD_API hex_dec;
} }
#pragma once #pragma once
namespace winstd namespace winstd
{ {
/// \addtogroup WinStdHexadecimal /// \addtogroup WinStdHexadecimal
/// @{ /// @{
/// ///
/// Hexadecimal encoding session /// Hexadecimal encoding session
/// ///
class WINSTD_API hex_enc class WINSTD_API hex_enc
{ {
public: public:
/// ///
/// Constructs blank encoding session /// Constructs blank encoding session
/// ///
inline hex_enc() inline hex_enc()
{ {
} }
/// ///
/// Encodes one block of information, and _appends_ it to the output /// Encodes one block of information, and _appends_ it to the output
/// ///
/// \param[out] out Output /// \param[out] out Output
/// \param[in ] data Data to encode /// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes /// \param[in ] size Length of `data` in bytes
/// ///
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size) inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
{ {
assert(data || !size); assert(data || !size);
// Preallocate output // Preallocate output
out.reserve(out.size() + enc_size(size)); out.reserve(out.size() + enc_size(size));
// Convert data character by character. // Convert data character by character.
for (size_t i = 0; i < size; i++) { for (size_t i = 0; i < size; i++) {
unsigned char unsigned char
x = reinterpret_cast<const unsigned char*>(data)[i], x = reinterpret_cast<const unsigned char*>(data)[i],
x_h = ((x & 0xf0) >> 4), x_h = ((x & 0xf0) >> 4),
x_l = ((x & 0x0f) ); x_l = ((x & 0x0f) );
out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h; out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l; out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
} }
} }
/// ///
/// Returns maximum encoded size /// Returns maximum encoded size
/// ///
/// \param size Number of bytes to encode /// \param size Number of bytes to encode
/// ///
/// \returns Maximum number of bytes for the encoded data of `size` length /// \returns Maximum number of bytes for the encoded data of `size` length
/// ///
inline size_t enc_size(size_t size) const inline size_t enc_size(size_t size) const
{ {
return size*2; return size*2;
} }
}; };
/// ///
/// Hexadecimal decoding session /// Hexadecimal decoding session
/// ///
class WINSTD_API hex_dec class WINSTD_API hex_dec
{ {
public: public:
/// ///
/// Constructs blank decoding session /// Constructs blank decoding session
/// ///
inline hex_dec() : num(0) inline hex_dec() :
{ buf(0),
} num(0)
{
}
///
/// Decodes one block of information, and _appends_ it to the output
/// ///
/// \param[out] out Output /// Decodes one block of information, and _appends_ it to the output
/// \param[out] is_last Was this the last block of data? Actually, is this block of data complete? ///
/// \param[in ] data Data to decode /// \param[inout] out Output
/// \param[in ] size Length of `data` in bytes /// \param[out ] is_last Was this the last block of data? Actually, is this block of data complete?
/// /// \param[in ] data Data to decode
template<class _Ty, class _Ax, class _Tchr> /// \param[in ] size Length of `data` in bytes
inline void decode(_Out_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size) ///
{ template<class _Ty, class _Ax, class _Tchr>
is_last = false; inline void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{
// Trim data size to first terminator. is_last = false;
for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; } // Trim data size to first terminator.
for (size_t k = 0; k < size; k++)
// Preallocate output if (!data[k]) { size = k; break; }
out.reserve(out.size() + dec_size(size));
// Preallocate output
for (size_t i = 0;; i++) { out.reserve(out.size() + dec_size(size));
if (num >= 2) {
// Buffer full. for (size_t i = 0;; i++) {
out.push_back(buf); if (num >= 2) {
num = 0; // Buffer full.
is_last = true; out.push_back(buf);
} else num = 0;
is_last = false; is_last = true;
} else
if (i >= size) is_last = false;
break;
if (i >= size)
int x = data[i]; break;
if ('0' <= x && x <= '9') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - '0'); int x = data[i];
num++; if ('0' <= x && x <= '9') {
} else if ('A' <= x && x <= 'F') { buf = ((buf & 0xf) << 4) | (unsigned char)(x - '0');
buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('A' - 10)); num++;
num++; } else if ('A' <= x && x <= 'F') {
} else if ('a' <= x && x <= 'f') { buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('A' - 10));
buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('a' - 10)); num++;
num++; } else if ('a' <= x && x <= 'f') {
} buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('a' - 10));
} num++;
} }
}
}
///
/// Resets decoding session
/// ///
inline void clear() /// Resets decoding session
{ ///
num = 0; inline void clear()
} {
num = 0;
}
///
/// Returns maximum decoded size
/// ///
/// \param size Number of bytes to decode /// Returns maximum decoded size
/// ///
/// \returns Maximum number of bytes for the decoded data of `size` length /// \param size Number of bytes to decode
/// ///
inline size_t dec_size(size_t size) const /// \returns Maximum number of bytes for the decoded data of `size` length
{ ///
return (size + 1)/2; inline size_t dec_size(size_t size) const
} {
return (size + 1)/2;
}
protected:
unsigned char buf; ///< Internal buffer
size_t num; ///< Number of nibbles used in `buf` protected:
}; unsigned char buf; ///< Internal buffer
size_t num; ///< Number of nibbles used in `buf`
/// @} };
}
/// @}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,137 +1,140 @@
/* /*
Copyright 1991-2018 Amebis Copyright 1991-2018 Amebis
Copyright 2016 GÉANT Copyright 2016 GÉANT
This file is part of WinStd. This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Setup is distributed in the hope that it will be useful, Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>. along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "StdAfx.h" #include "StdAfx.h"
#pragma comment(lib, "Eappcfg.lib") #pragma comment(lib, "Eappcfg.lib")
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::eap_attr // winstd::eap_attr
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::eap_attr::~eap_attr() winstd::eap_attr::~eap_attr()
{ {
if (pValue) if (pValue)
delete []pValue; delete []pValue;
} }
void winstd::eap_attr::create_ms_mppe_key(_In_ BYTE bVendorType, _In_count_(nKeySize) LPCBYTE pbKey, _In_ BYTE nKeySize) void winstd::eap_attr::create_ms_mppe_key(_In_ BYTE bVendorType, _In_count_(nKeySize) LPCBYTE pbKey, _In_ BYTE nKeySize)
{ {
BYTE nPaddingLength = (BYTE)((16 - (1 + (DWORD)nKeySize)) % 16); BYTE nPaddingLength = (BYTE)((16 - (1 + (DWORD)nKeySize)) % 16);
DWORD dwLengthNew = DWORD dwLengthNew =
4 + // Vendor-Id 4 + // Vendor-Id
1 + // Vendor type 1 + // Vendor type
1 + // Vendor length 1 + // Vendor length
2 + // Salt 2 + // Salt
1 + // Key-Length 1 + // Key-Length
nKeySize + // Key nKeySize + // Key
nPaddingLength; // Padding nPaddingLength; // Padding
LPBYTE p = new BYTE[dwLengthNew]; #pragma warning(push)
p[0] = 0x00; // Vendor-Id (0x137 = 311 = Microsoft) #pragma warning(disable: 6386)
p[1] = 0x00; // --| LPBYTE p = new BYTE[dwLengthNew];
p[2] = 0x01; // --| p[0] = 0x00; // Vendor-Id (0x137 = 311 = Microsoft)
p[3] = 0x37; // --^ p[1] = 0x00; // --|
p[4] = bVendorType; // Vendor type p[2] = 0x01; // --|
p[5] = (BYTE)(dwLengthNew - 4); // Vendor length p[3] = 0x37; // --^
p[6] = 0x00; // Salt p[4] = bVendorType; // Vendor type
p[7] = 0x00; // --^ p[5] = (BYTE)(dwLengthNew - 4); // Vendor length
p[8] = nKeySize; // Key-Length p[6] = 0x00; // Salt
memcpy(p + 9, pbKey, nKeySize); // Key p[7] = 0x00; // --^
memset(p + 9 + nKeySize, 0, nPaddingLength); // Padding p[8] = nKeySize; // Key-Length
#pragma warning(pop)
if (pValue) memcpy(p + 9, pbKey, nKeySize); // Key
delete [] pValue; memset(p + 9 + nKeySize, 0, nPaddingLength); // Padding
eaType = eatVendorSpecific; if (pValue)
dwLength = dwLengthNew; delete [] pValue;
pValue = p;
} eaType = eatVendorSpecific;
dwLength = dwLengthNew;
pValue = p;
const EAP_ATTRIBUTE winstd::eap_attr::blank = {}; }
////////////////////////////////////////////////////////////////////// const EAP_ATTRIBUTE winstd::eap_attr::blank = {};
// winstd::eap_packet
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
winstd::eap_packet::~eap_packet() // winstd::eap_packet
{ //////////////////////////////////////////////////////////////////////
if (m_h)
HeapFree(GetProcessHeap(), 0, m_h); winstd::eap_packet::~eap_packet()
} {
if (m_h)
HeapFree(GetProcessHeap(), 0, m_h);
void winstd::eap_packet::free_internal() }
{
HeapFree(GetProcessHeap(), 0, m_h);
} void winstd::eap_packet::free_internal()
{
HeapFree(GetProcessHeap(), 0, m_h);
winstd::eap_packet::handle_type winstd::eap_packet::duplicate_internal(_In_ handle_type h) const }
{
WORD n = ntohs(*(WORD*)h->Length);
handle_type h2 = (handle_type)HeapAlloc(GetProcessHeap(), 0, n); winstd::eap_packet::handle_type winstd::eap_packet::duplicate_internal(_In_ handle_type h) const
if (!h2) { {
SetLastError(ERROR_OUTOFMEMORY); WORD n = ntohs(*(WORD*)h->Length);
return NULL; handle_type h2 = (handle_type)HeapAlloc(GetProcessHeap(), 0, n);
} if (!h2) {
SetLastError(ERROR_OUTOFMEMORY);
memcpy(h2, h, n); return NULL;
}
return h2;
} memcpy(h2, h, n);
return h2;
////////////////////////////////////////////////////////////////////// }
// winstd::eap_method_info_array
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
winstd::eap_method_info_array::~eap_method_info_array() // winstd::eap_method_info_array
{ //////////////////////////////////////////////////////////////////////
if (pEapMethods)
free_internal(); winstd::eap_method_info_array::~eap_method_info_array()
} {
if (pEapMethods)
free_internal();
/// \cond internal }
void winstd::eap_method_info_array::free_internal()
{ /// \cond internal
for (DWORD i = 0; i < dwNumberOfMethods; i++)
free_internal(pEapMethods + i); void winstd::eap_method_info_array::free_internal()
{
EapHostPeerFreeMemory((BYTE*)pEapMethods); for (DWORD i = 0; i < dwNumberOfMethods; i++)
} free_internal(pEapMethods + i);
EapHostPeerFreeMemory((BYTE*)pEapMethods);
void winstd::eap_method_info_array::free_internal(_In_ EAP_METHOD_INFO *pMethodInfo) }
{
if (pMethodInfo->pInnerMethodInfo)
free_internal(pMethodInfo->pInnerMethodInfo); void winstd::eap_method_info_array::free_internal(_In_ EAP_METHOD_INFO *pMethodInfo)
{
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszAuthorName); if (pMethodInfo->pInnerMethodInfo)
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszFriendlyName); free_internal(pMethodInfo->pInnerMethodInfo);
}
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszAuthorName);
/// \endcond EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszFriendlyName);
}
/// \endcond

View File

@ -1,40 +1,42 @@
/* /*
Copyright 1991-2018 Amebis Copyright 1991-2018 Amebis
Copyright 2016 GÉANT Copyright 2016 GÉANT
This file is part of WinStd. This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Setup is distributed in the hope that it will be useful, Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>. along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include "../include/WinStd/Base64.h" #include "../include/WinStd/Base64.h"
#include "../include/WinStd/COM.h" #include "../include/WinStd/COM.h"
#include "../include/WinStd/Cred.h" #include "../include/WinStd/Cred.h"
#include "../include/WinStd/Crypt.h" #include "../include/WinStd/Crypt.h"
#include "../include/WinStd/EAP.h" #include "../include/WinStd/EAP.h"
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA #if _WIN32_WINNT >= _WIN32_WINNT_VISTA
#include "../include/WinStd/ETW.h" #include "../include/WinStd/ETW.h"
#endif #endif
#include "../include/WinStd/Common.h" #include "../include/WinStd/Hex.h"
#include "../include/WinStd/MSI.h" #include "../include/WinStd/MSI.h"
#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL) || defined(SECURITY_MAC) #if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL) || defined(SECURITY_MAC)
#include "../include/WinStd/Sec.h" #include "../include/WinStd/Sec.h"
#endif #endif
#include "../include/WinStd/Shell.h" #include "../include/WinStd/Shell.h"
#include "../include/WinStd/Win.h" #include "../include/WinStd/Win.h"
#include "../include/WinStd/WLAN.h" #include "../include/WinStd/WinTrust.h"
#include "../include/WinStd/WLAN.h"
#include <tchar.h> #include "../include/WinStd/Common.h"
#include <tchar.h>

View File

@ -1,328 +1,328 @@
/* /*
Copyright 1991-2018 Amebis Copyright 1991-2018 Amebis
Copyright 2016 GÉANT Copyright 2016 GÉANT
This file is part of WinStd. This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
Setup is distributed in the hope that it will be useful, Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>. along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "StdAfx.h" #include "StdAfx.h"
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// StringToGuidA // StringToGuidA
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
BOOL WINSTD_API StringToGuidA(_In_z_ LPCSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCSTR *lpszGuidEnd) _Success_(return) BOOL WINSTD_API StringToGuidA(_In_z_ LPCSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCSTR *lpszGuidEnd)
{ {
GUID g; GUID g;
LPSTR lpszEnd; LPSTR lpszEnd;
unsigned long ulTmp; unsigned long ulTmp;
unsigned long long ullTmp; unsigned long long ullTmp;
if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE; if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE;
lpszGuid++; lpszGuid++;
g.Data1 = strtoul(lpszGuid, &lpszEnd, 16); g.Data1 = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE) return FALSE; if (errno == ERANGE) return FALSE;
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16); ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE; if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data2 = (unsigned short)ulTmp; g.Data2 = (unsigned short)ulTmp;
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16); ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE; if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data3 = (unsigned short)ulTmp; g.Data3 = (unsigned short)ulTmp;
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16); ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE; if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff); g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff);
g.Data4[1] = (unsigned char)( ulTmp & 0xff); g.Data4[1] = (unsigned char)( ulTmp & 0xff);
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ullTmp = _strtoui64(lpszGuid, &lpszEnd, 16); ullTmp = _strtoui64(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE; if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE;
g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff); g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff);
g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff); g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff);
g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff); g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff);
g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff); g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff);
g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff); g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff);
g.Data4[7] = (unsigned char)( ullTmp & 0xff); g.Data4[7] = (unsigned char)( ullTmp & 0xff);
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '}') return FALSE; if (*lpszGuid != '}') return FALSE;
lpszGuid++; lpszGuid++;
if (lpszGuidEnd) if (lpszGuidEnd)
*lpszGuidEnd = lpszGuid; *lpszGuidEnd = lpszGuid;
*lpGuid = g; *lpGuid = g;
return TRUE; return TRUE;
} }
BOOL WINSTD_API StringToGuidW(_In_z_ LPCWSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCWSTR *lpszGuidEnd) _Success_(return) BOOL WINSTD_API StringToGuidW(_In_z_ LPCWSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCWSTR *lpszGuidEnd)
{ {
GUID g; GUID g;
LPWSTR lpszEnd; LPWSTR lpszEnd;
unsigned long ulTmp; unsigned long ulTmp;
unsigned long long ullTmp; unsigned long long ullTmp;
if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE; if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE;
lpszGuid++; lpszGuid++;
g.Data1 = wcstoul(lpszGuid, &lpszEnd, 16); g.Data1 = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE) return FALSE; if (errno == ERANGE) return FALSE;
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16); ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE; if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data2 = (unsigned short)ulTmp; g.Data2 = (unsigned short)ulTmp;
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16); ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE; if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data3 = (unsigned short)ulTmp; g.Data3 = (unsigned short)ulTmp;
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16); ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE; if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff); g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff);
g.Data4[1] = (unsigned char)( ulTmp & 0xff); g.Data4[1] = (unsigned char)( ulTmp & 0xff);
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE; if (*lpszGuid != '-') return FALSE;
lpszGuid++; lpszGuid++;
ullTmp = _wcstoui64(lpszGuid, &lpszEnd, 16); ullTmp = _wcstoui64(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE; if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE;
g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff); g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff);
g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff); g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff);
g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff); g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff);
g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff); g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff);
g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff); g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff);
g.Data4[7] = (unsigned char)( ullTmp & 0xff); g.Data4[7] = (unsigned char)( ullTmp & 0xff);
lpszGuid = lpszEnd; lpszGuid = lpszEnd;
if (*lpszGuid != '}') return FALSE; if (*lpszGuid != '}') return FALSE;
lpszGuid++; lpszGuid++;
if (lpszGuidEnd) if (lpszGuidEnd)
*lpszGuidEnd = lpszGuid; *lpszGuidEnd = lpszGuid;
*lpGuid = g; *lpGuid = g;
return TRUE; return TRUE;
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::win_handle // winstd::win_handle
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::win_handle::~win_handle() winstd::win_handle::~win_handle()
{ {
if (m_h) if (m_h)
CloseHandle(m_h); CloseHandle(m_h);
} }
void winstd::win_handle::free_internal() void winstd::win_handle::free_internal()
{ {
CloseHandle(m_h); CloseHandle(m_h);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::library // winstd::library
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::library::~library() winstd::library::~library()
{ {
if (m_h) if (m_h)
FreeLibrary(m_h); FreeLibrary(m_h);
} }
void winstd::library::free_internal() void winstd::library::free_internal()
{ {
FreeLibrary(m_h); FreeLibrary(m_h);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::heap // winstd::heap
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::heap::~heap() winstd::heap::~heap()
{ {
if (m_h) { if (m_h) {
enumerate(); enumerate();
HeapDestroy(m_h); HeapDestroy(m_h);
} }
} }
bool winstd::heap::enumerate() bool winstd::heap::enumerate()
{ {
bool found = false; bool found = false;
// Lock the heap for exclusive access. // Lock the heap for exclusive access.
HeapLock(m_h); HeapLock(m_h);
PROCESS_HEAP_ENTRY e; PROCESS_HEAP_ENTRY e;
e.lpData = NULL; e.lpData = NULL;
while (HeapWalk(m_h, &e) != FALSE) { while (HeapWalk(m_h, &e) != FALSE) {
if ((e.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) { if ((e.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
OutputDebugStr( OutputDebugStr(
_T("Allocated block%s%s\n") _T("Allocated block%s%s\n")
_T(" Data portion begins at: %#p\n Size: %d bytes\n") _T(" Data portion begins at: %#p\n Size: %d bytes\n")
_T(" Overhead: %d bytes\n Region index: %d\n"), _T(" Overhead: %d bytes\n Region index: %d\n"),
(e.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0 ? tstring_printf(_T(", movable with HANDLE %#p"), e.Block.hMem).c_str() : _T(""), (e.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0 ? tstring_printf(_T(", movable with HANDLE %#p"), e.Block.hMem).c_str() : _T(""),
(e.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0 ? _T(", DDESHARE") : _T(""), (e.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0 ? _T(", DDESHARE") : _T(""),
e.lpData, e.lpData,
e.cbData, e.cbData,
e.cbOverhead, e.cbOverhead,
e.iRegionIndex); e.iRegionIndex);
found = true; found = true;
} }
} }
DWORD dwResult = GetLastError(); DWORD dwResult = GetLastError();
if (dwResult != ERROR_NO_MORE_ITEMS) if (dwResult != ERROR_NO_MORE_ITEMS)
OutputDebugStr(_T("HeapWalk failed (error %u).\n"), dwResult); OutputDebugStr(_T("HeapWalk failed (error %u).\n"), dwResult);
// Unlock the heap. // Unlock the heap.
HeapUnlock(m_h); HeapUnlock(m_h);
return found; return found;
} }
void winstd::heap::free_internal() void winstd::heap::free_internal()
{ {
enumerate(); enumerate();
HeapDestroy(m_h); HeapDestroy(m_h);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::actctx_activator // winstd::actctx_activator
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::actctx_activator::actctx_activator(_In_ HANDLE hActCtx) winstd::actctx_activator::actctx_activator(_In_ HANDLE hActCtx)
{ {
if (!ActivateActCtx(hActCtx, &m_cookie)) if (!ActivateActCtx(hActCtx, &m_cookie))
m_cookie = 0; m_cookie = 0;
} }
winstd::actctx_activator::~actctx_activator() winstd::actctx_activator::~actctx_activator()
{ {
if (m_cookie) if (m_cookie)
DeactivateActCtx(0, m_cookie); DeactivateActCtx(0, m_cookie);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::user_impersonator // winstd::user_impersonator
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::user_impersonator::user_impersonator(_In_ HANDLE hToken) winstd::user_impersonator::user_impersonator(_In_ HANDLE hToken)
{ {
m_cookie = ImpersonateLoggedOnUser(hToken); m_cookie = ImpersonateLoggedOnUser(hToken);
} }
winstd::user_impersonator::~user_impersonator() winstd::user_impersonator::~user_impersonator()
{ {
if (m_cookie) if (m_cookie)
RevertToSelf(); RevertToSelf();
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::vmemory // winstd::vmemory
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::vmemory::~vmemory() winstd::vmemory::~vmemory()
{ {
if (m_h) if (m_h)
VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE); VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE);
} }
void winstd::vmemory::free_internal() void winstd::vmemory::free_internal()
{ {
VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE); VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::reg_key // winstd::reg_key
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::reg_key::~reg_key() winstd::reg_key::~reg_key()
{ {
if (m_h) if (m_h)
RegCloseKey(m_h); RegCloseKey(m_h);
} }
void winstd::reg_key::free_internal() void winstd::reg_key::free_internal()
{ {
RegCloseKey(m_h); RegCloseKey(m_h);
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// winstd::security_id // winstd::security_id
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
winstd::security_id::~security_id() winstd::security_id::~security_id()
{ {
if (m_h) if (m_h)
FreeSid(m_h); FreeSid(m_h);
} }
void winstd::security_id::free_internal() void winstd::security_id::free_internal()
{ {
FreeSid(m_h); FreeSid(m_h);
} }