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.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <support_level>core</support_level>
29 #include "asterisk/res_pjsip.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/module.h"
32 #include "asterisk/strings.h"
34 static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_sess, const struct ast_sip_auth_array *array)
36 struct ast_sip_auth **auths = ast_alloca(array->num * sizeof(*auths));
37 pjsip_cred_info *auth_creds = ast_alloca(array->num * sizeof(*auth_creds));
41 if (ast_sip_retrieve_auths(array, auths)) {
46 for (i = 0; i < array->num; ++i) {
47 pj_cstr(&auth_creds[i].realm, auths[i]->realm);
48 pj_cstr(&auth_creds[i].username, auths[i]->auth_user);
49 pj_cstr(&auth_creds[i].scheme, "digest");
50 switch (auths[i]->type) {
51 case AST_SIP_AUTH_TYPE_USER_PASS:
52 pj_cstr(&auth_creds[i].data, auths[i]->auth_pass);
53 auth_creds[i].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
55 case AST_SIP_AUTH_TYPE_MD5:
56 pj_cstr(&auth_creds[i].data, auths[i]->md5_creds);
57 auth_creds[i].data_type = PJSIP_CRED_DATA_DIGEST;
59 case AST_SIP_AUTH_TYPE_ARTIFICIAL:
60 ast_log(LOG_ERROR, "Trying to set artificial outbound auth credentials shouldn't happen.\n");
65 pjsip_auth_clt_set_credentials(auth_sess, array->num, auth_creds);
68 ast_sip_cleanup_auths(auths, array->num);
72 static int digest_create_request_with_auth(const struct ast_sip_auth_array *auths, pjsip_rx_data *challenge,
73 pjsip_transaction *tsx, pjsip_tx_data **new_request)
75 pjsip_auth_clt_sess auth_sess;
77 if (pjsip_auth_clt_init(&auth_sess, ast_sip_get_pjsip_endpoint(),
78 tsx->pool, 0) != PJ_SUCCESS) {
79 ast_log(LOG_WARNING, "Failed to initialize client authentication session\n");
83 if (set_outbound_authentication_credentials(&auth_sess, auths)) {
84 ast_log(LOG_WARNING, "Failed to set authentication credentials\n");
88 switch (pjsip_auth_clt_reinit_req(&auth_sess, challenge,
89 tsx->last_tx, new_request)) {
92 case PJSIP_ENOCREDENTIAL:
93 ast_log(LOG_WARNING, "Unable to create request with auth."
94 "No auth credentials for any realms in challenge.\n");
96 case PJSIP_EAUTHSTALECOUNT:
97 ast_log(LOG_WARNING, "Unable to create request with auth."
98 "Number of stale retries exceeded\n");
100 case PJSIP_EFAILEDCREDENTIAL:
101 ast_log(LOG_WARNING, "Authentication credentials not accepted by server\n");
104 ast_log(LOG_WARNING, "Unable to create request with auth. Unknown failure\n");
111 static struct ast_sip_outbound_authenticator digest_authenticator = {
112 .create_request_with_auth = digest_create_request_with_auth,
115 static int load_module(void)
117 if (ast_sip_register_outbound_authenticator(&digest_authenticator)) {
118 return AST_MODULE_LOAD_DECLINE;
120 return AST_MODULE_LOAD_SUCCESS;
123 static int unload_module(void)
125 ast_sip_unregister_outbound_authenticator(&digest_authenticator);
129 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP authentication resource",
131 .unload = unload_module,
132 .load_pri = AST_MODPRI_CHANNEL_DEPEND,