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
7035bool 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
7758bool 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 ;
0 commit comments