| 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 "qsslellipticcurve.h" |
| 5 | #include "qtlsbackend_p.h" |
| 6 | #include "qsslsocket_p.h" |
| 7 | |
| 8 | #ifndef QT_NO_DEBUG_STREAM |
| 9 | #include <QDebug> |
| 10 | #endif |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | QT_IMPL_METATYPE_EXTERN(QSslEllipticCurve) |
| 15 | |
| 16 | /*! |
| 17 | \class QSslEllipticCurve |
| 18 | \since 5.5 |
| 19 | |
| 20 | \brief Represents an elliptic curve for use by elliptic-curve cipher algorithms. |
| 21 | |
| 22 | \reentrant |
| 23 | \ingroup network |
| 24 | \ingroup ssl |
| 25 | \inmodule QtNetwork |
| 26 | |
| 27 | The class QSslEllipticCurve represents an elliptic curve for use by |
| 28 | elliptic-curve cipher algorithms. |
| 29 | |
| 30 | Elliptic curves can be constructed from a "short name" (SN) (fromShortName()), |
| 31 | and by a call to QSslConfiguration::supportedEllipticCurves(). |
| 32 | |
| 33 | QSslEllipticCurve instances can be compared for equality and can be used as keys |
| 34 | in QHash and QSet. They cannot be used as key in a QMap. |
| 35 | |
| 36 | \note This class is currently only supported in OpenSSL. |
| 37 | */ |
| 38 | |
| 39 | /*! |
| 40 | \fn QSslEllipticCurve::QSslEllipticCurve() |
| 41 | |
| 42 | Constructs an invalid elliptic curve. |
| 43 | |
| 44 | \sa isValid(), QSslConfiguration::supportedEllipticCurves() |
| 45 | */ |
| 46 | |
| 47 | /*! |
| 48 | Returns an QSslEllipticCurve instance representing the |
| 49 | named curve \a name. The \a name is the conventional short |
| 50 | name for the curve, as represented by RFC 4492 (for instance \c{secp521r1}), |
| 51 | or as NIST short names (for instance \c{P-256}). The actual set of |
| 52 | recognized names depends on the SSL implementation. |
| 53 | |
| 54 | If the given \a name is not supported, returns an invalid QSslEllipticCurve instance. |
| 55 | |
| 56 | \note The OpenSSL implementation of this function treats the name case-sensitively. |
| 57 | |
| 58 | \sa shortName() |
| 59 | */ |
| 60 | QSslEllipticCurve QSslEllipticCurve::fromShortName(const QString &name) |
| 61 | { |
| 62 | QSslEllipticCurve result; |
| 63 | if (name.isEmpty()) |
| 64 | return result; |
| 65 | |
| 66 | if (const auto *tlsBackend = QSslSocketPrivate::tlsBackendInUse()) |
| 67 | result.id = tlsBackend->curveIdFromShortName(name); |
| 68 | |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | /*! |
| 73 | Returns an QSslEllipticCurve instance representing the named curve \a name. |
| 74 | The \a name is a long name for the curve, whose exact spelling depends on the |
| 75 | SSL implementation. |
| 76 | |
| 77 | If the given \a name is not supported, returns an invalid QSslEllipticCurve instance. |
| 78 | |
| 79 | \note The OpenSSL implementation of this function treats the name case-sensitively. |
| 80 | |
| 81 | \sa longName() |
| 82 | */ |
| 83 | QSslEllipticCurve QSslEllipticCurve::fromLongName(const QString &name) |
| 84 | { |
| 85 | QSslEllipticCurve result; |
| 86 | if (name.isEmpty()) |
| 87 | return result; |
| 88 | |
| 89 | if (const auto *tlsBackend = QSslSocketPrivate::tlsBackendInUse()) |
| 90 | result.id = tlsBackend->curveIdFromLongName(name); |
| 91 | |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | /*! |
| 96 | Returns the conventional short name for this curve. If this |
| 97 | curve is invalid, returns an empty string. |
| 98 | |
| 99 | \sa longName() |
| 100 | */ |
| 101 | QString QSslEllipticCurve::shortName() const |
| 102 | { |
| 103 | QString name; |
| 104 | |
| 105 | if (const auto *tlsBackend = QSslSocketPrivate::tlsBackendInUse()) |
| 106 | name = tlsBackend->shortNameForId(cid: id); |
| 107 | |
| 108 | return name; |
| 109 | } |
| 110 | |
| 111 | /*! |
| 112 | Returns the conventional long name for this curve. If this |
| 113 | curve is invalid, returns an empty string. |
| 114 | |
| 115 | \sa shortName() |
| 116 | */ |
| 117 | QString QSslEllipticCurve::longName() const |
| 118 | { |
| 119 | QString name; |
| 120 | |
| 121 | if (const auto *tlsBackend = QSslSocketPrivate::tlsBackendInUse()) |
| 122 | name = tlsBackend->longNameForId(cid: id); |
| 123 | |
| 124 | return name; |
| 125 | } |
| 126 | |
| 127 | /*! |
| 128 | \fn bool QSslEllipticCurve::isValid() const |
| 129 | |
| 130 | Returns true if this elliptic curve is a valid curve, false otherwise. |
| 131 | */ |
| 132 | |
| 133 | /*! |
| 134 | Returns true if this elliptic curve is one of the named curves that can be |
| 135 | used in the key exchange when using an elliptic curve cipher with TLS; |
| 136 | false otherwise. |
| 137 | */ |
| 138 | bool QSslEllipticCurve::isTlsNamedCurve() const noexcept |
| 139 | { |
| 140 | if (const auto *tlsBackend = QSslSocketPrivate::tlsBackendInUse()) |
| 141 | return tlsBackend->isTlsNamedCurve(cid: id); |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /*! |
| 148 | \fn bool QSslEllipticCurve::operator==(QSslEllipticCurve lhs, QSslEllipticCurve rhs) |
| 149 | \since 5.5 |
| 150 | |
| 151 | Returns true if the curve \a lhs represents the same curve of \a rhs; |
| 152 | */ |
| 153 | |
| 154 | /*! |
| 155 | \fn bool QSslEllipticCurve::operator!=(QSslEllipticCurve lhs, QSslEllipticCurve rhs) |
| 156 | \since 5.5 |
| 157 | |
| 158 | Returns true if the curve \a lhs represents a different curve than \a rhs; |
| 159 | false otherwise. |
| 160 | */ |
| 161 | |
| 162 | /*! |
| 163 | \fn size_t qHash(QSslEllipticCurve key, size_t seed) |
| 164 | \since 5.5 |
| 165 | \qhashold{QHash} |
| 166 | */ |
| 167 | |
| 168 | #ifndef QT_NO_DEBUG_STREAM |
| 169 | /*! |
| 170 | \relates QSslEllipticCurve |
| 171 | \since 5.5 |
| 172 | |
| 173 | Writes the elliptic curve \a curve into the debug object \a debug for |
| 174 | debugging purposes. |
| 175 | |
| 176 | \sa {Debugging Techniques} |
| 177 | */ |
| 178 | QDebug operator<<(QDebug debug, QSslEllipticCurve curve) |
| 179 | { |
| 180 | QDebugStateSaver saver(debug); |
| 181 | debug.resetFormat().nospace(); |
| 182 | debug << "QSslEllipticCurve(" << curve.shortName() << ')'; |
| 183 | return debug; |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | QT_END_NAMESPACE |
| 188 | |