Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef SVX_MSCODEC_HXX
30 : : #define SVX_MSCODEC_HXX
31 : :
32 : : #include <com/sun/star/uno/Sequence.hxx>
33 : : #include <com/sun/star/beans/NamedValue.hpp>
34 : :
35 : : #include <rtl/cipher.h>
36 : : #include <rtl/digest.h>
37 : : #include "filter/msfilter/msfilterdllapi.h"
38 : :
39 : : namespace msfilter {
40 : :
41 : : // ============================================================================
42 : :
43 : : /** Encodes and decodes data from protected MSO 95- documents.
44 : : */
45 : : class MSFILTER_DLLPUBLIC MSCodec_Xor95
46 : : {
47 : : public:
48 : : explicit MSCodec_Xor95(int nRotateDistance);
49 : : virtual ~MSCodec_Xor95();
50 : :
51 : : /** Initializes the algorithm with the specified password.
52 : :
53 : : @param pPassData
54 : : Character array containing the password. Must be zero terminated,
55 : : which results in a maximum length of 15 characters.
56 : : */
57 : : void InitKey( const sal_uInt8 pnPassData[ 16 ] );
58 : :
59 : : /** Initializes the algorithm with the encryption data.
60 : :
61 : : @param aData
62 : : The sequence contains the necessary data to initialize
63 : : the codec.
64 : : */
65 : : sal_Bool InitCodec( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aData );
66 : :
67 : : /** Retrieves the encryption data
68 : :
69 : : @return
70 : : The sequence contains the necessary data to initialize
71 : : the codec.
72 : : */
73 : : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > GetEncryptionData();
74 : :
75 : :
76 : : /** Verifies the validity of the password using the passed key and hash.
77 : :
78 : : @precond
79 : : The codec must be initialized with InitKey() before this function
80 : : can be used.
81 : :
82 : : @param nKey
83 : : Password key value read from the file.
84 : : @param nHash
85 : : Password hash value read from the file.
86 : :
87 : : @return
88 : : true = Test was successful.
89 : : */
90 : : bool VerifyKey( sal_uInt16 nKey, sal_uInt16 nHash ) const;
91 : :
92 : : /** Reinitializes the codec to start a new memory block.
93 : :
94 : : Resets the internal key offset to 0.
95 : :
96 : : @precond
97 : : The codec must be initialized with InitKey() before this function
98 : : can be used.
99 : : */
100 : : void InitCipher();
101 : :
102 : : /** Decodes a block of memory inplace.
103 : :
104 : : @precond
105 : : The codec must be initialized with InitKey() before this function
106 : : can be used.
107 : :
108 : : @param pnData
109 : : Encrypted data block. Will contain the decrypted data afterwards.
110 : : @param nBytes
111 : : Size of the passed data block.
112 : : */
113 : : virtual void Decode( sal_uInt8* pnData, sal_Size nBytes )=0;
114 : :
115 : : /** Lets the cipher skip a specific amount of bytes.
116 : :
117 : : This function sets the cipher to the same state as if the specified
118 : : amount of data has been decoded with one or more calls of Decode().
119 : :
120 : : @precond
121 : : The codec must be initialized with InitKey() before this function
122 : : can be used.
123 : :
124 : : @param nBytes
125 : : Number of bytes to be skipped (cipher "seeks" forward).
126 : : */
127 : : void Skip( sal_Size nBytes );
128 : :
129 : : protected:
130 : : sal_uInt8 mpnKey[ 16 ]; /// Encryption key.
131 : : sal_Size mnOffset; /// Key offset.
132 : :
133 : : private:
134 : : MSFILTER_DLLPRIVATE MSCodec_Xor95( const MSCodec_Xor95& );
135 : : MSFILTER_DLLPRIVATE MSCodec_Xor95& operator=( const MSCodec_Xor95& );
136 : :
137 : : sal_uInt16 mnKey; /// Base key from password.
138 : : sal_uInt16 mnHash; /// Hash value from password.
139 : : int mnRotateDistance;
140 : : };
141 : :
142 : : /** Encodes and decodes data from protected MSO XLS 95- documents.
143 : : */
144 [ # # ]: 0 : class MSFILTER_DLLPUBLIC MSCodec_XorXLS95 : public MSCodec_Xor95
145 : : {
146 : : public:
147 : 0 : explicit MSCodec_XorXLS95() : MSCodec_Xor95(2) {}
148 : :
149 : : /** Decodes a block of memory inplace.
150 : :
151 : : @precond
152 : : The codec must be initialized with InitKey() before this function
153 : : can be used.
154 : :
155 : : @param pnData
156 : : Encrypted data block. Will contain the decrypted data afterwards.
157 : : @param nBytes
158 : : Size of the passed data block.
159 : : */
160 : : virtual void Decode( sal_uInt8* pnData, sal_Size nBytes );
161 : : };
162 : :
163 : : /** Encodes and decodes data from protected MSO Word 95- documents.
164 : : */
165 [ # # ]: 0 : class MSFILTER_DLLPUBLIC MSCodec_XorWord95 : public MSCodec_Xor95
166 : : {
167 : : public:
168 : 0 : explicit MSCodec_XorWord95() : MSCodec_Xor95(7) {}
169 : :
170 : : /** Decodes a block of memory inplace.
171 : :
172 : : @precond
173 : : The codec must be initialized with InitKey() before this function
174 : : can be used.
175 : :
176 : : @param pnData
177 : : Encrypted data block. Will contain the decrypted data afterwards.
178 : : @param nBytes
179 : : Size of the passed data block.
180 : : */
181 : : virtual void Decode( sal_uInt8* pnData, sal_Size nBytes );
182 : : };
183 : :
184 : :
185 : : // ============================================================================
186 : :
187 : : /** Encodes and decodes data from protected MSO 97+ documents.
188 : :
189 : : This is a wrapper class around low level cryptographic functions from RTL.
190 : : Implementation is based on the wvDecrypt package by Caolan McNamara:
191 : : http://www.csn.ul.ie/~caolan/docs/wvDecrypt.html
192 : : */
193 : : class MSFILTER_DLLPUBLIC MSCodec_Std97
194 : : {
195 : : public:
196 : : explicit MSCodec_Std97();
197 : : ~MSCodec_Std97();
198 : :
199 : : /** Initializes the algorithm with the encryption data.
200 : :
201 : : @param aData
202 : : The sequence contains the necessary data to initialize
203 : : the codec.
204 : : */
205 : : sal_Bool InitCodec( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aData );
206 : :
207 : : /** Retrieves the encryption data
208 : :
209 : : @return
210 : : The sequence contains the necessary data to initialize
211 : : the codec.
212 : : */
213 : : ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > GetEncryptionData();
214 : :
215 : :
216 : : /** Initializes the algorithm with the specified password and document ID.
217 : :
218 : : @param pPassData
219 : : Wide character array containing the password. Must be zero
220 : : terminated, which results in a maximum length of 15 characters.
221 : : @param pDocId
222 : : Unique document identifier read from or written to the file.
223 : : */
224 : : void InitKey(
225 : : const sal_uInt16 pPassData[ 16 ],
226 : : const sal_uInt8 pDocId[ 16 ] );
227 : :
228 : : /** Verifies the validity of the password using the passed salt data.
229 : :
230 : : @precond
231 : : The codec must be initialized with InitKey() before this function
232 : : can be used.
233 : :
234 : : @param pSaltData
235 : : Salt data block read from the file.
236 : : @param pSaltDigest
237 : : Salt digest read from the file.
238 : :
239 : : @return
240 : : true = Test was successful.
241 : : */
242 : : bool VerifyKey(
243 : : const sal_uInt8 pSaltData[ 16 ],
244 : : const sal_uInt8 pSaltDigest[ 16 ] );
245 : :
246 : : /** Rekeys the codec using the specified counter.
247 : :
248 : : After reading a specific amount of data the cipher algorithm needs to
249 : : be rekeyed using a counter that counts the data blocks.
250 : :
251 : : The block size is for example 512 Bytes for Word files and 1024 Bytes
252 : : for Excel files.
253 : :
254 : : @precond
255 : : The codec must be initialized with InitKey() before this function
256 : : can be used.
257 : :
258 : : @param nCounter
259 : : Block counter used to rekey the cipher.
260 : : */
261 : : bool InitCipher( sal_uInt32 nCounter );
262 : :
263 : : /** Creates an MD5 digest of salt digest. */
264 : : bool CreateSaltDigest(
265 : : const sal_uInt8 nSaltData[16], sal_uInt8 nSaltDigest[16] );
266 : :
267 : : /** Encodes a block of memory.
268 : :
269 : : @see rtl_cipher_encode()
270 : :
271 : : @precond
272 : : The codec must be initialized with InitKey() before this function
273 : : can be used. The destination buffer must be able to take all
274 : : unencoded data from the source buffer (usually this means it must be
275 : : as long as or longer than the source buffer).
276 : :
277 : : @param pData
278 : : Unencrypted source data block.
279 : : @param nDatLen
280 : : Size of the passed source data block.
281 : : @param pBuffer
282 : : Destination buffer for the encrypted data.
283 : : @param nBufLen
284 : : Size of the destination buffer.
285 : :
286 : : @return
287 : : true = Encoding was successful (no error occurred).
288 : : */
289 : : bool Encode(
290 : : const void* pData, sal_Size nDatLen,
291 : : sal_uInt8* pBuffer, sal_Size nBufLen );
292 : :
293 : : /** Decodes a block of memory.
294 : :
295 : : @see rtl_cipher_decode()
296 : :
297 : : @precond
298 : : The codec must be initialized with InitKey() before this function
299 : : can be used. The destination buffer must be able to take all
300 : : encoded data from the source buffer (usually this means it must be
301 : : as long as or longer than the source buffer).
302 : :
303 : : @param pData
304 : : Encrypted source data block.
305 : : @param nDatLen
306 : : Size of the passed source data block.
307 : : @param pBuffer
308 : : Destination buffer for the decrypted data.
309 : : @param nBufLen
310 : : Size of the destination buffer.
311 : :
312 : : @return
313 : : true = Decoding was successful (no error occurred).
314 : : */
315 : : bool Decode(
316 : : const void* pData, sal_Size nDatLen,
317 : : sal_uInt8* pBuffer, sal_Size nBufLen );
318 : :
319 : : /** Lets the cipher skip a specific amount of bytes.
320 : :
321 : : This function sets the cipher to the same state as if the specified
322 : : amount of data has been decoded with one or more calls of Decode().
323 : :
324 : : @precond
325 : : The codec must be initialized with InitKey() before this function
326 : : can be used.
327 : :
328 : : @param nDatLen
329 : : Number of bytes to be skipped (cipher "seeks" forward).
330 : : */
331 : : bool Skip( sal_Size nDatLen );
332 : :
333 : : /** Gets salt data and salt digest.
334 : :
335 : : @precond
336 : : The codec must be initialized with InitKey() before this function
337 : : can be used.
338 : :
339 : : @param pSalt
340 : : Salt, a random number.
341 : : @param pSaltData
342 : : Salt data block generated from the salt.
343 : : @param pSaltDigest
344 : : Salt digest generated from the salt.
345 : : */
346 : : void GetEncryptKey (
347 : : const sal_uInt8 pSalt[16],
348 : : sal_uInt8 pSaltData[16],
349 : : sal_uInt8 pSaltDigest[16]);
350 : :
351 : : /* allows to get the unique document id from the codec
352 : : */
353 : : void GetDocId( sal_uInt8 pDocId[16] );
354 : :
355 : : void GetDigestFromSalt( const sal_uInt8 pSaltData[16], sal_uInt8 pDigest[16] );
356 : :
357 : : private:
358 : : void InitKeyImpl(
359 : : const sal_uInt8 pKeyData[64],
360 : : const sal_uInt8 pDocId[16] );
361 : :
362 : :
363 : : private:
364 : : MSFILTER_DLLPRIVATE MSCodec_Std97( const MSCodec_Std97& );
365 : : MSFILTER_DLLPRIVATE MSCodec_Std97& operator=( const MSCodec_Std97& );
366 : :
367 : : rtlCipher m_hCipher;
368 : : rtlDigest m_hDigest;
369 : : sal_uInt8 m_pDigestValue[ RTL_DIGEST_LENGTH_MD5 ];
370 : : sal_uInt8 m_pDocId[16];
371 : : };
372 : :
373 : : // ============================================================================
374 : :
375 : : } // namespace msfilter
376 : :
377 : : #endif
378 : :
379 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|