Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/package/inc/ZipPackageStream.hxx
Line
Count
Source (jump to first uncovered line)
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
#ifndef INCLUDED_PACKAGE_INC_ZIPPACKAGESTREAM_HXX
20
#define INCLUDED_PACKAGE_INC_ZIPPACKAGESTREAM_HXX
21
22
#include <com/sun/star/io/XActiveDataSink.hpp>
23
#include <com/sun/star/beans/NamedValue.hpp>
24
#include <com/sun/star/packages/XDataSinkEncrSupport.hpp>
25
#include <com/sun/star/uno/XComponentContext.hpp>
26
#include "ZipPackageEntry.hxx"
27
#include <rtl/ref.hxx>
28
#include <cppuhelper/implbase.hxx>
29
30
#include "EncryptionData.hxx"
31
32
33
685k
#define PACKAGE_STREAM_NOTSET           0
34
699k
#define PACKAGE_STREAM_PACKAGEMEMBER    1
35
229k
#define PACKAGE_STREAM_DETECT           2
36
556
#define PACKAGE_STREAM_DATA             3
37
2.59k
#define PACKAGE_STREAM_RAW              4
38
39
struct ImportedAlgorithms
40
{
41
    sal_Int32 nImportedStartKeyAlgorithm;
42
    sal_Int32 nImportedEncryptionAlgorithm;
43
    // optional because it is not used with AEAD
44
    ::std::optional<sal_Int32> oImportedChecksumAlgorithm;
45
    // GPG encrypted ODF does not have this in the file, but don't use optional
46
    // here because it depends on the nImportedEncryptionAlgorithm of the same
47
    // entry, so theoretically it could be different for different entries.
48
    sal_Int32 nImportedDerivedKeySize;
49
};
50
51
class ZipPackage;
52
struct ZipEntry;
53
class ZipPackageStream final : public cppu::ImplInheritanceHelper
54
<
55
    ZipPackageEntry,
56
    css::io::XActiveDataSink,
57
    css::packages::XDataSinkEncrSupport
58
>
59
{
60
private:
61
    css::uno::Reference < css::io::XInputStream > m_xStream;
62
    ZipPackage          &m_rZipPackage;
63
    bool                 m_bToBeCompressed, m_bToBeEncrypted, m_bHaveOwnKey, m_bIsEncrypted;
64
65
    ::rtl::Reference< BaseEncryptionData > m_xBaseEncryptionData;
66
    css::uno::Sequence< css::beans::NamedValue > m_aStorageEncryptionKeys;
67
    css::uno::Sequence< sal_Int8 > m_aEncryptionKey;
68
69
    ::std::optional<ImportedAlgorithms> m_oImportedAlgorithms;
70
71
    sal_uInt8   m_nStreamMode;
72
    sal_uInt32  m_nMagicalHackPos;
73
    sal_Int64   m_nOwnStreamOrigSize;
74
75
    bool m_bHasSeekable;
76
    bool m_bCompressedIsSetFromOutside;
77
    bool m_bFromManifest;
78
    bool m_bUseWinEncoding;
79
    bool m_bRawStream;
80
81
    /// Check that m_xStream implements io::XSeekable and return it
82
    css::uno::Reference< css::io::XInputStream > const & GetOwnSeekStream();
83
    /// get raw data using unbuffered stream
84
    /// @throws css::uno::RuntimeException
85
    css::uno::Reference< css::io::XInputStream > getRawData();
86
87
public:
88
243k
    bool IsPackageMember () const { return m_nStreamMode == PACKAGE_STREAM_PACKAGEMEMBER;}
89
90
0
    bool IsFromManifest() const { return m_bFromManifest; }
91
0
    void SetFromManifest( bool bValue ) { m_bFromManifest = bValue; }
92
93
    enum class Bugs { None, WinEncodingWrongSHA1, WrongSHA1 };
94
    ::rtl::Reference<EncryptionData> GetEncryptionData(Bugs bugs = Bugs::None);
95
96
    css::uno::Sequence<sal_Int8> GetEncryptionKey(Bugs bugs = Bugs::None);
97
98
    sal_Int32 GetStartKeyGenID() const;
99
100
    sal_Int32 GetEncryptionAlgorithm() const;
101
    sal_Int32 GetIVSize() const;
102
103
0
    void SetToBeCompressed (bool bNewValue) { m_bToBeCompressed = bNewValue;}
104
0
    void SetIsEncrypted (bool bNewValue) { m_bIsEncrypted = bNewValue;}
105
    void SetImportedAlgorithms(ImportedAlgorithms const algorithms)
106
0
    {
107
0
        m_oImportedAlgorithms.emplace(algorithms);
108
0
    }
109
    void SetToBeEncrypted (bool bNewValue)
110
0
    {
111
0
        m_bToBeEncrypted  = bNewValue;
112
0
        if ( m_bToBeEncrypted && !m_xBaseEncryptionData.is())
113
0
            m_xBaseEncryptionData = new BaseEncryptionData;
114
0
        else if ( !m_bToBeEncrypted && m_xBaseEncryptionData.is() )
115
0
            m_xBaseEncryptionData.clear();
116
0
    }
117
    void SetPackageMember (bool bNewValue);
118
119
    void setInitialisationVector (const css::uno::Sequence < sal_Int8 >& rNewVector )
120
0
    { m_xBaseEncryptionData->m_aInitVector = rNewVector;}
121
    void setSalt (const css::uno::Sequence < sal_Int8 >& rNewSalt )
122
0
    { m_xBaseEncryptionData->m_aSalt = rNewSalt;}
123
    void setDigest (const css::uno::Sequence < sal_Int8 >& rNewDigest )
124
0
    { m_xBaseEncryptionData->m_aDigest = rNewDigest;}
125
    void setIterationCount(::std::optional<sal_Int32> const oNewCount)
126
0
    {
127
0
        m_xBaseEncryptionData->m_oPBKDFIterationCount = oNewCount;
128
0
    }
129
    void setArgon2Args(::std::optional<::std::tuple<sal_Int32, sal_Int32, sal_Int32>> const oArgon2Args)
130
0
    {
131
0
        m_xBaseEncryptionData->m_oArgon2Args = oArgon2Args;
132
0
    }
133
    void setSize (const sal_Int64 nNewSize);
134
135
    ZipPackageStream( ZipPackage & rNewPackage,
136
                      const css::uno::Reference < css::uno::XComponentContext >& xContext,
137
                      sal_Int32 nFormat,
138
                      bool bAllowRemoveOnInsert );
139
    virtual ~ZipPackageStream() override;
140
141
    css::uno::Reference< css::io::XInputStream > GetRawEncrStreamNoHeaderCopy();
142
    css::uno::Reference< css::io::XInputStream > TryToGetRawFromDataStream(bool bAddHeaderForEncr );
143
144
    bool ParsePackageRawStream();
145
    virtual bool saveChild( const OUString &rPath,
146
                            std::vector < css::uno::Sequence < css::beans::PropertyValue > > &rManList,
147
                            ZipOutputStream & rZipOut,
148
                            const css::uno::Sequence < sal_Int8 >& rEncryptionKey,
149
                            ::std::optional<sal_Int32> oPBKDF2IterationCount,
150
                            ::std::optional<::std::tuple<sal_Int32, sal_Int32, sal_Int32>> oArgon2Args) override;
151
152
    void setZipEntryOnLoading( const ZipEntry &rInEntry);
153
    void successfullyWritten( ZipEntry const *pEntry );
154
155
    // XActiveDataSink
156
    virtual void SAL_CALL setInputStream( const css::uno::Reference< css::io::XInputStream >& aStream ) override;
157
    virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getInputStream(  ) override;
158
159
    // XDataSinkEncrSupport
160
    virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getDataStream() override;
161
    virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getRawStream() override;
162
    virtual void SAL_CALL setDataStream(
163
                    const css::uno::Reference< css::io::XInputStream >& aStream ) override;
164
    virtual void SAL_CALL setRawStream(
165
                    const css::uno::Reference< css::io::XInputStream >& aStream ) override;
166
    virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getPlainRawStream() override;
167
168
    // XPropertySet
169
    virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const css::uno::Any& aValue ) override;
170
    virtual css::uno::Any SAL_CALL getPropertyValue( const OUString& PropertyName ) override;
171
172
    // XServiceInfo
173
    virtual OUString SAL_CALL getImplementationName(  ) override;
174
    virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
175
    virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) override;
176
};
177
#endif
178
179
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */