Skip to content

Commit 66b5295

Browse files
authored
Deprecated security methods (#77)
Signed-off-by: Alejandro Hernandez Cordero <[email protected]>
1 parent 6d3a10a commit 66b5295

File tree

5 files changed

+75
-98
lines changed

5 files changed

+75
-98
lines changed

rmw_dds_common/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ find_package(ament_cmake REQUIRED)
1616
find_package(rcpputils REQUIRED)
1717
find_package(rcutils REQUIRED)
1818
find_package(rmw REQUIRED)
19+
find_package(rmw_security_common REQUIRED)
1920
find_package(rosidl_default_generators REQUIRED)
2021
find_package(rosidl_runtime_c REQUIRED)
2122

2223
ament_add_default_options()
2324
ament_export_dependencies(ament_cmake_core)
2425
ament_export_dependencies(rcutils)
26+
ament_export_dependencies(rmw_security_common)
2527
ament_export_dependencies(rmw)
2628

2729
rosidl_generate_interfaces(
@@ -43,6 +45,7 @@ set_target_properties(${PROJECT_NAME}_library
4345
PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
4446
target_link_libraries(${PROJECT_NAME}_library PUBLIC
4547
rcutils::rcutils
48+
rmw_security_common::rmw_security_common_library
4649
rmw::rmw)
4750
target_link_libraries(${PROJECT_NAME}_library PRIVATE
4851
rcpputils::rcpputils

rmw_dds_common/include/rmw_dds_common/security.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace rmw_dds_common
4747
* \param[out] result The map where the friendly name -> filename pairs are stored.
4848
* \return `true` if all required files exist in the security enclave, `false` otherwise.
4949
*/
50+
[[deprecated("This function is now available in rmw_security_common")]]
5051
RMW_DDS_COMMON_PUBLIC
5152
bool get_security_files(
5253
const std::string & prefix, const std::string & secure_root,
@@ -77,6 +78,7 @@ bool get_security_files(
7778
* \param[out] result The map where the friendly name -> filename pairs are stored.
7879
* \return `true` if all required files exist in the security enclave, `false` otherwise.
7980
*/
81+
[[deprecated("This function is now available in rmw_security_common")]]
8082
RMW_DDS_COMMON_PUBLIC
8183
bool get_security_files(
8284
bool supports_pkcs11,

rmw_dds_common/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<depend>rcutils</depend>
2121
<depend>rcpputils</depend>
2222
<depend>rmw</depend>
23+
<depend>rmw_security_common</depend>
2324
<depend>rosidl_runtime_c</depend>
2425
<depend>rosidl_runtime_cpp</depend>
2526

rmw_dds_common/src/security.cpp

Lines changed: 51 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -21,57 +21,38 @@
2121
#include <vector>
2222

2323
#include "rmw_dds_common/security.hpp"
24+
#include "rmw_security_common/security.hpp"
25+
#include "rmw/error_handling.h"
2426

25-
namespace rmw_dds_common
26-
{
27+
#include "rcpputils/scope_exit.hpp"
2728

28-
// Processor for security attributes with FILE URI
29-
static bool process_file_uri_security_file(
30-
bool /*supports_pkcs11*/,
31-
const std::string & prefix,
32-
const std::filesystem::path & full_path,
33-
std::string & result)
34-
{
35-
if (!std::filesystem::is_regular_file(full_path)) {
36-
return false;
37-
}
38-
result = prefix + full_path.generic_string();
39-
return true;
40-
}
29+
#include "rcutils/allocator.h"
30+
#include "rcutils/types/string_map.h"
4131

42-
// Processor for security attributes with PKCS#11 URI
43-
static bool process_pkcs_uri_security_file(
44-
bool supports_pkcs11,
45-
const std::string & /*prefix*/,
46-
const std::filesystem::path & full_path,
47-
std::string & result)
32+
namespace rmw_dds_common
4833
{
49-
if (!supports_pkcs11) {
50-
return false;
51-
}
52-
53-
const std::string p11_prefix("pkcs11:");
54-
55-
std::ifstream ifs(full_path);
56-
if (!ifs.is_open()) {
57-
return false;
58-
}
59-
60-
if (!(ifs >> result)) {
61-
return false;
62-
}
63-
if (result.find(p11_prefix) != 0) {
64-
return false;
65-
}
66-
67-
return true;
68-
}
6934

7035
bool get_security_files(
7136
const std::string & prefix, const std::string & secure_root,
7237
std::unordered_map<std::string, std::string> & result)
7338
{
39+
#if !defined(_WIN32)
40+
# pragma GCC diagnostic push
41+
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
42+
# ifdef __clang__
43+
# pragma clang diagnostic push
44+
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
45+
# endif
46+
#else // !defined(_WIN32)
47+
# pragma warning(push)
48+
# pragma warning(disable: 4996)
49+
#endif
7450
return get_security_files(false, prefix, secure_root, result);
51+
#if !defined(_WIN32)
52+
# pragma GCC diagnostic pop
53+
#else // !defined(_WIN32)
54+
# pragma warning(pop)
55+
#endif
7556
}
7657

7758
bool get_security_files(
@@ -80,68 +61,40 @@ bool get_security_files(
8061
const std::string & secure_root,
8162
std::unordered_map<std::string, std::string> & result)
8263
{
83-
using std::placeholders::_1;
84-
using std::placeholders::_2;
85-
using std::placeholders::_3;
86-
using std::placeholders::_4;
87-
using security_file_processor =
88-
std::function<bool (bool, const std::string &, const std::filesystem::path &, std::string &)>;
89-
using processor_vector =
90-
std::vector<std::pair<std::string, security_file_processor>>;
91-
92-
// Key: the security attribute
93-
// Value: ordered sequence of pairs. Each pair contains one possible file name
94-
// for the attribute and the corresponding processor method
95-
// Pairs are ordered by priority: the first one matching is used.
96-
const std::unordered_map<std::string, processor_vector> required_files{
97-
{"IDENTITY_CA", {
98-
{"identity_ca.cert.p11", std::bind(process_pkcs_uri_security_file, _1, _2, _3, _4)},
99-
{"identity_ca.cert.pem", std::bind(process_file_uri_security_file, _1, _2, _3, _4)}}},
100-
{"CERTIFICATE", {
101-
{"cert.p11", std::bind(process_pkcs_uri_security_file, _1, _2, _3, _4)},
102-
{"cert.pem", std::bind(process_file_uri_security_file, _1, _2, _3, _4)}}},
103-
{"PRIVATE_KEY", {
104-
{"key.p11", std::bind(process_pkcs_uri_security_file, _1, _2, _3, _4)},
105-
{"key.pem", std::bind(process_file_uri_security_file, _1, _2, _3, _4)}}},
106-
{"PERMISSIONS_CA", {
107-
{"permissions_ca.cert.p11", std::bind(process_pkcs_uri_security_file, _1, _2, _3, _4)},
108-
{"permissions_ca.cert.pem", std::bind(process_file_uri_security_file, _1, _2, _3, _4)}}},
109-
{"GOVERNANCE", {
110-
{"governance.p7s", std::bind(process_file_uri_security_file, _1, _2, _3, _4)}}},
111-
{"PERMISSIONS", {
112-
{"permissions.p7s", std::bind(process_file_uri_security_file, _1, _2, _3, _4)}}},
113-
};
64+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
65+
rcutils_string_map_t security_files = rcutils_get_zero_initialized_string_map();
66+
rcutils_ret_t ret = rcutils_string_map_init(&security_files, 0, allocator);
67+
68+
auto scope_exit_ws = rcpputils::make_scope_exit(
69+
[&security_files]()
70+
{
71+
rcutils_ret_t ret = rcutils_string_map_fini(&security_files);
72+
if (ret != RMW_RET_OK) {
73+
RMW_SET_ERROR_MSG("error cleaning string map memory");
74+
}
75+
});
11476

115-
const std::unordered_map<std::string, std::string> optional_files{
116-
{"CRL", "crl.pem"},
117-
};
77+
if (ret != RCUTILS_RET_OK) {
78+
RMW_SET_ERROR_MSG("error initializin map");
79+
return false;
80+
}
11881

119-
for (const std::pair<const std::string,
120-
std::vector<std::pair<std::string, security_file_processor>>> & el : required_files)
121-
{
122-
std::string attribute_value;
123-
bool processed = false;
124-
for (auto & proc : el.second) {
125-
std::filesystem::path full_path(secure_root);
126-
full_path /= proc.first;
127-
if (proc.second(supports_pkcs11, prefix, full_path, attribute_value)) {
128-
processed = true;
129-
break;
130-
}
131-
}
132-
if (!processed) {
133-
result.clear();
134-
return false;
135-
}
136-
result[el.first] = attribute_value;
82+
ret = get_security_files_support_pkcs(
83+
supports_pkcs11, prefix.c_str(), secure_root.c_str(), &security_files);
84+
if (ret != RCUTILS_RET_OK) {
85+
RMW_SET_ERROR_MSG("error calling get_security_files_support_pkcs");
86+
return false;
13787
}
13888

139-
for (const std::pair<const std::string, std::string> & el : optional_files) {
140-
std::filesystem::path full_path(secure_root);
141-
full_path /= el.second;
142-
if (std::filesystem::is_regular_file(full_path)) {
143-
result[el.first] = prefix + full_path.generic_string();
89+
const char * key = rcutils_string_map_get_next_key(&security_files, NULL);
90+
while (key != NULL) {
91+
const char * value = rcutils_string_map_get(&security_files, key);
92+
if (NULL == value) {
93+
RMW_SET_ERROR_MSG("unable to get value for known key, should not happen");
94+
return false;
14495
}
96+
result[key] = value;
97+
key = rcutils_string_map_get_next_key(&security_files, key);
14598
}
14699

147100
return true;

rmw_dds_common/test/test_security.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ static void write_test_pkcs11_content(const std::array<std::string, N> & pkcs11_
4949

5050
class test_security : public ::testing::TestWithParam<bool> {};
5151

52+
#if !defined(_WIN32)
53+
# pragma GCC diagnostic push
54+
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
55+
# ifdef __clang__
56+
# pragma clang diagnostic push
57+
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
58+
# endif
59+
#else // !defined(_WIN32)
60+
# pragma warning(push)
61+
# pragma warning(disable: 4996)
62+
#endif
63+
5264
TEST_P(test_security, files_exist_no_prefix)
5365
{
5466
std::filesystem::path dir = std::filesystem::path("./test_folder");
@@ -403,3 +415,9 @@ INSTANTIATE_TEST_SUITE_P(
403415
[](const testing::TestParamInfo<bool> & info) {
404416
return info.param ? "with_pkcs11_support" : "with_no_pkcs11_support";
405417
});
418+
419+
#if !defined(_WIN32)
420+
# pragma GCC diagnostic pop
421+
#else // !defined(_WIN32)
422+
# pragma warning(pop)
423+
#endif

0 commit comments

Comments
 (0)