LibreOffice Module comphelper (master) 1
Static Public Member Functions | Private Member Functions | List of all members
comphelper::DocPasswordHelper Class Reference

Helper that asks for a document password and checks its validity. More...

#include <docpasswordhelper.hxx>

Static Public Member Functions

static css::uno::Sequence< css::beans::PropertyValue > GenerateNewModifyPasswordInfo (std::u16string_view aPassword)
 This helper function generates the information related to "Password to modify" provided by user. More...
 
static css::uno::Sequence< css::beans::PropertyValue > ConvertPasswordInfo (const css::uno::Sequence< css::beans::PropertyValue > &aInfo)
 This helper function converts a grab-bagged password, e.g. More...
 
static css::uno::Sequence< css::beans::PropertyValue > GenerateNewModifyPasswordInfoOOXML (std::u16string_view aPassword)
 
static bool IsModifyPasswordCorrect (std::u16string_view aPassword, const css::uno::Sequence< css::beans::PropertyValue > &aInfo)
 This helper function allows to check whether the "Password to modify" provided by user is the correct one. More...
 
static sal_uInt32 GetWordHashAsUINT32 (std::u16string_view aString)
 This helper function generates the hash code based on the algorithm specified by MS for "Password to modify" feature of Word. More...
 
static sal_uInt16 GetXLHashAsUINT16 (std::u16string_view aString, rtl_TextEncoding nEnc=RTL_TEXTENCODING_UTF8)
 This helper function generates the hash code based on the algorithm specified by MS for "Password to modify" and passwords related to table protection of Excel. More...
 
static css::uno::Sequence< sal_Int8GetXLHashAsSequence (std::u16string_view aString)
 This helper function generates the hash code based on the algorithm specified by MS for "Password to modify" and passwords related to table protection. More...
 
static css::uno::Sequence< sal_Int8GetOoxHashAsSequence (const OUString &rPassword, std::u16string_view rSaltValue, sal_uInt32 nSpinCount, comphelper::Hash::IterCount eIterCount, std::u16string_view rAlgorithmName)
 Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the OOXML sheetProtection and fileSharing elements, or https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 for Standard and Agile Encryption. More...
 
static OUString GetOoxHashAsBase64 (const OUString &rPassword, std::u16string_view rSaltValue, sal_uInt32 nSpinCount, comphelper::Hash::IterCount eIterCount, std::u16string_view rAlgorithmName)
 Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the OOXML sheetProtection and fileSharing elements, or https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 for Standard and Agile Encryption. More...
 
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)
 Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the OOXML sheetProtection and fileSharing elements, or https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 for Standard and Agile Encryption. More...
 
static css::uno::Sequence< sal_Int8GenerateRandomByteSequence (sal_Int32 nLength)
 This helper function generates a random sequence of bytes of requested length. More...
 
static css::uno::Sequence< sal_Int8GenerateStd97Key (std::u16string_view aPassword, const css::uno::Sequence< sal_Int8 > &aDocId)
 This helper function generates a byte sequence representing the key digest value used by MSCodec_Std97 codec. More...
 
static css::uno::Sequence< sal_Int8GenerateStd97Key (const sal_uInt16 pPassData[16], const css::uno::Sequence< sal_Int8 > &aDocId)
 This helper function generates a byte sequence representing the key digest value used by MSCodec_Std97 codec. More...
 
static css::uno::Sequence< sal_Int8GenerateStd97Key (const sal_uInt16 pPassData[16], const sal_uInt8 pDocId[16])
 This helper function generates a byte sequence representing the key digest value used by MSCodec_Std97 codec. More...
 
static css::uno::Sequence< css::beans::NamedValue > requestAndVerifyDocPassword (IDocPasswordVerifier &rVerifier, const css::uno::Sequence< css::beans::NamedValue > &rMediaEncData, const OUString &rMediaPassword, const css::uno::Reference< css::task::XInteractionHandler > &rxInteractHandler, const OUString &rDocumentUrl, DocPasswordRequestType eRequestType, const ::std::vector< OUString > *pDefaultPasswords=nullptr, bool *pbIsDefaultPassword=nullptr)
 This helper function tries to request and verify a password to load a protected document. More...
 
static css::uno::Sequence< css::beans::NamedValue > decryptGpgSession (const css::uno::Sequence< css::uno::Sequence< css::beans::NamedValue > > &rGpgProperties)
 

Private Member Functions

 ~DocPasswordHelper ()
 

Detailed Description

Helper that asks for a document password and checks its validity.

Definition at line 96 of file docpasswordhelper.hxx.

Constructor & Destructor Documentation

◆ ~DocPasswordHelper()

comphelper::DocPasswordHelper::~DocPasswordHelper ( )
private

Member Function Documentation

◆ ConvertPasswordInfo()

uno::Sequence< beans::PropertyValue > comphelper::DocPasswordHelper::ConvertPasswordInfo ( const css::uno::Sequence< css::beans::PropertyValue > &  aInfo)
static

This helper function converts a grab-bagged password, e.g.

the trackChanges password which has no complete inner equivalent to the inner format. The result sequence contains the hash and the algorithm-related info to use e.g. in IsModifyPasswordCorrect().

Parameters
aInfoThe sequence containing the hash and the algorithm-related info according to the OOXML origin, used by grab-bagging.
Returns
The sequence containing the hash and the algorithm-related info in the inner format.

Definition at line 139 of file docpasswordhelper.cxx.

References comphelper::makePropertyValue(), nCount, and sHash.

◆ decryptGpgSession()

uno::Sequence< css::beans::NamedValue > comphelper::DocPasswordHelper::decryptGpgSession ( const css::uno::Sequence< css::uno::Sequence< css::beans::NamedValue > > &  rGpgProperties)
static

Definition at line 656 of file docpasswordhelper.cxx.

References ctx, err, i, nLength, PACKAGE_ENCRYPTIONDATA_SHA256UTF8, pValues, result, and SAL_INFO.

◆ GenerateNewModifyPasswordInfo()

uno::Sequence< beans::PropertyValue > comphelper::DocPasswordHelper::GenerateNewModifyPasswordInfo ( std::u16string_view  aPassword)
static

This helper function generates the information related to "Password to modify" provided by user.

The result sequence contains the hash and the algorithm-related info.

Parameters
aStringThe string for which the info should be generated
Returns
The sequence containing the hash and the algorithm-related info

Definition at line 88 of file docpasswordhelper.cxx.

References comphelper::GeneratePBKDF2Hash(), GenerateRandomByteSequence(), and comphelper::makePropertyValue().

◆ GenerateNewModifyPasswordInfoOOXML()

uno::Sequence< beans::PropertyValue > comphelper::DocPasswordHelper::GenerateNewModifyPasswordInfoOOXML ( std::u16string_view  aPassword)
static

◆ GenerateRandomByteSequence()

uno::Sequence< sal_Int8 > comphelper::DocPasswordHelper::GenerateRandomByteSequence ( sal_Int32  nLength)
static

This helper function generates a random sequence of bytes of requested length.

Definition at line 423 of file docpasswordhelper.cxx.

References nLength.

Referenced by GenerateNewModifyPasswordInfo(), GenerateNewModifyPasswordInfoOOXML(), and requestAndVerifyDocPassword().

◆ GenerateStd97Key() [1/3]

static css::uno::Sequence< sal_Int8 > comphelper::DocPasswordHelper::GenerateStd97Key ( const sal_uInt16  pPassData[16],
const css::uno::Sequence< sal_Int8 > &  aDocId 
)
static

This helper function generates a byte sequence representing the key digest value used by MSCodec_Std97 codec.

◆ GenerateStd97Key() [2/3]

uno::Sequence< sal_Int8 > comphelper::DocPasswordHelper::GenerateStd97Key ( const sal_uInt16  pPassData[16],
const sal_uInt8  pDocId[16] 
)
static

This helper function generates a byte sequence representing the key digest value used by MSCodec_Std97 codec.

Definition at line 463 of file docpasswordhelper.cxx.

◆ GenerateStd97Key() [3/3]

static css::uno::Sequence< sal_Int8 > comphelper::DocPasswordHelper::GenerateStd97Key ( std::u16string_view  aPassword,
const css::uno::Sequence< sal_Int8 > &  aDocId 
)
static

This helper function generates a byte sequence representing the key digest value used by MSCodec_Std97 codec.

Referenced by requestAndVerifyDocPassword().

◆ GetOoxHashAsBase64()

OUString comphelper::DocPasswordHelper::GetOoxHashAsBase64 ( const OUString &  rPassword,
std::u16string_view  rSaltValue,
sal_uInt32  nSpinCount,
comphelper::Hash::IterCount  eIterCount,
std::u16string_view  rAlgorithmName 
)
static

Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the OOXML sheetProtection and fileSharing elements, or https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 for Standard and Agile Encryption.

Parameters
rPasswordUTF-16 encoded string without leading BOM character
rSaltValueBase64 encoded salt that will be decoded and prepended to password data.
nSpinCountIf >0 the number of repeated iterations.
eIterCountIf Hash::IterCount::APPEND, append iteration count as per https://msdn.microsoft.com/en-us/library/dd920692 If Hash::IterCount::PREPEND, prepend iteration count as per https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 If Hash::IterCount::NONE, do not add the iteration count to hash iterations.
rAlgorithmNameOne of "SHA-512", "SHA-256", ... as listed for AlgorithmName in https://msdn.microsoft.com/en-us/library/dd920692 or "SHA512", "SHA256", ... as listed for HashAlgorithm in https://msdn.microsoft.com/en-us/library/dd925810 that have a valid match in comphelper::HashType. If not, an empty sequence is returned. Not all algorithm names are supported.
Returns
the base64 encoded string of the hash value, that can be compared against a stored base64 encoded hash value.

Definition at line 407 of file docpasswordhelper.cxx.

References aBuf, aSeq, comphelper::Base64::encode(), and GetOoxHashAsSequence().

Referenced by GenerateNewModifyPasswordInfoOOXML(), and IsModifyPasswordCorrect().

◆ GetOoxHashAsSequence()

css::uno::Sequence< sal_Int8 > comphelper::DocPasswordHelper::GetOoxHashAsSequence ( const OUString &  rPassword,
std::u16string_view  rSaltValue,
sal_uInt32  nSpinCount,
comphelper::Hash::IterCount  eIterCount,
std::u16string_view  rAlgorithmName 
)
static

Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the OOXML sheetProtection and fileSharing elements, or https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 for Standard and Agile Encryption.

Parameters
rPasswordUTF-16 encoded string without leading BOM character
rSaltValueBase64 encoded salt that will be decoded and prepended to password data.
nSpinCountIf >0 the number of repeated iterations.
eIterCountIf Hash::IterCount::APPEND, append iteration count as per https://msdn.microsoft.com/en-us/library/dd920692 If Hash::IterCount::PREPEND, prepend iteration count as per https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 If Hash::IterCount::NONE, do not add the iteration count to hash iterations.
rAlgorithmNameOne of "SHA-512", "SHA-256", ... as listed for AlgorithmName in https://msdn.microsoft.com/en-us/library/dd920692 or "SHA512", "SHA256", ... as listed for HashAlgorithm in https://msdn.microsoft.com/en-us/library/dd925810 that have a valid match in comphelper::HashType. If not, an empty sequence is returned. Not all algorithm names are supported.
Returns
the raw hash value as sal_Int8 sequence.

Definition at line 387 of file docpasswordhelper.cxx.

References comphelper::Base64::decode(), and GetOoxHashAsVector().

Referenced by GetOoxHashAsBase64().

◆ GetOoxHashAsVector()

std::vector< unsigned char > comphelper::DocPasswordHelper::GetOoxHashAsVector ( const OUString &  rPassword,
const std::vector< unsigned char > &  rSaltValue,
sal_uInt32  nSpinCount,
comphelper::Hash::IterCount  eIterCount,
std::u16string_view  rAlgorithmName 
)
static

Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the OOXML sheetProtection and fileSharing elements, or https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 for Standard and Agile Encryption.

Parameters
rPasswordUTF-16 encoded string without leading BOM character
rSaltValueA raw salt that will be prepended to password data.
nSpinCountIf >0 the number of repeated iterations.
eIterCountIf Hash::IterCount::APPEND, append iteration count as per https://msdn.microsoft.com/en-us/library/dd920692 If Hash::IterCount::PREPEND, prepend iteration count as per https://msdn.microsoft.com/en-us/library/dd924776 and https://msdn.microsoft.com/en-us/library/dd925430 If Hash::IterCount::NONE, do not add the iteration count to hash iterations.
rAlgorithmNameOne of "SHA-512", "SHA-256", ... as listed for AlgorithmName in https://msdn.microsoft.com/en-us/library/dd920692 or "SHA512", "SHA256", ... as listed for HashAlgorithm in https://msdn.microsoft.com/en-us/library/dd925810 that have a valid match in comphelper::HashType. If not, an empty sequence is returned. Not all algorithm names are supported.
Returns
the raw the hash value.

Definition at line 364 of file docpasswordhelper.cxx.

References comphelper::Hash::calculateHash(), eType, comphelper::MD5, comphelper::SHA1, comphelper::SHA256, comphelper::SHA512, and u.

Referenced by GetOoxHashAsSequence().

◆ GetWordHashAsUINT32()

sal_uInt32 comphelper::DocPasswordHelper::GetWordHashAsUINT32 ( std::u16string_view  aString)
static

This helper function generates the hash code based on the algorithm specified by MS for "Password to modify" feature of Word.

Parameters
aStringThe string for which the hash should be calculated
Returns
The hash represented by sal_uInt32

Definition at line 255 of file docpasswordhelper.cxx.

◆ GetXLHashAsSequence()

Sequence< sal_Int8 > comphelper::DocPasswordHelper::GetXLHashAsSequence ( std::u16string_view  aString)
static

This helper function generates the hash code based on the algorithm specified by MS for "Password to modify" and passwords related to table protection.

Parameters
aStringThe string for which the hash should be calculated
Returns
The hash represented by sequence of bytes in BigEndian form

Definition at line 356 of file docpasswordhelper.cxx.

References GetXLHashAsUINT16().

◆ GetXLHashAsUINT16()

sal_uInt16 comphelper::DocPasswordHelper::GetXLHashAsUINT16 ( std::u16string_view  aString,
rtl_TextEncoding  nEnc = RTL_TEXTENCODING_UTF8 
)
static

This helper function generates the hash code based on the algorithm specified by MS for "Password to modify" and passwords related to table protection of Excel.

Parameters
aStringThe string for which the hash should be calculated
nEncThe encoding that should be used to generate the 8-bit string before the hash is generated
Returns
The hash represented by sal_uInt16

Definition at line 331 of file docpasswordhelper.cxx.

References OUStringToOString(), and SAL_MAX_UINT16.

Referenced by GetXLHashAsSequence().

◆ IsModifyPasswordCorrect()

bool comphelper::DocPasswordHelper::IsModifyPasswordCorrect ( std::u16string_view  aPassword,
const css::uno::Sequence< css::beans::PropertyValue > &  aInfo 
)
static

This helper function allows to check whether the "Password to modify" provided by user is the correct one.

Parameters
aStringThe string containing the provided password
aInfoThe sequence containing the hash and the algorithm-info
Returns
<TRUE> if the password is correct one <FALSE> otherwise

Definition at line 196 of file docpasswordhelper.cxx.

References comphelper::Hash::APPEND, comphelper::GeneratePBKDF2Hash(), GetOoxHashAsBase64(), nCount, sHash, and uno::Any::Value.

◆ requestAndVerifyDocPassword()

css::uno::Sequence< css::beans::NamedValue > comphelper::DocPasswordHelper::requestAndVerifyDocPassword ( IDocPasswordVerifier rVerifier,
const css::uno::Sequence< css::beans::NamedValue > &  rMediaEncData,
const OUString &  rMediaPassword,
const css::uno::Reference< css::task::XInteractionHandler > &  rxInteractHandler,
const OUString &  rDocumentUrl,
DocPasswordRequestType  eRequestType,
const ::std::vector< OUString > *  pDefaultPasswords = nullptr,
bool *  pbIsDefaultPassword = nullptr 
)
static

This helper function tries to request and verify a password to load a protected document.

First, the list of default passwords will be tried if provided. This is needed by import filters for external file formats that have to check a predefined password in some cases without asking the user for a password. Every password is checked using the passed password verifier.

If not successful, the passed password of a medium is tried, that has been set e.g. by an API call to load a document. If existing, the password is checked using the passed password verifier.

If still not successful, the passed interaction handler is used to request a password from the user. This will be repeated until the passed password verifier validates the entered password, or if the user chooses to cancel password input.

Parameters
rVerifierThe password verifier used to check every processed password.
rMediaPasswordIf not empty, will be passed to the password validator before requesting a password from the user. This password usually should be querried from a media descriptor.
rxInteractHandlerThe interaction handler that will be used to request a password from the user, e.g. by showing a password input dialog.
rDocumentUrlThe URL of the related document that will be shown in the password input dialog.
eRequestTypeThe password request type that will be passed to the DocPasswordRequest object created internally. See docpasswordrequest.hxx for more details.
pDefaultPasswordsIf not null, contains default passwords that will be tried before a password will be requested from the media descriptor or the user.
pbIsDefaultPassword(output parameter) If not null, the type of the found password will be returned. True means the password has been found in the passed list of default passwords. False means the password has been taken from the rMediaPassword parameter or has been entered by the user.
Returns
If not empty, contains the password that has been validated by the passed password verifier. If empty, no valid password has been found, or the user has chosen to cancel password input.

Definition at line 518 of file docpasswordhelper.cxx.

References comphelper::Abort, comphelper::concatSequences(), comphelper::OStorageHelper::CreatePackageEncryptionData(), Exception, GenerateRandomByteSequence(), GenerateStd97Key(), comphelper::OK, PACKAGE_ENCRYPTIONDATA_SHA256UTF8, comphelper::IDocPasswordVerifier::verifyEncryptionData(), comphelper::IDocPasswordVerifier::verifyPassword(), and comphelper::WrongPassword.


The documentation for this class was generated from the following files: