Qt Cryptographic Architecture
qca_core.h
Go to the documentation of this file.
1 /*
2  * qca_core.h - Qt Cryptographic Architecture
3  * Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
4  * Copyright (C) 2004,2005 Brad Hards <bradh@frogmouth.net>
5  * Copyright (C) 2014-2016 Ivan Romanov <drizt@land.ru>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
34 #ifndef QCA_CORE_H
35 #define QCA_CORE_H
36 
37 #include <QString>
38 #include <QStringList>
39 #include <QList>
40 #include <QSharedData>
41 #include <QSharedDataPointer>
42 #include "qca_export.h"
43 #include "qca_support.h"
44 #include "qca_tools.h"
45 #include "qca_version.h"
46 
53 QCA_EXPORT int qcaVersion();
54 
61 QCA_EXPORT const char *qcaVersionStr();
62 
69 QCA_EXPORT int qcaMajorVersion();
70 
77 QCA_EXPORT int qcaMinorVersion();
78 
85 QCA_EXPORT int qcaPatchVersion();
86 
90 namespace QCA {
91 
92 class Provider;
93 class Random;
94 class CertificateCollection;
95 class Global;
96 class KeyStore;
97 class KeyStoreEntry;
98 class KeyStoreInfo;
99 class KeyStoreManager;
100 class Logger;
101 
112 
128 {
132 };
133 
141 {
144 };
145 
151 QCA_EXPORT void init();
152 
160 QCA_EXPORT void init(MemoryMode m, int prealloc);
161 
169 QCA_EXPORT void deinit();
170 
176 QCA_EXPORT bool haveSecureMemory();
177 
186 QCA_EXPORT bool haveSecureRandom();
187 
219 QCA_EXPORT bool isSupported(const char *features, const QString &provider = QString());
220 
229 QCA_EXPORT bool isSupported(const QStringList &features, const QString &provider = QString());
230 
247 QCA_EXPORT QStringList supportedFeatures();
248 
266 QCA_EXPORT QStringList defaultFeatures();
267 
286 QCA_EXPORT bool insertProvider(Provider *p, int priority = 0);
287 
300 QCA_EXPORT bool unloadProvider(const QString &name);
301 
333 QCA_EXPORT void setProviderPriority(const QString &name, int priority);
334 
348 QCA_EXPORT int providerPriority(const QString &name);
349 
359 QCA_EXPORT ProviderList providers();
360 
366 QCA_EXPORT Provider *findProvider(const QString &name);
367 
371 QCA_EXPORT Provider *defaultProvider();
372 
384 QCA_EXPORT QStringList pluginPaths();
385 
389 QCA_EXPORT void scanForPlugins();
390 
394 QCA_EXPORT void unloadAllPlugins();
395 
399 QCA_EXPORT QString pluginDiagnosticText();
400 
404 QCA_EXPORT void clearPluginDiagnosticText();
405 
413 QCA_EXPORT void appendPluginDiagnosticText(const QString &text);
414 
423 QCA_EXPORT void setProperty(const QString &name, const QVariant &value);
424 
432 QCA_EXPORT QVariant getProperty(const QString &name);
433 
442 QCA_EXPORT void setProviderConfig(const QString &name, const QVariantMap &config);
443 
449 QCA_EXPORT QVariantMap getProviderConfig(const QString &name);
450 
456 QCA_EXPORT void saveProviderConfig(const QString &name);
457 
461 QCA_EXPORT QString globalRandomProvider();
462 
473 QCA_EXPORT void setGlobalRandomProvider(const QString &provider);
474 
481 QCA_EXPORT Logger *logger();
482 
493 #define QCA_logTextMessage(message, severity) \
494  do { \
495  QCA::Logger::Severity s = severity; \
496  QCA::Logger *l = QCA::logger (); \
497  if (s <= l->level ()) { \
498  l->logTextMessage (message, s); \
499  } \
500  } while (false)
501 
512 #define QCA_logBinaryMessage(blob, severity) \
513  do { \
514  QCA::Logger::Severity s = severity; \
515  QCA::Logger *l = QCA::logger (); \
516  if (s <= l->level ()) { \
517  l->logBinaryMessage (blob, s); \
518  } \
519  } while (false)
520 
529 QCA_EXPORT bool haveSystemStore();
530 
551 QCA_EXPORT CertificateCollection systemStore();
552 
560 QCA_EXPORT QString appName();
561 
571 QCA_EXPORT void setAppName(const QString &name);
572 
593 QCA_EXPORT QString arrayToHex(const QByteArray &array);
594 
620 QCA_EXPORT QByteArray hexToArray(const QString &hexString);
621 
632 QCA_EXPORT QString arrayToBase64(const QByteArray &array);
633 
646 QCA_EXPORT QByteArray base64ToArray(const QString &base64String);
647 
659 class QCA_EXPORT Initializer
660 {
661 public:
669  explicit Initializer(MemoryMode m = Practical, int prealloc = 64);
670  ~Initializer();
671 };
672 
697 class QCA_EXPORT KeyLength
698 {
699 public:
708  KeyLength(int min, int max, int multiple)
709  : _min( min ), _max(max), _multiple( multiple )
710  { }
711 
715  int minimum() const { return _min; }
716 
720  int maximum() const { return _max; }
721 
728  int multiple() const { return _multiple; }
729 
730 private:
731  const int _min, _max, _multiple;
732 };
733 
749 class QCA_EXPORT Provider
750 {
751 public:
752  virtual ~Provider();
753 
754  class Context;
755 
765  virtual void init();
766 
776  virtual void deinit();
777 
786  virtual int version() const;
787 
799  virtual int qcaVersion() const = 0;
800 
818  virtual QString name() const = 0;
819 
835  virtual QStringList features() const = 0;
836 
847  virtual QString credit() const;
848 
875  virtual Context *createContext(const QString &type) = 0;
876 
901  virtual QVariantMap defaultConfig() const;
902 
912  virtual void configChanged(const QVariantMap &config);
913 };
914 
924 class QCA_EXPORT Provider::Context : public QObject
925 {
926  Q_OBJECT
927 public:
928  virtual ~Context();
929 
933  Provider *provider() const;
934 
938  QString type() const;
939 
943  virtual Context *clone() const = 0;
944 
953  bool sameProvider(const Context *c) const;
954 
955 protected:
963  Context(Provider *parent, const QString &type);
964 
970  Context(const Context &from);
971 
972 private:
973  // disable assignment
974  Context & operator=(const Context &from);
975 
976  Provider *_provider;
977  QString _type;
978 };
979 
994 class QCA_EXPORT BasicContext : public Provider::Context
995 {
996  Q_OBJECT
997 public:
998  ~BasicContext();
999 
1000 protected:
1008  BasicContext(Provider *parent, const QString &type);
1009 
1015  BasicContext(const BasicContext &from);
1016 
1017 private:
1018  // disable assignment
1019  BasicContext & operator=(const BasicContext &from);
1020 };
1021 
1036 class QCA_EXPORT BufferedComputation
1037 {
1038 public:
1039  virtual ~BufferedComputation();
1040 
1044  virtual void clear() = 0;
1045 
1052  virtual void update(const MemoryRegion &a) = 0;
1053 
1057  virtual MemoryRegion final() = 0;
1058 
1071  MemoryRegion process(const MemoryRegion &a);
1072 };
1073 
1092 class QCA_EXPORT Filter
1093 {
1094 public:
1095  virtual ~Filter();
1096 
1100  virtual void clear() = 0;
1101 
1108  virtual MemoryRegion update(const MemoryRegion &a) = 0;
1109 
1114  virtual MemoryRegion final() = 0;
1115 
1121  virtual bool ok() const = 0;
1122 
1135  MemoryRegion process(const MemoryRegion &a);
1136 };
1137 
1148 class QCA_EXPORT Algorithm
1149 {
1150 public:
1156  Algorithm(const Algorithm &from);
1157 
1158  virtual ~Algorithm();
1159 
1165  Algorithm & operator=(const Algorithm &from);
1166 
1170  QString type() const;
1171 
1178  Provider *provider() const;
1179 
1180  // Note: The next five functions are not public!
1181 
1187  Provider::Context *context();
1188 
1194  const Provider::Context *context() const;
1195 
1203  void change(Provider::Context *c);
1204 
1213  void change(const QString &type, const QString &provider);
1214 
1220  Provider::Context *takeContext();
1221 
1222 protected:
1226  Algorithm();
1227 
1234  Algorithm(const QString &type, const QString &provider);
1235 
1236 private:
1237  class Private;
1238  QSharedDataPointer<Private> d;
1239 };
1240 
1248 class QCA_EXPORT SymmetricKey : public SecureArray
1249 {
1250 public:
1254  SymmetricKey();
1255 
1263  SymmetricKey(int size);
1264 
1270  SymmetricKey(const SecureArray &a);
1271 
1277  SymmetricKey(const QByteArray &a);
1278 
1284  bool isWeakDESKey();
1285 };
1286 
1294 class QCA_EXPORT InitializationVector : public SecureArray
1295 {
1296 public:
1301 
1307  InitializationVector(int size);
1308 
1315 
1321  InitializationVector(const QByteArray &a);
1322 };
1323 
1331 class QCA_EXPORT AuthTag : public SecureArray
1332 {
1333 public:
1337  AuthTag();
1338 
1344  AuthTag(int size);
1345 
1351  AuthTag(const SecureArray &a);
1352 
1358  AuthTag(const QByteArray &a);
1359 };
1360 
1375 class QCA_EXPORT Event
1376 {
1377 public:
1383  enum Type
1384  {
1386  Token
1387  };
1388 
1401  enum Source
1402  {
1404  Data
1405  };
1406 
1416  {
1419  StylePIN
1420  };
1421 
1425  Event();
1426 
1432  Event(const Event &from);
1433 
1437  ~Event();
1438 
1444  Event & operator=(const Event &from);
1445 
1449  bool isNull() const;
1450 
1454  Type type() const;
1455 
1459  Source source() const;
1460 
1468  PasswordStyle passwordStyle() const;
1469 
1475  KeyStoreInfo keyStoreInfo() const;
1476 
1482  KeyStoreEntry keyStoreEntry() const;
1483 
1490  QString fileName() const;
1491 
1495  void *ptr() const;
1496 
1510  void setPasswordKeyStore(PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
1511 
1523  void setPasswordData(PasswordStyle pstyle, const QString &fileName, void *ptr);
1524 
1536  void setToken(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
1537 
1538 private:
1539  class Private;
1540  QSharedDataPointer<Private> d;
1541 };
1542 
1560 class QCA_EXPORT EventHandler : public QObject
1561 {
1562  Q_OBJECT
1563 public:
1569  EventHandler(QObject *parent = 0);
1570  ~EventHandler();
1571 
1577  void start();
1578 
1589  void submitPassword(int id, const SecureArray &password);
1590 
1600  void tokenOkay(int id);
1601 
1611  void reject(int id);
1612 
1613 Q_SIGNALS:
1623  void eventReady(int id, const QCA::Event &context);
1624 
1625 private:
1626  Q_DISABLE_COPY(EventHandler)
1627 
1628  class Private;
1629  friend class Private;
1630  Private *d;
1631 };
1632 
1642 class QCA_EXPORT PasswordAsker : public QObject
1643 {
1644  Q_OBJECT
1645 public:
1651  PasswordAsker(QObject *parent = 0);
1652  ~PasswordAsker();
1653 
1665  void ask(Event::PasswordStyle pstyle, const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
1666 
1676  void ask(Event::PasswordStyle pstyle, const QString &fileName, void *ptr);
1677 
1681  void cancel();
1682 
1690  void waitForResponse();
1691 
1700  bool accepted() const;
1701 
1706  SecureArray password() const;
1707 
1708 Q_SIGNALS:
1715  void responseReady();
1716 
1717 private:
1718  Q_DISABLE_COPY(PasswordAsker)
1719 
1720  class Private;
1721  friend class Private;
1722  Private *d;
1723 };
1724 
1734 class QCA_EXPORT TokenAsker : public QObject
1735 {
1736  Q_OBJECT
1737 public:
1743  TokenAsker(QObject *parent = 0);
1744  ~TokenAsker();
1745 
1755  void ask(const KeyStoreInfo &keyStoreInfo, const KeyStoreEntry &keyStoreEntry, void *ptr);
1756 
1760  void cancel();
1761 
1768  void waitForResponse();
1769 
1775  bool accepted() const;
1776 
1777 Q_SIGNALS:
1784  void responseReady();
1785 
1786 private:
1787  Q_DISABLE_COPY(TokenAsker)
1788 
1789  class Private;
1790  friend class Private;
1791  Private *d;
1792 };
1793 
1794 }
1795 
1796 #endif
An asynchronous event.
Definition: qca_core.h:1375
QCA_EXPORT Logger * logger()
Return a reference to the QCA Logger, which is used for diagnostics and error recording.
General superclass for an algorithm.
Definition: qca_core.h:1148
QCA_EXPORT void setProviderConfig(const QString &name, const QVariantMap &config)
Set provider configuration.
QCA_EXPORT void appendPluginDiagnosticText(const QString &text)
Add plugin diagnostic text.
QCA_EXPORT void deinit()
Clean up routine.
A simple logging system.
Definition: qca_support.h:954
QCA_EXPORT QStringList supportedFeatures()
Generate a list of all the supported features in plugins, and in built in capabilities ...
QCA_EXPORT bool haveSecureRandom()
Test if secure random is available.
QCA_EXPORT void clearPluginDiagnosticText()
Clear plugin diagnostic text.
KeyLength(int min, int max, int multiple)
Construct a KeyLength object.
Definition: qca_core.h:708
Header file with QCA version.
QCA_EXPORT QString pluginDiagnosticText()
Retrieve plugin diagnostic text.
User password / passphrase / PIN handler.
Definition: qca_core.h:1642
QCA_EXPORT void setProviderPriority(const QString &name, int priority)
Change the priority of a specified provider.
Source
Source of the event
Definition: qca_core.h:1401
QCA_EXPORT void scanForPlugins()
Scan for new plugins.
Algorithm provider.
Definition: qca_core.h:749
Key store information, outside of a KeyStore object.
Definition: qca_keystore.h:623
Interface class for password / passphrase / PIN and token handlers.
Definition: qca_core.h:1560
QCA_EXPORT bool haveSystemStore()
Test if QCA can access the root CA certificates.
Header file for "support" classes used in QCA.
QCA_EXPORT void setAppName(const QString &name)
Set the application name that will be used by SASL server mode.
Asking for a password, PIN or passphrase.
Definition: qca_core.h:1385
QCA_EXPORT ProviderList providers()
Return a list of the current providers.
QCA_EXPORT void unloadAllPlugins()
Unload the current plugins.
QCA_EXPORT QVariantMap getProviderConfig(const QString &name)
Retrieve provider configuration.
QCA_EXPORT const char * qcaVersionStr()
The current version of QCA.
QList< Provider * > ProviderList
Convenience representation for the plugin providers.
Definition: qca_core.h:100
General superclass for buffered computation algorithms.
Definition: qca_core.h:1036
QCA_EXPORT QByteArray base64ToArray(const QString &base64String)
Convert a QString containing a base64 representation of a byte array into a QByteArray.
Container for keys for symmetric encryption algorithms.
Definition: qca_core.h:1248
QCA_EXPORT bool unloadProvider(const QString &name)
Unload specified provider.
Container for authentication tag.
Definition: qca_core.h:1331
Simple container for acceptable key lengths
Definition: qca_core.h:697
QCA_EXPORT Provider * defaultProvider()
Return the default provider.
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
User should be prompted for a "Passphrase".
Definition: qca_core.h:1418
PasswordStyle
password variation
Definition: qca_core.h:1415
Preprocessor magic to allow export of library symbols.
QCA_EXPORT QStringList defaultFeatures()
Generate a list of the built in features.
mlock and drop root
Definition: qca_core.h:130
QCA_EXPORT bool insertProvider(Provider *p, int priority=0)
Add a provider to the current list of providers.
QCA_EXPORT int providerPriority(const QString &name)
Return the priority of a specified provider.
Container for initialisation vectors and nonces.
Definition: qca_core.h:1294
mlock and drop root if available, else mmap
Definition: qca_core.h:129
Direction
Direction settings for symmetric algorithms.
Definition: qca_core.h:140
KeyStore generated the event.
Definition: qca_core.h:1403
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:48
QCA_EXPORT QStringList pluginPaths()
Retrieve plugin paths.
Type
Type of event
Definition: qca_core.h:1383
Base class to use for primitive provider contexts.
Definition: qca_core.h:994
Single entry in a KeyStore
Definition: qca_keystore.h:140
QCA_EXPORT Provider * findProvider(const QString &name)
Return the named provider, or 0 if not found.
QCA_EXPORT CertificateCollection systemStore()
Get system-wide root Certificate Authority (CA) certificates.
QCA_EXPORT QString arrayToHex(const QByteArray &array)
Convert a byte array to printable hexadecimal representation.
QCA_EXPORT QString arrayToBase64(const QByteArray &array)
Convert a byte array to printable base64 representation.
QCA_EXPORT void init()
Initialise QCA.
int minimum() const
Obtain the minimum length for the key, in bytes.
Definition: qca_core.h:715
Secure array of bytes.
Definition: qca_tools.h:316
QCA_EXPORT QByteArray hexToArray(const QString &hexString)
Convert a QString containing a hexadecimal representation of a byte array into a QByteArray ...
QCA_EXPORT int qcaVersion()
The current version of QCA.
User should be prompted for a "Password".
Definition: qca_core.h:1417
QCA_EXPORT int qcaMinorVersion()
The current version of QCA.
QCA_EXPORT void setGlobalRandomProvider(const QString &provider)
Change the global random number provider.
Operate in the "forward" direction; for example, encrypting.
Definition: qca_core.h:142
Header file for "tool" classes used in QCA.
General superclass for filtering transformation algorithms.
Definition: qca_core.h:1092
Operate in the "reverse" direction; for example, decrypting.
Definition: qca_core.h:143
mlock, retaining root privileges
Definition: qca_core.h:131
User token handler.
Definition: qca_core.h:1734
Convenience method for initialising and cleaning up QCA.
Definition: qca_core.h:659
MemoryMode
Mode settings for memory allocation.
Definition: qca_core.h:127
QCA_EXPORT int qcaMajorVersion()
The current version of QCA.
QCA_EXPORT void saveProviderConfig(const QString &name)
Save provider configuration to persistent storage.
QCA_EXPORT QString globalRandomProvider()
Return the name of the global random number provider.
Internal context class used for the plugin.
QCA_EXPORT void setProperty(const QString &name, const QVariant &value)
Set a global property.
QCA_EXPORT QVariant getProperty(const QString &name)
Retrieve a global property.
int maximum() const
Obtain the maximum length for the key, in bytes.
Definition: qca_core.h:720
int multiple() const
Return the number of bytes that the key must be a multiple of.
Definition: qca_core.h:728
QCA_EXPORT bool haveSecureMemory()
Test if secure storage memory is available.
QCA_EXPORT int qcaPatchVersion()
The current version of QCA.
Array of bytes that may be optionally secured.
Definition: qca_tools.h:90
QCA_EXPORT QString appName()
Get the application name that will be used by SASL server mode.