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 COMPHELPER_DOCPASSWORDHELPR_HXX
21 #define COMPHELPER_DOCPASSWORDHELPR_HXX
23 #include <com/sun/star/beans/NamedValue.hpp>
24 #include "comphelper/comphelperdllapi.h"
26 #include "comphelper/docpasswordrequest.hxx"
28 namespace com
{ namespace sun
{ namespace star
{ namespace task
{ class XInteractionHandler
; } } } }
29 namespace com
{ namespace sun
{ namespace star
{ namespace beans
{ struct PropertyValue
; } } } }
31 namespace comphelper
{
33 class MediaDescriptor
;
35 // ============================================================================
37 enum DocPasswordVerifierResult
39 DocPasswordVerifierResult_OK
,
40 DocPasswordVerifierResult_WRONG_PASSWORD
,
41 DocPasswordVerifierResult_ABORT
44 // ============================================================================
46 /** Base class for a password verifier used by the DocPasswordHelper class
49 Users have to implement the virtual functions and pass an instance of the
50 verifier to one of the password request functions.
52 class COMPHELPER_DLLPUBLIC IDocPasswordVerifier
55 virtual ~IDocPasswordVerifier();
57 /** Will be called everytime a password needs to be verified.
60 The password to be verified
62 @param o_rEncryptionData
63 Output parameter, that is filled with the EncryptionData generated
64 from the password. The data is filled only if the validation was
67 @return The result of the verification.
68 - DocPasswordVerifierResult_OK, if and only if the passed password
69 is valid and can be used to process the related document.
70 - DocPasswordVerifierResult_WRONG_PASSWORD, if the password is
71 wrong. The user may be asked again for a new password.
72 - DocPasswordVerifierResult_ABORT, if an unrecoverable error
73 occurred while password verification. The password request loop
76 virtual DocPasswordVerifierResult
verifyPassword( const OUString
& rPassword
, ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& o_rEncryptionData
) = 0;
78 /** Will be called everytime an encryption data needs to be verified.
80 @param rEncryptionData
81 The data will be validated
83 @return The result of the verification.
84 - DocPasswordVerifierResult_OK, if and only if the passed encryption data
85 is valid and can be used to process the related document.
86 - DocPasswordVerifierResult_WRONG_PASSWORD, if the encryption data is
88 - DocPasswordVerifierResult_ABORT, if an unrecoverable error
89 occured while data verification. The password request loop
92 virtual DocPasswordVerifierResult
verifyEncryptionData( const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& o_rEncryptionData
) = 0;
96 // ============================================================================
98 /** Helper that asks for a document password and checks its validity.
100 class COMPHELPER_DLLPUBLIC DocPasswordHelper
103 // ------------------------------------------------------------------------
105 /** This helper function generates the information related
106 to "Password to modify" provided by user. The result
107 sequence contains the hash and the algorithm-related
111 The string for which the info should be generated
114 The sequence containing the hash and the algorithm-related info
117 static ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>
118 GenerateNewModifyPasswordInfo( const OUString
& aPassword
);
120 // ------------------------------------------------------------------------
122 /** This helper function allows to check whether
123 the "Password to modify" provided by user is the correct one.
126 The string containing the provided password
129 The sequence containing the hash and the algorithm-info
132 <TRUE/> if the password is correct one
136 static sal_Bool
IsModifyPasswordCorrect(
137 const OUString
& aPassword
,
138 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& aInfo
);
141 // ------------------------------------------------------------------------
143 /** This helper function generates the hash code based on the algorithm
144 specified by MS for "Password to modify" feature of Word.
147 The string for which the hash should be calculated
150 The hash represented by sal_uInt32
153 static sal_uInt32
GetWordHashAsUINT32(
154 const OUString
& aString
);
156 // ------------------------------------------------------------------------
158 /** This helper function generates the hash code based on the algorithm
159 specified by MS for "Password to modify" and passwords related to
160 table protection of Excel.
163 The string for which the hash should be calculated
166 The encoding that should be used to generate the 8-bit string
167 before the hash is generated
170 The hash represented by sal_uInt16
173 static sal_uInt16
GetXLHashAsUINT16(
174 const OUString
& aString
,
175 rtl_TextEncoding nEnc
= RTL_TEXTENCODING_UTF8
);
177 // ------------------------------------------------------------------------
179 /** This helper function generates the hash code based on the algorithm
180 specified by MS for "Password to modify" and passwords related to
184 The string for which the hash should be calculated
187 The encoding that should be used to generate the 8-bit string
188 before the hash is generated
191 The hash represented by sequence of bytes in BigEndian form
194 static ::com::sun::star::uno::Sequence
< sal_Int8
> GetXLHashAsSequence(
195 const OUString
& aString
,
196 rtl_TextEncoding nEnc
= RTL_TEXTENCODING_UTF8
);
198 // ------------------------------------------------------------------------
200 /** This helper function generates a random sequence of bytes of
204 static ::com::sun::star::uno::Sequence
< sal_Int8
> GenerateRandomByteSequence(
207 // ------------------------------------------------------------------------
209 /** This helper function generates a byte sequence representing the
210 key digest value used by MSCodec_Std97 codec.
213 static ::com::sun::star::uno::Sequence
< sal_Int8
> GenerateStd97Key(
214 const OUString
& aPassword
,
215 const ::com::sun::star::uno::Sequence
< sal_Int8
>& aDocId
);
217 // ------------------------------------------------------------------------
219 /** This helper function generates a byte sequence representing the
220 key digest value used by MSCodec_Std97 codec.
223 static ::com::sun::star::uno::Sequence
< sal_Int8
> GenerateStd97Key(
224 const sal_uInt16 pPassData
[16],
225 const ::com::sun::star::uno::Sequence
< sal_Int8
>& aDocId
);
227 // ------------------------------------------------------------------------
229 /** This helper function tries to request and verify a password to load a
232 First, the list of default passwords will be tried if provided. This is
233 needed by import filters for external file formats that have to check a
234 predefined password in some cases without asking the user for a
235 password. Every password is checked using the passed password verifier.
237 If not successful, the passed password of a medium is tried, that has
238 been set e.g. by an API call to load a document. If existing, the
239 password is checked using the passed password verifier.
241 If still not successful, the passed interaction handler is used to
242 request a password from the user. This will be repeated until the
243 passed password verifier validates the entered password, or if the user
244 chooses to cancel password input.
247 The password verifier used to check every processed password.
249 @param rMediaPassword
250 If not empty, will be passed to the password validator before
251 requesting a password from the user. This password usually should
252 be querried from a media descriptor.
254 @param rxInteractHandler
255 The interaction handler that will be used to request a password
256 from the user, e.g. by showing a password input dialog.
259 The name of the related document that will be shown in the password
263 The password request type that will be passed to the
264 DocPasswordRequest object created internally. See
265 docpasswordrequest.hxx for more details.
267 @param pDefaultPasswords
268 If not null, contains default passwords that will be tried before a
269 password will be requested from the media descriptor or the user.
271 @param pbIsDefaultPassword
272 (output parameter) If not null, the type of the found password will
273 be returned. True means the password has been found in the passed
274 list of default passwords. False means the password has been taken
275 from the rMediaPassword parameter or has been entered by the user.
278 If not empty, contains the password that has been validated by the
279 passed password verifier. If empty, no valid password has been
280 found, or the user has chossen to cancel password input.
282 static ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
> requestAndVerifyDocPassword(
283 IDocPasswordVerifier
& rVerifier
,
284 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& rMediaEncData
,
285 const OUString
& rMediaPassword
,
286 const ::com::sun::star::uno::Reference
<
287 ::com::sun::star::task::XInteractionHandler
>& rxInteractHandler
,
288 const OUString
& rDocumentName
,
289 DocPasswordRequestType eRequestType
,
290 const ::std::vector
< OUString
>* pDefaultPasswords
= 0,
291 bool* pbIsDefaultPassword
= 0 );
293 // ------------------------------------------------------------------------
295 /** This helper function tries to find a password for the document
296 described by the passed media descriptor.
298 First, the list of default passwords will be tried if provided. This is
299 needed by import filters for external file formats that have to check a
300 predefined password in some cases without asking the user for a
301 password. Every password is checked using the passed password verifier.
303 If not successful, the passed media descriptor is asked for a password,
304 that has been set e.g. by an API call to load a document. If existing,
305 the password is checked using the passed password verifier.
307 If still not successful, the interaction handler contained in the
308 passed nmedia descriptor is used to request a password from the user.
309 This will be repeated until the passed password verifier validates the
310 entered password, or if the user chooses to cancel password input.
313 The password verifier used to check every processed password.
316 The media descriptor of the document that needs to be opened with
317 a password. If a valid password (that is not contained in the
318 passed list of default passwords) was found, it will be inserted
319 into the "Password" property of this descriptor.
322 The password request type that will be passed to the
323 DocPasswordRequest object created internally. See
324 docpasswordrequest.hxx for more details.
326 @param pDefaultPasswords
327 If not null, contains default passwords that will be tried before a
328 password will be requested from the media descriptor or the user.
331 If not empty, contains the password that has been validated by the
332 passed password verifier. If empty, no valid password has been
333 found, or the user has chossen to cancel password input.
335 static ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
> requestAndVerifyDocPassword(
336 IDocPasswordVerifier
& rVerifier
,
337 MediaDescriptor
& rMediaDesc
,
338 DocPasswordRequestType eRequestType
,
339 const ::std::vector
< OUString
>* pDefaultPasswords
= 0 );
341 // ------------------------------------------------------------------------
344 ~DocPasswordHelper();
347 // ============================================================================
349 } // namespace comphelper
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */