Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions doc/api_ref/ffi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1737,13 +1737,27 @@ X.509 Certificates
const char* key, size_t index, \
uint8_t out[], size_t* out_len)

Get a value from the issuer DN field.
Get a value from the issuer DN field. If the index is out of range,
:cpp:enumerator:`BOTAN_FFI_ERROR_BAD_PARAMETER` is returned for historical
reasons.

.. cpp:function:: int botan_x509_cert_get_issuer_dn_count(botan_x509_cert_t cert, \
const char* key, size_t* count)

Get the number of values for a given key in the issuer DN field.

.. cpp:function:: 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)

Get a value from the subject DN field.
Get a value from the subject DN field. If the index is out of range,
:cpp:enumerator:`BOTAN_FFI_ERROR_BAD_PARAMETER` is returned for historical
reasons.

.. cpp:function:: int botan_x509_cert_get_subject_dn_count(botan_x509_cert_t cert, \
const char* key, size_t* count)

Get the number of values for a given key in the subject DN field.

.. cpp:function:: int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t* out_len)

Expand Down
19 changes: 17 additions & 2 deletions src/lib/ffi/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2182,15 +2182,30 @@ BOTAN_FFI_EXPORT(3, 11) int botan_x509_cert_is_ca(botan_x509_cert_t cert);
*/
BOTAN_FFI_EXPORT(3, 11) int botan_x509_cert_get_path_length_constraint(botan_x509_cert_t cert, size_t* path_limit);

/* TODO(Botan4) this should use char for the out param */
/**
* Enumerates the names of the given @p key in the issuer DN. If @p index is
* out of bounds, BOTAN_FFI_ERROR_BAD_PARAMETER is returned.
*
* TODO(Botan4) use BOTAN_FFI_ERROR_OUT_OF_RANGE instead of BAD_PARAMETER
* TODO(Botan4) this should use char for the out param
*/
BOTAN_FFI_EXPORT(2, 0)
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);
Comment thread
reneme marked this conversation as resolved.
BOTAN_FFI_EXPORT(3, 11) int botan_x509_cert_get_issuer_dn_count(botan_x509_cert_t cert, const char* key, size_t* count);

/* TODO(Botan4) this should use char for the out param */
/**
* Enumerates the names of the given @p key in the subject DN. If @p index is
* out of bounds, BOTAN_FFI_ERROR_BAD_PARAMETER is returned.
*
* TODO(Botan4) use BOTAN_FFI_ERROR_OUT_OF_RANGE instead of BAD_PARAMETER
* TODO(Botan4) this should use char for the out param
*/
BOTAN_FFI_EXPORT(2, 0)
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);
Comment thread
reneme marked this conversation as resolved.
BOTAN_FFI_EXPORT(3, 11)
int botan_x509_cert_get_subject_dn_count(botan_x509_cert_t cert, const char* key, size_t* count);

BOTAN_FFI_EXPORT(2, 0) int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t* out_len);

Expand Down
36 changes: 34 additions & 2 deletions src/lib/ffi/ffi_cert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int botan_x509_cert_get_issuer_dn(
// TODO(Botan4) change the type of out and remove this cast
return write_str_output(reinterpret_cast<char*>(out), out_len, c.issuer_info(key).at(index));
} else {
return BOTAN_FFI_ERROR_BAD_PARAMETER;
return BOTAN_FFI_ERROR_BAD_PARAMETER; // TODO(Botan4): use BOTAN_FFI_ERROR_OUT_OF_RANGE
Comment thread
reneme marked this conversation as resolved.
}
});
#else
Expand All @@ -146,6 +146,22 @@ int botan_x509_cert_get_issuer_dn(
#endif
}

int botan_x509_cert_get_issuer_dn_count(botan_x509_cert_t cert, const char* key, size_t* count) {
#if defined(BOTAN_HAS_X509_CERTIFICATES)
return BOTAN_FFI_VISIT(cert, [=](const auto& c) -> int {
if(Botan::any_null_pointers(count)) {
return BOTAN_FFI_ERROR_NULL_POINTER;
}

*count = c.issuer_info(key).size();
return BOTAN_FFI_SUCCESS;
});
#else
BOTAN_UNUSED(cert, key, count);
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
}

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) {
#if defined(BOTAN_HAS_X509_CERTIFICATES)
Expand All @@ -155,7 +171,7 @@ int botan_x509_cert_get_subject_dn(
// TODO(Botan4) change the type of out and remove this cast
return write_str_output(reinterpret_cast<char*>(out), out_len, c.subject_info(key).at(index));
} else {
return BOTAN_FFI_ERROR_BAD_PARAMETER;
return BOTAN_FFI_ERROR_BAD_PARAMETER; // TODO(Botan4): use BOTAN_FFI_ERROR_OUT_OF_RANGE
Comment thread
reneme marked this conversation as resolved.
}
});
#else
Expand All @@ -164,6 +180,22 @@ int botan_x509_cert_get_subject_dn(
#endif
}

int botan_x509_cert_get_subject_dn_count(botan_x509_cert_t cert, const char* key, size_t* count) {
#if defined(BOTAN_HAS_X509_CERTIFICATES)
return BOTAN_FFI_VISIT(cert, [=](const auto& c) -> int {
if(Botan::any_null_pointers(count)) {
return BOTAN_FFI_ERROR_NULL_POINTER;
}

*count = c.subject_info(key).size();
return BOTAN_FFI_SUCCESS;
});
#else
BOTAN_UNUSED(cert, key, count);
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
}

int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t* out_len) {
return copy_view_str(reinterpret_cast<uint8_t*>(out), out_len, botan_x509_cert_view_as_string, cert);
}
Expand Down
11 changes: 11 additions & 0 deletions src/tests/test_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,12 @@ class FFI_ECDSA_Certificate_Test final : public FFI_Test {
}
#endif

size_t rdn_count;
TEST_FFI_OK(botan_x509_cert_get_issuer_dn_count, (cert, "Name", &rdn_count));
result.test_eq("issuer DN 'name' count", rdn_count, 1);
TEST_FFI_OK(botan_x509_cert_get_issuer_dn_count, (cert, "Organizational Unit", &rdn_count));
result.test_eq("issuer DN 'organizational unit' count", rdn_count, 0);

size_t dn_len = 0;
TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
botan_x509_cert_get_issuer_dn,
Expand All @@ -977,6 +983,11 @@ class FFI_ECDSA_Certificate_Test final : public FFI_Test {
TEST_FFI_OK(botan_x509_cert_get_issuer_dn, (cert, "Name", 0, dn.data(), &dn_len));
result.test_eq("issuer dn", reinterpret_cast<const char*>(dn.data()), "ISRG Root X2");

TEST_FFI_OK(botan_x509_cert_get_subject_dn_count, (cert, "Name", &rdn_count));
result.test_eq("subject DN 'name' count", rdn_count, 1);
TEST_FFI_OK(botan_x509_cert_get_subject_dn_count, (cert, "Organizational Unit", &rdn_count));
result.test_eq("subject DN 'organizational unit' count", rdn_count, 0);

dn_len = 0;
TEST_FFI_RC(BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE,
botan_x509_cert_get_subject_dn,
Expand Down
Loading