Skip to content

Commit c9c7cd9

Browse files
author
Lauris Kaplinski
committed
Fix error flow for cancel/incorrect/locked PIN for online container
1 parent 25e0bb2 commit c9c7cd9

4 files changed

Lines changed: 47 additions & 7 deletions

File tree

client/CDocSupport.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,17 @@ DDConfiguration::getValue(std::string_view domain, std::string_view param) const
216216
std::string
217217
DDNetworkBackend::getLastErrorStr(libcdoc::result_t code) const
218218
{
219-
if (code == BACKEND_ERROR) return last_error;
219+
switch (code) {
220+
case DDCryptoBackend::PIN_CANCELED:
221+
return "PIN entry canceled";
222+
case DDCryptoBackend::PIN_INCORRECT:
223+
return "PIN incorrect";
224+
case DDCryptoBackend::PIN_LOCKED:
225+
return "PIN locked";
226+
case BACKEND_ERROR:
227+
case DDCryptoBackend::BACKEND_ERROR:
228+
return last_error;
229+
}
220230
return libcdoc::NetworkBackend::getLastErrorStr(code);
221231
}
222232

@@ -278,7 +288,14 @@ DDNetworkBackend::fetchKey(std::vector<uint8_t> &result,
278288
last_error = "No connection";
279289
return BACKEND_ERROR;
280290
}
281-
auto authKey = dispatchToMain(&QSigner::key, qApp->signer());
291+
QCryptoBackend::PinStatus pin_status;
292+
auto authKey = dispatchToMain([&] {
293+
return qApp->signer()->key(pin_status);
294+
});
295+
if (!authKey.handle()) {
296+
last_error = qApp->signer()->getLastErrorStr().toStdString();
297+
return getDecryptStatus(result, pin_status);
298+
}
282299
QScopedPointer<QNetworkAccessManager,QScopedPointerDeleteLater> nam(
283300
CheckConnection::setupNAM(req, qApp->signer()->tokenauth().cert(), authKey, Settings::CDOC2_GET_CERT));
284301
QEventLoop e;

client/CryptoDoc.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,15 @@ bool CryptoDoc::decrypt(const libcdoc::Lock *lock, const QByteArray& secret)
393393
case libcdoc::INPUT_STREAM_ERROR:
394394
str = tr("Cannot read file.");
395395
break;
396+
case DDCryptoBackend::PIN_CANCELED:
397+
str = tr("PIN entry canceled");
398+
break;
399+
case DDCryptoBackend::PIN_INCORRECT:
400+
str = tr("PIN incorrect");
401+
break;
402+
case DDCryptoBackend::PIN_LOCKED:
403+
str = tr("PIN locked");
404+
break;
396405
default:
397406
str = tr("Please check your internet connection and network settings.");
398407
break;

client/QSigner.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,29 @@ QByteArray QSigner::decrypt(std::function<QByteArray (QCryptoBackend *)> &&func,
211211
return result;
212212
}
213213

214-
QSslKey QSigner::key() const
214+
QSslKey QSigner::key(QCryptoBackend::PinStatus& pin_status)
215215
{
216216
QSslKey key = d->auth.cert().publicKey();
217-
if(!key.handle())
217+
if(!key.handle()) {
218+
pin_status = QCryptoBackend::GeneralError;
218219
return {};
219-
if(!d->lock.tryLockForWrite(10 * 1000))
220+
}
221+
if(!d->lock.tryLockForWrite(10 * 1000)) {
222+
Q_EMIT error(tr("Failed to decrypt document"), tr("Signing/decrypting is already in progress another window."));
223+
pin_status = QCryptoBackend::GeneralError;
224+
return {};
225+
}
226+
switch(pin_status = QCryptoBackend::PinStatus(login(d->auth)))
227+
{
228+
case QCryptoBackend::PinOK: break;
229+
case QCryptoBackend::PinCanceled: return {};
230+
case QCryptoBackend::PinLocked:
231+
Q_EMIT error(tr("Failed to decrypt document"), QCryptoBackend::errorString(pin_status));
220232
return {};
221-
if(login(d->auth) != QCryptoBackend::PinOK)
233+
default:
234+
Q_EMIT error(tr("Failed to decrypt document"), tr("Failed to login token") + ' ' + QCryptoBackend::errorString(pin_status));
222235
return {};
236+
}
223237
if(key.algorithm() == QSsl::Ec)
224238
{
225239
auto *ec = (EC_KEY*)key.handle();

client/QSigner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class QSigner final: public QThread, public digidoc::Signer
4242
QList<TokenData> cache() const;
4343
digidoc::X509Cert cert() const final;
4444
QByteArray decrypt(std::function<QByteArray (QCryptoBackend *)> &&func, QCryptoBackend::PinStatus& pin_status);
45-
QSslKey key() const;
45+
QSslKey key(QCryptoBackend::PinStatus& pin_status);
4646
void logout() const;
4747
void selectCard(const TokenData &token);
4848
std::vector<unsigned char> sign( const std::string &method,

0 commit comments

Comments
 (0)