30#include <com/sun/star/xml/sax/XFastParser.hpp>
31#include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
32#include <com/sun/star/xml/sax/FastParser.hpp>
33#include <com/sun/star/xml/sax/FastToken.hpp>
36using namespace css::beans;
37using namespace css::io;
38using namespace css::lang;
39using namespace css::uno;
40using namespace css::xml::sax;
41using namespace css::xml;
47std::u16string_view stripNamespacePrefix(std::u16string_view rsInputName)
49 size_t idx = rsInputName.find(
':');
50 if (
idx == std::u16string_view::npos)
52 return rsInputName.substr(
idx + 1);
60 return FastToken::DONTKNOW;
63 virtual Sequence<sal_Int8> SAL_CALL getUTF8Identifier(sal_Int32 )
override
65 return Sequence<sal_Int8>();
68 virtual sal_Int32 getTokenDirect(
const char * , sal_Int32 )
const override
74class AgileDocumentHandler :
public ::cppu::WeakImplHelper<XFastDocumentHandler>
79 explicit AgileDocumentHandler(AgileEncryptionInfo& rInfo) :
83 void SAL_CALL startDocument()
override {}
84 void SAL_CALL endDocument()
override {}
85 void SAL_CALL processingInstruction(
const OUString& ,
const OUString& )
override {}
86 void SAL_CALL setDocumentLocator(
const Reference< XLocator >& )
override {}
87 void SAL_CALL startFastElement( sal_Int32 ,
const Reference< XFastAttributeList >& )
override {}
89 void SAL_CALL startUnknownElement(
const OUString& ,
const OUString& rName,
const Reference< XFastAttributeList >& aAttributeList )
override
91 std::u16string_view rLocalName = stripNamespacePrefix(rName);
93 const css::uno::Sequence<Attribute> aUnknownAttributes = aAttributeList->getUnknownAttributes();
94 for (
const Attribute& rAttribute : aUnknownAttributes)
96 std::u16string_view rAttrLocalName = stripNamespacePrefix(rAttribute.Name);
98 if (rAttrLocalName == u
"spinCount")
102 else if (rAttrLocalName == u
"saltSize")
106 else if (rAttrLocalName == u
"blockSize")
110 else if (rAttrLocalName == u
"keyBits")
114 else if (rAttrLocalName == u
"hashSize")
118 else if (rAttrLocalName == u
"cipherAlgorithm")
120 mInfo.cipherAlgorithm = rAttribute.Value;
122 else if (rAttrLocalName == u
"cipherChaining")
124 mInfo.cipherChaining = rAttribute.Value;
126 else if (rAttrLocalName == u
"hashAlgorithm")
128 mInfo.hashAlgorithm = rAttribute.Value;
130 else if (rAttrLocalName == u
"saltValue")
132 Sequence<sal_Int8> saltValue;
134 if (rLocalName == u
"encryptedKey")
135 mInfo.saltValue = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(saltValue);
136 else if (rLocalName == u
"keyData")
137 mInfo.keyDataSalt = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(saltValue);
139 else if (rAttrLocalName == u
"encryptedVerifierHashInput")
141 Sequence<sal_Int8> encryptedVerifierHashInput;
143 mInfo.encryptedVerifierHashInput = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(encryptedVerifierHashInput);
145 else if (rAttrLocalName == u
"encryptedVerifierHashValue")
147 Sequence<sal_Int8> encryptedVerifierHashValue;
149 mInfo.encryptedVerifierHashValue = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(encryptedVerifierHashValue);
151 else if (rAttrLocalName == u
"encryptedKeyValue")
153 Sequence<sal_Int8> encryptedKeyValue;
155 mInfo.encryptedKeyValue = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(encryptedKeyValue);
157 if (rAttrLocalName == u
"encryptedHmacKey")
159 Sequence<sal_Int8> aValue;
161 mInfo.hmacEncryptedKey = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(aValue);
163 if (rAttrLocalName == u
"encryptedHmacValue")
165 Sequence<sal_Int8> aValue;
167 mInfo.hmacEncryptedValue = comphelper::sequenceToContainer<std::vector<sal_uInt8>>(aValue);
172 void SAL_CALL endFastElement( sal_Int32 )
override
174 void SAL_CALL endUnknownElement(
const OUString& ,
const OUString& )
override
177 Reference< XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 ,
const Reference< XFastAttributeList >& )
override
182 Reference< XFastContextHandler > SAL_CALL createUnknownChildContext(
const OUString& ,
const OUString& ,
const Reference< XFastAttributeList >& )
override
187 void SAL_CALL characters(
const OUString& )
override
191constexpr const sal_uInt32 constSegmentLength = 4096;
193const std::vector<sal_uInt8> constBlock1 { 0xfe, 0xa7, 0xd2, 0x76, 0x3b, 0x4b, 0x9e, 0x79 };
194const std::vector<sal_uInt8> constBlock2 { 0xd7, 0xaa, 0x0f, 0x6d, 0x30, 0x61, 0x34, 0x4e };
195const std::vector<sal_uInt8> constBlock3 { 0x14, 0x6e, 0x0b, 0xe7, 0xab, 0xac, 0xd0, 0xd6 };
196const std::vector<sal_uInt8> constBlockHmac1 { 0x5f, 0xb2, 0xad, 0x01, 0x0c, 0xb9, 0xe1, 0xf6 };
197const std::vector<sal_uInt8> constBlockHmac2 { 0xa0, 0x67, 0x7f, 0x02, 0xb2, 0x2c, 0x84, 0x33 };
199bool hashCalc(std::vector<sal_uInt8>& output,
200 std::vector<sal_uInt8>& input,
201 std::u16string_view sAlgorithm )
203 if (sAlgorithm == u
"SHA1")
209 else if (sAlgorithm == u
"SHA512")
218CryptoHashType cryptoHashTypeFromString(std::u16string_view sAlgorithm)
220 if (sAlgorithm == u
"SHA512")
241 std::vector<sal_uInt8>
const & rSalt,
242 std::vector<sal_uInt8>
const & rBlock,
243 sal_Int32 nCipherBlockSize)
246 aHasher.
update(rSalt.data(), rSalt.size());
247 aHasher.
update(rBlock.data(), rBlock.size());
248 std::vector<sal_uInt8> aIV = aHasher.
finalize();
249 aIV.resize(
roundUp(sal_Int32(aIV.size()), nCipherBlockSize), 0x36);
254 std::vector<sal_uInt8>
const & rBlock,
255 std::vector<sal_uInt8>& rHashFinal,
256 std::vector<sal_uInt8>& rInput,
257 std::vector<sal_uInt8>& rOutput)
260 std::vector<sal_uInt8> dataFinal(
mInfo.
hashSize + rBlock.size(), 0);
261 std::copy(rHashFinal.begin(), rHashFinal.end(), dataFinal.begin());
262 std::copy(rBlock.begin(), rBlock.end(), dataFinal.begin() +
mInfo.
hashSize);
267 std::vector<sal_uInt8> key(keySize, 0);
269 std::copy(hash.begin(), hash.begin() + keySize, key.begin());
272 aDecryptor.
update(rOutput, rInput);
276 std::vector<sal_uInt8>
const & rBlock,
277 std::vector<sal_uInt8> & rHashFinal,
278 std::vector<sal_uInt8> & rInput,
279 std::vector<sal_uInt8> & rOutput)
282 std::vector<sal_uInt8> dataFinal(
mInfo.
hashSize + rBlock.size(), 0);
283 std::copy(rHashFinal.begin(), rHashFinal.end(), dataFinal.begin());
284 std::copy(rBlock.begin(), rBlock.end(), dataFinal.begin() +
mInfo.
hashSize);
289 std::vector<sal_uInt8> key(keySize, 0);
291 std::copy(hash.begin(), hash.begin() + keySize, key.begin());
295 aEncryptor.
update(rOutput, rInput);
308bool generateBytes(std::vector<sal_uInt8> & rBytes, sal_Int32 nSize)
310 size_t nMax = std::min(rBytes.size(),
size_t(nSize));
312 for (
size_t i = 0;
i < nMax; ++
i)
325 size_t encryptedHashValueSize = encryptedHashValue.size();
327 if (nHashValueSize > encryptedHashValueSize)
330 std::vector<sal_uInt8> hashFinal(nHashValueSize, 0);
336 if (nSaltSize < encryptedHashInput.size())
338 std::vector<sal_uInt8> hashInput(nSaltSize, 0);
339 calculateBlock(constBlock1, hashFinal, encryptedHashInput, hashInput);
341 std::vector<sal_uInt8> hashValue(encryptedHashValueSize, 0);
342 calculateBlock(constBlock2, hashFinal, encryptedHashValue, hashValue);
344 std::vector<sal_uInt8> hash(nHashValueSize, 0);
347 return std::equal(hash.begin(), hash.end(), hashValue.begin());
355 mKey.resize(nKeySize, 0);
441 sal_uInt32 totalSize = aInputStream.
readuInt32();
443 std::vector<sal_uInt8> aSizeBytes(
sizeof(sal_uInt32));
445 aCryptoHash.
update(aSizeBytes);
447 aInputStream.
skip(4);
449 std::vector<sal_uInt8> aReserved{0,0,0,0};
450 aCryptoHash.
update(aReserved);
458 sal_uInt32 segment = 0;
460 std::vector<sal_uInt8> saltWithBlockKey(saltSize +
sizeof(segment), 0);
461 std::copy(keyDataSalt.begin(), keyDataSalt.end(), saltWithBlockKey.begin());
464 std::vector<sal_uInt8> iv(keySize, 0);
466 std::vector<sal_uInt8> inputBuffer(constSegmentLength);
467 std::vector<sal_uInt8> outputBuffer(constSegmentLength);
468 sal_uInt32 inputLength;
469 sal_uInt32 outputLength;
470 sal_uInt32 remaining = totalSize;
472 while ((inputLength = aInputStream.
readMemory(inputBuffer.data(), inputBuffer.size())) > 0)
474 auto p = saltWithBlockKey.begin() + saltSize;
475 p[0] = segment & 0xFF;
476 p[1] = (segment >> 8) & 0xFF;
477 p[2] = (segment >> 16) & 0xFF;
478 p[3] = segment >> 24;
483 std::copy(hash.begin(), hash.begin() + keySize, iv.begin());
486 outputLength = aDecryptor.
update(outputBuffer, inputBuffer, inputLength);
488 sal_uInt32 writeLength = std::min(outputLength, remaining);
490 aCryptoHash.
update(inputBuffer, inputLength);
492 aOutputStream.
writeMemory(outputBuffer.data(), writeLength);
494 remaining -= outputLength;
506 std::vector<sal_uInt8> aExpectedReservedBytes(
sizeof(sal_uInt32));
509 uno::Sequence<sal_Int8> aReadReservedBytes(
sizeof(sal_uInt32));
510 rxInputStream->readBytes(aReadReservedBytes, aReadReservedBytes.getLength());
512 if (!std::equal(std::cbegin(aReadReservedBytes), std::cend(aReadReservedBytes), aExpectedReservedBytes.begin()))
521 Reference<XFastDocumentHandler> xFastDocumentHandler(
new AgileDocumentHandler(
mInfo));
522 Reference<XFastTokenHandler> xFastTokenHandler(
new AgileTokenHandler);
526 xParser->setFastDocumentHandler(xFastDocumentHandler);
527 xParser->setTokenHandler(xFastTokenHandler);
529 InputSource aInputSource;
530 aInputSource.aInputStream = rxInputStream;
531 xParser->parseStream(aInputSource);
571 std::vector<sal_uInt8> unencryptedVerifierHashInput(
mInfo.
saltSize);
572 if (!generateBytes(unencryptedVerifierHashInput,
mInfo.
saltSize))
577 std::vector<sal_uInt8> unencryptedVerifierHashValue;
578 if (!hashCalc(unencryptedVerifierHashValue, unencryptedVerifierHashInput,
mInfo.
hashAlgorithm))
580 unencryptedVerifierHashValue.resize(nVerifierHash, 0);
606 extendedSalt.resize(nEncryptedSaltSize, 0x36);
637 extendedHash.resize(nEncryptedValueSize, 0x36);
662 mKey.resize(nKeySize, 0);
667 if (!generateBytes(
mKey, nKeySize))
683 setupEncryptionParameters({ 100000, 16, 256, 64, 16, OUString(
"AES"), OUString(
"ChainingModeCBC"), OUString(
"SHA512") });
729 aXmlWriter.
startElement(
"",
"encryption",
"http://schemas.microsoft.com/office/2006/encryption");
730 aXmlWriter.
attribute(
"xmlns:p", OString(
"http://schemas.microsoft.com/office/2006/keyEncryptor/password"));
750 aXmlWriter.
attribute(
"uri", OString(
"http://schemas.microsoft.com/office/2006/keyEncryptor/password"));
777 css::uno::Reference<css::io::XOutputStream> & rxOutputStream,
785 std::vector<sal_uInt8> aSizeBytes(
sizeof(sal_uInt32));
787 aBinaryOutputStream.
writeMemory(aSizeBytes.data(), aSizeBytes.size());
788 aCryptoHash.
update(aSizeBytes, aSizeBytes.size());
790 std::vector<sal_uInt8> aNull{0,0,0,0};
791 aBinaryOutputStream.
writeMemory(aNull.data(), aNull.size());
792 aCryptoHash.
update(aNull, aNull.size());
799 sal_uInt32 nSegment = 0;
800 sal_uInt32 nSegmentByteSize =
sizeof(nSegment);
802 std::vector<sal_uInt8> saltWithBlockKey(saltSize + nSegmentByteSize, 0);
803 std::copy(keyDataSalt.begin(), keyDataSalt.end(), saltWithBlockKey.begin());
806 std::vector<sal_uInt8> iv(keySize, 0);
808 std::vector<sal_uInt8> inputBuffer(constSegmentLength);
809 std::vector<sal_uInt8> outputBuffer(constSegmentLength);
810 sal_uInt32 inputLength;
811 sal_uInt32 outputLength;
813 while ((inputLength = aBinaryInputStream.
readMemory(inputBuffer.data(), inputBuffer.size())) > 0)
815 sal_uInt32 correctedInputLength = inputLength %
mInfo.
blockSize == 0 ?
819 auto p = saltWithBlockKey.begin() + saltSize;
820 p[0] = nSegment & 0xFF;
821 p[1] = (nSegment >> 8) & 0xFF;
822 p[2] = (nSegment >> 16) & 0xFF;
823 p[3] = nSegment >> 24;
828 std::copy(hash.begin(), hash.begin() + keySize, iv.begin());
831 outputLength = aEncryptor.
update(outputBuffer, inputBuffer, correctedInputLength);
832 aBinaryOutputStream.
writeMemory(outputBuffer.data(), outputLength);
833 aCryptoHash.
update(outputBuffer, outputLength);
AgileEncryptionInfo & mInfo
static void decode(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
static std::vector< unsigned char > GetOoxHashAsVector(const OUString &rPassword, const std::vector< unsigned char > &rSaltValue, sal_uInt32 nSpinCount, comphelper::Hash::IterCount eIterCount, std::u16string_view rAlgorithmName)
std::vector< unsigned char > finalize()
static std::vector< unsigned char > calculateHash(const unsigned char *pInput, size_t length, HashType eType)
void update(const unsigned char *pInput, size_t length)
BinaryOutputStream & WriteUInt32(sal_uInt32 x)
Wraps a UNO output stream and provides convenient access functions.
virtual void writeMemory(const void *pMem, sal_Int32 nBytes, size_t nAtomSize=1) override
Write nBytes bytes from the (preallocated!) buffer pMem.
static void writeLittleEndian(void *pDstBuffer, Type nValue)
Writes a value to memory, while converting it to little-endian.
void decryptEncryptionKey(OUString const &rPassword)
bool decryptAndCheckVerifierHash(OUString const &rPassword)
void setupEncryptionParameters(AgileEncryptionParameters const &rAgileEncryptionParameters)
AgileEncryptionPreset meEncryptionPreset
bool checkDataIntegrity() override
void writeEncryptionInfo(BinaryXOutputStream &rStream) override
void calculateBlock(std::vector< sal_uInt8 > const &rBlock, std::vector< sal_uInt8 > &rHashFinal, std::vector< sal_uInt8 > &rInput, std::vector< sal_uInt8 > &rOutput)
bool encryptEncryptionKey(OUString const &rPassword)
bool generateAndEncryptVerifierHash(OUString const &rPassword)
bool setupEncryptionKey(OUString const &rPassword)
void encryptBlock(std::vector< sal_uInt8 > const &rBlock, std::vector< sal_uInt8 > &rHashFinal, std::vector< sal_uInt8 > &rInput, std::vector< sal_uInt8 > &rOutput)
void calculateHashFinal(const OUString &rPassword, std::vector< sal_uInt8 > &aHashFinal)
bool setupEncryption(OUString const &rPassword) override
void encrypt(const css::uno::Reference< css::io::XInputStream > &rxInputStream, css::uno::Reference< css::io::XOutputStream > &rxOutputStream, sal_uInt32 nSize) override
bool readEncryptionInfo(css::uno::Reference< css::io::XInputStream > &rxInputStream) override
bool generateEncryptionKey(OUString const &rPassword) override
bool decrypt(BinaryXInputStream &aInputStream, BinaryXOutputStream &aOutputStream) override
static Crypto::CryptoType cryptoType(const AgileEncryptionInfo &rInfo)
AgileEncryptionInfo mInfo
std::vector< sal_uInt8 > mKey
std::vector< sal_uInt8 > finalize()
bool update(std::vector< sal_uInt8 > &rInput, sal_uInt32 nInputLength=0)
sal_uInt32 update(std::vector< sal_uInt8 > &output, std::vector< sal_uInt8 > &input, sal_uInt32 inputLength=0)
sal_uInt32 update(std::vector< sal_uInt8 > &output, std::vector< sal_uInt8 > &input, sal_uInt32 inputLength=0)
static bool convertNumber(sal_Int32 &rValue, std::u16string_view aString, sal_Int32 nMin=SAL_MIN_INT32, sal_Int32 nMax=SAL_MAX_INT32)
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
const sal_uInt32 SHA512_HASH_LENGTH
Reference< XComponentContext > getProcessComponentContext()
const sal_uInt32 SHA1_HASH_LENGTH
const sal_uInt32 AGILE_ENCRYPTION_RESERVED
const sal_uInt32 VERSION_INFO_AGILE
T roundUp(T input, T multiple)
Rounds up the input to the nearest multiple.
static std::vector< sal_uInt8 > calculateIV(comphelper::HashType eType, std::vector< sal_uInt8 > const &rSalt, std::vector< sal_uInt8 > const &rBlock, sal_Int32 nCipherBlockSize)
std::vector< sal_uInt8 > hmacEncryptedKey
std::vector< sal_uInt8 > hmacCalculatedHash
std::vector< sal_uInt8 > hmacKey
std::vector< sal_uInt8 > encryptedVerifierHashInput
std::vector< sal_uInt8 > keyDataSalt
std::vector< sal_uInt8 > encryptedKeyValue
std::vector< sal_uInt8 > saltValue
std::vector< sal_uInt8 > encryptedVerifierHashValue
std::vector< sal_uInt8 > hmacEncryptedValue
std::vector< sal_uInt8 > hmacHash