Skip to content

Commit 13d5810

Browse files
committed
split hardware token encryption init process to enable it when needed
if the account is configured to use encryption based on hardware token, the account will have a non empty path to the specific driver switching triggering the initialization process Signed-off-by: Matthieu Gallien <[email protected]>
1 parent d0701a1 commit 13d5810

File tree

4 files changed

+91
-66
lines changed

4 files changed

+91
-66
lines changed

src/libsync/account.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,16 @@ bool Account::askUserForMnemonic() const
990990
return _e2eAskUserForMnemonic;
991991
}
992992

993+
bool Account::useHardwareTokenEncryption() const
994+
{
995+
return !encryptionHardwareTokenDriverPath().isEmpty();
996+
}
997+
998+
QString Account::encryptionHardwareTokenDriverPath() const
999+
{
1000+
return {};
1001+
}
1002+
9931003
void Account::setAskUserForMnemonic(const bool ask)
9941004
{
9951005
_e2eAskUserForMnemonic = ask;

src/libsync/account.h

+8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject
8787
Q_PROPERTY(QUrl url MEMBER _url)
8888
Q_PROPERTY(bool e2eEncryptionKeysGenerationAllowed MEMBER _e2eEncryptionKeysGenerationAllowed)
8989
Q_PROPERTY(bool askUserForMnemonic READ askUserForMnemonic WRITE setAskUserForMnemonic NOTIFY askUserForMnemonicChanged)
90+
Q_PROPERTY(bool useHardwareTokenEncryption READ useHardwareTokenEncryption NOTIFY useHardwareTokenEncryptionChanged)
91+
Q_PROPERTY(QString encryptionHardwareTokenDriverPath READ encryptionHardwareTokenDriverPath NOTIFY encryptionHardwareTokenDriverPathChanged)
9092

9193
public:
9294
static AccountPtr create();
@@ -323,6 +325,10 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject
323325

324326
[[nodiscard]] bool askUserForMnemonic() const;
325327

328+
[[nodiscard]] bool useHardwareTokenEncryption() const;
329+
330+
[[nodiscard]] QString encryptionHardwareTokenDriverPath() const;
331+
326332
public slots:
327333
/// Used when forgetting credentials
328334
void clearQNAMCache();
@@ -351,6 +357,8 @@ public slots:
351357
void accountChangedDisplayName();
352358
void prettyNameChanged();
353359
void askUserForMnemonicChanged();
360+
void useHardwareTokenEncryptionChanged();
361+
void encryptionHardwareTokenDriverPathChanged();
354362

355363
/// Used in RemoteWipe
356364
void appPasswordRetrieved(QString);

src/libsync/clientsideencryption.cpp

+72-66
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,81 @@ std::optional<QByteArray> decryptStringAsymmetricWithToken(ENGINE *sslEngine, PK
937937
}
938938

939939

940-
ClientSideEncryption::ClientSideEncryption()
940+
ClientSideEncryption::ClientSideEncryption() = default;
941+
942+
const QSslKey &ClientSideEncryption::getPublicKey() const
943+
{
944+
return _publicKey;
945+
}
946+
947+
void ClientSideEncryption::setPublicKey(const QSslKey &publicKey)
948+
{
949+
_publicKey = publicKey;
950+
}
951+
952+
const QByteArray &ClientSideEncryption::getPrivateKey() const
953+
{
954+
return _privateKey;
955+
}
956+
957+
void ClientSideEncryption::setPrivateKey(const QByteArray &privateKey)
958+
{
959+
_privateKey = privateKey;
960+
}
961+
962+
PKCS11_KEY* ClientSideEncryption::getTokenPublicKey() const
963+
{
964+
return _tokenPublicKey;
965+
}
966+
967+
PKCS11_KEY* ClientSideEncryption::getTokenPrivateKey() const
968+
{
969+
return _tokenPrivateKey;
970+
}
971+
972+
bool ClientSideEncryption::useTokenBasedEncryption() const
973+
{
974+
return _tokenPublicKey && _tokenPrivateKey;
975+
}
976+
977+
const QString &ClientSideEncryption::getMnemonic() const
978+
{
979+
return _mnemonic;
980+
}
981+
982+
void ClientSideEncryption::setCertificate(const QSslCertificate &certificate)
983+
{
984+
_certificate = certificate;
985+
}
986+
987+
ENGINE* ClientSideEncryption::sslEngine() const
988+
{
989+
return ENGINE_get_default_RSA();
990+
}
991+
992+
void ClientSideEncryption::initialize(const AccountPtr &account)
993+
{
994+
Q_ASSERT(account);
995+
996+
if (account->useHardwareTokenEncryption()) {
997+
initializeHardwareTokenEncryption(account);
998+
}
999+
1000+
qCInfo(lcCse()) << "Initializing";
1001+
if (!account->capabilities().clientSideEncryptionAvailable()) {
1002+
qCInfo(lcCse()) << "No Client side encryption available on server.";
1003+
emit initializationFinished();
1004+
return;
1005+
}
1006+
1007+
fetchCertificateFromKeyChain(account);
1008+
}
1009+
1010+
void ClientSideEncryption::initializeHardwareTokenEncryption(const AccountPtr &account)
9411011
{
9421012
auto ctx = PKCS11_CTX_new();
9431013

944-
auto rc = PKCS11_CTX_load(ctx, "");
1014+
auto rc = PKCS11_CTX_load(ctx, account->encryptionHardwareTokenDriverPath().toLatin1().constData());
9451015
if (rc) {
9461016
qCWarning(lcCse()) << "loading pkcs11 engine failed:" << ERR_reason_error_string(ERR_get_error());
9471017
rc = 1;
@@ -1047,70 +1117,6 @@ ClientSideEncryption::ClientSideEncryption()
10471117
<< "need login:" << (tokenPublicKey->needLogin ? "true" : "false");
10481118
}
10491119

1050-
const QSslKey &ClientSideEncryption::getPublicKey() const
1051-
{
1052-
return _publicKey;
1053-
}
1054-
1055-
void ClientSideEncryption::setPublicKey(const QSslKey &publicKey)
1056-
{
1057-
_publicKey = publicKey;
1058-
}
1059-
1060-
const QByteArray &ClientSideEncryption::getPrivateKey() const
1061-
{
1062-
return _privateKey;
1063-
}
1064-
1065-
void ClientSideEncryption::setPrivateKey(const QByteArray &privateKey)
1066-
{
1067-
_privateKey = privateKey;
1068-
}
1069-
1070-
PKCS11_KEY* ClientSideEncryption::getTokenPublicKey() const
1071-
{
1072-
return _tokenPublicKey;
1073-
}
1074-
1075-
PKCS11_KEY* ClientSideEncryption::getTokenPrivateKey() const
1076-
{
1077-
return _tokenPrivateKey;
1078-
}
1079-
1080-
bool ClientSideEncryption::useTokenBasedEncryption() const
1081-
{
1082-
return _tokenPublicKey && _tokenPrivateKey;
1083-
}
1084-
1085-
const QString &ClientSideEncryption::getMnemonic() const
1086-
{
1087-
return _mnemonic;
1088-
}
1089-
1090-
void ClientSideEncryption::setCertificate(const QSslCertificate &certificate)
1091-
{
1092-
_certificate = certificate;
1093-
}
1094-
1095-
ENGINE* ClientSideEncryption::sslEngine() const
1096-
{
1097-
return ENGINE_get_default_RSA();
1098-
}
1099-
1100-
void ClientSideEncryption::initialize(const AccountPtr &account)
1101-
{
1102-
Q_ASSERT(account);
1103-
1104-
qCInfo(lcCse()) << "Initializing";
1105-
if (!account->capabilities().clientSideEncryptionAvailable()) {
1106-
qCInfo(lcCse()) << "No Client side encryption available on server.";
1107-
emit initializationFinished();
1108-
return;
1109-
}
1110-
1111-
fetchCertificateFromKeyChain(account);
1112-
}
1113-
11141120
void ClientSideEncryption::fetchCertificateFromKeyChain(const AccountPtr &account)
11151121
{
11161122
const QString kck = AbstractCredentials::keychainKey(

src/libsync/clientsideencryption.h

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class OWNCLOUDSYNC_EXPORT ClientSideEncryption : public QObject {
183183

184184
public slots:
185185
void initialize(const OCC::AccountPtr &account);
186+
void initializeHardwareTokenEncryption(const AccountPtr &account);
186187
void forgetSensitiveData(const OCC::AccountPtr &account);
187188

188189
private slots:

0 commit comments

Comments
 (0)