Botan  2.6.0
Crypto and TLS for C++11
ffi_cert.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/ffi.h>
8 #include <botan/internal/ffi_util.h>
9 #include <botan/internal/ffi_pkey.h>
10 #include <botan/x509cert.h>
11 #include <botan/data_src.h>
12 
13 extern "C" {
14 
15 using namespace Botan_FFI;
16 
17 BOTAN_FFI_DECLARE_STRUCT(botan_x509_cert_struct, Botan::X509_Certificate, 0x8F628937);
18 
19 int botan_x509_cert_load_file(botan_x509_cert_t* cert_obj, const char* cert_path)
20  {
21  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
22  if(!cert_obj || !cert_path)
24 
25 #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
26  std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(cert_path));
27  *cert_obj = new botan_x509_cert_struct(c.release());
28  return BOTAN_FFI_SUCCESS;
29 #else
31 #endif
32  });
33  }
34 
35 int botan_x509_cert_load(botan_x509_cert_t* cert_obj, const uint8_t cert_bits[], size_t cert_bits_len)
36  {
37  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
38  if(!cert_obj || !cert_bits)
40 
41  Botan::DataSource_Memory bits(cert_bits, cert_bits_len);
42 
43  std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(bits));
44  *cert_obj = new botan_x509_cert_struct(c.release());
45  return BOTAN_FFI_SUCCESS;
46  });
47  }
48 
50  {
51  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
52  if(key == nullptr)
54 
55  *key = nullptr;
56 
57 #if defined(BOTAN_HAS_RSA)
58  std::unique_ptr<Botan::Public_Key> publicKey = safe_get(cert).load_subject_public_key();
59  *key = new botan_pubkey_struct(publicKey.release());
60  return BOTAN_FFI_SUCCESS;
61 #else
63 #endif
64  });
65  }
66 
68  const char* key, size_t index,
69  uint8_t out[], size_t* out_len)
70  {
71  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.issuer_info(key).at(index)); });
72  }
73 
75  const char* key, size_t index,
76  uint8_t out[], size_t* out_len)
77  {
78  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.subject_info(key).at(index)); });
79  }
80 
81 int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t* out_len)
82  {
83  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.to_string()); });
84  }
85 
86 int botan_x509_cert_allowed_usage(botan_x509_cert_t cert, unsigned int key_usage)
87  {
88  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, {
89  const Botan::Key_Constraints k = static_cast<Botan::Key_Constraints>(key_usage);
90  if(c.allowed_usage(k))
91  return BOTAN_FFI_SUCCESS;
92  return 1;
93  });
94  }
95 
97  {
98  return BOTAN_FFI_CHECKED_DELETE(cert);
99  }
100 
101 int botan_x509_cert_get_time_starts(botan_x509_cert_t cert, char out[], size_t* out_len)
102  {
103  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.not_before().to_string()); });
104  }
105 
106 int botan_x509_cert_get_time_expires(botan_x509_cert_t cert, char out[], size_t* out_len)
107  {
108  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.not_after().to_string()); });
109  }
110 
111 int botan_x509_cert_get_serial_number(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
112  {
113  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.serial_number()); });
114  }
115 
116 int botan_x509_cert_get_fingerprint(botan_x509_cert_t cert, const char* hash, uint8_t out[], size_t* out_len)
117  {
118  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.fingerprint(hash)); });
119  }
120 
121 int botan_x509_cert_get_authority_key_id(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
122  {
123  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.authority_key_id()); });
124  }
125 
126 int botan_x509_cert_get_subject_key_id(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
127  {
128  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.subject_key_id()); });
129  }
130 
131 int botan_x509_cert_get_public_key_bits(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
132  {
133  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.subject_public_key_bits()); });
134  }
135 
136 int botan_x509_cert_hostname_match(botan_x509_cert_t cert, const char* hostname)
137  {
138  if(hostname == nullptr)
140 
141  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c,
142  { return c.matches_dns_name(hostname) ? 0 : -1; });
143  }
144 
145 }
int botan_x509_cert_destroy(botan_x509_cert_t cert)
Definition: ffi_cert.cpp:96
int botan_x509_cert_load(botan_x509_cert_t *cert_obj, const uint8_t cert_bits[], size_t cert_bits_len)
Definition: ffi_cert.cpp:35
int botan_x509_cert_get_authority_key_id(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi_cert.cpp:121
int botan_x509_cert_get_time_starts(botan_x509_cert_t cert, char out[], size_t *out_len)
Definition: ffi_cert.cpp:101
int botan_x509_cert_get_public_key_bits(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi_cert.cpp:131
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
BOTAN_FFI_DECLARE_STRUCT(botan_x509_cert_struct, Botan::X509_Certificate, 0x8F628937)
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:146
bool matches_dns_name(const std::string &name) const
Definition: x509cert.cpp:711
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
int botan_x509_cert_get_public_key(botan_x509_cert_t cert, botan_pubkey_t *key)
Definition: ffi_cert.cpp:49
int botan_x509_cert_get_subject_key_id(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi_cert.cpp:126
struct botan_x509_cert_struct * botan_x509_cert_t
Definition: ffi.h:1123
int botan_x509_cert_load_file(botan_x509_cert_t *cert_obj, const char *cert_path)
Definition: ffi_cert.cpp:19
int botan_x509_cert_get_serial_number(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi_cert.cpp:111
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126
int botan_x509_cert_get_fingerprint(botan_x509_cert_t cert, const char *hash, uint8_t out[], size_t *out_len)
Definition: ffi_cert.cpp:116
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:49
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)
Definition: ffi_cert.cpp:74
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
int botan_x509_cert_allowed_usage(botan_x509_cert_t cert, unsigned int key_usage)
Definition: ffi_cert.cpp:86
int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t *out_len)
Definition: ffi_cert.cpp:81
struct botan_pubkey_struct * botan_pubkey_t
Definition: ffi.h:791
int botan_x509_cert_hostname_match(botan_x509_cert_t cert, const char *hostname)
Definition: ffi_cert.cpp:136
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
MechanismType hash
int botan_x509_cert_get_time_expires(botan_x509_cert_t cert, char out[], size_t *out_len)
Definition: ffi_cert.cpp:106
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)
Definition: ffi_cert.cpp:67