| 1 | // Copyright (C) 2020 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 3 |  | 
|---|
| 4 |  | 
|---|
| 5 | #ifndef QSSLCERTIFICATE_H | 
|---|
| 6 | #define QSSLCERTIFICATE_H | 
|---|
| 7 |  | 
|---|
| 8 | #ifdef verify | 
|---|
| 9 | #undef verify | 
|---|
| 10 | #endif | 
|---|
| 11 |  | 
|---|
| 12 | #include <QtNetwork/qtnetworkglobal.h> | 
|---|
| 13 | #include <QtCore/qnamespace.h> | 
|---|
| 14 | #include <QtCore/qbytearray.h> | 
|---|
| 15 | #include <QtCore/qcryptographichash.h> | 
|---|
| 16 | #include <QtCore/qdatetime.h> | 
|---|
| 17 | #include <QtCore/qmap.h> | 
|---|
| 18 | #include <QtCore/qshareddata.h> | 
|---|
| 19 | #include <QtNetwork/qssl.h> | 
|---|
| 20 |  | 
|---|
| 21 | QT_BEGIN_NAMESPACE | 
|---|
| 22 |  | 
|---|
| 23 | class QDateTime; | 
|---|
| 24 | class QIODevice; | 
|---|
| 25 | class QSslError; | 
|---|
| 26 | class QSslKey; | 
|---|
| 27 | class QSslCertificateExtension; | 
|---|
| 28 |  | 
|---|
| 29 | class QSslCertificate; | 
|---|
| 30 | // qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4) | 
|---|
| 31 | Q_NETWORK_EXPORT size_t qHash(const QSslCertificate &key, size_t seed = 0) noexcept; | 
|---|
| 32 |  | 
|---|
| 33 | class QSslCertificatePrivate; | 
|---|
| 34 | QT_DECLARE_QESDP_SPECIALIZATION_DTOR(QSslCertificatePrivate) | 
|---|
| 35 |  | 
|---|
| 36 | class Q_NETWORK_EXPORT QSslCertificate | 
|---|
| 37 | { | 
|---|
| 38 | public: | 
|---|
| 39 | enum SubjectInfo { | 
|---|
| 40 | Organization, | 
|---|
| 41 | CommonName, | 
|---|
| 42 | LocalityName, | 
|---|
| 43 | OrganizationalUnitName, | 
|---|
| 44 | CountryName, | 
|---|
| 45 | StateOrProvinceName, | 
|---|
| 46 | DistinguishedNameQualifier, | 
|---|
| 47 | SerialNumber, | 
|---|
| 48 | EmailAddress | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 51 | enum class PatternSyntax { | 
|---|
| 52 | RegularExpression, | 
|---|
| 53 | Wildcard, | 
|---|
| 54 | FixedString | 
|---|
| 55 | }; | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | explicit QSslCertificate(QIODevice *device, QSsl::EncodingFormat format = QSsl::Pem); | 
|---|
| 59 | explicit QSslCertificate(const QByteArray &data = QByteArray(), QSsl::EncodingFormat format = QSsl::Pem); | 
|---|
| 60 | QSslCertificate(const QSslCertificate &other); | 
|---|
| 61 | QSslCertificate(QSslCertificate &&other) noexcept = default; | 
|---|
| 62 | ~QSslCertificate(); | 
|---|
| 63 | QSslCertificate &operator=(QSslCertificate &&other) noexcept { swap(other); return *this; } | 
|---|
| 64 | QSslCertificate &operator=(const QSslCertificate &other); | 
|---|
| 65 |  | 
|---|
| 66 | void swap(QSslCertificate &other) noexcept | 
|---|
| 67 | { d.swap(other&: other.d); } | 
|---|
| 68 |  | 
|---|
| 69 | bool operator==(const QSslCertificate &other) const; | 
|---|
| 70 | inline bool operator!=(const QSslCertificate &other) const { return !operator==(other); } | 
|---|
| 71 |  | 
|---|
| 72 | bool isNull() const; | 
|---|
| 73 | bool isBlacklisted() const; | 
|---|
| 74 | bool isSelfSigned() const; | 
|---|
| 75 | void clear(); | 
|---|
| 76 |  | 
|---|
| 77 | // Certificate info | 
|---|
| 78 | QByteArray version() const; | 
|---|
| 79 | QByteArray serialNumber() const; | 
|---|
| 80 | QByteArray digest(QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5) const; | 
|---|
| 81 | QStringList issuerInfo(SubjectInfo info) const; | 
|---|
| 82 | QStringList issuerInfo(const QByteArray &attribute) const; | 
|---|
| 83 | QStringList subjectInfo(SubjectInfo info) const; | 
|---|
| 84 | QStringList subjectInfo(const QByteArray &attribute) const; | 
|---|
| 85 | QString issuerDisplayName() const; | 
|---|
| 86 | QString subjectDisplayName() const; | 
|---|
| 87 |  | 
|---|
| 88 | QList<QByteArray> subjectInfoAttributes() const; | 
|---|
| 89 | QList<QByteArray> issuerInfoAttributes() const; | 
|---|
| 90 | QMultiMap<QSsl::AlternativeNameEntryType, QString> subjectAlternativeNames() const; | 
|---|
| 91 | QDateTime effectiveDate() const; | 
|---|
| 92 | QDateTime expiryDate() const; | 
|---|
| 93 | #ifndef QT_NO_SSL | 
|---|
| 94 | QSslKey publicKey() const; | 
|---|
| 95 | #endif | 
|---|
| 96 | QList<QSslCertificateExtension> extensions() const; | 
|---|
| 97 |  | 
|---|
| 98 | QByteArray toPem() const; | 
|---|
| 99 | QByteArray toDer() const; | 
|---|
| 100 | QString toText() const; | 
|---|
| 101 |  | 
|---|
| 102 | static QList<QSslCertificate> fromPath(const QString &path, | 
|---|
| 103 | QSsl::EncodingFormat format = QSsl::Pem, | 
|---|
| 104 | PatternSyntax syntax = PatternSyntax::FixedString); | 
|---|
| 105 |  | 
|---|
| 106 | static QList<QSslCertificate> fromDevice( | 
|---|
| 107 | QIODevice *device, QSsl::EncodingFormat format = QSsl::Pem); | 
|---|
| 108 | static QList<QSslCertificate> fromData( | 
|---|
| 109 | const QByteArray &data, QSsl::EncodingFormat format = QSsl::Pem); | 
|---|
| 110 |  | 
|---|
| 111 | #ifndef QT_NO_SSL | 
|---|
| 112 | static QList<QSslError> verify(const QList<QSslCertificate> &certificateChain, const QString &hostName = QString()); | 
|---|
| 113 | static bool importPkcs12(QIODevice *device, | 
|---|
| 114 | QSslKey *key, QSslCertificate *cert, | 
|---|
| 115 | QList<QSslCertificate> *caCertificates = nullptr, | 
|---|
| 116 | const QByteArray &passPhrase=QByteArray()); | 
|---|
| 117 | #endif | 
|---|
| 118 |  | 
|---|
| 119 | Qt::HANDLE handle() const; | 
|---|
| 120 |  | 
|---|
| 121 | private: | 
|---|
| 122 | QExplicitlySharedDataPointer<QSslCertificatePrivate> d; | 
|---|
| 123 | friend class QTlsBackend; | 
|---|
| 124 |  | 
|---|
| 125 | friend Q_NETWORK_EXPORT size_t qHash(const QSslCertificate &key, size_t seed) noexcept; | 
|---|
| 126 | }; | 
|---|
| 127 | Q_DECLARE_SHARED(QSslCertificate) | 
|---|
| 128 |  | 
|---|
| 129 | #ifndef QT_NO_DEBUG_STREAM | 
|---|
| 130 | class QDebug; | 
|---|
| 131 | Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QSslCertificate &certificate); | 
|---|
| 132 | Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, QSslCertificate::SubjectInfo info); | 
|---|
| 133 | #endif | 
|---|
| 134 |  | 
|---|
| 135 | QT_END_NAMESPACE | 
|---|
| 136 |  | 
|---|
| 137 | QT_DECL_METATYPE_EXTERN(QSslCertificate, Q_NETWORK_EXPORT) | 
|---|
| 138 |  | 
|---|
| 139 | #endif | 
|---|
| 140 |  | 
|---|