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