| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#define _TRACE_PREFIX_ "rss",__LINE__, "" |
| 27 |
|
| 28 |
#include "asterisk.h" |
| 29 |
|
| 30 |
#include "asterisk/app.h" |
| 31 |
#include "asterisk/cli.h" |
| 32 |
#include "asterisk/conversions.h" |
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/global_datastores.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/vector.h" |
| 37 |
|
| 38 |
#include "res_stir_shaken/stir_shaken.h" |
| 39 |
|
| 40 |
AST_VECTOR(verification_vector, struct stir_shaken_verification_ds *); |
| 41 |
|
| 42 |
static int tn_auth_list_nid; |
| 43 |
|
| 44 |
int get_tn_auth_nid(void) |
| 45 |
{ |
| 46 |
return tn_auth_list_nid; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void verification_ds_free(struct stir_shaken_verification_ds *datastore) |
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static void verification_ds_destroy_cb(void *data) |
| 71 |
{ |
| 72 |
struct verification_vector *verifies = data; |
| 73 |
|
| 74 |
AST_VECTOR_RESET(verifies, verification_ds_free); |
| 75 |
ast_free(verifies); |
| 76 |
} |
| 77 |
|
| 78 |
static const struct ast_datastore_info verification_ds_info = { |
| 79 |
.type = STIR_SHAKEN_VERIFICATION_DS, |
| 80 |
.destroy = verification_ds_destroy_cb, |
| 81 |
}; |
| 82 |
|
| 83 |
int ast_stir_shaken_add_result_to_channel( |
| 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 |
|
| 129 |
ast_channel_lock(ctx->chan); |
| 130 |
chan_datastore = ast_channel_datastore_find(ctx->chan, &verification_ds_info, NULL); |
| 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 |
} |
| 137 |
ast_channel_unlock(ctx->chan); |
| 138 |
return res; |
| 139 |
} |
| 140 |
|
| 141 |
chan_datastore = ast_datastore_alloc(&verification_ds_info, NULL); |
| 142 |
if (!chan_datastore) { |
| 143 |
verification_ds_free(stir_datastore); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
chan_datastore->inheritance = 0; |
| 151 |
|
| 152 |
verifies = ast_calloc(sizeof(*verifies), 1); |
| 153 |
if (!verifies) { |
| 154 |
ast_channel_unlock(ctx->chan); |
| 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 |
} |
| 169 |
ast_channel_unlock(ctx->chan); |
| 170 |
|
| 171 |
return res; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
static 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; |
| 198 |
AST_DECLARE_APP_ARGS(args, |
| 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 |
|
| 216 |
AST_STANDARD_APP_ARGS(args, parse); |
| 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 |
|
| 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 |
|
| 250 |
|
| 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")) { |
| 286 |
ast_copy_string(buf, vs_response_code_to_str(stir_datastore->verify_result), len); |
| 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 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
static void attestation_ds_destroy(void *data) |
| 305 |
{ |
| 306 |
ast_free(data); |
| 307 |
} |
| 308 |
|
| 309 |
static const struct ast_datastore_info attestation_ds_info = { |
| 310 |
.type = STIR_SHAKEN_ATTESTATION_DS, |
| 311 |
.destroy = attestation_ds_destroy, |
| 312 |
}; |
| 313 |
|
| 314 |
struct stir_shaken_attestation_ds *ast_stir_shaken_get_attestation_datastore( |
| 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 |
|
| 329 |
static 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"; |
| 338 |
AST_DECLARE_APP_ARGS(args, |
| 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 |
|
| 354 |
AST_STANDARD_APP_ARGS(args, parse); |
| 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 |
|
| 401 |
static struct ast_custom_function stir_shaken_verification = { |
| 402 |
.name = "STIR_SHAKEN", |
| 403 |
.read = func_read_verification, |
| 404 |
}; |
| 405 |
|
| 406 |
static struct ast_custom_function stir_shaken_attestation = { |
| 407 |
.name = "STIR_SHAKEN_ATTESTATION", |
| 408 |
.write = func_write_attestation, |
| 409 |
}; |
| 410 |
|
| 411 |
static int reload_module(void) |
| 412 |
{ |
| 413 |
return common_config_reload(); |
| 414 |
} |
| 415 |
|
| 416 |
static int unload_module(void) |
| 417 |
{ |
| 418 |
common_config_unload(); |
| 419 |
crypto_unload(); |
| 420 |
|
| 421 |
ast_custom_function_unregister(&stir_shaken_verification); |
| 422 |
ast_custom_function_unregister(&stir_shaken_attestation); |
| 423 |
|
| 424 |
return 0; |
| 425 |
} |
| 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 |
|
| 431 |
static 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."; |
| 438 |
RAII_VAR(struct ast_config *, cfg, NULL, ast_config_destroy); |
| 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 |
|
| 446 |
|
| 447 |
|
| 448 |
return AST_MODULE_LOAD_SUCCESS; |
| 449 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 450 |
cfg = NULL; |
| 451 |
ast_log(LOG_ERROR, "The stir_shaken.conf file is invalid\n"); |
| 452 |
return AST_MODULE_LOAD_DECLINE; |
| 453 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 454 |
|
| 455 |
cfg = NULL; |
| 456 |
return AST_MODULE_LOAD_SUCCESS; |
| 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); |
| 463 |
return AST_MODULE_LOAD_DECLINE; |
| 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); |
| 469 |
return AST_MODULE_LOAD_DECLINE; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
return AST_MODULE_LOAD_SUCCESS; |
| 474 |
} |
| 475 |
|
| 476 |
static 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 |
|
| 490 |
tn_auth_list_nid = crypto_register_x509_extension(TN_AUTH_LIST_OID, |
| 491 |
TN_AUTH_LIST_SHORT, TN_AUTH_LIST_LONG); |
| 492 |
if (tn_auth_list_nid < 0) { |
| 493 |
unload_module(); |
| 494 |
return AST_MODULE_LOAD_DECLINE; |
| 495 |
} |
| 496 |
|
| 497 |
res = common_config_load(); |
| 498 |
if (res != AST_MODULE_LOAD_SUCCESS) { |
| 499 |
unload_module(); |
| 500 |
return res; |
| 501 |
} |
| 502 |
|
| 503 |
res = ast_custom_function_register(&stir_shaken_verification); |
| 504 |
if (res != 0) { |
| 505 |
unload_module(); |
| 506 |
return AST_MODULE_LOAD_DECLINE; |
| 507 |
} |
| 508 |
|
| 509 |
res = ast_custom_function_register(&stir_shaken_attestation); |
| 510 |
if (res != 0) { |
| 511 |
unload_module(); |
| 512 |
return AST_MODULE_LOAD_DECLINE; |
| 513 |
} |
| 514 |
|
| 515 |
return AST_MODULE_LOAD_SUCCESS; |
| 516 |
} |
| 517 |
|
| 518 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "STIR/SHAKEN Module for Asterisk", |
| 519 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 520 |
.load = load_module, |
| 521 |
.unload = unload_module, |
| 522 |
.reload = reload_module, |
| 523 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 1, |
| 524 |
.requires = "res_curl", |
| 525 |
); |