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
73KWalletD *KWalletFreedesktopSession::backend() const
74{
75 return fdoService()->backend();
76}
77
78QDBusObjectPath KWalletFreedesktopSession::fdoObjectPath() const
79{
80 return QDBusObjectPath(m_sessionPath);
81}
82
83QByteArray KWalletFreedesktopSessionAlgorithmPlain::negotiationOutput() const
84{
85 return QByteArray();
86}
87
88bool KWalletFreedesktopSessionAlgorithmPlain::encrypt(FreedesktopSecret &secret) const
89{
90 secret.parameters = QByteArray();
91 return true;
92}
93
94bool KWalletFreedesktopSessionAlgorithmPlain::decrypt(FreedesktopSecret &) const
95{
96 return true;
97}
98
99KWalletFreedesktopSessionAlgorithmDhAes::KWalletFreedesktopSessionAlgorithmDhAes(const QCA::PublicKey &publicKey, QCA::SymmetricKey symmetricKey)
100 : m_publicKey(publicKey)
101 , m_symmetricKey(std::move(symmetricKey))
102{
103}
104
105QByteArray KWalletFreedesktopSessionAlgorithmDhAes::negotiationOutput() const
106{
107 return m_publicKey.toDH().y().toArray().toByteArray();
108}
109
110bool KWalletFreedesktopSessionAlgorithmDhAes::encrypt(FreedesktopSecret &secret) const
111{
112 auto initVector = QCA::InitializationVector(FDO_SECRETS_CIPHER_KEY_SIZE);
113 auto cipher = QCA::Cipher(QStringLiteral("aes128"), QCA::Cipher::CBC, QCA::Cipher::PKCS7, QCA::Encode, m_symmetricKey, initVector);
114 QCA::SecureArray result;
115 result.append(cipher.update(QCA::MemoryRegion(secret.value)));
116 if (cipher.ok()) {
117 result.append(cipher.final());
118 if (cipher.ok()) {
119 secret.value = std::move(result);
120 secret.parameters = initVector;
121 return true;
122 }
123 }
124 return false;
125}
126
127bool KWalletFreedesktopSessionAlgorithmDhAes::decrypt(FreedesktopSecret &secret) const
128{
129 auto cipher =
130 QCA::Cipher(QStringLiteral("aes128"), QCA::Cipher::CBC, QCA::Cipher::PKCS7, QCA::Decode, m_symmetricKey, QCA::InitializationVector(secret.parameters));
131 QCA::SecureArray result;
132 result.append(cipher.update(QCA::MemoryRegion(secret.value)));
133 if (cipher.ok()) {
134 result.append(cipher.final());
135 if (cipher.ok()) {
136 secret.value = std::move(result);
137 return true;
138 }
139 }
140 return false;
141}
142
143#include "moc_kwalletfreedesktopsession.cpp"
QCA::SecureArray toArray() const
BigInteger y() const
DHPublicKey toDH() const
QByteArray toByteArray() const
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
QString service() 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-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.