QtBase  v6.3.1
qssldiffiehellmanparameters_openssl.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 Mikkel Krautz <mikkel@krautz.dk>
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 
42 #include "qtlsbackend_openssl_p.h"
43 
44 #include <QtNetwork/private/qsslsocket_p.h>
45 
46 #include <QtCore/qscopeguard.h>
47 #include <QtCore/qbytearray.h>
48 #include <QtCore/qiodevice.h>
49 #include <QtCore/qdebug.h>
50 
51 #include <openssl/bn.h>
52 #include <openssl/dh.h>
53 
55 
56 #ifndef OPENSSL_NO_DEPRECATED_3_0
57 
58 namespace {
59 
60 bool isSafeDH(DH *dh)
61 {
62  int status = 0;
63  int bad = 0;
64 
65  // TLSTODO: check it's needed or if supportsSsl()
66  // is enough.
68 
69  // From https://wiki.openssl.org/index.php/Diffie-Hellman_parameters:
70  //
71  // The additional call to BN_mod_word(dh->p, 24)
72  // (and unmasking of DH_NOT_SUITABLE_GENERATOR)
73  // is performed to ensure your program accepts
74  // IETF group parameters. OpenSSL checks the prime
75  // is congruent to 11 when g = 2; while the IETF's
76  // primes are congruent to 23 when g = 2.
77  // Without the test, the IETF parameters would
78  // fail validation. For details, see Diffie-Hellman
79  // Parameter Check (when g = 2, must p mod 24 == 11?).
80  // Mark p < 1024 bits as unsafe.
81  if (q_DH_bits(dh) < 1024)
82  return false;
83 
84  if (q_DH_check(dh, &status) != 1)
85  return false;
86 
87  const BIGNUM *p = nullptr;
88  const BIGNUM *q = nullptr;
89  const BIGNUM *g = nullptr;
90  q_DH_get0_pqg(dh, &p, &q, &g);
91 
92  if (q_BN_is_word(const_cast<BIGNUM *>(g), DH_GENERATOR_2)) {
93  const unsigned long residue = q_BN_mod_word(p, 24);
94  if (residue == 11 || residue == 23)
95  status &= ~DH_NOT_SUITABLE_GENERATOR;
96  }
97 
98  bad |= DH_CHECK_P_NOT_PRIME;
99  bad |= DH_CHECK_P_NOT_SAFE_PRIME;
100  bad |= DH_NOT_SUITABLE_GENERATOR;
101 
102  return !(status & bad);
103 }
104 
105 } // unnamed namespace
106 
107 #endif
108 
109 int QTlsBackendOpenSSL::dhParametersFromDer(const QByteArray &der, QByteArray *derData) const
110 {
111 #ifndef OPENSSL_NO_DEPRECATED_3_0
112  Q_ASSERT(derData);
113 
114  if (der.isEmpty())
116 
117  const unsigned char *data = reinterpret_cast<const unsigned char *>(der.data());
118  const int len = der.size();
119 
120  // TLSTODO: check it's needed (loading ciphers and certs in
121  // addition to the library!)
123 
124  DH *dh = q_d2i_DHparams(nullptr, &data, len);
125  if (dh) {
126  const auto dhRaii = qScopeGuard([dh] {q_DH_free(dh);});
127 
128  if (isSafeDH(dh))
129  *derData = der;
130  else
132  } else {
134  }
135 #else
136  Q_UNUSED(der);
137  Q_UNUSED(derData);
138  qCWarning(lcTlsBackend, "Diffie-Hellman parameters are not supported, because OpenSSL v3 was built with deprecated API removed");
139 #endif
140  return DHParams::NoError;
141 }
142 
143 int QTlsBackendOpenSSL::dhParametersFromPem(const QByteArray &pem, QByteArray *data) const
144 {
145 #ifndef OPENSSL_NO_DEPRECATED_3_0
146  Q_ASSERT(data);
147 
148  if (pem.isEmpty())
150 
151  // TLSTODO: check it was not a cargo-cult programming in case of
152  // DH ...
154 
155  BIO *bio = q_BIO_new_mem_buf(const_cast<char *>(pem.data()), pem.size());
156  if (!bio)
158 
159  const auto bioRaii = qScopeGuard([bio]
160  {
161  q_BIO_free(bio);
162  });
163 
164  DH *dh = nullptr;
165  q_PEM_read_bio_DHparams(bio, &dh, nullptr, nullptr);
166 
167  if (dh) {
168  const auto dhGuard = qScopeGuard([dh]
169  {
170  q_DH_free(dh);
171  });
172 
173  if (isSafeDH(dh)) {
174  char *buf = nullptr;
175  const int len = q_i2d_DHparams(dh, reinterpret_cast<unsigned char **>(&buf));
176  if (len > 0)
177  *data = QByteArray(buf, len);
178  else
180  } else {
182  }
183  } else {
185  }
186 #else
187  Q_UNUSED(pem);
188  Q_UNUSED(data);
189  qCWarning(lcTlsBackend, "Diffie-Hellman parameters are not supported, because OpenSSL v3 was built with deprecated API removed");
190 #endif
191  return DHParams::NoError;
192 }
193 
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
char * data()
Definition: qbytearray.h:516
qsizetype size() const noexcept
Definition: qbytearray.h:470
bool isEmpty() const noexcept
Definition: qbytearray.h:129
static void ensureInitialized()
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qCWarning(category,...)
GLenum GLuint GLenum GLsizei const GLchar * buf
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean GLboolean g
GLenum GLsizei len
Definition: qopenglext.h:3292
GLdouble GLdouble GLdouble GLdouble q
Definition: qopenglext.h:259
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition: qscopeguard.h:93
int q_DH_check(DH *dh, int *codes)
void q_DH_free(DH *dh)
BN_ULONG q_BN_mod_word(const BIGNUM *a, BN_ULONG w)
int q_i2d_DHparams(DH *a, unsigned char **p)
int q_DH_bits(DH *dh)
int q_BN_is_word(BIGNUM *a, BN_ULONG w)
DH * q_PEM_read_bio_DHparams(BIO *a, DH **b, pem_password_cb *c, void *d)
DH * q_d2i_DHparams(DH **a, const unsigned char **pp, long length)
void q_DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
int q_BIO_free(BIO *a)
BIO * q_BIO_new_mem_buf(void *a, int b)
Q_UNUSED(salary)
[21]