From 33f7ee49dfeb87a0f43182d256c12d6b6a9766b9 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Tue, 24 Apr 2018 17:45:58 +0300 Subject: [PATCH 1/9] Bugs --- DesktopEditor/xmlsec/src/include/XmlCertificate.h | 4 ++-- DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp | 2 ++ DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h | 8 ++++---- DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp | 4 ++-- DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/DesktopEditor/xmlsec/src/include/XmlCertificate.h b/DesktopEditor/xmlsec/src/include/XmlCertificate.h index 1d1a1a3526..dfbdbb8101 100644 --- a/DesktopEditor/xmlsec/src/include/XmlCertificate.h +++ b/DesktopEditor/xmlsec/src/include/XmlCertificate.h @@ -106,8 +106,8 @@ public: virtual bool LoadFromBase64Data(const std::string& data) = 0; public: - virtual int ShowSelectDialog() = 0; - virtual int ShowCertificate() = 0; + virtual int ShowSelectDialog(void* parent = NULL) = 0; + virtual int ShowCertificate(void* parent = NULL) = 0; static CCertificateInfo GetDefault(); static ICertificate* GetById(const std::string& id); diff --git a/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp b/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp index cf49deb1b7..71cddf05ed 100644 --- a/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp +++ b/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp @@ -609,6 +609,8 @@ public: sFile = m_arSignaturesFiles.at(i); m_arSignatures.erase(m_arSignatures.begin() + i); delete pSignature; + --i; + --nCountSignatures; } } diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h b/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h index 98ceb0257e..7615e9ae36 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h @@ -369,19 +369,19 @@ public: return true; } - virtual int ShowCertificate() + virtual int ShowCertificate(void* parent = NULL) { - return (int)CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, m_context, NULL, NULL, 0, NULL); + return (int)CryptUIDlgViewContext(CERT_STORE_CERTIFICATE_CONTEXT, m_context, (NULL == parent) ? NULL : (*((HWND*)parent)), NULL, 0, NULL); } public: - virtual int ShowSelectDialog() + virtual int ShowSelectDialog(void* parent = NULL) { m_store = CertOpenSystemStoreA(NULL, "MY"); if (!m_store) return 0; - m_context = CryptUIDlgSelectCertificateFromStore(m_store, NULL, NULL, NULL, CRYPTUI_SELECT_LOCATION_COLUMN, 0, NULL); + m_context = CryptUIDlgSelectCertificateFromStore(m_store, (NULL == parent) ? NULL : (*((HWND*)parent)), NULL, NULL, CRYPTUI_SELECT_LOCATION_COLUMN, 0, NULL); if (!m_context) { CertCloseStore(m_store, 0); diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp index d416288769..a74100bf1f 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp @@ -769,12 +769,12 @@ bool CCertificate_openssl::LoadFromBase64Data(const std::string& data) return m_internal->LoadFromBase64Data(data); } -int CCertificate_openssl::ShowSelectDialog() +int CCertificate_openssl::ShowSelectDialog(void* parent) { return m_internal->ShowSelectDialog(); } -int CCertificate_openssl::ShowCertificate() +int CCertificate_openssl::ShowCertificate(void* parent) { return m_internal->ShowCertificate(); } diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h index cc324d3ca6..34cc2640e2 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h @@ -42,8 +42,8 @@ public: virtual bool LoadFromBase64Data(const std::string& data); public: - virtual int ShowSelectDialog(); - virtual int ShowCertificate(); + virtual int ShowSelectDialog(void* parent = NULL); + virtual int ShowCertificate(void* parent = NULL); virtual bool FromFiles(const std::wstring& keyPath, const std::string& keyPassword, const std::wstring& certPath, const std::string& certPassword); bool FromId(const std::string& id); From 58cd45f4eefc337157fbea73e69bd867ec8a58e9 Mon Sep 17 00:00:00 2001 From: Sergey Konovalov Date: Tue, 24 Apr 2018 18:52:25 +0300 Subject: [PATCH 2/9] [x2t] Bump compatibilityMode setting Prevent opening files in compatibility mode in Word 2016. --- ASCOfficeDocxFile2/BinReader/SettingWriter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ASCOfficeDocxFile2/BinReader/SettingWriter.h b/ASCOfficeDocxFile2/BinReader/SettingWriter.h index a3a2ff76a4..e5b9b01e8b 100644 --- a/ASCOfficeDocxFile2/BinReader/SettingWriter.h +++ b/ASCOfficeDocxFile2/BinReader/SettingWriter.h @@ -37,7 +37,7 @@ namespace Writers { static std::wstring g_string_set_Start = _T(""); - static std::wstring g_string_set_Default = _T(""); + static std::wstring g_string_set_Default = _T(""); static std::wstring g_string_set_End = _T(""); class SettingWriter From d7e2bedc6b0d201a81b0adf7fb087674b0d99cb9 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Wed, 25 Apr 2018 16:51:10 +0300 Subject: [PATCH 3/9] Openssl errors --- Common/FileDownloader/FileDownloader_mac.mm | 13 ++++++++----- DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp | 11 ++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Common/FileDownloader/FileDownloader_mac.mm b/Common/FileDownloader/FileDownloader_mac.mm index 274b0b569a..e64e09e67e 100644 --- a/Common/FileDownloader/FileDownloader_mac.mm +++ b/Common/FileDownloader/FileDownloader_mac.mm @@ -1,4 +1,4 @@ -#include "FileDownloader.h" +#include "FileDownloader.h" #if _IOS #import @@ -27,10 +27,13 @@ public : } virtual int DownloadFile() - { - m_sFilePath = NSFile::CFileBinary::CreateTempFileWithUniqueName(NSFile::CFileBinary::GetTempPath(), L"DWD"); - if (NSFile::CFileBinary::Exists(m_sFilePath)) - NSFile::CFileBinary::Remove(m_sFilePath); + { + if (m_sFilePath.empty()) + { + m_sFilePath = NSFile::CFileBinary::CreateTempFileWithUniqueName(NSFile::CFileBinary::GetTempPath(), L"DWD"); + if (NSFile::CFileBinary::Exists(m_sFilePath)) + NSFile::CFileBinary::Remove(m_sFilePath); + } NSString* stringURL = StringWToNSString(m_sFileUrl); NSURL *url = [NSURL URLWithString:stringURL]; diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp index a74100bf1f..3ad48fb83b 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp @@ -354,8 +354,10 @@ public: return -1; } - bool FromFiles(const std::wstring& sKeyPath, const std::string& sKeyPassword, const std::wstring& certPath, const std::string& certPassword) + bool FromFiles(const std::wstring& keyPath, const std::string& keyPassword, const std::wstring& certPath, const std::string& certPassword) { + std::wstring sKeyPath = keyPath; + std::string sKeyPassword = keyPassword; std::wstring sCertPath = certPath; std::string sCertPassword = certPassword; @@ -364,6 +366,11 @@ public: sCertPath = sKeyPath; sCertPassword = sKeyPassword; } + else if (sKeyPath.empty()) + { + sKeyPath = sCertPath; + sKeyPassword = sCertPassword; + } int nErr = LoadKey(sKeyPath, sKeyPassword, &m_key); @@ -423,6 +430,8 @@ public: int VerifySelf() { + return OPEN_SSL_WARNING_OK; + if (NULL == m_cert) return OPEN_SSL_WARNING_NOVERIFY; From b9b843f25dbb753795f3d79060f08d6231af319c Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Thu, 26 Apr 2018 13:45:02 +0300 Subject: [PATCH 4/9] Windows: add long file names support (x2t) --- Common/OfficeFileFormatChecker2.cpp | 8 +++- DesktopEditor/common/Directory.h | 3 +- DesktopEditor/common/File.h | 14 +++--- OfficeUtils/src/OfficeUtils.cpp | 68 +++++++++++++++++++++++++---- 4 files changed, 78 insertions(+), 15 deletions(-) diff --git a/Common/OfficeFileFormatChecker2.cpp b/Common/OfficeFileFormatChecker2.cpp index e921ff292b..6e157fd9f7 100644 --- a/Common/OfficeFileFormatChecker2.cpp +++ b/Common/OfficeFileFormatChecker2.cpp @@ -245,8 +245,14 @@ bool COfficeFileFormatChecker::isMS_OFFCRYPTOFormatFile (POLE::Storage * storage return true; return false; } -bool COfficeFileFormatChecker::isOfficeFile(const std::wstring & fileName) +bool COfficeFileFormatChecker::isOfficeFile(const std::wstring & _fileName) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring fileName = CorrectPathW(_fileName); +#else + std::wstring fileName = _fileName; +#endif + //приоритет как оказывается важен //Metamorphic Manual for windows 28415.doc POLE::Storage storage(fileName.c_str()); diff --git a/DesktopEditor/common/Directory.h b/DesktopEditor/common/Directory.h index cdaeea37b1..c969f84c99 100644 --- a/DesktopEditor/common/Directory.h +++ b/DesktopEditor/common/Directory.h @@ -314,7 +314,8 @@ namespace NSDirectory static bool Exists(const std::wstring& strDirectory) { #if defined(_WIN32) || defined (_WIN64) - DWORD dwAttrib = ::GetFileAttributesW(strDirectory.c_str()); + std::wstring sDirectoryW = CorrectPathW(strDirectory); + DWORD dwAttrib = ::GetFileAttributesW(strDirectoryW.c_str()); return (dwAttrib != INVALID_FILE_ATTRIBUTES && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); #elif __linux__ BYTE* pUtf8 = NULL; diff --git a/DesktopEditor/common/File.h b/DesktopEditor/common/File.h index 7cf654892e..181e72daf1 100644 --- a/DesktopEditor/common/File.h +++ b/DesktopEditor/common/File.h @@ -57,14 +57,17 @@ // local files: '\\?\' prefix // server files: '\\?\UNC\' prefix <== TODO! int nLen = GetFullPathNameW(path_str, 0, 0, 0); - wchar_t* pBuf = new wchar_t[(4 + nLen) * sizeof(wchar_t)]; + if (2 > nLen) + return path; - pBuf[0] = L'\\', pBuf[1] = L'\\', pBuf[2] = L'?', pBuf[3] = L'\\'; - GetFullPathNameW(path_str, nLen, pBuf + 4, NULL); + wchar_t* pBuf = new wchar_t[nLen * sizeof(wchar_t)]; + GetFullPathNameW(path_str, nLen, pBuf, NULL); + if (pBuf[0] == '\\' || pBuf[1] == '/') + return path; std::wstring retPath(pBuf); delete [] pBuf; - return retPath; + return L"\\\\?\\" + retPath; } #endif @@ -854,8 +857,9 @@ namespace NSFile static bool Exists(const std::wstring& strFileName) { #if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring sFileNameW = CorrectPathW(sFileName); FILE* pFile = NULL; - if ( 0 != _wfopen_s( &pFile, strFileName.c_str(), L"rb")) + if ( 0 != _wfopen_s( &pFile, sFileNameW.c_str(), L"rb")) return false; #else BYTE* pUtf8 = NULL; diff --git a/OfficeUtils/src/OfficeUtils.cpp b/OfficeUtils/src/OfficeUtils.cpp index ef91f5143d..2d01f5b333 100644 --- a/OfficeUtils/src/OfficeUtils.cpp +++ b/OfficeUtils/src/OfficeUtils.cpp @@ -39,8 +39,16 @@ COfficeUtils::COfficeUtils(OnProgressCallback* fCallback) m_fCallback = fCallback; } -HRESULT COfficeUtils::ExtractToDirectory(const std::wstring& zipFile, const std::wstring& unzipDir, wchar_t* password, SHORT extract_without_path) +HRESULT COfficeUtils::ExtractToDirectory(const std::wstring& _zipFile, const std::wstring& _unzipDir, wchar_t* password, SHORT extract_without_path) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring zipFile = CorrectPathW(_zipFile); + std::wstring unzipDir = CorrectPathW(_unzipDir); +#else + std::wstring zipFile = _zipFile; + std::wstring unzipDir = _unzipDir; +#endif + if( ZLibZipUtils::UnzipToDir( zipFile.c_str(), unzipDir.c_str(), m_fCallback, password, ( extract_without_path > 0 ) ? (true) : (false) ) == 0 ) { return S_OK; @@ -52,8 +60,16 @@ HRESULT COfficeUtils::ExtractToDirectory(const std::wstring& zipFile, const std: } -HRESULT COfficeUtils::CompressFileOrDirectory(const std::wstring& name, const std::wstring& outputFile, bool bSorted, short level) +HRESULT COfficeUtils::CompressFileOrDirectory(const std::wstring& _name, const std::wstring& _outputFile, bool bSorted, short level) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring name = CorrectPathW(_name); + std::wstring outputFile = CorrectPathW(_outputFile); +#else + std::wstring name = _name; + std::wstring outputFile = _outputFile; +#endif + HRESULT result = S_FALSE; if(NSDirectory::Exists(name)) { @@ -104,8 +120,14 @@ HRESULT COfficeUtils::Compress(BYTE* destBuf, ULONG* destSize, BYTE* sourceBuf, } } -HRESULT COfficeUtils::IsArchive(const std::wstring& filename) +HRESULT COfficeUtils::IsArchive(const std::wstring& _filename) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring filename = CorrectPathW(_filename); +#else + std::wstring filename = _filename; +#endif + if( ZLibZipUtils::IsArchive(filename.c_str()) ) { return S_OK; @@ -116,8 +138,14 @@ HRESULT COfficeUtils::IsArchive(const std::wstring& filename) } } -HRESULT COfficeUtils::IsFileExistInArchive(const std::wstring& zipFile, const std::wstring& filePath) +HRESULT COfficeUtils::IsFileExistInArchive(const std::wstring& _zipFile, const std::wstring& filePath) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring zipFile = CorrectPathW(_zipFile); +#else + std::wstring zipFile = _zipFile; +#endif + if( ZLibZipUtils::IsFileExistInArchive( zipFile.c_str(), filePath.c_str()) ) { return S_OK; @@ -128,8 +156,14 @@ HRESULT COfficeUtils::IsFileExistInArchive(const std::wstring& zipFile, const st } } -HRESULT COfficeUtils::LoadFileFromArchive(const std::wstring& zipFile, const std::wstring& filePath, BYTE** fileInBytes, ULONG& nFileSize) +HRESULT COfficeUtils::LoadFileFromArchive(const std::wstring& _zipFile, const std::wstring& filePath, BYTE** fileInBytes, ULONG& nFileSize) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring zipFile = CorrectPathW(_zipFile); +#else + std::wstring zipFile = _zipFile; +#endif + if( ZLibZipUtils::LoadFileFromArchive( zipFile.c_str(), filePath.c_str(), fileInBytes, nFileSize)) { return S_OK; @@ -140,20 +174,38 @@ HRESULT COfficeUtils::LoadFileFromArchive(const std::wstring& zipFile, const std } } -HRESULT COfficeUtils::ExtractFilesToMemory(const std::wstring& zipFile, const ExtractedFileCallback& data_receiver, void* pParam, bool* result) +HRESULT COfficeUtils::ExtractFilesToMemory(const std::wstring& _zipFile, const ExtractedFileCallback& data_receiver, void* pParam, bool* result) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring zipFile = CorrectPathW(_zipFile); +#else + std::wstring zipFile = _zipFile; +#endif + *result = ZLibZipUtils::ExtractFiles(zipFile.c_str(), data_receiver, pParam) ? true : false; return S_OK; } -HRESULT COfficeUtils::CompressFilesFromMemory(const std::wstring& zipFile, const RequestFileCallback& data_source, void* pParam, SHORT compression_level, bool* result) +HRESULT COfficeUtils::CompressFilesFromMemory(const std::wstring& _zipFile, const RequestFileCallback& data_source, void* pParam, SHORT compression_level, bool* result) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring zipFile = CorrectPathW(_zipFile); +#else + std::wstring zipFile = _zipFile; +#endif + *result = ZLibZipUtils::CompressFiles(zipFile.c_str(), data_source, pParam, compression_level) ? true : false; return S_OK; } -HRESULT COfficeUtils::GetFilesSize(const std::wstring& zipFile, const std::wstring& searchPattern, ULONG& nCommpressed, ULONG& nUncommpressed) +HRESULT COfficeUtils::GetFilesSize(const std::wstring& _zipFile, const std::wstring& searchPattern, ULONG& nCommpressed, ULONG& nUncommpressed) { +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) + std::wstring zipFile = CorrectPathW(_zipFile); +#else + std::wstring zipFile = _zipFile; +#endif + if (ZLibZipUtils::GetFilesSize(zipFile.c_str(), searchPattern, nCommpressed, nUncommpressed)) { return S_OK; From 261e48227d26ddad29cd6b80907b0ef0e4168dda Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Thu, 26 Apr 2018 18:29:39 +0300 Subject: [PATCH 5/9] Bugs --- DesktopEditor/common/Directory.h | 2 +- DesktopEditor/common/File.h | 2 +- .../xmlsec/src/src/OOXMLVerifier.cpp | 35 +++++++++++++++++++ OfficeUtils/src/OfficeUtils.cpp | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/DesktopEditor/common/Directory.h b/DesktopEditor/common/Directory.h index c969f84c99..02a7f2dd02 100644 --- a/DesktopEditor/common/Directory.h +++ b/DesktopEditor/common/Directory.h @@ -314,7 +314,7 @@ namespace NSDirectory static bool Exists(const std::wstring& strDirectory) { #if defined(_WIN32) || defined (_WIN64) - std::wstring sDirectoryW = CorrectPathW(strDirectory); + std::wstring strDirectoryW = CorrectPathW(strDirectory); DWORD dwAttrib = ::GetFileAttributesW(strDirectoryW.c_str()); return (dwAttrib != INVALID_FILE_ATTRIBUTES && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); #elif __linux__ diff --git a/DesktopEditor/common/File.h b/DesktopEditor/common/File.h index 181e72daf1..d9d7519c1f 100644 --- a/DesktopEditor/common/File.h +++ b/DesktopEditor/common/File.h @@ -857,7 +857,7 @@ namespace NSFile static bool Exists(const std::wstring& strFileName) { #if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) - std::wstring sFileNameW = CorrectPathW(sFileName); + std::wstring sFileNameW = CorrectPathW(strFileName); FILE* pFile = NULL; if ( 0 != _wfopen_s( &pFile, sFileNameW.c_str(), L"rb")) return false; diff --git a/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp b/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp index 71cddf05ed..4ae3b6af37 100644 --- a/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp +++ b/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp @@ -691,6 +691,41 @@ public: sXml += L""; NSFile::CFileBinary::SaveToFile(m_sFolder + L"/_rels/.rels", sXml); } + else + { + std::wstring sFileFound = sFile.substr(m_sFolder.length()); + std::wstring::size_type posRemove = sFileFound.find(L"/_xmlsignatures/"); + if (std::wstring::npos != posRemove) + sFileFound = sFileFound.substr(posRemove + 16); + + std::wstring sOriginRels = m_sFolder + L"/_xmlsignatures/_rels/origin.sigs.rels"; + + XmlUtils::CXmlNode oRels; + if (!oRels.FromXmlFile(sOriginRels)) + return; + + sXml = L""; + XmlUtils::CXmlNodes oNodes; + if (oRels.GetNodes(L"*", oNodes)) + { + int nCount = oNodes.GetCount(); + + for (int i = 0; i < nCount; ++i) + { + XmlUtils::CXmlNode oNode; + oNodes.GetAt(i, oNode); + + if (L"Relationship" == oNode.GetName() && + L"http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature" == oNode.GetAttribute(L"Type") && + sFileFound == oNode.GetAttribute(L"Target")) + continue; + + sXml += oNode.GetXml(); + } + } + sXml += L""; + NSFile::CFileBinary::SaveToFile(sOriginRels, sXml); + } } }; diff --git a/OfficeUtils/src/OfficeUtils.cpp b/OfficeUtils/src/OfficeUtils.cpp index 2d01f5b333..b831bdbda2 100644 --- a/OfficeUtils/src/OfficeUtils.cpp +++ b/OfficeUtils/src/OfficeUtils.cpp @@ -63,7 +63,7 @@ HRESULT COfficeUtils::ExtractToDirectory(const std::wstring& _zipFile, const std HRESULT COfficeUtils::CompressFileOrDirectory(const std::wstring& _name, const std::wstring& _outputFile, bool bSorted, short level) { #if defined(_WIN32) || defined(_WIN32_WCE) || defined(_WIN64) - std::wstring name = CorrectPathW(_name); + std::wstring name = _name;//CorrectPathW(_name); std::wstring outputFile = CorrectPathW(_outputFile); #else std::wstring name = _name; From effdab1ff43ea565cd96ce4e4656f8bfe6f0131f Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Fri, 27 Apr 2018 19:01:51 +0300 Subject: [PATCH 6/9] Add not sha1 algorithms to sign/verify providers --- .../xmlsec/src/include/XmlCertificate.h | 15 +- DesktopEditor/xmlsec/src/src/OOXMLSigner.cpp | 25 +-- .../xmlsec/src/src/OOXMLVerifier.cpp | 4 +- .../xmlsec/src/src/XmlCertificate.cpp | 71 +++++++++ .../xmlsec/src/src/XmlSigner_mscrypto.h | 141 +++++++++++++++-- .../xmlsec/src/src/XmlSigner_openssl.cpp | 149 ++++++++++++++---- .../xmlsec/src/src/XmlSigner_openssl.h | 3 + 7 files changed, 358 insertions(+), 50 deletions(-) diff --git a/DesktopEditor/xmlsec/src/include/XmlCertificate.h b/DesktopEditor/xmlsec/src/include/XmlCertificate.h index dfbdbb8101..3a621af17a 100644 --- a/DesktopEditor/xmlsec/src/include/XmlCertificate.h +++ b/DesktopEditor/xmlsec/src/include/XmlCertificate.h @@ -8,6 +8,9 @@ #define OOXML_HASH_ALG_SHA1 0 #define OOXML_HASH_ALG_INVALID 1 #define OOXML_HASH_ALG_SHA256 2 +#define OOXML_HASH_ALG_SHA224 3 +#define OOXML_HASH_ALG_SHA384 4 +#define OOXML_HASH_ALG_SHA512 5 #define OPEN_SSL_WARNING_OK 0 #define OPEN_SSL_WARNING_ERR 1 @@ -103,7 +106,10 @@ public: virtual std::string GetHash(const std::wstring& sXmlFile, int nAlg) = 0; virtual bool Verify(const std::string& sXml, std::string& sXmlSignature, int nAlg) = 0; - virtual bool LoadFromBase64Data(const std::string& data) = 0; + virtual bool LoadFromBase64Data(const std::string& data) = 0; + + virtual std::vector GetHashAlgs() = 0; + virtual int GetHashAlg() = 0; public: virtual int ShowSelectDialog(void* parent = NULL) = 0; @@ -118,7 +124,12 @@ public: public: static int GetOOXMLHashAlg(const std::string& sAlg); - static ICertificate* CreateInstance(); + static std::string GetDigestMethodA(const int& nAlg); + static std::wstring GetDigestMethod(const int& nAlg); + static std::string GetSignatureMethodA(const int& nAlg); + static std::wstring GetSignatureMethod(const int& nAlg); + + static ICertificate* CreateInstance(); }; #endif // _XML_SERTIFICATE_BASE_H_ diff --git a/DesktopEditor/xmlsec/src/src/OOXMLSigner.cpp b/DesktopEditor/xmlsec/src/src/OOXMLSigner.cpp index 0ce74d5d75..c293cfb881 100644 --- a/DesktopEditor/xmlsec/src/src/OOXMLSigner.cpp +++ b/DesktopEditor/xmlsec/src/src/OOXMLSigner.cpp @@ -46,7 +46,9 @@ public: m_date = NSFile::CUtf8Converter::GetUnicodeFromCharPtr(date); m_signed_info.WriteString(""); - m_signed_info.WriteString(""); + m_signed_info.WriteString("GetHashAlg())); + m_signed_info.WriteString("\"/>"); } ~COOXMLSigner_private() { @@ -55,9 +57,9 @@ public: std::wstring GetReference(const std::wstring& file, const std::wstring& content_type) { std::wstring sXml = L""; - sXml += L""; + sXml += (L"GetHashAlg()) + L"\"/>"); sXml += L""; - std::string sTmp = m_certificate->GetHash(m_sFolder + file, OOXML_HASH_ALG_SHA1); + std::string sTmp = m_certificate->GetHash(m_sFolder + file, m_certificate->GetHashAlg()); sXml += UTF8_TO_U(sTmp); sXml += L""; sXml += L""; @@ -68,7 +70,7 @@ public: { std::string sXmlSigned = U_TO_UTF8(xml); sXmlSigned = CXmlCanonicalizator::Execute(sXmlSigned, XML_C14N_1_0); - return m_certificate->GetHash(sXmlSigned, OOXML_HASH_ALG_SHA1); + return m_certificate->GetHash(sXmlSigned, m_certificate->GetHashAlg()); } std::string GetReferenceMain(const std::wstring& xml, const std::wstring& id, const bool& isCannon = true) @@ -87,7 +89,7 @@ public: if (isCannon) sRet = ""; - sRet += ("" + sHash + ""); + sRet += ("GetHashAlg()) + "\"/>" + sHash + ""); return sRet; } @@ -144,7 +146,7 @@ public: builder.WriteString(file); builder.WriteString(L"?ContentType=application/vnd.openxmlformats-package.relationships+xml\">"); builder.WriteString(oRels.GetTransforms()); - builder.WriteString(L""); + builder.WriteString(L"GetHashAlg()) + L"\"/>"); std::wstring sXml = oRels.GetXml(); std::string sHash = GetHashXml(sXml); @@ -489,13 +491,16 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign std::string sKeyA = m_certificate->GetNumber(); std::wstring sKey = UTF8_TO_U(sKeyA); + std::string sCertHA = m_certificate->GetCertificateHash(); + std::wstring sCertHW = UTF8_TO_U(sCertHA); + std::wstring sXml = (L"\ " + m_date + L"\ \ \ \ -\ -MJJT2Y0iMxaPGVXBmOLb9bY60pA=\ +GetHashAlg()) + L"\"/>\ +" + sCertHW + L"\ \ \ CN=" + sName + L"\ @@ -516,8 +521,8 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign m_signed_info.WriteString(""); m_signed_info.WriteString(""); - m_signed_info.WriteString(""); - m_signed_info.WriteString(m_certificate->GetHash(sXmlTmp, OOXML_HASH_ALG_SHA1)); + m_signed_info.WriteString("GetHashAlg()) + "\"/>"); + m_signed_info.WriteString(m_certificate->GetHash(sXmlTmp, m_certificate->GetHashAlg())); m_signed_info.WriteString(""); return (L"\ diff --git a/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp b/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp index 4ae3b6af37..9f5a426d3b 100644 --- a/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp +++ b/DesktopEditor/xmlsec/src/src/OOXMLVerifier.cpp @@ -252,7 +252,9 @@ public: return; } - // 5) Check signature + // 5) Check sert digest! (TODO:) + + // 6) Check signature CXmlStackNamespaces stack(m_node); CXmlStackNamespaces stackRes = stack.GetById("SignedInfo", true); std::string sXml = stackRes.GetXml(); diff --git a/DesktopEditor/xmlsec/src/src/XmlCertificate.cpp b/DesktopEditor/xmlsec/src/src/XmlCertificate.cpp index 1e04e8448d..040dc17696 100644 --- a/DesktopEditor/xmlsec/src/src/XmlCertificate.cpp +++ b/DesktopEditor/xmlsec/src/src/XmlCertificate.cpp @@ -17,6 +17,8 @@ namespace NSOpenSSL #endif +#include "../../../common/File.h" + #if defined(_LINUX) && !defined(_MAC) #include "./XmlSigner_openssl.h" #define XML_CERTIFICATE_USE_OPENSSL @@ -35,9 +37,78 @@ int ICertificate::GetOOXMLHashAlg(const std::string& sAlg) "http://www.w3.org/2000/09/xmldsig#sha1" == sAlg) return OOXML_HASH_ALG_SHA1; + /* + if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha224" == sAlg || + "http://www.w3.org/2001/04/xmldsig-more#sha224" == sAlg) + return OOXML_HASH_ALG_SHA224; + */ + + if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" == sAlg || + "http://www.w3.org/2001/04/xmldsig-more#sha256" == sAlg || + "http://www.w3.org/2001/04/xmlenc#sha256" == sAlg) + return OOXML_HASH_ALG_SHA256; + + if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384" == sAlg || + "http://www.w3.org/2001/04/xmldsig-more#sha384" == sAlg) + return OOXML_HASH_ALG_SHA384; + + if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512" == sAlg || + "http://www.w3.org/2001/04/xmldsig-more#sha512" == sAlg || + "http://www.w3.org/2001/04/xmlenc#sha512" == sAlg) + return OOXML_HASH_ALG_SHA512; + return OOXML_HASH_ALG_INVALID; } +std::string ICertificate::GetDigestMethodA(const int& nAlg) +{ + switch (nAlg) + { + case OOXML_HASH_ALG_SHA1: + return "http://www.w3.org/2000/09/xmldsig#sha1"; + case OOXML_HASH_ALG_SHA224: + return "http://www.w3.org/2001/04/xmldsig-more#sha224"; + case OOXML_HASH_ALG_SHA256: + return "http://www.w3.org/2001/04/xmlenc#sha256"; + case OOXML_HASH_ALG_SHA384: + return "http://www.w3.org/2001/04/xmldsig-more#sha384"; + case OOXML_HASH_ALG_SHA512: + return "http://www.w3.org/2001/04/xmlenc#sha512"; + default: + break; + } + return "http://www.w3.org/2000/09/xmldsig#sha1"; +} +std::wstring ICertificate::GetDigestMethod(const int& nAlg) +{ + std::string sTmp = GetDigestMethodA(nAlg); + return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(sTmp); +} +std::string ICertificate::GetSignatureMethodA(const int& nAlg) +{ + switch (nAlg) + { + case OOXML_HASH_ALG_SHA1: + return "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; + case OOXML_HASH_ALG_SHA224: + return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha224"; + case OOXML_HASH_ALG_SHA256: + return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; + case OOXML_HASH_ALG_SHA384: + return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384"; + case OOXML_HASH_ALG_SHA512: + return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512"; + default: + break; + } + return "http://www.w3.org/2000/09/xmldsig#rsa-sha1"; +} +std::wstring ICertificate::GetSignatureMethod(const int& nAlg) +{ + std::string sTmp = GetSignatureMethodA(nAlg); + return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(sTmp); +} + ICertificate* ICertificate::CreateInstance() { ICertificate* pCert = new CCertificate(); diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h b/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h index 7615e9ae36..d4b0a033fb 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h @@ -22,6 +22,8 @@ protected: BYTE* m_rawData; int m_rawDataLen; + int m_alg; + public: CCertificate_mscrypto() : ICertificate() { @@ -32,6 +34,7 @@ public: m_rawDataLen = 0; m_release = false; + m_alg = OOXML_HASH_ALG_INVALID; } CCertificate_mscrypto(PCCERT_CONTEXT ctx) : ICertificate() { @@ -42,6 +45,8 @@ public: m_rawDataLen = 0; m_release = false; + + GetHashAlgs(); } virtual ~CCertificate_mscrypto() @@ -101,7 +106,7 @@ public: virtual std::string GetCertificateHash() { - return GetHash(m_context->pbCertEncoded, (unsigned int)m_context->cbCertEncoded, OOXML_HASH_ALG_SHA1); + return GetHash(m_context->pbCertEncoded, (unsigned int)m_context->cbCertEncoded, GetHashAlg()); } virtual std::string GetDate() @@ -136,7 +141,62 @@ public: return OPEN_SSL_WARNING_OK; } + std::vector GetHashAlgs() + { + std::vector algs; + if (!m_context || !m_context->pCertInfo) + return algs; + + std::string sAlg(m_context->pCertInfo->SignatureAlgorithm.pszObjId); + + if ("1.2.840.113549.1.1.5" == sAlg) + algs.push_back(OOXML_HASH_ALG_SHA1); + else if ("1.2.840.113549.1.1.11" == sAlg) + algs.push_back(OOXML_HASH_ALG_SHA256); + else if ("1.2.840.113549.1.1.12" == sAlg) + algs.push_back(OOXML_HASH_ALG_SHA384); + else if ("1.2.840.113549.1.1.13" == sAlg) + algs.push_back(OOXML_HASH_ALG_SHA512); + else + algs.push_back(OOXML_HASH_ALG_SHA1); + + if (algs.empty()) + m_alg = OOXML_HASH_ALG_SHA1; + else + m_alg = algs[0]; + + return algs; + } + int GetHashAlg() + { + if (m_alg == OOXML_HASH_ALG_INVALID) + GetHashAlgs(); + return m_alg; + } + public: + PCRYPT_KEY_PROV_INFO GetProviderInfo() + { + LPBYTE pInfoData = NULL; + DWORD dwInfoDataLength = 0; + + if (!CertGetCertificateContextProperty(m_context, CERT_KEY_PROV_INFO_PROP_ID, NULL, &dwInfoDataLength)) + return NULL; + + if (dwInfoDataLength > 0) + { + pInfoData = (LPBYTE)malloc(dwInfoDataLength * sizeof(BYTE)); + + if (!CertGetCertificateContextProperty(m_context, CERT_KEY_PROV_INFO_PROP_ID, pInfoData, &dwInfoDataLength)) + { + free(pInfoData); + return NULL; + } + } + + return (PCRYPT_KEY_PROV_INFO)pInfoData; + } + virtual std::string Sign(const std::string& sXml) { BOOL bResult = TRUE; @@ -144,16 +204,46 @@ public: HCRYPTHASH hHash = NULL; HCRYPTPROV hCryptProv = NULL; - bResult = CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL); + bResult = CryptAcquireCertificatePrivateKey(m_context, CRYPT_ACQUIRE_COMPARE_KEY_FLAG, NULL, &hCryptProv, &dwKeySpec, NULL); if (!bResult) return ""; - bResult = CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash); + int nAlg = GetHashAlg(); + bResult = CryptCreateHash(hCryptProv, GetHashId(nAlg), 0, 0, &hHash); if (!bResult) { + PCRYPT_KEY_PROV_INFO info = GetProviderInfo(); + CryptReleaseContext(hCryptProv, 0); - return ""; + if (!CryptAcquireContextW(&hCryptProv, info->pwszContainerName, info->pwszProvName, info->dwProvType, 0)) + { + CryptReleaseContext(hCryptProv, 0); + free(info); + return ""; + } + + bResult = CryptCreateHash(hCryptProv, GetHashId(nAlg), 0, 0, &hHash); + if (!bResult) + { + CryptReleaseContext(hCryptProv, 0); + if (!CryptAcquireContextW(&hCryptProv, info->pwszContainerName, NULL, PROV_RSA_AES, 0)) + { + CryptReleaseContext(hCryptProv, 0); + free(info); + return ""; + } + + bResult = CryptCreateHash(hCryptProv, GetHashId(nAlg), 0, 0, &hHash); + } + + free(info); + + if (!bResult) + { + CryptReleaseContext(hCryptProv, 0); + return ""; + } } bResult = CryptHashData(hHash, (BYTE*)sXml.c_str(), (DWORD)sXml.length(), 0); @@ -223,10 +313,23 @@ public: HCRYPTPROV hCryptProv = NULL; - bResult = (NULL != m_context) ? CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL) : FALSE; + bResult = FALSE;//(NULL != m_context) ? CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL) : FALSE; + + DWORD dwProvType = PROV_RSA_FULL; + switch (nAlg) + { + case OOXML_HASH_ALG_SHA256: + case OOXML_HASH_ALG_SHA512: + { + dwProvType = PROV_RSA_AES; + break; + } + default: + break; + } if (!bResult) - bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, (nAlg == OOXML_HASH_ALG_SHA256) ? PROV_RSA_AES : PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT); if (!bResult) return ""; @@ -308,10 +411,23 @@ public: HCRYPTKEY hPubKey = NULL; HCRYPTPROV hCryptProv = NULL; - BOOL bResult = CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL); + BOOL bResult = FALSE;//CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL); + + DWORD dwProvType = PROV_RSA_FULL; + switch (nAlg) + { + case OOXML_HASH_ALG_SHA256: + case OOXML_HASH_ALG_SHA512: + { + dwProvType = PROV_RSA_AES; + break; + } + default: + break; + } if (!bResult) - bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT); if (!bResult) return false; @@ -365,7 +481,7 @@ public: m_rawDataLen = 0; return false; } - + GetHashAlgs(); return true; } @@ -388,6 +504,7 @@ public: m_store = NULL; return 0; } + GetHashAlgs(); return 1; } @@ -406,6 +523,12 @@ private: return CALG_SHA1; case OOXML_HASH_ALG_SHA256: return CALG_SHA_256; + case OOXML_HASH_ALG_SHA384: + return CALG_SHA_384; + case OOXML_HASH_ALG_SHA512: + return CALG_SHA_512; + case OOXML_HASH_ALG_SHA224: + return CALG_SHA1; default: return CALG_SHA1; } diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp index 3ad48fb83b..148838686b 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp @@ -17,6 +17,36 @@ #include #include +const EVP_MD* Get_EVP_MD(int nAlg) +{ + switch (nAlg) + { + case OOXML_HASH_ALG_SHA1: + { + return EVP_sha1(); + } + case OOXML_HASH_ALG_SHA224: + { + return EVP_sha224(); + } + case OOXML_HASH_ALG_SHA256: + { + return EVP_sha256(); + } + case OOXML_HASH_ALG_SHA384: + { + return EVP_sha384(); + } + case OOXML_HASH_ALG_SHA512: + { + return EVP_sha512(); + } + default: + break; + } + return EVP_sha1(); +} + void BIO_FREE(BIO*& bio) { if (bio) @@ -50,6 +80,8 @@ class CCertificate_openssl_private std::string m_separator; std::string m_id; + int m_alg; + public: ICertificate* m_pBase; @@ -169,7 +201,7 @@ public: int nLen = 0; if (NSFile::CBase64Converter::Decode(sBase64.c_str(), (int)sBase64.length(), pData, nLen)) { - std::string sHash = GetHash(pData, (unsigned int)nLen, OOXML_HASH_ALG_SHA1); + std::string sHash = GetHash(pData, (unsigned int)nLen, GetHashAlg()); RELEASEARRAYOBJECTS(pData); return sHash; } @@ -211,11 +243,11 @@ public: return GetNumber(); } -public: +public: std::string Sign(const std::string& sXml) { EVP_MD_CTX* pCtx = EVP_MD_CTX_create(); - const EVP_MD* pDigest = EVP_sha1(); + const EVP_MD* pDigest = Get_EVP_MD(this->GetHashAlg()); int n1 = EVP_SignInit(pCtx, pDigest); n1 = n1; @@ -243,35 +275,63 @@ public: std::string GetHash(unsigned char* pData, unsigned int nSize, int nAlg) { - if (nAlg == OOXML_HASH_ALG_SHA1) + int nBufLen = 0; + unsigned char* pBufData = NULL; + + switch (nAlg) { - unsigned char obuf[20]; - SHA1(pData, (size_t)nSize, obuf); - - char* pBase64_hash = NULL; - int nBase64Len_hash = 0; - NSFile::CBase64Converter::Encode(obuf, 20, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF); - - std::string sReturn(pBase64_hash, nBase64Len_hash); - delete [] pBase64_hash; - - return sReturn; + case OOXML_HASH_ALG_SHA1: + { + nBufLen = 20; + pBufData = new unsigned char[nBufLen]; + SHA1(pData, (size_t)nSize, pBufData); + break; + } + case OOXML_HASH_ALG_SHA224: + { + nBufLen = 28; + pBufData = new unsigned char[nBufLen]; + SHA224(pData, (size_t)nSize, pBufData); + break; + } + case OOXML_HASH_ALG_SHA256: + { + nBufLen = 32; + pBufData = new unsigned char[nBufLen]; + SHA256(pData, (size_t)nSize, pBufData); + break; + } + case OOXML_HASH_ALG_SHA384: + { + nBufLen = 48; + pBufData = new unsigned char[nBufLen]; + SHA384(pData, (size_t)nSize, pBufData); + break; + } + case OOXML_HASH_ALG_SHA512: + { + nBufLen = 64; + pBufData = new unsigned char[nBufLen]; + SHA512(pData, (size_t)nSize, pBufData); + break; + } + default: + break; } - else if (nAlg == OOXML_HASH_ALG_SHA256) - { - unsigned char obuf[32]; - SHA256(pData, (size_t)nSize, obuf); - char* pBase64_hash = NULL; - int nBase64Len_hash = 0; - NSFile::CBase64Converter::Encode(obuf, 32, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF); + if (0 == nBufLen) + return ""; - std::string sReturn(pBase64_hash, nBase64Len_hash); - delete [] pBase64_hash; + char* pBase64_hash = NULL; + int nBase64Len_hash = 0; + NSFile::CBase64Converter::Encode(pBufData, nBufLen, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF); - return sReturn; - } - return ""; + std::string sReturn(pBase64_hash, nBase64Len_hash); + delete [] pBase64_hash; + + delete [] pBufData; + + return sReturn; } std::string GetHash(const std::string& sXml, int nAlg) @@ -297,7 +357,7 @@ public: bool Verify(const std::string& sXml, std::string& sXmlSignature, int nAlg) { EVP_MD_CTX* pCtx = EVP_MD_CTX_create(); - const EVP_MD* pDigest = EVP_sha1(); + const EVP_MD* pDigest = Get_EVP_MD(this->GetHashAlg()); int n1 = EVP_VerifyInit(pCtx, pDigest); n1 = n1; @@ -348,6 +408,29 @@ public: return false; } + std::vector GetHashAlgs() + { + std::vector algs; + if (!m_cert) + return algs; + + // TODO: + // Check algs in cert + + if (algs.empty()) + m_alg = OOXML_HASH_ALG_SHA1; + else + m_alg = algs[0]; + + return algs; + } + int GetHashAlg() + { + if (m_alg == OOXML_HASH_ALG_INVALID) + GetHashAlgs(); + return m_alg; + } + public: int ShowSelectDialog() { @@ -390,6 +473,7 @@ public: m_id += m_separator; m_id += sCertPassword; + GetHashAlgs(); return true; } @@ -798,6 +882,15 @@ bool CCertificate_openssl::FromId(const std::string& id) return m_internal->FromKey(id); } +std::vector CCertificate_openssl::GetHashAlgs() +{ + return m_internal->GetHashAlgs(); +} +int CCertificate_openssl::GetHashAlg() +{ + return m_internal->GetHashAlg(); +} + namespace NSOpenSSL { int LoadKey(std::wstring file, std::string password) diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h index 34cc2640e2..8652e4f233 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.h @@ -41,6 +41,9 @@ public: virtual bool LoadFromBase64Data(const std::string& data); + virtual std::vector GetHashAlgs(); + virtual int GetHashAlg(); + public: virtual int ShowSelectDialog(void* parent = NULL); virtual int ShowCertificate(void* parent = NULL); From 76b682063e57c67ac2d62686d138d4f5cb8a0343 Mon Sep 17 00:00:00 2001 From: ElenaSubbotina Date: Fri, 27 Apr 2018 19:11:58 +0300 Subject: [PATCH 7/9] x2t - save comments for presentation (undelete ms office) --- ASCOfficePPTXFile/Editor/BinaryFileReaderWriter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ASCOfficePPTXFile/Editor/BinaryFileReaderWriter.cpp b/ASCOfficePPTXFile/Editor/BinaryFileReaderWriter.cpp index 1adfbcb494..6efe98b1f2 100644 --- a/ASCOfficePPTXFile/Editor/BinaryFileReaderWriter.cpp +++ b/ASCOfficePPTXFile/Editor/BinaryFileReaderWriter.cpp @@ -1289,7 +1289,7 @@ namespace NSBinPptxRW void CRelsGenerator::WritePresentationComments(int nComment) { std::wstring strRels = L""; m_pWriter->WriteString(strRels); From 27a0e77ef13c425626d3bcab28fb61990858da59 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Sat, 28 Apr 2018 15:08:38 +0300 Subject: [PATCH 8/9] [bugs] Crypto bugs --- .../xmlsec/src/src/XmlSigner_mscrypto.h | 87 +++++++++++++++++++ .../xmlsec/src/src/XmlSigner_openssl.cpp | 22 +++++ 2 files changed, 109 insertions(+) diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h b/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h index d4b0a033fb..da17e69817 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_mscrypto.h @@ -11,6 +11,80 @@ #include "../../../common/File.h" #include "../../../common/BigInteger.h" +#ifdef MS_CRYPTO_PRIVATE +namespace NSMSCryptoPrivate +{ + typedef BOOL (WINAPI * PFNCCERTDISPLAYPROC) ( + _In_ PCCERT_CONTEXT pCertContext, + _In_ HWND hWndSelCertDlg, + _In_ void *pvCallbackData + ); + + typedef struct _CRYPTUI_SELECTCERTIFICATE_STRUCT { + DWORD dwSize; + HWND hwndParent; + DWORD dwFlags; + LPCTSTR szTitle; + DWORD dwDontUseColumn; + LPCTSTR szDisplayString; + PFNCFILTERPROC pFilterCallback; + PFNCCERTDISPLAYPROC pDisplayCallback; + void *pvCallbackData; + DWORD cDisplayStores; + HCERTSTORE *rghDisplayStores; + DWORD cStores; + HCERTSTORE *rghStores; + DWORD cPropSheetPages; + LPCPROPSHEETPAGE rgPropSheetPages; + HCERTSTORE hSelectedCertStore; + } CRYPTUI_SELECTCERTIFICATE_STRUCT, *PCRYPTUI_SELECTCERTIFICATE_STRUCT; + + typedef PCCERT_CONTEXT (WINAPI * _CryptUIDlgSelectCertificate) ( + _In_ PCRYPTUI_SELECTCERTIFICATE_STRUCT pcsc + ); + + class CCertificate_mscrypto_methods + { + public: + HINSTANCE m_instance; + _CryptUIDlgSelectCertificate CryptUIDlgSelectCertificate_internal; + + CCertificate_mscrypto_methods() + { + CryptUIDlgSelectCertificate_internal = NULL; + m_instance = LoadLibraryA("Cryptui.dll"); + + if (m_instance) + { + CryptUIDlgSelectCertificate_internal = (_CryptUIDlgSelectCertificate)GetProcAddress(m_instance, "CryptUIDlgSelectCertificate"); + } + } + ~CCertificate_mscrypto_methods() + { + if (m_instance) + FreeLibrary(m_instance); + } + + PCCERT_CONTEXT ShowSelectCertificate(HWND* parent) + { + if (!CryptUIDlgSelectCertificate_internal) + return NULL; + + CRYPTUI_SELECTCERTIFICATE_STRUCT info; + memset(&info, 0, sizeof(info)); + info.hSelectedCertStore = CertOpenSystemStoreA(NULL, "MY"); + + PCCERT_CONTEXT ctx = CryptUIDlgSelectCertificate_internal(&info); + + if (info.hSelectedCertStore != NULL) + CertCloseStore(info.hSelectedCertStore, 0); + + return ctx; + } + }; +} +#endif + class CCertificate_mscrypto : public ICertificate { public: @@ -165,6 +239,7 @@ public: else m_alg = algs[0]; + m_alg = OOXML_HASH_ALG_SHA1; return algs; } int GetHashAlg() @@ -493,6 +568,17 @@ public: public: virtual int ShowSelectDialog(void* parent = NULL) { +#ifdef MS_CRYPTO_PRIVATE + NSMSCryptoPrivate::CCertificate_mscrypto_methods methods; + m_context = methods.ShowSelectCertificate((HWND*)parent); + + if (m_context) + { + GetHashAlgs(); + return 1; + } +#endif + m_store = CertOpenSystemStoreA(NULL, "MY"); if (!m_store) return 0; @@ -504,6 +590,7 @@ public: m_store = NULL; return 0; } + GetHashAlgs(); return 1; } diff --git a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp index 148838686b..926897aa3c 100644 --- a/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp +++ b/DesktopEditor/xmlsec/src/src/XmlSigner_openssl.cpp @@ -171,6 +171,7 @@ public: if (NULL == m_cert) return ""; +#if 0 BIO* bio = BIO_new(BIO_s_mem()); PEM_write_bio_X509_AUX(bio, m_cert); @@ -191,6 +192,27 @@ public: string_replace(sReturn, "\n", ""); BIO_free(bio); + return sReturn; +#endif + + BIO* bio = BIO_new(BIO_s_mem()); + + i2d_X509_bio(bio, m_cert); + BIO_flush(bio); + + BYTE* data = NULL; + int size = (int)BIO_get_mem_data(bio, &data); + + char* dataDst = NULL; + int lenDst = 0; + NSFile::CBase64Converter::Encode(data, size, dataDst, lenDst); + + std::string sReturn(dataDst); + + RELEASEARRAYOBJECTS(dataDst); + + BIO_free(bio); + return sReturn; } From 6afa54ebef987ddeb2a0c465b491c8a64bca17f5 Mon Sep 17 00:00:00 2001 From: ElenaSubbotina Date: Sat, 28 Apr 2018 17:04:49 +0300 Subject: [PATCH 9/9] x2t - fix users files --- ASCOfficeDocFile/Common/FormatUtils.h | 3 +++ ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h | 8 ++++++-- ASCOfficeRtfFile/RtfFormatLib/source/Ole1FormatReader.h | 3 ++- .../source/XlsFormat/Logic/Biff_unions/SUPBOOK_bu.cpp | 4 +++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ASCOfficeDocFile/Common/FormatUtils.h b/ASCOfficeDocFile/Common/FormatUtils.h index 1da9b0bf8b..3e6a08090a 100644 --- a/ASCOfficeDocFile/Common/FormatUtils.h +++ b/ASCOfficeDocFile/Common/FormatUtils.h @@ -58,6 +58,9 @@ #include +#define GETBITS(from, numL, numH) ((from & (((1 << (numH - numL + 1)) - 1) << numL)) >> numL) +#define GETBIT(from, num) ((from & (1 << num)) != 0) + namespace DocFileFormat { typedef unsigned char Bool8; diff --git a/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h b/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h index 3894922c10..0b67181bae 100644 --- a/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h +++ b/ASCOfficeDocFile/DocDocxConverter/CommentsMapping.h @@ -72,10 +72,14 @@ namespace DocFileFormat { AnnotationReferenceDescriptor* atrdPre10 = static_cast(m_document->AnnotationsReferencePlex->Elements[index]); + unsigned short index_author = atrdPre10->GetAuthorIndex(); m_pXmlWriter->WriteNodeBegin( L"w:comment", TRUE ); m_pXmlWriter->WriteAttribute( L"w:id", FormatUtils::IntToWideString( index + 1 )); - m_pXmlWriter->WriteAttribute( L"w:author", - FormatUtils::XmlEncode(m_document->AnnotationOwners->at( atrdPre10->GetAuthorIndex() ) )); + if (index_author < m_document->AnnotationOwners->size()) //conv_253l2H1CehgKwsxCtNk__docx.doc + { + m_pXmlWriter->WriteAttribute( L"w:author", + FormatUtils::XmlEncode(m_document->AnnotationOwners->at( index_author ) )); + } m_pXmlWriter->WriteAttribute( L"w:initials", atrdPre10->GetUserInitials()); //!!!TODO!!! diff --git a/ASCOfficeRtfFile/RtfFormatLib/source/Ole1FormatReader.h b/ASCOfficeRtfFile/RtfFormatLib/source/Ole1FormatReader.h index 9ce677c24c..3be40bdd97 100644 --- a/ASCOfficeRtfFile/RtfFormatLib/source/Ole1FormatReader.h +++ b/ASCOfficeRtfFile/RtfFormatLib/source/Ole1FormatReader.h @@ -405,7 +405,8 @@ public: if (Header.FormatID == 2) { - if (Header.OLEVersion & 0x00000500) + if (Header.OLEVersion & 0x00000500 || + Header.OLEVersion & 0x00010001) { stream >> Header.ClassName; } diff --git a/ASCOfficeXlsFile2/source/XlsFormat/Logic/Biff_unions/SUPBOOK_bu.cpp b/ASCOfficeXlsFile2/source/XlsFormat/Logic/Biff_unions/SUPBOOK_bu.cpp index ab1d935c5e..4d964a90ce 100644 --- a/ASCOfficeXlsFile2/source/XlsFormat/Logic/Biff_unions/SUPBOOK_bu.cpp +++ b/ASCOfficeXlsFile2/source/XlsFormat/Logic/Biff_unions/SUPBOOK_bu.cpp @@ -39,6 +39,8 @@ #include "../Biff_records/XCT.h" #include "../Biff_records/CRN.h" +#include "../../../../../Common/DocxFormat/Source/XML/Utils.h" + namespace XLS { @@ -310,7 +312,7 @@ int SUPBOOK::serialize_book(std::wostream & strm) } CP_XML_NODE(L"v") { - CP_XML_STREAM() << strVal; + CP_XML_STREAM() << XmlUtils::EncodeXmlString(strVal); } } }