00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00033 #ifndef QCA_PUBLICKEY_H
00034 #define QCA_PUBLICKEY_H
00035
00036 #include <QObject>
00037 #include "qca_core.h"
00038
00039 namespace QCA {
00040
00041 class PublicKey;
00042 class PrivateKey;
00043 class KeyGenerator;
00044 class RSAPublicKey;
00045 class RSAPrivateKey;
00046 class DSAPublicKey;
00047 class DSAPrivateKey;
00048 class DHPublicKey;
00049 class DHPrivateKey;
00050
00054 enum EncryptionAlgorithm
00055 {
00056 EME_PKCS1v15,
00057 EME_PKCS1_OAEP
00058 };
00059
00063 enum SignatureAlgorithm
00064 {
00065 SignatureUnknown,
00066 EMSA1_SHA1,
00067 EMSA3_SHA1,
00068 EMSA3_MD5,
00069 EMSA3_MD2,
00070 EMSA3_RIPEMD160,
00071 EMSA3_Raw,
00072 EMSA3_SHA224,
00073 EMSA3_SHA256,
00074 EMSA3_SHA384,
00075 EMSA3_SHA512
00076 };
00077
00081 enum SignatureFormat
00082 {
00083 DefaultFormat,
00084 IEEE_1363,
00085 DERSequence
00086 };
00087
00091 enum PBEAlgorithm
00092 {
00093 PBEDefault,
00094 PBES2_DES_SHA1,
00095 PBES2_TripleDES_SHA1,
00096 PBES2_AES128_SHA1,
00097 PBES2_AES192_SHA1,
00098 PBES2_AES256_SHA1
00099 };
00100
00107 enum ConvertResult
00108 {
00109 ConvertGood,
00110 ErrorDecode,
00111 ErrorPassphrase,
00112 ErrorFile
00113 };
00114
00123 enum DLGroupSet
00124 {
00125 DSA_512,
00126 DSA_768,
00127 DSA_1024,
00128 IETF_768,
00129 IETF_1024,
00130 IETF_1536,
00131 IETF_2048,
00132 IETF_3072,
00133 IETF_4096,
00134 IETF_6144,
00135 IETF_8192
00136
00137 };
00138
00151 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
00152
00160 class QCA_EXPORT DLGroup
00161 {
00162 public:
00163 DLGroup();
00164
00172 DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
00173
00180 DLGroup(const BigInteger &p, const BigInteger &g);
00181
00187 DLGroup(const DLGroup &from);
00188 ~DLGroup();
00189
00195 DLGroup & operator=(const DLGroup &from);
00196
00203 static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
00204
00208 bool isNull() const;
00209
00213 BigInteger p() const;
00214
00218 BigInteger q() const;
00219
00223 BigInteger g() const;
00224
00225 private:
00226 class Private;
00227 Private *d;
00228 };
00229
00239 class QCA_EXPORT PKey : public Algorithm
00240 {
00241 public:
00245 enum Type {
00246 RSA,
00247 DSA,
00248 DH
00249 };
00250
00254 PKey();
00255
00261 PKey(const PKey &from);
00262
00263 ~PKey();
00264
00270 PKey & operator=(const PKey &from);
00271
00303 static QList<Type> supportedTypes(const QString &provider = QString());
00304
00334 static QList<Type> supportedIOTypes(const QString &provider = QString());
00335
00341 bool isNull() const;
00342
00348 Type type() const;
00349
00353 int bitSize() const;
00354
00358 bool isRSA() const;
00359
00363 bool isDSA() const;
00364
00368 bool isDH() const;
00369
00373 bool isPublic() const;
00374
00378 bool isPrivate() const;
00379
00384 bool canExport() const;
00385
00389 bool canKeyAgree() const;
00390
00397 PublicKey toPublicKey() const;
00398
00402 PrivateKey toPrivateKey() const;
00403
00409 bool operator==(const PKey &a) const;
00410
00416 bool operator!=(const PKey &a) const;
00417
00418 protected:
00425 PKey(const QString &type, const QString &provider);
00426
00432 void set(const PKey &k);
00433
00443 RSAPublicKey toRSAPublicKey() const;
00444
00454 RSAPrivateKey toRSAPrivateKey() const;
00455
00465 DSAPublicKey toDSAPublicKey() const;
00466
00476 DSAPrivateKey toDSAPrivateKey() const;
00477
00487 DHPublicKey toDHPublicKey() const;
00488
00498 DHPrivateKey toDHPrivateKey() const;
00499
00500 private:
00501 void assignToPublic(PKey *dest) const;
00502 void assignToPrivate(PKey *dest) const;
00503
00504 class Private;
00505 Private *d;
00506 };
00507
00516 class QCA_EXPORT PublicKey : public PKey
00517 {
00518 public:
00522 PublicKey();
00523
00529 PublicKey(const PrivateKey &k);
00530
00538 PublicKey(const QString &fileName);
00539
00545 PublicKey(const PublicKey &from);
00546
00547 ~PublicKey();
00548
00554 PublicKey & operator=(const PublicKey &from);
00555
00562 RSAPublicKey toRSA() const;
00563
00570 DSAPublicKey toDSA() const;
00571
00578 DHPublicKey toDH() const;
00579
00585 bool canEncrypt() const;
00586
00592 bool canVerify() const;
00593
00600 int maximumEncryptSize(EncryptionAlgorithm alg) const;
00601
00608 SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
00609
00616 void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00617
00623 void update(const MemoryRegion &a);
00624
00650 bool validSignature(const QByteArray &sig);
00651
00665 bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00666
00670 QByteArray toDER() const;
00671
00680 QString toPEM() const;
00681
00693 bool toPEMFile(const QString &fileName) const;
00694
00717 static PublicKey fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
00718
00744 static PublicKey fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
00745
00773 static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
00774
00775 protected:
00782 PublicKey(const QString &type, const QString &provider);
00783
00784 private:
00785 class Private;
00786 Private *d;
00787 };
00788
00797 class QCA_EXPORT PrivateKey : public PKey
00798 {
00799 public:
00803 PrivateKey();
00804
00816 explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
00817
00823 PrivateKey(const PrivateKey &from);
00824
00825 ~PrivateKey();
00826
00832 PrivateKey & operator=(const PrivateKey &from);
00833
00837 RSAPrivateKey toRSA() const;
00838
00842 DSAPrivateKey toDSA() const;
00843
00847 DHPrivateKey toDH() const;
00848
00854 bool canDecrypt() const;
00855
00861 bool canSign() const;
00862
00873 bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
00874
00884 void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00885
00894 void update(const MemoryRegion &a);
00895
00902 QByteArray signature();
00903
00916 QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
00917
00923 SymmetricKey deriveKey(const PublicKey &theirs);
00924
00932 static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
00933
00944 SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00945
00958 QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00959
00976 bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
00977
00996 static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
00997
01016 static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
01017
01040 static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
01041
01042 protected:
01050 PrivateKey(const QString &type, const QString &provider);
01051
01052 private:
01053 class Private;
01054 Private *d;
01055 };
01056
01068 class QCA_EXPORT KeyGenerator : public QObject
01069 {
01070 Q_OBJECT
01071 public:
01077 KeyGenerator(QObject *parent = 0);
01078
01079 ~KeyGenerator();
01080
01089 bool blockingEnabled() const;
01090
01099 void setBlockingEnabled(bool b);
01100
01106 bool isBusy() const;
01107
01124 PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
01125
01141 PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
01142
01157 PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
01158
01165 PrivateKey key() const;
01166
01175 DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
01176
01180 DLGroup dlGroup() const;
01181
01182 Q_SIGNALS:
01188 void finished();
01189
01190 private:
01191 Q_DISABLE_COPY(KeyGenerator)
01192
01193 class Private;
01194 friend class Private;
01195 Private *d;
01196 };
01197
01206 class QCA_EXPORT RSAPublicKey : public PublicKey
01207 {
01208 public:
01212 RSAPublicKey();
01213
01222 RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
01223
01229 RSAPublicKey(const RSAPrivateKey &k);
01230
01238 BigInteger n() const;
01239
01246 BigInteger e() const;
01247 };
01248
01257 class QCA_EXPORT RSAPrivateKey : public PrivateKey
01258 {
01259 public:
01263 RSAPrivateKey();
01264
01276 RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
01277
01285 BigInteger n() const;
01286
01293 BigInteger e() const;
01294
01298 BigInteger p() const;
01299
01304 BigInteger q() const;
01305
01309 BigInteger d() const;
01310 };
01311
01320 class QCA_EXPORT DSAPublicKey : public PublicKey
01321 {
01322 public:
01326 DSAPublicKey();
01327
01336 DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01337
01343 DSAPublicKey(const DSAPrivateKey &k);
01344
01348 DLGroup domain() const;
01349
01353 BigInteger y() const;
01354 };
01355
01364 class QCA_EXPORT DSAPrivateKey : public PrivateKey
01365 {
01366 public:
01370 DSAPrivateKey();
01371
01381 DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01382
01386 DLGroup domain() const;
01387
01391 BigInteger y() const;
01392
01396 BigInteger x() const;
01397 };
01398
01407 class QCA_EXPORT DHPublicKey : public PublicKey
01408 {
01409 public:
01413 DHPublicKey();
01414
01423 DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
01424
01430 DHPublicKey(const DHPrivateKey &k);
01431
01435 DLGroup domain() const;
01436
01440 BigInteger y() const;
01441 };
01442
01451 class QCA_EXPORT DHPrivateKey : public PrivateKey
01452 {
01453 public:
01457 DHPrivateKey();
01458
01468 DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
01469
01473 DLGroup domain() const;
01474
01478 BigInteger y() const;
01479
01483 BigInteger x() const;
01484 };
01486 }
01487
01488 #endif