2008-04-29 12:55:25 +00:00
|
|
|
// This file implements methods from the QPDF class that involve
|
|
|
|
// encryption.
|
|
|
|
|
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QPDFExc.hh>
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/Pl_RC4.hh>
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <qpdf/Pl_AES_PDF.hh>
|
2009-10-17 23:37:55 +00:00
|
|
|
#include <qpdf/Pl_Buffer.hh>
|
2012-12-30 02:31:21 +00:00
|
|
|
#include <qpdf/Pl_SHA2.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/RC4.hh>
|
|
|
|
#include <qpdf/MD5.hh>
|
|
|
|
|
2013-11-29 15:20:50 +00:00
|
|
|
#include <algorithm>
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <assert.h>
|
2008-05-04 16:02:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-06-20 14:56:36 +00:00
|
|
|
static unsigned char const padding_string[] = {
|
2008-04-29 12:55:25 +00:00
|
|
|
0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
|
|
|
|
0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
|
|
|
|
0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
|
|
|
|
0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int const key_bytes = 32;
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
// V4 key lengths apply to V <= 4
|
|
|
|
static unsigned int const OU_key_bytes_V4 = sizeof(MD5::Digest);
|
|
|
|
|
|
|
|
static unsigned int const OU_key_bytes_V5 = 48;
|
|
|
|
static unsigned int const OUE_key_bytes_V5 = 32;
|
|
|
|
static unsigned int const Perms_key_bytes_V5 = 16;
|
|
|
|
|
2012-12-26 21:04:54 +00:00
|
|
|
int
|
|
|
|
QPDF::EncryptionData::getV() const
|
|
|
|
{
|
|
|
|
return this->V;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
QPDF::EncryptionData::getR() const
|
|
|
|
{
|
|
|
|
return this->R;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
QPDF::EncryptionData::getLengthBytes() const
|
|
|
|
{
|
|
|
|
return this->Length_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
QPDF::EncryptionData::getP() const
|
|
|
|
{
|
|
|
|
return this->P;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
QPDF::EncryptionData::getO() const
|
|
|
|
{
|
|
|
|
return this->O;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
QPDF::EncryptionData::getU() const
|
|
|
|
{
|
|
|
|
return this->U;
|
|
|
|
}
|
|
|
|
|
2012-12-26 22:10:25 +00:00
|
|
|
std::string const&
|
|
|
|
QPDF::EncryptionData::getOE() const
|
|
|
|
{
|
|
|
|
return this->OE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
QPDF::EncryptionData::getUE() const
|
|
|
|
{
|
|
|
|
return this->UE;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
QPDF::EncryptionData::getPerms() const
|
|
|
|
{
|
|
|
|
return this->Perms;
|
|
|
|
}
|
|
|
|
|
2012-12-26 21:04:54 +00:00
|
|
|
std::string const&
|
|
|
|
QPDF::EncryptionData::getId1() const
|
|
|
|
{
|
|
|
|
return this->id1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::EncryptionData::getEncryptMetadata() const
|
|
|
|
{
|
|
|
|
return this->encrypt_metadata;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
2012-12-26 21:04:54 +00:00
|
|
|
QPDF::EncryptionData::setO(std::string const& O)
|
|
|
|
{
|
|
|
|
this->O = O;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::EncryptionData::setU(std::string const& U)
|
|
|
|
{
|
|
|
|
this->U = U;
|
|
|
|
}
|
|
|
|
|
2012-12-26 22:10:25 +00:00
|
|
|
void
|
|
|
|
QPDF::EncryptionData::setV5EncryptionParameters(
|
|
|
|
std::string const& O,
|
|
|
|
std::string const& OE,
|
|
|
|
std::string const& U,
|
|
|
|
std::string const& UE,
|
|
|
|
std::string const& Perms)
|
|
|
|
{
|
|
|
|
this->O = O;
|
|
|
|
this->OE = OE;
|
|
|
|
this->U = U;
|
|
|
|
this->UE = UE;
|
|
|
|
this->Perms = Perms;
|
|
|
|
}
|
|
|
|
|
2012-12-26 21:04:54 +00:00
|
|
|
static void
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4(std::string const& password, char k1[key_bytes])
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
int password_bytes = std::min(static_cast<size_t>(key_bytes),
|
|
|
|
password.length());
|
2008-04-29 12:55:25 +00:00
|
|
|
int pad_bytes = key_bytes - password_bytes;
|
|
|
|
memcpy(k1, password.c_str(), password_bytes);
|
|
|
|
memcpy(k1 + password_bytes, padding_string, pad_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::trim_user_password(std::string& user_password)
|
|
|
|
{
|
|
|
|
// Although unnecessary, this routine trims the padding string
|
|
|
|
// from the end of a user password. Its only purpose is for
|
|
|
|
// recovery of user passwords which is done in the test suite.
|
|
|
|
char const* cstr = user_password.c_str();
|
|
|
|
size_t len = user_password.length();
|
|
|
|
if (len < key_bytes)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-28 17:21:11 +00:00
|
|
|
char const* p1 = cstr;
|
|
|
|
char const* p2 = 0;
|
|
|
|
while ((p2 = strchr(p1, '\x28')) != 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-12-28 17:21:11 +00:00
|
|
|
if (memcmp(p2, padding_string, len - (p2 - cstr)) == 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-12-28 17:21:11 +00:00
|
|
|
user_password = user_password.substr(0, p2 - cstr);
|
2008-04-29 12:55:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-12-28 17:21:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption skip 0x28");
|
|
|
|
p1 = p2 + 1;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4(std::string const& password)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
char k1[key_bytes];
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4(password, k1);
|
2008-04-29 12:55:25 +00:00
|
|
|
return std::string(k1, key_bytes);
|
|
|
|
}
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
static std::string
|
|
|
|
truncate_password_V5(std::string const& password)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
return password.substr(
|
|
|
|
0, std::min(static_cast<size_t>(127), password.length()));
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
static void
|
|
|
|
iterate_md5_digest(MD5& md5, MD5::Digest& digest, int iterations)
|
|
|
|
{
|
|
|
|
md5.digest(digest);
|
|
|
|
|
|
|
|
for (int i = 0; i < iterations; ++i)
|
|
|
|
{
|
|
|
|
MD5 m;
|
2013-02-24 02:46:21 +00:00
|
|
|
m.encodeDataIncrementally(reinterpret_cast<char*>(digest),
|
|
|
|
sizeof(digest));
|
2008-04-29 12:55:25 +00:00
|
|
|
m.digest(digest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
iterate_rc4(unsigned char* data, int data_len,
|
|
|
|
unsigned char* okey, int key_len,
|
|
|
|
int iterations, bool reverse)
|
|
|
|
{
|
|
|
|
unsigned char* key = new unsigned char[key_len];
|
|
|
|
for (int i = 0; i < iterations; ++i)
|
|
|
|
{
|
|
|
|
int const xor_value = (reverse ? iterations - 1 - i : i);
|
|
|
|
for (int j = 0; j < key_len; ++j)
|
|
|
|
{
|
|
|
|
key[j] = okey[j] ^ xor_value;
|
|
|
|
}
|
|
|
|
RC4 rc4(key, key_len);
|
|
|
|
rc4.process(data, data_len);
|
|
|
|
}
|
|
|
|
delete [] key;
|
|
|
|
}
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
static std::string
|
|
|
|
process_with_aes(std::string const& key,
|
|
|
|
bool encrypt,
|
|
|
|
std::string const& data,
|
|
|
|
size_t outlength = 0,
|
|
|
|
unsigned int repetitions = 1,
|
|
|
|
unsigned char const* iv = 0,
|
|
|
|
size_t iv_length = 0)
|
|
|
|
{
|
|
|
|
Pl_Buffer buffer("buffer");
|
|
|
|
Pl_AES_PDF aes("aes", &buffer, encrypt,
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::unsigned_char_pointer(key),
|
|
|
|
key.length());
|
2012-12-30 02:31:21 +00:00
|
|
|
if (iv)
|
|
|
|
{
|
|
|
|
aes.setIV(iv, iv_length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aes.useZeroIV();
|
|
|
|
}
|
|
|
|
aes.disablePadding();
|
|
|
|
for (unsigned int i = 0; i < repetitions; ++i)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
aes.write(QUtil::unsigned_char_pointer(data), data.length());
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
aes.finish();
|
|
|
|
PointerHolder<Buffer> bufp = buffer.getBuffer();
|
|
|
|
if (outlength == 0)
|
|
|
|
{
|
|
|
|
outlength = bufp->getSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outlength = std::min(outlength, bufp->getSize());
|
|
|
|
}
|
2013-02-24 02:46:21 +00:00
|
|
|
return std::string(reinterpret_cast<char*>(bufp->getBuffer()), outlength);
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
hash_V5(std::string const& password,
|
|
|
|
std::string const& salt,
|
|
|
|
std::string const& udata,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
|
|
|
Pl_SHA2 hash(256);
|
2013-02-24 02:46:21 +00:00
|
|
|
hash.write(QUtil::unsigned_char_pointer(password), password.length());
|
|
|
|
hash.write(QUtil::unsigned_char_pointer(salt), salt.length());
|
|
|
|
hash.write(QUtil::unsigned_char_pointer(udata), udata.length());
|
2012-12-30 02:31:21 +00:00
|
|
|
hash.finish();
|
|
|
|
std::string K = hash.getRawDigest();
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
if (data.getR() < 6)
|
|
|
|
{
|
|
|
|
result = K;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Algorithm 2.B from ISO 32000-1 chapter 7: Computing a hash
|
|
|
|
|
|
|
|
int round_number = 0;
|
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
|
|
|
// The hash algorithm has us setting K initially to the R5
|
|
|
|
// value and then repeating a series of steps 64 times
|
|
|
|
// before starting with the termination case testing. The
|
|
|
|
// wording of the specification is very unclear as to the
|
|
|
|
// exact number of times it should be run since the
|
|
|
|
// wording about whether the initial setup counts as round
|
|
|
|
// 0 or not is ambiguous. This code counts the initial
|
|
|
|
// setup (R5) value as round 0, which appears to be
|
|
|
|
// correct. This was determined to be correct by
|
|
|
|
// increasing or decreasing the number of rounds by 1 or 2
|
|
|
|
// from this value and generating 20 test files. In this
|
|
|
|
// interpretation, all the test files worked with Adobe
|
|
|
|
// Reader X. In the other configurations, many of the
|
|
|
|
// files did not work, and we were accurately able to
|
|
|
|
// predict which files didn't work by looking at the
|
|
|
|
// conditions under which we terminated repetition.
|
|
|
|
|
|
|
|
++round_number;
|
|
|
|
std::string K1 = password + K + udata;
|
|
|
|
assert(K.length() >= 32);
|
|
|
|
std::string E = process_with_aes(
|
|
|
|
K.substr(0, 16), true, K1, 0, 64,
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::unsigned_char_pointer(K.substr(16, 16)), 16);
|
2012-12-30 02:31:21 +00:00
|
|
|
|
|
|
|
// E_mod_3 is supposed to be mod 3 of the first 16 bytes
|
|
|
|
// of E taken as as a (128-bit) big-endian number. Since
|
|
|
|
// (xy mod n) is equal to ((x mod n) + (y mod n)) mod n
|
|
|
|
// and since 256 mod n is 1, we can just take the sums of
|
|
|
|
// the the mod 3s of each byte to get the same result.
|
|
|
|
int E_mod_3 = 0;
|
|
|
|
for (unsigned int i = 0; i < 16; ++i)
|
|
|
|
{
|
2013-10-05 23:42:39 +00:00
|
|
|
E_mod_3 += static_cast<unsigned char>(E.at(i));
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
E_mod_3 %= 3;
|
|
|
|
int next_hash = ((E_mod_3 == 0) ? 256 :
|
|
|
|
(E_mod_3 == 1) ? 384 :
|
|
|
|
512);
|
|
|
|
Pl_SHA2 hash(next_hash);
|
2013-02-24 02:46:21 +00:00
|
|
|
hash.write(QUtil::unsigned_char_pointer(E), E.length());
|
2012-12-30 02:31:21 +00:00
|
|
|
hash.finish();
|
|
|
|
K = hash.getRawDigest();
|
|
|
|
|
|
|
|
if (round_number >= 64)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
unsigned int ch = static_cast<unsigned char>(*(E.rbegin()));
|
2012-12-30 02:31:21 +00:00
|
|
|
|
2013-02-24 02:46:21 +00:00
|
|
|
if (ch <= static_cast<unsigned int>(round_number - 32))
|
2012-12-30 02:31:21 +00:00
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = K.substr(0, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-11 23:53:56 +00:00
|
|
|
static
|
|
|
|
void pad_short_parameter(std::string& param, unsigned int max_len)
|
|
|
|
{
|
|
|
|
if (param.length() < max_len)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption pad short parameter");
|
|
|
|
param.append(max_len - param.length(), '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string
|
|
|
|
QPDF::compute_data_key(std::string const& encryption_key,
|
2012-12-30 02:31:21 +00:00
|
|
|
int objid, int generation, bool use_aes,
|
|
|
|
int encryption_V, int encryption_R)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.1 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::string result = encryption_key;
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
if (encryption_V >= 5)
|
|
|
|
{
|
|
|
|
// Algorithm 3.1a (PDF 1.7 extension level 3): just use
|
|
|
|
// encryption key straight.
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Append low three bytes of object ID and low two bytes of generation
|
2013-02-24 02:46:21 +00:00
|
|
|
result += static_cast<char>(objid & 0xff);
|
|
|
|
result += static_cast<char>((objid >> 8) & 0xff);
|
|
|
|
result += static_cast<char>((objid >> 16) & 0xff);
|
|
|
|
result += static_cast<char>(generation & 0xff);
|
|
|
|
result += static_cast<char>((generation >> 8) & 0xff);
|
2009-10-17 03:14:47 +00:00
|
|
|
if (use_aes)
|
|
|
|
{
|
|
|
|
result += "sAlT";
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
MD5 md5;
|
2013-02-24 02:46:21 +00:00
|
|
|
md5.encodeDataIncrementally(result.c_str(), result.length());
|
2008-04-29 12:55:25 +00:00
|
|
|
MD5::Digest digest;
|
|
|
|
md5.digest(digest);
|
2013-02-24 02:46:21 +00:00
|
|
|
return std::string(reinterpret_cast<char*>(digest),
|
|
|
|
std::min(result.length(), static_cast<size_t>(16)));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::compute_encryption_key(
|
|
|
|
std::string const& password, EncryptionData const& data)
|
2012-12-30 02:31:21 +00:00
|
|
|
{
|
|
|
|
if (data.getV() >= 5)
|
|
|
|
{
|
|
|
|
// For V >= 5, the encryption key is generated and stored in
|
|
|
|
// the file, encrypted separately with both user and owner
|
|
|
|
// passwords.
|
|
|
|
return recover_encryption_key_with_password(password, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For V < 5, the encryption key is derived from the user
|
|
|
|
// password.
|
|
|
|
return compute_encryption_key_from_password(password, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::compute_encryption_key_from_password(
|
|
|
|
std::string const& password, EncryptionData const& data)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.2 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
// This code does not properly handle Unicode passwords.
|
|
|
|
// Passwords are supposed to be converted from OS codepage
|
|
|
|
// characters to PDFDocEncoding. Unicode passwords are supposed
|
|
|
|
// to be converted to OS codepage before converting to
|
|
|
|
// PDFDocEncoding. We instead require the password to be
|
|
|
|
// presented in its final form.
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
MD5 md5;
|
|
|
|
md5.encodeDataIncrementally(
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4(password).c_str(), key_bytes);
|
2012-12-26 21:04:54 +00:00
|
|
|
md5.encodeDataIncrementally(data.getO().c_str(), key_bytes);
|
2008-04-29 12:55:25 +00:00
|
|
|
char pbytes[4];
|
2012-12-26 21:04:54 +00:00
|
|
|
int P = data.getP();
|
2013-02-24 02:46:21 +00:00
|
|
|
pbytes[0] = static_cast<char>(P & 0xff);
|
|
|
|
pbytes[1] = static_cast<char>((P >> 8) & 0xff);
|
|
|
|
pbytes[2] = static_cast<char>((P >> 16) & 0xff);
|
|
|
|
pbytes[3] = static_cast<char>((P >> 24) & 0xff);
|
2008-04-29 12:55:25 +00:00
|
|
|
md5.encodeDataIncrementally(pbytes, 4);
|
2012-12-26 21:04:54 +00:00
|
|
|
md5.encodeDataIncrementally(data.getId1().c_str(),
|
2013-02-24 02:46:21 +00:00
|
|
|
data.getId1().length());
|
2012-12-26 21:04:54 +00:00
|
|
|
if ((data.getR() >= 4) && (! data.getEncryptMetadata()))
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
|
|
|
char bytes[4];
|
|
|
|
memset(bytes, 0xff, 4);
|
|
|
|
md5.encodeDataIncrementally(bytes, 4);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
MD5::Digest digest;
|
2012-12-26 21:04:54 +00:00
|
|
|
iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0));
|
2015-02-21 22:30:45 +00:00
|
|
|
return std::string(reinterpret_cast<char*>(digest),
|
|
|
|
std::min(static_cast<int>(sizeof(digest)),
|
|
|
|
data.getLengthBytes()));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
compute_O_rc4_key(std::string const& user_password,
|
|
|
|
std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data,
|
2012-12-30 02:31:21 +00:00
|
|
|
unsigned char key[OU_key_bytes_V4])
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-12-30 02:31:21 +00:00
|
|
|
if (data.getV() >= 5)
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"compute_O_rc4_key called for file with V >= 5");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string password = owner_password;
|
|
|
|
if (password.empty())
|
|
|
|
{
|
|
|
|
password = user_password;
|
|
|
|
}
|
|
|
|
MD5 md5;
|
|
|
|
md5.encodeDataIncrementally(
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4(password).c_str(), key_bytes);
|
2008-04-29 12:55:25 +00:00
|
|
|
MD5::Digest digest;
|
2012-12-26 21:04:54 +00:00
|
|
|
iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0));
|
2012-12-30 02:31:21 +00:00
|
|
|
memcpy(key, digest, OU_key_bytes_V4);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
compute_O_value(std::string const& user_password,
|
|
|
|
std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.3 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
unsigned char O_key[OU_key_bytes_V4];
|
2008-04-29 12:55:25 +00:00
|
|
|
compute_O_rc4_key(user_password, owner_password, data, O_key);
|
|
|
|
|
|
|
|
char upass[key_bytes];
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4(user_password, upass);
|
2017-08-27 01:10:07 +00:00
|
|
|
std::string k1(reinterpret_cast<char*>(O_key), OU_key_bytes_V4);
|
|
|
|
pad_short_parameter(k1, data.getLengthBytes());
|
2013-02-24 02:46:21 +00:00
|
|
|
iterate_rc4(QUtil::unsigned_char_pointer(upass), key_bytes,
|
2012-12-26 21:04:54 +00:00
|
|
|
O_key, data.getLengthBytes(),
|
|
|
|
(data.getR() >= 3) ? 20 : 1, false);
|
2008-04-29 12:55:25 +00:00
|
|
|
return std::string(upass, key_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
std::string
|
|
|
|
compute_U_value_R2(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.4 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::string k1 = QPDF::compute_encryption_key(user_password, data);
|
|
|
|
char udata[key_bytes];
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4("", udata);
|
2017-08-27 01:10:07 +00:00
|
|
|
pad_short_parameter(k1, data.getLengthBytes());
|
2013-02-24 02:46:21 +00:00
|
|
|
iterate_rc4(QUtil::unsigned_char_pointer(udata), key_bytes,
|
|
|
|
QUtil::unsigned_char_pointer(k1),
|
|
|
|
data.getLengthBytes(), 1, false);
|
2008-04-29 12:55:25 +00:00
|
|
|
return std::string(udata, key_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
std::string
|
|
|
|
compute_U_value_R3(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.5 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::string k1 = QPDF::compute_encryption_key(user_password, data);
|
|
|
|
MD5 md5;
|
|
|
|
md5.encodeDataIncrementally(
|
2012-12-30 02:31:21 +00:00
|
|
|
pad_or_truncate_password_V4("").c_str(), key_bytes);
|
2012-12-26 21:04:54 +00:00
|
|
|
md5.encodeDataIncrementally(data.getId1().c_str(),
|
2013-02-24 02:46:21 +00:00
|
|
|
data.getId1().length());
|
2008-04-29 12:55:25 +00:00
|
|
|
MD5::Digest digest;
|
|
|
|
md5.digest(digest);
|
2017-08-27 01:10:07 +00:00
|
|
|
pad_short_parameter(k1, data.getLengthBytes());
|
2008-04-29 12:55:25 +00:00
|
|
|
iterate_rc4(digest, sizeof(MD5::Digest),
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::unsigned_char_pointer(k1),
|
|
|
|
data.getLengthBytes(), 20, false);
|
2008-04-29 12:55:25 +00:00
|
|
|
char result[key_bytes];
|
|
|
|
memcpy(result, digest, sizeof(MD5::Digest));
|
|
|
|
// pad with arbitrary data -- make it consistent for the sake of
|
|
|
|
// testing
|
|
|
|
for (unsigned int i = sizeof(MD5::Digest); i < key_bytes; ++i)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
result[i] = static_cast<char>((i * i) % 0xff);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return std::string(result, key_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
compute_U_value(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
2012-12-26 21:04:54 +00:00
|
|
|
if (data.getR() >= 3)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return compute_U_value_R3(user_password, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return compute_U_value_R2(user_password, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-12-30 02:31:21 +00:00
|
|
|
check_user_password_V4(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.6 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
std::string u_value = compute_U_value(user_password, data);
|
2012-12-30 02:31:21 +00:00
|
|
|
int to_compare = ((data.getR() >= 3) ? sizeof(MD5::Digest)
|
|
|
|
: key_bytes);
|
2012-12-26 21:04:54 +00:00
|
|
|
return (memcmp(data.getU().c_str(), u_value.c_str(), to_compare) == 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-12-30 02:31:21 +00:00
|
|
|
check_user_password_V5(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
|
|
|
// Algorithm 3.11 from the PDF 1.7 extension level 3
|
|
|
|
|
|
|
|
std::string user_data = data.getU().substr(0, 32);
|
|
|
|
std::string validation_salt = data.getU().substr(32, 8);
|
|
|
|
std::string password = truncate_password_V5(user_password);
|
|
|
|
return (hash_V5(password, validation_salt, "", data) == user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
check_user_password(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
|
|
|
if (data.getV() < 5)
|
|
|
|
{
|
|
|
|
return check_user_password_V4(user_password, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return check_user_password_V5(user_password, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
check_owner_password_V4(std::string& user_password,
|
|
|
|
std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
// Algorithm 3.7 from the PDF 1.7 Reference Manual
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
unsigned char key[OU_key_bytes_V4];
|
2008-04-29 12:55:25 +00:00
|
|
|
compute_O_rc4_key(user_password, owner_password, data, key);
|
|
|
|
unsigned char O_data[key_bytes];
|
2013-02-24 02:46:21 +00:00
|
|
|
memcpy(O_data, QUtil::unsigned_char_pointer(data.getO()), key_bytes);
|
2017-08-27 01:10:07 +00:00
|
|
|
std::string k1(reinterpret_cast<char*>(key), OU_key_bytes_V4);
|
|
|
|
pad_short_parameter(k1, data.getLengthBytes());
|
|
|
|
iterate_rc4(O_data, key_bytes, QUtil::unsigned_char_pointer(k1),
|
|
|
|
data.getLengthBytes(),
|
2012-12-30 02:31:21 +00:00
|
|
|
(data.getR() >= 3) ? 20 : 1, true);
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string new_user_password =
|
2013-02-24 02:46:21 +00:00
|
|
|
std::string(reinterpret_cast<char*>(O_data), key_bytes);
|
2008-04-29 12:55:25 +00:00
|
|
|
bool result = false;
|
|
|
|
if (check_user_password(new_user_password, data))
|
|
|
|
{
|
2012-12-30 02:31:21 +00:00
|
|
|
result = true;
|
|
|
|
user_password = new_user_password;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
static bool
|
|
|
|
check_owner_password_V5(std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
|
|
|
// Algorithm 3.12 from the PDF 1.7 extension level 3
|
|
|
|
|
|
|
|
std::string user_data = data.getU().substr(0, 48);
|
|
|
|
std::string owner_data = data.getO().substr(0, 32);
|
|
|
|
std::string validation_salt = data.getO().substr(32, 8);
|
|
|
|
std::string password = truncate_password_V5(owner_password);
|
|
|
|
return (hash_V5(password, validation_salt, user_data,
|
|
|
|
data) == owner_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
check_owner_password(std::string& user_password,
|
|
|
|
std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
|
|
|
if (data.getV() < 5)
|
|
|
|
{
|
|
|
|
return check_owner_password_V4(user_password, owner_password, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return check_owner_password_V5(owner_password, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::recover_encryption_key_with_password(
|
|
|
|
std::string const& password, EncryptionData const& data)
|
|
|
|
{
|
|
|
|
// Disregard whether Perms is valid.
|
|
|
|
bool disregard;
|
|
|
|
return recover_encryption_key_with_password(password, data, disregard);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
compute_U_UE_value_V5(std::string const& user_password,
|
|
|
|
std::string const& encryption_key,
|
|
|
|
QPDF::EncryptionData const& data,
|
|
|
|
std::string& U, std::string& UE)
|
|
|
|
{
|
|
|
|
// Algorithm 3.8 from the PDF 1.7 extension level 3
|
|
|
|
char k[16];
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::initializeWithRandomBytes(
|
|
|
|
QUtil::unsigned_char_pointer(k), sizeof(k));
|
2012-12-30 02:31:21 +00:00
|
|
|
std::string validation_salt(k, 8);
|
|
|
|
std::string key_salt(k + 8, 8);
|
|
|
|
U = hash_V5(user_password, validation_salt, "", data) +
|
|
|
|
validation_salt + key_salt;
|
|
|
|
std::string intermediate_key = hash_V5(user_password, key_salt, "", data);
|
|
|
|
UE = process_with_aes(intermediate_key, true, encryption_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
compute_O_OE_value_V5(std::string const& owner_password,
|
|
|
|
std::string const& encryption_key,
|
|
|
|
QPDF::EncryptionData const& data,
|
|
|
|
std::string const& U,
|
|
|
|
std::string& O, std::string& OE)
|
|
|
|
{
|
|
|
|
// Algorithm 3.9 from the PDF 1.7 extension level 3
|
|
|
|
char k[16];
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::initializeWithRandomBytes(
|
|
|
|
QUtil::unsigned_char_pointer(k), sizeof(k));
|
2012-12-30 02:31:21 +00:00
|
|
|
std::string validation_salt(k, 8);
|
|
|
|
std::string key_salt(k + 8, 8);
|
|
|
|
O = hash_V5(owner_password, validation_salt, U, data) +
|
|
|
|
validation_salt + key_salt;
|
|
|
|
std::string intermediate_key = hash_V5(owner_password, key_salt, U, data);
|
|
|
|
OE = process_with_aes(intermediate_key, true, encryption_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
compute_Perms_value_V5_clear(std::string const& encryption_key,
|
|
|
|
QPDF::EncryptionData const& data,
|
|
|
|
unsigned char k[16])
|
|
|
|
{
|
|
|
|
// From algorithm 3.10 from the PDF 1.7 extension level 3
|
|
|
|
unsigned long long extended_perms = 0xffffffff00000000LL | data.getP();
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
k[i] = static_cast<unsigned char>(extended_perms & 0xff);
|
2012-12-30 02:31:21 +00:00
|
|
|
extended_perms >>= 8;
|
|
|
|
}
|
|
|
|
k[8] = data.getEncryptMetadata() ? 'T' : 'F';
|
|
|
|
k[9] = 'a';
|
|
|
|
k[10] = 'd';
|
|
|
|
k[11] = 'b';
|
|
|
|
QUtil::initializeWithRandomBytes(k + 12, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
compute_Perms_value_V5(std::string const& encryption_key,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
|
|
|
// Algorithm 3.10 from the PDF 1.7 extension level 3
|
|
|
|
unsigned char k[16];
|
|
|
|
compute_Perms_value_V5_clear(encryption_key, data, k);
|
2013-02-24 02:46:21 +00:00
|
|
|
return process_with_aes(
|
|
|
|
encryption_key, true,
|
|
|
|
std::string(reinterpret_cast<char*>(k), sizeof(k)));
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::recover_encryption_key_with_password(
|
|
|
|
std::string const& password, EncryptionData const& data,
|
|
|
|
bool& perms_valid)
|
|
|
|
{
|
|
|
|
// Algorithm 3.2a from the PDF 1.7 extension level 3
|
|
|
|
|
|
|
|
// This code does not handle Unicode passwords correctly.
|
|
|
|
// Empirical evidence suggests that most viewers don't. We are
|
|
|
|
// supposed to process the input string with the SASLprep (RFC
|
|
|
|
// 4013) profile of stringprep (RFC 3454) and then convert the
|
|
|
|
// result to UTF-8.
|
|
|
|
|
|
|
|
perms_valid = false;
|
|
|
|
std::string key_password = truncate_password_V5(password);
|
|
|
|
std::string key_salt;
|
|
|
|
std::string user_data;
|
|
|
|
std::string encrypted_file_key;
|
|
|
|
if (check_owner_password_V5(key_password, data))
|
|
|
|
{
|
|
|
|
key_salt = data.getO().substr(40, 8);
|
|
|
|
user_data = data.getU().substr(0, 48);
|
|
|
|
encrypted_file_key = data.getOE().substr(0, 32);
|
|
|
|
}
|
|
|
|
else if (check_user_password_V5(key_password, data))
|
|
|
|
{
|
|
|
|
key_salt = data.getU().substr(40, 8);
|
|
|
|
encrypted_file_key = data.getUE().substr(0, 32);
|
|
|
|
}
|
|
|
|
std::string intermediate_key =
|
|
|
|
hash_V5(key_password, key_salt, user_data, data);
|
|
|
|
std::string file_key =
|
|
|
|
process_with_aes(intermediate_key, false, encrypted_file_key);
|
|
|
|
|
|
|
|
// Decrypt Perms and check against expected value
|
|
|
|
std::string perms_check =
|
|
|
|
process_with_aes(file_key, false, data.getPerms(), 12);
|
|
|
|
unsigned char k[16];
|
|
|
|
compute_Perms_value_V5_clear(file_key, data, k);
|
|
|
|
perms_valid = (memcmp(perms_check.c_str(), k, 12) == 0);
|
|
|
|
|
|
|
|
return file_key;
|
|
|
|
}
|
|
|
|
|
2009-10-17 23:37:55 +00:00
|
|
|
QPDF::encryption_method_e
|
|
|
|
QPDF::interpretCF(QPDFObjectHandle cf)
|
|
|
|
{
|
|
|
|
if (cf.isName())
|
|
|
|
{
|
|
|
|
std::string filter = cf.getName();
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->crypt_filters.count(filter) != 0)
|
2009-10-17 23:37:55 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->crypt_filters[filter];
|
2009-10-17 23:37:55 +00:00
|
|
|
}
|
2009-10-18 19:54:24 +00:00
|
|
|
else if (filter == "/Identity")
|
|
|
|
{
|
|
|
|
return e_none;
|
|
|
|
}
|
2009-10-17 23:37:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return e_unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-18 19:54:24 +00:00
|
|
|
// Default: /Identity
|
2009-10-17 23:37:55 +00:00
|
|
|
return e_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
QPDF::initializeEncryption()
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->encryption_initialized)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encryption_initialized = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// After we initialize encryption parameters, we must used stored
|
|
|
|
// key information and never look at /Encrypt again. Otherwise,
|
|
|
|
// things could go wrong if someone mutates the encryption
|
|
|
|
// dictionary.
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->trailer.hasKey("/Encrypt"))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
// Go ahead and set this->m->encrypted here. That way, isEncrypted
|
2009-10-05 00:42:48 +00:00
|
|
|
// will return true even if there were errors reading the
|
|
|
|
// encryption dictionary.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encrypted = true;
|
2009-10-05 00:42:48 +00:00
|
|
|
|
2013-06-15 16:38:25 +00:00
|
|
|
std::string id1;
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFObjectHandle id_obj = this->m->trailer.getKey("/ID");
|
2013-06-15 16:38:25 +00:00
|
|
|
if ((id_obj.isArray() &&
|
|
|
|
(id_obj.getArrayNItems() == 2) &&
|
|
|
|
id_obj.getArrayItem(0).isString()))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-06-15 16:38:25 +00:00
|
|
|
id1 = id_obj.getArrayItem(0).getStringValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Treating a missing ID as the empty string enables qpdf to
|
|
|
|
// decrypt some invalid encrypted files with no /ID that
|
|
|
|
// poppler can read but Adobe Reader can't.
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"trailer", this->m->file->getLastOffset(),
|
2013-06-15 16:38:25 +00:00
|
|
|
"invalid /ID in trailer dictionary"));
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFObjectHandle encryption_dict = this->m->trailer.getKey("/Encrypt");
|
2008-04-29 12:55:25 +00:00
|
|
|
if (! encryption_dict.isDictionary())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"/Encrypt in trailer dictionary is not a dictionary");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! (encryption_dict.getKey("/Filter").isName() &&
|
|
|
|
(encryption_dict.getKey("/Filter").getName() == "/Standard")))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"encryption dictionary", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"unsupported encryption filter");
|
|
|
|
}
|
2009-10-17 23:37:55 +00:00
|
|
|
if (! encryption_dict.getKey("/SubFilter").isNull())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_unsupported, this->m->file->getName(),
|
|
|
|
"encryption dictionary", this->m->file->getLastOffset(),
|
2009-10-17 23:37:55 +00:00
|
|
|
"file uses encryption SubFilters,"
|
|
|
|
" which qpdf does not support"));
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
if (! (encryption_dict.getKey("/V").isInteger() &&
|
|
|
|
encryption_dict.getKey("/R").isInteger() &&
|
|
|
|
encryption_dict.getKey("/O").isString() &&
|
|
|
|
encryption_dict.getKey("/U").isString() &&
|
|
|
|
encryption_dict.getKey("/P").isInteger()))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"encryption dictionary", this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"some encryption dictionary parameters are missing "
|
|
|
|
"or the wrong type");
|
|
|
|
}
|
|
|
|
|
|
|
|
int V = encryption_dict.getKey("/V").getIntValue();
|
|
|
|
int R = encryption_dict.getKey("/R").getIntValue();
|
|
|
|
std::string O = encryption_dict.getKey("/O").getStringValue();
|
|
|
|
std::string U = encryption_dict.getKey("/U").getStringValue();
|
2013-02-24 02:46:21 +00:00
|
|
|
unsigned int P = encryption_dict.getKey("/P").getIntValue();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
// If supporting new encryption R/V values, remember to update
|
|
|
|
// error message inside this if statement.
|
|
|
|
if (! (((R >= 2) && (R <= 6)) &&
|
|
|
|
((V == 1) || (V == 2) || (V == 4) || (V == 5))))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_unsupported, this->m->file->getName(),
|
|
|
|
"encryption dictionary", this->m->file->getLastOffset(),
|
2012-12-30 02:31:21 +00:00
|
|
|
"Unsupported /R or /V in encryption dictionary; R = " +
|
|
|
|
QUtil::int_to_string(R) + " (max 6), V = " +
|
|
|
|
QUtil::int_to_string(V) + " (max 5)");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encryption_V = V;
|
|
|
|
this->m->encryption_R = R;
|
2012-12-30 02:31:21 +00:00
|
|
|
|
|
|
|
// OE, UE, and Perms are only present if V >= 5.
|
|
|
|
std::string OE;
|
|
|
|
std::string UE;
|
|
|
|
std::string Perms;
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
if (V < 5)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-27 01:10:07 +00:00
|
|
|
// These must be exactly the right number of bytes.
|
2017-08-11 23:53:56 +00:00
|
|
|
pad_short_parameter(O, key_bytes);
|
|
|
|
pad_short_parameter(U, key_bytes);
|
2012-12-30 02:31:21 +00:00
|
|
|
if (! ((O.length() == key_bytes) && (U.length() == key_bytes)))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"encryption dictionary",
|
|
|
|
this->m->file->getLastOffset(),
|
2012-12-30 02:31:21 +00:00
|
|
|
"incorrect length for /O and/or /U in "
|
|
|
|
"encryption dictionary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (! (encryption_dict.getKey("/OE").isString() &&
|
|
|
|
encryption_dict.getKey("/UE").isString() &&
|
|
|
|
encryption_dict.getKey("/Perms").isString()))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"encryption dictionary",
|
|
|
|
this->m->file->getLastOffset(),
|
2012-12-30 02:31:21 +00:00
|
|
|
"some V=5 encryption dictionary parameters are "
|
|
|
|
"missing or the wrong type");
|
|
|
|
}
|
|
|
|
OE = encryption_dict.getKey("/OE").getStringValue();
|
|
|
|
UE = encryption_dict.getKey("/UE").getStringValue();
|
|
|
|
Perms = encryption_dict.getKey("/Perms").getStringValue();
|
|
|
|
|
2017-08-27 01:10:07 +00:00
|
|
|
// These may be longer than the minimum number of bytes.
|
2017-08-11 23:53:56 +00:00
|
|
|
pad_short_parameter(O, OU_key_bytes_V5);
|
|
|
|
pad_short_parameter(U, OU_key_bytes_V5);
|
|
|
|
pad_short_parameter(OE, OUE_key_bytes_V5);
|
|
|
|
pad_short_parameter(UE, OUE_key_bytes_V5);
|
|
|
|
pad_short_parameter(Perms, Perms_key_bytes_V5);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Length = 40;
|
|
|
|
if (encryption_dict.getKey("/Length").isInteger())
|
|
|
|
{
|
|
|
|
Length = encryption_dict.getKey("/Length").getIntValue();
|
2012-12-30 02:31:21 +00:00
|
|
|
if ((Length % 8) || (Length < 40) || (Length > 256))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"encryption dictionary",
|
|
|
|
this->m->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"invalid /Length value in encryption dictionary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encrypt_metadata = true;
|
2009-10-17 03:14:47 +00:00
|
|
|
if ((V >= 4) && (encryption_dict.getKey("/EncryptMetadata").isBool()))
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encrypt_metadata =
|
2009-10-17 03:14:47 +00:00
|
|
|
encryption_dict.getKey("/EncryptMetadata").getBoolValue();
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
if ((V == 4) || (V == 5))
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
2009-10-17 23:37:55 +00:00
|
|
|
QPDFObjectHandle CF = encryption_dict.getKey("/CF");
|
|
|
|
std::set<std::string> keys = CF.getKeys();
|
|
|
|
for (std::set<std::string>::iterator iter = keys.begin();
|
|
|
|
iter != keys.end(); ++iter)
|
|
|
|
{
|
|
|
|
std::string const& filter = *iter;
|
|
|
|
QPDFObjectHandle cdict = CF.getKey(filter);
|
|
|
|
if (cdict.isDictionary())
|
|
|
|
{
|
|
|
|
encryption_method_e method = e_none;
|
|
|
|
if (cdict.getKey("/CFM").isName())
|
|
|
|
{
|
|
|
|
std::string method_name = cdict.getKey("/CFM").getName();
|
|
|
|
if (method_name == "/V2")
|
|
|
|
{
|
2009-10-18 19:54:24 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encryption CFM V2");
|
2009-10-17 23:37:55 +00:00
|
|
|
method = e_rc4;
|
|
|
|
}
|
|
|
|
else if (method_name == "/AESV2")
|
|
|
|
{
|
2009-10-18 19:54:24 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encryption CFM AESV2");
|
2009-10-17 23:37:55 +00:00
|
|
|
method = e_aes;
|
|
|
|
}
|
2012-12-30 02:31:21 +00:00
|
|
|
else if (method_name == "/AESV3")
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption CFM AESV3");
|
|
|
|
method = e_aesv3;
|
|
|
|
}
|
2009-10-17 23:37:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Don't complain now -- maybe we won't need
|
|
|
|
// to reference this type.
|
|
|
|
method = e_unknown;
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->crypt_filters[filter] = method;
|
2009-10-17 23:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle StmF = encryption_dict.getKey("/StmF");
|
|
|
|
QPDFObjectHandle StrF = encryption_dict.getKey("/StrF");
|
|
|
|
QPDFObjectHandle EFF = encryption_dict.getKey("/EFF");
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->cf_stream = interpretCF(StmF);
|
|
|
|
this->m->cf_string = interpretCF(StrF);
|
2009-10-17 23:37:55 +00:00
|
|
|
if (EFF.isName())
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->cf_file = interpretCF(EFF);
|
2009-10-17 23:37:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->cf_file = this->m->cf_stream;
|
2009-10-17 23:37:55 +00:00
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
2012-12-30 02:31:21 +00:00
|
|
|
|
|
|
|
EncryptionData data(V, R, Length / 8, P, O, U, OE, UE, Perms,
|
2017-08-22 01:33:44 +00:00
|
|
|
id1, this->m->encrypt_metadata);
|
2018-01-14 15:17:17 +00:00
|
|
|
if (this->m->provided_password_is_hex_key)
|
|
|
|
{
|
|
|
|
// ignore passwords in file
|
|
|
|
}
|
|
|
|
else if (check_owner_password(
|
|
|
|
this->m->user_password, this->m->provided_password, data))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// password supplied was owner password; user_password has
|
2012-12-30 02:31:21 +00:00
|
|
|
// been initialized for V < 5
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2017-08-22 01:33:44 +00:00
|
|
|
else if (check_user_password(this->m->provided_password, data))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->user_password = this->m->provided_password;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_password, this->m->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"", 0, "invalid password");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 15:17:17 +00:00
|
|
|
if (this->m->provided_password_is_hex_key)
|
|
|
|
{
|
|
|
|
this->m->encryption_key = QUtil::hex_decode(this->m->provided_password);
|
|
|
|
}
|
|
|
|
else if (V < 5)
|
2012-12-30 02:31:21 +00:00
|
|
|
{
|
|
|
|
// For V < 5, the user password is encrypted with the owner
|
|
|
|
// password, and the user password is always used for
|
|
|
|
// computing the encryption key.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encryption_key = compute_encryption_key(
|
|
|
|
this->m->user_password, data);
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// For V >= 5, either password can be used independently to
|
|
|
|
// compute the encryption key, and neither password can be
|
|
|
|
// used to recover the other.
|
|
|
|
bool perms_valid;
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->encryption_key = recover_encryption_key_with_password(
|
|
|
|
this->m->provided_password, data, perms_valid);
|
2012-12-30 02:31:21 +00:00
|
|
|
if (! perms_valid)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
"encryption dictionary",
|
|
|
|
this->m->file->getLastOffset(),
|
2012-12-30 02:31:21 +00:00
|
|
|
"/Perms field in encryption dictionary"
|
|
|
|
" doesn't match expected value"));
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2009-10-17 18:54:51 +00:00
|
|
|
QPDF::getKeyForObject(int objid, int generation, bool use_aes)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! this->m->encrypted)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"request for encryption key in non-encrypted PDF");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
if (! ((objid == this->m->cached_key_objid) &&
|
|
|
|
(generation == this->m->cached_key_generation)))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->cached_object_encryption_key =
|
|
|
|
compute_data_key(this->m->encryption_key, objid, generation,
|
|
|
|
use_aes, this->m->encryption_V,
|
|
|
|
this->m->encryption_R);
|
|
|
|
this->m->cached_key_objid = objid;
|
|
|
|
this->m->cached_key_generation = generation;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->cached_object_encryption_key;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::decryptString(std::string& str, int objid, int generation)
|
|
|
|
{
|
|
|
|
if (objid == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-10-17 23:37:55 +00:00
|
|
|
bool use_aes = false;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->encryption_V >= 4)
|
2009-10-17 23:37:55 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
switch (this->m->cf_string)
|
2009-10-17 23:37:55 +00:00
|
|
|
{
|
|
|
|
case e_none:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case e_aes:
|
|
|
|
use_aes = true;
|
|
|
|
break;
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
case e_aesv3:
|
|
|
|
use_aes = true;
|
|
|
|
break;
|
|
|
|
|
2009-10-17 23:37:55 +00:00
|
|
|
case e_rc4:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2009-10-17 23:37:55 +00:00
|
|
|
"unknown encryption filter for strings"
|
|
|
|
" (check /StrF in /Encrypt dictionary);"
|
|
|
|
" strings may be decrypted improperly"));
|
|
|
|
// To avoid repeated warnings, reset cf_string. Assume
|
|
|
|
// we'd want to use AES if V == 4.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->cf_string = e_aes;
|
2009-10-17 23:37:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
std::string key = getKeyForObject(objid, generation, use_aes);
|
2009-10-19 23:09:19 +00:00
|
|
|
try
|
2009-10-17 18:54:51 +00:00
|
|
|
{
|
2009-10-19 23:09:19 +00:00
|
|
|
if (use_aes)
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption aes decode string");
|
|
|
|
Pl_Buffer bufpl("decrypted string");
|
|
|
|
Pl_AES_PDF pl("aes decrypt string", &bufpl, false,
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::unsigned_char_pointer(key),
|
|
|
|
key.length());
|
|
|
|
pl.write(QUtil::unsigned_char_pointer(str), str.length());
|
2009-10-19 23:09:19 +00:00
|
|
|
pl.finish();
|
|
|
|
PointerHolder<Buffer> buf = bufpl.getBuffer();
|
2013-02-24 02:46:21 +00:00
|
|
|
str = std::string(reinterpret_cast<char*>(buf->getBuffer()),
|
|
|
|
buf->getSize());
|
2009-10-19 23:09:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption rc4 decode string");
|
2013-02-24 02:46:21 +00:00
|
|
|
unsigned int vlen = str.length();
|
2011-08-11 15:56:37 +00:00
|
|
|
// Using PointerHolder guarantees that tmp will
|
2009-10-19 23:09:19 +00:00
|
|
|
// be freed even if rc4.process throws an exception.
|
2011-08-11 15:56:37 +00:00
|
|
|
PointerHolder<char> tmp(true, QUtil::copy_string(str));
|
2013-02-24 02:46:21 +00:00
|
|
|
RC4 rc4(QUtil::unsigned_char_pointer(key), key.length());
|
|
|
|
rc4.process(QUtil::unsigned_char_pointer(tmp.getPointer()), vlen);
|
2009-10-19 23:09:19 +00:00
|
|
|
str = std::string(tmp.getPointer(), vlen);
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
}
|
2013-02-25 15:28:34 +00:00
|
|
|
catch (QPDFExc&)
|
2009-10-19 23:09:19 +00:00
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (std::runtime_error& e)
|
2009-10-17 18:54:51 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"error decrypting string for object " +
|
|
|
|
QUtil::int_to_string(objid) + " " +
|
|
|
|
QUtil::int_to_string(generation) + ": " + e.what());
|
2009-10-17 18:54:51 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::decryptStream(Pipeline*& pipeline, int objid, int generation,
|
2009-10-17 18:54:51 +00:00
|
|
|
QPDFObjectHandle& stream_dict,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<PointerHolder<Pipeline> >& heap)
|
|
|
|
{
|
2009-10-17 18:54:51 +00:00
|
|
|
std::string type;
|
|
|
|
if (stream_dict.getKey("/Type").isName())
|
|
|
|
{
|
|
|
|
type = stream_dict.getKey("/Type").getName();
|
|
|
|
}
|
|
|
|
if (type == "/XRef")
|
|
|
|
{
|
2009-10-17 23:37:55 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encryption xref stream from encrypted file");
|
|
|
|
return;
|
2009-10-17 18:54:51 +00:00
|
|
|
}
|
|
|
|
bool use_aes = false;
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->encryption_V >= 4)
|
2009-10-17 18:54:51 +00:00
|
|
|
{
|
2009-10-17 23:37:55 +00:00
|
|
|
encryption_method_e method = e_unknown;
|
|
|
|
std::string method_source = "/StmF from /Encrypt dictionary";
|
|
|
|
|
2012-12-30 00:00:05 +00:00
|
|
|
if (stream_dict.getKey("/Filter").isOrHasName("/Crypt"))
|
|
|
|
{
|
|
|
|
if (stream_dict.getKey("/DecodeParms").isDictionary())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle decode_parms =
|
|
|
|
stream_dict.getKey("/DecodeParms");
|
|
|
|
if (decode_parms.getKey("/Type").isName() &&
|
|
|
|
(decode_parms.getKey("/Type").getName() ==
|
|
|
|
"/CryptFilterDecodeParms"))
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
|
|
|
|
method = interpretCF(decode_parms.getKey("/Name"));
|
|
|
|
method_source = "stream's Crypt decode parameters";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (stream_dict.getKey("/DecodeParms").isArray() &&
|
|
|
|
stream_dict.getKey("/Filter").isArray())
|
|
|
|
{
|
|
|
|
QPDFObjectHandle filter = stream_dict.getKey("/Filter");
|
|
|
|
QPDFObjectHandle decode = stream_dict.getKey("/DecodeParms");
|
|
|
|
if (filter.getArrayNItems() == decode.getArrayNItems())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
|
|
|
{
|
|
|
|
if (filter.getArrayItem(i).isName() &&
|
|
|
|
(filter.getArrayItem(i).getName() == "/Crypt"))
|
|
|
|
{
|
|
|
|
QPDFObjectHandle crypt_params =
|
|
|
|
decode.getArrayItem(i);
|
|
|
|
if (crypt_params.isDictionary() &&
|
|
|
|
crypt_params.getKey("/Name").isName())
|
|
|
|
{
|
2012-12-30 02:31:21 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encrypt crypt array");
|
2012-12-30 00:00:05 +00:00
|
|
|
method = interpretCF(
|
|
|
|
crypt_params.getKey("/Name"));
|
|
|
|
method_source = "stream's Crypt "
|
|
|
|
"decode parameters (array)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-19 01:58:31 +00:00
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
|
2009-10-17 23:37:55 +00:00
|
|
|
if (method == e_unknown)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if ((! this->m->encrypt_metadata) && (type == "/Metadata"))
|
2009-10-17 23:37:55 +00:00
|
|
|
{
|
2009-10-18 19:54:24 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encryption cleartext metadata");
|
2009-10-17 23:37:55 +00:00
|
|
|
method = e_none;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->attachment_streams.count(
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjGen(objid, generation)) > 0)
|
2012-12-30 00:00:05 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
method = this->m->cf_file;
|
2012-12-30 00:00:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
method = this->m->cf_stream;
|
2012-12-30 00:00:05 +00:00
|
|
|
}
|
2009-10-17 23:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
use_aes = false;
|
2009-10-18 02:26:09 +00:00
|
|
|
switch (method)
|
2009-10-17 23:37:55 +00:00
|
|
|
{
|
|
|
|
case e_none:
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case e_aes:
|
|
|
|
use_aes = true;
|
|
|
|
break;
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
case e_aesv3:
|
|
|
|
use_aes = true;
|
|
|
|
break;
|
|
|
|
|
2009-10-17 23:37:55 +00:00
|
|
|
case e_rc4:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// filter local to this stream.
|
2017-08-22 01:33:44 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
|
|
|
this->m->last_object_description,
|
|
|
|
this->m->file->getLastOffset(),
|
2009-10-17 23:37:55 +00:00
|
|
|
"unknown encryption filter for streams"
|
|
|
|
" (check " + method_source + ");"
|
|
|
|
" streams may be decrypted improperly"));
|
|
|
|
// To avoid repeated warnings, reset cf_stream. Assume
|
|
|
|
// we'd want to use AES if V == 4.
|
2017-08-22 01:33:44 +00:00
|
|
|
this->m->cf_stream = e_aes;
|
2009-10-17 23:37:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
std::string key = getKeyForObject(objid, generation, use_aes);
|
|
|
|
if (use_aes)
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
2009-10-18 19:54:24 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encryption aes decode stream");
|
2009-10-17 18:54:51 +00:00
|
|
|
pipeline = new Pl_AES_PDF("AES stream decryption", pipeline,
|
2013-02-24 02:46:21 +00:00
|
|
|
false, QUtil::unsigned_char_pointer(key),
|
|
|
|
key.length());
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-17 23:37:55 +00:00
|
|
|
QTC::TC("qpdf", "QPDF_encryption rc4 decode stream");
|
2009-10-17 03:14:47 +00:00
|
|
|
pipeline = new Pl_RC4("RC4 stream decryption", pipeline,
|
2013-02-24 02:46:21 +00:00
|
|
|
QUtil::unsigned_char_pointer(key),
|
|
|
|
key.length());
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
heap.push_back(pipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDF::compute_encryption_O_U(
|
|
|
|
char const* user_password, char const* owner_password,
|
2009-10-17 18:54:51 +00:00
|
|
|
int V, int R, int key_len, int P, bool encrypt_metadata,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string const& id1, std::string& O, std::string& U)
|
|
|
|
{
|
2012-12-30 02:31:21 +00:00
|
|
|
if (V >= 5)
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"compute_encryption_O_U called for file with V >= 5");
|
|
|
|
}
|
2012-12-26 22:10:25 +00:00
|
|
|
EncryptionData data(V, R, key_len, P, "", "", "", "", "",
|
|
|
|
id1, encrypt_metadata);
|
2012-12-26 21:04:54 +00:00
|
|
|
data.setO(compute_O_value(user_password, owner_password, data));
|
|
|
|
O = data.getO();
|
|
|
|
data.setU(compute_U_value(user_password, data));
|
|
|
|
U = data.getU();
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
void
|
|
|
|
QPDF::compute_encryption_parameters_V5(
|
|
|
|
char const* user_password, char const* owner_password,
|
|
|
|
int V, int R, int key_len, int P, bool encrypt_metadata,
|
|
|
|
std::string const& id1,
|
|
|
|
std::string& encryption_key,
|
|
|
|
std::string& O, std::string& U,
|
|
|
|
std::string& OE, std::string& UE, std::string& Perms)
|
|
|
|
{
|
|
|
|
EncryptionData data(V, R, key_len, P, "", "", "", "", "",
|
|
|
|
id1, encrypt_metadata);
|
|
|
|
unsigned char k[key_bytes];
|
|
|
|
QUtil::initializeWithRandomBytes(k, key_bytes);
|
2013-02-24 02:46:21 +00:00
|
|
|
encryption_key = std::string(reinterpret_cast<char*>(k), key_bytes);
|
2012-12-30 02:31:21 +00:00
|
|
|
compute_U_UE_value_V5(user_password, encryption_key, data, U, UE);
|
|
|
|
compute_O_OE_value_V5(owner_password, encryption_key, data, U, O, OE);
|
|
|
|
Perms = compute_Perms_value_V5(encryption_key, data);
|
|
|
|
data.setV5EncryptionParameters(O, OE, U, UE, Perms);
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string const&
|
2009-09-27 03:11:29 +00:00
|
|
|
QPDF::getPaddedUserPassword() const
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->user_password;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2009-09-27 03:11:29 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::getTrimmedUserPassword() const
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
std::string result = this->m->user_password;
|
2009-09-27 03:11:29 +00:00
|
|
|
trim_user_password(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
std::string
|
|
|
|
QPDF::getEncryptionKey() const
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->encryption_key;
|
2012-12-30 02:31:21 +00:00
|
|
|
}
|
|
|
|
|
2009-09-27 03:11:29 +00:00
|
|
|
bool
|
|
|
|
QPDF::isEncrypted() const
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
return this->m->encrypted;
|
2009-09-27 03:11:29 +00:00
|
|
|
}
|
2009-09-27 20:05:38 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::isEncrypted(int& R, int& P)
|
2012-07-16 01:15:24 +00:00
|
|
|
{
|
|
|
|
int V;
|
|
|
|
encryption_method_e stream, string, file;
|
|
|
|
return isEncrypted(R, P, V, stream, string, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::isEncrypted(int& R, int& P, int& V,
|
|
|
|
encryption_method_e& stream_method,
|
|
|
|
encryption_method_e& string_method,
|
|
|
|
encryption_method_e& file_method)
|
2009-09-27 20:05:38 +00:00
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
if (this->m->encrypted)
|
2009-09-27 20:05:38 +00:00
|
|
|
{
|
|
|
|
QPDFObjectHandle trailer = getTrailer();
|
|
|
|
QPDFObjectHandle encrypt = trailer.getKey("/Encrypt");
|
|
|
|
QPDFObjectHandle Pkey = encrypt.getKey("/P");
|
|
|
|
QPDFObjectHandle Rkey = encrypt.getKey("/R");
|
2012-07-16 01:15:24 +00:00
|
|
|
QPDFObjectHandle Vkey = encrypt.getKey("/V");
|
2009-09-27 20:05:38 +00:00
|
|
|
P = Pkey.getIntValue();
|
|
|
|
R = Rkey.getIntValue();
|
2012-07-16 01:15:24 +00:00
|
|
|
V = Vkey.getIntValue();
|
2017-08-22 01:33:44 +00:00
|
|
|
stream_method = this->m->cf_stream;
|
|
|
|
string_method = this->m->cf_stream;
|
|
|
|
file_method = this->m->cf_file;
|
2009-09-27 20:05:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
is_bit_set(int P, int bit)
|
|
|
|
{
|
|
|
|
// Bits in P are numbered from 1 in the spec
|
|
|
|
return (P & (1 << (bit - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowAccessibility()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
if (R < 3)
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowExtractAll()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 5);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowPrintLowRes()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 3);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowPrintHighRes()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 3);
|
|
|
|
if ((R >= 3) && (! is_bit_set(P, 12)))
|
|
|
|
{
|
|
|
|
status = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowModifyAssembly()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
if (R < 3)
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 11);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowModifyForm()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
if (R < 3)
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowModifyAnnotation()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 6);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowModifyOther()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
status = is_bit_set(P, 4);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::allowModifyAll()
|
|
|
|
{
|
|
|
|
int R = 0;
|
|
|
|
int P = 0;
|
|
|
|
bool status = true;
|
|
|
|
if (isEncrypted(R, P))
|
|
|
|
{
|
|
|
|
status = (is_bit_set(P, 4) && is_bit_set(P, 6));
|
|
|
|
if (R >= 3)
|
|
|
|
{
|
|
|
|
status = status && (is_bit_set(P, 9) && is_bit_set(P, 11));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|