summaryrefslogtreecommitdiff
path: root/vcl/inc/pdf/PDFEncryptor.hxx
blob: 9a65c5a0043bc57b179cb8b8f67fcfedefaf787a (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
/* -*- 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/.
 *
 */

#pragma once

#include <rtl/cipher.h>
#include <rtl/ustring.hxx>
#include <vector>
#include <pdf/IPDFEncryptor.hxx>

namespace com::sun::star::uno
{
template <typename> class Reference;
}

namespace vcl::pdf
{
class EncryptionHashTransporter;

class PDFEncryptor : public IPDFEncryptor
{
private:
    /* the numerical value of the access permissions, according to PDF spec, must be signed */
    sal_Int32 m_nAccessPermissions = 0;

    /* The encryption key, formed with the user password according to algorithm 3.2,
     * maximum length is 16 bytes + 3 + 2 for 128 bit security */

    sal_Int32 m_nKeyLength = 0; // key length, 16 or 5
    sal_Int32 m_nRC4KeyLength = 0; // key length, 16 or 10, to be input to the algorithm 3.1

    /* used to cipher the stream data and for password management */
    rtlCipher m_aCipher = nullptr;

public:
    PDFEncryptor();
    ~PDFEncryptor();

    sal_Int32 getVersion() override { return 2; };
    sal_Int32 getRevision() override { return 3; };

    sal_Int32 getAccessPermissions() override { return m_nAccessPermissions; }
    sal_Int32 getKeyLength() override { return m_nKeyLength; }
    sal_Int32 getRC4KeyLength() { return m_nRC4KeyLength; }

    static void initEncryption(EncryptionHashTransporter& rEncryptionHashTransporter,
                               const OUString& i_rOwnerPassword, const OUString& i_rUserPassword);

    bool prepareEncryption(
        const css::uno::Reference<css::beans::XMaterialHolder>& xEncryptionMaterialHolder,
        PDFEncryptionProperties& rProperties) override;

    void setupKeysAndCheck(PDFEncryptionProperties& rProperties) override;

    void setupEncryption(std::vector<sal_uInt8>& rEncryptionKey, sal_Int32 nObject) override;

    void encrypt(const void* pInput, sal_uInt64 nInputSize, std::vector<sal_uInt8>& rOutput,
                 sal_uInt64 nOutputSize) override;
};
}

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