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