00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00032 #ifndef QCA_CORE_H
00033 #define QCA_CORE_H
00034
00042 #define QCA_VERSION 0x016363
00043
00044 #include <QString>
00045 #include <QStringList>
00046 #include <QList>
00047 #include <QSharedData>
00048 #include <QSharedDataPointer>
00049 #include "qca_export.h"
00050 #include "qca_support.h"
00051 #include "qca_tools.h"
00052
00059 QCA_EXPORT int qcaVersion();
00060
00064 namespace QCA {
00065
00066 class Provider;
00067 class Random;
00068 class CertificateCollection;
00069 class Global;
00070 class KeyStore;
00071 class KeyStoreEntry;
00072 class KeyStoreInfo;
00073 class KeyStoreManager;
00074 class Logger;
00075
00085 typedef QList<Provider*> ProviderList;
00086
00101 enum MemoryMode
00102 {
00103 Practical,
00104 Locking,
00105 LockingKeepPrivileges
00106 };
00107
00114 enum Direction
00115 {
00116 Encode,
00117 Decode
00118 };
00119
00125 QCA_EXPORT void init();
00126
00134 QCA_EXPORT void init(MemoryMode m, int prealloc);
00135
00143 QCA_EXPORT void deinit();
00144
00150 QCA_EXPORT bool haveSecureMemory();
00151
00160 QCA_EXPORT bool haveSecureRandom();
00161
00193 QCA_EXPORT bool isSupported(const char *features, const QString &provider = QString());
00194
00203 QCA_EXPORT bool isSupported(const QStringList &features, const QString &provider = QString());
00204
00221 QCA_EXPORT QStringList supportedFeatures();
00222
00240 QCA_EXPORT QStringList defaultFeatures();
00241
00257 QCA_EXPORT bool insertProvider(Provider *p, int priority = 0);
00258
00289 QCA_EXPORT void setProviderPriority(const QString &name, int priority);
00290
00304 QCA_EXPORT int providerPriority(const QString &name);
00305
00315 QCA_EXPORT ProviderList providers();
00316
00320 QCA_EXPORT Provider *findProvider(const QString &name);
00321
00325 QCA_EXPORT Provider *defaultProvider();
00326
00330 QCA_EXPORT void scanForPlugins();
00331
00335 QCA_EXPORT void unloadAllPlugins();
00336
00340 QCA_EXPORT QString pluginDiagnosticText();
00341
00345 QCA_EXPORT void clearPluginDiagnosticText();
00346
00352 QCA_EXPORT void appendPluginDiagnosticText(const QString &text);
00353
00357 QCA_EXPORT void setProperty(const QString &name, const QVariant &value);
00358
00362 QCA_EXPORT QVariant getProperty(const QString &name);
00363
00369 QCA_EXPORT void setProviderConfig(const QString &name, const QVariantMap &config);
00370
00374 QCA_EXPORT QVariantMap getProviderConfig(const QString &name);
00375
00379 QCA_EXPORT void saveProviderConfig(const QString &name);
00380
00384 QCA_EXPORT QString globalRandomProvider();
00385
00393 QCA_EXPORT void setGlobalRandomProvider(const QString &provider);
00394
00401 QCA_EXPORT Logger *logger();
00402
00413 #define QCA_logTextMessage(message, severity) \
00414 do { \
00415 register QCA::Logger::Severity s = severity; \
00416 register QCA::Logger *l = QCA::logger (); \
00417 if (s <= l->level ()) { \
00418 l->logTextMessage (message, s); \
00419 } \
00420 } while (false)
00421
00432 #define QCA_logBinaryMessage(blob, severity) \
00433 do { \
00434 register QCA::Logger::Severity s = severity; \
00435 register QCA::Logger *l = QCA::logger (); \
00436 if (s <= l->level ()) { \
00437 l->logBinaryMessage (blob, s); \
00438 } \
00439 } while (false)
00440
00449 QCA_EXPORT bool haveSystemStore();
00450
00471 QCA_EXPORT CertificateCollection systemStore();
00472
00480 QCA_EXPORT QString appName();
00481
00491 QCA_EXPORT void setAppName(const QString &name);
00492
00513 QCA_EXPORT QString arrayToHex(const QByteArray &array);
00514
00540 QCA_EXPORT QByteArray hexToArray(const QString &hexString);
00541
00551 class QCA_EXPORT Initializer
00552 {
00553 public:
00561 explicit Initializer(MemoryMode m = Practical, int prealloc = 64);
00562 ~Initializer();
00563 };
00564
00587 class QCA_EXPORT KeyLength
00588 {
00589 public:
00598 KeyLength(int min, int max, int multiple)
00599 : _min( min ), _max(max), _multiple( multiple )
00600 { }
00601
00605 int minimum() const { return _min; }
00606
00610 int maximum() const { return _max; }
00611
00618 int multiple() const { return _multiple; }
00619
00620 private:
00621 const int _min, _max, _multiple;
00622 };
00623
00637 class QCA_EXPORT Provider
00638 {
00639 public:
00640 virtual ~Provider();
00641
00642 class Context;
00643
00653 virtual void init();
00654
00664 virtual void deinit();
00665
00674 virtual int version() const;
00675
00687 virtual int qcaVersion() const = 0;
00688
00706 virtual QString name() const = 0;
00707
00723 virtual QStringList features() const = 0;
00724
00735 virtual QString credit() const;
00736
00763 virtual Context *createContext(const QString &type) = 0;
00764
00789 virtual QVariantMap defaultConfig() const;
00790
00798 virtual void configChanged(const QVariantMap &config);
00799 };
00800
00808 class QCA_EXPORT Provider::Context : public QObject
00809 {
00810 Q_OBJECT
00811 public:
00812 virtual ~Context();
00813
00817 Provider *provider() const;
00818
00822 QString type() const;
00823
00827 virtual Context *clone() const = 0;
00828
00837 bool sameProvider(const Context *c) const;
00838
00839 protected:
00847 Context(Provider *parent, const QString &type);
00848
00854 Context(const Context &from);
00855
00856 private:
00857
00858 Context & operator=(const Context &from);
00859
00860 Provider *_provider;
00861 QString _type;
00862 };
00863
00876 class QCA_EXPORT BasicContext : public Provider::Context
00877 {
00878 Q_OBJECT
00879 public:
00880 ~BasicContext();
00881
00882 protected:
00890 BasicContext(Provider *parent, const QString &type);
00891
00897 BasicContext(const BasicContext &from);
00898
00899 private:
00900
00901 BasicContext & operator=(const BasicContext &from);
00902 };
00903
00916 class QCA_EXPORT BufferedComputation
00917 {
00918 public:
00919 virtual ~BufferedComputation();
00920
00924 virtual void clear() = 0;
00925
00932 virtual void update(const MemoryRegion &a) = 0;
00933
00937 virtual MemoryRegion final() = 0;
00938
00949 MemoryRegion process(const MemoryRegion &a);
00950 };
00951
00968 class QCA_EXPORT Filter
00969 {
00970 public:
00971 virtual ~Filter();
00972
00976 virtual void clear() = 0;
00977
00984 virtual MemoryRegion update(const MemoryRegion &a) = 0;
00985
00990 virtual MemoryRegion final() = 0;
00991
00997 virtual bool ok() const = 0;
00998
01009 MemoryRegion process(const MemoryRegion &a);
01010 };
01011
01020 class QCA_EXPORT Algorithm
01021 {
01022 public:
01028 Algorithm(const Algorithm &from);
01029
01030 virtual ~Algorithm();
01031
01037 Algorithm & operator=(const Algorithm &from);
01038
01042 QString type() const;
01043
01050 Provider *provider() const;
01051
01052
01053
01059 Provider::Context *context();
01060
01066 const Provider::Context *context() const;
01067
01075 void change(Provider::Context *c);
01076
01085 void change(const QString &type, const QString &provider);
01086
01092 Provider::Context *takeContext();
01093
01094 protected:
01098 Algorithm();
01099
01106 Algorithm(const QString &type, const QString &provider);
01107
01108 private:
01109 class Private;
01110 QSharedDataPointer<Private> d;
01111 };
01112
01118 class QCA_EXPORT SymmetricKey : public SecureArray
01119 {
01120 public:
01124 SymmetricKey();
01125
01133 SymmetricKey(int size);
01134
01140 SymmetricKey(const SecureArray &a);
01141
01147 SymmetricKey(const QByteArray &a);
01148
01154 bool isWeakDESKey();
01155 };
01156
01162 class QCA_EXPORT InitializationVector : public SecureArray
01163 {
01164 public:
01168 InitializationVector();
01169
01175 InitializationVector(int size);
01176
01182 InitializationVector(const SecureArray &a);
01183
01189 InitializationVector(const QByteArray &a);
01190 };
01191
01202 class QCA_EXPORT Event
01203 {
01204 public:
01210 enum Type
01211 {
01212 Password,
01213 Token
01214 };
01215
01228 enum Source
01229 {
01230 KeyStore,
01231 Data
01232 };
01233
01242 enum PasswordStyle
01243 {
01244 StylePassword,
01245 StylePassphrase,
01246 StylePIN
01247 };
01248
01252 Event();
01253
01259 Event(const Event &from);
01260
01264 ~Event();
01265
01271 Event & operator=(const Event &from);
01272
01276 bool isNull() const;
01277
01281 Type type() const;
01282
01286 Source source() const;
01287
01295 PasswordStyle passwordStyle() const;
01296
01302 KeyStoreInfo keyStoreInfo() const;
01303
01309 KeyStoreEntry keyStoreEntry() const;
01310
01317 QString fileName() const;
01318
01322 void *ptr() const;
01323
01337 void setPasswordKeyStore(PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01338
01350 void setPasswordData(PasswordStyle pstyle, const QString &fileName, void *ptr);
01351
01363 void setToken(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01364
01365 private:
01366 class Private;
01367 QSharedDataPointer<Private> d;
01368 };
01369
01383 class QCA_EXPORT EventHandler : public QObject
01384 {
01385 Q_OBJECT
01386 public:
01392 EventHandler(QObject *parent = 0);
01393 ~EventHandler();
01394
01400 void start();
01401
01412 void submitPassword(int id, const SecureArray &password);
01413
01423 void tokenOkay(int id);
01424
01434 void reject(int id);
01435
01436 Q_SIGNALS:
01443 void eventReady(int id, const QCA::Event &context);
01444
01445 private:
01446 Q_DISABLE_COPY(EventHandler)
01447
01448 class Private;
01449 friend class Private;
01450 Private *d;
01451 };
01452
01458 class QCA_EXPORT PasswordAsker : public QObject
01459 {
01460 Q_OBJECT
01461 public:
01467 PasswordAsker(QObject *parent = 0);
01468 ~PasswordAsker();
01469
01481 void ask(Event::PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01482
01492 void ask(Event::PasswordStyle pstyle, const QString &fileName, void *ptr);
01493
01497 void cancel();
01498
01506 void waitForResponse();
01507
01516 bool accepted() const;
01517
01522 SecureArray password() const;
01523
01524 Q_SIGNALS:
01531 void responseReady();
01532
01533 private:
01534 Q_DISABLE_COPY(PasswordAsker)
01535
01536 class Private;
01537 friend class Private;
01538 Private *d;
01539 };
01540
01546 class QCA_EXPORT TokenAsker : public QObject
01547 {
01548 Q_OBJECT
01549 public:
01555 TokenAsker(QObject *parent = 0);
01556 ~TokenAsker();
01557
01567 void ask(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01568
01572 void cancel();
01573
01580 void waitForResponse();
01581
01587 bool accepted() const;
01588
01589 Q_SIGNALS:
01596 void responseReady();
01597
01598 private:
01599 Q_DISABLE_COPY(TokenAsker)
01600
01601 class Private;
01602 friend class Private;
01603 Private *d;
01604 };
01605
01606 }
01607
01608 #endif