Reduce indentation by returning early on error condition

Change-Id: Iebf6eca68f0dcba87ab517952009ee6dfb4b588c
Reviewed-on: https://gerrit.libreoffice.org/54475
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
This commit is contained in:
Samuel Mehrbrodt
2018-05-17 11:34:37 +02:00
parent b9ebabd675
commit c4409edb4e

View File

@@ -3667,71 +3667,103 @@ bool SfxMedium::SignContents_Impl(const Reference<XCertificate> xCert, const OUS
{ {
bool bChanges = false; bool bChanges = false;
// the medium should be closed to be able to sign, the caller is responsible to close it if (IsOpen() || GetError())
if ( !IsOpen() && !GetError() )
{ {
// The component should know if there was a valid document signature, since SAL_WARN("sfx.doc", "The medium must be closed by the signer!");
// it should show a warning in this case return bChanges;
uno::Reference< security::XDocumentDigitalSignatures > xSigner( }
security::DocumentDigitalSignatures::createWithVersionAndValidSignature(
comphelper::getProcessComponentContext(), aODFVersion, bHasValidDocumentSignature ) );
uno::Reference< embed::XStorage > xWriteableZipStor; // The component should know if there was a valid document signature, since
// Signing is not modification of the document, as seen by the user // it should show a warning in this case
// ("only a saved document can be signed"). So allow signing in the uno::Reference< security::XDocumentDigitalSignatures > xSigner(
// "opened read-only, but not physically-read-only" case. security::DocumentDigitalSignatures::createWithVersionAndValidSignature(
if (!IsOriginallyReadOnly()) comphelper::getProcessComponentContext(), aODFVersion, bHasValidDocumentSignature ) );
uno::Reference< embed::XStorage > xWriteableZipStor;
// Signing is not modification of the document, as seen by the user
// ("only a saved document can be signed"). So allow signing in the
// "opened read-only, but not physically-read-only" case.
if (!IsOriginallyReadOnly())
{
// we can reuse the temporary file if there is one already
CreateTempFile( false );
GetMedium_Impl();
try
{ {
// we can reuse the temporary file if there is one already if ( !pImpl->xStream.is() )
CreateTempFile( false ); throw uno::RuntimeException();
GetMedium_Impl();
bool bODF = GetFilter()->IsOwnFormat();
try try
{ {
if ( !pImpl->xStream.is() ) xWriteableZipStor = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream( ZIP_STORAGE_FORMAT_STRING, pImpl->xStream );
}
catch (const io::IOException& rException)
{
if (bODF)
SAL_WARN("sfx.doc", "ODF stream is not a zip storage: " << rException);
}
if ( !xWriteableZipStor.is() && bODF )
throw uno::RuntimeException();
uno::Reference< embed::XStorage > xMetaInf;
uno::Reference<container::XNameAccess> xNameAccess(xWriteableZipStor, uno::UNO_QUERY);
if (xNameAccess.is() && xNameAccess->hasByName("META-INF"))
{
xMetaInf = xWriteableZipStor->openStorageElement(
"META-INF",
embed::ElementModes::READWRITE );
if ( !xMetaInf.is() )
throw uno::RuntimeException(); throw uno::RuntimeException();
}
bool bODF = GetFilter()->IsOwnFormat(); if ( bScriptingContent )
try {
// If the signature has already the document signature it will be removed
// after the scripting signature is inserted.
uno::Reference< io::XStream > xStream(
xMetaInf->openStreamElement( xSigner->getScriptingContentSignatureDefaultStreamName(),
embed::ElementModes::READWRITE ),
uno::UNO_SET_THROW );
if ( xSigner->signScriptingContent( GetZipStorageToSign_Impl(), xStream ) )
{ {
xWriteableZipStor = ::comphelper::OStorageHelper::GetStorageOfFormatFromStream( ZIP_STORAGE_FORMAT_STRING, pImpl->xStream ); // remove the document signature if any
OUString aDocSigName = xSigner->getDocumentContentSignatureDefaultStreamName();
if ( !aDocSigName.isEmpty() && xMetaInf->hasByName( aDocSigName ) )
xMetaInf->removeElement( aDocSigName );
uno::Reference< embed::XTransactedObject > xTransact( xMetaInf, uno::UNO_QUERY_THROW );
xTransact->commit();
xTransact.set( xWriteableZipStor, uno::UNO_QUERY_THROW );
xTransact->commit();
// the temporary file has been written, commit it to the original file
Commit();
bChanges = true;
} }
catch (const io::IOException& rException) }
else
{
if (xMetaInf.is())
{ {
if (bODF) // ODF.
SAL_WARN("sfx.doc", "ODF stream is not a zip storage: " << rException); uno::Reference< io::XStream > xStream;
} if (GetFilter() && GetFilter()->IsOwnFormat())
xStream.set(xMetaInf->openStreamElement(xSigner->getDocumentContentSignatureDefaultStreamName(), embed::ElementModes::READWRITE), uno::UNO_SET_THROW);
if ( !xWriteableZipStor.is() && bODF ) bool bSuccess = false;
throw uno::RuntimeException(); if (xCert.is())
bSuccess = xSigner->signDocumentContentWithCertificate(
GetZipStorageToSign_Impl(), xStream, xCert, aSignatureLineId);
else
bSuccess = xSigner->signDocumentContent(GetZipStorageToSign_Impl(),
xStream);
uno::Reference< embed::XStorage > xMetaInf; if (bSuccess)
uno::Reference<container::XNameAccess> xNameAccess(xWriteableZipStor, uno::UNO_QUERY);
if (xNameAccess.is() && xNameAccess->hasByName("META-INF"))
{
xMetaInf = xWriteableZipStor->openStorageElement(
"META-INF",
embed::ElementModes::READWRITE );
if ( !xMetaInf.is() )
throw uno::RuntimeException();
}
if ( bScriptingContent )
{
// If the signature has already the document signature it will be removed
// after the scripting signature is inserted.
uno::Reference< io::XStream > xStream(
xMetaInf->openStreamElement( xSigner->getScriptingContentSignatureDefaultStreamName(),
embed::ElementModes::READWRITE ),
uno::UNO_SET_THROW );
if ( xSigner->signScriptingContent( GetZipStorageToSign_Impl(), xStream ) )
{ {
// remove the document signature if any
OUString aDocSigName = xSigner->getDocumentContentSignatureDefaultStreamName();
if ( !aDocSigName.isEmpty() && xMetaInf->hasByName( aDocSigName ) )
xMetaInf->removeElement( aDocSigName );
uno::Reference< embed::XTransactedObject > xTransact( xMetaInf, uno::UNO_QUERY_THROW ); uno::Reference< embed::XTransactedObject > xTransact( xMetaInf, uno::UNO_QUERY_THROW );
xTransact->commit(); xTransact->commit();
xTransact.set( xWriteableZipStor, uno::UNO_QUERY_THROW ); xTransact.set( xWriteableZipStor, uno::UNO_QUERY_THROW );
@@ -3742,107 +3774,77 @@ bool SfxMedium::SignContents_Impl(const Reference<XCertificate> xCert, const OUS
bChanges = true; bChanges = true;
} }
} }
else else if (xWriteableZipStor.is())
{ {
if (xMetaInf.is()) // OOXML.
uno::Reference<io::XStream> xStream;
bool bSuccess = false;
if (xCert.is())
{ {
// ODF. bSuccess = xSigner->signDocumentContentWithCertificate(
uno::Reference< io::XStream > xStream; GetZipStorageToSign_Impl(/*bReadOnly=*/false), xStream, xCert, aSignatureLineId);
if (GetFilter() && GetFilter()->IsOwnFormat())
xStream.set(xMetaInf->openStreamElement(xSigner->getDocumentContentSignatureDefaultStreamName(), embed::ElementModes::READWRITE), uno::UNO_SET_THROW);
bool bSuccess = false;
if (xCert.is())
bSuccess = xSigner->signDocumentContentWithCertificate(
GetZipStorageToSign_Impl(), xStream, xCert, aSignatureLineId);
else
bSuccess = xSigner->signDocumentContent(GetZipStorageToSign_Impl(),
xStream);
if (bSuccess)
{
uno::Reference< embed::XTransactedObject > xTransact( xMetaInf, uno::UNO_QUERY_THROW );
xTransact->commit();
xTransact.set( xWriteableZipStor, uno::UNO_QUERY_THROW );
xTransact->commit();
// the temporary file has been written, commit it to the original file
Commit();
bChanges = true;
}
}
else if (xWriteableZipStor.is())
{
// OOXML.
uno::Reference<io::XStream> xStream;
bool bSuccess = false;
if (xCert.is())
{
bSuccess = xSigner->signDocumentContentWithCertificate(
GetZipStorageToSign_Impl(/*bReadOnly=*/false), xStream, xCert, aSignatureLineId);
}
else
{
// We need read-write to be able to add the signature relation.
bSuccess =xSigner->signDocumentContent(
GetZipStorageToSign_Impl(/*bReadOnly=*/false), xStream);
}
if (bSuccess)
{
uno::Reference<embed::XTransactedObject> xTransact(xWriteableZipStor, uno::UNO_QUERY_THROW);
xTransact->commit();
// the temporary file has been written, commit it to the original file
Commit();
bChanges = true;
}
} }
else else
{ {
// Something not ZIP based: e.g. PDF. // We need read-write to be able to add the signature relation.
std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ | StreamMode::WRITE)); bSuccess =xSigner->signDocumentContent(
uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStream)); GetZipStorageToSign_Impl(/*bReadOnly=*/false), xStream);
if (xSigner->signDocumentContent(uno::Reference<embed::XStorage>(), xStream)) }
bChanges = true;
if (bSuccess)
{
uno::Reference<embed::XTransactedObject> xTransact(xWriteableZipStor, uno::UNO_QUERY_THROW);
xTransact->commit();
// the temporary file has been written, commit it to the original file
Commit();
bChanges = true;
} }
} }
else
{
// Something not ZIP based: e.g. PDF.
std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ | StreamMode::WRITE));
uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pStream));
if (xSigner->signDocumentContent(uno::Reference<embed::XStorage>(), xStream))
bChanges = true;
}
} }
catch ( const uno::Exception& )
{
SAL_WARN( "sfx.doc", "Couldn't use signing functionality!" );
}
CloseAndRelease();
} }
else catch ( const uno::Exception& )
{ {
try SAL_WARN( "sfx.doc", "Couldn't use signing functionality!" );
{
if ( bScriptingContent )
xSigner->showScriptingContentSignatures( GetZipStorageToSign_Impl(), uno::Reference< io::XInputStream >() );
else
{
uno::Reference<embed::XStorage> xStorage = GetZipStorageToSign_Impl();
if (xStorage.is())
xSigner->showDocumentContentSignatures(xStorage, uno::Reference<io::XInputStream>());
else
{
std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ));
uno::Reference<io::XInputStream> xStream(new utl::OStreamWrapper(*pStream));
xSigner->showDocumentContentSignatures(uno::Reference<embed::XStorage>(), xStream);
}
}
}
catch( const uno::Exception& )
{
SAL_WARN( "sfx.doc", "Couldn't use signing functionality!" );
}
} }
ResetError(); CloseAndRelease();
} }
else
{
try
{
if ( bScriptingContent )
xSigner->showScriptingContentSignatures( GetZipStorageToSign_Impl(), uno::Reference< io::XInputStream >() );
else
{
uno::Reference<embed::XStorage> xStorage = GetZipStorageToSign_Impl();
if (xStorage.is())
xSigner->showDocumentContentSignatures(xStorage, uno::Reference<io::XInputStream>());
else
{
std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(GetName(), StreamMode::READ));
uno::Reference<io::XInputStream> xStream(new utl::OStreamWrapper(*pStream));
xSigner->showDocumentContentSignatures(uno::Reference<embed::XStorage>(), xStream);
}
}
}
catch( const uno::Exception& )
{
SAL_WARN( "sfx.doc", "Couldn't use signing functionality!" );
}
}
ResetError();
return bChanges; return bChanges;
} }