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 .
19 #ifndef INCLUDED_PACKAGE_INC_ZIPPACKAGESTREAM_HXX
20 #define INCLUDED_PACKAGE_INC_ZIPPACKAGESTREAM_HXX
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>
30 #include "EncryptionData.hxx"
33 #define PACKAGE_STREAM_NOTSET 0
34 #define PACKAGE_STREAM_PACKAGEMEMBER 1
35 #define PACKAGE_STREAM_DETECT 2
36 #define PACKAGE_STREAM_DATA 3
37 #define PACKAGE_STREAM_RAW 4
39 struct ImportedAlgorithms
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
;
53 class ZipPackageStream final
: public cppu::ImplInheritanceHelper
56 css::io::XActiveDataSink
,
57 css::packages::XDataSinkEncrSupport
61 css::uno::Reference
< css::io::XInputStream
> m_xStream
;
62 ZipPackage
&m_rZipPackage
;
63 bool m_bToBeCompressed
, m_bToBeEncrypted
, m_bHaveOwnKey
, m_bIsEncrypted
;
65 ::rtl::Reference
< BaseEncryptionData
> m_xBaseEncryptionData
;
66 css::uno::Sequence
< css::beans::NamedValue
> m_aStorageEncryptionKeys
;
67 css::uno::Sequence
< sal_Int8
> m_aEncryptionKey
;
69 ::std::optional
<ImportedAlgorithms
> m_oImportedAlgorithms
;
71 sal_uInt8 m_nStreamMode
;
72 sal_uInt32 m_nMagicalHackPos
;
73 sal_Int64 m_nOwnStreamOrigSize
;
76 bool m_bCompressedIsSetFromOutside
;
78 bool m_bUseWinEncoding
;
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();
88 bool IsPackageMember () const { return m_nStreamMode
== PACKAGE_STREAM_PACKAGEMEMBER
;}
90 bool IsFromManifest() const { return m_bFromManifest
; }
91 void SetFromManifest( bool bValue
) { m_bFromManifest
= bValue
; }
93 enum class Bugs
{ None
, WinEncodingWrongSHA1
, WrongSHA1
};
94 ::rtl::Reference
<EncryptionData
> GetEncryptionData(Bugs bugs
= Bugs::None
);
96 css::uno::Sequence
<sal_Int8
> GetEncryptionKey(Bugs bugs
= Bugs::None
);
98 sal_Int32
GetStartKeyGenID() const;
100 sal_Int32
GetEncryptionAlgorithm() const;
101 sal_Int32
GetIVSize() const;
103 void SetToBeCompressed (bool bNewValue
) { m_bToBeCompressed
= bNewValue
;}
104 void SetIsEncrypted (bool bNewValue
) { m_bIsEncrypted
= bNewValue
;}
105 void SetImportedAlgorithms(ImportedAlgorithms
const algorithms
)
107 m_oImportedAlgorithms
.emplace(algorithms
);
109 void SetToBeEncrypted (bool bNewValue
)
111 m_bToBeEncrypted
= bNewValue
;
112 if ( m_bToBeEncrypted
&& !m_xBaseEncryptionData
.is())
113 m_xBaseEncryptionData
= new BaseEncryptionData
;
114 else if ( !m_bToBeEncrypted
&& m_xBaseEncryptionData
.is() )
115 m_xBaseEncryptionData
.clear();
117 void SetPackageMember (bool bNewValue
);
119 void setInitialisationVector (const css::uno::Sequence
< sal_Int8
>& rNewVector
)
120 { m_xBaseEncryptionData
->m_aInitVector
= rNewVector
;}
121 void setSalt (const css::uno::Sequence
< sal_Int8
>& rNewSalt
)
122 { m_xBaseEncryptionData
->m_aSalt
= rNewSalt
;}
123 void setDigest (const css::uno::Sequence
< sal_Int8
>& rNewDigest
)
124 { m_xBaseEncryptionData
->m_aDigest
= rNewDigest
;}
125 void setIterationCount(::std::optional
<sal_Int32
> const oNewCount
)
127 m_xBaseEncryptionData
->m_oPBKDFIterationCount
= oNewCount
;
129 void setArgon2Args(::std::optional
<::std::tuple
<sal_Int32
, sal_Int32
, sal_Int32
>> const oArgon2Args
)
131 m_xBaseEncryptionData
->m_oArgon2Args
= oArgon2Args
;
133 void setSize (const sal_Int64 nNewSize
);
135 ZipPackageStream( ZipPackage
& rNewPackage
,
136 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
138 bool bAllowRemoveOnInsert
);
139 virtual ~ZipPackageStream() override
;
141 css::uno::Reference
< css::io::XInputStream
> GetRawEncrStreamNoHeaderCopy();
142 css::uno::Reference
< css::io::XInputStream
> TryToGetRawFromDataStream(bool bAddHeaderForEncr
);
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
;
152 void setZipEntryOnLoading( const ZipEntry
&rInEntry
);
153 void successfullyWritten( ZipEntry
const *pEntry
);
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
;
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
;
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
;
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
;
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */