• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • remote
credentials.cpp
Go to the documentation of this file.
1/*
2 * Copyright © 2009 Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
12 *
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include "credentials.h"
20
21#include "authorizationmanager.h"
22#include "config-plasma.h"
23
24#include <QObject>
25
26#ifdef ENABLE_REMOTE_WIDGETS
27#include <QtCrypto>
28#endif
29
30#include <kdebug.h>
31#include <kstandarddirs.h>
32
33#define REQUIRED_FEATURES "rsa,sha1,pkey"
34
35namespace Plasma {
36
37class CredentialsPrivate {
38public:
39 CredentialsPrivate()
40 {
41 }
42
43 CredentialsPrivate(const QString &id, const QString &name,
44 const QString &pemKey, bool isPrivateKey)
45 : id(id),
46 name(name)
47 {
48 #ifdef ENABLE_REMOTE_WIDGETS
49 if (!QCA::isSupported(REQUIRED_FEATURES)) {
50 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
51 return;
52 }
53
54 if (isPrivateKey) {
55 privateKey = QCA::PrivateKey::fromPEM(pemKey);
56 publicKey = privateKey.toPublicKey();
57 } else {
58 publicKey = QCA::PublicKey::fromPEM(pemKey);
59 }
60 #endif
61 }
62
63 ~CredentialsPrivate()
64 {
65 }
66
67 QString id;
68 QString name;
69
70#ifdef ENABLE_REMOTE_WIDGETS
71 QCA::PublicKey publicKey;
72 QCA::PrivateKey privateKey;
73#endif
74};
75
76Credentials::Credentials(const QString &id, const QString &name,
77 const QString &key, bool isPrivateKey)
78 : d(new CredentialsPrivate(id, name, key, isPrivateKey))
79{
80}
81
82Credentials::Credentials()
83 : d(new CredentialsPrivate())
84{
85}
86
87Credentials::Credentials(const Credentials &other)
88 : d(new CredentialsPrivate())
89{
90 *d = *other.d;
91}
92
93Credentials::~Credentials()
94{
95 delete d;
96}
97
98Credentials &Credentials::operator=(const Credentials &other)
99{
100 *d = *other.d;
101 return *this;
102}
103
104Credentials Credentials::createCredentials(const QString &name)
105{
106#ifdef ENABLE_REMOTE_WIDGETS
107 if (!QCA::isSupported(REQUIRED_FEATURES)) {
108 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
109 return Credentials();
110 }
111
112 QCA::KeyGenerator generator;
113 QCA::PrivateKey key = generator.createRSA(2048);
114 QString pemKey(key.toPublicKey().toPEM());
115 QString id = QCA::Hash("sha1").hashToString(pemKey.toLatin1());
116 return Credentials(id, name, key.toPEM(), true);
117#else
118 return Credentials();
119#endif
120}
121
122TrustLevel Credentials::trustLevel() const
123{
134 //Trust no one ;)
135 return ValidCredentials;
136}
137
138bool Credentials::isValid() const
139{
140#ifdef ENABLE_REMOTE_WIDGETS
141 if (!QCA::isSupported(REQUIRED_FEATURES)) {
142 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
143 return false;
144 }
145
146 if (d->publicKey.isNull()) {
147 return false;
148 } else {
149 QString id = QCA::Hash("sha1").hashToString(d->publicKey.toPEM().toLatin1());
150 return (id == d->id);
151 }
152#else
153 kDebug() << "libplasma is compiled without support for remote widgets. Key invalid.";
154 return false;
155#endif
156}
157
158QString Credentials::name() const
159{
160 return d->name;
161}
162
163QString Credentials::id() const
164{
165 return d->id;
166}
167
168bool Credentials::isValidSignature(const QByteArray &signature, const QByteArray &payload)
169{
170#ifdef ENABLE_REMOTE_WIDGETS
171 if (!QCA::isSupported(REQUIRED_FEATURES)) {
172 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
173 return false;
174 }
175
176 if (d->publicKey.canVerify()) {
177 if (!isValid()) {
178 kDebug() << "Key is null?";
179 }
180 QCA::PublicKey publicKey = QCA::PublicKey::fromPEM(d->publicKey.toPEM());
181 publicKey.startVerify( QCA::EMSA3_MD5 );
182 publicKey.update(payload);
183 return ( publicKey.validSignature( signature ) );
184 } else {
185 kDebug() << "Can't verify?";
186 return false;
187 }
188#else
189 return false;
190#endif
191}
192
193bool Credentials::canSign() const
194{
195#ifdef ENABLE_REMOTE_WIDGETS
196 if (!QCA::isSupported(REQUIRED_FEATURES)) {
197 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
198 return false;
199 }
200
201 return d->privateKey.canSign();
202#else
203 return false;
204#endif
205}
206
207QByteArray Credentials::signMessage(const QByteArray &message)
208{
209#ifdef ENABLE_REMOTE_WIDGETS
210 if(!QCA::isSupported(REQUIRED_FEATURES)) {
211 kDebug() << "RSA not supported";
212 return QByteArray();
213 } else if (canSign()) {
214 //QCA::PrivateKey privateKey = QCA::PrivateKey::fromPEM(d->privateKey.toPEM());
215 d->privateKey.startSign( QCA::EMSA3_MD5 );
216 d->privateKey.update( message );
217 QByteArray signature = d->privateKey.signature();
218 return signature;
219 } else {
220 return QByteArray();
221 }
222#else
223 return QByteArray();
224#endif
225}
226
227Credentials Credentials::toPublicCredentials() const
228{
229#ifdef ENABLE_REMOTE_WIDGETS
230 Credentials result(*this);
231 result.d->privateKey = QCA::PrivateKey();
232 return result;
233#else
234 return Credentials();
235#endif
236}
237
238QDataStream &operator<<(QDataStream &out, const Credentials &myObj)
239{
240#ifdef ENABLE_REMOTE_WIDGETS
241 if (!QCA::isSupported(REQUIRED_FEATURES)) {
242 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
243 return out;
244 }
245
246 QString privateKeyPem;
247 QString publicKeyPem;
248
249 if (!myObj.d->privateKey.isNull()) {
250 privateKeyPem = myObj.d->privateKey.toPEM();
251 }
252 if (!myObj.d->publicKey.isNull()) {
253 publicKeyPem = myObj.d->publicKey.toPEM();
254 }
255
256 out << 1 << myObj.d->id << myObj.d->name << privateKeyPem << publicKeyPem;
257#endif
258
259 return out;
260}
261
262QDataStream &operator>>(QDataStream &in, Credentials &myObj)
263{
264#ifdef ENABLE_REMOTE_WIDGETS
265 if (!QCA::isSupported(REQUIRED_FEATURES)) {
266 kWarning() << "QCA doesn't support " << REQUIRED_FEATURES;
267 return in;
268 }
269
270 QString privateKeyString;
271 QString publicKeyString;
272 uint version;
273
274 in >> version >> myObj.d->id >> myObj.d->name >> privateKeyString >> publicKeyString;
275 QCA::ConvertResult conversionResult;
276
277 if (!privateKeyString.isEmpty()) {
278 myObj.d->privateKey = QCA::PrivateKey::fromPEM(privateKeyString,
279 QByteArray(), &conversionResult);
280 }
281
282 if (!publicKeyString.isEmpty()) {
283 myObj.d->publicKey = QCA::PublicKey::fromPEM(publicKeyString, &conversionResult);
284 }
285
286 if (conversionResult != QCA::ConvertGood) {
287 kDebug() << "Unsuccessfull conversion of key?";
288 }
289#endif
290
291 return in;
292}
293
294}
authorizationmanager.h
Plasma::Credentials
This class encapsules someone's identity.
Definition: credentials.h:43
Plasma::Credentials::canSign
bool canSign() const
Definition: credentials.cpp:193
Plasma::Credentials::name
QString name() const
Definition: credentials.cpp:158
Plasma::Credentials::Credentials
Credentials()
Default constructor.
Definition: credentials.cpp:82
Plasma::Credentials::isValid
bool isValid() const
Definition: credentials.cpp:138
REQUIRED_FEATURES
#define REQUIRED_FEATURES
Definition: credentials.cpp:33
credentials.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
Plasma::version
unsigned int version()
The runtime version of libplasma.
Definition: version.cpp:26
Plasma::TrustLevel
TrustLevel
Definition: plasma.h:271
Plasma::ValidCredentials
@ ValidCredentials
The credentials are valid.
Definition: plasma.h:274
Plasma::operator<<
QDataStream & operator<<(QDataStream &out, const Credentials &myObj)
Streaming operators for sending/storing identities.
Definition: credentials.cpp:238
Plasma::operator>>
QDataStream & operator>>(QDataStream &in, Credentials &myObj)
Definition: credentials.cpp:262
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal