KWallet

kwalletfreedesktopservice.h
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#ifndef _KWALLETFREEDESKTOPSERVICE_H_
8#define _KWALLETFREEDESKTOPSERVICE_H_
9
10#include <KConfig>
11#include <QDBusArgument>
12#include <QDBusServiceWatcher>
13#include <QHash>
14#include <QPointer>
15#include <QString>
16#include <QtCrypto>
17
18#include "kwalletdbuscontext.h"
19
20#define FDO_APPID QString()
21#define FDO_SECRETS_SERVICE_OBJECT "/org/freedesktop/secrets"
22#define FDO_ALIAS_PATH "/org/freedesktop/secrets/aliases/"
23
24static inline constexpr size_t FDO_SECRETS_CIPHER_KEY_SIZE = 16;
25static inline constexpr int FDO_DH_PUBLIC_KEY_SIZE = 128;
26
27class KWalletD;
28
29class FreedesktopSecret
30{
31public:
32 FreedesktopSecret() = default;
33
34 FreedesktopSecret(QDBusObjectPath iSession, const QCA::SecureArray &iValue, QString iMimeType)
35 : session(std::move(iSession))
36 , value(iValue)
37 , mimeType(std::move(iMimeType))
38 {
39 }
40
41 friend QDBusArgument &operator<<(QDBusArgument &arg, const FreedesktopSecret &secret);
42 friend const QDBusArgument &operator>>(const QDBusArgument &arg, FreedesktopSecret &secret);
43
44 QDBusObjectPath session;
45 QCA::SecureArray parameters;
46 QCA::SecureArray value;
47 QString mimeType;
48};
49
50struct PropertiesMap {
51 QVariantMap map;
52};
53
54struct EntryLocation {
55 static EntryLocation fromUniqueLabel(const struct FdoUniqueLabel &uniqLabel);
56 struct FdoUniqueLabel toUniqueLabel() const;
57
58 bool operator==(const EntryLocation &rhs) const
59 {
60 return folder == rhs.folder && key == rhs.key;
61 }
62
63 bool operator!=(const EntryLocation &rhs) const
64 {
65 return !(*this == rhs);
66 }
67
68 QString folder;
69 QString key;
70};
71
72struct FdoUniqueLabel {
73 static FdoUniqueLabel fromEntryLocation(const EntryLocation &entryLocation);
74 static FdoUniqueLabel fromName(const QString &name);
75 static QString makeName(const QString &label, int copyId);
76
77 bool operator==(const FdoUniqueLabel &rhs) const
78 {
79 return copyId == rhs.copyId && label == rhs.label;
80 }
81
82 bool operator!=(const FdoUniqueLabel &rhs) const
83 {
84 return !(*this == rhs);
85 }
86
87 QString toName() const;
88 EntryLocation toEntryLocation() const;
89
90 QString label;
91 int copyId = -1;
92};
93
96
97Q_DECLARE_METATYPE(FreedesktopSecret)
98Q_DECLARE_METATYPE(FreedesktopSecretMap)
99Q_DECLARE_METATYPE(PropertiesMap)
100Q_DECLARE_METATYPE(StrStrMap)
101Q_DECLARE_METATYPE(QCA::SecureArray)
102
103class KWalletFreedesktopSession;
104class KWalletFreedesktopSessionAlgorithm;
105class KWalletFreedesktopCollection;
106class KWalletFreedesktopPrompt;
107class KWalletFreedesktopItem;
108
109class KWalletFreedesktopService : public QObject, protected FDO_DBUS_CONTEXT
110{
111 /* org.freedesktop.Secret.Service properties */
112public:
113 Q_PROPERTY(QList<QDBusObjectPath> Collections READ collections)
114 QList<QDBusObjectPath> collections() const;
115
117
118public:
119 explicit KWalletFreedesktopService(KWalletD *parent);
120 ~KWalletFreedesktopService();
121
122 KWalletFreedesktopService(const KWalletFreedesktopService &) = delete;
123 KWalletFreedesktopService &operator=(const KWalletFreedesktopService &) = delete;
124
125 KWalletFreedesktopService(KWalletFreedesktopService &&) = delete;
126 KWalletFreedesktopService &operator=(KWalletFreedesktopService &&) = delete;
127
128 static QString wrapToCollectionPath(const QString &itemPath);
129
130 static QDBusObjectPath nextPromptPath();
131 KWalletD *backend() const;
132 QDBusObjectPath fdoObjectPath() const;
133
134 bool desecret(const QDBusMessage &message, FreedesktopSecret &secret);
135 bool ensecret(const QDBusMessage &message, FreedesktopSecret &secret);
136 KWalletFreedesktopItem *getItemByObjectPath(const QDBusObjectPath &path) const;
137 KWalletFreedesktopCollection *getCollectionByWalletName(const QString &walletName) const;
138 KWalletFreedesktopPrompt *getPromptByObjectPath(const QDBusObjectPath &path) const;
139
140 FdoUniqueLabel makeUniqueCollectionLabel(const QString &label);
141 QString makeUniqueWalletName(const QString &labelPrefix);
142 QDBusObjectPath makeUniqueObjectPath(const QString &walletName) const;
143
144 QString resolveIfAlias(QString alias);
145 QStringList readAliasesFor(const QString &walletName);
146 void createCollectionAlias(const QString &alias, KWalletFreedesktopCollection *collection);
147 void createCollectionAlias(const QString &alias, const QString &walletName);
148 void updateCollectionAlias(const QString &alias, const QString &walletName);
149 void removeAlias(const QString &alias);
150
151 void deletePrompt(const QString &objectPath);
152 void deleteSession(const QString &objectPath);
153 QDBusObjectPath promptUnlockCollection(const QString &walletName, int handle);
154
155 /* Emitters */
156 void onCollectionCreated(const QDBusObjectPath &path);
157 void onCollectionChanged(const QDBusObjectPath &path);
158 void onCollectionDeleted(const QDBusObjectPath &path);
159 void onPropertiesChanged(const QVariantMap &properties);
160
161private Q_SLOTS:
162 void lockCollection(const QString &name);
163 void entryUpdated(const QString &walletName, const QString &folder, const QString &entryName);
164 void entryDeleted(const QString &walletName, const QString &folder, const QString &entryName);
165 void entryRenamed(const QString &walletName, const QString &folder, const QString &oldName, const QString &newName);
166 void walletDeleted(const QString &walletName);
167 void walletCreated(const QString &walletCreated);
168 /*
169 void slotServiceOwnerChanged(const QString &name, const QString &oldOwner,
170 const QString &newOwner);
171 */
172
173private:
174 std::unique_ptr<KWalletFreedesktopSessionAlgorithm> createSessionAlgorithmPlain() const;
175 std::unique_ptr<KWalletFreedesktopSessionAlgorithm> createSessionAlgorithmDhAes(const QByteArray &clientKey) const;
176 QString createSession(std::unique_ptr<KWalletFreedesktopSessionAlgorithm> algorithm);
177 QString defaultWalletName(KConfigGroup &cfg);
178
179private:
180 std::map<QString, std::unique_ptr<KWalletFreedesktopSession>> m_sessions;
181 std::map<QString, std::unique_ptr<KWalletFreedesktopCollection>> m_collections;
182 std::map<QString, std::unique_ptr<KWalletFreedesktopPrompt>> m_prompts;
183
184 uint64_t m_session_counter = 0;
185
186 /*
187 QDBusServiceWatcher _serviceWatcher;
188 */
189 KWalletD *m_parent;
190 QCA::Initializer m_init;
191 KConfig m_kwalletrc;
192
193 /* Freedesktop API */
194
195 /* org.freedesktop.Secret.Service methods */
196public Q_SLOTS:
197 QDBusObjectPath CreateCollection(const QVariantMap &properties, const QString &alias, QDBusObjectPath &prompt);
198 FreedesktopSecretMap GetSecrets(const QList<QDBusObjectPath> &items, const QDBusObjectPath &session);
200 QDBusVariant OpenSession(const QString &algorithm, const QDBusVariant &input, QDBusObjectPath &result);
201 QDBusObjectPath ReadAlias(const QString &name);
202 QList<QDBusObjectPath> SearchItems(const StrStrMap &attributes, QList<QDBusObjectPath> &locked);
203 void SetAlias(const QString &name, const QDBusObjectPath &collection);
204 QList<QDBusObjectPath> Unlock(const QList<QDBusObjectPath> &objects, QDBusObjectPath &prompt);
205
206 /* org.freedesktop.Secret.Service signals */
208 void CollectionChanged(const QDBusObjectPath &collection);
209 void CollectionCreated(const QDBusObjectPath &collection);
210 void CollectionDeleted(const QDBusObjectPath &collection);
211};
212
213QDataStream &operator<<(QDataStream &stream, const QDBusObjectPath &value);
215
216const QDBusArgument &operator>>(const QDBusArgument &arg, PropertiesMap &value);
217QDBusArgument &operator<<(QDBusArgument &arg, const PropertiesMap &value);
218
219QDataStream &operator<<(QDataStream &stream, const QCA::SecureArray &value);
223
224void explicit_zero_mem(void *data, size_t size);
225
226#endif
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.