open-license-manager
2014-10-13 bc33836f06e315005db152854ceaa199ed752820
src/library/os/win/os-win.c
@@ -18,8 +18,7 @@
   FUNCTION_RETURN result = FUNC_RET_ERROR;
   char buffer[MAX_COMPUTERNAME_LENGTH + 1];
   int bufsize = MAX_COMPUTERNAME_LENGTH + 1;
   BOOL cmpName = GetComputerName(
      buffer, &bufsize);
   BOOL cmpName = GetComputerName(buffer, &bufsize);
   if (cmpName){
      strncpy(identifier, buffer, 6);
      result = FUNC_RET_OK;
@@ -52,38 +51,38 @@
   FUNCTION_RETURN return_value;
   DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives);
   if (dwResult > 0 && dwResult <= MAX_PATH)
   {
   if (dwResult > 0 && dwResult <= MAX_PATH) {
      return_value = FUNC_RET_OK;
      szSingleDrive = szLogicalDrives;
      while (*szSingleDrive && ndrives < MAX_UNITS)
      {
      while (*szSingleDrive && ndrives < MAX_UNITS) {
         // get the next drive
         driveType = GetDriveType(szSingleDrive);
         if (driveType == DRIVE_FIXED){
            success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH, &volSerial,
               &FileMaxLen, &FileFlags, FileSysName, MAX_PATH);
            success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH,
                  &volSerial, &FileMaxLen, &FileFlags, FileSysName,
                  MAX_PATH);
            if (success) {
               LOG_INFO("drive         : %s\n", szSingleDrive);
               LOG_INFO("Volume Name   : %s\n", volName);
               LOG_INFO("Volume Serial : 0x%x\n", volSerial);
               LOG_DEBUG("Max file length : %d\n", FileMaxLen);
               LOG_DEBUG("Filesystem      : %s\n", FileSysName);
               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);
                  strncpy(diskInfos[ndrives].label, FileSysName,
                        MAX_PATH);
                  diskInfos[ndrives].id = ndrives;
                  diskInfos[ndrives].preferred = (strncmp(szSingleDrive, "C", 1) != 0);
                  diskInfos[ndrives].preferred = (strncmp(szSingleDrive,
                        "C", 1) != 0);
               }
               ndrives++;
            }
            else {
               LOG_WARN("Unable to retrieve information of '%s'\n", szSingleDrive);
            } else {
               LOG_WARN("Unable to retrieve information of '%s'\n",
                     szSingleDrive);
            }
         }
         LOG_INFO("This volume is not fixed : %s, type: %d\n", szSingleDrive);
         LOG_INFO("This volume is not fixed : %s, type: %d\n",
               szSingleDrive);
         szSingleDrive += strlen(szSingleDrive) + 1;
      }
   }
@@ -104,8 +103,7 @@
      if (isdigit((unsigned char)*str2)) {
         ipv4[index] *= 10;
         ipv4[index] += *str2 - '0';
      }
      else {
      } else {
         index++;
      }
      str2++;
@@ -115,7 +113,8 @@
//http://stackoverflow.com/questions/18046063/mac-address-using-c
//TODO: count only interfaces with type (MIB_IF_TYPE_ETHERNET IF_TYPE_IEEE80211)
FUNCTION_RETURN getAdapterInfos(OsAdapterInfo * adapterInfos, size_t * adapter_info_size) {
FUNCTION_RETURN getAdapterInfos(OsAdapterInfo * adapterInfos,
      size_t * adapter_info_size) {
   DWORD dwStatus;
   unsigned int i = 0;
   FUNCTION_RETURN result;
@@ -125,7 +124,8 @@
   i = 3;
   do{
      pAdapterInfo = (PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO)*dwBufLen);
      pAdapterInfo = (PIP_ADAPTER_INFO) malloc(
            sizeof(IP_ADAPTER_INFO) * dwBufLen);
      dwStatus = GetAdaptersInfo(               // Call GetAdapterInfo
         pAdapterInfo, // [out] buffer to receive data
         &dwBufLen   // [in] size of receive data buffer
@@ -150,9 +150,12 @@
   i = 0;
   result = FUNC_RET_OK;
   while (pAdapter) {
      strncpy(adapterInfos[i].description, pAdapter->Description, min(sizeof(adapterInfos->description), MAX_ADAPTER_DESCRIPTION_LENGTH));
      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);
      translate(pAdapter->IpAddressList.IpAddress.String,
            adapterInfos[i].ipv4_address);
      adapterInfos[i].type = IFACE_TYPE_ETHERNET;
      i++;
      pAdapter = pAdapter->Next;
@@ -181,22 +184,22 @@
   char* hashStr;
   int i;
   if (CryptGetHashParam(*hHash,HP_HASHSIZE,(BYTE *)&dwHashLen, &dwHashLenSize, 0))
   {
   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);
         } LOG_DEBUG("Hash to verify: %s", hashStr);
      }
      free(pbHash);
      free(hashStr);
   }
}
FUNCTION_RETURN verifySignature(const char* stringToVerify,   const char* signatureB64) {
FUNCTION_RETURN verifySignature(const char* stringToVerify,
      const char* signatureB64) {
   //--------------------------------------------------------------------
   // Declare variables.
   //
@@ -218,33 +221,20 @@
   //--------------------------------------------------------------------
   // Acquire a handle to the CSP.
   if (!CryptAcquireContext(
      &hProv,
      NULL,
      MS_ENHANCED_PROV,
      PROV_RSA_FULL,
      CRYPT_VERIFYCONTEXT))
   {
   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());
      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
      {
      } else {
         LOG_ERROR(" Error in AcquireContext 0x%08x \n", GetLastError());
         return FUNC_RET_ERROR;
      }
@@ -254,19 +244,14 @@
   // 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());
   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))
   {
   if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) {
      LOG_DEBUG("Hash object created.");
   }
   else
   {
   } else {
      LOG_ERROR("Error in hash creation 0x%08x ", GetLastError());
      CryptReleaseContext(hProv,0);
      return FUNC_RET_ERROR;
@@ -284,8 +269,7 @@
#endif
   sigBlob = unbase64(signatureB64, strlen(signatureB64), &dwSigLen);
   LOG_DEBUG("raw signature lenght %d", dwSigLen);
   if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0))
   {
   if (!CryptVerifySignature(hHash, sigBlob, dwSigLen, hKey, NULL, 0)) {
      LOG_ERROR("Signature not validated!  0x%08x ", GetLastError());
      free(sigBlob);
      CryptDestroyHash(hHash);