00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00032 #ifndef QCA_CERT_H
00033 #define QCA_CERT_H
00034
00035 #include <QMap>
00036 #include <QDateTime>
00037 #include "qca_core.h"
00038 #include "qca_publickey.h"
00039
00040 namespace QCA {
00041
00042 class CertContext;
00043 class CSRContext;
00044 class CRLContext;
00045 class Certificate;
00046 class CRL;
00047 class CertificateCollection;
00048 class CertificateChain;
00049
00050
00054 enum CertificateRequestFormat
00055 {
00056 PKCS10,
00057 SPKAC
00058 };
00059
00065 enum CertificateInfoTypeKnown
00066 {
00067 CommonName,
00068 Email,
00069 EmailLegacy,
00070 Organization,
00071 OrganizationalUnit,
00072 Locality,
00073 IncorporationLocality,
00074 State,
00075 IncorporationState,
00076 Country,
00077 IncorporationCountry,
00078 URI,
00079 DNS,
00080 IPAddress,
00081 XMPP
00082 };
00083
00120 class QCA_EXPORT CertificateInfoType
00121 {
00122 public:
00126 enum Section
00127 {
00128 DN,
00129 AlternativeName
00130 };
00131
00135 CertificateInfoType();
00136
00145 CertificateInfoType(CertificateInfoTypeKnown known);
00146
00155 CertificateInfoType(const QString &id, Section section);
00156
00160 CertificateInfoType(const CertificateInfoType &from);
00161
00162 ~CertificateInfoType();
00163
00167 CertificateInfoType & operator=(const CertificateInfoType &from);
00168
00172 Section section() const;
00173
00181 CertificateInfoTypeKnown known() const;
00182
00201 QString id() const;
00202
00206 bool operator<(const CertificateInfoType &other) const;
00207
00211 bool operator==(const CertificateInfoType &other) const;
00212
00216 inline bool operator!=(const CertificateInfoType &other) const
00217 {
00218 return !(*this == other);
00219 }
00220
00221 private:
00222 class Private;
00223 QSharedDataPointer<Private> d;
00224 };
00225
00233 class QCA_EXPORT CertificateInfoPair
00234 {
00235 public:
00239 CertificateInfoPair();
00240
00247 CertificateInfoPair(const CertificateInfoType &type, const QString &value);
00248
00252 CertificateInfoPair(const CertificateInfoPair &from);
00253
00254 ~CertificateInfoPair();
00255
00259 CertificateInfoPair & operator=(const CertificateInfoPair &from);
00260
00264 CertificateInfoType type() const;
00265
00269 QString value() const;
00270
00274 bool operator==(const CertificateInfoPair &other) const;
00275
00279 inline bool operator!=(const CertificateInfoPair &other) const
00280 {
00281 return !(*this == other);
00282 }
00283
00284 private:
00285 class Private;
00286 QSharedDataPointer<Private> d;
00287 };
00288
00289
00295 enum ConstraintTypeKnown
00296 {
00297
00298 DigitalSignature,
00299 NonRepudiation,
00300 KeyEncipherment,
00301 DataEncipherment,
00302 KeyAgreement,
00303 KeyCertificateSign,
00304 CRLSign,
00305 EncipherOnly,
00306 DecipherOnly,
00307
00308
00309 ServerAuth,
00310 ClientAuth,
00311 CodeSigning,
00312 EmailProtection,
00313 IPSecEndSystem,
00314 IPSecTunnel,
00315 IPSecUser,
00316 TimeStamping,
00317 OCSPSigning
00318 };
00319
00333 class QCA_EXPORT ConstraintType
00334 {
00335 public:
00339 enum Section
00340 {
00341 KeyUsage,
00342 ExtendedKeyUsage
00343 };
00344
00348 ConstraintType();
00349
00358 ConstraintType(ConstraintTypeKnown known);
00359
00368 ConstraintType(const QString &id, Section section);
00369
00373 ConstraintType(const ConstraintType &from);
00374
00375 ~ConstraintType();
00376
00380 ConstraintType & operator=(const ConstraintType &from);
00381
00385 Section section() const;
00386
00394 ConstraintTypeKnown known() const;
00395
00414 QString id() const;
00415
00419 bool operator<(const ConstraintType &other) const;
00420
00424 bool operator==(const ConstraintType &other) const;
00425
00429 inline bool operator!=(const ConstraintType &other) const
00430 {
00431 return !(*this == other);
00432 }
00433
00434 private:
00435 class Private;
00436 QSharedDataPointer<Private> d;
00437 };
00438
00442 enum UsageMode
00443 {
00444 UsageAny = 0x00,
00445 UsageTLSServer = 0x01,
00446 UsageTLSClient = 0x02,
00447 UsageCodeSigning = 0x04,
00448 UsageEmailProtection = 0x08,
00449 UsageTimeStamping = 0x10,
00450 UsageCRLSigning = 0x20
00451 };
00452
00456 enum Validity
00457 {
00458 ValidityGood,
00459 ErrorRejected,
00460 ErrorUntrusted,
00461 ErrorSignatureFailed,
00462 ErrorInvalidCA,
00463 ErrorInvalidPurpose,
00464 ErrorSelfSigned,
00465 ErrorRevoked,
00466 ErrorPathLengthExceeded,
00467 ErrorExpired,
00468 ErrorExpiredCA,
00469 ErrorValidityUnknown = 64
00470 };
00471
00475 enum ValidateFlags
00476 {
00477 ValidateAll = 0x00,
00478 ValidateRevoked = 0x01,
00479 ValidateExpired = 0x02,
00480 ValidatePolicy = 0x04
00481 };
00482
00494 typedef QMultiMap<CertificateInfoType, QString> CertificateInfo;
00495
00506 class CertificateInfoOrdered : public QList<CertificateInfoPair>
00507 {
00508 public:
00512 inline QString toString() const;
00513
00518 inline CertificateInfoOrdered dnOnly() const;
00519 };
00520
00524 QCA_EXPORT QString orderedToDNString(const CertificateInfoOrdered &in);
00525
00530 QCA_EXPORT CertificateInfoOrdered orderedDNOnly(const CertificateInfoOrdered &in);
00531
00532 inline QString CertificateInfoOrdered::toString() const
00533 {
00534 return orderedToDNString(*this);
00535 }
00536
00537 inline CertificateInfoOrdered CertificateInfoOrdered::dnOnly() const
00538 {
00539 return orderedDNOnly(*this);
00540 }
00541
00545 typedef QList<ConstraintType> Constraints;
00546
00550 QCA_EXPORT QStringList makeFriendlyNames(const QList<Certificate> &list);
00551
00561 class QCA_EXPORT CertificateOptions
00562 {
00563 public:
00569 CertificateOptions(CertificateRequestFormat format = PKCS10);
00570
00576 CertificateOptions(const CertificateOptions &from);
00577 ~CertificateOptions();
00578
00584 CertificateOptions & operator=(const CertificateOptions &from);
00585
00589 CertificateRequestFormat format() const;
00590
00596 void setFormat(CertificateRequestFormat f);
00597
00603 bool isValid() const;
00604
00612 QString challenge() const;
00613
00619 CertificateInfo info() const;
00620
00627 CertificateInfoOrdered infoOrdered() const;
00628
00632 Constraints constraints() const;
00633
00637 QStringList policies() const;
00638
00646 QStringList crlLocations() const;
00647
00655 QStringList issuerLocations() const;
00656
00662 QStringList ocspLocations() const;
00663
00670 bool isCA() const;
00671
00675 int pathLimit() const;
00676
00682 BigInteger serialNumber() const;
00683
00689 QDateTime notValidBefore() const;
00690
00696 QDateTime notValidAfter() const;
00697
00706 void setChallenge(const QString &s);
00707
00716 void setInfo(const CertificateInfo &info);
00717
00726 void setInfoOrdered(const CertificateInfoOrdered &info);
00727
00733 void setConstraints(const Constraints &constraints);
00734
00740 void setPolicies(const QStringList &policies);
00741
00749 void setCRLLocations(const QStringList &locations);
00750
00758 void setIssuerLocations(const QStringList &locations);
00759
00765 void setOCSPLocations(const QStringList &locations);
00766
00772 void setAsCA(int pathLimit = 8);
00773
00777 void setAsUser();
00778
00784 void setSerialNumber(const BigInteger &i);
00785
00792 void setValidityPeriod(const QDateTime &start, const QDateTime &end);
00793
00794 private:
00795 class Private;
00796 Private *d;
00797 };
00798
00808 class QCA_EXPORT Certificate : public Algorithm
00809 {
00810 public:
00814 Certificate();
00815
00822 Certificate(const QString &fileName);
00823
00833 Certificate(const CertificateOptions &opts, const PrivateKey &key, const QString &provider = QString());
00834
00838 Certificate(const Certificate &from);
00839
00840 ~Certificate();
00841
00845 Certificate & operator=(const Certificate &from);
00846
00851 bool isNull() const;
00852
00856 QDateTime notValidBefore() const;
00857
00861 QDateTime notValidAfter() const;
00862
00880 CertificateInfo subjectInfo() const;
00881
00901 CertificateInfoOrdered subjectInfoOrdered() const;
00902
00908 CertificateInfo issuerInfo() const;
00909
00922 CertificateInfoOrdered issuerInfoOrdered() const;
00923
00927 Constraints constraints() const;
00928
00934 QStringList policies() const;
00935
00941 QStringList crlLocations() const;
00942
00948 QStringList issuerLocations() const;
00949
00953 QStringList ocspLocations() const;
00954
00961 QString commonName() const;
00962
00966 BigInteger serialNumber() const;
00967
00971 PublicKey subjectPublicKey() const;
00972
00978 bool isCA() const;
00979
00985 bool isSelfSigned() const;
00986
00993 bool isIssuerOf(const Certificate &other) const;
00994
00999 int pathLimit() const;
01000
01004 SignatureAlgorithm signatureAlgorithm() const;
01005
01009 QByteArray subjectKeyId() const;
01010
01014 QByteArray issuerKeyId() const;
01015
01027 Validity validate(const CertificateCollection &trusted, const CertificateCollection &untrusted, UsageMode u = UsageAny, ValidateFlags vf = ValidateAll) const;
01028
01032 QByteArray toDER() const;
01033
01037 QString toPEM() const;
01038
01044 bool toPEMFile(const QString &fileName) const;
01045
01058 static Certificate fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
01059
01072 static Certificate fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
01073
01087 static Certificate fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
01088
01101 bool matchesHostName(const QString &host) const;
01102
01108 bool operator==(const Certificate &a) const;
01109
01113 inline bool operator!=(const Certificate &other) const
01114 {
01115 return !(*this == other);
01116 }
01117
01121 void change(CertContext *c);
01122
01123 private:
01124 class Private;
01125 friend class Private;
01126 QSharedDataPointer<Private> d;
01127
01128 friend class CertificateChain;
01129 Validity chain_validate(const CertificateChain &chain, const CertificateCollection &trusted, const QList<CRL> &untrusted_crls, UsageMode u, ValidateFlags vf) const;
01130 CertificateChain chain_complete(const CertificateChain &chain, const QList<Certificate> &issuers, Validity *result) const;
01131 };
01132
01155 class CertificateChain : public QList<Certificate>
01156 {
01157 public:
01161 inline CertificateChain() {}
01162
01169 inline CertificateChain(const Certificate &primary) { append(primary); }
01170
01174 inline const Certificate & primary() const { return first(); }
01175
01189 inline Validity validate(const CertificateCollection &trusted, const QList<CRL> &untrusted_crls = QList<CRL>(), UsageMode u = UsageAny, ValidateFlags vf = ValidateAll) const;
01190
01214 inline CertificateChain complete(const QList<Certificate> &issuers = QList<Certificate>(), Validity *result = 0) const;
01215 };
01216
01217 inline Validity CertificateChain::validate(const CertificateCollection &trusted, const QList<CRL> &untrusted_crls, UsageMode u, ValidateFlags vf) const
01218 {
01219 if(isEmpty())
01220 return ErrorValidityUnknown;
01221 return first().chain_validate(*this, trusted, untrusted_crls, u, vf);
01222 }
01223
01224 inline CertificateChain CertificateChain::complete(const QList<Certificate> &issuers, Validity *result) const
01225 {
01226 if(isEmpty())
01227 return CertificateChain();
01228 return first().chain_complete(*this, issuers, result);
01229 }
01230
01240 class QCA_EXPORT CertificateRequest : public Algorithm
01241 {
01242 public:
01246 CertificateRequest();
01247
01254 CertificateRequest(const QString &fileName);
01255
01265 CertificateRequest(const CertificateOptions &opts, const PrivateKey &key, const QString &provider = QString());
01266
01270 CertificateRequest(const CertificateRequest &from);
01271
01272 ~CertificateRequest();
01273
01277 CertificateRequest & operator=(const CertificateRequest &from);
01278
01284 bool isNull() const;
01285
01296 static bool canUseFormat(CertificateRequestFormat f, const QString &provider = QString());
01297
01301 CertificateRequestFormat format() const;
01302
01311 CertificateInfo subjectInfo() const;
01312
01323 CertificateInfoOrdered subjectInfoOrdered() const;
01324
01330 Constraints constraints() const;
01331
01337 QStringList policies() const;
01338
01342 PublicKey subjectPublicKey() const;
01343
01350 bool isCA() const;
01351
01357 int pathLimit() const;
01358
01362 QString challenge() const;
01363
01368 SignatureAlgorithm signatureAlgorithm() const;
01369
01375 bool operator==(const CertificateRequest &csr) const;
01376
01380 inline bool operator!=(const CertificateRequest &other) const
01381 {
01382 return !(*this == other);
01383 }
01384
01390 QByteArray toDER() const;
01391
01397 QString toPEM() const;
01398
01406 bool toPEMFile(const QString &fileName) const;
01407
01422 static CertificateRequest fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
01423
01439 static CertificateRequest fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
01440
01456 static CertificateRequest fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
01457
01465 QString toString() const;
01466
01481 static CertificateRequest fromString(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
01482
01486 void change(CSRContext *c);
01487
01488 private:
01489 class Private;
01490 friend class Private;
01491 QSharedDataPointer<Private> d;
01492 };
01493
01501 class QCA_EXPORT CRLEntry
01502 {
01503 public:
01507 enum Reason
01508 {
01509 Unspecified,
01510 KeyCompromise,
01511 CACompromise,
01512 AffiliationChanged,
01513 Superseded,
01514 CessationOfOperation,
01515 CertificateHold,
01516 RemoveFromCRL,
01517 PrivilegeWithdrawn,
01518 AACompromise
01519 };
01520
01524 CRLEntry();
01525
01532 explicit CRLEntry(const Certificate &c, Reason r = Unspecified);
01533
01542 CRLEntry(const BigInteger serial, const QDateTime &time, Reason r = Unspecified);
01543
01549 CRLEntry(const CRLEntry &from);
01550
01551 ~CRLEntry();
01552
01558 CRLEntry & operator=(const CRLEntry &from);
01559
01563 BigInteger serialNumber() const;
01564
01568 QDateTime time() const;
01569
01573 bool isNull() const;
01574
01581 Reason reason() const;
01582
01588 bool operator<(const CRLEntry &a) const;
01589
01595 bool operator==(const CRLEntry &a) const;
01596
01600 inline bool operator!=(const CRLEntry &other) const
01601 {
01602 return !(*this == other);
01603 }
01604
01605 private:
01606 BigInteger _serial;
01607 QDateTime _time;
01608 Reason _reason;
01609
01610 class Private;
01611 Private *d;
01612 };
01613
01634 class QCA_EXPORT CRL : public Algorithm
01635 {
01636 public:
01637 CRL();
01638
01642 CRL(const CRL &from);
01643
01644 ~CRL();
01645
01649 CRL & operator=(const CRL &from);
01650
01656 bool isNull() const;
01657
01664 CertificateInfo issuerInfo() const;
01665
01674 CertificateInfoOrdered issuerInfoOrdered() const;
01675
01682 int number() const;
01683
01687 QDateTime thisUpdate() const;
01688
01694 QDateTime nextUpdate() const;
01695
01699 QList<CRLEntry> revoked() const;
01700
01704 SignatureAlgorithm signatureAlgorithm() const;
01705
01709 QByteArray issuerKeyId() const;
01710
01716 bool operator==(const CRL &a) const;
01717
01721 inline bool operator!=(const CRL &other) const
01722 {
01723 return !(*this == other);
01724 }
01725
01731 QByteArray toDER() const;
01732
01738 QString toPEM() const;
01739
01746 bool toPEMFile(const QString &fileName) const;
01747
01759 static CRL fromDER(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
01760
01772 static CRL fromPEM(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
01773
01786 static CRL fromPEMFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
01787
01791 void change(CRLContext *c);
01792
01793 private:
01794 class Private;
01795 friend class Private;
01796 QSharedDataPointer<Private> d;
01797 };
01798
01812 class QCA_EXPORT CertificateCollection
01813 {
01814 public:
01818 CertificateCollection();
01819
01825 CertificateCollection(const CertificateCollection &from);
01826
01827 ~CertificateCollection();
01828
01834 CertificateCollection & operator=(const CertificateCollection &from);
01835
01841 void addCertificate(const Certificate &cert);
01842
01849 void addCRL(const CRL &crl);
01850
01854 QList<Certificate> certificates() const;
01855
01859 QList<CRL> crls() const;
01860
01866 void append(const CertificateCollection &other);
01867
01873 CertificateCollection operator+(const CertificateCollection &other) const;
01874
01880 CertificateCollection & operator+=(const CertificateCollection &other);
01881
01892 static bool canUsePKCS7(const QString &provider = QString());
01893
01902 bool toFlatTextFile(const QString &fileName);
01903
01914 bool toPKCS7File(const QString &fileName, const QString &provider = QString());
01915
01929 static CertificateCollection fromFlatTextFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
01930
01944 static CertificateCollection fromPKCS7File(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
01945
01946 private:
01947 class Private;
01948 QSharedDataPointer<Private> d;
01949 };
01950
01959 class QCA_EXPORT CertificateAuthority : public Algorithm
01960 {
01961 public:
01970 CertificateAuthority(const Certificate &cert, const PrivateKey &key, const QString &provider);
01971
01977 CertificateAuthority(const CertificateAuthority &from);
01978
01979 ~CertificateAuthority();
01980
01986 CertificateAuthority & operator=(const CertificateAuthority &from);
01987
01994 Certificate certificate() const;
01995
02003 Certificate signRequest(const CertificateRequest &req, const QDateTime ¬ValidAfter) const;
02004
02011 Certificate createCertificate(const PublicKey &key, const CertificateOptions &opts) const;
02012
02020 CRL createCRL(const QDateTime &nextUpdate) const;
02021
02031 CRL updateCRL(const CRL &crl, const QList<CRLEntry> &entries, const QDateTime &nextUpdate) const;
02032
02033 private:
02034 class Private;
02035 Private *d;
02036 };
02037
02057 class QCA_EXPORT KeyBundle
02058 {
02059 public:
02063 KeyBundle();
02064
02082 explicit KeyBundle(const QString &fileName, const SecureArray &passphrase = SecureArray());
02083
02089 KeyBundle(const KeyBundle &from);
02090
02091 ~KeyBundle();
02092
02098 KeyBundle & operator=(const KeyBundle &from);
02099
02103 bool isNull() const;
02104
02114 QString name() const;
02115
02121 CertificateChain certificateChain() const;
02122
02128 PrivateKey privateKey() const;
02129
02135 void setName(const QString &s);
02136
02146 void setCertificateChainAndKey(const CertificateChain &c, const PrivateKey &key);
02147
02171 QByteArray toArray(const SecureArray &passphrase, const QString &provider = QString()) const;
02172
02197 bool toFile(const QString &fileName, const SecureArray &passphrase, const QString &provider = QString()) const;
02198
02229 static KeyBundle fromArray(const QByteArray &a, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
02230
02261 static KeyBundle fromFile(const QString &fileName, const SecureArray &passphrase = SecureArray(), ConvertResult *result = 0, const QString &provider = QString());
02262
02263 private:
02264 class Private;
02265 QSharedDataPointer<Private> d;
02266 };
02267
02282 class QCA_EXPORT PGPKey : public Algorithm
02283 {
02284 public:
02288 PGPKey();
02289
02296 PGPKey(const QString &fileName);
02297
02303 PGPKey(const PGPKey &from);
02304
02305 ~PGPKey();
02306
02312 PGPKey & operator=(const PGPKey &from);
02313
02319 bool isNull() const;
02320
02324 QString keyId() const;
02325
02329 QString primaryUserId() const;
02330
02334 QStringList userIds() const;
02335
02341 bool isSecret() const;
02342
02346 QDateTime creationDate() const;
02347
02351 QDateTime expirationDate() const;
02352
02359 QString fingerprint() const;
02360
02369 bool inKeyring() const;
02370
02376 bool isTrusted() const;
02377
02387 QByteArray toArray() const;
02388
02397 QString toString() const;
02398
02404 bool toFile(const QString &fileName) const;
02405
02415 static PGPKey fromArray(const QByteArray &a, ConvertResult *result = 0, const QString &provider = QString());
02416
02426 static PGPKey fromString(const QString &s, ConvertResult *result = 0, const QString &provider = QString());
02427
02438 static PGPKey fromFile(const QString &fileName, ConvertResult *result = 0, const QString &provider = QString());
02439
02440 private:
02441 class Private;
02442 Private *d;
02443 };
02444
02484 class QCA_EXPORT KeyLoader : public QObject
02485 {
02486 Q_OBJECT
02487 public:
02493 KeyLoader(QObject *parent = 0);
02494 ~KeyLoader();
02495
02505 void loadPrivateKeyFromPEMFile(const QString &fileName);
02506
02515 void loadPrivateKeyFromPEM(const QString &s);
02516
02525 void loadPrivateKeyFromDER(const SecureArray &a);
02526
02535 void loadKeyBundleFromFile(const QString &fileName);
02536
02544 void loadKeyBundleFromArray(const QByteArray &a);
02545
02551 ConvertResult convertResult() const;
02552
02562 PrivateKey privateKey() const;
02563
02572 KeyBundle keyBundle() const;
02573
02574 Q_SIGNALS:
02582 void finished();
02583
02584 private:
02585 Q_DISABLE_COPY(KeyLoader)
02586
02587 class Private;
02588 friend class Private;
02589 Private *d;
02590 };
02591
02592 }
02593
02594 #endif