From 18606e23c6187f3a17fd9f6596ba41c23e0582a6 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Tue, 2 May 2017 17:00:15 +0300 Subject: [PATCH] Content types correct --- .../xmlsec/test/windows_list_serts/main.cpp | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/DesktopEditor/xmlsec/test/windows_list_serts/main.cpp b/DesktopEditor/xmlsec/test/windows_list_serts/main.cpp index 82136332dd..065166f7d4 100644 --- a/DesktopEditor/xmlsec/test/windows_list_serts/main.cpp +++ b/DesktopEditor/xmlsec/test/windows_list_serts/main.cpp @@ -1022,6 +1022,77 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign return rId; } + + void CorrectContentTypes(int nCountSigsNeeds) + { + std::wstring file = m_sFolder + L"/[Content_Types].xml"; + XmlUtils::CXmlNode oNode; + oNode.FromXmlFile(file); + + XmlUtils::CXmlNodes nodesDefaults; + oNode.GetNodes(L"Default", nodesDefaults); + + XmlUtils::CXmlNodes nodesOverrides; + oNode.GetNodes(L"Override", nodesOverrides); + + std::string sAddition = ""; + + bool bIsSigsExist = false; + int nCount = nodesDefaults.GetCount(); + for (int i = 0; i < nCount; ++i) + { + XmlUtils::CXmlNode node; + nodesDefaults.GetAt(i, node); + + if ("sigs" == node.GetAttributeA("Extension") && + "application/vnd.openxmlformats-package.digital-signature-origin" == node.GetAttributeA("ContentType")) + { + bIsSigsExist = true; + break; + } + } + + if (!bIsSigsExist) + sAddition += ""; + + int nCountSigs = 0; + nCount = nodesOverrides.GetCount(); + for (int i = 0; i < nCount; ++i) + { + XmlUtils::CXmlNode node; + nodesOverrides.GetAt(i, node); + + if ("application/vnd.openxmlformats-package.digital-signature-origin" == node.GetAttributeA("ContentType")) + { + ++nCountSigs; + } + } + + for (int i = nCountSigs; i < nCountSigsNeeds; ++i) + { + sAddition += ""; + } + + std::string sXmlA; + NSFile::CFileBinary::ReadAllTextUtf8A(file, sXmlA); + + std::string::size_type pos = sXmlA.rfind(""); + if (pos == std::string::npos) + return; + + std::string sRet = sXmlA.substr(0, pos); + sRet += sAddition; + sRet += ""; + + NSFile::CFileBinary::Remove(file); + + NSFile::CFileBinary oFile; + oFile.CreateFileW(file); + oFile.WriteFile((BYTE*)sRet.c_str(), (DWORD)sRet.length()); + oFile.CloseFile(); + } }; bool SignDocument(std::wstring sFolderOOXML, PCCERT_CONTEXT pCertContext, std::wstring sign_id) @@ -1212,6 +1283,8 @@ bool SignDocument(std::wstring sFolderOOXML, PCCERT_CONTEXT pCertContext, std::w int nSignNum = oOOXMLSigner.GetCountSigns(sDirectory + L"/_rels/origin.sigs.rels"); + oOOXMLSigner.CorrectContentTypes(nSignNum); + NSFile::CFileBinary::SaveToFile(sDirectory + L"/sig" + std::to_wstring(nSignNum) + L".xml", sXmlData, false); return true; }