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";
56 unsigned long ec = ERR_get_error();
57 if (rc != 1 && ec != 0)
59 throw std::runtime_error(
67 unsigned long ec = ERR_get_error();
68 if (rc == 0 && ec != 0)
70 throw std::runtime_error(
80 throw std::runtime_error(
"OpenSSL error: missing object");
89 unsigned long ec = ERR_get_error();
90 throw std::runtime_error(
100 throw std::runtime_error(
"OpenSSL error: expected positive value");
114 template <
class T, T* (*CTOR)(),
void (*DTOR)(T*)>
119 std::unique_ptr<T, void (*)(T*)>
p;
168 BIO_new_mem_buf(s.data(), s.size()), [](auto x) { BIO_free(x); })
172 BIO_new_mem_buf(pem.data(), -1), [](auto x) { BIO_free(x); })
176 BIO_new_ssl_connect(ctx), [](auto x) { BIO_free_all(x); })
198 PEM_read_bio_PUBKEY(mem, nullptr, nullptr, nullptr), EVP_PKEY_free)
214 EVP_PKEY_CTX_new_id(key_type, nullptr), EVP_PKEY_CTX_free)
219 EVP_PKEY_CTX_new_from_name(nullptr, name.c_str(), nullptr),
237 PEM_read_bio_X509_REQ(mem, nullptr, nullptr, nullptr), X509_REQ_free)
247 PEM_read_bio_X509_CRL(mem, nullptr, nullptr, nullptr), X509_CRL_free)
251 static const char pem_prefix[] =
"-----BEGIN CERTIFICATE-----";
253 static constexpr size_t pem_prefix_len =
sizeof(pem_prefix) - 1;
261 std::vector<char> buf(pem_prefix_len);
262 auto read = BIO_read(mem, buf.data(), pem_prefix_len);
265 read != pem_prefix_len || std::memcmp(buf.data(), pem_prefix, read) != 0)
269 return PEM_read_bio_X509(mem,
nullptr,
nullptr,
nullptr);
278 pem ?
read_pem(mem) : d2i_X509_bio(mem, nullptr), X509_free, check_null)
312 sk_X509_new_null(), [](auto x) { sk_X509_pop_free(x, X509_free); })
321 sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free);
327 [](auto x) { sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free); },
355 auto t = ccf::ds::to_x509_time_string(s);
356 CHECK1(ASN1_TIME_set_string(*
this, t.c_str()));
357 CHECK1(ASN1_TIME_normalize(*
this));
377 EC_GROUP_new_by_curve_name(nid), EC_GROUP_free, true)
411 d2i_X509_REQ_bio(mem, nullptr), X509_REQ_free)
Definition openssl_wrappers.h:116
std::unique_ptr< T, void(*)(T *)> p
Pointer owning storage.
Definition openssl_wrappers.h:119
T * release()
Release pointer, so it's freed elsewhere (CAUTION!)
Definition openssl_wrappers.h:152
Unique_SSL_OBJECT(T *ptr, void(*dtor)(T *), bool check_null=true)
C-tor with pointer created in base class.
Definition openssl_wrappers.h:128
void reset(T *other)
Reset pointer, free old if any.
Definition openssl_wrappers.h:147
Unique_SSL_OBJECT()
C-tor with new pointer via T's c-tor.
Definition openssl_wrappers.h:123
Definition openssl_wrappers.h:27
void CHECKNULL(void *ptr)
Throws if ptr is null.
Definition openssl_wrappers.h:76
void CHECK0(int rc)
Throws if rc is 0 and has error.
Definition openssl_wrappers.h:65
void CHECKEQUAL(int expect, int actual)
Definition openssl_wrappers.h:85
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:259
void CHECK1(int rc)
Throws if rc is not 1 and has error.
Definition openssl_wrappers.h:54
void CHECKPOSITIVE(int val)
Definition openssl_wrappers.h:96
Definition app_interface.h:14
Definition openssl_wrappers.h:342
Unique_BIGNUM(const BIGNUM *n)
Definition openssl_wrappers.h:345
Definition openssl_wrappers.h:159
Unique_BIO(const void *buf, int len)
Definition openssl_wrappers.h:163
Unique_BIO(const Pem &pem)
Definition openssl_wrappers.h:170
Unique_BIO(std::span< const uint8_t > s)
Definition openssl_wrappers.h:166
Unique_BIO()
Definition openssl_wrappers.h:160
Unique_BIO(SSL_CTX *ctx)
Definition openssl_wrappers.h:174
Definition openssl_wrappers.h:369
Definition openssl_wrappers.h:334
Unique_ECDSA_SIG(ECDSA_SIG *ecdsa_sig)
Definition openssl_wrappers.h:336
Definition openssl_wrappers.h:374
Unique_EC_GROUP(int nid)
Definition openssl_wrappers.h:375
Definition openssl_wrappers.h:382
Unique_EC_POINT(EC_POINT *point)
Definition openssl_wrappers.h:386
Unique_EC_POINT(const EC_GROUP *group)
Definition openssl_wrappers.h:383
Definition openssl_wrappers.h:303
Definition openssl_wrappers.h:395
Definition openssl_wrappers.h:226
Unique_EVP_MD_CTX()
Definition openssl_wrappers.h:227
Definition openssl_wrappers.h:208
Unique_EVP_PKEY_CTX(const std::string &name)
Definition openssl_wrappers.h:217
Unique_EVP_PKEY_CTX(int key_type=EVP_PKEY_EC)
Definition openssl_wrappers.h:212
Unique_EVP_PKEY_CTX(EVP_PKEY *key)
Definition openssl_wrappers.h:209
Definition openssl_wrappers.h:401
Unique_EVP_PKEY(EVP_PKEY *key)
Definition openssl_wrappers.h:403
Unique_EVP_PKEY()=default
Definition openssl_wrappers.h:194
Unique_PKEY(BIO *mem)
Definition openssl_wrappers.h:196
Unique_PKEY(EVP_PKEY *pkey)
Definition openssl_wrappers.h:201
Definition openssl_wrappers.h:181
Unique_SSL_CTX(const SSL_METHOD *m)
Definition openssl_wrappers.h:182
Definition openssl_wrappers.h:188
Unique_SSL(SSL_CTX *ctx)
Definition openssl_wrappers.h:189
Definition openssl_wrappers.h:318
Unique_STACK_OF_X509_EXTENSIONS()
Definition openssl_wrappers.h:319
Unique_STACK_OF_X509_EXTENSIONS(STACK_OF(X509_EXTENSION) *exts)
Definition openssl_wrappers.h:324
Definition openssl_wrappers.h:309
Unique_STACK_OF_X509()
Definition openssl_wrappers.h:310
Definition openssl_wrappers.h:243
Unique_X509_CRL(BIO *mem)
Definition openssl_wrappers.h:245
Definition openssl_wrappers.h:408
Unique_X509_REQ_DER(BIO *mem)
Definition openssl_wrappers.h:409
Definition openssl_wrappers.h:233
Unique_X509_REQ(BIO *mem)
Definition openssl_wrappers.h:235
Definition openssl_wrappers.h:295
Definition openssl_wrappers.h:287
Definition openssl_wrappers.h:350
Unique_X509_TIME(ASN1_TIME *t)
Definition openssl_wrappers.h:359
Unique_X509_TIME(const std::string &s)
Definition openssl_wrappers.h:352
Unique_X509_TIME(const std::chrono::system_clock::time_point &t)
Definition openssl_wrappers.h:362
Definition openssl_wrappers.h:273
Unique_X509(BIO *mem, bool pem, bool check_null=false)
Definition openssl_wrappers.h:276
Unique_X509(X509 *cert, bool check_null)
Definition openssl_wrappers.h:280