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 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
; }
35 // ============================================================================
37 class OOX_DLLPUBLIC CodecHelper
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
);
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
58 /** Enumerates codec types supported by this XOR codec implementation. */
61 CODEC_WORD
, ///< MS Word XOR codec.
62 CODEC_EXCEL
///< MS Excel XOR codec.
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
);
76 /** Initializes the algorithm with the specified password.
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.
87 The sequence contains the necessary data to initialize
90 bool initCodec( const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& aData
);
92 /** Retrieves the encryption data
95 The sequence contains the necessary data to initialize
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.
103 The codec must be initialized with the initKey() function before
104 this function can be used.
107 Password key value read from the file.
109 Password hash value read from the file.
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.
121 The codec must be initialized with the initKey() function before
122 this function can be used.
126 /** Decodes a block of memory.
129 The codec must be initialized with the initKey() function before
130 this function can be used.
133 Destination buffer. Will contain the decrypted data afterwards.
135 Encrypted data block.
137 Size of the passed data blocks. pnDestData and pnSrcData must be of
141 True = decoding was successful (no error occurred).
144 sal_uInt8
* pnDestData
,
145 const sal_uInt8
* pnSrcData
,
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().
154 The codec must be initialized with the initKey() function before
155 this function can be used.
158 Number of bytes to be skipped (cipher "seeks" forward).
161 True = skip was successful (no error occurred).
163 bool skip( sal_Int32 nBytes
);
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
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();
194 /** Initializes the algorithm with the encryption data.
197 The sequence contains the necessary data to initialize
200 bool initCodec( const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& aData
);
202 /** Retrieves the encryption data
205 The sequence contains the necessary data to initialize
208 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
> getEncryptionData();
210 /** Initializes the algorithm with the specified password and document ID.
213 Unicode character array containing the password. Must be zero
214 terminated, which results in a maximum length of 15 characters.
216 Random salt data block read from or written to the file.
219 const sal_uInt16 pnPassData
[ 16 ],
220 const sal_uInt8 pnSalt
[ 16 ] );
222 /** Verifies the validity of the password using the passed salt data.
225 The codec must be initialized with the initKey() function before
226 this function can be used.
229 Verifier block read from the file.
230 @param pnVerifierHash
231 Verifier hash read from the file.
234 True = test was successful.
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.
249 The codec must be initialized with the initKey() function before
250 this function can be used.
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()
262 The codec must be initialized with the initKey() function before
263 this function can be used.
266 Destination buffer. Will contain the decrypted data afterwards.
268 Encrypted data block.
270 Size of the passed data blocks. pnDestData and pnSrcData must be of
274 True = decoding was successful (no error occurred).
277 sal_uInt8
* pnDestData
,
278 const sal_uInt8
* pnSrcData
,
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().
287 The codec must be initialized with the initKey() function before
288 this function can be used.
291 Number of bytes to be skipped (cipher "seeks" forward).
294 True = skip was successful (no error occurred).
296 bool skip( sal_Int32 nBytes
);
300 const sal_uInt8 pKeyData
[64],
301 const sal_uInt8 pUnique
[16] );
305 sal_uInt8 mpnDigestValue
[ RTL_DIGEST_LENGTH_MD5
];
306 sal_uInt8 mpnUnique
[16];
309 // ============================================================================
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */