Botan 3.8.1
Crypto and TLS for C&
ffi_oid.cpp
Go to the documentation of this file.
1/*
2* (C) 2025 Jack Lloyd
3* (C) 2025 Dominik Schricker
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/ffi.h>
9
10#include <botan/pk_keys.h>
11#include <botan/internal/ffi_oid.h>
12#include <botan/internal/ffi_pkey.h>
13#include <botan/internal/ffi_util.h>
14
15extern "C" {
16
17using namespace Botan_FFI;
18
22
23int botan_oid_from_string(botan_asn1_oid_t* oid_obj, const char* oid_str) {
24 return ffi_guard_thunk(__func__, [=]() -> int {
25 if(oid_obj == nullptr || oid_str == nullptr) {
27 }
28 Botan::OID oid;
29 // This returns a Lookup_Error if an unknown name is passed,
30 // which would get turned into NOT_IMPLEMENTED
31 try {
32 oid = Botan::OID::from_string(oid_str);
33 } catch(Botan::Lookup_Error&) {
35 }
36 auto oid_ptr = std::make_unique<Botan::OID>(std::move(oid));
37 *oid_obj = new botan_asn1_oid_struct(std::move(oid_ptr));
38 return BOTAN_FFI_SUCCESS;
39 });
40}
41
42int botan_oid_register(botan_asn1_oid_t oid, const char* name) {
43 return BOTAN_FFI_VISIT(oid, [=](const auto& o) -> int {
44 if(name == nullptr) {
46 }
48 return BOTAN_FFI_SUCCESS;
49 });
50}
51
53 return BOTAN_FFI_VISIT(oid, [=](const auto& o) -> int { return invoke_view_callback(view, ctx, o.to_string()); });
54}
55
57 return BOTAN_FFI_VISIT(
58 oid, [=](const auto& o) -> int { return invoke_view_callback(view, ctx, o.to_formatted_string()); });
59}
60
62 return BOTAN_FFI_VISIT(a_w, [=](const auto& a) -> int { return a == safe_get(b_w); });
63}
64
66 return BOTAN_FFI_VISIT(a_w, [=](auto& a) {
67 if(result == nullptr) {
69 }
70 Botan::OID b = safe_get(b_w);
71 // we don't have .cmp for OID
72 if(a == b) {
73 *result = 0;
74 } else if(a < b) {
75 *result = -1;
76 } else {
77 *result = 1;
78 }
79 return BOTAN_FFI_SUCCESS;
80 });
81}
82}
static void register_oid(const OID &oid, std::string_view name)
Definition asn1_oid.cpp:67
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:86
struct botan_asn1_oid_struct * botan_asn1_oid_t
Definition ffi.h:1117
void * botan_view_ctx
Definition ffi.h:150
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:130
@ BOTAN_FFI_SUCCESS
Definition ffi.h:113
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:131
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
Definition ffi.h:168
int botan_oid_equal(botan_asn1_oid_t a_w, botan_asn1_oid_t b_w)
Definition ffi_oid.cpp:61
int botan_oid_register(botan_asn1_oid_t oid, const char *name)
Definition ffi_oid.cpp:42
int botan_oid_view_string(botan_asn1_oid_t oid, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_oid.cpp:52
int botan_oid_cmp(int *result, botan_asn1_oid_t a_w, botan_asn1_oid_t b_w)
Definition ffi_oid.cpp:65
int botan_oid_view_name(botan_asn1_oid_t oid, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_oid.cpp:56
int botan_oid_from_string(botan_asn1_oid_t *oid_obj, const char *oid_str)
Definition ffi_oid.cpp:23
int botan_oid_destroy(botan_asn1_oid_t oid)
Definition ffi_oid.cpp:19
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:145
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:164
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:166
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:67
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:83