Skip to content

Commit ff7edeb

Browse files
committed
addd a cmake option to enable single account desktop client
Signed-off-by: Matthieu Gallien <[email protected]>
1 parent 8c1eec0 commit ff7edeb

13 files changed

+247
-10
lines changed

NEXTCLOUD.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ endif()
8080
if (APPLE)
8181
option( BUILD_FILE_PROVIDER_MODULE "Build the macOS virtual files File Provider module" OFF )
8282
endif()
83+
84+
set (CLIENTSIDEENCRYPTION_ENFORCE_USB_TOKEN true)

config.h.in

+2
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@
6161

6262
#cmakedefine CFAPI_SHELL_EXTENSIONS_LIB_NAME "@CFAPI_SHELL_EXTENSIONS_LIB_NAME@"
6363

64+
#cmakedefine CLIENTSIDEENCRYPTION_ENFORCE_USB_TOKEN @CLIENTSIDEENCRYPTION_ENFORCE_USB_TOKEN@
65+
6466
#endif

src/gui/accountsettings.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ void AccountSettings::slotE2eEncryptionMnemonicReady()
283283
void AccountSettings::slotE2eEncryptionGenerateKeys()
284284
{
285285
connect(_accountState->account()->e2e(), &ClientSideEncryption::initializationFinished, this, &AccountSettings::slotE2eEncryptionInitializationFinished);
286+
connect(_accountState->account()->e2e(), &ClientSideEncryption::displayTokenInitDialog, this, &AccountSettings::slotDisplayTokenInitDialog);
286287
_accountState->account()->setE2eEncryptionKeysGenerationAllowed(true);
287288
_accountState->account()->setAskUserForMnemonic(true);
288289
_accountState->account()->e2e()->initialize(_accountState->account());
@@ -291,6 +292,7 @@ void AccountSettings::slotE2eEncryptionGenerateKeys()
291292
void AccountSettings::slotE2eEncryptionInitializationFinished(bool isNewMnemonicGenerated)
292293
{
293294
disconnect(_accountState->account()->e2e(), &ClientSideEncryption::initializationFinished, this, &AccountSettings::slotE2eEncryptionInitializationFinished);
295+
disconnect(_accountState->account()->e2e(), &ClientSideEncryption::displayTokenInitDialog, this, &AccountSettings::slotDisplayTokenInitDialog);
294296
if (_accountState->account()->e2e()->isInitialized()) {
295297
removeActionFromEncryptionMessage(e2EeUiActionEnableEncryptionId);
296298
slotE2eEncryptionMnemonicReady();
@@ -301,6 +303,13 @@ void AccountSettings::slotE2eEncryptionInitializationFinished(bool isNewMnemonic
301303
_accountState->account()->setAskUserForMnemonic(false);
302304
}
303305

306+
void AccountSettings::slotDisplayTokenInitDialog()
307+
{
308+
disconnect(_accountState->account()->e2e(), &ClientSideEncryption::initializationFinished, this, &AccountSettings::slotE2eEncryptionInitializationFinished);
309+
disconnect(_accountState->account()->e2e(), &ClientSideEncryption::displayTokenInitDialog, this, &AccountSettings::slotDisplayTokenInitDialog);
310+
Systray::instance()->createTokenInitDialog(_accountState->account()->e2e()->discoveredTokens());
311+
}
312+
304313
void AccountSettings::slotEncryptFolderFinished(int status)
305314
{
306315
qCInfo(lcAccountSettings) << "Current folder encryption status code:" << status;

src/gui/accountsettings.h

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ protected slots:
106106
void slotE2eEncryptionMnemonicReady();
107107
void slotE2eEncryptionGenerateKeys();
108108
void slotE2eEncryptionInitializationFinished(bool isNewMnemonicGenerated);
109+
void slotDisplayTokenInitDialog();
109110
void slotEncryptFolderFinished(int status);
110111

111112
void slotSelectiveSyncChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,

src/gui/systray.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,45 @@ void Systray::createFileActivityDialog(const QString &localPath)
412412
Q_EMIT showFileDetailsPage(localPath, FileDetailsPage::Activity);
413413
}
414414

415+
void Systray::createTokenInitDialog(const QVariantList &tokensInfo)
416+
{
417+
if(_tokenInitDialog) {
418+
destroyDialog(_tokenInitDialog);
419+
_tokenInitDialog = nullptr;
420+
}
421+
422+
qCDebug(lcSystray) << "Opening new token init dialog with " << tokensInfo.size() << "possible tokens";
423+
424+
if (!_trayEngine) {
425+
qCWarning(lcSystray) << "Could not open token init dialog as no tray engine was available";
426+
return;
427+
}
428+
429+
const QVariantMap initialProperties{
430+
{"tokensInfo", tokensInfo},
431+
};
432+
433+
QQmlComponent tokenInitDialogComponent(_trayEngine, QStringLiteral("qrc:/qml/src/gui/filedetails/FileDetailsWindow.qml"));
434+
435+
if (!tokenInitDialogComponent.isError()) {
436+
const auto createdDialog = tokenInitDialogComponent.createWithInitialProperties(initialProperties);
437+
const auto dialog = qobject_cast<QQuickWindow*>(createdDialog);
438+
439+
if(!dialog) {
440+
qCWarning(lcSystray) << "Token init dialog window resulted in creation of object that was not a window!";
441+
return;
442+
}
443+
444+
_tokenInitDialog = dialog;
445+
446+
_tokenInitDialog->show();
447+
_tokenInitDialog->raise();
448+
_tokenInitDialog->requestActivate();
449+
} else {
450+
qCWarning(lcSystray) << tokenInitDialogComponent.errorString();
451+
}
452+
}
453+
415454
void Systray::presentShareViewInTray(const QString &localPath)
416455
{
417456
const auto folder = FolderMan::instance()->folderForPath(localPath);

src/gui/systray.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
#ifndef SYSTRAY_H
1616
#define SYSTRAY_H
1717

18-
#include <QSystemTrayIcon>
19-
2018
#include "accountmanager.h"
2119
#include "tray/usermodel.h"
2220

21+
#include <QSystemTrayIcon>
22+
#include <QQuickImageProvider>
2323
#include <QQmlNetworkAccessManagerFactory>
2424

2525
class QScreen;
@@ -145,6 +145,7 @@ public slots:
145145

146146
void createShareDialog(const QString &localPath);
147147
void createFileActivityDialog(const QString &localPath);
148+
void createTokenInitDialog(const QVariantList &tokensInfo);
148149

149150
void presentShareViewInTray(const QString &localPath);
150151

@@ -185,6 +186,7 @@ private slots:
185186
QSet<qlonglong> _callsAlreadyNotified;
186187
QPointer<QObject> _editFileLocallyLoadingDialog;
187188
QVector<QQuickWindow*> _fileDetailDialogs;
189+
QQuickWindow* _tokenInitDialog = nullptr;
188190
};
189191

190192
} // namespace OCC

src/libsync/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ set(libsync_SRCS
105105
clientsideencryption.cpp
106106
clientsideencryptionjobs.h
107107
clientsideencryptionjobs.cpp
108+
clientsidetokenselector.h
109+
clientsidetokenselector.cpp
108110
datetimeprovider.h
109111
datetimeprovider.cpp
110112
ocsuserstatusconnector.h

src/libsync/account.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "clientsideencryption.h"
3434
#include "ocsuserstatusconnector.h"
3535

36+
#include "config.h"
37+
3638
#include <QLoggingCategory>
3739
#include <QNetworkReply>
3840
#include <QNetworkAccessManager>
@@ -1023,9 +1025,9 @@ bool Account::askUserForMnemonic() const
10231025
return _e2eAskUserForMnemonic;
10241026
}
10251027

1026-
bool Account::useHardwareTokenEncryption() const
1028+
bool Account::enforceUseHardwareTokenEncryption() const
10271029
{
1028-
return !encryptionHardwareTokenDriverPath().isEmpty();
1030+
return CLIENTSIDEENCRYPTION_ENFORCE_USB_TOKEN;
10291031
}
10301032

10311033
QString Account::encryptionHardwareTokenDriverPath() const

src/libsync/account.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject
8888
Q_PROPERTY(QUrl url MEMBER _url)
8989
Q_PROPERTY(bool e2eEncryptionKeysGenerationAllowed MEMBER _e2eEncryptionKeysGenerationAllowed)
9090
Q_PROPERTY(bool askUserForMnemonic READ askUserForMnemonic WRITE setAskUserForMnemonic NOTIFY askUserForMnemonicChanged)
91-
Q_PROPERTY(bool useHardwareTokenEncryption READ useHardwareTokenEncryption NOTIFY useHardwareTokenEncryptionChanged)
91+
Q_PROPERTY(bool enforceUseHardwareTokenEncryption READ enforceUseHardwareTokenEncryption NOTIFY enforceUseHardwareTokenEncryptionChanged)
9292
Q_PROPERTY(QString encryptionHardwareTokenDriverPath READ encryptionHardwareTokenDriverPath NOTIFY encryptionHardwareTokenDriverPathChanged)
9393

9494
public:
@@ -328,7 +328,7 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject
328328

329329
[[nodiscard]] bool askUserForMnemonic() const;
330330

331-
[[nodiscard]] bool useHardwareTokenEncryption() const;
331+
[[nodiscard]] bool enforceUseHardwareTokenEncryption() const;
332332

333333
[[nodiscard]] QString encryptionHardwareTokenDriverPath() const;
334334

@@ -360,7 +360,7 @@ public slots:
360360
void accountChangedDisplayName();
361361
void prettyNameChanged();
362362
void askUserForMnemonicChanged();
363-
void useHardwareTokenEncryptionChanged();
363+
void enforceUseHardwareTokenEncryptionChanged();
364364
void encryptionHardwareTokenDriverPathChanged();
365365

366366
/// Used in RemoteWipe

src/libsync/clientsideencryption.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -1012,13 +1012,22 @@ std::optional<QByteArray> decryptStringAsymmetricWithToken(ENGINE *sslEngine,
10121012
}
10131013

10141014

1015-
ClientSideEncryption::ClientSideEncryption() = default;
1015+
ClientSideEncryption::ClientSideEncryption()
1016+
{
1017+
connect(&_usbTokenInformation, &ClientSideTokenSelector::discoveredTokensChanged,
1018+
this, &ClientSideEncryption::displayTokenInitDialog);
1019+
}
10161020

10171021
bool ClientSideEncryption::isInitialized() const
10181022
{
10191023
return !getMnemonic().isEmpty();
10201024
}
10211025

1026+
QVariantList ClientSideEncryption::discoveredTokens() const
1027+
{
1028+
return _usbTokenInformation.discoveredTokens();
1029+
}
1030+
10221031
const QSslKey &ClientSideEncryption::getPublicKey() const
10231032
{
10241033
return _publicKey;
@@ -1080,8 +1089,19 @@ void ClientSideEncryption::initialize(const AccountPtr &account)
10801089
return;
10811090
}
10821091

1083-
if (account->useHardwareTokenEncryption()) {
1084-
initializeHardwareTokenEncryption(account);
1092+
if (account->enforceUseHardwareTokenEncryption()) {
1093+
if (_usbTokenInformation.isSetup()) {
1094+
initializeHardwareTokenEncryption(account);
1095+
} else if (account->e2eEncryptionKeysGenerationAllowed() && account->askUserForMnemonic()) {
1096+
_usbTokenInformation.searchForToken();
1097+
if (_usbTokenInformation.isSetup()) {
1098+
initializeHardwareTokenEncryption(account);
1099+
} else {
1100+
emit initializationFinished();
1101+
}
1102+
} else {
1103+
emit initializationFinished();
1104+
}
10851105
} else {
10861106
fetchCertificateFromKeyChain(account);
10871107
}

src/libsync/clientsideencryption.h

+9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#define CLIENTSIDEENCRYPTION_H
1717

1818
#include "accountfwd.h"
19+
1920
#include "networkjobs.h"
21+
#include "clientsidetokenselector.h"
2022

2123
#include <QString>
2224
#include <QObject>
@@ -139,6 +141,10 @@ class OWNCLOUDSYNC_EXPORT ClientSideEncryption : public QObject {
139141

140142
[[nodiscard]] bool isInitialized() const;
141143

144+
[[nodiscard]] bool tokenIsSetup() const;
145+
146+
[[nodiscard]] QVariantList discoveredTokens() const;
147+
142148
[[nodiscard]] const QSslKey& getPublicKey() const;
143149

144150
void setPublicKey(const QSslKey &publicKey);
@@ -166,6 +172,7 @@ class OWNCLOUDSYNC_EXPORT ClientSideEncryption : public QObject {
166172
void certificateDeleted();
167173
void mnemonicDeleted();
168174
void publicKeyDeleted();
175+
void displayTokenInitDialog();
169176

170177
public slots:
171178
void initialize(const OCC::AccountPtr &account);
@@ -248,6 +255,8 @@ private slots:
248255
QString _mnemonic;
249256
bool _newMnemonicGenerated = false;
250257

258+
ClientSideTokenSelector _usbTokenInformation;
259+
251260
PKCS11_KEY* _tokenPublicKey = nullptr;
252261
PKCS11_KEY* _tokenPrivateKey = nullptr;
253262
};
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright © 2023, Matthieu Gallien <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* for more details.
13+
*/
14+
15+
#define OPENSSL_SUPPRESS_DEPRECATED
16+
17+
#include "clientsidetokenselector.h"
18+
19+
#include <QLoggingCategory>
20+
21+
#include <libp11.h>
22+
23+
namespace OCC
24+
{
25+
26+
Q_LOGGING_CATEGORY(lcCseSelector, "nextcloud.sync.clientsideencryption.selector", QtInfoMsg)
27+
28+
ClientSideTokenSelector::ClientSideTokenSelector(QObject *parent)
29+
: QObject{parent}
30+
{
31+
32+
}
33+
34+
bool ClientSideTokenSelector::isSetup() const
35+
{
36+
return false;
37+
}
38+
39+
QVariantList ClientSideTokenSelector::discoveredTokens() const
40+
{
41+
return _discoveredTokens;
42+
}
43+
44+
void failedToInitialize(std::nullptr_t)
45+
{
46+
}
47+
48+
void ClientSideTokenSelector::searchForToken()
49+
{
50+
auto account = nullptr;
51+
auto ctx = PKCS11_CTX_new();
52+
53+
auto rc = PKCS11_CTX_load(ctx, "opensc-pkcs11.so");
54+
if (rc) {
55+
qCWarning(lcCseSelector()) << "loading pkcs11 engine failed:" << ERR_reason_error_string(ERR_get_error());
56+
57+
failedToInitialize(account);
58+
return;
59+
}
60+
61+
auto tokensCount = 0u;
62+
PKCS11_SLOT *tokenSlots = nullptr;
63+
/* get information on all slots */
64+
if (PKCS11_enumerate_slots(ctx, &tokenSlots, &tokensCount) < 0) {
65+
qCWarning(lcCseSelector()) << "no slots available" << ERR_reason_error_string(ERR_get_error());
66+
67+
failedToInitialize(account);
68+
return;
69+
}
70+
71+
_discoveredTokens.clear();
72+
auto currentSlot = static_cast<PKCS11_SLOT*>(nullptr);
73+
for(auto i = 0u; i < tokensCount; ++i) {
74+
currentSlot = PKCS11_find_next_token(ctx, tokenSlots, tokensCount, currentSlot);
75+
if (currentSlot == nullptr || currentSlot->token == nullptr) {
76+
qCWarning(lcCseSelector()) << "no token available" << ERR_reason_error_string(ERR_get_error());
77+
78+
failedToInitialize(account);
79+
return;
80+
}
81+
qCInfo(lcCseSelector()) << "Slot manufacturer......:" << currentSlot->manufacturer;
82+
qCInfo(lcCseSelector()) << "Slot description.......:" << currentSlot->description;
83+
qCInfo(lcCseSelector()) << "Slot token label.......:" << currentSlot->token->label;
84+
qCInfo(lcCseSelector()) << "Slot token manufacturer:" << currentSlot->token->manufacturer;
85+
qCInfo(lcCseSelector()) << "Slot token model.......:" << currentSlot->token->model;
86+
qCInfo(lcCseSelector()) << "Slot token serialnr....:" << currentSlot->token->serialnr;
87+
88+
_discoveredTokens.push_back(QVariantMap{{QStringLiteral("manufacturer"), QString::fromLatin1(currentSlot->manufacturer)},
89+
{QStringLiteral("description"), QString::fromLatin1(currentSlot->description)},
90+
{QStringLiteral("label"), QString::fromLatin1(currentSlot->token->label)},});
91+
}
92+
emit discoveredTokensChanged();
93+
}
94+
95+
}

0 commit comments

Comments
 (0)