Debian Codegraph @

res/res_stir_shaken/attestation_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_ "ac",__LINE__, ""
20
21 #include "asterisk.h"
22
23 #include "asterisk/cli.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/sorcery.h"
26 #include "asterisk/paths.h"
27
28 #include "stir_shaken.h"
29
30 #define CONFIG_TYPE "attestation"
31
32 #define DEFAULT_global_disable 0
33
34 #define DEFAULT_check_tn_cert_public_url check_tn_cert_public_url_NO
35 #define DEFAULT_private_key_file NULL
36 #define DEFAULT_public_cert_url NULL
37 #define DEFAULT_attest_level attest_level_NOT_SET
38 #define DEFAULT_unknown_tn_attest_level attest_level_NOT_SET
39 #define DEFAULT_send_mky send_mky_NO
40
41 static struct attestation_cfg *empty_cfg = NULL;
42
43 struct attestation_cfg *as_get_cfg(void)
44 {
45 struct attestation_cfg *cfg = ast_sorcery_retrieve_by_id(get_sorcery(),
46 CONFIG_TYPE, CONFIG_TYPE);
47 if (cfg) {
48 return cfg;
49 }
50
51 return empty_cfg ? ao2_bump(empty_cfg) : NULL;
52 }
53
54 int as_is_config_loaded(void)
55 {
56 struct attestation_cfg *cfg = ast_sorcery_retrieve_by_id(get_sorcery(),
57 CONFIG_TYPE, CONFIG_TYPE);
58 ao2_cleanup(cfg);
59
60 return !!cfg;
61 }
62
63 generate_acfg_common_sorcery_handlers(attestation_cfg);
64
65 generate_sorcery_enum_from_str_ex(attestation_cfg,,unknown_tn_attest_level, attest_level, UNKNOWN);
66 generate_sorcery_enum_to_str_ex(attestation_cfg,,unknown_tn_attest_level, attest_level);
67
68 void acfg_cleanup(struct attestation_cfg_common *acfg_common)
69 {
70 if (!acfg_common) {
71 return;
72 }
73 ast_string_field_free_memory(acfg_common);
74 ao2_cleanup(acfg_common->raw_key);
75 }
76
77 static void attestation_destructor(void *obj)
78 {
79 struct attestation_cfg *cfg = obj;
80
81 ast_string_field_free_memory(cfg);
82 acfg_cleanup(&cfg->acfg_common);
83 }
84
85 static void *attestation_alloc(const char *name)
86 {
87 struct attestation_cfg *cfg;
88
89 cfg = ast_sorcery_generic_alloc(sizeof(*cfg), attestation_destructor);
90 if (!cfg) {
91 return NULL;
92 }
93
94 if (ast_string_field_init(cfg, 1024)) {
95 ao2_ref(cfg, -1);
96 return NULL;
97 }
98
99 /*
100 * The memory for acfg_common actually comes from cfg
101 * due to the weirdness of the STRFLDSET macro used with
102 * sorcery. We just use a token amount of memory in
103 * this call so the initialize doesn't fail.
104 */
105 if (ast_string_field_init(&cfg->acfg_common, 8)) {
106 ao2_ref(cfg, -1);
107 return NULL;
108 }
109
110 return cfg;
111 }
112
113 int as_copy_cfg_common(const char *id, struct attestation_cfg_common *cfg_dst,
114 struct attestation_cfg_common *cfg_src)
115 {
116 int rc = 0;
117
118 if (!cfg_dst || !cfg_src) {
119 return -1;
120 }
121
122 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, private_key_file);
123 cfg_sf_copy_wrapper(id, cfg_dst, cfg_src, public_cert_url);
124
125 cfg_enum_copy(cfg_dst, cfg_src, attest_level);
126 cfg_enum_copy(cfg_dst, cfg_src, check_tn_cert_public_url);
127 cfg_enum_copy(cfg_dst, cfg_src, send_mky);
128
129 if (cfg_src->raw_key) {
130 /* Free and overwrite the destination */
131 ao2_cleanup(cfg_dst->raw_key);
132 cfg_dst->raw_key = ao2_bump(cfg_src->raw_key);
133 cfg_dst->raw_key_length = cfg_src->raw_key_length;
134 }
135
136 return rc;
137 }
138
139 int as_check_common_config(const char *id, struct attestation_cfg_common *acfg_common)
140 {
141 SCOPE_ENTER(3, "%s: Checking common config\n", id);
142
143 if (!ast_strlen_zero(acfg_common->private_key_file)
144 && !ast_file_is_readable(acfg_common->private_key_file)) {
145 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: default_private_key_path %s is missing or not readable\n", id,
146 acfg_common->private_key_file);
147 }
148
149 if (ENUM_BOOL(acfg_common->check_tn_cert_public_url,
150 check_tn_cert_public_url)
151 && !ast_strlen_zero(acfg_common->public_cert_url)) {
152 RAII_VAR(char *, public_cert_data, NULL, ast_std_free);
153 X509 *public_cert;
154 size_t public_cert_len;
155 int rc = 0;
156 long http_code;
157 SCOPE_ENTER(3 , "%s: Checking public cert url '%s'\n",
158 id, acfg_common->public_cert_url);
159
160 http_code = curl_download_to_memory(acfg_common->public_cert_url,
161 &public_cert_len, &public_cert_data, NULL);
162 if (http_code / 100 != 2) {
163 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: public_cert '%s' could not be downloaded\n", id,
164 acfg_common->public_cert_url);
165 }
166
167 public_cert = crypto_load_cert_chain_from_memory(public_cert_data,
168 public_cert_len, NULL);
169 if (!public_cert) {
170 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: public_cert '%s' could not be parsed as a certificate\n", id,
171 acfg_common->public_cert_url);
172 }
173 rc = crypto_is_cert_time_valid(public_cert, 0);
174 X509_free(public_cert);
175 if (!rc) {
176 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: public_cert '%s' is not valid yet or has expired\n", id,
177 acfg_common->public_cert_url);
178 }
179
180 rc = crypto_has_private_key_from_memory(public_cert_data, public_cert_len);
181 if (rc) {
182 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: DANGER!!! public_cert_url '%s' has a private key in the file!!!\n", id,
183 acfg_common->public_cert_url);
184 }
185 SCOPE_EXIT("%s: Done\n", id);
186 }
187
188 if (!ast_strlen_zero(acfg_common->private_key_file)) {
189 EVP_PKEY *private_key;
190 RAII_VAR(unsigned char *, raw_key, NULL, ast_free);
191
192 private_key = crypto_load_privkey_from_file(acfg_common->private_key_file);
193 if (!private_key) {
194 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Could not extract raw private key from file '%s'\n", id,
195 acfg_common->private_key_file);
196 }
197
198 acfg_common->raw_key_length = crypto_extract_raw_privkey(private_key, &raw_key);
199 EVP_PKEY_free(private_key);
200 if (acfg_common->raw_key_length == 0 || raw_key == NULL) {
201 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Could not extract raw private key from file '%s'\n", id,
202 acfg_common->private_key_file);
203 }
204
205 /*
206 * We're making this an ao2 object so it can be referenced
207 * by a profile instead of having to copy it.
208 */
209 acfg_common->raw_key = ao2_alloc(acfg_common->raw_key_length, NULL);
210 if (!acfg_common->raw_key) {
211 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR,
212 "%s: Could not allocate memory for raw private key\n", id);
213 }
214 memcpy(acfg_common->raw_key, raw_key, acfg_common->raw_key_length);
215
216 }
217
218 SCOPE_EXIT_RTN_VALUE(0, "%s: Done\n", id);
219 }
220
221 static int attestation_apply(const struct ast_sorcery *sorcery, void *obj)
222 {
223 struct attestation_cfg *cfg = obj;
224 const char *id = ast_sorcery_object_get_id(cfg);
225
226 if (as_check_common_config(id, &cfg->acfg_common) != 0) {
227 return -1;
228 }
229
230 return 0;
231 }
232
233 static char *attestation_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
234 {
235 struct attestation_cfg *cfg;
236 struct config_object_cli_data data = {
237 .title = "Default Attestation",
238 .object_type = config_object_type_attestation,
239 };
240
241 switch(cmd) {
242 case CLI_INIT:
243 e->command = "stir_shaken show attestation";
244 e->usage =
245 "Usage: stir_shaken show attestation\n"
246 " Show the stir/shaken attestation settings\n";
247 return NULL;
248 case CLI_GENERATE:
249 return NULL;
250 }
251
252 if (a->argc != 3) {
253 return CLI_SHOWUSAGE;
254 }
255
256 if (!as_is_config_loaded()) {
257 ast_log(LOG_WARNING,"Stir/Shaken attestation service disabled. Either there were errors in the 'attestation' object in stir_shaken.conf or it was missing altogether.\n");
258 return CLI_FAILURE;
259 }
260
261 cfg = as_get_cfg();
262 config_object_cli_show(cfg, a, &data, 0);
263 ao2_cleanup(cfg);
264
265 return CLI_SUCCESS;
266 }
267
268 static struct ast_cli_entry attestation_cli[] = {
269 AST_CLI_DEFINE(attestation_show, "Show stir/shaken attestation configuration"),
270 };
271
272 int as_config_reload(void)
273 {
274 struct ast_sorcery *sorcery = get_sorcery();
275 ast_sorcery_force_reload_object(sorcery, CONFIG_TYPE);
276
277 if (!as_is_config_loaded()) {
278 ast_log(LOG_WARNING,"Stir/Shaken attestation service disabled. Either there were errors in the 'attestation' object in stir_shaken.conf or it was missing altogether.\n");
279 }
280 if (!empty_cfg) {
281 empty_cfg = attestation_alloc(CONFIG_TYPE);
282 if (!empty_cfg) {
283 return -1;
284 }
285 empty_cfg->global_disable = 1;
286 }
287
288 return 0;
289 }
290
291 int as_config_unload(void)
292 {
293 ast_cli_unregister_multiple(attestation_cli,
294 ARRAY_LEN(attestation_cli));
295 ao2_cleanup(empty_cfg);
296
297 return 0;
298 }
299
300 int as_config_load(void)
301 {
302 struct ast_sorcery *sorcery = get_sorcery();
303
304 ast_sorcery_apply_default(sorcery, CONFIG_TYPE, "config",
305 "stir_shaken.conf,criteria=type=" CONFIG_TYPE ",single_object=yes,explicit_name=" CONFIG_TYPE);
306
307 if (ast_sorcery_object_register(sorcery, CONFIG_TYPE, attestation_alloc,
308 NULL, attestation_apply)) {
309 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
310 return -1;
311 }
312
313 ast_sorcery_object_field_register_nodoc(sorcery, CONFIG_TYPE, "type",
314 "", OPT_NOOP_T, 0, 0);
315
316 ast_sorcery_object_field_register(sorcery, CONFIG_TYPE, "global_disable",
317 DEFAULT_global_disable ? "yes" : "no",
318 OPT_YESNO_T, 1, FLDSET(struct attestation_cfg, global_disable));
319
320 enum_option_register_ex(sorcery, CONFIG_TYPE, unknown_tn_attest_level,
321 unknown_tn_attest_level, attest_level,);
322
323 register_common_attestation_fields(sorcery, attestation_cfg, CONFIG_TYPE,);
324
325 ast_sorcery_load_object(sorcery, CONFIG_TYPE);
326
327 if (!as_is_config_loaded()) {
328 ast_log(LOG_WARNING,"Stir/Shaken attestation service disabled. Either there were errors in the 'attestation' object in stir_shaken.conf or it was missing altogether.\n");
329 }
330 if (!empty_cfg) {
331 empty_cfg = attestation_alloc(CONFIG_TYPE);
332 if (!empty_cfg) {
333 return -1;
334 }
335 empty_cfg->global_disable = 1;
336 }
337
338 ast_cli_register_multiple(attestation_cli,
339 ARRAY_LEN(attestation_cli));
340
341 return 0;
342 }