Debian Codegraph @

src/security/core/tests/common/security_config_test_utils.c

raw
1 /*
2 * Copyright(c) 2006 to 2021 ZettaScale Technology and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
7 * v. 1.0 which is available at
8 * http://www.eclipse.org/org/documents/edl-v10.php.
9 *
10 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11 */
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "CUnit/Test.h"
17 #include "dds/dds.h"
18 #include "dds/ddsrt/environ.h"
19 #include "dds/ddsrt/expand_vars.h"
20 #include "dds/ddsrt/heap.h"
21 #include "dds/ddsrt/string.h"
22 #include "dds/ddsrt/io.h"
23 #include "dds/security/openssl_support.h"
24 #include "common/config_env.h"
25 #include "common/test_utils.h"
26 #include "security_config_test_utils.h"
27
28 static const char *topic_rule =
29 " <topic_rule>"
30 " <topic_expression>${TOPIC_EXPRESSION}</topic_expression>"
31 " <enable_discovery_protection>${ENABLE_DISC_PROTECTION}</enable_discovery_protection>"
32 " <enable_liveliness_protection>${ENABLE_LIVELINESS_PROTECTION}</enable_liveliness_protection>"
33 " <enable_read_access_control>${ENABLE_READ_AC}</enable_read_access_control>"
34 " <enable_write_access_control>${ENABLE_WRITE_AC}</enable_write_access_control>"
35 " <metadata_protection_kind>${METADATA_PROTECTION_KIND}</metadata_protection_kind>"
36 " <data_protection_kind>${DATA_PROTECTION_KIND}</data_protection_kind>"
37 " </topic_rule>";
38
39 static const char *governance_xml =
40 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
41 "<dds xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"https://www.omg.org/spec/DDS-SECURITY/20170901/omg_shared_ca_governance.xsd\">"
42 " <domain_access_rules>"
43 " <domain_rule>"
44 " <domains>"
45 " <id_range>"
46 " <min>0</min>"
47 " <max>230</max>"
48 " </id_range>"
49 " </domains>"
50 " <allow_unauthenticated_participants>${ALLOW_UNAUTH_PP:-false}</allow_unauthenticated_participants>"
51 " <enable_join_access_control>${ENABLE_JOIN_AC:-false}</enable_join_access_control>"
52 " <discovery_protection_kind>${DISCOVERY_PROTECTION_KIND:-NONE}</discovery_protection_kind>"
53 " <liveliness_protection_kind>${LIVELINESS_PROTECTION_KIND:-NONE}</liveliness_protection_kind>"
54 " <rtps_protection_kind>${RTPS_PROTECTION_KIND:-NONE}</rtps_protection_kind>"
55 " <topic_access_rules>"
56 " ${TOPIC_RULES}"
57 " </topic_access_rules>"
58 " </domain_rule>"
59 " </domain_access_rules>"
60 "</dds>";
61
62 static const char *permissions_xml_pub =
63 " <publish>"
64 " <topics><topic>${TOPIC_NAME}</topic></topics>"
65 " <partitions>${PARTITIONS:-<partition>*</partition>}</partitions>"
66 " </publish>";
67
68 static const char *permissions_xml_sub =
69 " <subscribe>"
70 " <topics><topic>${TOPIC_NAME}</topic></topics>"
71 " <partitions>${PARTITIONS:-<partition>*</partition>}</partitions>"
72 " </subscribe>";
73
74 static const char *permissions_xml_allow_rule =
75 " <allow_rule>"
76 " <domains>${DOMAIN_ID:+<id>}${DOMAIN_ID:-<id_range><min>0</min><max>230</max></id_range>}${DOMAIN_ID:+</id>}</domains>"
77 " ${PUBLISH}"
78 " ${SUBSCRIBE}"
79 " </allow_rule>";
80
81 static const char *permissions_xml_deny_rule =
82 " <deny_rule>"
83 " <domains>${DOMAIN_ID:+<id>}${DOMAIN_ID:-<id_range><min>0</min><max>230</max></id_range>}${DOMAIN_ID:+</id>}</domains>"
84 " ${PUBLISH}"
85 " ${SUBSCRIBE}"
86 " </deny_rule>";
87
88 static const char *permissions_xml_grant =
89 " <grant name=\"${GRANT_NAME}\">"
90 " <subject_name>${SUBJECT_NAME}</subject_name>"
91 " <validity><not_before>${NOT_BEFORE:-2015-09-15T01:00:00}</not_before><not_after>${NOT_AFTER:-2115-09-15T01:00:00}</not_after></validity>"
92 " ${RULES}"
93 " <default>${DEFAULT_POLICY:-DENY}</default>"
94 " </grant>";
95
96 static const char *permissions_xml =
97 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
98 "<dds xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"https://www.omg.org/spec/DDS-SECURITY/20170901/omg_shared_ca_permissions.xsd\">"
99 " <permissions>"
100 " ${GRANTS}"
101 " </permissions>"
102 "</dds>";
103
104
105 const char * expand_lookup_vars(const char *name, void * data)
106 {
107 struct kvp *vars = (struct kvp *)data;
108 for (uint32_t i = 0; vars[i].key != NULL; i++)
109 {
110 if (!strcmp(vars[i].key, name))
111 {
112 vars[i].count--;
113 return vars[i].value;
114 }
115 }
116 return NULL;
117 }
118
119 const char * expand_lookup_vars_env(const char *name, void * data)
120 {
121 const char *env;
122 if ((env = expand_lookup_vars (name, data)))
123 return env;
124 return ((ddsrt_getenv(name, &env)) == DDS_RETCODE_OK) ? env : NULL;
125 }
126
127 int32_t expand_lookup_unmatched (const struct kvp * lookup_table)
128 {
129 int32_t unmatched = 0;
130 for (uint32_t i = 0; lookup_table[i].key != NULL; i++)
131 {
132 int32_t c = lookup_table[i].count;
133 if (c > 0 && unmatched >= INT32_MAX - c)
134 return INT32_MAX;
135 if (c < 0 && unmatched <= INT32_MIN - c)
136 return INT32_MIN;
137 unmatched += c;
138 }
139 return unmatched;
140 }
141
142 static char * get_xml_datetime(dds_time_t t, char * buf, size_t len)
143 {
144 struct tm tm;
145 time_t sec = (time_t)(t / DDS_NSECS_IN_SEC);
146 #if _WIN32
147 (void)gmtime_s(&tm, &sec);
148 #else
149 (void)gmtime_r(&sec, &tm);
150 #endif /* _WIN32 */
151 strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", &tm);
152 return buf;
153 }
154
155 static char * smime_sign(char * ca_cert_path, char * ca_priv_key_path, const char * data)
156 {
157 dds_openssl_init ();
158
159 // Read CA certificate
160 BIO *ca_cert_bio = BIO_new (BIO_s_file ());
161 if (BIO_read_filename (ca_cert_bio, ca_cert_path) <= 0)
162 {
163 printf ("Error reading CA certificate file %s\n", ca_cert_path);
164 CU_ASSERT_FATAL (false);
165 }
166
167 // Read CA private key
168 BIO *ca_priv_key_bio = BIO_new (BIO_s_file ());
169 if (BIO_read_filename (ca_priv_key_bio, ca_priv_key_path) <= 0)
170 {
171 printf ("Error reading CA private key file %s\n", ca_priv_key_path);
172 CU_ASSERT_FATAL (false);
173 }
174
175 // Create Openssl certificate and private key from the BIO's
176 X509 *ca_cert = PEM_read_bio_X509_AUX (ca_cert_bio, NULL, NULL, NULL);
177 EVP_PKEY* ca_priv_key = PEM_read_bio_PrivateKey (ca_priv_key_bio, NULL, 0, NULL);
178
179 // Read the data
180 BIO *data_bio = BIO_new (BIO_s_mem ());
181 if (BIO_puts (data_bio, data) <= 0) {
182 printf ("Error getting configuration data for signing\n");
183 CU_ASSERT_FATAL (false);
184 }
185
186 // Create the data signing object
187 PKCS7 *signed_data = PKCS7_sign (ca_cert, ca_priv_key, NULL, data_bio, PKCS7_DETACHED | PKCS7_STREAM | PKCS7_TEXT);
188 if (!signed_data) {
189 printf ("Error signing configuration data\n");
190 CU_ASSERT_FATAL (false);
191 }
192
193 // Create BIO for writing output
194 BIO *output_bio = BIO_new (BIO_s_mem ());
195 if (!SMIME_write_PKCS7 (output_bio, signed_data, data_bio, PKCS7_DETACHED | PKCS7_STREAM | PKCS7_TEXT)) {
196 printf ("Error writing signed XML configuration\n");
197 CU_ASSERT_FATAL (false);
198 }
199
200 // Get string
201 char *output_tmp = NULL;
202 size_t output_sz = (size_t)BIO_get_mem_data (output_bio, &output_tmp);
203 char * output = ddsrt_malloc(output_sz + 1);
204 memcpy(output, output_tmp, output_sz);
205 output[output_sz] = 0;
206
207 BIO_free (output_bio);
208 PKCS7_free (signed_data);
209 BIO_free (data_bio);
210 EVP_PKEY_free (ca_priv_key);
211 X509_free (ca_cert);
212 BIO_free (ca_priv_key_bio);
213 BIO_free (ca_cert_bio);
214
215 return output;
216 }
217
218 static char *get_signed_data(const char *data)
219 {
220 return smime_sign (
221 COMMON_ETC_PATH("default_permissions_ca.pem"),
222 COMMON_ETC_PATH("default_permissions_ca_key.pem"),
223 data);
224 }
225
226 static char * prefix_data (char * config_signed, bool add_prefix)
227 {
228 if (add_prefix)
229 {
230 char * tmp = config_signed;
231 ddsrt_asprintf (&config_signed, "data:,%s", tmp);
232 ddsrt_free (tmp);
233 }
234 return config_signed;
235 }
236
237 static void print_config_vars(struct kvp *vars)
238 {
239 for (uint32_t i = 0; vars[i].key != NULL; i++)
240 printf("%s=%s; ", vars[i].key, vars[i].value);
241 }
242
243 char * get_governance_topic_rule(const char * topic_expr, bool discovery_protection, bool liveliness_protection,
244 bool read_ac, bool write_ac, DDS_Security_ProtectionKind metadata_protection_kind, DDS_Security_BasicProtectionKind data_protection_kind)
245 {
246 struct kvp vars[] = {
247 { "TOPIC_EXPRESSION", topic_expr != NULL ? topic_expr : "*", 1 },
248 { "ENABLE_DISC_PROTECTION", discovery_protection ? "true" : "false", 1 },
249 { "ENABLE_LIVELINESS_PROTECTION", liveliness_protection ? "true" : "false", 1 },
250 { "ENABLE_READ_AC", read_ac ? "true" : "false", 1 },
251 { "ENABLE_WRITE_AC", write_ac ? "true" : "false", 1 },
252 { "METADATA_PROTECTION_KIND", pk_to_str (metadata_protection_kind), 1 },
253 { "DATA_PROTECTION_KIND", bpk_to_str (data_protection_kind), 1 },
254 { NULL, NULL, 0 }
255 };
256 return ddsrt_expand_vars (topic_rule, &expand_lookup_vars, vars);
257 }
258
259 char * get_governance_config (bool allow_unauth_pp, bool enable_join_ac, DDS_Security_ProtectionKind discovery_protection_kind, DDS_Security_ProtectionKind liveliness_protection_kind,
260 DDS_Security_ProtectionKind rtps_protection_kind, const char * topic_rules, bool add_prefix)
261 {
262 struct kvp vars[] = {
263 { "ALLOW_UNAUTH_PP", allow_unauth_pp ? "true" : "false", 1 },
264 { "ENABLE_JOIN_AC", enable_join_ac ? "true" : "false", 1 },
265 { "DISCOVERY_PROTECTION_KIND", pk_to_str (discovery_protection_kind), 1 },
266 { "LIVELINESS_PROTECTION_KIND", pk_to_str (liveliness_protection_kind), 1 },
267 { "RTPS_PROTECTION_KIND", pk_to_str (rtps_protection_kind), 1 },
268 { "TOPIC_RULES", topic_rules != NULL ? topic_rules : get_governance_topic_rule (NULL, false, false, false, false, PK_N, BPK_N), 1 },
269 { NULL, NULL, 0 }
270 };
271 char * config = ddsrt_expand_vars (governance_xml, &expand_lookup_vars, vars);
272 char * config_signed = get_signed_data (config);
273 ddsrt_free (config);
274
275 print_test_msg ("governance configuration: ");
276 print_config_vars (vars);
277 printf("\n");
278
279 return prefix_data (config_signed, add_prefix);
280 }
281
282 static char * expand_permissions_pubsub (const char * template, const char * topic_name, const char ** parts)
283 {
284 char *xml_parts = NULL;
285 if (parts)
286 {
287 static const char open[] = "<partition>", close[] = "</partition>";
288 size_t len = 0;
289 for (int i = 0; parts[i]; i++)
290 len += strlen (parts[i]) + sizeof (open) + sizeof (close) - 2;
291 xml_parts = ddsrt_malloc (len + 1);
292 int pos = 0;
293 for (int i = 0; parts[i]; i++)
294 pos += snprintf (xml_parts + pos, len + 1 - (size_t) pos, "%s%s%s", open, parts[i], close);
295 assert ((size_t) pos == len);
296 }
297 struct kvp vars[3] = {
298 { "TOPIC_NAME", topic_name, 1 },
299 { xml_parts ? "PARTITIONS" : NULL, xml_parts, xml_parts != NULL },
300 { NULL, NULL, 0 }
301 };
302 char * x = ddsrt_expand_vars (template, &expand_lookup_vars, vars);
303 ddsrt_free (xml_parts);
304 return x;
305 }
306
307 static char * expand_permissions_rule (const char * template, const char * domain_id, const char * pub_xml, const char * sub_xml)
308 {
309 struct kvp vars[] = {
310 { "DOMAIN_ID", domain_id, 3 },
311 { "PUBLISH", pub_xml, 1 },
312 { "SUBSCRIBE", sub_xml, 1 },
313 { NULL, NULL, 0 }
314 };
315 return ddsrt_expand_vars (template, &expand_lookup_vars, vars);
316 }
317
318 char * get_permissions_rules_w_partitions (const char * domain_id, const char * allow_pub_topic, const char * allow_sub_topic, const char ** allow_parts, const char * deny_pub_topic, const char * deny_sub_topic, const char ** deny_parts)
319 {
320 char * allow_pub_xml = NULL, * allow_sub_xml = NULL, * deny_pub_xml = NULL, * deny_sub_xml = NULL;
321 char * allow_rule_xml = NULL, * deny_rule_xml = NULL, * rules_xml;
322
323 if (allow_pub_topic != NULL) allow_pub_xml = expand_permissions_pubsub (permissions_xml_pub, allow_pub_topic, allow_parts);
324 if (allow_sub_topic != NULL) allow_sub_xml = expand_permissions_pubsub (permissions_xml_sub, allow_sub_topic, allow_parts);
325 if (deny_pub_topic != NULL) deny_pub_xml = expand_permissions_pubsub (permissions_xml_pub, deny_pub_topic, deny_parts);
326 if (deny_sub_topic != NULL) deny_sub_xml = expand_permissions_pubsub (permissions_xml_sub, deny_sub_topic, deny_parts);
327
328 if (allow_pub_xml != NULL || allow_sub_xml != NULL)
329 {
330 allow_rule_xml = expand_permissions_rule (permissions_xml_allow_rule, domain_id, allow_pub_xml, allow_sub_xml);
331 if (allow_pub_xml != NULL) ddsrt_free (allow_pub_xml);
332 if (allow_sub_xml != NULL) ddsrt_free (allow_sub_xml);
333 }
334 if (deny_pub_xml != NULL || deny_sub_xml != NULL)
335 {
336 deny_rule_xml = expand_permissions_rule (permissions_xml_deny_rule, domain_id, deny_pub_xml, deny_sub_xml);
337 if (deny_pub_xml != NULL) ddsrt_free (deny_pub_xml);
338 if (deny_sub_xml != NULL) ddsrt_free (deny_sub_xml);
339 }
340 ddsrt_asprintf (&rules_xml, "%s%s", allow_rule_xml != NULL ? allow_rule_xml : "", deny_rule_xml != NULL ? deny_rule_xml : "");
341 if (allow_rule_xml != NULL) ddsrt_free (allow_rule_xml);
342 if (deny_rule_xml != NULL) ddsrt_free (deny_rule_xml);
343 return rules_xml;
344 }
345
346 char * get_permissions_rules (const char * domain_id, const char * allow_pub_topic, const char * allow_sub_topic, const char * deny_pub_topic, const char * deny_sub_topic)
347 {
348 return get_permissions_rules_w_partitions (domain_id, allow_pub_topic, allow_sub_topic, NULL, deny_pub_topic, deny_sub_topic, NULL);
349 }
350
351 char * get_permissions_grant (const char * grant_name, const char * subject_name, dds_time_t not_before, dds_time_t not_after, const char * rules_xml, const char * default_policy)
352 {
353 char not_before_str[] = "0000-00-00T00:00:00Z";
354 char not_after_str[] = "0000-00-00T00:00:00Z";
355 get_xml_datetime (not_before, not_before_str, sizeof(not_before_str));
356 get_xml_datetime (not_after, not_after_str, sizeof(not_after_str));
357
358 struct kvp vars[] = {
359 { "GRANT_NAME", grant_name, 1 },
360 { "SUBJECT_NAME", subject_name, 1 },
361 { "NOT_BEFORE", not_before_str, 1 },
362 { "NOT_AFTER", not_after_str, 1 },
363 { "RULES", rules_xml, 1 },
364 { "DEFAULT_POLICY", default_policy, 1 },
365 { NULL, NULL, 0 }
366 };
367 char * res = ddsrt_expand_vars (permissions_xml_grant, &expand_lookup_vars, vars);
368 CU_ASSERT_FATAL (expand_lookup_unmatched (vars) == 0);
369 return res;
370 }
371
372 char * get_permissions_default_grant (const char * grant_name, const char * subject_name, const char * topic_name)
373 {
374 dds_time_t now = dds_time ();
375 char * rules_xml = get_permissions_rules (NULL, topic_name, topic_name, NULL, NULL);
376 char * grant_xml = get_permissions_grant (grant_name, subject_name, now, now + DDS_SECS(3600), rules_xml, "DENY");
377 ddsrt_free (rules_xml);
378 return grant_xml;
379 }
380
381 char * get_permissions_config(char * grants[], size_t ngrants, bool add_prefix)
382 {
383 char *grants_str = NULL;
384 for (size_t n = 0; n < ngrants; n++)
385 {
386 char * tmp = grants_str;
387 ddsrt_asprintf (&grants_str, "%s%s", grants_str ? grants_str : "", grants[n]);
388 ddsrt_free (tmp);
389 }
390 struct kvp vars[] = {
391 { "GRANTS", grants_str, 1 },
392 { NULL, NULL, 0}
393 };
394 char *config = ddsrt_expand_vars (permissions_xml, &expand_lookup_vars, vars);
395 char *config_signed = get_signed_data (config);
396 ddsrt_free (grants_str);
397 ddsrt_free (config);
398 return prefix_data (config_signed, add_prefix);
399 }