open-license-manager
2014-10-13 62258ba3b4737432a95c3af8d0e03ed3fb7953e7
src/library/os/win/os-win.c
@@ -1,22 +1,34 @@
#include <Windows.h>
#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;
}
FUNCTION_RETURN getMachineName(unsigned char identifier[6]) {
   FUNCTION_RETURN result = FUNC_RET_ERROR;
   char buffer[MAX_COMPUTERNAME_LENGTH + 1];
   int bufsize = MAX_COMPUTERNAME_LENGTH + 1;
   BOOL cmpName = GetComputerName(
      buffer, &bufsize);
   strncpy(identifier, buffer, 6);
   if (cmpName){
      strncpy(identifier, buffer, 6);
      result = FUNC_RET_OK;
   }
   return result;
}
FUNCTION_RETURN getCpuId(unsigned char identifier[6]) {
   return FUNC_RET_NOT_AVAIL;
}
void os_initialize() {
@@ -30,6 +42,7 @@
   int ndrives = 0;
   DWORD FileFlags;
   char volName[_MAX_FNAME], FileSysName[_MAX_FNAME];
   char* szSingleDrive;
   DWORD volSerial = 0;
   BOOL success;
   UINT driveType;
@@ -42,7 +55,7 @@
   if (dwResult > 0 && dwResult <= MAX_PATH)
   {
      return_value = FUNC_RET_OK;
      char* szSingleDrive = szLogicalDrives;
      szSingleDrive = szLogicalDrives;
      while (*szSingleDrive && ndrives < MAX_UNITS)
      {
@@ -57,11 +70,11 @@
               LOG_INFO("Volume Serial : 0x%x\n", volSerial);
               LOG_DEBUG("Max file length : %d\n", FileMaxLen);
               LOG_DEBUG("Filesystem      : %s\n", FileSysName);
               if (diskInfos != NULL && * disk_info_size<ndrives){
                  strncpy(diskInfos[ndrives].device,volName,MAX_PATH);
                  strncpy(diskInfos[ndrives].label, FileSysName , MAX_PATH);
                  diskInfos[ndrives].id=ndrives;
                  diskInfos[ndrives].preferred = (strncmp(szSingleDrive,"C",1)!=0);
               if (diskInfos != NULL && * disk_info_size < ndrives){
                  strncpy(diskInfos[ndrives].device, volName, MAX_PATH);
                  strncpy(diskInfos[ndrives].label, FileSysName, MAX_PATH);
                  diskInfos[ndrives].id = ndrives;
                  diskInfos[ndrives].preferred = (strncmp(szSingleDrive, "C", 1) != 0);
               }
               ndrives++;
@@ -132,7 +145,7 @@
      return FUNC_RET_BUFFER_TOO_SMALL;
   }
   memset(adapterInfos, 0,*adapter_info_size);
   memset(adapterInfos, 0, *adapter_info_size);
   pAdapter = pAdapterInfo;
   i = 0;
   result = FUNC_RET_OK;
@@ -140,6 +153,7 @@
      strncpy(adapterInfos[i].description, pAdapter->Description, min(sizeof(adapterInfos->description), MAX_ADAPTER_DESCRIPTION_LENGTH));
      memcpy(adapterInfos[i].mac_address, pAdapter->Address, 8);
      translate(pAdapter->IpAddressList.IpAddress.String, adapterInfos[i].ipv4_address);
      adapterInfos[i].type = IFACE_TYPE_ETHERNET;
      i++;
      pAdapter = pAdapter->Next;
      if (i == *adapter_info_size){
@@ -151,4 +165,136 @@
   *adapter_info_size = i;
   return result;
}
FUNCTION_RETURN getModuleName(char buffer[MAX_PATH]) {
   FUNCTION_RETURN result=FUNC_RET_OK;
   DWORD wres = GetModuleFileName(NULL, buffer, MAX_PATH);
   if (wres == 0){
      result = FUNC_RET_ERROR;
   }
   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;
}