Skip to content

Commit 6b3beec

Browse files
committed
fixed the decryption issue with hardware token being used
switch to another encryption method (not sure this is really necessary) fixed missing base64 conversion fixed the check for decrypted length Signed-off-by: Matthieu Gallien <[email protected]>
1 parent 283ac67 commit 6b3beec

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

src/libsync/clientsideencryption.cpp

+50-9
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,9 @@ namespace internals {
665665
EVP_PKEY *publicKey,
666666
const QByteArray& binaryData);
667667

668+
[[nodiscard]] std::optional<QByteArray> encryptStringAsymmetricWithToken(EVP_PKEY *publicKey,
669+
const QByteArray& binaryData);
670+
668671
[[nodiscard]] std::optional<QByteArray> decryptStringAsymmetric(ENGINE *sslEngine,
669672
EVP_PKEY *privateKey,
670673
const QByteArray& binaryData);
@@ -684,17 +687,11 @@ std::optional<QByteArray> encryptStringAsymmetric(const ClientSideEncryption &en
684687
qCWarning(lcCseEncryption()) << "encryptStringAsymmetric" << "hardware public key" << publicKey;
685688
}
686689

687-
{
688-
Bio publicKeyBio;
689-
PEM_write_bio_PKCS8PrivateKey(publicKeyBio, publicKey, nullptr, nullptr, 0, nullptr, nullptr);
690-
const auto publicKeyBase64 = BIO2ByteArray(publicKeyBio).toBase64();
691-
qCInfo(lcCseDecryption()) << "decryptStringAsymmetric" << "hardware public key to base64" << publicKeyBase64;
692-
}
693-
694690
qCInfo(lcCseEncryption()) << "encryptStringAsymmetric:"
695691
<< "data:" << binaryData.toBase64();
696692

697-
auto encryptedBase64Result = internals::encryptStringAsymmetric(encryptionEngine.sslEngine(), publicKey, binaryData);
693+
auto encryptedBase64Result = internals::encryptStringAsymmetricWithToken(PKCS11_get_public_key(encryptionEngine.getTokenPublicKey()),
694+
binaryData);
698695

699696
if (!encryptedBase64Result) {
700697
qCWarning(lcCseEncryption()) << "encrypt failed";
@@ -1008,6 +1005,39 @@ void debugOpenssl()
10081005

10091006
namespace internals {
10101007

1008+
std::optional<QByteArray> encryptStringAsymmetricWithToken(EVP_PKEY *publicKey,
1009+
const QByteArray& binaryData)
1010+
{
1011+
const auto initialLength = RSA_size(EVP_PKEY_get0_RSA(publicKey));
1012+
auto out = QByteArray{static_cast<int>(initialLength), 0};
1013+
1014+
const auto rc = RSA_public_encrypt(binaryData.size(),
1015+
reinterpret_cast<const unsigned char*>(binaryData.data()),
1016+
reinterpret_cast<unsigned char*>(out.data()),
1017+
const_cast<RSA*>(EVP_PKEY_get0_RSA(publicKey)),
1018+
RSA_PKCS1_PADDING);
1019+
1020+
if (rc != initialLength) {
1021+
qCInfo(lcCseEncryption()) << "data to encrypt:" << binaryData.toBase64()
1022+
<< "size:" << binaryData.size()
1023+
<< "key size:" << initialLength;
1024+
qCCritical(lcCseEncryption()) << "RSA_public_encrypt failed"
1025+
<< "expected encrypted length" << initialLength
1026+
<< "result" << rc << ERR_reason_error_string(ERR_get_error());
1027+
1028+
return {};
1029+
} else {
1030+
qCInfo(lcCseEncryption()) << "data to encrypt:" << binaryData.toBase64()
1031+
<< "size:" << binaryData.size()
1032+
<< "key size:" << initialLength;
1033+
qCInfo(lcCseEncryption()) << "data encrypted:" << out.toBase64()
1034+
<< "size:" << out.size()
1035+
<< "key size:" << initialLength;
1036+
}
1037+
1038+
return out.toBase64();
1039+
}
1040+
10111041
std::optional<QByteArray> decryptStringAsymmetricWithToken(PKCS11_KEY *privateKey,
10121042
EVP_PKEY *publicKey,
10131043
const QByteArray &binaryData)
@@ -1020,7 +1050,11 @@ std::optional<QByteArray> decryptStringAsymmetricWithToken(PKCS11_KEY *privateKe
10201050
reinterpret_cast<unsigned char*>(decryptedData.data()),
10211051
privateKey,
10221052
RSA_PKCS1_PADDING);
1023-
if (rc != initialLength) {
1053+
1054+
if (rc < 0) {
1055+
qCInfo(lcCseDecryption()) << "data to decrypt:" << binaryData.toBase64()
1056+
<< "size:" << binaryData.size()
1057+
<< "key size:" << initialLength;
10241058
qCCritical(lcCseDecryption()) << "PKCS11_private_decrypt failed"
10251059
<< "expected decrypted length" << initialLength
10261060
<< "result" << rc << ERR_reason_error_string(ERR_get_error());
@@ -1030,6 +1064,13 @@ std::optional<QByteArray> decryptStringAsymmetricWithToken(PKCS11_KEY *privateKe
10301064
<< "need login:" << (privateKey->needLogin ? "true" : "false");
10311065

10321066
return {};
1067+
} else {
1068+
qCInfo(lcCseDecryption()) << "data to decrypt:" << binaryData.toBase64()
1069+
<< "size:" << binaryData.size()
1070+
<< "key size:" << initialLength;
1071+
qCInfo(lcCseDecryption()) << "data decrypted:" << decryptedData.toBase64()
1072+
<< "size:" << decryptedData.size()
1073+
<< "key size:" << initialLength;
10331074
}
10341075

10351076
qCInfo(lcCseDecryption()) << "result to base64" << decryptedData.toBase64();

0 commit comments

Comments
 (0)