GateKeeper.cpp
changeset 127 7f39af9b8dee
parent 126 8cf20db557f1
child 128 a026de7eb3cd
     1.1 --- a/GateKeeper.cpp	Fri Jun 24 21:32:46 2016 +0200
     1.2 +++ b/GateKeeper.cpp	Fri Jun 24 22:24:47 2016 +0200
     1.3 @@ -348,8 +348,9 @@
     1.4  
     1.5      void GateKeeper::update_product(product p, DWORD context)
     1.6      {
     1.7 +        BCRYPT_KEY_HANDLE dk = delivery_key();
     1.8  #ifdef UNICODE
     1.9 -        tstring delivery = utility::utf16_string(wrapped_delivery_key(delivery_key()));
    1.10 +        tstring delivery = utility::utf16_string(wrapped_delivery_key(dk));
    1.11  #else
    1.12          tstring delivery = wrapped_delivery_key(delivery_key());
    1.13  #endif
    1.14 @@ -362,16 +363,8 @@
    1.15          if (hUrl == NULL)
    1.16              return;
    1.17  
    1.18 -        TCHAR temp_path[MAX_PATH + 1];
    1.19 -        GetTempPath(MAX_PATH, temp_path);
    1.20 -        tstring filename = temp_path;
    1.21 -        filename += _T("\\pEp_");
    1.22 -        filename += delivery.substr(0, 32);
    1.23 -        filename += _T(".msi");
    1.24 -
    1.25 -        HANDLE hFile = CreateFile(filename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    1.26 -        if (!hFile)
    1.27 -            goto closing;
    1.28 +        string crypted;
    1.29 +        string unencrypted;
    1.30  
    1.31          do {
    1.32              static char buffer[32768];
    1.33 @@ -379,26 +372,61 @@
    1.34              BOOL bResult = InternetReadFile(hUrl, buffer, 32768, &reading);
    1.35              if (!bResult || !reading)
    1.36                  break;
    1.37 -            DWORD writing;
    1.38 -            WriteFile(hFile, buffer, reading, &writing, NULL);
    1.39 +            crypted += string(buffer, reading);
    1.40          } while (1);
    1.41  
    1.42 -        CloseHandle(hFile);
    1.43 -        hFile = NULL;
    1.44          InternetCloseHandle(hUrl);
    1.45          hUrl = NULL;
    1.46  
    1.47 +        tstring filename;
    1.48 +        HANDLE hFile = NULL;
    1.49 +        char *unencrypted_buffer = NULL;
    1.50 +
    1.51 +        ULONG unencrypted_size;
    1.52 +        NTSTATUS status = BCryptDecrypt(dk, (PUCHAR) crypted.data(), crypted.size(),
    1.53 +                NULL, NULL, 0, NULL, 0, &unencrypted_size, 0);
    1.54 +        if (status)
    1.55 +            goto closing;
    1.56 +        
    1.57 +        unencrypted_buffer = new char[unencrypted_size];
    1.58 +
    1.59 +        status = BCryptDecrypt(dk, (PUCHAR) crypted.data(), crypted.size(),
    1.60 +            NULL, NULL, 0, (PUCHAR) unencrypted_buffer, unencrypted_size, &unencrypted_size, 0);
    1.61 +        if (status) {
    1.62 +            delete[] unencrypted_buffer;
    1.63 +            goto closing;
    1.64 +        }
    1.65 +
    1.66 +        TCHAR temp_path[MAX_PATH + 1];
    1.67 +        GetTempPath(MAX_PATH, temp_path);
    1.68 +        filename = temp_path;
    1.69 +        filename += _T("\\pEp_");
    1.70 +        filename += delivery.substr(0, 32);
    1.71 +        filename += _T(".msi");
    1.72 +
    1.73 +        hFile = CreateFile(filename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    1.74 +        if (!hFile)
    1.75 +            goto closing;
    1.76 +        DWORD writing;
    1.77 +        WriteFile(hFile, unencrypted_buffer, unencrypted_size, &writing, NULL);
    1.78 +        CloseHandle(hFile);
    1.79 +
    1.80          install_msi(filename);
    1.81 +
    1.82          DeleteFile(filename.c_str());
    1.83 -
    1.84 +        BCryptDestroyKey(dk);
    1.85          return;
    1.86  
    1.87      closing:
    1.88 +        if (unencrypted_buffer)
    1.89 +            delete[] unencrypted_buffer;
    1.90          if (hFile)
    1.91              CloseHandle(hFile);
    1.92          if (hUrl)
    1.93              InternetCloseHandle(hUrl);
    1.94 -        DeleteFile(filename.c_str());
    1.95 +        if (filename.length())
    1.96 +            DeleteFile(filename.c_str());
    1.97 +        BCryptDestroyKey(dk);
    1.98      }
    1.99  
   1.100      void GateKeeper::keep_updated()