Merge the pimp_my_sip branch into trunk.
[asterisk/asterisk.git] / res / res_sip_outbound_authenticator_digest.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
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.
13  *
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.
17  */
18
19 #include "asterisk.h"
20
21 #include <pjsip.h>
22
23 #include "asterisk/res_sip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/module.h"
26 #include "asterisk/strings.h"
27
28 static int set_outbound_authentication_credentials(pjsip_auth_clt_sess *auth_sess, const char **auth_strs, size_t num_auths)
29 {
30         struct ast_sip_auth **auths = ast_alloca(num_auths * sizeof(*auths));
31         pjsip_cred_info *auth_creds = ast_alloca(num_auths * sizeof(*auth_creds));
32         int res = 0;
33         int i;
34
35         if (ast_sip_retrieve_auths(auth_strs, num_auths, auths)) {
36                 res = -1;
37                 goto cleanup;
38         }
39
40         for (i = 0; i < num_auths; ++i) {
41                 pj_cstr(&auth_creds[i].realm, auths[i]->realm);
42                 pj_cstr(&auth_creds[i].username, auths[i]->auth_user);
43                 pj_cstr(&auth_creds[i].scheme, "digest");
44                 switch (auths[i]->type) {
45                 case AST_SIP_AUTH_TYPE_USER_PASS:
46                         pj_cstr(&auth_creds[i].data, auths[i]->auth_pass);
47                         auth_creds[i].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
48                         break;
49                 case AST_SIP_AUTH_TYPE_MD5:
50                         pj_cstr(&auth_creds[i].data, auths[i]->md5_creds);
51                         auth_creds[i].data_type = PJSIP_CRED_DATA_DIGEST;
52                         break;
53                 }
54         }
55
56         pjsip_auth_clt_set_credentials(auth_sess, num_auths, auth_creds);
57
58 cleanup:
59         ast_sip_cleanup_auths(auths, num_auths);
60         return res;
61 }
62
63 static int digest_create_request_with_auth(const char **auths, size_t num_auths, pjsip_rx_data *challenge,
64                 pjsip_transaction *tsx, pjsip_tx_data **new_request)
65 {
66         pjsip_auth_clt_sess auth_sess;
67
68         if (pjsip_auth_clt_init(&auth_sess, ast_sip_get_pjsip_endpoint(),
69                                 tsx->pool, 0) != PJ_SUCCESS) {
70                 ast_log(LOG_WARNING, "Failed to initialize client authentication session\n");
71                 return -1;
72         }
73
74         if (set_outbound_authentication_credentials(&auth_sess, auths, num_auths)) {
75                 ast_log(LOG_WARNING, "Failed to set authentication credentials\n");
76                 return -1;
77         }
78
79         if (pjsip_auth_clt_reinit_req(&auth_sess, challenge,
80                                 tsx->last_tx, new_request) != PJ_SUCCESS) {
81                 ast_log(LOG_WARNING, "Failed to create new request with authentication credentials\n");
82                 return -1;
83         }
84
85         return 0;
86 }
87
88 static struct ast_sip_outbound_authenticator digest_authenticator = {
89         .create_request_with_auth = digest_create_request_with_auth,
90 };
91
92 static int load_module(void)
93 {
94         if (ast_sip_register_outbound_authenticator(&digest_authenticator)) {
95                 return AST_MODULE_LOAD_DECLINE;
96         }
97         return AST_MODULE_LOAD_SUCCESS;
98 }
99
100 static int unload_module(void)
101 {
102         ast_sip_unregister_outbound_authenticator(&digest_authenticator);
103         return 0;
104 }
105
106 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP authentication resource",
107                 .load = load_module,
108                 .unload = unload_module,
109                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
110 );