-
Notifications
You must be signed in to change notification settings - Fork 623
Add CRL creation to FFI #5166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add CRL creation to FFI #5166
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,10 @@ | |
|
|
||
| #include <botan/ffi.h> | ||
|
|
||
| #include <botan/internal/ffi_cert.h> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be making FFI hard-depend on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This I don't quite understand, all the methods, including |
||
| #include <botan/internal/ffi_mp.h> | ||
| #include <botan/internal/ffi_pkey.h> | ||
| #include <botan/internal/ffi_rng.h> | ||
| #include <botan/internal/ffi_util.h> | ||
| #include <memory> | ||
|
|
||
|
|
@@ -19,16 +22,24 @@ | |
| #include <botan/internal/ffi_oid.h> | ||
| #endif | ||
|
|
||
| extern "C" { | ||
| namespace { | ||
| std::chrono::system_clock::time_point timepoint_from_timestamp(uint64_t time_since_epoch) { | ||
| return std::chrono::system_clock::time_point(std::chrono::seconds(time_since_epoch)); | ||
| } | ||
|
|
||
| using namespace Botan_FFI; | ||
| std::string default_from_ptr(const char* value) { | ||
| std::string ret; | ||
| if(value != nullptr) { | ||
| ret = value; | ||
| } | ||
| return ret; | ||
| } | ||
arckoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| } // namespace | ||
|
|
||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_cert_struct, Botan::X509_Certificate, 0x8F628937); | ||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_general_name_struct, Botan::GeneralName, 0x563654FD); | ||
| extern "C" { | ||
|
|
||
| #endif | ||
| using namespace Botan_FFI; | ||
|
|
||
| int botan_x509_cert_load_file(botan_x509_cert_t* cert_obj, const char* cert_path) { | ||
| if(cert_obj == nullptr || cert_path == nullptr) { | ||
|
|
@@ -782,13 +793,6 @@ const char* botan_x509_cert_validation_status(int code) { | |
| #endif | ||
| } | ||
|
|
||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
|
|
||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_crl_struct, Botan::X509_CRL, 0x2C628910); | ||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_crl_entry_struct, Botan::CRL_Entry, 0x4EAA5346); | ||
|
|
||
| #endif | ||
|
|
||
| int botan_x509_crl_load_file(botan_x509_crl_t* crl_obj, const char* crl_path) { | ||
| if(crl_obj == nullptr || crl_path == nullptr) { | ||
| return BOTAN_FFI_ERROR_NULL_POINTER; | ||
|
|
@@ -859,6 +863,107 @@ int botan_x509_crl_next_update(botan_x509_crl_t crl, uint64_t* time_since_epoch) | |
| #endif | ||
| } | ||
|
|
||
| int botan_x509_crl_create(botan_x509_crl_t* crl_obj, | ||
| botan_rng_t rng, | ||
| botan_x509_cert_t ca_cert, | ||
| botan_privkey_t ca_key, | ||
| uint64_t issue_time, | ||
| uint32_t next_update, | ||
| const char* hash_fn, | ||
| const char* padding) { | ||
| if(Botan::any_null_pointers(crl_obj)) { | ||
| return BOTAN_FFI_ERROR_NULL_POINTER; | ||
| } | ||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| return ffi_guard_thunk(__func__, [=]() -> int { | ||
| auto& rng_ = safe_get(rng); | ||
| auto ca = Botan::X509_CA( | ||
| safe_get(ca_cert), safe_get(ca_key), default_from_ptr(hash_fn), default_from_ptr(padding), rng_); | ||
| auto crl = std::make_unique<Botan::X509_CRL>( | ||
| ca.new_crl(rng_, timepoint_from_timestamp(issue_time), std::chrono::seconds(next_update))); | ||
| return ffi_new_object(crl_obj, std::move(crl)); | ||
| }); | ||
| #else | ||
| BOTAN_UNUSED(rng, ca_cert, ca_key, hash_fn, padding, issue_time, next_update); | ||
| return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | ||
| #endif | ||
| } | ||
|
|
||
| int botan_x509_crl_entry_create(botan_x509_crl_entry_t* entry, botan_x509_cert_t cert, int reason_code) { | ||
| if(Botan::any_null_pointers(entry)) { | ||
| return BOTAN_FFI_ERROR_NULL_POINTER; | ||
| } | ||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| return ffi_guard_thunk(__func__, [=]() -> int { | ||
| return ffi_new_object( | ||
| entry, std::make_unique<Botan::CRL_Entry>(safe_get(cert), static_cast<Botan::CRL_Code>(reason_code))); | ||
| }); | ||
| #else | ||
| BOTAN_UNUSED(cert, reason_code); | ||
| return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | ||
| #endif | ||
| } | ||
|
|
||
| int botan_x509_crl_update(botan_x509_crl_t* crl_obj, | ||
| botan_x509_crl_t last_crl, | ||
| botan_rng_t rng, | ||
| botan_x509_cert_t ca_cert, | ||
| botan_privkey_t ca_key, | ||
| uint64_t issue_time, | ||
| uint32_t next_update, | ||
| const botan_x509_crl_entry_t* new_entries, | ||
| size_t new_entries_len, | ||
| const char* hash_fn, | ||
| const char* padding) { | ||
| if(Botan::any_null_pointers(crl_obj)) { | ||
| return BOTAN_FFI_ERROR_NULL_POINTER; | ||
| } | ||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| return ffi_guard_thunk(__func__, [=]() -> int { | ||
| if(new_entries_len == 0) { | ||
| return BOTAN_FFI_ERROR_BAD_PARAMETER; | ||
| } | ||
arckoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| auto& rng_ = safe_get(rng); | ||
| auto ca = Botan::X509_CA( | ||
| safe_get(ca_cert), safe_get(ca_key), default_from_ptr(hash_fn), default_from_ptr(padding), rng_); | ||
|
|
||
| std::vector<Botan::CRL_Entry> entries; | ||
| entries.reserve(new_entries_len); | ||
| for(size_t i = 0; i < new_entries_len; i++) { | ||
| entries.push_back(safe_get(new_entries[i])); | ||
| } | ||
|
|
||
| auto crl = std::make_unique<Botan::X509_CRL>(ca.update_crl( | ||
| safe_get(last_crl), entries, rng_, timepoint_from_timestamp(issue_time), std::chrono::seconds(next_update))); | ||
| return ffi_new_object(crl_obj, std::move(crl)); | ||
| }); | ||
| #else | ||
| BOTAN_UNUSED( | ||
| last_crl, rng, ca_cert, ca_key, hash_fn, padding, issue_time, next_update, new_entries, new_entries_len); | ||
| return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | ||
| #endif | ||
| } | ||
|
|
||
| int botan_x509_crl_verify_signature(botan_x509_crl_t crl, botan_pubkey_t key, int* result) { | ||
| if(Botan::any_null_pointers(result)) { | ||
| return BOTAN_FFI_ERROR_NULL_POINTER; | ||
| } | ||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| return ffi_guard_thunk(__func__, [=]() -> int { | ||
| const bool ok = safe_get(crl).check_signature(safe_get(key)); | ||
| if(ok) { | ||
| *result = 1; | ||
| } else { | ||
| *result = 0; | ||
| } | ||
| return BOTAN_FFI_SUCCESS; | ||
| }); | ||
| #else | ||
| BOTAN_UNUSED(crl, key); | ||
| return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | ||
| #endif | ||
| } | ||
|
|
||
| int botan_x509_crl_destroy(botan_x509_crl_t crl) { | ||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| return BOTAN_FFI_CHECKED_DELETE(crl); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * (C) 2026 Jack Lloyd | ||
| * | ||
| * Botan is released under the Simplified BSD License (see license.txt) | ||
| */ | ||
|
|
||
| #ifndef BOTAN_FFI_CERT_H_ | ||
| #define BOTAN_FFI_CERT_H_ | ||
|
|
||
| #include <botan/internal/ffi_util.h> | ||
|
|
||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
| #include <botan/data_src.h> | ||
| #include <botan/x509_ca.h> | ||
| #include <botan/x509_crl.h> | ||
| #include <botan/x509cert.h> | ||
| #include <botan/x509path.h> | ||
| #endif | ||
|
|
||
| extern "C" { | ||
| #if defined(BOTAN_HAS_X509_CERTIFICATES) | ||
|
|
||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_cert_struct, Botan::X509_Certificate, 0x8F628937); | ||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_crl_struct, Botan::X509_CRL, 0x2C628910); | ||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_crl_entry_struct, Botan::CRL_Entry, 0x4EAA5346); | ||
| BOTAN_FFI_DECLARE_STRUCT(botan_x509_general_name_struct, Botan::GeneralName, 0x563654FD); | ||
|
|
||
| #endif | ||
| } | ||
|
|
||
| #endif |
Uh oh!
There was an error while loading. Please reload this page.