Skip to content

Commit

Permalink
Use Uft8Char string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
FAlbertDev committed Jan 16, 2025
1 parent 52fb517 commit cc1ceff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/lib/prov/pkcs11/p11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ class BOTAN_PUBLIC_API(3, 7) InterfaceWrapper {
Version version() const;

/// Access the name of the interface
std::string_view name() const;
std::basic_string_view<Utf8Char> name() const;

/// Access a function list that contains all methods since PKCS #11 v.2.40
virtual const FunctionList& func_2_40() const;
Expand All @@ -1227,13 +1227,13 @@ class BOTAN_PUBLIC_API(3, 7) InterfaceWrapper {
static std::unique_ptr<InterfaceWrapper> latest_p11_interface(Dynamically_Loaded_Library& library);

/**
* Returns an immortal pointer to the uint8_t string "PKCS 11".
* Returns an immortal pointer to the Utf8Char string "PKCS 11".
* Used to define an interface object.
*
* @warning Unfortunately, the interface object requires a non constant
* pointer. However, this string MUST NOT be modified!
*/
static uint8_t* p11_interface_name_ptr();
static Utf8Char* p11_interface_name_ptr();
};

/// Provides access to all PKCS#11 functions
Expand Down
20 changes: 9 additions & 11 deletions src/lib/prov/pkcs11/p11_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace Botan::PKCS11 {

namespace {

constexpr std::array<Utf8Char, 8> PKCS11_INTERFACE_NAME_ARR = {"PKCS 11"};
constexpr std::basic_string_view<Utf8Char> PKCS11_INTERFACE_NAME(PKCS11_INTERFACE_NAME_ARR.data());

std::strong_ordering operator<=>(const Version& left, const Version& right) {
// Compare both versions by concatenating their bytes: major || minor
auto version_value = [](const Version& v) -> uint16_t {
Expand All @@ -34,19 +37,17 @@ Version version_of(const Interface& interface) {
return *reinterpret_cast<Version*>(interface.pFunctionList);
};

std::string_view name_of(const Interface& interface) {
return std::string_view(reinterpret_cast<const char*>(interface.pInterfaceName));
std::basic_string_view<Utf8Char> name_of(const Interface& interface) {
return std::basic_string_view<Utf8Char>(interface.pInterfaceName);
}

constexpr std::string_view PKCS11_INTERFACE_NAME = "PKCS 11";

} // namespace

Version InterfaceWrapper::version() const {
return version_of(m_interface);
}

std::string_view InterfaceWrapper::name() const {
std::basic_string_view<Utf8Char> InterfaceWrapper::name() const {
return name_of(m_interface);
}

Expand Down Expand Up @@ -78,9 +79,6 @@ std::unique_ptr<InterfaceWrapper> InterfaceWrapper::latest_p11_interface(Dynamic
// We only load interfaces named "PKCS 11" (which are the pure ones defined in the spec) with
// version >= 2.40.
auto is_valid_interface = [](const Interface& i) {
if(name_of(i) != PKCS11_INTERFACE_NAME) {
return false;
}
// This is also done by the example in PKCS #11 (version >= 3.0) spec.
// Note that version above the currently supported maximal version should
// be compatible too.
Expand Down Expand Up @@ -138,9 +136,9 @@ const FunctionList32& InterfaceWrapper::func_3_2() const {
return *reinterpret_cast<FunctionList32*>(raw_interface().pFunctionList);
}

uint8_t* InterfaceWrapper::p11_interface_name_ptr() {
static std::array<uint8_t, 8> PKCS11_INTERFACE_NAME_ARR = {'P', 'K', 'C', 'S', ' ', '1', '1', '\0'};
return PKCS11_INTERFACE_NAME_ARR.data();
Utf8Char* InterfaceWrapper::p11_interface_name_ptr() {
static std::array<Utf8Char, 8> STATIC_PKCS11_INTERFACE_NAME_ARR = {"PKCS 11"};
return STATIC_PKCS11_INTERFACE_NAME_ARR.data();
}

} // namespace Botan::PKCS11

0 comments on commit cc1ceff

Please sign in to comment.