9#include <botan/internal/ffi_pkey.h>
10#include <botan/internal/ffi_util.h>
13#if defined(BOTAN_HAS_X509_CERTIFICATES)
14 #include <botan/data_src.h>
15 #include <botan/x509_crl.h>
16 #include <botan/x509cert.h>
17 #include <botan/x509path.h>
24#if defined(BOTAN_HAS_X509_CERTIFICATES)
31 if(!cert_obj || !cert_path) {
35#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
38 auto c = std::make_unique<Botan::X509_Certificate>(cert_path);
39 *cert_obj =
new botan_x509_cert_struct(std::move(c));
53#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
56 auto c = std::make_unique<Botan::X509_Certificate>(
safe_get(cert));
57 *cert_obj =
new botan_x509_cert_struct(std::move(c));
68 if(!cert_obj || !cert_bits) {
72#if defined(BOTAN_HAS_X509_CERTIFICATES)
75 auto c = std::make_unique<Botan::X509_Certificate>(bits);
76 *cert_obj =
new botan_x509_cert_struct(std::move(c));
92#if defined(BOTAN_HAS_X509_CERTIFICATES)
94 auto public_key =
safe_get(cert).subject_public_key();
95 *key =
new botan_pubkey_struct(std::move(public_key));
105 botan_x509_cert_t cert,
const char* key,
size_t index, uint8_t out[],
size_t* out_len) {
106#if defined(BOTAN_HAS_X509_CERTIFICATES)
108 [=](
const auto& c) {
return write_str_output(out, out_len, c.issuer_info(key).at(index)); });
116 botan_x509_cert_t cert,
const char* key,
size_t index, uint8_t out[],
size_t* out_len) {
117#if defined(BOTAN_HAS_X509_CERTIFICATES)
119 [=](
const auto& c) {
return write_str_output(out, out_len, c.subject_info(key).at(index)); });
131#if defined(BOTAN_HAS_X509_CERTIFICATES)
140#if defined(BOTAN_HAS_X509_CERTIFICATES)
143 if(c.allowed_usage(k))
154#if defined(BOTAN_HAS_X509_CERTIFICATES)
163#if defined(BOTAN_HAS_X509_CERTIFICATES)
165 [=](
const auto& c) {
return write_str_output(out, out_len, c.not_before().to_string()); });
173#if defined(BOTAN_HAS_X509_CERTIFICATES)
175 [=](
const auto& c) {
return write_str_output(out, out_len, c.not_after().to_string()); });
183#if defined(BOTAN_HAS_X509_CERTIFICATES)
184 return BOTAN_FFI_VISIT(cert, [=](
const auto& c) { *time_since_epoch = c.not_before().time_since_epoch(); });
192#if defined(BOTAN_HAS_X509_CERTIFICATES)
193 return BOTAN_FFI_VISIT(cert, [=](
const auto& c) { *time_since_epoch = c.not_after().time_since_epoch(); });
201#if defined(BOTAN_HAS_X509_CERTIFICATES)
210#if defined(BOTAN_HAS_X509_CERTIFICATES)
219#if defined(BOTAN_HAS_X509_CERTIFICATES)
228#if defined(BOTAN_HAS_X509_CERTIFICATES)
241#if defined(BOTAN_HAS_X509_CERTIFICATES)
251 if(hostname ==
nullptr) {
255#if defined(BOTAN_HAS_X509_CERTIFICATES)
256 return BOTAN_FFI_VISIT(cert, [=](
const auto& c) {
return c.matches_dns_name(hostname) ? 0 : -1; });
266 size_t intermediates_len,
269 const char* trusted_path,
270 size_t required_strength,
271 const char* hostname_cstr,
272 uint64_t reference_time) {
273 if(required_strength == 0) {
274 required_strength = 110;
277#if defined(BOTAN_HAS_X509_CERTIFICATES)
279 const std::string hostname((hostname_cstr ==
nullptr) ?
"" : hostname_cstr);
281 const auto validation_time = reference_time == 0
282 ? std::chrono::system_clock::now()
283 : std::chrono::system_clock::from_time_t(
static_cast<time_t
>(reference_time));
285 std::vector<Botan::X509_Certificate> end_certs;
286 end_certs.push_back(
safe_get(cert));
287 for(
size_t i = 0; i != intermediates_len; ++i) {
288 end_certs.push_back(
safe_get(intermediates[i]));
291 std::unique_ptr<Botan::Certificate_Store> trusted_from_path;
292 std::unique_ptr<Botan::Certificate_Store_In_Memory> trusted_extra;
293 std::vector<Botan::Certificate_Store*> trusted_roots;
295 if(trusted_path && *trusted_path) {
296 trusted_from_path = std::make_unique<Botan::Certificate_Store_In_Memory>(trusted_path);
297 trusted_roots.push_back(trusted_from_path.get());
300 if(trusted_len > 0) {
301 trusted_extra = std::make_unique<Botan::Certificate_Store_In_Memory>();
302 for(
size_t i = 0; i != trusted_len; ++i) {
303 trusted_extra->add_certificate(
safe_get(trusted[i]));
305 trusted_roots.push_back(trusted_extra.get());
310 auto validation_result =
314 *result_code =
static_cast<int>(validation_result.result());
317 if(validation_result.successful_validation()) {
324 BOTAN_UNUSED(result_code, cert, intermediates, intermediates_len, trusted);
325 BOTAN_UNUSED(trusted_len, trusted_path, hostname_cstr, reference_time);
335#if defined(BOTAN_HAS_X509_CERTIFICATES)
343#if defined(BOTAN_HAS_X509_CERTIFICATES)
350 if(!crl_obj || !crl_path) {
354#if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
357 auto c = std::make_unique<Botan::X509_CRL>(crl_path);
358 *crl_obj =
new botan_x509_crl_struct(std::move(c));
368 if(!crl_obj || !crl_bits) {
372#if defined(BOTAN_HAS_X509_CERTIFICATES)
375 auto c = std::make_unique<Botan::X509_CRL>(bits);
376 *crl_obj =
new botan_x509_crl_struct(std::move(c));
386#if defined(BOTAN_HAS_X509_CERTIFICATES)
395#if defined(BOTAN_HAS_X509_CERTIFICATES)
407 size_t intermediates_len,
412 const char* trusted_path,
413 size_t required_strength,
414 const char* hostname_cstr,
415 uint64_t reference_time) {
416 if(required_strength == 0) {
417 required_strength = 110;
420#if defined(BOTAN_HAS_X509_CERTIFICATES)
422 const std::string hostname((hostname_cstr ==
nullptr) ?
"" : hostname_cstr);
424 const auto validation_time = reference_time == 0
425 ? std::chrono::system_clock::now()
426 : std::chrono::system_clock::from_time_t(
static_cast<time_t
>(reference_time));
428 std::vector<Botan::X509_Certificate> end_certs;
429 end_certs.push_back(
safe_get(cert));
430 for(
size_t i = 0; i != intermediates_len; ++i) {
431 end_certs.push_back(
safe_get(intermediates[i]));
434 std::unique_ptr<Botan::Certificate_Store> trusted_from_path;
435 std::unique_ptr<Botan::Certificate_Store_In_Memory> trusted_extra;
436 std::unique_ptr<Botan::Certificate_Store_In_Memory> trusted_crls;
437 std::vector<Botan::Certificate_Store*> trusted_roots;
439 if(trusted_path && *trusted_path) {
440 trusted_from_path = std::make_unique<Botan::Certificate_Store_In_Memory>(trusted_path);
441 trusted_roots.push_back(trusted_from_path.get());
444 if(trusted_len > 0) {
445 trusted_extra = std::make_unique<Botan::Certificate_Store_In_Memory>();
446 for(
size_t i = 0; i != trusted_len; ++i) {
447 trusted_extra->add_certificate(
safe_get(trusted[i]));
449 trusted_roots.push_back(trusted_extra.get());
453 trusted_crls = std::make_unique<Botan::Certificate_Store_In_Memory>();
454 for(
size_t i = 0; i != crls_len; ++i) {
455 trusted_crls->add_crl(
safe_get(crls[i]));
457 trusted_roots.push_back(trusted_crls.get());
462 auto validation_result =
466 *result_code =
static_cast<int>(validation_result.result());
469 if(validation_result.successful_validation()) {
476 BOTAN_UNUSED(result_code, cert, intermediates, intermediates_len, trusted);
477 BOTAN_UNUSED(trusted_len, trusted_path, hostname_cstr, reference_time, crls, crls_len);
struct botan_pubkey_struct * botan_pubkey_t
struct botan_x509_crl_struct * botan_x509_crl_t
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
struct botan_x509_cert_struct * botan_x509_cert_t
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
int botan_x509_is_revoked(botan_x509_crl_t crl, botan_x509_cert_t cert)
int botan_x509_crl_destroy(botan_x509_crl_t crl)
int botan_x509_cert_destroy(botan_x509_cert_t cert)
int botan_x509_cert_load_file(botan_x509_cert_t *cert_obj, const char *cert_path)
int botan_x509_cert_dup(botan_x509_cert_t *cert_obj, botan_x509_cert_t cert)
int botan_x509_cert_verify_with_crl(int *result_code, botan_x509_cert_t cert, const botan_x509_cert_t *intermediates, size_t intermediates_len, const botan_x509_cert_t *trusted, size_t trusted_len, const botan_x509_crl_t *crls, size_t crls_len, const char *trusted_path, size_t required_strength, const char *hostname_cstr, uint64_t reference_time)
int botan_x509_cert_get_public_key(botan_x509_cert_t cert, botan_pubkey_t *key)
const char * botan_x509_cert_validation_status(int code)
int botan_x509_cert_get_authority_key_id(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
int botan_x509_cert_get_issuer_dn(botan_x509_cert_t cert, const char *key, size_t index, uint8_t out[], size_t *out_len)
int botan_x509_cert_get_time_expires(botan_x509_cert_t cert, char out[], size_t *out_len)
int botan_x509_cert_view_as_string(botan_x509_cert_t cert, botan_view_ctx ctx, botan_view_str_fn view)
int botan_x509_cert_get_time_starts(botan_x509_cert_t cert, char out[], size_t *out_len)
int botan_x509_cert_load(botan_x509_cert_t *cert_obj, const uint8_t cert_bits[], size_t cert_bits_len)
int botan_x509_crl_load(botan_x509_crl_t *crl_obj, const uint8_t crl_bits[], size_t crl_bits_len)
int botan_x509_cert_get_subject_dn(botan_x509_cert_t cert, const char *key, size_t index, uint8_t out[], size_t *out_len)
int botan_x509_cert_not_before(botan_x509_cert_t cert, uint64_t *time_since_epoch)
int botan_x509_cert_verify(int *result_code, botan_x509_cert_t cert, const botan_x509_cert_t *intermediates, size_t intermediates_len, const botan_x509_cert_t *trusted, size_t trusted_len, const char *trusted_path, size_t required_strength, const char *hostname_cstr, uint64_t reference_time)
int botan_x509_cert_hostname_match(botan_x509_cert_t cert, const char *hostname)
int botan_x509_cert_get_serial_number(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
int botan_x509_cert_get_subject_key_id(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
int botan_x509_cert_view_public_key_bits(botan_x509_cert_t cert, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_x509_crl_load_file(botan_x509_crl_t *crl_obj, const char *crl_path)
int botan_x509_cert_allowed_usage(botan_x509_cert_t cert, unsigned int key_usage)
int botan_x509_cert_not_after(botan_x509_cert_t cert, uint64_t *time_since_epoch)
int botan_x509_cert_get_public_key_bits(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
int botan_x509_cert_get_fingerprint(botan_x509_cert_t cert, const char *hash, uint8_t out[], size_t *out_len)
int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t *out_len)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
T & safe_get(botan_struct< T, M > *p)
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Path_Validation_Result x509_path_validate(const std::vector< X509_Certificate > &end_certs, const Path_Validation_Restrictions &restrictions, const std::vector< Certificate_Store * > &trusted_roots, std::string_view hostname, Usage_Type usage, std::chrono::system_clock::time_point ref_time, std::chrono::milliseconds ocsp_timeout, const std::vector< std::optional< OCSP::Response > > &ocsp_resp)
std::string to_string(ErrorType type)
Convert an ErrorType to string.