LibreOffice Module filter (master) 1
mscodec.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_FILTER_MSFILTER_MSCODEC_HXX
21#define INCLUDED_FILTER_MSFILTER_MSCODEC_HXX
22
23#include <com/sun/star/uno/Sequence.hxx>
25#include <rtl/cipher.h>
26#include <rtl/digest.h>
27#include <sal/types.h>
28#include <comphelper/hash.hxx>
29#include <vector>
30
31namespace com::sun::star {
32 namespace beans { struct NamedValue; }
33}
34
35namespace msfilter {
36
37
41{
42public:
43 explicit MSCodec_Xor95(int nRotateDistance);
44 virtual ~MSCodec_Xor95();
45
52 void InitKey( const sal_uInt8 pnPassData[ 16 ] );
53
60 bool InitCodec( const css::uno::Sequence< css::beans::NamedValue >& aData );
61
68 css::uno::Sequence< css::beans::NamedValue > GetEncryptionData();
69
70
85 bool VerifyKey( sal_uInt16 nKey, sal_uInt16 nHash ) const;
86
95 void InitCipher();
96
108 virtual void Decode( sal_uInt8* pnData, std::size_t nBytes )=0;
109
122 void Skip( std::size_t nBytes );
123
124protected:
125 sal_uInt8 mpnKey[ 16 ] = {};
126 std::size_t mnOffset;
127
128private:
129 MSCodec_Xor95( const MSCodec_Xor95& ) = delete;
131
132 sal_uInt16 mnKey;
133 sal_uInt16 mnHash;
135};
136
140{
141public:
143
155 virtual void Decode( sal_uInt8* pnData, std::size_t nBytes ) override;
156};
157
161{
162public:
164
176 virtual void Decode( sal_uInt8* pnData, std::size_t nBytes ) override;
177};
178
180{
181public:
182 MSCodec97(size_t nHashLen, OUString aEncKeyName);
183 virtual ~MSCodec97();
184
191 bool InitCodec(const css::uno::Sequence< css::beans::NamedValue >& aData);
192
199 virtual css::uno::Sequence<css::beans::NamedValue> GetEncryptionData();
200
209 virtual void InitKey(const sal_uInt16 pPassData[16],
210 const sal_uInt8 pDocId[16]) = 0;
211
212
227 bool VerifyKey(const sal_uInt8* pSaltData, const sal_uInt8* pSaltDigest);
228
229 virtual void GetDigestFromSalt(const sal_uInt8* pSaltData, sal_uInt8* pDigest) = 0;
230
246 virtual bool InitCipher(sal_uInt32 nCounter) = 0;
247
270 bool Encode(const void* pData, std::size_t nDatLen,
271 sal_uInt8* pBuffer, std::size_t nBufLen);
272
295 bool Decode(const void* pData, std::size_t nDatLen,
296 sal_uInt8* pBuffer, std::size_t nBufLen);
297
310 bool Skip(std::size_t nDatLen);
311
312 /* allows to get the unique document id from the codec
313 */
314 void GetDocId( sal_uInt8 pDocId[16] );
315
316private:
317 MSCodec97(const MSCodec97&) = delete;
318 MSCodec97& operator=(const MSCodec97&) = delete;
319
320protected:
323 rtlCipher m_hCipher;
324 std::vector<sal_uInt8> m_aDocId;
325 std::vector<sal_uInt8> m_aDigestValue;
326};
327
335{
336public:
338 virtual ~MSCodec_Std97() override;
339
348 virtual void InitKey(const sal_uInt16 pPassData[16],
349 const sal_uInt8 pDocId[16]) override;
350
366 virtual bool InitCipher(sal_uInt32 nCounter) override;
367
369 void CreateSaltDigest(
370 const sal_uInt8 nSaltData[16], sal_uInt8 nSaltDigest[16] );
371
385 void GetEncryptKey (
386 const sal_uInt8 pSalt[16],
387 sal_uInt8 pSaltData[16],
388 sal_uInt8 pSaltDigest[16]);
389
390 virtual void GetDigestFromSalt(const sal_uInt8* pSaltData, sal_uInt8* pDigest) override;
391
392private:
393 MSCodec_Std97( const MSCodec_Std97& ) = delete;
395
396 rtlDigest m_hDigest;
397};
398
400{
401private:
402 css::uno::Sequence<sal_Int8> m_aStd97Key;
403public:
405
406 virtual void InitKey(const sal_uInt16 pPassData[16],
407 const sal_uInt8 pDocId[16]) override;
408 virtual bool InitCipher(sal_uInt32 nCounter) override;
409 virtual void GetDigestFromSalt(const sal_uInt8* pSaltData, sal_uInt8* pDigest) override;
410 virtual css::uno::Sequence<css::beans::NamedValue> GetEncryptionData() override;
411};
412
413const sal_uInt32 ENCRYPTINFO_CRYPTOAPI = 0x00000004;
414const sal_uInt32 ENCRYPTINFO_DOCPROPS = 0x00000008;
415const sal_uInt32 ENCRYPTINFO_EXTERNAL = 0x00000010;
416const sal_uInt32 ENCRYPTINFO_AES = 0x00000020;
417
418const sal_uInt32 ENCRYPT_ALGO_AES128 = 0x0000660E;
419const sal_uInt32 ENCRYPT_ALGO_AES192 = 0x0000660F;
420const sal_uInt32 ENCRYPT_ALGO_AES256 = 0x00006610;
421const sal_uInt32 ENCRYPT_ALGO_RC4 = 0x00006801;
422
423const sal_uInt32 ENCRYPT_HASH_SHA1 = 0x00008004;
424
425const sal_uInt32 ENCRYPT_KEY_SIZE_AES_128 = 0x00000080;
426const sal_uInt32 ENCRYPT_KEY_SIZE_AES_192 = 0x000000C0;
427const sal_uInt32 ENCRYPT_KEY_SIZE_AES_256 = 0x00000100;
428
429const sal_uInt32 ENCRYPT_PROVIDER_TYPE_AES = 0x00000018;
430const sal_uInt32 ENCRYPT_PROVIDER_TYPE_RC4 = 0x00000001;
431
432// version of encryption info used in MS Office 1997 (major = 1, minor = 1)
433const sal_uInt32 VERSION_INFO_1997_FORMAT = 0x00010001;
434// version of encryption info used in MS Office 2007 (major = 3, minor = 2)
435const sal_uInt32 VERSION_INFO_2007_FORMAT = 0x00020003;
436// version of encryption info used in MS Office 2007 SP2 and older (major = 4, minor = 2)
437const sal_uInt32 VERSION_INFO_2007_FORMAT_SP2 = 0x00020004;
438
439// version of encryption info - agile (major = 4, minor = 4)
440const sal_uInt32 VERSION_INFO_AGILE = 0x00040004;
441
442const sal_uInt32 AGILE_ENCRYPTION_RESERVED = 0x00000040;
443
444const sal_uInt32 SALT_LENGTH = 16;
445const sal_uInt32 ENCRYPTED_VERIFIER_LENGTH = 16;
446
448{
449 sal_uInt32 flags;
450 sal_uInt32 sizeExtra; // 0
451 sal_uInt32 algId; // if flag AES && CRYPTOAPI this defaults to 128-bit AES
452 sal_uInt32 algIdHash; // 0: determine by flags - defaults to SHA-1 if not external
453 sal_uInt32 keyBits; // key size in bits: 0 (determine by flags), 128, 192, 256
454 sal_uInt32 providedType; // AES or RC4
455 sal_uInt32 reserved1; // 0
456 sal_uInt32 reserved2; // 0
457
459};
460
462{
463 sal_uInt32 saltSize; // must be 0x00000010
464 sal_uInt8 salt[SALT_LENGTH] = {}; // random generated salt value
465 sal_uInt8 encryptedVerifier[ENCRYPTED_VERIFIER_LENGTH] = {}; // randomly generated verifier value
466 sal_uInt32 encryptedVerifierHashSize; // actually written hash size - depends on algorithm
467 sal_uInt8 encryptedVerifierHash[comphelper::SHA256_HASH_LENGTH] = {}; // verifier value hash - itself also encrypted
468
470};
471
473{
474 sal_uInt32 saltSize; // must be 0x00000010
475 sal_uInt8 salt[SALT_LENGTH] = {}; // random generated salt value
476 sal_uInt8 encryptedVerifier[ENCRYPTED_VERIFIER_LENGTH] = {}; // randomly generated verifier value
477 sal_uInt32 encryptedVerifierHashSize; // actually written hash size - depends on algorithm
478 sal_uInt8 encryptedVerifierHash[comphelper::SHA1_HASH_LENGTH] = {}; // verifier value hash - itself also encrypted
479
481};
482
484{
487};
488
490{
493};
494
495} // namespace msfilter
496
497#endif
498
499/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< sal_uInt8 > m_aDigestValue
Definition: mscodec.hxx:325
MSCodec97 & operator=(const MSCodec97 &)=delete
virtual bool InitCipher(sal_uInt32 nCounter)=0
Rekeys the codec using the specified counter.
virtual void InitKey(const sal_uInt16 pPassData[16], const sal_uInt8 pDocId[16])=0
Initializes the algorithm with the specified password and document ID.
virtual void GetDigestFromSalt(const sal_uInt8 *pSaltData, sal_uInt8 *pDigest)=0
MSCodec97(const MSCodec97 &)=delete
rtlCipher m_hCipher
Definition: mscodec.hxx:323
std::vector< sal_uInt8 > m_aDocId
Definition: mscodec.hxx:324
OUString m_sEncKeyName
Definition: mscodec.hxx:321
css::uno::Sequence< sal_Int8 > m_aStd97Key
Definition: mscodec.hxx:402
Encodes and decodes data from protected MSO 97+ documents.
Definition: mscodec.hxx:335
MSCodec_Std97(const MSCodec_Std97 &)=delete
MSCodec_Std97 & operator=(const MSCodec_Std97 &)=delete
Encodes and decodes data from protected MSO 95- documents.
Definition: mscodec.hxx:41
std::size_t mnOffset
Encryption key.
Definition: mscodec.hxx:126
MSCodec_Xor95(const MSCodec_Xor95 &)=delete
Key offset.
MSCodec_Xor95 & operator=(const MSCodec_Xor95 &)=delete
virtual void Decode(sal_uInt8 *pnData, std::size_t nBytes)=0
Decodes a block of memory inplace.
int mnRotateDistance
Hash value from password.
Definition: mscodec.hxx:134
sal_uInt16 mnHash
Base key from password.
Definition: mscodec.hxx:133
Encodes and decodes data from protected MSO Word 95- documents.
Definition: mscodec.hxx:161
Encodes and decodes data from protected MSO XLS 95- documents.
Definition: mscodec.hxx:140
#define MSFILTER_DLLPUBLIC
constexpr OUStringLiteral aData
Shape IDs per cluster in DGG atom.
const sal_uInt32 SHA256_HASH_LENGTH
const sal_uInt32 SHA1_HASH_LENGTH
const sal_uInt32 ENCRYPT_ALGO_AES192
Definition: mscodec.hxx:419
const sal_uInt32 VERSION_INFO_1997_FORMAT
Definition: mscodec.hxx:433
const sal_uInt32 ENCRYPT_ALGO_AES256
Definition: mscodec.hxx:420
const sal_uInt32 ENCRYPT_PROVIDER_TYPE_AES
Definition: mscodec.hxx:429
const sal_uInt32 VERSION_INFO_2007_FORMAT
Definition: mscodec.hxx:435
const sal_uInt32 ENCRYPT_KEY_SIZE_AES_128
Definition: mscodec.hxx:425
const sal_uInt32 VERSION_INFO_2007_FORMAT_SP2
Definition: mscodec.hxx:437
const sal_uInt32 ENCRYPT_ALGO_AES128
Definition: mscodec.hxx:418
const sal_uInt32 ENCRYPT_HASH_SHA1
Definition: mscodec.hxx:423
const sal_uInt32 AGILE_ENCRYPTION_RESERVED
Definition: mscodec.hxx:442
const sal_uInt32 ENCRYPT_KEY_SIZE_AES_256
Definition: mscodec.hxx:427
const sal_uInt32 ENCRYPTINFO_AES
Definition: mscodec.hxx:416
const sal_uInt32 ENCRYPT_PROVIDER_TYPE_RC4
Definition: mscodec.hxx:430
const sal_uInt32 ENCRYPTED_VERIFIER_LENGTH
Definition: mscodec.hxx:445
const sal_uInt32 VERSION_INFO_AGILE
Definition: mscodec.hxx:440
const sal_uInt32 ENCRYPTINFO_DOCPROPS
Definition: mscodec.hxx:414
const sal_uInt32 SALT_LENGTH
Definition: mscodec.hxx:444
const sal_uInt32 ENCRYPT_KEY_SIZE_AES_192
Definition: mscodec.hxx:426
const sal_uInt32 ENCRYPTINFO_CRYPTOAPI
Definition: mscodec.hxx:413
const sal_uInt32 ENCRYPT_ALGO_RC4
Definition: mscodec.hxx:421
const sal_uInt32 ENCRYPTINFO_EXTERNAL
Definition: mscodec.hxx:415
EncryptionStandardHeader header
Definition: mscodec.hxx:491
EncryptionVerifierRC4 verifier
Definition: mscodec.hxx:492
EncryptionVerifierAES verifier
Definition: mscodec.hxx:486
EncryptionStandardHeader header
Definition: mscodec.hxx:485
unsigned char sal_uInt8