9#include <QtNetwork/private/qsslkey_p.h>
11#include <QtNetwork/qsslsocket.h>
13#include <QtCore/qscopeguard.h>
34void TlsKeyOpenSSL::readGenericKey(BIO *bio,
void *phrase, QSsl::KeyType keyType)
36 if (keyType == QSsl::PublicKey)
45void TlsKeyOpenSSL::
decodePem(KeyType type, KeyAlgorithm algorithm,
const QByteArray &pem,
46 const QByteArray &passPhrase,
bool deepClear)
52 keyAlgorithm = algorithm;
56 BIO *bio = q_BIO_new_mem_buf(
const_cast<
char *>(pem.data()), pem.size());
60 const auto bioRaii = qScopeGuard([bio]{
q_BIO_free(bio
);});
62 void *phrase =
const_cast<
char *>(passPhrase.data());
64#ifdef OPENSSL_NO_DEPRECATED_3_0
65 readGenericKey(bio, phrase, keyType);
68 if (algorithm == QSsl::Rsa) {
69 RSA *result = (type == QSsl::PublicKey)
70 ? q_PEM_read_bio_RSA_PUBKEY(bio, &rsa,
nullptr, phrase)
71 : q_PEM_read_bio_RSAPrivateKey(bio, &rsa,
nullptr, phrase);
72 if (rsa && rsa == result)
74 }
else if (algorithm == QSsl::Dsa) {
75 DSA *result = (type == QSsl::PublicKey)
76 ? q_PEM_read_bio_DSA_PUBKEY(bio, &dsa,
nullptr, phrase)
77 : q_PEM_read_bio_DSAPrivateKey(bio, &dsa,
nullptr, phrase);
78 if (dsa && dsa == result)
80 }
else if (algorithm == QSsl::Dh) {
81 EVP_PKEY *result = (type == QSsl::PublicKey)
82 ? q_PEM_read_bio_PUBKEY(bio,
nullptr,
nullptr, phrase)
83 : q_PEM_read_bio_PrivateKey(bio,
nullptr,
nullptr, phrase);
90 }
else if (algorithm == QSsl::Ec) {
91 EC_KEY *result = (type == QSsl::PublicKey)
92 ? q_PEM_read_bio_EC_PUBKEY(bio, &ec,
nullptr, phrase)
93 : q_PEM_read_bio_ECPrivateKey(bio, &ec,
nullptr, phrase);
94 if (ec && ec == result)
97 }
else if (algorithm == QSsl::MlDsa) {
98 readGenericKey(bio, phrase, keyType);
106 QByteArray header = pemHeader();
107 QByteArray footer = pemFooter();
111 int headerIndex = der.indexOf(header);
112 int footerIndex = der.indexOf(footer, headerIndex + header.size());
113 if (type() != QSsl::PublicKey) {
114 if (headerIndex == -1 || footerIndex == -1) {
115 header = pkcs8Header(
true);
116 footer = pkcs8Footer(
true);
117 headerIndex = der.indexOf(header);
118 footerIndex = der.indexOf(footer, headerIndex + header.size());
120 if (headerIndex == -1 || footerIndex == -1) {
121 header = pkcs8Header(
false);
122 footer = pkcs8Footer(
false);
123 headerIndex = der.indexOf(header);
124 footerIndex = der.indexOf(footer, headerIndex + header.size());
127 if (headerIndex == -1 || footerIndex == -1)
130 der = der.mid(headerIndex + header.size(), footerIndex - (headerIndex + header.size()));
132 if (der.contains(
"Proc-Type:")) {
135 while (i < der.size()) {
136 int j = der.indexOf(
':', i);
139 const QByteArray field = der.mid(i, j - i).trimmed();
144 i = der.indexOf(
'\n', j);
147 if (!value.isEmpty())
150 bool hasCR = (i && der[i-1] ==
'\r');
151 int length = i -(hasCR ? 1: 0) - j;
152 value += der.mid(j, length).trimmed();
154 }
while (i < der.size() && (der.at(i) ==
' ' || der.at(i) ==
'\t'));
158 headers->insert(field, value);
163 return QByteArray::fromBase64(der);
170#ifndef OPENSSL_NO_DEPRECATED_3_0
171 if (algorithm() == QSsl::Rsa && rsa) {
176 if (algorithm() == QSsl::Dsa && dsa) {
181 if (algorithm() == QSsl::Dh && dh) {
187 if (algorithm() == QSsl::Ec && ec) {
195 if (algorithm() == QSsl::Opaque && opaque) {
206 genericKey =
nullptr;
212 if (keyAlgorithm == QSsl::Opaque)
213 return Qt::HANDLE(opaque);
215#ifndef OPENSSL_NO_DEPRECATED_3_0
216 switch (keyAlgorithm) {
218 return Qt::HANDLE(rsa);
220 return Qt::HANDLE(dsa);
222 return Qt::HANDLE(dh);
225 return Qt::HANDLE(ec);
228 return Qt::HANDLE(
nullptr);
231 qCWarning(lcTlsBackend,
232 "This version of OpenSSL disabled direct manipulation with RSA/DSA/DH/EC_KEY structures, consider using QSsl::Opaque instead.");
233 return Qt::HANDLE(genericKey);
239 if (isNull() || algorithm() == QSsl::Opaque)
242#if defined(OPENSSL_VERSION_MAJOR
) && OPENSSL_VERSION_MAJOR
>= 3
243 if (algorithm() == QSsl::MlDsa) {
244 Q_ASSERT(genericKey);
245 return q_EVP_PKEY_get_security_bits(genericKey);
249#ifndef OPENSSL_NO_DEPRECATED_3_0
250 switch (algorithm()) {
265 Q_ASSERT(genericKey);
266 return q_EVP_PKEY_get_bits(genericKey);
272 if (!QSslSocket::supportsSsl() || isNull() || algorithm() == QSsl::Opaque)
275 const EVP_CIPHER *cipher =
nullptr;
276 if (type() == QSsl::PrivateKey && !passPhrase.isEmpty()) {
277#ifndef OPENSSL_NO_DES
288 const auto bioRaii = qScopeGuard([bio]{
q_BIO_free(bio
);});
290#ifndef OPENSSL_NO_DEPRECATED_3_0
292#define write_pubkey(alg, key) q_PEM_write_bio_##alg##_PUBKEY(bio, key)
293#define write_privatekey(alg, key)
294 q_PEM_write_bio_##alg##PrivateKey(bio, key, cipher, (uchar *)passPhrase.data(),
295 passPhrase.size(), nullptr, nullptr)
299#define write_pubkey(alg, key) q_PEM_write_bio_PUBKEY(bio, genericKey)
300#define write_privatekey(alg, key)
301 q_PEM_write_bio_PrivateKey_traditional(bio, genericKey, cipher, (uchar *)passPhrase.data(), passPhrase.size(), nullptr, nullptr)
306 if (algorithm() == QSsl::Rsa) {
307 if (type() == QSsl::PublicKey) {
313 }
else if (algorithm() == QSsl::Dsa) {
314 if (type() == QSsl::PublicKey) {
320 }
else if (algorithm() == QSsl::Dh) {
321#ifdef OPENSSL_NO_DEPRECATED_3_0
322 EVP_PKEY *result = genericKey;
330 if (type() == QSsl::PublicKey) {
333 }
else if (!q_PEM_write_bio_PrivateKey(bio, result, cipher, (uchar *)passPhrase.data(),
334 passPhrase.size(),
nullptr,
nullptr)) {
338 }
else if (algorithm() == QSsl::Ec) {
339 if (type() == QSsl::PublicKey) {
347 }
else if (algorithm() == QSsl::MlDsa) {
348 if (type() == QSsl::PublicKey) {
352 if (!q_PEM_write_bio_PrivateKey(bio, genericKey, cipher, (uchar *)passPhrase.data(),
353 passPhrase.size(),
nullptr,
nullptr))
362 char *data =
nullptr;
364 if (size > 0 && data)
365 pem = QByteArray(data, size);
392#ifndef OPENSSL_NO_DEPRECATED_3_0
393#define get_key(key, alg) key = q_EVP_PKEY_get1_##alg(pkey)
395#define get_key(key, alg) q_EVP_PKEY_up_ref(pkey); genericKey = pkey;
398#if defined(OPENSSL_VERSION_MAJOR
) && OPENSSL_VERSION_MAJOR
>= 3
400 const auto keyTypeName = QLatin1StringView(q_EVP_PKEY_type_name(pkey));
401 if (keyTypeName.contains(QLatin1String(
"ML-DSA")) ||
402 keyTypeName.contains(QLatin1String(
"mldsa"))) {
403 keyAlgorithm = QSsl::MlDsa;
405 keyType = QSsl::PrivateKey;
415 keyAlgorithm = QSsl::Rsa;
416 keyType = QSsl::PrivateKey;
421 keyAlgorithm = QSsl::Dsa;
422 keyType = QSsl::PrivateKey;
427 keyAlgorithm = QSsl::Dh;
428 keyType = QSsl::PrivateKey;
434 keyAlgorithm = QSsl::Ec;
435 keyType = QSsl::PrivateKey;
450 const QByteArray &key,
const QByteArray &iv,
bool enc)
452 const EVP_CIPHER *type =
nullptr;
457#ifndef OPENSSL_NO_DES
461 case Cipher::DesEde3Cbc:
462#ifndef OPENSSL_NO_DES
467#ifndef OPENSSL_NO_RC2
471 case Cipher::Aes128Cbc:
474 case Cipher::Aes192Cbc:
477 case Cipher::Aes256Cbc:
486 output.resize(data.size() + EVP_MAX_BLOCK_LENGTH);
496 q_EVP_CIPHER_CTX_set_key_length(ctx, key.size());
497 if (cipher == Cipher::Rc2Cbc)
498 q_EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, 8 * key.size(),
nullptr);
501 reinterpret_cast<
const unsigned char *>(key.constData())
,
502 reinterpret_cast<
const unsigned char *>(iv.constData())
,
504 q_EVP_CipherUpdate(ctx,
505 reinterpret_cast<
unsigned char *>(output.data()), &len,
506 reinterpret_cast<
const unsigned char *>(data.constData()), data.size());
508 reinterpret_cast<
unsigned char *>(output.data()) + len
, &i
);
514 return output.left(len);
518 const QByteArray &key,
const QByteArray &iv)
const
520 return doCrypt(cipher, data, key, iv,
false);
524 const QByteArray &key,
const QByteArray &iv)
const
526 return doCrypt(cipher, data, key, iv,
true);
531 TlsKeyOpenSSL *tlsKey =
new TlsKeyOpenSSL;
532 std::unique_ptr<TlsKeyOpenSSL> keyRaii(tlsKey);
534 tlsKey->keyType = QSsl::PublicKey;
536#ifndef OPENSSL_NO_DEPRECATED_3_0
538#define get_pubkey(keyName, alg) tlsKey->keyName = q_EVP_PKEY_get1_##alg(pkey)
542#define get_pubkey(a, b) tlsKey->genericKey = pkey
550#if defined(OPENSSL_VERSION_MAJOR
) && OPENSSL_VERSION_MAJOR
>= 3
552 const auto keyTypeName = QLatin1StringView(q_EVP_PKEY_type_name(pkey));
555 if (keyType == EVP_PKEY_RSA) {
557 tlsKey->keyAlgorithm = QSsl::Rsa;
559 }
else if (keyType == EVP_PKEY_DSA) {
561 tlsKey->keyAlgorithm = QSsl::Dsa;
564 }
else if (keyType == EVP_PKEY_EC) {
566 tlsKey->keyAlgorithm = QSsl::Ec;
569 }
else if (keyType == EVP_PKEY_DH) {
571#if defined(OPENSSL_VERSION_MAJOR
) && OPENSSL_VERSION_MAJOR
>= 3
572 }
else if (keyTypeName.contains(QLatin1String(
"ML-DSA")) ||
573 keyTypeName.contains(QLatin1String(
"mldsa"))) {
575 tlsKey->keyAlgorithm = QSsl::MlDsa;
582#ifndef OPENSSL_NO_DEPRECATED_3_0
586 return keyRaii.release();
static void logAndClearErrorQueue()
void decodePem(KeyType type, KeyAlgorithm algorithm, const QByteArray &pem, const QByteArray &passPhrase, bool deepClear) override
void clear(bool deep) override
bool fromEVP_PKEY(EVP_PKEY *pkey)
QByteArray derFromPem(const QByteArray &pem, QMap< QByteArray, QByteArray > *headers) const override
QByteArray decrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv) const override
QByteArray toPem(const QByteArray &passPhrase) const override
int length() const override
Qt::HANDLE handle() const override
QByteArray encrypt(Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv) const override
static TlsKeyOpenSSL * publicKeyFromX509(X509 *x)
Namespace containing onternal types that TLS backends implement.
QByteArray doCrypt(QSslKeyPrivate::Cipher cipher, const QByteArray &data, const QByteArray &key, const QByteArray &iv, bool enc)
int q_EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
const EVP_CIPHER * q_EVP_des_cbc()
const EVP_CIPHER * q_EVP_aes_192_cbc()
const EVP_CIPHER * q_EVP_aes_256_cbc()
int q_EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, const unsigned char *key, const unsigned char *iv, int enc)
const EVP_CIPHER * q_EVP_rc2_cbc()
int q_PEM_write_bio_RSA_PUBKEY(BIO *a, RSA *b)
#define q_BIO_get_mem_data(b, pp)
void q_EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *a)
RSA * q_EVP_PKEY_get1_RSA(EVP_PKEY *a)
int q_PEM_write_bio_DSA_PUBKEY(BIO *a, DSA *b)
EVP_PKEY * q_PEM_read_bio_PrivateKey(BIO *a, EVP_PKEY **b, pem_password_cb *c, void *d)
EVP_PKEY * q_EVP_PKEY_new()
void q_EVP_PKEY_free(EVP_PKEY *a)
void q_EC_KEY_free(EC_KEY *ecdh)
BIO * q_BIO_new(const BIO_METHOD *a)
const EVP_CIPHER * q_EVP_des_ede3_cbc()
EVP_PKEY * q_PEM_read_bio_PUBKEY(BIO *a, EVP_PKEY **b, pem_password_cb *c, void *d)
int q_EVP_PKEY_up_ref(EVP_PKEY *a)
EVP_PKEY * q_X509_get_pubkey(X509 *a)
EC_KEY * q_EVP_PKEY_get1_EC_KEY(EVP_PKEY *a)
int q_EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
int q_EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc)
const BIO_METHOD * q_BIO_s_mem()
int q_PEM_write_bio_PUBKEY(BIO *a, EVP_PKEY *b)
int q_EVP_PKEY_set1_DH(EVP_PKEY *a, DH *b)
int q_EC_GROUP_get_degree(const EC_GROUP *g)
DH * q_EVP_PKEY_get1_DH(EVP_PKEY *a)
DSA * q_EVP_PKEY_get1_DSA(EVP_PKEY *a)
const EVP_CIPHER * q_EVP_aes_128_cbc()
int q_PEM_write_bio_EC_PUBKEY(BIO *a, EC_KEY *b)
EVP_CIPHER_CTX * q_EVP_CIPHER_CTX_new()
int q_EVP_PKEY_type(int a)
const EC_GROUP * q_EC_KEY_get0_group(const EC_KEY *k)
#define write_pubkey(alg, key)
#define write_privatekey(alg, key)
#define get_key(key, alg)
#define get_pubkey(keyName, alg)