| 1 | // Copyright (C) 2014 Governikus GmbH & Co. KG. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qsslpresharedkeyauthenticator.h" |
| 5 | #include "qsslpresharedkeyauthenticator_p.h" |
| 6 | |
| 7 | #include <QSharedData> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | QT_IMPL_METATYPE_EXTERN(QSslPreSharedKeyAuthenticator) |
| 12 | QT_IMPL_METATYPE_EXTERN_TAGGED(QSslPreSharedKeyAuthenticator*, QSslPreSharedKeyAuthenticator_ptr) |
| 13 | |
| 14 | /*! |
| 15 | \internal |
| 16 | */ |
| 17 | QSslPreSharedKeyAuthenticatorPrivate::QSslPreSharedKeyAuthenticatorPrivate() |
| 18 | : maximumIdentityLength(0), |
| 19 | maximumPreSharedKeyLength(0) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /*! |
| 24 | \class QSslPreSharedKeyAuthenticator |
| 25 | |
| 26 | \brief The QSslPreSharedKeyAuthenticator class provides authentication data for pre |
| 27 | shared keys (PSK) ciphersuites. |
| 28 | |
| 29 | \inmodule QtNetwork |
| 30 | |
| 31 | \reentrant |
| 32 | |
| 33 | \ingroup network |
| 34 | \ingroup ssl |
| 35 | \ingroup shared |
| 36 | |
| 37 | \since 5.5 |
| 38 | |
| 39 | The QSslPreSharedKeyAuthenticator class is used by an SSL socket to provide |
| 40 | the required authentication data in a pre shared key (PSK) ciphersuite. |
| 41 | |
| 42 | In a PSK handshake, the client must derive a key, which must match the key |
| 43 | set on the server. The exact algorithm of deriving the key depends on the |
| 44 | application; however, for this purpose, the server may send an \e{identity |
| 45 | hint} to the client. This hint, combined with other information (for |
| 46 | instance a passphrase), is then used by the client to construct the shared |
| 47 | key. |
| 48 | |
| 49 | The QSslPreSharedKeyAuthenticator provides means to client applications for |
| 50 | completing the PSK handshake. The client application needs to connect a |
| 51 | slot to the QSslSocket::preSharedKeyAuthenticationRequired() signal: |
| 52 | |
| 53 | \snippet code/src_network_ssl_qsslpresharedkeyauthenticator.cpp 0 |
| 54 | |
| 55 | The signal carries a QSslPreSharedKeyAuthenticator object containing the |
| 56 | identity hint the server sent to the client, and which must be filled with the |
| 57 | corresponding client identity and the derived key: |
| 58 | |
| 59 | \snippet code/src_network_ssl_qsslpresharedkeyauthenticator.cpp 1 |
| 60 | |
| 61 | \note PSK ciphersuites are supported only when using OpenSSL 1.0.1 (or |
| 62 | greater) as the SSL backend. |
| 63 | |
| 64 | \note PSK is currently only supported in OpenSSL. |
| 65 | |
| 66 | \sa QSslSocket |
| 67 | */ |
| 68 | |
| 69 | /*! |
| 70 | Constructs a default QSslPreSharedKeyAuthenticator object. |
| 71 | |
| 72 | The identity hint, the identity and the key will be initialized to empty |
| 73 | byte arrays; the maximum length for both the identity and the key will be |
| 74 | initialized to 0. |
| 75 | */ |
| 76 | QSslPreSharedKeyAuthenticator::QSslPreSharedKeyAuthenticator() |
| 77 | : d(new QSslPreSharedKeyAuthenticatorPrivate) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | /*! |
| 82 | Destroys the QSslPreSharedKeyAuthenticator object. |
| 83 | */ |
| 84 | QSslPreSharedKeyAuthenticator::~QSslPreSharedKeyAuthenticator() |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | /*! |
| 89 | Constructs a QSslPreSharedKeyAuthenticator object as a copy of \a authenticator. |
| 90 | |
| 91 | \sa operator=() |
| 92 | */ |
| 93 | QSslPreSharedKeyAuthenticator::QSslPreSharedKeyAuthenticator(const QSslPreSharedKeyAuthenticator &authenticator) |
| 94 | : d(authenticator.d) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | /*! |
| 99 | Assigns the QSslPreSharedKeyAuthenticator object \a authenticator to this object, |
| 100 | and returns a reference to the copy. |
| 101 | */ |
| 102 | QSslPreSharedKeyAuthenticator &QSslPreSharedKeyAuthenticator::operator=(const QSslPreSharedKeyAuthenticator &authenticator) |
| 103 | { |
| 104 | d = authenticator.d; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | \fn QSslPreSharedKeyAuthenticator &QSslPreSharedKeyAuthenticator::operator=(QSslPreSharedKeyAuthenticator &&authenticator) |
| 110 | |
| 111 | Move-assigns the QSslPreSharedKeyAuthenticator object \a authenticator to this |
| 112 | object, and returns a reference to the moved instance. |
| 113 | */ |
| 114 | |
| 115 | /*! |
| 116 | \fn void QSslPreSharedKeyAuthenticator::swap(QSslPreSharedKeyAuthenticator &other) |
| 117 | \memberswap{authenticator} |
| 118 | */ |
| 119 | |
| 120 | /*! |
| 121 | Returns the PSK identity hint as provided by the server. The interpretation |
| 122 | of this hint is left to the application. |
| 123 | */ |
| 124 | QByteArray QSslPreSharedKeyAuthenticator::identityHint() const |
| 125 | { |
| 126 | return d->identityHint; |
| 127 | } |
| 128 | |
| 129 | /*! |
| 130 | Sets the PSK client identity (to be advised to the server) to \a identity. |
| 131 | |
| 132 | \note it is possible to set an identity whose length is greater than |
| 133 | maximumIdentityLength(); in this case, only the first maximumIdentityLength() |
| 134 | bytes will be actually sent to the server. |
| 135 | |
| 136 | \sa identity(), maximumIdentityLength() |
| 137 | */ |
| 138 | void QSslPreSharedKeyAuthenticator::setIdentity(const QByteArray &identity) |
| 139 | { |
| 140 | d->identity = identity; |
| 141 | } |
| 142 | |
| 143 | /*! |
| 144 | Returns the PSK client identity. |
| 145 | |
| 146 | \sa setIdentity() |
| 147 | */ |
| 148 | QByteArray QSslPreSharedKeyAuthenticator::identity() const |
| 149 | { |
| 150 | return d->identity; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /*! |
| 155 | Returns the maximum length, in bytes, of the PSK client identity. |
| 156 | |
| 157 | \note it is possible to set an identity whose length is greater than |
| 158 | maximumIdentityLength(); in this case, only the first maximumIdentityLength() |
| 159 | bytes will be actually sent to the server. |
| 160 | |
| 161 | \sa setIdentity() |
| 162 | */ |
| 163 | int QSslPreSharedKeyAuthenticator::maximumIdentityLength() const |
| 164 | { |
| 165 | return d->maximumIdentityLength; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /*! |
| 170 | Sets the pre shared key to \a preSharedKey. |
| 171 | |
| 172 | \note it is possible to set a key whose length is greater than the |
| 173 | maximumPreSharedKeyLength(); in this case, only the first |
| 174 | maximumPreSharedKeyLength() bytes will be actually sent to the server. |
| 175 | |
| 176 | \sa preSharedKey(), maximumPreSharedKeyLength(), QByteArray::fromHex() |
| 177 | */ |
| 178 | void QSslPreSharedKeyAuthenticator::setPreSharedKey(const QByteArray &preSharedKey) |
| 179 | { |
| 180 | d->preSharedKey = preSharedKey; |
| 181 | } |
| 182 | |
| 183 | /*! |
| 184 | Returns the pre shared key. |
| 185 | |
| 186 | \sa setPreSharedKey() |
| 187 | */ |
| 188 | QByteArray QSslPreSharedKeyAuthenticator::preSharedKey() const |
| 189 | { |
| 190 | return d->preSharedKey; |
| 191 | } |
| 192 | |
| 193 | /*! |
| 194 | Returns the maximum length, in bytes, of the pre shared key. |
| 195 | |
| 196 | \note it is possible to set a key whose length is greater than the |
| 197 | maximumPreSharedKeyLength(); in this case, only the first |
| 198 | maximumPreSharedKeyLength() bytes will be actually sent to the server. |
| 199 | |
| 200 | \sa setPreSharedKey() |
| 201 | */ |
| 202 | int QSslPreSharedKeyAuthenticator::maximumPreSharedKeyLength() const |
| 203 | { |
| 204 | return d->maximumPreSharedKeyLength; |
| 205 | } |
| 206 | |
| 207 | /*! |
| 208 | \fn bool QSslPreSharedKeyAuthenticator::operator==(const QSslPreSharedKeyAuthenticator &lhs, const QSslPreSharedKeyAuthenticator &rhs) |
| 209 | \since 5.5 |
| 210 | |
| 211 | Returns \c true if the authenticator object \a lhs is equal to \a rhs; |
| 212 | \c false otherwise. |
| 213 | |
| 214 | Two authenticator objects are equal if and only if they have the same |
| 215 | identity hint, identity, pre shared key, maximum length for the identity |
| 216 | and maximum length for the pre shared key. |
| 217 | */ |
| 218 | |
| 219 | /*! |
| 220 | \fn bool QSslPreSharedKeyAuthenticator::operator!=(const QSslPreSharedKeyAuthenticator &lhs, const QSslPreSharedKeyAuthenticator &rhs) |
| 221 | \since 5.5 |
| 222 | |
| 223 | Returns \c true if the authenticator object \a lhs is not equal to \a rhs; |
| 224 | \c false otherwise. |
| 225 | */ |
| 226 | |
| 227 | /*! |
| 228 | \internal |
| 229 | */ |
| 230 | bool QSslPreSharedKeyAuthenticator::isEqual(const QSslPreSharedKeyAuthenticator &other) const |
| 231 | { |
| 232 | return ((d == other.d) || |
| 233 | (d->identityHint == other.d->identityHint && |
| 234 | d->identity == other.d->identity && |
| 235 | d->maximumIdentityLength == other.d->maximumIdentityLength && |
| 236 | d->preSharedKey == other.d->preSharedKey && |
| 237 | d->maximumPreSharedKeyLength == other.d->maximumPreSharedKeyLength)); |
| 238 | } |
| 239 | |
| 240 | QT_END_NAMESPACE |
| 241 | |