From 62258ba3b4737432a95c3af8d0e03ed3fb7953e7 Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 周一, 13 10月 2014 05:05:37 +0800
Subject: [PATCH] windows ok

---
 test/functional/date_test.cpp                  |    2 
 src/library/LicenseReader.cpp                  |    3 
 src/library/os/os.h                            |    2 
 src/library/os/win/os-win.c                    |  128 +++++++++++++++++++++++++
 src/tools/base_lib/win/CryptoHelperWindows.h   |    1 
 src/library/os/win/os-win.cpp                  |    9 -
 src/tools/base_lib/win/CryptoHelperWindows.cpp |  107 +++++++++++++--------
 src/library/base/logger.h                      |    4 
 8 files changed, 203 insertions(+), 53 deletions(-)

diff --git a/src/library/LicenseReader.cpp b/src/library/LicenseReader.cpp
index 7451b9e..7b1ae65 100644
--- a/src/library/LicenseReader.cpp
+++ b/src/library/LicenseReader.cpp
@@ -52,8 +52,9 @@
 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 {
diff --git a/src/library/base/logger.h b/src/library/base/logger.h
index aa7a1f7..cf08405 100644
--- a/src/library/base/logger.h
+++ b/src/library/base/logger.h
@@ -9,8 +9,8 @@
 
 #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
diff --git a/src/library/os/os.h b/src/library/os/os.h
index 277d969..13c52f9 100644
--- a/src/library/os/os.h
+++ b/src/library/os/os.h
@@ -73,6 +73,8 @@
 VIRTUALIZATION getVirtualization();
 void os_initialize();
 
+FUNCTION_RETURN verifySignature(const char* stringToVerify, const char* signatureB64);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/library/os/win/os-win.c b/src/library/os/win/os-win.c
index a241734..169821b 100644
--- a/src/library/os/win/os-win.c
+++ b/src/library/os/win/os-win.c
@@ -2,9 +2,13 @@
 #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;
@@ -170,3 +174,127 @@
 	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;
+}
+
diff --git a/src/library/os/win/os-win.cpp b/src/library/os/win/os-win.cpp
index 9eff973..654dc06 100644
--- a/src/library/os/win/os-win.cpp
+++ b/src/library/os/win/os-win.cpp
@@ -1,17 +1,10 @@
 #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() {
diff --git a/src/tools/base_lib/win/CryptoHelperWindows.cpp b/src/tools/base_lib/win/CryptoHelperWindows.cpp
index 5fb1019..e2d8cff 100644
--- a/src/tools/base_lib/win/CryptoHelperWindows.cpp
+++ b/src/tools/base_lib/win/CryptoHelperWindows.cpp
@@ -20,16 +20,36 @@
 	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))	{
-		}
-		else
+			PROV_RSA_FULL,
+			0))
 		{
-			throw exception("Error during CryptAcquireContext");
+			// 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
+			{
+				printf(" Error in AcquireContext 0x%08x \n", GetLastError());
+				throw logic_error("");
+			}
 		}
 
 	}
@@ -198,10 +218,32 @@
 		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;
@@ -215,24 +257,9 @@
 		//-------------------------------------------------------------------
 		// 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()));
 		}
 
 		//-------------------------------------------------------------------
@@ -249,18 +276,18 @@
 		}
 		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
 		{
@@ -306,7 +333,7 @@
 			pbSignature,
 			&dwSigLen))
 		{
-			printf("pbSignature is the hash signature.\n");
+			printf("pbSignature is the signature length. %d\n", dwSigLen);
 		}
 		else
 		{
@@ -315,11 +342,9 @@
 		//-------------------------------------------------------------------
 		// 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);
@@ -410,20 +435,20 @@
 		//-------------------------------------------------------------------
 		// 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 */
diff --git a/src/tools/base_lib/win/CryptoHelperWindows.h b/src/tools/base_lib/win/CryptoHelperWindows.h
index e0e8753..a432e27 100644
--- a/src/tools/base_lib/win/CryptoHelperWindows.h
+++ b/src/tools/base_lib/win/CryptoHelperWindows.h
@@ -27,6 +27,7 @@
 	HCRYPTPROV m_hCryptProv;
 	//	Handle to the cryptography key.
 	HCRYPTKEY m_hCryptKey;
+	void printHash(HCRYPTHASH* hHash) const;
 public:
 	CryptoHelperWindows();
 
diff --git a/test/functional/date_test.cpp b/test/functional/date_test.cpp
index 3210a45..c15ab4e 100644
--- a/test/functional/date_test.cpp
+++ b/test/functional/date_test.cpp
@@ -1,4 +1,4 @@
-#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>

--
Gitblit v1.9.1