KWallet

kwalletfreedesktopsession.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2021 Slava Aseev <nullptrnine@basealt.ru>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7#include "kwalletfreedesktopsession.h"
8
9#include "kwalletfreedesktopsessionadaptor.h"
10#include <QDBusConnection>
11
12KWalletFreedesktopSession::KWalletFreedesktopSession(KWalletFreedesktopService *service,
13 std::unique_ptr<KWalletFreedesktopSessionAlgorithm> algorithm,
14 QString sessionPath,
15 const QDBusConnection &connection,
16 const QDBusMessage &message)
17 : m_service(service)
18 , m_algorithm(std::move(algorithm))
19 , m_sessionPath(std::move(sessionPath))
20 , m_serviceBusName(message.service())
21{
22 (void)new KWalletFreedesktopSessionAdaptor(this);
23 QDBusConnection::sessionBus().registerObject(m_sessionPath, this);
24
25 m_serviceWatcher.setConnection(connection);
26 m_serviceWatcher.addWatchedService(m_serviceBusName);
27 m_serviceWatcher.setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
28 connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &KWalletFreedesktopSession::slotServiceOwnerChanged);
29}
30
31void KWalletFreedesktopSession::slotServiceOwnerChanged(const QString &, const QString &, const QString &)
32{
33 fdoService()->deleteSession(m_sessionPath);
34}
35
36void KWalletFreedesktopSession::Close()
37{
38 if (message().service() != m_serviceBusName) {
39 sendErrorReply(QDBusError::ErrorType::UnknownObject, QStringLiteral("Can't find session ") + m_sessionPath);
40 } else {
41 fdoService()->deleteSession(m_sessionPath);
42 }
43}
44
45QByteArray KWalletFreedesktopSession::negotiationOutput() const
46{
47 return m_algorithm->negotiationOutput();
48}
49
50bool KWalletFreedesktopSession::encrypt(const QDBusMessage &message, FreedesktopSecret &secret) const
51{
52 if (message.service() != m_serviceBusName) {
53 return false;
54 }
55
56 return m_algorithm->encrypt(secret);
57}
58
59bool KWalletFreedesktopSession::decrypt(const QDBusMessage &message, FreedesktopSecret &secret) const
60{
61 if (message.service() != m_serviceBusName) {
62 return false;
63 }
64
65 return m_algorithm->decrypt(secret);
66}
67
68KWalletFreedesktopService *KWalletFreedesktopSession::fdoService() const
69{
70 return m_service;
71}
72
73QByteArray KWalletFreedesktopSessionAlgorithmPlain::negotiationOutput() const
74{
75 return QByteArray();
76}
77
78bool KWalletFreedesktopSessionAlgorithmPlain::encrypt(FreedesktopSecret &secret) const
79{
80 secret.parameters = QByteArray();
81 return true;
82}
83
84bool KWalletFreedesktopSessionAlgorithmPlain::decrypt(FreedesktopSecret &) const
85{
86 return true;
87}
88
89KWalletFreedesktopSessionAlgorithmDhAes::KWalletFreedesktopSessionAlgorithmDhAes(const QCA::PublicKey &publicKey, QCA::SymmetricKey symmetricKey)
90 : m_publicKey(publicKey)
91 , m_symmetricKey(std::move(symmetricKey))
92{
93}
94
95QByteArray KWalletFreedesktopSessionAlgorithmDhAes::negotiationOutput() const
96{
97 return m_publicKey.toDH().y().toArray().toByteArray();
98}
99
100bool KWalletFreedesktopSessionAlgorithmDhAes::encrypt(FreedesktopSecret &secret) const
101{
102 auto initVector = QCA::InitializationVector(FDO_SECRETS_CIPHER_KEY_SIZE);
103 auto cipher = QCA::Cipher(QStringLiteral("aes128"), QCA::Cipher::CBC, QCA::Cipher::PKCS7, QCA::Encode, m_symmetricKey, initVector);
104 QCA::SecureArray result;
105 result.append(cipher.update(QCA::MemoryRegion(secret.value)));
106 if (cipher.ok()) {
107 result.append(cipher.final());
108 if (cipher.ok()) {
109 secret.value = std::move(result);
110 secret.parameters = initVector;
111 return true;
112 }
113 }
114 return false;
115}
116
117bool KWalletFreedesktopSessionAlgorithmDhAes::decrypt(FreedesktopSecret &secret) const
118{
119 auto cipher =
120 QCA::Cipher(QStringLiteral("aes128"), QCA::Cipher::CBC, QCA::Cipher::PKCS7, QCA::Decode, m_symmetricKey, QCA::InitializationVector(secret.parameters));
121 QCA::SecureArray result;
122 result.append(cipher.update(QCA::MemoryRegion(secret.value)));
123 if (cipher.ok()) {
124 result.append(cipher.final());
125 if (cipher.ok()) {
126 secret.value = std::move(result);
127 return true;
128 }
129 }
130 return false;
131}
132
133#include "moc_kwalletfreedesktopsession.cpp"
SecureArray & append(const SecureArray &a)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
const QDBusMessage & message() const const
void sendErrorReply(QDBusError::ErrorType type, const QString &msg) const const
void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:48:58 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.