Coverage Report

Created: 2026-04-09 11:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/inc/pdf/PDFEncryptorR6.hxx
Line
Count
Source
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
10
#pragma once
11
12
#include <rtl/ustring.hxx>
13
#include <string_view>
14
#include <vector>
15
#include <vcl/dllapi.h>
16
#include <pdf/IPDFEncryptor.hxx>
17
18
namespace vcl::pdf
19
{
20
class EncryptionHashTransporter;
21
22
/** Algorithm 2.B: Computing a hash (revision 6 and later)
23
 *
24
 * Described in ISO 32000-2:2020(E) - 7.6.4.3.4
25
 */
26
VCL_DLLPUBLIC std::vector<sal_uInt8>
27
computeHashR6(const sal_uInt8* pPassword, size_t nPasswordLength,
28
              std::vector<sal_uInt8> const& rValidationSalt,
29
              std::vector<sal_uInt8> const& rUserKey = std::vector<sal_uInt8>());
30
31
/** Algorithm 11: Authenticating the user password (Security handlers of revision 6)
32
 *
33
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.10
34
 */
35
VCL_DLLPUBLIC bool validateUserPassword(const sal_uInt8* pUserPass, size_t nPasswordLength,
36
                                        std::vector<sal_uInt8>& U);
37
38
/** Algorithm 12: Authenticating the owner password (Security handlers of revision 6)
39
 *
40
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.11
41
 */
42
VCL_DLLPUBLIC bool validateOwnerPassword(const sal_uInt8* pUserPass, size_t nPasswordLength,
43
                                         const std::vector<sal_uInt8>& U,
44
                                         std::vector<sal_uInt8>& O);
45
46
/** Generates the encryption key - random data 32-byte */
47
VCL_DLLPUBLIC std::vector<sal_uInt8> generateKey();
48
49
/** Algorithm 8: U and UE
50
 *
51
 * Computing the encryption dictionary’s U (user password) and UE (user encryption) values
52
 * (Security handlers of revision 6)
53
 *
54
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.7
55
 */
56
VCL_DLLPUBLIC void generateUandUE(const sal_uInt8* pUserPass, size_t nPasswordLength,
57
                                  std::vector<sal_uInt8>& rFileEncryptionKey,
58
                                  std::vector<sal_uInt8>& U, std::vector<sal_uInt8>& UE);
59
60
/** Algorithm 9: O and OE
61
 *
62
 * Computing the encryption dictionary’s O (owner password) and OE (owner encryption) values
63
 * (Security handlers of revision 6)
64
 *
65
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.8
66
 */
67
VCL_DLLPUBLIC void generateOandOE(const sal_uInt8* pUserPass, size_t nPasswordLength,
68
                                  std::vector<sal_uInt8>& rFileEncryptionKey,
69
                                  const std::vector<sal_uInt8>& U, std::vector<sal_uInt8>& O,
70
                                  std::vector<sal_uInt8>& OE);
71
72
/** Algorithm 8 step b) in reverse
73
 *
74
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.7
75
 *
76
 * - compute the hash with password and user key salt
77
 * - decrypt with hash as key and zero IV
78
 */
79
VCL_DLLPUBLIC std::vector<sal_uInt8> decryptKey(const sal_uInt8* pUserPass, size_t nPasswordLength,
80
                                                std::vector<sal_uInt8>& U,
81
                                                std::vector<sal_uInt8>& UE);
82
83
/** Algorithm 13: Validating the permissions (Security handlers of revision 6)
84
 *
85
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.12
86
 */
87
VCL_DLLPUBLIC std::vector<sal_uInt8> decryptPerms(std::vector<sal_uInt8>& rPermsEncrypted,
88
                                                  std::vector<sal_uInt8>& rFileEncryptionKey);
89
90
/** Algorithm 10 step f)
91
 *
92
 * Computing the encryption dictionary’s Perms (permissions) value (Security handlers of revision 6)
93
 *
94
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.9
95
 */
96
VCL_DLLPUBLIC std::vector<sal_uInt8> encryptPerms(std::vector<sal_uInt8>& rPerms,
97
                                                  std::vector<sal_uInt8>& rFileEncryptionKey);
98
99
/** Algorithm 10 steps a) - e)
100
 *
101
 * Computing the encryption dictionary’s Perms (permissions) value (Security handlers of revision 6)
102
 *
103
 * Described in ISO 32000-2:2020(E) - 7.6.4.4.9
104
 */
105
VCL_DLLPUBLIC std::vector<sal_uInt8> createPerms(sal_Int32 nAccessPermissions,
106
                                                 bool bEncryptMetadata);
107
108
/** Padding as described in Internet RFC 8018
109
 *
110
 * Described in ISO 32000-2:2020(E) - 7.6.3.1
111
 */
112
VCL_DLLPUBLIC size_t addPaddingToVector(std::vector<sal_uInt8>& rVector, size_t nBlockSize);
113
114
class EncryptionContext;
115
116
/** IPDFEncryptor implementation of PDF encryption version 5 revision 6 added in PDF 2.0
117
 *
118
 * The complete algorithm is defined in PDF 2.0 specification ISO 32000-2:2020(E)
119
 */
120
class VCL_DLLPUBLIC PDFEncryptorR6 : public IPDFEncryptor
121
{
122
    std::unique_ptr<EncryptionContext> m_pEncryptionContext;
123
    sal_Int32 m_nAccessPermissions = 0;
124
125
public:
126
    PDFEncryptorR6();
127
    ~PDFEncryptorR6();
128
129
0
    sal_Int32 getVersion() override { return 5; }
130
0
    sal_Int32 getRevision() override { return 6; }
131
0
    sal_Int32 getAccessPermissions() override { return m_nAccessPermissions; }
132
0
    bool isMetadataEncrypted() override { return true; }
133
    /** Key length - AES 256 bit */
134
0
    sal_Int32 getKeyLength() override { return 256 / 8; }
135
136
    std::vector<sal_uInt8> getEncryptedAccessPermissions(std::vector<sal_uInt8>& rKey) override;
137
138
    static void initEncryption(EncryptionHashTransporter& rEncryptionHashTransporter,
139
                               const OUString& i_rOwnerPassword, const OUString& i_rUserPassword);
140
141
    bool prepareEncryption(
142
        const css::uno::Reference<css::beans::XMaterialHolder>& xEncryptionMaterialHolder,
143
        PDFEncryptionProperties& rProperties) override;
144
145
    void setupKeysAndCheck(PDFEncryptionProperties& rProperties) override;
146
147
    sal_uInt64 calculateSizeIncludingHeader(sal_uInt64 nSize) override;
148
149
    void setupEncryption(std::vector<sal_uInt8>& rEncryptionKey, sal_Int32 nObject) override;
150
151
    /** Encrypts using Algorithm 1.A: Encryption of data using the AES algorithms
152
     *
153
     * Described in ISO 32000-2:2020(E) - 7.6.3.3
154
     */
155
    void encrypt(const void* pInput, sal_uInt64 nInputSize, std::vector<sal_uInt8>& rOutput,
156
                 sal_uInt64 nOutputsSize) override;
157
158
    void encryptWithIV(const void* pInput, sal_uInt64 nInputSize, std::vector<sal_uInt8>& rOutput,
159
                       std::vector<sal_uInt8>& rIV);
160
};
161
162
} // end vcl::pdf
163
164
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */