| 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 | \memberswap{cipher instance} |
| 121 | */ |
| 122 | |
| 123 | /*! |
| 124 | Returns \c true if this cipher is the same as \a other; otherwise, |
| 125 | false is returned. |
| 126 | */ |
| 127 | bool QSslCipher::operator==(const QSslCipher &other) const |
| 128 | { |
| 129 | return d->name == other.d->name && d->protocol == other.d->protocol; |
| 130 | } |
| 131 | |
| 132 | /*! |
| 133 | \fn bool QSslCipher::operator!=(const QSslCipher &other) const |
| 134 | |
| 135 | Returns \c true if this cipher is not the same as \a other; |
| 136 | otherwise, false is returned. |
| 137 | */ |
| 138 | |
| 139 | /*! |
| 140 | Returns \c true if this is a null cipher; otherwise returns \c false. |
| 141 | */ |
| 142 | bool QSslCipher::isNull() const |
| 143 | { |
| 144 | return d->isNull; |
| 145 | } |
| 146 | |
| 147 | /*! |
| 148 | Returns the name of the cipher, or an empty QString if this is a null |
| 149 | cipher. |
| 150 | |
| 151 | \sa isNull() |
| 152 | */ |
| 153 | QString QSslCipher::name() const |
| 154 | { |
| 155 | return d->name; |
| 156 | } |
| 157 | |
| 158 | /*! |
| 159 | Returns the number of bits supported by the cipher. |
| 160 | |
| 161 | \sa usedBits() |
| 162 | */ |
| 163 | int QSslCipher::supportedBits()const |
| 164 | { |
| 165 | return d->supportedBits; |
| 166 | } |
| 167 | |
| 168 | /*! |
| 169 | Returns the number of bits used by the cipher. |
| 170 | |
| 171 | \sa supportedBits() |
| 172 | */ |
| 173 | int QSslCipher::usedBits() const |
| 174 | { |
| 175 | return d->bits; |
| 176 | } |
| 177 | |
| 178 | /*! |
| 179 | Returns the cipher's key exchange method as a QString. |
| 180 | */ |
| 181 | QString QSslCipher::keyExchangeMethod() const |
| 182 | { |
| 183 | return d->keyExchangeMethod; |
| 184 | } |
| 185 | |
| 186 | /*! |
| 187 | Returns the cipher's authentication method as a QString. |
| 188 | */ |
| 189 | QString QSslCipher::authenticationMethod() const |
| 190 | { |
| 191 | return d->authenticationMethod; |
| 192 | } |
| 193 | |
| 194 | /*! |
| 195 | Returns the cipher's encryption method as a QString. |
| 196 | */ |
| 197 | QString QSslCipher::encryptionMethod() const |
| 198 | { |
| 199 | return d->encryptionMethod; |
| 200 | } |
| 201 | |
| 202 | /*! |
| 203 | Returns the cipher's protocol as a QString. |
| 204 | |
| 205 | \sa protocol() |
| 206 | */ |
| 207 | QString QSslCipher::protocolString() const |
| 208 | { |
| 209 | return d->protocolString; |
| 210 | } |
| 211 | |
| 212 | /*! |
| 213 | Returns the cipher's protocol type, or \l QSsl::UnknownProtocol if |
| 214 | QSslCipher is unable to determine the protocol (protocolString() may |
| 215 | contain more information). |
| 216 | |
| 217 | \sa protocolString() |
| 218 | */ |
| 219 | QSsl::SslProtocol QSslCipher::protocol() const |
| 220 | { |
| 221 | return d->protocol; |
| 222 | } |
| 223 | |
| 224 | #ifndef QT_NO_DEBUG_STREAM |
| 225 | QDebug operator<<(QDebug debug, const QSslCipher &cipher) |
| 226 | { |
| 227 | QDebugStateSaver saver(debug); |
| 228 | debug.resetFormat().nospace().noquote(); |
| 229 | debug << "QSslCipher(name=" << cipher.name() |
| 230 | << ", bits=" << cipher.usedBits() |
| 231 | << ", proto=" << cipher.protocolString() |
| 232 | << ')'; |
| 233 | return debug; |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | QT_END_NAMESPACE |
| 238 | |