From 229340812f6e6cc8c868915055583f60c82a8cf3 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 23 Aug 2018 18:10:01 +0200 Subject: Related rhbz#1618703: Properly handle failure encoding MS file ...when e.g. FIPS mode makes EncryptRC4 (in sw/source/filter/ww8/wrtww8.cxx) fail, but which hadn't been propagated out to SwWW8Writer::WriteStorage (in sw/source/filter/ww8/wrtww8.cxx) Change-Id: I1123136ce1a25e181a0a27486954621a2dd095ea Reviewed-on: https://gerrit.libreoffice.org/59518 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- sw/source/filter/ww8/docxexport.cxx | 4 +++- sw/source/filter/ww8/docxexport.hxx | 2 +- sw/source/filter/ww8/rtfexport.cxx | 4 +++- sw/source/filter/ww8/rtfexport.hxx | 2 +- sw/source/filter/ww8/wrtww8.cxx | 39 +++++++++++++++++++++++++++---------- sw/source/filter/ww8/wrtww8.hxx | 6 +++--- 6 files changed, 40 insertions(+), 17 deletions(-) diff --git a/sw/source/filter/ww8/docxexport.cxx b/sw/source/filter/ww8/docxexport.cxx index b7b12532f87c..f7a0603bf011 100644 --- a/sw/source/filter/ww8/docxexport.cxx +++ b/sw/source/filter/ww8/docxexport.cxx @@ -504,7 +504,7 @@ void DocxExport::OutputDML(uno::Reference const & xShape) aExport.WriteShape(xShape); } -void DocxExport::ExportDocument_Impl() +ErrCode DocxExport::ExportDocument_Impl() { // Set the 'Track Revisions' flag in the settings structure m_aSettings.trackRevisions = bool( RedlineFlags::On & m_nOrigRedlineFlags ); @@ -543,6 +543,8 @@ void DocxExport::ExportDocument_Impl() delete m_pStyles; m_pStyles = nullptr; m_pSections.reset(); + + return ERRCODE_NONE; } void DocxExport::AppendSection( const SwPageDesc *pPageDesc, const SwSectionFormat* pFormat, sal_uLong nLnNum ) diff --git a/sw/source/filter/ww8/docxexport.hxx b/sw/source/filter/ww8/docxexport.hxx index 8361f81e2691..59a8dd9df2ba 100644 --- a/sw/source/filter/ww8/docxexport.hxx +++ b/sw/source/filter/ww8/docxexport.hxx @@ -191,7 +191,7 @@ public: protected: /// Format-dependent part of the actual export. - virtual void ExportDocument_Impl() override; + virtual ErrCode ExportDocument_Impl() override; /// Output SwEndNode virtual void OutputEndNode( const SwEndNode& ) override; diff --git a/sw/source/filter/ww8/rtfexport.cxx b/sw/source/filter/ww8/rtfexport.cxx index 292c7772c35b..89354bafc1dc 100644 --- a/sw/source/filter/ww8/rtfexport.cxx +++ b/sw/source/filter/ww8/rtfexport.cxx @@ -676,7 +676,7 @@ void RtfExport::WritePageDescTable() m_pTableInfo = std::make_shared(); } -void RtfExport::ExportDocument_Impl() +ErrCode RtfExport::ExportDocument_Impl() { // Make the header Strm() @@ -947,6 +947,8 @@ void RtfExport::ExportDocument_Impl() WriteMainText(); Strm().WriteChar('}'); + + return ERRCODE_NONE; } void RtfExport::PrepareNewPageDesc(const SfxItemSet* pSet, const SwNode& rNd, diff --git a/sw/source/filter/ww8/rtfexport.hxx b/sw/source/filter/ww8/rtfexport.hxx index fd2c7a285e7b..a1255cac5a11 100644 --- a/sw/source/filter/ww8/rtfexport.hxx +++ b/sw/source/filter/ww8/rtfexport.hxx @@ -118,7 +118,7 @@ public: protected: /// Format-dependent part of the actual export. - void ExportDocument_Impl() override; + ErrCode ExportDocument_Impl() override; void SectionBreaksAndFrames(const SwTextNode& /*rNode*/) override {} diff --git a/sw/source/filter/ww8/wrtww8.cxx b/sw/source/filter/ww8/wrtww8.cxx index 81fdf13a4abf..1cea99be077f 100644 --- a/sw/source/filter/ww8/wrtww8.cxx +++ b/sw/source/filter/ww8/wrtww8.cxx @@ -3119,7 +3119,7 @@ namespace { const sal_uLong WW_BLOCKSIZE = 0x200; - void EncryptRC4(msfilter::MSCodec_Std97& rCtx, SvStream &rIn, SvStream &rOut) + ErrCode EncryptRC4(msfilter::MSCodec_Std97& rCtx, SvStream &rIn, SvStream &rOut) { rIn.Seek(STREAM_SEEK_TO_END); sal_uLong nLen = rIn.Tell(); @@ -3130,14 +3130,17 @@ namespace { std::size_t nBS = std::min(nLen - nI, WW_BLOCKSIZE); nBS = rIn.ReadBytes(in, nBS); - rCtx.InitCipher(nBlock); + if (!rCtx.InitCipher(nBlock)) { + return ERRCODE_IO_NOTSUPPORTED; + } rCtx.Encode(in, nBS, in, nBS); rOut.WriteBytes(in, nBS); } + return ERRCODE_NONE; } } -void MSWordExportBase::ExportDocument( bool bWriteAll ) +ErrCode MSWordExportBase::ExportDocument( bool bWriteAll ) { m_nCharFormatStart = DEFAULT_STYLES_COUNT; m_nFormatCollStart = m_nCharFormatStart + m_pDoc->GetCharFormats()->size() - 1; @@ -3204,7 +3207,7 @@ void MSWordExportBase::ExportDocument( bool bWriteAll ) if ( m_pDoc->getIDocumentDrawModelAccess().GetDrawModel() ) m_pDoc->getIDocumentDrawModelAccess().GetDrawModel()->GetPage( 0 )->RecalcObjOrdNums(); - ExportDocument_Impl(); + ErrCode err = ExportDocument_Impl(); m_aFrames.clear(); @@ -3217,6 +3220,8 @@ void MSWordExportBase::ExportDocument( bool bWriteAll ) *m_pCurPam = *m_pOrigPam; m_pDoc->getIDocumentRedlineAccess().SetRedlineFlags(m_nOrigRedlineFlags); + + return err; } bool SwWW8Writer::InitStd97CodecUpdateMedium( ::msfilter::MSCodec_Std97& rCodec ) @@ -3267,7 +3272,7 @@ bool SwWW8Writer::InitStd97CodecUpdateMedium( ::msfilter::MSCodec_Std97& rCodec return ( aEncryptionData.getLength() != 0 ); } -void WW8Export::ExportDocument_Impl() +ErrCode WW8Export::ExportDocument_Impl() { PrepareStorage(); @@ -3365,6 +3370,7 @@ void WW8Export::ExportDocument_Impl() StoreDoc1(); + ErrCode err = ERRCODE_NONE; if ( bEncrypt ) { SvStream *pStrmTemp, *pTableStrmTemp, *pDataStrmTemp; @@ -3373,9 +3379,15 @@ void WW8Export::ExportDocument_Impl() pDataStrmTemp = xDataStrm.get(); if ( pDataStrmTemp && pDataStrmTemp != pStrmTemp) - EncryptRC4(aCtx, *pDataStrm, *pDataStrmTemp); + err = EncryptRC4(aCtx, *pDataStrm, *pDataStrmTemp); + if (err != ERRCODE_NONE) { + goto done; + } - EncryptRC4(aCtx, *pTableStrm, *pTableStrmTemp); + err = EncryptRC4(aCtx, *pTableStrm, *pTableStrmTemp); + if (err != ERRCODE_NONE) { + goto done; + } // Write Unencrypted Header 52 bytes to the start of the table stream // EncryptionVersionInfo (4 bytes): A Version structure where Version.vMajor MUST be 0x0001, and Version.vMinor MUST be 0x0001. @@ -3393,7 +3405,10 @@ void WW8Export::ExportDocument_Impl() pTableStrmTemp->WriteBytes(pSaltData, 16); pTableStrmTemp->WriteBytes(pSaltDigest, 16); - EncryptRC4(aCtx, GetWriter().Strm(), *pStrmTemp); + err = EncryptRC4(aCtx, GetWriter().Strm(), *pStrmTemp); + if (err != ERRCODE_NONE) { + goto done; + } // Write Unencrypted Fib 68 bytes to the start of the workdocument stream pFib->m_fEncrypted = true; // fEncrypted indicates the document is encrypted. @@ -3403,6 +3418,7 @@ void WW8Export::ExportDocument_Impl() pStrmTemp->Seek( 0 ); pFib->WriteHeader( *pStrmTemp ); + done:; } DELETEZ( m_pGrf ); @@ -3443,6 +3459,8 @@ void WW8Export::ExportDocument_Impl() pDataStrm = nullptr; GetWriter().GetStorage().Remove(SL::aData); } + + return err; } void WW8Export::PrepareStorage() @@ -3522,16 +3540,17 @@ ErrCode SwWW8Writer::WriteStorage() } // Do the actual export + ErrCode err = ERRCODE_NONE; { bool bDot = mpMedium->GetFilter()->GetName().endsWith("Vorlage"); WW8Export aExport(this, m_pDoc, m_pCurrentPam, m_pOrigPam, bDot); m_pExport = &aExport; - aExport.ExportDocument( m_bWriteAll ); + err = aExport.ExportDocument( m_bWriteAll ); m_pExport = nullptr; } ::EndProgress( m_pDoc->GetDocShell() ); - return ERRCODE_NONE; + return err; } ErrCode SwWW8Writer::WriteMedium( SfxMedium& ) diff --git a/sw/source/filter/ww8/wrtww8.hxx b/sw/source/filter/ww8/wrtww8.hxx index cec21326bec3..8845e9a09477 100644 --- a/sw/source/filter/ww8/wrtww8.hxx +++ b/sw/source/filter/ww8/wrtww8.hxx @@ -577,7 +577,7 @@ public: public: /// The main function to export the document. - void ExportDocument( bool bWriteAll ); + ErrCode ExportDocument( bool bWriteAll ); /// Iterate through the nodes and call the appropriate OutputNode() on them. void WriteText(); @@ -795,7 +795,7 @@ public: protected: /// Format-dependent part of the actual export. - virtual void ExportDocument_Impl() = 0; + virtual ErrCode ExportDocument_Impl() = 0; /// Get the next position in the text node to output sal_Int32 GetNextPos( SwWW8AttrIter const * pAttrIter, const SwTextNode& rNode, sal_Int32 nCurrentPos ); @@ -996,7 +996,7 @@ public: virtual bool AddSectionBreaksForTOX() const override { return false; } private: /// Format-dependent part of the actual export. - virtual void ExportDocument_Impl() override; + virtual ErrCode ExportDocument_Impl() override; void PrepareStorage(); void WriteFkpPlcUsw(); -- cgit