diff --git a/.reuse/dep5 b/.reuse/dep5 index 26d26221da..1bf09f9718 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -84,7 +84,7 @@ Copyright: 2020 Christian Boxdörfer License: MIT # D-Bus interfaces and adaptors -Files: src/*/*dbus_interface.* src/*/*dbus_adaptor.* src/*/dbus*/* src/plugins/daemon/daemonplugin-mountcontrol/private/mountcontrol_adapter.* src/plugins/desktop/ddplugin-background/appearance_interface.* +Files: src/*/*dbus_interface.* src/*/*dbus_adaptor.* src/*/dbus*/* src/plugins/daemon/daemonplugin-mountcontrol/private/mountcontrol_adapter.* src/plugins/desktop/ddplugin-background/appearance_interface.* src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.* Copyright: 2016 The Qt Company Ltd. License: CC0-1.0 diff --git a/src/plugins/common/dfmplugin-dirshare/utils/usersharehelper.cpp b/src/plugins/common/dfmplugin-dirshare/utils/usersharehelper.cpp index 8ff0a5367b..1a813c1fad 100644 --- a/src/plugins/common/dfmplugin-dirshare/utils/usersharehelper.cpp +++ b/src/plugins/common/dfmplugin-dirshare/utils/usersharehelper.cpp @@ -31,6 +31,7 @@ #include #include +Q_DECLARE_METATYPE(QString *) DFMBASE_USE_NAMESPACE namespace dfmplugin_dirshare { @@ -141,7 +142,15 @@ bool UserShareHelper::share(const ShareInfo &info) void UserShareHelper::setSambaPasswd(const QString &userName, const QString &passwd) { - QDBusReply reply = userShareInter->call(DaemonServiceIFace::kFuncSetPasswd, userName, passwd); + QString encPass; + auto ret = dpfSlotChannel->push("dfmplugin_stringencrypt", "slot_OpenSSL_EncryptString", + passwd, &encPass); + if (ret != 0) { + fmWarning() << "cannot encrypt password!!!"; + DialogManagerInstance->showErrorDialog(tr("Error"), tr("Cannot encrypt password")); + return; + } + QDBusReply reply = userShareInter->call(DaemonServiceIFace::kFuncSetPasswd, userName, encPass); bool result = reply.isValid() && reply.error().message().isEmpty(); fmInfo() << "Samba password set result :" << result << ",error msg:" << reply.error().message(); diff --git a/src/plugins/daemon/CMakeLists.txt b/src/plugins/daemon/CMakeLists.txt index 237fe8eb4b..ad76c20e23 100644 --- a/src/plugins/daemon/CMakeLists.txt +++ b/src/plugins/daemon/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory(daemonplugin-accesscontrol) add_subdirectory(daemonplugin-sharecontrol) add_subdirectory(daemonplugin-anything) add_subdirectory(daemonplugin-mountcontrol) +add_subdirectory(daemonplugin-stringdecrypt) diff --git a/src/plugins/daemon/daemonplugin-accesscontrol/accesscontroldbus.cpp b/src/plugins/daemon/daemonplugin-accesscontrol/accesscontroldbus.cpp index 3edc46e99c..68ab230931 100644 --- a/src/plugins/daemon/daemonplugin-accesscontrol/accesscontroldbus.cpp +++ b/src/plugins/daemon/daemonplugin-accesscontrol/accesscontroldbus.cpp @@ -7,6 +7,7 @@ #include "polkit/policykithelper.h" #include +#include #include #include @@ -21,6 +22,7 @@ #include #include +Q_DECLARE_METATYPE(QString *) DFMBASE_USE_NAMESPACE DAEMONPAC_USE_NAMESPACE @@ -212,8 +214,16 @@ void AccessControlDBus::ChangeDiskPassword(const QString &oldPwd, const QString return; } - const QByteArray &tmpOldPwd = oldPwd.toLocal8Bit(); - const QByteArray &tmpNewPwd = newPwd.toLocal8Bit(); + QString oldPwdDec, newPwdDec; + int r = dpfSlotChannel->push("daemonplugin_stringdecrypt", "slot_OpenSSL_DecryptString", oldPwd, &oldPwdDec).toInt(); + r = dpfSlotChannel->push("daemonplugin_stringdecrypt", "slot_OpenSSL_DecryptString", newPwd, &newPwdDec).toInt(); + if (r != 0) { + fmCritical() << "cannot decrypt password!!!"; + return; + } + + const QByteArray &tmpOldPwd = oldPwdDec.toLocal8Bit(); + const QByteArray &tmpNewPwd = newPwdDec.toLocal8Bit(); int ret = kNoError; QStringList successList; diff --git a/src/plugins/daemon/daemonplugin-sharecontrol/sharecontroldbus.cpp b/src/plugins/daemon/daemonplugin-sharecontrol/sharecontroldbus.cpp index c319d53bac..b9fa41a1b1 100644 --- a/src/plugins/daemon/daemonplugin-sharecontrol/sharecontroldbus.cpp +++ b/src/plugins/daemon/daemonplugin-sharecontrol/sharecontroldbus.cpp @@ -7,12 +7,16 @@ #include "dbusadapter/sharecontrol_adapter.h" #include "daemonplugin_sharecontrol_global.h" +#include + #include #include #include #include #include +Q_DECLARE_METATYPE(QString *) + static constexpr char kUserShareObjPath[] { "/com/deepin/filemanager/daemon/UserShareManager" }; static constexpr char kPolicyKitActionId[] { "com.deepin.filemanager.daemon.UserShareManager" }; DAEMONPSHARECONTROL_USE_NAMESPACE @@ -73,18 +77,26 @@ bool ShareControlDBus::SetUserSharePassword(const QString &name, const QString & return false; } - fmDebug() << name; // << passward; // log password? + QString clearPasswd; + int ret = dpfSlotChannel->push("daemonplugin_stringdecrypt", "slot_OpenSSL_DecryptString", + passwd, &clearPasswd) + .toInt(); + if (ret != 0) { + fmWarning() << "cannot decrypt password!!!"; + return false; + } + QStringList args; args << "-a" << name << "-s"; QProcess p; p.start("smbpasswd", args); - p.write(passwd.toStdString().c_str()); + p.write(clearPasswd.toStdString().c_str()); p.write("\n"); - p.write(passwd.toStdString().c_str()); + p.write(clearPasswd.toStdString().c_str()); p.closeWriteChannel(); - bool ret = p.waitForFinished(); + bool r = p.waitForFinished(); fmDebug() << p.readAll() << p.readAllStandardError() << p.readAllStandardOutput(); - return ret; + return r; } bool ShareControlDBus::EnableSmbServices() diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/CMakeLists.txt b/src/plugins/daemon/daemonplugin-stringdecrypt/CMakeLists.txt new file mode 100644 index 0000000000..46c5b6cd24 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.10) + +project(daemonplugin-stringdecrypt) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +FILE(GLOB FILEOPERATIONS_FILES + "${CMAKE_CURRENT_SOURCE_DIR}/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*.json" + "${CMAKE_CURRENT_SOURCE_DIR}/*.xml" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.policy" + ) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(OpenSSL REQUIRED openssl) + +add_library(${PROJECT_NAME} + SHARED + ${FILEOPERATIONS_FILES} +) + +set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../../) + +find_package(Qt5 COMPONENTS + DBus + REQUIRED +) + +target_link_libraries(${PROJECT_NAME} + DFM::framework + DFM::base + Qt5::DBus + ${OpenSSL_LIBRARIES} +) + +#install library file +install(TARGETS + ${PROJECT_NAME} + LIBRARY + DESTINATION + ${DFM_PLUGIN_DAEMON_EDGE_DIR} +) + +# execute_process(COMMAND qdbuscpp2xml stringdecryptdbus.h -o ./stringdecryptdbus.xml +# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +# execute_process(COMMAND qdbusxml2cpp -i stringdecryptdbus.h -c StringDecryptAdapter -l StringDecryptDBus -a stringdecrypt_adapter stringdecryptdbus.xml +# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/opensslhandler.cpp b/src/plugins/daemon/daemonplugin-stringdecrypt/opensslhandler.cpp new file mode 100644 index 0000000000..ec8d666be7 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/opensslhandler.cpp @@ -0,0 +1,80 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "opensslhandler.h" + +#include + +using namespace daemonplugin_stringdecrypt; + +OpenSSLHandler *OpenSSLHandler::instance() +{ + static OpenSSLHandler ins; + return &ins; +} + +void OpenSSLHandler::initKeyPairs() +{ + if (rsa) + return; + rsa = RSA_generate_key(2048, RSA_F4, nullptr, nullptr); + + BIO *bioPrivKey = BIO_new(BIO_s_mem()); + PEM_write_bio_RSAPrivateKey(bioPrivKey, rsa, nullptr, nullptr, 0, nullptr, nullptr); + char *privKeyBuf; + long privKeyLen = BIO_get_mem_data(bioPrivKey, &privKeyBuf); + auto privKey = QByteArray(privKeyBuf, privKeyLen); + BIO_free(bioPrivKey); + + BIO *bioPubKey = BIO_new(BIO_s_mem()); + PEM_write_bio_RSA_PUBKEY(bioPubKey, rsa); + char *pubKeyBuf; + long pubKeyLen = BIO_get_mem_data(bioPubKey, &pubKeyBuf); + auto pubKey = QByteArray(pubKeyBuf, pubKeyLen); + BIO_free(bioPubKey); + + keys = { pubKey, privKey }; +} + +QString OpenSSLHandler::pubKey() const +{ + return keys.first; +} + +int OpenSSLHandler::decrypt(const QString &in, QString *out) +{ + Q_ASSERT(rsa); + Q_ASSERT(out); + + QByteArray cipher = QByteArray::fromBase64(in.toLocal8Bit()); + + int rsaSize = RSA_size(rsa); + unsigned char *decrypted = new unsigned char[rsaSize]; + int decryptedLen = RSA_private_decrypt(cipher.length(), + reinterpret_cast(cipher.data()), + decrypted, + rsa, + RSA_PKCS1_PADDING); + + if (decryptedLen == -1) { + delete[] decrypted; + return -1; + } + + QByteArray source(reinterpret_cast(decrypted), decryptedLen); + *out = QString(source); + delete[] decrypted; + return 0; +} + +OpenSSLHandler::OpenSSLHandler(QObject *parent) +{ +} + +OpenSSLHandler::~OpenSSLHandler() +{ + if (rsa) + RSA_free(rsa); + rsa = nullptr; +} diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/opensslhandler.h b/src/plugins/daemon/daemonplugin-stringdecrypt/opensslhandler.h new file mode 100644 index 0000000000..d2776370c7 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/opensslhandler.h @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#ifndef OPENSSLHANDLER_H +#define OPENSSLHANDLER_H + +#include + +#include + +namespace daemonplugin_stringdecrypt { + +class OpenSSLHandler : public QObject +{ + Q_OBJECT + +public: + static OpenSSLHandler *instance(); + + void initKeyPairs(); + + QString pubKey() const; + int decrypt(const QString &in, QString *out); + +private: + explicit OpenSSLHandler(QObject *parent = nullptr); + ~OpenSSLHandler(); + + RSA *rsa { nullptr }; + QPair keys; +}; +} + +#endif // OPENSSLHANDLER_H diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt.json b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt.json new file mode 100644 index 0000000000..368898d5a8 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt.json @@ -0,0 +1,14 @@ +{ + "Name" : "daemonplugin-stringdecrypt", + "Version" : "1.0.0", + "CompatVersion" : "1.0.0", + "Vendor" : "The Uniontech Software Technology Co., Ltd.", + "Copyright" : "Copyright (C) 2024 Uniontech Software Technology Co., Ltd.", + "License" : [ + ], + "Category" : "", + "Description" : "The string decrypt plugin for the dde-file-manager-daemon.", + "UrlLink" : "https://www.uniontech.com", + "Depends" : [ + ] +} diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.cpp b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.cpp new file mode 100644 index 0000000000..0555169be7 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.cpp @@ -0,0 +1,41 @@ +/* + * This file was generated by qdbusxml2cpp version 0.8 + * Command line was: qdbusxml2cpp -i stringdecryptdbus.h -c StringDecryptAdapter -l StringDecryptDBus -a stringdecrypt_adapter stringdecryptdbus.xml + * + * qdbusxml2cpp is Copyright (C) 2017 The Qt Company Ltd. + * + * This is an auto-generated file. + * Do not edit! All changes made to it will be lost. + */ + +#include "stringdecrypt_adapter.h" +#include +#include +#include +#include +#include +#include +#include + +/* + * Implementation of adaptor class StringDecryptAdapter + */ + +StringDecryptAdapter::StringDecryptAdapter(StringDecryptDBus *parent) + : QDBusAbstractAdaptor(parent) +{ + // constructor + setAutoRelaySignals(true); +} + +StringDecryptAdapter::~StringDecryptAdapter() +{ + // destructor +} + +QString StringDecryptAdapter::PublicKey() +{ + // handle method call com.deepin.filemanager.daemon.EncryptKeyHelper.PublicKey + return parent()->PublicKey(); +} + diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.h b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.h new file mode 100644 index 0000000000..3c457c7ba9 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecrypt_adapter.h @@ -0,0 +1,54 @@ +/* + * This file was generated by qdbusxml2cpp version 0.8 + * Command line was: qdbusxml2cpp -i stringdecryptdbus.h -c StringDecryptAdapter -l StringDecryptDBus -a stringdecrypt_adapter stringdecryptdbus.xml + * + * qdbusxml2cpp is Copyright (C) 2017 The Qt Company Ltd. + * + * This is an auto-generated file. + * This file may have been hand-edited. Look for HAND-EDIT comments + * before re-generating it. + */ + +#ifndef STRINGDECRYPT_ADAPTER_H +#define STRINGDECRYPT_ADAPTER_H + +#include +#include +#include "stringdecryptdbus.h" +QT_BEGIN_NAMESPACE +class QByteArray; +template class QList; +template class QMap; +class QString; +class QStringList; +class QVariant; +QT_END_NAMESPACE + +/* + * Adaptor class for interface com.deepin.filemanager.daemon.EncryptKeyHelper + */ +class StringDecryptAdapter: public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "com.deepin.filemanager.daemon.EncryptKeyHelper") + Q_CLASSINFO("D-Bus Introspection", "" +" \n" +" \n" +" \n" +" \n" +" \n" + "") +public: + StringDecryptAdapter(StringDecryptDBus *parent); + virtual ~StringDecryptAdapter(); + + inline StringDecryptDBus *parent() const + { return static_cast(QObject::parent()); } + +public: // PROPERTIES +public Q_SLOTS: // METHODS + QString PublicKey(); +Q_SIGNALS: // SIGNALS +}; + +#endif diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.cpp b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.cpp new file mode 100644 index 0000000000..e1e9130932 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.cpp @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "stringdecryptdbus.h" +#include "stringdecrypt_adapter.h" +#include "opensslhandler.h" + +#include + +StringDecryptDBus::StringDecryptDBus(QObject *parent) + : QObject(parent), QDBusContext() +{ + QDBusConnection::systemBus() + .registerObject("/com/deepin/filemanager/daemon/EncryptKeyHelper", + this); + new StringDecryptAdapter(this); +} + +QString StringDecryptDBus::PublicKey() +{ + return daemonplugin_stringdecrypt::OpenSSLHandler::instance() + ->pubKey() + .toLocal8Bit() + .toBase64(); +} diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.h b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.h new file mode 100644 index 0000000000..b147e4e00c --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.h @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#ifndef STRINGENCRYPTDBUS_H +#define STRINGENCRYPTDBUS_H + +#include +#include + +class StringDecryptDBus : public QObject, public QDBusContext +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "com.deepin.filemanager.daemon.EncryptKeyHelper") + +public: + explicit StringDecryptDBus(QObject *parent = nullptr); + +public Q_SLOTS: + QString PublicKey(); +}; + +#endif // STRINGENCRYPTDBUS_H diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.xml b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.xml new file mode 100644 index 0000000000..56dcced688 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptdbus.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptplugin.cpp b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptplugin.cpp new file mode 100644 index 0000000000..512c427333 --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptplugin.cpp @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "stringdecryptplugin.h" +#include "opensslhandler.h" +#include "stringdecryptdbus.h" + +Q_DECLARE_METATYPE(QString *) + +using namespace daemonplugin_stringdecrypt; + +bool StringDecryptPlugin::start() +{ + OpenSSLHandler::instance()->initKeyPairs(); + mng.reset(new StringDecryptDBus(this)); + bindEvents(); + return true; +} + +void StringDecryptPlugin::bindEvents() +{ + dpfSlotChannel->connect("daemonplugin_stringdecrypt", "slot_OpenSSL_DecryptString", + OpenSSLHandler::instance(), &OpenSSLHandler::decrypt); +} diff --git a/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptplugin.h b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptplugin.h new file mode 100644 index 0000000000..6e03b559ea --- /dev/null +++ b/src/plugins/daemon/daemonplugin-stringdecrypt/stringdecryptplugin.h @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#ifndef STRINGENCRYPTPLUGIN_H +#define STRINGENCRYPTPLUGIN_H + +#include +#include + +class StringDecryptDBus; +namespace daemonplugin_stringdecrypt { +class StringDecryptPlugin : public DPF_NAMESPACE::Plugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.deepin.plugin.daemon" FILE "stringdecrypt.json") + + DPF_EVENT_NAMESPACE(daemonplugin_stringdecrypt) + DPF_EVENT_REG_SLOT(slot_OpenSSL_DecryptString) + +public: + virtual bool start() override; + +private: + void bindEvents(); + +private: + QScopedPointer mng; +}; +} + +#endif // STRINGENCRYPTPLUGIN_H diff --git a/src/plugins/filemanager/CMakeLists.txt b/src/plugins/filemanager/CMakeLists.txt index bddc716592..eac800ff27 100644 --- a/src/plugins/filemanager/CMakeLists.txt +++ b/src/plugins/filemanager/CMakeLists.txt @@ -16,3 +16,4 @@ add_subdirectory(dfmplugin-vault) add_subdirectory(dfmplugin-myshares) add_subdirectory(dfmplugin-smbbrowser) add_subdirectory(dfmplugin-avfsbrowser) +add_subdirectory(dfmplugin-stringencrypt) diff --git a/src/plugins/filemanager/core/dfmplugin-titlebar/dialogs/dpcwidget/dpcconfirmwidget.cpp b/src/plugins/filemanager/core/dfmplugin-titlebar/dialogs/dpcwidget/dpcconfirmwidget.cpp index ea57989cea..78b50630b9 100644 --- a/src/plugins/filemanager/core/dfmplugin-titlebar/dialogs/dpcwidget/dpcconfirmwidget.cpp +++ b/src/plugins/filemanager/core/dfmplugin-titlebar/dialogs/dpcwidget/dpcconfirmwidget.cpp @@ -4,7 +4,9 @@ #include "dpcconfirmwidget.h" +#include #include +#include #include #include @@ -23,6 +25,7 @@ #include #include +Q_DECLARE_METATYPE(QString *) DCORE_USE_NAMESPACE DFMBASE_USE_NAMESPACE DWIDGET_USE_NAMESPACE @@ -285,7 +288,15 @@ void DPCConfirmWidget::onSaveBtnClicked() if (accessControlInter->isValid()) { setEnabled(false); - accessControlInter->asyncCall(DaemonServiceIFace::kFuncChangePwd, oldPwdEdit->text(), newPwdEdit->text()); + QString oldPass(oldPwdEdit->text().trimmed()), newPass(newPwdEdit->text().trimmed()), + oldPassEnc, newPassEnc; + int ret = dpfSlotChannel->push("dfmplugin_stringencrypt", "slot_OpenSSL_EncryptString", oldPass, &oldPassEnc).toInt(); + ret = dpfSlotChannel->push("dfmplugin_stringencrypt", "slot_OpenSSL_EncryptString", newPass, &newPassEnc).toInt(); + if (ret != 0) { + DialogManagerInstance->showErrorDialog(tr("Error"), tr("Cannot encrypt password!")); + return; + } + accessControlInter->asyncCall(DaemonServiceIFace::kFuncChangePwd, oldPassEnc, newPassEnc); } } diff --git a/src/plugins/filemanager/dfmplugin-stringencrypt/CMakeLists.txt b/src/plugins/filemanager/dfmplugin-stringencrypt/CMakeLists.txt new file mode 100644 index 0000000000..f25982635a --- /dev/null +++ b/src/plugins/filemanager/dfmplugin-stringencrypt/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.10) + +project(dfmplugin-stringencrypt) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +FILE(GLOB FILEOPERATIONS_FILES + "${CMAKE_CURRENT_SOURCE_DIR}/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/*.json" + "${CMAKE_CURRENT_SOURCE_DIR}/*.xml" + "${CMAKE_CURRENT_SOURCE_DIR}/*/*.policy" + ) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(OpenSSL REQUIRED openssl) + +add_library(${PROJECT_NAME} + SHARED + ${FILEOPERATIONS_FILES} +) + +set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../../) + +find_package(Qt5 COMPONENTS + DBus + REQUIRED +) + +target_link_libraries(${PROJECT_NAME} + DFM::framework + DFM::base + Qt5::DBus + ${OpenSSL_LIBRARIES} +) + +#install library file +install(TARGETS + ${PROJECT_NAME} + LIBRARY + DESTINATION + ${DFM_PLUGIN_FILEMANAGER_EDGE_DIR} +) + +# execute_process(COMMAND qdbuscpp2xml stringencryptdbus.h -o ./stringencryptdbus.xml +# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +# execute_process(COMMAND qdbusxml2cpp -i stringencryptdbus.h -c StringEncryptAdapter -l StringEncryptDBus -a stringencrypt_adapter stringencryptdbus.xml +# WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/plugins/filemanager/dfmplugin-stringencrypt/opensslhandler.cpp b/src/plugins/filemanager/dfmplugin-stringencrypt/opensslhandler.cpp new file mode 100644 index 0000000000..cfba5fd4e9 --- /dev/null +++ b/src/plugins/filemanager/dfmplugin-stringencrypt/opensslhandler.cpp @@ -0,0 +1,92 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "opensslhandler.h" + +#include +#include + +#include +#include +#include + +#include + +DFM_LOG_USE_CATEGORY(dfmplugin_stringencrypt) + +using namespace dfmplugin_stringencrypt; + +OpenSSLHandler *OpenSSLHandler::instance() +{ + static OpenSSLHandler ins; + return &ins; +} + +int OpenSSLHandler::encrypt(const QString &in, QString *out) +{ + const auto &&publicKey = pubKey(); + Q_ASSERT(!publicKey.isEmpty()); + Q_ASSERT(out); + + BIO *bio { nullptr }; + RSA *rsa { nullptr }; + unsigned char *cipherData { nullptr }; + + dfmbase::FinallyUtil finalClear([=] { + if (bio) BIO_free(bio); + if (rsa) RSA_free(rsa); + if (cipherData) delete[] cipherData; + }); + + bio = BIO_new(BIO_s_mem()); + BIO_write(bio, publicKey.toStdString().c_str(), publicKey.length()); + + rsa = PEM_read_bio_RSA_PUBKEY(bio, nullptr, nullptr, nullptr); + if (!rsa) { + fmWarning() << "cannot read rsa by pubkey"; + return -1; + } + + int rsaSize = RSA_size(rsa); + cipherData = new unsigned char[rsaSize]; + int result = RSA_public_encrypt(in.length(), + reinterpret_cast(in.toStdString().c_str()), + cipherData, + rsa, + RSA_PKCS1_PADDING); + + if (result == -1) { + fmWarning() << "cannot encrypt by pubkey"; + return result; + } + + QByteArray cipher(reinterpret_cast(cipherData), result); + *out = QString(cipher.toBase64()); + return 0; +} + +OpenSSLHandler::OpenSSLHandler(QObject *parent) +{ +} + +OpenSSLHandler::~OpenSSLHandler() +{ +} + +QString OpenSSLHandler::pubKey() +{ + QDBusInterface iface("com.deepin.filemanager.daemon", + "/com/deepin/filemanager/daemon/EncryptKeyHelper", + "com.deepin.filemanager.daemon.EncryptKeyHelper", + QDBusConnection::systemBus()); + if (iface.isValid()) { + QDBusReply reply = iface.call("PublicKey"); + if (reply.isValid()) { + QByteArray b64Pk = reply.value().toLocal8Bit(); + return QByteArray::fromBase64(b64Pk); + } + } + fmWarning() << "dbus interface not valid or no public key returned"; + return ""; +} diff --git a/src/plugins/filemanager/dfmplugin-stringencrypt/opensslhandler.h b/src/plugins/filemanager/dfmplugin-stringencrypt/opensslhandler.h new file mode 100644 index 0000000000..7ffe264608 --- /dev/null +++ b/src/plugins/filemanager/dfmplugin-stringencrypt/opensslhandler.h @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#ifndef OPENSSLHANDLER_H +#define OPENSSLHANDLER_H + +#include + +#include + +namespace dfmplugin_stringencrypt { + +class OpenSSLHandler : public QObject +{ + Q_OBJECT + +public: + static OpenSSLHandler *instance(); + int encrypt(const QString &in, QString *out); + +private: + explicit OpenSSLHandler(QObject *parent = nullptr); + ~OpenSSLHandler(); + QString pubKey(); +}; +} + +#endif // OPENSSLHANDLER_H diff --git a/src/plugins/filemanager/dfmplugin-stringencrypt/stringencrypt.json b/src/plugins/filemanager/dfmplugin-stringencrypt/stringencrypt.json new file mode 100644 index 0000000000..896839d406 --- /dev/null +++ b/src/plugins/filemanager/dfmplugin-stringencrypt/stringencrypt.json @@ -0,0 +1,14 @@ +{ + "Name" : "dfmplugin-stringencrypt", + "Version" : "1.0.0", + "CompatVersion" : "1.0.0", + "Vendor" : "The Uniontech Software Technology Co., Ltd.", + "Copyright" : "Copyright (C) 2024 Uniontech Software Technology Co., Ltd.", + "License" : [ + ], + "Category" : "", + "Description" : "The string encrypt plugin for the dde-file-manager.", + "UrlLink" : "https://www.uniontech.com", + "Depends" : [ + ] +} diff --git a/src/plugins/filemanager/dfmplugin-stringencrypt/stringencryptplugin.cpp b/src/plugins/filemanager/dfmplugin-stringencrypt/stringencryptplugin.cpp new file mode 100644 index 0000000000..baffc4728f --- /dev/null +++ b/src/plugins/filemanager/dfmplugin-stringencrypt/stringencryptplugin.cpp @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "stringencryptplugin.h" +#include "opensslhandler.h" + +Q_DECLARE_METATYPE(QString *); + +using namespace dfmplugin_stringencrypt; +DFM_LOG_REISGER_CATEGORY(dfmplugin_stringencrypt) + +bool StringEncryptPlugin::start() +{ + bindEvents(); + return true; +} + +void StringEncryptPlugin::bindEvents() +{ + dpfSlotChannel->connect("dfmplugin_stringencrypt", "slot_OpenSSL_EncryptString", + OpenSSLHandler::instance(), &OpenSSLHandler::encrypt); +} diff --git a/src/plugins/filemanager/dfmplugin-stringencrypt/stringencryptplugin.h b/src/plugins/filemanager/dfmplugin-stringencrypt/stringencryptplugin.h new file mode 100644 index 0000000000..d014ce1b20 --- /dev/null +++ b/src/plugins/filemanager/dfmplugin-stringencrypt/stringencryptplugin.h @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later +#ifndef STRINGENCRYPTPLUGIN_H +#define STRINGENCRYPTPLUGIN_H + +#include +#include +#include + +namespace dfmplugin_stringencrypt { +class StringEncryptPlugin : public DPF_NAMESPACE::Plugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.deepin.plugin.filemanager" FILE "stringencrypt.json") + + DPF_EVENT_NAMESPACE(dfmplugin_stringencrypt) + DPF_EVENT_REG_SLOT(slot_OpenSSL_EncryptString) + +public: + virtual bool start() override; + +private: + void bindEvents(); +}; +} + +#endif // STRINGENCRYPTPLUGIN_H