Debian Codegraph @

res/res_stir_shaken/verification_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 #define _TRACE_PREFIX_ "vc",__LINE__, ""
20
21 #include "asterisk.h"
22
23 #include "asterisk/cli.h"
24 #include "asterisk/logger.h"
25 #include "stir_shaken.h"
26
27 #define CONFIG_TYPE "verification"
28
29 #define DEFAULT_global_disable 0
30
31 #define DEFAULT_ca_file NULL
32 #define DEFAULT_ca_path NULL
33 #define DEFAULT_crl_file NULL
34 #define DEFAULT_crl_path NULL
35 #define DEFAULT_untrusted_cert_file NULL
36 #define DEFAULT_untrusted_cert_path NULL
37 static char DEFAULT_cert_cache_dir[PATH_MAX];
38
39 #define DEFAULT_curl_timeout 2
40 #define DEFAULT_max_iat_age 15
41 #define DEFAULT_max_date_header_age 15
42 #define DEFAULT_max_cache_entry_age 3600
43 #define DEFAULT_max_cache_size 1000
44 #define DEFAULT_stir_shaken_failure_action stir_shaken_failure_action_CONTINUE
45 #define DEFAULT_use_rfc9410_responses use_rfc9410_responses_NO
46 #define DEFAULT_relax_x5u_port_scheme_restrictions relax_x5u_port_scheme_restrictions_NO
47 #define DEFAULT_relax_x5u_path_restrictions relax_x5u_path_restrictions_NO
48 #define DEFAULT_load_system_certs load_system_certs_NO
49 #define DEFAULT_ignore_sip_date_header ignore_sip_date_header_NO
50
51 static struct verification_cfg *empty_cfg = NULL;
52
53 #define STIR_SHAKEN_DIR_NAME "stir_shaken"
54
55 struct verification_cfg *vs_get_cfg(void)
56 {
57 struct verification_cfg *cfg = ast_sorcery_retrieve_by_id(get_sorcery(),
58 CONFIG_TYPE, CONFIG_TYPE);
59 if (cfg) {
60 return cfg;
61 }
62
63 return empty_cfg ? ao2_bump(empty_cfg) : NULL;
64 }
65
66 int vs_is_config_loaded(void)
67 {
68 struct verification_cfg *cfg = ast_sorcery_retrieve_by_id(get_sorcery(),
69 CONFIG_TYPE, CONFIG_TYPE);
70 ao2_cleanup(cfg);
71
72 return !!cfg;
73 }
74
75 generate_vcfg_common_sorcery_handlers(verification_cfg);
76
77 void vcfg_cleanup(struct verification_cfg_common *vcfg_common)
78 {
79 if (!vcfg_common) {
80 return;
81 }
82 ast_string_field_free_memory(vcfg_common);
83 if (vcfg_common->tcs) {
84 crypto_free_cert_store(vcfg_common->tcs);
85 }
86 ast_free_acl_list(vcfg_common->acl);
87 }
88
89 static void verification_destructor(void *obj)
90 {
91 struct verification_cfg *cfg = obj;
92 ast_string_field_free_memory(cfg);
93 vcfg_cleanup(&cfg->vcfg_common);
94 }
95
96 static void *verification_alloc(const char *name)
97 {
98 struct verification_cfg *cfg;
99
100 cfg = ast_sorcery_generic_alloc(sizeof(*cfg), verification_destructor);
101 if (!cfg) {
102 return NULL;
103 }
104
105 if (ast_string_field_init(cfg, 1024)) {
106 ao2_ref(cfg, -1);
107 return NULL;
108 }
109
110 /*
111 * The memory for vcfg_common actually comes from cfg
112 * due to the weirdness of the STRFLDSET macro used with
113 * sorcery. We just use a token amount of memory in
114 * this call so the initialize doesn't fail.
115 */
116 if (ast_string_field_init(&cfg->vcfg_common, 8)) {
117 ao2_ref(cfg, -1);
118 return NULL;
119 }
120
121 return cfg;
122 }
123
124 int vs_copy_cfg_common(const char *id, struct verification_cfg_common *cfg_dst,
125 struct verification_cfg_common *cfg_src)
126 {
127 int rc = 0;
128
129 if (!cfg_dst || !cfg_src) {
130 return -1;
131 }
132
133 if (!cfg_dst->tcs && cfg_src->tcs) {
134 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, ca_file);
135 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, ca_path);
136 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, crl_file);
137 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, crl_path);
138 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, untrusted_cert_file);
139 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, untrusted_cert_path);
140 ao2_bump(cfg_src->tcs);
141 cfg_dst->tcs = cfg_src->tcs;
142 }
143
144 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, cert_cache_dir);
145
146 cfg_uint_copy(cfg_dst, cfg_src, curl_timeout);
147 cfg_uint_copy(cfg_dst, cfg_src, max_iat_age);
148 cfg_uint_copy(cfg_dst, cfg_src, max_date_header_age);
149 cfg_uint_copy(cfg_dst, cfg_src, max_cache_entry_age);
150 cfg_uint_copy(cfg_dst, cfg_src, max_cache_size);
151
152 cfg_enum_copy(cfg_dst, cfg_src, stir_shaken_failure_action);
153 cfg_enum_copy(cfg_dst, cfg_src, use_rfc9410_responses);
154 cfg_enum_copy(cfg_dst, cfg_src, relax_x5u_port_scheme_restrictions);
155 cfg_enum_copy(cfg_dst, cfg_src, relax_x5u_path_restrictions);
156 cfg_enum_copy(cfg_dst, cfg_src, load_system_certs);
157 cfg_enum_copy(cfg_dst, cfg_src, ignore_sip_date_header);
158
159 if (cfg_src->acl) {
160 ast_free_acl_list(cfg_dst->acl);
161 cfg_dst->acl = ast_duplicate_acl_list(cfg_src->acl);
162 }
163
164 return rc;
165 }
166
167 int vs_check_common_config(const char *id,
168 struct verification_cfg_common *vcfg_common)
169 {
170 SCOPE_ENTER(3, "%s: Checking common config\n", id);
171
172 if (!ast_strlen_zero(vcfg_common->ca_file)
173 && !ast_file_is_readable(vcfg_common->ca_file)) {
174 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
175 "%s: ca_file '%s' not found, or is unreadable\n",
176 id, vcfg_common->ca_file);
177 }
178
179 if (!ast_strlen_zero(vcfg_common->ca_path)
180 && !ast_file_is_readable(vcfg_common->ca_path)) {
181 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
182 "%s: ca_path '%s' not found, or is unreadable\n",
183 id, vcfg_common->ca_path);
184 }
185
186 if (!ast_strlen_zero(vcfg_common->crl_file)
187 && !ast_file_is_readable(vcfg_common->crl_file)) {
188 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
189 "%s: crl_file '%s' not found, or is unreadable\n",
190 id, vcfg_common->crl_file);
191 }
192
193 if (!ast_strlen_zero(vcfg_common->crl_path)
194 && !ast_file_is_readable(vcfg_common->crl_path)) {
195 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
196 "%s: crl_path '%s' not found, or is unreadable\n",
197 id, vcfg_common->crl_path);
198 }
199
200 if (!ast_strlen_zero(vcfg_common->untrusted_cert_file)
201 && !ast_file_is_readable(vcfg_common->untrusted_cert_file)) {
202 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
203 "%s: untrusted_cert_file '%s' not found, or is unreadable\n",
204 id, vcfg_common->untrusted_cert_file);
205 }
206
207 if (!ast_strlen_zero(vcfg_common->untrusted_cert_path)
208 && !ast_file_is_readable(vcfg_common->untrusted_cert_path)) {
209 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
210 "%s: untrusted_cert_path '%s' not found, or is unreadable\n",
211 id, vcfg_common->untrusted_cert_path);
212 }
213
214 if (!ast_strlen_zero(vcfg_common->ca_file)
215 || !ast_strlen_zero(vcfg_common->ca_path)) {
216 int rc = 0;
217
218 if (!vcfg_common->tcs) {
219 vcfg_common->tcs = crypto_create_cert_store();
220 if (!vcfg_common->tcs) {
221 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
222 "%s: Unable to create CA cert store\n", id);
223 }
224 }
225 rc = crypto_load_cert_store(vcfg_common->tcs,
226 vcfg_common->ca_file, vcfg_common->ca_path);
227 if (rc != 0) {
228 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
229 "%s: Unable to load CA cert store from '%s' or '%s'\n",
230 id, vcfg_common->ca_file, vcfg_common->ca_path);
231 }
232 }
233
234 if (!ast_strlen_zero(vcfg_common->crl_file)
235 || !ast_strlen_zero(vcfg_common->crl_path)) {
236 int rc = 0;
237
238 if (!vcfg_common->tcs) {
239 vcfg_common->tcs = crypto_create_cert_store();
240 if (!vcfg_common->tcs) {
241 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
242 "%s: Unable to create CA cert store\n", id);
243 }
244 }
245 rc = crypto_load_crl_store(vcfg_common->tcs,
246 vcfg_common->crl_file, vcfg_common->crl_path);
247 if (rc != 0) {
248 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
249 "%s: Unable to load CA CRL store from '%s' or '%s'\n",
250 id, vcfg_common->crl_file, vcfg_common->crl_path);
251 }
252 }
253
254 if (!ast_strlen_zero(vcfg_common->untrusted_cert_file)
255 || !ast_strlen_zero(vcfg_common->untrusted_cert_path)) {
256 int rc = 0;
257
258 if (!vcfg_common->tcs) {
259 vcfg_common->tcs = crypto_create_cert_store();
260 if (!vcfg_common->tcs) {
261 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
262 "%s: Unable to create CA cert store\n", id);
263 }
264 }
265 rc = crypto_load_untrusted_cert_store(vcfg_common->tcs,
266 vcfg_common->untrusted_cert_file, vcfg_common->untrusted_cert_path);
267 if (rc != 0) {
268 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
269 "%s: Unable to load CA CRL store from '%s' or '%s'\n",
270 id, vcfg_common->untrusted_cert_file, vcfg_common->untrusted_cert_path);
271 }
272 }
273
274 if (vcfg_common->tcs) {
275 if (ENUM_BOOL(vcfg_common->load_system_certs, load_system_certs)) {
276 X509_STORE_set_default_paths(vcfg_common->tcs->certs);
277 }
278
279 if (!ast_strlen_zero(vcfg_common->crl_file)
280 || !ast_strlen_zero(vcfg_common->crl_path)) {
281 X509_STORE_set_flags(vcfg_common->tcs->certs, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_EXTENDED_CRL_SUPPORT);
282 }
283 }
284
285 if (!ast_strlen_zero(vcfg_common->cert_cache_dir)) {
286 FILE *fp;
287 char *testfile;
288
289 if (ast_asprintf(&testfile, "%s/testfile", vcfg_common->cert_cache_dir) <= 0) {
290 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
291 "%s: Unable to allocate memory for testfile\n", id);
292 }
293
294 fp = fopen(testfile, "w+");
295 if (!fp) {
296 ast_free(testfile);
297 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
298 "%s: cert_cache_dir '%s' was not writable\n",
299 id, vcfg_common->cert_cache_dir);
300 }
301 fclose(fp);
302 remove(testfile);
303 ast_free(testfile);
304 }
305
306 SCOPE_EXIT_RTN_VALUE(0, "%s: Done\n", id);
307 }
308
309 static char *special_addresses[] = {
310 "0.0.0.0/8",
311 "10.0.0.0/8",
312 "100.64.0.0/10",
313 "127.0.0.0/8",
314 "169.254.0.0/16",
315 "172.16.0.0/12",
316 "192.0.0.0/24",
317 "192.0.0.0/29",
318 "192.88.99.0/24",
319 "192.168.0.0/16",
320 "198.18.0.0/15",
321 "198.51.100.0/24",
322 "203.0.113.0/24",
323 "240.0.0.0/4",
324 "255.255.255.255/32",
325 "::1/128",
326 "::/128",
327 /* "64:ff9b::/96", IPv4-IPv6 translation addresses should probably not be blocked by default */
328 /* "::ffff:0:0/96", IPv4 mapped addresses should probably not be blocked by default */
329 "100::/64",
330 "2001::/23",
331 "2001::/32",
332 "2001:2::/48",
333 "2001:db8::/32",
334 "2001:10::/28",
335 /* "2002::/16", 6to4 should problably not be blocked by default */
336 "fc00::/7",
337 "fe80::/10",
338 };
339
340 static int verification_apply(const struct ast_sorcery *sorcery, void *obj)
341 {
342 struct verification_cfg *cfg = obj;
343 const char *id = ast_sorcery_object_get_id(cfg);
344
345 if (vs_check_common_config("verification", &cfg->vcfg_common) !=0) {
346 return -1;
347 }
348
349 if (!cfg->vcfg_common.acl) {
350 int error = 0;
351 int ignore;
352 int i;
353
354 ast_append_acl("permit", "0.0.0.0/0", &cfg->vcfg_common.acl, &error, &ignore);
355 if (error) {
356 ast_free_acl_list(cfg->vcfg_common.acl);
357 cfg->vcfg_common.acl = NULL;
358 ast_log(LOG_ERROR, "%s: Unable to create default acl rule for '%s: %s'\n",
359 id, "permit", "0.0.0.0/0");
360 return -1;
361 }
362
363 for (i = 0; i < ARRAY_LEN(special_addresses); i++) {
364 ast_append_acl("deny", special_addresses[i], &cfg->vcfg_common.acl, &error, &ignore);
365 if (error) {
366 ast_free_acl_list(cfg->vcfg_common.acl);
367 cfg->vcfg_common.acl = NULL;
368 ast_log(LOG_ERROR, "%s: Unable to create default acl rule for '%s: %s'\n",
369 id, "deny", special_addresses[i]);
370 return -1;
371 }
372 }
373 }
374
375 return 0;
376 }
377
378 static char *cli_verification_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
379 {
380 struct verification_cfg *cfg;
381 struct config_object_cli_data data = {
382 .title = "Default Verification",
383 .object_type = config_object_type_verification,
384 };
385
386 switch(cmd) {
387 case CLI_INIT:
388 e->command = "stir_shaken show verification";
389 e->usage =
390 "Usage: stir_shaken show verification\n"
391 " Show the stir/shaken verification settings\n";
392 return NULL;
393 case CLI_GENERATE:
394 return NULL;
395 }
396
397 if (a->argc != 3) {
398 return CLI_SHOWUSAGE;
399 }
400
401 if (!vs_is_config_loaded()) {
402 ast_log(LOG_WARNING,"Stir/Shaken verification service disabled. Either there were errors in the 'verification' object in stir_shaken.conf or it was missing altogether.\n");
403 return CLI_FAILURE;
404 }
405
406 cfg = vs_get_cfg();
407 config_object_cli_show(cfg, a, &data, 0);
408
409 ao2_cleanup(cfg);
410
411 return CLI_SUCCESS;
412 }
413
414 static struct ast_cli_entry verification_cli[] = {
415 AST_CLI_DEFINE(cli_verification_show, "Show stir/shaken verification configuration"),
416 };
417
418 int vs_config_reload(void)
419 {
420 struct ast_sorcery *sorcery = get_sorcery();
421 ast_sorcery_force_reload_object(sorcery, CONFIG_TYPE);
422
423 if (!vs_is_config_loaded()) {
424 ast_log(LOG_WARNING,"Stir/Shaken verification service disabled. Either there were errors in the 'verification' object in stir_shaken.conf or it was missing altogether.\n");
425 }
426 if (!empty_cfg) {
427 empty_cfg = verification_alloc(CONFIG_TYPE);
428 if (!empty_cfg) {
429 return -1;
430 }
431 empty_cfg->global_disable = 1;
432 }
433
434 return 0;
435 }
436
437 int vs_config_unload(void)
438 {
439 ast_cli_unregister_multiple(verification_cli,
440 ARRAY_LEN(verification_cli));
441 ao2_cleanup(empty_cfg);
442
443 return 0;
444 }
445
446 int vs_config_load(void)
447 {
448 struct ast_sorcery *sorcery = get_sorcery();
449
450 snprintf(DEFAULT_cert_cache_dir, sizeof(DEFAULT_cert_cache_dir), "%s/keys/%s/cache",
451 ast_config_AST_DATA_DIR, STIR_SHAKEN_DIR_NAME);
452
453 ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config",
454 "stir_shaken.conf,criteria=type=" CONFIG_TYPE ",single_object=yes,explicit_name=" CONFIG_TYPE);
455
456 if (ast_sorcery_object_register(sorcery, CONFIG_TYPE, verification_alloc,
457 NULL, verification_apply)) {
458 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
459 return -1;
460 }
461
462 ast_sorcery_object_field_register_nodoc(sorcery, CONFIG_TYPE, "type", "",
463 OPT_NOOP_T, 0, 0);
464
465 ast_sorcery_object_field_register(sorcery, CONFIG_TYPE, "global_disable",
466 DEFAULT_global_disable ? "yes" : "no",
467 OPT_YESNO_T, 1, FLDSET(struct verification_cfg, global_disable));
468
469 register_common_verification_fields(sorcery, verification_cfg, CONFIG_TYPE,);
470
471 ast_sorcery_load_object(sorcery, CONFIG_TYPE);
472
473 if (!vs_is_config_loaded()) {
474 ast_log(LOG_WARNING,"Stir/Shaken verification service disabled. Either there were errors in the 'verification' object in stir_shaken.conf or it was missing altogether.\n");
475 }
476 if (!empty_cfg) {
477 empty_cfg = verification_alloc(CONFIG_TYPE);
478 if (!empty_cfg) {
479 return -1;
480 }
481 empty_cfg->global_disable = 1;
482 }
483
484 ast_cli_register_multiple(verification_cli,
485 ARRAY_LEN(verification_cli));
486
487 return 0;
488 }