| | |
| | | EventRegistry FullLicenseInfo::validate(int sw_version) { |
| | | EventRegistry er; |
| | | os_initialize(); |
| | | bool sigVerified = OsFunctions::verifySignature(printForSign().c_str(), |
| | | FUNCTION_RETURN sigVer = verifySignature(printForSign().c_str(), |
| | | license_signature.c_str()); |
| | | bool sigVerified = sigVer == FUNC_RET_OK; |
| | | if (sigVerified) { |
| | | er.addEvent(LICENSE_VERIFIED, SVRT_INFO); |
| | | } else { |
| | |
| | | |
| | | #define clean_errno() (errno == 0 ? "None" : strerror(errno)) |
| | | |
| | | #ifdef NDEBUG |
| | | #define LOG_DEBUG(M, ...) _log("[INFO] %s (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| | | #ifdef _DEBUG |
| | | #define LOG_DEBUG(M, ...) _log("[DEBUG] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) |
| | | #else |
| | | #define LOG_DEBUG(M,...) |
| | | #endif |
| | |
| | | VIRTUALIZATION getVirtualization(); |
| | | void os_initialize(); |
| | | |
| | | FUNCTION_RETURN verifySignature(const char* stringToVerify, const char* signatureB64); |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | | #endif |
| | |
| | | #include <iphlpapi.h>
|
| | | //definition of size_t
|
| | | #include <stdlib.h>
|
| | | //#include "../../base/base64.h"
|
| | | #include "../../base/logger.h"
|
| | | #include"../os.h"
|
| | | #include "public-key.h"
|
| | |
|
| | | #pragma comment(lib, "IPHLPAPI.lib")
|
| | | unsigned char* unbase64(const char* ascii, int len, int *flen);
|
| | |
|
| | | FUNCTION_RETURN getOsSpecificIdentifier(unsigned char identifier[6]){
|
| | | return FUNC_RET_NOT_AVAIL;
|
| | |
| | | return result;
|
| | | }
|
| | |
|
| | | static void printHash(HCRYPTHASH* hHash){
|
| | | BYTE *pbHash;
|
| | | DWORD dwHashLen;
|
| | | DWORD dwHashLenSize = sizeof(DWORD); |
| | | char* hashStr;
|
| | | int i;
|
| | |
|
| | | if (CryptGetHashParam(*hHash,HP_HASHSIZE,(BYTE *)&dwHashLen, &dwHashLenSize, 0))
|
| | | {
|
| | | pbHash = (BYTE*)malloc(dwHashLen);
|
| | | hashStr = (char*)malloc(dwHashLen*2 +1);
|
| | | if (CryptGetHashParam(*hHash,HP_HASHVAL,pbHash, &dwHashLen, 0)) {
|
| | | for (i = 0; i < dwHashLen; i++) {
|
| | | sprintf(&hashStr[i * 2], "%02x", pbHash[i]);
|
| | | }
|
| | | LOG_DEBUG("Hash to verify: %s", hashStr);
|
| | | }
|
| | | free(pbHash);
|
| | | free(hashStr);
|
| | | }
|
| | | }
|
| | |
|
| | | FUNCTION_RETURN verifySignature(const char* stringToVerify, const char* signatureB64) {
|
| | | //--------------------------------------------------------------------
|
| | | // Declare variables.
|
| | | //
|
| | | // hProv: Cryptographic service provider (CSP). This example
|
| | | // uses the Microsoft Enhanced Cryptographic |
| | | // Provider.
|
| | | // hKey: Key to be used. In this example, you import the |
| | | // key as a PLAINTEXTKEYBLOB.
|
| | | // dwBlobLen: Length of the plaintext key.
|
| | | // pbKeyBlob: Pointer to the exported key.
|
| | | BYTE pubKey[] = PUBLIC_KEY;
|
| | |
|
| | | HCRYPTPROV hProv = 0;
|
| | | HCRYPTKEY hKey = 0;
|
| | | HCRYPTHASH hHash = 0;
|
| | | DWORD dwSigLen;
|
| | | BYTE* sigBlob;
|
| | |
|
| | | //--------------------------------------------------------------------
|
| | | // Acquire a handle to the CSP.
|
| | |
|
| | | if (!CryptAcquireContext(
|
| | | &hProv,
|
| | | NULL,
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | CRYPT_VERIFYCONTEXT))
|
| | | {
|
| | | // If the key container cannot be opened, try creating a new
|
| | | // container by specifying a container name and setting the |
| | | // CRYPT_NEWKEYSET flag.
|
| | | LOG_INFO("Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | if (NTE_BAD_KEYSET == GetLastError())
|
| | | {
|
| | | if (!CryptAcquireContext(
|
| | | &hProv,
|
| | | "license++verify",
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT))
|
| | | {
|
| | | LOG_ERROR("Error in AcquireContext 0x%08x \n",
|
| | | GetLastError());
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | LOG_ERROR(" Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | | }
|
| | |
|
| | | // Use the CryptImportKey function to import the PLAINTEXTKEYBLOB
|
| | | // BYTE array into the key container. The function returns a |
| | | // pointer to an HCRYPTKEY variable that contains the handle of
|
| | | // the imported key.
|
| | | if (!CryptImportKey(hProv, &pubKey[0], sizeof(pubKey), 0, 0, &hKey))
|
| | | {
|
| | | LOG_ERROR("Error 0x%08x in importing the PublicKey \n",
|
| | | GetLastError());
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | |
|
| | | if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
|
| | | {
|
| | | LOG_DEBUG("Hash object created.");
|
| | | }
|
| | | else
|
| | | {
|
| | | LOG_ERROR("Error in hash creation 0x%08x ", GetLastError());
|
| | | CryptReleaseContext(hProv,0);
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | |
|
| | | if (!CryptHashData(hHash, stringToVerify, strlen(stringToVerify), 0)){
|
| | | LOG_ERROR("Error in hashing data 0x%08x ", GetLastError());
|
| | | CryptDestroyHash(hHash);
|
| | | CryptReleaseContext(hProv, 0);
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | | #ifdef _DEBUG
|
| | | LOG_DEBUG("Lenght %d, hashed Data: [%s]", strlen(stringToVerify), stringToVerify);
|
| | | printHash(&hHash);
|
| | | #endif
|
| | | sigBlob = unbase64(signatureB64, strlen(signatureB64), &dwSigLen);
|
| | | LOG_DEBUG("raw signature lenght %d", dwSigLen);
|
| | | if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0))
|
| | | {
|
| | | LOG_ERROR("Signature not validated! 0x%08x ", GetLastError());
|
| | | free(sigBlob);
|
| | | CryptDestroyHash(hHash);
|
| | | CryptReleaseContext(hProv, 0);
|
| | | return FUNC_RET_ERROR;
|
| | | }
|
| | | CryptDestroyHash(hHash);
|
| | | free(sigBlob);
|
| | | CryptReleaseContext(hProv, 0);
|
| | | return FUNC_RET_OK;
|
| | | }
|
| | |
|
| | |
| | | #include <string>
|
| | | #include "../os-cpp.h"
|
| | | #include "../../base/public-key.h"
|
| | |
|
| | |
|
| | | namespace license {
|
| | |
|
| | | using namespace std;
|
| | |
|
| | |
|
| | | bool OsFunctions::verifySignature(const char* stringToVerify,
|
| | | const char* signatureB64) {
|
| | | return false;
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | VIRTUALIZATION getVirtualization() {
|
| | |
| | | CryptoHelperWindows::CryptoHelperWindows() {
|
| | | m_hCryptProv = NULL;
|
| | | m_hCryptKey = NULL;
|
| | | if (CryptAcquireContext(
|
| | | if (!CryptAcquireContext(
|
| | | &m_hCryptProv,
|
| | | "license-manager2++",
|
| | | "license++sign",
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL, //CRYPT_NEWKEYSET
|
| | | 0)) {
|
| | | PROV_RSA_FULL,
|
| | | 0))
|
| | | {
|
| | | // If the key container cannot be opened, try creating a new
|
| | | // container by specifying a container name and setting the |
| | | // CRYPT_NEWKEYSET flag.
|
| | | printf("Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | if (NTE_BAD_KEYSET == GetLastError())
|
| | | {
|
| | | if (!CryptAcquireContext(
|
| | | &m_hCryptProv,
|
| | | "license++sign",
|
| | | MS_ENHANCED_PROV,
|
| | | PROV_RSA_FULL,
|
| | | CRYPT_NEWKEYSET))
|
| | | {
|
| | | printf("Error in AcquireContext 0x%08x \n",
|
| | | GetLastError());
|
| | | throw logic_error("");
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | throw exception("Error during CryptAcquireContext");
|
| | | printf(" Error in AcquireContext 0x%08x \n", GetLastError());
|
| | | throw logic_error("");
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | |
| | | return ss.str();
|
| | | }
|
| | |
|
| | | void CryptoHelperWindows::printHash(HCRYPTHASH* hHash) const {
|
| | | BYTE *pbHash;
|
| | | DWORD dwHashLen;
|
| | | DWORD dwHashLenSize = sizeof(DWORD);
|
| | | char* hashStr;
|
| | | int i;
|
| | |
|
| | | if (CryptGetHashParam(*hHash, HP_HASHSIZE, (BYTE *)&dwHashLen, &dwHashLenSize, 0))
|
| | | {
|
| | | pbHash = (BYTE*)malloc(dwHashLen);
|
| | | hashStr = (char*)malloc(dwHashLen * 2 + 1);
|
| | | if (CryptGetHashParam(*hHash, HP_HASHVAL, pbHash, &dwHashLen, 0)) {
|
| | | for (i = 0; i < dwHashLen; i++) {
|
| | | sprintf(&hashStr[i * 2], "%02x", pbHash[i]);
|
| | | }
|
| | | printf("hash To be signed: %s \n", hashStr);
|
| | | }
|
| | | free(pbHash);
|
| | | free(hashStr);
|
| | | }
|
| | | }
|
| | |
|
| | | const string CryptoHelperWindows::signString(const void* privateKey, size_t pklen,
|
| | | const string& license) const{
|
| | | BYTE *pbBuffer = (BYTE *)license.c_str();
|
| | | DWORD dwBufferLen = strlen((char *)pbBuffer) + 1;
|
| | | DWORD dwBufferLen = strlen((char *)pbBuffer);
|
| | | HCRYPTHASH hHash;
|
| | |
|
| | | HCRYPTKEY hKey;
|
| | |
| | | //-------------------------------------------------------------------
|
| | | // Acquire a cryptographic provider context handle.
|
| | |
|
| | |
|
| | | //-------------------------------------------------------------------
|
| | | // Get the public at signature key. This is the public key
|
| | | // that will be used by the receiver of the hash to verify
|
| | | // the signature. In situations where the receiver could obtain the
|
| | | // sender's public key from a certificate, this step would not be
|
| | | // needed.
|
| | |
|
| | | if (CryptGetUserKey(
|
| | | m_hCryptProv,
|
| | | AT_SIGNATURE,
|
| | | &hKey))
|
| | | if (!CryptImportKey(m_hCryptProv, (const BYTE *) privateKey, pklen, 0, 0, &hKey))
|
| | | {
|
| | | printf("The signature key has been acquired. \n");
|
| | | }
|
| | | else
|
| | | {
|
| | | printf("Error during CryptGetUserKey for signkey. %d", GetLastError());
|
| | | throw logic_error(string("Error in importing the PrivateKey ") + to_string(GetLastError()));
|
| | | }
|
| | |
|
| | | //-------------------------------------------------------------------
|
| | |
| | | }
|
| | | else
|
| | | {
|
| | | CryptDestroyKey(hKey);
|
| | | throw logic_error(string("Error during CryptCreateHash."));
|
| | | }
|
| | | //-------------------------------------------------------------------
|
| | | // Compute the cryptographic hash of the buffer.
|
| | |
|
| | | if (CryptHashData(
|
| | | hHash,
|
| | | pbBuffer,
|
| | | dwBufferLen,
|
| | | 0))
|
| | | if (CryptHashData(hHash,pbBuffer,dwBufferLen, 0))
|
| | | {
|
| | | printf("The data buffer has been hashed.\n");
|
| | | #ifdef _DEBUG
|
| | | printf("Length of data to be hashed: %d \n", dwBufferLen);
|
| | | printHash(&hHash);
|
| | | #endif |
| | | }
|
| | | else
|
| | | {
|
| | |
| | | pbSignature,
|
| | | &dwSigLen))
|
| | | {
|
| | | printf("pbSignature is the hash signature.\n");
|
| | | printf("pbSignature is the signature length. %d\n", dwSigLen);
|
| | | }
|
| | | else
|
| | | {
|
| | |
| | | //-------------------------------------------------------------------
|
| | | // Destroy the hash object.
|
| | |
|
| | | if (hHash)
|
| | | CryptDestroyHash(hHash);
|
| | |
|
| | | printf("The hash object has been destroyed.\n");
|
| | | printf("The signing phase of this program is completed.\n\n");
|
| | | CryptDestroyHash(hHash);
|
| | | CryptDestroyKey(hKey);
|
| | |
|
| | | CryptBinaryToString(pbSignature,dwSigLen,
|
| | | CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &strLen);
|
| | |
| | | //-------------------------------------------------------------------
|
| | | // Free memory to be used to store signature.
|
| | |
|
| | | if (pbSignature)
|
| | | free(pbSignature);
|
| | |
|
| | |
|
| | | //-------------------------------------------------------------------
|
| | | // Destroy the hash object.
|
| | |
|
| | | if (hHash)
|
| | | CryptDestroyHash(hHash);*/
|
| | |
|
| | |
|
| | | //-------------------------------------------------------------------
|
| | | // Release the provider handle.
|
| | |
|
| | | /*if (hProv)
|
| | | CryptReleaseContext(hProv, 0);*/
|
| | | if (pbSignature)
|
| | | free(pbSignature);
|
| | | return string(&buffer[0]);
|
| | | }
|
| | | } /* namespace license */
|
| | |
| | | HCRYPTPROV m_hCryptProv;
|
| | | // Handle to the cryptography key.
|
| | | HCRYPTKEY m_hCryptKey;
|
| | | void printHash(HCRYPTHASH* hHash) const;
|
| | | public:
|
| | | CryptoHelperWindows();
|
| | |
|
| | |
| | | #define BOOST_TEST_MODULE standard_license_test |
| | | #define BOOST_TEST_MODULE date_test |
| | | //#define BOOST_TEST_MAIN |
| | | //#define BOOST_TEST_DYN_LINK |
| | | #include <boost/test/unit_test.hpp> |