Debian Codegraph @

podofo/test/unit/EncryptTest.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2008 by Dominik Seichter <[email protected]>
3 * Copyright (C) 2021 by Francesco Pretto <[email protected]>
4 *
5 * Licensed under GNU Library General Public 2.0 or later.
6 * Some rights reserved. See COPYING, AUTHORS.
7 */
8
9 #include <PdfTest.h>
10
11 using namespace std;
12 using namespace PoDoFo;
13
14 static void testAuthenticate(PdfEncrypt& encrypt);
15 static void testEncrypt(PdfEncrypt& encrypt);
16 static void createEncryptedPdf(const string_view& filename);
17
18 charbuff s_encBuffer;
19 PdfPermissions s_protection;
20
21 #define PDF_USER_PASSWORD "user"
22 #define PDF_OWNER_PASSWORD "podofo"
23
24 struct Paths
25 {
26 Paths()
27 {
28 const char* buffer1 = "Somekind of drawing \001 buffer that possibly \003 could contain PDF drawing commands";
29 const char* buffer2 = " possibly could contain PDF drawing\003 commands";
30
31 size_t len = strlen(buffer1) + 2 * strlen(buffer2);
32 s_encBuffer.resize(len);
33
34 memcpy(s_encBuffer.data(), buffer1, strlen(buffer1) * sizeof(char));
35 memcpy(s_encBuffer.data() + strlen(buffer1), buffer2, strlen(buffer2));
36 memcpy(s_encBuffer.data() + strlen(buffer1) + strlen(buffer2), buffer2, strlen(buffer2));
37
38 s_protection = PdfPermissions::Print |
39 PdfPermissions::Edit |
40 PdfPermissions::Copy |
41 PdfPermissions::EditNotes |
42 PdfPermissions::FillAndSign |
43 PdfPermissions::Accessible |
44 PdfPermissions::DocAssembly |
45 PdfPermissions::HighPrint;
46 }
47 } s_init;
48
49 TEST_CASE("testDefault")
50 {
51 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD);
52 testAuthenticate(*encrypt);
53 testEncrypt(*encrypt);
54 }
55
56 TEST_CASE("testRC4")
57 {
58 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
59 PdfEncryptAlgorithm::RC4V1,
60 PdfKeyLength::L40);
61
62 testAuthenticate(*encrypt);
63 testEncrypt(*encrypt);
64 }
65
66 TEST_CASE("testRC4v2_40")
67 {
68 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
69 PdfEncryptAlgorithm::RC4V2,
70 PdfKeyLength::L40);
71
72 testAuthenticate(*encrypt);
73 testEncrypt(*encrypt);
74 }
75
76 TEST_CASE("testRC4v2_56")
77 {
78 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
79 PdfEncryptAlgorithm::RC4V2,
80 PdfKeyLength::L56);
81
82 testAuthenticate(*encrypt);
83 testEncrypt(*encrypt);
84 }
85
86 TEST_CASE("testRC4v2_80")
87 {
88 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
89 PdfEncryptAlgorithm::RC4V2,
90 PdfKeyLength::L80);
91
92 testAuthenticate(*encrypt);
93 testEncrypt(*encrypt);
94 }
95
96 TEST_CASE("testRC4v2_96")
97 {
98 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
99 PdfEncryptAlgorithm::RC4V2,
100 PdfKeyLength::L96);
101
102 testAuthenticate(*encrypt);
103 testEncrypt(*encrypt);
104 }
105
106 TEST_CASE("testRC4v2_128")
107 {
108 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
109 PdfEncryptAlgorithm::RC4V2,
110 PdfKeyLength::L128);
111
112 testAuthenticate(*encrypt);
113 testEncrypt(*encrypt);
114 }
115
116 TEST_CASE("testAESV2")
117 {
118 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
119 PdfEncryptAlgorithm::AESV2,
120 PdfKeyLength::L128);
121
122 testAuthenticate(*encrypt);
123 // AES decryption is not yet implemented.
124 // Therefore we have to disable this test.
125 //TestEncrypt(encrypt);
126 }
127
128 #ifdef PODOFO_HAVE_LIBIDN
129
130 TEST_CASE("testAESV3")
131 {
132 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
133 PdfEncryptAlgorithm::AESV3,
134 PdfKeyLength::L256);
135
136 testAuthenticate(*encrypt);
137 // AES decryption is not yet implemented.
138 // Therefore we have to disable this test.
139 //TestEncrypt(encrypt);
140 }
141
142 TEST_CASE("testAESV3R6")
143 {
144 auto encrypt = PdfEncrypt::Create(PDF_USER_PASSWORD, PDF_OWNER_PASSWORD, s_protection,
145 PdfEncryptAlgorithm::AESV3R6,
146 PdfKeyLength::L256);
147
148 testAuthenticate(*encrypt);
149 // AES decryption is not yet implemented.
150 // Therefore we have to disable this test.
151 //TestEncrypt(encrypt);
152 }
153
154 #endif // PODOFO_HAVE_LIBIDN
155
156 TEST_CASE("testEnableAlgorithms")
157 {
158 auto enabledAlgorithms = PdfEncrypt::GetEnabledEncryptionAlgorithms();
159
160 // By default every algorithms should be enabled
161 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::RC4V1));
162 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::RC4V2));
163 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::AESV2));
164 #ifdef PODOFO_HAVE_LIBIDN
165 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::AESV3));
166 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::AESV3R6));
167 #endif // PODOFO_HAVE_LIBIDN
168
169 PdfEncryptAlgorithm testAlgorithms = PdfEncryptAlgorithm::AESV2;
170 testAlgorithms |= PdfEncryptAlgorithm::RC4V1 | PdfEncryptAlgorithm::RC4V2;
171 #ifdef PODOFO_HAVE_LIBIDN
172 testAlgorithms |= PdfEncryptAlgorithm::AESV3 | PdfEncryptAlgorithm::AESV3R6;;
173 #endif // PODOFO_HAVE_LIBIDN
174 REQUIRE(testAlgorithms == PdfEncrypt::GetEnabledEncryptionAlgorithms());
175
176 // Disable AES
177 PdfEncrypt::SetEnabledEncryptionAlgorithms(PdfEncryptAlgorithm::RC4V1 |
178 PdfEncryptAlgorithm::RC4V2);
179
180 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::RC4V1));
181 REQUIRE(PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::RC4V2));
182 REQUIRE(!PdfEncrypt::IsEncryptionEnabled(PdfEncryptAlgorithm::AESV2));
183
184 REQUIRE((PdfEncryptAlgorithm::RC4V1 | PdfEncryptAlgorithm::RC4V2) ==
185 PdfEncrypt::GetEnabledEncryptionAlgorithms());
186
187 PdfObject object;
188 object.GetDictionary().AddKey("Filter", PdfName("Standard"));
189 object.GetDictionary().AddKey("V", static_cast<int64_t>(4L));
190 object.GetDictionary().AddKey("R", static_cast<int64_t>(4L));
191 object.GetDictionary().AddKey("P", static_cast<int64_t>(1L));
192 object.GetDictionary().AddKey("O", PdfString(""));
193 object.GetDictionary().AddKey("U", PdfString(""));
194
195 try
196 {
197 (void)PdfEncrypt::CreateFromObject(object);
198 REQUIRE(false);
199 }
200 catch (PdfError& error)
201 {
202 REQUIRE(error.GetCode() == PdfErrorCode::UnsupportedFilter);
203 }
204
205 // Restore default
206 PdfEncrypt::SetEnabledEncryptionAlgorithms(enabledAlgorithms);
207 }
208
209 TEST_CASE("testLoadEncrypedFilePdfParser")
210 {
211 string tempFile = TestUtils::GetTestOutputFilePath("testLoadEncrypedFilePdfParser.pdf");
212 createEncryptedPdf(tempFile);
213
214 auto device = std::make_shared<FileStreamDevice>(tempFile);
215 // Try loading with PdfParser
216 PdfIndirectObjectList objects;
217 PdfParser parser(objects);
218
219 try
220 {
221 parser.Parse(*device, true);
222
223 // Must throw an exception
224 FAIL("Encrypted file not recognized!");
225 }
226 catch (PdfError& e)
227 {
228 if (e.GetCode() != PdfErrorCode::InvalidPassword)
229 FAIL("Invalid encryption exception thrown!");
230 }
231
232 parser.SetPassword(PDF_USER_PASSWORD);
233 }
234
235 TEST_CASE("testLoadEncrypedFilePdfMemDocument")
236 {
237 string tempFile = TestUtils::GetTestOutputFilePath("testLoadEncrypedFilePdfMemDocument.pdf");
238 createEncryptedPdf(tempFile);
239
240 // Try loading with PdfParser
241 PdfMemDocument document;
242 try
243 {
244 document.Load(tempFile);
245
246 // Must throw an exception
247 FAIL("Encrypted file not recognized!");
248 }
249 catch (...)
250 {
251
252 }
253
254 document.Load(tempFile, PDF_USER_PASSWORD);
255 }
256
257 // Test a big encrypted content writing and reading
258 TEST_CASE("TestEncryptBigBuffer")
259 {
260 string tempFile = TestUtils::GetTestOutputFilePath("TestBigBuffer.pdf");
261 PdfReference bufferRef;
262
263 constexpr unsigned BufferSize = 100000;
264
265 {
266 // Create a document with a big enough buffer and ensure it won't
267 // be compressed, so the encryption will operate on a big buffer
268 PdfMemDocument doc;
269 (void)doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
270 auto& obj = doc.GetObjects().CreateDictionaryObject();
271 {
272 vector<char> testBuff(BufferSize);
273 auto stream = obj.GetOrCreateStream().GetOutputStream(PdfFilterList());
274 stream.Write(testBuff.data(), testBuff.size());
275 }
276 bufferRef = obj.GetIndirectReference();
277 doc.GetCatalog().GetDictionary().AddKeyIndirect("TestBigBuffer", obj);
278
279 doc.SetEncrypted(PDF_USER_PASSWORD, "owner");
280 doc.Save(tempFile, PdfSaveOptions::NoFlateCompress);
281 }
282
283 {
284 PdfMemDocument doc;
285 doc.Load(tempFile, PDF_USER_PASSWORD);
286 auto& obj = doc.GetObjects().MustGetObject(bufferRef);
287 charbuff buff;
288 obj.MustGetStream().CopyTo(buff);
289 REQUIRE(buff.size() == BufferSize);
290 }
291 }
292
293 void testAuthenticate(PdfEncrypt& encrypt)
294 {
295 PdfString documentId = PdfString::FromHexData("BF37541A9083A51619AD5924ECF156DF");
296
297 encrypt.GenerateEncryptionKey(documentId);
298
299 INFO("authenticate using user password");
300 REQUIRE(encrypt.Authenticate(PDF_USER_PASSWORD, documentId));
301 INFO("authenticate using owner password");
302 REQUIRE(encrypt.Authenticate(PDF_OWNER_PASSWORD, documentId));
303 INFO("authenticate using wrong password");
304 REQUIRE(!encrypt.Authenticate("wrongpassword", documentId));
305 }
306
307 void testEncrypt(PdfEncrypt& encrypt)
308 {
309 charbuff encrypted;
310 // Encrypt buffer
311 try
312 {
313 encrypt.EncryptTo(encrypted, s_encBuffer, PdfReference(7, 0));
314 }
315 catch (PdfError& e)
316 {
317 FAIL(e.ErrorMessage(e.GetCode()));
318 }
319
320 charbuff decrypted;
321 // Decrypt buffer
322 try
323 {
324 encrypt.DecryptTo(decrypted, encrypted, PdfReference(7, 0));
325 }
326 catch (PdfError& e)
327 {
328 FAIL(e.ErrorMessage(e.GetCode()));
329 }
330
331 INFO("compare encrypted and decrypted buffers");
332 REQUIRE(memcmp(s_encBuffer.data(), decrypted.data(), s_encBuffer.size()) == 0);
333 }
334
335 void createEncryptedPdf(const string_view& filename)
336 {
337 PdfMemDocument doc;
338 auto& page = doc.GetPages().CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4));
339 PdfPainter painter;
340 painter.SetCanvas(page);
341
342 auto font = doc.GetFonts().SearchFont("LiberationSans");
343 if (font == nullptr)
344 FAIL("Coult not find Arial font");
345
346 painter.TextState.SetFont(*font, 16);
347 painter.DrawText("Hello World", 100, 100);
348 painter.FinishDrawing();
349
350 doc.SetEncrypted(PDF_USER_PASSWORD, "owner");
351 doc.Save(filename);
352
353 INFO(utls::Format("Wrote: {} (R={})", filename, doc.GetEncrypt()->GetRevision()));
354 }