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

source code of qtbase/src/network/ssl/qsslcertificate.h