25#include <osl/diagnose.h>
26#include <document.hxx>
30#define DEBUG_TAB_PROTECTION 0
32constexpr OUStringLiteral
URI_SHA1 =
u"http://www.w3.org/2000/09/xmldsig#sha1";
34constexpr OUStringLiteral
URI_SHA256_W3C =
u"http://www.w3.org/2001/04/xmlenc#sha256";
35constexpr OUStringLiteral
URI_XLS_LEGACY =
u"http://docs.oasis-open.org/office/ns/table/legacy-hash-excel";
38using ::com::sun::star::uno::Sequence;
46 if (!
p->isPasswordEmpty() && !
p->hasPasswordHash(eHash1, eHash2))
51 for (
SCTAB i = 0;
i < nTabCount; ++
i)
54 if (!
p || !
p->isProtected())
58 if (!
p->isPasswordEmpty() && !
p->hasPasswordHash(eHash1, eHash2))
131 const css::uno::Sequence<sal_Int8>& aPassword,
133 void setPasswordHash(
const OUString& rAlgorithmName,
const OUString& rHashValue,
134 const OUString& rSaltValue, sal_uInt32 nSpinCount );
160 Sequence<sal_Int8> aHash;
190 auto aChars = comphelper::sequenceToContainer<vector<char>>(rPassHash);
192 Sequence<sal_Int8> aNewHash;
210 maPassText(r.maPassText),
211 maPassHash(r.maPassHash),
212 maOptions(r.maOptions),
213 mbEmptyPass(r.mbEmptyPass),
214 mbProtected(r.mbProtected),
217 maPasswordHash(r.maPasswordHash),
218 maEnhancedProtection(r.maEnhancedProtection)
276 Sequence<sal_Int8> aPassHash;
310 return Sequence<sal_Int8>();
321 sal_Int32 nLen = aPassword.getLength();
327#if DEBUG_TAB_PROTECTION
329 printf(
"%2.2X ",
static_cast<sal_uInt8>(n));
335 const OUString& rSaltValue, sal_uInt32 nSpinCount )
337 if (!rHashValue.isEmpty())
359#if DEBUG_TAB_PROTECTION
360 fprintf(stdout,
"ScTableProtectionImpl::verifyPassword: input = '%s'\n",
365 return aPassText.isEmpty();
378#if DEBUG_TAB_PROTECTION
379 fprintf(stdout,
"ScTableProtectionImpl::verifyPassword: hash = ");
380 for (sal_Int32
i = 0;
i < aHash.getLength(); ++
i)
381 printf(
"%2.2X ",
static_cast<sal_uInt8>(aHash[
i]));
406 if (
maOptions.size() <=
static_cast<size_t>(nOptId) )
408 OSL_FAIL(
"ScTableProtectionImpl::isOptionEnabled: wrong size");
417 if (
maOptions.size() <=
static_cast<size_t>(nOptId) )
419 OSL_FAIL(
"ScTableProtectionImpl::setOption: wrong size");
434 bool bChanged =
false;
437 if (rEnhancedProtection.maRangeList.is())
438 bChanged |= rEnhancedProtection.maRangeList->UpdateReference(
eMode, &rDoc, rWhere, nDx, nDy, nDz);
460 return !rEnhancedProtection.hasSecurityDescriptor()
461 && rEnhancedProtection.maRangeList.is() && rEnhancedProtection.maRangeList->Contains( rRange)
462 && !rEnhancedProtection.hasPassword();
475 if (!rEnhancedProtection.hasSecurityDescriptor() && rEnhancedProtection.maRangeList.is())
477 ScRangeList aList( rEnhancedProtection.maRangeList->GetIntersectedRange( rRange));
478 if (aList.
size() == 1 && aList[0] == rRange)
481 if (!rEnhancedProtection.hasPassword())
495 if (!rEnhancedProtection.hasSecurityDescriptor() && rEnhancedProtection.maRangeList.is())
498 if (!rEnhancedProtection.hasPassword())
500 const ScRangeList& rRanges = *rEnhancedProtection.maRangeList;
501 size_t nRanges = rRanges.
size();
502 for (
size_t i=0;
i < nRanges; ++
i)
510 return aResultList.
size() == 1 && aResultList[0] == rRange;
515 if (rRangeList.
empty())
518 for (
size_t i=0, nRanges = rRangeList.
size();
i < nRanges; ++
i)
543 return mpImpl->isProtected();
548 return mpImpl->isProtectedWithPass();
553 mpImpl->setProtected(bProtected);
563 return mpImpl->isPasswordEmpty();
568 return mpImpl->hasPasswordHash(eHash, eHash2);
573 mpImpl->setPassword(aPassText);
578 return mpImpl->getPasswordHash(eHash, eHash2);
583 return mpImpl->getPasswordHash();
589 mpImpl->setPasswordHash(aPassword, eHash, eHash2);
593 const OUString& rSaltValue, sal_uInt32 nSpinCount )
595 mpImpl->setPasswordHash( rAlgorithmName, rHashValue, rSaltValue, nSpinCount);
600 return mpImpl->verifyPassword(aPassText);
605 return mpImpl->isOptionEnabled(eOption);
610 mpImpl->setOption(eOption, bEnabled);
633 return mpImpl->isProtected();
638 return mpImpl->isProtectedWithPass();
643 mpImpl->setProtected(bProtected);
648 return mpImpl->isPasswordEmpty();
653 return mpImpl->hasPasswordHash(eHash, eHash2);
658 mpImpl->setPassword(aPassText);
663 return mpImpl->getPasswordHash(eHash, eHash2);
668 return mpImpl->getPasswordHash();
674 mpImpl->setPasswordHash(aPassword, eHash, eHash2);
678 const OUString& rSaltValue, sal_uInt32 nSpinCount )
680 mpImpl->setPasswordHash( rAlgorithmName, rHashValue, rSaltValue, nSpinCount);
685 return mpImpl->verifyPassword(aPassText);
690 return mpImpl->isOptionEnabled(eOption);
695 mpImpl->setOption(eOption, bEnabled);
700 mpImpl->setEnhancedProtection(std::move(rProt));
705 return mpImpl->getEnhancedProtection();
711 return mpImpl->updateReference(
eMode, rDoc, rWhere, nDx, nDy, nDz);
716 return mpImpl->isBlockEditable( rRange);
721 return mpImpl->isSelectionEditable( rRangeList);
size_t SCSIZE
size_t typedef to be able to find places where code was changed from USHORT to size_t and is used to ...
virtual void setPasswordHash(const css::uno::Sequence< sal_Int8 > &aPassword, ScPasswordHash eHash, ScPasswordHash eHash2=PASSHASH_UNSPECIFIED) override
virtual bool verifyPassword(const OUString &aPassText) const override
virtual const ScOoxPasswordHash & getPasswordHash() const override
virtual bool isPasswordEmpty() const override
virtual ~ScDocProtection() override
virtual void setPassword(const OUString &aPassText) override
virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2=PASSHASH_UNSPECIFIED) const override
void setOption(Option eOption, bool bEnabled)
virtual bool isProtected() const override
virtual bool isProtectedWithPass() const override
virtual void setProtected(bool bProtected) override
std::unique_ptr< ScTableProtectionImpl > mpImpl
bool isOptionEnabled(Option eOption) const
SC_DLLPUBLIC const ScTableProtection * GetTabProtection(SCTAB nTab) const
SC_DLLPUBLIC bool IsDocProtected() const
SC_DLLPUBLIC ScDocProtection * GetDocProtection() const
SC_DLLPUBLIC SCTAB GetTableCount() const
virtual ~ScPassHashProtectable()=0
ScRangeList GetIntersectedRange(const ScRange &rRange) const
void push_back(const ScRange &rRange)
void setPassword(const OUString &aPassText)
bool isOptionEnabled(SCSIZE nOptId) const
bool verifyPassword(const OUString &aPassText) const
bool updateReference(UpdateRefMode, const ScDocument &, const ScRange &rWhere, SCCOL nDx, SCROW nDy, SCTAB nDz)
ScTableProtectionImpl(SCSIZE nOptSize)
::std::vector< ScEnhancedProtection > maEnhancedProtection
bool isProtectedWithPass() const
bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2) const
bool isPasswordEmpty() const
const ::std::vector< ScEnhancedProtection > & getEnhancedProtection() const
bool isBlockEditable(const ScRange &rRange) const
const ScOoxPasswordHash & getPasswordHash() const
static Sequence< sal_Int8 > hashPassword(std::u16string_view aPassText, ScPasswordHash eHash)
bool isSelectionEditable(const ScRangeList &rRangeList) const
::std::vector< bool > maOptions
ScOoxPasswordHash maPasswordHash
void setProtected(bool bProtected)
void setOption(SCSIZE nOptId, bool bEnabled)
void setEnhancedProtection(::std::vector< ScEnhancedProtection > &&rProt)
void setPasswordHash(const css::uno::Sequence< sal_Int8 > &aPassword, ScPasswordHash eHash, ScPasswordHash eHash2)
css::uno::Sequence< sal_Int8 > maPassHash
sheet protection state container
virtual bool isProtectedWithPass() const override
virtual bool verifyPassword(const OUString &aPassText) const override
std::unique_ptr< ScTableProtectionImpl > mpImpl
bool isOptionEnabled(Option eOption) const
virtual bool hasPasswordHash(ScPasswordHash eHash, ScPasswordHash eHash2=PASSHASH_UNSPECIFIED) const override
void setOption(Option eOption, bool bEnabled)
virtual void setPassword(const OUString &aPassText) override
virtual ~ScTableProtection() override
virtual bool isPasswordEmpty() const override
void setEnhancedProtection(::std::vector< ScEnhancedProtection > &&rProt)
virtual const ScOoxPasswordHash & getPasswordHash() const override
virtual bool isProtected() const override
virtual void setPasswordHash(const css::uno::Sequence< sal_Int8 > &aPassword, ScPasswordHash eHash, ScPasswordHash eHash2=PASSHASH_UNSPECIFIED) override
const ::std::vector< ScEnhancedProtection > & getEnhancedProtection() const
bool isBlockEditable(const ScRange &rRange) const
bool isSelectionEditable(const ScRangeList &rRangeList) const
virtual void setProtected(bool bProtected) override
bool updateReference(UpdateRefMode, const ScDocument &, const ScRange &rWhere, SCCOL nDx, SCROW nDy, SCTAB nDz)
static SVL_DLLPUBLIC void GetHashPassword(css::uno::Sequence< sal_Int8 > &rPassHash, const char *pPass, sal_uInt32 nLen)
static SVL_DLLPUBLIC void GetHashPasswordSHA1UTF8(css::uno::Sequence< sal_Int8 > &rPassHash, std::u16string_view sPass)
static SVL_DLLPUBLIC void GetHashPasswordSHA256(css::uno::Sequence< sal_Int8 > &rPassHash, std::u16string_view sPass)
static css::uno::Sequence< sal_Int8 > GetXLHashAsSequence(std::u16string_view aString)
static OUString GetOoxHashAsBase64(const OUString &rPassword, std::u16string_view rSaltValue, sal_uInt32 nSpinCount, comphelper::Hash::IterCount eIterCount, std::u16string_view rAlgorithmName)
ScPasswordHash getHashTypeFromURI(std::u16string_view rURI)
bool needsPassHashRegen(const ScDocument &rDoc, ScPasswordHash eHash1, ScPasswordHash eHash2=PASSHASH_UNSPECIFIED)
Check for the compatibility of all password hashes.
OUString getHashURI(ScPasswordHash eHash)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Container for the Excel EnhancedProtection feature.
OOXML password definitions: algorithmName, hashValue, saltValue, spinCount.
OUString maSaltValue
base64 encoded hash value
bool verifyPassword(const OUString &aPassText) const
OUString maHashValue
"SHA-512", ...
sal_uInt32 mnSpinCount
base64 encoded salt value
constexpr OUStringLiteral URI_SHA256_ODF12
constexpr OUStringLiteral URI_XLS_LEGACY
constexpr OUStringLiteral URI_SHA256_W3C
constexpr OUStringLiteral URI_SHA1