GateKeeper.cpp
author Thomas
Thu, 05 Dec 2019 12:17:32 +0100
branchCOM-106
changeset 382 bf5a83a0c9bc
parent 380 4093982a8fe9
permissions -rw-r--r--
COM-106: Provide method to try to update product
     1 #include "stdafx.h"
     2 
     3 #include "GateKeeper.h"
     4 #include "pEpCOMServerAdapter.h"
     5 #include "utf8_helper.h"
     6 
     7 using namespace std;
     8 
     9 // from https://msdn.microsoft.com/en-us/library/windows/desktop/dd388945(v=vs.85).aspx
    10 
    11 struct PUBLIC_KEY_VALUES {
    12     BLOBHEADER blobheader;
    13     RSAPUBKEY rsapubkey;
    14     BYTE modulus[4096];
    15 };
    16 
    17 static void ReverseMemCopy(
    18     _Out_ BYTE       *pbDest,
    19     _In_  BYTE const *pbSource,
    20     _In_  DWORD       cb
    21 )
    22 {
    23     for (DWORD i = 0; i < cb; i++) {
    24         pbDest[cb - 1 - i] = pbSource[i];
    25     }
    26 }
    27 
    28 static NTSTATUS ImportRsaPublicKey(
    29     _In_ BCRYPT_ALG_HANDLE  hAlg,    // CNG provider
    30     _In_ PUBLIC_KEY_VALUES *pKey,    // Pointer to the RSAPUBKEY blob.
    31     _In_ BCRYPT_KEY_HANDLE *phKey    // Receives a handle the imported public key.
    32 )
    33 {
    34     NTSTATUS hr = 0;
    35 
    36     BYTE *pbPublicKey = NULL;
    37     DWORD cbKey = 0;
    38 
    39     // Layout of the RSA public key blob:
    40 
    41     //  +----------------------------------------------------------------+
    42     //  |     BCRYPT_RSAKEY_BLOB    | BE( dwExp ) |   BE( Modulus )      |
    43     //  +----------------------------------------------------------------+
    44     //
    45     //  sizeof(BCRYPT_RSAKEY_BLOB)       cbExp           cbModulus 
    46     //  <--------------------------><------------><---------------------->
    47     //
    48     //   BE = Big Endian Format                                                     
    49 
    50     DWORD cbModulus = (pKey->rsapubkey.bitlen + 7) / 8;
    51     DWORD dwExp = pKey->rsapubkey.pubexp;
    52     DWORD cbExp = (dwExp & 0xFF000000) ? 4 :
    53         (dwExp & 0x00FF0000) ? 3 :
    54         (dwExp & 0x0000FF00) ? 2 : 1;
    55 
    56     BCRYPT_RSAKEY_BLOB *pRsaBlob;
    57     PBYTE pbCurrent;
    58 
    59     if (!SUCCEEDED(hr = DWordAdd(cbModulus, sizeof(BCRYPT_RSAKEY_BLOB), &cbKey))) {
    60         goto cleanup;
    61     }
    62 
    63     cbKey += cbExp;
    64 
    65     pbPublicKey = (PBYTE)CoTaskMemAlloc(cbKey);
    66     if (pbPublicKey == NULL) {
    67         hr = E_OUTOFMEMORY;
    68         goto cleanup;
    69     }
    70 
    71     ZeroMemory(pbPublicKey, cbKey);
    72     pRsaBlob = (BCRYPT_RSAKEY_BLOB *)(pbPublicKey);
    73 
    74     //
    75     // Make the Public Key Blob Header
    76     //
    77 
    78     pRsaBlob->Magic = BCRYPT_RSAPUBLIC_MAGIC;
    79     pRsaBlob->BitLength = pKey->rsapubkey.bitlen;
    80     pRsaBlob->cbPublicExp = cbExp;
    81     pRsaBlob->cbModulus = cbModulus;
    82     pRsaBlob->cbPrime1 = 0;
    83     pRsaBlob->cbPrime2 = 0;
    84 
    85     pbCurrent = (PBYTE)(pRsaBlob + 1);
    86 
    87     //
    88     // Copy pubExp Big Endian 
    89     //
    90 
    91     ReverseMemCopy(pbCurrent, (PBYTE)&dwExp, cbExp);
    92     pbCurrent += cbExp;
    93 
    94     //
    95     // Copy Modulus Big Endian 
    96     //
    97 
    98     ReverseMemCopy(pbCurrent, pKey->modulus, cbModulus);
    99 
   100     //
   101     // Import the public key
   102     //
   103 
   104     hr = BCryptImportKeyPair(hAlg, NULL, BCRYPT_RSAPUBLIC_BLOB, phKey, (PUCHAR)pbPublicKey, cbKey, 0);
   105 
   106 cleanup:
   107     CoTaskMemFree(pbPublicKey);
   108     return hr;
   109 }
   110 
   111 namespace pEp {
   112     const LPCTSTR GateKeeper::plugin_reg_path = _T("Software\\Microsoft\\Office\\Outlook\\Addins\\pEp");
   113     const LPCTSTR GateKeeper::plugin_reg_value_name = _T("LoadBehavior");
   114     const LPCTSTR GateKeeper::updater_reg_path = _T("Software\\pEp\\Updater");
   115 
   116     const time_t GateKeeper::cycle = 7200;   // 7200 sec is 2 h
   117     const time_t GateKeeper::fraction = 10;  // first update is at 10% of cycle
   118     const DWORD GateKeeper::waiting = 10000; //  10 sec
   119 
   120     GateKeeper::GateKeeper(CpEpCOMServerAdapterModule * self)
   121         : _self(self), now(time(NULL)), next(now /*+ time_diff()*/), hkUpdater(NULL),
   122         internet(NULL), hAES(NULL), hRSA(NULL)
   123     {
   124         if (the_gatekeeper)
   125             throw runtime_error("second instance of GateKeeper was initialized");
   126 
   127         DeleteFile(get_lockFile().c_str());
   128 
   129         LONG lResult = RegOpenCurrentUser(KEY_READ, &cu);
   130         assert(lResult == ERROR_SUCCESS);
   131         if (lResult == ERROR_SUCCESS)
   132             cu_open = true;
   133         else
   134             cu_open = false;
   135 
   136         if (cu_open) {
   137             LONG lResult = RegOpenKeyEx(cu, updater_reg_path, 0, KEY_READ, &hkUpdater);
   138             if (lResult != ERROR_SUCCESS)
   139                 RegCreateKeyEx(cu, updater_reg_path, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &hkUpdater, NULL);
   140         }
   141 
   142         the_gatekeeper = this;
   143     }
   144 
   145     GateKeeper::~GateKeeper()
   146     {
   147         the_gatekeeper = nullptr;
   148 
   149         if (cu_open) {
   150             if (hkUpdater)
   151                 RegCloseKey(hkUpdater);
   152             RegCloseKey(cu);
   153         }
   154     }
   155 
   156     time_t GateKeeper::time_diff()
   157     {
   158         try {
   159             static random_device rd;
   160             static mt19937 gen(rd());
   161 
   162             uniform_int_distribution<time_t> dist(0, cycle / fraction);
   163 
   164             return dist(gen);
   165         }
   166         catch (exception&) {
   167             assert(0);
   168             return 0;
   169         }
   170     }
   171 
   172     void GateKeeper::keep()
   173     {
   174         if (!cu_open)
   175             return;
   176 
   177         while (1) {
   178             keep_plugin();
   179 
   180             now = time(NULL);
   181             assert(now != -1);
   182 
   183             if (now > next) {
   184                 next = now + GateKeeper::cycle;
   185                 try_update(NULL);
   186             }
   187 
   188             Sleep(waiting);
   189         }
   190     }
   191 
   192     void GateKeeper::keep_plugin()
   193     {
   194         HKEY hkPluginStart = NULL;
   195 
   196         LONG lResult = RegOpenKeyEx(cu, plugin_reg_path, 0, KEY_WRITE, &hkPluginStart);
   197         if (lResult != ERROR_SUCCESS)
   198             return;
   199 
   200         DWORD v = 3;
   201         lResult = RegSetValueEx(hkPluginStart, plugin_reg_value_name, 0, REG_DWORD, (const BYTE *)&v, sizeof(DWORD));
   202         assert(lResult == ERROR_SUCCESS);
   203 
   204         RegCloseKey(hkPluginStart);
   205     }
   206 
   207     string GateKeeper::update_key()
   208     {
   209         static string key;
   210 
   211         if (key.length() == 0) {
   212             HRSRC res = FindResource(_self->hModule(), MAKEINTRESOURCE(IRD_UPDATEKEY), RT_RCDATA);
   213             assert(res);
   214             if (!res)
   215                 throw runtime_error("FindResource: IRD_UPDATEKEY");
   216 
   217             HGLOBAL hRes = LoadResource(_self->hModule(), res);
   218             assert(hRes);
   219             if (!hRes)
   220                 throw runtime_error("LoadResource: IRD_UPDATEKEY");
   221 
   222             key = string((char *)LockResource(hRes), SizeofResource(_self->hModule(), res));
   223             UnlockResource(hRes);
   224         }
   225 
   226         return key;
   227     }
   228 
   229     BCRYPT_KEY_HANDLE GateKeeper::delivery_key()
   230     {
   231         aeskey_t key;
   232 
   233         static random_device rd;
   234         static mt19937 gen(rd());
   235 
   236         uniform_int_distribution<int64_t> dist(0, UINT32_MAX);
   237 
   238         for (int i = 0; i < 8; i++)
   239             key.dw_key[i] = (uint32_t)dist(gen);
   240 
   241         BCRYPT_KEY_HANDLE hKey;
   242 
   243         NTSTATUS status = BCryptGenerateSymmetricKey(hAES, &hKey, NULL, 0, (PUCHAR)&key, (ULONG) sizeof(aeskey_t), 0);
   244         assert(status == 0);
   245         if (status)
   246             throw runtime_error("BCryptGenerateSymmetricKey");
   247 
   248 #ifndef NDEBUG
   249         DWORD keylength = 0;
   250         ULONG copied = 0;
   251         status = BCryptGetProperty(hKey, BCRYPT_KEY_LENGTH, (PUCHAR)&keylength, sizeof(DWORD), &copied, 0);
   252         assert(keylength == 256);
   253 #endif
   254 
   255         return hKey;
   256     }
   257 
   258     string GateKeeper::wrapped_delivery_key(BCRYPT_KEY_HANDLE hDeliveryKey)
   259     {
   260         string result;
   261 
   262         BCRYPT_KEY_HANDLE hUpdateKey = NULL;
   263         string _update_key = update_key();
   264 
   265         PCERT_PUBLIC_KEY_INFO uk = NULL;
   266         DWORD uk_size = 0;
   267 
   268         BOOL bResult = CryptDecodeObjectEx(X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO,
   269             (const BYTE *)_update_key.data(), _update_key.size(), CRYPT_DECODE_ALLOC_FLAG, NULL, &uk, &uk_size);
   270         if (!bResult)
   271             throw runtime_error("CryptDecodeObjectEx: X509_PUBLIC_KEY_INFO");
   272 
   273         PUBLIC_KEY_VALUES *_uk = NULL;
   274         DWORD _uk_size = 0;
   275 
   276         bResult = CryptDecodeObjectEx(X509_ASN_ENCODING, RSA_CSP_PUBLICKEYBLOB,
   277             uk->PublicKey.pbData, uk->PublicKey.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &_uk, &_uk_size);
   278         LocalFree(uk);
   279         if (!bResult)
   280             throw runtime_error("CryptDecodeObjectEx: X509_PUBLIC_KEY_INFO");
   281 
   282         HRESULT hResult = ImportRsaPublicKey(hRSA, _uk, &hUpdateKey);
   283         LocalFree(_uk);
   284         if (!hUpdateKey)
   285             throw runtime_error("ImportRsaPublicKey");
   286 
   287         ULONG psize;
   288         NTSTATUS status = BCryptGetProperty(hUpdateKey, BCRYPT_ALGORITHM_NAME, NULL, 0, &psize, 0);
   289         char *prop = new char[psize];
   290         TCHAR *_prop = (TCHAR *)prop;
   291         status = BCryptGetProperty(hUpdateKey, BCRYPT_ALGORITHM_NAME, (PUCHAR)prop, psize, &psize, 0);
   292         if (status)
   293             throw runtime_error("BCryptGetProperty: BCRYPT_ALGORITHM_NAME");
   294 
   295         ULONG export_size;
   296         status = BCryptExportKey(hDeliveryKey, NULL, BCRYPT_KEY_DATA_BLOB, NULL, NULL,
   297             &export_size, 0);
   298         if (status)
   299             throw runtime_error("BCryptExportKey: measuring export size");
   300 
   301         PUCHAR _delivery_key = new UCHAR[export_size];
   302         ULONG copied;
   303         status = BCryptExportKey(hDeliveryKey, NULL, BCRYPT_KEY_DATA_BLOB, _delivery_key, export_size,
   304             &copied, 0);
   305         if (status) {
   306             delete[] _delivery_key;
   307             throw runtime_error("BCryptExportKey: delivery_key");
   308         }
   309 
   310         BCRYPT_OAEP_PADDING_INFO pi;
   311         memset(&pi, 0, sizeof(BCRYPT_OAEP_PADDING_INFO));
   312         pi.pszAlgId = BCRYPT_SHA256_ALGORITHM;
   313 
   314         ULONG result_size = 0;
   315         PUCHAR _result = NULL;
   316         ULONG blob_size = export_size - sizeof(BCRYPT_KEY_DATA_BLOB_HEADER);
   317         PUCHAR blob = _delivery_key + sizeof(BCRYPT_KEY_DATA_BLOB_HEADER);
   318         status = BCryptEncrypt(hUpdateKey, blob, blob_size, &pi, NULL, 0, NULL, 0, &result_size, BCRYPT_PAD_OAEP);
   319         if (status) {
   320             delete[] _delivery_key;
   321             BCryptDestroyKey(hUpdateKey);
   322             throw runtime_error("BCryptEncrypt: calculating result size");
   323         }
   324 
   325         _result = new UCHAR[result_size + 1];
   326         status = BCryptEncrypt(hUpdateKey, blob, blob_size, &pi, NULL, 0, _result, result_size, &copied, BCRYPT_PAD_OAEP);
   327         delete[] _delivery_key;
   328         if (status) {
   329             BCryptDestroyKey(hUpdateKey);
   330             delete[] _result;
   331             throw runtime_error("BCryptEncrypt: encrypting using update_key");
   332         }
   333 
   334         BCryptDestroyKey(hUpdateKey);
   335 
   336         stringstream s;
   337         for (ULONG i = 0; i < copied; i++) {
   338             s << hex << setw(2) << setfill('0');
   339             s << (int)_result[i];
   340         }
   341         delete[] _result;
   342         s >> result;
   343 
   344         return result;
   345     }
   346 
   347     GateKeeper::product_list GateKeeper::registered_products()
   348     {
   349         product_list products;
   350 
   351         // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx
   352         static TCHAR value_name[16384];
   353         DWORD value_name_size;
   354         static TCHAR value[L_MAX_URL_LENGTH + 1];
   355         DWORD value_size;
   356 
   357         LONG lResult = ERROR_SUCCESS;
   358         for (DWORD i = 0; lResult == ERROR_SUCCESS; i++) {
   359             value_name_size = 16383;
   360             value_size = L_MAX_URL_LENGTH + 1;
   361             lResult = RegEnumValue(hkUpdater, i, value_name, &value_name_size, NULL, NULL, (LPBYTE)value, &value_size);
   362             if (lResult == ERROR_SUCCESS) {
   363                 products.push_back({ value_name, value });
   364             }
   365         }
   366 
   367         return products;
   368     }
   369 
   370     void GateKeeper::execute_file(tstring filename)
   371     {
   372         HANDLE hMutex = CreateMutex(NULL, TRUE, _T("PEPINSTALLERMUTEX"));
   373         if (hMutex) {
   374             CloseHandle(hMutex);
   375             ShellExecute(NULL, _T("open"), filename.c_str(), NULL, NULL, SW_SHOW);
   376         }
   377     }
   378 
   379     tstring GateKeeper::get_lockFile()
   380     {
   381         static const tstring _fileName = _T("\\pEpSetup.lck");
   382         static tstring fileName;
   383 
   384         if (fileName.length() == 0) {
   385             unique_ptr < TCHAR[] > _pathName(new TCHAR[MAX_PATH + 1]);
   386             DWORD size = GetTempPath(MAX_PATH, _pathName.get());
   387             if (size > MAX_PATH - _fileName.size())
   388                 throw runtime_error("TEMP path too long");
   389 
   390             fileName = _pathName.get();
   391             fileName += _fileName;
   392         }
   393 
   394         return fileName;
   395     }
   396 
   397     // Retrieving Headers Using HTTP_QUERY_CUSTOM
   398     static tstring httpQueryCustom(HINTERNET hHttp, tstring header)
   399     {
   400         DWORD dwResult = 0;
   401         LPTSTR lpOutBuffer = StrDup(header.c_str());
   402 
   403     retry:
   404 
   405         if (!HttpQueryInfo(hHttp, HTTP_QUERY_CUSTOM, (LPVOID)lpOutBuffer, &dwResult, NULL))
   406         {
   407             if (GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND)
   408             {
   409                 // Code to handle the case where the header isn't available.
   410                 LocalFree(lpOutBuffer);
   411                 throw(runtime_error("ERROR_HTTP_HEADER_NOT_FOUND"));
   412             }
   413             else
   414             {
   415                 // Check for an insufficient buffer.
   416                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
   417                 {
   418                     // Allocate the necessary buffer.
   419                     LocalFree(lpOutBuffer);
   420                     lpOutBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, dwResult + 1);
   421 
   422                     // Rewrite the header name in the buffer.
   423                     StringCchPrintf(lpOutBuffer, dwResult, header.c_str());
   424 
   425                     // Retry the call.
   426                     goto retry;
   427                 }
   428                 else
   429                 {
   430                     // Error handling code.
   431                     LocalFree(lpOutBuffer);
   432                     // FIXME: Add GetLastError()
   433                     throw(runtime_error("Unknown"));
   434                 }
   435             }
   436         }
   437 
   438         tstring result(lpOutBuffer);
   439         LocalFree(lpOutBuffer);
   440 
   441         return result;
   442     }
   443 
   444     bool GateKeeper::update_product(product p, DWORD context)
   445     {
   446         {
   447             HANDLE file = CreateFile(get_lockFile().c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
   448             if (file == INVALID_HANDLE_VALUE) {
   449                 return false;
   450             }
   451             else {
   452                 CloseHandle(file);
   453                 DeleteFile(get_lockFile().c_str());
   454             }
   455         }
   456 
   457         BCRYPT_KEY_HANDLE dk = delivery_key();
   458 #ifdef UNICODE
   459         tstring delivery = utility::utf16_string(wrapped_delivery_key(dk));
   460 #else
   461         tstring delivery = wrapped_delivery_key(delivery_key());
   462 #endif
   463         tstring url = p.second;
   464         url += _T("&challenge=");
   465         url += delivery;
   466         tstring headers;
   467         HINTERNET hUrl = InternetOpenUrl(internet, url.c_str(), headers.c_str(), headers.length(),
   468             INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE, context);
   469         if (hUrl == NULL)
   470             return false;
   471 
   472         string crypted;
   473         string unencrypted;
   474         UCHAR iv[12];
   475         UCHAR nonce[sizeof(iv)];
   476         UCHAR tag[16];
   477         tstring filename;
   478         HANDLE hFile = NULL;
   479         char *unencrypted_buffer = NULL;
   480         bool result = false;
   481 
   482         try {
   483 
   484             DWORD reading;
   485             InternetReadFile(hUrl, iv, sizeof(iv), &reading);
   486 
   487             if (reading) do {
   488                 static char buffer[1024 * 1024];
   489                 BOOL bResult = InternetReadFile(hUrl, buffer, 1024 * 1024, &reading);
   490                 if (!bResult || !reading)
   491                     break;
   492                 crypted += string(buffer, reading);
   493             } while (1);
   494 
   495             tstring contentDisposition = httpQueryCustom(hUrl, _T("Content-Disposition"));
   496 
   497             tregex filenameRegex(_T("filename=.([^\"]*)"), regex::extended); //FIXME: case insensitive
   498             tsmatch match;
   499 
   500             if (regex_search(contentDisposition, match, filenameRegex)) {
   501                 filename = match[1];
   502             }
   503 
   504             InternetCloseHandle(hUrl);
   505             hUrl = NULL;
   506 
   507             memcpy(nonce, iv, sizeof(iv));
   508 
   509             BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO authInfo;
   510             BCRYPT_INIT_AUTH_MODE_INFO(authInfo);
   511             authInfo.pbNonce = nonce;
   512             authInfo.cbNonce = sizeof(nonce);
   513             authInfo.pbTag = tag;
   514             authInfo.cbTag = sizeof(tag);
   515 
   516             ULONG unencrypted_size;
   517             NTSTATUS status = BCryptDecrypt(dk, (PUCHAR)crypted.data(), crypted.size(),
   518                 &authInfo, iv, sizeof(iv), NULL, 0, &unencrypted_size, 0);
   519             if (status)
   520                 goto closing;
   521 
   522             unencrypted_buffer = new char[unencrypted_size];
   523 
   524             PUCHAR crypted_data = (PUCHAR)crypted.data();
   525             ULONG crypted_size = (ULONG)crypted.size() - sizeof(tag);
   526             memcpy(tag, crypted_data + crypted_size, sizeof(tag));
   527 
   528             status = BCryptDecrypt(dk, crypted_data, crypted_size,
   529                 &authInfo, iv, sizeof(iv), (PUCHAR)unencrypted_buffer, unencrypted_size, &unencrypted_size, 0);
   530             if (status)
   531                 goto closing;
   532 
   533             BCryptDestroyKey(dk);
   534 
   535             TCHAR temp_path[MAX_PATH + 1];
   536             GetTempPath(MAX_PATH, temp_path);
   537 
   538             if (filename == _T("")) {
   539                 filename = temp_path;
   540                 filename += _T("\\pEp_");
   541                 filename += delivery.substr(0, 32);
   542                 filename += _T(".msi");
   543             }
   544             else {
   545                 filename = tstring(temp_path) + _T("\\") + filename;
   546             }
   547 
   548             hFile = CreateFile(filename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
   549             if (!hFile)
   550                 goto closing;
   551             DWORD writing;
   552             WriteFile(hFile, unencrypted_buffer, unencrypted_size, &writing, NULL);
   553             CloseHandle(hFile);
   554             hFile = NULL;
   555             delete[] unencrypted_buffer;
   556             unencrypted_buffer = nullptr;
   557         }
   558         catch (exception&) {
   559             goto closing;
   560         }
   561 
   562         execute_file(filename);
   563         result = true;
   564 
   565     closing:
   566         if (unencrypted_buffer)
   567             delete[] unencrypted_buffer;
   568         if (hFile)
   569             CloseHandle(hFile);
   570         if (hUrl)
   571             InternetCloseHandle(hUrl);
   572         BCryptDestroyKey(dk);
   573 
   574         return result;
   575     }
   576 
   577     bool GateKeeper::try_update(product *p)
   578     {
   579         bool result = false;
   580 
   581         NTSTATUS status = BCryptOpenAlgorithmProvider(&hAES, BCRYPT_AES_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
   582         assert(status == 0);
   583         if (status)
   584             goto closing;
   585         status = BCryptSetProperty(hAES, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM, sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
   586         if (status)
   587             goto closing;
   588 
   589         status = BCryptOpenAlgorithmProvider(&hRSA, BCRYPT_RSA_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
   590         assert(status == 0);
   591         if (status)
   592             goto closing;
   593 
   594         internet = InternetOpen(_T("pEp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
   595         if (!internet)
   596             goto closing;
   597 
   598         if (p)
   599         {
   600             try {
   601                 result = update_product(*p);
   602             }
   603             catch (exception&) {
   604                 result = false;
   605             }
   606         }
   607         else
   608         {
   609             product_list products = registered_products();
   610             DWORD context = 0;
   611 
   612             for (auto i = products.begin(); i != products.end(); i++) {
   613                 try {
   614                     update_product(*i, context++);
   615                 }
   616                 catch (exception&) {
   617 
   618                 }
   619             }
   620         }
   621 
   622     closing:
   623         if (internet)
   624             InternetCloseHandle(internet);
   625         if (hAES)
   626             BCryptCloseAlgorithmProvider(hAES, 0);
   627         if (hRSA)
   628             BCryptCloseAlgorithmProvider(hRSA, 0);
   629         internet = NULL;
   630         hAES = NULL;
   631         hRSA = NULL;
   632 
   633         return result;
   634     }
   635 
   636     GateKeeper *GateKeeper::the_gatekeeper = nullptr;
   637 
   638 } // namespace pEp