@@ -665,6 +665,9 @@ namespace internals {
665
665
EVP_PKEY *publicKey,
666
666
const QByteArray& binaryData);
667
667
668
+ [[nodiscard]] std::optional<QByteArray> encryptStringAsymmetricWithToken (EVP_PKEY *publicKey,
669
+ const QByteArray& binaryData);
670
+
668
671
[[nodiscard]] std::optional<QByteArray> decryptStringAsymmetric (ENGINE *sslEngine,
669
672
EVP_PKEY *privateKey,
670
673
const QByteArray& binaryData);
@@ -684,17 +687,11 @@ std::optional<QByteArray> encryptStringAsymmetric(const ClientSideEncryption &en
684
687
qCWarning (lcCseEncryption ()) << " encryptStringAsymmetric" << " hardware public key" << publicKey;
685
688
}
686
689
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
-
694
690
qCInfo (lcCseEncryption ()) << " encryptStringAsymmetric:"
695
691
<< " data:" << binaryData.toBase64 ();
696
692
697
- auto encryptedBase64Result = internals::encryptStringAsymmetric (encryptionEngine.sslEngine (), publicKey, binaryData);
693
+ auto encryptedBase64Result = internals::encryptStringAsymmetricWithToken (PKCS11_get_public_key (encryptionEngine.getTokenPublicKey ()),
694
+ binaryData);
698
695
699
696
if (!encryptedBase64Result) {
700
697
qCWarning (lcCseEncryption ()) << " encrypt failed" ;
@@ -1008,6 +1005,39 @@ void debugOpenssl()
1008
1005
1009
1006
namespace internals {
1010
1007
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
+
1011
1041
std::optional<QByteArray> decryptStringAsymmetricWithToken (PKCS11_KEY *privateKey,
1012
1042
EVP_PKEY *publicKey,
1013
1043
const QByteArray &binaryData)
@@ -1020,7 +1050,11 @@ std::optional<QByteArray> decryptStringAsymmetricWithToken(PKCS11_KEY *privateKe
1020
1050
reinterpret_cast <unsigned char *>(decryptedData.data ()),
1021
1051
privateKey,
1022
1052
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;
1024
1058
qCCritical (lcCseDecryption ()) << " PKCS11_private_decrypt failed"
1025
1059
<< " expected decrypted length" << initialLength
1026
1060
<< " result" << rc << ERR_reason_error_string (ERR_get_error ());
@@ -1030,6 +1064,13 @@ std::optional<QByteArray> decryptStringAsymmetricWithToken(PKCS11_KEY *privateKe
1030
1064
<< " need login:" << (privateKey->needLogin ? " true" : " false" );
1031
1065
1032
1066
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;
1033
1074
}
1034
1075
1035
1076
qCInfo (lcCseDecryption ()) << " result to base64" << decryptedData.toBase64 ();
0 commit comments