diff --git a/xmlsecurity/inc/pdfio/pdfdocument.hxx b/xmlsecurity/inc/pdfio/pdfdocument.hxx index d90fdf50245b..79ef60b2e8c0 100644 --- a/xmlsecurity/inc/pdfio/pdfdocument.hxx +++ b/xmlsecurity/inc/pdfio/pdfdocument.hxx @@ -113,6 +113,8 @@ class XMLSECURITY_DLLPUBLIC PDFDocument static int AsHex(char ch); /// Decode a hex dump. static std::vector DecodeHexString(PDFHexStringElement* pElement); + /// Suggest a minimal, yet free signature ID to use for the next signature. + sal_uInt32 GetNextSignature(); public: PDFDocument(); diff --git a/xmlsecurity/source/pdfio/pdfdocument.cxx b/xmlsecurity/source/pdfio/pdfdocument.cxx index 8bd942f8dc27..fa0d9cc99a4a 100644 --- a/xmlsecurity/source/pdfio/pdfdocument.cxx +++ b/xmlsecurity/source/pdfio/pdfdocument.cxx @@ -335,11 +335,30 @@ bool PDFDocument::RemoveSignature(size_t nPosition) return m_aEditBuffer.good(); } +sal_uInt32 PDFDocument::GetNextSignature() +{ + sal_uInt32 nRet = 0; + for (const auto& pSignature : GetSignatureWidgets()) + { + auto pT = dynamic_cast(pSignature->Lookup("T")); + if (!pT) + continue; + + const OString& rValue = pT->GetValue(); + const OString aPrefix = "Signature"; + if (!rValue.startsWith(aPrefix)) + continue; + + nRet = std::max(nRet, rValue.copy(aPrefix.getLength()).toUInt32()); + } + + return nRet + 1; +} + bool PDFDocument::Sign(const uno::Reference& xCertificate, const OUString& rDescription) { // Decide what identifier to use for the new signature. - std::vector aSignatures = GetSignatureWidgets(); - sal_uInt32 nNextSignature = aSignatures.size() + 1; + sal_uInt32 nNextSignature = GetNextSignature(); m_aEditBuffer.Seek(STREAM_SEEK_TO_END); m_aEditBuffer.WriteCharPtr("\n");