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 <QObject>
37 #include "qca_core.h"
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 {
86 };
87 
92 {
96 };
97 
102 {
109 };
110 
118 {
123 };
124 
134 {
146 
147 };
148 
161 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
162 
170 class QCA_EXPORT DLGroup
171 {
172 public:
173  DLGroup();
174 
182  DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
183 
190  DLGroup(const BigInteger &p, const BigInteger &g);
191 
197  DLGroup(const DLGroup &from);
198  ~DLGroup();
199 
205  DLGroup & operator=(const DLGroup &from);
206 
213  static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
214 
218  bool isNull() const;
219 
223  BigInteger p() const;
224 
228  BigInteger q() const;
229 
233  BigInteger g() const;
234 
235 private:
236  class Private;
237  Private *d;
238 };
239 
249 class QCA_EXPORT PKey : public Algorithm
250 {
251 public:
255  enum Type {
256  RSA,
257  DSA,
258  DH
259  };
260 
264  PKey();
265 
271  PKey(const PKey &from);
272 
273  ~PKey() override;
274 
280  PKey & operator=(const PKey &from);
281 
313  static QList<Type> supportedTypes(const QString &provider = QString());
314 
344  static QList<Type> supportedIOTypes(const QString &provider = QString());
345 
351  bool isNull() const;
352 
358  Type type() const;
359 
363  int bitSize() const;
364 
368  bool isRSA() const;
369 
373  bool isDSA() const;
374 
378  bool isDH() const;
379 
383  bool isPublic() const;
384 
388  bool isPrivate() const;
389 
394  bool canExport() const;
395 
399  bool canKeyAgree() const;
400 
407  PublicKey toPublicKey() const;
408 
412  PrivateKey toPrivateKey() const;
413 
419  bool operator==(const PKey &a) const;
420 
426  bool operator!=(const PKey &a) const;
427 
428 protected:
435  PKey(const QString &type, const QString &provider);
436 
442  void set(const PKey &k);
443 
453  RSAPublicKey toRSAPublicKey() const;
454 
464  RSAPrivateKey toRSAPrivateKey() const;
465 
475  DSAPublicKey toDSAPublicKey() const;
476 
486  DSAPrivateKey toDSAPrivateKey() const;
487 
497  DHPublicKey toDHPublicKey() const;
498 
508  DHPrivateKey toDHPrivateKey() const;
509 
510 private:
511  void assignToPublic(PKey *dest) const;
512  void assignToPrivate(PKey *dest) const;
513 
514  class Private;
515  Private *d;
516 };
517 
526 class QCA_EXPORT PublicKey : public PKey
527 {
528 public:
532  PublicKey();
533 
539  PublicKey(const PrivateKey &k);
540 
548  PublicKey(const QString &fileName);
549 
555  PublicKey(const PublicKey &from);
556 
557  ~PublicKey() override;
558 
564  PublicKey & operator=(const PublicKey &from);
565 
572  RSAPublicKey toRSA() const;
573 
580  DSAPublicKey toDSA() const;
581 
588  DHPublicKey toDH() const;
589 
595  bool canEncrypt() const;
596 
602  bool canDecrypt() const;
603 
609  bool canVerify() const;
610 
617  int maximumEncryptSize(EncryptionAlgorithm alg) const;
618 
625  SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
626 
637  bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
638 
645  void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
646 
652  void update(const MemoryRegion &a);
653 
679  bool validSignature(const QByteArray &sig);
680 
694  bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
695 
699  QByteArray toDER() const;
700 
709  QString toPEM() const;
710 
722  bool toPEMFile(const QString &fileName) const;
723 
746  static PublicKey fromDER(const QByteArray &a, ConvertResult *result = nullptr, const QString &provider = QString());
747 
773  static PublicKey fromPEM(const QString &s, ConvertResult *result = nullptr, const QString &provider = QString());
774 
802  static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = nullptr, const QString &provider = QString());
803 
804 protected:
811  PublicKey(const QString &type, const QString &provider);
812 
813 private:
814  class Private;
815  Private *d;
816 };
817 
826 class QCA_EXPORT PrivateKey : public PKey
827 {
828 public:
832  PrivateKey();
833 
845  explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
846 
852  PrivateKey(const PrivateKey &from);
853 
854  ~PrivateKey() override;
855 
861  PrivateKey & operator=(const PrivateKey &from);
862 
866  RSAPrivateKey toRSA() const;
867 
871  DSAPrivateKey toDSA() const;
872 
876  DHPrivateKey toDH() const;
877 
883  bool canDecrypt() const;
884 
890  bool canEncrypt() const;
891 
897  bool canSign() const;
898 
905  int maximumEncryptSize(EncryptionAlgorithm alg) const;
906 
917  bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
918 
925  SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
926 
936  void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
937 
946  void update(const MemoryRegion &a);
947 
954  QByteArray signature();
955 
968  QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
969 
975  SymmetricKey deriveKey(const PublicKey &theirs);
976 
984  static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
985 
996  SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
997 
1010  QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
1011 
1028  bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
1029 
1048  static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = nullptr, const QString &provider = QString());
1049 
1068  static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = nullptr, const QString &provider = QString());
1069 
1092  static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = nullptr, const QString &provider = QString());
1093 
1094 protected:
1102  PrivateKey(const QString &type, const QString &provider);
1103 
1104 private:
1105  class Private;
1106  Private *d;
1107 };
1108 
1120 class QCA_EXPORT KeyGenerator : public QObject
1121 {
1122  Q_OBJECT
1123 public:
1129  KeyGenerator(QObject *parent = nullptr);
1130 
1131  ~KeyGenerator() override;
1132 
1141  bool blockingEnabled() const;
1142 
1151  void setBlockingEnabled(bool b);
1152 
1158  bool isBusy() const;
1159 
1176  PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
1177 
1193  PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
1194 
1209  PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
1210 
1217  PrivateKey key() const;
1218 
1227  DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
1228 
1232  DLGroup dlGroup() const;
1233 
1234 Q_SIGNALS:
1240  void finished();
1241 
1242 private:
1243  Q_DISABLE_COPY(KeyGenerator)
1244 
1245  class Private;
1246  friend class Private;
1247  Private *d;
1248 };
1249 
1258 class QCA_EXPORT RSAPublicKey : public PublicKey
1259 {
1260 public:
1264  RSAPublicKey();
1265 
1274  RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
1275 
1281  RSAPublicKey(const RSAPrivateKey &k);
1282 
1290  BigInteger n() const;
1291 
1298  BigInteger e() const;
1299 };
1300 
1309 class QCA_EXPORT RSAPrivateKey : public PrivateKey
1310 {
1311 public:
1315  RSAPrivateKey();
1316 
1328  RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
1329 
1337  BigInteger n() const;
1338 
1345  BigInteger e() const;
1346 
1350  BigInteger p() const;
1351 
1356  BigInteger q() const;
1357 
1361  BigInteger d() const;
1362 };
1363 
1372 class QCA_EXPORT DSAPublicKey : public PublicKey
1373 {
1374 public:
1378  DSAPublicKey();
1379 
1388  DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
1389 
1395  DSAPublicKey(const DSAPrivateKey &k);
1396 
1400  DLGroup domain() const;
1401 
1405  BigInteger y() const;
1406 };
1407 
1416 class QCA_EXPORT DSAPrivateKey : public PrivateKey
1417 {
1418 public:
1422  DSAPrivateKey();
1423 
1433  DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
1434 
1438  DLGroup domain() const;
1439 
1443  BigInteger y() const;
1444 
1448  BigInteger x() const;
1449 };
1450 
1459 class QCA_EXPORT DHPublicKey : public PublicKey
1460 {
1461 public:
1465  DHPublicKey();
1466 
1475  DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
1476 
1482  DHPublicKey(const DHPrivateKey &k);
1483 
1487  DLGroup domain() const;
1488 
1492  BigInteger y() const;
1493 };
1494 
1503 class QCA_EXPORT DHPrivateKey : public PrivateKey
1504 {
1505 public:
1509  DHPrivateKey();
1510 
1520  DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
1521 
1525  DLGroup domain() const;
1526 
1530  BigInteger y() const;
1531 
1535  BigInteger x() const;
1536 };
1538 }
1539 
1540 #endif
QCA::DLGroup
Definition: qca_publickey.h:170
QCA::PKey::RSA
@ RSA
RSA key.
Definition: qca_publickey.h:256
QObject
QCA::ErrorFile
@ ErrorFile
Failure because of incorrect file.
Definition: qca_publickey.h:122
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:145
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:91
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:826
QCA
QCA - the Qt Cryptographic Architecture.
Definition: qca_basic.h:41
QCA::Algorithm
Definition: qca_core.h:1151
QCA::IETF_1024
@ IETF_1024
Group 2 from RFC 2412, Section E.2.
Definition: qca_publickey.h:139
QCA::PBES2_DES_SHA1
@ PBES2_DES_SHA1
PKCS#5 v2.0 DES/CBC,SHA1.
Definition: qca_publickey.h:104
QCA::DSA_512
@ DSA_512
512 bit group, for compatibility with JCE
Definition: qca_publickey.h:135
QCA::SymmetricKey
Definition: qca_core.h:1251
QList
QCA::RSAPublicKey
Definition: qca_publickey.h:1258
QCA::IETF_1536
@ IETF_1536
1536-bit MODP Group ("group 5") from RFC3526 Section 2.
Definition: qca_publickey.h:140
QCA::EMSA3_SHA224
@ EMSA3_SHA224
SHA224, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:82
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:107
QCA::PBES2_TripleDES_SHA1
@ PBES2_TripleDES_SHA1
PKCS#5 v2.0 TripleDES/CBC,SHA1.
Definition: qca_publickey.h:105
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:103
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:144
QCA::SecureArray
Definition: qca_tools.h:316
QCA::DefaultFormat
@ DefaultFormat
For DSA, this is the same as IEEE_1363.
Definition: qca_publickey.h:93
QCA::ErrorDecode
@ ErrorDecode
General failure in the decode stage.
Definition: qca_publickey.h:120
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:143
QCA::PBES2_AES256_SHA1
@ PBES2_AES256_SHA1
PKCS#5 v2.0 AES-256/CBC,SHA1.
Definition: qca_publickey.h:108
QCA::DSAPublicKey
Definition: qca_publickey.h:1372
QCA::PKey::DSA
@ DSA
DSA key.
Definition: qca_publickey.h:257
QCA::PKey::Type
Type
Types of public key cryptography keys supported by QCA.
Definition: qca_publickey.h:255
QCA::DSA_1024
@ DSA_1024
1024 bit group, for compatibility with JCE
Definition: qca_publickey.h:137
QCA::EMSA3_SHA384
@ EMSA3_SHA384
SHA384, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:84
QCA::IETF_768
@ IETF_768
Group 1 from RFC 2412, Section E.1.
Definition: qca_publickey.h:138
QCA::EMSA3_SHA256
@ EMSA3_SHA256
SHA256, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:83
QCA::ConvertResult
ConvertResult
Return value from a format conversion.
Definition: qca_publickey.h:117
QCA::KeyGenerator
Definition: qca_publickey.h:1120
QCA::PBEAlgorithm
PBEAlgorithm
Password-based encryption.
Definition: qca_publickey.h:101
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:1309
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:142
QCA::EMSA3_SHA512
@ EMSA3_SHA512
SHA512, with EMSA3 (ie PKCS#1 Version 1.5) encoding.
Definition: qca_publickey.h:85
QCA::IETF_2048
@ IETF_2048
2048-bit MODP Group ("group 14") from RFC3526 Section 3.
Definition: qca_publickey.h:141
QCA::DSAPrivateKey
Definition: qca_publickey.h:1416
QCA::DHPublicKey
Definition: qca_publickey.h:1459
QCA::DHPrivateKey
Definition: qca_publickey.h:1503
QCA::DERSequence
@ DERSequence
Signature wrapped in DER formatting (OpenSSL/Java)
Definition: qca_publickey.h:95
QCA::PublicKey
Definition: qca_publickey.h:526
QCA::ConvertGood
@ ConvertGood
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:119
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:136
QCA::PKey
Definition: qca_publickey.h:249
QCA::ErrorPassphrase
@ ErrorPassphrase
Failure because of incorrect passphrase.
Definition: qca_publickey.h:121
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:94
QCA::PBES2_AES128_SHA1
@ PBES2_AES128_SHA1
PKCS#5 v2.0 AES-128/CBC,SHA1.
Definition: qca_publickey.h:106
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:133