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>
42 std::string err(256,
'\0');
43 ERR_load_crypto_strings();
44 SSL_load_error_strings();
45 ERR_error_string_n(ec, err.data(), err.size());
48 err.resize(std::strlen(err.c_str()));
53 return "unknown error";
60 unsigned long ec = ERR_get_error();
61 if (rc != 1 && ec != 0)
63 throw std::runtime_error(
71 unsigned long ec = ERR_get_error();
72 if (rc == 0 && ec != 0)
74 throw std::runtime_error(
84 throw std::runtime_error(
"OpenSSL error: missing object");
93 unsigned long ec = ERR_get_error();
94 throw std::runtime_error(
104 throw std::runtime_error(
"OpenSSL error: expected positive value");
118 template <
class T, T* (*CTOR)(),
void (*DTOR)(T*)>
123 std::unique_ptr<T, void (*)(T*)>
p;
167 BIO_new_mem_buf(buf, len), [](auto x) { BIO_free(x); })
171 BIO_new_mem_buf(s.data(), s.size()), [](auto x) { BIO_free(x); })
175 BIO_new_mem_buf(pem.data(), -1), [](auto x) { BIO_free(x); })
179 BIO_new_ssl_connect(ctx), [](auto x) { BIO_free_all(x); })
201 PEM_read_bio_PUBKEY(mem, NULL, NULL, NULL), EVP_PKEY_free)
217 EVP_PKEY_CTX_new_id(key_type, NULL), EVP_PKEY_CTX_free)
222 EVP_PKEY_CTX_new_from_name(NULL, name.c_str(), NULL),
240 PEM_read_bio_X509_REQ(mem, NULL, NULL, NULL), X509_REQ_free)
250 PEM_read_bio_X509_CRL(mem, NULL, NULL, NULL), X509_CRL_free)
254 static const char pem_prefix[] =
"-----BEGIN CERTIFICATE-----\n";
256 static constexpr size_t pem_prefix_len =
sizeof(pem_prefix) - 1;
264 std::vector<char> buf(pem_prefix_len);
265 auto read = BIO_read(mem, buf.data(), pem_prefix_len);
268 read != pem_prefix_len ||
269 std::memcmp(buf.data(), pem_prefix, read) != 0)
273 return PEM_read_bio_X509(mem, NULL, NULL, NULL);
282 pem ?
read_pem(mem) : d2i_X509_bio(mem, NULL), X509_free, check_null)
316 sk_X509_new_null(), [](auto x) { sk_X509_pop_free(x, X509_free); })
325 sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free);
331 [](auto x) { sk_X509_EXTENSION_pop_free(x, X509_EXTENSION_free); },
359 auto t = ccf::ds::to_x509_time_string(s);
360 CHECK1(ASN1_TIME_set_string(*
this, t.c_str()));
361 CHECK1(ASN1_TIME_normalize(*
this));
382 EC_GROUP_new_by_curve_name(nid), EC_GROUP_free, true)
391 EC_POINT_new(group), EC_POINT_free, true)
418 d2i_X509_REQ_bio(mem, nullptr), X509_REQ_free)
Definition openssl_wrappers.h:120
std::unique_ptr< T, void(*)(T *)> p
Pointer owning storage.
Definition openssl_wrappers.h:123
T * release()
Release pointer, so it's freed elsewhere (CAUTION!)
Definition openssl_wrappers.h:154
Unique_SSL_OBJECT(T *ptr, void(*dtor)(T *), bool check_null=true)
C-tor with pointer created in base class.
Definition openssl_wrappers.h:132
void reset(T *other)
Reset pointer, free old if any.
Definition openssl_wrappers.h:149
Unique_SSL_OBJECT()
C-tor with new pointer via T's c-tor.
Definition openssl_wrappers.h:127
void CHECKNULL(void *ptr)
Throws if ptr is null.
Definition openssl_wrappers.h:80
void CHECK0(int rc)
Throws if rc is 0 and has error.
Definition openssl_wrappers.h:69
void CHECKEQUAL(int expect, int actual)
Definition openssl_wrappers.h:89
std::string error_string(unsigned long ec)
Returns the error string from an error code.
Definition openssl_wrappers.h:35
X509 * read_pem(BIO *mem)
Definition openssl_wrappers.h:262
void CHECK1(int rc)
Throws if rc is not 1 and has error.
Definition openssl_wrappers.h:58
void CHECKPOSITIVE(int val)
Definition openssl_wrappers.h:100
Definition app_interface.h:14
Definition openssl_wrappers.h:346
Unique_BIGNUM(const BIGNUM *n)
Definition openssl_wrappers.h:349
Definition openssl_wrappers.h:161
Unique_BIO(const void *buf, int len)
Definition openssl_wrappers.h:165
Unique_BIO(const Pem &pem)
Definition openssl_wrappers.h:173
Unique_BIO(std::span< const uint8_t > s)
Definition openssl_wrappers.h:169
Unique_BIO()
Definition openssl_wrappers.h:162
Unique_BIO(SSL_CTX *ctx)
Definition openssl_wrappers.h:177
Definition openssl_wrappers.h:373
Definition openssl_wrappers.h:338
Unique_ECDSA_SIG(ECDSA_SIG *ecdsa_sig)
Definition openssl_wrappers.h:340
Definition openssl_wrappers.h:379
Unique_EC_GROUP(int nid)
Definition openssl_wrappers.h:380
Definition openssl_wrappers.h:388
Unique_EC_POINT(EC_POINT *point)
Definition openssl_wrappers.h:393
Unique_EC_POINT(const EC_GROUP *group)
Definition openssl_wrappers.h:389
Definition openssl_wrappers.h:307
Definition openssl_wrappers.h:402
Definition openssl_wrappers.h:229
Unique_EVP_MD_CTX()
Definition openssl_wrappers.h:230
Definition openssl_wrappers.h:211
Unique_EVP_PKEY_CTX(const std::string &name)
Definition openssl_wrappers.h:220
Unique_EVP_PKEY_CTX(int key_type=EVP_PKEY_EC)
Definition openssl_wrappers.h:215
Unique_EVP_PKEY_CTX(EVP_PKEY *key)
Definition openssl_wrappers.h:212
Definition openssl_wrappers.h:408
Unique_EVP_PKEY(EVP_PKEY *key)
Definition openssl_wrappers.h:410
Unique_EVP_PKEY()=default
Definition openssl_wrappers.h:197
Unique_PKEY(BIO *mem)
Definition openssl_wrappers.h:199
Unique_PKEY(EVP_PKEY *pkey)
Definition openssl_wrappers.h:204
Definition openssl_wrappers.h:184
Unique_SSL_CTX(const SSL_METHOD *m)
Definition openssl_wrappers.h:185
Definition openssl_wrappers.h:191
Unique_SSL(SSL_CTX *ctx)
Definition openssl_wrappers.h:192
Definition openssl_wrappers.h:322
Unique_STACK_OF_X509_EXTENSIONS()
Definition openssl_wrappers.h:323
Unique_STACK_OF_X509_EXTENSIONS(STACK_OF(X509_EXTENSION) *exts)
Definition openssl_wrappers.h:328
Definition openssl_wrappers.h:313
Unique_STACK_OF_X509()
Definition openssl_wrappers.h:314
Definition openssl_wrappers.h:246
Unique_X509_CRL(BIO *mem)
Definition openssl_wrappers.h:248
Definition openssl_wrappers.h:415
Unique_X509_REQ_DER(BIO *mem)
Definition openssl_wrappers.h:416
Definition openssl_wrappers.h:236
Unique_X509_REQ(BIO *mem)
Definition openssl_wrappers.h:238
Definition openssl_wrappers.h:299
Definition openssl_wrappers.h:291
Definition openssl_wrappers.h:354
Unique_X509_TIME(ASN1_TIME *t)
Definition openssl_wrappers.h:363
Unique_X509_TIME(const std::string &s)
Definition openssl_wrappers.h:356
Unique_X509_TIME(const std::chrono::system_clock::time_point &t)
Definition openssl_wrappers.h:366
Definition openssl_wrappers.h:277
Unique_X509(BIO *mem, bool pem, bool check_null=false)
Definition openssl_wrappers.h:280
Unique_X509(X509 *cert, bool check_null)
Definition openssl_wrappers.h:284