diff --git a/PdfFile/PdfFile.cpp b/PdfFile/PdfFile.cpp index c9e7ba4da2..aac94c8748 100644 --- a/PdfFile/PdfFile.cpp +++ b/PdfFile/PdfFile.cpp @@ -108,11 +108,11 @@ void CPdfFile::Sign(const double& dX, const double& dY, const double& dW, const return; m_pInternal->pWriter->Sign(dX, dY, dW, dH, wsPicturePath); } -bool CPdfFile::PrepareSignature(BYTE** pDataToSign, DWORD& dwDataLength) +bool CPdfFile::PrepareSignature(const std::wstring& wsPath) { if (!m_pInternal->pWriter) return false; - return m_pInternal->pWriter->PrepareSignature(pDataToSign, dwDataLength); + return m_pInternal->pWriter->PrepareSignature(wsPath); } bool CPdfFile::FinalizeSignature(BYTE* pSignedData, DWORD dwDataLength) { diff --git a/PdfFile/PdfFile.h b/PdfFile/PdfFile.h index 8f6d09e7a0..51ff91bbbe 100644 --- a/PdfFile/PdfFile.h +++ b/PdfFile/PdfFile.h @@ -161,7 +161,7 @@ public: void SetPassword (const std::wstring& wsPassword); void SetDocumentID(const std::wstring& wsDocumentID); void Sign(const double& dX, const double& dY, const double& dW, const double& dH, const std::wstring& wsPicturePath); - bool PrepareSignature(BYTE** pDataToSign, DWORD& dwDataLength); + bool PrepareSignature(const std::wstring& wsPath); bool FinalizeSignature(BYTE* pSignedData, DWORD dwDataLength); void SetDocumentInfo(const std::wstring& wsTitle, const std::wstring& wsCreator, const std::wstring& wsSubject, const std::wstring& wsKeywords); void AddMetaData(const std::wstring& sMetaName, BYTE* pMetaData, DWORD nMetaLength); diff --git a/PdfFile/PdfWriter.cpp b/PdfFile/PdfWriter.cpp index 17332db1ba..5cc68d0fc0 100644 --- a/PdfFile/PdfWriter.cpp +++ b/PdfFile/PdfWriter.cpp @@ -3273,11 +3273,11 @@ void CPdfWriter::Sign(const double& dX, const double& dY, const double& dW, cons m_pDocument->Sign(PdfWriter::TRect(MM_2_PT(dX), m_pPage->GetHeight() - MM_2_PT(dY), MM_2_PT(dX + dW), m_pPage->GetHeight() - MM_2_PT(dY + dH)), pImage); } -bool CPdfWriter::PrepareSignature(BYTE** pDataToSign, DWORD& dwDataLength) +bool CPdfWriter::PrepareSignature(const std::wstring& wsPath) { if (!m_pDocument) return false; - return m_pDocument->PrepareSignature(pDataToSign, dwDataLength); + return m_pDocument->PrepareSignature(wsPath); } bool CPdfWriter::FinalizeSignature(BYTE* pSignedData, DWORD dwDataLength) { diff --git a/PdfFile/PdfWriter.h b/PdfFile/PdfWriter.h index c73ebea75d..91547af303 100644 --- a/PdfFile/PdfWriter.h +++ b/PdfFile/PdfWriter.h @@ -226,7 +226,7 @@ public: bool EditClose(); void PageRotate(int nRotate); void Sign(const double& dX, const double& dY, const double& dW, const double& dH, const std::wstring& wsPicturePath); - bool PrepareSignature(BYTE** pDataToSign, DWORD& dwDataLength); + bool PrepareSignature(const std::wstring& wsPath); bool FinalizeSignature(BYTE* pSignedData, DWORD dwDataLength); PdfWriter::CDocument* GetDocument(); PdfWriter::CPage* GetPage(); diff --git a/PdfFile/SrcWriter/Document.cpp b/PdfFile/SrcWriter/Document.cpp index bad58bf4fa..436b54e162 100644 --- a/PdfFile/SrcWriter/Document.cpp +++ b/PdfFile/SrcWriter/Document.cpp @@ -1963,20 +1963,16 @@ namespace PdfWriter { m_vSignatures.push_back(new TSignatureInfo(oRect, m_pCurPage ? m_pCurPage : m_pPageTree->GetPage(0), pImage)); } - bool CDocument::PrepareSignature(BYTE** pDataToSign, DWORD& dwDataLength) + bool CDocument::PrepareSignature(const std::wstring& wsPath) { // Сначала нужно сохранить основной файл // Это должно быть сделано в AddToFile или SaveToFile ПЕРЕД вызовом этого метода - if (m_vSignatures.empty()) + if (m_vSignatures.empty() || wsPath.empty()) return false; TSignatureInfo* pSI = m_vSignatures[0]; - std::wstring wsPath = pSI->wsPath; - if (wsPath.empty()) - return false; - unsigned int nSizeXRef = pSI->nSizeXRef; bool bNeedStreamXRef = pSI->bNeedStreamXRef; @@ -2059,7 +2055,7 @@ namespace PdfWriter pSI->pXref = pXref; } - pField->GetSignatureDict()->WriteToStream(pStream, pStream->Size(), pDataToSign, dwDataLength); + pField->GetSignatureDict()->WriteToStream(pStream, pStream->Size()); // Восстанавливаем XRef m_pXref = pXrefBefore; diff --git a/PdfFile/SrcWriter/Document.h b/PdfFile/SrcWriter/Document.h index 5511034b59..a48433ad42 100644 --- a/PdfFile/SrcWriter/Document.h +++ b/PdfFile/SrcWriter/Document.h @@ -198,7 +198,7 @@ namespace PdfWriter void AddObject(CObjectBase* pObj); bool MovePage(int nPageIndex, int nPos); void Sign(const TRect& oRect, CImageDict* pImage); - bool PrepareSignature(BYTE** pDataToSign, DWORD& dwDataLength); + bool PrepareSignature(const std::wstring& wsPath); bool FinalizeSignature(BYTE* pSignedData, DWORD dwDataLength); bool EditAnnot (CXref* pXref, CAnnotation* pAnnot, int nID); void AddParent(int nID, CDictObject* pParent); diff --git a/PdfFile/SrcWriter/EncryptDictionary.cpp b/PdfFile/SrcWriter/EncryptDictionary.cpp index dcb0d68fc3..66b3316b79 100644 --- a/PdfFile/SrcWriter/EncryptDictionary.cpp +++ b/PdfFile/SrcWriter/EncryptDictionary.cpp @@ -259,7 +259,7 @@ namespace PdfWriter } } } - void CSignatureDict::WriteToStream(CStream* pStream, int nFileEnd, BYTE** pDataForSignature, DWORD& dwLenDataForSignature) + void CSignatureDict::WriteToStream(CStream* pStream, int nFileEnd) { // Запись ByteRange if (m_nByteRangeBegin > 0 && m_nByteRangeEnd > 0 && m_nByteRangeBegin < m_nByteRangeEnd && m_nByteRangeEnd < nFileEnd) @@ -291,32 +291,6 @@ namespace PdfWriter } RELEASEOBJECT(pByteRange); } - // Запись Contents - if (m_nLen1 > 0 && m_nOffset2 > 0 && m_nLen1 < m_nOffset2 && m_nOffset2 < nFileEnd) - { - dwLenDataForSignature = m_nLen1 + nFileEnd - m_nOffset2; - *pDataForSignature = new BYTE[dwLenDataForSignature]; - if (!*pDataForSignature) - return; - - pStream->Seek(0, EWhenceMode::SeekSet); - unsigned int dwLenReadData = m_nLen1; - pStream->Read(*pDataForSignature, &dwLenReadData); - if ((int)dwLenReadData != m_nLen1) - { - RELEASEARRAYOBJECTS(*pDataForSignature); - return; - } - - pStream->Seek(m_nOffset2, EWhenceMode::SeekSet); - dwLenReadData = nFileEnd - m_nOffset2; - pStream->Read(*pDataForSignature + m_nLen1, &dwLenReadData); - if ((int)dwLenReadData != nFileEnd - m_nOffset2) - { - RELEASEARRAYOBJECTS(*pDataForSignature); - return; - } - } } bool CSignatureDict::FinalizeSignature(CStream* pStream, BYTE* pSignedData, DWORD dwDataLength) { diff --git a/PdfFile/SrcWriter/EncryptDictionary.h b/PdfFile/SrcWriter/EncryptDictionary.h index e5ad3cbaec..505cfa534e 100644 --- a/PdfFile/SrcWriter/EncryptDictionary.h +++ b/PdfFile/SrcWriter/EncryptDictionary.h @@ -76,7 +76,7 @@ namespace PdfWriter } void WriteToStream(CStream* pStream, CEncrypt* pEncrypt) override; - void WriteToStream(CStream* pStream, int nFileEnd, BYTE** pDataToSign, DWORD& dwDataLength); + void WriteToStream(CStream* pStream, int nFileEnd); bool FinalizeSignature(CStream* pStream, BYTE* pSignedData, DWORD dwDataLength); void SetContentsSize(unsigned int nSize); diff --git a/PdfFile/test/test.cpp b/PdfFile/test/test.cpp index cdfe2185a8..4d1f07a912 100644 --- a/PdfFile/test/test.cpp +++ b/PdfFile/test/test.cpp @@ -619,17 +619,19 @@ TEST_F(CPdfFileTest, EditPdfSign) // EditPdf & EditClose || CreatePdf & SaveToFile // И только после подготовка данных для подписания, подписываем, запись подписи + std::wstring wsSignPath = wsTempDir + L"/pdf_sign.pdf"; for (int i = 0; i < 3; ++i) { - BYTE* pDataToSign = NULL; - DWORD dwDataLength = 0; - - // Получили данные для подписания - pdfFile->PrepareSignature(&pDataToSign, dwDataLength); + pdfFile->PrepareSignature(wsSignPath); ICertificate* pCertificate = GetCertificate(); ASSERT_TRUE(pCertificate); + BYTE* pDataToSign = NULL; + DWORD dwDataLength = 0; + + NSFile::CFileBinary::ReadAllBytes(wsSignPath, &pDataToSign, dwDataLength); + BYTE* pDatatoWrite = NULL; unsigned int dwLenDatatoWrite = 0; // Предположим, что для подписи 1 произошло не успешное подписание, и данные остались пустыми diff --git a/X2tConverter/src/lib/pdf_image.h b/X2tConverter/src/lib/pdf_image.h index 84a6880bf3..aaf33ccc56 100644 --- a/X2tConverter/src/lib/pdf_image.h +++ b/X2tConverter/src/lib/pdf_image.h @@ -48,18 +48,19 @@ namespace NExtractTools { static int pdfSign(const std::wstring& file, NSFonts::IApplicationFonts* fonts, InputParams& params, ConvertParams& convertParams) { + /* ICertificate* certificate = NSSign::loadCertificate(params); if (!certificate) return 1; - std::wstring pdfTemp = combinePath(convertParams.m_sTempDir, L"pdf_sign.pdf"); NSFile::CFileBinary::Copy(file, pdfTemp); + */ CPdfFile pdfFile(fonts); pdfFile.SetTempDirectory(convertParams.m_sTempDir); std::wstring password = params.getSavePassword(); - if (!pdfFile.LoadFromFile(pdfTemp, L"", password.c_str(), password.c_str())) + if (!pdfFile.LoadFromFile(file, L"", password.c_str(), password.c_str())) return 2; if (!pdfFile.EditPdf(file)) @@ -71,14 +72,14 @@ namespace NExtractTools pdfFile.Sign(0, 0, 0, 0, L""); pdfFile.Close(); + if (!pdfFile.PrepareSignature(file)) + return 2; + + return 0; + + /* BYTE* pDataToSign = NULL; DWORD dwDataLength = 0; - if (!pdfFile.PrepareSignature(&pDataToSign, dwDataLength)) - { - RELEASEARRAYOBJECTS(pDataToSign); - return 2; - } - BYTE* pDatatoWrite = NULL; unsigned int dwLenDatatoWrite = 0; certificate->SignPKCS7(pDataToSign, dwDataLength, pDatatoWrite, dwLenDatatoWrite); @@ -93,6 +94,7 @@ namespace NExtractTools RELEASEARRAYOBJECTS(pDatatoWrite); RELEASEOBJECT(certificate); return 0; + */ } } @@ -129,7 +131,7 @@ namespace NExtractTools nRet = S_OK == pdfWriter.OnlineWordToPdf(sFrom, sTo, &oBufferParams) ? 0 : AVS_FILEUTILS_ERROR_CONVERT; } - if (0 == nRet) + if (0 == nRet && params.getSigningKeyStorePath() == L"_placeholder_") { pdfSign(sTo, pApplicationFonts, params, convertParams); } @@ -304,7 +306,7 @@ namespace NExtractTools int nReg = (convertParams.m_bIsPaid == false) ? 0 : 1; nRes = (S_OK == pdfWriter.OnlineWordToPdfFromBinary(sPdfBinFile, sTo, &oBufferParams)) ? nRes : AVS_FILEUTILS_ERROR_CONVERT; - if (0 == nRes) + if (0 == nRes && params.getSigningKeyStorePath() == L"_placeholder_") { pdfSign(sTo, pApplicationFonts, params, convertParams); } @@ -1107,7 +1109,7 @@ namespace NExtractTools { nRes = S_OK == pdfWriter.SaveToFile(sTo) ? 0 : AVS_FILEUTILS_ERROR_CONVERT; - if (0 == nRes) + if (0 == nRes && params.getSigningKeyStorePath() == L"_placeholder_") { pdfSign(sTo, pApplicationFonts, params, convertParams); }