@@ -892,13 +892,111 @@ std::optional<QByteArray> encryptStringAsymmetric(ENGINE *sslEngine, EVP_PKEY *p
892
892
return out.toBase64 ();
893
893
}
894
894
895
+ void debugOpenssl ()
896
+ {
897
+ if (ERR_peek_error () == 0 ) {
898
+ return ;
899
+ }
900
+
901
+ const char *file;
902
+ char errorMessage[255 ];
903
+ int line;
904
+ while (const auto errorNumber = ERR_get_error_line (&file, &line)) {
905
+ ERR_error_string (errorNumber, errorMessage);
906
+ qCWarning (lcCse ()) << errorMessage << file << line;
907
+ }
908
+ }
909
+
895
910
}
896
911
897
912
898
913
ClientSideEncryption::ClientSideEncryption ()
899
914
{
900
- _sslEngine = ENGINE_new ();
915
+ auto ctx = PKCS11_CTX_new ();
916
+
917
+ auto rc = PKCS11_CTX_load (ctx, " " );
918
+ if (rc) {
919
+ qCWarning (lcCse ()) << " loading pkcs11 engine failed:" << ERR_reason_error_string (ERR_get_error ());
920
+ rc = 1 ;
921
+ exit (-1 );
922
+ }
923
+
924
+ auto nslots = 0u ;
925
+ PKCS11_SLOT *tokenSlots = nullptr ;
926
+ /* get information on all slots */
927
+ rc = PKCS11_enumerate_slots (ctx, &tokenSlots, &nslots);
928
+ if (rc < 0 ) {
929
+ qCWarning (lcCse ()) << " no slots available" ;
930
+ rc = 2 ;
931
+ exit (-1 );
932
+ }
933
+
934
+ /* get first slot with a token */
935
+ auto slot = PKCS11_find_token (ctx, tokenSlots, nslots);
936
+ if (slot == NULL || slot->token == NULL ) {
937
+ qCWarning (lcCse ()) << " no token available" ;
938
+ rc = 3 ;
939
+ exit (-1 );
940
+ }
941
+ qCInfo (lcCse ()) << " Slot manufacturer......:" << slot->manufacturer ;
942
+ qCInfo (lcCse ()) << " Slot description.......:" << slot->description ;
943
+ qCInfo (lcCse ()) << " Slot token label.......:" << slot->token ->label ;
944
+ qCInfo (lcCse ()) << " Slot token manufacturer:" << slot->token ->manufacturer ;
945
+ qCInfo (lcCse ()) << " Slot token model.......:" << slot->token ->model ;
946
+ qCInfo (lcCse ()) << " Slot token serialnr....:" << slot->token ->serialnr ;
947
+
948
+ auto logged_in = 0 ;
949
+ rc = PKCS11_is_logged_in (slot, 0 , &logged_in);
950
+ if (rc != 0 ) {
951
+ qCWarning (lcCse ()) << " PKCS11_is_logged_in failed" ;
952
+ rc = 8 ;
953
+ exit (-1 );
954
+ }
955
+
956
+ /* perform pkcs #11 login */
957
+ QByteArray password = " 0000" ;
958
+ rc = PKCS11_login (slot, 0 , password.data ());
959
+ if (rc != 0 ) {
960
+ qCWarning (lcCse ()) << " PKCS11_login failed" ;
961
+ rc = 10 ;
962
+ exit (-1 );
963
+ }
964
+
965
+ /* check if user is logged in */
966
+ rc = PKCS11_is_logged_in (slot, 0 , &logged_in);
967
+ if (rc != 0 ) {
968
+ qCWarning (lcCse ()) << " PKCS11_is_logged_in failed" ;
969
+ rc = 11 ;
970
+ exit (-1 );
971
+ }
972
+ if (!logged_in) {
973
+ qCWarning (lcCse ()) << " PKCS11_is_logged_in says user is not logged in, expected to be logged in" ;
974
+ rc = 12 ;
975
+ exit (-1 );
976
+ }
977
+
978
+ ENGINE_load_dynamic ();
979
+
980
+ _sslEngine = ENGINE_by_id (" dynamic" );
901
981
qCInfo (lcCse ()) << " ssl engine" << _sslEngine;
982
+
983
+ if (!ENGINE_ctrl_cmd_string (_sslEngine, " VERBOSE" , nullptr , 0 )) {
984
+ qCWarning (lcCse ()) << " issue when adding hardware token to ssl engine" << _sslEngine;
985
+ EncryptionHelper::debugOpenssl ();
986
+ return ;
987
+ }
988
+
989
+ if (!ENGINE_ctrl_cmd_string (_sslEngine, " LOAD" , nullptr , 0 )) {
990
+ qCWarning (lcCse ()) << " issue when adding hardware token to ssl engine" << _sslEngine;
991
+ EncryptionHelper::debugOpenssl ();
992
+ return ;
993
+ }
994
+
995
+ if (!ENGINE_init (_sslEngine)) {
996
+ qCWarning (lcCse ()) << " issue when adding hardware token to ssl engine" << _sslEngine;
997
+ EncryptionHelper::debugOpenssl ();
998
+ return ;
999
+ }
902
1000
}
903
1001
904
1002
const QSslKey &ClientSideEncryption::getPublicKey () const
0 commit comments