diff --git a/Common/3dParty/openssl/common/common_openssl.cpp b/Common/3dParty/openssl/common/common_openssl.cpp index b5d32dd8ce..1d8d1d5dae 100644 --- a/Common/3dParty/openssl/common/common_openssl.cpp +++ b/Common/3dParty/openssl/common/common_openssl.cpp @@ -39,6 +39,8 @@ #include #include +#include "../../../../DesktopEditor/common/File.h" + namespace NSOpenSSL { unsigned char* openssl_alloc(unsigned int len) diff --git a/Common/3dParty/openssl/common/common_openssl.h b/Common/3dParty/openssl/common/common_openssl.h index 3e6b48a51c..d6f847971c 100644 --- a/Common/3dParty/openssl/common/common_openssl.h +++ b/Common/3dParty/openssl/common/common_openssl.h @@ -46,31 +46,25 @@ #define OPENSSL_AES_256_CBC 0 -#include "../../../../DesktopEditor/common/File.h" - -#ifdef COMMON_OPENSSL_BUILDING -#define OPENSSL_DECL Q_DECL_EXPORT -#else -#define OPENSSL_DECL Q_DECL_IMPORT -#endif +#include "../../../../DesktopEditor/xmlsec/src/include/openssl_config.h" namespace NSOpenSSL { // alloc - OPENSSL_DECL unsigned char* openssl_alloc(unsigned int len); - OPENSSL_DECL void openssl_free(unsigned char* data); + OPENSSL_DECL unsigned char* openssl_alloc(unsigned int len); + OPENSSL_DECL void openssl_free(unsigned char* data); // hash - OPENSSL_DECL unsigned int GetHashSize(const int& alg); - OPENSSL_DECL unsigned char* GetHash(const unsigned char* data, const unsigned int& size, const int& alg, unsigned int& len); + OPENSSL_DECL unsigned int GetHashSize(const int& alg); + OPENSSL_DECL unsigned char* GetHash(const unsigned char* data, const unsigned int& size, const int& alg, unsigned int& len); // rsa - OPENSSL_DECL bool RSA_GenerateKeys(unsigned char*& publicKey, unsigned char*& privateKey); - OPENSSL_DECL bool RSA_EncryptPublic(const unsigned char* publicKey, const unsigned char* data, const unsigned int& size, unsigned char*& data_crypt, unsigned int& data_crypt_len); - OPENSSL_DECL bool RSA_DecryptPrivate(const unsigned char* privateKey, const unsigned char* data, const unsigned int& size, unsigned char*& data_decrypt, unsigned int& data_decrypt_len); + OPENSSL_DECL bool RSA_GenerateKeys(unsigned char*& publicKey, unsigned char*& privateKey); + OPENSSL_DECL bool RSA_EncryptPublic(const unsigned char* publicKey, const unsigned char* data, const unsigned int& size, unsigned char*& data_crypt, unsigned int& data_crypt_len); + OPENSSL_DECL bool RSA_DecryptPrivate(const unsigned char* privateKey, const unsigned char* data, const unsigned int& size, unsigned char*& data_decrypt, unsigned int& data_decrypt_len); - OPENSSL_DECL bool RSA_EncryptPublic_desktop(const unsigned char* publicKey, const std::string& input, std::string& out); - OPENSSL_DECL bool RSA_DecryptPrivate_desktop(const unsigned char* privateKey, const std::string& input, std::string& out); + OPENSSL_DECL bool RSA_EncryptPublic_desktop(const unsigned char* publicKey, const std::string& input, std::string& out); + OPENSSL_DECL bool RSA_DecryptPrivate_desktop(const unsigned char* privateKey, const std::string& input, std::string& out); // pbkdf2 OPENSSL_DECL unsigned char* PBKDF2(const char* pass, int passlen, const unsigned char* salt, int saltlen, int hash_alg, int key_len); diff --git a/DesktopEditor/xmlsec/src/include/Certificate.h b/DesktopEditor/xmlsec/src/include/Certificate.h index 0d38dcf281..f1cdb2d849 100644 --- a/DesktopEditor/xmlsec/src/include/Certificate.h +++ b/DesktopEditor/xmlsec/src/include/Certificate.h @@ -1,9 +1,7 @@ #ifndef _XML_CERTIFICATE_BASE_H_ #define _XML_CERTIFICATE_BASE_H_ -#include -#include -#include "../../../common/base_export.h" +#include "./openssl_config.h" #define OOXML_HASH_ALG_SHA1 0 #define OOXML_HASH_ALG_INVALID 1 @@ -28,16 +26,11 @@ #define CERTIFICATE_ENGINE_TYPE_OPENSSL 1 #define CERTIFICATE_ENGINE_TYPE_MSCRYPTO 2 -class Q_DECL_EXPORT ICertificate +class OPENSSL_DECL ICertificate { public: - ICertificate() - { - } - - virtual ~ICertificate() - { - } + ICertificate(); + virtual ~ICertificate(); public: virtual int GetType() = 0; diff --git a/DesktopEditor/xmlsec/src/include/CertificateCommon.h b/DesktopEditor/xmlsec/src/include/CertificateCommon.h index dd056f4813..0c4e28eb00 100644 --- a/DesktopEditor/xmlsec/src/include/CertificateCommon.h +++ b/DesktopEditor/xmlsec/src/include/CertificateCommon.h @@ -1,12 +1,9 @@ #ifndef _XML_CERTIFICATE_COMMON_H_ #define _XML_CERTIFICATE_COMMON_H_ -#include -#include -#include "../../../common/base_export.h" #include "./Certificate.h" -class Q_DECL_EXPORT CCertificateInfo +class OPENSSL_DECL CCertificateInfo { public: std::wstring Name; @@ -15,36 +12,36 @@ public: std::string RawData; public: - CCertificateInfo() {} - ~CCertificateInfo() {} + CCertificateInfo(); + ~CCertificateInfo(); }; namespace NSOpenSSL { - Q_DECL_EXPORT int LoadKey(std::wstring file, std::string password); - Q_DECL_EXPORT int LoadCert(std::wstring file, std::string password); - Q_DECL_EXPORT int LoadKeyRaw(unsigned char* data, unsigned int len, std::string password); - Q_DECL_EXPORT int LoadCertRaw(unsigned char* data, unsigned int len, std::string password); + OPENSSL_DECL int LoadKey(std::wstring file, std::string password); + OPENSSL_DECL int LoadCert(std::wstring file, std::string password); + OPENSSL_DECL int LoadKeyRaw(unsigned char* data, unsigned int len, std::string password); + OPENSSL_DECL int LoadCertRaw(unsigned char* data, unsigned int len, std::string password); } namespace NSCertificate { - Q_DECL_EXPORT CCertificateInfo GetDefault(); - Q_DECL_EXPORT ICertificate* GetById(const std::string& id); + OPENSSL_DECL CCertificateInfo GetDefault(); + OPENSSL_DECL ICertificate* GetById(const std::string& id); - Q_DECL_EXPORT CCertificateInfo GetInfo(ICertificate* pCert); + OPENSSL_DECL CCertificateInfo GetInfo(ICertificate* pCert); - Q_DECL_EXPORT ICertificate* FromFiles(const std::wstring& keyPath, const std::string& keyPassword, const std::wstring& certPath, const std::string& certPassword); - Q_DECL_EXPORT ICertificate* FromFilesRaw(unsigned char* key, unsigned int keyLen, const std::string& keyPassword, + OPENSSL_DECL ICertificate* FromFiles(const std::wstring& keyPath, const std::string& keyPassword, const std::wstring& certPath, const std::string& certPassword); + OPENSSL_DECL ICertificate* FromFilesRaw(unsigned char* key, unsigned int keyLen, const std::string& keyPassword, unsigned char* cert, unsigned int certLen, const std::string& certPassword); - Q_DECL_EXPORT int GetOOXMLHashAlg(const std::string& sAlg); - Q_DECL_EXPORT std::string GetDigestMethodA(const int& nAlg); - Q_DECL_EXPORT std::wstring GetDigestMethod(const int& nAlg); - Q_DECL_EXPORT std::string GetSignatureMethodA(const int& nAlg); - Q_DECL_EXPORT std::wstring GetSignatureMethod(const int& nAlg); + OPENSSL_DECL int GetOOXMLHashAlg(const std::string& sAlg); + OPENSSL_DECL std::string GetDigestMethodA(const int& nAlg); + OPENSSL_DECL std::wstring GetDigestMethod(const int& nAlg); + OPENSSL_DECL std::string GetSignatureMethodA(const int& nAlg); + OPENSSL_DECL std::wstring GetSignatureMethod(const int& nAlg); - Q_DECL_EXPORT ICertificate* CreateInstance(const int& type = CERTIFICATE_ENGINE_TYPE_DEFAULT); + OPENSSL_DECL ICertificate* CreateInstance(const int& type = CERTIFICATE_ENGINE_TYPE_DEFAULT); }; #endif // _XML_CERTIFICATE_COMMON_H_ diff --git a/DesktopEditor/xmlsec/src/include/OOXMLSigner.h b/DesktopEditor/xmlsec/src/include/OOXMLSigner.h index c542722217..6476a2b871 100644 --- a/DesktopEditor/xmlsec/src/include/OOXMLSigner.h +++ b/DesktopEditor/xmlsec/src/include/OOXMLSigner.h @@ -4,7 +4,7 @@ #include "./Certificate.h" class COOXMLSigner_private; -class Q_DECL_EXPORT COOXMLSigner +class OPENSSL_DECL COOXMLSigner { public: COOXMLSigner(const std::wstring& sFolder, ICertificate* pContext); diff --git a/DesktopEditor/xmlsec/src/include/OOXMLVerifier.h b/DesktopEditor/xmlsec/src/include/OOXMLVerifier.h index 4518d0b5ce..4b087251f9 100644 --- a/DesktopEditor/xmlsec/src/include/OOXMLVerifier.h +++ b/DesktopEditor/xmlsec/src/include/OOXMLVerifier.h @@ -9,7 +9,7 @@ #define OOXML_SIGNATURE_BAD 3 class COOXMLSignature_private; -class Q_DECL_EXPORT COOXMLSignature +class OPENSSL_DECL COOXMLSignature { public: COOXMLSignature(); diff --git a/DesktopEditor/xmlsec/src/include/openssl_config.h b/DesktopEditor/xmlsec/src/include/openssl_config.h new file mode 100644 index 0000000000..0221ea206d --- /dev/null +++ b/DesktopEditor/xmlsec/src/include/openssl_config.h @@ -0,0 +1,18 @@ +#ifndef _XML_OPENSSL_CONFIG_H_ +#define _XML_OPENSSL_CONFIG_H_ + +#include +#include +#include "../../../common/base_export.h" + +#ifdef COMMON_OPENSSL_BUILDING_INTERNAL +#define OPENSSL_DECL +#else +#ifdef COMMON_OPENSSL_BUILDING +#define OPENSSL_DECL Q_DECL_EXPORT +#else +#define OPENSSL_DECL Q_DECL_IMPORT +#endif +#endif + +#endif // _XML_OPENSSL_CONFIG_H_ diff --git a/DesktopEditor/xmlsec/src/ooxmlsignature.pro b/DesktopEditor/xmlsec/src/ooxmlsignature.pro index 946a14bbb8..de2cf476e5 100644 --- a/DesktopEditor/xmlsec/src/ooxmlsignature.pro +++ b/DesktopEditor/xmlsec/src/ooxmlsignature.pro @@ -37,6 +37,7 @@ SOURCES += \ src/OOXMLVerifier.cpp core_windows { + DEFINES += SUPPORT_MS_CRYPTO HEADERS += src/Certificate_mscrypto.h LIBS += -lcrypt32 diff --git a/DesktopEditor/xmlsec/src/src/CertificateCommon.cpp b/DesktopEditor/xmlsec/src/src/CertificateCommon.cpp index 25ca1cec6e..e7e1a6ee72 100644 --- a/DesktopEditor/xmlsec/src/src/CertificateCommon.cpp +++ b/DesktopEditor/xmlsec/src/src/CertificateCommon.cpp @@ -1,4 +1,4 @@ -#ifdef _WIN32 +#ifdef SUPPORT_MS_CRYPTO #include "./Certificate_mscrypto.h" #endif @@ -6,9 +6,20 @@ #include "./../include/CertificateCommon.h" +ICertificate::ICertificate() +{ +} + +ICertificate::~ICertificate() +{ +} + +CCertificateInfo::CCertificateInfo() {} +CCertificateInfo::~CCertificateInfo() {} + namespace NSOpenSSL { - int LoadKey(std::wstring file, std::string password) + int LoadKey(std::wstring file, std::string password) { return CCertificate_openssl::LoadKey(file, password, NULL); } @@ -34,7 +45,7 @@ namespace NSCertificate { CCertificateInfo info; - #ifdef _WIN32 + #ifdef SUPPORT_MS_CRYPTO // detect user name std::wstring sUserName; @@ -66,7 +77,7 @@ namespace NSCertificate } ICertificate* GetById(const std::string& id) { - #ifdef _WIN32 + #ifdef SUPPORT_MS_CRYPTO HANDLE hStoreHandle = CertOpenSystemStoreA(NULL, "MY"); if (!hStoreHandle) return NULL; @@ -250,7 +261,7 @@ namespace NSCertificate if (typeCreate == CERTIFICATE_ENGINE_TYPE_OPENSSL) return new CCertificate_openssl(); -#ifdef _WIN32 +#ifdef SUPPORT_MS_CRYPTO if (typeCreate == CERTIFICATE_ENGINE_TYPE_MSCRYPTO) return new CCertificate_mscrypto(); #endif diff --git a/DesktopEditor/xmlsec/src/src/Certificate_mscrypto.h b/DesktopEditor/xmlsec/src/src/Certificate_mscrypto.h index 7f1dbbc061..2db74ea5cc 100644 --- a/DesktopEditor/xmlsec/src/src/Certificate_mscrypto.h +++ b/DesktopEditor/xmlsec/src/src/Certificate_mscrypto.h @@ -1,7 +1,7 @@ #ifndef _XMLSIGNER_MSCRYPTO_H_ #define _XMLSIGNER_MSCRYPTO_H_ -#include "./include/Certificate.h" +#include "./../include/Certificate.h" #include #include diff --git a/DesktopEditor/xmlsec/src/wasm/openssl/main.cpp b/DesktopEditor/xmlsec/src/wasm/openssl/main.cpp new file mode 100644 index 0000000000..11fe9d3313 --- /dev/null +++ b/DesktopEditor/xmlsec/src/wasm/openssl/main.cpp @@ -0,0 +1,194 @@ +#include "../../../../common/File.h" + +#include "../../include/Certificate.h" +#include "../../include/CertificateCommon.h" + + +#ifdef _WIN32 +#define WASM_EXPORT __declspec(dllexport) +#else +#define WASM_EXPORT __attribute__((visibility("default"))) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +WASM_EXPORT void* XmlSignature_Malloc(unsigned int size) +{ + return ::malloc(size); +} +WASM_EXPORT void XmlSignature_Free(void* p) +{ + if (p) ::free(p); +} + +class CMemoryFile +{ +public: + unsigned int Length; + BYTE* Data; + +public: + CMemoryFile(BYTE* data = NULL, unsigned int len = 0) { Length = len; Data = data; } + ~CMemoryFile() { if (Data) delete [] Data; } +}; + +class CCertificate +{ +public: + ICertificate* m_pCert; + +private: + unsigned char* m_key; + unsigned int m_keyLen; + std::string m_keyPass; + + unsigned char* m_cert; + unsigned int m_certLen; + std::string m_certPass; + +public: + CCertificate() + { + m_pCert = NULL; + } + ~CCertificate() + { + delete m_pCert; + } + +public: + int LoadCert(unsigned char* data, unsigned int len, char* pass) + { + m_cert = NULL; m_certLen = 0; m_certPass = ""; + int nRes = NSOpenSSL::LoadCertRaw(data, len, (NULL != pass) ? std::string(pass) : ""); + + if (OPEN_SSL_WARNING_ALL_OK == nRes || OPEN_SSL_WARNING_OK == nRes) + { + m_cert = data; + m_certLen = len; + m_certPass = (NULL != pass) ? std::string(pass) : ""; + } + if (OPEN_SSL_WARNING_ALL_OK == nRes) + { + m_key = m_cert; + m_keyLen = m_certLen; + m_keyPass = m_certPass; + } + + return nRes; + } + int LoadKey(unsigned char* data, unsigned int len, char* pass) + { + m_key = NULL; m_keyLen = 0; m_keyPass = ""; + int nRes = NSOpenSSL::LoadKeyRaw(data, len, (NULL != pass) ? std::string(pass) : ""); + + if (OPEN_SSL_WARNING_ALL_OK == nRes || OPEN_SSL_WARNING_OK == nRes) + { + m_key = data; + m_keyLen = len; + m_keyPass = (NULL != pass) ? std::string(pass) : ""; + } + if (OPEN_SSL_WARNING_ALL_OK == nRes) + { + m_cert = m_key; + m_certLen = m_keyLen; + m_certPass = m_keyPass; + } + + return nRes; + } + + void Load() + { + m_pCert = NSCertificate::FromFilesRaw(m_key, m_keyLen, m_keyPass, m_cert, m_certLen, m_certPass); + } + + ICertificate* GetCertificate() + { + return m_pCert; + } +}; + +WASM_EXPORT void* XmlSignature_CreateCertificate() +{ + return new CCertificate(); +} +WASM_EXPORT int XmlSignature_LoadCert(void* cert, unsigned char* data, unsigned int len, char* pass) +{ + CCertificate* pCert = (CCertificate*)cert; + return pCert->LoadCert(data, len, pass); +} +WASM_EXPORT int XmlSignature_LoadKey(void* cert, unsigned char* data, unsigned int len, char* pass) +{ + CCertificate* pCert = (CCertificate*)cert; + return pCert->LoadKey(data, len, pass); +} +WASM_EXPORT void XmlSignature_DestroyCertificate(void* cert) +{ + CCertificate* pCert = (CCertificate*)cert; + delete pCert; +} + +WASM_EXPORT void* XmlSignature_CreateFile(unsigned char* file, unsigned int len) +{ + return new CMemoryFile(file, len); +} +WASM_EXPORT unsigned int XmlSignature_GetFileSize(void* file) +{ + CMemoryFile* pZipFile = (CMemoryFile*)file; + return pZipFile->Length; +} +WASM_EXPORT void* XmlSignature_GetFileRaw(void* file) +{ + CMemoryFile* pZipFile = (CMemoryFile*)file; + return pZipFile->Data; +} +WASM_EXPORT void XmlSignature_DestroyFile(void* file) +{ + CMemoryFile* pZipFile = (CMemoryFile*)file; + delete pZipFile; +} + +#ifdef __cplusplus +} +#endif + +#ifdef TEST_AS_EXECUTABLE +int main() +{ + std::wstring sTestDir = NSFile::GetProcessDirectory() + L"/../../../../test/"; + void* cert = XmlSignature_CreateCertificate(); + + BYTE* pCertData = NULL; + DWORD nCertLen = 0; + NSFile::CFileBinary::ReadAllBytes(sTestDir + L"keys/cert.crt", &pCertData, nCertLen); + XmlSignature_LoadCert(cert, pCertData, nCertLen, NULL); + + BYTE* pKeyData = NULL; + DWORD nKeyLen = 0; + NSFile::CFileBinary::ReadAllBytes(sTestDir + L"keys/key.key", &pKeyData, nKeyLen); + XmlSignature_LoadKey(cert, pKeyData, nKeyLen, NULL); + + BYTE* pData = NULL; + DWORD nLen = 0; + NSFile::CFileBinary::ReadAllBytes(sTestDir + L"file.docx", &pData, nLen); + CMemoryFile* file = (CMemoryFile*)XmlSignature_CreateFile(pData, nLen); + CMemoryFile* res;// = (CMemoryFile*)XmlSignature_Sign(file, cert); + + NSFile::CFileBinary oFileDst; + oFileDst.CreateFileW(sTestDir + L"/file2.docx"); + oFileDst.WriteFile(res->Data, (DWORD)res->Length); + oFileDst.CloseFile(); + + XmlSignature_DestroyFile(file); // RELEASEARRAYOBJECTS(pData); + XmlSignature_DestroyFile(res); + XmlSignature_DestroyCertificate(cert); + + RELEASEARRAYOBJECTS(pCertData); + RELEASEARRAYOBJECTS(pKeyData); + + return 0; +} +#endif diff --git a/DesktopEditor/xmlsec/src/wasm/openssl/openssl_module.pro b/DesktopEditor/xmlsec/src/wasm/openssl/openssl_module.pro new file mode 100644 index 0000000000..79cc0d8089 --- /dev/null +++ b/DesktopEditor/xmlsec/src/wasm/openssl/openssl_module.pro @@ -0,0 +1,41 @@ +QT -= core gui + +TARGET = wasm +TEMPLATE = app +CONFIG += console +CONFIG -= app_bundle + +DEFINES += TEST_AS_EXECUTABLE + +CORE_ROOT_DIR = $$PWD/../../../../.. +PWD_ROOT_DIR = $$PWD +include($$CORE_ROOT_DIR/Common/base.pri) + +DEFINES += KERNEL_NO_USE_DYNAMIC_LIBRARY +DEFINES += COMMON_OPENSSL_BUILDING_INTERNAL + +SOURCES += \ + $$CORE_ROOT_DIR/DesktopEditor/common/File.cpp \ + $$CORE_ROOT_DIR/DesktopEditor/common/Base64.cpp + +HEADERS += \ + $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/include/Certificate.h \ + $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/include/CertificateCommon.h + +SOURCES += \ + $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/CertificateCommon.cpp + +# OPENSSL +include($$CORE_ROOT_DIR/Common/3dParty/openssl/openssl.pri) + +core_windows { + LIBS += -lcrypt32 + LIBS += -lcryptui + LIBS += -lAdvapi32 + LIBS += -lws2_32 + LIBS += -lUser32 +} + + +# WASM EXPORT +SOURCES += main.cpp diff --git a/DesktopEditor/xmlsec/src/wasm/xmlsec.pro b/DesktopEditor/xmlsec/src/wasm/xmlsec.pro index cc995cd95f..46e4517485 100644 --- a/DesktopEditor/xmlsec/src/wasm/xmlsec.pro +++ b/DesktopEditor/xmlsec/src/wasm/xmlsec.pro @@ -12,7 +12,9 @@ PWD_ROOT_DIR = $$PWD include($$CORE_ROOT_DIR/Common/base.pri) DEFINES += KERNEL_NO_USE_DYNAMIC_LIBRARY -DEFINES += DISABLE_Q_DECL_EXPORT +DEFINES += COMMON_OPENSSL_BUILDING_INTERNAL + +ADD_DEPENDENCY(UnicodeConverter) # XML DEFINES += \ @@ -92,10 +94,9 @@ SOURCES += \ # SIGN SOURCES += \ $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/XmlTransform.cpp \ - $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/XmlCertificate.cpp \ + $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/CertificateCommon.cpp \ $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/OOXMLSigner.cpp \ - $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp \ - $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp + $$CORE_ROOT_DIR/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp core_windows { LIBS += -lcrypt32