Coverage Report

Created: 2026-08-31 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/Pl_AES_PDF.cc
Line
Count
Source
1
#include <qpdf/Pl_AES_PDF.hh>
2
3
#include <qpdf/QIntC.hh>
4
#include <qpdf/QPDFCryptoProvider.hh>
5
#include <qpdf/QUtil.hh>
6
#include <qpdf/Util.hh>
7
8
#include <cstring>
9
#include <string>
10
11
using namespace qpdf;
12
13
bool Pl_AES_PDF::use_static_iv = false;
14
15
Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next, bool encrypt, std::string key) :
16
0
    Pipeline(identifier, next),
17
0
    key(key),
18
0
    crypto(QPDFCryptoProvider::getImpl()),
19
0
    encrypt(encrypt)
20
0
{
21
0
    util::assertion(next, "Attempt to create Pl_AES_PDF with nullptr as next");
22
0
    util::no_ci_rt_error_if(!(key.size() == 32 || key.size() == 16), "unsupported key length");
23
0
    std::memset(this->inbuf, 0, this->buf_size);
24
0
    std::memset(this->outbuf, 0, this->buf_size);
25
0
    std::memset(this->cbc_block, 0, this->buf_size);
26
0
}
27
28
void
29
Pl_AES_PDF::useZeroIV()
30
0
{
31
0
    use_zero_iv = true;
32
0
}
33
34
void
35
Pl_AES_PDF::disablePadding()
36
0
{
37
0
    disable_padding = true;
38
0
}
39
40
void
41
Pl_AES_PDF::setIV(unsigned char const* iv, size_t bytes)
42
0
{
43
0
    util::assertion(
44
0
        bytes == buf_size,
45
0
        "Pl_AES_PDF: specified initialization vector size in bytes must be " +
46
0
            std::to_string(bytes));
47
0
    use_specified_iv = true;
48
0
    memcpy(specified_iv, iv, bytes);
49
0
}
50
51
void
52
Pl_AES_PDF::disableCBC()
53
0
{
54
0
    cbc_mode = false;
55
0
}
56
57
void
58
Pl_AES_PDF::useStaticIV()
59
0
{
60
0
    use_static_iv = true;
61
0
}
62
63
void
64
Pl_AES_PDF::write(unsigned char const* data, size_t len)
65
0
{
66
0
    size_t bytes_left = len;
67
0
    unsigned char const* p = data;
68
69
0
    while (bytes_left > 0) {
70
0
        if (offset == buf_size) {
71
0
            flush(false);
72
0
        }
73
74
0
        size_t available = buf_size - offset;
75
0
        size_t bytes = (bytes_left < available ? bytes_left : available);
76
0
        bytes_left -= bytes;
77
0
        std::memcpy(inbuf + offset, p, bytes);
78
0
        offset += bytes;
79
0
        p += bytes;
80
0
    }
81
0
}
82
83
void
84
Pl_AES_PDF::finish()
85
0
{
86
0
    if (encrypt) {
87
0
        if (offset == buf_size) {
88
0
            flush(false);
89
0
        }
90
0
        if (!disable_padding) {
91
            // Pad as described in section 3.5.1 of version 1.7 of the PDF specification, including
92
            // providing an entire block of padding if the input was a multiple of 16 bytes.
93
0
            unsigned char pad = QIntC::to_uchar(buf_size - offset);
94
0
            memset(inbuf + offset, pad, pad);
95
0
            offset = buf_size;
96
0
            flush(false);
97
0
        }
98
0
    } else {
99
0
        if (offset != buf_size) {
100
            // This is never supposed to happen as the output is always supposed to be padded.
101
            // However, we have encountered files for which the output is not a multiple of the
102
            // block size.  In this case, pad with zeroes and hope for the best.
103
0
            util::assertion(offset < buf_size, "buffer overflow in AES encryption pipeline");
104
0
            std::memset(inbuf + offset, 0, buf_size - offset);
105
0
            offset = buf_size;
106
0
        }
107
0
        flush(!disable_padding);
108
0
    }
109
0
    crypto->rijndael_finalize();
110
0
    next()->finish();
111
0
}
112
113
void
114
Pl_AES_PDF::initializeVector()
115
0
{
116
0
    if (use_zero_iv) {
117
0
        for (unsigned int i = 0; i < buf_size; ++i) {
118
0
            cbc_block[i] = 0;
119
0
        }
120
0
    } else if (use_specified_iv) {
121
0
        std::memcpy(cbc_block, specified_iv, buf_size);
122
0
    } else if (use_static_iv) {
123
0
        for (unsigned int i = 0; i < buf_size; ++i) {
124
0
            cbc_block[i] = static_cast<unsigned char>(14U * (1U + i));
125
0
        }
126
0
    } else {
127
0
        QUtil::initializeWithRandomBytes(cbc_block, buf_size);
128
0
    }
129
0
}
130
131
void
132
Pl_AES_PDF::flush(bool strip_padding)
133
0
{
134
0
    util::assertion(offset == buf_size, "AES pipeline: flush called when buffer was not full");
135
136
0
    if (first) {
137
0
        first = false;
138
0
        bool return_after_init = false;
139
0
        if (cbc_mode) {
140
0
            if (encrypt) {
141
                // Set cbc_block to the initialization vector, and if not zero, write it to the
142
                // output stream.
143
0
                initializeVector();
144
0
                if (!(use_zero_iv || use_specified_iv)) {
145
0
                    next()->write(cbc_block, buf_size);
146
0
                }
147
0
            } else if (use_zero_iv || use_specified_iv) {
148
                // Initialize vector with zeroes; zero vector was not written to the beginning of
149
                // the input file.
150
0
                initializeVector();
151
0
            } else {
152
                // Take the first block of input as the initialization vector.  There's nothing to
153
                // write at this time.
154
0
                memcpy(cbc_block, inbuf, buf_size);
155
0
                offset = 0;
156
0
                return_after_init = true;
157
0
            }
158
0
        }
159
0
        crypto->rijndael_init(
160
0
            encrypt,
161
0
            reinterpret_cast<const unsigned char*>(key.data()),
162
0
            key.size(),
163
0
            cbc_mode,
164
0
            cbc_block);
165
0
        if (return_after_init) {
166
0
            return;
167
0
        }
168
0
    }
169
170
0
    crypto->rijndael_process(inbuf, outbuf);
171
0
    unsigned int bytes = buf_size;
172
0
    if (strip_padding) {
173
0
        unsigned char last = outbuf[buf_size - 1];
174
0
        if (last <= buf_size) {
175
0
            bool strip = true;
176
0
            for (unsigned int i = 1; i <= last; ++i) {
177
0
                if (outbuf[buf_size - i] != last) {
178
0
                    strip = false;
179
0
                    break;
180
0
                }
181
0
            }
182
0
            if (strip) {
183
0
                bytes -= last;
184
0
            }
185
0
        }
186
0
    }
187
0
    offset = 0;
188
0
    next()->write(outbuf, bytes);
189
0
}