Qt Cryptographic Architecture
qca_publickey.h
Go to the documentation of this file.
1 /*
2  * qca_publickey.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  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22 
33 #ifndef QCA_PUBLICKEY_H
34 #define QCA_PUBLICKEY_H
35 
36 #include "qca_core.h"
37 #include <QObject>
38 
39 namespace QCA {
40 
41 class PublicKey;
42 class PrivateKey;
43 class KeyGenerator;
44 class RSAPublicKey;
45 class RSAPrivateKey;
46 class DSAPublicKey;
47 class DSAPrivateKey;
48 class DHPublicKey;
49 class DHPrivateKey;
50 
55 {
60 };
61 
74 {
82  EMSA3_SHA224,
87 };
88 
93 {
97 };
98 
103 {
110 };
111 
119 {
124 };
125 
135 {
147 
148 };
149 
162 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
163 
171 class QCA_EXPORT DLGroup
172 {
173 public:
174  DLGroup();
175 
183  DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
184 
191  DLGroup(const BigInteger &p, const BigInteger &g);
192 
198  DLGroup(const DLGroup &from);
199  ~DLGroup();
200 
206  DLGroup &operator=(const DLGroup &from);
207 
214  static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
215 
219  bool isNull() const;
220 
224  BigInteger p() const;
225 
229  BigInteger q() const;
230 
234  BigInteger g() const;
235 
236 private:
237  class Private;
238  Private *d;
239 };
240 
250 class QCA_EXPORT PKey : public Algorithm
251 {
252 public:
256  enum Type
257  {
258  RSA,
259  DSA,
260  DH
261  };
262 
266  PKey();
267 
273  PKey(const PKey &from);
274 
275  ~PKey() override;
276 
282  PKey &operator=(const PKey &from);
283 
315  static QList<Type> supportedTypes(const QString &provider = QString());
316 
346  static QList<Type> supportedIOTypes(const QString &provider = QString());
347 
353  bool isNull() const;
354 
360  Type type() const;
361 
365  int bitSize() const;
366 
370  bool isRSA() const;
371 
375  bool isDSA() const;
376 
380  bool isDH() const;
381 
385  bool isPublic() const;
386 
390  bool isPrivate() const;
391 
396  bool canExport() const;
397 
401  bool canKeyAgree() const;
402 
409  PublicKey toPublicKey() const;
410 
414  PrivateKey toPrivateKey() const;
415 
421  bool operator==(const PKey &a) const;
422 
428  bool operator!=(const PKey &a) const;
429 
430 protected:
437  PKey(const QString &type, const QString &provider);
438 
444  void set(const PKey &k);
445 
455  RSAPublicKey toRSAPublicKey() const;
456 
466  RSAPrivateKey toRSAPrivateKey() const;
467 
477  DSAPublicKey toDSAPublicKey() const;
478 
488  DSAPrivateKey toDSAPrivateKey() const;
489 
499  DHPublicKey toDHPublicKey() const;
500 
510  DHPrivateKey toDHPrivateKey() const;
511 
512 private:
513  void assignToPublic(PKey *dest) const;
514  void assignToPrivate(PKey *dest) const;
515 
516  class Private;
517  Private *d;
518 };
519 
528 class QCA_EXPORT PublicKey : public PKey
529 {
530 public:
534  PublicKey();
535 
541  PublicKey(const PrivateKey &k);
542 
550  PublicKey(const QString &fileName);
551 
557  PublicKey(const PublicKey &from);
558 
559  ~PublicKey() override;
560 
566  PublicKey &operator=(const PublicKey &from);
567 
574  RSAPublicKey toRSA() const;
575 
582  DSAPublicKey toDSA() const;
583 
590  DHPublicKey toDH() const;
591 
597  bool canEncrypt() const;
598 
604  bool canDecrypt() const;
605 
611  bool canVerify() const;
612 
619  int maximumEncryptSize(EncryptionAlgorithm alg) const;
620 
627  SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
628 
639  bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
640 
647  void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
648 
654  void update(const MemoryRegion &a);
655 
681  bool validSignature(const QByteArray &sig);
682 
696  bool verifyMessage(const MemoryRegion &a,
697  const QByteArray & sig,
698  SignatureAlgorithm alg,
699  SignatureFormat format = DefaultFormat);
700 
704  QByteArray toDER() const;
705 
714  QString toPEM() const;
715 
727  bool toPEMFile(const QString &fileName) const;
728 
751  static PublicKey fromDER(const QByteArray &a, ConvertResult *result = nullptr, const QString &provider = QString());
752 
778  static PublicKey fromPEM(const QString &s, ConvertResult *result = nullptr, const QString &provider = QString());
779 
807  static PublicKey
808  fromPEMFile(const QString &fileName, ConvertResult *result = nullptr, const QString &provider = QString());
809 
810 protected:
817  PublicKey(const QString &type, const QString &provider);
818 
819 private:
820  class Private;
821  Private *d;
822 };
823 
832 class QCA_EXPORT PrivateKey : public PKey
833 {
834 public:
838  PrivateKey();
839 
851  explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
852 
858  PrivateKey(const PrivateKey &from);
859 
860  ~PrivateKey() override;
861 
867  PrivateKey &operator=(const PrivateKey &from);
868 
872  RSAPrivateKey toRSA() const;
873 
877  DSAPrivateKey toDSA() const;
878 
882  DHPrivateKey toDH() const;
883 
889  bool canDecrypt() const;
890 
896  bool canEncrypt() const;
897 
903  bool canSign() const;
904 
911  int maximumEncryptSize(EncryptionAlgorithm alg) const;
912 
923  bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
924 
931  SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
932 
942  void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
943 
952  void update(const MemoryRegion &a);
953 
960  QByteArray signature();
961 
974  QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
975 
981  SymmetricKey deriveKey(const PublicKey &theirs);
982 
990  static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
991 
1002  SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
1003 
1016  QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
1017 
1034  bool toPEMFile(const QString & fileName,
1035  const SecureArray &passphrase = SecureArray(),
1036  PBEAlgorithm pbe = PBEDefault) const;
1037 
1056  static PrivateKey fromDER(const SecureArray &a,
1057  const SecureArray &passphrase = SecureArray(),
1058  ConvertResult * result = nullptr,
1059  const QString & provider = QString());
1060 
1079  static PrivateKey fromPEM(const QString & s,
1080  const SecureArray &passphrase = SecureArray(),
1081  ConvertResult * result = nullptr,
1082  const QString & provider = QString());
1083 
1106  static PrivateKey fromPEMFile(const QString & fileName,
1107  const SecureArray &passphrase = SecureArray(),
1108  ConvertResult * result = nullptr,
1109  const QString & provider = QString());
1110 
1111 protected:
1119  PrivateKey(const QString &type, const QString &provider);
1120 
1121 private:
1122  class Private;
1123  Private *d;
1124 };
1125 
1137 class QCA_EXPORT KeyGenerator : public QObject
1138 {
1139  Q_OBJECT
1140 public:
1146  KeyGenerator(QObject *parent = nullptr);
1147 
1148  ~KeyGenerator() override;
1149 
1158  bool blockingEnabled() const;
1159 
1168  void setBlockingEnabled(bool b);
1169 
1175  bool isBusy() const;
1176 
1193  PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
1194 
1210  PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
1211 
1226  PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
1227 
1234  PrivateKey key() const;
1235 
1244  DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
1245 
1249  DLGroup dlGroup() const;
1250 
1251 Q_SIGNALS:
1257  void finished();
1258 
1259 private:
1260  Q_DISABLE_COPY(KeyGenerator)
1261 
1262  class Private;
1263  friend class Private;
1264  Private *d;
1265 };
1266 
1275 class QCA_EXPORT RSAPublicKey : public PublicKey
1276 {
1277 public:
1281  RSAPublicKey();
1282 
1291  RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
1292 
1298  RSAPublicKey(const RSAPrivateKey &k);
1299 
1307  BigInteger n() const;
1308 
1315  BigInteger e() const;
1316 };
1317 
1326 class QCA_EXPORT RSAPrivateKey : public PrivateKey
1327 {
1328 public:
1332  RSAPrivateKey();
1333 
1345  RSAPrivateKey(const BigInteger &n,
1346  const BigInteger &e,
1347  const BigInteger &p,
1348  const BigInteger &q,
1349  const BigInteger &d,
1350  const QString & provider = QString());
1351 
1359  BigInteger n() const;
1360 
1367  BigInteger e() const;
1368 
1372  BigInteger p() const;
1373 
1378  BigInteger q() const;
1379 
1383  BigInteger d() const;
1384 };
1385 
1394 class QCA_EXPORT DSAPublicKey : public PublicKey
1395 {
1396 public:
1400  DSAPublicKey();
1401 
1410  DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
1411 
1417  DSAPublicKey(const DSAPrivateKey &k);
1418 
1422  DLGroup domain() const;
1423 
1427  BigInteger y() const;
1428 };
1429 
1438 class QCA_EXPORT DSAPrivateKey : public PrivateKey
1439 {
1440 public:
1444  DSAPrivateKey();
1445 
1455  DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
1456 
1460  DLGroup domain() const;
1461 
1465  BigInteger y() const;
1466 
1470  BigInteger x() const;
1471 };
1472 
1481 class QCA_EXPORT DHPublicKey : public PublicKey
1482 {
1483 public:
1487  DHPublicKey();
1488 
1497  DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
1498 
1504  DHPublicKey(const DHPrivateKey &k);
1505 
1509  DLGroup domain() const;
1510 
1514  BigInteger y() const;
1515 };
1516 
1525 class QCA_EXPORT DHPrivateKey : public PrivateKey
1526 {
1527 public:
1531  DHPrivateKey();
1532 
1542  DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
1543 
1547  DLGroup domain() const;
1548 
1552  BigInteger y() const;
1553 
1557  BigInteger x() const;
1558 };
1560 }
1561 
1562 #endif
QCA::DLGroup
Definition: qca_publickey.h:171
QCA::PKey::RSA
@ RSA
RSA key.
Definition: qca_publickey.h:258
QObject
QCA::ErrorFile
@ ErrorFile
Failure because of incorrect file.
Definition: qca_publickey.h:123
QCA::EMSA3_MD5
@ EMSA3_MD5
MD5, with EMSA3 (ie PKCS#1 Version 1.5) encoding (this is the usual RSA algorithm)
Definition: qca_publickey.h:78
QCA::IETF_8192
@ IETF_8192
8192-bit MODP Group ("group 18") from RFC3526 Section 7.
Definition: qca_publickey.h:146
QCA::EMSA3_Raw
@ EMSA3_Raw
EMSA3 without computing a message digest or a DigestInfo encoding (identical to PKCS#11's CKM_RSA_PKC...
Definition: qca_publickey.h:81
QCA::SignatureFormat
SignatureFormat
Signature formats (DSA only)
Definition: qca_publickey.h:92
QCA::EMSA1_SHA1
@ EMSA1_SHA1
SHA1, with EMSA1 (IEEE1363-2000) encoding (this is the usual DSA algorithm - FIPS186)
Definition: qca_publickey.h:76
QCA::PrivateKey
Definition: qca_publickey.h:832
QCA
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA::Algorithm
Definition: qca_core.h:1163
QCA::IETF_1024
@ IETF_1024
Group 2 from RFC 2412, Section E.2.
Definition: qca_publickey.h:140
QCA::PBES2_DES_SHA1
@ PBES2_DES_SHA1
PKCS#5 v2.0 DES/CBC,SHA1.
Definition: qca_publickey.h:105
QCA::DSA_512
@ DSA_512
512 bit group, for compatibility with JCE
Definition: qca_publickey.h:136
QCA::SymmetricKey
Definition: qca_core.h:1263
QList
QCA::RSAPublicKey
Definition: qca_publickey.h:1275
QCA::IETF_1536
@ IETF_1536
1536-bit MODP Group ("group 5") from RFC3526 Section 2.
Definition: qca_publickey.h:141
QCA::EMSA3_SHA224
@ EMSA3_SHA224
SHA224, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:83
QCA::SignatureAlgorithm
SignatureAlgorithm
Signature algorithm variants.
Definition: qca_publickey.h:73
QCA::EME_PKCS1v15_SSL
@ EME_PKCS1v15_SSL
PKCS#1, Version 1.5 with an SSL-specific modification.
Definition: qca_publickey.h:58
QCA::EMSA3_RIPEMD160
@ EMSA3_RIPEMD160
RIPEMD160, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:80
QCA::PBES2_AES192_SHA1
@ PBES2_AES192_SHA1
PKCS#5 v2.0 AES-192/CBC,SHA1.
Definition: qca_publickey.h:108
QCA::PBES2_TripleDES_SHA1
@ PBES2_TripleDES_SHA1
PKCS#5 v2.0 TripleDES/CBC,SHA1.
Definition: qca_publickey.h:106
QCA::EncryptionAlgorithm
EncryptionAlgorithm
Encryption algorithms.
Definition: qca_publickey.h:54
QCA::PBEDefault
@ PBEDefault
Use modern default (same as PBES2_TripleDES_SHA1)
Definition: qca_publickey.h:104
QCA::EMSA3_MD2
@ EMSA3_MD2
MD2, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:79
QCA::IETF_6144
@ IETF_6144
6144-bit MODP Group ("group 17") from RFC3526 Section 6.
Definition: qca_publickey.h:145
QCA::SecureArray
Definition: qca_tools.h:316
QCA::DefaultFormat
@ DefaultFormat
For DSA, this is the same as IEEE_1363.
Definition: qca_publickey.h:94
QCA::ErrorDecode
@ ErrorDecode
General failure in the decode stage.
Definition: qca_publickey.h:121
QCA::BigInteger
Definition: qca_tools.h:570
QCA::IETF_4096
@ IETF_4096
4096-bit MODP Group ("group 16") from RFC3526 Section 5.
Definition: qca_publickey.h:144
QCA::PBES2_AES256_SHA1
@ PBES2_AES256_SHA1
PKCS#5 v2.0 AES-256/CBC,SHA1.
Definition: qca_publickey.h:109
QCA::DSAPublicKey
Definition: qca_publickey.h:1394
QCA::PKey::DSA
@ DSA
DSA key.
Definition: qca_publickey.h:259
QCA::PKey::Type
Type
Types of public key cryptography keys supported by QCA.
Definition: qca_publickey.h:256
QCA::DSA_1024
@ DSA_1024
1024 bit group, for compatibility with JCE
Definition: qca_publickey.h:138
QCA::EMSA3_SHA384
@ EMSA3_SHA384
SHA384, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:85
QCA::IETF_768
@ IETF_768
Group 1 from RFC 2412, Section E.1.
Definition: qca_publickey.h:139
QCA::EMSA3_SHA256
@ EMSA3_SHA256
SHA256, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:84
QCA::ConvertResult
ConvertResult
Return value from a format conversion.
Definition: qca_publickey.h:118
QCA::KeyGenerator
Definition: qca_publickey.h:1137
QCA::PBEAlgorithm
PBEAlgorithm
Password-based encryption.
Definition: qca_publickey.h:102
qca_core.h
QCA::SignatureUnknown
@ SignatureUnknown
Unknown signing algorithm.
Definition: qca_publickey.h:75
QCA::EME_NO_PADDING
@ EME_NO_PADDING
Raw RSA encryption.
Definition: qca_publickey.h:59
QCA::RSAPrivateKey
Definition: qca_publickey.h:1326
QCA::MemoryRegion
Definition: qca_tools.h:90
QCA::EME_PKCS1v15
@ EME_PKCS1v15
Block type 2 (PKCS#1, Version 1.5)
Definition: qca_publickey.h:56
QCA::IETF_3072
@ IETF_3072
3072-bit MODP Group ("group 15") from RFC3526 Section 4.
Definition: qca_publickey.h:143
QCA::EMSA3_SHA512
@ EMSA3_SHA512
SHA512, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:86
QCA::IETF_2048
@ IETF_2048
2048-bit MODP Group ("group 14") from RFC3526 Section 3.
Definition: qca_publickey.h:142
QCA::DSAPrivateKey
Definition: qca_publickey.h:1438
QCA::DHPublicKey
Definition: qca_publickey.h:1481
QCA::DHPrivateKey
Definition: qca_publickey.h:1525
QCA::DERSequence
@ DERSequence
Signature wrapped in DER formatting (OpenSSL/Java)
Definition: qca_publickey.h:96
QCA::PublicKey
Definition: qca_publickey.h:528
QCA::ConvertGood
@ ConvertGood
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:120
QCA::EMSA3_SHA1
@ EMSA3_SHA1
SHA1, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:77
QCA::DSA_768
@ DSA_768
768 bit group, for compatibility with JCE
Definition: qca_publickey.h:137
QCA::PKey
Definition: qca_publickey.h:250
QCA::ErrorPassphrase
@ ErrorPassphrase
Failure because of incorrect passphrase.
Definition: qca_publickey.h:122
QCA::emsa3Encode
QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size=-1)
Encode a hash result in EMSA3 (PKCS#1) format.
QCA::IEEE_1363
@ IEEE_1363
40-byte format from IEEE 1363 (Botan/.NET)
Definition: qca_publickey.h:95
QCA::PBES2_AES128_SHA1
@ PBES2_AES128_SHA1
PKCS#5 v2.0 AES-128/CBC,SHA1.
Definition: qca_publickey.h:107
QCA::EME_PKCS1_OAEP
@ EME_PKCS1_OAEP
Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
Definition: qca_publickey.h:57
QCA::DLGroupSet
DLGroupSet
Well known discrete logarithm group sets.
Definition: qca_publickey.h:134