2009-10-17 03:14:47 +00:00
|
|
|
#include <qpdf/Pl_AES_PDF.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
|
|
|
#include <qpdf/QPDFCryptoProvider.hh>
|
2009-10-17 03:14:47 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <cstring>
|
2009-10-17 03:14:47 +00:00
|
|
|
#include <stdexcept>
|
2009-10-17 18:54:51 +00:00
|
|
|
#include <stdlib.h>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <string>
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2009-10-19 00:36:51 +00:00
|
|
|
bool Pl_AES_PDF::use_static_iv = false;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
Pl_AES_PDF::Pl_AES_PDF(
|
|
|
|
char const* identifier,
|
|
|
|
Pipeline* next,
|
|
|
|
bool encrypt,
|
|
|
|
unsigned char const* key,
|
|
|
|
size_t key_bytes) :
|
2009-10-17 03:14:47 +00:00
|
|
|
Pipeline(identifier, next),
|
2019-11-04 18:59:19 +00:00
|
|
|
crypto(QPDFCryptoProvider::getImpl()),
|
2009-10-17 03:14:47 +00:00
|
|
|
encrypt(encrypt),
|
2009-10-17 18:54:51 +00:00
|
|
|
cbc_mode(true),
|
|
|
|
first(true),
|
2009-10-17 15:01:20 +00:00
|
|
|
offset(0),
|
2019-11-04 19:41:10 +00:00
|
|
|
key_bytes(key_bytes),
|
2012-12-28 02:37:57 +00:00
|
|
|
use_zero_iv(false),
|
2012-12-29 15:43:46 +00:00
|
|
|
use_specified_iv(false),
|
2012-12-28 02:37:57 +00:00
|
|
|
disable_padding(false)
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
2022-02-05 13:15:07 +00:00
|
|
|
this->key = std::make_unique<unsigned char[]>(key_bytes);
|
2019-11-04 18:59:19 +00:00
|
|
|
std::memcpy(this->key.get(), key, key_bytes);
|
2009-10-17 15:01:20 +00:00
|
|
|
std::memset(this->inbuf, 0, this->buf_size);
|
|
|
|
std::memset(this->outbuf, 0, this->buf_size);
|
2009-10-17 18:54:51 +00:00
|
|
|
std::memset(this->cbc_block, 0, this->buf_size);
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2012-12-28 02:37:57 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::useZeroIV()
|
|
|
|
{
|
|
|
|
this->use_zero_iv = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_AES_PDF::disablePadding()
|
|
|
|
{
|
|
|
|
this->disable_padding = true;
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
|
2012-12-29 15:43:46 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::setIV(unsigned char const* iv, size_t bytes)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bytes != this->buf_size) {
|
2012-12-29 15:43:46 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"Pl_AES_PDF: specified initialization vector"
|
2022-04-02 21:14:10 +00:00
|
|
|
" size in bytes must be " +
|
2022-09-21 16:49:21 +00:00
|
|
|
std::to_string(bytes));
|
2012-12-29 15:43:46 +00:00
|
|
|
}
|
|
|
|
this->use_specified_iv = true;
|
|
|
|
memcpy(this->specified_iv, iv, bytes);
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::disableCBC()
|
|
|
|
{
|
|
|
|
this->cbc_mode = false;
|
|
|
|
}
|
|
|
|
|
2009-10-19 00:36:51 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::useStaticIV()
|
|
|
|
{
|
|
|
|
use_static_iv = true;
|
|
|
|
}
|
|
|
|
|
2009-10-17 03:14:47 +00:00
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_AES_PDF::write(unsigned char const* data, size_t len)
|
2009-10-17 03:14:47 +00:00
|
|
|
{
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t bytes_left = len;
|
2022-05-03 21:43:07 +00:00
|
|
|
unsigned char const* p = data;
|
2009-10-17 03:14:47 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
while (bytes_left > 0) {
|
|
|
|
if (this->offset == this->buf_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
flush(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t available = this->buf_size - this->offset;
|
|
|
|
size_t bytes = (bytes_left < available ? bytes_left : available);
|
|
|
|
bytes_left -= bytes;
|
|
|
|
std::memcpy(this->inbuf + this->offset, p, bytes);
|
|
|
|
this->offset += bytes;
|
|
|
|
p += bytes;
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_AES_PDF::finish()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->encrypt) {
|
|
|
|
if (this->offset == this->buf_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
flush(false);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->disable_padding) {
|
2012-12-28 02:37:57 +00:00
|
|
|
// Pad as described in section 3.5.1 of version 1.7 of the PDF
|
|
|
|
// specification, including providing an entire block of padding
|
|
|
|
// if the input was a multiple of 16 bytes.
|
2022-04-02 21:14:10 +00:00
|
|
|
unsigned char pad = QIntC::to_uchar(this->buf_size - this->offset);
|
2012-12-28 02:37:57 +00:00
|
|
|
memset(this->inbuf + this->offset, pad, pad);
|
|
|
|
this->offset = this->buf_size;
|
|
|
|
flush(false);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
if (this->offset != this->buf_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// This is never supposed to happen as the output is
|
|
|
|
// always supposed to be padded. However, we have
|
|
|
|
// encountered files for which the output is not a
|
|
|
|
// multiple of the block size. In this case, pad with
|
|
|
|
// zeroes and hope for the best.
|
2022-05-03 11:41:07 +00:00
|
|
|
if (this->offset >= this->buf_size) {
|
|
|
|
throw std::logic_error("buffer overflow in AES encryption"
|
|
|
|
" pipeline");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
std::memset(
|
|
|
|
this->inbuf + this->offset, 0, this->buf_size - this->offset);
|
2022-02-08 14:18:08 +00:00
|
|
|
this->offset = this->buf_size;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
flush(!this->disable_padding);
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
2019-11-04 18:59:19 +00:00
|
|
|
this->crypto->rijndael_finalize();
|
2009-10-17 03:14:47 +00:00
|
|
|
getNext()->finish();
|
|
|
|
}
|
|
|
|
|
2009-10-17 18:54:51 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::initializeVector()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (use_zero_iv) {
|
|
|
|
for (unsigned int i = 0; i < this->buf_size; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->cbc_block[i] = 0;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (use_specified_iv) {
|
2012-12-29 15:43:46 +00:00
|
|
|
std::memcpy(this->cbc_block, this->specified_iv, this->buf_size);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (use_static_iv) {
|
|
|
|
for (unsigned int i = 0; i < this->buf_size; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->cbc_block[i] = static_cast<unsigned char>(14U * (1U + i));
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2012-12-28 21:37:46 +00:00
|
|
|
QUtil::initializeWithRandomBytes(this->cbc_block, this->buf_size);
|
2009-10-17 18:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 03:14:47 +00:00
|
|
|
void
|
|
|
|
Pl_AES_PDF::flush(bool strip_padding)
|
|
|
|
{
|
2022-05-03 11:41:07 +00:00
|
|
|
if (this->offset != this->buf_size) {
|
|
|
|
throw std::logic_error(
|
|
|
|
"AES pipeline: flush called when buffer was not full");
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (first) {
|
2022-02-08 14:18:08 +00:00
|
|
|
first = false;
|
2019-11-04 19:41:10 +00:00
|
|
|
bool return_after_init = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->cbc_mode) {
|
|
|
|
if (encrypt) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Set cbc_block to the initialization vector, and if
|
|
|
|
// not zero, write it to the output stream.
|
|
|
|
initializeVector();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!(this->use_zero_iv || this->use_specified_iv)) {
|
2012-12-28 02:37:57 +00:00
|
|
|
getNext()->write(this->cbc_block, this->buf_size);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (this->use_zero_iv || this->use_specified_iv) {
|
2012-12-28 02:37:57 +00:00
|
|
|
// Initialize vector with zeroes; zero vector was not
|
|
|
|
// written to the beginning of the input file.
|
|
|
|
initializeVector();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Take the first block of input as the initialization
|
|
|
|
// vector. There's nothing to write at this time.
|
|
|
|
memcpy(this->cbc_block, this->inbuf, this->buf_size);
|
|
|
|
this->offset = 0;
|
2019-11-04 19:41:10 +00:00
|
|
|
return_after_init = true;
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 19:41:10 +00:00
|
|
|
this->crypto->rijndael_init(
|
2022-04-02 21:14:10 +00:00
|
|
|
encrypt,
|
|
|
|
this->key.get(),
|
|
|
|
key_bytes,
|
|
|
|
this->cbc_mode,
|
|
|
|
this->cbc_block);
|
|
|
|
if (return_after_init) {
|
2019-11-04 19:41:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-10-17 18:54:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 09:50:42 +00:00
|
|
|
this->crypto->rijndael_process(this->inbuf, this->outbuf);
|
2009-10-17 03:14:47 +00:00
|
|
|
unsigned int bytes = this->buf_size;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (strip_padding) {
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned char last = this->outbuf[this->buf_size - 1];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (last <= this->buf_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
bool strip = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (unsigned int i = 1; i <= last; ++i) {
|
|
|
|
if (this->outbuf[this->buf_size - i] != last) {
|
2022-02-08 14:18:08 +00:00
|
|
|
strip = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (strip) {
|
2022-02-08 14:18:08 +00:00
|
|
|
bytes -= last;
|
|
|
|
}
|
|
|
|
}
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|
|
|
|
this->offset = 0;
|
2021-01-04 16:55:28 +00:00
|
|
|
getNext()->write(this->outbuf, bytes);
|
2009-10-17 03:14:47 +00:00
|
|
|
}
|