forked from open-eid/updater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idupdater.cpp
313 lines (279 loc) · 10.1 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
/*
* 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 <QVersionNumber>
#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, m_updateStatus, [this](const QString &msg) {
m_updateStatus->setText(idupdater::tr("Failed: ") + msg);
});
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::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}"))
, conf(new Configuration(this))
{
QString userAgent = QStringLiteral("%1/%2 (%3) Lang: %4 Devices: %5")
.arg(QApplication::applicationName(), QApplication::applicationVersion(), Common::applicationOs(),
QLocale().uiLanguages().first(), QPCSC::instance().drivers().join('/'));
qDebug() << "User-Agent:" << userAgent;
request.setRawHeader( "User-Agent", userAgent.toUtf8() );
connect(conf, &Configuration::finished, this, &idupdater::finished);
connect(this, &QNetworkAccessManager::sslErrors, this, [=](QNetworkReply *reply, const QList<QSslError> &errors){
QList<QSslError> ignore;
for(const QSslError &error: errors)
{
switch(error.error())
{
case QSslError::UnableToGetLocalIssuerCertificate:
case QSslError::CertificateUntrusted:
case QSslError::SelfSignedCertificateInChain:
if(trusted.contains(reply->sslConfiguration().peerCertificate())) {
ignore << error;
break;
}
default: break;
}
}
reply->ignoreSslErrors(ignore);
});
}
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.."));
conf->update();
}
void idupdater::finished(bool /*changed*/, const QString &err)
{
if(!err.isEmpty())
return emit error(err);
emit status(tr("Check completed"));
QJsonObject obj = conf->object();
trusted.clear();
for(const auto &c: conf->object().value(QLatin1String("CERT-BUNDLE")).toArray())
trusted.append(QSslCertificate(QByteArray::fromBase64(c.toString().toLatin1()), QSsl::Der));
if(obj.contains(QLatin1String("UPDATER-MESSAGE-URL")))
{
QSslConfiguration ssl = QSslConfiguration::defaultConfiguration();
ssl.setCaCertificates({});
auto copy = request;
copy.setSslConfiguration(ssl);
copy.setUrl(obj.value(QLatin1String("UPDATER-MESSAGE-URL")).toString());
QNetworkReply *reply = get(copy);
connect(reply, &QNetworkReply::finished, this, [this, reply]{
if(reply->error() == QNetworkReply::NoError)
emit message(reply->readAll());
reply->deleteLater();
});
}
else if(obj.contains(QLatin1String("WIN-MESSAGE")))
emit message(obj.value(QLatin1String("WIN-MESSAGE")).toString());
if(obj.contains(QLatin1String("WIN-UPGRADECODE")))
version = installedVersion(obj.value(QLatin1String("WIN-UPGRADECODE")).toString());
QString available = obj.value(QLatin1String("WIN-LATEST")).toString();
request.setUrl(obj.value(QLatin1String("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();
QSettings s(QStringLiteral("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), QSettings::Registry32Format);
for(const QString &key: s.childGroups()) {
s.beginGroup(key);
if(s.value(QStringLiteral("/BundleUpgradeCode")).toString().toUpper() == code)
return s.value(QStringLiteral("/DisplayVersion")).toString();
s.endGroup();
}
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)
{
return QVersionNumber::fromString(current) < QVersionNumber::fromString(available);
}
void idupdater::startInstall()
{
qDebug() << "Starting install";
emit status( tr("Downloading...") );
QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this,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();
});
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, nullptr, nullptr, nullptr, &store, &msg, nullptr))
return false;
DWORD infoSize = 0;
if(!CryptMsgGetParam(msg, CMSG_SIGNER_CERT_INFO_PARAM, 0, nullptr, &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(), nullptr);
CertCloseStore(store, 0);
if(!certContext)
return false;
QSslCertificate cert(QByteArray::fromRawData(
(const char*)certContext->pbCertEncoded, certContext->cbCertEncoded ), QSsl::Der);
CertFreeCertificateContext(certContext);
if(!trusted.contains(cert))
return false;
WINTRUST_FILE_INFO FileData { sizeof(WINTRUST_FILE_INFO) };
FileData.pcwszFilePath = LPCWSTR(path.utf16());
WINTRUST_DATA WinTrustData { 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;
}