8#include <QtNetwork/private/qsslkey_p.h>
10#include <QtNetwork/qsslsocket.h>
12#include <QtCore/qscopeguard.h>
33void TlsKeyOpenSSL::
decodePem(KeyType type, KeyAlgorithm algorithm,
const QByteArray &pem,
34 const QByteArray &passPhrase,
bool deepClear)
40 keyAlgorithm = algorithm;
44 BIO *bio = q_BIO_new_mem_buf(
const_cast<
char *>(pem.data()), pem.size());
48 const auto bioRaii = qScopeGuard([bio]{
q_BIO_free(bio
);});
50 void *phrase =
const_cast<
char *>(passPhrase.data());
52#ifdef OPENSSL_NO_DEPRECATED_3_0
53 if (type == QSsl::PublicKey)
54 genericKey = q_PEM_read_bio_PUBKEY(bio,
nullptr,
nullptr, phrase);
56 genericKey = q_PEM_read_bio_PrivateKey(bio,
nullptr,
nullptr, phrase);
57 keyIsNull = !genericKey;
59 QTlsBackendOpenSSL::logAndClearErrorQueue();
62 if (algorithm == QSsl::Rsa) {
63 RSA *result = (type == QSsl::PublicKey)
64 ? q_PEM_read_bio_RSA_PUBKEY(bio, &rsa,
nullptr, phrase)
65 : q_PEM_read_bio_RSAPrivateKey(bio, &rsa,
nullptr, phrase);
66 if (rsa && rsa == result)
68 }
else if (algorithm == QSsl::Dsa) {
69 DSA *result = (type == QSsl::PublicKey)
70 ? q_PEM_read_bio_DSA_PUBKEY(bio, &dsa,
nullptr, phrase)
71 : q_PEM_read_bio_DSAPrivateKey(bio, &dsa,
nullptr, phrase);
72 if (dsa && dsa == result)
74 }
else if (algorithm == QSsl::Dh) {
75 EVP_PKEY *result = (type == QSsl::PublicKey)
76 ? q_PEM_read_bio_PUBKEY(bio,
nullptr,
nullptr, phrase)
77 : q_PEM_read_bio_PrivateKey(bio,
nullptr,
nullptr, phrase);
84 }
else if (algorithm == QSsl::Ec) {
85 EC_KEY *result = (type == QSsl::PublicKey)
86 ? q_PEM_read_bio_EC_PUBKEY(bio, &ec,
nullptr, phrase)
87 : q_PEM_read_bio_ECPrivateKey(bio, &ec,
nullptr, phrase);
88 if (ec && ec == result)
98 QByteArray header = pemHeader();
99 QByteArray footer = pemFooter();
103 int headerIndex = der.indexOf(header);
104 int footerIndex = der.indexOf(footer, headerIndex + header.size());
105 if (type() != QSsl::PublicKey) {
106 if (headerIndex == -1 || footerIndex == -1) {
107 header = pkcs8Header(
true);
108 footer = pkcs8Footer(
true);
109 headerIndex = der.indexOf(header);
110 footerIndex = der.indexOf(footer, headerIndex + header.size());
112 if (headerIndex == -1 || footerIndex == -1) {
113 header = pkcs8Header(
false);
114 footer = pkcs8Footer(
false);
115 headerIndex = der.indexOf(header);
116 footerIndex = der.indexOf(footer, headerIndex + header.size());
119 if (headerIndex == -1 || footerIndex == -1)
122 der = der.mid(headerIndex + header.size(), footerIndex - (headerIndex + header.size()));
124 if (der.contains(
"Proc-Type:")) {
127 while (i < der.size()) {
128 int j = der.indexOf(
':', i);
131 const QByteArray field = der.mid(i, j - i).trimmed();
136 i = der.indexOf(
'\n', j);
139 if (!value.isEmpty())
142 bool hasCR = (i && der[i-1] ==
'\r');
143 int length = i -(hasCR ? 1: 0) - j;
144 value += der.mid(j, length).trimmed();
146 }
while (i < der.size() && (der.at(i) ==
' ' || der.at(i) ==
'\t'));
150 headers->insert(field, value);
155 return QByteArray::fromBase64(der);
162#ifndef OPENSSL_NO_DEPRECATED_3_0
163 if (algorithm() == QSsl::Rsa && rsa) {
168 if (algorithm() == QSsl::Dsa && dsa) {
173 if (algorithm() == QSsl::Dh && dh) {
179 if (algorithm() == QSsl::Ec && ec) {
187 if (algorithm() == QSsl::Opaque && opaque) {
198 genericKey =
nullptr;
204 if (keyAlgorithm == QSsl::Opaque)
205 return Qt::HANDLE(opaque);
207#ifndef OPENSSL_NO_DEPRECATED_3_0
208 switch (keyAlgorithm) {
210 return Qt::HANDLE(rsa);
212 return Qt::HANDLE(dsa);
214 return Qt::HANDLE(dh);
217 return Qt::HANDLE(ec);
220 return Qt::HANDLE(
nullptr);
223 qCWarning(lcTlsBackend,
224 "This version of OpenSSL disabled direct manipulation with RSA/DSA/DH/EC_KEY structures, consider using QSsl::Opaque instead.");
225 return Qt::HANDLE(genericKey);
231 if (isNull() || algorithm() == QSsl::Opaque)
234#ifndef OPENSSL_NO_DEPRECATED_3_0
235 switch (algorithm()) {
250 Q_ASSERT(genericKey);
251 return q_EVP_PKEY_get_bits(genericKey);
257 if (!QSslSocket::supportsSsl() || isNull() || algorithm() == QSsl::Opaque)
260 const EVP_CIPHER *cipher =
nullptr;
261 if (type() == QSsl::PrivateKey && !passPhrase.isEmpty()) {
262#ifndef OPENSSL_NO_DES
273 const auto bioRaii = qScopeGuard([bio]{
q_BIO_free(bio
);});
275#ifndef OPENSSL_NO_DEPRECATED_3_0
277#define write_pubkey(alg, key) q_PEM_write_bio_##alg##_PUBKEY(bio, key)
278#define write_privatekey(alg, key)
279 q_PEM_write_bio_##alg##PrivateKey(bio, key, cipher, (uchar *)passPhrase.data(),
280 passPhrase.size(), nullptr, nullptr)
284#define write_pubkey(alg, key) q_PEM_write_bio_PUBKEY(bio, genericKey)
285#define write_privatekey(alg, key)
286 q_PEM_write_bio_PrivateKey_traditional(bio, genericKey, cipher, (uchar *)passPhrase.data(), passPhrase.size(), nullptr, nullptr)
291 if (algorithm() == QSsl::Rsa) {
292 if (type() == QSsl::PublicKey) {
298 }
else if (algorithm() == QSsl::Dsa) {
299 if (type() == QSsl::PublicKey) {
305 }
else if (algorithm() == QSsl::Dh) {
306#ifdef OPENSSL_NO_DEPRECATED_3_0
307 EVP_PKEY *result = genericKey;
315 if (type() == QSsl::PublicKey) {
318 }
else if (!q_PEM_write_bio_PrivateKey(bio, result, cipher, (uchar *)passPhrase.data(),
319 passPhrase.size(),
nullptr,
nullptr)) {
323 }
else if (algorithm() == QSsl::Ec) {
324 if (type() == QSsl::PublicKey) {
338 char *data =
nullptr;
340 if (size > 0 && data)
341 pem = QByteArray(data, size);
368#ifndef OPENSSL_NO_DEPRECATED_3_0
369#define get_key(key, alg) key = q_EVP_PKEY_get1_##alg(pkey)
371#define get_key(key, alg) q_EVP_PKEY_up_ref(pkey); genericKey = pkey;
377 keyAlgorithm = QSsl::Rsa;
378 keyType = QSsl::PrivateKey;
383 keyAlgorithm = QSsl::Dsa;
384 keyType = QSsl::PrivateKey;
389 keyAlgorithm = QSsl::Dh;
390 keyType = QSsl::PrivateKey;
396 keyAlgorithm = QSsl::Ec;
397 keyType = QSsl::PrivateKey;
412 const QByteArray &key,
const QByteArray &iv,
bool enc)
414 const EVP_CIPHER *type =
nullptr;
419#ifndef OPENSSL_NO_DES
423 case Cipher::DesEde3Cbc:
424#ifndef OPENSSL_NO_DES
429#ifndef OPENSSL_NO_RC2
433 case Cipher::Aes128Cbc:
436 case Cipher::Aes192Cbc:
439 case Cipher::Aes256Cbc:
448 output.resize(data.size() + EVP_MAX_BLOCK_LENGTH);
458 q_EVP_CIPHER_CTX_set_key_length(ctx, key.size());
459 if (cipher == Cipher::Rc2Cbc)
460 q_EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, 8 * key.size(),
nullptr);
463 reinterpret_cast<
const unsigned char *>(key.constData())
,
464 reinterpret_cast<
const unsigned char *>(iv.constData())
,
466 q_EVP_CipherUpdate(ctx,
467 reinterpret_cast<
unsigned char *>(output.data()), &len,
468 reinterpret_cast<
const unsigned char *>(data.constData()), data.size());
470 reinterpret_cast<
unsigned char *>(output.data()) + len
, &i
);
476 return output.left(len);
480 const QByteArray &key,
const QByteArray &iv)
const
482 return doCrypt(cipher, data, key, iv,
false);
486 const QByteArray &key,
const QByteArray &iv)
const
488 return doCrypt(cipher, data, key, iv,
true);
493 TlsKeyOpenSSL *tlsKey =
new TlsKeyOpenSSL;
494 std::unique_ptr<TlsKeyOpenSSL> keyRaii(tlsKey);
496 tlsKey->keyType = QSsl::PublicKey;
498#ifndef OPENSSL_NO_DEPRECATED_3_0
500#define get_pubkey(keyName, alg) tlsKey->keyName = q_EVP_PKEY_get1_##alg(pkey)
504#define get_pubkey(a, b) tlsKey->genericKey = pkey
512 if (keyType == EVP_PKEY_RSA) {
514 tlsKey->keyAlgorithm = QSsl::Rsa;
516 }
else if (keyType == EVP_PKEY_DSA) {
518 tlsKey->keyAlgorithm = QSsl::Dsa;
521 }
else if (keyType == EVP_PKEY_EC) {
523 tlsKey->keyAlgorithm = QSsl::Ec;
526 }
else if (keyType == EVP_PKEY_DH) {
532#ifndef OPENSSL_NO_DEPRECATED_3_0
536 return keyRaii.release();
static void logAndClearErrorQueue()
static QString getErrorsFromOpenSsl()
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)
Combined button and popup list for selecting options.
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_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_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)