00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00032 #ifndef QCA_PUBLICKEY_H
00033 #define QCA_PUBLICKEY_H
00034
00035 #include <QObject>
00036 #include "qca_core.h"
00037
00038 namespace QCA {
00039
00040 class PublicKey;
00041 class PrivateKey;
00042 class KeyGenerator;
00043 class RSAPublicKey;
00044 class RSAPrivateKey;
00045 class DSAPublicKey;
00046 class DSAPrivateKey;
00047 class DHPublicKey;
00048 class DHPrivateKey;
00049
00053 enum EncryptionAlgorithm
00054 {
00055 EME_PKCS1v15,
00056 EME_PKCS1_OAEP
00057 };
00058
00062 enum SignatureAlgorithm
00063 {
00064 SignatureUnknown,
00065 EMSA1_SHA1,
00066 EMSA3_SHA1,
00067 EMSA3_MD5,
00068 EMSA3_MD2,
00069 EMSA3_RIPEMD160,
00070 EMSA3_Raw
00071 };
00072
00076 enum SignatureFormat
00077 {
00078 DefaultFormat,
00079 IEEE_1363,
00080 DERSequence
00081 };
00082
00086 enum PBEAlgorithm
00087 {
00088 PBEDefault,
00089 PBES2_DES_SHA1,
00090 PBES2_TripleDES_SHA1,
00091 PBES2_AES128_SHA1,
00092 PBES2_AES192_SHA1,
00093 PBES2_AES256_SHA1
00094 };
00095
00102 enum ConvertResult
00103 {
00104 ConvertGood,
00105 ErrorDecode,
00106 ErrorPassphrase,
00107 ErrorFile
00108 };
00109
00118 enum DLGroupSet
00119 {
00120 DSA_512,
00121 DSA_768,
00122 DSA_1024,
00123 IETF_768,
00124 IETF_1024,
00125 IETF_1536,
00126 IETF_2048,
00127 IETF_3072,
00128 IETF_4096,
00129 IETF_6144,
00130 IETF_8192
00131
00132 };
00133
00142 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
00143
00149 class QCA_EXPORT DLGroup
00150 {
00151 public:
00152 DLGroup();
00153
00161 DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
00162
00169 DLGroup(const BigInteger &p, const BigInteger &g);
00170
00174 DLGroup(const DLGroup &from);
00175 ~DLGroup();
00176
00182 DLGroup & operator=(const DLGroup &from);
00183
00190 static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
00191
00195 bool isNull() const;
00196
00200 BigInteger p() const;
00201
00205 BigInteger q() const;
00206
00210 BigInteger g() const;
00211
00212 private:
00213 class Private;
00214 Private *d;
00215 };
00216
00223 class QCA_EXPORT PKey : public Algorithm
00224 {
00225 public:
00229 enum Type {
00230 RSA,
00231 DSA,
00232 DH
00233 };
00234
00235 PKey();
00236
00242 PKey(const PKey &from);
00243 ~PKey();
00244
00250 PKey & operator=(const PKey &from);
00251
00280 static QList<Type> supportedTypes(const QString &provider = QString());
00281
00308 static QList<Type> supportedIOTypes(const QString &provider = QString());
00309
00315 bool isNull() const;
00316
00322 Type type() const;
00323
00327 int bitSize() const;
00328
00332 bool isRSA() const;
00333
00337 bool isDSA() const;
00338
00342 bool isDH() const;
00343
00347 bool isPublic() const;
00348
00352 bool isPrivate() const;
00353
00358 bool canExport() const;
00359
00363 bool canKeyAgree() const;
00364
00371 PublicKey toPublicKey() const;
00372
00376 PrivateKey toPrivateKey() const;
00377
00381 bool operator==(const PKey &a) const;
00382
00386 bool operator!=(const PKey &a) const;
00387
00388 protected:
00392 PKey(const QString &type, const QString &provider);
00393
00397 void set(const PKey &k);
00398
00408 RSAPublicKey toRSAPublicKey() const;
00409
00419 RSAPrivateKey toRSAPrivateKey() const;
00420
00430 DSAPublicKey toDSAPublicKey() const;
00431
00441 DSAPrivateKey toDSAPrivateKey() const;
00442
00452 DHPublicKey toDHPublicKey() const;
00453
00463 DHPrivateKey toDHPrivateKey() const;
00464
00465 private:
00466 void assignToPublic(PKey *dest) const;
00467 void assignToPrivate(PKey *dest) const;
00468
00469 class Private;
00470 Private *d;
00471 };
00472
00478 class QCA_EXPORT PublicKey : public PKey
00479 {
00480 public:
00484 PublicKey();
00485
00491 PublicKey(const PrivateKey &k);
00492
00500 PublicKey(const QString &fileName);
00501
00507 PublicKey(const PublicKey &from);
00508
00509 ~PublicKey();
00510
00516 PublicKey & operator=(const PublicKey &from);
00517
00524 RSAPublicKey toRSA() const;
00525
00532 DSAPublicKey toDSA() const;
00533
00540 DHPublicKey toDH() const;
00541
00547 bool canEncrypt() const;
00548
00554 bool canVerify() const;
00555
00562 int maximumEncryptSize(EncryptionAlgorithm alg) const;
00563
00570 SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
00571
00578 void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00579
00585 void update(const MemoryRegion &a);
00586
00612 bool validSignature(const QByteArray &sig);
00613
00627 bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00628
00632 QByteArray toDER() const;
00633
00642 QString toPEM() const;
00643
00655 bool toPEMFile(const QString &fileName) const;
00656
00679 static PublicKey fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
00680
00706 static PublicKey fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
00707
00735 static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
00736
00737 protected:
00744 PublicKey(const QString &type, const QString &provider);
00745
00746 private:
00747 class Private;
00748 Private *d;
00749 };
00750
00756 class QCA_EXPORT PrivateKey : public PKey
00757 {
00758 public:
00762 PrivateKey();
00763
00775 explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
00776
00782 PrivateKey(const PrivateKey &from);
00783
00784 ~PrivateKey();
00785
00791 PrivateKey & operator=(const PrivateKey &from);
00792
00796 RSAPrivateKey toRSA() const;
00797
00801 DSAPrivateKey toDSA() const;
00802
00806 DHPrivateKey toDH() const;
00807
00813 bool canDecrypt() const;
00814
00820 bool canSign() const;
00821
00832 bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
00833
00843 void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00844
00853 void update(const MemoryRegion &a);
00854
00861 QByteArray signature();
00862
00875 QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00876
00882 SymmetricKey deriveKey(const PublicKey &theirs);
00883
00891 static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
00892
00903 SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00904
00917 QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00918
00935 bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00936
00955 static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
00956
00975 static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
00976
00999 static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
01000
01001 protected:
01009 PrivateKey(const QString &type, const QString &provider);
01010
01011 private:
01012 class Private;
01013 Private *d;
01014 };
01015
01024 class QCA_EXPORT KeyGenerator : public QObject
01025 {
01026 Q_OBJECT
01027 public:
01033 KeyGenerator(QObject *parent = 0);
01034
01035 ~KeyGenerator();
01036
01045 bool blockingEnabled() const;
01046
01055 void setBlockingEnabled(bool b);
01056
01062 bool isBusy() const;
01063
01080 PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
01081
01097 PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
01098
01113 PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
01114
01121 PrivateKey key() const;
01122
01131 DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
01132
01136 DLGroup dlGroup() const;
01137
01138 Q_SIGNALS:
01144 void finished();
01145
01146 private:
01147 Q_DISABLE_COPY(KeyGenerator)
01148
01149 class Private;
01150 friend class Private;
01151 Private *d;
01152 };
01153
01159 class QCA_EXPORT RSAPublicKey : public PublicKey
01160 {
01161 public:
01165 RSAPublicKey();
01166
01175 RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
01176
01182 RSAPublicKey(const RSAPrivateKey &k);
01183
01191 BigInteger n() const;
01192
01199 BigInteger e() const;
01200 };
01201
01207 class QCA_EXPORT RSAPrivateKey : public PrivateKey
01208 {
01209 public:
01213 RSAPrivateKey();
01214
01226 RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
01227
01235 BigInteger n() const;
01236
01243 BigInteger e() const;
01244
01248 BigInteger p() const;
01249
01254 BigInteger q() const;
01255
01259 BigInteger d() const;
01260 };
01261
01267 class QCA_EXPORT DSAPublicKey : public PublicKey
01268 {
01269 public:
01273 DSAPublicKey();
01274
01283 DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01284
01290 DSAPublicKey(const DSAPrivateKey &k);
01291
01295 DLGroup domain() const;
01296
01300 BigInteger y() const;
01301 };
01302
01308 class QCA_EXPORT DSAPrivateKey : public PrivateKey
01309 {
01310 public:
01314 DSAPrivateKey();
01315
01325 DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01326
01330 DLGroup domain() const;
01331
01335 BigInteger y() const;
01336
01340 BigInteger x() const;
01341 };
01342
01348 class QCA_EXPORT DHPublicKey : public PublicKey
01349 {
01350 public:
01354 DHPublicKey();
01355
01364 DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01365
01371 DHPublicKey(const DHPrivateKey &k);
01372
01376 DLGroup domain() const;
01377
01381 BigInteger y() const;
01382 };
01383
01389 class QCA_EXPORT DHPrivateKey : public PrivateKey
01390 {
01391 public:
01395 DHPrivateKey();
01396
01406 DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01407
01411 DLGroup domain() const;
01412
01416 BigInteger y() const;
01417
01421 BigInteger x() const;
01422 };
01423
01424 }
01425
01426 #endif