2
* qca.h - Qt Cryptographic Architecture
3
* Copyright (C) 2003 Justin Karneges
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.
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.
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
34
# define QCA_EXPORT __declspec(dllexport)
36
# define QCA_EXPORT __declspec(dllimport)
45
# ifdef QCA_PLUGIN_DLL
46
# define QCA_PLUGIN_EXPORT extern "C" __declspec(dllexport)
48
# define QCA_PLUGIN_EXPORT extern "C" __declspec(dllimport)
51
#ifndef QCA_PLUGIN_EXPORT
52
#define QCA_PLUGIN_EXPORT extern "C"
59
class QCA_HashContext;
60
class QCA_CipherContext;
61
class QCA_CertContext;
69
CAP_BlowFish = 0x0008,
70
CAP_TripleDES = 0x0010,
89
QCA_EXPORT void init();
90
QCA_EXPORT bool isSupported(int capabilities);
91
QCA_EXPORT void insertProvider(QCAProvider *);
92
QCA_EXPORT void unloadAllPlugins();
94
QCA_EXPORT QString arrayToHex(const QByteArray &);
95
QCA_EXPORT QByteArray hexToArray(const QString &);
101
Hash & operator=(const Hash &);
105
void update(const QByteArray &a);
109
Hash(QCA_HashContext *);
117
class QCA_EXPORT HashStatic
122
static QByteArray hash(const QByteArray &a)
129
static QByteArray hash(const QCString &cs)
131
QByteArray a(cs.length());
132
memcpy(a.data(), cs.data(), a.size());
136
static QString hashToString(const QByteArray &a)
138
return arrayToHex(hash(a));
141
static QString hashToString(const QCString &cs)
143
return arrayToHex(hash(cs));
147
class QCA_EXPORT Cipher
150
Cipher(const Cipher &);
151
Cipher & operator=(const Cipher &);
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);
161
Cipher(QCA_CipherContext *, int dir, int mode, const QByteArray &key, const QByteArray &iv, bool pad);
169
class QCA_EXPORT CipherStatic
174
static QByteArray generateKey(int size=-1)
177
return obj.dyn_generateKey(size);
180
static QByteArray generateIV()
183
return obj.dyn_generateIV();
187
class QCA_EXPORT SHA1 : public Hash, public HashStatic<SHA1>
193
class QCA_EXPORT SHA256 : public Hash, public HashStatic<SHA256>
199
class QCA_EXPORT MD5 : public Hash, public HashStatic<MD5>
205
class QCA_EXPORT BlowFish : public Cipher, public CipherStatic<BlowFish>
208
BlowFish(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
211
class QCA_EXPORT TripleDES : public Cipher, public CipherStatic<TripleDES>
214
TripleDES(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
217
class QCA_EXPORT AES128 : public Cipher, public CipherStatic<AES128>
220
AES128(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
223
class QCA_EXPORT AES256 : public Cipher, public CipherStatic<AES256>
226
AES256(int dir=Encrypt, int mode=CBC, const QByteArray &key=QByteArray(), const QByteArray &iv=QByteArray(), bool pad=true);
230
class QCA_EXPORT RSAKey
234
RSAKey(const RSAKey &from);
235
RSAKey & operator=(const RSAKey &from);
239
bool havePublic() const;
240
bool havePrivate() const;
242
QByteArray toDER(bool publicOnly=false) const;
243
bool fromDER(const QByteArray &a);
245
QString toPEM(bool publicOnly=false) const;
246
bool fromPEM(const QString &);
248
// only call if you know what you are doing
249
bool fromNative(void *);
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);
269
void setKey(const RSAKey &);
271
bool encrypt(const QByteArray &a, QByteArray *out, bool oaep=false) const;
272
bool decrypt(const QByteArray &a, QByteArray *out, bool oaep=false) const;
274
static RSAKey generateKey(unsigned int bits);
280
typedef QMap<QString, QString> CertProperties;
281
class QCA_EXPORT Cert
286
Cert & operator=(const Cert &);
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;
300
QByteArray toDER() const;
301
bool fromDER(const QByteArray &a);
303
QString toPEM() const;
304
bool fromPEM(const QString &);
311
void fromContext(QCA_CertContext *);
314
class QCA_EXPORT TLS : public QObject
333
enum Error { ErrHandshake, ErrCrypt };
335
TLS(QObject *parent=0);
338
void setCertificate(const Cert &cert, const RSAKey &key);
339
void setCertificateStore(const QPtrList<Cert> &store); // note: store must persist
342
bool startClient(const QString &host="");
345
bool isHandshaken() const;
347
// plain (application side)
348
void write(const QByteArray &a);
351
// encoded (socket side)
352
void writeIncoming(const QByteArray &a);
353
QByteArray readOutgoing();
354
QByteArray readUnprocessed();
357
const Cert & peerCertificate() const;
358
int certificateValidityResult() const;
363
void readyReadOutgoing(int plainBytes);
375
class QCA_EXPORT SASL : public QObject
379
enum Error { ErrAuth, ErrCrypt };
393
SASL(QObject *parent=0);
396
static void setAppName(const QString &name);
399
int errorCondition() const;
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);
410
void setMinimumSSF(int);
411
void setMaximumSSF(int);
412
void setExternalAuthID(const QString &authid);
413
void setExternalSSF(int);
415
void setLocalAddr(const QHostAddress &addr, Q_UINT16 port);
416
void setRemoteAddr(const QHostAddress &addr, Q_UINT16 port);
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);
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();
435
void write(const QByteArray &a);
437
void writeIncoming(const QByteArray &a);
438
QByteArray readOutgoing();
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();
448
// for security layer
450
void readyReadOutgoing(int plainBytes);
462
void handleServerFirstStep(int r);