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
00553 class QCA_EXPORT Initializer
00554 {
00555 public:
00563 explicit Initializer(MemoryMode m = Practical, int prealloc = 64);
00564 ~Initializer();
00565 };
00566
00591 class QCA_EXPORT KeyLength
00592 {
00593 public:
00602 KeyLength(int min, int max, int multiple)
00603 : _min( min ), _max(max), _multiple( multiple )
00604 { }
00605
00609 int minimum() const { return _min; }
00610
00614 int maximum() const { return _max; }
00615
00622 int multiple() const { return _multiple; }
00623
00624 private:
00625 const int _min, _max, _multiple;
00626 };
00627
00643 class QCA_EXPORT Provider
00644 {
00645 public:
00646 virtual ~Provider();
00647
00648 class Context;
00649
00659 virtual void init();
00660
00670 virtual void deinit();
00671
00680 virtual int version() const;
00681
00693 virtual int qcaVersion() const = 0;
00694
00712 virtual QString name() const = 0;
00713
00729 virtual QStringList features() const = 0;
00730
00741 virtual QString credit() const;
00742
00769 virtual Context *createContext(const QString &type) = 0;
00770
00795 virtual QVariantMap defaultConfig() const;
00796
00804 virtual void configChanged(const QVariantMap &config);
00805 };
00806
00816 class QCA_EXPORT Provider::Context : public QObject
00817 {
00818 Q_OBJECT
00819 public:
00820 virtual ~Context();
00821
00825 Provider *provider() const;
00826
00830 QString type() const;
00831
00835 virtual Context *clone() const = 0;
00836
00845 bool sameProvider(const Context *c) const;
00846
00847 protected:
00855 Context(Provider *parent, const QString &type);
00856
00862 Context(const Context &from);
00863
00864 private:
00865
00866 Context & operator=(const Context &from);
00867
00868 Provider *_provider;
00869 QString _type;
00870 };
00871
00886 class QCA_EXPORT BasicContext : public Provider::Context
00887 {
00888 Q_OBJECT
00889 public:
00890 ~BasicContext();
00891
00892 protected:
00900 BasicContext(Provider *parent, const QString &type);
00901
00907 BasicContext(const BasicContext &from);
00908
00909 private:
00910
00911 BasicContext & operator=(const BasicContext &from);
00912 };
00913
00928 class QCA_EXPORT BufferedComputation
00929 {
00930 public:
00931 virtual ~BufferedComputation();
00932
00936 virtual void clear() = 0;
00937
00944 virtual void update(const MemoryRegion &a) = 0;
00945
00949 virtual MemoryRegion final() = 0;
00950
00961 MemoryRegion process(const MemoryRegion &a);
00962 };
00963
00982 class QCA_EXPORT Filter
00983 {
00984 public:
00985 virtual ~Filter();
00986
00990 virtual void clear() = 0;
00991
00998 virtual MemoryRegion update(const MemoryRegion &a) = 0;
00999
01004 virtual MemoryRegion final() = 0;
01005
01011 virtual bool ok() const = 0;
01012
01023 MemoryRegion process(const MemoryRegion &a);
01024 };
01025
01036 class QCA_EXPORT Algorithm
01037 {
01038 public:
01044 Algorithm(const Algorithm &from);
01045
01046 virtual ~Algorithm();
01047
01053 Algorithm & operator=(const Algorithm &from);
01054
01058 QString type() const;
01059
01066 Provider *provider() const;
01067
01068
01069
01075 Provider::Context *context();
01076
01082 const Provider::Context *context() const;
01083
01091 void change(Provider::Context *c);
01092
01101 void change(const QString &type, const QString &provider);
01102
01108 Provider::Context *takeContext();
01109
01110 protected:
01114 Algorithm();
01115
01122 Algorithm(const QString &type, const QString &provider);
01123
01124 private:
01125 class Private;
01126 QSharedDataPointer<Private> d;
01127 };
01128
01136 class QCA_EXPORT SymmetricKey : public SecureArray
01137 {
01138 public:
01142 SymmetricKey();
01143
01151 SymmetricKey(int size);
01152
01158 SymmetricKey(const SecureArray &a);
01159
01165 SymmetricKey(const QByteArray &a);
01166
01172 bool isWeakDESKey();
01173 };
01174
01182 class QCA_EXPORT InitializationVector : public SecureArray
01183 {
01184 public:
01188 InitializationVector();
01189
01195 InitializationVector(int size);
01196
01202 InitializationVector(const SecureArray &a);
01203
01209 InitializationVector(const QByteArray &a);
01210 };
01211
01226 class QCA_EXPORT Event
01227 {
01228 public:
01234 enum Type
01235 {
01236 Password,
01237 Token
01238 };
01239
01252 enum Source
01253 {
01254 KeyStore,
01255 Data
01256 };
01257
01266 enum PasswordStyle
01267 {
01268 StylePassword,
01269 StylePassphrase,
01270 StylePIN
01271 };
01272
01276 Event();
01277
01283 Event(const Event &from);
01284
01288 ~Event();
01289
01295 Event & operator=(const Event &from);
01296
01300 bool isNull() const;
01301
01305 Type type() const;
01306
01310 Source source() const;
01311
01319 PasswordStyle passwordStyle() const;
01320
01326 KeyStoreInfo keyStoreInfo() const;
01327
01333 KeyStoreEntry keyStoreEntry() const;
01334
01341 QString fileName() const;
01342
01346 void *ptr() const;
01347
01361 void setPasswordKeyStore(PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01362
01374 void setPasswordData(PasswordStyle pstyle, const QString &fileName, void *ptr);
01375
01387 void setToken(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01388
01389 private:
01390 class Private;
01391 QSharedDataPointer<Private> d;
01392 };
01393
01411 class QCA_EXPORT EventHandler : public QObject
01412 {
01413 Q_OBJECT
01414 public:
01420 EventHandler(QObject *parent = 0);
01421 ~EventHandler();
01422
01428 void start();
01429
01440 void submitPassword(int id, const SecureArray &password);
01441
01451 void tokenOkay(int id);
01452
01462 void reject(int id);
01463
01464 Q_SIGNALS:
01471 void eventReady(int id, const QCA::Event &context);
01472
01473 private:
01474 Q_DISABLE_COPY(EventHandler)
01475
01476 class Private;
01477 friend class Private;
01478 Private *d;
01479 };
01480
01490 class QCA_EXPORT PasswordAsker : public QObject
01491 {
01492 Q_OBJECT
01493 public:
01499 PasswordAsker(QObject *parent = 0);
01500 ~PasswordAsker();
01501
01513 void ask(Event::PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01514
01524 void ask(Event::PasswordStyle pstyle, const QString &fileName, void *ptr);
01525
01529 void cancel();
01530
01538 void waitForResponse();
01539
01548 bool accepted() const;
01549
01554 SecureArray password() const;
01555
01556 Q_SIGNALS:
01563 void responseReady();
01564
01565 private:
01566 Q_DISABLE_COPY(PasswordAsker)
01567
01568 class Private;
01569 friend class Private;
01570 Private *d;
01571 };
01572
01582 class QCA_EXPORT TokenAsker : public QObject
01583 {
01584 Q_OBJECT
01585 public:
01591 TokenAsker(QObject *parent = 0);
01592 ~TokenAsker();
01593
01603 void ask(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
01604
01608 void cancel();
01609
01616 void waitForResponse();
01617
01623 bool accepted() const;
01624
01625 Q_SIGNALS:
01632 void responseReady();
01633
01634 private:
01635 Q_DISABLE_COPY(TokenAsker)
01636
01637 class Private;
01638 friend class Private;
01639 Private *d;
01640 };
01641
01642 }
01643
01644 #endif