1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SC_INC_TABPROTECTION_HXX
21 #define INCLUDED_SC_INC_TABPROTECTION_HXX
23 #include <sal/types.h>
26 #include "rangelst.hxx"
30 class ScTableProtectionImpl
;
35 PASSHASH_SHA1_UTF8
, // tdf#115483 this is UTF8, previous one is wrong UTF16
41 /// OOXML password definitions: algorithmName, hashValue, saltValue, spinCount
42 struct ScOoxPasswordHash
44 OUString maAlgorithmName
; /// "SHA-512", ...
45 OUString maHashValue
; /// base64 encoded hash value
46 OUString maSaltValue
; /// base64 encoded salt value
47 sal_uInt32 mnSpinCount
; /// spin count, iteration runs
49 ScOoxPasswordHash() : mnSpinCount(0) {}
50 bool hasPassword() const { return !maHashValue
.isEmpty(); }
53 // Keep algorithm and spin count.
57 bool verifyPassword( const OUString
& aPassText
) const;
60 namespace ScPassHashHelper
62 /** Check for the compatibility of all password hashes. If there is at
63 * least one hash that needs to be regenerated, it returns true. If all
64 * hash values are compatible with the specified hash type, then it
66 bool needsPassHashRegen(const ScDocument
& rDoc
, ScPasswordHash eHash1
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
);
68 OUString
getHashURI(ScPasswordHash eHash
);
70 ScPasswordHash
getHashTypeFromURI(const OUString
& rURI
);
73 class SAL_NO_VTABLE ScPassHashProtectable
76 virtual ~ScPassHashProtectable() = 0;
78 virtual bool isProtected() const = 0;
79 virtual bool isProtectedWithPass() const = 0;
80 virtual void setProtected(bool bProtected
) = 0;
82 virtual bool isPasswordEmpty() const = 0;
83 virtual bool hasPasswordHash(ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) const = 0;
84 virtual void setPassword(const OUString
& aPassText
) = 0;
85 virtual css::uno::Sequence
<sal_Int8
> getPasswordHash(
86 ScPasswordHash eHash
, ScPasswordHash eHas2
= PASSHASH_UNSPECIFIED
) const = 0;
87 virtual const ScOoxPasswordHash
& getPasswordHash() const = 0;
88 virtual void setPasswordHash(
89 const css::uno::Sequence
<sal_Int8
>& aPassword
,
90 ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) = 0;
91 virtual void setPasswordHash( const OUString
& rAlgorithmName
, const OUString
& rHashValue
,
92 const OUString
& rSaltValue
, sal_uInt32 nSpinCount
) = 0;
93 virtual bool verifyPassword(const OUString
& aPassText
) const = 0;
96 class SC_DLLPUBLIC ScDocProtection final
: public ScPassHashProtectable
103 NONE
///< last item - used to resize the vector
106 explicit ScDocProtection();
107 explicit ScDocProtection(const ScDocProtection
& r
);
108 virtual ~ScDocProtection() override
;
110 virtual bool isProtected() const override
;
111 virtual bool isProtectedWithPass() const override
;
112 virtual void setProtected(bool bProtected
) override
;
114 virtual bool isPasswordEmpty() const override
;
115 virtual bool hasPasswordHash(ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) const override
;
116 virtual void setPassword(const OUString
& aPassText
) override
;
117 virtual css::uno::Sequence
<sal_Int8
> getPasswordHash(
118 ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) const override
;
119 virtual const ScOoxPasswordHash
& getPasswordHash() const override
;
120 virtual void setPasswordHash(
121 const css::uno::Sequence
<sal_Int8
>& aPassword
,
122 ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) override
;
123 virtual void setPasswordHash( const OUString
& rAlgorithmName
, const OUString
& rHashValue
,
124 const OUString
& rSaltValue
, sal_uInt32 nSpinCount
) override
;
125 virtual bool verifyPassword(const OUString
& aPassText
) const override
;
127 bool isOptionEnabled(Option eOption
) const;
128 void setOption(Option eOption
, bool bEnabled
);
131 std::unique_ptr
<ScTableProtectionImpl
> mpImpl
;
134 /** Container for the Excel EnhancedProtection feature.
136 struct ScEnhancedProtection
138 ScRangeListRef maRangeList
;
139 sal_uInt32 mnAreserved
;
140 sal_uInt32 mnPasswordVerifier
;
142 ::std::vector
< sal_uInt8
> maSecurityDescriptor
; // imported as raw BIFF data
143 OUString maSecurityDescriptorXML
; // imported from OOXML
144 ScOoxPasswordHash maPasswordHash
;
146 ScEnhancedProtection() : mnAreserved(0), mnPasswordVerifier(0) {}
148 bool hasSecurityDescriptor() const
150 return !maSecurityDescriptor
.empty() || !maSecurityDescriptorXML
.isEmpty();
153 bool hasPassword() const
155 return mnPasswordVerifier
!= 0 || maPasswordHash
.hasPassword();
159 /** sheet protection state container
161 * This class stores sheet's protection state: 1) whether the protection
162 * is on, 2) password and/or password hash, and 3) any associated
163 * protection options. This class is also used as a protection state
164 * container for the undo/redo stack, in which case the password, hash and
165 * the options need to be preserved even when the protection flag is
167 class SC_DLLPUBLIC ScTableProtection final
: public ScPassHashProtectable
185 SELECT_UNLOCKED_CELLS
,
187 NONE
///< last item - used to resize the vector
190 explicit ScTableProtection();
191 explicit ScTableProtection(const ScTableProtection
& r
);
192 virtual ~ScTableProtection() override
;
194 virtual bool isProtected() const override
;
195 virtual bool isProtectedWithPass() const override
;
196 virtual void setProtected(bool bProtected
) override
;
198 virtual bool isPasswordEmpty() const override
;
199 virtual bool hasPasswordHash(ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) const override
;
200 virtual void setPassword(const OUString
& aPassText
) override
;
201 virtual css::uno::Sequence
<sal_Int8
> getPasswordHash(
202 ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) const override
;
203 virtual const ScOoxPasswordHash
& getPasswordHash() const override
;
204 virtual void setPasswordHash(
205 const css::uno::Sequence
<sal_Int8
>& aPassword
,
206 ScPasswordHash eHash
, ScPasswordHash eHash2
= PASSHASH_UNSPECIFIED
) override
;
207 virtual void setPasswordHash( const OUString
& rAlgorithmName
, const OUString
& rHashValue
,
208 const OUString
& rSaltValue
, sal_uInt32 nSpinCount
) override
;
209 virtual bool verifyPassword(const OUString
& aPassText
) const override
;
211 bool isOptionEnabled(Option eOption
) const;
212 void setOption(Option eOption
, bool bEnabled
);
214 void setEnhancedProtection( const ::std::vector
< ScEnhancedProtection
> & rProt
);
215 const ::std::vector
< ScEnhancedProtection
> & getEnhancedProtection() const;
216 bool updateReference( UpdateRefMode
, const ScDocument
*, const ScRange
& rWhere
, SCCOL nDx
, SCROW nDy
, SCTAB nDz
);
217 bool isBlockEditable( const ScRange
& rRange
) const;
218 bool isSelectionEditable( const ScRangeList
& rRangeList
) const;
221 std::unique_ptr
<ScTableProtectionImpl
> mpImpl
;
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */