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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
35 namespace Plasma {
36 
37 class CredentialsPrivate {
38 public:
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 
76 Credentials::Credentials(const QString &id, const QString &name,
77  const QString &key, bool isPrivateKey)
78  : d(new CredentialsPrivate(id, name, key, isPrivateKey))
79 {
80 }
81 
82 Credentials::Credentials()
83  : d(new CredentialsPrivate())
84 {
85 }
86 
87 Credentials::Credentials(const Credentials &other)
88  : d(new CredentialsPrivate())
89 {
90  *d = *other.d;
91 }
92 
93 Credentials::~Credentials()
94 {
95  delete d;
96 }
97 
98 Credentials &Credentials::operator=(const Credentials &other)
99 {
100  *d = *other.d;
101  return *this;
102 }
103 
104 Credentials 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 
122 TrustLevel Credentials::trustLevel() const
123 {
134  //Trust no one ;)
135  return ValidCredentials;
136 }
137 
138 bool 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 
158 QString Credentials::name() const
159 {
160  return d->name;
161 }
162 
163 QString Credentials::id() const
164 {
165  return d->id;
166 }
167 
168 bool 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 
193 bool 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 
207 QByteArray 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 
227 Credentials 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 
238 QDataStream &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 
262 QDataStream &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::createCredentials
static Credentials createCredentials(const QString &name)
Create a new identity with a new set of random public/private keys.
Definition: credentials.cpp:104
Plasma::Credentials::isValid
bool isValid() const
Definition: credentials.cpp:138
Plasma::Credentials::name
QString name() const
Definition: credentials.cpp:158
Plasma::Credentials::id
QString id() const
Definition: credentials.cpp:163
Plasma::version
unsigned int version()
The runtime version of libplasma.
Definition: version.cpp:26
Plasma::Credentials::operator=
Credentials & operator=(const Credentials &other)
Definition: credentials.cpp:98
Plasma::Credentials::~Credentials
~Credentials()
Definition: credentials.cpp:93
Plasma::TrustLevel
TrustLevel
Definition: plasma.h:271
credentials.h
Plasma::operator<<
QDataStream & operator<<(QDataStream &out, const Credentials &myObj)
Streaming operators for sending/storing identities.
Definition: credentials.cpp:238
Plasma::Credentials::isValidSignature
bool isValidSignature(const QByteArray &signature, const QByteArray &message)
Definition: credentials.cpp:168
Plasma::ValidCredentials
The credentials are valid.
Definition: plasma.h:274
Plasma::Credentials::toPublicCredentials
Credentials toPublicCredentials() const
Definition: credentials.cpp:227
Plasma::Credentials::trustLevel
TrustLevel trustLevel() const
Definition: credentials.cpp:122
Plasma::operator>>
QDataStream & operator>>(QDataStream &in, Credentials &myObj)
Definition: credentials.cpp:262
Plasma::Credentials
This class encapsules someone's identity.
Definition: credentials.h:42
REQUIRED_FEATURES
#define REQUIRED_FEATURES
Definition: credentials.cpp:33
Plasma::Credentials::signMessage
QByteArray signMessage(const QByteArray &message)
Definition: credentials.cpp:207
Plasma::Credentials::Credentials
Credentials()
Default constructor.
Definition: credentials.cpp:82
Plasma::Credentials::canSign
bool canSign() const
Definition: credentials.cpp:193
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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