3 #include "GateKeeper.h"
4 #include "pEpCOMServerAdapter.h"
10 const LPCTSTR GateKeeper::plugin_reg_path = _T("Software\\Microsoft\\Office\\Outlook\\Addins\\pEp");
11 const LPCTSTR GateKeeper::plugin_reg_value_name = _T("LoadBehavior");
12 const LPCTSTR GateKeeper::updater_reg_path = _T("Software\\pEp\\Updater";)
14 const time_t GateKeeper::cycle = 7200; // 7200 sec is 2 h
15 const DWORD GateKeeper::waiting = 10000; // 10000 ms is 10 sec
17 GateKeeper::GateKeeper(CpEpCOMServerAdapterModule * const self)
18 : _self(self), now(time(NULL)), next(now + time_diff()), hkUpdater(NULL), internet(NULL)
20 LONG lResult = RegOpenCurrentUser(KEY_READ, &cu);
21 assert(lResult == ERROR_SUCCESS);
22 if (lResult == ERROR_SUCCESS)
28 LONG lResult = RegOpenKeyEx(cu, updater_reg_path, 0, KEY_READ, &hkUpdater);
29 assert(lResult == ERROR_SUCCESS);
30 if (lResult != ERROR_SUCCESS)
35 GateKeeper::~GateKeeper()
39 RegCloseKey(hkUpdater);
44 time_t GateKeeper::time_diff()
47 static random_device rd;
48 static mt19937 gen(rd());
50 uniform_int_distribution<time_t> dist(0, cycle);
60 void GateKeeper::keep()
72 next = now + GateKeeper::cycle;
80 void GateKeeper::keep_plugin()
82 while (!_self->m_bComInitialized)
85 MessageBox(NULL, _T("test"), _T("keep_plugin"), MB_ICONINFORMATION | MB_TOPMOST);
90 LONG lResult = RegGetValue(cu, plugin_reg_path, plugin_reg_value_name, RRF_RT_REG_DWORD, NULL, &value, &size);
91 if (lResult != ERROR_SUCCESS)
95 lResult = RegSetValue(cu, plugin_reg_path, RRF_RT_REG_DWORD, plugin_reg_value_name, 3);
96 assert(lResult == ERROR_SUCCESS);
100 string GateKeeper::update_key()
104 if (key.length() == 0) {
105 HRSRC res = FindResource(_self->hModule(), MAKEINTRESOURCE(IRD_UPDATEKEY), RT_RCDATA);
108 throw runtime_error("FindResource: IRD_UPDATEKEY");
110 HGLOBAL hRes = LoadResource(_self->hModule(), res);
113 throw runtime_error("LoadResource: IRD_UPDATEKEY");
115 key = string((char *)LockResource(hRes), SizeofResource(_self->hModule(), res));
116 UnlockResource(hRes);
122 GateKeeper::aeskey_t GateKeeper::delivery_key()
126 static random_device rd;
127 static mt19937 gen(rd());
129 uniform_int_distribution<time_t> dist(0, UINT64_MAX);
131 key.ll_key[0] = dist(gen);
132 key.ll_key[1] = dist(gen);
135 GateKeeper::product_list& GateKeeper::registered_products()
137 static product_list products;
139 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx
140 static TCHAR value_name[16384];
141 DWORD value_name_size;
142 static TCHAR value[L_MAX_URL_LENGTH + 1];
147 LONG lResult = ERROR_SUCCESS;
148 for (DWORD i = 0; lResult == ERROR_SUCCESS; i++) {
149 value_size = L_MAX_URL_LENGTH + 1;
150 lResult = RegEnumValue(hkUpdater, 0, value_name, &value_name_size, NULL, NULL, (LPBYTE) value, &value_size);
151 if (lResult == ERROR_SUCCESS)
152 products.push_back({ value_name, value });
158 void GateKeeper::update_product(product p, DWORD context)
160 HINTERNET hUrl = InternetOpenUrl(internet, p.second.c_str(), NULL, 0,
161 INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE, context);
166 PCTSTR rgpszAcceptTypes[] = { _T("text/plain"), NULL };
167 HINTERNET hRequest = HttpOpenRequest(hUrl, NULL, _T("challenge"), NULL, NULL, rgpszAcceptTypes, INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE, context);
170 InternetCloseHandle(hUrl);
173 void GateKeeper::keep_updated()
175 return; // disabled for now
177 internet = InternetOpen(_T("pEp"), INTERNET_OPEN_TYPE_PROXY, NULL, NULL, 0);
179 product_list& products = registered_products();
181 for (auto i = products.begin(); i != products.end(); i++) {
182 update_product(*i, context++);
185 InternetCloseHandle(internet);