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
00146 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
00147
00155 class QCA_EXPORT DLGroup
00156 {
00157 public:
00158 DLGroup();
00159
00167 DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
00168
00175 DLGroup(const BigInteger &p, const BigInteger &g);
00176
00182 DLGroup(const DLGroup &from);
00183 ~DLGroup();
00184
00190 DLGroup & operator=(const DLGroup &from);
00191
00198 static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
00199
00203 bool isNull() const;
00204
00208 BigInteger p() const;
00209
00213 BigInteger q() const;
00214
00218 BigInteger g() const;
00219
00220 private:
00221 class Private;
00222 Private *d;
00223 };
00224
00234 class QCA_EXPORT PKey : public Algorithm
00235 {
00236 public:
00240 enum Type {
00241 RSA,
00242 DSA,
00243 DH
00244 };
00245
00249 PKey();
00250
00256 PKey(const PKey &from);
00257
00258 ~PKey();
00259
00265 PKey & operator=(const PKey &from);
00266
00298 static QList<Type> supportedTypes(const QString &provider = QString());
00299
00329 static QList<Type> supportedIOTypes(const QString &provider = QString());
00330
00336 bool isNull() const;
00337
00343 Type type() const;
00344
00348 int bitSize() const;
00349
00353 bool isRSA() const;
00354
00358 bool isDSA() const;
00359
00363 bool isDH() const;
00364
00368 bool isPublic() const;
00369
00373 bool isPrivate() const;
00374
00379 bool canExport() const;
00380
00384 bool canKeyAgree() const;
00385
00392 PublicKey toPublicKey() const;
00393
00397 PrivateKey toPrivateKey() const;
00398
00404 bool operator==(const PKey &a) const;
00405
00411 bool operator!=(const PKey &a) const;
00412
00413 protected:
00420 PKey(const QString &type, const QString &provider);
00421
00427 void set(const PKey &k);
00428
00438 RSAPublicKey toRSAPublicKey() const;
00439
00449 RSAPrivateKey toRSAPrivateKey() const;
00450
00460 DSAPublicKey toDSAPublicKey() const;
00461
00471 DSAPrivateKey toDSAPrivateKey() const;
00472
00482 DHPublicKey toDHPublicKey() const;
00483
00493 DHPrivateKey toDHPrivateKey() const;
00494
00495 private:
00496 void assignToPublic(PKey *dest) const;
00497 void assignToPrivate(PKey *dest) const;
00498
00499 class Private;
00500 Private *d;
00501 };
00502
00511 class QCA_EXPORT PublicKey : public PKey
00512 {
00513 public:
00517 PublicKey();
00518
00524 PublicKey(const PrivateKey &k);
00525
00533 PublicKey(const QString &fileName);
00534
00540 PublicKey(const PublicKey &from);
00541
00542 ~PublicKey();
00543
00549 PublicKey & operator=(const PublicKey &from);
00550
00557 RSAPublicKey toRSA() const;
00558
00565 DSAPublicKey toDSA() const;
00566
00573 DHPublicKey toDH() const;
00574
00580 bool canEncrypt() const;
00581
00587 bool canVerify() const;
00588
00595 int maximumEncryptSize(EncryptionAlgorithm alg) const;
00596
00603 SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
00604
00611 void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00612
00618 void update(const MemoryRegion &a);
00619
00645 bool validSignature(const QByteArray &sig);
00646
00660 bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00661
00665 QByteArray toDER() const;
00666
00675 QString toPEM() const;
00676
00688 bool toPEMFile(const QString &fileName) const;
00689
00712 static PublicKey fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
00713
00739 static PublicKey fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
00740
00768 static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
00769
00770 protected:
00777 PublicKey(const QString &type, const QString &provider);
00778
00779 private:
00780 class Private;
00781 Private *d;
00782 };
00783
00792 class QCA_EXPORT PrivateKey : public PKey
00793 {
00794 public:
00798 PrivateKey();
00799
00811 explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
00812
00818 PrivateKey(const PrivateKey &from);
00819
00820 ~PrivateKey();
00821
00827 PrivateKey & operator=(const PrivateKey &from);
00828
00832 RSAPrivateKey toRSA() const;
00833
00837 DSAPrivateKey toDSA() const;
00838
00842 DHPrivateKey toDH() const;
00843
00849 bool canDecrypt() const;
00850
00856 bool canSign() const;
00857
00868 bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
00869
00879 void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00880
00889 void update(const MemoryRegion &a);
00890
00897 QByteArray signature();
00898
00911 QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00912
00918 SymmetricKey deriveKey(const PublicKey &theirs);
00919
00927 static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
00928
00939 SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00940
00953 QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00954
00971 bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00972
00991 static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
00992
01011 static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
01012
01035 static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
01036
01037 protected:
01045 PrivateKey(const QString &type, const QString &provider);
01046
01047 private:
01048 class Private;
01049 Private *d;
01050 };
01051
01063 class QCA_EXPORT KeyGenerator : public QObject
01064 {
01065 Q_OBJECT
01066 public:
01072 KeyGenerator(QObject *parent = 0);
01073
01074 ~KeyGenerator();
01075
01084 bool blockingEnabled() const;
01085
01094 void setBlockingEnabled(bool b);
01095
01101 bool isBusy() const;
01102
01119 PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
01120
01136 PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
01137
01152 PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
01153
01160 PrivateKey key() const;
01161
01170 DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
01171
01175 DLGroup dlGroup() const;
01176
01177 Q_SIGNALS:
01183 void finished();
01184
01185 private:
01186 Q_DISABLE_COPY(KeyGenerator)
01187
01188 class Private;
01189 friend class Private;
01190 Private *d;
01191 };
01192
01201 class QCA_EXPORT RSAPublicKey : public PublicKey
01202 {
01203 public:
01207 RSAPublicKey();
01208
01217 RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
01218
01224 RSAPublicKey(const RSAPrivateKey &k);
01225
01233 BigInteger n() const;
01234
01241 BigInteger e() const;
01242 };
01243
01252 class QCA_EXPORT RSAPrivateKey : public PrivateKey
01253 {
01254 public:
01258 RSAPrivateKey();
01259
01271 RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
01272
01280 BigInteger n() const;
01281
01288 BigInteger e() const;
01289
01293 BigInteger p() const;
01294
01299 BigInteger q() const;
01300
01304 BigInteger d() const;
01305 };
01306
01315 class QCA_EXPORT DSAPublicKey : public PublicKey
01316 {
01317 public:
01321 DSAPublicKey();
01322
01331 DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01332
01338 DSAPublicKey(const DSAPrivateKey &k);
01339
01343 DLGroup domain() const;
01344
01348 BigInteger y() const;
01349 };
01350
01359 class QCA_EXPORT DSAPrivateKey : public PrivateKey
01360 {
01361 public:
01365 DSAPrivateKey();
01366
01376 DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01377
01381 DLGroup domain() const;
01382
01386 BigInteger y() const;
01387
01391 BigInteger x() const;
01392 };
01393
01402 class QCA_EXPORT DHPublicKey : public PublicKey
01403 {
01404 public:
01408 DHPublicKey();
01409
01418 DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01419
01425 DHPublicKey(const DHPrivateKey &k);
01426
01430 DLGroup domain() const;
01431
01435 BigInteger y() const;
01436 };
01437
01446 class QCA_EXPORT DHPrivateKey : public PrivateKey
01447 {
01448 public:
01452 DHPrivateKey();
01453
01463 DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01464
01468 DLGroup domain() const;
01469
01473 BigInteger y() const;
01474
01478 BigInteger x() const;
01479 };
01481 }
01482
01483 #endif