2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
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.
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.
23 #include "asterisk/res_pjsip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/module.h"
26 #include "asterisk/strings.h"
29 <depend>pjproject</depend>
30 <depend>res_pjsip</depend>
31 <support_level>core</support_level>
34 AO2_GLOBAL_OBJ_STATIC(entity_id);
37 * \brief Determine if authentication is required
39 * Authentication is required if the endpoint has at least one auth
42 static int digest_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
44 RAII_VAR(struct ast_sip_endpoint *, artificial, ast_sip_get_artificial_endpoint(), ao2_cleanup);
46 return endpoint == artificial || AST_VECTOR_SIZE(&endpoint->inbound_auths) > 0;
49 static void auth_store_cleanup(void *data)
51 struct ast_sip_auth **auth = data;
58 * \brief Thread-local storage for \ref ast_sip_auth
60 * The PJSIP authentication API is a bit annoying. When you set
61 * up an authentication server, you specify a lookup callback to
62 * call into when verifying incoming credentials. The problem
63 * with this callback is that it only gives you the realm and
64 * authentication username. In 2.0.5, there is a new version of
65 * the callback you can use that gives the pjsip_rx_data in
68 * Unfortunately, the data we actually \b need is the
69 * \ref ast_sip_auth we are currently observing. So we have two
71 * 1) Use the current PJSIP API and use thread-local storage
72 * to temporarily store our SIP authentication information. Then
73 * in the callback, we can retrieve the authentication info and
74 * use as needed. Given our threading model, this is safe.
75 * 2) Use the 2.0.5 API and temporarily store the authentication
76 * information in the rdata's endpoint_info. Then in the callback,
77 * we can retrieve the authentication info from the rdata.
79 * I've chosen option 1 since it does not require backporting
80 * any APIs from future versions of PJSIP, plus I feel the
81 * thread-local option is a bit cleaner.
83 AST_THREADSTORAGE_CUSTOM(auth_store, NULL, auth_store_cleanup);
86 * \brief Store authentication information in thread-local storage
88 static int store_auth(struct ast_sip_auth *auth)
90 struct ast_sip_auth **pointing;
91 pointing = ast_threadstorage_get(&auth_store, sizeof(pointing));
92 if (!pointing || *pointing) {
102 * \brief Remove authentication information from thread-local storage
104 static int remove_auth(void)
106 struct ast_sip_auth **pointing;
107 pointing = ast_threadstorage_get(&auth_store, sizeof(pointing));
112 ao2_cleanup(*pointing);
118 * \brief Retrieve authentication information from thread-local storage
120 static struct ast_sip_auth *get_auth(void)
122 struct ast_sip_auth **auth;
123 auth = ast_threadstorage_get(&auth_store, sizeof(auth));
132 * \brief Lookup callback for authentication verification
134 * This function is called when we call pjsip_auth_srv_verify(). It
135 * expects us to verify that the realm and account name from the
136 * Authorization header is correct. We are then supposed to supply
137 * a password or MD5 sum of credentials.
139 * \param pool A memory pool we can use for allocations
140 * \param realm The realm from the Authorization header
141 * \param acc_name the user from the Authorization header
142 * \param[out] info The credentials we need to fill in
143 * \retval PJ_SUCCESS Successful authentication
144 * \retval other Unsuccessful
146 static pj_status_t digest_lookup(pj_pool_t *pool, const pj_str_t *realm,
147 const pj_str_t *acc_name, pjsip_cred_info *info)
149 RAII_VAR(struct ast_sip_auth *, auth, get_auth(), ao2_cleanup);
151 return PJSIP_SC_FORBIDDEN;
154 if (auth->type == AST_SIP_AUTH_TYPE_ARTIFICIAL) {
155 return PJSIP_SC_FORBIDDEN;
158 if (pj_strcmp2(realm, auth->realm)) {
159 return PJSIP_SC_FORBIDDEN;
161 if (pj_strcmp2(acc_name, auth->auth_user)) {
162 return PJSIP_SC_FORBIDDEN;
165 pj_strdup2(pool, &info->realm, auth->realm);
166 pj_strdup2(pool, &info->username, auth->auth_user);
168 switch (auth->type) {
169 case AST_SIP_AUTH_TYPE_USER_PASS:
170 pj_strdup2(pool, &info->data, auth->auth_pass);
171 info->data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
173 case AST_SIP_AUTH_TYPE_MD5:
174 pj_strdup2(pool, &info->data, auth->md5_creds);
175 info->data_type = PJSIP_CRED_DATA_DIGEST;
178 return PJSIP_SC_FORBIDDEN;
184 * \brief Calculate a nonce
186 * We use this in order to create authentication challenges. We also use this in order
187 * to verify that an incoming request with credentials could be in response to one
190 * The nonce is calculated from a timestamp, the source IP address, the source port, a
191 * unique ID for us, and the realm. This helps to ensure that the incoming request
192 * is from the same source that the nonce was calculated for. Including the realm
193 * ensures that multiple challenges to the same request have different nonces.
195 * \param A UNIX timestamp expressed as a string
196 * \param rdata The incoming request
197 * \param realm The realm for which authentication should occur
199 static int build_nonce(struct ast_str **nonce, const char *timestamp, const pjsip_rx_data *rdata, const char *realm)
201 struct ast_str *str = ast_str_alloca(256);
202 RAII_VAR(char *, eid, ao2_global_obj_ref(entity_id), ao2_cleanup);
205 ast_str_append(&str, 0, "%s", timestamp);
206 ast_str_append(&str, 0, ":%s", rdata->pkt_info.src_name);
207 ast_str_append(&str, 0, ":%d", rdata->pkt_info.src_port);
208 ast_str_append(&str, 0, ":%s", eid);
209 ast_str_append(&str, 0, ":%s", realm);
210 ast_md5_hash(hash, ast_str_buffer(str));
212 ast_str_append(nonce, 0, "%s/%s", timestamp, hash);
217 * \brief Ensure that a nonce on an incoming request is sane.
219 * The nonce in an incoming Authorization header needs to pass some scrutiny in order
220 * for us to consider accepting it. What we do is re-build a nonce based on request
221 * data and a realm and see if it matches the nonce they sent us.
222 * \param candidate The nonce on an incoming request
223 * \param rdata The incoming request
224 * \param auth The auth credentials we are trying to match against.
225 * \retval 0 Nonce does not pass validity checks
226 * \retval 1 Nonce passes validity check
228 static int check_nonce(const char *candidate, const pjsip_rx_data *rdata, const struct ast_sip_auth *auth)
230 char *copy = ast_strdupa(candidate);
231 char *timestamp = strsep(©, "/");
233 time_t now = time(NULL);
234 struct ast_str *calculated = ast_str_alloca(64);
237 /* Clearly a bad nonce! */
241 if (sscanf(timestamp, "%30d", ×tamp_int) != 1) {
245 if ((int) now - timestamp_int > auth->nonce_lifetime) {
249 build_nonce(&calculated, timestamp, rdata, auth->realm);
250 ast_debug(3, "Calculated nonce %s. Actual nonce is %s\n", ast_str_buffer(calculated), candidate);
251 if (strcmp(ast_str_buffer(calculated), candidate)) {
257 static int find_challenge(const pjsip_rx_data *rdata, const struct ast_sip_auth *auth)
259 struct pjsip_authorization_hdr *auth_hdr = (pjsip_authorization_hdr *) &rdata->msg_info.msg->hdr;
260 int challenge_found = 0;
263 while ((auth_hdr = (pjsip_authorization_hdr *) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, auth_hdr->next))) {
264 ast_copy_pj_str(nonce, &auth_hdr->credential.digest.nonce, sizeof(nonce));
265 if (check_nonce(nonce, rdata, auth) && !pj_strcmp2(&auth_hdr->credential.digest.realm, auth->realm)) {
271 return challenge_found;
275 * \brief Common code for initializing a pjsip_auth_srv
277 static void setup_auth_srv(pj_pool_t *pool, pjsip_auth_srv *auth_server, const char *realm)
280 pj_cstr(&realm_str, realm);
282 pjsip_auth_srv_init(pool, auth_server, &realm_str, digest_lookup, 0);
286 * \brief Result of digest verification
288 enum digest_verify_result {
289 /*! Authentication credentials incorrect */
291 /*! Authentication credentials correct */
293 /*! Authentication credentials correct but nonce mismatch */
295 /*! Authentication credentials were not provided */
300 * \brief astobj2 callback for verifying incoming credentials
302 * \param auth The ast_sip_auth to check against
303 * \param rdata The incoming request
304 * \param pool A pool to use for the auth server
305 * \return CMP_MATCH on successful authentication
306 * \return 0 on failed authentication
308 static int verify(struct ast_sip_auth *auth, pjsip_rx_data *rdata, pj_pool_t *pool)
312 pjsip_auth_srv auth_server;
315 if (!find_challenge(rdata, auth)) {
316 /* Couldn't find a challenge with a sane nonce.
317 * Nonce mismatch may just be due to staleness.
322 setup_auth_srv(pool, &auth_server, auth->realm);
326 authed = pjsip_auth_srv_verify(&auth_server, rdata, &response_code);
330 if (authed == PJ_SUCCESS) {
338 if (authed == PJSIP_EAUTHNOAUTH) {
346 * \brief astobj2 callback for adding digest challenges to responses
348 * \param realm An auth's realm to build a challenge from
349 * \param tdata The response to add the challenge to
350 * \param rdata The request the challenge is in response to
351 * \param is_stale Indicates whether nonce on incoming request was stale
353 static void challenge(const char *realm, pjsip_tx_data *tdata, const pjsip_rx_data *rdata, int is_stale)
357 pjsip_auth_srv auth_server;
358 struct ast_str *nonce = ast_str_alloca(256);
360 time_t timestamp = time(NULL);
361 snprintf(time_buf, sizeof(time_buf), "%d", (int) timestamp);
363 build_nonce(&nonce, time_buf, rdata, realm);
365 setup_auth_srv(tdata->pool, &auth_server, realm);
367 pj_cstr(&pj_nonce, ast_str_buffer(nonce));
368 pj_cstr(&qop, "auth");
369 pjsip_auth_srv_challenge(&auth_server, &qop, &pj_nonce, NULL, is_stale ? PJ_TRUE : PJ_FALSE, tdata);
373 * \brief Check authentication using Digest scheme
375 * This function will check an incoming message against configured authentication
376 * options. If \b any of the incoming Authorization headers result in successful
377 * authentication, then authentication is considered successful.
379 * \see ast_sip_check_authentication
381 static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint *endpoint,
382 pjsip_rx_data *rdata, pjsip_tx_data *tdata)
384 struct ast_sip_auth **auths;
385 enum digest_verify_result *verify_res;
386 enum ast_sip_check_auth_result res;
391 RAII_VAR(struct ast_sip_endpoint *, artificial_endpoint,
392 ast_sip_get_artificial_endpoint(), ao2_cleanup);
394 auth_size = AST_VECTOR_SIZE(&endpoint->inbound_auths);
396 auths = ast_alloca(auth_size * sizeof(*auths));
397 verify_res = ast_alloca(auth_size * sizeof(*verify_res));
400 return AST_SIP_AUTHENTICATION_ERROR;
403 if (endpoint == artificial_endpoint) {
404 auths[0] = ast_sip_get_artificial_auth();
405 } else if (ast_sip_retrieve_auths(&endpoint->inbound_auths, auths)) {
406 res = AST_SIP_AUTHENTICATION_ERROR;
410 for (i = 0; i < auth_size; ++i) {
411 if (ast_strlen_zero(auths[i]->realm)) {
412 ast_string_field_set(auths[i], realm, "asterisk");
414 verify_res[i] = verify(auths[i], rdata, tdata->pool);
415 if (verify_res[i] == AUTH_SUCCESS) {
416 res = AST_SIP_AUTHENTICATION_SUCCESS;
419 if (verify_res[i] == AUTH_FAIL) {
424 for (i = 0; i < auth_size; ++i) {
425 challenge(auths[i]->realm, tdata, rdata, verify_res[i] == AUTH_STALE);
428 if (failures == auth_size) {
429 res = AST_SIP_AUTHENTICATION_FAILED;
431 res = AST_SIP_AUTHENTICATION_CHALLENGE;
435 ast_sip_cleanup_auths(auths, auth_size);
439 static struct ast_sip_authenticator digest_authenticator = {
440 .requires_authentication = digest_requires_authentication,
441 .check_authentication = digest_check_auth,
444 static int build_entity_id(void)
446 RAII_VAR(struct ast_uuid *, uu, ast_uuid_generate(), ast_free_ptr);
447 RAII_VAR(char *, eid, ao2_alloc(AST_UUID_STR_LEN, NULL), ao2_cleanup);
453 ast_uuid_to_str(uu, eid, AST_UUID_STR_LEN);
454 ao2_global_obj_replace_unref(entity_id, eid);
458 static int reload_module(void)
460 if (build_entity_id()) {
466 static int load_module(void)
468 if (build_entity_id()) {
469 return AST_MODULE_LOAD_DECLINE;
471 if (ast_sip_register_authenticator(&digest_authenticator)) {
472 ao2_global_obj_release(entity_id);
473 return AST_MODULE_LOAD_DECLINE;
475 return AST_MODULE_LOAD_SUCCESS;
478 static int unload_module(void)
480 ast_sip_unregister_authenticator(&digest_authenticator);
481 ao2_global_obj_release(entity_id);
485 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP authentication resource",
487 .unload = unload_module,
488 .reload = reload_module,
489 .load_pri = AST_MODPRI_CHANNEL_DEPEND,