/* Copyright 2015-2016 Amebis Copyright 2016 GÉANT This file is part of GÉANTLink. GÉANTLink is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. GÉANTLink is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GÉANTLink. If not, see . */ #include "StdAfx.h" using namespace std; using namespace winstd; ////////////////////////////////////////////////////////////////////// // eap::credentials_tls ////////////////////////////////////////////////////////////////////// eap::credentials_tls::credentials_tls(_In_ module &mod) : credentials(mod) { } eap::credentials_tls::credentials_tls(_In_ const credentials_tls &other) : m_cert(other.m_cert), credentials(other) { } eap::credentials_tls::credentials_tls(_Inout_ credentials_tls &&other) : m_cert(std::move(other.m_cert)), credentials(std::move(other)) { } eap::credentials_tls& eap::credentials_tls::operator=(_In_ const credentials_tls &other) { if (this != &other) { (credentials&)*this = other; m_cert = other.m_cert; } return *this; } eap::credentials_tls& eap::credentials_tls::operator=(_Inout_ credentials_tls &&other) { if (this != &other) { (credentials&)*this = std::move(other); m_cert = std::move(other.m_cert); } return *this; } eap::config* eap::credentials_tls::clone() const { return new credentials_tls(*this); } void eap::credentials_tls::clear() { credentials::clear(); m_cert.free(); } bool eap::credentials_tls::empty() const { return credentials::empty() && !m_cert; } bool eap::credentials_tls::save(_In_ IXMLDOMDocument *pDoc, _In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR **ppEapError) const { assert(pDoc); assert(pConfigRoot); assert(ppEapError); if (!credentials::save(pDoc, pConfigRoot, ppEapError)) return false; const bstr bstrNamespace(L"urn:ietf:params:xml:ns:yang:ietf-eap-metadata"); DWORD dwResult; HRESULT hr; // com_obj pXmlElClientCertificate; if ((dwResult = eapxml::create_element(pDoc, bstr(L"ClientCertificate"), bstrNamespace, &pXmlElClientCertificate))) { *ppEapError = m_module.make_error(dwResult, _T(__FUNCTION__) _T(" Error creating element.")); return false; } if (m_cert) { // / if ((dwResult = eapxml::put_element_value(pDoc, pXmlElClientCertificate, bstr(L"format"), bstrNamespace, bstr(L"PEM"))) != ERROR_SUCCESS) { *ppEapError = m_module.make_error(dwResult, _T(__FUNCTION__) _T(" Error creating element.")); return false; } // / if ((dwResult = eapxml::put_element_base64(pDoc, pXmlElClientCertificate, bstr(L"cert-data"), bstrNamespace, m_cert->pbCertEncoded, m_cert->cbCertEncoded)) != ERROR_SUCCESS) { *ppEapError = m_module.make_error(dwResult, _T(__FUNCTION__) _T(" Error creating element.")); return false; } } if (FAILED(hr = pConfigRoot->appendChild(pXmlElClientCertificate, NULL))) { *ppEapError = m_module.make_error(HRESULT_CODE(hr), _T(__FUNCTION__) _T(" Error appending element.")); return false; } return true; } bool eap::credentials_tls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR **ppEapError) { assert(pConfigRoot); assert(ppEapError); DWORD dwResult; if (!credentials::load(pConfigRoot, ppEapError)) return false; std::wstring xpath(eapxml::get_xpath(pConfigRoot)); m_cert.free(); // com_obj pXmlElClientCertificate; if ((dwResult = eapxml::select_element(pConfigRoot, bstr(L"eap-metadata:ClientCertificate"), &pXmlElClientCertificate)) != ERROR_SUCCESS) { *ppEapError = m_module.make_error(dwResult, _T(__FUNCTION__) _T(" Error reading element."), _T("Please make sure profile XML is a valid ") _T(PRODUCT_NAME_STR) _T(" profile XML document.")); return false; } // / bstr bstrFormat; if ((dwResult = eapxml::get_element_value(pXmlElClientCertificate, bstr(L"eap-metadata:format"), &bstrFormat)) == ERROR_SUCCESS) { if (CompareStringEx(LOCALE_NAME_INVARIANT, NORM_IGNORECASE, bstrFormat, bstrFormat.length(), L"PEM", -1, NULL, NULL, 0) == CSTR_EQUAL) { // / vector aData; if ((dwResult = eapxml::get_element_base64(pXmlElClientCertificate, bstr(L"eap-metadata:cert-data"), aData)) == ERROR_SUCCESS) m_cert.create(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, aData.data(), (DWORD)aData.size()); } } m_module.log_config((xpath + L"/ClientCertificate").c_str(), get_name().c_str()); return true; } bool eap::credentials_tls::store(_In_ LPCTSTR pszTargetName, _Out_ EAP_ERROR **ppEapError) const { assert(pszTargetName); assert(ppEapError); // Encrypt the certificate using user's key. DATA_BLOB cred_blob = { m_cert->cbCertEncoded, m_cert->pbCertEncoded }; DATA_BLOB entropy_blob = { sizeof(s_entropy) , (LPBYTE)s_entropy }; data_blob cred_enc; if (!CryptProtectData(&cred_blob, NULL, &entropy_blob, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &cred_enc)) { *ppEapError = m_module.make_error(GetLastError(), _T(__FUNCTION__) _T(" CryptProtectData failed.")); return false; } tstring target(target_name(pszTargetName)); wstring name(std::move(get_name())); // Write credentials. assert(cred_enc.cbData < CRED_MAX_CREDENTIAL_BLOB_SIZE); assert(name.length() < CRED_MAX_USERNAME_LENGTH ); CREDENTIAL cred = { 0, // Flags CRED_TYPE_GENERIC, // Type (LPTSTR)target.c_str(), // TargetName _T(""), // Comment { 0, 0 }, // LastWritten cred_enc.cbData, // CredentialBlobSize cred_enc.pbData, // CredentialBlob CRED_PERSIST_ENTERPRISE, // Persist 0, // AttributeCount NULL, // Attributes NULL, // TargetAlias (LPTSTR)name.c_str() // UserName }; if (!CredWrite(&cred, 0)) { *ppEapError = m_module.make_error(GetLastError(), _T(__FUNCTION__) _T(" CredWrite failed.")); return false; } return true; } bool eap::credentials_tls::retrieve(_In_ LPCTSTR pszTargetName, _Out_ EAP_ERROR **ppEapError) { assert(pszTargetName); // Read credentials. unique_ptr > cred; if (!CredRead(target_name(pszTargetName).c_str(), CRED_TYPE_GENERIC, 0, (PCREDENTIAL*)&cred)) { *ppEapError = m_module.make_error(GetLastError(), _T(__FUNCTION__) _T(" CredRead failed.")); return false; } // Decrypt the certificate using user's key. DATA_BLOB cred_enc = { cred->CredentialBlobSize, cred->CredentialBlob }; DATA_BLOB entropy_blob = { sizeof(s_entropy) , (LPBYTE)s_entropy }; data_blob cred_int; if (!CryptUnprotectData(&cred_enc, NULL, &entropy_blob, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN | CRYPTPROTECT_VERIFY_PROTECTION, &cred_int)) { *ppEapError = m_module.make_error(GetLastError(), _T(__FUNCTION__) _T(" CryptUnprotectData failed.")); return false; } bool bResult = m_cert.create(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, cred_int.pbData, cred_int.cbData); SecureZeroMemory(cred_int.pbData, cred_int.cbData); if (!bResult) { *ppEapError = m_module.make_error(GetLastError(), _T(__FUNCTION__) _T(" Error loading certificate.")); return false; } m_module.log_config((wstring(pszTargetName) + L"/Certificate").c_str(), get_name().c_str()); return true; } std::wstring eap::credentials_tls::get_identity() const { if (m_cert) { wstring identity; CertGetNameString(m_cert, CERT_NAME_EMAIL_TYPE, 0, NULL, identity); return identity; } else return L""; } tstring eap::credentials_tls::get_name() const { return m_cert ? std::move(eap::get_cert_title(m_cert)) : L""; } LPCTSTR eap::credentials_tls::target_suffix() const { return _T("TLS"); } const unsigned char eap::credentials_tls::s_entropy[1024] = { 0xb9, 0xd1, 0x62, 0xd4, 0x1c, 0xe6, 0x8c, 0x25, 0x98, 0x9b, 0x1d, 0xbc, 0x40, 0x46, 0x9e, 0x6d, 0x63, 0xba, 0xda, 0x78, 0x65, 0x56, 0x97, 0x4f, 0xa0, 0x89, 0xf4, 0xc5, 0x1b, 0xf5, 0x8d, 0x69, 0xa1, 0x8c, 0xf3, 0xf9, 0x91, 0x86, 0x7f, 0xf3, 0x47, 0x2e, 0x23, 0x61, 0xde, 0x4f, 0x61, 0x94, 0xba, 0xba, 0x27, 0x63, 0x0b, 0xf0, 0x4e, 0xa0, 0x24, 0xab, 0x17, 0x62, 0x3f, 0xc4, 0xd8, 0xad, 0xd6, 0x03, 0x1f, 0x3b, 0xdd, 0x88, 0xf7, 0x9a, 0x56, 0xf4, 0x0d, 0xce, 0x9b, 0x18, 0x33, 0x54, 0x5a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