KWallet

kwalletbackend.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2001-2004 George Staikos <staikos@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef _KWALLETBACKEND_H
9#define _KWALLETBACKEND_H
10
11#include "backendpersisthandler.h"
12#include "kwalletbackend_export.h"
13#include "kwalletentry.h"
14#include <QMap>
15#include <QString>
16#include <QStringList>
17
18#ifdef HAVE_GPGMEPP
19#include <gpgme++/key.h>
20#endif // HAVE_GPGMEPP
21
22#define PBKDF2_SHA512_KEYSIZE 56
23#define PBKDF2_SHA512_SALTSIZE 56
24#define PBKDF2_SHA512_ITERATIONS 50000
25
26namespace KWallet
27{
28/**
29 * @internal
30 */
31class MD5Digest : public QByteArray
32{
33public:
34 MD5Digest()
35 : QByteArray(16, 0)
36 {
37 }
38 MD5Digest(const char *data)
39 : QByteArray(data, 16)
40 {
41 }
42 MD5Digest(const QByteArray &digest)
43 : QByteArray(digest)
44 {
45 }
46 virtual ~MD5Digest()
47 {
48 }
49
50 int operator<(const MD5Digest &r) const
51 {
52 int i = 0;
53 char x, y;
54 for (; i < 16; ++i) {
55 x = at(i);
56 y = r.at(i);
57 if (x != y) {
58 break;
59 }
60 }
61 if (i < 16 && x < y) {
62 return 1;
63 }
64 return 0;
65 }
66};
67
68/* @internal
69 */
70class KWALLETBACKEND_EXPORT Backend
71{
72public:
73 explicit Backend(const QString &name = QStringLiteral("kdewallet"), bool isPath = false);
74 ~Backend();
75
76 // Open and unlock the wallet.
77 // If opening succeeds, the password's hash will be remembered.
78 // If opening fails, the password's hash will be cleared.
79 int open(const QByteArray &password, WId w = 0);
80#ifdef HAVE_GPGMEPP
81 int open(const GpgME::Key &key);
82#endif
83
84 // Open and unlock the wallet using a pre-hashed password.
85 // If opening succeeds, the password's hash will be remembered.
86 // If opening fails, the password's hash will be cleared.
87 int openPreHashed(const QByteArray &passwordHash);
88
89 // Close the wallet, losing any changes.
90 // if save is true, the wallet is saved prior to closing it.
91 int close(bool save = false);
92
93 // Write the wallet to disk
94 int sync(WId w);
95
96 // Returns true if the current wallet is open.
97 bool isOpen() const;
98
99 // Returns the current wallet name.
100 const QString &walletName() const;
101
102 // Rename the wallet
103 int renameWallet(const QString &newName, bool isPath = false);
104
105 // The list of folders.
106 QStringList folderList() const;
107
108 // Force creation of a folder.
109 bool createFolder(const QString &f);
110
111 // Change the folder.
112 void setFolder(const QString &f)
113 {
114 _folder = f;
115 }
116
117 // Current folder. If empty, it's the global folder.
118 const QString &folder() const
119 {
120 return _folder;
121 }
122
123 // Does it have this folder?
124 bool hasFolder(const QString &f) const
125 {
126 return _entries.contains(f);
127 }
128
129 // Look up an entry. Returns null if it doesn't exist.
130 Entry *readEntry(const QString &key);
131
132#if KWALLET_BUILD_DEPRECATED_SINCE(5, 72)
133 // Look up a list of entries. Supports wildcards.
134 // You delete the list.
135 // Deprecated since 5.72, use entriesList()
136 QList<Entry *> readEntryList(const QString &key);
137#endif
138
139 // Get a list of all the entries in the current folder.
140 // @since 5.72
141 QList<Entry *> entriesList() const;
142
143 // Store an entry.
144 void writeEntry(Entry *e);
145
146 // Does this folder contain this entry?
147 bool hasEntry(const QString &key) const;
148
149 // Returns true if the entry was removed
150 bool removeEntry(const QString &key);
151
152 // Returns true if the folder was removed
153 bool removeFolder(const QString &f);
154
155 // The list of entries in this folder.
156 QStringList entryList() const;
157
158 // Rename an entry in this folder.
159 int renameEntry(const QString &oldName, const QString &newName);
160
161 // Set the password used for opening/closing the wallet.
162 // This does not sync the wallet to disk!
163 void setPassword(const QByteArray &password);
164
165 int ref()
166 {
167 return ++_ref;
168 }
169
170 int deref();
171
172 int refCount() const
173 {
174 return _ref;
175 }
176
177 static bool exists(const QString &wallet);
178
179 bool folderDoesNotExist(const QString &folder) const;
180
181 bool entryDoesNotExist(const QString &folder, const QString &entry) const;
182
183 static QString openRCToString(int rc);
184
185 void setCipherType(BackendCipherType ct);
186 BackendCipherType cipherType() const
187 {
188 return _cipherType;
189 }
190#ifdef HAVE_GPGMEPP
191 const GpgME::Key &gpgKey() const;
192#endif
193
194 static QString getSaveLocation();
195 static QString encodeWalletName(const QString &name);
196 static QString decodeWalletName(const QString &encodedName);
197
198private:
199 Q_DISABLE_COPY(Backend)
200 class BackendPrivate;
201 BackendPrivate *const d;
202 QString _name;
203 QString _path;
204 bool _open;
205 bool _useNewHash = false;
206 QString _folder;
207 int _ref = 0;
208 // Map Folder->Entries
209 typedef QMap<QString, Entry *> EntryMap;
210 typedef QMap<QString, EntryMap> FolderMap;
211 FolderMap _entries;
212 typedef QMap<MD5Digest, QList<MD5Digest>> HashMap;
213 HashMap _hashes;
214 QByteArray _passhash; // password hash used for saving the wallet
215 QByteArray _newPassHash; // Modern hash using KWALLET_HASH_PBKDF2_SHA512
216 BackendCipherType _cipherType; // the kind of encryption used for this wallet
217
218#ifdef HAVE_GPGMEPP
219 GpgME::Key _gpgKey;
220#endif
221 friend class BlowfishPersistHandler;
222 friend class GpgPersistHandler;
223
224 // open the wallet with the password already set. This is
225 // called internally by both open and openPreHashed.
226 int openInternal(WId w = 0);
227 int closeInternal(bool save);
228 void swapToNewHash();
229 QByteArray createAndSaveSalt(const QString &path) const;
230};
231
232}
233
234#endif
char at(qsizetype i) const const
char * data()
bool contains(QChar ch, Qt::CaseSensitivity cs) 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.