/* -*- 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/.
*/
// Load the exported result in PDFium
std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument = parsePDFExport("secret"_ostr);
CPPUNIT_ASSERT_EQUAL(1, pPdfDocument->getPageCount()); int nFileVersion = pPdfDocument->getFileVersion();
CPPUNIT_ASSERT_EQUAL(17, nFileVersion);
}
// Load the exported result in PDFium
std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument = parsePDFExport("secret"_ostr);
CPPUNIT_ASSERT_EQUAL(1, pPdfDocument->getPageCount()); int nFileVersion = pPdfDocument->getFileVersion();
CPPUNIT_ASSERT_EQUAL(20, nFileVersion);
}
// Generate the U and UE from the user password and encrypt the // encryption key into UE
vcl::pdf::generateUandUE(pUserPass, 4, aInputKey, U, UE);
// Checks that the U validates the password (would fail if the U // would be calculated wrongly).
CPPUNIT_ASSERT_EQUAL(true, vcl::pdf::validateUserPassword(pUserPass, 4, U));
// Decrypt the key - this would fail if U and UE would be calculated // wrongly auto aDecryptedKey = vcl::pdf::decryptKey(pUserPass, 4, U, UE);
// Decrypted key and input key need to match
CPPUNIT_ASSERT_EQUAL(comphelper::hashToString(aInputKey),
comphelper::hashToString(aDecryptedKey));
}
CPPUNIT_TEST_FIXTURE(PDFEncryptionTest, testGenerateOandOE)
{ // Checks we calculate O and OE correctly
// Generates U and UE - we need U in generateOandOE
vcl::pdf::generateUandUE(aUserPass.data(), aUserPass.size(), aInputKey, U, UE);
vcl::pdf::generateOandOE(aOwnerPass.data(), aOwnerPass.size(), aInputKey, U, O, OE);
// Checks the user password is valid
CPPUNIT_ASSERT_EQUAL(true,
vcl::pdf::validateUserPassword(aUserPass.data(), aUserPass.size(), U));
// Checks the owner password is valid
CPPUNIT_ASSERT_EQUAL( true, vcl::pdf::validateOwnerPassword(aOwnerPass.data(), aOwnerPass.size(), U, O));
}
CPPUNIT_TEST_FIXTURE(PDFEncryptionTest, testPermsEncryption)
{ // Encrypts file permissions for /Perms entry
// We use a existing encrypted /Perm to validate the decryption and re-encryption // algorithm works correctly.
// U and UE taken from an PDF that was encrypted with "Test" as password
std::vector<sal_uInt8> U = parseHex("7BD210807A0277FECC52C261C442F02E1AD62C1A23553348B8F8AF7320" "DC9978FAB7E65E1BF4CA76F4BE5E6D2AA8C7D5");
std::vector<sal_uInt8> UE
= parseHex("67022D91A6BDF3179F488DC9658E54B78A0AD05C6A9C419DCD17A6941C151197");
// We decrypt the key, which is needed to decrypt and encrypt the /Perms content
std::vector<sal_uInt8> aKey = vcl::pdf::decryptKey(pUserPass, 4, U, UE);
// Known encrypted /Perms content taken from the PDF
std::vector<sal_uInt8> aPermEncrypted = parseHex("6a2306c6e5e71a5bbd8404b07abec38f");
// Encrypt again
std::vector<sal_uInt8> aPermsReencrypted = vcl::pdf::encryptPerms(aPermsDecrpyted, aKey);
// Original encrypted /Perm content should be equal to decrypted and encrypted again
CPPUNIT_ASSERT_EQUAL(std::string("6a2306c6e5e71a5bbd8404b07abec38f"),
comphelper::hashToString(aPermsReencrypted));
// Always should be a,b,d
CPPUNIT_ASSERT_EQUAL(sal_uInt8('a'), aPermsDecrpyted[9]);
CPPUNIT_ASSERT_EQUAL(sal_uInt8('d'), aPermsDecrpyted[10]);
CPPUNIT_ASSERT_EQUAL(sal_uInt8('b'), aPermsDecrpyted[11]);
// Metadata encrypted? - T or F
CPPUNIT_ASSERT_EQUAL(sal_uInt8('T'), aPermsDecrpyted[8]);
// Taken from the PDF (/P entry)
sal_Int32 nExpectedAccessPermisssions = -4;
CPPUNIT_ASSERT_EQUAL(nExpectedAccessPermisssions, aAccessPermissions);
// the whole decrypted /Perms content
CPPUNIT_ASSERT_EQUAL(std::string("fcffffffffffffff54616462bb609a8a"),
comphelper::hashToString(aPermsDecrpyted));
// Check the creating /Perm content from access permissions works correctly
std::vector<sal_uInt8> aPermsCreated = vcl::pdf::createPerms(nExpectedAccessPermisssions, true);
// Last 12 bytes are random, so we shouldn't check those
std::vector<sal_uInt8> aPermsWithoutRandomBytes(aPermsCreated.begin(),
aPermsCreated.begin() + 12);
// Should match the decrypted content
CPPUNIT_ASSERT_EQUAL(std::string("fcffffffffffffff54616462"),
comphelper::hashToString(aPermsWithoutRandomBytes));
}
CPPUNIT_TEST_FIXTURE(PDFEncryptionTest, testFileEncryption_checkDifferentIV)
{ // Check each call to encrypt is using a different IV (initialization vector)
// All IV should be different
CPPUNIT_ASSERT(!std::equal(aIV_1.begin(), aIV_1.end(), aIV_2.begin()));
CPPUNIT_ASSERT(!std::equal(aIV_1.begin(), aIV_1.end(), aIV_3.begin()));
CPPUNIT_ASSERT(!std::equal(aIV_2.begin(), aIV_2.end(), aIV_3.begin()));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.