Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
res_stir_shaken.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2020, Sangoma Technologies Corporation
5 *
6 * Kevin Harwell <kharwell@digium.com>
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/*** MODULEINFO
20 <depend>curl</depend>
21 <depend>res_curl</depend>
22 <depend>libjwt</depend>
23 <support_level>core</support_level>
24 ***/
25
26#define _TRACE_PREFIX_ "rss",__LINE__, ""
27
28#include "asterisk.h"
29
30#include "asterisk/app.h"
31#include "asterisk/cli.h"
33#include "asterisk/module.h"
35#include "asterisk/pbx.h"
36#include "asterisk/vector.h"
37
39
41
43
45{
46 return tn_auth_list_nid;
47}
48
49/*!
50 * \brief Frees a stir_shaken_datastore structure
51 *
52 * \param datastore The datastore to free
53 */
55{
56 if (!datastore) {
57 return;
58 }
59
60 ast_free(datastore->identity);
61 ast_free(datastore->attestation);
62 ast_free(datastore);
63}
64
65/*!
66 * \brief The callback to destroy a stir_shaken_datastore
67 *
68 * \param data The stir_shaken_datastore
69 */
70static void verification_ds_destroy_cb(void *data)
71{
72 struct verification_vector *verifies = data;
73
75 ast_free(verifies);
76}
77
82
84 struct ast_stir_shaken_vs_ctx *ctx)
85{
86 struct stir_shaken_verification_ds *stir_datastore;
87 struct ast_datastore *chan_datastore;
88 struct verification_vector *verifies;
89 const char *chan_name;
90 int res = 0;
91
92 if (!ctx->chan) {
93 ast_log(LOG_ERROR, "Channel is required to add verification\n");
94 return -1;
95 }
96
97 chan_name = ast_channel_name(ctx->chan);
98
99 if (!ctx->identity_hdr) {
100 ast_log(LOG_ERROR, "%s: No identity to add to datastore\n",
101 chan_name);
102 return -1;
103 }
104
105 if (!ctx->attestation) {
106 ast_log(LOG_ERROR, "%s: Attestation cannot be NULL\n", chan_name);
107 return -1;
108 }
109
110 stir_datastore = ast_calloc(1, sizeof(*stir_datastore));
111 if (!stir_datastore) {
112 return -1;
113 }
114
115 stir_datastore->identity = ast_strdup(ctx->identity_hdr);
116 if (!stir_datastore->identity) {
117 verification_ds_free(stir_datastore);
118 return -1;
119 }
120
121 stir_datastore->attestation = ast_strdup(ctx->attestation);
122 if (!stir_datastore->attestation) {
123 verification_ds_free(stir_datastore);
124 return -1;
125 }
126
127 stir_datastore->verify_result = ctx->failure_reason;
128
131 if (chan_datastore) {
132 verifies = chan_datastore->data;
133 res = AST_VECTOR_APPEND(verifies, stir_datastore);
134 if (res != 0) {
135 verification_ds_free(stir_datastore);
136 }
138 return res;
139 }
140
142 if (!chan_datastore) {
143 verification_ds_free(stir_datastore);
144 return -1;
145 }
146 /*
147 * We don't pass this datastore to other channels at the current time.
148 * Inheritance is disabled by default but it's called out here for clarity.
149 */
150 chan_datastore->inheritance = 0;
151
152 verifies = ast_calloc(sizeof(*verifies), 1);
153 if (!verifies) {
155 verification_ds_free(stir_datastore);
156 ast_datastore_free(chan_datastore);
157 return -1;
158 }
159 AST_VECTOR_INIT(verifies, 0);
160
161 res = AST_VECTOR_APPEND(verifies, stir_datastore);
162 if (res != 0) {
163 ast_free(verifies);
164 verification_ds_free(stir_datastore);
165 } else {
166 chan_datastore->data = verifies;
167 ast_channel_datastore_add(ctx->chan, chan_datastore);
168 }
170
171 return res;
172}
173
174/*!
175 * \brief Retrieves STIR/SHAKEN verification information for the channel via dialplan.
176 * Examples:
177 *
178 * STIR_SHAKEN(count)
179 * STIR_SHAKEN(0, identity)
180 * STIR_SHAKEN(1, attestation)
181 * STIR_SHAKEN(27, verify_result)
182 *
183 * \retval -1 on failure
184 * \retval 0 on success
185 */
186static int func_read_verification(struct ast_channel *chan, const char *function,
187 char *data, char *buf, size_t len)
188{
189 struct stir_shaken_verification_ds *stir_datastore;
190 struct ast_datastore *chan_datastore;
191 struct verification_vector *verifies;
192 const char *chan_name;
193 char *parse;
194 char *first;
195 char *second;
196 unsigned int target_index = 0;
197 int res = 0;
199 AST_APP_ARG(first_param);
200 AST_APP_ARG(second_param);
201 );
202
203 if (ast_strlen_zero(data)) {
204 ast_log(LOG_WARNING, "%s requires at least one argument\n", function);
205 return -1;
206 }
207
208 if (!chan) {
209 ast_log(LOG_ERROR, "No channel for %s function\n", function);
210 return -1;
211 }
212 chan_name = ast_channel_name(chan);
213
214 parse = ast_strdupa(data);
215
217
218 first = ast_strip(args.first_param);
219 if (ast_strlen_zero(first)) {
220 ast_log(LOG_ERROR, "%s: An argument must be passed to %s\n",
221 chan_name, function);
222 return -1;
223 }
224
225 second = ast_strip(args.second_param);
226
227 /* Check if we are only looking for the number of STIR/SHAKEN verification results */
228 if (!strcasecmp(first, "count")) {
229 size_t count = 0;
230
231 if (!ast_strlen_zero(second)) {
232 ast_log(LOG_ERROR, "%s: %s only takes 1 paramater for 'count'\n",
233 chan_name, function);
234 return -1;
235 }
236
237 ast_channel_lock(chan);
238 chan_datastore = ast_channel_datastore_find(chan, &verification_ds_info, NULL);
239 if (chan_datastore && chan_datastore->data) {
240 verifies = chan_datastore->data;
241 count = AST_VECTOR_SIZE(verifies);
242 }
243 ast_channel_unlock(chan);
244
245 snprintf(buf, len, "%zu", count);
246 return 0;
247 }
248
249 /* If we aren't doing a count, then there should be two parameters. The field
250 * we are searching for will be the second parameter. The index is the first.
251 */
252 if (ast_strlen_zero(second)) {
253 ast_log(LOG_ERROR, "%s: Retrieving a value using %s requires two paramaters (index, value) "
254 "- only index was given\n", chan_name, function);
255 return -1;
256 }
257
258 if (ast_str_to_uint(first, &target_index)) {
259 ast_log(LOG_ERROR, "%s: Failed to convert index %s to integer for function %s\n",
260 chan_name, first, function);
261 return -1;
262 }
263
264 ast_channel_lock(chan);
265 chan_datastore = ast_channel_datastore_find(chan, &verification_ds_info, NULL);
266 if (!chan_datastore || !chan_datastore->data) {
267 ast_channel_unlock(chan);
268 ast_log(LOG_WARNING, "%s: No STIR/SHAKEN results for index '%s'\n",
269 chan_name, first);
270 return -1;
271 }
272 verifies = chan_datastore->data;
273 if (target_index >= AST_VECTOR_SIZE(verifies)) {
274 ast_channel_unlock(chan);
275 ast_log(LOG_WARNING, "%s: No STIR/SHAKEN results for index '%s'\n",
276 chan_name, first);
277 return -1;
278 }
279 stir_datastore = AST_VECTOR_GET(verifies, target_index);
280
281 if (!strcasecmp(second, "identity")) {
282 ast_copy_string(buf, stir_datastore->identity, len);
283 } else if (!strcasecmp(second, "attestation")) {
284 ast_copy_string(buf, stir_datastore->attestation, len);
285 } else if (!strcasecmp(second, "verify_result")) {
287 } else {
288 ast_log(LOG_ERROR, "%s: No such value '%s' for %s\n",
289 chan_name, second, function);
290 res = -1;
291 }
292
293 ast_channel_unlock(chan);
294
295 return res;
296}
297
298/*
299 * Unlike verification, on an outgoing channel there can be at most
300 * one attestation datastore so there's no need for a vector. One
301 * can be added at a later date if this changes.
302 */
303
304static void attestation_ds_destroy(void *data)
305{
306 ast_free(data);
307}
308
313
315 struct ast_channel *chan)
316{
317 struct stir_shaken_attestation_ds *attestation_ds;
318 struct ast_datastore *chan_datastore;
319
320 chan_datastore = ast_channel_datastore_find(chan, &attestation_ds_info, NULL);
321 if (!chan_datastore) {
322 return NULL;
323 }
324 attestation_ds = chan_datastore->data;
325 return attestation_ds;
326}
327
328
329static int func_write_attestation(struct ast_channel *chan, const char *function, char *data,
330 const char *value)
331{
332 struct stir_shaken_attestation_ds *attestation_ds;
333 struct ast_datastore *chan_datastore;
334 char *parse;
335 char *field;
336 char *stripped_value;
337 const char *channel_name = chan ? ast_channel_name(chan) : "unknown_channel";
339 AST_APP_ARG(field);
340 );
341
342 if (!chan) {
343 ast_log(LOG_ERROR, "No channel for %s function\n", function);
344 return -1;
345 }
346
347 if (ast_strlen_zero(data)) {
348 ast_log(LOG_WARNING, "%s: %s requires a field to set\n", channel_name, function);
349 return -1;
350 }
351
352 parse = ast_strdupa(data);
353
355
356 field = ast_strip(args.field);
357 if (ast_strlen_zero(field)) {
358 ast_log(LOG_WARNING, "%s: %s requires a field to set\n", channel_name, function);
359 return -1;
360 }
361
362 if (!ast_strings_equal(field, "suppress")) {
363 ast_log(LOG_ERROR, "%s: %s was passed invalid field '%s'\n",
364 channel_name, function, field);
365 return -1;
366 }
367
368 stripped_value = ast_strip(ast_strdupa(value));
369 if (ast_strlen_zero(stripped_value)) {
370 ast_log(LOG_ERROR, "%s: %s requires a boolean value\n", channel_name, function);
371 return -1;
372 }
373
374 ast_channel_lock(chan);
375 chan_datastore = ast_channel_datastore_find(chan, &attestation_ds_info, NULL);
376
377 if (chan_datastore) {
378 attestation_ds = chan_datastore->data;
379 } else {
380 attestation_ds = ast_calloc(1, sizeof(*attestation_ds));
381 chan_datastore = ast_datastore_alloc(&attestation_ds_info, NULL);
382 if (!attestation_ds || !chan_datastore) {
383 ast_channel_unlock(chan);
384 attestation_ds_destroy(attestation_ds);
385 ast_datastore_free(chan_datastore);
386 return -1;
387 }
388
389 chan_datastore->data = attestation_ds;
390 chan_datastore->inheritance = 0;
391
392 ast_channel_datastore_add(chan, chan_datastore);
393 }
394
395 attestation_ds->suppress = ast_true(stripped_value);
396 ast_channel_unlock(chan);
397
398 return 0;
399}
400
402 .name = "STIR_SHAKEN",
404};
405
407 .name = "STIR_SHAKEN_ATTESTATION",
408 .write = func_write_attestation,
409};
410
411static int reload_module(void)
412{
413 return common_config_reload();
414}
415
426
427#define TN_AUTH_LIST_OID "1.3.6.1.5.5.7.1.26"
428#define TN_AUTH_LIST_SHORT "TNAuthList"
429#define TN_AUTH_LIST_LONG "TNAuthorizationList"
430
431static int check_for_old_config(void)
432{
433 const char *error_msg = "There appears to be a 'stir_shaken.conf' file"
434 " with old configuration options in it. Please see the new config"
435 " file format in the configs/samples/stir_shaken.conf.sample file"
436 " in the source tree at https://github.com/asterisk/asterisk/raw/master/configs/samples/stir_shaken.conf.sample"
437 " or visit https://docs.asterisk.org/Deployment/STIR-SHAKEN for more information.";
439 struct ast_flags config_flags = { 0 };
440 char *cat = NULL;
441
442 cfg = ast_config_load("stir_shaken.conf", config_flags);
443 if (cfg == CONFIG_STATUS_FILEMISSING) {
444 /*
445 * They may be loading from realtime so the fact that there's
446 * no stir-shaken.conf file isn't an issue for this purpose.
447 */
449 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
450 cfg = NULL;
451 ast_log(LOG_ERROR, "The stir_shaken.conf file is invalid\n");
453 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
454 /* This can never happen but is included for completeness */
455 cfg = NULL;
457 }
458
459 while ((cat = ast_category_browse(cfg, cat))) {
460 const char *val;
461 if (strcasecmp(cat, "general") == 0) {
462 ast_log(LOG_ERROR, "%s\n", error_msg);
464 }
465 val = ast_variable_retrieve(cfg, cat, "type");
466 if (val && (strcasecmp(val, "store") == 0 ||
467 strcasecmp(val, "certificate") == 0)) {
468 ast_log(LOG_ERROR, "%s\n", error_msg);
470 }
471 }
472
474}
475
476static int load_module(void)
477{
478 int res = 0;
479
480 res = check_for_old_config();
481 if (res != AST_MODULE_LOAD_SUCCESS) {
482 return res;
483 }
484
485 res = crypto_load();
486 if (res != AST_MODULE_LOAD_SUCCESS) {
487 return res;
488 }
489
492 if (tn_auth_list_nid < 0) {
495 }
496
497 res = common_config_load();
498 if (res != AST_MODULE_LOAD_SUCCESS) {
500 return res;
501 }
502
504 if (res != 0) {
507 }
508
510 if (res != 0) {
513 }
514
516}
517
519 .support_level = AST_MODULE_SUPPORT_CORE,
520 .load = load_module,
521 .unload = unload_module,
523 .load_pri = AST_MODPRI_CHANNEL_DEPEND - 1,
524 .requires = "res_curl",
struct sla_ringing_trunk * first
Definition app_sla.c:338
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
#define ast_log
Definition astobj2.c:42
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition channel.c:2375
#define ast_channel_lock(chan)
Definition channel.h:2982
#define ast_channel_unlock(chan)
Definition channel.h:2983
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition channel.c:2389
Standard Command Line Interface.
int common_config_unload(void)
int common_config_reload(void)
int common_config_load(void)
Conversion utility functions.
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
Definition conversions.c:56
int crypto_register_x509_extension(const char *oid, const char *short_name, const char *long_name)
Register a certificate extension to openssl.
int crypto_load(void)
Initialize the crypto utils.
int crypto_unload(void)
Clean up the crypto utils.
#define ast_datastore_alloc(info, uid)
Definition datastore.h:85
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition datastore.c:68
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
globally accessible channel datastores
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition extconf.c:3324
#define CONFIG_STATUS_FILEMISSING
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition extconf.c:1287
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition module.h:331
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition module.h:330
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition module.h:557
@ AST_MODPRI_CHANNEL_DEPEND
Definition module.h:340
@ AST_MODULE_SUPPORT_CORE
Definition module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
Core PBX routines and definitions.
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static int reload(void)
struct stir_shaken_attestation_ds * ast_stir_shaken_get_attestation_datastore(struct ast_channel *chan)
#define TN_AUTH_LIST_SHORT
int ast_stir_shaken_add_result_to_channel(struct ast_stir_shaken_vs_ctx *ctx)
Add a STIR/SHAKEN verification result to a channel.
static struct ast_custom_function stir_shaken_attestation
static const struct ast_datastore_info verification_ds_info
#define TN_AUTH_LIST_OID
#define TN_AUTH_LIST_LONG
static void attestation_ds_destroy(void *data)
static struct ast_custom_function stir_shaken_verification
static const struct ast_datastore_info attestation_ds_info
static int reload_module(void)
static int check_for_old_config(void)
static void verification_ds_free(struct stir_shaken_verification_ds *datastore)
Frees a stir_shaken_datastore structure.
static int tn_auth_list_nid
static int load_module(void)
static int unload_module(void)
static int func_read_verification(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
Retrieves STIR/SHAKEN verification information for the channel via dialplan. Examples:
static int func_write_attestation(struct ast_channel *chan, const char *function, char *data, const char *value)
int get_tn_auth_nid(void)
Retrieves the OpenSSL NID for the TN Auth list extension.
static void verification_ds_destroy_cb(void *data)
The callback to destroy a stir_shaken_datastore.
static struct @519 args
#define NULL
Definition resample.c:96
const char * vs_response_code_to_str(enum ast_stir_shaken_vs_response_code vs_rc)
Return string version of VS response code.
#define STIR_SHAKEN_ATTESTATION_DS
Definition stir_shaken.h:42
#define STIR_SHAKEN_VERIFICATION_DS
Definition stir_shaken.h:32
int ast_strings_equal(const char *str1, const char *str2)
Compare strings for equality checking for NULL.
Definition strings.c:238
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition utils.c:2235
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition strings.h:223
Main Channel structure associated with a channel.
Data structure associated with a custom dialplan function.
Definition pbx.h:118
const char * name
Definition pbx.h:119
Structure for a data store type.
Definition datastore.h:31
const char * type
Definition datastore.h:32
Structure for a data store object.
Definition datastore.h:64
void * data
Definition datastore.h:66
unsigned int inheritance
Definition datastore.h:69
Structure used to handle boolean flags.
Definition utils.h:220
const ast_string_field attestation
const ast_string_field identity_hdr
struct ast_channel * chan
enum ast_stir_shaken_vs_response_code failure_reason
enum ast_stir_shaken_vs_response_code verify_result
Definition stir_shaken.h:39
int value
Definition syslog.c:37
#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:981
Vector container support.
#define AST_VECTOR_RESET(vec, cleanup)
Reset vector.
Definition vector.h:636
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition vector.h:620
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition vector.h:124
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition vector.h:267
#define AST_VECTOR(name, type)
Define a vector structure.
Definition vector.h:44
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:691