Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions client/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "Configuration.h"
#include "CDocSupport.h"
#include "MainWindow.h"
#include "QSigner.h"
#include "QCryptoBackend.h"
#include "QSmartCard.h"
#include "DigiDoc.h"
#include "Settings.h"
Expand Down Expand Up @@ -296,7 +296,7 @@ class Application::Private
Configuration *conf {};
QAction *closeAction {}, *newClientAction {}, *helpAction {};
std::unique_ptr<MacMenuBar> bar;
QSigner *signer {};
QCryptoManager *cryptoManager {};

QTranslator appTranslator, qtTranslator;
QString lang;
Expand All @@ -307,7 +307,7 @@ class Application::Private
#endif // Q_OS_WIN

~Private() {
delete signer;
delete cryptoManager;
}
};

Expand Down Expand Up @@ -432,6 +432,7 @@ Application::Application( int &argc, char **argv )

// Clear obsolete registriy settings
Settings::CDOC2_NOTIFICATION.clear();
Settings::MOBILEID_ORDER.clear();
#ifndef Q_OS_DARWIN
Settings::DEFAULT_DIR.clear();
#endif
Expand Down Expand Up @@ -462,7 +463,7 @@ Application::Application( int &argc, char **argv )
try
{
digidoc::Conf::init( new DigidocConf );
d->signer = new QSigner(this);
d->cryptoManager = new QCryptoManager();
updateTSLCache(QDateTime::currentDateTimeUtc().addDays(-7));

digidoc::initialize(applicationName().toUtf8().constData(), QStringLiteral("%1/%2 (%3)")
Expand Down Expand Up @@ -968,7 +969,7 @@ void Application::showWarning(const QString &title, const digidoc::Exception &e)
WarningDialog::create()->withTitle(title)->withDetails(causes.join('\n'))->open();
}

QSigner* Application::signer() const { return d->signer; }
QCryptoManager* Application::cryptoManager() const { return d->cryptoManager; }

void Application::updateTSLCache(const QDateTime &tslTime)
{
Expand Down
4 changes: 2 additions & 2 deletions client/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using BaseApplication = QtSingleApplication;
namespace digidoc { class Exception; }
class Configuration;
class QAction;
class QSigner;
class QCryptoManager;
class Application final: public BaseApplication
{
Q_OBJECT
Expand All @@ -61,7 +61,7 @@ class Application final: public BaseApplication
Configuration *conf();
void loadTranslation( const QString &lang );
bool notify(QObject *object, QEvent *event ) final;
QSigner* signer() const;
QCryptoManager* cryptoManager() const;
int run();
void waitForTSL( const QString &file );

Expand Down
22 changes: 11 additions & 11 deletions client/CDocSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "Application.h"
#include "CheckConnection.h"
#include "QCryptoBackend.h"
#include "QSigner.h"
#include "Settings.h"
#include "TokenData.h"
#include "Utils.h"
Expand Down Expand Up @@ -124,7 +123,7 @@ libcdoc::result_t
DDCryptoBackend::decryptRSA(std::vector<uint8_t>& dst, const std::vector<uint8_t> &data, bool oaep, unsigned int idx)
{
if (!backend) {
auto val = QCryptoBackend::getBackend(qApp->signer()->tokenauth());
auto val = QCryptoBackend::getBackend(token);
if (!val)
return getDecryptStatus(val.error());
backend.reset(val.value());
Expand All @@ -144,7 +143,7 @@ DDCryptoBackend::deriveConcatKDF(std::vector<uint8_t>& dst, const std::vector<ui
{"http://www.w3.org/2001/04/xmlenc#sha512", QCryptographicHash::Sha512}
};
if (!backend) {
auto val = QCryptoBackend::getBackend(qApp->signer()->tokenauth());
auto val = QCryptoBackend::getBackend(token);
if (!val)
return getDecryptStatus(val.error());
backend.reset(val.value());
Expand All @@ -159,7 +158,7 @@ libcdoc::result_t
DDCryptoBackend::deriveHMACExtract(std::vector<uint8_t>& dst, const std::vector<uint8_t> &key_material, const std::vector<uint8_t> &salt, unsigned int idx)
{
if (!backend) {
auto val = QCryptoBackend::getBackend(qApp->signer()->tokenauth());
auto val = QCryptoBackend::getBackend(token);
if (!val)
return getDecryptStatus(val.error());
backend.reset(val.value());
Expand Down Expand Up @@ -189,20 +188,21 @@ DDCryptoBackend::getLastErrorStr(libcdoc::result_t code) const
case IN_PROGRESS:
return "Signing/decrypting is already in progress another window.";
case BACKEND_ERROR:
return qApp->signer()->getLastErrorStr().toStdString();
return "Backend error";
}
return libcdoc::CryptoBackend::getLastErrorStr(code);
}

static bool
checkConnection()
{
if(CheckConnection().check()) {
CheckConnection check;
if(check.check()) {
return true;
}
return dispatchToMain([] {
return dispatchToMain([check] {
FadeInNotification::error(Application::mainWindow()->findChild<QWidget*>(QStringLiteral("topBar")),
QCoreApplication::translate("MainWindow", "Check internet connection"));
check.errorString());
return false;
});
}
Expand Down Expand Up @@ -321,8 +321,8 @@ DDNetworkBackend::fetchKey(std::vector<uint8_t> &result, const std::string &url,
return BACKEND_ERROR;
}

TokenData auth = qApp->signer()->tokenauth();
auto val = QCryptoBackend::getBackend(qApp->signer()->tokenauth());
const TokenData &auth = crypto.token;
auto val = QCryptoBackend::getBackend(auth);
if (!val)
return getDecryptStatus(val.error());
std::unique_ptr<QCryptoBackend> backend(val.value());
Expand All @@ -333,7 +333,7 @@ DDNetworkBackend::fetchKey(std::vector<uint8_t> &result, const std::string &url,
return BACKEND_ERROR;
}
QScopedPointer<QNetworkAccessManager,QScopedPointerDeleteLater> nam(
CheckConnection::setupNAM(req, qApp->signer()->tokenauth().cert(), authKey, Settings::CDOC2_GET_CERT));
CheckConnection::setupNAM(req, auth.cert(), authKey, Settings::CDOC2_GET_CERT));
QEventLoop e;
QNetworkReply *reply = nam->get(req);
connect(reply, &QNetworkReply::finished, &e, &QEventLoop::quit);
Expand Down
3 changes: 2 additions & 1 deletion client/CDocSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct DDConfiguration : public libcdoc::Configuration {
//
// CryptoBackend
//
// Bridges to qApp->signer()
// Bridges to qApp->cryptoManager()
//

struct DDCryptoBackend final : public libcdoc::CryptoBackend {
Expand Down Expand Up @@ -81,6 +81,7 @@ struct DDCryptoBackend final : public libcdoc::CryptoBackend {

std::unique_ptr<QCryptoBackend> backend;
std::vector<uint8_t> secret;
TokenData token;

explicit DDCryptoBackend() = default;

Expand Down
3 changes: 1 addition & 2 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_subdirectory(libcdoc EXCLUDE_FROM_ALL)
file(GLOB WIDGETS CONFIGURE_DEPENDS
"dialogs/*.cpp" "dialogs/*.h" "dialogs/*.ui"
"effects/*.cpp" "effects/*.h" "effects/*.ui"
"signer/*.cpp" "signer/*.h"
"widgets/*.cpp" "widgets/*.h" "widgets/*.ui"
)
add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE
Expand Down Expand Up @@ -66,8 +67,6 @@ add_executable(${PROJECT_NAME} WIN32 MACOSX_BUNDLE
QPCSC.h
QPKCS11.cpp
QPKCS11.h
QSigner.cpp
QSigner.h
QSmartCard.cpp
QSmartCard_p.h
QSmartCard.h
Expand Down
2 changes: 1 addition & 1 deletion client/CheckConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ QString CheckConnection::errorString() const
case QNetworkReply::ProxyAuthenticationRequiredError:
return QCoreApplication::translate("CheckConnection", "Check proxy username and password");
default:
return QCoreApplication::translate("CheckConnection", "Cannot connect to certificate status service!");
return QCoreApplication::translate("CheckConnection", "Check internet connection");
}
}

Expand Down
51 changes: 11 additions & 40 deletions client/CryptoDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "CDocSupport.h"
#include "TokenData.h"
#include "QCryptoBackend.h"
#include "QSigner.h"
#include "Settings.h"
#include "SslCertificate.h"
#include "Utils.h"
Expand Down Expand Up @@ -254,16 +253,6 @@ bool CryptoDoc::addEncryptionKey(const CKey& key) {
return true;
}

bool CryptoDoc::canDecrypt(const QSslCertificate &cert) {
if (!d->reader)
return false;
if (cert.isNull())
return false;
QByteArray der = cert.toDer();
return d->reader->getLockForCert(
std::vector<uint8_t>(der.cbegin(), der.cend())) >= 0;
}

void CryptoDoc::clear(const QString &file, int version)
{
d->documents->clearTempFolder();
Expand All @@ -278,49 +267,31 @@ ContainerState CryptoDoc::state() const
return d->isEncrypted() ? EncryptedContainer : UnencryptedContainer;
}

bool CryptoDoc::decrypt(const libcdoc::Lock *lock, const QByteArray& secret)
bool CryptoDoc::decrypt(const libcdoc::Lock &lock, const QByteArray& secret, const TokenData &token)
{
if(!d->reader)
{
WarningDialog::create()
->withTitle(QSigner::tr("Failed to decrypt document"))
->withTitle(tr("Failed to decrypt document"))
->withText(tr("Container is not open"))
->open();
return false;
}

int lock_idx = -1;
const std::vector<libcdoc::Lock> &locks = d->reader->getLocks();
if (lock == nullptr) {
QByteArray der = qApp->signer()->tokenauth().cert().toDer();
lock_idx = d->reader->getLockForCert(
std::vector<uint8_t>(der.cbegin(), der.cend()));
if (lock_idx < 0) {
WarningDialog::create()
->withTitle(QSigner::tr("Failed to decrypt document"))
->withText(tr("You do not have the key to decrypt this document"))
->open();
return false;
}
lock = &locks.at(lock_idx);
} else {
for (lock_idx = 0; lock_idx < locks.size(); lock_idx++) {
if (lock->label == locks[lock_idx].label) {
lock = &locks.at(lock_idx);
break;
}
}
if (lock_idx >= locks.size())
lock_idx = -1;
}
if (!lock || (lock->isSymmetric() && secret.isEmpty())) {
auto found = std::find_if(locks.cbegin(), locks.cend(),
[&lock](const libcdoc::Lock &l) { return l.label == lock.label; });
if (found == locks.cend() || (found->isSymmetric() && secret.isEmpty())) {
WarningDialog::create()
->withTitle(QSigner::tr("Failed to decrypt document"))
->withTitle(tr("Failed to decrypt document"))
->withText(tr("You do not have the key to decrypt this document"))
->open();
return false;
}
int lock_idx = int(std::distance(locks.cbegin(), found));

d->crypto.setBackend({});
d->crypto.token = token;
d->crypto.secret.assign(secret.cbegin(), secret.cend());

TempListConsumer cons;
Expand All @@ -342,7 +313,7 @@ bool CryptoDoc::decrypt(const libcdoc::Lock *lock, const QByteArray& secret)
const std::string &msg = d->reader->getLastErrorStr();
switch (result) {
case libcdoc::WRONG_KEY:
str = (lock->type == libcdoc::Lock::PASSWORD) ? tr("Wrong password.") : tr("Wrong key.");
str = (found->type == libcdoc::Lock::PASSWORD) ? tr("Wrong password.") : tr("Wrong key.");
break;
case libcdoc::HASH_MISMATCH:
case libcdoc::DATA_FORMAT_ERROR:
Expand All @@ -368,7 +339,7 @@ bool CryptoDoc::decrypt(const libcdoc::Lock *lock, const QByteArray& secret)
break;
}
WarningDialog::create()
->withTitle(QSigner::tr("Failed to decrypt document"))
->withTitle(tr("Failed to decrypt document"))
->withText(str)
->withDetails(QString::fromStdString(msg))
->open();
Expand Down
4 changes: 2 additions & 2 deletions client/CryptoDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <cdoc/Recipient.h>

class QSslKey;
class TokenData;

Q_DECLARE_LOGGING_CATEGORY(CRYPTO)

Expand Down Expand Up @@ -61,9 +62,8 @@ class CryptoDoc final: public QObject

bool supportsSymmetricKeys() const;
bool addEncryptionKey(const CKey& key);
bool canDecrypt(const QSslCertificate &cert);
void clear(const QString &file = {}, int version = -1);
bool decrypt(const libcdoc::Lock *lock, const QByteArray& secret);
bool decrypt(const libcdoc::Lock &lock, const QByteArray& secret, const TokenData &token);
bool encrypt(const QString &filename = {}, const QString& label = {}, const QByteArray& secret = {});
DocumentModel* documentModel() const;
QString fileName() const;
Expand Down
20 changes: 14 additions & 6 deletions client/DigiDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "Common.h"
#include "MainWindow.h"
#include "QCryptoBackend.h"
#include "QSigner.h"
#include "Settings.h"
#include "TokenData.h"
#include "Utils.h"
Expand All @@ -33,6 +32,7 @@

#include <digidocpp/DataFile.h>
#include <digidocpp/Signature.h>
#include <digidocpp/crypto/Signer.h>
#include <digidocpp/crypto/X509Cert.h>

#include <QtCore/QDateTime>
Expand All @@ -45,6 +45,16 @@
using namespace digidoc;
using namespace ria::qdigidoc4;

struct ExtendSigner final: public Signer
{
X509Cert cert() const final { return X509Cert(); }
std::vector<unsigned char> sign(const std::string & /*method*/,
const std::vector<unsigned char> & /*digest*/) const final
{
throw Exception(__FILE__, __LINE__, "Not implemented");
}
};

static std::string to(const QString &str) { return str.toStdString(); }
static QString from(const std::string &str) { return FileDialog::normalized(QString::fromStdString(str)); }

Expand Down Expand Up @@ -424,8 +434,8 @@ bool DigiDoc::extend()
{
QWidget *parent = parentWidget();
try {
auto *signer = qApp->signer();
signer->setUserAgent(QStringLiteral("%1/%2 (%3) Devices: %4").arg(
ExtendSigner signer;
signer.setUserAgent(QStringLiteral("%1/%2 (%3) Devices: %4").arg(
QCoreApplication::applicationName(),
QCoreApplication::applicationVersion(),
Common::applicationOs(),
Expand All @@ -434,12 +444,10 @@ bool DigiDoc::extend()
ServiceConfirmation cb(parent);
QString current = m_fileName;
size_t extendCount = 0;
bool wrapped = false;
if(std::unique_ptr<Container> extended = waitFor([&] {
return Container::extendContainerValidity(*b, signer, extendCount);
return Container::extendContainerValidity(*b, &signer, extendCount);
}))
{
wrapped = true;
const QString asics = QCoreApplication::translate("MainWindow", "Documents (%1)").arg(QLatin1String("*.asics *.scs"));
QFileInfo f(current);
QString name = f.absolutePath() + '/' + f.completeBaseName() + QStringLiteral(".asics");
Expand Down
Loading
Loading