20#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
21#include <com/sun/star/xml/crypto/DigestID.hpp>
22#include <com/sun/star/xml/crypto/CipherID.hpp>
23#include <com/sun/star/beans/PropertyValue.hpp>
24#include <com/sun/star/beans/NamedValue.hpp>
25#include <com/sun/star/uno/RuntimeException.hpp>
30#include <osl/diagnose.h>
31#include <rtl/ustrbuf.hxx>
40#if OSL_DEBUG_LEVEL > 0
41#define THROW_WHERE SAL_WHERE
46ManifestExport::ManifestExport( uno::Reference< xml::sax::XDocumentHandler >
const & xHandler,
const uno::Sequence< uno::Sequence < beans::PropertyValue > >& rManList )
48 static constexpr OUStringLiteral sKeyInfo (
u"KeyInfo" );
49 static constexpr OUStringLiteral sPgpKeyIDProperty (
u"KeyId" );
50 static constexpr OUStringLiteral sPgpKeyPacketProperty (
u"KeyPacket" );
51 static constexpr OUStringLiteral sCipherValueProperty (
u"CipherValue" );
52 static constexpr OUStringLiteral sFullPathProperty (
u"FullPath" );
53 static constexpr OUStringLiteral sVersionProperty (
u"Version" );
54 static constexpr OUStringLiteral sMediaTypeProperty (
u"MediaType" );
55 static constexpr OUStringLiteral sIterationCountProperty (
u"IterationCount" );
56 static constexpr OUStringLiteral sDerivedKeySizeProperty (
u"DerivedKeySize" );
57 static constexpr OUStringLiteral sSaltProperty (
u"Salt" );
58 static constexpr OUStringLiteral sInitialisationVectorProperty(
u"InitialisationVector" );
59 static constexpr OUStringLiteral sSizeProperty (
u"Size" );
60 static constexpr OUStringLiteral sDigestProperty (
u"Digest" );
61 static constexpr OUStringLiteral sEncryptionAlgProperty (
u"EncryptionAlgorithm" );
62 static constexpr OUStringLiteral sStartKeyAlgProperty (
u"StartKeyAlgorithm" );
63 static constexpr OUStringLiteral sDigestAlgProperty (
u"DigestAlgorithm" );
65 static constexpr OUStringLiteral sWhiteSpace (
u" " );
77 const OUString sPGP_Name (
PGP_NAME );
82 OUString aDocMediaType;
84 const uno::Sequence<beans::PropertyValue>* pRootFolderPropSeq =
nullptr;
85 for (
const uno::Sequence < beans::PropertyValue >& rSequence : rManList)
91 for (
const beans::PropertyValue& rValue : rSequence)
93 if (rValue.Name == sMediaTypeProperty )
95 rValue.Value >>= aMediaType;
97 else if (rValue.Name == sFullPathProperty )
99 rValue.Value >>= aPath;
101 else if (rValue.Name == sVersionProperty )
103 rValue.Value >>= aVersion;
106 if ( !aPath.isEmpty() && !aMediaType.isEmpty() && !aVersion.isEmpty() )
112 aDocMediaType = aMediaType;
113 aDocVersion = aVersion;
114 pRootFolderPropSeq = &rSequence;
119 bool bProvideDTD =
false;
120 bool bAcceptNonemptyVersion =
false;
121 bool bStoreStartKeyGeneration =
false;
122 if ( !aDocMediaType.isEmpty() )
145 bAcceptNonemptyVersion =
true;
150 bStoreStartKeyGeneration =
true;
168 xHandler->startDocument();
169 uno::Reference < xml::sax::XExtendedDocumentHandler > xExtHandler ( xHandler, uno::UNO_QUERY );
170 if ( xExtHandler.is() && bProvideDTD )
173 xHandler->ignorableWhitespace ( sWhiteSpace );
177 const uno::Any *pKeyInfoProperty =
nullptr;
178 if ( pRootFolderPropSeq )
181 for (
const beans::PropertyValue& rValue : *pRootFolderPropSeq)
183 if (rValue.Name == sKeyInfo )
184 pKeyInfoProperty = &rValue.
Value;
187 if ( pKeyInfoProperty )
193 xHandler->ignorableWhitespace ( sWhiteSpace );
201 xHandler->ignorableWhitespace ( sWhiteSpace );
203 uno::Sequence< uno::Sequence < beans::NamedValue > > aKeyInfoSequence;
204 *pKeyInfoProperty >>= aKeyInfoSequence;
205 for (
const uno::Sequence<beans::NamedValue>& rKeyInfoSequence : std::as_const(aKeyInfoSequence))
207 uno::Sequence < sal_Int8 > aPgpKeyID;
208 uno::Sequence < sal_Int8 > aPgpKeyPacket;
209 uno::Sequence < sal_Int8 > aCipherValue;
210 for (
const beans::NamedValue& rNValue : rKeyInfoSequence)
212 if (rNValue.Name == sPgpKeyIDProperty )
213 rNValue.
Value >>= aPgpKeyID;
214 else if (rNValue.Name == sPgpKeyPacketProperty )
215 rNValue.Value >>= aPgpKeyPacket;
216 else if (rNValue.Name == sCipherValueProperty )
217 rNValue.Value >>= aCipherValue;
220 if (aPgpKeyID.hasElements() && aCipherValue.hasElements() )
224 xHandler->ignorableWhitespace ( sWhiteSpace );
228 pNewAttrList->AddAttribute(
230 "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" );
233 xHandler->ignorableWhitespace ( sWhiteSpace );
237 xHandler->ignorableWhitespace ( sWhiteSpace );
240 xHandler->ignorableWhitespace ( sWhiteSpace );
244 xHandler->characters(
aBuffer.makeStringAndClear() );
246 xHandler->ignorableWhitespace ( sWhiteSpace );
249 if (aPgpKeyPacket.hasElements())
253 xHandler->characters(
aBuffer.makeStringAndClear() );
255 xHandler->ignorableWhitespace ( sWhiteSpace );
259 xHandler->ignorableWhitespace ( sWhiteSpace );
262 xHandler->ignorableWhitespace ( sWhiteSpace );
265 xHandler->ignorableWhitespace ( sWhiteSpace );
269 xHandler->characters(
aBuffer.makeStringAndClear() );
271 xHandler->ignorableWhitespace ( sWhiteSpace );
274 xHandler->ignorableWhitespace ( sWhiteSpace );
277 xHandler->ignorableWhitespace ( sWhiteSpace );
285 xHandler->ignorableWhitespace ( sWhiteSpace );
290 for (
const uno::Sequence<beans::PropertyValue>& rSequence : rManList)
294 const uno::Any *pVector =
nullptr, *pSalt =
nullptr, *pIterationCount =
nullptr, *pDigest =
nullptr, *pDigestAlg =
nullptr, *pEncryptAlg =
nullptr, *pStartKeyAlg =
nullptr, *pDerivedKeySize =
nullptr;
295 for (
const beans::PropertyValue& rValue : rSequence)
297 if (rValue.Name == sMediaTypeProperty )
299 rValue.
Value >>= aString;
302 else if (rValue.Name == sVersionProperty )
304 rValue.Value >>= aString;
306 if ( bAcceptNonemptyVersion && !aString.isEmpty() )
309 else if (rValue.Name == sFullPathProperty )
311 rValue.Value >>= aString;
314 else if (rValue.Name == sSizeProperty )
317 rValue.Value >>= nSize;
318 pAttrList->AddAttribute (
ATTRIBUTE_SIZE, OUString::number( nSize ) );
320 else if (rValue.Name == sInitialisationVectorProperty )
321 pVector = &rValue.
Value;
322 else if (rValue.Name == sSaltProperty )
323 pSalt = &rValue.Value;
324 else if (rValue.Name == sIterationCountProperty )
325 pIterationCount = &rValue.Value;
326 else if (rValue.Name == sDigestProperty )
327 pDigest = &rValue.Value;
328 else if (rValue.Name == sDigestAlgProperty )
329 pDigestAlg = &rValue.Value;
330 else if (rValue.Name == sEncryptionAlgProperty )
331 pEncryptAlg = &rValue.Value;
332 else if (rValue.Name == sStartKeyAlgProperty )
333 pStartKeyAlg = &rValue.Value;
334 else if (rValue.Name == sDerivedKeySizeProperty )
335 pDerivedKeySize = &rValue.Value;
338 xHandler->ignorableWhitespace ( sWhiteSpace );
340 if ( pVector && pSalt && pIterationCount && pDigest && pDigestAlg && pEncryptAlg && pStartKeyAlg && pDerivedKeySize )
345 uno::Sequence < sal_Int8 > aSequence;
347 xHandler->ignorableWhitespace ( sWhiteSpace );
350 OUString sChecksumType;
351 sal_Int32 nDigestAlgID = 0;
352 *pDigestAlg >>= nDigestAlgID;
353 if ( nDigestAlgID == xml::crypto::DigestID::SHA256_1K )
354 sChecksumType = sSHA256_1k_URL;
355 else if ( nDigestAlgID == xml::crypto::DigestID::SHA1_1K )
356 sChecksumType = sSHA1_1k_Name;
358 throw uno::RuntimeException(
THROW_WHERE "Unexpected digest algorithm is provided!" );
361 *pDigest >>= aSequence;
368 pNewAttrList = new ::comphelper::AttributeList;
370 sal_Int32 nEncAlgID = 0;
371 sal_Int32 nDerivedKeySize = 0;
372 *pEncryptAlg >>= nEncAlgID;
373 *pDerivedKeySize >>= nDerivedKeySize;
375 OUString sEncAlgName;
376 if ( nEncAlgID == xml::crypto::CipherID::AES_CBC_W3C_PADDING )
378 OSL_ENSURE( nDerivedKeySize,
"Unexpected key size is provided!" );
379 if ( nDerivedKeySize != 32 )
380 throw uno::RuntimeException(
THROW_WHERE "Unexpected key size is provided!" );
382 sEncAlgName = sAES256_URL;
384 else if ( nEncAlgID == xml::crypto::CipherID::BLOWFISH_CFB_8 )
386 sEncAlgName = sBlowfish_Name;
389 throw uno::RuntimeException(
THROW_WHERE "Unexpected encryption algorithm is provided!" );
393 *pVector >>= aSequence;
397 xHandler->ignorableWhitespace ( sWhiteSpace );
399 xHandler->ignorableWhitespace ( sWhiteSpace );
402 if ( bStoreStartKeyGeneration )
405 pNewAttrList = new ::comphelper::AttributeList;
407 OUString sStartKeyAlg;
408 OUString sStartKeySize;
409 sal_Int32 nStartKeyAlgID = 0;
410 *pStartKeyAlg >>= nStartKeyAlgID;
411 if ( nStartKeyAlgID == xml::crypto::DigestID::SHA256 )
413 sStartKeyAlg = sSHA256_URL_ODF12;
414 aBuffer.append( sal_Int32(32) );
415 sStartKeySize =
aBuffer.makeStringAndClear();
417 else if ( nStartKeyAlgID == xml::crypto::DigestID::SHA1 )
419 sStartKeyAlg = sSHA1_Name;
420 aBuffer.append( sal_Int32(20) );
421 sStartKeySize =
aBuffer.makeStringAndClear();
424 throw uno::RuntimeException(
THROW_WHERE "Unexpected start key algorithm is provided!" );
429 xHandler->ignorableWhitespace ( sWhiteSpace );
431 xHandler->ignorableWhitespace ( sWhiteSpace );
436 pNewAttrList = new ::comphelper::AttributeList;
438 if (pKeyInfoProperty)
444 bStoreStartKeyGeneration =
false;
451 if (bStoreStartKeyGeneration)
453 aBuffer.append(nDerivedKeySize);
458 *pIterationCount >>=
nCount;
462 *pSalt >>= aSequence;
467 xHandler->ignorableWhitespace(sWhiteSpace);
469 xHandler->ignorableWhitespace(sWhiteSpace);
472 xHandler->ignorableWhitespace ( sWhiteSpace );
475 xHandler->ignorableWhitespace ( sWhiteSpace );
478 xHandler->ignorableWhitespace ( sWhiteSpace );
480 xHandler->endDocument();
constexpr OUStringLiteral MANIFEST_OASIS_NAMESPACE
constexpr OUStringLiteral ELEMENT_ENCRYPTIONMETHOD
constexpr OUStringLiteral SHA256_1K_URL
constexpr OUStringLiteral ATTRIBUTE_MEDIA_TYPE
constexpr OUStringLiteral ATTRIBUTE_INITIALISATION_VECTOR
constexpr OUStringLiteral ELEMENT_PGPDATA
constexpr OUStringLiteral SHA1_1K_NAME
constexpr OUStringLiteral ELEMENT_MANIFEST_KEYINFO
constexpr OUStringLiteral ATTRIBUTE_ALGORITHM_NAME
constexpr OUStringLiteral SHA1_NAME
constexpr OUStringLiteral ATTRIBUTE_VERSION
constexpr OUStringLiteral ELEMENT_MANIFEST13_KEYINFO
constexpr OUStringLiteral ELEMENT_PGPKEYPACKET13
constexpr OUStringLiteral ELEMENT_FILE_ENTRY
constexpr OUStringLiteral ATTRIBUTE_CHECKSUM_TYPE
constexpr OUStringLiteral ATTRIBUTE_XMLNS
constexpr OUStringLiteral ELEMENT_ENCRYPTEDKEY13
constexpr OUStringLiteral ATTRIBUTE_KEY_DERIVATION_NAME
constexpr OUStringLiteral ELEMENT_ENCRYPTION_DATA
constexpr OUStringLiteral ELEMENT_CIPHERDATA
constexpr OUStringLiteral BLOWFISH_NAME
constexpr OUStringLiteral MANIFEST_DOCTYPE
constexpr OUStringLiteral ATTRIBUTE_ITERATION_COUNT
constexpr OUStringLiteral ATTRIBUTE_FULL_PATH
constexpr OUStringLiteral ATTRIBUTE_SIZE
constexpr OUStringLiteral ELEMENT_ENCRYPTIONMETHOD13
constexpr OUStringLiteral ATTRIBUTE_XMLNS_LOEXT
constexpr OUStringLiteral ELEMENT_PGPDATA13
constexpr OUStringLiteral PBKDF2_NAME
constexpr OUStringLiteral ELEMENT_KEY_DERIVATION
constexpr OUStringLiteral ELEMENT_PGPKEYPACKET
constexpr OUStringLiteral ELEMENT_START_KEY_GENERATION
constexpr OUStringLiteral AES256_URL
constexpr OUStringLiteral SHA256_URL_ODF12
OFFICE-3708: wrong URL cited in ODF 1.2 and used since OOo 3.4 beta.
constexpr OUStringLiteral ATTRIBUTE_SALT
constexpr OUStringLiteral ATTRIBUTE_ALGORITHM
constexpr OUStringLiteral ELEMENT_ENCRYPTEDKEY
constexpr OUStringLiteral MANIFEST_NAMESPACE
constexpr OUStringLiteral ATTRIBUTE_CHECKSUM
constexpr OUStringLiteral MANIFEST_LOEXT_NAMESPACE
constexpr OUStringLiteral ELEMENT_CIPHERVALUE13
constexpr OUStringLiteral ATTRIBUTE_ALGORITHM13
constexpr OUStringLiteral PGP_NAME
constexpr OUStringLiteral ATTRIBUTE_START_KEY_GENERATION_NAME
constexpr OUStringLiteral ELEMENT_PGPKEYID13
constexpr OUStringLiteral ATTRIBUTE_KEY_SIZE
constexpr OUStringLiteral ELEMENT_CIPHERDATA13
constexpr OUStringLiteral ELEMENT_MANIFEST
constexpr OUStringLiteral ELEMENT_ALGORITHM
constexpr OUStringLiteral ELEMENT_PGPKEYID
constexpr OUStringLiteral ELEMENT_CIPHERVALUE
ManifestExport(css::uno::Reference< css::xml::sax::XDocumentHandler > const &xHandler, const css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > &rManList)
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_DATABASE_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_TEMPLATE_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_TEMPLATE_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_TEXT_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_TEXT_TEMPLATE_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_TEXT_GLOBAL_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_CHART_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_ASCII
constexpr OUStringLiteral ODFVER_013_TEXT
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_TEXT_WEB_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_CHART_TEMPLATE_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_DRAWING_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_FORMULA_TEMPLATE_ASCII
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_PRESENTATION_TEMPLATE_ASCII
constexpr OUStringLiteral ODFVER_012_TEXT
constexpr OUStringLiteral MIMETYPE_OASIS_OPENDOCUMENT_SPREADSHEET_TEMPLATE_ASCII
std::unique_ptr< char[]> aBuffer