forked from open-eid/updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idupdater.cpp
388 lines (344 loc) · 11.7 KB
/
idupdater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* id-updater
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "idupdater.h"
#include "common/Common.h"
#include "common/Configuration.h"
#include "common/QPCSC.h"
#include <QDebug>
#include <QDir>
#include <QElapsedTimer>
#include <QJsonArray>
#include <QJsonObject>
#include <QNetworkReply>
#include <QProcess>
#include <QPushButton>
#include <QSettings>
#include <QScopedPointer>
#include <QSslCertificate>
#include <QUrl>
#include <qt_windows.h>
#include <Msi.h>
#include <Softpub.h>
idupdaterui::idupdaterui( const QString &version, idupdater *parent )
: QWidget()
{
setupUi( this );
m_message->hide();
connect( parent, &idupdater::status, m_updateStatus, &QLabel::setText );
connect( parent, &idupdater::message, m_message, [this](const QString &msg) {
m_message->setHidden(msg.isEmpty());
m_message->setText(msg);
});
connect( parent, &idupdater::error, this, &idupdaterui::setError );
connect( buttonBox, &QDialogButtonBox::accepted, parent, &idupdater::startInstall );
connect( buttonBox, &QDialogButtonBox::rejected, qApp, &QCoreApplication::quit );
buttonBox->button( QDialogButtonBox::Ok )->setText( tr("Start downloading") );
buttonBox->button( QDialogButtonBox::Close )->setText( tr("Close") );
setDownloadEnabled( false );
m_installedVer->setText( version );
show();
}
void idupdaterui::setDownloadEnabled( bool enabled )
{
availableVerLabel->setVisible( enabled );
m_availableVer->setVisible( enabled );
m_downloadProgress->setVisible( enabled );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
}
void idupdaterui::setError( const QString &msg )
{
m_updateStatus->setText( idupdater::tr("Failed: ") + msg );
}
void idupdaterui::setInfo( const QString &version, const QString &available )
{
setDownloadEnabled( idupdater::lessThanVersion( version, available ) );
m_installedVer->setText( version );
m_availableVer->setText( available );
}
void idupdaterui::setProgress( QNetworkReply *reply )
{
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
m_downloadProgress->setValue( 0 );
connect( reply, &QNetworkReply::downloadProgress, this, [&]( qint64 recvd, qint64 total ) {
static QElapsedTimer timer;
static qint64 lastRecvd = 0;
if( timer.hasExpired( 1000 ) )
{
m_downloadStatus->setText( QString( "%1 KB/s" ).arg( (recvd - lastRecvd) / timer.elapsed() ) );
lastRecvd = recvd;
timer.restart();
}
m_downloadProgress->setMaximum( total );
m_downloadProgress->setValue( recvd );
});
}
idupdater::idupdater( QObject *parent )
: QNetworkAccessManager( parent )
, version(installedVersion("{f1c4d351-269d-4bee-8cdb-6ea70c968875}"))
{
QLocale::Language language = QLocale::system().language();
QString locale = language == QLocale::C ? "English/United States" : QLocale::languageToString( language );
CPINFOEX CPInfoEx = {};
if( GetCPInfoExW( GetConsoleCP(), 0, &CPInfoEx ) != 0 )
locale += QStringLiteral(" / ") + QString::fromWCharArray(CPInfoEx.CodePageName);
QString userAgent = QStringLiteral( "%1/%2 (%3) Locale: %4 Devices: %5")
.arg(qApp->applicationName(), version, Common::applicationOs(), locale, QPCSC::instance().drivers().join("/"));
qDebug() << "User-Agent:" << userAgent;
request.setRawHeader( "User-Agent", userAgent.toUtf8() );
connect( this, &QNetworkAccessManager::finished, this, &idupdater::reply );
connect(&Configuration::instance(), &Configuration::finished, this, &idupdater::finished);
}
void idupdater::reply( QNetworkReply *reply )
{
if(reply->error() != QNetworkReply::NoError)
{
emit error(reply->errorString());
return reply->deleteLater();
}
qDebug() << "Downloaded" << reply->url().toString();
emit status(tr("Download finished, starting installation..."));
QFile tmp(QDir::tempPath() + "/" + reply->url().fileName());
if(!tmp.open(QFile::WriteOnly))
return emit error(tr("Downloaded package integrity check failed"));
tmp.write(reply->readAll());
tmp.close();
reply->deleteLater();
bool verify = verifyPackage(tmp.fileName());
qDebug() << "Package signature" << (verify ? "OK" : "NOT OK");
if(!verify)
return emit error( tr("Downloaded package integrity check failed") );
if(!QProcess::startDetached( tmp.fileName(),
m_autoupdate ? QStringList("/quiet") : QStringList()))
return emit error( tr("Package installation failed"));
emit status(tr("Package installed"));
QApplication::quit();
}
void idupdater::checkUpdates(bool autoupdate, bool autoclose)
{
m_autoupdate = autoupdate;
m_autoclose = autoclose;
if(!autoclose && !w)
{
w = new idupdaterui(version, this);
request.setRawHeader("User-Agent", request.rawHeader( "User-Agent" ) + " manual");
}
emit status(tr("Checking for update.."));
Configuration::instance().update();
}
void idupdater::finished(bool /*changed*/, const QString &err)
{
if(!err.isEmpty())
return emit error(err);
emit status(tr("Check completed"));
QJsonObject obj = Configuration::instance().object();
if(obj.contains("WIN-MESSAGE"))
emit message(obj.value("WIN-MESSAGE").toString());
if(obj.contains("WIN-UPGRADECODE"))
version = installedVersion(obj.value("WIN-UPGRADECODE").toString());
QString available = obj.value("WIN-LATEST").toString();
request.setUrl(obj.value("WIN-DOWNLOAD").toString());
qDebug() << "Installed version" << version << "available version" << available;
if(!lessThanVersion(version, available))
{
emit status(tr("No updates are available"));
if(m_autoclose)
QApplication::quit();
}
else
{
if(!m_autoupdate)
{
if( !w ) w = new idupdaterui(version, this);
emit status(tr("Update is available"));
}
else
startInstall();
}
if(w) w->setInfo(version, available);
}
QString idupdater::installedVersion(const QString &upgradeCode) const
{
QString code = upgradeCode.toUpper();
QString path = QStringLiteral("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
HKEY reg = HKEY_LOCAL_MACHINE;
REGSAM param = KEY_READ|KEY_WOW64_32KEY;
HKEY key;
long result = RegOpenKeyEx(reg, LPWSTR(path.utf16()), 0, param, &key);
if(result != ERROR_SUCCESS)
return {};
DWORD numSubgroups = 0, maxSubgroupSize = 0;
result = RegQueryInfoKey(key, 0, 0, 0, &numSubgroups, &maxSubgroupSize, 0, 0, 0, 0, 0, 0);
if(result != ERROR_SUCCESS)
{
RegCloseKey(key);
return {};
}
for(DWORD j = 0; j < numSubgroups; ++j)
{
DWORD groupSize = maxSubgroupSize + 1;
QString group(groupSize, 0);
result = RegEnumKeyEx(key, j, LPWSTR(group.data()), &groupSize, 0, 0, 0, 0);
if(result != ERROR_SUCCESS)
return {};
group.resize(groupSize);
HKEY subkey;
QString subpath = path + "\\" + group;
result = RegOpenKeyEx(reg, LPCWSTR(subpath.utf16()), 0, param, &subkey);
if(result != ERROR_SUCCESS)
return {};
DWORD numKeys = 0, maxKeySize = 0, maxValueSize = 0;
result = RegQueryInfoKey(subkey, 0, 0, 0, 0, 0, 0, &numKeys, &maxKeySize, &maxValueSize, 0, 0);
if(result != ERROR_SUCCESS)
{
RegCloseKey(subkey);
return {};
}
QString regcode, version;
for(DWORD k = 0; k < numKeys; ++k)
{
DWORD dataType = 0;
DWORD keySize = maxKeySize + 1;
DWORD dataSize = maxValueSize;
QString key(keySize, 0);
QByteArray data(dataSize, 0);
result = RegEnumValue(subkey, k, LPWSTR(key.data()), &keySize, 0,
&dataType, (unsigned char*)data.data(), &dataSize);
if(result != ERROR_SUCCESS)
continue;
key.resize(keySize);
data.resize(dataSize);
QString value;
switch(dataType)
{
case REG_SZ:
value = QString::fromUtf16((const ushort*)data.constData());
break;
case REG_MULTI_SZ:
{
QStringList l;
for(int i = 0;;) {
QString s = QString::fromUtf16((const ushort*)data.constData() + i);
i += s.length() + 1;
if (s.isEmpty())
break;
l.append(s);
}
value = l.value(0);
break;
}
default: continue;
}
if(key == "BundleUpgradeCode") regcode = value;
if(key == "DisplayVersion") version = value;
}
RegCloseKey(subkey);
if(regcode.toUpper() == code)
return version;
}
RegCloseKey(key);
WCHAR prodCode[40];
if(ERROR_SUCCESS != MsiEnumRelatedProducts(L"{58A1DBA8-81A2-4D58-980B-4A6174D5B66B}", 0, 0, prodCode))
return {};
DWORD size = 0;
MsiGetProductInfo(prodCode, INSTALLPROPERTY_VERSIONSTRING, 0, &size);
QString version(size, 0);
size += 1;
MsiGetProductInfo(prodCode, INSTALLPROPERTY_VERSIONSTRING, LPWSTR(version.data()), &size);
return version;
}
bool idupdater::lessThanVersion(const QString ¤t, const QString &available)
{
QStringList curList = current.split('.');
QStringList avaList = available.split('.');
for(int i = 0; i < std::max<int>(curList.size(), avaList.size()); ++i)
{
bool curconv = false, avaconv = false;
unsigned int cur = curList.value(i).toUInt(&curconv);
unsigned int ava = avaList.value(i).toUInt(&avaconv);
if(curconv && avaconv)
{
if(cur != ava)
return cur < ava;
}
else
{
int status = QString::localeAwareCompare(curList.value(i), avaList.value(i));
if(status != 0)
return status < 0;
}
}
return false;
}
void idupdater::startInstall()
{
qDebug() << "Starting install";
emit status( tr("Downloading...") );
QNetworkReply *reply = get( request );
if( w ) w->setProgress( reply );
}
bool idupdater::verifyPackage(const QString &filePath) const
{
QString path = QDir::toNativeSeparators(filePath);
HCERTSTORE store = nullptr;
HCRYPTMSG msg = nullptr;
if(!CryptQueryObject(CERT_QUERY_OBJECT_FILE, LPCWSTR(path.utf16()),
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED, CERT_QUERY_FORMAT_FLAG_BINARY,
0, 0, 0, 0, &store, &msg, nullptr))
return false;
DWORD infoSize = 0;
if(!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, 0, 0, &infoSize))
{
CryptMsgClose(msg);
CertCloseStore(store, 0);
return false;
}
QScopedPointer<CERT_INFO,QScopedPointerPodDeleter> info(PCERT_INFO(std::malloc(infoSize)));
if(!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, 0, info.data(), &infoSize))
{
CryptMsgClose(msg);
CertCloseStore(store, 0);
return false;
}
CryptMsgClose(msg);
PCCERT_CONTEXT certContext = CertFindCertificateInStore(store,
X509_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, info.data(), 0);
CertCloseStore(store, 0);
if(!certContext)
return false;
QSslCertificate cert(QByteArray::fromRawData(
(const char*)certContext->pbCertEncoded, certContext->cbCertEncoded ), QSsl::Der);
CertFreeCertificateContext(certContext);
QList<QSslCertificate> list;
for(const QJsonValue &cert: Configuration::instance().object().value("CERT-BUNDLE").toArray())
list << QSslCertificate(QByteArray::fromBase64(cert.toString().toLatin1()), QSsl::Der);
if(!list.contains(cert))
return false;
WINTRUST_FILE_INFO FileData {};
FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
FileData.pcwszFilePath = LPCWSTR(path.utf16());
WINTRUST_DATA WinTrustData {};
WinTrustData.cbStruct = sizeof(WinTrustData);
WinTrustData.dwUIChoice = m_autoupdate ? WTD_UI_NONE : WTD_UI_ALL;
WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;
WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;
WinTrustData.dwProvFlags = WTD_SAFER_FLAG;
WinTrustData.pFile = &FileData;
GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
return WinVerifyTrust(0, &WVTPolicyGUID, &WinTrustData) == ERROR_SUCCESS;
}