1 | // Copyright (C) 2016 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 | /*! |
6 | \class QSslCipher |
7 | \brief The QSslCipher class represents an SSL cryptographic cipher. |
8 | \since 4.3 |
9 | |
10 | \reentrant |
11 | \ingroup network |
12 | \ingroup ssl |
13 | \ingroup shared |
14 | \inmodule QtNetwork |
15 | |
16 | QSslCipher stores information about one cryptographic cipher. It |
17 | is most commonly used with QSslSocket, either for configuring |
18 | which ciphers the socket can use, or for displaying the socket's |
19 | ciphers to the user. |
20 | |
21 | \sa QSslSocket, QSslKey |
22 | */ |
23 | |
24 | #include "qsslcipher.h" |
25 | #include "qsslcipher_p.h" |
26 | #include "qsslsocket.h" |
27 | #include "qsslconfiguration.h" |
28 | |
29 | #ifndef QT_NO_DEBUG_STREAM |
30 | #include <QtCore/qdebug.h> |
31 | #endif |
32 | |
33 | QT_BEGIN_NAMESPACE |
34 | |
35 | static_assert(QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
36 | && sizeof(QScopedPointer<QSslCipherPrivate>) == sizeof(std::unique_ptr<QSslCipherPrivate>)); |
37 | |
38 | /*! |
39 | Constructs an empty QSslCipher object. |
40 | */ |
41 | QSslCipher::QSslCipher() |
42 | : d(new QSslCipherPrivate) |
43 | { |
44 | } |
45 | |
46 | /*! |
47 | \since 5.3 |
48 | |
49 | Constructs a QSslCipher object for the cipher determined by \a |
50 | name. The constructor accepts only supported ciphers (i.e., the |
51 | \a name must identify a cipher in the list of ciphers returned by |
52 | QSslSocket::supportedCiphers()). |
53 | |
54 | You can call isNull() after construction to check if \a name |
55 | correctly identified a supported cipher. |
56 | */ |
57 | QSslCipher::QSslCipher(const QString &name) |
58 | : d(new QSslCipherPrivate) |
59 | { |
60 | const auto ciphers = QSslConfiguration::supportedCiphers(); |
61 | for (const QSslCipher &cipher : ciphers) { |
62 | if (cipher.name() == name) { |
63 | *this = cipher; |
64 | return; |
65 | } |
66 | } |
67 | } |
68 | |
69 | /*! |
70 | Constructs a QSslCipher object for the cipher determined by \a |
71 | name and \a protocol. The constructor accepts only supported |
72 | ciphers (i.e., the \a name and \a protocol must identify a cipher |
73 | in the list of ciphers returned by |
74 | QSslSocket::supportedCiphers()). |
75 | |
76 | You can call isNull() after construction to check if \a name and |
77 | \a protocol correctly identified a supported cipher. |
78 | */ |
79 | QSslCipher::QSslCipher(const QString &name, QSsl::SslProtocol protocol) |
80 | : d(new QSslCipherPrivate) |
81 | { |
82 | const auto ciphers = QSslConfiguration::supportedCiphers(); |
83 | for (const QSslCipher &cipher : ciphers) { |
84 | if (cipher.name() == name && cipher.protocol() == protocol) { |
85 | *this = cipher; |
86 | return; |
87 | } |
88 | } |
89 | } |
90 | |
91 | /*! |
92 | Constructs an identical copy of the \a other cipher. |
93 | */ |
94 | QSslCipher::QSslCipher(const QSslCipher &other) |
95 | : d(new QSslCipherPrivate) |
96 | { |
97 | *d.get() = *other.d.get(); |
98 | } |
99 | |
100 | /*! |
101 | Destroys the QSslCipher object. |
102 | */ |
103 | QSslCipher::~QSslCipher() |
104 | { |
105 | } |
106 | |
107 | /*! |
108 | Copies the contents of \a other into this cipher, making the two |
109 | ciphers identical. |
110 | */ |
111 | QSslCipher &QSslCipher::operator=(const QSslCipher &other) |
112 | { |
113 | *d.get() = *other.d.get(); |
114 | return *this; |
115 | } |
116 | |
117 | /*! |
118 | \fn void QSslCipher::swap(QSslCipher &other) |
119 | \since 5.0 |
120 | |
121 | Swaps this cipher instance with \a other. This function is very |
122 | fast and never fails. |
123 | */ |
124 | |
125 | /*! |
126 | Returns \c true if this cipher is the same as \a other; otherwise, |
127 | false is returned. |
128 | */ |
129 | bool QSslCipher::operator==(const QSslCipher &other) const |
130 | { |
131 | return d->name == other.d->name && d->protocol == other.d->protocol; |
132 | } |
133 | |
134 | /*! |
135 | \fn bool QSslCipher::operator!=(const QSslCipher &other) const |
136 | |
137 | Returns \c true if this cipher is not the same as \a other; |
138 | otherwise, false is returned. |
139 | */ |
140 | |
141 | /*! |
142 | Returns \c true if this is a null cipher; otherwise returns \c false. |
143 | */ |
144 | bool QSslCipher::isNull() const |
145 | { |
146 | return d->isNull; |
147 | } |
148 | |
149 | /*! |
150 | Returns the name of the cipher, or an empty QString if this is a null |
151 | cipher. |
152 | |
153 | \sa isNull() |
154 | */ |
155 | QString QSslCipher::name() const |
156 | { |
157 | return d->name; |
158 | } |
159 | |
160 | /*! |
161 | Returns the number of bits supported by the cipher. |
162 | |
163 | \sa usedBits() |
164 | */ |
165 | int QSslCipher::supportedBits()const |
166 | { |
167 | return d->supportedBits; |
168 | } |
169 | |
170 | /*! |
171 | Returns the number of bits used by the cipher. |
172 | |
173 | \sa supportedBits() |
174 | */ |
175 | int QSslCipher::usedBits() const |
176 | { |
177 | return d->bits; |
178 | } |
179 | |
180 | /*! |
181 | Returns the cipher's key exchange method as a QString. |
182 | */ |
183 | QString QSslCipher::keyExchangeMethod() const |
184 | { |
185 | return d->keyExchangeMethod; |
186 | } |
187 | |
188 | /*! |
189 | Returns the cipher's authentication method as a QString. |
190 | */ |
191 | QString QSslCipher::authenticationMethod() const |
192 | { |
193 | return d->authenticationMethod; |
194 | } |
195 | |
196 | /*! |
197 | Returns the cipher's encryption method as a QString. |
198 | */ |
199 | QString QSslCipher::encryptionMethod() const |
200 | { |
201 | return d->encryptionMethod; |
202 | } |
203 | |
204 | /*! |
205 | Returns the cipher's protocol as a QString. |
206 | |
207 | \sa protocol() |
208 | */ |
209 | QString QSslCipher::protocolString() const |
210 | { |
211 | return d->protocolString; |
212 | } |
213 | |
214 | /*! |
215 | Returns the cipher's protocol type, or \l QSsl::UnknownProtocol if |
216 | QSslCipher is unable to determine the protocol (protocolString() may |
217 | contain more information). |
218 | |
219 | \sa protocolString() |
220 | */ |
221 | QSsl::SslProtocol QSslCipher::protocol() const |
222 | { |
223 | return d->protocol; |
224 | } |
225 | |
226 | #ifndef QT_NO_DEBUG_STREAM |
227 | QDebug operator<<(QDebug debug, const QSslCipher &cipher) |
228 | { |
229 | QDebugStateSaver saver(debug); |
230 | debug.resetFormat().nospace().noquote(); |
231 | debug << "QSslCipher(name=" << cipher.name() |
232 | << ", bits=" << cipher.usedBits() |
233 | << ", proto=" << cipher.protocolString() |
234 | << ')'; |
235 | return debug; |
236 | } |
237 | #endif |
238 | |
239 | QT_END_NAMESPACE |
240 | |