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>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/RC4.hh>
|
|
|
|
#include <qpdf/MD5.hh>
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <assert.h>
|
2008-05-04 16:02:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
static char const padding_string[] = {
|
|
|
|
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 O_key_bytes = sizeof(MD5::Digest);
|
|
|
|
static unsigned int const key_bytes = 32;
|
|
|
|
|
|
|
|
void
|
|
|
|
pad_or_truncate_password(std::string const& password, char k1[key_bytes])
|
|
|
|
{
|
2008-05-05 02:08:26 +00:00
|
|
|
int password_bytes = std::min((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;
|
|
|
|
}
|
|
|
|
|
2009-07-12 22:52:13 +00:00
|
|
|
char const* p = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
while ((p = strchr(cstr, '\x28')) != 0)
|
|
|
|
{
|
|
|
|
if (memcmp(p, padding_string, len - (p - cstr)) == 0)
|
|
|
|
{
|
|
|
|
user_password = user_password.substr(0, p - cstr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
pad_or_truncate_password(std::string const& password)
|
|
|
|
{
|
|
|
|
char k1[key_bytes];
|
|
|
|
pad_or_truncate_password(password, k1);
|
|
|
|
return std::string(k1, key_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iterate_md5_digest(MD5& md5, MD5::Digest& digest, int iterations)
|
|
|
|
{
|
|
|
|
md5.digest(digest);
|
|
|
|
|
|
|
|
for (int i = 0; i < iterations; ++i)
|
|
|
|
{
|
|
|
|
MD5 m;
|
|
|
|
m.encodeDataIncrementally((char*)digest, sizeof(digest));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::compute_data_key(std::string const& encryption_key,
|
2009-10-17 03:14:47 +00:00
|
|
|
int objid, int generation,
|
|
|
|
bool use_aes)
|
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;
|
|
|
|
|
|
|
|
// Append low three bytes of object ID and low two bytes of generation
|
|
|
|
result += (char) (objid & 0xff);
|
|
|
|
result += (char) ((objid >> 8) & 0xff);
|
|
|
|
result += (char) ((objid >> 16) & 0xff);
|
|
|
|
result += (char) (generation & 0xff);
|
|
|
|
result += (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;
|
|
|
|
md5.encodeDataIncrementally(result.c_str(), result.length());
|
|
|
|
MD5::Digest digest;
|
|
|
|
md5.digest(digest);
|
|
|
|
return std::string((char*) digest,
|
|
|
|
std::min(result.length(), (size_t) 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::compute_encryption_key(
|
|
|
|
std::string const& password, EncryptionData const& data)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
MD5 md5;
|
|
|
|
md5.encodeDataIncrementally(
|
|
|
|
pad_or_truncate_password(password).c_str(), key_bytes);
|
|
|
|
md5.encodeDataIncrementally(data.O.c_str(), key_bytes);
|
|
|
|
char pbytes[4];
|
|
|
|
pbytes[0] = (char) (data.P & 0xff);
|
|
|
|
pbytes[1] = (char) ((data.P >> 8) & 0xff);
|
|
|
|
pbytes[2] = (char) ((data.P >> 16) & 0xff);
|
|
|
|
pbytes[3] = (char) ((data.P >> 24) & 0xff);
|
|
|
|
md5.encodeDataIncrementally(pbytes, 4);
|
2010-04-25 14:55:36 +00:00
|
|
|
md5.encodeDataIncrementally(data.id1.c_str(), data.id1.length());
|
2009-10-17 03:14:47 +00:00
|
|
|
if ((data.R >= 4) && (! data.encrypt_metadata))
|
|
|
|
{
|
|
|
|
char bytes[4];
|
|
|
|
memset(bytes, 0xff, 4);
|
|
|
|
md5.encodeDataIncrementally(bytes, 4);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
MD5::Digest digest;
|
2009-10-17 03:14:47 +00:00
|
|
|
iterate_md5_digest(md5, digest, ((data.R >= 3) ? 50 : 0));
|
2008-04-29 12:55:25 +00:00
|
|
|
return std::string((char*)digest, data.Length_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
compute_O_rc4_key(std::string const& user_password,
|
|
|
|
std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data,
|
|
|
|
unsigned char key[O_key_bytes])
|
|
|
|
{
|
|
|
|
std::string password = owner_password;
|
|
|
|
if (password.empty())
|
|
|
|
{
|
|
|
|
password = user_password;
|
|
|
|
}
|
|
|
|
MD5 md5;
|
|
|
|
md5.encodeDataIncrementally(
|
|
|
|
pad_or_truncate_password(password).c_str(), key_bytes);
|
|
|
|
MD5::Digest digest;
|
2009-10-17 03:14:47 +00:00
|
|
|
iterate_md5_digest(md5, digest, ((data.R >= 3) ? 50 : 0));
|
2008-04-29 12:55:25 +00:00
|
|
|
memcpy(key, digest, O_key_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
unsigned char O_key[O_key_bytes];
|
|
|
|
compute_O_rc4_key(user_password, owner_password, data, O_key);
|
|
|
|
|
|
|
|
char upass[key_bytes];
|
|
|
|
pad_or_truncate_password(user_password, upass);
|
|
|
|
iterate_rc4((unsigned char*) upass, key_bytes,
|
2009-10-17 03:14:47 +00:00
|
|
|
O_key, data.Length_bytes, (data.R >= 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];
|
|
|
|
pad_or_truncate_password("", udata);
|
|
|
|
iterate_rc4((unsigned char*) udata, key_bytes,
|
|
|
|
(unsigned char*)k1.c_str(), data.Length_bytes, 1, false);
|
|
|
|
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(
|
|
|
|
pad_or_truncate_password("").c_str(), key_bytes);
|
|
|
|
md5.encodeDataIncrementally(data.id1.c_str(), data.id1.length());
|
|
|
|
MD5::Digest digest;
|
|
|
|
md5.digest(digest);
|
|
|
|
iterate_rc4(digest, sizeof(MD5::Digest),
|
|
|
|
(unsigned char*) k1.c_str(), data.Length_bytes, 20, false);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
result[i] = (char)((i * i) % 0xff);
|
|
|
|
}
|
|
|
|
return std::string(result, key_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string
|
|
|
|
compute_U_value(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
2009-10-17 03:14:47 +00:00
|
|
|
if (data.R >= 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
|
|
|
|
check_user_password(std::string const& user_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
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);
|
2009-10-17 03:14:47 +00:00
|
|
|
int to_compare = ((data.R >= 3) ? sizeof(MD5::Digest) : key_bytes);
|
2008-04-29 12:55:25 +00:00
|
|
|
return (memcmp(data.U.c_str(), u_value.c_str(), to_compare) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
check_owner_password(std::string& user_password,
|
|
|
|
std::string const& owner_password,
|
|
|
|
QPDF::EncryptionData const& data)
|
|
|
|
{
|
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
|
|
|
|
|
|
|
unsigned char key[O_key_bytes];
|
|
|
|
compute_O_rc4_key(user_password, owner_password, data, key);
|
|
|
|
unsigned char O_data[key_bytes];
|
|
|
|
memcpy(O_data, (unsigned char*) data.O.c_str(), key_bytes);
|
|
|
|
iterate_rc4(O_data, key_bytes, key, data.Length_bytes,
|
2009-10-17 03:14:47 +00:00
|
|
|
(data.R >= 3) ? 20 : 1, true);
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string new_user_password =
|
|
|
|
std::string((char*)O_data, key_bytes);
|
|
|
|
bool result = false;
|
|
|
|
if (check_user_password(new_user_password, data))
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
user_password = new_user_password;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-10-17 23:37:55 +00:00
|
|
|
QPDF::encryption_method_e
|
|
|
|
QPDF::interpretCF(QPDFObjectHandle cf)
|
|
|
|
{
|
|
|
|
if (cf.isName())
|
|
|
|
{
|
|
|
|
std::string filter = cf.getName();
|
|
|
|
if (this->crypt_filters.count(filter) != 0)
|
|
|
|
{
|
|
|
|
return this->crypt_filters[filter];
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
if (this->encryption_initialized)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->encryption_initialized = true;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
if (! this->trailer.hasKey("/Encrypt"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-05 00:42:48 +00:00
|
|
|
// Go ahead and set this->encryption here. That way, isEncrypted
|
|
|
|
// will return true even if there were errors reading the
|
|
|
|
// encryption dictionary.
|
|
|
|
this->encrypted = true;
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle id_obj = this->trailer.getKey("/ID");
|
|
|
|
if (! (id_obj.isArray() &&
|
|
|
|
(id_obj.getArrayNItems() == 2) &&
|
|
|
|
id_obj.getArrayItem(0).isString()))
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
|
|
|
"trailer", this->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"invalid /ID in trailer dictionary");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string id1 = id_obj.getArrayItem(0).getStringValue();
|
2009-10-17 23:37:55 +00:00
|
|
|
QPDFObjectHandle encryption_dict = this->trailer.getKey("/Encrypt");
|
2008-04-29 12:55:25 +00:00
|
|
|
if (! encryption_dict.isDictionary())
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
this->last_object_description,
|
2010-09-24 19:10:08 +00:00
|
|
|
this->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")))
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
|
|
|
"encryption dictionary", this->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())
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
warn(QPDFExc(qpdf_e_unsupported, this->file->getName(),
|
|
|
|
"encryption dictionary", this->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()))
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
|
|
|
"encryption dictionary", this->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();
|
|
|
|
unsigned int P = (unsigned int) encryption_dict.getKey("/P").getIntValue();
|
|
|
|
|
2009-10-17 03:14:47 +00:00
|
|
|
if (! (((R == 2) || (R == 3) || (R == 4)) &&
|
|
|
|
((V == 1) || (V == 2) || (V == 4))))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_unsupported, this->file->getName(),
|
|
|
|
"encryption dictionary", this->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"Unsupported /R or /V in encryption dictionary");
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
this->encryption_V = V;
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
if (! ((O.length() == key_bytes) && (U.length() == key_bytes)))
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
|
|
|
"encryption dictionary", this->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"incorrect length for /O and/or /P in "
|
|
|
|
"encryption dictionary");
|
|
|
|
}
|
|
|
|
|
|
|
|
int Length = 40;
|
|
|
|
if (encryption_dict.getKey("/Length").isInteger())
|
|
|
|
{
|
|
|
|
Length = encryption_dict.getKey("/Length").getIntValue();
|
|
|
|
if ((Length % 8) || (Length < 40) || (Length > 128))
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
|
|
|
"encryption dictionary", this->file->getLastOffset(),
|
2008-04-29 12:55:25 +00:00
|
|
|
"invalid /Length value in encryption dictionary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
this->encrypt_metadata = true;
|
2009-10-17 03:14:47 +00:00
|
|
|
if ((V >= 4) && (encryption_dict.getKey("/EncryptMetadata").isBool()))
|
|
|
|
{
|
2009-10-17 18:54:51 +00:00
|
|
|
this->encrypt_metadata =
|
2009-10-17 03:14:47 +00:00
|
|
|
encryption_dict.getKey("/EncryptMetadata").getBoolValue();
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
|
|
|
|
if (V == 4)
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Don't complain now -- maybe we won't need
|
|
|
|
// to reference this type.
|
|
|
|
method = e_unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->crypt_filters[filter] = method;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle StmF = encryption_dict.getKey("/StmF");
|
|
|
|
QPDFObjectHandle StrF = encryption_dict.getKey("/StrF");
|
|
|
|
QPDFObjectHandle EFF = encryption_dict.getKey("/EFF");
|
|
|
|
this->cf_stream = interpretCF(StmF);
|
|
|
|
this->cf_string = interpretCF(StrF);
|
|
|
|
if (EFF.isName())
|
|
|
|
{
|
|
|
|
this->cf_file = interpretCF(EFF);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->cf_file = this->cf_stream;
|
|
|
|
}
|
2009-10-18 19:54:24 +00:00
|
|
|
if (this->cf_file != this->cf_stream)
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_unsupported, this->file->getName(),
|
|
|
|
"encryption dictionary", this->file->getLastOffset(),
|
2009-10-18 19:54:24 +00:00
|
|
|
"This document has embedded files that are"
|
|
|
|
" encrypted differently from the rest of the file."
|
|
|
|
" qpdf does not presently support this due to"
|
|
|
|
" lack of test data; if possible, please submit"
|
|
|
|
" a bug report that includes this file.");
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
EncryptionData data(V, R, Length / 8, P, O, U, id1, this->encrypt_metadata);
|
|
|
|
if (check_owner_password(
|
|
|
|
this->user_password, this->provided_password, data))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// password supplied was owner password; user_password has
|
|
|
|
// been initialized
|
|
|
|
}
|
|
|
|
else if (check_user_password(this->provided_password, data))
|
|
|
|
{
|
|
|
|
this->user_password = this->provided_password;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_password, this->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
"", 0, "invalid password");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->encryption_key = compute_encryption_key(this->user_password, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (! this->encrypted)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (! ((objid == this->cached_key_objid) &&
|
|
|
|
(generation == this->cached_key_generation)))
|
|
|
|
{
|
|
|
|
this->cached_object_encryption_key =
|
2009-10-17 18:54:51 +00:00
|
|
|
compute_data_key(this->encryption_key, objid, generation, use_aes);
|
2008-04-29 12:55:25 +00:00
|
|
|
this->cached_key_objid = objid;
|
|
|
|
this->cached_key_generation = generation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->cached_object_encryption_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (this->encryption_V == 4)
|
|
|
|
{
|
|
|
|
switch (this->cf_string)
|
|
|
|
{
|
|
|
|
case e_none:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case e_aes:
|
|
|
|
use_aes = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case e_rc4:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-09-24 19:10:08 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
this->last_object_description,
|
2010-09-24 19:10:08 +00:00
|
|
|
this->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.
|
|
|
|
this->cf_string = e_aes;
|
|
|
|
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");
|
|
|
|
assert(key.length() == Pl_AES_PDF::key_size);
|
|
|
|
Pl_Buffer bufpl("decrypted string");
|
|
|
|
Pl_AES_PDF pl("aes decrypt string", &bufpl, false,
|
|
|
|
(unsigned char const*)key.c_str());
|
|
|
|
pl.write((unsigned char*)str.c_str(), str.length());
|
|
|
|
pl.finish();
|
|
|
|
PointerHolder<Buffer> buf = bufpl.getBuffer();
|
|
|
|
str = std::string((char*)buf.getPointer()->getBuffer(),
|
|
|
|
(size_t)buf.getPointer()->getSize());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTC::TC("qpdf", "QPDF_encryption rc4 decode string");
|
|
|
|
unsigned int vlen = str.length();
|
|
|
|
// Using PointerHolder will cause a new char[] to be deleted
|
|
|
|
// with delete instead of delete [], but it's okay since the
|
|
|
|
// array is of a fundamental type, so there is no destructor
|
|
|
|
// to be called. Using PointerHolder guarantees that tmp will
|
|
|
|
// be freed even if rc4.process throws an exception.
|
|
|
|
PointerHolder<char> tmp = QUtil::copy_string(str);
|
|
|
|
RC4 rc4((unsigned char const*)key.c_str(), key.length());
|
|
|
|
rc4.process((unsigned char*)tmp.getPointer(), vlen);
|
|
|
|
str = std::string(tmp.getPointer(), vlen);
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
}
|
2009-10-19 23:09:19 +00:00
|
|
|
catch (QPDFExc& e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (std::runtime_error& e)
|
2009-10-17 18:54:51 +00:00
|
|
|
{
|
2010-09-24 19:10:08 +00:00
|
|
|
throw QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
|
|
|
this->last_object_description,
|
|
|
|
this->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;
|
|
|
|
if (this->encryption_V == 4)
|
|
|
|
{
|
2009-10-17 23:37:55 +00:00
|
|
|
encryption_method_e method = e_unknown;
|
|
|
|
std::string method_source = "/StmF from /Encrypt dictionary";
|
|
|
|
|
2009-10-19 01:58:31 +00:00
|
|
|
if (stream_dict.getKey("/Filter").isOrHasName("/Crypt") &&
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
|
2009-10-17 23:37:55 +00:00
|
|
|
if (method == e_unknown)
|
|
|
|
{
|
|
|
|
if ((! this->encrypt_metadata) && (type == "/Metadata"))
|
|
|
|
{
|
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
|
|
|
|
{
|
2009-10-18 19:54:24 +00:00
|
|
|
// NOTE: We should should use cf_file if this is an
|
|
|
|
// embedded file, but we can't yet detect embedded
|
|
|
|
// file streams as such.
|
2009-10-17 23:37:55 +00:00
|
|
|
method = this->cf_stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
case e_rc4:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// filter local to this stream.
|
2010-09-24 19:10:08 +00:00
|
|
|
warn(QPDFExc(qpdf_e_damaged_pdf, this->file->getName(),
|
2009-10-19 23:09:19 +00:00
|
|
|
this->last_object_description,
|
2010-09-24 19:10:08 +00:00
|
|
|
this->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.
|
|
|
|
this->cf_stream = e_aes;
|
|
|
|
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
|
|
|
assert(key.length() == Pl_AES_PDF::key_size);
|
|
|
|
pipeline = new Pl_AES_PDF("AES stream decryption", pipeline,
|
|
|
|
false, (unsigned char*) key.c_str());
|
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,
|
|
|
|
(unsigned char*) key.c_str(), key.length());
|
|
|
|
}
|
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)
|
|
|
|
{
|
2009-10-17 18:54:51 +00:00
|
|
|
EncryptionData data(V, R, key_len, P, "", "", id1, encrypt_metadata);
|
2008-04-29 12:55:25 +00:00
|
|
|
data.O = compute_O_value(user_password, owner_password, data);
|
|
|
|
O = data.O;
|
|
|
|
U = compute_U_value(user_password, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
2009-09-27 03:11:29 +00:00
|
|
|
QPDF::getPaddedUserPassword() const
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return this->user_password;
|
|
|
|
}
|
2009-09-27 03:11:29 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDF::getTrimmedUserPassword() const
|
|
|
|
{
|
|
|
|
std::string result = this->user_password;
|
|
|
|
trim_user_password(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::isEncrypted() const
|
|
|
|
{
|
|
|
|
return this->encrypted;
|
|
|
|
}
|
2009-09-27 20:05:38 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
QPDF::isEncrypted(int& R, int& P)
|
|
|
|
{
|
|
|
|
if (this->encrypted)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle trailer = getTrailer();
|
|
|
|
QPDFObjectHandle encrypt = trailer.getKey("/Encrypt");
|
|
|
|
QPDFObjectHandle Pkey = encrypt.getKey("/P");
|
|
|
|
QPDFObjectHandle Rkey = encrypt.getKey("/R");
|
|
|
|
P = Pkey.getIntValue();
|
|
|
|
R = Rkey.getIntValue();
|
|
|
|
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;
|
|
|
|
}
|