Botan 3.9.0
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 return ffi_new_object(oid_obj, std::move(oid_ptr));
38 });
39}
40
41int botan_oid_register(botan_asn1_oid_t oid, const char* name) {
42 return BOTAN_FFI_VISIT(oid, [=](const auto& o) -> int {
43 if(name == nullptr) {
45 }
47 return BOTAN_FFI_SUCCESS;
48 });
49}
50
52 return BOTAN_FFI_VISIT(oid, [=](const auto& o) -> int { return invoke_view_callback(view, ctx, o.to_string()); });
53}
54
56 return BOTAN_FFI_VISIT(
57 oid, [=](const auto& o) -> int { return invoke_view_callback(view, ctx, o.to_formatted_string()); });
58}
59
61 return BOTAN_FFI_VISIT(a_w, [=](const auto& a) -> int { return a == safe_get(b_w); });
62}
63
65 return BOTAN_FFI_VISIT(a_w, [=](auto& a) {
66 if(result == nullptr) {
68 }
69 Botan::OID b = safe_get(b_w);
70 // we don't have .cmp for OID
71 if(a == b) {
72 *result = 0;
73 } else if(a < b) {
74 *result = -1;
75 } else {
76 *result = 1;
77 }
78 return BOTAN_FFI_SUCCESS;
79 });
80}
81}
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:1121
void * botan_view_ctx
Definition ffi.h:152
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:133
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
Definition ffi.h:170
int botan_oid_equal(botan_asn1_oid_t a_w, botan_asn1_oid_t b_w)
Definition ffi_oid.cpp:60
int botan_oid_register(botan_asn1_oid_t oid, const char *name)
Definition ffi_oid.cpp:41
int botan_oid_view_string(botan_asn1_oid_t oid, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_oid.cpp:51
int botan_oid_cmp(int *result, botan_asn1_oid_t a_w, botan_asn1_oid_t b_w)
Definition ffi_oid.cpp:64
int botan_oid_view_name(botan_asn1_oid_t oid, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_oid.cpp:55
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:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:187
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95