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
00151 class QCA_EXPORT DLGroup
00152 {
00153 public:
00154 DLGroup();
00155
00163 DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
00164
00171 DLGroup(const BigInteger &p, const BigInteger &g);
00172
00176 DLGroup(const DLGroup &from);
00177 ~DLGroup();
00178
00184 DLGroup & operator=(const DLGroup &from);
00185
00192 static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
00193
00197 bool isNull() const;
00198
00202 BigInteger p() const;
00203
00207 BigInteger q() const;
00208
00212 BigInteger g() const;
00213
00214 private:
00215 class Private;
00216 Private *d;
00217 };
00218
00228 class QCA_EXPORT PKey : public Algorithm
00229 {
00230 public:
00234 enum Type {
00235 RSA,
00236 DSA,
00237 DH
00238 };
00239
00240 PKey();
00241
00247 PKey(const PKey &from);
00248 ~PKey();
00249
00255 PKey & operator=(const PKey &from);
00256
00285 static QList<Type> supportedTypes(const QString &provider = QString());
00286
00313 static QList<Type> supportedIOTypes(const QString &provider = QString());
00314
00320 bool isNull() const;
00321
00327 Type type() const;
00328
00332 int bitSize() const;
00333
00337 bool isRSA() const;
00338
00342 bool isDSA() const;
00343
00347 bool isDH() const;
00348
00352 bool isPublic() const;
00353
00357 bool isPrivate() const;
00358
00363 bool canExport() const;
00364
00368 bool canKeyAgree() const;
00369
00376 PublicKey toPublicKey() const;
00377
00381 PrivateKey toPrivateKey() const;
00382
00386 bool operator==(const PKey &a) const;
00387
00391 bool operator!=(const PKey &a) const;
00392
00393 protected:
00397 PKey(const QString &type, const QString &provider);
00398
00402 void set(const PKey &k);
00403
00413 RSAPublicKey toRSAPublicKey() const;
00414
00424 RSAPrivateKey toRSAPrivateKey() const;
00425
00435 DSAPublicKey toDSAPublicKey() const;
00436
00446 DSAPrivateKey toDSAPrivateKey() const;
00447
00457 DHPublicKey toDHPublicKey() const;
00458
00468 DHPrivateKey toDHPrivateKey() const;
00469
00470 private:
00471 void assignToPublic(PKey *dest) const;
00472 void assignToPrivate(PKey *dest) const;
00473
00474 class Private;
00475 Private *d;
00476 };
00477
00486 class QCA_EXPORT PublicKey : public PKey
00487 {
00488 public:
00492 PublicKey();
00493
00499 PublicKey(const PrivateKey &k);
00500
00508 PublicKey(const QString &fileName);
00509
00515 PublicKey(const PublicKey &from);
00516
00517 ~PublicKey();
00518
00524 PublicKey & operator=(const PublicKey &from);
00525
00532 RSAPublicKey toRSA() const;
00533
00540 DSAPublicKey toDSA() const;
00541
00548 DHPublicKey toDH() const;
00549
00555 bool canEncrypt() const;
00556
00562 bool canVerify() const;
00563
00570 int maximumEncryptSize(EncryptionAlgorithm alg) const;
00571
00578 SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
00579
00586 void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00587
00593 void update(const MemoryRegion &a);
00594
00620 bool validSignature(const QByteArray &sig);
00621
00635 bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00636
00640 QByteArray toDER() const;
00641
00650 QString toPEM() const;
00651
00663 bool toPEMFile(const QString &fileName) const;
00664
00687 static PublicKey fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
00688
00714 static PublicKey fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
00715
00743 static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
00744
00745 protected:
00752 PublicKey(const QString &type, const QString &provider);
00753
00754 private:
00755 class Private;
00756 Private *d;
00757 };
00758
00767 class QCA_EXPORT PrivateKey : public PKey
00768 {
00769 public:
00773 PrivateKey();
00774
00786 explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
00787
00793 PrivateKey(const PrivateKey &from);
00794
00795 ~PrivateKey();
00796
00802 PrivateKey & operator=(const PrivateKey &from);
00803
00807 RSAPrivateKey toRSA() const;
00808
00812 DSAPrivateKey toDSA() const;
00813
00817 DHPrivateKey toDH() const;
00818
00824 bool canDecrypt() const;
00825
00831 bool canSign() const;
00832
00843 bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
00844
00854 void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00855
00864 void update(const MemoryRegion &a);
00865
00872 QByteArray signature();
00873
00886 QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00887
00893 SymmetricKey deriveKey(const PublicKey &theirs);
00894
00902 static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
00903
00914 SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00915
00928 QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00929
00946 bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00947
00966 static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
00967
00986 static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
00987
01010 static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
01011
01012 protected:
01020 PrivateKey(const QString &type, const QString &provider);
01021
01022 private:
01023 class Private;
01024 Private *d;
01025 };
01026
01038 class QCA_EXPORT KeyGenerator : public QObject
01039 {
01040 Q_OBJECT
01041 public:
01047 KeyGenerator(QObject *parent = 0);
01048
01049 ~KeyGenerator();
01050
01059 bool blockingEnabled() const;
01060
01069 void setBlockingEnabled(bool b);
01070
01076 bool isBusy() const;
01077
01094 PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
01095
01111 PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
01112
01127 PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
01128
01135 PrivateKey key() const;
01136
01145 DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
01146
01150 DLGroup dlGroup() const;
01151
01152 Q_SIGNALS:
01158 void finished();
01159
01160 private:
01161 Q_DISABLE_COPY(KeyGenerator)
01162
01163 class Private;
01164 friend class Private;
01165 Private *d;
01166 };
01167
01176 class QCA_EXPORT RSAPublicKey : public PublicKey
01177 {
01178 public:
01182 RSAPublicKey();
01183
01192 RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
01193
01199 RSAPublicKey(const RSAPrivateKey &k);
01200
01208 BigInteger n() const;
01209
01216 BigInteger e() const;
01217 };
01218
01227 class QCA_EXPORT RSAPrivateKey : public PrivateKey
01228 {
01229 public:
01233 RSAPrivateKey();
01234
01246 RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
01247
01255 BigInteger n() const;
01256
01263 BigInteger e() const;
01264
01268 BigInteger p() const;
01269
01274 BigInteger q() const;
01275
01279 BigInteger d() const;
01280 };
01281
01290 class QCA_EXPORT DSAPublicKey : public PublicKey
01291 {
01292 public:
01296 DSAPublicKey();
01297
01306 DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01307
01313 DSAPublicKey(const DSAPrivateKey &k);
01314
01318 DLGroup domain() const;
01319
01323 BigInteger y() const;
01324 };
01325
01334 class QCA_EXPORT DSAPrivateKey : public PrivateKey
01335 {
01336 public:
01340 DSAPrivateKey();
01341
01351 DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01352
01356 DLGroup domain() const;
01357
01361 BigInteger y() const;
01362
01366 BigInteger x() const;
01367 };
01368
01377 class QCA_EXPORT DHPublicKey : public PublicKey
01378 {
01379 public:
01383 DHPublicKey();
01384
01393 DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01394
01400 DHPublicKey(const DHPrivateKey &k);
01401
01405 DLGroup domain() const;
01406
01410 BigInteger y() const;
01411 };
01412
01421 class QCA_EXPORT DHPrivateKey : public PrivateKey
01422 {
01423 public:
01427 DHPrivateKey();
01428
01438 DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01439
01443 DLGroup domain() const;
01444
01448 BigInteger y() const;
01449
01453 BigInteger x() const;
01454 };
01456 }
01457
01458 #endif