summaryrefslogtreecommitdiffstats
path: root/oox/source/crypto/StrongEncryptionDataSpace.cxx
blob: fd1b823b8870e8b714bd52efb045352b2b0d28bd (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
/* -*- 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/crypto/StrongEncryptionDataSpace.hxx>
#include <oox/crypto/AgileEngine.hxx>
#include <oox/crypto/Standard2007Engine.hxx>
#include <oox/helper/binaryoutputstream.hxx>
#include <oox/helper/binaryinputstream.hxx>
#include <com/sun/star/io/SequenceInputStream.hpp>
#include <com/sun/star/io/XSequenceOutputStream.hpp>

#include <comphelper/sequenceashashmap.hxx>
#include <cppuhelper/supportsservice.hxx>

using namespace css;
using namespace css::beans;
using namespace css::io;
using namespace css::lang;
using namespace css::uno;

namespace oox::crypto
{
StrongEncryptionDataSpace::StrongEncryptionDataSpace(const Reference<XComponentContext>& rxContext)
    : mxContext(rxContext)
    , mCryptoEngine(new Standard2007Engine)
{
}

sal_Bool StrongEncryptionDataSpace::generateEncryptionKey(const OUString& rPassword)
{
    if (!mCryptoEngine)
        return false;

    return mCryptoEngine->generateEncryptionKey(rPassword);
}

sal_Bool StrongEncryptionDataSpace::checkDataIntegrity()
{
    if (!mCryptoEngine)
        return false;

    return mCryptoEngine->checkDataIntegrity();
}

sal_Bool StrongEncryptionDataSpace::decrypt(const Reference<XInputStream>& rxInputStream,
                                            Reference<XOutputStream>& rxOutputStream)
{
    if (!mCryptoEngine)
        return false;

    BinaryXInputStream aInputStream(rxInputStream, true);
    BinaryXOutputStream aOutputStream(rxOutputStream, true);

    mCryptoEngine->decrypt(aInputStream, aOutputStream);

    rxOutputStream->flush();
    return true;
}

Reference<XInputStream> StrongEncryptionDataSpace::getStream(const Sequence<NamedValue>& rStreams,
                                                             std::u16string_view sStreamName)
{
    for (const auto& aStream : rStreams)
    {
        if (aStream.Name == sStreamName)
        {
            Sequence<sal_Int8> aSeq;
            aStream.Value >>= aSeq;
            Reference<XInputStream> aStream2(
                io::SequenceInputStream::createStreamFromSequence(mxContext, aSeq),
                UNO_QUERY_THROW);
            return aStream2;
        }
    }
    return nullptr;
}

sal_Bool StrongEncryptionDataSpace::readEncryptionInfo(const Sequence<NamedValue>& aStreams)
{
    Reference<XInputStream> xEncryptionInfo = getStream(aStreams, u"EncryptionInfo");
    if (!xEncryptionInfo.is())
        return false;

    BinaryXInputStream aBinaryInputStream(xEncryptionInfo, true);
    sal_uInt32 aVersion = aBinaryInputStream.readuInt32();

    switch (aVersion)
    {
        case msfilter::VERSION_INFO_2007_FORMAT:
        case msfilter::VERSION_INFO_2007_FORMAT_SP2:
            mCryptoEngine.reset(new Standard2007Engine);
            break;
        case msfilter::VERSION_INFO_AGILE:
            mCryptoEngine.reset(new AgileEngine());
            break;
        default:
            break;
    }

    if (!mCryptoEngine)
        return false;

    return mCryptoEngine->readEncryptionInfo(xEncryptionInfo);
}

sal_Bool StrongEncryptionDataSpace::setupEncryption(const Sequence<NamedValue>& rMediaEncData)
{
    if (!mCryptoEngine)
        return false;

    OUString sPassword;
    for (const auto& aParam : rMediaEncData)
    {
        if (aParam.Name == "OOXPassword")
        {
            aParam.Value >>= sPassword;
        }
    }

    return mCryptoEngine->setupEncryption(sPassword);
}

Sequence<NamedValue> StrongEncryptionDataSpace::createEncryptionData(const OUString& rPassword)
{
    comphelper::SequenceAsHashMap aEncryptionData;
    aEncryptionData["OOXPassword"] <<= rPassword;
    aEncryptionData["CryptoType"] <<= OUString("StrongEncryptionDataSpace");

    return aEncryptionData.getAsConstNamedValueList();
}

Sequence<NamedValue>
StrongEncryptionDataSpace::encrypt(const Reference<XInputStream>& rxInputStream)
{
    if (!mCryptoEngine)
        return Sequence<NamedValue>();

    Reference<XSeekable> xSeekable(rxInputStream, UNO_QUERY);
    if (!xSeekable.is())
        return Sequence<NamedValue>();

    sal_uInt32 aLength = xSeekable->getLength(); // check length of the stream

    Reference<XOutputStream> xOutputStream(
        mxContext->getServiceManager()->createInstanceWithContext(
            "com.sun.star.io.SequenceOutputStream", mxContext),
        UNO_QUERY);

    mCryptoEngine->encrypt(rxInputStream, xOutputStream, aLength);

    comphelper::SequenceAsHashMap aStreams;

    Reference<XSequenceOutputStream> xEncodedFileSequenceStream(xOutputStream, UNO_QUERY);
    aStreams["EncryptedPackage"] <<= xEncodedFileSequenceStream->getWrittenBytes();

    Reference<XOutputStream> aEncryptionInfoStream(
        mxContext->getServiceManager()->createInstanceWithContext(
            "com.sun.star.io.SequenceOutputStream", mxContext),
        UNO_QUERY);
    BinaryXOutputStream rStream(aEncryptionInfoStream, false);
    mCryptoEngine->writeEncryptionInfo(rStream);
    aEncryptionInfoStream->flush();
    Reference<XSequenceOutputStream> aEncryptionInfoSequenceStream(aEncryptionInfoStream,
                                                                   UNO_QUERY);

    aStreams["EncryptionInfo"] <<= aEncryptionInfoSequenceStream->getWrittenBytes();

    return aStreams.getAsConstNamedValueList();
}

OUString SAL_CALL StrongEncryptionDataSpace::getImplementationName()
{
    return "com.sun.star.comp.oox.crypto.StrongEncryptionDataSpace";
}

sal_Bool SAL_CALL StrongEncryptionDataSpace::supportsService(const OUString& rServiceName)
{
    return cppu::supportsService(this, rServiceName);
}

css::uno::Sequence<OUString> SAL_CALL StrongEncryptionDataSpace::getSupportedServiceNames()
{
    Sequence<OUString> aServices{ "com.sun.star.packages.PackageEncryption" };
    return aServices;
}

} // namespace oox::crypto

extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
com_sun_star_comp_oox_crypto_StrongEncryptionDataSpace_get_implementation(
    uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
{
    return cppu::acquire(new oox::crypto::StrongEncryptionDataSpace(pCtx));
}

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