aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFCrypto_openssl.cc
blob: 92125b45c23a07ddca2d167416fab56608379943 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <qpdf/QPDFCrypto_openssl.hh>

#include <cstring>
#include <memory>
#include <stdexcept>
#include <string>

#if (defined(__GNUC__) || defined(__clang__))
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
#include <openssl/err.h>
#ifndef QPDF_OPENSSL_1
# include <openssl/provider.h>
#endif
#if (defined(__GNUC__) || defined(__clang__))
# pragma GCC diagnostic pop
#endif

#include <qpdf/QIntC.hh>

#ifndef QPDF_OPENSSL_1
namespace
{
    class RC4Loader
    {
      public:
        static EVP_CIPHER const* getRC4();
        ~RC4Loader();

      private:
        RC4Loader();
        OSSL_PROVIDER* legacy;
        OSSL_LIB_CTX* libctx;
        EVP_CIPHER* rc4;
    };
} // namespace

EVP_CIPHER const*
RC4Loader::getRC4()
{
    static auto loader = std::shared_ptr<RC4Loader>(new RC4Loader());
    return loader->rc4;
}

RC4Loader::RC4Loader()
{
    libctx = OSSL_LIB_CTX_new();
    if (libctx == nullptr) {
        throw std::runtime_error("unable to create openssl library context");
        return;
    }
    legacy = OSSL_PROVIDER_load(libctx, "legacy");
    if (legacy == nullptr) {
        OSSL_LIB_CTX_free(libctx);
        throw std::runtime_error("unable to load openssl legacy provider");
        return;
    }
    rc4 = EVP_CIPHER_fetch(libctx, "RC4", nullptr);
    if (rc4 == nullptr) {
        OSSL_PROVIDER_unload(legacy);
        OSSL_LIB_CTX_free(libctx);
        throw std::runtime_error("unable to load openssl rc4 algorithm");
        return;
    }
}

RC4Loader::~RC4Loader()
{
    EVP_CIPHER_free(rc4);
    OSSL_PROVIDER_unload(legacy);
    OSSL_LIB_CTX_free(libctx);
}
#endif // not QPDF_OPENSSL_1

static void
bad_bits(int bits)
{
    throw std::logic_error(std::string("unsupported key length: ") + std::to_string(bits));
}

static void
check_openssl(int status)
{
    if (status != 1) {
        // OpenSSL creates a "queue" of errors; copy the first (innermost) error to the exception
        // message.
        char buf[256] = "";
        ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
        std::string what = "OpenSSL error: ";
        what += buf;
        throw std::runtime_error(what);
    }
    ERR_clear_error();
}

QPDFCrypto_openssl::QPDFCrypto_openssl() :
    md_ctx(EVP_MD_CTX_new()),
    cipher_ctx(EVP_CIPHER_CTX_new())
{
    memset(md_out, 0, sizeof(md_out));
    EVP_MD_CTX_init(md_ctx);
    EVP_CIPHER_CTX_init(cipher_ctx);
}

QPDFCrypto_openssl::~QPDFCrypto_openssl()
{
    EVP_MD_CTX_reset(md_ctx);
    EVP_CIPHER_CTX_reset(cipher_ctx);
    EVP_CIPHER_CTX_free(cipher_ctx);
    EVP_MD_CTX_free(md_ctx);
}

void
QPDFCrypto_openssl::provideRandomData(unsigned char* data, size_t len)
{
    check_openssl(RAND_bytes(data, QIntC::to_int(len)));
}

void
QPDFCrypto_openssl::MD5_init()
{
    check_openssl(EVP_MD_CTX_reset(md_ctx));
    check_openssl(EVP_DigestInit_ex(md_ctx, EVP_md5(), nullptr));
}

void
QPDFCrypto_openssl::SHA2_init(int bits)
{
    static const EVP_MD* md = EVP_sha512();
    switch (bits) {
    case 256:
        md = EVP_sha256();
        break;
    case 384:
        md = EVP_sha384();
        break;
    case 512:
        md = EVP_sha512();
        break;
    default:
        bad_bits(bits);
        return;
    }
    sha2_bits = static_cast<size_t>(bits);
    check_openssl(EVP_MD_CTX_reset(md_ctx));
    check_openssl(EVP_DigestInit_ex(md_ctx, md, nullptr));
}

void
QPDFCrypto_openssl::MD5_update(unsigned char const* data, size_t len)
{
    check_openssl(EVP_DigestUpdate(md_ctx, data, len));
}

void
QPDFCrypto_openssl::SHA2_update(unsigned char const* data, size_t len)
{
    check_openssl(EVP_DigestUpdate(md_ctx, data, len));
}

void
QPDFCrypto_openssl::MD5_finalize()
{
#ifdef QPDF_OPENSSL_1
    auto md = EVP_MD_CTX_md(md_ctx);
#else
    auto md = EVP_MD_CTX_get0_md(md_ctx);
#endif
    if (md) {
        check_openssl(EVP_DigestFinal(md_ctx, md_out + 0, nullptr));
    }
}

void
QPDFCrypto_openssl::SHA2_finalize()
{
#ifdef QPDF_OPENSSL_1
    auto md = EVP_MD_CTX_md(md_ctx);
#else
    auto md = EVP_MD_CTX_get0_md(md_ctx);
#endif
    if (md) {
        check_openssl(EVP_DigestFinal(md_ctx, md_out + 0, nullptr));
    }
}

void
QPDFCrypto_openssl::MD5_digest(MD5_Digest d)
{
    memcpy(d, md_out, sizeof(QPDFCryptoImpl::MD5_Digest));
}

std::string
QPDFCrypto_openssl::SHA2_digest()
{
    return {reinterpret_cast<char*>(md_out), sha2_bits / 8};
}

void
QPDFCrypto_openssl::RC4_init(unsigned char const* key_data, int key_len)
{
#ifdef QPDF_OPENSSL_1
    static auto const rc4 = EVP_rc4();
#else
    static auto const rc4 = RC4Loader::getRC4();
#endif
    check_openssl(EVP_CIPHER_CTX_reset(cipher_ctx));
    if (key_len == -1) {
        key_len = QIntC::to_int(strlen(reinterpret_cast<const char*>(key_data)));
    }
    check_openssl(EVP_EncryptInit_ex(cipher_ctx, rc4, nullptr, nullptr, nullptr));
    check_openssl(EVP_CIPHER_CTX_set_key_length(cipher_ctx, key_len));
    check_openssl(EVP_EncryptInit_ex(cipher_ctx, nullptr, nullptr, key_data, nullptr));
}

void
QPDFCrypto_openssl::rijndael_init(
    bool encrypt,
    unsigned char const* key_data,
    size_t key_len,
    bool cbc_mode,
    unsigned char* cbc_block)
{
    const EVP_CIPHER* cipher = nullptr;
    switch (key_len) {
    case 32:
        cipher = cbc_mode ? EVP_aes_256_cbc() : EVP_aes_256_ecb();
        break;
    case 24:
        cipher = cbc_mode ? EVP_aes_192_cbc() : EVP_aes_192_ecb();
        break;
    default:
        cipher = cbc_mode ? EVP_aes_128_cbc() : EVP_aes_128_ecb();
        break;
    }

    check_openssl(EVP_CIPHER_CTX_reset(cipher_ctx));
    check_openssl(
        // line-break
        EVP_CipherInit_ex(cipher_ctx, cipher, nullptr, key_data, cbc_block, encrypt));
    check_openssl(EVP_CIPHER_CTX_set_padding(cipher_ctx, 0));
}

void
QPDFCrypto_openssl::RC4_process(unsigned char const* in_data, size_t len, unsigned char* out_data)
{
    int out_len = static_cast<int>(len);
    check_openssl(EVP_EncryptUpdate(cipher_ctx, out_data, &out_len, in_data, out_len));
}

void
QPDFCrypto_openssl::rijndael_process(unsigned char* in_data, unsigned char* out_data)
{
    int len = QPDFCryptoImpl::rijndael_buf_size;
    check_openssl(EVP_CipherUpdate(cipher_ctx, out_data, &len, in_data, len));
}

void
QPDFCrypto_openssl::RC4_finalize()
{
    if (EVP_CIPHER_CTX_cipher(cipher_ctx)) {
        check_openssl(EVP_CIPHER_CTX_reset(cipher_ctx));
    }
}

void
QPDFCrypto_openssl::rijndael_finalize()
{
    if (EVP_CIPHER_CTX_cipher(cipher_ctx)) {
        check_openssl(EVP_CIPHER_CTX_reset(cipher_ctx));
    }
}