Qt Cryptographic Architecture: qca_publickey.h Source File

# qca_publickey.h

Go to the documentation of this file.

    00001 /*
    00002  * qca_publickey.h - Qt Cryptographic Architecture
    00003  * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
    00004  * Copyright (C) 2004,2005  Brad Hards <bradh@frogmouth.net>
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the GNU Lesser General Public
    00008  * License as published by the Free Software Foundation; either
    00009  * version 2.1 of the License, or (at your option) any later version.
    00010  *
    00011  * This library is distributed in the hope that it will be useful,
    00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014  * Lesser General Public License for more details.
    00015  *
    00016  * You should have received a copy of the GNU Lesser General Public
    00017  * License along with this library; if not, write to the Free Software
    00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    00019  * 02110-1301  USA
    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 };
    00073
    00077 enum SignatureFormat
    00078 {
    00079         DefaultFormat,
    00080         IEEE_1363,
    00081         DERSequence
    00082 };
    00083
    00087 enum PBEAlgorithm
    00088 {
    00089         PBEDefault,
    00090         PBES2_DES_SHA1,
    00091         PBES2_TripleDES_SHA1,
    00092         PBES2_AES128_SHA1,
    00093         PBES2_AES192_SHA1,
    00094         PBES2_AES256_SHA1
    00095 };
    00096
    00103 enum ConvertResult
    00104 {
    00105         ConvertGood,
    00106         ErrorDecode,
    00107         ErrorPassphrase,
    00108         ErrorFile
    00109 };
    00110
    00119 enum DLGroupSet
    00120 {
    00121         DSA_512,
    00122         DSA_768,
    00123         DSA_1024,
    00124         IETF_768,
    00125         IETF_1024,
    00126         IETF_1536,
    00127         IETF_2048,
    00128         IETF_3072,
    00129         IETF_4096,
    00130         IETF_6144,
    00131         IETF_8192
    00132
    00133 };
    00134
    00147 QCA_EXPORT QByteArray emsa3Encode(const QString &hashName, const QByteArray &digest, int size = -1);
    00148
    00156 class QCA_EXPORT DLGroup
    00157 {
    00158 public:
    00159         DLGroup();
    00160
    00168         DLGroup(const BigInteger &p, const BigInteger &q, const BigInteger &g);
    00169
    00176         DLGroup(const BigInteger &p, const BigInteger &g);
    00177
    00183         DLGroup(const DLGroup &from);
    00184         ~DLGroup();
    00185
    00191         DLGroup & operator=(const DLGroup &from);
    00192
    00199         static QList<DLGroupSet> supportedGroupSets(const QString &provider = QString());
    00200
    00204         bool isNull() const;
    00205
    00209         BigInteger p() const;
    00210
    00214         BigInteger q() const;
    00215
    00219         BigInteger g() const;
    00220
    00221 private:
    00222         class Private;
    00223         Private *d;
    00224 };
    00225
    00235 class QCA_EXPORT PKey : public Algorithm
    00236 {
    00237 public:
    00241         enum Type {
    00242                 RSA,
    00243                 DSA,
    00244                 DH
    00245         };
    00246
    00250         PKey();
    00251
    00257         PKey(const PKey &from);
    00258
    00259         ~PKey();
    00260
    00266         PKey & operator=(const PKey &from);
    00267
    00299         static QList<Type> supportedTypes(const QString &provider = QString());
    00300
    00330         static QList<Type> supportedIOTypes(const QString &provider = QString());
    00331
    00337         bool isNull() const;
    00338
    00344         Type type() const;
    00345
    00349         int bitSize() const;
    00350
    00354         bool isRSA() const;
    00355
    00359         bool isDSA() const;
    00360
    00364         bool isDH() const;
    00365
    00369         bool isPublic() const;
    00370
    00374         bool isPrivate() const;
    00375
    00380         bool canExport() const;
    00381
    00385         bool canKeyAgree() const;
    00386
    00393         PublicKey toPublicKey() const;
    00394
    00398         PrivateKey toPrivateKey() const;
    00399
    00405         bool operator==(const PKey &a) const;
    00406
    00412         bool operator!=(const PKey &a) const;
    00413
    00414 protected:
    00421         PKey(const QString &type, const QString &provider);
    00422
    00428         void set(const PKey &k);
    00429
    00439         RSAPublicKey toRSAPublicKey() const;
    00440
    00450         RSAPrivateKey toRSAPrivateKey() const;
    00451
    00461         DSAPublicKey toDSAPublicKey() const;
    00462
    00472         DSAPrivateKey toDSAPrivateKey() const;
    00473
    00483         DHPublicKey toDHPublicKey() const;
    00484
    00494         DHPrivateKey toDHPrivateKey() const;
    00495
    00496 private:
    00497         void assignToPublic(PKey *dest) const;
    00498         void assignToPrivate(PKey *dest) const;
    00499
    00500         class Private;
    00501         Private *d;
    00502 };
    00503
    00512 class QCA_EXPORT PublicKey : public PKey
    00513 {
    00514 public:
    00518         PublicKey();
    00519
    00525         PublicKey(const PrivateKey &k);
    00526
    00534         PublicKey(const QString &fileName);
    00535
    00541         PublicKey(const PublicKey &from);
    00542
    00543         ~PublicKey();
    00544
    00550         PublicKey & operator=(const PublicKey &from);
    00551
    00558         RSAPublicKey toRSA() const;
    00559
    00566         DSAPublicKey toDSA() const;
    00567
    00574         DHPublicKey toDH() const;
    00575
    00581         bool canEncrypt() const;
    00582
    00588         bool canVerify() const;
    00589
    00596         int maximumEncryptSize(EncryptionAlgorithm alg) const;
    00597
    00604         SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg);
    00605
    00612         void startVerify(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
    00613
    00619         void update(const MemoryRegion &a);
    00620
    00646         bool validSignature(const QByteArray &sig);
    00647
    00661         bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
    00662
    00666         QByteArray toDER() const;
    00667
    00676         QString toPEM() const;
    00677
    00689         bool toPEMFile(const QString &fileName) const;
    00690
    00713         static PublicKey fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
    00714
    00740         static PublicKey fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
    00741
    00769         static PublicKey fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
    00770
    00771 protected:
    00778         PublicKey(const QString &type, const QString &provider);
    00779
    00780 private:
    00781         class Private;
    00782         Private *d;
    00783 };
    00784
    00793 class QCA_EXPORT PrivateKey : public PKey
    00794 {
    00795 public:
    00799         PrivateKey();
    00800
    00812         explicit PrivateKey(const QString &fileName, const SecureArray &passphrase = SecureArray());
    00813
    00819         PrivateKey(const PrivateKey &from);
    00820
    00821         ~PrivateKey();
    00822
    00828         PrivateKey & operator=(const PrivateKey &from);
    00829
    00833         RSAPrivateKey toRSA() const;
    00834
    00838         DSAPrivateKey toDSA() const;
    00839
    00843         DHPrivateKey toDH() const;
    00844
    00850         bool canDecrypt() const;
    00851
    00857         bool canSign() const;
    00858
    00869         bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg);
    00870
    00880         void startSign(SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
    00881
    00890         void update(const MemoryRegion &a);
    00891
    00898         QByteArray signature();
    00899
    00912         QByteArray signMessage(const MemoryRegion &a, SignatureAlgorithm alg, SignatureFormat format = DefaultFormat);
    00913
    00919         SymmetricKey deriveKey(const PublicKey &theirs);
    00920
    00928         static QList<PBEAlgorithm> supportedPBEAlgorithms(const QString &provider = QString());
    00929
    00940         SecureArray toDER(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
    00941
    00954         QString toPEM(const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
    00955
    00972         bool toPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), PBEAlgorithm pbe = PBEDefault) const;
    00973
    00992         static PrivateKey fromDER(const SecureArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
    00993
    01012         static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
    01013
    01036         static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
    01037
    01038 protected:
    01046         PrivateKey(const QString &type, const QString &provider);
    01047
    01048 private:
    01049         class Private;
    01050         Private *d;
    01051 };
    01052
    01064 class QCA_EXPORT KeyGenerator : public QObject
    01065 {
    01066         Q_OBJECT
    01067 public:
    01073         KeyGenerator(QObject *parent = 0);
    01074
    01075         ~KeyGenerator();
    01076
    01085         bool blockingEnabled() const;
    01086
    01095         void setBlockingEnabled(bool b);
    01096
    01102         bool isBusy() const;
    01103
    01120         PrivateKey createRSA(int bits, int exp = 65537, const QString &provider = QString());
    01121
    01137         PrivateKey createDSA(const DLGroup &domain, const QString &provider = QString());
    01138
    01153         PrivateKey createDH(const DLGroup &domain, const QString &provider = QString());
    01154
    01161         PrivateKey key() const;
    01162
    01171         DLGroup createDLGroup(QCA::DLGroupSet set, const QString &provider = QString());
    01172
    01176         DLGroup dlGroup() const;
    01177
    01178 Q_SIGNALS:
    01184         void finished();
    01185
    01186 private:
    01187         Q_DISABLE_COPY(KeyGenerator)
    01188
    01189         class Private;
    01190         friend class Private;
    01191         Private *d;
    01192 };
    01193
    01202 class QCA_EXPORT RSAPublicKey : public PublicKey
    01203 {
    01204 public:
    01208         RSAPublicKey();
    01209
    01218         RSAPublicKey(const BigInteger &n, const BigInteger &e, const QString &provider = QString());
    01219
    01225         RSAPublicKey(const RSAPrivateKey &k);
    01226
    01234         BigInteger n() const;
    01235
    01242         BigInteger e() const;
    01243 };
    01244
    01253 class QCA_EXPORT RSAPrivateKey : public PrivateKey
    01254 {
    01255 public:
    01259         RSAPrivateKey();
    01260
    01272         RSAPrivateKey(const BigInteger &n, const BigInteger &e, const BigInteger &p, const BigInteger &q, const BigInteger &d, const QString &provider = QString());
    01273
    01281         BigInteger n() const;
    01282
    01289         BigInteger e() const;
    01290
    01294         BigInteger p() const;
    01295
    01300         BigInteger q() const;
    01301
    01305         BigInteger d() const;
    01306 };
    01307
    01316 class QCA_EXPORT DSAPublicKey : public PublicKey
    01317 {
    01318 public:
    01322         DSAPublicKey();
    01323
    01332         DSAPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
    01333
    01339         DSAPublicKey(const DSAPrivateKey &k);
    01340
    01344         DLGroup domain() const;
    01345
    01349         BigInteger y() const;
    01350 };
    01351
    01360 class QCA_EXPORT DSAPrivateKey : public PrivateKey
    01361 {
    01362 public:
    01366         DSAPrivateKey();
    01367
    01377         DSAPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
    01378
    01382         DLGroup domain() const;
    01383
    01387         BigInteger y() const;
    01388
    01392         BigInteger x() const;
    01393 };
    01394
    01403 class QCA_EXPORT DHPublicKey : public PublicKey
    01404 {
    01405 public:
    01409         DHPublicKey();
    01410
    01419         DHPublicKey(const DLGroup &domain, const BigInteger &y, const QString &provider = QString());
    01420
    01426         DHPublicKey(const DHPrivateKey &k);
    01427
    01431         DLGroup domain() const;
    01432
    01436         BigInteger y() const;
    01437 };
    01438
    01447 class QCA_EXPORT DHPrivateKey : public PrivateKey
    01448 {
    01449 public:
    01453         DHPrivateKey();
    01454
    01464         DHPrivateKey(const DLGroup &domain, const BigInteger &y, const BigInteger &x, const QString &provider = QString());
    01465
    01469         DLGroup domain() const;
    01470
    01474         BigInteger y() const;
    01475
    01479         BigInteger x() const;
    01480 };
    01482 }
    01483
    01484 #endif