| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "asterisk.h" |
| 20 |
|
| 21 |
#include <pjsip.h> |
| 22 |
|
| 23 |
#include "asterisk/res_pjsip.h" |
| 24 |
#include "asterisk/logger.h" |
| 25 |
#include "asterisk/module.h" |
| 26 |
#include "asterisk/strings.h" |
| 27 |
#include "asterisk/test.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static char default_realm[AST_SIP_AUTH_MAX_REALM_LENGTH + 1]; |
| 44 |
|
| 45 |
AO2_GLOBAL_OBJ_STATIC(entity_id); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static int digest_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) |
| 54 |
{ |
| 55 |
RAII_VAR(struct ast_sip_endpoint *, artificial, ast_sip_get_artificial_endpoint(), ao2_cleanup); |
| 56 |
|
| 57 |
return endpoint == artificial || AST_VECTOR_SIZE(&endpoint->inbound_auths) > 0; |
| 58 |
} |
| 59 |
|
| 60 |
static void auth_store_cleanup(void *data) |
| 61 |
{ |
| 62 |
struct ast_sip_auth **auth = data; |
| 63 |
|
| 64 |
ao2_cleanup(*auth); |
| 65 |
ast_free(data); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
AST_THREADSTORAGE_CUSTOM(auth_store, NULL, auth_store_cleanup); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static int store_auth(const struct ast_sip_auth *auth) |
| 100 |
{ |
| 101 |
const struct ast_sip_auth **pointing; |
| 102 |
|
| 103 |
pointing = ast_threadstorage_get(&auth_store, sizeof(pointing)); |
| 104 |
if (!pointing) { |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
*pointing = auth; |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static int remove_auth(void) |
| 116 |
{ |
| 117 |
struct ast_sip_auth **pointing; |
| 118 |
|
| 119 |
pointing = ast_threadstorage_get(&auth_store, sizeof(pointing)); |
| 120 |
if (!pointing) { |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
*pointing = NULL; |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static const struct ast_sip_auth *get_auth(void) |
| 132 |
{ |
| 133 |
struct ast_sip_auth **auth; |
| 134 |
|
| 135 |
auth = ast_threadstorage_get(&auth_store, sizeof(auth)); |
| 136 |
if (auth) { |
| 137 |
return *auth; |
| 138 |
} |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
|
| 142 |
static struct pjsip_authorization_hdr *get_authorization_hdr( |
| 143 |
const char *auth_id, const char *realm, const pjsip_rx_data *rdata) |
| 144 |
{ |
| 145 |
const char *src_name = rdata->pkt_info.src_name; |
| 146 |
struct pjsip_authorization_hdr *auth_hdr = |
| 147 |
(pjsip_authorization_hdr *) &rdata->msg_info.msg->hdr; |
| 148 |
SCOPE_ENTER(3, "%s:%s: realm: %s\n", auth_id, src_name, realm); |
| 149 |
|
| 150 |
while ((auth_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, |
| 151 |
PJSIP_H_AUTHORIZATION, auth_hdr ? auth_hdr->next : NULL))) { |
| 152 |
if (pj_strcmp2(&auth_hdr->credential.common.realm, realm) == 0) { |
| 153 |
SCOPE_EXIT_RTN_VALUE(auth_hdr, "%s:%s: realm: %s Found header\n", |
| 154 |
auth_id, src_name, realm); |
| 155 |
} |
| 156 |
} |
| 157 |
SCOPE_EXIT_RTN_VALUE(NULL, "%s:%s: realm: %s No auth header found\n", |
| 158 |
auth_id, src_name, realm); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static pj_status_t digest_lookup(pj_pool_t *pool, |
| 179 |
const pjsip_auth_lookup_cred_param *param, |
| 180 |
pjsip_cred_info *cred_info) |
| 181 |
{ |
| 182 |
const struct ast_sip_auth *auth = get_auth(); |
| 183 |
const char *realm = S_OR(auth->realm, default_realm); |
| 184 |
const char *creds; |
| 185 |
const char *auth_name = (auth ? ast_sorcery_object_get_id(auth) : "none"); |
| 186 |
struct pjsip_authorization_hdr *auth_hdr = get_authorization_hdr(auth_name, realm, param->rdata); |
| 187 |
const pjsip_auth_algorithm *algorithm = auth_hdr ? |
| 188 |
ast_sip_auth_get_algorithm_by_iana_name(&auth_hdr->credential.digest.algorithm) : NULL; |
| 189 |
const char *src_name = param->rdata->pkt_info.src_name; |
| 190 |
SCOPE_ENTER(4, "%s:%s:" |
| 191 |
" srv realm: " PJSTR_PRINTF_SPEC |
| 192 |
" auth realm: %s" |
| 193 |
" auth user: %s" |
| 194 |
" hdr user: " PJSTR_PRINTF_SPEC |
| 195 |
"\n", |
| 196 |
auth_name, src_name, |
| 197 |
PJSTR_PRINTF_VAR(param->realm), |
| 198 |
realm, |
| 199 |
auth->auth_user, |
| 200 |
PJSTR_PRINTF_VAR(param->acc_name)); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
if (!auth) { |
| 212 |
|
| 213 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, "%s:%s: No auth object found\n", |
| 214 |
auth_name, src_name); |
| 215 |
} |
| 216 |
|
| 217 |
if (auth_hdr == NULL) { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, |
| 223 |
"%s:%s: No Authorization header found for realm '%s'\n", |
| 224 |
auth_name, src_name, realm); |
| 225 |
} |
| 226 |
|
| 227 |
if (algorithm == NULL) { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, |
| 233 |
"%s:%s: Unsupported algorithm '" PJSTR_PRINTF_SPEC "'\n", |
| 234 |
auth_name, src_name, PJSTR_PRINTF_VAR(auth_hdr->credential.digest.algorithm)); |
| 235 |
} |
| 236 |
|
| 237 |
if (auth->type == AST_SIP_AUTH_TYPE_ARTIFICIAL) { |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, "%s:%s: Artificial auth object\n", |
| 243 |
auth_name, src_name); |
| 244 |
} |
| 245 |
|
| 246 |
if (pj_strcmp2(¶m->realm, realm) != 0) { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, "%s:%s: Realm '%s' mismatch\n", |
| 252 |
auth_name, src_name, realm); |
| 253 |
} |
| 254 |
|
| 255 |
if (pj_strcmp2(¶m->acc_name, auth->auth_user) != 0) { |
| 256 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, "%s:%s: Username '%s' mismatch\n", |
| 257 |
auth_name, src_name, auth->auth_user); |
| 258 |
} |
| 259 |
|
| 260 |
if (!ast_sip_auth_is_algorithm_available(auth, &auth->supported_algorithms_uas, |
| 261 |
algorithm->algorithm_type)) { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, "%s:%s: Algorithm '" PJSTR_PRINTF_SPEC |
| 267 |
"' not supported or auth doesn't contain appropriate credentials\n", |
| 268 |
auth_name, src_name, PJSTR_PRINTF_VAR(algorithm->iana_name)); |
| 269 |
} |
| 270 |
|
| 271 |
pj_strdup2(pool, &cred_info->realm, realm); |
| 272 |
pj_strdup2(pool, &cred_info->username, auth->auth_user); |
| 273 |
|
| 274 |
creds = ast_sip_auth_get_creds(auth, algorithm->algorithm_type, &cred_info->data_type); |
| 275 |
if (!creds) { |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
SCOPE_EXIT_RTN_VALUE(PJSIP_SC_FORBIDDEN, "%s:%s: No plain text or digest password found for algorithm '" PJSTR_PRINTF_SPEC "'\n", |
| 282 |
auth_name, src_name, PJSTR_PRINTF_VAR(algorithm->iana_name)); |
| 283 |
} |
| 284 |
pj_strdup2(pool, &cred_info->data, creds); |
| 285 |
#ifdef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 286 |
if (cred_info->data_type == PJSIP_CRED_DATA_DIGEST) { |
| 287 |
cred_info->algorithm_type = algorithm->algorithm_type; |
| 288 |
} |
| 289 |
#endif |
| 290 |
|
| 291 |
SCOPE_EXIT_RTN_VALUE(PJ_SUCCESS, "%s:%s: Success. Data type: %s Algorithm '" PJSTR_PRINTF_SPEC "'\n", |
| 292 |
auth_name, src_name, cred_info->data_type ? "digest" : "plain text", PJSTR_PRINTF_VAR(algorithm->iana_name)); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static int build_nonce(struct ast_str **nonce, const char *timestamp, |
| 313 |
const pjsip_rx_data *rdata, const char *realm) |
| 314 |
{ |
| 315 |
struct ast_str *str = ast_str_alloca(256); |
| 316 |
RAII_VAR(char *, eid, ao2_global_obj_ref(entity_id), ao2_cleanup); |
| 317 |
char hash[33]; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
ast_str_append(&str, 0, "%s", timestamp); |
| 324 |
ast_str_append(&str, 0, ":%s", rdata->pkt_info.src_name); |
| 325 |
ast_str_append(&str, 0, ":%s", eid); |
| 326 |
ast_str_append(&str, 0, ":%s", realm); |
| 327 |
ast_md5_hash(hash, ast_str_buffer(str)); |
| 328 |
|
| 329 |
ast_str_append(nonce, 0, "%s/%s", timestamp, hash); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
static int check_nonce(const char *candidate, const pjsip_rx_data *rdata, const struct ast_sip_auth *auth) |
| 346 |
{ |
| 347 |
char *copy = ast_strdupa(candidate); |
| 348 |
char *timestamp = strsep(©, "/"); |
| 349 |
int timestamp_int; |
| 350 |
time_t now = time(NULL); |
| 351 |
struct ast_str *calculated = ast_str_alloca(64); |
| 352 |
|
| 353 |
if (!copy) { |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
if (sscanf(timestamp, "%30d", ×tamp_int) != 1) { |
| 359 |
return 0; |
| 360 |
} |
| 361 |
|
| 362 |
if ((int) now - timestamp_int > auth->nonce_lifetime) { |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
build_nonce(&calculated, timestamp, rdata, S_OR(auth->realm, default_realm)); |
| 367 |
ast_debug(3, "Calculated nonce %s. Actual nonce is %s\n", ast_str_buffer(calculated), candidate); |
| 368 |
if (strcmp(ast_str_buffer(calculated), candidate)) { |
| 369 |
return 0; |
| 370 |
} |
| 371 |
return 1; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
enum digest_verify_result { |
| 378 |
|
| 379 |
AUTH_FAIL = 0, |
| 380 |
|
| 381 |
AUTH_SUCCESS, |
| 382 |
|
| 383 |
AUTH_STALE, |
| 384 |
|
| 385 |
AUTH_NOAUTH, |
| 386 |
}; |
| 387 |
|
| 388 |
static char *verify_result_str[] = { |
| 389 |
"FAIL", |
| 390 |
"SUCCESS", |
| 391 |
"STALE", |
| 392 |
"NOAUTH" |
| 393 |
}; |
| 394 |
|
| 395 |
static enum digest_verify_result find_authorization(const char *endpoint_id, |
| 396 |
const struct ast_sip_auth *auth, const pjsip_rx_data *rdata) |
| 397 |
{ |
| 398 |
const char *auth_id = ast_sorcery_object_get_id(auth); |
| 399 |
const char *src_name = rdata->pkt_info.src_name; |
| 400 |
const char *realm = S_OR(auth->realm, default_realm); |
| 401 |
struct pjsip_authorization_hdr *auth_hdr = |
| 402 |
(pjsip_authorization_hdr *) &rdata->msg_info.msg->hdr; |
| 403 |
enum digest_verify_result res = AUTH_NOAUTH; |
| 404 |
int authorization_found = 0; |
| 405 |
char nonce[64]; |
| 406 |
SCOPE_ENTER(3, "%s:%s:%s: realm: %s\n", |
| 407 |
endpoint_id, auth_id, src_name, realm); |
| 408 |
|
| 409 |
while ((auth_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, |
| 410 |
PJSIP_H_AUTHORIZATION, auth_hdr ? auth_hdr->next : NULL))) { |
| 411 |
ast_copy_pj_str(nonce, &auth_hdr->credential.digest.nonce, sizeof(nonce)); |
| 412 |
ast_trace(-1, "%s:%s:%s: Checking nonce %s hdr-realm: " PJSTR_PRINTF_SPEC " hdr-algo: " PJSTR_PRINTF_SPEC " \n", |
| 413 |
endpoint_id, auth_id, src_name, nonce, |
| 414 |
PJSTR_PRINTF_VAR(auth_hdr->credential.digest.realm), |
| 415 |
PJSTR_PRINTF_VAR(auth_hdr->credential.digest.algorithm)); |
| 416 |
authorization_found++; |
| 417 |
if (check_nonce(nonce, rdata, auth) |
| 418 |
&& pj_strcmp2(&auth_hdr->credential.digest.realm, realm) == 0) { |
| 419 |
res = AUTH_SUCCESS; |
| 420 |
break; |
| 421 |
} else { |
| 422 |
res = AUTH_STALE; |
| 423 |
} |
| 424 |
} |
| 425 |
if (!authorization_found) { |
| 426 |
ast_trace(-1, "%s:%s:%s: No Authorization header found\n", |
| 427 |
endpoint_id, auth_id, src_name); |
| 428 |
res = AUTH_NOAUTH; |
| 429 |
} |
| 430 |
|
| 431 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s:%s: realm: %s Result %s\n", |
| 432 |
endpoint_id, auth_id, src_name, realm, verify_result_str[res]); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
static void setup_auth_srv(pj_pool_t *pool, pjsip_auth_srv *auth_server, const char *realm) |
| 439 |
{ |
| 440 |
pjsip_auth_srv_init_param *param = pj_pool_alloc(pool, sizeof(*param)); |
| 441 |
pj_str_t *pj_realm = pj_pool_alloc(pool, sizeof(*pj_realm)); |
| 442 |
|
| 443 |
pj_cstr(pj_realm, realm); |
| 444 |
param->realm = pj_realm; |
| 445 |
param->lookup2 = digest_lookup; |
| 446 |
param->options = 0; |
| 447 |
|
| 448 |
pjsip_auth_srv_init2(pool, auth_server, param); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
static int verify(const char *endpoint_id, const struct ast_sip_auth *auth, |
| 461 |
pjsip_rx_data *rdata, pj_pool_t *pool) |
| 462 |
{ |
| 463 |
const char *auth_id = ast_sorcery_object_get_id(auth); |
| 464 |
const char *realm = S_OR(auth->realm, default_realm); |
| 465 |
const char *src_name = rdata->pkt_info.src_name; |
| 466 |
pj_status_t authed; |
| 467 |
int response_code; |
| 468 |
pjsip_auth_srv auth_server; |
| 469 |
int stale = 0; |
| 470 |
enum digest_verify_result res = AUTH_FAIL; |
| 471 |
SCOPE_ENTER(3, "%s:%s:%s: realm: %s\n", |
| 472 |
endpoint_id, auth_id, src_name, realm); |
| 473 |
|
| 474 |
res = find_authorization(endpoint_id, auth, rdata); |
| 475 |
if (res == AUTH_NOAUTH) |
| 476 |
{ |
| 477 |
ast_test_suite_event_notify("INCOMING_AUTH_VERIFY_RESULT", |
| 478 |
"Realm: %s\r\n" |
| 479 |
"Username: %s\r\n" |
| 480 |
"Status: %s", |
| 481 |
realm, auth->auth_user, verify_result_str[res]); |
| 482 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s:%s: No Authorization header found\n", |
| 483 |
endpoint_id, auth_id, src_name); |
| 484 |
} |
| 485 |
|
| 486 |
if (res == AUTH_STALE) { |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
stale = 1; |
| 491 |
} |
| 492 |
|
| 493 |
setup_auth_srv(pool, &auth_server, realm); |
| 494 |
store_auth(auth); |
| 495 |
|
| 496 |
authed = SCOPE_CALL_WITH_RESULT(-1, pj_status_t, pjsip_auth_srv_verify, &auth_server, rdata, &response_code); |
| 497 |
remove_auth(); |
| 498 |
if (authed == PJ_SUCCESS) { |
| 499 |
if (stale) { |
| 500 |
res = AUTH_STALE; |
| 501 |
} else { |
| 502 |
res = AUTH_SUCCESS; |
| 503 |
} |
| 504 |
} else { |
| 505 |
char err[256]; |
| 506 |
res = AUTH_FAIL; |
| 507 |
pj_strerror(authed, err, sizeof(err)); |
| 508 |
ast_trace(-1, "%s:%s:%s: authed: %s\n", endpoint_id, auth_id, src_name, err); |
| 509 |
} |
| 510 |
|
| 511 |
ast_test_suite_event_notify("INCOMING_AUTH_VERIFY_RESULT", |
| 512 |
"Realm: %s\r\n" |
| 513 |
"Username: %s\r\n" |
| 514 |
"Status: %s", |
| 515 |
realm, auth->auth_user, verify_result_str[res]); |
| 516 |
|
| 517 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s:%s: Realm: %s Username: %s Result: %s\n", |
| 518 |
endpoint_id, auth_id, src_name, realm, |
| 519 |
auth->auth_user, verify_result_str[res]); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
static void challenge(const char *endpoint_id, struct ast_sip_auth *auth, |
| 533 |
pjsip_tx_data *tdata, const pjsip_rx_data *rdata, int is_stale, |
| 534 |
const pjsip_auth_algorithm *algorithm) |
| 535 |
{ |
| 536 |
pj_str_t qop; |
| 537 |
pj_str_t pj_nonce; |
| 538 |
pjsip_auth_srv auth_server; |
| 539 |
struct ast_str *nonce = ast_str_alloca(256); |
| 540 |
char time_buf[32]; |
| 541 |
time_t timestamp = time(NULL); |
| 542 |
pj_status_t res; |
| 543 |
const char *realm = S_OR(auth->realm, default_realm); |
| 544 |
const char *auth_id = ast_sorcery_object_get_id(auth); |
| 545 |
const char *src_name = rdata->pkt_info.src_name; |
| 546 |
SCOPE_ENTER(5, "%s:%s:%s: realm: %s time: %d algorithm: " PJSTR_PRINTF_SPEC " stale? %s\n", |
| 547 |
endpoint_id, auth_id, src_name, realm, (int)timestamp, |
| 548 |
PJSTR_PRINTF_VAR(algorithm->iana_name), is_stale ? "yes" : "no"); |
| 549 |
|
| 550 |
snprintf(time_buf, sizeof(time_buf), "%d", (int) timestamp); |
| 551 |
|
| 552 |
build_nonce(&nonce, time_buf, rdata, realm); |
| 553 |
|
| 554 |
setup_auth_srv(tdata->pool, &auth_server, realm); |
| 555 |
|
| 556 |
pj_cstr(&pj_nonce, ast_str_buffer(nonce)); |
| 557 |
pj_cstr(&qop, "auth"); |
| 558 |
#ifdef HAVE_PJSIP_AUTH_NEW_DIGESTS |
| 559 |
res = pjsip_auth_srv_challenge2(&auth_server, &qop, &pj_nonce, |
| 560 |
NULL, is_stale ? PJ_TRUE : PJ_FALSE, tdata, algorithm->algorithm_type); |
| 561 |
#else |
| 562 |
res = pjsip_auth_srv_challenge(&auth_server, &qop, &pj_nonce, |
| 563 |
NULL, is_stale ? PJ_TRUE : PJ_FALSE, tdata); |
| 564 |
#endif |
| 565 |
SCOPE_EXIT_RTN("%s:%s:%s: Sending challenge for realm: %s algorithm: " PJSTR_PRINTF_SPEC |
| 566 |
" %s\n", |
| 567 |
endpoint_id, auth_id, src_name, realm, PJSTR_PRINTF_VAR(algorithm->iana_name), |
| 568 |
res == PJ_SUCCESS ? "succeeded" : "failed"); |
| 569 |
} |
| 570 |
|
| 571 |
static char *check_auth_result_str[] = { |
| 572 |
"CHALLENGE", |
| 573 |
"SUCCESS", |
| 574 |
"FAILED", |
| 575 |
"ERROR", |
| 576 |
}; |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint *endpoint, |
| 609 |
pjsip_rx_data *rdata, pjsip_tx_data *tdata) |
| 610 |
{ |
| 611 |
struct ast_sip_auth **auths; |
| 612 |
enum digest_verify_result *verify_res; |
| 613 |
struct ast_sip_endpoint *artificial_endpoint; |
| 614 |
enum ast_sip_check_auth_result res = AST_SIP_AUTHENTICATION_ERROR; |
| 615 |
int idx; |
| 616 |
int is_artificial; |
| 617 |
int failures = 0; |
| 618 |
size_t auth_size; |
| 619 |
const char *endpoint_id = ast_sorcery_object_get_id(endpoint); |
| 620 |
char *src_name = rdata->pkt_info.src_name; |
| 621 |
SCOPE_ENTER(3, "%s:%s\n", endpoint_id, src_name); |
| 622 |
|
| 623 |
auth_size = AST_VECTOR_SIZE(&endpoint->inbound_auths); |
| 624 |
ast_assert(0 < auth_size); |
| 625 |
|
| 626 |
auths = ast_alloca(auth_size * sizeof(*auths)); |
| 627 |
verify_res = ast_alloca(auth_size * sizeof(*verify_res)); |
| 628 |
|
| 629 |
artificial_endpoint = ast_sip_get_artificial_endpoint(); |
| 630 |
if (!artificial_endpoint) { |
| 631 |
|
| 632 |
SCOPE_EXIT_RTN_VALUE(AST_SIP_AUTHENTICATION_ERROR); |
| 633 |
} |
| 634 |
|
| 635 |
is_artificial = endpoint == artificial_endpoint; |
| 636 |
ao2_ref(artificial_endpoint, -1); |
| 637 |
if (is_artificial) { |
| 638 |
ast_trace(3, "%s:%s: Using artificial endpoint for authentication\n", |
| 639 |
endpoint_id, src_name); |
| 640 |
ast_assert(auth_size == 1); |
| 641 |
auths[0] = ast_sip_get_artificial_auth(); |
| 642 |
if (!auths[0]) { |
| 643 |
|
| 644 |
SCOPE_EXIT_RTN_VALUE(AST_SIP_AUTHENTICATION_ERROR); |
| 645 |
} |
| 646 |
} else { |
| 647 |
ast_trace(3, "%s:%s: Using endpoint for authentication\n", |
| 648 |
endpoint_id, src_name); |
| 649 |
memset(auths, 0, auth_size * sizeof(*auths)); |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
if (ast_sip_retrieve_auths(&endpoint->inbound_auths, auths)) { |
| 656 |
ast_sip_cleanup_auths(auths, auth_size); |
| 657 |
SCOPE_EXIT_RTN_VALUE(AST_SIP_AUTHENTICATION_ERROR, |
| 658 |
"%s:%s: Failed to retrieve some or all auth objects from endpoint\n", |
| 659 |
endpoint_id, src_name); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
for (idx = 0; idx < auth_size; ++idx) { |
| 673 |
struct ast_sip_auth *auth = auths[idx]; |
| 674 |
const char *auth_id = ast_sorcery_object_get_id(auth); |
| 675 |
SCOPE_ENTER(4, "%s:%s:%s: Auth %d of %d: Verifying\n", |
| 676 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size); |
| 677 |
|
| 678 |
verify_res[idx] = SCOPE_CALL_WITH_RESULT(-1, int, verify, endpoint_id, auth, rdata, tdata->pool); |
| 679 |
switch((int)verify_res[idx]) { |
| 680 |
case AUTH_SUCCESS: |
| 681 |
res = AST_SIP_AUTHENTICATION_SUCCESS; |
| 682 |
break; |
| 683 |
case AUTH_FAIL: |
| 684 |
failures++; |
| 685 |
break; |
| 686 |
case AUTH_NOAUTH: |
| 687 |
case AUTH_STALE: |
| 688 |
break; |
| 689 |
} |
| 690 |
|
| 691 |
SCOPE_EXIT("%s:%s:%s: Auth %d of %d: Result: %s Failure count: %d\n", |
| 692 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size, |
| 693 |
verify_result_str[verify_res[idx]], failures); |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
if (verify_res[idx] == AUTH_SUCCESS || verify_res[idx] == AUTH_NOAUTH) { |
| 700 |
break; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
if (res == AST_SIP_AUTHENTICATION_SUCCESS) { |
| 705 |
ast_sip_cleanup_auths(auths, auth_size); |
| 706 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s: Result: %s\n", |
| 707 |
endpoint_id, src_name, |
| 708 |
check_auth_result_str[res]); |
| 709 |
} |
| 710 |
ast_trace(-1, "%s:%s: Done with verification. Failures: %d of %d\n", |
| 711 |
endpoint_id, src_name, failures, (int)auth_size); |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
for (idx = 0; idx < auth_size; ++idx) { |
| 720 |
int i = 0; |
| 721 |
struct ast_sip_auth *auth = auths[idx]; |
| 722 |
const char *realm = S_OR(auth->realm, default_realm); |
| 723 |
const char *auth_id = ast_sorcery_object_get_id(auth); |
| 724 |
SCOPE_ENTER(4, "%s:%s:%s: Auth %d of %d: Sending challenges\n", |
| 725 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size); |
| 726 |
|
| 727 |
for (i = 0; i < AST_VECTOR_SIZE(&auth->supported_algorithms_uas); i++) { |
| 728 |
pjsip_auth_algorithm_type algorithm_type = AST_VECTOR_GET(&auth->supported_algorithms_uas, i); |
| 729 |
const pjsip_auth_algorithm *algorithm = ast_sip_auth_get_algorithm_by_type(algorithm_type); |
| 730 |
pjsip_www_authenticate_hdr *auth_hdr = NULL; |
| 731 |
int already_sent_challenge = 0; |
| 732 |
SCOPE_ENTER(5, "%s:%s:%s: Auth %d of %d: Challenging with " PJSTR_PRINTF_SPEC "\n", |
| 733 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size, |
| 734 |
PJSTR_PRINTF_VAR(algorithm->iana_name)); |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
while ((auth_hdr = pjsip_msg_find_hdr(tdata->msg, |
| 741 |
PJSIP_H_WWW_AUTHENTICATE, auth_hdr ? auth_hdr->next : NULL))) { |
| 742 |
if (pj_strcmp2(&auth_hdr->challenge.common.realm, realm) == 0 && |
| 743 |
!pj_stricmp(&auth_hdr->challenge.digest.algorithm, &algorithm->iana_name)) { |
| 744 |
ast_trace(-1, "%s:%s:%s: Auth %d of %d: Not sending duplicate challenge for realm: %s algorithm: " |
| 745 |
PJSTR_PRINTF_SPEC "\n", |
| 746 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size, |
| 747 |
realm, PJSTR_PRINTF_VAR(algorithm->iana_name)); |
| 748 |
already_sent_challenge = 1; |
| 749 |
} |
| 750 |
} |
| 751 |
if (already_sent_challenge) { |
| 752 |
SCOPE_EXIT_EXPR(continue); |
| 753 |
} |
| 754 |
|
| 755 |
SCOPE_CALL(5, challenge, endpoint_id, auth, tdata, rdata, |
| 756 |
verify_res[idx] == AUTH_STALE, algorithm); |
| 757 |
res = AST_SIP_AUTHENTICATION_CHALLENGE; |
| 758 |
|
| 759 |
SCOPE_EXIT("%s:%s:%s: Auth %d of %d: Challenged with " PJSTR_PRINTF_SPEC "\n", |
| 760 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size, |
| 761 |
PJSTR_PRINTF_VAR(algorithm->iana_name)); |
| 762 |
} |
| 763 |
SCOPE_EXIT("%s:%s:%s: Auth %d of %d: Done with challenges\n", |
| 764 |
endpoint_id, auth_id, src_name, idx + 1, (int)auth_size); |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
if (failures == auth_size) { |
| 785 |
res = AST_SIP_AUTHENTICATION_FAILED; |
| 786 |
} |
| 787 |
|
| 788 |
ast_sip_cleanup_auths(auths, auth_size); |
| 789 |
SCOPE_EXIT_RTN_VALUE(res, "%s:%s: Result: %s\n", |
| 790 |
endpoint_id, src_name, |
| 791 |
check_auth_result_str[res]); |
| 792 |
|
| 793 |
} |
| 794 |
|
| 795 |
static struct ast_sip_authenticator digest_authenticator = { |
| 796 |
.requires_authentication = digest_requires_authentication, |
| 797 |
.check_authentication = digest_check_auth, |
| 798 |
}; |
| 799 |
|
| 800 |
static int build_entity_id(void) |
| 801 |
{ |
| 802 |
char *eid; |
| 803 |
|
| 804 |
eid = ao2_alloc(AST_UUID_STR_LEN, NULL); |
| 805 |
if (!eid) { |
| 806 |
return -1; |
| 807 |
} |
| 808 |
|
| 809 |
ast_uuid_generate_str(eid, AST_UUID_STR_LEN); |
| 810 |
ao2_global_obj_replace_unref(entity_id, eid); |
| 811 |
ao2_ref(eid, -1); |
| 812 |
return 0; |
| 813 |
} |
| 814 |
|
| 815 |
static void global_loaded(const char *object_type) |
| 816 |
{ |
| 817 |
ast_sip_get_default_realm(default_realm, sizeof(default_realm)); |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
static struct ast_sorcery_observer global_observer = { |
| 822 |
.loaded = global_loaded, |
| 823 |
}; |
| 824 |
|
| 825 |
static int reload_module(void) |
| 826 |
{ |
| 827 |
if (build_entity_id()) { |
| 828 |
return -1; |
| 829 |
} |
| 830 |
return 0; |
| 831 |
} |
| 832 |
|
| 833 |
static int load_module(void) |
| 834 |
{ |
| 835 |
if (build_entity_id()) { |
| 836 |
return AST_MODULE_LOAD_DECLINE; |
| 837 |
} |
| 838 |
|
| 839 |
ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer); |
| 840 |
ast_sorcery_reload_object(ast_sip_get_sorcery(), "global"); |
| 841 |
|
| 842 |
if (ast_sip_register_authenticator(&digest_authenticator)) { |
| 843 |
ao2_global_obj_release(entity_id); |
| 844 |
return AST_MODULE_LOAD_DECLINE; |
| 845 |
} |
| 846 |
return AST_MODULE_LOAD_SUCCESS; |
| 847 |
} |
| 848 |
|
| 849 |
static int unload_module(void) |
| 850 |
{ |
| 851 |
ast_sorcery_observer_remove(ast_sip_get_sorcery(), "global", &global_observer); |
| 852 |
ast_sip_unregister_authenticator(&digest_authenticator); |
| 853 |
ao2_global_obj_release(entity_id); |
| 854 |
return 0; |
| 855 |
} |
| 856 |
|
| 857 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP authentication resource", |
| 858 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 859 |
.load = load_module, |
| 860 |
.unload = unload_module, |
| 861 |
.reload = reload_module, |
| 862 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 5, |
| 863 |
.requires = "res_pjsip", |
| 864 |
); |