~ubuntu-branches/ubuntu/edgy/psi/edgy

« back to all changes in this revision

Viewing changes to qca/qca.h

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2005-09-14 16:33:49 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050914163349-3zacov4afysz5cw5
Tags: 0.9.3-2ubuntu1
* Sync with debian
* Applied patch to psi.desktop to start psi without gpg-agent use (known
  issue)
* Updated README.Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifdef 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
 
#endif
51
 
#ifndef QCA_PLUGIN_EXPORT
52
 
#define QCA_PLUGIN_EXPORT extern "C"
53
 
#endif
54
 
 
55
 
class QHostAddress;
56
 
class QStringList;
57
 
 
58
 
class QCAProvider;
59
 
class QCA_HashContext;
60
 
class QCA_CipherContext;
61
 
class QCA_CertContext;
62
 
 
63
 
namespace QCA
64
 
{
65
 
        enum {
66
 
                CAP_SHA1      = 0x0001,
67
 
                CAP_SHA256    = 0x0002,
68
 
                CAP_MD5       = 0x0004,
69
 
                CAP_BlowFish  = 0x0008,
70
 
                CAP_TripleDES = 0x0010,
71
 
                CAP_AES128    = 0x0020,
72
 
                CAP_AES256    = 0x0040,
73
 
                CAP_RSA       = 0x0080,
74
 
                CAP_X509      = 0x0100,
75
 
                CAP_TLS       = 0x0200,
76
 
                CAP_SASL      = 0x0400
77
 
        };
78
 
 
79
 
        enum {
80
 
                CBC = 0x0001,
81
 
                CFB = 0x0002
82
 
        };
83
 
 
84
 
        enum {
85
 
                Encrypt = 0x0001,
86
 
                Decrypt = 0x0002
87
 
        };
88
 
 
89
 
        QCA_EXPORT void init();
90
 
        QCA_EXPORT bool isSupported(int capabilities);
91
 
        QCA_EXPORT void insertProvider(QCAProvider *);
92
 
        QCA_EXPORT void unloadAllPlugins();
93
 
 
94
 
        QCA_EXPORT QString arrayToHex(const QByteArray &);
95
 
        QCA_EXPORT QByteArray hexToArray(const QString &);
96
 
 
97
 
        class QCA_EXPORT Hash
98
 
        {
99
 
        public:
100
 
                Hash(const Hash &);
101
 
                Hash & operator=(const Hash &);
102
 
                ~Hash();
103
 
 
104
 
                void clear();
105
 
                void update(const QByteArray &a);
106
 
                QByteArray final();
107
 
 
108
 
        protected:
109
 
                Hash(QCA_HashContext *);
110
 
 
111
 
        private:
112
 
                class Private;
113
 
                Private *d;
114
 
        };
115
 
 
116
 
        template <class T>
117
 
        class QCA_EXPORT HashStatic
118
 
        {
119
 
        public:
120
 
                HashStatic<T>() {}
121
 
 
122
 
                static QByteArray hash(const QByteArray &a)
123
 
                {
124
 
                        T obj;
125
 
                        obj.update(a);
126
 
                        return obj.final();
127
 
                }
128
 
 
129
 
                static QByteArray hash(const QCString &cs)
130
 
                {
131
 
                        QByteArray a(cs.length());
132
 
                        memcpy(a.data(), cs.data(), a.size());
133
 
                        return hash(a);
134
 
                }
135
 
 
136
 
                static QString hashToString(const QByteArray &a)
137
 
                {
138
 
                        return arrayToHex(hash(a));
139
 
                }
140
 
 
141
 
                static QString hashToString(const QCString &cs)
142
 
                {
143
 
                        return arrayToHex(hash(cs));
144
 
                }
145
 
        };
146
 
 
147
 
        class QCA_EXPORT Cipher
148
 
        {
149
 
        public:
150
 
                Cipher(const Cipher &);
151
 
                Cipher & operator=(const Cipher &);
152
 
                ~Cipher();
153
 
 
154
 
                QByteArray dyn_generateKey(int size=-1) const;
155
 
                QByteArray dyn_generateIV() const;
156
 
                void reset(int dir, int mode, const QByteArray &key, const QByteArray &iv, bool pad=true);
157
 
                bool update(const QByteArray &a);
158
 
                QByteArray final(bool *ok=0);
159
 
 
160
 
        protected:
161
 
                Cipher(QCA_CipherContext *, int dir, int mode, const QByteArray &key, const QByteArray &iv, bool pad);
162
 
 
163
 
        private:
164
 
                class Private;
165
 
                Private *d;
166
 
        };
167
 
 
168
 
        template <class T>
169
 
        class QCA_EXPORT CipherStatic
170
 
        {
171
 
        public:
172
 
                CipherStatic<T>() {}
173
 
 
174
 
                static QByteArray generateKey(int size=-1)
175
 
                {
176
 
                        T obj;
177
 
                        return obj.dyn_generateKey(size);
178
 
                }
179
 
 
180
 
                static QByteArray generateIV()
181
 
                {
182
 
                        T obj;
183
 
                        return obj.dyn_generateIV();
184
 
                }
185
 
        };
186
 
 
187
 
        class QCA_EXPORT SHA1 : public Hash, public HashStatic<SHA1>
188
 
        {
189
 
        public:
190
 
                SHA1();
191
 
        };
192
 
 
193
 
        class QCA_EXPORT SHA256 : public Hash, public HashStatic<SHA256>
194
 
        {
195
 
        public:
196
 
                SHA256();
197
 
        };
198
 
 
199
 
        class QCA_EXPORT MD5 : public Hash, public HashStatic<MD5>
200
 
        {
201
 
        public:
202
 
                MD5();
203
 
        };
204
 
 
205
 
        class QCA_EXPORT BlowFish : public Cipher, public CipherStatic<BlowFish>
206
 
        {
207
 
        public:
208
 
                BlowFish(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
209
 
        };
210
 
 
211
 
        class QCA_EXPORT TripleDES : public Cipher, public CipherStatic<TripleDES>
212
 
        {
213
 
        public:
214
 
                TripleDES(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
215
 
        };
216
 
 
217
 
        class QCA_EXPORT AES128 : public Cipher, public CipherStatic<AES128>
218
 
        {
219
 
        public:
220
 
                AES128(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
221
 
        };
222
 
 
223
 
        class QCA_EXPORT AES256 : public Cipher, public CipherStatic<AES256>
224
 
        {
225
 
        public:
226
 
                AES256(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
227
 
        };
228
 
 
229
 
        class RSA;
230
 
        class QCA_EXPORT RSAKey
231
 
        {
232
 
        public:
233
 
                RSAKey();
234
 
                RSAKey(const RSAKey &from);
235
 
                RSAKey & operator=(const RSAKey &from);
236
 
                ~RSAKey();
237
 
 
238
 
                bool isNull() const;
239
 
                bool havePublic() const;
240
 
                bool havePrivate() const;
241
 
 
242
 
                QByteArray toDER(bool publicOnly=false) const;
243
 
                bool fromDER(const QByteArray &a);
244
 
 
245
 
                QString toPEM(bool publicOnly=false) const;
246
 
                bool fromPEM(const QString &);
247
 
 
248
 
                // only call if you know what you are doing
249
 
                bool fromNative(void *);
250
 
 
251
 
        private:
252
 
                class Private;
253
 
                Private *d;
254
 
 
255
 
                friend class RSA;
256
 
                friend class TLS;
257
 
                bool encrypt(const QByteArray &a, QByteArray *out, bool oaep) const;
258
 
                bool decrypt(const QByteArray &a, QByteArray *out, bool oaep) const;
259
 
                bool generate(unsigned int bits);
260
 
        };
261
 
 
262
 
        class QCA_EXPORT RSA
263
 
        {
264
 
        public:
265
 
                RSA();
266
 
                ~RSA();
267
 
 
268
 
                RSAKey key() const;
269
 
                void setKey(const RSAKey &);
270
 
 
271
 
                bool encrypt(const QByteArray &a, QByteArray *out, bool oaep=false) const;
272
 
                bool decrypt(const QByteArray &a, QByteArray *out, bool oaep=false) const;
273
 
 
274
 
                static RSAKey generateKey(unsigned int bits);
275
 
 
276
 
        private:
277
 
                RSAKey v_key;
278
 
        };
279
 
 
280
 
        typedef QMap<QString, QString> CertProperties;
281
 
        class QCA_EXPORT Cert
282
 
        {
283
 
        public:
284
 
                Cert();
285
 
                Cert(const Cert &);
286
 
                Cert & operator=(const Cert &);
287
 
                ~Cert();
288
 
 
289
 
                bool isNull() const;
290
 
 
291
 
                QString commonName() const;
292
 
                QString serialNumber() const;
293
 
                QString subjectString() const;
294
 
                QString issuerString() const;
295
 
                CertProperties subject() const;
296
 
                CertProperties issuer() const;
297
 
                QDateTime notBefore() const;
298
 
                QDateTime notAfter() const;
299
 
 
300
 
                QByteArray toDER() const;
301
 
                bool fromDER(const QByteArray &a);
302
 
 
303
 
                QString toPEM() const;
304
 
                bool fromPEM(const QString &);
305
 
 
306
 
        private:
307
 
                class Private;
308
 
                Private *d;
309
 
 
310
 
                friend class TLS;
311
 
                void fromContext(QCA_CertContext *);
312
 
        };
313
 
 
314
 
        class QCA_EXPORT TLS : public QObject
315
 
        {
316
 
                Q_OBJECT
317
 
        public:
318
 
                enum Validity {
319
 
                        NoCert,
320
 
                        Valid,
321
 
                        HostMismatch,
322
 
                        Rejected,
323
 
                        Untrusted,
324
 
                        SignatureFailed,
325
 
                        InvalidCA,
326
 
                        InvalidPurpose,
327
 
                        SelfSigned,
328
 
                        Revoked,
329
 
                        PathLengthExceeded,
330
 
                        Expired,
331
 
                        Unknown
332
 
                };
333
 
                enum Error { ErrHandshake, ErrCrypt };
334
 
 
335
 
                TLS(QObject *parent=0);
336
 
                ~TLS();
337
 
 
338
 
                void setCertificate(const Cert &cert, const RSAKey &key);
339
 
                void setCertificateStore(const QPtrList<Cert> &store);  // note: store must persist
340
 
 
341
 
                void reset();
342
 
                bool startClient(const QString &host="");
343
 
                bool startServer();
344
 
                void close();
345
 
                bool isHandshaken() const;
346
 
 
347
 
                // plain (application side)
348
 
                void write(const QByteArray &a);
349
 
                QByteArray read();
350
 
 
351
 
                // encoded (socket side)
352
 
                void writeIncoming(const QByteArray &a);
353
 
                QByteArray readOutgoing();
354
 
                QByteArray readUnprocessed();
355
 
 
356
 
                // cert related
357
 
                const Cert & peerCertificate() const;
358
 
                int certificateValidityResult() const;
359
 
 
360
 
        signals:
361
 
                void handshaken();
362
 
                void readyRead();
363
 
                void readyReadOutgoing(int plainBytes);
364
 
                void closed();
365
 
                void error(int);
366
 
 
367
 
        private slots:
368
 
                void update();
369
 
 
370
 
        private:
371
 
                class Private;
372
 
                Private *d;
373
 
        };
374
 
 
375
 
        class QCA_EXPORT SASL : public QObject
376
 
        {
377
 
                Q_OBJECT
378
 
        public:
379
 
                enum Error { ErrAuth, ErrCrypt };
380
 
                enum ErrorCond {
381
 
                        NoMech,
382
 
                        BadProto,
383
 
                        BadServ,
384
 
                        BadAuth,
385
 
                        NoAuthzid,
386
 
                        TooWeak,
387
 
                        NeedEncrypt,
388
 
                        Expired,
389
 
                        Disabled,
390
 
                        NoUser,
391
 
                        RemoteUnavail
392
 
                };
393
 
                SASL(QObject *parent=0);
394
 
                ~SASL();
395
 
 
396
 
                static void setAppName(const QString &name);
397
 
 
398
 
                void reset();
399
 
                int errorCondition() const;
400
 
 
401
 
                // options
402
 
                void setAllowPlain(bool);
403
 
                void setAllowAnonymous(bool);
404
 
                void setAllowActiveVulnerable(bool);
405
 
                void setAllowDictionaryVulnerable(bool);
406
 
                void setRequireForwardSecrecy(bool);
407
 
                void setRequirePassCredentials(bool);
408
 
                void setRequireMutualAuth(bool);
409
 
 
410
 
                void setMinimumSSF(int);
411
 
                void setMaximumSSF(int);
412
 
                void setExternalAuthID(const QString &authid);
413
 
                void setExternalSSF(int);
414
 
 
415
 
                void setLocalAddr(const QHostAddress &addr, Q_UINT16 port);
416
 
                void setRemoteAddr(const QHostAddress &addr, Q_UINT16 port);
417
 
 
418
 
                // initialize
419
 
                bool startClient(const QString &service, const QString &host, const QStringList &mechlist, bool allowClientSendFirst=true);
420
 
                bool startServer(const QString &service, const QString &host, const QString &realm, QStringList *mechlist);
421
 
 
422
 
                // authentication
423
 
                void putStep(const QByteArray &stepData);
424
 
                void putServerFirstStep(const QString &mech);
425
 
                void putServerFirstStep(const QString &mech, const QByteArray &clientInit);
426
 
                void setUsername(const QString &user);
427
 
                void setAuthzid(const QString &auth);
428
 
                void setPassword(const QString &pass);
429
 
                void setRealm(const QString &realm);
430
 
                void continueAfterParams();
431
 
                void continueAfterAuthCheck();
432
 
 
433
 
                // security layer
434
 
                int ssf() const;
435
 
                void write(const QByteArray &a);
436
 
                QByteArray read();
437
 
                void writeIncoming(const QByteArray &a);
438
 
                QByteArray readOutgoing();
439
 
 
440
 
        signals:
441
 
                // for authentication
442
 
                void clientFirstStep(const QString &mech, const QByteArray *clientInit);
443
 
                void nextStep(const QByteArray &stepData);
444
 
                void needParams(bool user, bool authzid, bool pass, bool realm);
445
 
                void authCheck(const QString &user, const QString &authzid);
446
 
                void authenticated();
447
 
 
448
 
                // for security layer
449
 
                void readyRead();
450
 
                void readyReadOutgoing(int plainBytes);
451
 
 
452
 
                // error
453
 
                void error(int);
454
 
 
455
 
        private slots:
456
 
                void tryAgain();
457
 
 
458
 
        private:
459
 
                class Private;
460
 
                Private *d;
461
 
 
462
 
                void handleServerFirstStep(int r);
463
 
        };
464
 
};
465
 
 
466
 
#endif