Debian Codegraph @

res/res_stir_shaken/common_config.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2023, Sangoma Technologies Corporation
5 *
6 * George Joseph <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 #include "asterisk.h"
20 #include "asterisk/cli.h"
21 #include "asterisk/cli.h"
22 #include "asterisk/logger.h"
23 #include "asterisk/module.h"
24 #include "asterisk/utils.h"
25 #include "asterisk/stasis.h"
26 #include "asterisk/security_events.h"
27
28 #define AST_API_MODULE
29 #include "stir_shaken.h"
30
31 static struct ast_sorcery *sorcery;
32 struct stasis_subscription *named_acl_changed_sub = NULL;
33
34 struct ast_sorcery *get_sorcery(void)
35 {
36 return sorcery;
37 }
38
39 #define generate_bool_handler_functions(param_name) \
40 static const char *param_name ## _map[] = { \
41 [ param_name ## _NOT_SET ] = "not_set", \
42 [ param_name ## _YES ] = "yes", \
43 [ param_name ## _NO ] = "no", \
44 }; \
45 enum param_name ## _enum \
46 param_name ## _from_str(const char *value) \
47 { \
48 if (!strcasecmp(value, param_name ## _map[param_name ## _NOT_SET])) { \
49 return param_name ## _NOT_SET; \
50 } else if (ast_true(value)) { \
51 return param_name ## _YES; \
52 } else if (ast_false(value)) { \
53 return param_name ## _NO; \
54 } \
55 ast_log(LOG_WARNING, "Unknown " #param_name " response value '%s'\n", value); \
56 return param_name ## _UNKNOWN; \
57 }\
58 const char *param_name ## _to_str(enum param_name ## _enum value) \
59 { \
60 return ARRAY_IN_BOUNDS(value, param_name ## _map) ? \
61 param_name ## _map[value] : NULL; \
62 }
63
64 generate_bool_handler_functions(use_rfc9410_responses);
65 generate_bool_handler_functions(ignore_sip_date_header);
66 generate_bool_handler_functions(send_mky);
67 generate_bool_handler_functions(check_tn_cert_public_url);
68 generate_bool_handler_functions(relax_x5u_port_scheme_restrictions);
69 generate_bool_handler_functions(relax_x5u_path_restrictions);
70
71 generate_bool_handler_functions(load_system_certs);
72
73 struct enum_name_xref_entry {
74 int value;
75 const char *name;
76 };
77
78 #define generate_enum_string_functions(param_name, default_value, ...)\
79 static struct enum_name_xref_entry param_name ## _map[] = { \
80 __VA_ARGS__ \
81 } ; \
82 enum param_name ## _enum param_name ## _from_str( \
83 const char *value) \
84 { \
85 int i; \
86 for (i = 0; i < ARRAY_LEN(param_name ## _map); i++) { \
87 if (strcasecmp(value, param_name ##_map[i].name) == 0) { \
88 return param_name ##_map[i].value; \
89 } \
90 } \
91 return param_name ## _ ## default_value; \
92 } \
93 const char *param_name ## _to_str( \
94 enum param_name ## _enum value) \
95 { \
96 int i; \
97 for (i = 0; i < ARRAY_LEN(param_name ## _map); i++) { \
98 if (value == param_name ## _map[i].value) return param_name ## _map[i].name; \
99 } \
100 return NULL; \
101 }
102
103 generate_enum_string_functions(attest_level, UNKNOWN,
104 {attest_level_NOT_SET, "not_set"},
105 {attest_level_A, "A"},
106 {attest_level_B, "B"},
107 {attest_level_C, "C"},
108 );
109
110 generate_enum_string_functions(endpoint_behavior, OFF,
111 {endpoint_behavior_OFF, "off"},
112 {endpoint_behavior_OFF, "none"},
113 {endpoint_behavior_ATTEST, "attest"},
114 {endpoint_behavior_VERIFY, "verify"},
115 {endpoint_behavior_ON, "on"},
116 {endpoint_behavior_ON, "both"}
117 );
118
119 generate_enum_string_functions(stir_shaken_failure_action, CONTINUE,
120 {stir_shaken_failure_action_CONTINUE, "continue"},
121 {stir_shaken_failure_action_REJECT_REQUEST, "reject_request"},
122 {stir_shaken_failure_action_CONTINUE_RETURN_REASON, "continue_return_reason"},
123 );
124
125 static const char *translate_value(const char *val)
126 {
127 if (val[0] == '0'
128 || val[0] == '\0'
129 || strcmp(val, "not_set") == 0) {
130 return "";
131 }
132
133 return val;
134 }
135
136 static void print_acl(int fd, struct ast_acl_list *acl_list, const char *prefix)
137 {
138 struct ast_acl *acl;
139
140 AST_LIST_LOCK(acl_list);
141 AST_LIST_TRAVERSE(acl_list, acl, list) {
142 if (ast_strlen_zero(acl->name)) {
143 ast_cli(fd, "%s(permit/deny)\n", prefix);
144 } else {
145 ast_cli(fd, "%s%s\n", prefix, acl->name);
146 }
147 ast_ha_output(fd, acl->acl, prefix);
148 }
149 AST_LIST_UNLOCK(acl_list);
150 }
151
152 #define print_acl_cert_store(cfg, a, max_name_len) \
153 ({ \
154 if (cfg->vcfg_common.acl) { \
155 ast_cli(a->fd, "x5u_acl:\n"); \
156 print_acl(a->fd, cfg->vcfg_common.acl, " "); \
157 } else { \
158 ast_cli(a->fd, "%-*s: (none)\n", max_name_len, "x5u_acl"); \
159 }\
160 if (cfg->vcfg_common.tcs) { \
161 int count = 0; \
162 ast_cli(a->fd, "%-*s:\n", max_name_len, "Verification CA certificate store"); \
163 count = crypto_show_cli_store(cfg->vcfg_common.tcs, a->fd); \
164 if (count == 0 && (!ast_strlen_zero(cfg->vcfg_common.ca_path) \
165 || !ast_strlen_zero(cfg->vcfg_common.crl_path))) { \
166 ast_cli(a->fd, " Note: Certs in ca_path or crl_path won't show until used.\n"); \
167 } \
168 } else { \
169 ast_cli(a->fd, "%-*s: (none)\n", max_name_len, "Verification CA certificate store"); \
170 } \
171 })
172
173 int config_object_cli_show(void *obj, void *arg, void *data, int flags)
174 {
175 struct ast_cli_args *a = arg;
176 struct config_object_cli_data *cli_data = data;
177 struct ast_variable *options;
178 struct ast_variable *i;
179 const char *title = NULL;
180 const char *cfg_name = NULL;
181 int max_name_len = 0;
182
183 if (!obj) {
184 ast_cli(a->fd, "No stir/shaken configuration found\n");
185 return 0;
186 }
187
188 if (!ast_strlen_zero(cli_data->title)) {
189 title = cli_data->title;
190 } else {
191 title = ast_sorcery_object_get_type(obj);
192 }
193 max_name_len = strlen(title);
194
195 if (cli_data->object_type == config_object_type_profile
196 || cli_data->object_type == config_object_type_tn) {
197 cfg_name = ast_sorcery_object_get_id(obj);
198 max_name_len += strlen(cfg_name) + 2 /* ": " */;
199 }
200
201 options = ast_variable_list_sort(ast_sorcery_objectset_create2(
202 get_sorcery(), obj, AST_HANDLER_ONLY_STRING));
203 if (!options) {
204 return 0;
205 }
206
207 for (i = options; i; i = i->next) {
208 int nlen = strlen(i->name);
209 max_name_len = (nlen > max_name_len) ? nlen : max_name_len;
210 }
211
212 ast_cli(a->fd, "\n==============================================================================\n");
213 if (ast_strlen_zero(cfg_name)) {
214 ast_cli(a->fd, "%s\n", title);
215 } else {
216 ast_cli(a->fd, "%s: %s\n", title, cfg_name);
217 }
218 ast_cli(a->fd, "------------------------------------------------------------------------------\n");
219
220 for (i = options; i; i = i->next) {
221 if (!ast_strings_equal(i->name, "x5u_acl")) {
222 ast_cli(a->fd, "%-*s: %s\n", max_name_len, i->name,
223 translate_value(i->value));
224 }
225 }
226
227 ast_variables_destroy(options);
228
229 if (cli_data->object_type == config_object_type_profile) {
230 struct profile_cfg *cfg = obj;
231 print_acl_cert_store(cfg, a, max_name_len);
232 } else if (cli_data->object_type == config_object_type_verification) {
233 struct verification_cfg *cfg = obj;
234 print_acl_cert_store(cfg, a, max_name_len);
235 }
236 ast_cli(a->fd, "---------------------------------------------\n\n"); \
237
238 return 0;
239 }
240
241 char *config_object_tab_complete_name(const char *word, struct ao2_container *container)
242 {
243 void *obj;
244 struct ao2_iterator it;
245 int wordlen = strlen(word);
246 int ret;
247
248 it = ao2_iterator_init(container, 0);
249 while ((obj = ao2_iterator_next(&it))) {
250 if (!strncasecmp(word, ast_sorcery_object_get_id(obj), wordlen)) {
251 ret = ast_cli_completion_add(ast_strdup(ast_sorcery_object_get_id(obj)));
252 if (ret) {
253 ao2_ref(obj, -1);
254 break;
255 }
256 }
257 ao2_ref(obj, -1);
258 }
259 ao2_iterator_destroy(&it);
260
261 return NULL;
262 }
263
264
265 /* Remove everything except 0-9, *, and # in telephone number according to RFC 8224
266 * (required by RFC 8225 as part of canonicalization) */
267 char *canonicalize_tn(const char *tn, char *dest_tn)
268 {
269 int i;
270 const char *s = tn;
271 size_t len = tn ? strlen(tn) : 0;
272 char *new_tn = dest_tn;
273 SCOPE_ENTER(3, "tn: %s\n", S_OR(tn, "(null)"));
274
275 if (ast_strlen_zero(tn)) {
276 *dest_tn = '\0';
277 SCOPE_EXIT_RTN_VALUE(NULL, "Empty TN\n");
278 }
279
280 if (!dest_tn) {
281 SCOPE_EXIT_RTN_VALUE(NULL, "No destination buffer\n");
282 }
283
284 for (i = 0; i < len; i++) {
285 if (isdigit(*s) || *s == '#' || *s == '*') { /* Only characters allowed */
286 *new_tn++ = *s;
287 }
288 s++;
289 }
290 *new_tn = '\0';
291 SCOPE_EXIT_RTN_VALUE(dest_tn, "Canonicalized '%s' -> '%s'\n", tn, dest_tn);
292 }
293
294 char *canonicalize_tn_alloc(const char *tn)
295 {
296 char *canon_tn = ast_strlen_zero(tn) ? NULL : ast_malloc(strlen(tn) + 1);
297 if (!canon_tn) {
298 return NULL;
299 }
300 return canonicalize_tn(tn, canon_tn);
301 }
302
303 static char *cli_verify_cert(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
304 {
305 RAII_VAR(struct profile_cfg *, profile, NULL, ao2_cleanup);
306 RAII_VAR(struct verification_cfg *, vs_cfg, NULL, ao2_cleanup);
307 struct crypto_cert_store *tcs;
308 X509 *cert = NULL;
309 STACK_OF(X509) *cert_chain = NULL;
310 const char *errmsg = NULL;
311
312 switch(cmd) {
313 case CLI_INIT:
314 e->command = "stir_shaken verify certificate_file";
315 e->usage =
316 "Usage: stir_shaken verify certificate_file <certificate_file> [ <profile> ]\n"
317 " Verify an external certificate file against the global or profile verification store\n";
318 return NULL;
319 case CLI_GENERATE:
320 if (a->pos == 4) {
321 return config_object_tab_complete_name(a->word, profile_get_all());
322 } else {
323 return NULL;
324 }
325 }
326
327 if (a->argc < 4) {
328 return CLI_SHOWUSAGE;
329 }
330
331 if (a->argc == 5) {
332 profile = profile_get_cfg(a->argv[4]);
333 if (!profile) {
334 ast_cli(a->fd, "Profile %s doesn't exist\n", a->argv[4]);
335 return CLI_SUCCESS;
336 }
337 if (!profile->vcfg_common.tcs) {
338 ast_cli(a->fd,"Profile %s doesn't have a certificate store\n", a->argv[4]);
339 return CLI_SUCCESS;
340 }
341 tcs = profile->vcfg_common.tcs;
342 } else {
343 vs_cfg = vs_get_cfg();
344 if (!vs_cfg) {
345 ast_cli(a->fd, "No verification store found\n");
346 return CLI_SUCCESS;
347 }
348 tcs = vs_cfg->vcfg_common.tcs;
349 }
350
351 cert = crypto_load_cert_chain_from_file(a->argv[3], &cert_chain);
352 if (!cert) {
353 ast_cli(a->fd, "Failed to load certificate from %s. See log for details\n", a->argv[3]);
354 return CLI_SUCCESS;
355 }
356
357 if (crypto_is_cert_trusted(tcs, cert, cert_chain, &errmsg)) {
358 ast_cli(a->fd, "Certificate %s trusted\n", a->argv[3]);
359 } else {
360 ast_cli(a->fd, "Certificate %s NOT trusted: %s\n", a->argv[3], errmsg);
361 }
362 X509_free(cert);
363 if (cert_chain) {
364 sk_X509_pop_free(cert_chain, X509_free);
365 }
366
367 return CLI_SUCCESS;
368 }
369
370 static struct ast_cli_entry cli_commands[] = {
371 AST_CLI_DEFINE(cli_verify_cert, "Verify a certificate file against the global or a profile verification store"),
372 };
373
374 int common_config_reload(void)
375 {
376 SCOPE_ENTER(2, "Stir Shaken Reload\n");
377 if (vs_reload()) {
378 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken VS Reload failed\n");
379 }
380
381 if (as_reload()) {
382 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken AS Reload failed\n");
383 }
384
385 if (tn_config_reload()) {
386 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken TN Reload failed\n");
387 }
388
389 if (profile_reload()) {
390 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken Profile Reload failed\n");
391 }
392
393 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_SUCCESS, "Stir Shaken Reload Done\n");
394 }
395
396 int common_config_unload(void)
397 {
398 ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
399
400 profile_unload();
401 tn_config_unload();
402 as_unload();
403 vs_unload();
404
405 if (named_acl_changed_sub) {
406 stasis_unsubscribe(named_acl_changed_sub);
407 named_acl_changed_sub = NULL;
408 }
409 ast_sorcery_unref(sorcery);
410 sorcery = NULL;
411
412 return 0;
413 }
414
415 static void named_acl_changed_cb(void *data,
416 struct stasis_subscription *sub, struct stasis_message *message)
417 {
418 if (stasis_message_type(message) != ast_named_acl_change_type()) {
419 return;
420 }
421 ast_log(LOG_NOTICE, "Named acl changed. Reloading verification and profile\n");
422 common_config_reload();
423 }
424
425 int common_config_load(void)
426 {
427 SCOPE_ENTER(2, "Stir Shaken Load\n");
428
429 if (!(sorcery = ast_sorcery_open())) {
430 common_config_unload();
431 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken sorcery load failed\n");
432 }
433
434 if (vs_load()) {
435 common_config_unload();
436 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken VS load failed\n");
437 }
438
439 if (as_load()) {
440 common_config_unload();
441 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken AS load failed\n");
442 }
443
444 if (tn_config_load()) {
445 common_config_unload();
446 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken TN load failed\n");
447 }
448
449 if (profile_load()) {
450 common_config_unload();
451 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken profile load failed\n");
452 }
453
454 if (!named_acl_changed_sub) {
455 named_acl_changed_sub = stasis_subscribe(ast_security_topic(),
456 named_acl_changed_cb, NULL);
457 if (!named_acl_changed_sub) {
458 common_config_unload();
459 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_DECLINE, "Stir Shaken acl change subscribe failed\n");
460 }
461 stasis_subscription_accept_message_type(
462 named_acl_changed_sub, ast_named_acl_change_type());
463 }
464
465 ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
466
467 SCOPE_EXIT_RTN_VALUE(AST_MODULE_LOAD_SUCCESS, "Stir Shaken Load Done\n");
468 }