Relocate data encryption key

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-01-04 10:20:37 +01:00
parent 40193b7a68
commit 840e055bf6
7 changed files with 99 additions and 29 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -18,9 +18,6 @@
along with GÉANTLink. If not, see <http://www.gnu.org/licenses/>.
*/
#define IDR_EAP_KEY_PUBLIC 1
#define IDR_EAP_KEY_PRIVATE 2
///
/// \defgroup EAPBaseStream Memory Packaging
/// Simple serialization/deserialization of data to/from memory opaque BLOBs

View File

@ -411,20 +411,14 @@ namespace eap
template<class _Ty, class _Ax>
std::vector<_Ty, _Ax> decrypt(_In_ HCRYPTPROV hProv, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ HCRYPTHASH hHash = NULL) const
{
// Import the private RSA key.
HRSRC res = FindResource(m_instance, MAKEINTRESOURCE(IDR_EAP_KEY_PRIVATE), RT_RCDATA);
if (!res)
throw winstd::win_runtime_error(__FUNCTION__ " FindResource failed.");
HGLOBAL res_handle = LoadResource(m_instance, res);
if (!res_handle)
throw winstd::win_runtime_error(__FUNCTION__ " LoadResource failed.");
// Import the RSA key.
winstd::crypt_key key_rsa;
std::unique_ptr<unsigned char[], winstd::LocalFree_delete<unsigned char[]> > keyinfo_data;
DWORD keyinfo_size = 0;
if (!CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_RSA_PRIVATE_KEY, (const BYTE*)::LockResource(res_handle), ::SizeofResource(m_instance, res), CRYPT_DECODE_ALLOC_FLAG, NULL, &keyinfo_data, &keyinfo_size))
if (!CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_RSA_PRIVATE_KEY, s_rsa_key, sizeof(s_rsa_key), CRYPT_DECODE_ALLOC_FLAG, NULL, &keyinfo_data, &keyinfo_size))
throw winstd::win_runtime_error(__FUNCTION__ " CryptDecodeObjectEx failed.");
if (!key_rsa.import(hProv, keyinfo_data.get(), keyinfo_size, NULL, 0))
throw winstd::win_runtime_error(__FUNCTION__ " Private key import failed.");
throw winstd::win_runtime_error(__FUNCTION__ " Key import failed.");
// Import the 256-bit AES session key.
winstd::crypt_key key_aes;
@ -712,12 +706,16 @@ namespace eap
/// @}
public:
HINSTANCE m_instance; ///< Windows module instance
const winstd::eap_type_t m_eap_method; ///< EAP method type
HINSTANCE m_instance; ///< Windows module instance
const winstd::eap_type_t m_eap_method; ///< EAP method type
protected:
winstd::heap m_heap; ///< Heap
mutable winstd::event_provider m_ep; ///< Event Provider
winstd::heap m_heap; ///< Heap
mutable winstd::event_provider m_ep; ///< Event Provider
/// \cond internal
static const unsigned char s_rsa_key[1191];
/// \endcond
};

View File

@ -200,20 +200,14 @@ std::vector<unsigned char> eap::module::encrypt(_In_ HCRYPTPROV hProv, _In_bytec
if (!CryptGenKey(hProv, CALG_AES_256, MAKELONG(CRYPT_EXPORTABLE, 256), &key_aes))
throw win_runtime_error(__FUNCTION__ " CryptGenKey failed.");
// Import the public RSA key.
HRSRC res = FindResource(m_instance, MAKEINTRESOURCE(IDR_EAP_KEY_PUBLIC), RT_RCDATA);
if (!res)
throw winstd::win_runtime_error(__FUNCTION__ " FindResource failed.");
HGLOBAL res_handle = LoadResource(m_instance, res);
if (!res_handle)
throw winstd::win_runtime_error(__FUNCTION__ " LoadResource failed.");
crypt_key key_rsa;
unique_ptr<CERT_PUBLIC_KEY_INFO, LocalFree_delete<CERT_PUBLIC_KEY_INFO> > keyinfo_data;
// Import the RSA key.
winstd::crypt_key key_rsa;
std::unique_ptr<unsigned char[], winstd::LocalFree_delete<unsigned char[]> > keyinfo_data;
DWORD keyinfo_size = 0;
if (!CryptDecodeObjectEx(X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO, (const BYTE*)::LockResource(res_handle), ::SizeofResource(m_instance, res), CRYPT_DECODE_ALLOC_FLAG, NULL, &keyinfo_data, &keyinfo_size))
throw win_runtime_error(__FUNCTION__ " CryptDecodeObjectEx failed.");
if (!key_rsa.import_public(hProv, X509_ASN_ENCODING, keyinfo_data.get()))
throw win_runtime_error(__FUNCTION__ " Public key import failed.");
if (!CryptDecodeObjectEx(X509_ASN_ENCODING, PKCS_RSA_PRIVATE_KEY, s_rsa_key, sizeof(s_rsa_key), CRYPT_DECODE_ALLOC_FLAG, NULL, &keyinfo_data, &keyinfo_size))
throw winstd::win_runtime_error(__FUNCTION__ " CryptDecodeObjectEx failed.");
if (!key_rsa.import(hProv, keyinfo_data.get(), keyinfo_size, NULL, 0))
throw winstd::win_runtime_error(__FUNCTION__ " Key import failed.");
// Export AES session key encrypted with public RSA key.
vector<unsigned char, sanitizing_allocator<unsigned char> > buf;
@ -258,6 +252,87 @@ std::vector<unsigned char> eap::module::encrypt_md5(_In_ HCRYPTPROV hProv, _In_b
}
/// \cond internal
const unsigned char eap::module::s_rsa_key[1191] = {
0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb5, 0x70, 0x43, 0x1a,
0x1d, 0x2b, 0xa1, 0x9e, 0x6a, 0x1c, 0xac, 0x80, 0x4b, 0x15, 0xb1, 0x4b, 0xc8, 0x9b, 0xc5, 0xf4,
0x28, 0xad, 0x6c, 0x1d, 0x67, 0xb5, 0xbb, 0xde, 0x5c, 0x75, 0xb4, 0x66, 0x2a, 0xcc, 0x4c, 0x5e,
0x55, 0xd4, 0x97, 0xb1, 0xbc, 0xf7, 0xe8, 0xb7, 0xe5, 0x92, 0x57, 0x16, 0x85, 0xa1, 0xf7, 0xbb,
0x74, 0x34, 0xd1, 0x57, 0x19, 0x70, 0x2a, 0x0b, 0xe1, 0xa9, 0x20, 0xb4, 0x78, 0x25, 0x25, 0xf1,
0x78, 0x3b, 0xf9, 0x2a, 0xf8, 0x8c, 0xa5, 0xee, 0x52, 0x95, 0xc2, 0x15, 0x28, 0x30, 0x37, 0x8a,
0xe2, 0x9b, 0x98, 0x47, 0x05, 0xfd, 0xbb, 0xb3, 0xa3, 0xf2, 0x1b, 0xe3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0xd6, 0x8a, 0x84, 0xd2, 0xfb,
0x34, 0x20, 0xd4, 0x6a, 0x8a, 0x44, 0x3b, 0xf2, 0x47, 0x11, 0x95, 0x59, 0x68, 0x44, 0x22, 0xa0,
0x07, 0x8d, 0x16, 0x59, 0x02, 0x81, 0x80, 0x4f, 0xe8, 0xc7, 0x9a, 0xee, 0x2a, 0x81, 0xf6, 0x2f,
0x01, 0x71, 0xba, 0xdb, 0xf0, 0x91, 0x6b, 0x08, 0xd9, 0x2b, 0x2b, 0x1d, 0x5f, 0xd8, 0x09, 0x8c,
0x71, 0x44, 0xde, 0xaf, 0xae, 0x25, 0xac, 0xd2, 0xa1, 0xc5, 0x6f, 0xd8, 0xf6, 0x9a, 0xc7, 0x47,
0x6b, 0xd5, 0xa0, 0x11, 0x64, 0xf1, 0xbb, 0x25, 0x3b, 0x03, 0x6b, 0x6f, 0xd7, 0x77, 0xfd, 0x91,
0x71, 0xe0, 0xe9, 0xcf, 0x79, 0x0b, 0x0e, 0x57, 0x98, 0xd7, 0x42, 0x8f, 0x53, 0x51, 0x77, 0xd9,
0x29, 0x9e, 0xed, 0x49, 0x2c, 0x37, 0xd5, 0xca, 0x5b, 0xc6, 0xb6, 0xd0, 0x6a, 0x1f, 0x4f, 0x2f,
0x06, 0xf7, 0xf3, 0x4f, 0x73, 0x36, 0xff, 0x2f, 0x1b, 0x4e, 0x6a, 0x7e, 0xa7, 0x68, 0xa3, 0xae,
0x0a, 0x8b, 0xeb, 0xbc, 0xb6, 0x96, 0x75, 0xe9, 0x1c, 0xae, 0x65, 0xff, 0xa1, 0x75, 0xa2, 0x1c,
0xee, 0xdd, 0xf3, 0xc9, 0x62, 0x6b, 0x0a
};
/// \endcond
//////////////////////////////////////////////////////////////////////
// eap::peer
//////////////////////////////////////////////////////////////////////