[Libreoffice-commits] core.git: 3 commits - xmlsecurity/inc xmlsecurity/qa xmlsecurity/source
Miklos Vajna
vmiklos at collabora.co.uk
Fri Mar 4 15:02:32 UTC 2016
xmlsecurity/inc/documentsignaturemanager.hxx | 2
xmlsecurity/qa/unit/signing/data/multi.docx |binary
xmlsecurity/qa/unit/signing/signing.cxx | 28 ++++++++++
xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx | 18 ------
xmlsecurity/source/helper/documentsignaturehelper.cxx | 4 +
xmlsecurity/source/helper/documentsignaturemanager.cxx | 44 ++++++++++++++++-
6 files changed, 78 insertions(+), 18 deletions(-)
New commits:
commit 3cefd33eb54d355d21f3541963ad1e89793c95f1
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date: Fri Mar 4 15:41:35 2016 +0100
CppunitTest_xmlsecurity_signing: add signature removal testcase
Fails with the previous commit reverted.
Change-Id: I050a03fa923980e46b31eff457d0b83f6c38ceaf
diff --git a/xmlsecurity/qa/unit/signing/data/multi.docx b/xmlsecurity/qa/unit/signing/data/multi.docx
new file mode 100644
index 0000000..aba6950
Binary files /dev/null and b/xmlsecurity/qa/unit/signing/data/multi.docx differ
diff --git a/xmlsecurity/qa/unit/signing/signing.cxx b/xmlsecurity/qa/unit/signing/signing.cxx
index 5d97d71..aa062d0 100644
--- a/xmlsecurity/qa/unit/signing/signing.cxx
+++ b/xmlsecurity/qa/unit/signing/signing.cxx
@@ -66,6 +66,8 @@ public:
void testOOXMLDescription();
/// Test appending a new signature next to an existing one.
void testOOXMLAppend();
+ /// Test removing a signature from existing ones.
+ void testOOXMLRemove();
CPPUNIT_TEST_SUITE(SigningTest);
CPPUNIT_TEST(testDescription);
@@ -73,6 +75,7 @@ public:
CPPUNIT_TEST(testOOXMLBroken);
CPPUNIT_TEST(testOOXMLDescription);
CPPUNIT_TEST(testOOXMLAppend);
+ CPPUNIT_TEST(testOOXMLRemove);
CPPUNIT_TEST_SUITE_END();
private:
@@ -220,6 +223,31 @@ void SigningTest::testOOXMLAppend()
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), rInformations.size());
}
+void SigningTest::testOOXMLRemove()
+{
+ // Load the test document as a storage and read its signatures: purpose1 and purpose2.
+ DocumentSignatureManager aManager(mxComponentContext, SignatureModeDocumentContent);
+ CPPUNIT_ASSERT(aManager.maSignatureHelper.Init());
+ OUString aURL = getURLFromSrc(DATA_DIRECTORY) + "multi.docx";
+ uno::Reference <embed::XStorage> xStorage = comphelper::OStorageHelper::GetStorageOfFormatFromURL(ZIP_STORAGE_FORMAT_STRING, aURL, embed::ElementModes::READWRITE);
+ CPPUNIT_ASSERT(xStorage.is());
+ aManager.mxStore = xStorage;
+ aManager.maSignatureHelper.SetStorage(xStorage, "1.2");
+ aManager.read(/*bUseTempStream=*/false);
+ std::vector<SignatureInformation>& rInformations = aManager.maCurrentSignatureInformations;
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), rInformations.size());
+
+ // Then remove the last added signature.
+ uno::Reference<security::XCertificate> xCertificate = getCertificate(aManager.maSignatureHelper);
+ CPPUNIT_ASSERT(xCertificate.is());
+ aManager.remove(0);
+
+ // Read back the signatures and make sure that only purpose1 is left.
+ aManager.read(/*bUseTempStream=*/true);
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rInformations.size());
+ CPPUNIT_ASSERT_EQUAL(OUString("purpose1"), rInformations[0].ouDescription);
+}
+
void SigningTest::testOOXMLPartial()
{
createDoc(getURLFromSrc(DATA_DIRECTORY) + "partial.docx");
commit c30bf0284c643b6c574fcc5dabb840bb51174b30
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date: Fri Mar 4 12:34:39 2016 +0100
xmlsecurity OOXML export: handle keeping all signatures but one
The removal of the _xmlsignatures sub-storage in
DocumentSignatureHelper::OpenSignatureStream() serves 3 purposes:
1) Remove no longer needed signature streams
2) Truncate signatures relation stream
3) Truncate still needed signature streams
2) and 3) could be done using io::XTruncate as well, but if the whole
storage is removed to handle 1), then individual truncate() calls are
not necessary.
Change-Id: Id9ed9c87c94f340dc947124b28f085561798d361
diff --git a/xmlsecurity/source/helper/documentsignaturehelper.cxx b/xmlsecurity/source/helper/documentsignaturehelper.cxx
index 9c14715..b009736 100644
--- a/xmlsecurity/source/helper/documentsignaturehelper.cxx
+++ b/xmlsecurity/source/helper/documentsignaturehelper.cxx
@@ -390,6 +390,10 @@ SignatureStreamHelper DocumentSignatureHelper::OpenSignatureStream(
{
try
{
+ if (xNameAccess->hasByName("_xmlsignatures") && (nOpenMode & embed::ElementModes::TRUNCATE))
+ // Truncate, then all signatures will be written -> remove previous ones.
+ rxStore->removeElement("_xmlsignatures");
+
aHelper.xSignatureStorage = rxStore->openStorageElement("_xmlsignatures", nSubStorageOpenMode);
aHelper.nStorageFormat = embed::StorageFormats::OFOPXML;
}
diff --git a/xmlsecurity/source/helper/documentsignaturemanager.cxx b/xmlsecurity/source/helper/documentsignaturemanager.cxx
index d5066bd..f22d937 100644
--- a/xmlsecurity/source/helper/documentsignaturemanager.cxx
+++ b/xmlsecurity/source/helper/documentsignaturemanager.cxx
@@ -283,7 +283,7 @@ bool DocumentSignatureManager::add(const uno::Reference<security::XCertificate>&
xOutputStream->closeOutput();
uno::Reference<io::XTempFile> xTempFile(aStreamHelper.xSignatureStream, uno::UNO_QUERY);
- SAL_INFO("xmlsecurity.dialogs", "AddButtonHdl: temporary storage is at " << xTempFile->getUri());
+ SAL_INFO("xmlsecurity.helper", "DocumentSignatureManager::add temporary storage at " << xTempFile->getUri());
}
maSignatureHelper.EndMission();
@@ -296,15 +296,40 @@ void DocumentSignatureManager::remove(sal_uInt16 nPosition)
// Export all other signatures...
SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, /*bTempStream=*/true);
- uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream, uno::UNO_QUERY_THROW);
- uno::Reference<xml::sax::XWriter> xSaxWriter = maSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream);
- uno::Reference< xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter, uno::UNO_QUERY_THROW);
- size_t nInfos = maCurrentSignatureInformations.size();
- for (size_t n = 0 ; n < nInfos ; ++n)
- XMLSignatureHelper::ExportSignature(xDocumentHandler, maCurrentSignatureInformations[n]);
+ if (aStreamHelper.nStorageFormat != embed::StorageFormats::OFOPXML)
+ {
+ uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream, uno::UNO_QUERY_THROW);
+ uno::Reference<xml::sax::XWriter> xSaxWriter = maSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream);
+
+ uno::Reference< xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter, uno::UNO_QUERY_THROW);
+ size_t nInfos = maCurrentSignatureInformations.size();
+ for (size_t n = 0 ; n < nInfos ; ++n)
+ XMLSignatureHelper::ExportSignature(xDocumentHandler, maCurrentSignatureInformations[n]);
+
+ XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler);
+ }
+ else
+ {
+ // OOXML
+
+ // Handle relations.
+ int nSignatureCount = maCurrentSignatureInformations.size();
+ maSignatureHelper.ExportSignatureRelations(aStreamHelper.xSignatureStorage, nSignatureCount);
+
+ // Export old signatures.
+ for (size_t i = 0; i < maCurrentSignatureInformations.size(); ++i)
+ maSignatureHelper.ExportOOXMLSignature(mxStore, aStreamHelper.xSignatureStorage, maCurrentSignatureInformations[i], i + 1);
+
+ // Flush objects.
+ uno::Reference<embed::XTransactedObject> xTransact(aStreamHelper.xSignatureStorage, uno::UNO_QUERY);
+ xTransact->commit();
+ uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream, uno::UNO_QUERY);
+ xOutputStream->closeOutput();
- XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler);
+ uno::Reference<io::XTempFile> xTempFile(aStreamHelper.xSignatureStream, uno::UNO_QUERY);
+ SAL_INFO("xmlsecurity.helper", "DocumentSignatureManager::remove: temporary storage is at " << xTempFile->getUri());
+ }
}
void DocumentSignatureManager::read(bool bUseTempStream, bool bCacheLastSignature)
commit 4351ff1f12268a0b97413e811503ac65a51665eb
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date: Fri Mar 4 12:20:38 2016 +0100
xmlsecurity: extract signature removal logic from DigitalSignaturesDialog
So that it'll be possible to call that code without an active dialog,
from a headless unit test.
Change-Id: I1728a666ff5d84b337efd7e2b7eb68469896257a
diff --git a/xmlsecurity/inc/documentsignaturemanager.hxx b/xmlsecurity/inc/documentsignaturemanager.hxx
index b5a7e1b..918c02e 100644
--- a/xmlsecurity/inc/documentsignaturemanager.hxx
+++ b/xmlsecurity/inc/documentsignaturemanager.hxx
@@ -53,6 +53,8 @@ public:
SignatureStreamHelper ImplOpenSignatureStream(sal_Int32 eStreamMode, bool bTempStream);
/// Add a new signature, using xCert as a signing certificate, and rDescription as description.
bool add(const css::uno::Reference<css::security::XCertificate>& xCert, const OUString& rDescription, sal_Int32& nSecurityId);
+ /// Remove signature at nPosition.
+ void remove(sal_uInt16 nPosition);
/// Read signatures from either a temp stream or the real storage.
void read(bool bUseTempStream, bool bCacheLastSignature = true);
};
diff --git a/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx b/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
index 8e7806e..1dc929d 100644
--- a/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
+++ b/xmlsecurity/source/dialogs/digitalsignaturesdialog.cxx
@@ -436,26 +436,10 @@ IMPL_LINK_NOARG_TYPED(DigitalSignaturesDialog, RemoveButtonHdl, Button*, void)
try
{
sal_uInt16 nSelected = (sal_uInt16) reinterpret_cast<sal_uIntPtr>( m_pSignaturesLB->FirstSelected()->GetUserData() );
- maSignatureManager.maCurrentSignatureInformations.erase( maSignatureManager.maCurrentSignatureInformations.begin()+nSelected );
-
- // Export all other signatures...
- SignatureStreamHelper aStreamHelper = maSignatureManager.ImplOpenSignatureStream(css::embed::ElementModes::WRITE | css::embed::ElementModes::TRUNCATE, true);
- Reference< css::io::XOutputStream > xOutputStream(
- aStreamHelper.xSignatureStream, UNO_QUERY_THROW);
- Reference< css::xml::sax::XWriter> xSaxWriter =
- maSignatureManager.maSignatureHelper.CreateDocumentHandlerWithHeader( xOutputStream );
-
- uno::Reference< xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter, UNO_QUERY_THROW);
- size_t nInfos = maSignatureManager.maCurrentSignatureInformations.size();
- for( size_t n = 0 ; n < nInfos ; ++n )
- XMLSignatureHelper::ExportSignature( xDocumentHandler, maSignatureManager.maCurrentSignatureInformations[ n ] );
-
- XMLSignatureHelper::CloseDocumentHandler( xDocumentHandler);
+ maSignatureManager.remove(nSelected);
mbSignaturesChanged = true;
- aStreamHelper = SignatureStreamHelper(); // release objects...
-
ImplFillSignaturesBox();
}
catch ( uno::Exception& )
diff --git a/xmlsecurity/source/helper/documentsignaturemanager.cxx b/xmlsecurity/source/helper/documentsignaturemanager.cxx
index 5127e3c..d5066bd 100644
--- a/xmlsecurity/source/helper/documentsignaturemanager.cxx
+++ b/xmlsecurity/source/helper/documentsignaturemanager.cxx
@@ -290,6 +290,23 @@ bool DocumentSignatureManager::add(const uno::Reference<security::XCertificate>&
return true;
}
+void DocumentSignatureManager::remove(sal_uInt16 nPosition)
+{
+ maCurrentSignatureInformations.erase(maCurrentSignatureInformations.begin() + nPosition);
+
+ // Export all other signatures...
+ SignatureStreamHelper aStreamHelper = ImplOpenSignatureStream(embed::ElementModes::WRITE | embed::ElementModes::TRUNCATE, /*bTempStream=*/true);
+ uno::Reference<io::XOutputStream> xOutputStream(aStreamHelper.xSignatureStream, uno::UNO_QUERY_THROW);
+ uno::Reference<xml::sax::XWriter> xSaxWriter = maSignatureHelper.CreateDocumentHandlerWithHeader(xOutputStream);
+
+ uno::Reference< xml::sax::XDocumentHandler> xDocumentHandler(xSaxWriter, uno::UNO_QUERY_THROW);
+ size_t nInfos = maCurrentSignatureInformations.size();
+ for (size_t n = 0 ; n < nInfos ; ++n)
+ XMLSignatureHelper::ExportSignature(xDocumentHandler, maCurrentSignatureInformations[n]);
+
+ XMLSignatureHelper::CloseDocumentHandler(xDocumentHandler);
+}
+
void DocumentSignatureManager::read(bool bUseTempStream, bool bCacheLastSignature)
{
maCurrentSignatureInformations.clear();
More information about the Libreoffice-commits
mailing list