Asterisk - The Open Source Telephony Project GIT-master-8f1982c
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Macros | Functions | Variables
attestation_config.c File Reference
#include "asterisk.h"
#include "asterisk/cli.h"
#include "asterisk/logger.h"
#include "asterisk/sorcery.h"
#include "asterisk/paths.h"
#include "stir_shaken.h"
Include dependency graph for attestation_config.c:

Go to the source code of this file.

Macros

#define _TRACE_PREFIX_   "ac",__LINE__, ""
 
#define CONFIG_TYPE   "attestation"
 
#define DEFAULT_attest_level   attest_level_NOT_SET
 
#define DEFAULT_check_tn_cert_public_url   check_tn_cert_public_url_NO
 
#define DEFAULT_global_disable   0
 
#define DEFAULT_private_key_file   NULL
 
#define DEFAULT_public_cert_url   NULL
 
#define DEFAULT_send_mky   send_mky_NO
 
#define DEFAULT_unknown_tn_attest_level   attest_level_NOT_SET
 

Functions

void acfg_cleanup (struct attestation_cfg_common *acfg_common)
 
int as_check_common_config (const char *id, struct attestation_cfg_common *acfg_common)
 
int as_config_load (void)
 
int as_config_reload (void)
 
int as_config_unload (void)
 
int as_copy_cfg_common (const char *id, struct attestation_cfg_common *cfg_dst, struct attestation_cfg_common *cfg_src)
 
struct attestation_cfgas_get_cfg (void)
 
int as_is_config_loaded (void)
 
static void * attestation_alloc (const char *name)
 
static int attestation_apply (const struct ast_sorcery *sorcery, void *obj)
 
static void attestation_destructor (void *obj)
 
static char * attestation_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
 generate_acfg_common_sorcery_handlers (attestation_cfg)
 
 generate_sorcery_enum_from_str_ex (attestation_cfg,, unknown_tn_attest_level, attest_level, UNKNOWN)
 
 generate_sorcery_enum_to_str_ex (attestation_cfg,, unknown_tn_attest_level, attest_level)
 

Variables

static struct ast_cli_entry attestation_cli []
 
static struct attestation_cfgempty_cfg = NULL
 

Macro Definition Documentation

◆ _TRACE_PREFIX_

#define _TRACE_PREFIX_   "ac",__LINE__, ""

Definition at line 19 of file attestation_config.c.

◆ CONFIG_TYPE

#define CONFIG_TYPE   "attestation"

Definition at line 30 of file attestation_config.c.

◆ DEFAULT_attest_level

#define DEFAULT_attest_level   attest_level_NOT_SET

Definition at line 37 of file attestation_config.c.

◆ DEFAULT_check_tn_cert_public_url

#define DEFAULT_check_tn_cert_public_url   check_tn_cert_public_url_NO

Definition at line 34 of file attestation_config.c.

◆ DEFAULT_global_disable

#define DEFAULT_global_disable   0

Definition at line 32 of file attestation_config.c.

◆ DEFAULT_private_key_file

#define DEFAULT_private_key_file   NULL

Definition at line 35 of file attestation_config.c.

◆ DEFAULT_public_cert_url

#define DEFAULT_public_cert_url   NULL

Definition at line 36 of file attestation_config.c.

◆ DEFAULT_send_mky

#define DEFAULT_send_mky   send_mky_NO

Definition at line 39 of file attestation_config.c.

◆ DEFAULT_unknown_tn_attest_level

#define DEFAULT_unknown_tn_attest_level   attest_level_NOT_SET

Definition at line 38 of file attestation_config.c.

Function Documentation

◆ acfg_cleanup()

void acfg_cleanup ( struct attestation_cfg_common acfg_common)

Definition at line 68 of file attestation_config.c.

69{
70 if (!acfg_common) {
71 return;
72 }
74 ao2_cleanup(acfg_common->raw_key);
75}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374
unsigned char * raw_key

References attestation_cfg::acfg_common, ao2_cleanup, ast_string_field_free_memory, and attestation_cfg_common::raw_key.

Referenced by attestation_destructor(), profile_destructor(), and tn_destructor().

◆ as_check_common_config()

int as_check_common_config ( const char *  id,
struct attestation_cfg_common acfg_common 
)

Definition at line 139 of file attestation_config.c.

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_from_memory(public_cert_data,
168 public_cert_len);
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) {
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}
void ast_std_free(void *ptr)
Definition: astmm.c:1734
#define ast_free(a)
Definition: astmm.h:180
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
#define ENUM_BOOL(__enum1, __field)
X509 * crypto_load_cert_from_memory(const char *buffer, size_t size)
Load an X509 Cert from a NULL terminated buffer.
Definition: crypto_utils.c:213
EVP_PKEY * crypto_load_privkey_from_file(const char *filename)
Load a private key from a file.
Definition: crypto_utils.c:141
int crypto_extract_raw_privkey(EVP_PKEY *key, unsigned char **buffer)
Extract raw private key from EVP_PKEY.
Definition: crypto_utils.c:320
int crypto_is_cert_time_valid(X509 *cert, time_t reftime)
Check if the reftime is within the cert's valid dates.
Definition: crypto_utils.c:721
int crypto_has_private_key_from_memory(const char *buffer, size_t size)
Check if the supplied buffer has a private key.
Definition: crypto_utils.c:266
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
#define SCOPE_ENTER(level,...)
#define SCOPE_EXIT(...)
long curl_download_to_memory(const char *url, size_t *returned_length, char **returned_data, struct ast_variable **headers)
Really simple document retrieval to memory.
Definition: curl_utils.c:300
#define LOG_ERROR
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
enum check_tn_cert_public_url_enum check_tn_cert_public_url
const ast_string_field public_cert_url
const ast_string_field private_key_file
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941
int ast_file_is_readable(const char *filename)
Test that a file exists and is readable by the effective user.
Definition: utils.c:3107

References attestation_cfg::acfg_common, ao2_alloc, ast_file_is_readable(), ast_free, ast_std_free(), ast_strlen_zero(), attestation_cfg_common::check_tn_cert_public_url, crypto_extract_raw_privkey(), crypto_has_private_key_from_memory(), crypto_is_cert_time_valid(), crypto_load_cert_from_memory(), crypto_load_privkey_from_file(), curl_download_to_memory(), ENUM_BOOL, LOG_ERROR, NULL, attestation_cfg_common::private_key_file, attestation_cfg_common::public_cert_url, RAII_VAR, attestation_cfg_common::raw_key, attestation_cfg_common::raw_key_length, SCOPE_ENTER, SCOPE_EXIT, SCOPE_EXIT_LOG_RTN_VALUE, and SCOPE_EXIT_RTN_VALUE.

Referenced by attestation_apply(), profile_apply(), and tn_apply().

◆ as_config_load()

int as_config_load ( void  )

Definition at line 300 of file attestation_config.c.

301{
302 struct ast_sorcery *sorcery = get_sorcery();
303
305 "stir_shaken.conf,criteria=type=" CONFIG_TYPE ",single_object=yes,explicit_name=" CONFIG_TYPE);
306
309 ast_log(LOG_ERROR, "stir/shaken - failed to register '%s' sorcery object\n", CONFIG_TYPE);
310 return -1;
311 }
312
314 "", OPT_NOOP_T, 0, 0);
315
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
324
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) {
332 if (!empty_cfg) {
333 return -1;
334 }
336 }
337
340
341 return 0;
342}
#define ast_log
Definition: astobj2.c:42
static struct ast_cli_entry attestation_cli[]
static struct attestation_cfg * empty_cfg
#define DEFAULT_global_disable
int as_is_config_loaded(void)
static void * attestation_alloc(const char *name)
#define CONFIG_TYPE
static int attestation_apply(const struct ast_sorcery *sorcery, void *obj)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
struct ast_sorcery * get_sorcery(void)
Retrieve the stir/shaken sorcery context.
Definition: common_config.c:34
#define register_common_attestation_fields(sorcery, object, CONFIG_TYPE, nodoc)
#define enum_option_register_ex(sorcery, CONFIG_TYPE, name, field, function_prefix, nodoc)
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
@ OPT_NOOP_T
Type for a default handler that should do nothing.
@ OPT_YESNO_T
Type for default option handler for bools (ast_true/ast_false)
#define LOG_WARNING
static struct ast_sorcery * sorcery
#define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object without documentation.
Definition: sorcery.h:987
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:837
void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to load persistent objects.
Definition: sorcery.c:1393
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:476
Full structure for sorcery.
Definition: sorcery.c:230
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, as_is_config_loaded(), ast_cli_register_multiple, ast_log, ast_sorcery_apply_default, ast_sorcery_load_object(), ast_sorcery_object_field_register, ast_sorcery_object_field_register_nodoc, ast_sorcery_object_register, attestation_alloc(), attestation_apply(), attestation_cli, CONFIG_TYPE, DEFAULT_global_disable, empty_cfg, enum_option_register_ex, FLDSET, get_sorcery(), attestation_cfg::global_disable, LOG_ERROR, LOG_WARNING, NULL, OPT_NOOP_T, OPT_YESNO_T, register_common_attestation_fields, and sorcery.

Referenced by as_load().

◆ as_config_reload()

int as_config_reload ( void  )

Definition at line 272 of file attestation_config.c.

273{
274 struct ast_sorcery *sorcery = get_sorcery();
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) {
282 if (!empty_cfg) {
283 return -1;
284 }
286 }
287
288 return 0;
289}
void ast_sorcery_force_reload_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to reload persistent objects even if no changes determin...
Definition: sorcery.c:1457

References as_is_config_loaded(), ast_log, ast_sorcery_force_reload_object(), attestation_alloc(), CONFIG_TYPE, empty_cfg, get_sorcery(), attestation_cfg::global_disable, LOG_WARNING, and sorcery.

Referenced by as_reload().

◆ as_config_unload()

int as_config_unload ( void  )

Definition at line 291 of file attestation_config.c.

292{
296
297 return 0;
298}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

References ao2_cleanup, ARRAY_LEN, ast_cli_unregister_multiple(), attestation_cli, and empty_cfg.

Referenced by as_unload().

◆ as_copy_cfg_common()

int as_copy_cfg_common ( const char *  id,
struct attestation_cfg_common cfg_dst,
struct attestation_cfg_common cfg_src 
)

Definition at line 113 of file attestation_config.c.

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}
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
#define cfg_sf_copy_wrapper(id, __cfg_dst, __cfg_src, __field)
cfg_copy_wrapper
#define cfg_enum_copy(__cfg_dst, __cfg_src, __field)

References ao2_bump, ao2_cleanup, cfg_enum_copy, cfg_sf_copy_wrapper, attestation_cfg_common::raw_key, and attestation_cfg_common::raw_key_length.

Referenced by create_effective_profile(), and tn_get_etn().

◆ as_get_cfg()

struct attestation_cfg * as_get_cfg ( void  )

Definition at line 43 of file attestation_config.c.

44{
47 if (cfg) {
48 return cfg;
49 }
50
51 return empty_cfg ? ao2_bump(empty_cfg) : NULL;
52}
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853

References ao2_bump, ast_sorcery_retrieve_by_id(), CONFIG_TYPE, empty_cfg, get_sorcery(), and NULL.

Referenced by ast_stir_shaken_as_ctx_create(), attestation_show(), and create_effective_profile().

◆ as_is_config_loaded()

int as_is_config_loaded ( void  )

◆ attestation_alloc()

static void * attestation_alloc ( const char *  name)
static

Definition at line 85 of file attestation_config.c.

86{
87 struct attestation_cfg *cfg;
88
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}
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static void attestation_destructor(void *obj)
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
struct attestation_cfg_common acfg_common

References attestation_cfg::acfg_common, ao2_ref, ast_sorcery_generic_alloc(), ast_string_field_init, attestation_destructor(), and NULL.

Referenced by as_config_load(), and as_config_reload().

◆ attestation_apply()

static int attestation_apply ( const struct ast_sorcery sorcery,
void *  obj 
)
static

Definition at line 221 of file attestation_config.c.

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}
int as_check_common_config(const char *id, struct attestation_cfg_common *acfg_common)
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317

References attestation_cfg::acfg_common, as_check_common_config(), and ast_sorcery_object_get_id().

Referenced by as_config_load().

◆ attestation_destructor()

static void attestation_destructor ( void *  obj)
static

Definition at line 77 of file attestation_config.c.

78{
79 struct attestation_cfg *cfg = obj;
80
83}
void acfg_cleanup(struct attestation_cfg_common *acfg_common)

References acfg_cleanup(), attestation_cfg::acfg_common, and ast_string_field_free_memory.

Referenced by attestation_alloc().

◆ attestation_show()

static char * attestation_show ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 233 of file attestation_config.c.

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}
struct attestation_cfg * as_get_cfg(void)
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define CLI_FAILURE
Definition: cli.h:46
int config_object_cli_show(void *obj, void *arg, void *data, int flags)
Output configuration settings to the Asterisk CLI.
@ config_object_type_attestation
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
static struct test_val a

References a, ao2_cleanup, as_get_cfg(), as_is_config_loaded(), ast_log, CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, config_object_cli_show(), config_object_type_attestation, LOG_WARNING, NULL, config_object_cli_data::title, and ast_cli_entry::usage.

◆ generate_acfg_common_sorcery_handlers()

generate_acfg_common_sorcery_handlers ( attestation_cfg  )

◆ generate_sorcery_enum_from_str_ex()

generate_sorcery_enum_from_str_ex ( attestation_cfg  ,
unknown_tn_attest_level  ,
attest_level  ,
UNKNOWN   
)

◆ generate_sorcery_enum_to_str_ex()

generate_sorcery_enum_to_str_ex ( attestation_cfg  ,
unknown_tn_attest_level  ,
attest_level   
)

Variable Documentation

◆ attestation_cli

struct ast_cli_entry attestation_cli[]
static
Initial value:
= {
{ .handler = attestation_show , .summary = "Show stir/shaken attestation configuration" ,},
}
static char * attestation_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Definition at line 268 of file attestation_config.c.

Referenced by as_config_load(), and as_config_unload().

◆ empty_cfg

struct attestation_cfg* empty_cfg = NULL
static