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