1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40
41#ifndef QSSLCERTIFICATE_H
42#define QSSLCERTIFICATE_H
43
44#ifdef verify
45#undef verify
46#endif
47
48#include <QtNetwork/qtnetworkglobal.h>
49#include <QtCore/qnamespace.h>
50#include <QtCore/qbytearray.h>
51#include <QtCore/qcryptographichash.h>
52#include <QtCore/qdatetime.h>
53#include <QtCore/qsharedpointer.h>
54#include <QtCore/qmap.h>
55#include <QtNetwork/qssl.h>
56
57QT_BEGIN_NAMESPACE
58
59class QDateTime;
60class QIODevice;
61class QSslError;
62class QSslKey;
63class QSslCertificateExtension;
64
65class QSslCertificate;
66// qHash is a friend, but we can't use default arguments for friends (ยง8.3.6.4)
67Q_NETWORK_EXPORT size_t qHash(const QSslCertificate &key, size_t seed = 0) noexcept;
68
69class QSslCertificatePrivate;
70class Q_NETWORK_EXPORT QSslCertificate
71{
72public:
73 enum SubjectInfo {
74 Organization,
75 CommonName,
76 LocalityName,
77 OrganizationalUnitName,
78 CountryName,
79 StateOrProvinceName,
80 DistinguishedNameQualifier,
81 SerialNumber,
82 EmailAddress
83 };
84
85 enum class PatternSyntax {
86 RegularExpression,
87 Wildcard,
88 FixedString
89 };
90
91
92 explicit QSslCertificate(QIODevice *device, QSsl::EncodingFormat format = QSsl::Pem);
93 explicit QSslCertificate(const QByteArray &data = QByteArray(), QSsl::EncodingFormat format = QSsl::Pem);
94 QSslCertificate(const QSslCertificate &other);
95 ~QSslCertificate();
96 QSslCertificate &operator=(QSslCertificate &&other) noexcept { swap(other); return *this; }
97 QSslCertificate &operator=(const QSslCertificate &other);
98
99 void swap(QSslCertificate &other) noexcept
100 { qSwap(d, other.d); }
101
102 bool operator==(const QSslCertificate &other) const;
103 inline bool operator!=(const QSslCertificate &other) const { return !operator==(other); }
104
105 bool isNull() const;
106 bool isBlacklisted() const;
107 bool isSelfSigned() const;
108 void clear();
109
110 // Certificate info
111 QByteArray version() const;
112 QByteArray serialNumber() const;
113 QByteArray digest(QCryptographicHash::Algorithm algorithm = QCryptographicHash::Md5) const;
114 QStringList issuerInfo(SubjectInfo info) const;
115 QStringList issuerInfo(const QByteArray &attribute) const;
116 QStringList subjectInfo(SubjectInfo info) const;
117 QStringList subjectInfo(const QByteArray &attribute) const;
118 QString issuerDisplayName() const;
119 QString subjectDisplayName() const;
120
121 QList<QByteArray> subjectInfoAttributes() const;
122 QList<QByteArray> issuerInfoAttributes() const;
123 QMultiMap<QSsl::AlternativeNameEntryType, QString> subjectAlternativeNames() const;
124 QDateTime effectiveDate() const;
125 QDateTime expiryDate() const;
126#ifndef QT_NO_SSL
127 QSslKey publicKey() const;
128#endif
129 QList<QSslCertificateExtension> extensions() const;
130
131 QByteArray toPem() const;
132 QByteArray toDer() const;
133 QString toText() const;
134
135 static QList<QSslCertificate> fromPath(const QString &path,
136 QSsl::EncodingFormat format = QSsl::Pem,
137 PatternSyntax syntax = PatternSyntax::FixedString);
138
139 static QList<QSslCertificate> fromDevice(
140 QIODevice *device, QSsl::EncodingFormat format = QSsl::Pem);
141 static QList<QSslCertificate> fromData(
142 const QByteArray &data, QSsl::EncodingFormat format = QSsl::Pem);
143
144#ifndef QT_NO_SSL
145#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
146 static QList<QSslError> verify(const QList<QSslCertificate> &certificateChain, const QString &hostName = QString());
147#else
148 static QList<QSslError> verify(QList<QSslCertificate> certificateChain, const QString &hostName = QString());
149#endif
150
151 static bool importPkcs12(QIODevice *device,
152 QSslKey *key, QSslCertificate *cert,
153 QList<QSslCertificate> *caCertificates = nullptr,
154 const QByteArray &passPhrase=QByteArray());
155#endif
156
157 Qt::HANDLE handle() const;
158
159private:
160 QExplicitlySharedDataPointer<QSslCertificatePrivate> d;
161 friend class QSslCertificatePrivate;
162 friend class QSslSocketBackendPrivate;
163
164 friend Q_NETWORK_EXPORT size_t qHash(const QSslCertificate &key, size_t seed) noexcept;
165};
166Q_DECLARE_SHARED(QSslCertificate)
167
168#ifndef QT_NO_DEBUG_STREAM
169class QDebug;
170Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, const QSslCertificate &certificate);
171Q_NETWORK_EXPORT QDebug operator<<(QDebug debug, QSslCertificate::SubjectInfo info);
172#endif
173
174QT_END_NAMESPACE
175
176Q_DECLARE_METATYPE(QSslCertificate)
177
178#endif
179