| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2014 Governikus GmbH & Co. KG. | 
| 4 | ** Copyright (C) 2016 Richard J. Moore <rich@kde.org> | 
| 5 | ** Contact: https://www.qt.io/licensing/ | 
| 6 | ** | 
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | 
| 8 | ** | 
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 10 | ** Commercial License Usage | 
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 12 | ** accordance with the commercial license agreement provided with the | 
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 17 | ** | 
| 18 | ** GNU Lesser General Public License Usage | 
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 20 | ** General Public License version 3 as published by the Free Software | 
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 22 | ** packaging of this file. Please review the following information to | 
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 25 | ** | 
| 26 | ** GNU General Public License Usage | 
| 27 | ** Alternatively, this file may be used under the terms of the GNU | 
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 29 | ** Public license version 3 or any later version approved by the KDE Free | 
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 32 | ** included in the packaging of this file. Please review the following | 
| 33 | ** information to ensure the GNU General Public License requirements will | 
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 36 | ** | 
| 37 | ** $QT_END_LICENSE$ | 
| 38 | ** | 
| 39 | ****************************************************************************/ | 
| 40 |  | 
| 41 | #include "qsslellipticcurve.h" | 
| 42 | #include "qsslsocket_p.h" | 
| 43 | #include "qsslsocket_openssl_symbols_p.h" | 
| 44 |  | 
| 45 | #include <openssl/ssl.h> | 
| 46 | #include <openssl/obj_mac.h> | 
| 47 |  | 
| 48 | #include <algorithm> | 
| 49 |  | 
| 50 | QT_BEGIN_NAMESPACE | 
| 51 |  | 
| 52 | QString QSslEllipticCurve::shortName() const | 
| 53 | { | 
| 54 |     QString result; | 
| 55 | #ifndef OPENSSL_NO_EC | 
| 56 |     if (id != 0) | 
| 57 |         result = QString::fromLatin1(str: q_OBJ_nid2sn(a: id)); | 
| 58 | #endif | 
| 59 |     return result; | 
| 60 | } | 
| 61 |  | 
| 62 | QString QSslEllipticCurve::longName() const | 
| 63 | { | 
| 64 |     QString result; | 
| 65 | #ifndef OPENSSL_NO_EC | 
| 66 |     if (id != 0) | 
| 67 |         result = QString::fromLatin1(str: q_OBJ_nid2ln(a: id)); | 
| 68 | #endif | 
| 69 |     return result; | 
| 70 | } | 
| 71 |  | 
| 72 | QSslEllipticCurve QSslEllipticCurve::fromShortName(const QString &name) | 
| 73 | { | 
| 74 |     if (name.isEmpty()) | 
| 75 |         return QSslEllipticCurve(); | 
| 76 |  | 
| 77 |     QSslSocketPrivate::ensureInitialized(); | 
| 78 |  | 
| 79 |     QSslEllipticCurve result; | 
| 80 |  | 
| 81 | #ifndef OPENSSL_NO_EC | 
| 82 |  | 
| 83 |     const QByteArray curveNameLatin1 = name.toLatin1(); | 
| 84 |     int nid = q_OBJ_sn2nid(s: curveNameLatin1.data()); | 
| 85 |  | 
| 86 |     if (nid == 0) | 
| 87 |         nid = q_EC_curve_nist2nid(name: curveNameLatin1.data()); | 
| 88 |  | 
| 89 |     result.id = nid; | 
| 90 |  | 
| 91 | #endif // !OPENSSL_NO_EC | 
| 92 |  | 
| 93 |     return result; | 
| 94 | } | 
| 95 |  | 
| 96 | QSslEllipticCurve QSslEllipticCurve::fromLongName(const QString &name) | 
| 97 | { | 
| 98 |     if (name.isEmpty()) | 
| 99 |         return QSslEllipticCurve(); | 
| 100 |  | 
| 101 |     QSslSocketPrivate::ensureInitialized(); | 
| 102 |  | 
| 103 |     QSslEllipticCurve result; | 
| 104 |  | 
| 105 | #ifndef OPENSSL_NO_EC | 
| 106 |     const QByteArray curveNameLatin1 = name.toLatin1(); | 
| 107 |  | 
| 108 |     int nid = q_OBJ_ln2nid(s: curveNameLatin1.data()); | 
| 109 |     result.id = nid; | 
| 110 | #endif | 
| 111 |  | 
| 112 |     return result; | 
| 113 | } | 
| 114 |  | 
| 115 |  | 
| 116 | // The brainpool curve NIDs (RFC 7027) have been introduced in OpenSSL 1.0.2, | 
| 117 | // redefine them here to make Qt compile with previous versions of OpenSSL | 
| 118 | // (yet correctly recognize them as TLS named curves). | 
| 119 | // See crypto/objects/obj_mac.h | 
| 120 | #ifndef NID_brainpoolP256r1 | 
| 121 | #define NID_brainpoolP256r1 927 | 
| 122 | #endif | 
| 123 |  | 
| 124 | #ifndef NID_brainpoolP384r1 | 
| 125 | #define NID_brainpoolP384r1 931 | 
| 126 | #endif | 
| 127 |  | 
| 128 | #ifndef NID_brainpoolP512r1 | 
| 129 | #define NID_brainpoolP512r1 933 | 
| 130 | #endif | 
| 131 |  | 
| 132 | // NIDs of named curves allowed in TLS as per RFCs 4492 and 7027, | 
| 133 | // see also https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8 | 
| 134 | static const int tlsNamedCurveNIDs[] = { | 
| 135 |     // RFC 4492 | 
| 136 |     NID_sect163k1, | 
| 137 |     NID_sect163r1, | 
| 138 |     NID_sect163r2, | 
| 139 |     NID_sect193r1, | 
| 140 |     NID_sect193r2, | 
| 141 |     NID_sect233k1, | 
| 142 |     NID_sect233r1, | 
| 143 |     NID_sect239k1, | 
| 144 |     NID_sect283k1, | 
| 145 |     NID_sect283r1, | 
| 146 |     NID_sect409k1, | 
| 147 |     NID_sect409r1, | 
| 148 |     NID_sect571k1, | 
| 149 |     NID_sect571r1, | 
| 150 |  | 
| 151 |     NID_secp160k1, | 
| 152 |     NID_secp160r1, | 
| 153 |     NID_secp160r2, | 
| 154 |     NID_secp192k1, | 
| 155 |     NID_X9_62_prime192v1, // secp192r1 | 
| 156 |     NID_secp224k1, | 
| 157 |     NID_secp224r1, | 
| 158 |     NID_secp256k1, | 
| 159 |     NID_X9_62_prime256v1, // secp256r1 | 
| 160 |     NID_secp384r1, | 
| 161 |     NID_secp521r1, | 
| 162 |  | 
| 163 |     // RFC 7027 | 
| 164 |     NID_brainpoolP256r1, | 
| 165 |     NID_brainpoolP384r1, | 
| 166 |     NID_brainpoolP512r1 | 
| 167 | }; | 
| 168 |  | 
| 169 | static const size_t tlsNamedCurveNIDCount = sizeof(tlsNamedCurveNIDs) / sizeof(tlsNamedCurveNIDs[0]); | 
| 170 |  | 
| 171 | bool QSslEllipticCurve::isTlsNamedCurve() const noexcept | 
| 172 | { | 
| 173 |     const int * const tlsNamedCurveNIDsEnd = tlsNamedCurveNIDs + tlsNamedCurveNIDCount; | 
| 174 |     return std::find(first: tlsNamedCurveNIDs, last: tlsNamedCurveNIDsEnd, val: id) != tlsNamedCurveNIDsEnd; | 
| 175 | } | 
| 176 |  | 
| 177 | QT_END_NAMESPACE | 
| 178 |  |