Asterisk - The Open Source Telephony Project GIT-master-8f1982c
|
PJSIP UAS Authentication. More...
#include "asterisk.h"
#include <pjsip.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/logger.h"
#include "asterisk/module.h"
#include "asterisk/strings.h"
#include "asterisk/test.h"
Go to the source code of this file.
Enumerations | |
enum | digest_verify_result { AUTH_FAIL = 0 , AUTH_SUCCESS , AUTH_STALE , AUTH_NOAUTH } |
Result of digest verification. More... | |
Functions | |
static void | __init_auth_store (void) |
Thread-local storage for ast_sip_auth. More... | |
static void | __reg_module (void) |
static void | __unreg_module (void) |
AO2_GLOBAL_OBJ_STATIC (entity_id) | |
struct ast_module * | AST_MODULE_SELF_SYM (void) |
static void | auth_store_cleanup (void *data) |
static int | build_entity_id (void) |
static int | build_nonce (struct ast_str **nonce, const char *timestamp, const pjsip_rx_data *rdata, const char *realm) |
Calculate a nonce. More... | |
static void | challenge (const char *endpoint_id, struct ast_sip_auth *auth, pjsip_tx_data *tdata, const pjsip_rx_data *rdata, int is_stale, const pjsip_auth_algorithm *algorithm) |
Send a WWW-Authenticate challenge. More... | |
static int | check_nonce (const char *candidate, const pjsip_rx_data *rdata, const struct ast_sip_auth *auth) |
Ensure that a nonce on an incoming request is sane. More... | |
static enum ast_sip_check_auth_result | digest_check_auth (struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata) |
Check authentication using Digest scheme. More... | |
static pj_status_t | digest_lookup (pj_pool_t *pool, const pjsip_auth_lookup_cred_param *param, pjsip_cred_info *cred_info) |
Lookup callback for authentication verification. More... | |
static int | digest_requires_authentication (struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) |
Determine if authentication is required. More... | |
static enum digest_verify_result | find_authorization (const char *endpoint_id, const struct ast_sip_auth *auth, const pjsip_rx_data *rdata) |
static const struct ast_sip_auth * | get_auth (void) |
Retrieve shallow copy authentication information from thread-local storage. More... | |
static struct pjsip_authorization_hdr * | get_authorization_hdr (const char *auth_id, const char *realm, const pjsip_rx_data *rdata) |
static void | global_loaded (const char *object_type) |
static int | load_module (void) |
static int | reload_module (void) |
static int | remove_auth (void) |
Remove shallow copy authentication information from thread-local storage. More... | |
static void | setup_auth_srv (pj_pool_t *pool, pjsip_auth_srv *auth_server, const char *realm) |
Common code for initializing a pjsip_auth_srv. More... | |
static int | store_auth (const struct ast_sip_auth *auth) |
Store shallow copy authentication information in thread-local storage. More... | |
static int | unload_module (void) |
static int | verify (const char *endpoint_id, const struct ast_sip_auth *auth, pjsip_rx_data *rdata, pj_pool_t *pool) |
Verify incoming credentials. More... | |
Variables | |
static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP authentication resource" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND - 5, .requires = "res_pjsip", } |
static const struct ast_module_info * | ast_module_info = &__mod_info |
static struct ast_threadstorage | auth_store = { .once = PTHREAD_ONCE_INIT , .key_init = __init_auth_store , .custom_init = NULL , } |
static char * | check_auth_result_str [] |
static char | default_realm [AST_SIP_AUTH_MAX_REALM_LENGTH+1] |
static struct ast_sip_authenticator | digest_authenticator |
static struct ast_sorcery_observer | global_observer |
Observer which is used to update our default_realm when the global setting changes. More... | |
static char * | verify_result_str [] |
PJSIP UAS Authentication.
This module handles authentication when Asterisk is the UAS.
Definition in file res_pjsip_authenticator_digest.c.
enum digest_verify_result |
Result of digest verification.
Definition at line 352 of file res_pjsip_authenticator_digest.c.
|
static |
Thread-local storage for ast_sip_auth.
The PJSIP authentication API is a bit annoying. When you set up an authentication server, you specify a lookup callback to call into when verifying incoming credentials. The problem with this callback is that it only gives you the realm and authentication username. In 2.0.5, there is a new version of the callback you can use that gives the pjsip_rx_data in addition.
Unfortunately, the data we actually need is the ast_sip_auth we are currently observing. So we have two choices: 1) Use the current PJSIP API and use thread-local storage to temporarily store our SIP authentication information. Then in the callback, we can retrieve the authentication info and use as needed. Given our threading model, this is safe. 2) Use the 2.0.5 API and temporarily store the authentication information in the rdata's endpoint_info. Then in the callback, we can retrieve the authentication info from the rdata.
I've chosen option 1 since it does not require backporting any APIs from future versions of PJSIP, plus I feel the thread-local option is a bit cleaner.
Definition at line 94 of file res_pjsip_authenticator_digest.c.
|
static |
Definition at line 839 of file res_pjsip_authenticator_digest.c.
|
static |
Definition at line 839 of file res_pjsip_authenticator_digest.c.
AO2_GLOBAL_OBJ_STATIC | ( | entity_id | ) |
struct ast_module * AST_MODULE_SELF_SYM | ( | void | ) |
Definition at line 839 of file res_pjsip_authenticator_digest.c.
|
static |
Definition at line 60 of file res_pjsip_authenticator_digest.c.
References ao2_cleanup, and ast_free.
|
static |
Definition at line 775 of file res_pjsip_authenticator_digest.c.
References ao2_alloc, ao2_global_obj_replace_unref, ao2_ref, ast_uuid_generate_str(), AST_UUID_STR_LEN, and NULL.
Referenced by load_module(), and reload_module().
|
static |
Calculate a nonce.
We use this in order to create authentication challenges. We also use this in order to verify that an incoming request with credentials could be in response to one of our challenges.
The nonce is calculated from a timestamp, the source IP address, the source port, a unique ID for us, and the realm. This helps to ensure that the incoming request is from the same source that the nonce was calculated for. Including the realm ensures that multiple challenges to the same request have different nonces.
nonce | |
timestamp | A UNIX timestamp expressed as a string |
rdata | The incoming request |
realm | The realm for which authentication should occur |
Definition at line 287 of file res_pjsip_authenticator_digest.c.
References ao2_cleanup, ao2_global_obj_ref, ast_md5_hash(), ast_str_alloca, ast_str_append(), ast_str_buffer(), RAII_VAR, and str.
Referenced by challenge(), and check_nonce().
|
static |
Send a WWW-Authenticate challenge.
endpoint_id | For logging |
auth | The auth object to use for the challenge |
tdata | The response to add the challenge to |
rdata | The request the challenge is in response to |
is_stale | Indicates whether nonce on incoming request was stale |
algorithm_type | The algorithm to use for the challenge |
Definition at line 507 of file res_pjsip_authenticator_digest.c.
References pjsip_auth_algorithm::algorithm_type, ast_sorcery_object_get_id(), ast_str_alloca, ast_str_buffer(), build_nonce(), default_realm, pjsip_auth_algorithm::iana_name, NULL, PJSTR_PRINTF_SPEC, PJSTR_PRINTF_VAR, ast_sip_auth::realm, S_OR, SCOPE_ENTER, SCOPE_EXIT_RTN, and setup_auth_srv().
Referenced by ast_sip_create_request_with_auth(), authenticate(), authenticate_reply(), authenticate_request(), decrypt_frame(), digest_check_auth(), digest_create_request_with_auth(), get_auth_search_type(), manager_login(), register_verify(), registry_authrequest(), registry_rerequest(), send_request_cb(), and set_auth_creds().
|
static |
Ensure that a nonce on an incoming request is sane.
The nonce in an incoming Authorization header needs to pass some scrutiny in order for us to consider accepting it. What we do is re-build a nonce based on request data and a realm and see if it matches the nonce they sent us.
candidate | The nonce on an incoming request |
rdata | The incoming request |
auth | The auth credentials we are trying to match against. |
0 | Nonce does not pass validity checks |
1 | Nonce passes validity check |
Definition at line 320 of file res_pjsip_authenticator_digest.c.
References ast_debug, ast_str_alloca, ast_str_buffer(), ast_strdupa, build_nonce(), copy(), default_realm, ast_sip_auth::nonce_lifetime, NULL, ast_sip_auth::realm, S_OR, and strsep().
Referenced by find_authorization().
|
static |
Check authentication using Digest scheme.
This function will check an incoming message against configured authentication options. If any of the incoming Authorization headers result in successful authentication, then authentication is considered successful.
AST_SIP_AUTHENTICATION_SUCCESS | There was an Authorization header in the request and it verified successfully with at least one auth object on the endpoint. No further challenges sent. |
AST_SIP_AUTHENTICATION_CHALLENGE | There was NO Authorization header in the incoming request. We sent a 401 with one or more challenges. |
AST_SIP_AUTHENTICATION_FAILED | There were one or more Authorization headers in the request but they all failed to verify with any auth object on the endpoint. We sent a 401 with one or more challenges. |
AST_SIP_AUTHENTICATION_ERROR | An internal error occurred. No challenges were sent. |
Definition at line 583 of file res_pjsip_authenticator_digest.c.
References ao2_ref, artificial_endpoint, ast_alloca, ast_assert, ast_sip_auth_get_algorithm_by_type(), AST_SIP_AUTHENTICATION_CHALLENGE, AST_SIP_AUTHENTICATION_ERROR, AST_SIP_AUTHENTICATION_FAILED, AST_SIP_AUTHENTICATION_SUCCESS, ast_sip_cleanup_auths(), ast_sip_get_artificial_auth(), ast_sip_get_artificial_endpoint(), ast_sip_retrieve_auths(), ast_sorcery_object_get_id(), ast_trace, AST_VECTOR_GET, AST_VECTOR_SIZE, AUTH_FAIL, AUTH_NOAUTH, AUTH_STALE, AUTH_SUCCESS, challenge(), check_auth_result_str, default_realm, pjsip_auth_algorithm::iana_name, ast_sip_endpoint::inbound_auths, NULL, PJSTR_PRINTF_SPEC, PJSTR_PRINTF_VAR, ast_sip_auth::realm, S_OR, SCOPE_CALL, SCOPE_CALL_WITH_RESULT, SCOPE_ENTER, SCOPE_EXIT, SCOPE_EXIT_EXPR, SCOPE_EXIT_RTN_VALUE, ast_sip_auth::supported_algorithms_uas, verify(), and verify_result_str.
|
static |
Lookup callback for authentication verification.
This function is called when we call pjsip_auth_srv_verify(). It expects us to verify that the realm and account name from the Authorization header are correct and that we can support the digest algorithm specified. We are then supposed to supply a password or password_digest for the algorithm.
The auth object must have previously been saved to thread-local storage.
pool | A memory pool we can use for allocations |
param | Contains the realm, username, rdata and auth header |
cred_info | The credentials we need to fill in |
PJ_SUCCESS | Successful authentication |
other | Unsuccessful |
Definition at line 178 of file res_pjsip_authenticator_digest.c.
References pjsip_auth_algorithm::algorithm_type, ast_sip_auth_get_algorithm_by_iana_name(), ast_sip_auth_get_creds(), ast_sip_auth_is_algorithm_available(), AST_SIP_AUTH_TYPE_ARTIFICIAL, ast_sorcery_object_get_id(), ast_sip_auth::auth_user, default_realm, get_auth(), get_authorization_hdr(), pjsip_auth_algorithm::iana_name, PJSTR_PRINTF_SPEC, PJSTR_PRINTF_VAR, ast_sip_auth::realm, S_OR, SCOPE_ENTER, SCOPE_EXIT_RTN_VALUE, ast_sip_auth::supported_algorithms_uas, and ast_sip_auth::type.
Referenced by setup_auth_srv().
|
static |
Determine if authentication is required.
Authentication is required if the endpoint has at least one auth section specified
Definition at line 53 of file res_pjsip_authenticator_digest.c.
References ao2_cleanup, ast_sip_get_artificial_endpoint(), AST_VECTOR_SIZE, ast_sip_endpoint::inbound_auths, and RAII_VAR.
|
static |
Definition at line 370 of file res_pjsip_authenticator_digest.c.
References ast_copy_pj_str(), ast_sorcery_object_get_id(), ast_trace, AUTH_NOAUTH, AUTH_STALE, AUTH_SUCCESS, check_nonce(), default_realm, NULL, PJSTR_PRINTF_SPEC, PJSTR_PRINTF_VAR, ast_sip_auth::realm, S_OR, SCOPE_ENTER, SCOPE_EXIT_RTN_VALUE, and verify_result_str.
Referenced by verify().
|
static |
Retrieve shallow copy authentication information from thread-local storage.
Definition at line 131 of file res_pjsip_authenticator_digest.c.
References ast_threadstorage_get(), auth_store, and NULL.
Referenced by digest_lookup().
|
static |
Definition at line 142 of file res_pjsip_authenticator_digest.c.
References NULL, SCOPE_ENTER, and SCOPE_EXIT_RTN_VALUE.
Referenced by digest_lookup().
|
static |
Definition at line 790 of file res_pjsip_authenticator_digest.c.
References ast_sip_get_default_realm(), and default_realm.
|
static |
Definition at line 808 of file res_pjsip_authenticator_digest.c.
References ao2_global_obj_release, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_sip_get_sorcery(), ast_sip_register_authenticator(), ast_sorcery_observer_add(), ast_sorcery_reload_object(), build_entity_id(), digest_authenticator, and global_observer.
|
static |
Definition at line 800 of file res_pjsip_authenticator_digest.c.
References build_entity_id().
|
static |
Remove shallow copy authentication information from thread-local storage.
Definition at line 115 of file res_pjsip_authenticator_digest.c.
References ast_threadstorage_get(), auth_store, and NULL.
Referenced by verify().
|
static |
Common code for initializing a pjsip_auth_srv.
Definition at line 413 of file res_pjsip_authenticator_digest.c.
References digest_lookup().
Referenced by challenge(), and verify().
|
static |
Store shallow copy authentication information in thread-local storage.
Definition at line 99 of file res_pjsip_authenticator_digest.c.
References ast_threadstorage_get(), and auth_store.
Referenced by verify().
|
static |
Definition at line 824 of file res_pjsip_authenticator_digest.c.
References ao2_global_obj_release, ast_sip_get_sorcery(), ast_sip_unregister_authenticator(), ast_sorcery_observer_remove(), digest_authenticator, and global_observer.
|
static |
Verify incoming credentials.
endpoint_id | For logging |
auth | The ast_sip_auth to check against |
rdata | The incoming request |
pool | A pool to use for the auth server |
Definition at line 435 of file res_pjsip_authenticator_digest.c.
References ast_sorcery_object_get_id(), ast_test_suite_event_notify, ast_trace, AUTH_FAIL, AUTH_NOAUTH, AUTH_STALE, AUTH_SUCCESS, ast_sip_auth::auth_user, default_realm, find_authorization(), ast_sip_auth::realm, remove_auth(), S_OR, SCOPE_CALL_WITH_RESULT, SCOPE_ENTER, SCOPE_EXIT_RTN_VALUE, setup_auth_srv(), store_auth(), and verify_result_str.
Referenced by digest_check_auth().
|
static |
Definition at line 839 of file res_pjsip_authenticator_digest.c.
|
static |
Definition at line 839 of file res_pjsip_authenticator_digest.c.
|
static |
Definition at line 94 of file res_pjsip_authenticator_digest.c.
Referenced by get_auth(), remove_auth(), and store_auth().
|
static |
Definition at line 546 of file res_pjsip_authenticator_digest.c.
Referenced by digest_check_auth().
|
static |
Definition at line 43 of file res_pjsip_authenticator_digest.c.
Referenced by alloc_artificial_auth(), ast_sip_initialize_sorcery_global(), challenge(), check_nonce(), create_artificial_auth(), digest_check_auth(), digest_lookup(), find_authorization(), global_loaded(), and verify().
|
static |
Definition at line 770 of file res_pjsip_authenticator_digest.c.
Referenced by load_module(), and unload_module().
|
static |
Observer which is used to update our default_realm when the global setting changes.
Definition at line 796 of file res_pjsip_authenticator_digest.c.
Referenced by load_module(), and unload_module().
|
static |
Definition at line 363 of file res_pjsip_authenticator_digest.c.
Referenced by digest_check_auth(), find_authorization(), and verify().