Debian Codegraph @

res/res_stir_shaken/profile_config.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2022, Sangoma Technologies Corporation
5 *
6 * Ben Ford <[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
21 #include "asterisk/cli.h"
22 #include "asterisk/sorcery.h"
23 #include "asterisk/acl.h"
24 #include "asterisk/stasis.h"
25 #include "asterisk/security_events.h"
26
27 #include "stir_shaken.h"
28
29 #define CONFIG_TYPE "profile"
30
31 #define DEFAULT_endpoint_behavior endpoint_behavior_OFF
32
33 #define DEFAULT_ca_file NULL
34 #define DEFAULT_ca_path NULL
35 #define DEFAULT_crl_file NULL
36 #define DEFAULT_crl_path NULL
37 #define DEFAULT_untrusted_cert_file NULL
38 #define DEFAULT_untrusted_cert_path NULL
39 #define DEFAULT_cert_cache_dir NULL
40
41 #define DEFAULT_curl_timeout 0
42 #define DEFAULT_max_iat_age 0
43 #define DEFAULT_max_date_header_age 0
44 #define DEFAULT_max_cache_entry_age 0
45 #define DEFAULT_max_cache_size 0
46
47 #define DEFAULT_stir_shaken_failure_action stir_shaken_failure_action_NOT_SET
48 #define DEFAULT_use_rfc9410_responses use_rfc9410_responses_NOT_SET
49 #define DEFAULT_relax_x5u_port_scheme_restrictions relax_x5u_port_scheme_restrictions_NOT_SET
50 #define DEFAULT_relax_x5u_path_restrictions relax_x5u_path_restrictions_NOT_SET
51 #define DEFAULT_load_system_certs load_system_certs_NOT_SET
52 #define DEFAULT_ignore_sip_date_header ignore_sip_date_header_NOT_SET
53
54 #define DEFAULT_check_tn_cert_public_url check_tn_cert_public_url_NOT_SET
55 #define DEFAULT_private_key_file NULL
56 #define DEFAULT_public_cert_url NULL
57 #define DEFAULT_attest_level attest_level_NOT_SET
58 #define DEFAULT_unknown_tn_attest_level attest_level_NOT_SET
59 #define DEFAULT_send_mky send_mky_NOT_SET
60
61 static void profile_destructor(void *obj)
62 {
63 struct profile_cfg *cfg = obj;
64 ast_string_field_free_memory(cfg);
65
66 acfg_cleanup(&cfg->acfg_common);
67 vcfg_cleanup(&cfg->vcfg_common);
68
69 ao2_cleanup(cfg->eprofile);
70
71 return;
72 }
73
74 static void *profile_alloc(const char *name)
75 {
76 struct profile_cfg *profile;
77
78 profile = ast_sorcery_generic_alloc(sizeof(*profile), profile_destructor);
79 if (!profile) {
80 return NULL;
81 }
82
83 if (ast_string_field_init(profile, 2048)) {
84 ao2_ref(profile, -1);
85 return NULL;
86 }
87
88 /*
89 * The memory for the commons actually comes from cfg
90 * due to the weirdness of the STRFLDSET macro used with
91 * sorcery. We just use a token amount of memory in
92 * this call so the initialize doesn't fail.
93 */
94 if (ast_string_field_init(&profile->acfg_common, 8)) {
95 ao2_ref(profile, -1);
96 return NULL;
97 }
98
99 if (ast_string_field_init(&profile->vcfg_common, 8)) {
100 ao2_ref(profile, -1);
101 return NULL;
102 }
103
104 return profile;
105 }
106
107 struct ao2_container *profile_get_all(void)
108 {
109 return ast_sorcery_retrieve_by_fields(get_sorcery(), CONFIG_TYPE,
110 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
111 }
112
113 struct profile_cfg *profile_get_cfg(const char *id)
114 {
115 if (ast_strlen_zero(id)) {
116 return NULL;
117 }
118 return ast_sorcery_retrieve_by_id(get_sorcery(), CONFIG_TYPE, id);
119 }
120
121 struct ao2_container *eprofile_get_all(void)
122 {
123 return ast_sorcery_retrieve_by_fields(get_sorcery(), "eprofile",
124 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
125 }
126
127 struct profile_cfg *eprofile_get_cfg(const char *id)
128 {
129 if (ast_strlen_zero(id)) {
130 return NULL;
131 }
132 return ast_sorcery_retrieve_by_id(get_sorcery(), "eprofile", id);
133 }
134
135 static struct profile_cfg *create_effective_profile(
136 struct profile_cfg *base_profile)
137 {
138 struct profile_cfg *eprofile;
139 struct profile_cfg *existing_eprofile;
140 RAII_VAR(struct attestation_cfg*, acfg, as_get_cfg(), ao2_cleanup);
141 RAII_VAR(struct verification_cfg*, vcfg, vs_get_cfg(), ao2_cleanup);
142 const char *id = ast_sorcery_object_get_id(base_profile);
143 int rc = 0;
144
145 eprofile = ast_sorcery_alloc(get_sorcery(), "eprofile", id);
146 if (!eprofile) {
147 ast_log(LOG_ERROR, "%s: Unable to allocate memory for effective profile\n", id);
148 return NULL;
149 }
150
151 rc = vs_copy_cfg_common(id, &eprofile->vcfg_common,
152 &vcfg->vcfg_common);
153 if (rc != 0) {
154 ao2_cleanup(eprofile);
155 return NULL;
156 }
157
158 rc = vs_copy_cfg_common(id, &eprofile->vcfg_common,
159 &base_profile->vcfg_common);
160 if (rc != 0) {
161 ao2_cleanup(eprofile);
162 return NULL;
163 }
164
165 rc = as_copy_cfg_common(id, &eprofile->acfg_common,
166 &acfg->acfg_common);
167 if (rc != 0) {
168 ao2_cleanup(eprofile);
169 return NULL;
170 }
171
172 cfg_enum_copy_ex(eprofile, acfg, unknown_tn_attest_level,
173 attest_level_NOT_SET, attest_level_UNKNOWN);
174
175 rc = as_copy_cfg_common(id, &eprofile->acfg_common,
176 &base_profile->acfg_common);
177 if (rc != 0) {
178 ao2_cleanup(eprofile);
179 return NULL;
180 }
181
182 cfg_enum_copy_ex(eprofile, base_profile, unknown_tn_attest_level,
183 attest_level_NOT_SET, attest_level_UNKNOWN);
184
185
186 eprofile->endpoint_behavior = base_profile->endpoint_behavior;
187
188 if (eprofile->endpoint_behavior == endpoint_behavior_ON) {
189 if (acfg->global_disable && vcfg->global_disable) {
190 eprofile->endpoint_behavior = endpoint_behavior_OFF;
191 } else if (acfg->global_disable && !vcfg->global_disable) {
192 eprofile->endpoint_behavior = endpoint_behavior_VERIFY;
193 } else if (!acfg->global_disable && vcfg->global_disable) {
194 eprofile->endpoint_behavior = endpoint_behavior_ATTEST;
195 }
196 } else if (eprofile->endpoint_behavior == endpoint_behavior_ATTEST
197 && acfg->global_disable) {
198 eprofile->endpoint_behavior = endpoint_behavior_OFF;
199 } else if (eprofile->endpoint_behavior == endpoint_behavior_VERIFY
200 && vcfg->global_disable) {
201 eprofile->endpoint_behavior = endpoint_behavior_OFF;
202 }
203
204 existing_eprofile = ast_sorcery_retrieve_by_id(get_sorcery(), "eprofile", id);
205 if (existing_eprofile) {
206 ao2_cleanup(existing_eprofile);
207 ast_sorcery_update(get_sorcery(), eprofile);
208 } else {
209 ast_sorcery_create(get_sorcery(), eprofile);
210 }
211
212 /*
213 * This triggers eprofile_apply. We _could_ just call
214 * eprofile_apply directly but this seems more keeping
215 * with how sorcery works.
216 */
217 ast_sorcery_objectset_apply(get_sorcery(), eprofile, NULL);
218
219 return eprofile;
220 }
221
222 static int profile_apply(const struct ast_sorcery *sorcery, void *obj)
223 {
224 struct profile_cfg *cfg = obj;
225 const char *id = ast_sorcery_object_get_id(cfg);
226
227 if (PROFILE_ALLOW_ATTEST(cfg)
228 && as_check_common_config(id, &cfg->acfg_common) != 0) {
229 return -1;
230 }
231
232 if (PROFILE_ALLOW_VERIFY(cfg)
233 && vs_check_common_config(id, &cfg->vcfg_common) !=0) {
234 return -1;
235 }
236
237 cfg->eprofile = create_effective_profile(cfg);
238 if (!cfg->eprofile) {
239 return -1;
240 }
241
242 return 0;
243 }
244
245 static int eprofile_apply(const struct ast_sorcery *sorcery, void *obj)
246 {
247 struct profile_cfg *cfg = obj;
248 const char *id = ast_sorcery_object_get_id(cfg);
249
250 if (PROFILE_ALLOW_VERIFY(cfg) && !cfg->vcfg_common.tcs) {
251 ast_log(LOG_ERROR, "%s: Neither this profile nor default"
252 " verification options specify ca_file or ca_path\n", id);
253 return -1;
254 }
255
256 return 0;
257 }
258 generate_acfg_common_sorcery_handlers(profile_cfg);
259 generate_vcfg_common_sorcery_handlers(profile_cfg);
260
261 generate_sorcery_enum_from_str(profile_cfg, , endpoint_behavior, UNKNOWN);
262 generate_sorcery_enum_to_str(profile_cfg, , endpoint_behavior);
263
264 generate_sorcery_enum_from_str_ex(profile_cfg,,unknown_tn_attest_level, attest_level, UNKNOWN);
265 generate_sorcery_enum_to_str_ex(profile_cfg,,unknown_tn_attest_level, attest_level);
266
267 static char *cli_profile_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
268 {
269 struct profile_cfg *profile;
270 struct config_object_cli_data data = {
271 .title = "Profile",
272 .object_type = config_object_type_profile,
273 };
274
275 switch(cmd) {
276 case CLI_INIT:
277 e->command = "stir_shaken show profile";
278 e->usage =
279 "Usage: stir_shaken show profile <id>\n"
280 " Show the stir/shaken profile settings for a given id\n";
281 return NULL;
282 case CLI_GENERATE:
283 if (a->pos == 3) {
284 return config_object_tab_complete_name(a->word, profile_get_all());
285 } else {
286 return NULL;
287 }
288 }
289
290 if (a->argc != 4) {
291 return CLI_SHOWUSAGE;
292 }
293
294 profile = profile_get_cfg(a->argv[3]);
295 if (!profile) {
296 ast_log(LOG_ERROR,"Profile %s doesn't exist\n", a->argv[3]);
297 return CLI_FAILURE;
298 }
299 config_object_cli_show(profile, a, &data, 0);
300
301 ao2_cleanup(profile);
302
303 return CLI_SUCCESS;
304 }
305
306 static char *cli_profile_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
307 {
308 struct ao2_container *container;
309 struct config_object_cli_data data = {
310 .title = "Profile",
311 .object_type = config_object_type_profile,
312 };
313
314 switch(cmd) {
315 case CLI_INIT:
316 e->command = "stir_shaken show profiles";
317 e->usage =
318 "Usage: stir_shaken show profiles\n"
319 " Show all profiles for stir/shaken\n";
320 return NULL;
321 case CLI_GENERATE:
322 return NULL;
323 }
324
325 if (a->argc != 3) {
326 return CLI_SHOWUSAGE;
327 }
328
329 container = profile_get_all();
330 if (!container || ao2_container_count(container) == 0) {
331 ast_cli(a->fd, "No stir/shaken profiles found\n");
332 ao2_cleanup(container);
333 return CLI_SUCCESS;
334 }
335
336 ao2_callback_data(container, OBJ_NODATA, config_object_cli_show, a, &data);
337 ao2_ref(container, -1);
338
339 return CLI_SUCCESS;
340 }
341
342 static char *cli_eprofile_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
343 {
344 struct profile_cfg *profile;
345 struct config_object_cli_data data = {
346 .title = "Effective Profile",
347 .object_type = config_object_type_profile,
348 };
349
350 switch(cmd) {
351 case CLI_INIT:
352 e->command = "stir_shaken show eprofile";
353 e->usage =
354 "Usage: stir_shaken show eprofile <id>\n"
355 " Show the stir/shaken eprofile settings for a given id\n";
356 return NULL;
357 case CLI_GENERATE:
358 if (a->pos == 3) {
359 return config_object_tab_complete_name(a->word, eprofile_get_all());
360 } else {
361 return NULL;
362 }
363 }
364
365 if (a->argc != 4) {
366 return CLI_SHOWUSAGE;
367 }
368
369 profile = eprofile_get_cfg(a->argv[3]);
370 if (!profile) {
371 ast_log(LOG_ERROR,"Effective Profile %s doesn't exist\n", a->argv[3]);
372 return CLI_FAILURE;
373 }
374 config_object_cli_show(profile, a, &data, 0);
375
376 ao2_cleanup(profile);
377
378 return CLI_SUCCESS;
379 }
380
381 static char *cli_eprofile_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
382 {
383 struct ao2_container *container;
384 struct config_object_cli_data data = {
385 .title = "Effective Profile",
386 .object_type = config_object_type_profile,
387 };
388
389 switch(cmd) {
390 case CLI_INIT:
391 e->command = "stir_shaken show eprofiles";
392 e->usage =
393 "Usage: stir_shaken show eprofiles\n"
394 " Show all eprofiles for stir/shaken\n";
395 return NULL;
396 case CLI_GENERATE:
397 return NULL;
398 }
399
400 if (a->argc != 3) {
401 return CLI_SHOWUSAGE;
402 }
403
404 container = eprofile_get_all();
405 if (!container || ao2_container_count(container) == 0) {
406 ast_cli(a->fd, "No stir/shaken eprofiles found\n");
407 ao2_cleanup(container);
408 return CLI_SUCCESS;
409 }
410
411 ao2_callback_data(container, OBJ_NODATA, config_object_cli_show, a, &data);
412 ao2_ref(container, -1);
413
414 return CLI_SUCCESS;
415 }
416
417 static struct ast_cli_entry stir_shaken_profile_cli[] = {
418 AST_CLI_DEFINE(cli_profile_show, "Show stir/shaken profile by id"),
419 AST_CLI_DEFINE(cli_profile_show_all, "Show all stir/shaken profiles"),
420 AST_CLI_DEFINE(cli_eprofile_show, "Show stir/shaken eprofile by id"),
421 AST_CLI_DEFINE(cli_eprofile_show_all, "Show all stir/shaken eprofiles"),
422 };
423
424 int profile_reload(void)
425 {
426 struct ast_sorcery *sorcery = get_sorcery();
427 ast_sorcery_force_reload_object(sorcery, CONFIG_TYPE);
428 ast_sorcery_force_reload_object(sorcery, "eprofile");
429 return 0;
430 }
431
432 int profile_unload(void)
433 {
434 ast_cli_unregister_multiple(stir_shaken_profile_cli,
435 ARRAY_LEN(stir_shaken_profile_cli));
436
437 return 0;
438 }
439
440 int profile_load(void)
441 {
442 struct ast_sorcery *sorcery = get_sorcery();
443 enum ast_sorcery_apply_result apply_rc;
444
445 /*
446 * eprofile MUST be registered first because profile needs it.
447 */
448 apply_rc = ast_sorcery_apply_default(sorcery, "eprofile", "memory", NULL);
449 if (apply_rc != AST_SORCERY_APPLY_SUCCESS) {
450 abort();
451 }
452 if (ast_sorcery_internal_object_register(sorcery, "eprofile",
453 profile_alloc, NULL, eprofile_apply)) {
454 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", "eprofile");
455 return -1;
456 }
457
458 ast_sorcery_object_field_register_nodoc(sorcery, "eprofile", "type", "", OPT_NOOP_T, 0, 0);
459 enum_option_register(sorcery, "eprofile", endpoint_behavior, _nodoc);
460 enum_option_register_ex(sorcery, "eprofile", unknown_tn_attest_level,
461 unknown_tn_attest_level, attest_level,_nodoc);
462
463 register_common_verification_fields(sorcery, profile_cfg, "eprofile", _nodoc);
464 register_common_attestation_fields(sorcery, profile_cfg, "eprofile", _nodoc);
465
466 /*
467 * Now we can do profile
468 */
469 ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config", "stir_shaken.conf,criteria=type=profile");
470 if (ast_sorcery_object_register(sorcery, CONFIG_TYPE, profile_alloc,
471 NULL, profile_apply)) {
472 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
473 return -1;
474 }
475
476 ast_sorcery_object_field_register(sorcery, CONFIG_TYPE, "type", "", OPT_NOOP_T, 0, 0);
477 enum_option_register(sorcery, CONFIG_TYPE, endpoint_behavior,);
478 enum_option_register_ex(sorcery, CONFIG_TYPE, unknown_tn_attest_level,
479 unknown_tn_attest_level, attest_level,);
480
481 register_common_verification_fields(sorcery, profile_cfg, CONFIG_TYPE,);
482 register_common_attestation_fields(sorcery, profile_cfg, CONFIG_TYPE,);
483
484 ast_sorcery_load_object(sorcery, CONFIG_TYPE);
485 ast_sorcery_load_object(sorcery, "eprofile");
486
487 ast_cli_register_multiple(stir_shaken_profile_cli,
488 ARRAY_LEN(stir_shaken_profile_cli));
489
490 return 0;
491 }