2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-10-25 18:35:23 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_OPENSSL_H
|
|
|
|
|
2017-12-21 21:30:28 +01:00
|
|
|
// Forward declarations for OpenSSL because it is not included here. Note
|
|
|
|
// that these are declared in the extern namespace outside of ircd:: but
|
|
|
|
// match those in the OpenSSL headers and should not be too much trouble.
|
2017-11-16 02:27:36 +01:00
|
|
|
struct ssl_st;
|
2019-03-05 19:50:12 +01:00
|
|
|
struct ssl_ctx_st;
|
|
|
|
struct ssl_cipher_st;
|
2017-12-21 21:30:28 +01:00
|
|
|
struct rsa_st;
|
2017-11-16 02:27:36 +01:00
|
|
|
struct x509_st;
|
2018-01-05 07:00:10 +01:00
|
|
|
struct x509_store_ctx_st;
|
2017-12-21 21:30:28 +01:00
|
|
|
struct bignum_st;
|
|
|
|
struct bignum_ctx;
|
|
|
|
struct bio_st;
|
|
|
|
struct evp_pkey_st;
|
2018-01-12 07:00:41 +01:00
|
|
|
struct ec_group_st;
|
|
|
|
struct ec_point_st;
|
|
|
|
struct ec_key_st;
|
2018-08-28 21:54:13 +02:00
|
|
|
struct dh_st;
|
2017-11-16 02:27:36 +01:00
|
|
|
|
|
|
|
/// OpenSSL library interface. Provides things we need to expose from OpenSSL
|
2018-01-27 03:13:40 +01:00
|
|
|
/// to the rest of the project.
|
2017-10-25 18:35:23 +02:00
|
|
|
namespace ircd::openssl
|
|
|
|
{
|
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
2017-12-21 21:30:28 +01:00
|
|
|
IRCD_EXCEPTION(error, buffer_error)
|
2017-10-25 18:35:23 +02:00
|
|
|
|
|
|
|
struct init;
|
2017-12-21 21:30:28 +01:00
|
|
|
struct bignum;
|
2017-10-25 18:35:23 +02:00
|
|
|
|
2018-01-27 03:13:40 +01:00
|
|
|
// typedef analogues
|
2017-11-16 02:27:36 +01:00
|
|
|
using SSL = ::ssl_st;
|
2019-03-05 19:50:12 +01:00
|
|
|
using SSL_CTX = ::ssl_ctx_st;
|
|
|
|
using SSL_CIPHER = ::ssl_cipher_st;
|
2017-12-21 21:30:28 +01:00
|
|
|
using RSA = ::rsa_st;
|
2017-11-16 02:27:36 +01:00
|
|
|
using X509 = ::x509_st;
|
2018-01-05 07:00:10 +01:00
|
|
|
using X509_STORE_CTX = ::x509_store_ctx_st;
|
2017-12-21 21:30:28 +01:00
|
|
|
using BIGNUM = ::bignum_st;
|
|
|
|
using BN_CTX = ::bignum_ctx;
|
|
|
|
using EVP_PKEY = ::evp_pkey_st;
|
|
|
|
using BIO = ::bio_st;
|
2018-01-12 07:00:41 +01:00
|
|
|
using EC_GROUP = ::ec_group_st;
|
|
|
|
using EC_POINT = ::ec_point_st;
|
|
|
|
using EC_KEY = ::ec_key_st;
|
2018-08-28 21:54:13 +02:00
|
|
|
using DH = ::dh_st;
|
2017-11-16 02:27:36 +01:00
|
|
|
|
|
|
|
// Observers
|
2017-10-25 18:35:23 +02:00
|
|
|
string_view error_string(const mutable_buffer &buf, const ulong &);
|
|
|
|
ulong peek_error();
|
|
|
|
|
|
|
|
// Using these will clobber other libraries (like boost); so don't
|
|
|
|
ulong get_error();
|
|
|
|
void clear_error();
|
2017-11-16 02:27:36 +01:00
|
|
|
|
2017-12-21 21:30:28 +01:00
|
|
|
// Envelope suite
|
|
|
|
EVP_PKEY &read_pem_pub(EVP_PKEY &out, const string_view &pem);
|
|
|
|
EVP_PKEY &read_pem_priv(EVP_PKEY &out, const string_view &pem);
|
|
|
|
string_view write_pem_pub(const mutable_buffer &out, const EVP_PKEY &);
|
|
|
|
string_view write_pem_priv(const mutable_buffer &out, const EVP_PKEY &);
|
2018-01-12 07:00:41 +01:00
|
|
|
void set(EVP_PKEY &out, RSA &in);
|
|
|
|
void set(EVP_PKEY &out, EC_KEY &in);
|
2017-11-16 02:27:36 +01:00
|
|
|
|
2017-12-21 21:30:28 +01:00
|
|
|
// RSA suite
|
|
|
|
void check(const RSA &);
|
|
|
|
bool check(const RSA &, std::nothrow_t);
|
|
|
|
size_t size(const RSA &); // RSA_size() / mod size in bytes
|
|
|
|
string_view print(const mutable_buffer &buf, const RSA &, const off_t &offset = 0);
|
|
|
|
RSA &genrsa(RSA &out, const uint &bits = 2048, const uint &e = 0x10001);
|
|
|
|
void genrsa(const string_view &skfile, const string_view &pkfile, const json::object &opts = {});
|
2018-01-12 07:00:41 +01:00
|
|
|
|
|
|
|
// EC suite
|
|
|
|
extern const EC_GROUP *secp256k1;
|
|
|
|
void check(const EC_KEY &);
|
|
|
|
bool check(const EC_KEY &, const std::nothrow_t);
|
|
|
|
string_view print(const mutable_buffer &buf, const EC_KEY &, const off_t &offset = 0);
|
|
|
|
void genec(const string_view &skfile, const string_view &pkfile, const EC_GROUP *const & = secp256k1);
|
2017-11-16 02:27:36 +01:00
|
|
|
|
2018-08-28 21:54:13 +02:00
|
|
|
// DH suite
|
|
|
|
extern const size_t DH_DEFAULT_GEN;
|
|
|
|
extern const size_t DH_DEFAULT_BITS;
|
2018-08-30 01:16:35 +02:00
|
|
|
extern const string_view rfc3526_dh_params_pem;
|
2018-08-28 21:54:13 +02:00
|
|
|
DH &gendh(DH &, const uint &bits = DH_DEFAULT_BITS, const uint &gen = DH_DEFAULT_GEN);
|
|
|
|
string_view gendh(const mutable_buffer &, const uint &bits = DH_DEFAULT_BITS, const uint &gen = DH_DEFAULT_GEN);
|
|
|
|
void gendh(const string_view &dhfile, const uint &bits = DH_DEFAULT_BITS, const uint &gen = DH_DEFAULT_GEN);
|
|
|
|
|
2017-12-21 21:30:28 +01:00
|
|
|
// X.509 suite
|
2018-02-03 08:20:26 +01:00
|
|
|
const_buffer i2d(const mutable_buffer &out, const X509 &);
|
|
|
|
const_buffer cert2d(const mutable_buffer &out, const string_view &pem);
|
2017-12-21 21:30:28 +01:00
|
|
|
X509 &read_pem(X509 &out, const string_view &pem);
|
|
|
|
string_view write_pem(const mutable_buffer &out, const X509 &);
|
2018-01-12 03:37:55 +01:00
|
|
|
string_view print(const mutable_buffer &buf, const X509 &, ulong flags = -1);
|
2018-01-12 04:24:05 +01:00
|
|
|
string_view printX509(const mutable_buffer &buf, const string_view &pem, ulong flags = -1);
|
2018-01-12 07:00:41 +01:00
|
|
|
string_view genX509(const mutable_buffer &out, EVP_PKEY &, const json::object &opts);
|
|
|
|
string_view genX509_rsa(const mutable_buffer &out, const json::object &opts);
|
|
|
|
string_view genX509_ec(const mutable_buffer &out, const json::object &opts);
|
2018-01-13 00:31:58 +01:00
|
|
|
string_view print_subject(const mutable_buffer &buf, const X509 &, ulong flags = -1);
|
2018-01-27 03:35:08 +01:00
|
|
|
string_view print_subject(const mutable_buffer &buf, const string_view &pem, ulong flags = -1);
|
2018-01-12 04:24:05 +01:00
|
|
|
string_view subject_common_name(const mutable_buffer &out, const X509 &);
|
2018-03-12 19:12:38 +01:00
|
|
|
time_t not_before(const X509 &);
|
|
|
|
time_t not_after(const X509 &);
|
2018-03-12 18:40:06 +01:00
|
|
|
std::string stringify(const X509 &);
|
2018-01-05 06:59:39 +01:00
|
|
|
const X509 &peer_cert(const SSL &);
|
|
|
|
X509 &peer_cert(SSL &);
|
2019-09-30 22:11:52 +02:00
|
|
|
const X509 &cert(const SSL_CTX &);
|
|
|
|
X509 &cert(SSL_CTX &);
|
2018-01-05 07:00:10 +01:00
|
|
|
|
|
|
|
int get_error(const X509_STORE_CTX &);
|
|
|
|
const char *cert_error_string(const long &);
|
|
|
|
const char *get_error_string(const X509_STORE_CTX &);
|
|
|
|
uint get_error_depth(const X509_STORE_CTX &);
|
|
|
|
const X509 ¤t_cert(const X509_STORE_CTX &);
|
|
|
|
X509 ¤t_cert(X509_STORE_CTX &);
|
2019-03-05 19:50:12 +01:00
|
|
|
|
2019-03-13 19:50:01 +01:00
|
|
|
// Cipher suite
|
2019-03-05 19:50:12 +01:00
|
|
|
string_view name(const SSL_CIPHER &);
|
|
|
|
const SSL_CIPHER *current_cipher(const SSL &);
|
|
|
|
string_view shared_ciphers(const mutable_buffer &buf, const SSL &);
|
2019-03-13 01:47:27 +01:00
|
|
|
string_view cipher_list(const SSL &, const int &priority);
|
|
|
|
std::string cipher_list(const SSL_CTX &, const int &priority = 0);
|
2019-03-11 00:22:12 +01:00
|
|
|
void set_cipher_list(SSL &, const std::string &list);
|
|
|
|
void set_cipher_list(SSL_CTX &, const std::string &list);
|
|
|
|
void set_ecdh_auto(SSL_CTX &, const bool & = true);
|
|
|
|
void set_ecdh_auto(SSL &, const bool & = true);
|
|
|
|
void set_tmp_ecdh(SSL_CTX &, EC_KEY &);
|
|
|
|
void set_curves(SSL_CTX &, std::string list);
|
|
|
|
void set_curves(SSL &, std::string list);
|
2019-03-13 19:50:01 +01:00
|
|
|
|
|
|
|
// SNI suite
|
|
|
|
string_view server_name(const SSL &); // provided by client
|
2019-03-13 21:32:56 +01:00
|
|
|
void server_name(SSL &, const string_view &); // set by client
|
2019-06-01 01:06:55 +02:00
|
|
|
|
|
|
|
// Header version; library version
|
|
|
|
extern const info::versions version_api, version_abi;
|
2019-06-04 23:01:33 +02:00
|
|
|
extern const info::versions libressl_version_api;
|
2017-10-25 18:35:23 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 21:30:28 +01:00
|
|
|
/// OpenSSL BIO convenience utils and wraps; also secure file IO closures
|
|
|
|
namespace ircd::openssl::bio
|
|
|
|
{
|
|
|
|
// Presents a memory BIO closure hiding boilerplate
|
|
|
|
using closure = std::function<void (BIO *const &)>;
|
|
|
|
string_view write(const mutable_buffer &, const closure &);
|
|
|
|
void read(const const_buffer &, const closure &);
|
|
|
|
|
|
|
|
// Presents a secure buffer file IO closure for writing to path
|
|
|
|
using mb_closure = std::function<string_view (const mutable_buffer &)>;
|
|
|
|
void write_file(const string_view &path, const mb_closure &closure, const size_t &bufsz = 64_KiB);
|
|
|
|
|
|
|
|
// Presents a secure buffer file IO closure with data read from path
|
|
|
|
using cb_closure = std::function<void (const string_view &)>;
|
|
|
|
void read_file(const string_view &path, const cb_closure &closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenSSL BIGNUM convenience utils and wraps.
|
|
|
|
namespace ircd::openssl
|
|
|
|
{
|
|
|
|
size_t size(const BIGNUM *const &); // bytes binary
|
2018-02-03 08:20:26 +01:00
|
|
|
mutable_buffer data(const mutable_buffer &out, const BIGNUM *const &); // le
|
2017-12-21 21:30:28 +01:00
|
|
|
string_view u2a(const mutable_buffer &out, const BIGNUM *const &);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Light semantic-complete wrapper for BIGNUM.
|
|
|
|
class ircd::openssl::bignum
|
|
|
|
{
|
|
|
|
BIGNUM *a;
|
|
|
|
|
|
|
|
public:
|
|
|
|
const BIGNUM *get() const;
|
|
|
|
BIGNUM *get();
|
|
|
|
BIGNUM *release();
|
|
|
|
|
|
|
|
size_t bits() const;
|
|
|
|
size_t bytes() const;
|
|
|
|
|
|
|
|
explicit operator uint128_t() const;
|
|
|
|
operator const BIGNUM *() const;
|
|
|
|
operator const BIGNUM &() const;
|
|
|
|
operator BIGNUM *const &();
|
|
|
|
operator BIGNUM **();
|
|
|
|
operator BIGNUM &();
|
|
|
|
|
|
|
|
// default constructor does not BN_new()
|
|
|
|
bignum()
|
|
|
|
:a{nullptr}
|
|
|
|
{}
|
|
|
|
|
|
|
|
// acquisitional constructor for OpenSSL API return values
|
|
|
|
explicit bignum(BIGNUM *const &a)
|
|
|
|
:a{a}
|
|
|
|
{}
|
|
|
|
|
|
|
|
explicit bignum(const uint128_t &val);
|
2018-02-03 08:20:26 +01:00
|
|
|
bignum(const const_buffer &bin); // le
|
2017-12-21 21:30:28 +01:00
|
|
|
explicit bignum(const BIGNUM &a);
|
|
|
|
bignum(const bignum &);
|
|
|
|
bignum(bignum &&) noexcept;
|
|
|
|
bignum &operator=(const bignum &);
|
|
|
|
bignum &operator=(bignum &&) noexcept;
|
|
|
|
~bignum() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-10-25 18:35:23 +02:00
|
|
|
struct ircd::openssl::init
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
~init() noexcept;
|
|
|
|
};
|