1 | /*
|
---|
2 | * qca.h - Qt Cryptographic Architecture
|
---|
3 | * Copyright (C) 2003 Justin Karneges
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2.1 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef QCA_H
|
---|
22 | #define QCA_H
|
---|
23 |
|
---|
24 | #include<qstring.h>
|
---|
25 | #include<qcstring.h>
|
---|
26 | #include<qdatetime.h>
|
---|
27 | #include<qmap.h>
|
---|
28 | #include<qptrlist.h>
|
---|
29 | #include<qobject.h>
|
---|
30 |
|
---|
31 | #ifdef Q_OS_WIN32
|
---|
32 | # ifndef QCA_STATIC
|
---|
33 | # ifdef QCA_MAKEDLL
|
---|
34 | # define QCA_EXPORT __declspec(dllexport)
|
---|
35 | # else
|
---|
36 | # define QCA_EXPORT __declspec(dllimport)
|
---|
37 | # endif
|
---|
38 | # endif
|
---|
39 | #endif
|
---|
40 | #ifndef QCA_EXPORT
|
---|
41 | #define QCA_EXPORT
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if defined(Q_OS_WIN32)
|
---|
45 | # ifdef QCA_PLUGIN_DLL
|
---|
46 | # define QCA_PLUGIN_EXPORT extern "C" __declspec(dllexport)
|
---|
47 | # else
|
---|
48 | # define QCA_PLUGIN_EXPORT extern "C" __declspec(dllimport)
|
---|
49 | # endif
|
---|
50 | #elif defined(Q_OS_OS2)
|
---|
51 | # define QCA_PLUGIN_EXPORT extern "C" _System
|
---|
52 | #endif
|
---|
53 | #ifndef QCA_PLUGIN_EXPORT
|
---|
54 | #define QCA_PLUGIN_EXPORT extern "C"
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #if defined(_MSC_VER) && _MSC_VER >= 1300
|
---|
58 | #define MSC_NET_HOTFIX
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | class QHostAddress;
|
---|
62 | class QStringList;
|
---|
63 |
|
---|
64 | class QCAProvider;
|
---|
65 | class QCA_HashContext;
|
---|
66 | class QCA_CipherContext;
|
---|
67 | class QCA_CertContext;
|
---|
68 |
|
---|
69 | namespace QCA
|
---|
70 | {
|
---|
71 | enum {
|
---|
72 | CAP_SHA1 = 0x0001,
|
---|
73 | CAP_SHA256 = 0x0002,
|
---|
74 | CAP_MD5 = 0x0004,
|
---|
75 | CAP_BlowFish = 0x0008,
|
---|
76 | CAP_TripleDES = 0x0010,
|
---|
77 | CAP_AES128 = 0x0020,
|
---|
78 | CAP_AES256 = 0x0040,
|
---|
79 | CAP_RSA = 0x0080,
|
---|
80 | CAP_X509 = 0x0100,
|
---|
81 | CAP_TLS = 0x0200,
|
---|
82 | CAP_SASL = 0x0400
|
---|
83 | };
|
---|
84 |
|
---|
85 | enum {
|
---|
86 | CBC = 0x0001,
|
---|
87 | CFB = 0x0002
|
---|
88 | };
|
---|
89 |
|
---|
90 | enum {
|
---|
91 | Encrypt = 0x0001,
|
---|
92 | Decrypt = 0x0002
|
---|
93 | };
|
---|
94 |
|
---|
95 | QCA_EXPORT void init();
|
---|
96 | QCA_EXPORT bool isSupported(int capabilities);
|
---|
97 | QCA_EXPORT void insertProvider(QCAProvider *);
|
---|
98 | QCA_EXPORT void unloadAllPlugins();
|
---|
99 |
|
---|
100 | QCA_EXPORT QString arrayToHex(const QByteArray &);
|
---|
101 | QCA_EXPORT QByteArray hexToArray(const QString &);
|
---|
102 |
|
---|
103 | class QCA_EXPORT Hash
|
---|
104 | {
|
---|
105 | public:
|
---|
106 | Hash(const Hash &);
|
---|
107 | Hash & operator=(const Hash &);
|
---|
108 | ~Hash();
|
---|
109 |
|
---|
110 | void clear();
|
---|
111 | void update(const QByteArray &a);
|
---|
112 | QByteArray final();
|
---|
113 |
|
---|
114 | protected:
|
---|
115 | Hash(QCA_HashContext *);
|
---|
116 |
|
---|
117 | private:
|
---|
118 | class Private;
|
---|
119 | Private *d;
|
---|
120 | };
|
---|
121 |
|
---|
122 | #if !defined(MSC_NET_HOTFIX)
|
---|
123 | template <class T>
|
---|
124 | class QCA_EXPORT HashStatic
|
---|
125 | {
|
---|
126 | public:
|
---|
127 | HashStatic<T>() {}
|
---|
128 |
|
---|
129 | static QByteArray hash(const QByteArray &a)
|
---|
130 | {
|
---|
131 | T obj;
|
---|
132 | obj.update(a);
|
---|
133 | return obj.final();
|
---|
134 | }
|
---|
135 |
|
---|
136 | static QByteArray hash(const QCString &cs)
|
---|
137 | {
|
---|
138 | QByteArray a(cs.length());
|
---|
139 | memcpy(a.data(), cs.data(), a.size());
|
---|
140 | return hash(a);
|
---|
141 | }
|
---|
142 |
|
---|
143 | static QString hashToString(const QByteArray &a)
|
---|
144 | {
|
---|
145 | return arrayToHex(hash(a));
|
---|
146 | }
|
---|
147 |
|
---|
148 | static QString hashToString(const QCString &cs)
|
---|
149 | {
|
---|
150 | return arrayToHex(hash(cs));
|
---|
151 | }
|
---|
152 | };
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | class QCA_EXPORT Cipher
|
---|
156 | {
|
---|
157 | public:
|
---|
158 | Cipher(const Cipher &);
|
---|
159 | Cipher & operator=(const Cipher &);
|
---|
160 | ~Cipher();
|
---|
161 |
|
---|
162 | QByteArray dyn_generateKey(int size=-1) const;
|
---|
163 | QByteArray dyn_generateIV() const;
|
---|
164 | void reset(int dir, int mode, const QByteArray &key, const QByteArray &iv, bool pad=true);
|
---|
165 | bool update(const QByteArray &a);
|
---|
166 | QByteArray final(bool *ok=0);
|
---|
167 |
|
---|
168 | protected:
|
---|
169 | Cipher(QCA_CipherContext *, int dir, int mode, const QByteArray &key, const QByteArray &iv, bool pad);
|
---|
170 |
|
---|
171 | private:
|
---|
172 | class Private;
|
---|
173 | Private *d;
|
---|
174 | };
|
---|
175 |
|
---|
176 | template <class T>
|
---|
177 | class QCA_EXPORT CipherStatic
|
---|
178 | {
|
---|
179 | public:
|
---|
180 | CipherStatic<T>() {}
|
---|
181 |
|
---|
182 | static QByteArray generateKey(int size=-1)
|
---|
183 | {
|
---|
184 | T obj;
|
---|
185 | return obj.dyn_generateKey(size);
|
---|
186 | }
|
---|
187 |
|
---|
188 | static QByteArray generateIV()
|
---|
189 | {
|
---|
190 | T obj;
|
---|
191 | return obj.dyn_generateIV();
|
---|
192 | }
|
---|
193 | };
|
---|
194 |
|
---|
195 | class QCA_EXPORT SHA1 : public Hash
|
---|
196 | #if !defined(MSC_NET_HOTFIX)
|
---|
197 | , public HashStatic<SHA1>
|
---|
198 | #endif
|
---|
199 | {
|
---|
200 | public:
|
---|
201 | SHA1();
|
---|
202 |
|
---|
203 | #if defined(MSC_NET_HOTFIX)
|
---|
204 | static QByteArray hash(const QByteArray &a)
|
---|
205 | {
|
---|
206 | SHA1 obj;
|
---|
207 | obj.update(a);
|
---|
208 | return obj.final();
|
---|
209 | }
|
---|
210 |
|
---|
211 | static QByteArray hash(const QCString &cs)
|
---|
212 | {
|
---|
213 | QByteArray a(cs.length());
|
---|
214 | memcpy(a.data(), cs.data(), a.size());
|
---|
215 | return hash(a);
|
---|
216 | }
|
---|
217 |
|
---|
218 | static QString hashToString(const QByteArray &a)
|
---|
219 | {
|
---|
220 | return arrayToHex(hash(a));
|
---|
221 | }
|
---|
222 |
|
---|
223 | static QString hashToString(const QCString &cs)
|
---|
224 | {
|
---|
225 | return arrayToHex(hash(cs));
|
---|
226 | }
|
---|
227 | #endif
|
---|
228 | };
|
---|
229 |
|
---|
230 | #if !defined(MSC_NET_HOTFIX)
|
---|
231 | class QCA_EXPORT SHA256 : public Hash, public HashStatic<SHA256>
|
---|
232 | {
|
---|
233 | public:
|
---|
234 | SHA256();
|
---|
235 | };
|
---|
236 | #endif
|
---|
237 |
|
---|
238 | class QCA_EXPORT MD5 : public Hash
|
---|
239 | #if !defined(MSC_NET_HOTFIX)
|
---|
240 | , public HashStatic<MD5>
|
---|
241 | #endif
|
---|
242 | {
|
---|
243 | public:
|
---|
244 | MD5();
|
---|
245 |
|
---|
246 | #if defined(MSC_NET_HOTFIX)
|
---|
247 | static QByteArray hash(const QByteArray &a)
|
---|
248 | {
|
---|
249 | MD5 obj;
|
---|
250 | obj.update(a);
|
---|
251 | return obj.final();
|
---|
252 | }
|
---|
253 |
|
---|
254 | static QByteArray hash(const QCString &cs)
|
---|
255 | {
|
---|
256 | QByteArray a(cs.length());
|
---|
257 | memcpy(a.data(), cs.data(), a.size());
|
---|
258 | return hash(a);
|
---|
259 | }
|
---|
260 |
|
---|
261 | static QString hashToString(const QByteArray &a)
|
---|
262 | {
|
---|
263 | return arrayToHex(hash(a));
|
---|
264 | }
|
---|
265 |
|
---|
266 | static QString hashToString(const QCString &cs)
|
---|
267 | {
|
---|
268 | return arrayToHex(hash(cs));
|
---|
269 | }
|
---|
270 | #endif
|
---|
271 | };
|
---|
272 |
|
---|
273 | class QCA_EXPORT BlowFish : public Cipher, public CipherStatic<BlowFish>
|
---|
274 | {
|
---|
275 | public:
|
---|
276 | BlowFish(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
|
---|
277 | };
|
---|
278 |
|
---|
279 | class QCA_EXPORT TripleDES : public Cipher, public CipherStatic<TripleDES>
|
---|
280 | {
|
---|
281 | public:
|
---|
282 | TripleDES(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
|
---|
283 | };
|
---|
284 |
|
---|
285 | class QCA_EXPORT AES128 : public Cipher, public CipherStatic<AES128>
|
---|
286 | {
|
---|
287 | public:
|
---|
288 | AES128(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
|
---|
289 | };
|
---|
290 |
|
---|
291 | class QCA_EXPORT AES256 : public Cipher, public CipherStatic<AES256>
|
---|
292 | {
|
---|
293 | public:
|
---|
294 | AES256(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
|
---|
295 | };
|
---|
296 |
|
---|
297 | class RSA;
|
---|
298 | class QCA_EXPORT RSAKey
|
---|
299 | {
|
---|
300 | public:
|
---|
301 | RSAKey();
|
---|
302 | RSAKey(const RSAKey &from);
|
---|
303 | RSAKey & operator=(const RSAKey &from);
|
---|
304 | ~RSAKey();
|
---|
305 |
|
---|
306 | bool isNull() const;
|
---|
307 | bool havePublic() const;
|
---|
308 | bool havePrivate() const;
|
---|
309 |
|
---|
310 | QByteArray toDER(bool publicOnly=false) const;
|
---|
311 | bool fromDER(const QByteArray &a);
|
---|
312 |
|
---|
313 | QString toPEM(bool publicOnly=false) const;
|
---|
314 | bool fromPEM(const QString &);
|
---|
315 |
|
---|
316 | // only call if you know what you are doing
|
---|
317 | bool fromNative(void *);
|
---|
318 |
|
---|
319 | private:
|
---|
320 | class Private;
|
---|
321 | Private *d;
|
---|
322 |
|
---|
323 | friend class RSA;
|
---|
324 | friend class TLS;
|
---|
325 | bool encrypt(const QByteArray &a, QByteArray *out, bool oaep) const;
|
---|
326 | bool decrypt(const QByteArray &a, QByteArray *out, bool oaep) const;
|
---|
327 | bool generate(unsigned int bits);
|
---|
328 | };
|
---|
329 |
|
---|
330 | class QCA_EXPORT RSA
|
---|
331 | {
|
---|
332 | public:
|
---|
333 | RSA();
|
---|
334 | ~RSA();
|
---|
335 |
|
---|
336 | RSAKey key() const;
|
---|
337 | void setKey(const RSAKey &);
|
---|
338 |
|
---|
339 | bool encrypt(const QByteArray &a, QByteArray *out, bool oaep=false) const;
|
---|
340 | bool decrypt(const QByteArray &a, QByteArray *out, bool oaep=false) const;
|
---|
341 |
|
---|
342 | static RSAKey generateKey(unsigned int bits);
|
---|
343 |
|
---|
344 | private:
|
---|
345 | RSAKey v_key;
|
---|
346 | };
|
---|
347 |
|
---|
348 | typedef QMap<QString, QString> CertProperties;
|
---|
349 | class QCA_EXPORT Cert
|
---|
350 | {
|
---|
351 | public:
|
---|
352 | Cert();
|
---|
353 | Cert(const Cert &);
|
---|
354 | Cert & operator=(const Cert &);
|
---|
355 | ~Cert();
|
---|
356 |
|
---|
357 | bool isNull() const;
|
---|
358 |
|
---|
359 | QString commonName() const;
|
---|
360 | QString serialNumber() const;
|
---|
361 | QString subjectString() const;
|
---|
362 | QString issuerString() const;
|
---|
363 | CertProperties subject() const;
|
---|
364 | CertProperties issuer() const;
|
---|
365 | QDateTime notBefore() const;
|
---|
366 | QDateTime notAfter() const;
|
---|
367 |
|
---|
368 | QByteArray toDER() const;
|
---|
369 | bool fromDER(const QByteArray &a);
|
---|
370 |
|
---|
371 | QString toPEM() const;
|
---|
372 | bool fromPEM(const QString &);
|
---|
373 |
|
---|
374 | private:
|
---|
375 | class Private;
|
---|
376 | Private *d;
|
---|
377 |
|
---|
378 | friend class TLS;
|
---|
379 | void fromContext(QCA_CertContext *);
|
---|
380 | };
|
---|
381 |
|
---|
382 | class QCA_EXPORT TLS : public QObject
|
---|
383 | {
|
---|
384 | Q_OBJECT
|
---|
385 | public:
|
---|
386 | enum Validity {
|
---|
387 | NoCert,
|
---|
388 | Valid,
|
---|
389 | HostMismatch,
|
---|
390 | Rejected,
|
---|
391 | Untrusted,
|
---|
392 | SignatureFailed,
|
---|
393 | InvalidCA,
|
---|
394 | InvalidPurpose,
|
---|
395 | SelfSigned,
|
---|
396 | Revoked,
|
---|
397 | PathLengthExceeded,
|
---|
398 | Expired,
|
---|
399 | Unknown
|
---|
400 | };
|
---|
401 | enum Error { ErrHandshake, ErrCrypt };
|
---|
402 |
|
---|
403 | TLS(QObject *parent=0);
|
---|
404 | ~TLS();
|
---|
405 |
|
---|
406 | void setCertificate(const Cert &cert, const RSAKey &key);
|
---|
407 | void setCertificateStore(const QPtrList<Cert> &store); // note: store must persist
|
---|
408 |
|
---|
409 | void reset();
|
---|
410 | bool startClient(const QString &host="");
|
---|
411 | bool startServer();
|
---|
412 | void close();
|
---|
413 | bool isHandshaken() const;
|
---|
414 |
|
---|
415 | // plain (application side)
|
---|
416 | void write(const QByteArray &a);
|
---|
417 | QByteArray read();
|
---|
418 |
|
---|
419 | // encoded (socket side)
|
---|
420 | void writeIncoming(const QByteArray &a);
|
---|
421 | QByteArray readOutgoing();
|
---|
422 | QByteArray readUnprocessed();
|
---|
423 |
|
---|
424 | // cert related
|
---|
425 | const Cert & peerCertificate() const;
|
---|
426 | int certificateValidityResult() const;
|
---|
427 |
|
---|
428 | signals:
|
---|
429 | void handshaken();
|
---|
430 | void readyRead();
|
---|
431 | void readyReadOutgoing(int plainBytes);
|
---|
432 | void closed();
|
---|
433 | void error(int);
|
---|
434 |
|
---|
435 | private slots:
|
---|
436 | void update();
|
---|
437 |
|
---|
438 | private:
|
---|
439 | class Private;
|
---|
440 | Private *d;
|
---|
441 | };
|
---|
442 |
|
---|
443 | class QCA_EXPORT SASL : public QObject
|
---|
444 | {
|
---|
445 | Q_OBJECT
|
---|
446 | public:
|
---|
447 | enum Error { ErrAuth, ErrCrypt };
|
---|
448 | enum ErrorCond {
|
---|
449 | NoMech,
|
---|
450 | BadProto,
|
---|
451 | BadServ,
|
---|
452 | BadAuth,
|
---|
453 | NoAuthzid,
|
---|
454 | TooWeak,
|
---|
455 | NeedEncrypt,
|
---|
456 | Expired,
|
---|
457 | Disabled,
|
---|
458 | NoUser,
|
---|
459 | RemoteUnavail
|
---|
460 | };
|
---|
461 | SASL(QObject *parent=0);
|
---|
462 | ~SASL();
|
---|
463 |
|
---|
464 | static void setAppName(const QString &name);
|
---|
465 |
|
---|
466 | void reset();
|
---|
467 | int errorCondition() const;
|
---|
468 |
|
---|
469 | // options
|
---|
470 | void setAllowPlain(bool);
|
---|
471 | void setAllowAnonymous(bool);
|
---|
472 | void setAllowActiveVulnerable(bool);
|
---|
473 | void setAllowDictionaryVulnerable(bool);
|
---|
474 | void setRequireForwardSecrecy(bool);
|
---|
475 | void setRequirePassCredentials(bool);
|
---|
476 | void setRequireMutualAuth(bool);
|
---|
477 |
|
---|
478 | void setMinimumSSF(int);
|
---|
479 | void setMaximumSSF(int);
|
---|
480 | void setExternalAuthID(const QString &authid);
|
---|
481 | void setExternalSSF(int);
|
---|
482 |
|
---|
483 | void setLocalAddr(const QHostAddress &addr, Q_UINT16 port);
|
---|
484 | void setRemoteAddr(const QHostAddress &addr, Q_UINT16 port);
|
---|
485 |
|
---|
486 | // initialize
|
---|
487 | bool startClient(const QString &service, const QString &host, const QStringList &mechlist, bool allowClientSendFirst=true);
|
---|
488 | bool startServer(const QString &service, const QString &host, const QString &realm, QStringList *mechlist);
|
---|
489 |
|
---|
490 | // authentication
|
---|
491 | void putStep(const QByteArray &stepData);
|
---|
492 | void putServerFirstStep(const QString &mech);
|
---|
493 | void putServerFirstStep(const QString &mech, const QByteArray &clientInit);
|
---|
494 | void setUsername(const QString &user);
|
---|
495 | void setAuthzid(const QString &auth);
|
---|
496 | void setPassword(const QString &pass);
|
---|
497 | void setRealm(const QString &realm);
|
---|
498 | void continueAfterParams();
|
---|
499 | void continueAfterAuthCheck();
|
---|
500 |
|
---|
501 | // security layer
|
---|
502 | int ssf() const;
|
---|
503 | void write(const QByteArray &a);
|
---|
504 | QByteArray read();
|
---|
505 | void writeIncoming(const QByteArray &a);
|
---|
506 | QByteArray readOutgoing();
|
---|
507 |
|
---|
508 | signals:
|
---|
509 | // for authentication
|
---|
510 | void clientFirstStep(const QString &mech, const QByteArray *clientInit);
|
---|
511 | void nextStep(const QByteArray &stepData);
|
---|
512 | void needParams(bool user, bool authzid, bool pass, bool realm);
|
---|
513 | void authCheck(const QString &user, const QString &authzid);
|
---|
514 | void authenticated();
|
---|
515 |
|
---|
516 | // for security layer
|
---|
517 | void readyRead();
|
---|
518 | void readyReadOutgoing(int plainBytes);
|
---|
519 |
|
---|
520 | // error
|
---|
521 | void error(int);
|
---|
522 |
|
---|
523 | private slots:
|
---|
524 | void tryAgain();
|
---|
525 |
|
---|
526 | private:
|
---|
527 | class Private;
|
---|
528 | Private *d;
|
---|
529 |
|
---|
530 | void handleServerFirstStep(int r);
|
---|
531 | };
|
---|
532 | };
|
---|
533 |
|
---|
534 | #endif
|
---|