12#include <fmt/format.h>
14#include <openssl/asn1.h>
15#include <openssl/bn.h>
16#include <openssl/ec.h>
17#include <openssl/engine.h>
18#include <openssl/err.h>
19#include <openssl/evp.h>
20#include <openssl/pem.h>
21#include <openssl/rsa.h>
22#include <openssl/ssl.h>
23#include <openssl/x509.h>
24#include <openssl/x509v3.h>
40 constexpr size_t max_error_size = 256;
41 std::string err(max_error_size,
'\0');
42 ERR_load_crypto_strings();
43 SSL_load_error_strings();
44 ERR_error_string_n(ec, err.data(), err.size());
47 err.resize(std::strlen(err.c_str()));
50 return "unknown error";
58 unsigned long ec = ERR_get_error();
59 throw std::runtime_error(fmt::format(
60 "OpenSSL error (rc={}, ec={}): {}", rc, ec,
error_string(ec)));
69 throw std::runtime_error(
"OpenSSL error: missing object");
78 unsigned long ec = ERR_get_error();
79 throw std::runtime_error(fmt::format(
80 "OpenSSL error (rc={}, ec={}): {}", actual, ec,
error_string(ec)));
89 unsigned long ec = ERR_get_error();
90 throw std::runtime_error(fmt::format(
91 "OpenSSL error (rc={}, ec={}): {}", val, ec,
error_string(ec)));
105 template <
class T, T* (*CTOR)(),
void (*DTOR)(T*)>
110 std::unique_ptr<T, void (*)(T*)>
p;
159 BIO_new_mem_buf(s.data(), s.size()), [](auto x) { BIO_free(x); })
163 BIO_new_mem_buf(pem.data(), -1), [](auto x) { BIO_free(x); })
167 BIO_new_ssl_connect(ctx), [](auto x) { BIO_free_all(x); })
189 PEM_read_bio_PUBKEY(mem, nullptr, nullptr, nullptr), EVP_PKEY_free)
205 EVP_PKEY_CTX_new_id(key_type, nullptr), EVP_PKEY_CTX_free)
210 EVP_PKEY_CTX_new_from_name(nullptr, name.c_str(), nullptr),
228 PEM_read_bio_X509_REQ(mem, nullptr, nullptr, nullptr), X509_REQ_free)
238 PEM_read_bio_X509_CRL(mem, nullptr, nullptr, nullptr), X509_CRL_free)
242 static const char pem_prefix[] =
"-----BEGIN CERTIFICATE-----";
244 static constexpr size_t pem_prefix_len =
sizeof(pem_prefix) - 1;
252 std::vector<char> buf(pem_prefix_len);
253 auto read = BIO_read(mem, buf.data(), pem_prefix_len);
256 read != pem_prefix_len || std::memcmp(buf.data(), pem_prefix, read) != 0)
260 return PEM_read_bio_X509(mem,
nullptr,
nullptr,
nullptr);
269 pem ?
read_pem(mem) : d2i_X509_bio(mem, nullptr), X509_free, check_null)
303 sk_X509_new_null(), [](auto x) { sk_X509_pop_free(x, X509_free); })
312 sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free);
318 [](auto x) { sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free); },
346 auto t = ccf::ds::to_x509_time_string(s);
347 CHECK1(ASN1_TIME_set_string(*
this, t.c_str()));
348 CHECK1(ASN1_TIME_normalize(*
this));
368 EC_GROUP_new_by_curve_name(nid), EC_GROUP_free, true)
402 d2i_X509_REQ_bio(mem, nullptr), X509_REQ_free)
Definition openssl_wrappers.h:107
std::unique_ptr< T, void(*)(T *)> p
Pointer owning storage.
Definition openssl_wrappers.h:110
T * release()
Release pointer, so it's freed elsewhere (CAUTION!)
Definition openssl_wrappers.h:143
Unique_SSL_OBJECT(T *ptr, void(*dtor)(T *), bool check_null=true)
C-tor with pointer created in base class.
Definition openssl_wrappers.h:119
void reset(T *other)
Reset pointer, free old if any.
Definition openssl_wrappers.h:138
Unique_SSL_OBJECT()
C-tor with new pointer via T's c-tor.
Definition openssl_wrappers.h:114
Definition openssl_wrappers.h:27
void CHECKNULL(void *ptr)
Throws if ptr is null.
Definition openssl_wrappers.h:65
void CHECKEQUAL(int expect, int actual)
Definition openssl_wrappers.h:74
std::string error_string(unsigned long ec)
Returns the error string from an error code.
Definition openssl_wrappers.h:33
X509 * read_pem(BIO *mem)
Definition openssl_wrappers.h:250
void CHECK1(int rc)
Throws if rc is not 1.
Definition openssl_wrappers.h:54
void CHECKPOSITIVE(int val)
Definition openssl_wrappers.h:85
Definition app_interface.h:13
Definition openssl_wrappers.h:333
Unique_BIGNUM(const BIGNUM *n)
Definition openssl_wrappers.h:336
Definition openssl_wrappers.h:150
Unique_BIO(const void *buf, int len)
Definition openssl_wrappers.h:154
Unique_BIO(const Pem &pem)
Definition openssl_wrappers.h:161
Unique_BIO(std::span< const uint8_t > s)
Definition openssl_wrappers.h:157
Unique_BIO()
Definition openssl_wrappers.h:151
Unique_BIO(SSL_CTX *ctx)
Definition openssl_wrappers.h:165
Definition openssl_wrappers.h:360
Definition openssl_wrappers.h:325
Unique_ECDSA_SIG(ECDSA_SIG *ecdsa_sig)
Definition openssl_wrappers.h:327
Definition openssl_wrappers.h:365
Unique_EC_GROUP(int nid)
Definition openssl_wrappers.h:366
Definition openssl_wrappers.h:373
Unique_EC_POINT(EC_POINT *point)
Definition openssl_wrappers.h:377
Unique_EC_POINT(const EC_GROUP *group)
Definition openssl_wrappers.h:374
Definition openssl_wrappers.h:294
Definition openssl_wrappers.h:386
Definition openssl_wrappers.h:217
Unique_EVP_MD_CTX()
Definition openssl_wrappers.h:218
Definition openssl_wrappers.h:199
Unique_EVP_PKEY_CTX(const std::string &name)
Definition openssl_wrappers.h:208
Unique_EVP_PKEY_CTX(int key_type=EVP_PKEY_EC)
Definition openssl_wrappers.h:203
Unique_EVP_PKEY_CTX(EVP_PKEY *key)
Definition openssl_wrappers.h:200
Definition openssl_wrappers.h:392
Unique_EVP_PKEY(EVP_PKEY *key)
Definition openssl_wrappers.h:394
Unique_EVP_PKEY()=default
Definition openssl_wrappers.h:185
Unique_PKEY(BIO *mem)
Definition openssl_wrappers.h:187
Unique_PKEY(EVP_PKEY *pkey)
Definition openssl_wrappers.h:192
Definition openssl_wrappers.h:172
Unique_SSL_CTX(const SSL_METHOD *m)
Definition openssl_wrappers.h:173
Definition openssl_wrappers.h:179
Unique_SSL(SSL_CTX *ctx)
Definition openssl_wrappers.h:180
Definition openssl_wrappers.h:309
Unique_STACK_OF_X509_EXTENSIONS()
Definition openssl_wrappers.h:310
Unique_STACK_OF_X509_EXTENSIONS(STACK_OF(X509_EXTENSION) *exts)
Definition openssl_wrappers.h:315
Definition openssl_wrappers.h:300
Unique_STACK_OF_X509()
Definition openssl_wrappers.h:301
Definition openssl_wrappers.h:234
Unique_X509_CRL(BIO *mem)
Definition openssl_wrappers.h:236
Definition openssl_wrappers.h:399
Unique_X509_REQ_DER(BIO *mem)
Definition openssl_wrappers.h:400
Definition openssl_wrappers.h:224
Unique_X509_REQ(BIO *mem)
Definition openssl_wrappers.h:226
Definition openssl_wrappers.h:286
Definition openssl_wrappers.h:278
Definition openssl_wrappers.h:341
Unique_X509_TIME(ASN1_TIME *t)
Definition openssl_wrappers.h:350
Unique_X509_TIME(const std::string &s)
Definition openssl_wrappers.h:343
Unique_X509_TIME(const ccf::nonstd::SystemClock::time_point &t)
Definition openssl_wrappers.h:353
Definition openssl_wrappers.h:264
Unique_X509(BIO *mem, bool pem, bool check_null=false)
Definition openssl_wrappers.h:267
Unique_X509(X509 *cert, bool check_null)
Definition openssl_wrappers.h:271
std::chrono::time_point< SystemClock > time_point
Definition nonstd.h:231