-
Notifications
You must be signed in to change notification settings - Fork 18
/
openssl_wrappers.h
419 lines (378 loc) · 12.2 KB
/
openssl_wrappers.h
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#define FMT_HEADER_ONLY
#include <ccf/crypto/pem.h>
#include <chrono>
#include <ds/x509_time_fmt.h>
#include <fmt/format.h>
#include <memory>
#include <openssl/asn1.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
// Note: This file was extended from:
// https://github.com/microsoft/CCF/blob/main/src/crypto/openssl/openssl_wrappers.h
#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3
# include <openssl/evp.h>
#endif
namespace scitt
{
namespace OpenSSL
{
/*
* Generic OpenSSL error handling
*/
/// Returns the error string from an error code
inline std::string error_string(unsigned long ec)
{
// ERR_error_string doesn't really expect the code could actually be zero
// and uses the `static char buf[256]` which is NOT cleaned nor checked
// if it has changed. So we use ERR_error_string_n directly.
if (ec)
{
std::string err(256, '\0');
ERR_error_string_n(ec, err.data(), err.size());
// Remove any trailing NULs before returning
err.resize(std::strlen(err.c_str()));
return err;
}
else
{
return "unknown error";
}
}
/// Throws if rc is not 1 and has error
inline void CHECK1(int rc)
{
unsigned long ec = ERR_get_error();
if (rc != 1 && ec != 0)
{
throw std::runtime_error(
fmt::format("OpenSSL error: {}", error_string(ec)));
}
}
/// Throws if rc is 0 and has error
inline void CHECK0(int rc)
{
unsigned long ec = ERR_get_error();
if (rc == 0 && ec != 0)
{
throw std::runtime_error(
fmt::format("OpenSSL error: {}", error_string(ec)));
}
}
/// Throws if ptr is null
inline void CHECKNULL(void* ptr)
{
if (ptr == NULL)
{
throw std::runtime_error("OpenSSL error: missing object");
}
}
// Throws if values are not equal
inline void CHECKEQUAL(int expect, int actual)
{
if (expect != actual)
{
unsigned long ec = ERR_get_error();
throw std::runtime_error(
fmt::format("OpenSSL error: {}", error_string(ec)));
}
}
// Throws if value is not positive
inline void CHECKPOSITIVE(int val)
{
if (val <= 0)
{
throw std::runtime_error("OpenSSL error: expected positive value");
}
}
/*
* Unique pointer wrappers for SSL objects, with SSL' specific constructors
* and destructors. Some objects need special functionality, others are just
* wrappers around the same template interface Unique_SSL_OBJECT.
*/
/// Generic template interface for different types of objects below
/// If there are no c-tors in the derived class that matches this one,
/// pass `nullptr` to the CTOR/DTOR parameters and make sure to implement
/// and delete the appropriate c-tors in the derived class.
template <class T, T* (*CTOR)(), void (*DTOR)(T*)>
class Unique_SSL_OBJECT
{
protected:
/// Pointer owning storage
std::unique_ptr<T, void (*)(T*)> p;
public:
/// C-tor with new pointer via T's c-tor
Unique_SSL_OBJECT() : p(CTOR(), DTOR)
{
CHECKNULL(p.get());
}
/// C-tor with pointer created in base class
Unique_SSL_OBJECT(T* ptr, void (*dtor)(T*), bool check_null = true) :
p(ptr, dtor)
{
if (check_null)
{
CHECKNULL(p.get());
}
}
/// Type cast to underlying pointer
operator T*()
{
return p.get();
}
/// Type cast to underlying pointer
operator T*() const
{
return p.get();
}
/// Reset pointer, free old if any
void reset(T* other)
{
p.reset(other);
}
/// Release pointer, so it's freed elsewhere (CAUTION!)
T* release()
{
return p.release();
}
};
struct Unique_BIO : public Unique_SSL_OBJECT<BIO, nullptr, nullptr>
{
Unique_BIO() :
Unique_SSL_OBJECT(BIO_new(BIO_s_mem()), [](auto x) { BIO_free(x); })
{}
Unique_BIO(const void* buf, int len) :
Unique_SSL_OBJECT(
BIO_new_mem_buf(buf, len), [](auto x) { BIO_free(x); })
{}
Unique_BIO(std::span<const uint8_t> d) :
Unique_SSL_OBJECT(
BIO_new_mem_buf(d.data(), d.size()), [](auto x) { BIO_free(x); })
{}
Unique_BIO(const ccf::crypto::Pem& pem) :
Unique_SSL_OBJECT(
BIO_new_mem_buf(pem.data(), -1), [](auto x) { BIO_free(x); })
{}
Unique_BIO(SSL_CTX* ctx) :
Unique_SSL_OBJECT(
BIO_new_ssl_connect(ctx), [](auto x) { BIO_free_all(x); })
{}
};
struct Unique_SSL_CTX : public Unique_SSL_OBJECT<SSL_CTX, nullptr, nullptr>
{
Unique_SSL_CTX(const SSL_METHOD* m) :
Unique_SSL_OBJECT(SSL_CTX_new(m), SSL_CTX_free)
{}
};
struct Unique_SSL : public Unique_SSL_OBJECT<SSL, nullptr, nullptr>
{
Unique_SSL(SSL_CTX* ctx) : Unique_SSL_OBJECT(SSL_new(ctx), SSL_free) {}
};
struct Unique_PKEY
: public Unique_SSL_OBJECT<EVP_PKEY, EVP_PKEY_new, EVP_PKEY_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
Unique_PKEY(BIO* mem) :
Unique_SSL_OBJECT(
PEM_read_bio_PUBKEY(mem, NULL, NULL, NULL), EVP_PKEY_free)
{}
#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3
Unique_PKEY(EVP_PKEY* pkey) :
Unique_SSL_OBJECT(EVP_PKEY_dup(pkey), EVP_PKEY_free)
{}
#endif
};
struct Unique_EVP_PKEY_CTX
: public Unique_SSL_OBJECT<EVP_PKEY_CTX, nullptr, nullptr>
{
Unique_EVP_PKEY_CTX(EVP_PKEY* key) :
Unique_SSL_OBJECT(EVP_PKEY_CTX_new(key, NULL), EVP_PKEY_CTX_free)
{}
Unique_EVP_PKEY_CTX() :
Unique_SSL_OBJECT(
EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL), EVP_PKEY_CTX_free)
{}
#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3
Unique_EVP_PKEY_CTX(const std::string& name) :
Unique_SSL_OBJECT(
EVP_PKEY_CTX_new_from_name(NULL, name.c_str(), NULL),
EVP_PKEY_CTX_free)
{}
#endif
};
struct Unique_X509_REQ
: public Unique_SSL_OBJECT<X509_REQ, X509_REQ_new, X509_REQ_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
Unique_X509_REQ(BIO* mem) :
Unique_SSL_OBJECT(
PEM_read_bio_X509_REQ(mem, NULL, NULL, NULL), X509_REQ_free)
{}
};
struct Unique_X509_CRL
: public Unique_SSL_OBJECT<X509_CRL, X509_CRL_new, X509_CRL_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
Unique_X509_CRL(BIO* mem) :
Unique_SSL_OBJECT(
PEM_read_bio_X509_CRL(mem, NULL, NULL, NULL), X509_CRL_free)
{}
};
struct Unique_X509 : public Unique_SSL_OBJECT<X509, X509_new, X509_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
// p == nullptr is OK (e.g. wrong format)
Unique_X509(BIO* mem, bool pem, bool check_null = false) :
Unique_SSL_OBJECT(
pem ? PEM_read_bio_X509(mem, NULL, NULL, NULL) :
d2i_X509_bio(mem, NULL),
X509_free,
check_null)
{}
Unique_X509(X509* cert, bool check_null) :
Unique_SSL_OBJECT(cert, X509_free, check_null)
{}
};
struct Unique_X509_STORE
: public Unique_SSL_OBJECT<X509_STORE, X509_STORE_new, X509_STORE_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
struct Unique_X509_STORE_CTX : public Unique_SSL_OBJECT<
X509_STORE_CTX,
X509_STORE_CTX_new,
X509_STORE_CTX_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
struct Unique_EVP_CIPHER_CTX : public Unique_SSL_OBJECT<
EVP_CIPHER_CTX,
EVP_CIPHER_CTX_new,
EVP_CIPHER_CTX_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
struct Unique_STACK_OF_X509
: public Unique_SSL_OBJECT<STACK_OF(X509), nullptr, nullptr>
{
Unique_STACK_OF_X509() :
Unique_SSL_OBJECT(
sk_X509_new_null(), [](auto x) { sk_X509_pop_free(x, X509_free); })
{}
};
struct Unique_STACK_OF_X509_EXTENSIONS
: public Unique_SSL_OBJECT<STACK_OF(X509_EXTENSION), nullptr, nullptr>
{
Unique_STACK_OF_X509_EXTENSIONS() :
Unique_SSL_OBJECT(sk_X509_EXTENSION_new_null(), [](auto x) {
sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free);
})
{}
Unique_STACK_OF_X509_EXTENSIONS(STACK_OF(X509_EXTENSION) * exts) :
Unique_SSL_OBJECT(
exts,
[](auto x) { sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free); },
/*check_null=*/false)
{}
};
struct Unique_ECDSA_SIG
: public Unique_SSL_OBJECT<ECDSA_SIG, ECDSA_SIG_new, ECDSA_SIG_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
Unique_ECDSA_SIG(ECDSA_SIG* sig, bool check_null) :
Unique_SSL_OBJECT(sig, ECDSA_SIG_free, check_null)
{}
};
struct Unique_BIGNUM : public Unique_SSL_OBJECT<BIGNUM, BN_new, BN_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
Unique_BIGNUM(const BIGNUM* n) : Unique_BIGNUM(BN_dup(n), BN_free) {}
};
struct Unique_X509_TIME
: public Unique_SSL_OBJECT<ASN1_TIME, ASN1_TIME_new, ASN1_TIME_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
Unique_X509_TIME(const std::string& s) :
Unique_SSL_OBJECT(ASN1_TIME_new(), ASN1_TIME_free, /*check_null=*/false)
{
auto t = ::ds::to_x509_time_string(s);
CHECK1(ASN1_TIME_set_string(*this, t.c_str()));
CHECK1(ASN1_TIME_normalize(*this));
}
Unique_X509_TIME(ASN1_TIME* t) :
Unique_SSL_OBJECT(t, ASN1_TIME_free, /*check_null=*/false)
{}
Unique_X509_TIME(const std::chrono::system_clock::time_point& t) :
Unique_X509_TIME(::ds::to_x509_time_string(t))
{}
};
struct Unique_BN_CTX
: public Unique_SSL_OBJECT<BN_CTX, BN_CTX_new, BN_CTX_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
struct Unique_EC_GROUP
: public Unique_SSL_OBJECT<EC_GROUP, nullptr, nullptr>
{
Unique_EC_GROUP(int nid) :
Unique_SSL_OBJECT(
EC_GROUP_new_by_curve_name(nid), EC_GROUP_free, /*check_null=*/true)
{}
};
struct Unique_EC_POINT
: public Unique_SSL_OBJECT<EC_POINT, nullptr, nullptr>
{
Unique_EC_POINT(EC_GROUP* group) :
Unique_SSL_OBJECT(
EC_POINT_new(group), EC_POINT_free, /*check_null=*/true)
{}
Unique_EC_POINT(EC_POINT* point) :
Unique_SSL_OBJECT(point, EC_POINT_free, /*check_null=*/true)
{}
};
#if !(defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3)
struct Unique_EC_KEY : public Unique_SSL_OBJECT<EC_KEY, nullptr, nullptr>
{
Unique_EC_KEY(int nid) :
Unique_SSL_OBJECT(
EC_KEY_new_by_curve_name(nid), EC_KEY_free, /*check_null=*/true)
{}
Unique_EC_KEY(EC_KEY* key) :
Unique_SSL_OBJECT(key, EC_KEY_free, /*check_null=*/true)
{}
};
struct Unique_RSA : public Unique_SSL_OBJECT<RSA, RSA_new, RSA_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
#endif
struct Unique_EVP_ENCODE_CTX : public Unique_SSL_OBJECT<
EVP_ENCODE_CTX,
EVP_ENCODE_CTX_new,
EVP_ENCODE_CTX_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
struct Unique_EVP_MD_CTX
: public Unique_SSL_OBJECT<EVP_MD_CTX, EVP_MD_CTX_new, EVP_MD_CTX_free>
{
using Unique_SSL_OBJECT::Unique_SSL_OBJECT;
};
struct Unique_EVP_PKEY
: public Unique_SSL_OBJECT<EVP_PKEY, EVP_PKEY_new, EVP_PKEY_free>
{
Unique_EVP_PKEY() = default;
Unique_EVP_PKEY(EVP_PKEY* key) : Unique_SSL_OBJECT(key, EVP_PKEY_free) {}
};
}
}