bump product version to 4.1.6.2
[LibreOffice.git] / include / oox / core / binarycodec.hxx
blob64b06d4244efb446ae39e1d56d2954a8b1e37ee0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 OOX_CORE_BINARYCODEC_HXX
21 #define OOX_CORE_BINARYCODEC_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/beans/NamedValue.hpp>
26 #include <rtl/cipher.h>
27 #include <rtl/digest.h>
28 #include "oox/dllapi.h"
30 namespace oox { class AttributeList; }
32 namespace oox {
33 namespace core {
35 // ============================================================================
37 class OOX_DLLPUBLIC CodecHelper
39 public:
40 /** Returns the password hash if it is in the required 16-bit limit. */
41 static sal_uInt16 getPasswordHash( const AttributeList& rAttribs, sal_Int32 nElement );
43 private:
44 CodecHelper();
45 ~CodecHelper();
48 // ============================================================================
50 /** Encodes and decodes data from/to protected MS Office documents.
52 Implements a simple XOR encoding/decoding algorithm used in MS Office
53 versions up to MSO 95.
55 class OOX_DLLPUBLIC BinaryCodec_XOR
57 public:
58 /** Enumerates codec types supported by this XOR codec implementation. */
59 enum CodecType
61 CODEC_WORD, ///< MS Word XOR codec.
62 CODEC_EXCEL ///< MS Excel XOR codec.
65 public:
66 /** Default constructor.
68 Two-step construction in conjunction with the initKey() and verifyKey()
69 functions allows to try to initialize with different passwords (e.g.
70 built-in default password used for Excel workbook protection).
72 explicit BinaryCodec_XOR( CodecType eCodecType );
74 ~BinaryCodec_XOR();
76 /** Initializes the algorithm with the specified password.
78 @param pnPassData
79 Character array containing the password. Must be zero terminated,
80 which results in a maximum length of 15 characters.
82 void initKey( const sal_uInt8 pnPassData[ 16 ] );
84 /** Initializes the algorithm with the encryption data.
86 @param aData
87 The sequence contains the necessary data to initialize
88 the codec.
90 bool initCodec( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aData );
92 /** Retrieves the encryption data
94 @return
95 The sequence contains the necessary data to initialize
96 the codec.
98 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > getEncryptionData();
100 /** Verifies the validity of the password using the passed key and hash.
102 @precond
103 The codec must be initialized with the initKey() function before
104 this function can be used.
106 @param nKey
107 Password key value read from the file.
108 @param nHash
109 Password hash value read from the file.
111 @return
112 True = test was successful.
114 bool verifyKey( sal_uInt16 nKey, sal_uInt16 nHash ) const;
116 /** Reinitializes the codec to start a new memory block.
118 Resets the internal key offset to 0.
120 @precond
121 The codec must be initialized with the initKey() function before
122 this function can be used.
124 void startBlock();
126 /** Decodes a block of memory.
128 @precond
129 The codec must be initialized with the initKey() function before
130 this function can be used.
132 @param pnDestData
133 Destination buffer. Will contain the decrypted data afterwards.
134 @param pnSrcData
135 Encrypted data block.
136 @param nBytes
137 Size of the passed data blocks. pnDestData and pnSrcData must be of
138 this size.
140 @return
141 True = decoding was successful (no error occurred).
143 bool decode(
144 sal_uInt8* pnDestData,
145 const sal_uInt8* pnSrcData,
146 sal_Int32 nBytes );
148 /** Lets the cipher skip a specific amount of bytes.
150 This function sets the cipher to the same state as if the specified
151 amount of data has been decoded with one or more calls of decode().
153 @precond
154 The codec must be initialized with the initKey() function before
155 this function can be used.
157 @param nBytes
158 Number of bytes to be skipped (cipher "seeks" forward).
160 @return
161 True = skip was successful (no error occurred).
163 bool skip( sal_Int32 nBytes );
165 private:
166 CodecType meCodecType; ///< Codec type.
167 sal_uInt8 mpnKey[ 16 ]; ///< Encryption key.
168 sal_Int32 mnOffset; ///< Key offset.
169 sal_uInt16 mnBaseKey; ///< Base key from password.
170 sal_uInt16 mnHash; ///< Hash value from password.
173 // ============================================================================
175 /** Encodes and decodes data from protected MSO 97+ documents.
177 This is a wrapper class around low level cryptographic functions from RTL.
178 Implementation is based on the wvDecrypt package by Caolan McNamara:
179 http://www.csn.ul.ie/~caolan/docs/wvDecrypt.html
181 class OOX_DLLPUBLIC BinaryCodec_RCF
183 public:
184 /** Default constructor.
186 Two-step construction in conjunction with the initKey() and verifyKey()
187 functions allows to try to initialize with different passwords (e.g.
188 built-in default password used for Excel workbook protection).
190 explicit BinaryCodec_RCF();
192 ~BinaryCodec_RCF();
194 /** Initializes the algorithm with the encryption data.
196 @param aData
197 The sequence contains the necessary data to initialize
198 the codec.
200 bool initCodec( const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >& aData );
202 /** Retrieves the encryption data
204 @return
205 The sequence contains the necessary data to initialize
206 the codec.
208 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue > getEncryptionData();
210 /** Initializes the algorithm with the specified password and document ID.
212 @param pnPassData
213 Unicode character array containing the password. Must be zero
214 terminated, which results in a maximum length of 15 characters.
215 @param pnSalt
216 Random salt data block read from or written to the file.
218 void initKey(
219 const sal_uInt16 pnPassData[ 16 ],
220 const sal_uInt8 pnSalt[ 16 ] );
222 /** Verifies the validity of the password using the passed salt data.
224 @precond
225 The codec must be initialized with the initKey() function before
226 this function can be used.
228 @param pnVerifier
229 Verifier block read from the file.
230 @param pnVerifierHash
231 Verifier hash read from the file.
233 @return
234 True = test was successful.
236 bool verifyKey(
237 const sal_uInt8 pnVerifier[ 16 ],
238 const sal_uInt8 pnVerifierHash[ 16 ] );
240 /** Rekeys the codec using the specified counter.
242 After reading a specific amount of data the cipher algorithm needs to
243 be rekeyed using a counter that counts the data blocks.
245 The block size is for example 512 bytes for MS Word files and 1024
246 bytes for MS Excel files.
248 @precond
249 The codec must be initialized with the initKey() function before
250 this function can be used.
252 @param nCounter
253 Block counter used to rekey the cipher.
255 bool startBlock( sal_Int32 nCounter );
257 /** Decodes a block of memory.
259 @see rtl_cipher_decode()
261 @precond
262 The codec must be initialized with the initKey() function before
263 this function can be used.
265 @param pnDestData
266 Destination buffer. Will contain the decrypted data afterwards.
267 @param pnSrcData
268 Encrypted data block.
269 @param nBytes
270 Size of the passed data blocks. pnDestData and pnSrcData must be of
271 this size.
273 @return
274 True = decoding was successful (no error occurred).
276 bool decode(
277 sal_uInt8* pnDestData,
278 const sal_uInt8* pnSrcData,
279 sal_Int32 nBytes );
281 /** Lets the cipher skip a specific amount of bytes.
283 This function sets the cipher to the same state as if the specified
284 amount of data has been decoded with one or more calls of decode().
286 @precond
287 The codec must be initialized with the initKey() function before
288 this function can be used.
290 @param nBytes
291 Number of bytes to be skipped (cipher "seeks" forward).
293 @return
294 True = skip was successful (no error occurred).
296 bool skip( sal_Int32 nBytes );
298 private:
299 void InitKeyImpl(
300 const sal_uInt8 pKeyData[64],
301 const sal_uInt8 pUnique[16] );
303 rtlCipher mhCipher;
304 rtlDigest mhDigest;
305 sal_uInt8 mpnDigestValue[ RTL_DIGEST_LENGTH_MD5 ];
306 sal_uInt8 mpnUnique[16];
309 // ============================================================================
311 } // namespace core
312 } // namespace oox
314 #endif
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */