summaryrefslogtreecommitdiff
path: root/oox/source/core/DocumentCrypt.cxx
blob: 5e29ee8a30966c52d6144f0887f2f33926dabf60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include "oox/core/DocumentCrypt.hxx"
#include <config_oox.h>

#include <comphelper/docpasswordhelper.hxx>
#include <comphelper/mediadescriptor.hxx>
#if USE_TLS_OPENSSL
#include <openssl/evp.h>
#endif // USE_TLS_OPENSSL
#if USE_TLS_NSS
#include <nss.h>
#include <pk11pub.h>
#endif // USE_TLS_NSS
#include <rtl/digest.h>
#include "oox/helper/binaryinputstream.hxx"
#include "oox/helper/binaryoutputstream.hxx"

#include <osl/time.h>
#include <rtl/random.h>

namespace oox {
namespace core {

using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::uno;

using ::comphelper::MediaDescriptor;
using ::comphelper::SequenceAsHashMap;

/* =========================================================================== */
/*  Kudos to Caolan McNamara who provided the core decryption implementations. */
/* =========================================================================== */

namespace {

const sal_uInt32 ENCRYPTINFO_CRYPTOAPI      = 0x00000004;
const sal_uInt32 ENCRYPTINFO_DOCPROPS       = 0x00000008;
const sal_uInt32 ENCRYPTINFO_EXTERNAL       = 0x00000010;
const sal_uInt32 ENCRYPTINFO_AES            = 0x00000020;

const sal_uInt32 ENCRYPT_ALGO_AES128        = 0x0000660E;
const sal_uInt32 ENCRYPT_ALGO_AES192        = 0x0000660F;
const sal_uInt32 ENCRYPT_ALGO_AES256        = 0x00006610;
const sal_uInt32 ENCRYPT_ALGO_RC4           = 0x00006801;

const sal_uInt32 ENCRYPT_HASH_SHA1          = 0x00008004;

const sal_uInt32 ENCRYPT_KEY_SIZE_AES_128   = 0x00000080;
const sal_uInt32 ENCRYPT_KEY_SIZE_AES_192   = 0x000000C0;
const sal_uInt32 ENCRYPT_KEY_SIZE_AES_256   = 0x00000100;

const sal_uInt32 ENCRYPT_PROVIDER_TYPE_AES  = 0x00000018;
const sal_uInt32 ENCRYPT_PROVIDER_TYPE_RC4  = 0x00000001;

struct PackageEncryptionInfo
{
    sal_uInt8           mpnSalt[ 16 ];
    sal_uInt8           mpnEncrVerifier[ 16 ];
    sal_uInt8           mpnEncrVerifierHash[ 32 ];
    sal_uInt32          mnFlags;
    sal_uInt32          mnAlgorithmId;
    sal_uInt32          mnAlgorithmIdHash;
    sal_uInt32          mnKeySize;
    sal_uInt32          mnSaltSize;
    sal_uInt32          mnVerifierHashSize;
};

void lclRandomGenerateValues( sal_Int32 nLength, sal_uInt8* aArray )
{
    TimeValue aTime;
    osl_getSystemTime( &aTime );
    rtlRandomPool aRandomPool = rtl_random_createPool ();
    rtl_random_addBytes ( aRandomPool, &aTime, 8 );
    rtl_random_getBytes ( aRandomPool, aArray, nLength );
    rtl_random_destroyPool ( aRandomPool );
}

bool lclReadEncryptionInfo( PackageEncryptionInfo& rEncrInfo, BinaryInputStream& rStrm )
{
    rStrm.skip( 4 );
    rStrm >> rEncrInfo.mnFlags;
    if( getFlag( rEncrInfo.mnFlags, ENCRYPTINFO_EXTERNAL ) )
        return false;

    sal_uInt32 nHeaderSize, nRepeatedFlags;
    rStrm >> nHeaderSize >> nRepeatedFlags;
    if( (nHeaderSize < 20) || (nRepeatedFlags != rEncrInfo.mnFlags) )
        return false;

    rStrm.skip( 4 );
    rStrm >> rEncrInfo.mnAlgorithmId >> rEncrInfo.mnAlgorithmIdHash >> rEncrInfo.mnKeySize;
    rStrm.skip( nHeaderSize - 20 );
    rStrm >> rEncrInfo.mnSaltSize;
    if( rEncrInfo.mnSaltSize != 16 )
        return false;

    rStrm.readMemory( rEncrInfo.mpnSalt, 16 );
    rStrm.readMemory( rEncrInfo.mpnEncrVerifier, 16 );
    rStrm >> rEncrInfo.mnVerifierHashSize;
    rStrm.readMemory( rEncrInfo.mpnEncrVerifierHash, 32 );
    return !rStrm.isEof();
}

struct EncryptionStandardHeader {
    sal_uInt32 flags;
    sal_uInt32 sizeExtra;
    sal_uInt32 algId;         // if flag AES && CRYPTOAPI this defaults to 128-bit AES
    sal_uInt32 algIdHash;     // 0 - determined by flags - defaults to SHA-1 if not external
    sal_uInt32 keySize;       // 0 - determined by flags, 128, 192, 256 for AES
    sal_uInt32 providedType;
    sal_uInt32 reserved1;
    sal_uInt32 reserved2;
};

struct EncryptionVerifierAES {
    sal_uInt32 saltSize; // must be 0x00000010
    sal_uInt8  salt[16]; //
    sal_uInt8  encryptedVerifier[16];     // randomly generated verifier value
    sal_uInt32 verifierHashSize;
    sal_uInt8  encryptedVerifierHash[32];
};

bool lclWriteEncryptionInfo( PackageEncryptionInfo& rEncrInfo, BinaryOutputStream& rStream )
{
    const sal_uInt16 versionInfoMajor = 0x003;
    const sal_uInt16 versionInfoMinor = 0x002;

    rStream.writeValue(versionInfoMajor);
    rStream.writeValue(versionInfoMinor);

    const OUString cspName = "Microsoft Enhanced RSA and AES Cryptographic Provider";
    sal_Int32      cspNameSize = (cspName.getLength() * 2) + 2;

    EncryptionStandardHeader encryptionHeader;
    sal_Int32 encryptionHeaderSize = static_cast<sal_Int32>(sizeof(EncryptionStandardHeader));
    memset(&encryptionHeader, 0, encryptionHeaderSize);

    EncryptionVerifierAES encryptionVerifier;
    sal_Int32 encryptionVerifierSize = static_cast<sal_Int32>(sizeof(EncryptionVerifierAES));
    memset(&encryptionVerifier, 0, encryptionVerifierSize);

    rStream << rEncrInfo.mnFlags;

    sal_uInt32 headerSize = encryptionHeaderSize + cspNameSize;
    rStream << headerSize;

    encryptionHeader.flags = rEncrInfo.mnFlags;
    encryptionHeader.algId = rEncrInfo.mnAlgorithmId;
    encryptionHeader.algIdHash = rEncrInfo.mnAlgorithmIdHash;
    encryptionHeader.keySize = rEncrInfo.mnKeySize;
    encryptionHeader.providedType = ENCRYPT_PROVIDER_TYPE_AES;

    rStream.writeMemory(&encryptionHeader, encryptionHeaderSize);
    rStream.writeUnicodeArray(cspName);
    rStream.writeValue<sal_uInt16>(0);

    if (rEncrInfo.mnSaltSize != 16)
        return false;

    encryptionVerifier.saltSize = rEncrInfo.mnSaltSize;

    memcpy(&encryptionVerifier.salt, rEncrInfo.mpnSalt, 16);

    memcpy(&encryptionVerifier.encryptedVerifier, rEncrInfo.mpnEncrVerifier, 16);

    encryptionVerifier.verifierHashSize = rEncrInfo.mnVerifierHashSize;

    memcpy(encryptionVerifier.encryptedVerifierHash, rEncrInfo.mpnEncrVerifierHash, 32);

    rStream.writeMemory(&encryptionVerifier, encryptionVerifierSize);

    return true;
}

void lclDeriveKey( const sal_uInt8* pnHash, sal_uInt32 nHashLen, sal_uInt8* pnKeyDerived, sal_uInt32 nRequiredKeyLen )
{
    sal_uInt8 pnBuffer[ 64 ];
    memset( pnBuffer, 0x36, sizeof( pnBuffer ) );
    for( sal_uInt32 i = 0; i < nHashLen; ++i )
        pnBuffer[ i ] ^= pnHash[ i ];

    rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
    rtl_digest_update( aDigest, pnBuffer, sizeof( pnBuffer ) );
    sal_uInt8 pnX1[ RTL_DIGEST_LENGTH_SHA1 ];
    rtl_digest_get( aDigest, pnX1, RTL_DIGEST_LENGTH_SHA1 );
    rtl_digest_destroy( aDigest );

    memset( pnBuffer, 0x5C, sizeof( pnBuffer ) );
    for( sal_uInt32 i = 0; i < nHashLen; ++i )
        pnBuffer[ i ] ^= pnHash[ i ];

    aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
    rtl_digest_update( aDigest, pnBuffer, sizeof( pnBuffer ) );
    sal_uInt8 pnX2[ RTL_DIGEST_LENGTH_SHA1 ];
    rtl_digest_get( aDigest, pnX2, RTL_DIGEST_LENGTH_SHA1 );
    rtl_digest_destroy( aDigest );

    if( nRequiredKeyLen > RTL_DIGEST_LENGTH_SHA1 )
    {
        memcpy( pnKeyDerived + RTL_DIGEST_LENGTH_SHA1, pnX2, nRequiredKeyLen - RTL_DIGEST_LENGTH_SHA1 );
        nRequiredKeyLen = RTL_DIGEST_LENGTH_SHA1;
    }
    memcpy( pnKeyDerived, pnX1, nRequiredKeyLen );
}

bool lclGenerateVerifier(PackageEncryptionInfo& rEncryptionInfo, const sal_uInt8* pKey, sal_uInt32 nKeySize)
{
    bool bResult = false;

    if (nKeySize != 16)
        return bResult;

    sal_uInt8 aVerifier[16];
    sal_Int32 aVerifierSize = sizeof(aVerifier);
    memset( aVerifier, 0, aVerifierSize );
    lclRandomGenerateValues(aVerifierSize, aVerifier);

#if USE_TLS_OPENSSL
    {
        EVP_CIPHER_CTX aContext;
        EVP_CIPHER_CTX_init( &aContext );
        EVP_EncryptInit_ex( &aContext, EVP_aes_128_ecb(), NULL, pKey, 0 );
        EVP_CIPHER_CTX_set_padding( &aContext, 0 );
        int aEncryptedVerifierSize = 0;
        EVP_EncryptUpdate( &aContext, rEncryptionInfo.mpnEncrVerifier, &aEncryptedVerifierSize, aVerifier, aVerifierSize );
        EVP_CIPHER_CTX_cleanup( &aContext );
    }

#endif // USE_TLS_OPENSSL

    sal_uInt8 pSha1Hash[ 32 ];
    memset(pSha1Hash, 0, 32);
    rEncryptionInfo.mnVerifierHashSize = RTL_DIGEST_LENGTH_SHA1;

    rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
    rtl_digest_update( aDigest, aVerifier, aVerifierSize );
    rtl_digest_get( aDigest, pSha1Hash, RTL_DIGEST_LENGTH_SHA1 );
    rtl_digest_destroy( aDigest );

#if USE_TLS_OPENSSL
    {
        memset(rEncryptionInfo.mpnEncrVerifierHash, 0, rEncryptionInfo.mnVerifierHashSize);
        int written = 0;

        EVP_CIPHER_CTX aContext;
        EVP_CIPHER_CTX_init( &aContext );
        EVP_EncryptInit_ex( &aContext, EVP_aes_128_ecb(), NULL, pKey, 0 );
        EVP_CIPHER_CTX_set_padding( &aContext, 0 );
        EVP_EncryptUpdate( &aContext, rEncryptionInfo.mpnEncrVerifierHash, &written, pSha1Hash, 32 );
        EVP_CIPHER_CTX_cleanup( &aContext );
    }

#endif // USE_TLS_OPENSSL

    bResult = true;

    return bResult;
}


bool lclCheckEncryptionData( const sal_uInt8* pnKey, sal_uInt32 nKeySize, const sal_uInt8* pnVerifier, sal_uInt32 nVerifierSize, const sal_uInt8* pnVerifierHash, sal_uInt32 nVerifierHashSize )
{
    bool bResult = false;

    // the only currently supported algorithm needs key size 128
    if ( nKeySize == 16 && nVerifierSize == 16 )
    {
        // check password
#if USE_TLS_OPENSSL
        EVP_CIPHER_CTX aes_ctx;
        EVP_CIPHER_CTX_init( &aes_ctx );
        EVP_DecryptInit_ex( &aes_ctx, EVP_aes_128_ecb(), 0, pnKey, 0 );
        EVP_CIPHER_CTX_set_padding( &aes_ctx, 0 );
        int nOutLen = 0;
        sal_uInt8 pnTmpVerifier[ 16 ];
        (void) memset( pnTmpVerifier, 0, sizeof(pnTmpVerifier) );

        /*int*/ EVP_DecryptUpdate( &aes_ctx, pnTmpVerifier, &nOutLen, pnVerifier, nVerifierSize );
        EVP_CIPHER_CTX_cleanup( &aes_ctx );

        EVP_CIPHER_CTX_init( &aes_ctx );
        EVP_DecryptInit_ex( &aes_ctx, EVP_aes_128_ecb(), 0, pnKey, 0 );
        EVP_CIPHER_CTX_set_padding( &aes_ctx, 0 );
        sal_uInt8* pnTmpVerifierHash = new sal_uInt8[nVerifierHashSize];
        (void) memset( pnTmpVerifierHash, 0, nVerifierHashSize );

        /*int*/ EVP_DecryptUpdate( &aes_ctx, pnTmpVerifierHash, &nOutLen, pnVerifierHash, nVerifierHashSize );
        EVP_CIPHER_CTX_cleanup( &aes_ctx );
#endif // USE_TLS_OPENSSL

#if USE_TLS_NSS
        PK11SlotInfo *aSlot( PK11_GetBestSlot( CKM_AES_ECB, NULL ) );
        sal_uInt8 *key( new sal_uInt8[ nKeySize ] );
        (void) memcpy( key, pnKey, nKeySize * sizeof(sal_uInt8) );

        SECItem keyItem;
        keyItem.type = siBuffer;
        keyItem.data = key;
        keyItem.len  = nKeySize;

        PK11SymKey *symKey( PK11_ImportSymKey( aSlot, CKM_AES_ECB, PK11_OriginUnwrap, CKA_ENCRYPT, &keyItem, NULL ) );
        SECItem *secParam( PK11_ParamFromIV( CKM_AES_ECB, NULL ) );
        PK11Context *encContext( PK11_CreateContextBySymKey( CKM_AES_ECB, CKA_DECRYPT, symKey, secParam ) );

        int nOutLen(0);
        sal_uInt8 pnTmpVerifier[ 16 ];
        (void) memset( pnTmpVerifier, 0, sizeof(pnTmpVerifier) );

        PK11_CipherOp( encContext, pnTmpVerifier, &nOutLen, sizeof(pnTmpVerifier), const_cast<sal_uInt8*>(pnVerifier), nVerifierSize );

        sal_uInt8* pnTmpVerifierHash = new sal_uInt8[nVerifierHashSize];
        (void) memset( pnTmpVerifierHash, 0, nVerifierHashSize );
        PK11_CipherOp( encContext, pnTmpVerifierHash, &nOutLen, nVerifierHashSize, const_cast<sal_uInt8*>(pnVerifierHash), nVerifierHashSize );

        PK11_DestroyContext( encContext, PR_TRUE );
        PK11_FreeSymKey( symKey );
        SECITEM_FreeItem( secParam, PR_TRUE );
        delete[] key;
#endif // USE_TLS_NSS

        rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
        rtl_digest_update( aDigest, pnTmpVerifier, sizeof( pnTmpVerifier ) );
        sal_uInt8 pnSha1Hash[ RTL_DIGEST_LENGTH_SHA1 ];
        rtl_digest_get( aDigest, pnSha1Hash, RTL_DIGEST_LENGTH_SHA1 );
        rtl_digest_destroy( aDigest );

        bResult = ( memcmp( pnSha1Hash, pnTmpVerifierHash, RTL_DIGEST_LENGTH_SHA1 ) == 0 );
    }

    return bResult;
}

// ----------------------------------------------------------------------------

Sequence< NamedValue > lclGenerateEncryptionKey( const PackageEncryptionInfo& rEncrInfo, const OUString& rPassword, sal_uInt8* pnKey, sal_uInt32 nRequiredKeyLen )
{
    size_t nBufferSize = rEncrInfo.mnSaltSize + 2 * rPassword.getLength();
    sal_uInt8* pnBuffer = new sal_uInt8[ nBufferSize ];
    memcpy( pnBuffer, rEncrInfo.mpnSalt, rEncrInfo.mnSaltSize );

    sal_uInt8* pnPasswordLoc = pnBuffer + rEncrInfo.mnSaltSize;
    const sal_Unicode* pStr = rPassword.getStr();
    for( sal_Int32 i = 0, nLen = rPassword.getLength(); i < nLen; ++i, ++pStr, pnPasswordLoc += 2 )
        ByteOrderConverter::writeLittleEndian( pnPasswordLoc, static_cast< sal_uInt16 >( *pStr ) );

    rtlDigest aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
    rtl_digest_update( aDigest, pnBuffer, nBufferSize );
    delete[] pnBuffer;

    size_t nHashSize = RTL_DIGEST_LENGTH_SHA1 + 4;
    sal_uInt8* pnHash = new sal_uInt8[ nHashSize ];
    rtl_digest_get( aDigest, pnHash + 4, RTL_DIGEST_LENGTH_SHA1 );
    rtl_digest_destroy( aDigest );

    for( sal_uInt32 i = 0; i < 50000; ++i )
    {
        ByteOrderConverter::writeLittleEndian( pnHash, i );
        aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
        rtl_digest_update( aDigest, pnHash, nHashSize );
        rtl_digest_get( aDigest, pnHash + 4, RTL_DIGEST_LENGTH_SHA1 );
        rtl_digest_destroy( aDigest );
    }

    memmove( pnHash, pnHash + 4, RTL_DIGEST_LENGTH_SHA1 );
    memset( pnHash + RTL_DIGEST_LENGTH_SHA1, 0, 4 );
    aDigest = rtl_digest_create( rtl_Digest_AlgorithmSHA1 );
    rtl_digest_update( aDigest, pnHash, nHashSize );
    rtl_digest_get( aDigest, pnHash, RTL_DIGEST_LENGTH_SHA1 );
    rtl_digest_destroy( aDigest );

    lclDeriveKey( pnHash, RTL_DIGEST_LENGTH_SHA1, pnKey, nRequiredKeyLen );
    delete[] pnHash;

    Sequence< NamedValue > aResult;
    if( lclCheckEncryptionData( pnKey, nRequiredKeyLen, rEncrInfo.mpnEncrVerifier, sizeof( rEncrInfo.mpnEncrVerifier ), rEncrInfo.mpnEncrVerifierHash, sizeof( rEncrInfo.mpnEncrVerifierHash ) ) )
    {
        SequenceAsHashMap aEncryptionData;
        aEncryptionData[ "AES128EncryptionKey" ] <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( pnKey ), nRequiredKeyLen );
        aEncryptionData[ "AES128EncryptionSalt" ] <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( rEncrInfo.mpnSalt ), rEncrInfo.mnSaltSize );
        aEncryptionData[ "AES128EncryptionVerifier" ] <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( rEncrInfo.mpnEncrVerifier ), sizeof( rEncrInfo.mpnEncrVerifier ) );
        aEncryptionData[ "AES128EncryptionVerifierHash" ] <<= Sequence< sal_Int8 >( reinterpret_cast< const sal_Int8* >( rEncrInfo.mpnEncrVerifierHash ), sizeof( rEncrInfo.mpnEncrVerifierHash ) );
        aResult = aEncryptionData.getAsConstNamedValueList();
    }

    return aResult;
}

} // namespace

AesEncoder::AesEncoder(Reference< XStream > xDocumentStream, oox::ole::OleStorage& rOleStorage, OUString aPassword) :
    mxDocumentStream(xDocumentStream),
    mrOleStorage(rOleStorage),
    maPassword(aPassword)
{
}

bool AesEncoder::encode()
{
    Reference< XInputStream > xInputStream ( mxDocumentStream->getInputStream(), UNO_SET_THROW );

    if (!mrOleStorage.isStorage())
        return false;

    Reference< XOutputStream > xEncryptionInfo( mrOleStorage.openOutputStream( "EncryptionInfo" ), UNO_SET_THROW );

    PackageEncryptionInfo rEncrInfo;
    rEncrInfo.mnFlags = ENCRYPTINFO_AES | ENCRYPTINFO_CRYPTOAPI;
    rEncrInfo.mnAlgorithmId = ENCRYPT_ALGO_AES128;
    rEncrInfo.mnAlgorithmIdHash = ENCRYPT_HASH_SHA1;
    rEncrInfo.mnKeySize = ENCRYPT_KEY_SIZE_AES_128;

    rEncrInfo.mnSaltSize = 16;

    lclRandomGenerateValues( rEncrInfo.mnSaltSize, rEncrInfo.mpnSalt );

    sal_Int32 keyLength = rEncrInfo.mnKeySize / 8;
    sal_uInt8 key[16];
    memset(key, 0, keyLength);

    lclGenerateEncryptionKey(rEncrInfo, maPassword, key, keyLength);

    lclGenerateVerifier(rEncrInfo, key, keyLength);

    bool aResult = lclCheckEncryptionData(key, keyLength, rEncrInfo.mpnEncrVerifier, 16, rEncrInfo.mpnEncrVerifierHash, 32);
    if (!aResult)
        return false;

    BinaryXOutputStream aEncryptionInfoBinaryOutputStream( xEncryptionInfo, false );
    lclWriteEncryptionInfo( rEncrInfo, aEncryptionInfoBinaryOutputStream );
    aEncryptionInfoBinaryOutputStream.close();

    xEncryptionInfo->flush();
    xEncryptionInfo->closeOutput();

    Reference< XOutputStream > xEncryptedPackage( mrOleStorage.openOutputStream( "EncryptedPackage" ), UNO_SET_THROW );
    BinaryXOutputStream aEncryptedPackageStream( xEncryptedPackage, false );

    BinaryXInputStream aDocumentInputStream( xInputStream, false );
    aDocumentInputStream.seekToStart();

#if USE_TLS_OPENSSL
    EVP_CIPHER_CTX aContext;
    EVP_CIPHER_CTX_init( &aContext );
    EVP_EncryptInit_ex( &aContext, EVP_aes_128_ecb(), NULL, key, 0 );
    EVP_CIPHER_CTX_set_padding( &aContext, 0 );

    sal_uInt8 inBuffer[ 1024 ];
    sal_uInt8 outBuffer[ 1024 ];

    sal_Int32 inLength;
    int outLength;

    aEncryptedPackageStream.writeValue<sal_uInt32>( 0 ); // size
    aEncryptedPackageStream.writeValue<sal_uInt32>( 0 ); // size

    do
    {
        inLength = aDocumentInputStream.readMemory( inBuffer, sizeof( inBuffer ) );
        if (inLength > 0)
        {
            inLength = inLength % 16 == 0 ? inLength : ((inLength/16)*16)+16;
            EVP_EncryptUpdate( &aContext, outBuffer, &outLength, inBuffer, inLength );
            aEncryptedPackageStream.writeMemory( outBuffer, outLength );
        }
    }
    while (inLength > 0);

    EVP_CIPHER_CTX_cleanup( &aContext );

#endif // USE_TLS_OPENSSL

    aEncryptedPackageStream.seekToStart();
    aEncryptedPackageStream.close();

    aDocumentInputStream.seekToStart();
    aDocumentInputStream.close();

    xEncryptedPackage->flush();
    xEncryptedPackage->closeOutput();

    return true;
}

} // namespace core
} // namespace oox

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */