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
00322 QCA_EXPORT Provider *findProvider(const QString &name);
00323
00327 QCA_EXPORT Provider *defaultProvider();
00328
00332 QCA_EXPORT void scanForPlugins();
00333
00337 QCA_EXPORT void unloadAllPlugins();
00338
00342 QCA_EXPORT QString pluginDiagnosticText();
00343
00347 QCA_EXPORT void clearPluginDiagnosticText();
00348
00356 QCA_EXPORT void appendPluginDiagnosticText(const QString &text);
00357
00366 QCA_EXPORT void setProperty(const QString &name, const QVariant &value);
00367
00375 QCA_EXPORT QVariant getProperty(const QString &name);
00376
00385 QCA_EXPORT void setProviderConfig(const QString &name, const QVariantMap &config);
00386
00392 QCA_EXPORT QVariantMap getProviderConfig(const QString &name);
00393
00399 QCA_EXPORT void saveProviderConfig(const QString &name);
00400
00404 QCA_EXPORT QString globalRandomProvider();
00405
00416 QCA_EXPORT void setGlobalRandomProvider(const QString &provider);
00417
00424 QCA_EXPORT Logger *logger();
00425
00436 #define QCA_logTextMessage(message, severity) \
00437 do { \
00438 register QCA::Logger::Severity s = severity; \
00439 register QCA::Logger *l = QCA::logger (); \
00440 if (s <= l->level ()) { \
00441 l->logTextMessage (message, s); \
00442 } \
00443 } while (false)
00444
00455 #define QCA_logBinaryMessage(blob, severity) \
00456 do { \
00457 register QCA::Logger::Severity s = severity; \
00458 register QCA::Logger *l = QCA::logger (); \
00459 if (s <= l->level ()) { \
00460 l->logBinaryMessage (blob, s); \
00461 } \
00462 } while (false)
00463
00472 QCA_EXPORT bool haveSystemStore();
00473
00494 QCA_EXPORT CertificateCollection systemStore();
00495
00503 QCA_EXPORT QString appName();
00504
00514 QCA_EXPORT void setAppName(const QString &name);
00515
00536 QCA_EXPORT QString arrayToHex(const QByteArray &array);
00537
00563 QCA_EXPORT QByteArray hexToArray(const QString &hexString);
00564
00576 class QCA_EXPORT Initializer
00577 {
00578 public:
00586 explicit Initializer(MemoryMode m = Practical, int prealloc = 64);
00587 ~Initializer();
00588 };
00589
00614 class QCA_EXPORT KeyLength
00615 {
00616 public:
00625 KeyLength(int min, int max, int multiple)
00626 : _min( min ), _max(max), _multiple( multiple )
00627 { }
00628
00632 int minimum() const { return _min; }
00633
00637 int maximum() const { return _max; }
00638
00645 int multiple() const { return _multiple; }
00646
00647 private:
00648 const int _min, _max, _multiple;
00649 };
00650
00666 class QCA_EXPORT Provider
00667 {
00668 public:
00669 virtual ~Provider();
00670
00671 class Context;
00672
00682 virtual void init();
00683
00693 virtual void deinit();
00694
00703 virtual int version() const;
00704
00716 virtual int qcaVersion() const = 0;
00717
00735 virtual QString name() const = 0;
00736
00752 virtual QStringList features() const = 0;
00753
00764 virtual QString credit() const;
00765
00792 virtual Context *createContext(const QString &type) = 0;
00793
00818 virtual QVariantMap defaultConfig() const;
00819
00829 virtual void configChanged(const QVariantMap &config);
00830 };
00831
00841 class QCA_EXPORT Provider::Context : public QObject
00842 {
00843 Q_OBJECT
00844 public:
00845 virtual ~Context();
00846
00850 Provider *provider() const;
00851
00855 QString type() const;
00856
00860 virtual Context *clone() const = 0;
00861
00870 bool sameProvider(const Context *c) const;
00871
00872 protected:
00880 Context(Provider *parent, const QString &type);
00881
00887 Context(const Context &from);
00888
00889 private:
00890
00891 Context & operator=(const Context &from);
00892
00893 Provider *_provider;
00894 QString _type;
00895 };
00896
00911 class QCA_EXPORT BasicContext : public Provider::Context
00912 {
00913 Q_OBJECT
00914 public:
00915 ~BasicContext();
00916
00917 protected:
00925 BasicContext(Provider *parent, const QString &type);
00926
00932 BasicContext(const BasicContext &from);
00933
00934 private:
00935
00936 BasicContext & operator=(const BasicContext &from);
00937 };
00938
00953 class QCA_EXPORT BufferedComputation
00954 {
00955 public:
00956 virtual ~BufferedComputation();
00957
00961 virtual void clear() = 0;
00962
00969 virtual void update(const MemoryRegion &a) = 0;
00970
00974 virtual MemoryRegion final() = 0;
00975
00988 MemoryRegion process(const MemoryRegion &a);
00989 };
00990
01009 class QCA_EXPORT Filter
01010 {
01011 public:
01012 virtual ~Filter();
01013
01017 virtual void clear() = 0;
01018
01025 virtual MemoryRegion update(const MemoryRegion &a) = 0;
01026
01031 virtual MemoryRegion final() = 0;
01032
01038 virtual bool ok() const = 0;
01039
01052 MemoryRegion process(const MemoryRegion &a);
01053 };
01054
01065 class QCA_EXPORT Algorithm
01066 {
01067 public:
01073 Algorithm(const Algorithm &from);
01074
01075 virtual ~Algorithm();
01076
01082 Algorithm & operator=(const Algorithm &from);
01083
01087 QString type() const;
01088
01095 Provider *provider() const;
01096
01097
01098
01104 Provider::Context *context();
01105
01111 const Provider::Context *context() const;
01112
01120 void change(Provider::Context *c);
01121
01130 void change(const QString &type, const QString &provider);
01131
01137 Provider::Context *takeContext();
01138
01139 protected:
01143 Algorithm();
01144
01151 Algorithm(const QString &type, const QString &provider);
01152
01153 private:
01154 class Private;
01155 QSharedDataPointer<Private> d;
01156 };
01157
01165 class QCA_EXPORT SymmetricKey : public SecureArray
01166 {
01167 public:
01171 SymmetricKey();
01172
01180 SymmetricKey(int size);
01181
01187 SymmetricKey(const SecureArray &a);
01188
01194 SymmetricKey(const QByteArray &a);
01195
01201 bool isWeakDESKey();
01202 };
01203
01211 class QCA_EXPORT InitializationVector : public SecureArray
01212 {
01213 public:
01217 InitializationVector();
01218
01224 InitializationVector(int size);
01225
01231 InitializationVector(const SecureArray &a);
01232
01238 InitializationVector(const QByteArray &a);
01239 };
01240
01255 class QCA_EXPORT Event
01256 {
01257 public:
01263 enum Type
01264 {
01265 Password,
01266 Token
01267 };
01268
01281 enum Source
01282 {
01283 KeyStore,
01284 Data
01285 };
01286
01295 enum PasswordStyle
01296 {
01297 StylePassword,
01298 StylePassphrase,
01299 StylePIN
01300 };
01301
01305 Event();
01306
01312 Event(const Event &from);
01313
01317 ~Event();
01318
01324 Event & operator=(const Event &from);
01325
01329 bool isNull() const;
01330
01334 Type type() const;
01335
01339 Source source() const;
01340
01348 PasswordStyle passwordStyle() const;
01349
01355 KeyStoreInfo keyStoreInfo() const;
01356
01362 KeyStoreEntry keyStoreEntry() const;
01363
01370 QString fileName() const;
01371
01375 void *ptr() const;
01376
01390 void setPasswordKeyStore(PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01391
01403 void setPasswordData(PasswordStyle pstyle, const QString &fileName, void *ptr);
01404
01416 void setToken(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01417
01418 private:
01419 class Private;
01420 QSharedDataPointer<Private> d;
01421 };
01422
01440 class QCA_EXPORT EventHandler : public QObject
01441 {
01442 Q_OBJECT
01443 public:
01449 EventHandler(QObject *parent = 0);
01450 ~EventHandler();
01451
01457 void start();
01458
01469 void submitPassword(int id, const SecureArray &password);
01470
01480 void tokenOkay(int id);
01481
01491 void reject(int id);
01492
01493 Q_SIGNALS:
01503 void eventReady(int id, const QCA::Event &context);
01504
01505 private:
01506 Q_DISABLE_COPY(EventHandler)
01507
01508 class Private;
01509 friend class Private;
01510 Private *d;
01511 };
01512
01522 class QCA_EXPORT PasswordAsker : public QObject
01523 {
01524 Q_OBJECT
01525 public:
01531 PasswordAsker(QObject *parent = 0);
01532 ~PasswordAsker();
01533
01545 void ask(Event::PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01546
01556 void ask(Event::PasswordStyle pstyle, const QString &fileName, void *ptr);
01557
01561 void cancel();
01562
01570 void waitForResponse();
01571
01580 bool accepted() const;
01581
01586 SecureArray password() const;
01587
01588 Q_SIGNALS:
01595 void responseReady();
01596
01597 private:
01598 Q_DISABLE_COPY(PasswordAsker)
01599
01600 class Private;
01601 friend class Private;
01602 Private *d;
01603 };
01604
01614 class QCA_EXPORT TokenAsker : public QObject
01615 {
01616 Q_OBJECT
01617 public:
01623 TokenAsker(QObject *parent = 0);
01624 ~TokenAsker();
01625
01635 void ask(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01636
01640 void cancel();
01641
01648 void waitForResponse();
01649
01655 bool accepted() const;
01656
01657 Q_SIGNALS:
01664 void responseReady();
01665
01666 private:
01667 Q_DISABLE_COPY(TokenAsker)
01668
01669 class Private;
01670 friend class Private;
01671 Private *d;
01672 };
01673
01674 }
01675
01676 #endif