| | |
| | | #include <wincrypt.h> |
| | | #include <iphlpapi.h> |
| | | #include <windows.h> |
| | | #pragma comment(lib, "bcrypt.lib") |
| | | //#pragma comment(lib, "bcrypt.lib") |
| | | |
| | | #include <public_key.h> |
| | | #include "../../base/logger.h" |
| | |
| | | using namespace std; |
| | | #define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0) |
| | | |
| | | static const void formatError(DWORD status, const char* description) { |
| | | static const void formatError(DWORD status, const char *description) { |
| | | char msgBuffer[256]; |
| | | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, status, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &msgBuffer[0], |
| | | sizeof(msgBuffer) - 1, nullptr); |
| | |
| | | cout << (len & 0x80) << endl; |
| | | if ((len & 0x80) > 0) { |
| | | size_t blen = len & 0x7F; |
| | | for (int i = 0; i < blen; i++) { |
| | | for (size_t i = 0; i < blen; i++) { |
| | | result += (*(ptr++) << (i * 8)); |
| | | } |
| | | } else { |
| | |
| | | if (expected_length < length) { |
| | | return FUNC_RET_ERROR; |
| | | } |
| | | for (int i = 0; i < length; i++) { |
| | | for (size_t i = 0; i < length; i++) { |
| | | location[i] = *(ptr++); |
| | | } |
| | | return FUNC_RET_OK; |
| | |
| | | sizeof(pubk), 0))) { |
| | | result = FUNC_RET_OK; |
| | | } else { |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | formatError(status, "error importing public key"); |
| | | #endif |
| | | } |
| | |
| | | DWORD status; |
| | | FUNCTION_RETURN result = FUNC_RET_ERROR; |
| | | PBYTE pbSignature = nullptr; |
| | | DWORD dwSigLen; |
| | | BYTE* sigBlob = nullptr; |
| | | BCRYPT_ALG_HANDLE hSignAlg = nullptr; |
| | | |
| | | // FIXME!! |
| | | sigBlob = unbase64(signatureBuffer.c_str(), (int)signatureBuffer.size(), (int*)&dwSigLen); |
| | | vector<uint8_t> signatureBlob = unbase64(signatureBuffer); |
| | | DWORD dwSigLen = (DWORD) signatureBlob.size(); |
| | | BYTE* sigBlob = &signatureBlob[0]; |
| | | |
| | | if (NT_SUCCESS(status = BCryptOpenAlgorithmProvider(&hSignAlg, BCRYPT_RSA_ALGORITHM, NULL, 0))) { |
| | | if ((result = readPublicKey(hSignAlg, &phKey)) == FUNC_RET_OK) { |
| | |
| | | result = FUNC_RET_OK; |
| | | } else { |
| | | result = FUNC_RET_ERROR; |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | formatError(status, "error verifying signature"); |
| | | #endif |
| | | } |
| | | } else { |
| | | LOG_DEBUG("Error reading public key"); |
| | | } |
| | | } else { |
| | | } |
| | | else { |
| | | result = FUNC_RET_NOT_AVAIL; |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | formatError(status, "error opening RSA provider"); |
| | | #endif |
| | | } |
| | |
| | | if (hSignAlg != nullptr) { |
| | | BCryptCloseAlgorithmProvider(hSignAlg, 0); |
| | | } |
| | | if (sigBlob) { |
| | | free(sigBlob); |
| | | } |
| | | //if (sigBlob) { |
| | | // free(sigBlob); |
| | | //} |
| | | return result; |
| | | } |
| | | |
| | |
| | | result = verifyHash(pbHashData, cbHashDataLenght, signatureB64); |
| | | } else { |
| | | result = FUNC_RET_NOT_AVAIL; |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | formatError(status, "error hashing data"); |
| | | #endif |
| | | } |
| | | } else { |
| | | result = FUNC_RET_NOT_AVAIL; |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | formatError(status, "error creating hash"); |
| | | #endif |
| | | } |
| | |
| | | } |
| | | } else { |
| | | result = FUNC_RET_NOT_AVAIL; |
| | | #ifdef _DEBUG |
| | | #ifndef NDEBUG |
| | | formatError(status, "**** Error returned by BCryptGetProperty"); |
| | | #endif |
| | | } |